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 #include "hdi_service_test.h"
17 #include "battery_log.h"
18 #include "battery_thread_test.h"
19 #include "hdf_base.h"
20 #include "power_supply_provider.h"
21 #include <chrono>
22 #include <condition_variable>
23 #include <csignal>
24 #include <cstring>
25 #include <dirent.h>
26 #include <fcntl.h>
27 #include <fstream>
28 #include <iostream>
29 #include <memory>
30 #include <mutex>
31 #include <streambuf>
32 #include <sys/stat.h>
33 #include <thread>
34 #include <vector>
35 
36 using namespace testing::ext;
37 using namespace OHOS;
38 using namespace OHOS::HDI::Battery;
39 using namespace OHOS::HDI::Battery::V2_0;
40 using namespace std;
41 
42 namespace HdiServiceTest {
43 const std::string SYSTEM_BATTERY_PATH = "/sys/class/power_supply";
44 const std::string MOCK_BATTERY_PATH = "/data/service/el0/battery/";
45 static std::vector<std::string> g_filenodeName;
46 static std::map<std::string, std::string> g_nodeInfo;
47 const int STR_TO_LONG_LEN = 10;
48 const int NUM_ZERO = 0;
49 constexpr int32_t ERROR = -1;
50 constexpr int32_t MAX_BUFF_SIZE = 128;
51 constexpr int32_t MAX_SYSFS_SIZE = 64;
52 std::unique_ptr<PowerSupplyProvider> giver_ = nullptr;
53 
SetUpTestCase(void)54 void HdiServiceTest::SetUpTestCase(void)
55 {
56     giver_ = std::make_unique<PowerSupplyProvider>();
57     if (giver_ == nullptr) {
58         BATTERY_HILOGI(LABEL_TEST, "Failed to get PowerSupplyProvider");
59     }
60 }
61 
TearDownTestCase(void)62 void HdiServiceTest::TearDownTestCase(void) {}
63 
SetUp(void)64 void HdiServiceTest::SetUp(void) {}
65 
TearDown(void)66 void HdiServiceTest::TearDown(void) {}
67 
68 struct StringEnumMap {
69     const char* str;
70     int32_t enumVal;
71 };
72 
CreateFile(std::string path,std::string content)73 std::string CreateFile(std::string path, std::string content)
74 {
75     std::ofstream stream(path.c_str());
76     if (!stream.is_open()) {
77         BATTERY_HILOGI(LABEL_TEST, "Cannot create file");
78         return nullptr;
79     }
80     stream << content.c_str() << std::endl;
81     stream.close();
82     return path;
83 }
84 
CheckSubfolderNode(const std::string & path)85 static void CheckSubfolderNode(const std::string& path)
86 {
87     DIR* dir = nullptr;
88     struct dirent* entry = nullptr;
89     std::string batteryPath = SYSTEM_BATTERY_PATH + "/" + path;
90 
91     dir = opendir(batteryPath.c_str());
92     if (dir == nullptr) {
93         BATTERY_HILOGI(LABEL_TEST, "subfolder file is not exist.");
94         return;
95     }
96 
97     while (true) {
98         entry = readdir(dir);
99         if (entry == nullptr) {
100             break;
101         }
102 
103         if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
104             continue;
105         }
106 
107         if (entry->d_type == DT_DIR || entry->d_type == DT_LNK) {
108             continue;
109         }
110 
111         if ((strcmp(entry->d_name, "type") == 0) && (g_nodeInfo["type"] == "") &&
112             (strcasecmp(path.c_str(), "battery") != 0)) {
113             g_nodeInfo["type"] = path;
114         }
115 
116         for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
117             if ((strcmp(entry->d_name, iter->first.c_str()) == 0) && (g_nodeInfo[iter->first] == "")) {
118                 g_nodeInfo[iter->first] = path;
119             }
120         }
121     }
122     closedir(dir);
123 }
124 
TraversalBaseNode()125 static void TraversalBaseNode()
126 {
127     g_nodeInfo.insert(std::make_pair("type", ""));
128     g_nodeInfo.insert(std::make_pair("online", ""));
129     g_nodeInfo.insert(std::make_pair("current_max", ""));
130     g_nodeInfo.insert(std::make_pair("voltage_max", ""));
131     g_nodeInfo.insert(std::make_pair("capacity", ""));
132     g_nodeInfo.insert(std::make_pair("voltage_now", ""));
133     g_nodeInfo.insert(std::make_pair("temp", ""));
134     g_nodeInfo.insert(std::make_pair("health", ""));
135     g_nodeInfo.insert(std::make_pair("status", ""));
136     g_nodeInfo.insert(std::make_pair("present", ""));
137     g_nodeInfo.insert(std::make_pair("charge_counter", ""));
138     g_nodeInfo.insert(std::make_pair("technology", ""));
139     g_nodeInfo.insert(std::make_pair("charge_full", ""));
140     g_nodeInfo.insert(std::make_pair("current_avg", ""));
141     g_nodeInfo.insert(std::make_pair("current_now", ""));
142     g_nodeInfo.insert(std::make_pair("charge_now", ""));
143 
144     auto iter = g_filenodeName.begin();
145     while (iter != g_filenodeName.end()) {
146         if (*iter == "battery") {
147             CheckSubfolderNode(*iter);
148             iter = g_filenodeName.erase(iter);
149         } else {
150             iter++;
151         }
152     }
153 
154     iter = g_filenodeName.begin();
155     while (iter != g_filenodeName.end()) {
156         if (*iter == "Battery") {
157             CheckSubfolderNode(*iter);
158             iter = g_filenodeName.erase(iter);
159         } else {
160             iter++;
161         }
162     }
163 
164     for (auto it = g_filenodeName.begin(); it != g_filenodeName.end(); ++it) {
165         CheckSubfolderNode(*it);
166     }
167 }
168 
InitBaseSysfs(void)169 static int32_t InitBaseSysfs(void)
170 {
171     DIR* dir = nullptr;
172     struct dirent* entry = nullptr;
173     int32_t index = 0;
174 
175     dir = opendir(SYSTEM_BATTERY_PATH.c_str());
176     if (dir == nullptr) {
177         BATTERY_HILOGE(LABEL_TEST, "cannot open POWER_SUPPLY_BASE_PATH");
178         return HDF_ERR_IO;
179     }
180 
181     while (true) {
182         entry = readdir(dir);
183         if (entry == nullptr) {
184             break;
185         }
186 
187         if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
188             continue;
189         }
190 
191         if (entry->d_type == DT_DIR || entry->d_type == DT_LNK) {
192             BATTERY_HILOGI(LABEL_TEST, "init sysfs info of %{public}s", entry->d_name);
193             if (index >= MAX_SYSFS_SIZE) {
194                 BATTERY_HILOGE(LABEL_TEST, "too many plugged types");
195                 break;
196             }
197             g_filenodeName.emplace_back(entry->d_name);
198             index++;
199         }
200     }
201 
202     TraversalBaseNode();
203     BATTERY_HILOGI(LABEL_TEST, "index is %{public}d", index);
204     closedir(dir);
205 
206     return HDF_SUCCESS;
207 }
208 
ReadTemperatureSysfs()209 static int32_t ReadTemperatureSysfs()
210 {
211     int strlen = 10;
212     char buf[128] = {0};
213     int32_t readSize;
214     InitBaseSysfs();
215     std::string tempNode = "battery";
216     for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
217         if (iter->first == "temp") {
218             tempNode = iter->second;
219             break;
220         }
221     }
222     std::string sysBattTemPath = SYSTEM_BATTERY_PATH + "/" + tempNode + "/" + "temp";
223 
224     int fd = open(sysBattTemPath.c_str(), O_RDONLY);
225     if (fd < NUM_ZERO) {
226         BATTERY_HILOGE(LABEL_TEST, "failed to open TemperatureSysfs");
227         return HDF_FAILURE;
228     }
229 
230     readSize = read(fd, buf, sizeof(buf) - 1);
231     if (readSize < NUM_ZERO) {
232         BATTERY_HILOGE(LABEL_TEST, "failed to read TemperatureSysfs");
233         close(fd);
234         return HDF_FAILURE;
235     }
236 
237     buf[readSize] = '\0';
238     int32_t battTemperature = strtol(buf, nullptr, strlen);
239     if (battTemperature < NUM_ZERO) {
240         BATTERY_HILOGE(LABEL_TEST, "read system file temperature is %{public}d", battTemperature);
241     }
242     BATTERY_HILOGE(LABEL_TEST, "read system file temperature is %{public}d", battTemperature);
243     close(fd);
244     return battTemperature;
245 }
246 
ReadVoltageSysfs()247 static int32_t ReadVoltageSysfs()
248 {
249     int strlen = 10;
250     char buf[128] = {0};
251     int32_t readSize;
252     std::string voltageNode = "battery";
253     for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
254         if (iter->first == "voltage_now") {
255             voltageNode = iter->second;
256             break;
257         }
258     }
259     std::string sysBattVolPath = SYSTEM_BATTERY_PATH + "/" + voltageNode + "/" + "voltage_now";
260 
261     int fd = open(sysBattVolPath.c_str(), O_RDONLY);
262     if (fd < NUM_ZERO) {
263         BATTERY_HILOGE(LABEL_TEST, "failed to open VoltageSysfs");
264         return HDF_FAILURE;
265     }
266 
267     readSize = read(fd, buf, sizeof(buf) - 1);
268     if (readSize < HDF_SUCCESS) {
269         BATTERY_HILOGE(LABEL_TEST, "failed to read VoltageSysfs");
270         close(fd);
271         return HDF_FAILURE;
272     }
273 
274     buf[readSize] = '\0';
275     int32_t battVoltage = strtol(buf, nullptr, strlen);
276     if (battVoltage < NUM_ZERO) {
277         BATTERY_HILOGE(LABEL_TEST, "read system file voltage is %{public}d", battVoltage);
278     }
279     BATTERY_HILOGE(LABEL_TEST, "read system file voltage is %{public}d", battVoltage);
280     close(fd);
281     return battVoltage;
282 }
283 
ReadCapacitySysfs()284 static int32_t ReadCapacitySysfs()
285 {
286     int strlen = 10;
287     char buf[128] = {0};
288     int32_t readSize;
289     std::string capacityNode = "battery";
290     for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
291         if (iter->first == "capacity") {
292             capacityNode = iter->second;
293             break;
294         }
295     }
296     std::string sysBattCapPath = SYSTEM_BATTERY_PATH + "/" + capacityNode + "/" + "capacity";
297 
298     int fd = open(sysBattCapPath.c_str(), O_RDONLY);
299     if (fd < NUM_ZERO) {
300         BATTERY_HILOGE(LABEL_TEST, "failed to open CapacitySysfs");
301         return HDF_FAILURE;
302     }
303 
304     readSize = read(fd, buf, sizeof(buf) - 1);
305     if (readSize < NUM_ZERO) {
306         BATTERY_HILOGE(LABEL_TEST, "failed to read CapacitySysfs");
307         close(fd);
308         return HDF_FAILURE;
309     }
310 
311     buf[readSize] = '\0';
312     int32_t battCapacity = strtol(buf, nullptr, strlen);
313     if (battCapacity < NUM_ZERO) {
314         BATTERY_HILOGE(LABEL_TEST, "read system file capacity is %{public}d", battCapacity);
315     }
316     BATTERY_HILOGE(LABEL_TEST, "read system file capacity is %{public}d", battCapacity);
317     close(fd);
318     return battCapacity;
319 }
320 
ReadTotalEnergySysfs()321 static int32_t ReadTotalEnergySysfs()
322 {
323     int strlen = 10;
324     char buf[128] = {0};
325     int32_t readSize;
326     InitBaseSysfs();
327     std::string totalEnergyNode = "battery";
328     for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
329         if (iter->first == "charge_full") {
330             totalEnergyNode = iter->second;
331             break;
332         }
333     }
334     std::string sysBattTotalEnergyPath = SYSTEM_BATTERY_PATH + "/" + totalEnergyNode + "/" + "charge_full";
335 
336     int fd = open(sysBattTotalEnergyPath.c_str(), O_RDONLY);
337     if (fd < NUM_ZERO) {
338         BATTERY_HILOGE(LABEL_TEST, "failed to open TotalEnergySysfs");
339         return HDF_FAILURE;
340     }
341 
342     readSize = read(fd, buf, sizeof(buf) - 1);
343     if (readSize < NUM_ZERO) {
344         BATTERY_HILOGE(LABEL_TEST, "failed to read TotalEnergySysfs");
345         close(fd);
346         return HDF_FAILURE;
347     }
348 
349     buf[readSize] = '\0';
350     int32_t totalEnergy = strtol(buf, nullptr, strlen);
351     if (totalEnergy < NUM_ZERO) {
352         BATTERY_HILOGE(LABEL_TEST, "read system file totalEnergy is %{public}d", totalEnergy);
353     }
354     BATTERY_HILOGE(LABEL_TEST, "read system file totalEnergy is %{public}d", totalEnergy);
355     close(fd);
356     return totalEnergy;
357 }
358 
ReadCurrentAverageSysfs()359 static int32_t ReadCurrentAverageSysfs()
360 {
361     int strlen = 10;
362     char buf[128] = {0};
363     int32_t readSize;
364     InitBaseSysfs();
365     std::string currentAvgNode = "battery";
366     for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
367         if (iter->first == "current_avg") {
368             currentAvgNode = iter->second;
369             break;
370         }
371     }
372     std::string sysBattCurrentAvgPath = SYSTEM_BATTERY_PATH + "/" + currentAvgNode + "/" + "current_avg";
373 
374     int fd = open(sysBattCurrentAvgPath.c_str(), O_RDONLY);
375     if (fd < NUM_ZERO) {
376         BATTERY_HILOGE(LABEL_TEST, "failed to open CurrentAverageSysfs");
377         return HDF_FAILURE;
378     }
379 
380     readSize = read(fd, buf, sizeof(buf) - 1);
381     if (readSize < NUM_ZERO) {
382         BATTERY_HILOGE(LABEL_TEST, "failed to read CurrentAverageSysfs");
383         close(fd);
384         return HDF_FAILURE;
385     }
386 
387     buf[readSize] = '\0';
388     int32_t currentAvg = strtol(buf, nullptr, strlen);
389     if (currentAvg < NUM_ZERO) {
390         BATTERY_HILOGE(LABEL_TEST, "read system file currentAvg is %{public}d", currentAvg);
391     }
392     BATTERY_HILOGE(LABEL_TEST, "read system file currentAvg is %{public}d", currentAvg);
393     close(fd);
394     return currentAvg;
395 }
396 
ReadCurrentNowSysfs()397 static int32_t ReadCurrentNowSysfs()
398 {
399     int strlen = 10;
400     char buf[128] = {0};
401     int32_t readSize;
402     InitBaseSysfs();
403     std::string currentNowNode = "battery";
404     for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
405         if (iter->first == "current_now") {
406             currentNowNode = iter->second;
407             break;
408         }
409     }
410     std::string sysBattCurrentNowPath = SYSTEM_BATTERY_PATH + "/" + currentNowNode + "/" + "current_now";
411 
412     int fd = open(sysBattCurrentNowPath.c_str(), O_RDONLY);
413     if (fd < NUM_ZERO) {
414         BATTERY_HILOGE(LABEL_TEST, "failed to open CurrentNowSysfs");
415         return HDF_FAILURE;
416     }
417 
418     readSize = read(fd, buf, sizeof(buf) - 1);
419     if (readSize < NUM_ZERO) {
420         BATTERY_HILOGE(LABEL_TEST, "failed to read CurrentNowSysfs");
421         close(fd);
422         return HDF_FAILURE;
423     }
424 
425     buf[readSize] = '\0';
426     int32_t currentNow = strtol(buf, nullptr, strlen);
427     if (currentNow < NUM_ZERO) {
428         BATTERY_HILOGE(LABEL_TEST, "read system file currentNow is %{public}d", currentNow);
429     }
430     BATTERY_HILOGE(LABEL_TEST, "read system file currentNow is %{public}d", currentNow);
431     close(fd);
432     return currentNow;
433 }
434 
ReadRemainEnergySysfs()435 static int32_t ReadRemainEnergySysfs()
436 {
437     int strlen = 10;
438     char buf[128] = {0};
439     int32_t readSize;
440     InitBaseSysfs();
441     std::string chargeNowNode = "battery";
442     for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
443         if (iter->first == "charge_now") {
444             chargeNowNode = iter->second;
445             break;
446         }
447     }
448     std::string sysBattChargeNowPath = SYSTEM_BATTERY_PATH + "/" + chargeNowNode + "/" + "charge_now";
449 
450     int fd = open(sysBattChargeNowPath.c_str(), O_RDONLY);
451     if (fd < NUM_ZERO) {
452         BATTERY_HILOGE(LABEL_TEST, "failed to open RemainEnergySysfs");
453         return HDF_FAILURE;
454     }
455 
456     readSize = read(fd, buf, sizeof(buf) - 1);
457     if (readSize < NUM_ZERO) {
458         BATTERY_HILOGE(LABEL_TEST, "failed to read RemainEnergySysfs");
459         close(fd);
460         return HDF_FAILURE;
461     }
462 
463     buf[readSize] = '\0';
464     int32_t chargeNow = strtol(buf, nullptr, strlen);
465     if (chargeNow < NUM_ZERO) {
466         BATTERY_HILOGE(LABEL_TEST, "read system file chargeNow is %{public}d", chargeNow);
467     }
468     BATTERY_HILOGE(LABEL_TEST, "read system file chargeNow is %{public}d", chargeNow);
469     close(fd);
470     return chargeNow;
471 }
472 
Trim(char * str)473 static void Trim(char* str)
474 {
475     if (str == nullptr) {
476         return;
477     }
478     str[strcspn(str, "\n")] = 0;
479 }
480 
HealthStateEnumConverter(const char * str)481 static int32_t HealthStateEnumConverter(const char* str)
482 {
483     struct StringEnumMap healthStateEnumMap[] = {
484         {"Good",                PowerSupplyProvider::BATTERY_HEALTH_GOOD       },
485         {"Cold",                PowerSupplyProvider::BATTERY_HEALTH_COLD       },
486         {"Warm",                PowerSupplyProvider::BATTERY_HEALTH_GOOD       }, // JEITA specification
487         {"Cool",                PowerSupplyProvider::BATTERY_HEALTH_GOOD       }, // JEITA specification
488         {"Hot",                 PowerSupplyProvider::BATTERY_HEALTH_OVERHEAT   }, // JEITA specification
489         {"Overheat",            PowerSupplyProvider::BATTERY_HEALTH_OVERHEAT   },
490         {"Over voltage",        PowerSupplyProvider::BATTERY_HEALTH_OVERVOLTAGE},
491         {"Dead",                PowerSupplyProvider::BATTERY_HEALTH_DEAD       },
492         {"Unknown",             PowerSupplyProvider::BATTERY_HEALTH_UNKNOWN    },
493         {"Unspecified failure", PowerSupplyProvider::BATTERY_HEALTH_UNKNOWN    },
494         {NULL,                  PowerSupplyProvider::BATTERY_HEALTH_UNKNOWN    },
495     };
496 
497     for (int i = 0; healthStateEnumMap[i].str; ++i) {
498         if (strcmp(str, healthStateEnumMap[i].str) == 0) {
499             return healthStateEnumMap[i].enumVal;
500         }
501     }
502 
503     return PowerSupplyProvider::BATTERY_HEALTH_UNKNOWN;
504 }
505 
ReadHealthStateSysfs()506 static int32_t ReadHealthStateSysfs()
507 {
508     char buf[128] = {0};
509     int32_t readSize;
510     std::string healthNode = "battery";
511     for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
512         if (iter->first == "health") {
513             healthNode = iter->second;
514             break;
515         }
516     }
517     std::string sysHealthStatePath = SYSTEM_BATTERY_PATH + "/" + healthNode + "/" + "health";
518 
519     int fd = open(sysHealthStatePath.c_str(), O_RDONLY);
520     if (fd < NUM_ZERO) {
521         BATTERY_HILOGE(LABEL_TEST, "failed to open HealthStateSysfs");
522         return HDF_FAILURE;
523     }
524 
525     readSize = read(fd, buf, sizeof(buf) - 1);
526     if (readSize < NUM_ZERO) {
527         BATTERY_HILOGE(LABEL_TEST, "failed to read HealthStateSysfs");
528         close(fd);
529         return HDF_FAILURE;
530     }
531 
532     Trim(buf);
533 
534     int32_t battHealthState = HealthStateEnumConverter(buf);
535     BATTERY_HILOGE(LABEL_TEST, "read system file healthState is %{public}d", battHealthState);
536     close(fd);
537     return battHealthState;
538 }
539 
PluggedTypeEnumConverter(const char * str)540 static int32_t PluggedTypeEnumConverter(const char* str)
541 {
542     struct StringEnumMap pluggedTypeEnumMap[] = {
543         {"USB",        PowerSupplyProvider::PLUGGED_TYPE_USB     },
544         {"USB_PD_DRP", PowerSupplyProvider::PLUGGED_TYPE_USB     },
545         {"Wireless",   PowerSupplyProvider::PLUGGED_TYPE_WIRELESS},
546         {"Mains",      PowerSupplyProvider::PLUGGED_TYPE_AC      },
547         {"UPS",        PowerSupplyProvider::PLUGGED_TYPE_AC      },
548         {"USB_ACA",    PowerSupplyProvider::PLUGGED_TYPE_AC      },
549         {"USB_C",      PowerSupplyProvider::PLUGGED_TYPE_AC      },
550         {"USB_CDP",    PowerSupplyProvider::PLUGGED_TYPE_AC      },
551         {"USB_DCP",    PowerSupplyProvider::PLUGGED_TYPE_AC      },
552         {"USB_HVDCP",  PowerSupplyProvider::PLUGGED_TYPE_AC      },
553         {"USB_PD",     PowerSupplyProvider::PLUGGED_TYPE_AC      },
554         {"Unknown",    PowerSupplyProvider::PLUGGED_TYPE_BUTT    },
555         {NULL,         PowerSupplyProvider::PLUGGED_TYPE_BUTT    },
556     };
557 
558     for (int i = 0; pluggedTypeEnumMap[i].str; ++i) {
559         if (strcmp(str, pluggedTypeEnumMap[i].str) == 0) {
560             return pluggedTypeEnumMap[i].enumVal;
561         }
562     }
563 
564     return PowerSupplyProvider::PLUGGED_TYPE_BUTT;
565 }
566 
ReadSysfsFile(const char * path,char * buf,size_t size)567 int32_t ReadSysfsFile(const char* path, char* buf, size_t size)
568 {
569     int fd = open(path, O_RDONLY, S_IRUSR | S_IRGRP | S_IROTH);
570     if (fd < NUM_ZERO) {
571         BATTERY_HILOGE(LABEL_TEST, "failed to open file");
572         return HDF_ERR_IO;
573     }
574 
575     int32_t readSize = read(fd, buf, size - 1);
576     if (readSize < NUM_ZERO) {
577         BATTERY_HILOGE(LABEL_TEST, "failed to read file");
578         close(fd);
579         return HDF_ERR_IO;
580     }
581 
582     buf[readSize] = '\0';
583     Trim(buf);
584     close(fd);
585 
586     return HDF_SUCCESS;
587 }
588 
ReadPluggedTypeSysfs()589 static int32_t ReadPluggedTypeSysfs()
590 {
591     std::string node = "USB";
592     int32_t online = ERROR;
593     char buf[MAX_BUFF_SIZE] = {0};
594 
595     auto iter = g_filenodeName.begin();
596     while (iter != g_filenodeName.end()) {
597         if (strcasecmp(iter->c_str(), "battery") == 0) {
598             continue;
599         }
600         std::string onlinePath = SYSTEM_BATTERY_PATH + "/" + *iter + "/" + "online";
601         if (ReadSysfsFile(onlinePath.c_str(), buf, MAX_BUFF_SIZE) != HDF_SUCCESS) {
602             BATTERY_HILOGW(LABEL_TEST, "read online path failed in loop");
603         }
604         online = strtol(buf, nullptr, STR_TO_LONG_LEN);
605         if (online) {
606             node = *iter;
607             break;
608         }
609         iter++;
610     }
611     if (online == ERROR) {
612         return ERROR;
613     }
614     std::string typePath = SYSTEM_BATTERY_PATH + "/" + node + "/" + "type";
615     if (ReadSysfsFile(typePath.c_str(), buf, MAX_BUFF_SIZE) != HDF_SUCCESS) {
616         BATTERY_HILOGI(LABEL_TEST, "read type path failed");
617         return ERROR;
618     }
619     Trim(buf);
620     return PluggedTypeEnumConverter(buf);
621 }
622 
ChargeStateEnumConverter(const char * str)623 int32_t ChargeStateEnumConverter(const char* str)
624 {
625     struct StringEnumMap chargeStateEnumMap[] = {
626         {"Discharging",  PowerSupplyProvider::CHARGE_STATE_NONE    },
627         {"Charging",     PowerSupplyProvider::CHARGE_STATE_ENABLE  },
628         {"Full",         PowerSupplyProvider::CHARGE_STATE_FULL    },
629         {"Not charging", PowerSupplyProvider::CHARGE_STATE_DISABLE },
630         {"Unknown",      PowerSupplyProvider::CHARGE_STATE_RESERVED},
631         {NULL,           PowerSupplyProvider::CHARGE_STATE_RESERVED},
632     };
633 
634     for (int i = 0; chargeStateEnumMap[i].str; ++i) {
635         if (strcmp(str, chargeStateEnumMap[i].str) == 0) {
636             return chargeStateEnumMap[i].enumVal;
637         }
638     }
639     return PowerSupplyProvider::CHARGE_STATE_RESERVED;
640 }
641 
ReadChargeStateSysfs()642 static int32_t ReadChargeStateSysfs()
643 {
644     char buf[128] = {0};
645     int32_t readSize;
646     std::string statusNode = "battery";
647     for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
648         if (iter->first == "status") {
649             statusNode = iter->second;
650             break;
651         }
652     }
653     std::string sysChargeStatePath = SYSTEM_BATTERY_PATH + "/" + statusNode + "/" + "status";
654 
655     int fd = open(sysChargeStatePath.c_str(), O_RDONLY);
656     if (fd < NUM_ZERO) {
657         BATTERY_HILOGE(LABEL_TEST, "failed to open ChargeStateSysfs");
658         return HDF_FAILURE;
659     }
660 
661     readSize = read(fd, buf, sizeof(buf) - 1);
662     if (readSize < NUM_ZERO) {
663         BATTERY_HILOGE(LABEL_TEST, "failed to read ChargeStateSysfs");
664         close(fd);
665         return HDF_FAILURE;
666     }
667 
668     Trim(buf);
669     int32_t battChargeState = ChargeStateEnumConverter(buf);
670     BATTERY_HILOGE(LABEL_TEST, "read system file chargeState is %{public}d", battChargeState);
671     close(fd);
672 
673     return battChargeState;
674 }
675 
ReadChargeCounterSysfs()676 static int32_t ReadChargeCounterSysfs()
677 {
678     int strlen = 10;
679     char buf[128] = {0};
680     int32_t readSize;
681     std::string counterNode = "battery";
682     for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
683         if (iter->first == "charge_counter") {
684             counterNode = iter->second;
685             break;
686         }
687     }
688     std::string sysChargeCounterPath = SYSTEM_BATTERY_PATH + "/" + counterNode + "/" + "charge_counter";
689 
690     int fd = open(sysChargeCounterPath.c_str(), O_RDONLY);
691     if (fd < NUM_ZERO) {
692         BATTERY_HILOGE(LABEL_TEST, "failed to open ChargeCounterSysfs");
693         return HDF_FAILURE;
694     }
695 
696     readSize = read(fd, buf, sizeof(buf) - 1);
697     if (readSize < NUM_ZERO) {
698         BATTERY_HILOGE(LABEL_TEST, "failed to read ChargeCounterSysfs");
699         close(fd);
700         return HDF_FAILURE;
701     }
702 
703     buf[readSize] = '\0';
704     int32_t battChargeCounter = strtol(buf, nullptr, strlen);
705     if (battChargeCounter < 0) {
706         BATTERY_HILOGE(LABEL_TEST, "read system file chargeState is %{public}d", battChargeCounter);
707     }
708     BATTERY_HILOGE(LABEL_TEST, "read system file chargeState is %{public}d", battChargeCounter);
709     close(fd);
710 
711     return battChargeCounter;
712 }
713 
ReadPresentSysfs()714 static int32_t ReadPresentSysfs()
715 {
716     int strlen = 10;
717     char buf[128] = {0};
718     int32_t readSize;
719     std::string presentNode = "battery";
720     for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
721         if (iter->first == "present") {
722             presentNode = iter->second;
723             break;
724         }
725     }
726     std::string sysPresentPath = SYSTEM_BATTERY_PATH + "/" + presentNode + "/" + "present";
727 
728     int fd = open(sysPresentPath.c_str(), O_RDONLY);
729     if (fd < NUM_ZERO) {
730         BATTERY_HILOGE(LABEL_TEST, "failed to open PresentSysfs");
731         return HDF_FAILURE;
732     }
733 
734     readSize = read(fd, buf, sizeof(buf) - 1);
735     if (readSize < NUM_ZERO) {
736         BATTERY_HILOGE(LABEL_TEST, "failed to read PresentSysfs");
737         close(fd);
738         return HDF_FAILURE;
739     }
740 
741     buf[readSize] = '\0';
742     int32_t battPresent = strtol(buf, nullptr, strlen);
743     if (battPresent < 0) {
744         BATTERY_HILOGE(LABEL_TEST, "read system file chargeState is %{public}d", battPresent);
745     }
746     BATTERY_HILOGE(LABEL_TEST, "read system file chargeState is %{public}d", battPresent);
747     close(fd);
748     return battPresent;
749 }
750 
ReadTechnologySysfs(std::string & battTechnology)751 static std::string ReadTechnologySysfs(std::string& battTechnology)
752 {
753     char buf[128] = {0};
754     int32_t readSize;
755     std::string technologyNode = "battery";
756     for (auto iter = g_nodeInfo.begin(); iter != g_nodeInfo.end(); ++iter) {
757         if (iter->first == "technology") {
758             technologyNode = iter->second;
759             break;
760         }
761     }
762     std::string sysTechnologyPath = SYSTEM_BATTERY_PATH + "/" + technologyNode + "/" + "technology";
763 
764     int fd = open(sysTechnologyPath.c_str(), O_RDONLY);
765     if (fd < NUM_ZERO) {
766         BATTERY_HILOGE(LABEL_TEST, "failed to open TechnologySysfs");
767         return "";
768     }
769 
770     readSize = read(fd, buf, sizeof(buf) - 1);
771     if (readSize < NUM_ZERO) {
772         BATTERY_HILOGE(LABEL_TEST, "failed to read TechnologySysfs");
773         close(fd);
774         return "";
775     }
776     buf[readSize] = '\0';
777     Trim(buf);
778 
779     battTechnology.assign(buf, strlen(buf));
780     BATTERY_HILOGE(LABEL_TEST, "read system file technology is %{public}s.", battTechnology.c_str());
781     close(fd);
782     return battTechnology;
783 }
784 
IsNotMock()785 static bool IsNotMock()
786 {
787     bool rootExist = access(SYSTEM_BATTERY_PATH.c_str(), F_OK) == 0;
788     bool lowerExist = access((SYSTEM_BATTERY_PATH + "/battery").c_str(), F_OK) == 0;
789     bool upperExist = access((SYSTEM_BATTERY_PATH + "/Battery").c_str(), F_OK) == 0;
790     return rootExist && (lowerExist || upperExist);
791 }
792 
793 /**
794  * @tc.name: ProviderIsNotNull
795  * @tc.desc: Test functions of PowerSupplyProvider
796  * @tc.type: FUNC
797  */
798 HWTEST_F(HdiServiceTest, ProviderIsNotNull, TestSize.Level1)
799 {
800     ASSERT_TRUE(giver_ != nullptr);
801     if (!IsNotMock()) {
802         giver_->SetSysFilePath(MOCK_BATTERY_PATH);
803         BATTERY_HILOGI(LABEL_TEST, "Is mock test");
804     }
805     giver_->InitPowerSupplySysfs();
806 }
807 
808 /**
809  * @tc.name: HdiService001
810  * @tc.desc: Test functions of ParseTemperature
811  * @tc.type: FUNC
812  */
813 HWTEST_F(HdiServiceTest, HdiService001, TestSize.Level1)
814 {
815     int32_t temperature = 0;
816     if (IsNotMock()) {
817         giver_->ParseTemperature(&temperature);
818         int32_t sysfsTemp = ReadTemperatureSysfs();
819         BATTERY_HILOGI(
820             LABEL_TEST, "Not Mock HdiService001::temperature=%{public}d, t=%{public}d", temperature, sysfsTemp);
821         ASSERT_TRUE(temperature == sysfsTemp);
822     } else {
823         CreateFile(MOCK_BATTERY_PATH + "battery/temp", "567");
824         giver_->ParseTemperature(&temperature);
825         BATTERY_HILOGI(LABEL_TEST, "HdiService001::temperature=%{public}d.", temperature);
826         ASSERT_TRUE(temperature == 567);
827     }
828 }
829 
830 /**
831  * @tc.name: HdiService002
832  * @tc.desc: Test functions of ParseVoltage
833  * @tc.type: FUNC
834  */
835 HWTEST_F(HdiServiceTest, HdiService002, TestSize.Level1)
836 {
837     int32_t voltage = 0;
838     if (IsNotMock()) {
839         giver_->ParseVoltage(&voltage);
840         int32_t sysfsVoltage = ReadVoltageSysfs();
841         BATTERY_HILOGI(LABEL_TEST, "Not Mock HdiService002::voltage=%{public}d, v=%{public}d", voltage, sysfsVoltage);
842         ASSERT_TRUE(voltage == sysfsVoltage);
843     } else {
844         CreateFile(MOCK_BATTERY_PATH + "battery/voltage_avg", "4123456");
845         CreateFile(MOCK_BATTERY_PATH + "battery/voltage_now", "4123456");
846         giver_->ParseVoltage(&voltage);
847         BATTERY_HILOGI(LABEL_TEST, "Not Mock HdiService002::voltage=%{public}d", voltage);
848         ASSERT_TRUE(voltage == 4123456);
849     }
850 }
851 
852 /**
853  * @tc.name: HdiService003
854  * @tc.desc: Test functions of ParseCapacity
855  * @tc.type: FUNC
856  */
857 HWTEST_F(HdiServiceTest, HdiService003, TestSize.Level1)
858 {
859     int32_t capacity = -1;
860     if (IsNotMock()) {
861         giver_->ParseCapacity(&capacity);
862         int32_t sysfsCapacity = ReadCapacitySysfs();
863         BATTERY_HILOGI(
864             LABEL_TEST, "Not Mcok HdiService003::capacity=%{public}d, l=%{public}d", capacity, sysfsCapacity);
865         ASSERT_TRUE(capacity == sysfsCapacity);
866     } else {
867         CreateFile(MOCK_BATTERY_PATH + "battery/capacity", "11");
868         giver_->ParseCapacity(&capacity);
869         BATTERY_HILOGI(LABEL_TEST, "HdiService003::capacity=%{public}d", capacity);
870         ASSERT_TRUE(capacity == 11);
871     }
872 }
873 
874 /**
875  * @tc.name: HdiService004
876  * @tc.desc: Test functions of ParseHealthState
877  * @tc.type: FUNC
878  */
879 HWTEST_F(HdiServiceTest, HdiService004, TestSize.Level1)
880 {
881     int32_t healthState = -1;
882     if (IsNotMock()) {
883         giver_->ParseHealthState(&healthState);
884         int32_t sysfsHealthState = ReadHealthStateSysfs();
885         BATTERY_HILOGI(
886             LABEL_TEST, "Not Mock HdiService004::healthState=%{public}d, h=%{public}d", healthState, sysfsHealthState);
887         ASSERT_TRUE(healthState == sysfsHealthState);
888     } else {
889         CreateFile(MOCK_BATTERY_PATH + "battery/health", "Good");
890         giver_->ParseHealthState(&healthState);
891         BATTERY_HILOGI(LABEL_TEST, "HdiService004::healthState=%{public}d.", healthState);
892         ASSERT_TRUE(PowerSupplyProvider::BatteryHealthState(healthState) ==
893             PowerSupplyProvider::BatteryHealthState::BATTERY_HEALTH_GOOD);
894     }
895 }
896 
897 /**
898  * @tc.name: HdiService005
899  * @tc.desc: Test functions of ParsePluggedType
900  * @tc.type: FUNC
901  */
902 HWTEST_F(HdiServiceTest, HdiService005, TestSize.Level1)
903 {
904     int32_t pluggedType = PowerSupplyProvider::PLUGGED_TYPE_NONE;
905     if (IsNotMock()) {
906         giver_->ParsePluggedType(&pluggedType);
907         int32_t sysfsPluggedType = ReadPluggedTypeSysfs();
908         BATTERY_HILOGI(
909             LABEL_TEST, "Not Mock HdiService005::pluggedType=%{public}d, p=%{public}d", pluggedType, sysfsPluggedType);
910         ASSERT_TRUE(pluggedType == sysfsPluggedType);
911     } else {
912         CreateFile(MOCK_BATTERY_PATH + "ohos_charger/online", "1");
913         CreateFile(MOCK_BATTERY_PATH + "ohos_charger/type", "Wireless");
914         giver_->ParsePluggedType(&pluggedType);
915         BATTERY_HILOGI(LABEL_TEST, "HdiService005::pluggedType=%{public}d.", pluggedType);
916         ASSERT_TRUE(PowerSupplyProvider::BatteryPluggedType(pluggedType) ==
917             PowerSupplyProvider::BatteryPluggedType::PLUGGED_TYPE_WIRELESS);
918     }
919 }
920 
921 /**
922  * @tc.name: HdiService006
923  * @tc.desc: Test functions of ParseChargeState
924  * @tc.type: FUNC
925  */
926 HWTEST_F(HdiServiceTest, HdiService006, TestSize.Level1)
927 {
928     int32_t chargeState = PowerSupplyProvider::CHARGE_STATE_RESERVED;
929     if (IsNotMock()) {
930         giver_->ParseChargeState(&chargeState);
931         int32_t sysfsChargeState = ReadChargeStateSysfs();
932         BATTERY_HILOGI(
933             LABEL_TEST, "Not Mock HdiService006::chargeState=%{public}d, cs=%{public}d", chargeState, sysfsChargeState);
934         ASSERT_TRUE(chargeState == sysfsChargeState);
935     } else {
936         CreateFile(MOCK_BATTERY_PATH + "battery/status", "Not charging");
937         giver_->ParseChargeState(&chargeState);
938         BATTERY_HILOGI(LABEL_TEST, "HdiService006::chargeState=%{public}d.", chargeState);
939         ASSERT_TRUE(PowerSupplyProvider::BatteryChargeState(chargeState) ==
940             PowerSupplyProvider::BatteryChargeState::CHARGE_STATE_DISABLE);
941     }
942 }
943 
944 /**
945  * @tc.name: HdiService007
946  * @tc.desc: Test functions of ParseChargeCounter
947  * @tc.type: FUNC
948  */
949 HWTEST_F(HdiServiceTest, HdiService007, TestSize.Level1)
950 {
951     int32_t chargeCounter = -1;
952     if (IsNotMock()) {
953         giver_->ParseChargeCounter(&chargeCounter);
954         int32_t sysfsChargeCounter = ReadChargeCounterSysfs();
955         BATTERY_HILOGI(LABEL_TEST, "Not Mcok HdiService007::chargeCounter=%{public}d, cc=%{public}d", chargeCounter,
956             sysfsChargeCounter);
957         ASSERT_TRUE(chargeCounter == sysfsChargeCounter);
958     } else {
959         CreateFile(MOCK_BATTERY_PATH + "battery/charge_counter", "12345");
960         giver_->ParseChargeCounter(&chargeCounter);
961         BATTERY_HILOGI(LABEL_TEST, "HdiService007::chargeCounter=%{public}d.", chargeCounter);
962         ASSERT_TRUE(chargeCounter == 12345);
963     }
964 }
965 
966 /**
967  * @tc.name: HdiService008
968  * @tc.desc: Test functions of ParsePresent
969  * @tc.type: FUNC
970  */
971 HWTEST_F(HdiServiceTest, HdiService008, TestSize.Level1)
972 {
973     int8_t present = -1;
974     if (IsNotMock()) {
975         giver_->ParsePresent(&present);
976         int32_t sysfsPresent = ReadPresentSysfs();
977         BATTERY_HILOGI(LABEL_TEST, "Not Mock HdiService008::present=%{public}d, p=%{public}d", present, sysfsPresent);
978         ASSERT_TRUE(present == sysfsPresent);
979     } else {
980         CreateFile(MOCK_BATTERY_PATH + "battery/present", "1");
981         giver_->ParsePresent(&present);
982         BATTERY_HILOGI(LABEL_TEST, "HdiService008::present=%{public}d.", present);
983         ASSERT_TRUE(present == 1);
984     }
985 }
986 
987 /**
988  * @tc.name: HdiService009
989  * @tc.desc: Test functions to get value of technology
990  * @tc.type: FUNC
991  */
992 HWTEST_F(HdiServiceTest, HdiService009, TestSize.Level1)
993 {
994     std::string technology = "invalid";
995     if (IsNotMock()) {
996         giver_->ParseTechnology(technology);
997         std::string sysfsTechnology = "";
998         ReadTechnologySysfs(sysfsTechnology);
999         BATTERY_HILOGI(LABEL_TEST, "HdiService009::technology=%{public}s, ty=%{public}s", technology.c_str(),
1000             sysfsTechnology.c_str());
1001         ASSERT_TRUE(technology == sysfsTechnology);
1002     } else {
1003         CreateFile(MOCK_BATTERY_PATH + "ohos-fgu/technology", "Li");
1004         giver_->ParseTechnology(technology);
1005         BATTERY_HILOGI(LABEL_TEST, "HdiService009::technology=%{public}s.", technology.c_str());
1006         ASSERT_TRUE(technology == "Li");
1007     }
1008 }
1009 
1010 /**
1011  * @tc.name: HdiService010
1012  * @tc.desc: Test functions to get fd of socket
1013  * @tc.type: FUNC
1014  */
1015 HWTEST_F(HdiServiceTest, HdiService010, TestSize.Level1)
1016 {
1017     using namespace OHOS::HDI::Battery::V2_0;
1018 
1019     BatteryThread bt;
1020     auto fd = OpenUeventSocketTest(bt);
1021     BATTERY_HILOGI(LABEL_TEST, "HdiService010::fd=%{public}d.", fd);
1022 
1023     ASSERT_TRUE(fd > 0);
1024     close(fd);
1025 }
1026 
1027 /**
1028  * @tc.name: HdiService011
1029  * @tc.desc: Test functions UpdateEpollInterval when charge-online
1030  * @tc.type: FUNC
1031  */
1032 HWTEST_F(HdiServiceTest, HdiService011, TestSize.Level1)
1033 {
1034     const int32_t CHARGE_STATE_ENABLE = 1;
1035     BatteryThread bt;
1036 
1037     UpdateEpollIntervalTest(CHARGE_STATE_ENABLE, bt);
1038     auto epollInterval = GetEpollIntervalTest(bt);
1039     BATTERY_HILOGI(LABEL_TEST, "HdiService011::epollInterval=%{public}d.", epollInterval);
1040 
1041     ASSERT_TRUE(epollInterval == 2000);
1042 }
1043 
1044 /**
1045  * @tc.name: HdiService012
1046  * @tc.desc: Test functions UpdateEpollInterval when charge-offline
1047  * @tc.type: FUNC
1048  */
1049 HWTEST_F(HdiServiceTest, HdiService012, TestSize.Level1)
1050 {
1051     const int32_t CHARGE_STATE_NONE = 0;
1052     BatteryThread bt;
1053 
1054     UpdateEpollIntervalTest(CHARGE_STATE_NONE, bt);
1055     auto epollInterval = GetEpollIntervalTest(bt);
1056     BATTERY_HILOGI(LABEL_TEST, "HdiService012::epollInterval=%{public}d.", epollInterval);
1057 
1058     ASSERT_TRUE(epollInterval == -1);
1059 }
1060 
1061 /**
1062  * @tc.name: HdiService013
1063  * @tc.desc: Test functions Init
1064  * @tc.type: FUNC
1065  */
1066 HWTEST_F(HdiServiceTest, HdiService013, TestSize.Level1)
1067 {
1068     void* service = nullptr;
1069     BatteryThread bt;
1070 
1071     InitTest(service, bt);
1072     BATTERY_HILOGI(LABEL_TEST, "HdiService013::InitTest success");
1073     auto epollFd = GetEpollFdTest(bt);
1074     BATTERY_HILOGI(LABEL_TEST, "HdiService013::epollFd=%{public}d", epollFd);
1075 
1076     ASSERT_TRUE(epollFd > 0);
1077 }
1078 
1079 /**
1080  * @tc.name: HdiService023
1081  * @tc.desc: Test functions of ParseTotalEnergy
1082  * @tc.type: FUNC
1083  */
1084 HWTEST_F(HdiServiceTest, HdiService023, TestSize.Level1)
1085 {
1086     int32_t totalEnergy = 0;
1087     if (IsNotMock()) {
1088         giver_->ParseTotalEnergy(&totalEnergy);
1089         int32_t sysfsTotalEnergy = ReadTotalEnergySysfs();
1090         BATTERY_HILOGI(
1091             LABEL_TEST, "Not Mock HdiService023::totalEnergy=%{public}d, t=%{public}d", totalEnergy, sysfsTotalEnergy);
1092         ASSERT_TRUE(totalEnergy == sysfsTotalEnergy);
1093     } else {
1094         CreateFile(MOCK_BATTERY_PATH + "battery/charge_full", "4000000");
1095         giver_->ParseTotalEnergy(&totalEnergy);
1096         BATTERY_HILOGI(LABEL_TEST, "HdiService023::totalEnergy=%{public}d.", totalEnergy);
1097         ASSERT_TRUE(totalEnergy == 4000000);
1098     }
1099 }
1100 
1101 /**
1102  * @tc.name: HdiService024
1103  * @tc.desc: Test functions of ParseCurrentAverage
1104  * @tc.type: FUNC
1105  */
1106 HWTEST_F(HdiServiceTest, HdiService024, TestSize.Level1)
1107 {
1108     int32_t currentAvg = HDF_FAILURE;
1109     if (IsNotMock()) {
1110         giver_->ParseCurrentAverage(&currentAvg);
1111         int32_t sysfsCurrentAvg = ReadCurrentAverageSysfs();
1112         BATTERY_HILOGI(
1113             LABEL_TEST, "Not Mock HdiService024::currentAvg=%{public}d, t=%{public}d", currentAvg, sysfsCurrentAvg);
1114         ASSERT_TRUE(currentAvg == sysfsCurrentAvg);
1115     } else {
1116         CreateFile(MOCK_BATTERY_PATH + "battery/current_avg", "1000");
1117         giver_->ParseCurrentAverage(&currentAvg);
1118         BATTERY_HILOGI(LABEL_TEST, "HdiService024::currentAvg=%{public}d.", currentAvg);
1119         ASSERT_TRUE(currentAvg == 1000);
1120     }
1121 }
1122 
1123 /**
1124  * @tc.name: HdiService025
1125  * @tc.desc: Test functions of ParseCurrentNow
1126  * @tc.type: FUNC
1127  */
1128 HWTEST_F(HdiServiceTest, HdiService025, TestSize.Level1)
1129 {
1130     int32_t currentNow = 0;
1131     if (IsNotMock()) {
1132         giver_->ParseCurrentNow(&currentNow);
1133         int32_t sysfsCurrentNow = ReadCurrentNowSysfs();
1134         BATTERY_HILOGI(
1135             LABEL_TEST, "Not Mock HdiService025::currentNow=%{public}d, t=%{public}d", currentNow, sysfsCurrentNow);
1136         ASSERT_TRUE(currentNow == sysfsCurrentNow);
1137     } else {
1138         CreateFile(MOCK_BATTERY_PATH + "battery/current_now", "1000");
1139         giver_->ParseCurrentNow(&currentNow);
1140         BATTERY_HILOGI(LABEL_TEST, "HdiService025::currentNow=%{public}d.", currentNow);
1141         ASSERT_TRUE(currentNow == 1000);
1142     }
1143 }
1144 
1145 /**
1146  * @tc.name: HdiService026
1147  * @tc.desc: Test functions of ParseChargeNow
1148  * @tc.type: FUNC
1149  */
1150 HWTEST_F(HdiServiceTest, HdiService026, TestSize.Level1)
1151 {
1152     int32_t chargeNow = 0;
1153     if (IsNotMock()) {
1154         giver_->ParseRemainEnergy(&chargeNow);
1155         int32_t sysfsChargeNow = ReadRemainEnergySysfs();
1156         BATTERY_HILOGI(
1157             LABEL_TEST, "Not Mock HdiService026::chargeNow=%{public}d, t=%{public}d", chargeNow, sysfsChargeNow);
1158         ASSERT_TRUE(chargeNow == sysfsChargeNow);
1159     } else {
1160         CreateFile(MOCK_BATTERY_PATH + "battery/charge_now", "1000");
1161         giver_->ParseRemainEnergy(&chargeNow);
1162         BATTERY_HILOGI(LABEL_TEST, "HdiService026::chargeNow=%{public}d.", chargeNow);
1163         ASSERT_TRUE(chargeNow == 1000);
1164     }
1165 }
1166 } // namespace HdiServiceTest
1167