1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 "utilities_test.h"
17
18 #include <chrono>
19 #include <thread>
20 #include "utilities.h"
21
22 using namespace testing::ext;
23 using namespace std;
24 namespace OHOS {
25 namespace Developtools {
26 namespace NativeDaemon {
27 const std::string TEST_PATH = "/data/test/resource/testdata/";
28
29 class UtilitiesTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35 void TestThread();
36 void StartThreads(const size_t count);
37 void ExitThreads();
38 bool exitThreads_ = true;
39 std::vector<pid_t> tids_;
40 std::vector<std::thread> threads_;
41 const int sleepTime_ = {500};
42 };
43
SetUpTestCase()44 void UtilitiesTest::SetUpTestCase() {}
45
TearDownTestCase()46 void UtilitiesTest::TearDownTestCase() {}
47
SetUp()48 void UtilitiesTest::SetUp() {}
49
TearDown()50 void UtilitiesTest::TearDown() {}
51
TestThread()52 void UtilitiesTest::TestThread()
53 {
54 printf("threads %ld create\n", static_cast<long>(gettid()));
55 int ret = fflush(nullptr);
56 if (ret == EOF) {
57 printf("fflush() error\n");
58 }
59
60 tids_.emplace_back(gettid());
61 while (!exitThreads_) {
62 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime_));
63 }
64 printf("threads %ld exited\n", static_cast<long>(gettid()));
65 ret = fflush(nullptr);
66 if (ret == EOF) {
67 printf("fflush() error\n");
68 }
69 }
70
StartThreads(const size_t count)71 void UtilitiesTest::StartThreads(const size_t count)
72 {
73 printf("create %zu threads\n", count);
74 int ret = fflush(nullptr);
75 if (ret == EOF) {
76 printf("fflush() error\n");
77 }
78
79 exitThreads_ = false;
80 size_t created = 0;
81 while (created < count) {
82 threads_.emplace_back(std::thread(&UtilitiesTest::TestThread, this));
83 created++;
84 }
85 while (tids_.size() < count) {
86 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime_));
87 }
88 printf("all threads created\n");
89 ret = fflush(nullptr);
90 if (ret == EOF) {
91 printf("fflush() error\n");
92 }
93
94 std::vector<pid_t> tids = GetSubthreadIDs(getpid());
95 tids_.clear();
96 for (const auto& tid : tids) {
97 tids_.emplace_back(tid);
98 }
99 }
100
ExitThreads()101 void UtilitiesTest::ExitThreads()
102 {
103 printf("wait all threads exit\n");
104 exitThreads_ = true;
105 for (std::thread &t : this->threads_) {
106 t.join();
107 }
108 tids_.clear();
109 printf("all threads exited\n");
110 }
111
112 /**
113 * @tc.name: StringReplace
114 * @tc.desc:
115 * @tc.type: FUNC
116 */
117 HWTEST_F(UtilitiesTest, StringReplace, TestSize.Level1)
118 {
119 const std::string testString = "1234567890";
120 EXPECT_EQ(StringReplace(testString, "1", ""), "234567890");
121 EXPECT_EQ(StringReplace(testString, "2", ""), "134567890");
122 EXPECT_EQ(StringReplace(testString, "0", ""), "123456789");
123 EXPECT_EQ(StringReplace(testString, "1", "0"), "0234567890");
124 EXPECT_EQ(StringReplace(testString, "0", "1"), "1234567891");
125 EXPECT_EQ(StringReplace(testString, "123", "1"), "14567890");
126 EXPECT_EQ(StringReplace(testString, "890", "1"), "12345671");
127 EXPECT_EQ(StringReplace(testString, "456", "1"), "12317890");
128 EXPECT_EQ(StringReplace(testString, "123", "321"), "3214567890");
129 EXPECT_EQ(StringReplace(testString, "890", "098"), "1234567098");
130 }
131
132 /**
133 * @tc.name: StringSplit
134 * @tc.desc:
135 * @tc.type: FUNC
136 */
137 HWTEST_F(UtilitiesTest, StringSplit, TestSize.Level1)
138 {
139 std::string testString = "1,23,456,7890,";
140 size_t testSize = testString.size();
141 EXPECT_EQ(StringSplit(testString, "1").size(), 1u);
142 EXPECT_EQ(StringSplit(testString, "2").size(), 2u);
143 EXPECT_EQ(StringSplit(testString, ",").size(), 4u);
144 EXPECT_EQ(StringSplit(testString, "456").size(), 2u);
145 EXPECT_EQ(StringSplit(testString, "000").size(), 1u);
146 EXPECT_EQ(StringSplit(testString, "").size(), 1u);
147 // dont change the input string
148 EXPECT_EQ(testString.size(), testSize);
149
150 EXPECT_EQ(StringSplit(testString = "").size(), 0u);
151 EXPECT_EQ(StringSplit(testString = "1,2,3").size(), 3u);
152 EXPECT_EQ(StringSplit(testString = "1,2,3,,,").size(), 3u);
153 }
154
155 /**
156 * @tc.name: SubStringCount
157 * @tc.desc:
158 * @tc.type: FUNC
159 */
160 HWTEST_F(UtilitiesTest, SubStringCount, TestSize.Level1)
161 {
162 std::string testString = "1,22,333,4444,";
163 EXPECT_EQ(SubStringCount(testString, ""), testString.size());
164 EXPECT_EQ(SubStringCount(testString, "1"), 1u);
165 EXPECT_EQ(SubStringCount(testString, "2"), 2u);
166 EXPECT_EQ(SubStringCount(testString, "3"), 3u);
167 EXPECT_EQ(SubStringCount(testString, "4"), 4u);
168
169 EXPECT_EQ(SubStringCount(testString, "22"), 1u);
170 EXPECT_EQ(SubStringCount(testString, "33"), 1u);
171 EXPECT_EQ(SubStringCount(testString, "333"), 1u);
172 EXPECT_EQ(SubStringCount(testString, "4444"), 1u);
173 EXPECT_EQ(SubStringCount(testString, "444"), 1u);
174 EXPECT_EQ(SubStringCount(testString, "44"), 2u);
175 }
176
177 /**
178 * @tc.name: StringEndsWith
179 * @tc.desc:
180 * @tc.type: FUNC
181 */
182 HWTEST_F(UtilitiesTest, StringEndsWith, TestSize.Level1)
183 {
184 std::string testString = "1,22,333,4444,";
185 EXPECT_EQ(StringEndsWith(testString, ""), true);
186 EXPECT_EQ(StringEndsWith(testString, "1"), false);
187 EXPECT_EQ(StringEndsWith(testString, ","), true);
188
189 EXPECT_EQ(StringEndsWith("", ""), true);
190 EXPECT_EQ(StringEndsWith("", "1"), false);
191 EXPECT_EQ(StringEndsWith("", ","), false);
192 }
193
194 /**
195 * @tc.name: StringStartsWith
196 * @tc.desc:
197 * @tc.type: FUNC
198 */
199 HWTEST_F(UtilitiesTest, StringStartsWith, TestSize.Level1)
200 {
201 std::string testString = "1,22,333,4444,";
202 EXPECT_EQ(StringStartsWith(testString, ""), true);
203 EXPECT_EQ(StringStartsWith(testString, "1"), true);
204 EXPECT_EQ(StringStartsWith(testString, ","), false);
205
206 EXPECT_EQ(StringStartsWith("", ""), true);
207 EXPECT_EQ(StringStartsWith("", "1"), false);
208 EXPECT_EQ(StringStartsWith("", ","), false);
209 }
210
211 /**
212 * @tc.name: VectorToString
213 * @tc.desc:
214 * @tc.type: FUNC
215 */
216 HWTEST_F(UtilitiesTest, VectorToString, TestSize.Level1)
217 {
218 EXPECT_EQ(VectorToString<std::string>(
219 {}), "<empty>");
220 EXPECT_EQ(VectorToString<std::string>(
221 {"a", "b", "c"}), "a,b,c");
222 EXPECT_EQ(VectorToString<std::string>(
223 {"a"}), "a");
224 EXPECT_EQ(VectorToString<std::vector<std::string>>(
225 { {} }), "[<empty>]");
226 EXPECT_EQ(VectorToString<std::vector<std::string>>(
227 { { "a", "b", "c" }, }), "[a,b,c]");
228 EXPECT_EQ(VectorToString<std::vector<std::string>>(
229 {
230 {"a", "b", "c"},
231 {"a", "b", "c"},
232 {"a", "b", "c"},
233 }),
234 "[a,b,c],[a,b,c],[a,b,c]");
235
236 EXPECT_EQ(VectorToString<int>(
237 {}), "<empty>");
238 EXPECT_EQ(VectorToString<int>(
239 {1}), "1");
240 EXPECT_EQ(VectorToString<int>(
241 {1, 2, 3}), "1,2,3");
242
243 EXPECT_EQ(VectorToString<float>(
244 {}), "<empty>");
245 EXPECT_EQ(VectorToString<float>(
246 {1.0, 2.0, 3.0}), "1.000000,2.000000,3.000000");
247 }
248
249 /**
250 * @tc.name: BufferToHexString
251 * @tc.desc:
252 * @tc.type: FUNC
253 */
254 HWTEST_F(UtilitiesTest, BufferToHexString, TestSize.Level1)
255 {
256 const unsigned char buf[] = "12345678";
257
258 EXPECT_STREQ(BufferToHexString(buf, 0).c_str(), "0:");
259 EXPECT_STREQ(BufferToHexString(buf, 1).c_str(), "1: 0x31");
260 EXPECT_STREQ(BufferToHexString(buf, 4).c_str(), "4: 0x31 0x32 0x33 0x34");
261 EXPECT_STREQ(BufferToHexString(buf, 5).c_str(), "5: 0x31 0x32 0x33 0x34 0x35");
262 EXPECT_STREQ(BufferToHexString(buf, 8).c_str(), "8: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
263
264 const std::vector<unsigned char> vbuf(buf, buf + sizeof(buf) - 1u);
265
266 EXPECT_STREQ(BufferToHexString(vbuf).c_str(), "8: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
267
268 const unsigned char buf2[] = "1234567812345678";
269 EXPECT_STREQ(BufferToHexString(buf2, 0).c_str(), "0:");
270 EXPECT_STREQ(BufferToHexString(buf2, 1).c_str(), "1: 0x31");
271 EXPECT_STREQ(BufferToHexString(buf2, 4).c_str(), "4: 0x31 0x32 0x33 0x34");
272 EXPECT_STREQ(BufferToHexString(buf2, 5).c_str(), "5: 0x31 0x32 0x33 0x34 0x35");
273 EXPECT_STREQ(BufferToHexString(buf2, 8).c_str(), "8: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
274 EXPECT_STREQ(BufferToHexString(buf2, 9).c_str(),
275 "9: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x31");
276 EXPECT_STREQ(
277 BufferToHexString(buf2, 16).c_str(),
278 "16: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
279
280 const std::vector<unsigned char> vbuf2(buf2, buf2 + sizeof(buf2) - 1u);
281 EXPECT_STREQ(
282 BufferToHexString(vbuf2).c_str(),
283 "16: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
284 }
285
286 /**
287 * @tc.name: HexDump
288 * @tc.desc:
289 * @tc.type: FUNC
290 */
291 HWTEST_F(UtilitiesTest, HexDump, TestSize.Level1)
292 {
293 const unsigned char buf[] = "12345678";
294 ScopeDebugLevel tempLogLevel(LEVEL_MUCH, true);
295
296 StdoutRecord stdoutRecord;
297 stdoutRecord.Start();
298 HexDump(buf, 0);
299 HexDump(buf, 1);
300 HexDump(buf, 4);
301 HexDump(buf, 5);
302 HexDump(buf, 8);
303 std::string out = stdoutRecord.Stop();
304 EXPECT_EQ(out.empty(), false);
305 EXPECT_NE(out.find("0x32"), std::string::npos);
306 EXPECT_NE(out.find("0x33"), std::string::npos);
307 EXPECT_NE(out.find("0x34"), std::string::npos);
308 EXPECT_EQ(out.find("0x39"), std::string::npos);
309 }
310
311 /**
312 * @tc.name: StringTrim
313 * @tc.desc:
314 * @tc.type: FUNC
315 */
316 HWTEST_F(UtilitiesTest, StringTrim, TestSize.Level1)
317 {
318 std::string test;
319 EXPECT_STREQ(StringTrim(test = " a ").c_str(), "a");
320 EXPECT_STREQ(StringTrim(test = " a").c_str(), "a");
321 EXPECT_STREQ(StringTrim(test = "a ").c_str(), "a");
322 EXPECT_STREQ(StringTrim(test = " a1a ").c_str(), "a1a");
323 EXPECT_STREQ(StringTrim(test = " a1a").c_str(), "a1a");
324 EXPECT_STREQ(StringTrim(test = "a1a ").c_str(), "a1a");
325 EXPECT_STREQ(StringTrim(test = " a1a ").c_str(), "a1a");
326 EXPECT_STREQ(StringTrim(test = " a1a").c_str(), "a1a");
327 EXPECT_STREQ(StringTrim(test = "a1a ").c_str(), "a1a");
328 }
329
330 /**
331 * @tc.name: RecordStdout
332 * @tc.desc:
333 * @tc.type: FUNC
334 */
335 HWTEST_F(UtilitiesTest, RecordStdout, TestSize.Level1)
336 {
337 StdoutRecord stdoutRecord;
338
339 ASSERT_EQ(stdoutRecord.Start(), true);
340 printf("line1: abc\n");
341 printf("line2: def\n");
342 printf("line3: ghi\n");
343 printf("\n");
344 std::string out = stdoutRecord.Stop();
345
346 printf("stdoutRecord:\n%s", out.c_str());
347 EXPECT_EQ(out.empty(), false);
348 EXPECT_NE(out.find("line1:"), std::string::npos);
349 EXPECT_NE(out.find("line2:"), std::string::npos);
350 EXPECT_NE(out.find("line3:"), std::string::npos);
351 EXPECT_EQ(out.find("line4:"), std::string::npos);
352 }
353
354 /**
355 * @tc.name: IsDigits
356 * @tc.desc:
357 * @tc.type: FUNC
358 */
359 HWTEST_F(UtilitiesTest, IsDigits, TestSize.Level1)
360 {
361 EXPECT_EQ(IsDigits(""), false);
362 EXPECT_EQ(IsDigits("1"), true);
363 EXPECT_EQ(IsDigits("12"), true);
364 EXPECT_EQ(IsDigits("1a"), false);
365 EXPECT_EQ(IsDigits("a1"), false);
366 EXPECT_EQ(IsDigits("1a2"), false);
367 EXPECT_EQ(IsDigits("a1b"), false);
368 EXPECT_EQ(IsDigits("_1"), false);
369 EXPECT_EQ(IsDigits("1_"), false);
370 }
371
372 /**
373 * @tc.name: IsHexxDigits
374 * @tc.desc:
375 * @tc.type: FUNC
376 */
377 HWTEST_F(UtilitiesTest, IsHexxDigits, TestSize.Level1)
378 {
379 EXPECT_EQ(IsHexDigits(""), false);
380 EXPECT_EQ(IsHexDigits("1"), true);
381 EXPECT_EQ(IsHexDigits("12"), true);
382 EXPECT_EQ(IsHexDigits("1a"), true);
383 EXPECT_EQ(IsHexDigits("f1"), true);
384 EXPECT_EQ(IsHexDigits("1f2"), true);
385 EXPECT_EQ(IsHexDigits("a1f"), true);
386 EXPECT_EQ(IsHexDigits("g1"), false);
387 EXPECT_EQ(IsHexDigits("1g"), false);
388 EXPECT_EQ(IsHexDigits("_1"), false);
389 EXPECT_EQ(IsHexDigits("1_"), false);
390 }
391
392 /**
393 * @tc.name: IsSameCommand
394 * @tc.desc:
395 * @tc.type: FUNC
396 */
397 HWTEST_F(UtilitiesTest, IsSameCommand, TestSize.Level1)
398 {
399 EXPECT_EQ(IsSameCommand("", ""), false);
400 EXPECT_EQ(IsSameCommand("a", ""), false);
401 EXPECT_EQ(IsSameCommand("", "b"), false);
402 EXPECT_EQ(IsSameCommand("1", "2"), false);
403 EXPECT_EQ(IsSameCommand("2", "1"), false);
404 EXPECT_EQ(IsSameCommand("1", "1"), true);
405 EXPECT_EQ(IsSameCommand("a", "a"), true);
406 EXPECT_EQ(IsSameCommand("a:1", "a:2"), false);
407 }
408
409 /**
410 * @tc.name: CompressFile
411 * @tc.desc:
412 * @tc.type: FUNC
413 */
414 HWTEST_F(UtilitiesTest, CompressFile, TestSize.Level1)
415 {
416 std::string srcPath = "./resource/testdata/elf_test_stripped_broken";
417 std::string destPath = "./test.gz";
418 EXPECT_EQ(CompressFile(srcPath, destPath), true);
419 srcPath = "";
420 EXPECT_EQ(CompressFile(srcPath, destPath), false);
421 srcPath = "./resource/testdata/elf_test_stripped_broken";
422 destPath = "";
423 EXPECT_EQ(CompressFile(srcPath, destPath), false);
424 }
425
426 /**
427 * @tc.name: UncompressFile
428 * @tc.desc:
429 * @tc.type: FUNC
430 */
431 HWTEST_F(UtilitiesTest, UncompressFile, TestSize.Level1)
432 {
433 std::string gzipPath = "./test.gz";
434 std::string dataPath = "./test";
435 EXPECT_EQ(UncompressFile(gzipPath, dataPath), true);
436 gzipPath = "./test.gz";
437 dataPath = "";
438 EXPECT_EQ(UncompressFile(gzipPath, dataPath), false);
439 gzipPath = "";
440 dataPath = "./resource/testdata/elf_test_stripped_broken";
441 EXPECT_EQ(UncompressFile(gzipPath, dataPath), false);
442 }
443
444 /**
445 * @tc.name: StringPrintf
446 * @tc.desc:
447 * @tc.type: FUNC
448 */
449 HWTEST_F(UtilitiesTest, StringPrintf, TestSize.Level1)
450 {
451 EXPECT_STREQ(StringPrintf("").c_str(), "");
452 EXPECT_STREQ(StringPrintf("123").c_str(), "123");
453 EXPECT_STREQ(StringPrintf("%d%s%c", 1, "2", 'c').c_str(), "12c");
454 EXPECT_STREQ(StringPrintf("%d%s%c\t\n", 1, "2", 'c').c_str(), "12c\t\n");
455
456 char format[PATH_MAX + 1];
457 std::fill(format, format + PATH_MAX, ' ');
458 format[PATH_MAX] = 0;
459 EXPECT_STRNE(StringPrintf(format).c_str(), format);
460 format[PATH_MAX - 1] = 0;
461 EXPECT_STREQ(StringPrintf(format).c_str(), format);
462 EXPECT_STREQ(StringPrintf(nullptr).c_str(), "");
463 }
464
465 /**
466 * @tc.name: GetEntriesInDir
467 * @tc.desc:
468 * @tc.type: FUNC
469 */
470 HWTEST_F(UtilitiesTest, GetEntriesInDir, TestSize.Level1)
471 {
472 std::vector<std::string> dirFileInfo;
473 dirFileInfo = GetEntriesInDir("./");
474 EXPECT_GE(dirFileInfo.size(), 0u);
475 }
476
477 /**
478 * @tc.name: GetSubDirs
479 * @tc.desc:
480 * @tc.type: FUNC
481 */
482 HWTEST_F(UtilitiesTest, GetSubDirs, TestSize.Level1)
483 {
484 std::vector<std::string> subDirFileInfo;
485 subDirFileInfo = GetSubDirs("../");
486 EXPECT_GE(subDirFileInfo.size(), 0u);
487 }
488
489 /**
490 * @tc.name: IsDir
491 * @tc.desc:
492 * @tc.type: FUNC
493 */
494 HWTEST_F(UtilitiesTest, IsDir, TestSize.Level1)
495 {
496 bool ret = IsDir("../");
497 EXPECT_EQ(ret, true);
498 }
499
500 /**
501 * @tc.name: IsPath
502 * @tc.desc:
503 * @tc.type: FUNC
504 */
505 HWTEST_F(UtilitiesTest, IsPath, TestSize.Level1)
506 {
507 bool ret = IsPath("./");
508 EXPECT_EQ(ret, true);
509 }
510
511 /**
512 * @tc.name: PlatformPathConvert
513 * @tc.desc:
514 * @tc.type: FUNC
515 */
516 HWTEST_F(UtilitiesTest, PlatformPathConvert, TestSize.Level1)
517 {
518 EXPECT_GE(PlatformPathConvert("./").length(), 0u);
519 }
520
521 /**
522 * @tc.name: ToHex
523 * @tc.desc:
524 * @tc.type: FUNC
525 */
526 HWTEST_F(UtilitiesTest, ToHex, TestSize.Level1)
527 {
528 unsigned char hVal = 'G';
529 EXPECT_STREQ(ToHex(hVal, 1, true).c_str(), "0x47");
530 }
531
532 /**
533 * @tc.name: ToHex
534 * @tc.desc:
535 * @tc.type: FUNC
536 */
537 HWTEST_F(UtilitiesTest, CopyFromBufferAndMove, TestSize.Level1)
538 {
539 unsigned char *buffer = new unsigned char[4];
540 buffer[0] = '1';
541 buffer[1] = '2';
542 buffer[2] = '3';
543 buffer[3] = '4';
544 int *dest = new int;
545 const unsigned char *srcStr = buffer;
546 EXPECT_EQ(CopyFromBufferAndMove(srcStr, dest, 4), 4u);
547 }
548
549 /**
550 * @tc.name: ReadIntFromProcFile
551 * @tc.desc:
552 * @tc.type: FUNC
553 */
554 HWTEST_F(UtilitiesTest, ReadIntFromProcFile, TestSize.Level1)
555 {
556 std::string strPath = "/proc/sys/kernel/perf_cpu_time_max_percent";
557 int strLen = 0;
558 EXPECT_EQ(ReadIntFromProcFile(strPath, strLen), true);
559 }
560
561 /**
562 * @tc.name: WriteIntToProcFile
563 * @tc.desc:
564 * @tc.type: FUNC
565 */
566 HWTEST_F(UtilitiesTest, WriteIntToProcFile, TestSize.Level1)
567 {
568 std::string strPath = "./hiperf_log.txt";
569 int strVal = 0;
570 EXPECT_EQ(WriteIntToProcFile(strPath, strVal), true);
571 }
572
573 /**
574 * @tc.name: ReadFileToString
575 * @tc.desc:
576 * @tc.type: FUNC
577 */
578 HWTEST_F(UtilitiesTest, ReadFileToString, TestSize.Level1)
579 {
580 std::string strPath = "./hiperf_log.txt";
581 EXPECT_NE(ReadFileToString(strPath).length(), 0u);
582 }
583
584 /**
585 * @tc.name: WriteStringToFile
586 * @tc.desc:
587 * @tc.type: FUNC
588 */
589 HWTEST_F(UtilitiesTest, WriteStringToFile, TestSize.Level1)
590 {
591 std::string strPath = "./hiperf_log.txt";
592 std::string content = "0";
593 EXPECT_EQ(WriteStringToFile(strPath, content), true);
594 }
595
596 /**
597 * @tc.name: Percentage
598 * @tc.desc:
599 * @tc.type: FUNC
600 */
601 HWTEST_F(UtilitiesTest, Percentage, TestSize.Level1)
602 {
603 EXPECT_EQ(Percentage(99, 100), 99);
604 }
605
606 /**
607 * @tc.name: PowerOfTwo
608 * @tc.desc:
609 * @tc.type: FUNC
610 */
611 HWTEST_F(UtilitiesTest, PowerOfTwo, TestSize.Level1)
612 {
613 EXPECT_EQ(PowerOfTwo(1), true);
614 }
615
616 /**
617 * @tc.name: GetSubthreadIDs
618 * @tc.desc:
619 * @tc.type: FUNC
620 */
621 HWTEST_F(UtilitiesTest, GetSubthreadIDs, TestSize.Level1)
622 {
623 StartThreads(1);
624 std::vector<pid_t> tids = GetSubthreadIDs(getpid());
625 EXPECT_EQ(tids.size(), tids_.size());
626 if (!HasFailure()) {
627 for (pid_t tid : tids) {
628 EXPECT_NE(find(tids_.begin(), tids_.end(), tid), tids_.end());
629 }
630 }
631 ExitThreads();
632 }
633
634 /**
635 * @tc.name: test funtion GetValueFromJsonFile return normal result
636 * @tc.desc:
637 * @tc.type: FUNC
638 */
639 HWTEST_F(UtilitiesTest, GetValueFromJsonFile, TestSize.Level1)
640 {
641 std::string filePath = TEST_PATH + "hiprofiler_cfg.json";
642 auto res = GetValueFromJsonFile(filePath, "hiprofiler_hook_process_count");
643 EXPECT_EQ(res, 2);
644 }
645
646 /**
647 * @tc.name: test funtion GetValueFromJsonFile when path is not exit
648 * @tc.desc:
649 * @tc.type: FUNC
650 */
651 HWTEST_F(UtilitiesTest, GetValueFromJsonFile_FileNotFound, TestSize.Level1)
652 {
653 std::string filePath = TEST_PATH + "hiprofiler_cfg22.json";
654 auto res = GetValueFromJsonFile(filePath, "hiprofiler_hook_process_count");
655 EXPECT_EQ(res, -1);
656 }
657
658 /**
659 * @tc.name: test funtion GetValueFromJsonFile when key is not exit
660 * @tc.desc:
661 * @tc.type: FUNC
662 */
663 HWTEST_F(UtilitiesTest, GetValueFromJsonFile_InvalidKey, TestSize.Level1)
664 {
665 std::string filePath = TEST_PATH + "hiprofiler_cfg.json";
666 auto res = GetValueFromJsonFile(filePath, "test");
667 EXPECT_EQ(res, -1);
668 }
669
670 /**
671 * @tc.name: test funtion GetValueFromJsonFile when value is not int
672 * @tc.desc:
673 * @tc.type: FUNC
674 */
675 HWTEST_F(UtilitiesTest, GetValueFromJsonFile_InvalidFormat, TestSize.Level1)
676 {
677 std::string filePath = TEST_PATH +"hiprofiler_cfg_2.json";
678 auto res = GetValueFromJsonFile(filePath, "hiprofiler_hook_process_count");
679 EXPECT_EQ(res, -1);
680 }
681 } // namespace NativeDaemon
682 } // namespace Developtools
683 } // namespace OHOS
684