1 /*
2 * Copyright (c) 2022-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 "adapter_utility_ohos_test.h"
17
18 #include <fcntl.h>
19 #include <fstream>
20 #include <string>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24 #include "ash_memory_utils.h"
25 #include "cjson_util.h"
26 #include "common_utils.h"
27 #include "file_util.h"
28 #include "freeze_json_util.h"
29 #include "hiview_config_util.h"
30 #include "hiview_db_util.h"
31 #include "hiview_zip_util.h"
32 #include "securec.h"
33 #include "socket_util.h"
34 #include "time_util.h"
35
36 using namespace std;
37
38 namespace OHOS {
39 namespace HiviewDFX {
40 namespace {
41 constexpr char LOG_FILE_PATH[] = "/data/test/adapter_utility_test/";
42 constexpr char TEST_JSON_FILE_PATH[] = "/data/test/test_data/test.json";
43 constexpr char FREEZE_JSON_FILE[] = "/data/test/test_data/0-0-123456";
44 constexpr char FREEZE_JSON_FILE_EMPTY[] = "/data/test/test_data/0-1-123456";
45 constexpr char TEST_ZIP_FILE[] = "/data/test/test_data/test_pack.zip";
46 const std::string SOURCE_PATH = "/data/test/test_data/";
47 const std::string ZIP_DES_PATH = "/data/test/test_data/zip_des/";
48 const std::string DB_PATH = "/data/test/test_data/db/";
49 const std::string UPLOAD_PATH = "/data/test/test_data/upload/";
50 constexpr char STRING_VAL[] = "OpenHarmony is a better choice for you.";
51 constexpr char STRING_ARR_FIRST_VAL[] = "3.1 release";
52 constexpr int64_t INT_VAL = 2024;
53 constexpr int64_t INT_VAL_DEFAULT = -1;
54 constexpr double DOU_VAL = 2024.0;
55 constexpr double DOU_VAL_DEFAULT = -1.0;
56 constexpr size_t TEST_ARRAY_SIZE = 2;
57 constexpr int SUFFIX_0 = 0;
58 constexpr int SUFFIX_1 = 1;
59
GetLogDir(std::string & testCaseName)60 std::string GetLogDir(std::string& testCaseName)
61 {
62 std::string workPath = std::string(LOG_FILE_PATH);
63 if (workPath.back() != '/') {
64 workPath = workPath + "/";
65 }
66 workPath.append(testCaseName);
67 workPath.append("/");
68 std::string logDestDir = workPath;
69 if (!FileUtil::FileExists(logDestDir)) {
70 FileUtil::ForceCreateDirectory(logDestDir, FileUtil::FILE_PERM_770);
71 }
72 return logDestDir;
73 }
74
GenerateLogFileName(std::string & testCaseName,int index)75 std::string GenerateLogFileName(std::string& testCaseName, int index)
76 {
77 return GetLogDir(testCaseName) + "testFile" + std::to_string(index);
78 }
79
80 struct AshMemTestStruct {
AshMemTestStructOHOS::HiviewDFX::__anonf631a0020110::AshMemTestStruct81 explicit AshMemTestStruct(std::string data) : data(data) {};
82
GetDataOHOS::HiviewDFX::__anonf631a0020110::AshMemTestStruct83 void* GetData(uint32_t& dataSize) const
84 {
85 auto dataLen = data.length() + 1;
86 char* buff = reinterpret_cast<char *>(malloc(dataLen));
87 if (buff == nullptr) {
88 return nullptr;
89 }
90 auto ret = memset_s(buff, dataLen, 0, dataLen);
91 if (ret != EOK) {
92 return nullptr;
93 }
94
95 ret = memcpy_s(buff, dataLen, data.c_str(), data.length());
96 if (ret != EOK) {
97 return nullptr;
98 }
99 dataSize = dataLen;
100 return buff;
101 }
102
ParseDataOHOS::HiviewDFX::__anonf631a0020110::AshMemTestStruct103 static AshMemTestStruct ParseData(const char* dataInput, const uint32_t dataSize)
104 {
105 if (dataInput == nullptr || dataInput[dataSize - 1] != 0) {
106 return AshMemTestStruct("");
107 }
108 return AshMemTestStruct(dataInput);
109 }
110 std::string data;
111 };
112 }
113
SetUpTestCase()114 void AdapterUtilityOhosTest::SetUpTestCase() {}
115
TearDownTestCase()116 void AdapterUtilityOhosTest::TearDownTestCase() {}
117
SetUp()118 void AdapterUtilityOhosTest::SetUp() {}
119
TearDown()120 void AdapterUtilityOhosTest::TearDown()
121 {
122 (void)FileUtil::ForceRemoveDirectory(LOG_FILE_PATH);
123 }
124
125 /**
126 * @tc.name: SocketUtilOhosTest001
127 * @tc.desc: Test GetExistingSocketServer defined in namespace SocketUtil
128 * @tc.type: FUNC
129 * @tc.require: issueI65DUW
130 */
131 HWTEST_F(AdapterUtilityOhosTest, SocketUtilOhosTest001, testing::ext::TestSize.Level3)
132 {
133 int socketFdIndex = 0;
134 auto ret = SocketUtil::GetExistingSocketServer("/dev/socket/unix/hisysevent", socketFdIndex);
135 int expectedRet = -1;
136 ASSERT_EQ(expectedRet, ret);
137 }
138
139 /**
140 * @tc.name: CommonUtilsOhosTest001
141 * @tc.desc: Test ExecCommand defined in namespace CommonUtils
142 * @tc.type: FUNC
143 * @tc.require: issueI65DUW
144 */
145 HWTEST_F(AdapterUtilityOhosTest, CommonUtilsOhosTest001, testing::ext::TestSize.Level3)
146 {
147 std::vector<std::string> cmdRet;
148 auto ret = CommonUtils::ExecCommand("hisysevent -l -m 10000", cmdRet);
149 int expectRet = 0;
150 ASSERT_EQ(expectRet, ret);
151 ret = CommonUtils::ExecCommand("", cmdRet);
152 ASSERT_EQ(expectRet, ret);
153 }
154
155 /**
156 * @tc.name: CommonUtilsOhosTest002
157 * @tc.desc: Test GetPidByName defined in namespace CommonUtils
158 * @tc.type: FUNC
159 * @tc.require: issueI65DUW
160 */
161 HWTEST_F(AdapterUtilityOhosTest, CommonUtilsOhosTest002, testing::ext::TestSize.Level3)
162 {
163 std::vector<std::string> cmdRet;
164 auto hiviewProcessId = CommonUtils::GetPidByName("hiview");
165 ASSERT_TRUE(hiviewProcessId > 0);
166 }
167
168 /**
169 * @tc.name: CommonUtilsOhosTest003
170 * @tc.desc: Test WriteCommandResultToFile defined in namespace CommonUtils
171 * @tc.type: FUNC
172 * @tc.require: issueI65DUW
173 */
174 HWTEST_F(AdapterUtilityOhosTest, CommonUtilsOhosTest003, testing::ext::TestSize.Level3)
175 {
176 std::string caseName("CommonUtilsOhosTest003");
177 std::string cmd = "";
178 auto ret = CommonUtils::WriteCommandResultToFile(0, cmd);
179 ASSERT_EQ(false, ret);
180 std::string cmd2 = "hisysevent -l -m 1 | wc -l";
181 auto fd = FileUtil::Open(GenerateLogFileName(caseName, SUFFIX_0),
182 O_CREAT | O_WRONLY | O_TRUNC, FileUtil::FILE_PERM_770);
183 ret = CommonUtils::WriteCommandResultToFile(fd, cmd2);
184 ASSERT_EQ(true, ret);
185 (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "");
186 fd = FileUtil::Open(GenerateLogFileName(caseName, SUFFIX_0),
187 O_CREAT | O_WRONLY | O_TRUNC, FileUtil::FILE_PERM_770);
188 ret = CommonUtils::WriteCommandResultToFile(fd, cmd2);
189 ASSERT_EQ(true, ret);
190 }
191
192 /**
193 * @tc.name: CommonUtilsOhosTest004
194 * @tc.desc: Test GetProcFullNameByPid defined in namespace CommonUtils
195 * @tc.type: FUNC
196 * @tc.require: issueI97MDA
197 */
198 HWTEST_F(AdapterUtilityOhosTest, CommonUtilsOhosTest004, testing::ext::TestSize.Level3)
199 {
200 auto ret = CommonUtils::GetProcFullNameByPid(1); // 1 is pid of init process
201 ASSERT_EQ(ret, "init");
202 ret = CommonUtils::GetProcFullNameByPid(-1); // -1 is a invalid pid value just for test
203 ASSERT_EQ(ret, "");
204 }
205
206 /**
207 * @tc.name: TimeUtilOhosTest001
208 * @tc.desc: Test Sleep/GetSeconds defined in namespace TimeUtil
209 * @tc.type: FUNC
210 * @tc.require: issueI65DUW
211 */
212 HWTEST_F(AdapterUtilityOhosTest, TimeUtilOhosTest001, testing::ext::TestSize.Level3)
213 {
214 auto time = TimeUtil::GetSeconds();
215 ASSERT_GE(time, 0);
216 int sleepSecs = 1;
217 TimeUtil::Sleep(sleepSecs);
218 ASSERT_TRUE(true);
219 }
220
221 /**
222 * @tc.name: TimeUtilOhosTest002
223 * @tc.desc: Test Get0ClockStampMs/GetSteadyClockTimeMs defined in namespace TimeUtil
224 * @tc.type: FUNC
225 * @tc.require: issueI65DUW
226 */
227 HWTEST_F(AdapterUtilityOhosTest, TimeUtilOhosTest002, testing::ext::TestSize.Level3)
228 {
229 auto time1 = TimeUtil::Get0ClockStampMs();
230 auto time2 = TimeUtil::GetSteadyClockTimeMs();
231 ASSERT_GE(time1, 0);
232 ASSERT_GE(time2, 0);
233 }
234
235 /**
236 * @tc.name: TimeUtilOhosTest003
237 * @tc.desc: Test GetBootTimeMs defined in namespace TimeUtil
238 * @tc.type: FUNC
239 * @tc.require: issueI9DYOQ
240 */
241 HWTEST_F(AdapterUtilityOhosTest, TimeUtilOhosTest003, testing::ext::TestSize.Level3)
242 {
243 auto time1 = TimeUtil::GetBootTimeMs();
244 ASSERT_GT(time1, 0);
245
246 TimeUtil::Sleep(1); // 1s
247
248 auto time2 = TimeUtil::GetBootTimeMs();
249 ASSERT_GT(time2, time1);
250 }
251
252 /**
253 * @tc.name: FileUtilOhosTest001
254 * @tc.desc: Test LoadBufferFromFile/SaveBufferToFile defined in namespace FileUtil
255 * @tc.type: FUNC
256 * @tc.require: issueI65DUW
257 */
258 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest001, testing::ext::TestSize.Level3)
259 {
260 std::string caseName("FileUtilOhosTest001");
261 std::vector<char> content;
262 (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "123");
263 (void)FileUtil::LoadBufferFromFile(GenerateLogFileName(caseName, SUFFIX_0), content);
264 ASSERT_TRUE(true);
265 (void)FileUtil::SaveBufferToFile(GenerateLogFileName(caseName, SUFFIX_0), content, true);
266 ASSERT_TRUE(true);
267 }
268
269 /**
270 * @tc.name: FileUtilOhosTest002
271 * @tc.desc: Test ExtractFilePath/ExtractFileName defined in namespace FileUtil
272 * @tc.type: FUNC
273 * @tc.require: issueI65DUW
274 */
275 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest002, testing::ext::TestSize.Level3)
276 {
277 std::string path = "ohos/test/123.txt";
278 auto dir = FileUtil::ExtractFilePath(path);
279 ASSERT_EQ("ohos/test/", dir);
280 auto name = FileUtil::ExtractFileName(path);
281 ASSERT_EQ("123.txt", name);
282 }
283
284 /**
285 * @tc.name: FileUtilOhosTest003
286 * @tc.desc: Test ExcludeTrailingPathDelimiter/IncludeTrailingPathDelimiter defined in namespace FileUtil
287 * @tc.type: FUNC
288 * @tc.require: issueI65DUW
289 */
290 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest003, testing::ext::TestSize.Level3)
291 {
292 auto excludeRet = FileUtil::ExcludeTrailingPathDelimiter("ohos/test/123.txt");
293 ASSERT_EQ("ohos/test/123.txt", excludeRet);
294 auto name = FileUtil::IncludeTrailingPathDelimiter("ohos/test/123.txt/");
295 ASSERT_EQ("ohos/test/123.txt/", name);
296 }
297
298 /**
299 * @tc.name: FileUtilOhosTest004
300 * @tc.desc: Test ChangeModeFile defined in namespace FileUtil
301 * @tc.type: FUNC
302 * @tc.require: issueI65DUW
303 */
304 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest004, testing::ext::TestSize.Level3)
305 {
306 std::string caseName("FileUtilOhosTest004");
307 (void)FileUtil::ChangeModeFile(GenerateLogFileName(caseName, SUFFIX_0), FileUtil::FILE_PERM_755);
308 ASSERT_TRUE(true);
309 (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "test content");
310 (void)FileUtil::ChangeModeFile(GenerateLogFileName(caseName, SUFFIX_0), FileUtil::FILE_PERM_660);
311 ASSERT_TRUE(true);
312 }
313
314 /**
315 * @tc.name: FileUtilOhosTest005
316 * @tc.desc: Test Umask defined in namespace FileUtil
317 * @tc.type: FUNC
318 * @tc.require: issueI65DUW
319 */
320 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest005, testing::ext::TestSize.Level3)
321 {
322 auto umask = FileUtil::Umask(FileUtil::FILE_PERM_755);
323 auto expectedRet = 18;
324 ASSERT_EQ(expectedRet, umask);
325 }
326
327 /**
328 * @tc.name: FileUtilOhosTest006
329 * @tc.desc: Test FormatPath2UnixStyle/RemoveFolderBeginWith defined in namespace FileUtil
330 * @tc.type: FUNC
331 * @tc.require: issueI65DUW
332 */
333 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest006, testing::ext::TestSize.Level3)
334 {
335 std::string path = std::string(LOG_FILE_PATH);
336 FileUtil::FormatPath2UnixStyle(path);
337 ASSERT_TRUE(true);
338 FileUtil::RemoveFolderBeginWith(path, "data");
339 ASSERT_TRUE(true);
340 }
341
342 /**
343 * @tc.name: FileUtilOhosTest007
344 * @tc.desc: Test WriteBufferToFd defined in namespace FileUtil
345 * @tc.type: FUNC
346 * @tc.require: issueI65DUW
347 */
348 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest007, testing::ext::TestSize.Level3)
349 {
350 std::string caseName("FileUtilOhosTest007");
351 auto fd = -1;
352 std::string writeContent = "write content";
353 auto ret = FileUtil::WriteBufferToFd(fd, writeContent.c_str(), writeContent.size());
354 ASSERT_TRUE(!ret);
355 (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "test");
356 fd = FileUtil::Open(GenerateLogFileName(caseName, SUFFIX_0),
357 O_CREAT | O_WRONLY | O_TRUNC, FileUtil::FILE_PERM_770);
358 ret = FileUtil::WriteBufferToFd(fd, nullptr, writeContent.size());
359 ASSERT_TRUE(!ret);
360 (void)FileUtil::WriteBufferToFd(fd, "", writeContent.size());
361 ASSERT_TRUE(true);
362 (void)FileUtil::WriteBufferToFd(fd, writeContent.c_str(), writeContent.size());
363 ASSERT_TRUE(true);
364 }
365
366 /**
367 * @tc.name: FileUtilOhosTest008
368 * @tc.desc: Test CreateFile defined in namespace FileUtil
369 * @tc.type: FUNC
370 * @tc.require: issueI65DUW
371 */
372 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest008, testing::ext::TestSize.Level3)
373 {
374 std::string caseName("FileUtilOhosTest008");
375 int expectedFailedRet = -1;
376 auto ret = FileUtil::CreateFile(GenerateLogFileName(caseName, SUFFIX_0));
377 ASSERT_EQ(expectedFailedRet, ret);
378 (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "1111");
379 (void)FileUtil::CreateFile(GenerateLogFileName(caseName, SUFFIX_0));
380 ASSERT_TRUE(true);
381 }
382
383 /**
384 * @tc.name: FileUtilOhosTest009
385 * @tc.desc: Test CopyFile defined in namespace FileUtil
386 * @tc.type: FUNC
387 * @tc.require: issueI65DUW
388 */
389 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest009, testing::ext::TestSize.Level3)
390 {
391 std::string caseName("FileUtilOhosTest009");
392 int expectedFailedRet = -1;
393 auto ret = FileUtil::CopyFile("//...../invalid_dest_file", GenerateLogFileName(caseName, SUFFIX_1));
394 ASSERT_EQ(expectedFailedRet, ret);
395 (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "test");
396 ret = FileUtil::CopyFile(GenerateLogFileName(caseName, SUFFIX_0), "//...../invalid_dest_file");
397 ASSERT_EQ(expectedFailedRet, ret);
398 (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_1), "test");
399 (void)FileUtil::CopyFile(GenerateLogFileName(caseName, SUFFIX_0), GenerateLogFileName(caseName, SUFFIX_1));
400 ASSERT_TRUE(true);
401 }
402
403 /**
404 * @tc.name: FileUtilOhosTest010
405 * @tc.desc: Test GetLastLine defined in namespace FileUtil
406 * @tc.type: FUNC
407 * @tc.require: issueI65DUW
408 */
409 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest010, testing::ext::TestSize.Level3)
410 {
411 std::string caseName("FileUtilOhosTest010");
412 ifstream in(GenerateLogFileName(caseName, SUFFIX_0));
413 std::string line;
414 int invalidMaxLen = 5;
415 auto ret = FileUtil::GetLastLine(in, line, invalidMaxLen);
416 ASSERT_TRUE(!ret);
417 (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_1), "line1");
418 (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_1), "\nline2");
419 (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_1), "\nline3");
420 ifstream in1(GenerateLogFileName(caseName, SUFFIX_1));
421 ret = FileUtil::GetLastLine(in1, line, invalidMaxLen);
422 ASSERT_TRUE(!ret);
423 int validMaxLen = 2;
424 ret = FileUtil::GetLastLine(in1, line, validMaxLen);
425 ASSERT_TRUE(!ret);
426 validMaxLen = 3;
427 ret = FileUtil::GetLastLine(in1, line, validMaxLen);
428 ASSERT_TRUE(!ret);
429 }
430
431 /**
432 * @tc.name: FileUtilOhosTest011
433 * @tc.desc: Test GetParentDir defined in namespace FileUtil
434 * @tc.type: FUNC
435 * @tc.require: issueI65DUW
436 */
437 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest011, testing::ext::TestSize.Level3)
438 {
439 auto ret = FileUtil::GetParentDir("/");
440 ASSERT_EQ("", ret);
441 ret = FileUtil::GetParentDir("123/345/789");
442 ASSERT_EQ("123/345", ret);
443 }
444
445 /**
446 * @tc.name: FileUtilOhosTest012
447 * @tc.desc: Test IslegalPath defined in namespace FileUtil
448 * @tc.type: FUNC
449 * @tc.require: issueI65DUW
450 */
451 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest012, testing::ext::TestSize.Level3)
452 {
453 auto ret = FileUtil::IsLegalPath("aa/../bb");
454 ASSERT_TRUE(!ret);
455 ret = FileUtil::IsLegalPath("aa/./bb");
456 ASSERT_TRUE(!ret);
457 ret = FileUtil::IsLegalPath("aa/bb/");
458 ASSERT_TRUE(ret);
459 ret = FileUtil::IsLegalPath("aa/bb/cc");
460 ASSERT_TRUE(ret);
461 }
462
463 /**
464 * @tc.name: FileUtilOhosTest013
465 * @tc.desc: Test RenameFile defined in namespace FileUtil
466 * @tc.type: FUNC
467 * @tc.require: issueI65DUW
468 */
469 HWTEST_F(AdapterUtilityOhosTest, FileUtilOhosTest013, testing::ext::TestSize.Level3)
470 {
471 std::string caseName("FileUtilOhosTest013");
472 auto ret = FileUtil::RenameFile(GenerateLogFileName(caseName, SUFFIX_0),
473 GenerateLogFileName(caseName, SUFFIX_1));
474 ASSERT_TRUE(!ret);
475 (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "line1");
476 (void)FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_1), "line1");
477 (void)FileUtil::RenameFile(GenerateLogFileName(caseName, SUFFIX_0),
478 GenerateLogFileName(caseName, SUFFIX_1));
479 ASSERT_TRUE(true);
480 }
481
482 /**
483 * @tc.name: AshMemoryUtilsOhosTest001
484 * @tc.desc: Test AshMemoryUtil
485 * @tc.type: FUNC
486 * @tc.require: issueI65DUW
487 */
488 HWTEST_F(AdapterUtilityOhosTest, AshMemoryUtilsOhosTest001, testing::ext::TestSize.Level3)
489 {
490 std::vector<AshMemTestStruct> dataIn = {
491 AshMemTestStruct("testData1"),
492 AshMemTestStruct("testData2"),
493 AshMemTestStruct("testData3"),
494 AshMemTestStruct("testData4"),
495 };
496 const uint32_t memSize = 256;
497 auto ashmem = AshMemoryUtils::GetAshmem("ashMemTest", memSize);
498 if (ashmem == nullptr) {
499 ASSERT_TRUE(false);
500 }
501 std::vector<uint32_t> allSize;
502 bool retIn = AshMemoryUtils::WriteBulkData<AshMemTestStruct>(dataIn, ashmem, memSize, allSize);
503 ASSERT_TRUE(retIn);
504 std::vector<AshMemTestStruct> dataOut;
505 bool retOut = AshMemoryUtils::ReadBulkData<AshMemTestStruct>(ashmem, allSize, dataOut);
506 ASSERT_TRUE(retOut);
507 ASSERT_TRUE(dataIn.size() == dataOut.size());
508 for (size_t i = 0; i < dataIn.size(); i++) {
509 ASSERT_EQ(dataIn[i].data, dataOut[i].data);
510 }
511 }
512
513 /**
514 * @tc.name: CJsonUtilTest001
515 * @tc.desc: Test api of CJsonUtil
516 * @tc.type: FUNC
517 * @tc.require: issueI9E8HA
518 */
519 HWTEST_F(AdapterUtilityOhosTest, CJsonUtilTest001, testing::ext::TestSize.Level3)
520 {
521 auto jsonRoot = CJsonUtil::ParseJsonRoot(TEST_JSON_FILE_PATH);
522 ASSERT_NE(jsonRoot, nullptr);
523 std::vector<std::string> strArr;
524 CJsonUtil::GetStringArray(jsonRoot, "STRING_ARRAY", strArr);
525 ASSERT_EQ(strArr.size(), TEST_ARRAY_SIZE);
526 ASSERT_EQ(strArr[0], STRING_ARR_FIRST_VAL);
527 std::vector<std::string> strArrNullptr;
528 CJsonUtil::GetStringArray(nullptr, "STRING_ARRAY", strArrNullptr);
529 ASSERT_EQ(strArrNullptr.size(), 0);
530 std::vector<std::string> strArrNotSet;
531 CJsonUtil::GetStringArray(jsonRoot, "STRING_ARRAY_NOT_SET", strArrNotSet);
532 ASSERT_EQ(strArrNotSet.size(), 0);
533 ASSERT_NE(CJsonUtil::GetArrayValue(jsonRoot, "STRING_ARRAY"), nullptr);
534 ASSERT_EQ(CJsonUtil::GetArrayValue(nullptr, "STRING_ARRAY"), nullptr);
535 ASSERT_EQ(CJsonUtil::GetArrayValue(jsonRoot, "STRING_ARRAY_NOT_SET"), nullptr);
536 ASSERT_EQ(CJsonUtil::GetStringValue(jsonRoot, "STRING"), STRING_VAL);
537 ASSERT_EQ(CJsonUtil::GetStringValue(jsonRoot, "STRING_NOT_SET"), "");
538 ASSERT_EQ(CJsonUtil::GetIntValue(jsonRoot, "INT"), INT_VAL);
539 ASSERT_EQ(CJsonUtil::GetDoubleValue(jsonRoot, "DOUBLE"), DOU_VAL);
540 ASSERT_EQ(CJsonUtil::GetIntValue(jsonRoot, "INT_NOT_SET", INT_VAL_DEFAULT), INT_VAL_DEFAULT);
541 ASSERT_EQ(CJsonUtil::GetDoubleValue(jsonRoot, "DOUBLE_NOT_SET", DOU_VAL_DEFAULT), DOU_VAL_DEFAULT);
542 ASSERT_EQ(CJsonUtil::GetStringValue(nullptr, "STRING"), "");
543 ASSERT_EQ(CJsonUtil::GetIntValue(nullptr, "INT", INT_VAL_DEFAULT), INT_VAL_DEFAULT);
544 ASSERT_EQ(CJsonUtil::GetDoubleValue(nullptr, "DOUBLE", DOU_VAL_DEFAULT), DOU_VAL_DEFAULT);
545 bool boolValue = false;
546 ASSERT_TRUE(CJsonUtil::GetBoolValue(jsonRoot, "BOOL", boolValue));
547 ASSERT_TRUE(boolValue);
548 ASSERT_FALSE(CJsonUtil::GetBoolValue(nullptr, "BOOL", boolValue));
549 ASSERT_FALSE(CJsonUtil::GetBoolValue(jsonRoot, "BOOL_NOT_SET", boolValue));
550 }
551
552 /**
553 * @tc.name: FreezeJsonUtilTest001
554 * @tc.desc: Test api of FreezeJsonUtil
555 * @tc.type: FUNC
556 * @tc.require: issueI9E8HA
557 */
558 HWTEST_F(AdapterUtilityOhosTest, FreezeJsonUtilTest001, testing::ext::TestSize.Level3)
559 {
560 FreezeJsonUtil::FreezeJsonCollector jsonCollector;
561 FreezeJsonUtil::LoadCollectorFromFile(FREEZE_JSON_FILE, jsonCollector);
562 ASSERT_NE(jsonCollector.domain, "");
563 ASSERT_NE(jsonCollector.stringId, "");
564 }
565
566 /**
567 * @tc.name: FreezeJsonUtilTest002
568 * @tc.desc: Test api of FreezeJsonUtil
569 * @tc.type: FUNC
570 */
571 HWTEST_F(AdapterUtilityOhosTest, FreezeJsonUtilTest002, testing::ext::TestSize.Level3)
572 {
573 FreezeJsonUtil::FreezeJsonCollector jsonCollector;
574 FreezeJsonUtil::LoadCollectorFromFile(FREEZE_JSON_FILE_EMPTY, jsonCollector);
575 ASSERT_EQ(jsonCollector.domain, "");
576 ASSERT_EQ(jsonCollector.stringId, "");
577 }
578
579 /**
580 * @tc.name: ZipUtilTest001
581 * @tc.desc: Test api of ZipUtil
582 * @tc.type: FUNC
583 * @tc.require: issueI9E8HA
584 */
585 HWTEST_F(AdapterUtilityOhosTest, ZipUtilTest001, testing::ext::TestSize.Level3)
586 {
587 std::string testSourceFile1 = SOURCE_PATH + "zip_test_file1.txt";
588 std::string testSourceFile2 = SOURCE_PATH + "zip_test_file2.txt";
589 FileUtil::SaveStringToFile(testSourceFile1, "zip_test_content1", true);
590 FileUtil::SaveStringToFile(testSourceFile2, "zip_test_content2", true);
591 {
592 HiviewZipUnit zipUnit(TEST_ZIP_FILE);
593 ASSERT_EQ(zipUnit.AddFileInZip(testSourceFile1, ZipFileLevel::KEEP_NONE_PARENT_PATH), 0);
594 ASSERT_EQ(zipUnit.AddFileInZip(testSourceFile2, ZipFileLevel::KEEP_ONE_PARENT_PATH), 0);
595 }
596 HiviewUnzipUnit unzipUnit(TEST_ZIP_FILE, ZIP_DES_PATH);
597 unzipUnit.UnzipFile();
598 const int waitUnzip = 2;
599 sleep(waitUnzip);
600 ASSERT_TRUE(FileUtil::FileExists(ZIP_DES_PATH + "zip_test_file1.txt"));
601 ASSERT_TRUE(FileUtil::FileExists(ZIP_DES_PATH + "test_data/zip_test_file2.txt"));
602 }
603
604 /**
605 * @tc.name: DbUtilTest001
606 * @tc.desc: Test api of DbUtil
607 * @tc.type: FUNC
608 */
609 HWTEST_F(AdapterUtilityOhosTest, DbUtilTest001, testing::ext::TestSize.Level3)
610 {
611 std::string uploadPath = UPLOAD_PATH;
612 ASSERT_TRUE(HiviewDbUtil::InitDbUploadPath("", uploadPath));
613 uploadPath = "";
614 ASSERT_FALSE(HiviewDbUtil::InitDbUploadPath("", uploadPath));
615 ASSERT_TRUE(HiviewDbUtil::InitDbUploadPath(DB_PATH, uploadPath));
616 std::string dbFile = HiviewDbUtil::CreateFileNameByDate("test");
617 std::string otherFile = DB_PATH + "testOhter.db-shm";
618 FileUtil::SaveStringToFile(DB_PATH + dbFile, "test db file", true);
619 FileUtil::SaveStringToFile(otherFile, "test other file", true);
620 HiviewDbUtil::MoveDbFilesToUploadDir(DB_PATH, uploadPath);
621 ASSERT_FALSE(FileUtil::FileExists(otherFile));
622 ASSERT_TRUE(FileUtil::FileExists(uploadPath + "/" + dbFile));
623 HiviewDbUtil::TryToAgeUploadDbFiles(uploadPath, 1); // 1 is the max file number
624 ASSERT_TRUE(FileUtil::FileExists(uploadPath + "/" + dbFile));
625 HiviewDbUtil::TryToAgeUploadDbFiles(uploadPath, 0); // 0 is the max file number
626 ASSERT_FALSE(FileUtil::FileExists(uploadPath + "/" + dbFile));
627 }
628
629 /**
630 * @tc.name: HiViewConfigUtilTest001
631 * @tc.desc: Test api of HiViewConfigUtil
632 * @tc.type: FUNC
633 */
634 HWTEST_F(AdapterUtilityOhosTest, HiViewConfigUtilTest001, testing::ext::TestSize.Level3)
635 {
636 std::string localVer;
637 FileUtil::LoadStringFromFile("/system/etc/hiview/hiview_config_version", localVer);
638 std::string cloudVer;
639 FileUtil::LoadStringFromFile("/data/system/hiview/hiview_config_version", cloudVer);
640 auto configPath = HiViewConfigUtil::GetConfigFilePath("test_file_name");
641 if (localVer >= cloudVer) {
642 ASSERT_EQ(configPath, "/system/etc/hiview/test_file_name");
643 } else {
644 ASSERT_EQ(configPath, "/data/system/hiview/test_file_name");
645 }
646 auto ret = HiViewConfigUtil::GetConfigFilePath("test_file_name", "/data/test/", "test_file_name");
647 ASSERT_EQ(ret, "/data/system/hiview/unzip_configs//data/test/test_file_name");
648 }
649 } // namespace HiviewDFX
650 } // namespace OHOS