1 /*
2 * Copyright (c) 2022-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 "fingerprint_auth_service_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "parcel.h"
22
23 #include "iam_logger.h"
24
25 #include "fingerprint_auth_service.h"
26
27 #define LOG_TAG "FINGERPRINT_AUTH_SA"
28
29 #undef private
30
31 using namespace std;
32
33 namespace OHOS {
34 namespace UserIam {
35 namespace FingerprintAuth {
36 namespace {
37 auto g_service = FingerprintAuthService::GetInstance();
38
FuzzOnStart(Parcel & parcel)39 void FuzzOnStart(Parcel &parcel)
40 {
41 IAM_LOGI("begin");
42 if (g_service != nullptr) {
43 g_service->OnStart();
44 }
45 IAM_LOGI("end");
46 }
47
FuzzOnStop(Parcel & parcel)48 void FuzzOnStop(Parcel &parcel)
49 {
50 IAM_LOGI("begin");
51 if (g_service != nullptr) {
52 g_service->OnStop();
53 }
54 IAM_LOGI("end");
55 }
56
57 using FuzzFunc = decltype(FuzzOnStart);
58 FuzzFunc *g_fuzzFuncs[] = { FuzzOnStart, FuzzOnStop };
59
FingerprintAuthServiceFuzzTest(const uint8_t * data,size_t size)60 void FingerprintAuthServiceFuzzTest(const uint8_t *data, size_t size)
61 {
62 Parcel parcel;
63 parcel.WriteBuffer(data, size);
64 parcel.RewindRead(0);
65 uint32_t index = parcel.ReadUint32() % (sizeof(g_fuzzFuncs) / sizeof(FuzzFunc *));
66 auto fuzzFunc = g_fuzzFuncs[index];
67 fuzzFunc(parcel);
68 return;
69 }
70 } // namespace
71 } // namespace FingerprintAuth
72 } // namespace UserIam
73 } // namespace OHOS
74
75 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
77 {
78 OHOS::UserIam::FingerprintAuth::FingerprintAuthServiceFuzzTest(data, size);
79 return 0;
80 }
81