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 "sensorregisterasyncandunregisterasynccallback_fuzzer.h"
17 #include "hdf_base.h"
18 #include "v3_0/isensor_interface.h"
19 #include <hdf_log.h>
20
21 using namespace OHOS::HDI::Sensor;
22 using namespace OHOS::HDI::Sensor::V3_0;
23
24 namespace OHOS {
25 namespace HDI {
26 namespace Sensor {
27 namespace V3_0 {
OnDataEvent(const HdfSensorEvents & event)28 int32_t SensorRegisterAsyncAndUnregisterAsyncCallbackFuzzer::OnDataEvent(const HdfSensorEvents& event)
29 {
30 HDF_LOGI("%{public}s: sensorId=%{public}d", __func__, event.deviceSensorInfo.sensorType);
31 (void)event;
32 return HDF_SUCCESS;
33 }
34
OnDataEventAsync(const std::vector<HdfSensorEvents> & events)35 int32_t SensorRegisterAsyncAndUnregisterAsyncCallbackFuzzer::OnDataEventAsync(
36 const std::vector<HdfSensorEvents>& events)
37 {
38 HDF_LOGI("%{public}s: sensorId=%{public}d, timestamp=%{public}s", __func__,
39 events[0].deviceSensorInfo.sensorType, std::to_string(events[0].timestamp).c_str());
40 (void)events;
41 return HDF_SUCCESS;
42 }
43 } // V3_0
44 } // Sensor
45 } // HDI
46 } // OHOS
47
48 namespace OHOS {
SensorRegisterAsyncAndUnregisterAsyncCallbackFuzzTest(const uint8_t * data,size_t size)49 bool SensorRegisterAsyncAndUnregisterAsyncCallbackFuzzTest(const uint8_t* data, size_t size)
50 {
51 bool result = false;
52 int32_t ret;
53 sptr<V3_0::ISensorInterface> sensorInterface = V3_0::ISensorInterface::Get();
54 sptr<V3_0::ISensorCallback> registerAsyncCallback = new SensorRegisterAsyncAndUnregisterAsyncCallbackFuzzer();
55 if (registerAsyncCallback == nullptr) {
56 return false;
57 }
58 ret = sensorInterface->RegisterAsync(*(int32_t *)data, registerAsyncCallback);
59 if (ret != HDF_SUCCESS) {
60 registerAsyncCallback = new SensorRegisterAsyncAndUnregisterAsyncCallbackFuzzer();
61 if (registerAsyncCallback == nullptr) {
62 return false;
63 }
64 }
65
66 ret = sensorInterface->UnregisterAsync(*(int32_t *)data, registerAsyncCallback);
67 if (ret == HDF_SUCCESS) {
68 return true;
69 }
70 return result;
71 }
72 }
73
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
75 {
76 if (data == nullptr) {
77 return 0;
78 }
79
80 if (size < sizeof(int32_t)) {
81 return 0;
82 }
83 OHOS::SensorRegisterAsyncAndUnregisterAsyncCallbackFuzzTest(data, size);
84 return 0;
85 }
86
87