1 /*
2  * Copyright (c) 2023-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 "inputer_data_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "parcel.h"
22 
23 #include "iam_fuzz_test.h"
24 #include "iam_logger.h"
25 #include "iam_ptr.h"
26 
27 #include "mock_iall_in_one_executor_fuzzer.h"
28 #include "mock_icollector_executor_fuzzer.h"
29 
30 #include "i_inputer_data_impl.h"
31 #include "pin_auth_hdi.h"
32 
33 #define LOG_TAG "PIN_AUTH_SA"
34 
35 #undef private
36 
37 using namespace std;
38 using namespace OHOS::UserIam::Common;
39 
40 namespace OHOS {
41 namespace UserIam {
42 namespace PinAuth {
43 namespace {
44 const uint32_t SCHEDULE_ID = 123;
45 auto executorProxy_ = sptr<IAllInOneExecutor>(new (std::nothrow) MockIAllInOneExecutorFuzzer());
46 auto hdi_ = Common::MakeShared<PinAuthAllInOneHdi>(executorProxy_);
47 auto g_service = new (std::nothrow) IInputerDataImpl(SCHEDULE_ID, hdi_);
48 
FuzzRegisterInputer(Parcel & parcel)49 void FuzzRegisterInputer(Parcel &parcel)
50 {
51     IAM_LOGI("begin");
52     int32_t authSubType = parcel.ReadInt32();
53     std::vector<uint8_t> data;
54     int32_t errorCode = parcel.ReadInt32();
55     FillFuzzUint8Vector(parcel, data);
56     if (g_service != nullptr) {
57         g_service->OnSetData(authSubType, data, errorCode);
58     }
59     IAM_LOGI("end");
60 }
61 
62 using FuzzFunc = decltype(FuzzRegisterInputer);
63 FuzzFunc *g_fuzzFuncs[] = {FuzzRegisterInputer};
64 
InputerDataFuzzTest(const uint8_t * data,size_t size)65 void InputerDataFuzzTest(const uint8_t *data, size_t size)
66 {
67     Parcel parcel;
68     parcel.WriteBuffer(data, size);
69     parcel.RewindRead(0);
70     uint32_t index = parcel.ReadUint32() % (sizeof(g_fuzzFuncs) / sizeof(FuzzFunc *));
71     auto fuzzFunc = g_fuzzFuncs[index];
72     fuzzFunc(parcel);
73     return;
74 }
75 } // namespace
76 } // namespace PinAuth
77 } // namespace UserIam
78 } // namespace OHOS
79 
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
82 {
83     OHOS::UserIam::PinAuth::InputerDataFuzzTest(data, size);
84     return 0;
85 }