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