1 /*
2 * Copyright (c) 2022-2024 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_TAG "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(const InputerGetDataParam & getDataParam)42 void OnGetData(const InputerGetDataParam &getDataParam)
43 {
44 static_cast<void>(getDataParam);
45 }
AsObject()46 sptr<IRemoteObject> AsObject()
47 {
48 sptr<IRemoteObject> obj(nullptr);
49 return obj;
50 }
51 };
52
53 auto g_service = PinAuthService::GetInstance();
54
FuzzRegisterInputer(Parcel & parcel)55 void FuzzRegisterInputer(Parcel &parcel)
56 {
57 IAM_LOGI("begin");
58 sptr<InputerGetData> remoteInputer(nullptr);
59 if (parcel.ReadBool()) {
60 remoteInputer = sptr<InputerGetData>(new (std::nothrow) DummyRemoteInputer());
61 }
62 if (g_service != nullptr) {
63 g_service->RegisterInputer(remoteInputer);
64 }
65 IAM_LOGI("end");
66 }
67
FuzzUnRegisterInputer(Parcel & parcel)68 void FuzzUnRegisterInputer(Parcel &parcel)
69 {
70 IAM_LOGI("begin");
71 if (g_service != nullptr) {
72 g_service->UnRegisterInputer();
73 }
74 IAM_LOGI("end");
75 }
76
FuzzCheckPermission(Parcel & parcel)77 void FuzzCheckPermission(Parcel &parcel)
78 {
79 IAM_LOGI("begin");
80 string permission;
81 FillFuzzString(parcel, permission);
82 if (g_service != nullptr) {
83 g_service->CheckPermission(permission);
84 }
85 IAM_LOGI("end");
86 }
87
FuzzStartDriverManager(Parcel & parcel)88 void FuzzStartDriverManager(Parcel &parcel)
89 {
90 IAM_LOGI("begin");
91 static_cast<void>(parcel);
92 if (g_service != nullptr) {
93 g_service->StartDriverManager();
94 }
95 IAM_LOGI("end");
96 }
97
FuzzOnStop(Parcel & parcel)98 void FuzzOnStop(Parcel &parcel)
99 {
100 IAM_LOGI("begin");
101 static_cast<void>(parcel);
102 if (g_service != nullptr) {
103 g_service->OnStop();
104 }
105 IAM_LOGI("end");
106 }
107
108 using FuzzFunc = decltype(FuzzRegisterInputer);
109 FuzzFunc *g_fuzzFuncs[] = {FuzzRegisterInputer, FuzzUnRegisterInputer, FuzzCheckPermission, FuzzStartDriverManager,
110 FuzzOnStop};
111
PinAuthServiceFuzzTest(const uint8_t * data,size_t size)112 void PinAuthServiceFuzzTest(const uint8_t *data, size_t size)
113 {
114 Parcel parcel;
115 parcel.WriteBuffer(data, size);
116 parcel.RewindRead(0);
117 uint32_t index = parcel.ReadUint32() % (sizeof(g_fuzzFuncs) / sizeof(FuzzFunc *));
118 auto fuzzFunc = g_fuzzFuncs[index];
119 fuzzFunc(parcel);
120 return;
121 }
122 } // namespace
123 } // namespace PinAuth
124 } // namespace UserIam
125 } // namespace OHOS
126
127 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)128 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
129 {
130 OHOS::UserIam::PinAuth::PinAuthServiceFuzzTest(data, size);
131 return 0;
132 }
133