• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* This files contains faultlog fuzzer test modules. */
17 
18 #include "battery_fuzzer_test.h"
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <random>
23 #include <algorithm>
24 #include "battery_info.h"
25 #include "battery_srv_client.h"
26 
27 using namespace OHOS::PowerMgr;
28 using namespace std;
29 
30 namespace {
31 auto& g_batterySrvClient = BatterySrvClient::GetInstance();
32 }
33 
TestGetCapacity(const uint8_t * data)34 static void TestGetCapacity(const uint8_t* data)
35 {
36     if (data == nullptr) {
37         return;
38     }
39 
40     g_batterySrvClient.GetCapacity();
41 }
42 
TestGetVoltage(const uint8_t * data)43 static void TestGetVoltage(const uint8_t* data)
44 {
45     if (data == nullptr) {
46         return;
47     }
48 
49     g_batterySrvClient.GetVoltage();
50 }
51 
TestGetChargingStatus(const uint8_t * data)52 static void TestGetChargingStatus(const uint8_t* data)
53 {
54     if (data == nullptr) {
55         return;
56     }
57 
58     g_batterySrvClient.GetChargingStatus();
59 }
60 
TestGetHealthStatus(const uint8_t * data)61 static void TestGetHealthStatus(const uint8_t* data)
62 {
63     if (data == nullptr) {
64         return;
65     }
66 
67     g_batterySrvClient.GetHealthStatus();
68 }
69 
TestGetPluggedType(const uint8_t * data)70 static void TestGetPluggedType(const uint8_t* data)
71 {
72     if (data == nullptr) {
73         return;
74     }
75 
76     g_batterySrvClient.GetPluggedType();
77 }
78 
TestGetPresent(const uint8_t * data)79 static void TestGetPresent(const uint8_t* data)
80 {
81     if (data == nullptr) {
82         return;
83     }
84 
85     g_batterySrvClient.GetPresent();
86 }
87 
TestGetTechnology(const uint8_t * data)88 static void TestGetTechnology(const uint8_t* data)
89 {
90     if (data == nullptr) {
91         return;
92     }
93 
94     g_batterySrvClient.GetTechnology();
95 }
96 
TestGetTemperature(const uint8_t * data)97 static void TestGetTemperature(const uint8_t* data)
98 {
99     if (data == nullptr) {
100         return;
101     }
102 
103     g_batterySrvClient.GetBatteryTemperature();
104 }
105 
106 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)107 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
108 {
109     int32_t idSize = 8;
110     if (static_cast<int32_t>(size) > idSize) {
111         std::random_device rd;
112         std::default_random_engine engine(rd());
113         std::uniform_int_distribution<int32_t> randomNum(static_cast<int32_t>(ApiNumber::NUM_ZERO),
114             static_cast<int32_t>(ApiNumber::NUM_END) - 1);
115         int32_t number = randomNum(engine);
116 
117         switch (static_cast<ApiNumber>(number)) {
118             case ApiNumber::NUM_ZERO:
119                 TestGetCapacity(data);
120                 break;
121             case ApiNumber::NUM_ONE:
122                 TestGetVoltage(data);
123                 break;
124             case ApiNumber::NUM_TWO:
125                 TestGetChargingStatus(data);
126                 break;
127             case ApiNumber::NUM_THREE:
128                 TestGetHealthStatus(data);
129                 break;
130             case ApiNumber::NUM_FOUR:
131                 TestGetPluggedType(data);
132                 break;
133             case ApiNumber::NUM_FIVE:
134                 TestGetPresent(data);
135                 break;
136             case ApiNumber::NUM_SIX:
137                 TestGetTechnology(data);
138                 break;
139             case ApiNumber::NUM_SEVEN:
140                 TestGetTemperature(data);
141                 break;
142             case ApiNumber::NUM_EIGHT:
143                 g_batterySrvClient.GetNowCurrent();
144                 break;
145             case ApiNumber::NUM_NINE:
146                 g_batterySrvClient.GetRemainEnergy();
147                 break;
148             case ApiNumber::NUM_TEN:
149                 g_batterySrvClient.GetTotalEnergy();
150                 break;
151             case ApiNumber::NUM_ELEVEN:
152                 g_batterySrvClient.GetCapacityLevel();
153                 break;
154             case ApiNumber::NUM_TWELVE:
155                 g_batterySrvClient.GetRemainingChargeTime();
156                 break;
157             default:
158                 break;
159         }
160     }
161     return true;
162 }
163 }
164 
165 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)166 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
167 {
168     /* Run your code on data */
169     OHOS::DoSomethingInterestingWithMyAPI(data, size);
170     return 0;
171 }
172