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 "thermal_test.h"
17
18 #include <cstdio>
19 #include <cstdlib>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <fstream>
23 #include <securec.h>
24 #include <sys/stat.h>
25
26 #include "errors.h"
27 #include "string_ex.h"
28 #include "thermal_log.h"
29
30 using namespace testing::ext;
31 using namespace OHOS;
32 using namespace std;
33
34 namespace OHOS {
35 namespace PowerMgr {
WriteFile(std::string path,std::string buf)36 int32_t ThermalTest::WriteFile(std::string path, std::string buf)
37 {
38 FILE *fp = fopen(path.c_str(), "w+");
39 if (fp == nullptr) {
40 return ERR_INVALID_VALUE;
41 }
42 size_t num = fwrite(buf.c_str(), sizeof(char), buf.length(), fp);
43 if (num != buf.length()) {
44 THERMAL_HILOGD(COMP_SVC, "Failed to fwrite %{public}s, num=%{public}zu", path.c_str(), num);
45 fclose(fp);
46 return ERR_INVALID_OPERATION;
47 }
48 fclose(fp);
49 return ERR_OK;
50 }
51
ReadFile(std::string path,std::string & buf)52 int32_t ThermalTest::ReadFile(std::string path, std::string& buf)
53 {
54 std::ifstream input(path);
55 if (!input.is_open()) {
56 return ERR_INVALID_VALUE;
57 }
58 getline(input, buf);
59 input.close();
60 return ERR_OK;
61 }
62
ConvertInt(const std::string & value)63 int32_t ThermalTest::ConvertInt(const std::string& value)
64 {
65 int32_t result = -1;
66 StrToInt(value, result);
67 return result;
68 }
69
InitNode()70 int32_t ThermalTest::InitNode()
71 {
72 char bufTemp[MAX_PATH] = {0};
73 int32_t ret = -1;
74 std::map<std::string, int32_t> sensor;
75 sensor["battery"] = 0;
76 sensor["charger"] = 0;
77 sensor["pa"] = 0;
78 sensor["ap"] = 0;
79 sensor["ambient"] = 0;
80 sensor["cpu"] = 0;
81 sensor["soc"] = 0;
82 sensor["shell"] = 0;
83 for (auto iter : sensor) {
84 ret = snprintf_s(bufTemp, MAX_PATH, sizeof(bufTemp) - 1, SIMULATION_TEMP_DIR, iter.first.c_str());
85 if (ret < EOK) {
86 return ret;
87 }
88 std::string temp = std::to_string(iter.second);
89 WriteFile(bufTemp, temp);
90 }
91 return ERR_OK;
92 }
93
IsMock(const std::string & path)94 bool ThermalTest::IsMock(const std::string& path)
95 {
96 struct stat pathStat;
97 int32_t ret = stat(path.c_str(), &pathStat);
98 if (ret != 0) {
99 return false;
100 }
101 if (S_ISREG(pathStat.st_mode)) {
102 return true;
103 }
104 if (!S_ISDIR(pathStat.st_mode)) {
105 return false;
106 }
107 DIR* dir = opendir(path.c_str());
108 if (dir == nullptr) {
109 return false;
110 }
111 struct dirent* ptr = nullptr;
112 while ((ptr = readdir(dir)) != nullptr) {
113 if (strcmp(".", ptr->d_name) != 0 && strcmp("..", ptr->d_name) != 0) {
114 closedir(dir);
115 return true;
116 }
117 }
118 closedir(dir);
119 return false;
120 }
121
IsVendor()122 bool ThermalTest::IsVendor()
123 {
124 if (access(VENDOR_CONFIG.c_str(), 0) != 0) {
125 return false;
126 }
127 return true;
128 }
129
GetNodeValue(const std::string & path)130 std::string ThermalTest::GetNodeValue(const std::string& path)
131 {
132 std::string value {};
133 int32_t ret = ThermalTest::ReadFile(path, value);
134 EXPECT_EQ(true, ret == ERR_OK);
135 return value;
136 }
137
SetNodeValue(int32_t value,const std::string & path)138 int32_t ThermalTest::SetNodeValue(int32_t value, const std::string& path)
139 {
140 std::string sValue = to_string(value);
141 int32_t ret = ThermalTest::WriteFile(path, sValue);
142 EXPECT_EQ(true, ret == ERR_OK);
143 return ret;
144 }
145
SetNodeString(std::string str,const std::string & path)146 int32_t ThermalTest::SetNodeString(std::string str, const std::string& path)
147 {
148 int32_t ret = ThermalTest::WriteFile(path, str);
149 EXPECT_EQ(true, ret == ERR_OK);
150 return ret;
151 }
152 } // namespace PowerMgr
153 } // namespace OHOS
154