1 /*
2 * Copyright (c) 2024 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 "sensoradapter_fuzz.h"
17
18 #include <vector>
19 #include <fuzzer/FuzzedDataProvider.h>
20
21 #define private public
22 #include "sensor_adapter_impl.h"
23 #undef private
24
25 namespace OHOS {
26 namespace NWeb {
27 constexpr int MAX_SET_NUMBER = 1000;
28
29 class SensorCallbackAdapterMock : public SensorCallbackAdapter {
30 public:
31 ~SensorCallbackAdapterMock() = default;
UpdateOhosSensorData(double timestamp,double value1,double value2,double value3,double value4)32 void UpdateOhosSensorData(double timestamp,
33 double value1, double value2, double value3, double value4) {}
34 };
35
SensorAdapterFuzzTest(const uint8_t * data,size_t size)36 bool SensorAdapterFuzzTest(const uint8_t* data, size_t size)
37 {
38 if ((data == nullptr) || (size == 0)) {
39 return false;
40 }
41 auto sensorCallbackAdapterMock = std::make_shared<SensorCallbackAdapterMock>();
42 auto callback = std::make_shared<SensorCallbackImpl>(sensorCallbackAdapterMock);
43 double timestamp = 100;
44 double value1 = 0.0;
45 double value2 = 0.0;
46 double value3 = 0.0;
47 double value4 = 0.0;
48 callback->UpdateOhosSensorData(timestamp, value1, value2, value3, value4);
49 NWeb::SensorAdapterImpl sensorAdapterImpl;
50 std::vector<int32_t> sensorTypes = { 2, 3, 4, 5, 6, 8, 9, 11 };
51
52 FuzzedDataProvider dataProvider(data, size);
53 for (int32_t sensorTypeId : sensorTypes) {
54 int64_t samplingInterval = dataProvider.ConsumeIntegralInRange<int64_t>(0, MAX_SET_NUMBER);
55 sensorAdapterImpl.IsOhosSensorSupported(sensorTypeId);
56 sensorAdapterImpl.GetOhosSensorReportingMode(sensorTypeId);
57 sensorAdapterImpl.GetOhosSensorDefaultSupportedFrequency(sensorTypeId);
58 sensorAdapterImpl.GetOhosSensorMinSupportedFrequency(sensorTypeId);
59 sensorAdapterImpl.GetOhosSensorMaxSupportedFrequency(sensorTypeId);
60 sensorAdapterImpl.SubscribeOhosSensor(sensorTypeId, samplingInterval);
61 auto callbackAdapter = std::make_shared<SensorCallbackAdapterMock>();
62 sensorAdapterImpl.RegistOhosSensorCallback(sensorTypeId, callbackAdapter);
63 SensorEvent event;
64 event.sensorTypeId = sensorTypeId;
65 event.data = nullptr;
66 sensorAdapterImpl.OhosSensorCallback(&event);
67 sensorAdapterImpl.UnsubscribeOhosSensor(sensorTypeId);
68 }
69 SensorEvent event;
70 event.data = nullptr;
71 sensorAdapterImpl.handleAccelerometerData(callback, &event);
72 sensorAdapterImpl.handleLinearAccelerometerData(callback, &event);
73 sensorAdapterImpl.handleGravityData(callback, &event);
74 sensorAdapterImpl.handleCyroscopeData(callback, &event);
75 sensorAdapterImpl.handleMagnetometerData(callback, &event);
76 sensorAdapterImpl.handleOrientationData(callback, &event);
77 sensorAdapterImpl.handleRotationVectorData(callback, &event);
78 sensorAdapterImpl.handleGameRotationVectorData(callback, &event);
79 return true;
80 }
81 } // namespace NWeb
82 } // namespace OHOS
83
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
85 {
86 OHOS::NWeb::SensorAdapterFuzzTest(data, size);
87 return 0;
88 }
89