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 "sensorregisterandunregistercallback_fuzzer.h"
17 #include "hdf_base.h"
18 #include "sensor_impl.h"
19 #include "v1_0/isensor_interface.h"
20
21 using namespace OHOS::HDI::Sensor::V1_0;
22
23 namespace OHOS {
24 namespace HDI {
25 namespace Sensor {
26 namespace V1_0 {
OnDataEvent(const HdfSensorEvents & event)27 int32_t SensorRegisterAndUnregisterCallbackFuzzer::OnDataEvent(const HdfSensorEvents& event)
28 {
29 (void)event;
30 return HDF_SUCCESS;
31 }
32 } // V1_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<ISensorCallback> registerCallback = new SensorRegisterAndUnregisterCallbackFuzzer();
44 ret = sensorInterface->Register(*(int32_t *)data, registerCallback);
45 if (ret != HDF_SUCCESS) {
46 registerCallback = new SensorRegisterAndUnregisterCallbackFuzzer();
47 }
48
49 ret = sensorInterface->Unregister(*(int32_t *)data, registerCallback);
50 if (ret == HDF_SUCCESS) {
51 return true;
52 }
53 return result;
54 }
55 }
56
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)57 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
58 {
59 if (data == nullptr) {
60 return 0;
61 }
62
63 if (size < sizeof(int32_t)) {
64 return 0;
65 }
66 OHOS::SensorRegisterAndUnregisterCallbackFuzzTest(data, size);
67 return 0;
68 }
69
70