1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "pin_auth_service_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "inputer_get_data.h"
22 #include "parcel.h"
23
24 #include "iam_fuzz_test.h"
25 #include "iam_logger.h"
26
27 #include "pin_auth_service.h"
28
29 #define LOG_LABEL UserIam::Common::LABEL_PIN_AUTH_SA
30
31 #undef private
32
33 using namespace std;
34 using namespace OHOS::UserIam::Common;
35
36 namespace OHOS {
37 namespace UserIam {
38 namespace PinAuth {
39 namespace {
40 class DummyRemoteInputer : public InputerGetData {
41 public:
OnGetData(int32_t authSubType,const std::vector<uint8_t> & algoParameter,const sptr<InputerSetData> & inputerSetData,uint32_t algoVersion,bool isEnroll)42 void OnGetData(int32_t authSubType, const std::vector<uint8_t> &algoParameter,
43 const sptr<InputerSetData> &inputerSetData, uint32_t algoVersion, bool isEnroll)
44 {
45 static_cast<void>(authSubType);
46 static_cast<void>(algoParameter);
47 static_cast<void>(inputerSetData);
48 static_cast<void>(algoVersion);
49 static_cast<void>(isEnroll);
50 }
AsObject()51 sptr<IRemoteObject> AsObject()
52 {
53 sptr<IRemoteObject> obj(nullptr);
54 return obj;
55 }
56 };
57
58 auto g_service = PinAuthService::GetInstance();
59
FuzzRegisterInputer(Parcel & parcel)60 void FuzzRegisterInputer(Parcel &parcel)
61 {
62 IAM_LOGI("begin");
63 sptr<InputerGetData> remoteInputer(nullptr);
64 if (parcel.ReadBool()) {
65 remoteInputer = sptr<InputerGetData>(new (std::nothrow) DummyRemoteInputer());
66 }
67 if (g_service != nullptr) {
68 g_service->RegisterInputer(remoteInputer);
69 }
70 IAM_LOGI("end");
71 }
72
FuzzUnRegisterInputer(Parcel & parcel)73 void FuzzUnRegisterInputer(Parcel &parcel)
74 {
75 IAM_LOGI("begin");
76 if (g_service != nullptr) {
77 g_service->UnRegisterInputer();
78 }
79 IAM_LOGI("end");
80 }
81
FuzzCheckPermission(Parcel & parcel)82 void FuzzCheckPermission(Parcel &parcel)
83 {
84 IAM_LOGI("begin");
85 string permission;
86 FillFuzzString(parcel, permission);
87 if (g_service != nullptr) {
88 g_service->CheckPermission(permission);
89 }
90 IAM_LOGI("end");
91 }
92
93 using FuzzFunc = decltype(FuzzRegisterInputer);
94 FuzzFunc *g_fuzzFuncs[] = {FuzzRegisterInputer, FuzzUnRegisterInputer, FuzzCheckPermission};
95
PinAuthServiceFuzzTest(const uint8_t * data,size_t size)96 void PinAuthServiceFuzzTest(const uint8_t *data, size_t size)
97 {
98 Parcel parcel;
99 parcel.WriteBuffer(data, size);
100 parcel.RewindRead(0);
101 uint32_t index = parcel.ReadUint32() % (sizeof(g_fuzzFuncs) / sizeof(FuzzFunc *));
102 auto fuzzFunc = g_fuzzFuncs[index];
103 fuzzFunc(parcel);
104 return;
105 }
106 } // namespace
107 } // namespace PinAuth
108 } // namespace UserIam
109 } // namespace OHOS
110
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
113 {
114 OHOS::UserIam::PinAuth::PinAuthServiceFuzzTest(data, size);
115 return 0;
116 }
117