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