1 /*
2  * Copyright (c) 2023 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_executor_stub_fuzzer.h"
17 #include "parcel.h"
18 #include "iam_logger.h"
19 #include "pin_auth_hdi.h"
20 #include "all_in_one_impl.h"
21 #include "v2_1/all_in_one_executor_service.h"
22 #include "v2_1/all_in_one_executor_stub.h"
23 
24 #undef LOG_TAG
25 #define LOG_TAG "PIN_AUTH_HDI"
26 
27 #undef private
28 
29 using namespace std;
30 using namespace OHOS::UserIam::Common;
31 
32 namespace OHOS {
33 namespace HDI {
34 namespace PinAuth {
35 namespace {
36 constexpr uint32_t PIN_AUTH_EXECUTOR_CODE_MIN = 0;
37 constexpr uint32_t PIN_AUTH_EXECUTOR_CODE_MAX = 11;
38 const std::u16string PIN_AUTH_EXECUTOR_TOKEN = u"ohos.hdi.pin_auth.v2_1.IExecutor";
PinAuthExecutorStubFuzzTest(const uint8_t * rawData,size_t size)39 bool PinAuthExecutorStubFuzzTest(const uint8_t *rawData, size_t size)
40 {
41     IAM_LOGI("start");
42     if (rawData == nullptr) {
43         return false;
44     }
45     std::shared_ptr<OHOS::UserIam::PinAuth::PinAuth> pinHdi = std::make_shared<OHOS::UserIam::PinAuth::PinAuth>();
46     AllInOneImpl *impl = new (std::nothrow) AllInOneImpl(pinHdi);
47     if (impl == nullptr) {
48         IAM_LOGE("%{public}s:get serviceImpl failed.", __func__);
49         return false;
50     }
51     sptr<OHOS::HDI::PinAuth::V2_1::AllInOneExecutorStub> executorStub =
52         new OHOS::HDI::PinAuth::V2_1::AllInOneExecutorStub(impl);
53     if (executorStub == nullptr) {
54         IAM_LOGE("%{public}s:new executorStub failed.", __func__);
55         return false;
56     }
57     for (uint32_t code = PIN_AUTH_EXECUTOR_CODE_MIN; code <= PIN_AUTH_EXECUTOR_CODE_MAX; code++) {
58         MessageParcel data;
59         MessageParcel reply;
60         MessageOption optionSync = MessageOption::TF_SYNC;
61         MessageOption optionAsync = MessageOption::TF_ASYNC;
62         std::u16string pin_auth_executor_token = PIN_AUTH_EXECUTOR_TOKEN;
63         // Sync
64         data.WriteInterfaceToken(pin_auth_executor_token);
65         data.WriteBuffer(rawData, size);
66         data.RewindRead(0);
67         (void)executorStub->OnRemoteRequest(code, data, reply, optionSync);
68         // Async
69         data.WriteInterfaceToken(pin_auth_executor_token);
70         data.WriteBuffer(rawData, size);
71         data.RewindRead(0);
72         (void)executorStub->OnRemoteRequest(code, data, reply, optionAsync);
73     }
74     return true;
75 }
76 } // namespace
77 } // namespace PinAuth
78 } // namespace UserIam
79 } // namespace OHOS
80 
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
83 {
84     OHOS::HDI::PinAuth::PinAuthExecutorStubFuzzTest(data, size);
85     return 0;
86 }
87