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 "sensorregisterandunregistercallback_fuzzer.h"
17 #include "hdf_base.h"
18 #include "v2_0/isensor_interface.h"
19 
20 using namespace OHOS::HDI::Sensor::V2_0;
21 
22 namespace OHOS {
23 namespace HDI {
24 namespace Sensor {
25 namespace V2_0 {
OnDataEvent(const HdfSensorEvents & event)26 int32_t SensorRegisterAndUnregisterCallbackFuzzer::OnDataEvent(const HdfSensorEvents& event)
27 {
28     (void)event;
29     return HDF_SUCCESS;
30 }
31 } // V2_0
32 } // Sensor
33 } // HDI
34 } // OHOS
35 
36 namespace OHOS {
SensorRegisterAndUnregisterCallbackFuzzTest(const uint8_t * data,size_t size)37     bool SensorRegisterAndUnregisterCallbackFuzzTest(const uint8_t* data, size_t size)
38     {
39         bool result = false;
40         int32_t ret;
41         sptr<ISensorInterface> sensorInterface = ISensorInterface::Get();
42         sptr<ISensorCallback> registerCallback = new SensorRegisterAndUnregisterCallbackFuzzer();
43         if (registerCallback == nullptr) {
44             return false;
45         }
46         ret = sensorInterface->Register(*(int32_t *)data, registerCallback);
47         if (ret != HDF_SUCCESS) {
48             registerCallback = new SensorRegisterAndUnregisterCallbackFuzzer();
49             if (registerCallback == nullptr) {
50                 return false;
51             }
52         }
53 
54         ret = sensorInterface->Unregister(*(int32_t *)data, registerCallback);
55         if (ret == HDF_SUCCESS) {
56             return true;
57         }
58         return result;
59     }
60 }
61 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
63 {
64     if (data == nullptr) {
65         return 0;
66     }
67 
68     if (size < sizeof(int32_t)) {
69         return 0;
70     }
71     OHOS::SensorRegisterAndUnregisterCallbackFuzzTest(data, size);
72     return 0;
73 }
74 
75