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_manager_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "parcel.h"
22 
23 #include "iam_common_defines.h"
24 #include "iam_fuzz_test.h"
25 #include "iam_logger.h"
26 #include "iam_ptr.h"
27 
28 #include "mock_inputer_get_data_fuzzer.h"
29 
30 #include "pin_auth_manager.h"
31 
32 #define LOG_TAG "PIN_AUTH_SA"
33 
34 #undef private
35 
36 using namespace std;
37 using namespace OHOS::UserIam::Common;
38 
39 namespace OHOS {
40 namespace UserIam {
41 namespace PinAuth {
42 namespace {
43 
FuzzRegisterInputer(Parcel & parcel)44 void FuzzRegisterInputer(Parcel &parcel)
45 {
46     IAM_LOGI("begin");
47     uint32_t tokenId = parcel.ReadUint32();
48     sptr<InputerGetData> inputer(nullptr);
49     if (parcel.ReadBool()) {
50         inputer = sptr<InputerGetData>(new (std::nothrow) MockInputerGetDataFuzzer());
51     }
52     PinAuthManager::GetInstance().RegisterInputer(tokenId, inputer);
53     PinAuthManager::GetInstance().RegisterInputer(tokenId, inputer);
54 
55     PinAuthManager::GetInstance().UnRegisterInputer(tokenId);
56     IAM_LOGI("end");
57 }
58 
FuzzUnRegisterInputer(Parcel & parcel)59 void FuzzUnRegisterInputer(Parcel &parcel)
60 {
61     IAM_LOGI("begin");
62     uint32_t tokenId = parcel.ReadUint32();
63     sptr<InputerGetData> inputer(nullptr);
64     if (parcel.ReadBool()) {
65         inputer = sptr<InputerGetData>(new (std::nothrow) MockInputerGetDataFuzzer());
66     }
67     PinAuthManager::GetInstance().UnRegisterInputer(tokenId);
68     PinAuthManager::GetInstance().RegisterInputer(tokenId, inputer);
69 
70     PinAuthManager::GetInstance().UnRegisterInputer(tokenId);
71     IAM_LOGI("end");
72 }
73 
FuzzGetInputerLock(Parcel & parcel)74 void FuzzGetInputerLock(Parcel &parcel)
75 {
76     IAM_LOGI("begin");
77     uint32_t tokenId = parcel.ReadUint32();
78     auto inputer = sptr<InputerGetData>(new (std::nothrow) MockInputerGetDataFuzzer());
79     PinAuthManager::GetInstance().RegisterInputer(tokenId, inputer);
80     PinAuthManager::GetInstance().GetInputerLock(tokenId);
81     PinAuthManager::GetInstance().UnRegisterInputer(tokenId);
82     IAM_LOGI("end");
83 }
84 
85 using FuzzFunc = decltype(FuzzRegisterInputer);
86 FuzzFunc *g_fuzzFuncs[] = {FuzzRegisterInputer, FuzzUnRegisterInputer, FuzzGetInputerLock};
87 
PinAuthManagerFuzzTest(const uint8_t * data,size_t size)88 void PinAuthManagerFuzzTest(const uint8_t *data, size_t size)
89 {
90     Parcel parcel;
91     parcel.WriteBuffer(data, size);
92     parcel.RewindRead(0);
93     uint32_t index = parcel.ReadUint32() % (sizeof(g_fuzzFuncs) / sizeof(FuzzFunc *));
94     auto fuzzFunc = g_fuzzFuncs[index];
95     fuzzFunc(parcel);
96     return;
97 }
98 } // namespace
99 } // namespace PinAuth
100 } // namespace UserIam
101 } // namespace OHOS
102 
103 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)104 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
105 {
106     OHOS::UserIam::PinAuth::PinAuthManagerFuzzTest(data, size);
107     return 0;
108 }
109