1 /*
2  * Copyright (c) 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_driver_hdi_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_pin_auth_interface_adapter_fuzzer.h"
29 
30 #include "pin_auth_driver_hdi.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 
44 std::shared_ptr<PinAuthDriverHdi> pinAuthDriverHdi_(nullptr);
45 
InitPinAuthDriverHdi(Parcel & parcel)46 void InitPinAuthDriverHdi(Parcel &parcel)
47 {
48     static_cast<void>(parcel);
49     std::shared_ptr<PinAuthInterfaceAdapter> adapter = Common::MakeShared<MockPinAuthInterfaceAdapterFuzzer>();
50     pinAuthDriverHdi_ = Common::MakeShared<PinAuthDriverHdi>(adapter);
51     IAM_LOGI("end");
52 }
53 
FuzzGetExecutorList(Parcel & parcel)54 void FuzzGetExecutorList(Parcel &parcel)
55 {
56     IAM_LOGI("begin");
57     static_cast<void>(parcel);
58     std::vector<std::shared_ptr<UserAuth::IAuthExecutorHdi>> executorList;
59     if (pinAuthDriverHdi_ != nullptr) {
60         pinAuthDriverHdi_->GetExecutorList(executorList);
61     }
62     IAM_LOGI("end");
63 }
64 
FuzzOnHdiDisconnect(Parcel & parcel)65 void FuzzOnHdiDisconnect(Parcel &parcel)
66 {
67     IAM_LOGI("begin");
68     static_cast<void>(parcel);
69     if (pinAuthDriverHdi_ != nullptr) {
70         pinAuthDriverHdi_->OnHdiDisconnect();
71     }
72     IAM_LOGI("end");
73 }
74 
75 using FuzzFunc = decltype(FuzzGetExecutorList);
76 FuzzFunc *g_fuzzFuncs[] = {FuzzGetExecutorList, FuzzOnHdiDisconnect};
77 
PinAuthDriverHdiFuzzTest(const uint8_t * data,size_t size)78 void PinAuthDriverHdiFuzzTest(const uint8_t *data, size_t size)
79 {
80     Parcel parcel;
81     parcel.WriteBuffer(data, size);
82     parcel.RewindRead(0);
83     InitPinAuthDriverHdi(parcel);
84     uint32_t index = parcel.ReadUint32() % (sizeof(g_fuzzFuncs) / sizeof(FuzzFunc *));
85     auto fuzzFunc = g_fuzzFuncs[index];
86     fuzzFunc(parcel);
87     return;
88 }
89 } // namespace
90 } // namespace PinAuth
91 } // namespace UserIam
92 } // namespace OHOS
93 
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
96 {
97     OHOS::UserIam::PinAuth::PinAuthDriverHdiFuzzTest(data, size);
98     return 0;
99 }
100