1 /*
2  * Copyright (c) 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 "batteryhdi_fuzz.h"
17 
18 #include "securec.h"
19 #include <cstdint>
20 #include <cstdlib>
21 #include <map>
22 #include <random>
23 #include <vector>
24 
25 #include "v2_0/battery_interface_proxy.h"
26 #include "v2_0/ibattery_callback.h"
27 #include "v2_0/types.h"
28 
29 using namespace OHOS::HDI::Battery::V2_0;
30 using namespace HDI::Battery;
31 
32 namespace OHOS {
33 namespace HDI {
34 namespace Battery {
35 namespace V2_0 {
36 namespace {
37 class BatteryCallback : public IBatteryCallback {
38 public:
BatteryCallback()39     BatteryCallback() {};
~BatteryCallback()40     ~BatteryCallback() override {};
41     int32_t Update([[maybe_unused]] const BatteryInfo &event) override
42     {
43         return 0;
44     };
45 };
46 sptr<IBatteryInterface> g_batteryInterface = IBatteryInterface::Get();
47 } // namespace
48 
49 void Register([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
50 {
51     sptr<IBatteryCallback> callback = new BatteryCallback();
52     g_batteryInterface->Register(callback);
53     g_batteryInterface->Register(nullptr);
54 }
55 
56 void UnRegister([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
57 {
58     g_batteryInterface->UnRegister();
59 }
60 
ChangePath(const uint8_t * data,size_t size)61 void ChangePath(const uint8_t *data, size_t size)
62 {
63     std::string result(reinterpret_cast<const char *>(data), size);
64     g_batteryInterface->ChangePath(result);
65 }
66 
67 void GetCapacity([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
68 {
69     int32_t out;
70     g_batteryInterface->GetCapacity(out);
71 }
72 
73 void GetVoltage([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
74 {
75     int32_t out;
76     g_batteryInterface->GetVoltage(out);
77 }
78 
79 void GetTemperature([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
80 {
81     int32_t out;
82     g_batteryInterface->GetTemperature(out);
83 }
84 
85 void GetHealthState([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
86 {
87     BatteryHealthState state;
88     g_batteryInterface->GetHealthState(state);
89 }
90 
91 void GetPluggedType([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
92 {
93     BatteryPluggedType type;
94     g_batteryInterface->GetPluggedType(type);
95 }
96 
97 void GetChargeState([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
98 {
99     BatteryChargeState state;
100     g_batteryInterface->GetChargeState(state);
101 }
102 
103 void GetPresent([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
104 {
105     bool present;
106     g_batteryInterface->GetPresent(present);
107 }
108 
109 void GetTechnology([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
110 {
111     std::string str;
112     g_batteryInterface->GetTechnology(str);
113 }
114 
115 void GetTotalEnergy([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
116 {
117     int32_t out;
118     g_batteryInterface->GetTotalEnergy(out);
119 }
120 
121 void GetCurrentAverage([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
122 {
123     int32_t out;
124     g_batteryInterface->GetCurrentAverage(out);
125 }
126 
127 void GetCurrentNow([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
128 {
129     int32_t out;
130     g_batteryInterface->GetCurrentNow(out);
131 }
132 
133 void GetRemainEnergy([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
134 {
135     int32_t out;
136     g_batteryInterface->GetRemainEnergy(out);
137 }
138 
139 void GetBatteryInfo([[maybe_unused]] const uint8_t *data, [[maybe_unused]] size_t size)
140 {
141     BatteryInfo info;
142     g_batteryInterface->GetBatteryInfo(info);
143 }
144 
SetChargingLimit(const uint8_t * data,size_t size)145 void SetChargingLimit(const uint8_t *data, size_t size)
146 {
147     int32_t inputData;
148     if (size < sizeof(inputData)) {
149         return;
150     }
151     if (memcpy_s(&inputData, sizeof(inputData), data, sizeof(inputData)) != EOK) {
152         return;
153     }
154     int32_t minVectorSize = 0;
155     int32_t maxVectorSize = 5000;
156     int32_t length = (inputData < minVectorSize) ? minVectorSize : inputData;
157     length = (length > maxVectorSize) ? maxVectorSize : length;
158     std::vector<ChargingLimit> scLimit;
159     scLimit.resize(length);
160     for (auto &item : scLimit) {
161         item.type = ChargingLimitType(inputData);
162         item.protocol = std::string(reinterpret_cast<const char *>(data), size);
163         item.value = inputData;
164     }
165     g_batteryInterface->SetChargingLimit(scLimit);
166 }
167 
168 static std::vector<std::function<void(const uint8_t *, size_t)>> fuzzFunc = {
169     &Register,
170     &UnRegister,
171     &ChangePath,
172     &GetCapacity,
173     &GetVoltage,
174     &GetTemperature,
175     &GetHealthState,
176     &GetPluggedType,
177     &GetChargeState,
178     &GetPresent,
179     &GetTechnology,
180     &GetTotalEnergy,
181     &GetCurrentAverage,
182     &GetCurrentNow,
183     &GetRemainEnergy,
184     &GetBatteryInfo,
185     &SetChargingLimit,
186 };
187 
BatteryHdiFuzzTest(const uint8_t * data,size_t size)188 void BatteryHdiFuzzTest(const uint8_t *data, size_t size)
189 {
190     std::random_device rd;
191     std::default_random_engine engine(rd());
192     std::uniform_int_distribution<int32_t> randomNum(0, fuzzFunc.size() - 1);
193     int32_t number = randomNum(engine);
194     fuzzFunc[number](data, size);
195 }
196 } // namespace V2_0
197 } // namespace Battery
198 } // namespace HDI
199 } // namespace OHOS
200 
201 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)202 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
203 {
204     /* Run your code on data */
205     OHOS::HDI::Battery::V2_0::BatteryHdiFuzzTest(data, size);
206     return 0;
207 }
208