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 "face_auth_service_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <memory>
21 
22 #include "ibuffer_producer.h"
23 #include "iconsumer_surface.h"
24 #include "parcel.h"
25 #include "refbase.h"
26 
27 #include "iam_logger.h"
28 
29 #include "face_auth_service.h"
30 
31 #define LOG_TAG "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 
FuzzSetBufferProducer(Parcel & parcel)43 void FuzzSetBufferProducer(Parcel &parcel)
44 {
45     IAM_LOGI("begin");
46     sptr<IBufferProducer> bufferProducer(nullptr);
47     if (parcel.ReadBool()) {
48         auto surface = IConsumerSurface::Create();
49         if (surface == nullptr) {
50             IAM_LOGE("CreateSurfaceAsConsumer fail");
51             return;
52         }
53         bufferProducer = surface->GetProducer();
54     }
55     if (g_service != nullptr) {
56         g_service->SetBufferProducer(bufferProducer);
57     }
58     IAM_LOGI("end");
59 }
60 
61 using FuzzFunc = decltype(FuzzSetBufferProducer);
62 FuzzFunc *g_fuzzFuncs[] = { FuzzSetBufferProducer };
63 
FaceAuthServiceFuzzTest(const uint8_t * data,size_t size)64 void FaceAuthServiceFuzzTest(const uint8_t *data, size_t size)
65 {
66     Parcel parcel;
67     parcel.WriteBuffer(data, size);
68     parcel.RewindRead(0);
69     uint32_t index = parcel.ReadUint32() % (sizeof(g_fuzzFuncs) / sizeof(FuzzFunc *));
70     auto fuzzFunc = g_fuzzFuncs[index];
71     fuzzFunc(parcel);
72     return;
73 }
74 } // namespace
75 } // namespace FaceAuth
76 } // namespace UserIam
77 } // namespace OHOS
78 
79 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)80 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
81 {
82     OHOS::UserIam::FaceAuth::FaceAuthServiceFuzzTest(data, size);
83     return 0;
84 }
85