1 /*
2 * Copyright (c) 2022 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 "face_auth_service_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <memory>
21
22 #include "ibuffer_producer.h"
23 #include "parcel.h"
24 #include "refbase.h"
25 #include "surface.h"
26
27 #include "iam_logger.h"
28
29 #include "face_auth_service.h"
30
31 #define LOG_LABEL UserIam::Common::LABEL_FACE_AUTH_SA
32
33 #undef private
34
35 using namespace std;
36
37 namespace OHOS {
38 namespace UserIam {
39 namespace FaceAuth {
40 namespace {
41 auto g_service = FaceAuthService::GetInstance();
42
FuzzOnStart(Parcel & parcel)43 void FuzzOnStart(Parcel &parcel)
44 {
45 IAM_LOGI("begin");
46 if (g_service != nullptr) {
47 g_service->OnStart();
48 }
49 IAM_LOGI("end");
50 }
51
FuzzOnStop(Parcel & parcel)52 void FuzzOnStop(Parcel &parcel)
53 {
54 IAM_LOGI("begin");
55 if (g_service != nullptr) {
56 g_service->OnStop();
57 }
58 IAM_LOGI("end");
59 }
60
FuzzSetBufferProducer(Parcel & parcel)61 void FuzzSetBufferProducer(Parcel &parcel)
62 {
63 IAM_LOGI("begin");
64 sptr<IBufferProducer> bufferProducer = nullptr;
65 if (parcel.ReadBool()) {
66 auto surface = Surface::CreateSurfaceAsConsumer();
67 if (surface == nullptr) {
68 IAM_LOGE("CreateSurfaceAsConsumer fail");
69 return;
70 }
71 bufferProducer = surface->GetProducer();
72 }
73 if (g_service != nullptr) {
74 g_service->SetBufferProducer(bufferProducer);
75 }
76 IAM_LOGI("end");
77 }
78
79 using FuzzFunc = decltype(FuzzOnStart);
80 FuzzFunc *g_fuzzFuncs[] = {FuzzOnStart, FuzzOnStop, FuzzSetBufferProducer};
81
FaceAuthServiceFuzzTest(const uint8_t * data,size_t size)82 void FaceAuthServiceFuzzTest(const uint8_t *data, size_t size)
83 {
84 Parcel parcel;
85 parcel.WriteBuffer(data, size);
86 parcel.RewindRead(0);
87 uint32_t index = parcel.ReadUint32() % (sizeof(g_fuzzFuncs) / sizeof(FuzzFunc *));
88 auto fuzzFunc = g_fuzzFuncs[index];
89 fuzzFunc(parcel);
90 return;
91 }
92 } // namespace
93 } // namespace FaceAuth
94 } // namespace UserIam
95 } // namespace OHOS
96
97 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)98 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
99 {
100 OHOS::UserIam::FaceAuth::FaceAuthServiceFuzzTest(data, size);
101 return 0;
102 }
103