1 /*
2 * Copyright (c) 2021-2022 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 "utilities_test.h"
17 #include <chrono>
18 #include <thread>
19 #include "ipc_utilities.h"
20 #include "test_utilities.h"
21 #include "utilities.h"
22
23 using namespace testing::ext;
24 namespace OHOS {
25 namespace Developtools {
26 namespace HiPerf {
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", 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", 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
ExitThreads()93 void UtilitiesTest::ExitThreads()
94 {
95 printf("wait all threads exit\n");
96 exitThreads_ = true;
97 for (std::thread &t : this->threads_) {
98 t.join();
99 }
100 tids_.clear();
101 printf("all threads exited\n");
102 }
103
104 /**
105 * @tc.name: StringReplace
106 * @tc.desc:
107 * @tc.type: FUNC
108 */
109 HWTEST_F(UtilitiesTest, StringReplace, TestSize.Level2)
110 {
111 const std::string testString = "1234567890";
112 EXPECT_EQ(StringReplace(testString, "1", ""), "234567890");
113 EXPECT_EQ(StringReplace(testString, "2", ""), "134567890");
114 EXPECT_EQ(StringReplace(testString, "0", ""), "123456789");
115 EXPECT_EQ(StringReplace(testString, "1", "0"), "0234567890");
116 EXPECT_EQ(StringReplace(testString, "0", "1"), "1234567891");
117 EXPECT_EQ(StringReplace(testString, "123", "1"), "14567890");
118 EXPECT_EQ(StringReplace(testString, "890", "1"), "12345671");
119 EXPECT_EQ(StringReplace(testString, "456", "1"), "12317890");
120 EXPECT_EQ(StringReplace(testString, "123", "321"), "3214567890");
121 EXPECT_EQ(StringReplace(testString, "890", "098"), "1234567098");
122 }
123
124 /**
125 * @tc.name: StringSplit
126 * @tc.desc:
127 * @tc.type: FUNC
128 */
129 HWTEST_F(UtilitiesTest, StringSplit, TestSize.Level0)
130 {
131 std::string testString = "1,23,456,7890,";
132 EXPECT_EQ(StringSplit(testString, "1").size(), 1u);
133 EXPECT_EQ(StringSplit(testString, "2").size(), 2u);
134 EXPECT_EQ(StringSplit(testString, ",").size(), 4u);
135 EXPECT_EQ(StringSplit(testString, "456").size(), 2u);
136 EXPECT_EQ(StringSplit(testString, "000").size(), 1u);
137 EXPECT_EQ(StringSplit(testString, "").size(), 1u);
138
139 EXPECT_EQ(StringSplit(testString = "").size(), 0u);
140 EXPECT_EQ(StringSplit(testString = "1,2,3").size(), 3u);
141 EXPECT_EQ(StringSplit(testString = "1,2,3,,,").size(), 3u);
142 }
143
144 /**
145 * @tc.name: SubStringCount
146 * @tc.desc:
147 * @tc.type: FUNC
148 */
149 HWTEST_F(UtilitiesTest, SubStringCount, TestSize.Level2)
150 {
151 std::string testString = "1,22,333,4444,";
152 EXPECT_EQ(SubStringCount(testString, ""), testString.size());
153 EXPECT_EQ(SubStringCount(testString, "1"), 1u);
154 EXPECT_EQ(SubStringCount(testString, "2"), 2u);
155 EXPECT_EQ(SubStringCount(testString, "3"), 3u);
156 EXPECT_EQ(SubStringCount(testString, "4"), 4u);
157
158 EXPECT_EQ(SubStringCount(testString, "22"), 1u);
159 EXPECT_EQ(SubStringCount(testString, "33"), 1u);
160 EXPECT_EQ(SubStringCount(testString, "333"), 1u);
161 EXPECT_EQ(SubStringCount(testString, "4444"), 1u);
162 EXPECT_EQ(SubStringCount(testString, "444"), 1u);
163 EXPECT_EQ(SubStringCount(testString, "44"), 2u);
164 }
165
166 /**
167 * @tc.name: StringEndsWith
168 * @tc.desc:
169 * @tc.type: FUNC
170 */
171 HWTEST_F(UtilitiesTest, StringEndsWith, TestSize.Level2)
172 {
173 std::string testString = "1,22,333,4444,";
174 EXPECT_EQ(StringEndsWith(testString, ""), true);
175 EXPECT_EQ(StringEndsWith(testString, "1"), false);
176 EXPECT_EQ(StringEndsWith(testString, ","), true);
177
178 EXPECT_EQ(StringEndsWith("", ""), true);
179 EXPECT_EQ(StringEndsWith("", "1"), false);
180 EXPECT_EQ(StringEndsWith("", ","), false);
181 }
182
183 /**
184 * @tc.name: StringStartsWith
185 * @tc.desc:
186 * @tc.type: FUNC
187 */
188 HWTEST_F(UtilitiesTest, StringStartsWith, TestSize.Level3)
189 {
190 std::string testString = "1,22,333,4444,";
191 EXPECT_EQ(StringStartsWith(testString, ""), true);
192 EXPECT_EQ(StringStartsWith(testString, "1"), true);
193 EXPECT_EQ(StringStartsWith(testString, ","), false);
194
195 EXPECT_EQ(StringStartsWith("", ""), true);
196 EXPECT_EQ(StringStartsWith("", "1"), false);
197 EXPECT_EQ(StringStartsWith("", ","), false);
198 }
199
200 /**
201 * @tc.name: VectorToString
202 * @tc.desc:
203 * @tc.type: FUNC
204 */
205 HWTEST_F(UtilitiesTest, VectorToString, TestSize.Level1)
206 {
207 EXPECT_EQ(VectorToString<std::string>({}), "<empty>");
208 EXPECT_EQ(VectorToString<std::string>({"a", "b", "c"}), "a,b,c");
209 EXPECT_EQ(VectorToString<std::string>({"a"}), "a");
210 EXPECT_EQ(VectorToString<std::vector<std::string>>({
211 {},
212 }),
213 "[<empty>]");
214 EXPECT_EQ(VectorToString<std::vector<std::string>>({
215 {"a", "b", "c"},
216 }),
217 "[a,b,c]");
218 EXPECT_EQ(VectorToString<std::vector<std::string>>({
219 {"a", "b", "c"},
220 {"a", "b", "c"},
221 {"a", "b", "c"},
222 }),
223 "[a,b,c],[a,b,c],[a,b,c]");
224
225 EXPECT_EQ(VectorToString<int>({}), "<empty>");
226 EXPECT_EQ(VectorToString<int>({1}), "1");
227 EXPECT_EQ(VectorToString<int>({1, 2, 3}), "1,2,3");
228
229 EXPECT_EQ(VectorToString<float>({}), "<empty>");
230 EXPECT_EQ(VectorToString<float>({1.0, 2.0, 3.0}), "1.000000,2.000000,3.000000");
231 }
232
233 /**
234 * @tc.name: SetToString
235 * @tc.desc:
236 * @tc.type: FUNC
237 */
238 HWTEST_F(UtilitiesTest, SetToString, TestSize.Level2)
239 {
240 EXPECT_EQ(SetToString<std::string>({}), "<empty>");
241 EXPECT_EQ(SetToString<std::string>({"a"}), "a");
242
243 EXPECT_EQ(SetToString<int>({}), "<empty>");
244 EXPECT_EQ(SetToString<int>({1}), "1");
245 EXPECT_EQ(SetToString<int>({1, 2, 3}).size(), 5);
246 EXPECT_EQ(SetToString<std::string>({"a", "b", "c"}).size(), 5);
247
248 EXPECT_EQ(SetToString<float>({}), "<empty>");
249 }
250
251 /**
252 * @tc.name: BufferToHexString
253 * @tc.desc:
254 * @tc.type: FUNC
255 */
256 HWTEST_F(UtilitiesTest, BufferToHexString, TestSize.Level2)
257 {
258 const unsigned char buf[] = "12345678";
259
260 EXPECT_STREQ(BufferToHexString(buf, 0).c_str(), "0:");
261 EXPECT_STREQ(BufferToHexString(buf, 1).c_str(), "1: 0x31");
262 EXPECT_STREQ(BufferToHexString(buf, 4).c_str(), "4: 0x31 0x32 0x33 0x34");
263 EXPECT_STREQ(BufferToHexString(buf, 5).c_str(), "5: 0x31 0x32 0x33 0x34 0x35");
264 EXPECT_STREQ(BufferToHexString(buf, 8).c_str(), "8: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
265
266 const std::vector<unsigned char> vbuf(buf, buf + sizeof(buf) - 1u);
267
268 EXPECT_STREQ(BufferToHexString(vbuf).c_str(), "8: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
269
270 const unsigned char buf2[] = "1234567812345678";
271 EXPECT_STREQ(BufferToHexString(buf2, 0).c_str(), "0:");
272 EXPECT_STREQ(BufferToHexString(buf2, 1).c_str(), "1: 0x31");
273 EXPECT_STREQ(BufferToHexString(buf2, 4).c_str(), "4: 0x31 0x32 0x33 0x34");
274 EXPECT_STREQ(BufferToHexString(buf2, 5).c_str(), "5: 0x31 0x32 0x33 0x34 0x35");
275 EXPECT_STREQ(BufferToHexString(buf2, 8).c_str(), "8: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
276 EXPECT_STREQ(BufferToHexString(buf2, 9).c_str(),
277 "9: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x31");
278 EXPECT_STREQ(
279 BufferToHexString(buf2, 16).c_str(),
280 "16: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
281
282 const std::vector<unsigned char> vbuf2(buf2, buf2 + sizeof(buf2) - 1u);
283 EXPECT_STREQ(
284 BufferToHexString(vbuf2).c_str(),
285 "16: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
286 }
287
288 /**
289 * @tc.name: HexDump
290 * @tc.desc:
291 * @tc.type: FUNC
292 */
293 HWTEST_F(UtilitiesTest, HexDump, TestSize.Level2)
294 {
295 const unsigned char buf[] = "12345678";
296 const void *vbuf = static_cast<const void *>(buf);
297 ScopeDebugLevel tempLogLevel(LEVEL_MUCH, true);
298
299 StdoutRecord stdoutRecord;
300 stdoutRecord.Start();
301 EXPECT_EQ(HexDump(vbuf, 0), true);
302 EXPECT_EQ(HexDump(vbuf, 1), true);
303 EXPECT_EQ(HexDump(vbuf, 4), true);
304 EXPECT_EQ(HexDump(vbuf, 5), true);
305 EXPECT_EQ(HexDump(vbuf, 8), true);
306 stdoutRecord.Stop();
307 }
308
309 /**
310 * @tc.name: StringTrim
311 * @tc.desc:
312 * @tc.type: FUNC
313 */
314 HWTEST_F(UtilitiesTest, StringTrim, TestSize.Level3)
315 {
316 std::string test;
317 EXPECT_STREQ(StringTrim(test = " a ").c_str(), "a");
318 EXPECT_STREQ(StringTrim(test = " a").c_str(), "a");
319 EXPECT_STREQ(StringTrim(test = "a ").c_str(), "a");
320 EXPECT_STREQ(StringTrim(test = " a1a ").c_str(), "a1a");
321 EXPECT_STREQ(StringTrim(test = " a1a").c_str(), "a1a");
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 }
327
328 /**
329 * @tc.name: RecordStdout
330 * @tc.desc:
331 * @tc.type: FUNC
332 */
333 HWTEST_F(UtilitiesTest, RecordStdout, TestSize.Level2)
334 {
335 StdoutRecord stdoutRecord;
336
337 ASSERT_EQ(stdoutRecord.Start(), true);
338 printf("line1: abc\n");
339 printf("line2: def\n");
340 printf("line3: ghi\n");
341 printf("\n");
342 std::string out = stdoutRecord.Stop();
343
344 printf("stdoutRecord:\n%s", out.c_str());
345 EXPECT_EQ(out.empty(), false);
346 EXPECT_NE(out.find("line1:"), std::string::npos);
347 EXPECT_NE(out.find("line2:"), std::string::npos);
348 EXPECT_NE(out.find("line3:"), std::string::npos);
349 EXPECT_EQ(out.find("line4:"), std::string::npos);
350 }
351
352 /**
353 * @tc.name: IsDigits
354 * @tc.desc:
355 * @tc.type: FUNC
356 */
357 HWTEST_F(UtilitiesTest, IsDigits, TestSize.Level1)
358 {
359 EXPECT_EQ(IsDigits(""), false);
360 EXPECT_EQ(IsDigits("1"), true);
361 EXPECT_EQ(IsDigits("12"), true);
362 EXPECT_EQ(IsDigits("1a"), false);
363 EXPECT_EQ(IsDigits("a1"), false);
364 EXPECT_EQ(IsDigits("1a2"), false);
365 EXPECT_EQ(IsDigits("a1b"), false);
366 EXPECT_EQ(IsDigits("_1"), false);
367 EXPECT_EQ(IsDigits("1_"), false);
368 }
369
370 /**
371 * @tc.name: IsHexxDigits
372 * @tc.desc:
373 * @tc.type: FUNC
374 */
375 HWTEST_F(UtilitiesTest, IsHexxDigits, TestSize.Level2)
376 {
377 EXPECT_EQ(IsHexDigits(""), false);
378 EXPECT_EQ(IsHexDigits("1"), true);
379 EXPECT_EQ(IsHexDigits("12"), true);
380 EXPECT_EQ(IsHexDigits("1a"), true);
381 EXPECT_EQ(IsHexDigits("f1"), true);
382 EXPECT_EQ(IsHexDigits("1f2"), true);
383 EXPECT_EQ(IsHexDigits("a1f"), true);
384 EXPECT_EQ(IsHexDigits("g1"), false);
385 EXPECT_EQ(IsHexDigits("1g"), false);
386 EXPECT_EQ(IsHexDigits("_1"), false);
387 EXPECT_EQ(IsHexDigits("1_"), false);
388 }
389
390 /**
391 * @tc.name: IsSameCommand
392 * @tc.desc:
393 * @tc.type: FUNC
394 */
395 HWTEST_F(UtilitiesTest, IsSameCommand, TestSize.Level2)
396 {
397 EXPECT_EQ(IsSameCommand("", ""), false);
398 EXPECT_EQ(IsSameCommand("a", ""), false);
399 EXPECT_EQ(IsSameCommand("", "b"), false);
400 EXPECT_EQ(IsSameCommand("1", "2"), false);
401 EXPECT_EQ(IsSameCommand("2", "1"), false);
402 EXPECT_EQ(IsSameCommand("1", "1"), true);
403 EXPECT_EQ(IsSameCommand("a", "a"), true);
404 EXPECT_EQ(IsSameCommand("a:1", "a:2"), false);
405 }
406
407 /**
408 * @tc.name: CompressFile
409 * @tc.desc:
410 * @tc.type: FUNC
411 */
412 HWTEST_F(UtilitiesTest, CompressFile, TestSize.Level1)
413 {
414 std::string srcPath = "./resource/testdata/elf_test_stripped_broken";
415 std::string destPath = "./test.gz";
416 EXPECT_EQ(CompressFile(srcPath, destPath), true);
417 srcPath = "";
418 EXPECT_EQ(CompressFile(srcPath, destPath), false);
419 srcPath = "./resource/testdata/elf_test_stripped_broken";
420 destPath = "";
421 EXPECT_EQ(CompressFile(srcPath, destPath), false);
422 }
423
424 /**
425 * @tc.name: UncompressFile
426 * @tc.desc:
427 * @tc.type: FUNC
428 */
429 HWTEST_F(UtilitiesTest, UncompressFile, TestSize.Level2)
430 {
431 std::string gzipPath = "./test.gz";
432 std::string dataPath = "./test";
433 EXPECT_EQ(UncompressFile(gzipPath, dataPath), true);
434 gzipPath = "./test.gz";
435 dataPath = "";
436 EXPECT_EQ(UncompressFile(gzipPath, dataPath), false);
437 gzipPath = "";
438 dataPath = "./resource/testdata/elf_test_stripped_broken";
439 EXPECT_EQ(UncompressFile(gzipPath, dataPath), false);
440 }
441
442 /**
443 * @tc.name: StringPrintf
444 * @tc.desc:
445 * @tc.type: FUNC
446 */
447 HWTEST_F(UtilitiesTest, StringPrintf, TestSize.Level1)
448 {
449 EXPECT_STREQ(StringPrintf("").c_str(), "");
450 EXPECT_STREQ(StringPrintf("123").c_str(), "123");
451 EXPECT_STREQ(StringPrintf("%d%s%c", 1, "2", 'c').c_str(), "12c");
452 EXPECT_STREQ(StringPrintf("%d%s%c\t\n", 1, "2", 'c').c_str(), "12c\t\n");
453
454 char format[PATH_MAX + 1];
455 std::fill(format, format + PATH_MAX, ' ');
456 format[PATH_MAX] = 0;
457 EXPECT_STRNE(StringPrintf(format).c_str(), format);
458 format[PATH_MAX - 1] = 0;
459 EXPECT_STREQ(StringPrintf(format).c_str(), format);
460 EXPECT_STREQ(StringPrintf(nullptr).c_str(), "");
461 }
462
463 /**
464 * @tc.name: GetEntriesInDir
465 * @tc.desc:
466 * @tc.type: FUNC
467 */
468 HWTEST_F(UtilitiesTest, GetEntriesInDir, TestSize.Level1)
469 {
470 std::vector<std::string> dirFileInfo;
471 dirFileInfo = GetEntriesInDir("./");
472 EXPECT_GE(dirFileInfo.size(), 0u);
473 }
474
475 /**
476 * @tc.name: GetSubDirs
477 * @tc.desc:
478 * @tc.type: FUNC
479 */
480 HWTEST_F(UtilitiesTest, GetSubDirs, TestSize.Level1)
481 {
482 std::vector<std::string> subDirFileInfo;
483 subDirFileInfo = GetSubDirs("../");
484 EXPECT_GE(subDirFileInfo.size(), 0u);
485 }
486
487 /**
488 * @tc.name: IsDir
489 * @tc.desc:
490 * @tc.type: FUNC
491 */
492 HWTEST_F(UtilitiesTest, IsDir, TestSize.Level1)
493 {
494 bool ret = IsDir("../");
495 EXPECT_EQ(ret, true);
496 }
497
498 /**
499 * @tc.name: IsPath
500 * @tc.desc:
501 * @tc.type: FUNC
502 */
503 HWTEST_F(UtilitiesTest, IsPath, TestSize.Level1)
504 {
505 bool ret = IsPath("./");
506 EXPECT_EQ(ret, true);
507 }
508
509 /**
510 * @tc.name: PlatformPathConvert
511 * @tc.desc:
512 * @tc.type: FUNC
513 */
514 HWTEST_F(UtilitiesTest, PlatformPathConvert, TestSize.Level1)
515 {
516 EXPECT_GE(PlatformPathConvert("./").length(), 0u);
517 }
518
519 /**
520 * @tc.name: ToHex
521 * @tc.desc:
522 * @tc.type: FUNC
523 */
524 HWTEST_F(UtilitiesTest, ToHex, TestSize.Level2)
525 {
526 unsigned char hVal = 'G';
527 EXPECT_STREQ(ToHex(hVal, 1, true).c_str(), "0x47");
528 }
529
530 /**
531 * @tc.name: ToHex
532 * @tc.desc:
533 * @tc.type: FUNC
534 */
535 HWTEST_F(UtilitiesTest, CopyFromBufferAndMove, TestSize.Level1)
536 {
537 unsigned char *buffer = new unsigned char[4];
538 buffer[0] = '1';
539 buffer[1] = '2';
540 buffer[2] = '3';
541 buffer[3] = '4';
542 int *dest = new int;
543 const unsigned char *srcStr = buffer;
544 EXPECT_EQ(CopyFromBufferAndMove(srcStr, dest, 4), 4u);
545 }
546
547 /**
548 * @tc.name: ReadIntFromProcFile01
549 * @tc.desc:
550 * @tc.type: FUNC
551 */
552 HWTEST_F(UtilitiesTest, ReadIntFromProcFile01, TestSize.Level1)
553 {
554 std::string strPath = "/proc/sys/kernel/perf_cpu_time_max_percent";
555 int strLen = 0;
556 EXPECT_EQ(ReadIntFromProcFile(strPath, strLen), true);
557 ASSERT_GT(strLen, 0);
558 }
559
560 /**
561 * @tc.name: ReadIntFromProcFile02
562 * @tc.desc:
563 * @tc.type: FUNC
564 */
565 HWTEST_F(UtilitiesTest, ReadIntFromProcFile02, TestSize.Level1)
566 {
567 std::string strPath = "/proc/sys/kernel/perf_event_max_sample_rate";
568 int strLen = 0;
569 EXPECT_EQ(ReadIntFromProcFile(strPath, strLen), true);
570 ASSERT_GT(strLen, 0);
571 }
572
573 /**
574 * @tc.name: ReadIntFromProcFile03
575 * @tc.desc:
576 * @tc.type: FUNC
577 */
578 HWTEST_F(UtilitiesTest, ReadIntFromProcFile03, TestSize.Level2)
579 {
580 std::string strPath = "/sys/kernel/tracing/saved_cmdlines_size";
581 int strLen = 0;
582 EXPECT_EQ(ReadIntFromProcFile(strPath, strLen), true);
583 ASSERT_GT(strLen, 0);
584 }
585
586 /**
587 * @tc.name: WriteIntToProcFile
588 * @tc.desc:
589 * @tc.type: FUNC
590 */
591 HWTEST_F(UtilitiesTest, WriteIntToProcFile, TestSize.Level2)
592 {
593 std::string strPath = "./hiperf_log.txt";
594 int strVal = 0;
595 EXPECT_EQ(WriteIntToProcFile(strPath, strVal), true);
596 }
597
598 /**
599 * @tc.name: ReadFileToString
600 * @tc.desc:
601 * @tc.type: FUNC
602 */
603 HWTEST_F(UtilitiesTest, ReadFileToString, TestSize.Level1)
604 {
605 std::string strPath = "./hiperf_log.txt";
606 EXPECT_NE(ReadFileToString(strPath).length(), 0u);
607 }
608
609 /**
610 * @tc.name: WriteStringToFile
611 * @tc.desc:
612 * @tc.type: FUNC
613 */
614 HWTEST_F(UtilitiesTest, WriteStringToFile, TestSize.Level1)
615 {
616 std::string strPath = "./hiperf_log.txt";
617 std::string content = "0";
618 EXPECT_EQ(WriteStringToFile(strPath, content), true);
619 }
620
621 /**
622 * @tc.name: Percentage
623 * @tc.desc:
624 * @tc.type: FUNC
625 */
626 HWTEST_F(UtilitiesTest, Percentage, TestSize.Level2)
627 {
628 EXPECT_EQ(Percentage(99, 100), 99);
629 }
630
631 /**
632 * @tc.name: IsRoot
633 * @tc.desc:
634 * @tc.type: FUNC
635 */
636 HWTEST_F(UtilitiesTest, IsRoot, TestSize.Level1)
637 {
638 bool isRoot = true;
639 #if is_linux || is_ohos
640 isRoot = (getuid() == 0);
641 #endif
642 EXPECT_EQ(IsRoot(), isRoot);
643 }
644
645 /**
646 * @tc.name: PowerOfTwo
647 * @tc.desc:
648 * @tc.type: FUNC
649 */
650 HWTEST_F(UtilitiesTest, PowerOfTwo, TestSize.Level1)
651 {
652 EXPECT_EQ(PowerOfTwo(1), true);
653 }
654
655 /**
656 * @tc.name: GetSubthreadIDs
657 * @tc.desc:
658 * @tc.type: FUNC
659 */
660 HWTEST_F(UtilitiesTest, GetSubthreadIDs, TestSize.Level1)
661 {
662 StartThreads(1);
663 std::vector<pid_t> tids = GetSubthreadIDs(getpid());
664 if (!HasFailure()) {
665 for (pid_t tid : tids_) {
666 EXPECT_NE(find(tids.begin(), tids.end(), tid), tids.end());
667 }
668 }
669 ExitThreads();
670 }
671
672 /**
673 * @tc.name: IsBeta
674 * @tc.desc:
675 * @tc.type: FUNC
676 */
677 HWTEST_F(UtilitiesTest, IsBeta, TestSize.Level2)
678 {
679 EXPECT_EQ(IsBeta(), true);
680 }
681
682 HWTEST_F(UtilitiesTest, CanonicalizeSpecPath, TestSize.Level0)
683 {
684 EXPECT_EQ(CanonicalizeSpecPath(nullptr), "");
685 EXPECT_EQ(CanonicalizeSpecPath("/data/local/tmp/test/../test.txt"), "");
686 EXPECT_EQ(CanonicalizeSpecPath("/data/local/tmp/nonexistent.txt"), "/data/local/tmp/nonexistent.txt");
687 string largePath = "./";
688 for (int i = 0; i < 512; i++) { // 512: loop size
689 largePath += "testpath";
690 }
691 largePath += ".txt";
692 EXPECT_EQ(CanonicalizeSpecPath(largePath.c_str()), "");
693 }
694
695 /**
696 * @tc.name: RecordStdoutInit
697 * @tc.desc:
698 * @tc.type: FUNC
699 */
700 HWTEST_F(UtilitiesTest, RecordStdoutInit, TestSize.Level1)
701 {
702 StdoutRecord stdnormaloutRecord("/data/local/tmp/hiperf_log.txt", "rw");
703 (void)stdnormaloutRecord.Stop();
704 StdoutRecord stdexceptoutRecord("/data/local/tmp/../hiperf_log.txt");
705 EXPECT_EQ(stdexceptoutRecord.Stop().empty(), true);
706 }
707
708 /**
709 * @tc.name: CollectPidsByAppname1
710 * @tc.desc:
711 * @tc.type: FUNC
712 */
713 HWTEST_F(UtilitiesTest, CollectPidsByAppname1, TestSize.Level1)
714 {
715 pid_t pid = getpid();
716 std::string name = GetProcessName(pid);
717 size_t pos = name.find_last_of("/");
718 if (pos != std::string::npos) {
719 name = name.substr(pos + 1);
720 }
721 std::set<pid_t> pids = {};
722 CollectPidsByAppname(pids, name);
723 ASSERT_GE(pids.size(), 1u);
724 bool get = false;
725 for (pid_t id : pids) {
726 if (pid == id) {
727 get = true;
728 break;
729 }
730 }
731 EXPECT_EQ(get, true);
732 }
733
734 /**
735 * @tc.name: CollectPidsByAppname2
736 * @tc.desc:
737 * @tc.type: FUNC
738 */
739 HWTEST_F(UtilitiesTest, CollectPidsByAppname2, TestSize.Level1)
740 {
741 pid_t pid = getpid();
742 std::string name = GetProcessName(pid);
743 size_t pos = name.find_last_of("/");
744 if (pos != std::string::npos) {
745 name = name.substr(pos + 1);
746 }
747 std::vector<std::string> names = { name };
748 std::set<pid_t> pids = {};
749 CollectPidsByAppname(pids, names);
750 ASSERT_GE(pids.size(), 1u);
751 bool get = false;
752 for (pid_t id : pids) {
753 if (pid == id) {
754 get = true;
755 break;
756 }
757 }
758 EXPECT_EQ(get, true);
759 }
760
761 /**
762 * @tc.name: CheckOutOfRange1
763 * @tc.desc:
764 * @tc.type: FUNC
765 */
766 HWTEST_F(UtilitiesTest, CheckOutOfRange1, TestSize.Level0)
767 {
768 static constexpr int min = 10;
769 static constexpr int max = 20;
770 static constexpr int val = 8;
771 EXPECT_EQ(CheckOutOfRange<int>(val, min, max), true);
772 }
773
774 /**
775 * @tc.name: CheckOutOfRange2
776 * @tc.desc:
777 * @tc.type: FUNC
778 */
779 HWTEST_F(UtilitiesTest, CheckOutOfRange2, TestSize.Level3)
780 {
781 static constexpr int min = 10;
782 static constexpr int max = 20;
783 static constexpr int val = 10;
784 EXPECT_EQ(CheckOutOfRange<int>(val, min, max), false);
785 }
786
787 /**
788 * @tc.name: CheckOutOfRange3
789 * @tc.desc:
790 * @tc.type: FUNC
791 */
792 HWTEST_F(UtilitiesTest, CheckOutOfRange3, TestSize.Level3)
793 {
794 static constexpr int min = 10;
795 static constexpr int max = 20;
796 static constexpr int val = 15;
797 EXPECT_EQ(CheckOutOfRange<int>(val, min, max), false);
798 }
799
800 /**
801 * @tc.name: CheckOutOfRange4
802 * @tc.desc:
803 * @tc.type: FUNC
804 */
805 HWTEST_F(UtilitiesTest, CheckOutOfRange4, TestSize.Level3)
806 {
807 static constexpr int min = 10;
808 static constexpr int max = 20;
809 static constexpr int val = 20;
810 EXPECT_EQ(CheckOutOfRange<int>(val, min, max), false);
811 }
812
813 /**
814 * @tc.name: CheckOutOfRange5
815 * @tc.desc:
816 * @tc.type: FUNC
817 */
818 HWTEST_F(UtilitiesTest, CheckOutOfRange5, TestSize.Level1)
819 {
820 static constexpr int min = 10;
821 static constexpr int max = 20;
822 static constexpr int val = 25;
823 EXPECT_EQ(CheckOutOfRange<int>(val, min, max), true);
824 }
825
826 /**
827 * @tc.name: IsSameCommand
828 * @tc.desc:
829 * @tc.type: FUNC
830 */
831 HWTEST_F(UtilitiesTest, IsSameCommand2, TestSize.Level2)
832 {
833 std::vector<std::string> v = {""};
834 EXPECT_EQ(IsSameCommand("", v), false);
835 EXPECT_EQ(IsSameCommand("a", v), false);
836
837 v = {"", "a"};
838 EXPECT_EQ(IsSameCommand("a", v), true);
839 }
840
841 /**
842 * @tc.name: IsArkJsFile
843 * @tc.desc:
844 * @tc.type: FUNC
845 */
846 HWTEST_F(UtilitiesTest, IsArkJsFile, TestSize.Level2)
847 {
848 EXPECT_EQ(IsArkJsFile("test.hap"), true);
849 EXPECT_EQ(IsArkJsFile("[anon:ArkTS Code:test.so/buffer.js]"), true);
850 EXPECT_EQ(IsArkJsFile("test.hsp"), true);
851 EXPECT_EQ(IsArkJsFile("test.abc"), true);
852 EXPECT_EQ(IsArkJsFile("test.hqf"), true);
853 EXPECT_EQ(IsArkJsFile("test.so"), false);
854 }
855
856 /**
857 * @tc.name: IsDirectoryExists
858 * @tc.desc:
859 * @tc.type: FUNC
860 */
861 HWTEST_F(UtilitiesTest, IsDirectoryExists, TestSize.Level1)
862 {
863 EXPECT_EQ(IsDirectoryExists("/data/local/tmp"), true);
864 }
865
866 /**
867 * @tc.name: CreateDirectory
868 * @tc.desc:
869 * @tc.type: FUNC
870 */
871 HWTEST_F(UtilitiesTest, CreateDirectory, TestSize.Level1)
872 {
873 std::string file = "/data/local/tmp/hiperf_test";
874 EXPECT_TRUE(CreateDirectory(file, HIPERF_FILE_PERM_770));
875 rmdir(file.c_str());
876 }
877
878 /**
879 * @tc.name: IsValidOutPath
880 * @tc.desc:
881 * @tc.type: FUNC
882 */
883 HWTEST_F(UtilitiesTest, IsValidOutPath, TestSize.Level1)
884 {
885 std::string file = "/data/local/tmp/perf.data";
886 EXPECT_TRUE(IsValidOutPath(file));
887 }
888
889 /**
890 * @tc.name: IsValidOutPathErr
891 * @tc.desc:
892 * @tc.type: FUNC
893 */
894 HWTEST_F(UtilitiesTest, IsValidOutPathErr, TestSize.Level2)
895 {
896 std::string file = "/data/log/hiperflog/perf.data";
897 EXPECT_FALSE(IsValidOutPath(file));
898 }
899
900 /**
901 * @tc.name: StringToIntTest
902 * @tc.desc: Test StringToUint64 function.
903 * @tc.type: FUNC
904 */
905 HWTEST_F(UtilitiesTest, StringToIntTest, TestSize.Level2)
906 {
907 std::string traceParamsStr = "1234567890";
908 uint64_t paramsUint64 = 0;
909 EXPECT_TRUE(StringToUint64(traceParamsStr, paramsUint64));
910 EXPECT_EQ(paramsUint64, 1234567890); // 1234567890: test value
911 traceParamsStr = "18446744073709551615";
912 EXPECT_TRUE(StringToUint64(traceParamsStr, paramsUint64));
913 EXPECT_EQ(paramsUint64, ULLONG_MAX);
914 }
915
916 /**
917 * @tc.name: StringToUint64ErrorTest
918 * @tc.desc: Test StringToUint64 function.
919 * @tc.type: FUNC
920 */
921 HWTEST_F(UtilitiesTest, StringToUint64ErrorTest, TestSize.Level2)
922 {
923 std::string traceParamsStr = "-1234567890";
924 uint64_t paramsUint64 = 0;
925 EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64));
926 traceParamsStr = "a123";
927 EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64));
928 traceParamsStr = "";
929 EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64));
930 traceParamsStr = "12a3";
931 EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64));
932 traceParamsStr = "abc";
933 EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64));
934 traceParamsStr = ".1";
935 EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64));
936 traceParamsStr = "1.1";
937 EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64));
938 traceParamsStr = "18446744073709551616";
939 EXPECT_FALSE(StringToUint64(traceParamsStr, paramsUint64));
940 }
941
942 /**
943 * @tc.name: IsV8File
944 * @tc.desc: Test IsV8File function.
945 * @tc.type: FUNC
946 */
947 HWTEST_F(UtilitiesTest, IsV8File, TestSize.Level2)
948 {
949 std::string filepath = "[anon:JSVM_JIT]";
950 EXPECT_TRUE(IsV8File(filepath));
951 filepath = "[anon:ARKWEB_JIT]";
952 EXPECT_TRUE(IsV8File(filepath));
953 filepath = "[anon:v8]";
954 EXPECT_TRUE(IsV8File(filepath));
955 filepath = "[anon:test]";
956 EXPECT_FALSE(IsV8File(filepath));
957 filepath = "/system/lib64/libv8_shared.so";
958 EXPECT_FALSE(IsV8File(filepath));
959 }
960
961 /**
962 * @tc.name: IscontainDigits_NoDigits_PureAlpha
963 * @tc.desc: Test string without digits (pure alphabet)
964 * @tc.type: FUNC
965 */
966 HWTEST_F(UtilitiesTest, IscontainDigitsNoDigitsPureAlpha, TestSize.Level1)
967 {
968 std::string str = "abcdefg";
969 bool result = IscontainDigits(str);
970 EXPECT_FALSE(result);
971 }
972
973 /**
974 * @tc.name: IsExistDebugByPid_InvalidPid_Negative
975 * @tc.desc: Test negative PID (e.g., -1)
976 * @tc.type: FUNC
977 */
978 HWTEST_F(UtilitiesTest, IsExistDebugByPidInvalidPidNegative, TestSize.Level2)
979 {
980 std::vector<pid_t> pids = {-1};
981 std::string err;
982
983 StdoutRecord stdoutRecord;
984 stdoutRecord.Start();
985
986 bool result = IsExistDebugByPid(pids, err);
987 std::string output = stdoutRecord.Stop();
988
989 EXPECT_FALSE(result);
990 EXPECT_EQ(err, "Invalid -p value '-1', the pid should be larger than 0\n");
991 EXPECT_NE(output.find("Invalid -p value '-1', the pid should be larger than 0"), std::string::npos);
992 }
993
994 /**
995 * @tc.name: IsNumeric_Invalid_WithAlpha
996 * @tc.desc: Test string with numbers and alphabet (invalid)
997 * @tc.type: FUNC
998 */
999 HWTEST_F(UtilitiesTest, IsNumericInvalidWithAlpha, TestSize.Level1)
1000 {
1001 std::string str = "123a";
1002 EXPECT_FALSE(IsNumeric(str));
1003 }
1004
1005 /**
1006 * @tc.name: IsDebugableApp
1007 * @tc.desc: Test IsDebugableApp fun
1008 * @tc.type: FUNC
1009 */
1010 HWTEST_F(UtilitiesTest, IsDebugableApp, TestSize.Level1)
1011 {
1012 std::string testProcesses = "com.ohos.launcher";
1013 if (!CheckTestApp(testProcesses)) {
1014 testProcesses = "hiview";
1015 }
1016 EXPECT_FALSE(IsDebugableApp(testProcesses));
1017 }
1018 } // namespace HiPerf
1019 } // namespace Developtools
1020 } // namespace OHOS
1021