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