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 "thermal_fuzzer_test.h"
19
20 #include "thermal_common.h"
21
22 using namespace std;
23 using namespace OHOS;
24 using namespace OHOS::PowerMgr;
25 namespace {
26 auto& g_thermalMgrClient = ThermalMgrClient::GetInstance();
27 constexpr int32_t WAIT_TIME = 1000;
28 } // namespace
29
30 static sptr<IThermalActionCallback> actionCb_;
31 static sptr<IThermalActionCallback> testActionCb_;
32 static sptr<IThermalTempCallback> tempCb_;
33 static sptr<IThermalTempCallback> testTempCb_;
34 static sptr<IThermalLevelCallback> levelCb_;
35 static sptr<IThermalLevelCallback> testLevelCb_;
36
OnThermalActionChanged(ActionCallbackMap & actionCbMap)37 bool ThermalFuzzerTest::ThermalActionTestCallback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
38 {
39 THERMAL_HILOGI(COMP_SVC, "action callback");
40 return true;
41 }
42
OnThermalTempChanged(TempCallbackMap & tempCbMap)43 bool ThermalFuzzerTest::ThermalTempTestCallback::OnThermalTempChanged(TempCallbackMap& tempCbMap)
44 {
45 THERMAL_HILOGI(COMP_SVC, "temp callback");
46 return true;
47 }
48
GetThermalLevel(ThermalLevel level)49 bool ThermalFuzzerTest::ThermalLevelTestCallback::GetThermalLevel(ThermalLevel level)
50 {
51 THERMAL_HILOGI(COMP_SVC, "level is: %{public}d", static_cast<int32_t>(level));
52 return true;
53 }
54
TestSubscribeTemp(const uint8_t * data)55 void ThermalFuzzerTest::TestSubscribeTemp(const uint8_t* data)
56 {
57 const int32_t NUMBER_FOUR = 4;
58 int32_t type[1];
59 int32_t idSize = 4;
60 std::vector<std::string> typeList;
61 for (int32_t i = 0; i < NUMBER_FOUR; i++) {
62 if (memcpy_s(type, sizeof(type), data, idSize) != EOK) {
63 return;
64 }
65 typeList.push_back(to_string(type[0]));
66 }
67 g_thermalMgrClient.SubscribeThermalTempCallback(typeList, tempCb_);
68 g_thermalMgrClient.SubscribeThermalTempCallback(typeList, testTempCb_);
69 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
70 TestUnSubscribeTemp();
71 }
72
TestUnSubscribeTemp()73 void ThermalFuzzerTest::TestUnSubscribeTemp()
74 {
75 g_thermalMgrClient.UnSubscribeThermalTempCallback(tempCb_);
76 g_thermalMgrClient.UnSubscribeThermalTempCallback(testTempCb_);
77 }
78
TestSubscribeAction(const uint8_t * data)79 void ThermalFuzzerTest::TestSubscribeAction(const uint8_t* data)
80 {
81 std::string desc = "";
82 const int32_t NUMBER_FOUR = 4;
83 int32_t type[1];
84 int32_t idSize = 4;
85 std::vector<std::string> actionList;
86 for (int32_t i = 0; i < NUMBER_FOUR; i++) {
87 if (memcpy_s(type, sizeof(type), data, idSize) != EOK) {
88 return;
89 }
90 actionList.push_back(to_string(type[0]));
91 }
92 g_thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, actionCb_);
93 g_thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, testActionCb_);
94 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
95 TestUnSubscribeAction();
96 }
97
TestUnSubscribeAction()98 void ThermalFuzzerTest::TestUnSubscribeAction()
99 {
100 g_thermalMgrClient.UnSubscribeThermalActionCallback(actionCb_);
101 g_thermalMgrClient.UnSubscribeThermalActionCallback(testActionCb_);
102 }
103
TestSubscribeLevel(const uint8_t * data)104 void ThermalFuzzerTest::TestSubscribeLevel(const uint8_t* data)
105 {
106 g_thermalMgrClient.SubscribeThermalLevelCallback(levelCb_);
107 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME));
108 TestUnSubscribeLevel();
109 }
110
TestUnSubscribeLevel()111 void ThermalFuzzerTest::TestUnSubscribeLevel()
112 {
113 g_thermalMgrClient.UnSubscribeThermalLevelCallback(levelCb_);
114 }
115
TestGetLevel()116 void ThermalFuzzerTest::TestGetLevel()
117 {
118 ThermalLevel level = g_thermalMgrClient.GetThermalLevel();
119 cout << "Thermal level is: " << static_cast<int32_t>(level) << endl;
120 }
121
TestSetScene(const uint8_t * data)122 void ThermalFuzzerTest::TestSetScene(const uint8_t* data)
123 {
124 int32_t type[1];
125 int32_t idSize = 4;
126 if (memcpy_s(type, sizeof(type), data, idSize) != EOK) {
127 return;
128 }
129
130 g_thermalMgrClient.SetScene(to_string(type[0]));
131 }
132
TestGetSensorTemp(const uint8_t * data)133 void ThermalFuzzerTest::TestGetSensorTemp(const uint8_t* data)
134 {
135 int32_t type[1];
136 int32_t idSize = 4;
137 if (memcpy_s(type, sizeof(type), data, idSize) != EOK) {
138 return;
139 }
140
141 SensorType sensorType = static_cast<SensorType>(type[0]);
142 int32_t temp = g_thermalMgrClient.GetThermalSensorTemp(sensorType);
143 cout << "Sensor temp is: " << temp << endl;
144 }
145
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)146 bool ThermalFuzzerTest::DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
147 {
148 int32_t idSize = 4;
149 int32_t cond[1];
150 if (static_cast<int32_t>(size) > idSize) {
151 if (memcpy_s(cond, sizeof(cond), data, idSize) != EOK) {
152 return false;
153 }
154
155 tempCb_ = new ThermalTempTestCallback();
156 levelCb_ = new ThermalLevelTestCallback();
157 std::random_device rd;
158 std::default_random_engine engine(rd());
159 std::uniform_int_distribution<int32_t> randomNum(
160 static_cast<int32_t>(ApiNumber::NUM_ZERO), static_cast<int32_t>(ApiNumber::NUM_END) - 1);
161 cond[0] = randomNum(engine);
162
163 switch (static_cast<ApiNumber>(cond[0])) {
164 case ApiNumber::NUM_ZERO:
165 TestSubscribeTemp(data);
166 break;
167 case ApiNumber::NUM_ONE:
168 TestSubscribeLevel(data);
169 break;
170 case ApiNumber::NUM_TWO:
171 TestGetLevel();
172 break;
173 case ApiNumber::NUM_THREE:
174 TestGetSensorTemp(data);
175 break;
176 case ApiNumber::NUM_FOUR:
177 TestSubscribeAction(data);
178 break;
179 case ApiNumber::NUM_FIVE:
180 TestSetScene(data);
181 break;
182 default:
183 break;
184 }
185 }
186 return true;
187 }
188
189 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)190 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
191 {
192 /* Run your code on data */
193 OHOS::PowerMgr::ThermalFuzzerTest::DoSomethingInterestingWithMyAPI(data, size);
194 return 0;
195 }
196