1 /*
2 * Copyright (c) 2021-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 <cmath>
17 #include <cstdio>
18 #include <gtest/gtest.h>
19 #include <mutex>
20 #include <fcntl.h>
21 #include <functional>
22 #include <securec.h>
23 #include <unistd.h>
24
25 #include "hdf_base.h"
26 #include "osal_time.h"
27 #include "v1_1/ifan_callback.h"
28 #include "v1_1/ithermal_interface.h"
29 #include "v1_1/ithermal_callback.h"
30 #include "v1_1/thermal_types.h"
31 #include "thermal_log.h"
32
33 using namespace OHOS::HDI;
34 using namespace OHOS::HDI::Thermal::V1_1;
35 using namespace testing::ext;
36
37 namespace {
38 class ThermalCallbackMock : public IThermalCallback {
39 public:
~ThermalCallbackMock()40 virtual ~ThermalCallbackMock() {}
41 using ThermalEventCallback = std::function<int32_t(const HdfThermalCallbackInfo &event)>;
RegisterThermalEvent(const ThermalEventCallback & eventCb)42 static int32_t RegisterThermalEvent(const ThermalEventCallback &eventCb)
43 {
44 (void)eventCb;
45 return 0;
46 }
OnThermalDataEvent(const HdfThermalCallbackInfo & event)47 int32_t OnThermalDataEvent(const HdfThermalCallbackInfo &event) override
48 {
49 (void)event;
50 return 0;
51 }
52 };
53
54 class FanCallbackMock : public IFanCallback {
55 public:
~FanCallbackMock()56 virtual ~FanCallbackMock() {}
57 using FanEventCallback = std::function<int32_t(const HdfThermalCallbackInfo &event)>;
RegisterFanEvent(const FanEventCallback & eventCb)58 static int32_t RegisterFanEvent(const FanEventCallback &eventCb)
59 {
60 (void)eventCb;
61 return 0;
62 }
OnFanDataEvent(const HdfThermalCallbackInfo & event)63 int32_t OnFanDataEvent(const HdfThermalCallbackInfo &event) override
64 {
65 (void)event;
66 return 0;
67 }
68 };
69
70 sptr<IThermalInterface> g_thermalInterface = nullptr;
71 sptr<IThermalCallback> g_callback = new ThermalCallbackMock();
72 sptr<IFanCallback> g_fanCallback = new FanCallbackMock();
73 std::mutex g_mutex;
74 const uint32_t MAX_PATH = 256;
75 const std::string CPU_FREQ_PATH = "/data/service/el0/thermal/cooling/cpu/freq";
76 const std::string GPU_FREQ_PATH = "/data/service/el0/thermal/cooling/gpu/freq";
77 const std::string BATTERY_CHARGER_CURRENT_PATH = "/data/service/el0/thermal/cooling/battery/current";
78 const std::string ISOLATE_PATH = "/data/service/el0/thermal/sensor/soc/isolate";
79
80 class HdfThermalHdiTest : public testing::Test {
81 public:
82 static void SetUpTestCase();
83 static void TearDownTestCase();
84 void SetUp();
85 void TearDown();
86 static int32_t ReadFile(const char *path, char *buf, size_t size);
87 static int32_t ConvertInt(const std::string &value);
88 };
89
SetUpTestCase()90 void HdfThermalHdiTest::SetUpTestCase()
91 {
92 g_thermalInterface = IThermalInterface::Get();
93 }
94
TearDownTestCase()95 void HdfThermalHdiTest::TearDownTestCase()
96 {
97 }
98
SetUp()99 void HdfThermalHdiTest::SetUp()
100 {
101 }
102
TearDown()103 void HdfThermalHdiTest::TearDown()
104 {
105 }
106
ReadFile(const char * path,char * buf,size_t size)107 int32_t HdfThermalHdiTest::ReadFile(const char *path, char *buf, size_t size)
108 {
109 std::lock_guard<std::mutex> lck(g_mutex);
110 int32_t ret;
111
112 int32_t fd = open(path, O_RDONLY, S_IRUSR | S_IRGRP | S_IROTH);
113 if (fd < HDF_SUCCESS) {
114 THERMAL_HILOGE(LABEL_TEST, "WriteFile: failed to open file %{public}d", fd);
115 return HDF_FAILURE;
116 }
117
118 ret = read(fd, buf, size);
119 if (ret < HDF_SUCCESS) {
120 THERMAL_HILOGE(LABEL_TEST, "WriteFile: failed to read file %{public}d", ret);
121 close(fd);
122 return HDF_FAILURE;
123 }
124
125 close(fd);
126 buf[size - 1] = '\0';
127 return HDF_SUCCESS;
128 }
129
ConvertInt(const std::string & value)130 int32_t HdfThermalHdiTest::ConvertInt(const std::string &value)
131 {
132 return std::stoi(value);
133 }
134 }
135
136 namespace {
137 /**
138 * @tc.name: HdfThermalHdiTest001
139 * @tc.desc: Get a client and check whether the client is empty.
140 * @tc.type: FUNC
141 */
142 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest001, TestSize.Level0)
143 {
144 ASSERT_NE(nullptr, g_thermalInterface);
145 }
146
147 /**
148 * @tc.name: HdfThermalHdiTest002
149 * @tc.desc: set cpu freq
150 * @tc.type: FUNC
151 */
152 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest002, TestSize.Level0)
153 {
154 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest002: start.");
155 int32_t cpuFreq = 1994100;
156 int32_t ret = g_thermalInterface->SetCpuFreq(cpuFreq);
157 EXPECT_EQ(0, ret);
158
159 char cpuBuf[MAX_PATH] = {0};
160 char freqValue[MAX_PATH] = {0};
161
162 ASSERT_FALSE(snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, CPU_FREQ_PATH.c_str()) < EOK);
163
164 ret = HdfThermalHdiTest::ReadFile(cpuBuf, freqValue, sizeof(freqValue));
165 ASSERT_EQ(ret, HDF_SUCCESS);
166
167 std::string freq = freqValue;
168 int32_t value = HdfThermalHdiTest::ConvertInt(freq);
169 THERMAL_HILOGD(LABEL_TEST, "freq is %{public}d", value);
170 EXPECT_EQ(value, cpuFreq) << "HdfThermalHdiTest002 failed";
171 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest002: return.");
172 }
173
174 /**
175 * @tc.name: HdfThermalHdiTest003
176 * @tc.desc: set gpu freq
177 * @tc.type: FUNC
178 */
179 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest003, TestSize.Level0)
180 {
181 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest003: start.");
182 int32_t gpuFreq = 40000;
183 int32_t ret = g_thermalInterface->SetGpuFreq(gpuFreq);
184 EXPECT_EQ(0, ret);
185
186 char cpuBuf[MAX_PATH] = {0};
187 char freqValue[MAX_PATH] = {0};
188
189
190 ASSERT_FALSE(snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, GPU_FREQ_PATH.c_str()) < EOK);
191
192 ret = HdfThermalHdiTest::ReadFile(cpuBuf, freqValue, sizeof(freqValue));
193 ASSERT_EQ(ret, HDF_SUCCESS);
194
195 std::string freq = freqValue;
196 int32_t value = HdfThermalHdiTest::ConvertInt(freq);
197 THERMAL_HILOGD(LABEL_TEST, "freq is %{public}d", value);
198 EXPECT_EQ(value, gpuFreq) << "HdfThermalHdiTest003 failed";
199 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest003: return.");
200 }
201
202 /**
203 * @tc.name: HdfThermalHdiTest004
204 * @tc.desc: set battery current
205 * @tc.type: FUNC
206 */
207 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest004, TestSize.Level0)
208 {
209 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest004: start.");
210 int32_t batteryCurrent = 1000;
211 int32_t ret = g_thermalInterface->SetBatteryCurrent(batteryCurrent);
212 EXPECT_EQ(0, ret);
213
214 char cpuBuf[MAX_PATH] = {0};
215 char currentValue[MAX_PATH] = {0};
216
217 ASSERT_FALSE(snprintf_s(cpuBuf, MAX_PATH, sizeof(cpuBuf) - 1, BATTERY_CHARGER_CURRENT_PATH.c_str()) < EOK);
218
219 ret = HdfThermalHdiTest::ReadFile(cpuBuf, currentValue, sizeof(currentValue));
220
221 ASSERT_EQ(ret, HDF_SUCCESS);
222
223 std::string current = currentValue;
224 int32_t value = HdfThermalHdiTest::ConvertInt(current);
225 THERMAL_HILOGD(LABEL_TEST, "freq is %{public}d", value);
226 EXPECT_EQ(value, batteryCurrent) << "HdfThermalHdiTest004 failed";
227 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest004: return.");
228 }
229
230 /**
231 * @tc.name: HdfThermalHdiTest005
232 * @tc.desc: get thermal zone info
233 * @tc.type: FUNC
234 */
235 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest005, TestSize.Level0)
236 {
237 HdfThermalCallbackInfo event;
238 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest005: start.");
239 int32_t ret = g_thermalInterface->GetThermalZoneInfo(event);
240 EXPECT_EQ(0, ret) << "HdfThermalHdiTest005 failed";
241 for (auto iter : event.info) {
242 THERMAL_HILOGD(LABEL_TEST, "type is %{public}s", iter.type.c_str());
243 THERMAL_HILOGD(LABEL_TEST, "temp is %{public}d", iter.temp);
244 }
245 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest005: return.");
246 }
247
248 /**
249 * @tc.name: HdfThermalHdiTest006
250 * @tc.desc: register callback
251 * @tc.type: FUNC
252 */
253 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest006, TestSize.Level0)
254 {
255 HdfThermalCallbackInfo event;
256 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest006: start.");
257 int32_t ret = g_thermalInterface->Register(g_callback);
258 EXPECT_EQ(0, ret) << "HdfThermalHdiTest006 failed";
259 ret = g_thermalInterface->Unregister();
260 EXPECT_EQ(0, ret) << "HdfThermalHdiTest006 failed";
261 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest006: return.");
262 }
263
264 /**
265 * @tc.name: HdfThermalHdiTest007
266 * @tc.desc: isolate cpu num
267 * @tc.type: FUNC
268 */
269 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest007, TestSize.Level0)
270 {
271 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest007: start.");
272 int32_t isolateNum = 2;
273 int32_t ret = g_thermalInterface->IsolateCpu(isolateNum);
274
275 char path[MAX_PATH] = {0};
276 char valueBuf[MAX_PATH] = {0};
277
278 ASSERT_FALSE(snprintf_s(path, MAX_PATH, sizeof(path) - 1, ISOLATE_PATH.c_str()) < EOK);
279
280 ret = HdfThermalHdiTest::ReadFile(path, valueBuf, sizeof(valueBuf));
281
282 if (ret == HDF_SUCCESS) {
283 std::string isolateNumStr = valueBuf;
284 int32_t value = HdfThermalHdiTest::ConvertInt(isolateNumStr);
285 THERMAL_HILOGD(LABEL_TEST, "isolate cpu num is %{public}d", value);
286 EXPECT_EQ(value, isolateNum) << "HdfThermalHdiTest007 failed";
287 }
288 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest007: return.");
289 }
290
291 /**
292 * @tc.name: HdfThermalHdiTest008
293 * @tc.desc: register fan callback
294 * @tc.type: FUNC
295 */
296 HWTEST_F(HdfThermalHdiTest, HdfThermalHdiTest008, TestSize.Level0)
297 {
298 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest008: start.");
299 int32_t ret = g_thermalInterface->RegisterFanCallback(g_fanCallback);
300 EXPECT_EQ(0, ret) << "HdfThermalHdiTest008 failed";
301 ret = g_thermalInterface->UnregisterFanCallback();
302 EXPECT_EQ(0, ret) << "HdfThermalHdiTest008 failed";
303 THERMAL_HILOGD(LABEL_TEST, "HdfThermalHdiTest008: return.");
304 }
305 }
306