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 
88 using FuzzFunc = decltype(FuzzRegisterInputer);
89 FuzzFunc *g_fuzzFuncs[] = {FuzzRegisterInputer, FuzzUnRegisterInputer, FuzzCheckPermission};
90 
PinAuthServiceFuzzTest(const uint8_t * data,size_t size)91 void PinAuthServiceFuzzTest(const uint8_t *data, size_t size)
92 {
93     Parcel parcel;
94     parcel.WriteBuffer(data, size);
95     parcel.RewindRead(0);
96     uint32_t index = parcel.ReadUint32() % (sizeof(g_fuzzFuncs) / sizeof(FuzzFunc *));
97     auto fuzzFunc = g_fuzzFuncs[index];
98     fuzzFunc(parcel);
99     return;
100 }
101 } // namespace
102 } // namespace PinAuth
103 } // namespace UserIam
104 } // namespace OHOS
105 
106 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)107 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
108 {
109     OHOS::UserIam::PinAuth::PinAuthServiceFuzzTest(data, size);
110     return 0;
111 }
112