• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "utilities.h"
20 
21 using namespace testing::ext;
22 namespace OHOS {
23 namespace Developtools {
24 namespace HiPerf {
25 class UtilitiesTest : public testing::Test {
26 public:
27     static void SetUpTestCase(void);
28     static void TearDownTestCase(void);
29     void SetUp();
30     void TearDown();
31     void TestThread();
32     void StartThreads(const size_t count);
33     void ExitThreads();
34     bool exitThreads_ = true;
35     std::vector<pid_t> tids_;
36     std::vector<std::thread> threads_;
37     const int sleepTime_ = {500};
38 };
39 
SetUpTestCase()40 void UtilitiesTest::SetUpTestCase() {}
41 
TearDownTestCase()42 void UtilitiesTest::TearDownTestCase() {}
43 
SetUp()44 void UtilitiesTest::SetUp() {}
45 
TearDown()46 void UtilitiesTest::TearDown() {}
47 
TestThread()48 void UtilitiesTest::TestThread()
49 {
50     printf("threads %ld create\n", gettid());
51     int ret = fflush(nullptr);
52     if (ret == EOF) {
53         printf("fflush() error\n");
54     }
55 
56     tids_.emplace_back(gettid());
57     while (!exitThreads_) {
58         std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime_));
59     }
60     printf("threads %ld exited\n", gettid());
61     ret = fflush(nullptr);
62     if (ret == EOF) {
63         printf("fflush() error\n");
64     }
65 }
66 
StartThreads(const size_t count)67 void UtilitiesTest::StartThreads(const size_t count)
68 {
69     printf("create %zu threads\n", count);
70     int ret = fflush(nullptr);
71     if (ret == EOF) {
72         printf("fflush() error\n");
73     }
74 
75     exitThreads_ = false;
76     size_t created = 0;
77     while (created < count) {
78         threads_.emplace_back(std::thread(&UtilitiesTest::TestThread, this));
79         created++;
80     }
81     while (tids_.size() < count) {
82         std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime_));
83     }
84     printf("all threads created\n");
85     ret = fflush(nullptr);
86     if (ret == EOF) {
87         printf("fflush() error\n");
88     }
89 }
90 
ExitThreads()91 void UtilitiesTest::ExitThreads()
92 {
93     printf("wait all threads exit\n");
94     exitThreads_ = true;
95     for (std::thread &t : this->threads_) {
96         t.join();
97     }
98     tids_.clear();
99     printf("all threads exited\n");
100 }
101 
102 /**
103  * @tc.name: StringReplace
104  * @tc.desc:
105  * @tc.type: FUNC
106  */
107 HWTEST_F(UtilitiesTest, StringReplace, TestSize.Level1)
108 {
109     const std::string testString = "1234567890";
110     EXPECT_EQ(StringReplace(testString, "1", ""), "234567890");
111     EXPECT_EQ(StringReplace(testString, "2", ""), "134567890");
112     EXPECT_EQ(StringReplace(testString, "0", ""), "123456789");
113     EXPECT_EQ(StringReplace(testString, "1", "0"), "0234567890");
114     EXPECT_EQ(StringReplace(testString, "0", "1"), "1234567891");
115     EXPECT_EQ(StringReplace(testString, "123", "1"), "14567890");
116     EXPECT_EQ(StringReplace(testString, "890", "1"), "12345671");
117     EXPECT_EQ(StringReplace(testString, "456", "1"), "12317890");
118     EXPECT_EQ(StringReplace(testString, "123", "321"), "3214567890");
119     EXPECT_EQ(StringReplace(testString, "890", "098"), "1234567098");
120 }
121 
122 /**
123  * @tc.name: StringSplit
124  * @tc.desc:
125  * @tc.type: FUNC
126  */
127 HWTEST_F(UtilitiesTest, StringSplit, TestSize.Level1)
128 {
129     std::string testString = "1,23,456,7890,";
130     size_t testSize = testString.size();
131     EXPECT_EQ(StringSplit(testString, "1").size(), 1u);
132     EXPECT_EQ(StringSplit(testString, "2").size(), 2u);
133     EXPECT_EQ(StringSplit(testString, ",").size(), 4u);
134     EXPECT_EQ(StringSplit(testString, "456").size(), 2u);
135     EXPECT_EQ(StringSplit(testString, "000").size(), 1u);
136     EXPECT_EQ(StringSplit(testString, "").size(), 1u);
137     // dont change the input string
138     EXPECT_EQ(testString.size(), testSize);
139 
140     EXPECT_EQ(StringSplit(testString = "").size(), 0u);
141     EXPECT_EQ(StringSplit(testString = "1,2,3").size(), 3u);
142     EXPECT_EQ(StringSplit(testString = "1,2,3,,,").size(), 3u);
143 }
144 
145 /**
146  * @tc.name: SubStringCount
147  * @tc.desc:
148  * @tc.type: FUNC
149  */
150 HWTEST_F(UtilitiesTest, SubStringCount, TestSize.Level1)
151 {
152     std::string testString = "1,22,333,4444,";
153     EXPECT_EQ(SubStringCount(testString, ""), testString.size());
154     EXPECT_EQ(SubStringCount(testString, "1"), 1u);
155     EXPECT_EQ(SubStringCount(testString, "2"), 2u);
156     EXPECT_EQ(SubStringCount(testString, "3"), 3u);
157     EXPECT_EQ(SubStringCount(testString, "4"), 4u);
158 
159     EXPECT_EQ(SubStringCount(testString, "22"), 1u);
160     EXPECT_EQ(SubStringCount(testString, "33"), 1u);
161     EXPECT_EQ(SubStringCount(testString, "333"), 1u);
162     EXPECT_EQ(SubStringCount(testString, "4444"), 1u);
163     EXPECT_EQ(SubStringCount(testString, "444"), 1u);
164     EXPECT_EQ(SubStringCount(testString, "44"), 2u);
165 }
166 
167 /**
168  * @tc.name: StringEndsWith
169  * @tc.desc:
170  * @tc.type: FUNC
171  */
172 HWTEST_F(UtilitiesTest, StringEndsWith, TestSize.Level1)
173 {
174     std::string testString = "1,22,333,4444,";
175     EXPECT_EQ(StringEndsWith(testString, ""), true);
176     EXPECT_EQ(StringEndsWith(testString, "1"), false);
177     EXPECT_EQ(StringEndsWith(testString, ","), true);
178 
179     EXPECT_EQ(StringEndsWith("", ""), true);
180     EXPECT_EQ(StringEndsWith("", "1"), false);
181     EXPECT_EQ(StringEndsWith("", ","), false);
182 }
183 
184 /**
185  * @tc.name: StringStartsWith
186  * @tc.desc:
187  * @tc.type: FUNC
188  */
189 HWTEST_F(UtilitiesTest, StringStartsWith, TestSize.Level1)
190 {
191     std::string testString = "1,22,333,4444,";
192     EXPECT_EQ(StringStartsWith(testString, ""), true);
193     EXPECT_EQ(StringStartsWith(testString, "1"), true);
194     EXPECT_EQ(StringStartsWith(testString, ","), false);
195 
196     EXPECT_EQ(StringStartsWith("", ""), true);
197     EXPECT_EQ(StringStartsWith("", "1"), false);
198     EXPECT_EQ(StringStartsWith("", ","), false);
199 }
200 
201 /**
202  * @tc.name: VectorToString
203  * @tc.desc:
204  * @tc.type: FUNC
205  */
206 HWTEST_F(UtilitiesTest, VectorToString, TestSize.Level1)
207 {
208     EXPECT_EQ(VectorToString<std::string>({}), "<empty>");
209     EXPECT_EQ(VectorToString<std::string>({"a", "b", "c"}), "a,b,c");
210     EXPECT_EQ(VectorToString<std::string>({"a"}), "a");
211     EXPECT_EQ(VectorToString<std::vector<std::string>>({
212                   {},
213               }),
214               "[<empty>]");
215     EXPECT_EQ(VectorToString<std::vector<std::string>>({
216                   {"a", "b", "c"},
217               }),
218               "[a,b,c]");
219     EXPECT_EQ(VectorToString<std::vector<std::string>>({
220                   {"a", "b", "c"},
221                   {"a", "b", "c"},
222                   {"a", "b", "c"},
223               }),
224               "[a,b,c],[a,b,c],[a,b,c]");
225 
226     EXPECT_EQ(VectorToString<int>({}), "<empty>");
227     EXPECT_EQ(VectorToString<int>({1}), "1");
228     EXPECT_EQ(VectorToString<int>({1, 2, 3}), "1,2,3");
229 
230     EXPECT_EQ(VectorToString<float>({}), "<empty>");
231     EXPECT_EQ(VectorToString<float>({1.0, 2.0, 3.0}), "1.000000,2.000000,3.000000");
232 }
233 
234 /**
235  * @tc.name: SetToString
236  * @tc.desc:
237  * @tc.type: FUNC
238  */
239 HWTEST_F(UtilitiesTest, SetToString, TestSize.Level1)
240 {
241     EXPECT_EQ(SetToString<std::string>({}), "<empty>");
242     EXPECT_EQ(SetToString<std::string>({"a"}), "a");
243 
244     EXPECT_EQ(SetToString<int>({}), "<empty>");
245     EXPECT_EQ(SetToString<int>({1}), "1");
246     EXPECT_EQ(SetToString<int>({1, 2, 3}).size(), 5);
247     EXPECT_EQ(SetToString<std::string>({"a", "b", "c"}).size(), 5);
248 
249     EXPECT_EQ(SetToString<float>({}), "<empty>");
250 }
251 
252 /**
253  * @tc.name: BufferToHexString
254  * @tc.desc:
255  * @tc.type: FUNC
256  */
257 HWTEST_F(UtilitiesTest, BufferToHexString, TestSize.Level1)
258 {
259     const unsigned char buf[] = "12345678";
260 
261     EXPECT_STREQ(BufferToHexString(buf, 0).c_str(), "0:");
262     EXPECT_STREQ(BufferToHexString(buf, 1).c_str(), "1: 0x31");
263     EXPECT_STREQ(BufferToHexString(buf, 4).c_str(), "4: 0x31 0x32 0x33 0x34");
264     EXPECT_STREQ(BufferToHexString(buf, 5).c_str(), "5: 0x31 0x32 0x33 0x34 0x35");
265     EXPECT_STREQ(BufferToHexString(buf, 8).c_str(), "8: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
266 
267     const std::vector<unsigned char> vbuf(buf, buf + sizeof(buf) - 1u);
268 
269     EXPECT_STREQ(BufferToHexString(vbuf).c_str(), "8: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
270 
271     const unsigned char buf2[] = "1234567812345678";
272     EXPECT_STREQ(BufferToHexString(buf2, 0).c_str(), "0:");
273     EXPECT_STREQ(BufferToHexString(buf2, 1).c_str(), "1: 0x31");
274     EXPECT_STREQ(BufferToHexString(buf2, 4).c_str(), "4: 0x31 0x32 0x33 0x34");
275     EXPECT_STREQ(BufferToHexString(buf2, 5).c_str(), "5: 0x31 0x32 0x33 0x34 0x35");
276     EXPECT_STREQ(BufferToHexString(buf2, 8).c_str(), "8: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
277     EXPECT_STREQ(BufferToHexString(buf2, 9).c_str(),
278                  "9: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x31");
279     EXPECT_STREQ(
280         BufferToHexString(buf2, 16).c_str(),
281         "16: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
282 
283     const std::vector<unsigned char> vbuf2(buf2, buf2 + sizeof(buf2) - 1u);
284     EXPECT_STREQ(
285         BufferToHexString(vbuf2).c_str(),
286         "16: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38");
287 }
288 
289 /**
290  * @tc.name: HexDump
291  * @tc.desc:
292  * @tc.type: FUNC
293  */
294 HWTEST_F(UtilitiesTest, HexDump, TestSize.Level1)
295 {
296     const unsigned char buf[] = "12345678";
297     const void *vbuf = static_cast<const void *>(buf);
298     ScopeDebugLevel tempLogLevel(LEVEL_MUCH, true);
299 
300     StdoutRecord stdoutRecord;
301     stdoutRecord.Start();
302     EXPECT_EQ(HexDump(vbuf, 0), true);
303     EXPECT_EQ(HexDump(vbuf, 1), true);
304     EXPECT_EQ(HexDump(vbuf, 4), true);
305     EXPECT_EQ(HexDump(vbuf, 5), true);
306     EXPECT_EQ(HexDump(vbuf, 8), true);
307     stdoutRecord.Stop();
308 }
309 
310 /**
311  * @tc.name: StringTrim
312  * @tc.desc:
313  * @tc.type: FUNC
314  */
315 HWTEST_F(UtilitiesTest, StringTrim, TestSize.Level1)
316 {
317     std::string test;
318     EXPECT_STREQ(StringTrim(test = " a ").c_str(), "a");
319     EXPECT_STREQ(StringTrim(test = " a").c_str(), "a");
320     EXPECT_STREQ(StringTrim(test = "a ").c_str(), "a");
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     EXPECT_STREQ(StringTrim(test = "a1a   ").c_str(), "a1a");
327 }
328 
329 /**
330  * @tc.name: RecordStdout
331  * @tc.desc:
332  * @tc.type: FUNC
333  */
334 HWTEST_F(UtilitiesTest, RecordStdout, TestSize.Level1)
335 {
336     StdoutRecord stdoutRecord;
337 
338     ASSERT_EQ(stdoutRecord.Start(), true);
339     printf("line1: abc\n");
340     printf("line2: def\n");
341     printf("line3: ghi\n");
342     printf("\n");
343     std::string out = stdoutRecord.Stop();
344 
345     printf("stdoutRecord:\n%s", out.c_str());
346     EXPECT_EQ(out.empty(), false);
347     EXPECT_NE(out.find("line1:"), std::string::npos);
348     EXPECT_NE(out.find("line2:"), std::string::npos);
349     EXPECT_NE(out.find("line3:"), std::string::npos);
350     EXPECT_EQ(out.find("line4:"), std::string::npos);
351 }
352 
353 /**
354  * @tc.name: IsDigits
355  * @tc.desc:
356  * @tc.type: FUNC
357  */
358 HWTEST_F(UtilitiesTest, IsDigits, TestSize.Level1)
359 {
360     EXPECT_EQ(IsDigits(""), false);
361     EXPECT_EQ(IsDigits("1"), true);
362     EXPECT_EQ(IsDigits("12"), true);
363     EXPECT_EQ(IsDigits("1a"), false);
364     EXPECT_EQ(IsDigits("a1"), false);
365     EXPECT_EQ(IsDigits("1a2"), false);
366     EXPECT_EQ(IsDigits("a1b"), false);
367     EXPECT_EQ(IsDigits("_1"), false);
368     EXPECT_EQ(IsDigits("1_"), false);
369 }
370 
371 /**
372  * @tc.name: IsHexxDigits
373  * @tc.desc:
374  * @tc.type: FUNC
375  */
376 HWTEST_F(UtilitiesTest, IsHexxDigits, TestSize.Level1)
377 {
378     EXPECT_EQ(IsHexDigits(""), false);
379     EXPECT_EQ(IsHexDigits("1"), true);
380     EXPECT_EQ(IsHexDigits("12"), true);
381     EXPECT_EQ(IsHexDigits("1a"), true);
382     EXPECT_EQ(IsHexDigits("f1"), true);
383     EXPECT_EQ(IsHexDigits("1f2"), true);
384     EXPECT_EQ(IsHexDigits("a1f"), true);
385     EXPECT_EQ(IsHexDigits("g1"), false);
386     EXPECT_EQ(IsHexDigits("1g"), false);
387     EXPECT_EQ(IsHexDigits("_1"), false);
388     EXPECT_EQ(IsHexDigits("1_"), false);
389 }
390 
391 /**
392  * @tc.name: IsSameCommand
393  * @tc.desc:
394  * @tc.type: FUNC
395  */
396 HWTEST_F(UtilitiesTest, IsSameCommand, TestSize.Level1)
397 {
398     EXPECT_EQ(IsSameCommand("", ""), false);
399     EXPECT_EQ(IsSameCommand("a", ""), false);
400     EXPECT_EQ(IsSameCommand("", "b"), false);
401     EXPECT_EQ(IsSameCommand("1", "2"), false);
402     EXPECT_EQ(IsSameCommand("2", "1"), false);
403     EXPECT_EQ(IsSameCommand("1", "1"), true);
404     EXPECT_EQ(IsSameCommand("a", "a"), true);
405     EXPECT_EQ(IsSameCommand("a:1", "a:2"), false);
406 }
407 
408 /**
409  * @tc.name: CompressFile
410  * @tc.desc:
411  * @tc.type: FUNC
412  */
413 HWTEST_F(UtilitiesTest, CompressFile, TestSize.Level1)
414 {
415     std::string srcPath = "./resource/testdata/elf_test_stripped_broken";
416     std::string destPath = "./test.gz";
417     EXPECT_EQ(CompressFile(srcPath, destPath), true);
418     srcPath = "";
419     EXPECT_EQ(CompressFile(srcPath, destPath), false);
420     srcPath = "./resource/testdata/elf_test_stripped_broken";
421     destPath = "";
422     EXPECT_EQ(CompressFile(srcPath, destPath), false);
423 }
424 
425 /**
426  * @tc.name: UncompressFile
427  * @tc.desc:
428  * @tc.type: FUNC
429  */
430 HWTEST_F(UtilitiesTest, UncompressFile, TestSize.Level1)
431 {
432     std::string gzipPath = "./test.gz";
433     std::string dataPath = "./test";
434     EXPECT_EQ(UncompressFile(gzipPath, dataPath), true);
435     gzipPath = "./test.gz";
436     dataPath = "";
437     EXPECT_EQ(UncompressFile(gzipPath, dataPath), false);
438     gzipPath = "";
439     dataPath = "./resource/testdata/elf_test_stripped_broken";
440     EXPECT_EQ(UncompressFile(gzipPath, dataPath), false);
441 }
442 
443 /**
444  * @tc.name: StringPrintf
445  * @tc.desc:
446  * @tc.type: FUNC
447  */
448 HWTEST_F(UtilitiesTest, StringPrintf, TestSize.Level1)
449 {
450     EXPECT_STREQ(StringPrintf("").c_str(), "");
451     EXPECT_STREQ(StringPrintf("123").c_str(), "123");
452     EXPECT_STREQ(StringPrintf("%d%s%c", 1, "2", 'c').c_str(), "12c");
453     EXPECT_STREQ(StringPrintf("%d%s%c\t\n", 1, "2", 'c').c_str(), "12c\t\n");
454 
455     char format[PATH_MAX + 1];
456     std::fill(format, format + PATH_MAX, ' ');
457     format[PATH_MAX] = 0;
458     EXPECT_STRNE(StringPrintf(format).c_str(), format);
459     format[PATH_MAX - 1] = 0;
460     EXPECT_STREQ(StringPrintf(format).c_str(), format);
461     EXPECT_STREQ(StringPrintf(nullptr).c_str(), "");
462 }
463 
464 /**
465  * @tc.name: GetEntriesInDir
466  * @tc.desc:
467  * @tc.type: FUNC
468  */
469 HWTEST_F(UtilitiesTest, GetEntriesInDir, TestSize.Level1)
470 {
471     std::vector<std::string> dirFileInfo;
472     dirFileInfo = GetEntriesInDir("./");
473     EXPECT_GE(dirFileInfo.size(), 0u);
474 }
475 
476 /**
477  * @tc.name: GetSubDirs
478  * @tc.desc:
479  * @tc.type: FUNC
480  */
481 HWTEST_F(UtilitiesTest, GetSubDirs, TestSize.Level1)
482 {
483     std::vector<std::string> subDirFileInfo;
484     subDirFileInfo = GetSubDirs("../");
485     EXPECT_GE(subDirFileInfo.size(), 0u);
486 }
487 
488 /**
489  * @tc.name: IsDir
490  * @tc.desc:
491  * @tc.type: FUNC
492  */
493 HWTEST_F(UtilitiesTest, IsDir, TestSize.Level1)
494 {
495     bool ret = IsDir("../");
496     EXPECT_EQ(ret, true);
497 }
498 
499 /**
500  * @tc.name: IsPath
501  * @tc.desc:
502  * @tc.type: FUNC
503  */
504 HWTEST_F(UtilitiesTest, IsPath, TestSize.Level1)
505 {
506     bool ret = IsPath("./");
507     EXPECT_EQ(ret, true);
508 }
509 
510 /**
511  * @tc.name: PlatformPathConvert
512  * @tc.desc:
513  * @tc.type: FUNC
514  */
515 HWTEST_F(UtilitiesTest, PlatformPathConvert, TestSize.Level1)
516 {
517     EXPECT_GE(PlatformPathConvert("./").length(), 0u);
518 }
519 
520 /**
521  * @tc.name: ToHex
522  * @tc.desc:
523  * @tc.type: FUNC
524  */
525 HWTEST_F(UtilitiesTest, ToHex, TestSize.Level1)
526 {
527     unsigned char hVal = 'G';
528     EXPECT_STREQ(ToHex(hVal, 1, true).c_str(), "0x47");
529 }
530 
531 /**
532  * @tc.name: ToHex
533  * @tc.desc:
534  * @tc.type: FUNC
535  */
536 HWTEST_F(UtilitiesTest, CopyFromBufferAndMove, TestSize.Level1)
537 {
538     unsigned char *buffer = new unsigned char[4];
539     buffer[0] = '1';
540     buffer[1] = '2';
541     buffer[2] = '3';
542     buffer[3] = '4';
543     int *dest = new int;
544     const unsigned char *srcStr = buffer;
545     EXPECT_EQ(CopyFromBufferAndMove(srcStr, dest, 4), 4u);
546 }
547 
548 /**
549  * @tc.name: ReadIntFromProcFile01
550  * @tc.desc:
551  * @tc.type: FUNC
552  */
553 HWTEST_F(UtilitiesTest, ReadIntFromProcFile01, TestSize.Level1)
554 {
555     std::string strPath = "/proc/sys/kernel/perf_cpu_time_max_percent";
556     int strLen = 0;
557     EXPECT_EQ(ReadIntFromProcFile(strPath, strLen), true);
558     ASSERT_GT(strLen, 0);
559 }
560 
561 /**
562  * @tc.name: ReadIntFromProcFile02
563  * @tc.desc:
564  * @tc.type: FUNC
565  */
566 HWTEST_F(UtilitiesTest, ReadIntFromProcFile02, TestSize.Level1)
567 {
568     std::string strPath = "/proc/sys/kernel/perf_event_max_sample_rate";
569     int strLen = 0;
570     EXPECT_EQ(ReadIntFromProcFile(strPath, strLen), true);
571     ASSERT_GT(strLen, 0);
572 }
573 
574 /**
575  * @tc.name: ReadIntFromProcFile03
576  * @tc.desc:
577  * @tc.type: FUNC
578  */
579 HWTEST_F(UtilitiesTest, ReadIntFromProcFile03, TestSize.Level1)
580 {
581     std::string strPath = "/sys/kernel/tracing/saved_cmdlines_size";
582     int strLen = 0;
583     EXPECT_EQ(ReadIntFromProcFile(strPath, strLen), true);
584     ASSERT_GT(strLen, 0);
585 }
586 
587 /**
588  * @tc.name: WriteIntToProcFile
589  * @tc.desc:
590  * @tc.type: FUNC
591  */
592 HWTEST_F(UtilitiesTest, WriteIntToProcFile, TestSize.Level1)
593 {
594     std::string strPath = "./hiperf_log.txt";
595     int strVal = 0;
596     EXPECT_EQ(WriteIntToProcFile(strPath, strVal), true);
597 }
598 
599 /**
600  * @tc.name: ReadFileToString
601  * @tc.desc:
602  * @tc.type: FUNC
603  */
604 HWTEST_F(UtilitiesTest, ReadFileToString, TestSize.Level1)
605 {
606     std::string strPath = "./hiperf_log.txt";
607     EXPECT_NE(ReadFileToString(strPath).length(), 0u);
608 }
609 
610 /**
611  * @tc.name: WriteStringToFile
612  * @tc.desc:
613  * @tc.type: FUNC
614  */
615 HWTEST_F(UtilitiesTest, WriteStringToFile, TestSize.Level1)
616 {
617     std::string strPath = "./hiperf_log.txt";
618     std::string content = "0";
619     EXPECT_EQ(WriteStringToFile(strPath, content), true);
620 }
621 
622 /**
623  * @tc.name: Percentage
624  * @tc.desc:
625  * @tc.type: FUNC
626  */
627 HWTEST_F(UtilitiesTest, Percentage, TestSize.Level1)
628 {
629     EXPECT_EQ(Percentage(99, 100), 99);
630 }
631 
632 /**
633  * @tc.name: IsRoot
634  * @tc.desc:
635  * @tc.type: FUNC
636  */
637 HWTEST_F(UtilitiesTest, IsRoot, TestSize.Level1)
638 {
639     bool isRoot = true;
640 #if is_linux || is_ohos
641     isRoot = (getuid() == 0);
642 #endif
643     EXPECT_EQ(IsRoot(), isRoot);
644 }
645 
646 /**
647  * @tc.name: PowerOfTwo
648  * @tc.desc:
649  * @tc.type: FUNC
650  */
651 HWTEST_F(UtilitiesTest, PowerOfTwo, TestSize.Level1)
652 {
653     EXPECT_EQ(PowerOfTwo(1), true);
654 }
655 
656 /**
657  * @tc.name: GetSubthreadIDs
658  * @tc.desc:
659  * @tc.type: FUNC
660  */
661 HWTEST_F(UtilitiesTest, GetSubthreadIDs, TestSize.Level1)
662 {
663     StartThreads(1);
664     std::vector<pid_t> tids = GetSubthreadIDs(getpid());
665     if (!HasFailure()) {
666         for (pid_t tid : tids_) {
667             EXPECT_NE(find(tids.begin(), tids.end(), tid), tids.end());
668         }
669     }
670     ExitThreads();
671 }
672 
673 /**
674  * @tc.name: IsBeta
675  * @tc.desc:
676  * @tc.type: FUNC
677  */
678 HWTEST_F(UtilitiesTest, IsBeta, TestSize.Level1)
679 {
680     EXPECT_EQ(IsBeta(), true);
681 }
682 
683 HWTEST_F(UtilitiesTest, CanonicalizeSpecPath, TestSize.Level1)
684 {
685     EXPECT_EQ(CanonicalizeSpecPath(nullptr), "");
686     EXPECT_EQ(CanonicalizeSpecPath("/data/local/tmp/test/../test.txt"), "");
687     EXPECT_EQ(CanonicalizeSpecPath("/data/local/tmp/nonexistent.txt"), "/data/local/tmp/nonexistent.txt");
688     string largePath = "./";
689     for (int i = 0; i < 512; i++) { // 512: loop size
690         largePath += "testpath";
691     }
692     largePath += ".txt";
693     EXPECT_EQ(CanonicalizeSpecPath(largePath.c_str()), "");
694 }
695 
696 /**
697  * @tc.name: RecordStdoutInit
698  * @tc.desc:
699  * @tc.type: FUNC
700  */
701 HWTEST_F(UtilitiesTest, RecordStdoutInit, TestSize.Level1)
702 {
703     StdoutRecord stdnormaloutRecord("/data/local/tmp/hiperf_log.txt", "rw");
704     (void)stdnormaloutRecord.Stop();
705     StdoutRecord stdexceptoutRecord("/data/local/tmp/../hiperf_log.txt");
706     EXPECT_EQ(stdexceptoutRecord.Stop().empty(), true);
707 }
708 
709 /**
710  * @tc.name: CollectPidsByAppname1
711  * @tc.desc:
712  * @tc.type: FUNC
713  */
714 HWTEST_F(UtilitiesTest, CollectPidsByAppname1, TestSize.Level1)
715 {
716     pid_t pid = getpid();
717     std::string name = GetProcessName(pid);
718     size_t pos = name.find_last_of("/");
719     if (pos != std::string::npos) {
720         name = name.substr(pos + 1);
721     }
722     std::set<pid_t> pids = {};
723     CollectPidsByAppname(pids, name);
724     ASSERT_GE(pids.size(), 1u);
725     bool get = false;
726     for (pid_t id : pids) {
727         if (pid == id) {
728             get = true;
729             break;
730         }
731     }
732     EXPECT_EQ(get, true);
733 }
734 
735 /**
736  * @tc.name: CollectPidsByAppname2
737  * @tc.desc:
738  * @tc.type: FUNC
739  */
740 HWTEST_F(UtilitiesTest, CollectPidsByAppname2, TestSize.Level1)
741 {
742     pid_t pid = getpid();
743     std::string name = GetProcessName(pid);
744     size_t pos = name.find_last_of("/");
745     if (pos != std::string::npos) {
746         name = name.substr(pos + 1);
747     }
748     std::vector<std::string> names = { name };
749     std::set<pid_t> pids = {};
750     CollectPidsByAppname(pids, names);
751     ASSERT_GE(pids.size(), 1u);
752     bool get = false;
753     for (pid_t id : pids) {
754         if (pid == id) {
755             get = true;
756             break;
757         }
758     }
759     EXPECT_EQ(get, true);
760 }
761 
762 /**
763  * @tc.name: CheckOutOfRange1
764  * @tc.desc:
765  * @tc.type: FUNC
766  */
767 HWTEST_F(UtilitiesTest, CheckOutOfRange1, TestSize.Level1)
768 {
769     static constexpr int min = 10;
770     static constexpr int max = 20;
771     static constexpr int val = 8;
772     EXPECT_EQ(CheckOutOfRange<int>(val, min, max), true);
773 }
774 
775 /**
776  * @tc.name: CheckOutOfRange2
777  * @tc.desc:
778  * @tc.type: FUNC
779  */
780 HWTEST_F(UtilitiesTest, CheckOutOfRange2, TestSize.Level1)
781 {
782     static constexpr int min = 10;
783     static constexpr int max = 20;
784     static constexpr int val = 10;
785     EXPECT_EQ(CheckOutOfRange<int>(val, min, max), false);
786 }
787 
788 /**
789  * @tc.name: CheckOutOfRange3
790  * @tc.desc:
791  * @tc.type: FUNC
792  */
793 HWTEST_F(UtilitiesTest, CheckOutOfRange3, TestSize.Level1)
794 {
795     static constexpr int min = 10;
796     static constexpr int max = 20;
797     static constexpr int val = 15;
798     EXPECT_EQ(CheckOutOfRange<int>(val, min, max), false);
799 }
800 
801 /**
802  * @tc.name: CheckOutOfRange4
803  * @tc.desc:
804  * @tc.type: FUNC
805  */
806 HWTEST_F(UtilitiesTest, CheckOutOfRange4, TestSize.Level1)
807 {
808     static constexpr int min = 10;
809     static constexpr int max = 20;
810     static constexpr int val = 20;
811     EXPECT_EQ(CheckOutOfRange<int>(val, min, max), false);
812 }
813 
814 /**
815  * @tc.name: CheckOutOfRange5
816  * @tc.desc:
817  * @tc.type: FUNC
818  */
819 HWTEST_F(UtilitiesTest, CheckOutOfRange5, TestSize.Level1)
820 {
821     static constexpr int min = 10;
822     static constexpr int max = 20;
823     static constexpr int val = 25;
824     EXPECT_EQ(CheckOutOfRange<int>(val, min, max), true);
825 }
826 
827 /**
828  * @tc.name: IsSameCommand
829  * @tc.desc:
830  * @tc.type: FUNC
831  */
832 HWTEST_F(UtilitiesTest, IsSameCommand2, TestSize.Level1)
833 {
834     std::vector<std::string> v = {""};
835     EXPECT_EQ(IsSameCommand("", v), false);
836     EXPECT_EQ(IsSameCommand("a", v), false);
837 
838     v = {"", "a"};
839     EXPECT_EQ(IsSameCommand("a", v), true);
840 }
841 
842 /**
843  * @tc.name: IsArkJsFile
844  * @tc.desc:
845  * @tc.type: FUNC
846  */
847 HWTEST_F(UtilitiesTest, IsArkJsFile, TestSize.Level1)
848 {
849     EXPECT_EQ(IsArkJsFile("test.hap"), true);
850     EXPECT_EQ(IsArkJsFile("[anon:ArkTS Code:test.so/buffer.js]"), true);
851     EXPECT_EQ(IsArkJsFile("test.hsp"), true);
852     EXPECT_EQ(IsArkJsFile("test.abc"), true);
853     EXPECT_EQ(IsArkJsFile("test.hqf"), true);
854     EXPECT_EQ(IsArkJsFile("test.so"), false);
855 }
856 } // namespace HiPerf
857 } // namespace Developtools
858 } // namespace OHOS
859