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