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 "user_auth_stub_fuzzer.h"
17
18 #include <cinttypes>
19 #include <cstddef>
20 #include <cstdint>
21
22 #include "parcel.h"
23
24 #include "iam_fuzz_test.h"
25 #include "iam_logger.h"
26 #include "user_auth_service.h"
27
28 #define LOG_TAG "USER_AUTH_SA"
29
30 using namespace std;
31
32 namespace OHOS {
33 namespace UserIam {
34 namespace UserAuth {
35 namespace {
36 constexpr uint32_t USER_AUTH_CODE_MIN = 0;
37 constexpr uint32_t USER_AUTH_CODE_MAX = 25;
38 const std::u16string USER_AUTH_INTERFACE_TOKEN = u"ohos.UserIam.UserAuth.IUserAuth";
39
UserAuthStubFuzzTest(const uint8_t * rawData,size_t size)40 bool UserAuthStubFuzzTest(const uint8_t *rawData, size_t size)
41 {
42 IAM_LOGI("start");
43 if (rawData == nullptr) {
44 return false;
45 }
46
47 auto service = UserAuthService::GetInstance();
48 for (uint32_t code = USER_AUTH_CODE_MIN; code < USER_AUTH_CODE_MAX; code++) {
49 MessageParcel data;
50 MessageParcel reply;
51 MessageOption optionSync = MessageOption::TF_SYNC;
52 MessageOption optionAsync = MessageOption::TF_ASYNC;
53 // Sync
54 data.WriteInterfaceToken(USER_AUTH_INTERFACE_TOKEN);
55 data.WriteBuffer(rawData, size);
56 data.RewindRead(0);
57 (void)service->OnRemoteRequest(code, data, reply, optionSync);
58 // Async
59 data.WriteInterfaceToken(USER_AUTH_INTERFACE_TOKEN);
60 data.WriteBuffer(rawData, size);
61 data.RewindRead(0);
62 (void)service->OnRemoteRequest(code, data, reply, optionAsync);
63 }
64 return true;
65 }
66 } // namespace
67 } // namespace UserAuth
68 } // namespace UserIam
69 } // namespace OHOS
70
71 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
73 {
74 /* Run your code on data */
75 OHOS::UserIam::UserAuth::UserAuthStubFuzzTest(data, size);
76 return 0;
77 }
78