1 /*
2 * Copyright (c) 2024 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 "res_sched_file_util_test.h"
17
18 #include "res_sched_file_util.h"
19
20 using namespace std;
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace ResourceSchedule {
25 namespace {
26 const std::string TEST_DIR = "/data/resschedutiltest";
27 const std::string TEST_COPY_DIR = "/data/resschedutiltest/copy";
28 const std::string TEST_FILE_PATH = "/data/resschedutiltest/test.txt";
29 const std::string TEST_COPY_FILE_PATH = "/data/resschedutiltest/copy/test_copy.txt";
30 const std::string TEST_CONFIG_DIR = "etc/ressched/res_sched_config.xml";
31 const std::string TEST_REAL_CONFIG_FILE_PATH = "/sys_prod/etc/ressched/res_sched_config.xml";
32 const std::string TEST_FILE_NAME = "test.txt";
33 const std::string TEST_CONTENT = "ressched util test";
34 const std::string DATA_DIR = "/data";
35 }
36
SetUpTestCase()37 void ResSchedFileUtilTest::SetUpTestCase() {}
38
TearDownTestCase()39 void ResSchedFileUtilTest::TearDownTestCase() {}
40
SetUp()41 void ResSchedFileUtilTest::SetUp() {}
42
TearDown()43 void ResSchedFileUtilTest::TearDown() {}
44
45 /**
46 * @tc.name: ResSchedFileUtilTest WriteFileReclaim_001
47 * @tc.desc: test WriteFileReclaim
48 * @tc.type: FUNC
49 * @tc.require: issueIB8Y0E
50 */
51 HWTEST_F(ResSchedFileUtilTest, WriteFileReclaim_001, Function | MediumTest | Level0)
52 {
53 ResCommonUtil::WriteFileReclaim(getpid());
54 std::string path = "/proc/" + std::to_string(getpid()) + "/reclaim";
55 if (ResCommonUtil::PathOrFileExists(path)) {
56 EXPECT_TRUE(open(path.c_str(), O_WRONLY) >= 0);
57 } else {
58 EXPECT_FALSE(open(path.c_str(), O_WRONLY) >= 0);
59 }
60 }
61
62 /**
63 * @tc.name: ResSchedFileUtilTest GetRealPath_001
64 * @tc.desc: test GetRealPath
65 * @tc.type: FUNC
66 * @tc.require: issueIB8Y0E
67 */
68 HWTEST_F(ResSchedFileUtilTest, GetRealPath_001, Function | MediumTest | Level0)
69 {
70 std::string path = "./";
71 std::string realPath;
72 EXPECT_TRUE(ResCommonUtil::GetRealPath(path, realPath));
73 path = "000/0/00\\00";
74 EXPECT_FALSE(ResCommonUtil::GetRealPath(path, realPath));
75 }
76
77 /**
78 * @tc.name: ResSchedFileUtilTest PathOrFileExists_001
79 * @tc.desc: test PathOrFileExists
80 * @tc.type: FUNC
81 * @tc.require: issueIB8Y0E
82 */
83 HWTEST_F(ResSchedFileUtilTest, PathOrFileExists_001, Function | MediumTest | Level0)
84 {
85 std::string path = "./";
86 EXPECT_TRUE(ResCommonUtil::PathOrFileExists(path));
87 }
88
89 /**
90 * @tc.name: ResSchedFileUtilTest DirIterator_001
91 * @tc.desc: test DirIterator
92 * @tc.type: FUNC
93 * @tc.require: issueIB8Y0E
94 */
95 HWTEST_F(ResSchedFileUtilTest, DirIterator_001, Function | MediumTest | Level0)
96 {
97 std::string path = "./";
98 std::vector<std::string> result;
99 EXPECT_TRUE(ResCommonUtil::DirIterator(path, result));
100 path = "000/0/00\\00";
101 result.clear();
102 EXPECT_FALSE(ResCommonUtil::DirIterator(path, result));
103 }
104
105 /**
106 * @tc.name: ResSchedFileUtilTest IsEmptyDir_001
107 * @tc.desc: test IsEmptyDir
108 * @tc.type: FUNC
109 * @tc.require: issueIB8Y0E
110 */
111 HWTEST_F(ResSchedFileUtilTest, IsEmptyDir_001, Function | MediumTest | Level0)
112 {
113 ResCommonUtil::CreateDir(TEST_DIR, S_IXUSR | S_IWUSR | S_IRUSR);
114 EXPECT_TRUE(ResCommonUtil::IsEmptyDir(TEST_DIR));
115 EXPECT_FALSE(ResCommonUtil::IsEmptyDir(DATA_DIR));
116 ResCommonUtil::RemoveDirs(TEST_DIR);
117 SUCCEED();
118 }
119
120 /**
121 * @tc.name: ResSchedFileUtilTest IsDir_001
122 * @tc.desc: test IsDir
123 * @tc.type: FUNC
124 * @tc.require: issueIB8Y0E
125 */
126 HWTEST_F(ResSchedFileUtilTest, IsDir_001, Function | MediumTest | Level0)
127 {
128 EXPECT_FALSE(ResCommonUtil::IsDir(TEST_DIR));
129 ResCommonUtil::CreateDir(TEST_DIR, S_IXUSR | S_IWUSR | S_IRUSR);
130 EXPECT_TRUE(ResCommonUtil::IsDir(TEST_DIR));
131 ResCommonUtil::RemoveDirs(TEST_DIR);
132 SUCCEED();
133 }
134
135 /**
136 * @tc.name: ResSchedFileUtilTest CreateDir_001
137 * @tc.desc: test CreateDir
138 * @tc.type: FUNC
139 * @tc.require: issueIB8Y0E
140 */
141 HWTEST_F(ResSchedFileUtilTest, CreateDir_001, Function | MediumTest | Level0)
142 {
143 EXPECT_TRUE(ResCommonUtil::CreateDir(TEST_DIR, S_IXUSR | S_IWUSR | S_IRUSR));
144 ResCommonUtil::RemoveDirs(TEST_DIR);
145 EXPECT_TRUE(ResCommonUtil::CreateDir(TEST_COPY_DIR, S_IXUSR | S_IWUSR | S_IRUSR));
146 }
147
148 /**
149 * @tc.name: ResSchedFileUtilTest RemoveDirs_001
150 * @tc.desc: test RemoveDirs
151 * @tc.type: FUNC
152 * @tc.require: issueIB8Y0E
153 */
154 HWTEST_F(ResSchedFileUtilTest, RemoveDirs_001, Function | MediumTest | Level0)
155 {
156 EXPECT_TRUE(ResCommonUtil::CreateDir(TEST_DIR, S_IXUSR | S_IWUSR | S_IRUSR));
157 EXPECT_TRUE(ResCommonUtil::RemoveDirs(TEST_DIR));
158 EXPECT_FALSE(ResCommonUtil::RemoveDirs(TEST_DIR));
159 }
160
161 /**
162 * @tc.name: ResSchedFileUtilTest RemoveFile_001
163 * @tc.desc: test RemoveFile
164 * @tc.type: FUNC
165 * @tc.require: issueIB8Y0E
166 */
167 HWTEST_F(ResSchedFileUtilTest, RemoveFile_001, Function | MediumTest | Level0)
168 {
169 EXPECT_TRUE(ResCommonUtil::RemoveFile(TEST_FILE_PATH));
170 ResCommonUtil::CreateDir(TEST_DIR, S_IXUSR | S_IWUSR | S_IRUSR);
171 ResCommonUtil::CreateFile(TEST_FILE_PATH, S_IXUSR | S_IWUSR | S_IRUSR);
172 EXPECT_TRUE(ResCommonUtil::RemoveFile(TEST_FILE_PATH));
173 ResCommonUtil::RemoveDirs(TEST_DIR);
174 SUCCEED();
175 }
176
177 /**
178 * @tc.name: ResSchedFileUtilTest CreateFile_001
179 * @tc.desc: test CreateFile
180 * @tc.type: FUNC
181 * @tc.require: issueIB8Y0E
182 */
183 HWTEST_F(ResSchedFileUtilTest, CreateFile_001, Function | MediumTest | Level0)
184 {
185 EXPECT_FALSE(ResCommonUtil::CreateFile(TEST_FILE_PATH, S_IXUSR | S_IWUSR | S_IRUSR));
186 ResCommonUtil::CreateDir(TEST_DIR, S_IXUSR | S_IWUSR | S_IRUSR);
187 EXPECT_TRUE(ResCommonUtil::CreateFile(TEST_FILE_PATH, S_IXUSR | S_IWUSR | S_IRUSR));
188 ResCommonUtil::RemoveFile(TEST_FILE_PATH);
189 ResCommonUtil::RemoveDirs(TEST_DIR);
190 SUCCEED();
191 }
192
193 /**
194 * @tc.name: ResSchedFileUtilTest ExtractFileName_001
195 * @tc.desc: test ExtractFileName
196 * @tc.type: FUNC
197 * @tc.require: issueIB8Y0E
198 */
199 HWTEST_F(ResSchedFileUtilTest, ExtractFileName_001, Function | MediumTest | Level0)
200 {
201 EXPECT_EQ(ResCommonUtil::ExtractFileName(TEST_FILE_PATH), TEST_FILE_NAME);
202 EXPECT_EQ(ResCommonUtil::ExtractFileName(TEST_DIR), "resschedutiltest");
203 }
204
205 /**
206 * @tc.name: ResSchedFileUtilTest IsBLKPath_001
207 * @tc.desc: test IsBLKPath
208 * @tc.type: FUNC
209 * @tc.require: issueIB8Y0E
210 */
211 HWTEST_F(ResSchedFileUtilTest, IsBLKPath_001, Function | MediumTest | Level0)
212 {
213 std::string path = "/dev/block/sda";
214 if (ResCommonUtil::PathOrFileExists(path)) {
215 EXPECT_EQ(ResCommonUtil::IsBLKPath(path), true);
216 }
217 path = "/sys/block/dm-0";
218 EXPECT_EQ(ResCommonUtil::IsBLKPath(path), false);
219 path = "";
220 EXPECT_EQ(ResCommonUtil::IsBLKPath(path), false);
221 path = "/sys/block/fo_test";
222 EXPECT_EQ(ResCommonUtil::IsBLKPath(path), false);
223 }
224
225 /**
226 * @tc.name: ResSchedFileUtilTest SaveStringToFile_001
227 * @tc.desc: test SaveStringToFile
228 * @tc.type: FUNC
229 * @tc.require: issueIB8Y0E
230 */
231 HWTEST_F(ResSchedFileUtilTest, SaveStringToFile_001, Function | MediumTest | Level0)
232 {
233 EXPECT_FALSE(ResCommonUtil::SaveStringToFile(TEST_FILE_PATH, TEST_CONTENT));
234 ResCommonUtil::CreateDir(TEST_DIR, S_IXUSR | S_IWUSR | S_IRUSR);
235 ResCommonUtil::CreateFile(TEST_FILE_PATH, S_IXUSR | S_IWUSR | S_IRUSR);
236 EXPECT_TRUE(ResCommonUtil::SaveStringToFile(TEST_FILE_PATH, TEST_CONTENT));
237 ResCommonUtil::RemoveFile(TEST_FILE_PATH);
238 ResCommonUtil::RemoveDirs(TEST_DIR);
239 SUCCEED();
240 }
241
242 /**
243 * @tc.name: ResSchedFileUtilTest ReadLinesFromFile_001
244 * @tc.desc: test ReadLinesFromFile
245 * @tc.type: FUNC
246 * @tc.require: issueIB8Y0E
247 */
248 HWTEST_F(ResSchedFileUtilTest, ReadLinesFromFile_001, Function | MediumTest | Level0)
249 {
250 ResCommonUtil::CreateDir(TEST_DIR, S_IXUSR | S_IWUSR | S_IRUSR);
251 ResCommonUtil::CreateFile(TEST_FILE_PATH, S_IXUSR | S_IWUSR | S_IRUSR);
252 std::vector<std::string> texts = {"ressched", "util", "test"};
253 std::string content = "";
254 for (auto& text : texts) {
255 ResCommonUtil::SaveStringToFile(TEST_FILE_PATH, text, false);
256 ResCommonUtil::SaveStringToFile(TEST_FILE_PATH, "\n", false);
257 }
258 std::vector<std::string> lines;
259 EXPECT_TRUE(ResCommonUtil::ReadLinesFromFile(TEST_FILE_PATH, lines));
260 for (std::size_t i = 0; i < lines.size(); ++i) {
261 EXPECT_EQ(lines[i], texts[i]);
262 }
263 ResCommonUtil::RemoveFile(TEST_FILE_PATH);
264 ResCommonUtil::RemoveDirs(TEST_DIR);
265 }
266
267 /**
268 * @tc.name: ResSchedFileUtilTest CopyFile_001
269 * @tc.desc: test CopyFile
270 * @tc.type: FUNC
271 * @tc.require: issueIB8Y0E
272 */
273 HWTEST_F(ResSchedFileUtilTest, CopyFile_001, Function | MediumTest | Level0)
274 {
275 EXPECT_FALSE(ResCommonUtil::CopyFile(TEST_FILE_PATH, TEST_COPY_FILE_PATH));
276 ResCommonUtil::CreateDir(TEST_DIR, S_IXUSR | S_IWUSR | S_IRUSR);
277 ResCommonUtil::CreateFile(TEST_FILE_PATH, S_IXUSR | S_IWUSR | S_IRUSR);
278 EXPECT_TRUE(ResCommonUtil::CopyFile(TEST_FILE_PATH, TEST_COPY_DIR));
279 ResCommonUtil::RemoveFile(TEST_FILE_PATH);
280 ResCommonUtil::RemoveFile(TEST_COPY_FILE_PATH);
281 ResCommonUtil::RemoveDirs(TEST_DIR);
282 }
283
284 /**
285 * @tc.name: ResSchedFileUtilTest GetRealConfigPath_001
286 * @tc.desc: test GetRealConfigPath
287 * @tc.type: FUNC
288 * @tc.require: issueIB8Y0E
289 */
290 HWTEST_F(ResSchedFileUtilTest, GetRealConfigPath_001, Function | MediumTest | Level0)
291 {
292 std::string configPath = "";
293 std::string realConfigPath;
294 EXPECT_FALSE(ResCommonUtil::GetRealConfigPath(configPath, realConfigPath));
295 EXPECT_TRUE(ResCommonUtil::GetRealConfigPath(TEST_CONFIG_DIR, realConfigPath));
296 }
297 } // namespace ResourceSchedule
298 } // namespace OHOS
299