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