• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "report_json_file_test.h"
17 #include "utilities.h"
18 
19 #include <bitset>
20 #include <memory>
21 
22 using namespace testing::ext;
23 namespace OHOS {
24 namespace Developtools {
25 namespace HiPerf {
26 class ReportJsonFileTest : public testing::Test {
27 public:
28     static void SetUpTestCase(void);
29     static void TearDownTestCase(void);
30     void SetUp();
31     void TearDown();
32     std::unique_ptr<ReportJsonFile> PrepairReportJson(const VirtualRuntime &virtualRuntime) const;
33 };
34 
SetUpTestCase()35 void ReportJsonFileTest::SetUpTestCase() {}
36 
TearDownTestCase()37 void ReportJsonFileTest::TearDownTestCase() {}
38 
SetUp()39 void ReportJsonFileTest::SetUp() {}
40 
TearDown()41 void ReportJsonFileTest::TearDown() {}
42 
43 /*
44 this func give a report json object which have
45 1 conifg with name "eventName" and ids is  1,2,3
46     1 process : pid 1
47     2 thread : tid is 3 and 4
48     3 lib : liba , libb and libc
49     5 func :
50         funca1 in liba
51         funca2 in liba
52         funcb1 in libb
53         funcc1 in libc
54 
55     2 callstack
56         in thread id 3:
57             funca1 - liba count 10
58             funca2 - liba count 0
59         in thread id 4:
60             funcc1 - libc count 20
61             funcb1 - libb count 0
62 */
PrepairReportJson(const VirtualRuntime & virtualRuntime) const63 std::unique_ptr<ReportJsonFile> ReportJsonFileTest::PrepairReportJson(
64     const VirtualRuntime &virtualRuntime) const
65 {
66     std::unique_ptr<ReportJsonFile> json =
67         std::make_unique<ReportJsonFile>(nullptr, virtualRuntime);
68     std::vector<DfxFrame> frames = {
69         {0x1u, 0x1u, "liba", "funca1"},
70         {0x2u, 0x1u, "liba", "funca2"},
71         {0x3u, 0x1u, "libb", "funcb1"},
72     };
73     std::vector<DfxFrame> frames2 = {
74         {0x1u, 0x1u, "libc", "funcc1"},
75         {0x2u, 0x1u, "libb", "funcb1"},
76     };
77     std::vector<uint64_t> ids = {1, 2, 3};
78     json->reportConfigItems_.emplace(
79         ids, ReportConfigItem(json->reportConfigItems_.size(), "eventName"));
80     json->libList_ = {"liba", "libb", "libc"};
81     json->AddNewFunction(0, "funca1");
82     json->AddNewFunction(0, "funca2");
83     json->AddNewFunction(1, "funcb1");
84     json->AddNewFunction(2, "funcc1"); // 2: num two
85     // id , pid , tid , event count
86     uint64_t id = 1;
87     uint64_t eventCount = 10;
88     uint64_t eventCount2 = 20;
89     int pid = 2;
90     int tid = 3;
91     int tid2 = 4;
92     json->UpdateReportCallStack(id, pid, tid, eventCount, frames);
93     json->UpdateReportCallStack(id, pid, tid2, eventCount2, frames2);
94 
95     return json;
96 }
97 
98 /**
99  * @tc.name: OutputJsonKey
100  * @tc.desc:
101  * @tc.type: FUNC
102  */
103 HWTEST_F(ReportJsonFileTest, OutputJsonKey, TestSize.Level2)
104 {
105     StdoutRecord output;
106 
107     // no name
108     output.Start();
109     OutputJsonKey(stdout, std::string());
110     EXPECT_STREQ(output.Stop().c_str(), "");
111 
112     output.Start();
113     OutputJsonKey(stdout, "");
114     EXPECT_STREQ(output.Stop().c_str(), "");
115 
116     // have name
117     output.Start();
118     OutputJsonKey(stdout, "keyname");
119     EXPECT_STREQ(output.Stop().c_str(), "\"keyname\":");
120 
121     output.Start();
122     OutputJsonKey(stdout, static_cast<int>(1));
123     EXPECT_STREQ(output.Stop().c_str(), "\"1\":");
124 
125     output.Start();
126     OutputJsonKey(stdout, static_cast<long>(1));
127     EXPECT_STREQ(output.Stop().c_str(), "\"1\":");
128 
129     output.Start();
130     OutputJsonKey(stdout, static_cast<size_t>(2));
131     EXPECT_STREQ(output.Stop().c_str(), "\"2\":");
132 
133     output.Start();
134 
135     OutputJsonKey(stdout, std::string("keyname"));
136     EXPECT_STREQ(output.Stop().c_str(), "\"keyname\":");
137 }
138 
139 /**
140  * @tc.name: OutputJsonValue
141  * @tc.desc:
142  * @tc.type: FUNC
143  */
144 HWTEST_F(ReportJsonFileTest, OutputJsonValue, TestSize.Level1)
145 {
146     StdoutRecord output;
147 
148     output.Start();
149     OutputJsonValue(stdout, std::string("value"));
150     EXPECT_STREQ(output.Stop().c_str(), "\"value\"");
151 
152     output.Start();
153     OutputJsonValue(stdout, int(1));
154     EXPECT_STREQ(output.Stop().c_str(), "1");
155 
156     output.Start();
157     OutputJsonValue(stdout, uint64_t(1));
158     EXPECT_STREQ(output.Stop().c_str(), "1");
159 
160     output.Start();
161     OutputJsonValue(stdout, bool(true));
162     EXPECT_STREQ(output.Stop().c_str(), "1");
163 
164     output.Start();
165     OutputJsonValue(stdout, size_t(1));
166     EXPECT_STREQ(output.Stop().c_str(), "1");
167 
168     output.Start();
169     OutputJsonValue(stdout, "value");
170     EXPECT_STREQ(output.Stop().c_str(), "\"value\"");
171 
172     output.Start();
173     OutputJsonValue(stdout, "value", false);
174     EXPECT_STREQ(output.Stop().c_str(), ",\"value\"");
175 }
176 
177 /**
178  * @tc.name: OutputJsonPair
179  * @tc.desc:
180  * @tc.type: FUNC
181  */
182 HWTEST_F(ReportJsonFileTest, OutputJsonPair, TestSize.Level1)
183 {
184     StdoutRecord output;
185     output.Start();
186     OutputJsonValue(stdout, std::string("value"));
187     EXPECT_STREQ(output.Stop().c_str(), "\"value\"");
188 }
189 
190 /**
191  * @tc.name: OutputJsonVectorList
192  * @tc.desc:
193  * @tc.type: FUNC
194  */
195 HWTEST_F(ReportJsonFileTest, OutputJsonVectorList, TestSize.Level2)
196 {
197     StdoutRecord output;
198 
199     output.Start();
200     OutputJsonVectorList<int>(stdout, "listname", {1, 2, 3}, true);
201     EXPECT_STREQ(output.Stop().c_str(), "\"listname\":[1,2,3]");
202 
203     output.Start();
204     OutputJsonVectorList<std::string>(stdout, "listname", {"1", "2", "3"}, true);
205     EXPECT_STREQ(output.Stop().c_str(), "\"listname\":[\"1\",\"2\",\"3\"]");
206 }
207 
208 /**
209  * @tc.name: OutputJsonMapList
210  * @tc.desc:
211  * @tc.type: FUNC
212  */
213 HWTEST_F(ReportJsonFileTest, OutputJsonMapList, TestSize.Level2)
214 {
215     StdoutRecord output;
216     std::map<int, int> map = {
217         {1, 2},
218         {3, 4},
219         {5, 6},
220     };
221 
222     output.Start();
223     OutputJsonMapList(stdout, "map", map, true);
224     EXPECT_STREQ(output.Stop().c_str(), "\"map\":[2,4,6]");
225 
226     std::map<std::string, std::string> map2 = {
227         {"1", "2"},
228         {"3", "4"},
229         {"5", "6"},
230     };
231 
232     output.Start();
233     OutputJsonMapList(stdout, "map2", map2, true);
234     EXPECT_STREQ(output.Stop().c_str(), "\"map2\":[\"2\",\"4\",\"6\"]");
235 }
236 
237 /**
238  * @tc.name: OutputJsonMap
239  * @tc.desc:
240  * @tc.type: FUNC
241  */
242 HWTEST_F(ReportJsonFileTest, OutputJsonMap, TestSize.Level0)
243 {
244     StdoutRecord output;
245     std::map<int, int> map = {
246         {1, 2},
247         {3, 4},
248         {5, 6},
249     };
250 
251     output.Start();
252     OutputJsonMap(stdout, "map", map, true);
253     EXPECT_STREQ(output.Stop().c_str(), "\"map\":{\"1\":2,\"3\":4,\"5\":6}");
254 
255     std::map<std::string, std::string> map2 = {
256         {"1", "2"},
257         {"3", "4"},
258         {"5", "6"},
259     };
260 
261     output.Start();
262     OutputJsonMap(stdout, "map2", map2, true);
263     EXPECT_STREQ(output.Stop().c_str(), "\"map2\":{\"1\":\"2\",\"3\":\"4\",\"5\":\"6\"}");
264 }
265 
266 /**
267  * @tc.name: GetOrCreateMapItem
268  * @tc.desc:
269  * @tc.type: FUNC
270  */
271 HWTEST_F(ReportJsonFileTest, GetOrCreateMapItem, TestSize.Level1)
272 {
273     std::map<int, int> map = {
274         {1, 2},
275         {3, 4},
276         {5, 6},
277     };
278 
279     EXPECT_EQ(GetOrCreateMapItem(map, 1), 2);
280     EXPECT_EQ(GetOrCreateMapItem(map, 2), 2);
281     EXPECT_EQ(GetOrCreateMapItem(map, 3), 4);
282     EXPECT_EQ(GetOrCreateMapItem(map, 4), 4);
283 }
284 
285 /**
286  * @tc.name: ReportFuncItem
287  * @tc.desc:
288  * @tc.type: FUNC
289  */
290 HWTEST_F(ReportJsonFileTest, ReportFuncItem, TestSize.Level2)
291 {
292     StdoutRecord output;
293     ReportFuncItem func(1);
294     func.sampleCount_ = 2;
295     func.eventCount_ = 3;
296     func.subTreeEventCount_ = 4;
297 
298     output.Start();
299     func.OutputJson(stdout);
300 
301     EXPECT_STREQ(output.Stop().c_str(), "{\"symbol\":1,\"counts\":[2,3,4]}");
302 }
303 
304 /**
305  * @tc.name: ReportCallNodeItem
306  * @tc.desc:
307  * @tc.type: FUNC
308  */
309 HWTEST_F(ReportJsonFileTest, ReportCallNodeItem, TestSize.Level1)
310 {
311     StdoutRecord output;
312     /*
313     2 / 12
314         4 / 10
315             6 / 6
316     */
317 
318     ReportCallNodeItem callnode(1);
319     callnode.selfEventCount_ = 2;
320     callnode.subTreeEventCount_ = 12;
321 
322     output.Start();
323     callnode.OutputJson(stdout);
324 
325     EXPECT_STREQ(output.Stop().c_str(),
326                  "{\"selfEvents\":2,\"subEvents\":12,\"symbol\":1,\"callStack\":[]}");
327 
328     ReportCallNodeItem &callnode2 = GetOrCreateMapItem(callnode.childrenMap, 2);
329     callnode2.selfEventCount_ = 4;
330     callnode2.subTreeEventCount_ = 10;
331     output.Start();
332     callnode.OutputJson(stdout);
333     EXPECT_STREQ(output.Stop().c_str(),
334                  "{\"selfEvents\":2,\"subEvents\":12,\"symbol\":1,\"callStack\":[{\"selfEvents\":4,"
335                  "\"subEvents\":10,\"symbol\":2,\"callStack\":[]}]}");
336 
337     ReportCallNodeItem &callnode3 = GetOrCreateMapItem(callnode2.childrenMap, 3);
338     callnode3.selfEventCount_ = 6;
339     callnode3.subTreeEventCount_ = 6;
340     output.Start();
341     callnode.OutputJson(stdout);
342 
343     EXPECT_STREQ(output.Stop().c_str(),
344                  "{\"selfEvents\":2,\"subEvents\":12,\"symbol\":1,\"callStack\":[{\"selfEvents\":4,"
345                  "\"subEvents\":10,\"symbol\":2,\"callStack\":[{\"selfEvents\":6,\"subEvents\":6,"
346                  "\"symbol\":3,\"callStack\":[]}]}]}");
347 }
348 
349 /**
350  * @tc.name: ReportCallNodeItem
351  * @tc.desc:
352  * @tc.type: FUNC
353  */
354 HWTEST_F(ReportJsonFileTest, UpdateChildrenEventCount, TestSize.Level2)
355 {
356     StdoutRecord output;
357     /*
358     2 / 12
359         4 / 10
360             6 / 6
361     */
362 
363     ReportCallNodeItem callnode(1);
364     callnode.selfEventCount_ = 2;
365     callnode.subTreeEventCount_ = 0;
366 
367     ReportCallNodeItem &callnode2 = GetOrCreateMapItem(callnode.childrenMap, 2);
368     callnode2.selfEventCount_ = 4;
369     callnode2.subTreeEventCount_ = 0;
370 
371     ReportCallNodeItem &callnode3 = GetOrCreateMapItem(callnode2.childrenMap, 3);
372     callnode3.selfEventCount_ = 6;
373     callnode3.subTreeEventCount_ = 0;
374 
375     output.Start();
376     callnode.UpdateChildrenEventCount();
377     callnode.OutputJson(stdout);
378 
379     EXPECT_STREQ(output.Stop().c_str(),
380                  "{\"selfEvents\":2,\"subEvents\":12,\"symbol\":1,\"callStack\":[{\"selfEvents\":4,"
381                  "\"subEvents\":10,\"symbol\":2,\"callStack\":[{\"selfEvents\":6,\"subEvents\":6,"
382                  "\"symbol\":3,\"callStack\":[]}]}]}");
383 }
384 
385 /**
386  * @tc.name: ReportLibItem
387  * @tc.desc:
388  * @tc.type: FUNC
389  */
390 HWTEST_F(ReportJsonFileTest, ReportLibItem, TestSize.Level1)
391 {
392     StdoutRecord output;
393     ReportLibItem lib;
394     lib.libId_ = 1;
395     lib.eventCount_ = 2;
396 
397     ReportFuncItem &func = GetOrCreateMapItem(lib.funcs_, 1);
398     func.sampleCount_ = 2;
399     func.eventCount_ = 3;
400     func.subTreeEventCount_ = 4;
401 
402     output.Start();
403     lib.OutputJson(stdout);
404 
405     EXPECT_STREQ(
406         output.Stop().c_str(),
407         "{\"fileId\":1,\"eventCount\":2,\"functions\":[{\"symbol\":1,\"counts\":[2,3,4]}]}");
408 }
409 
410 /**
411  * @tc.name: ReportThreadItem
412  * @tc.desc:
413  * @tc.type: FUNC
414  */
415 HWTEST_F(ReportJsonFileTest, ReportThreadItem, TestSize.Level1)
416 {
417     StdoutRecord output;
418     ReportThreadItem thread(1);
419     thread.tid_ = 2;
420     thread.eventCount_ = 3;
421     thread.sampleCount_ = 4;
422 
423     output.Start();
424     thread.OutputJson(stdout);
425 
426     EXPECT_STREQ(output.Stop().c_str(),
427                  "{\"tid\":2,\"eventCount\":3,\"sampleCount\":4,\"libs\":[],\"CallOrder\":{"
428                  "\"selfEvents\":0,\"subEvents\":0,\"symbol\":-1,\"callStack\":[]},\"CalledOrder\":"
429                  "{\"selfEvents\":0,\"subEvents\":0,\"symbol\":-1,\"callStack\":[]}}");
430 }
431 
432 /**
433  * @tc.name: ReportProcessItem
434  * @tc.desc:
435  * @tc.type: FUNC
436  */
437 HWTEST_F(ReportJsonFileTest, ReportProcessItem, TestSize.Level1)
438 {
439     StdoutRecord output;
440     ReportProcessItem process(1);
441     process.pid_ = 2;
442     process.eventCount_ = 3;
443 
444     output.Start();
445     process.OutputJson(stdout);
446 
447     EXPECT_STREQ(output.Stop().c_str(), "{\"pid\":2,\"eventCount\":3,\"threads\":[]}");
448 }
449 
450 /**
451  * @tc.name: ReportConfigItem
452  * @tc.desc:
453  * @tc.type: FUNC
454  */
455 HWTEST_F(ReportJsonFileTest, ReportConfigItem, TestSize.Level2)
456 {
457     StdoutRecord output;
458     ReportConfigItem config(1, "configname");
459     config.eventCount_ = 3;
460 
461     output.Start();
462     config.OutputJson(stdout);
463 
464     EXPECT_STREQ(output.Stop().c_str(),
465                  "{\"eventConfigName\":\"configname\",\"eventCount\":3,\"processes\":[]}");
466 }
467 
468 /**
469  * @tc.name: UpdateReportSample
470  * @tc.desc:
471  * @tc.type: FUNC
472  */
473 HWTEST_F(ReportJsonFileTest, UpdateReportSample, TestSize.Level1)
474 {
475     StdoutRecord output;
476     VirtualRuntime virtualRuntime;
477     ReportJsonFile json(nullptr, virtualRuntime);
478     std::vector<uint64_t> ids = {1, 2, 3};
479     json.reportConfigItems_.emplace(ids, ReportConfigItem(1, "eventName"));
480     // id , pid , tid , event count
481     json.UpdateReportSample(1, 2, 3, 4);
482     auto configIt = json.reportConfigItems_.begin();
483 
484     EXPECT_EQ(configIt->second.eventCount_, 4u);
485     ASSERT_EQ(configIt->second.processes_.size(), 1u);
486     EXPECT_EQ(configIt->second.processes_.at(2).pid_, 2);
487     EXPECT_EQ(configIt->second.processes_.at(2).eventCount_, 4u);
488     ASSERT_EQ(configIt->second.processes_.at(2).threads_.size(), 1u);
489     EXPECT_EQ(configIt->second.processes_.at(2).threads_.at(3).tid_, 3);
490     EXPECT_EQ(configIt->second.processes_.at(2).threads_.at(3).eventCount_, 4u);
491     EXPECT_EQ(configIt->second.processes_.at(2).threads_.at(3).sampleCount_, 1u);
492     EXPECT_EQ(json.sampleCount_, 1u);
493 }
494 
495 /**
496  * @tc.name: UpdateReportCallStack
497  * @tc.desc:
498  * @tc.type: FUNC
499  */
500 HWTEST_F(ReportJsonFileTest, UpdateReportCallStack, TestSize.Level0)
501 {
502     StdoutRecord output;
503     VirtualRuntime virtualRuntime;
504     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
505 
506     // check
507     auto configIt = json->reportConfigItems_.begin();
508 
509     // this should not update here
510     EXPECT_EQ(configIt->second.eventCount_, 0u);
511     ASSERT_EQ(configIt->second.processes_.size(), 1u);
512     ASSERT_EQ(configIt->second.processes_.begin()->second.threads_.size(), 2u);
513     ReportProcessItem &process = configIt->second.processes_.begin()->second;
514     ReportThreadItem &thread = process.threads_.begin()->second;
515     // only liba and libb
516     ASSERT_EQ(thread.libs_.size(), 2u);
517     ReportLibItem &lib = thread.libs_.begin()->second;
518     // we have to function , a1 and a2 in liba
519     ASSERT_EQ(lib.libId_, 0);
520     ASSERT_EQ(lib.funcs_.size(), 2u);
521 
522     // chec func count
523     ReportFuncItem &funca1 = lib.funcs_.at(0);
524     ReportFuncItem &funca2 = lib.funcs_.at(1);
525 
526     // only fist one have count
527     ASSERT_EQ(funca1.eventCount_, 10u);
528     ASSERT_EQ(funca1.subTreeEventCount_, 10u);
529     ASSERT_EQ(funca2.eventCount_, 0u);
530     ASSERT_EQ(funca2.subTreeEventCount_, 10u);
531 }
532 
533 /**
534  * @tc.name: UpdateCallNodeEventCount
535  * @tc.desc:
536  * @tc.type: FUNC
537  */
538 HWTEST_F(ReportJsonFileTest, UpdateCallNodeEventCount, TestSize.Level2)
539 {
540     StdoutRecord output;
541     VirtualRuntime virtualRuntime;
542     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
543 
544     auto configIt = json->reportConfigItems_.begin();
545     ReportProcessItem &process = configIt->second.processes_.begin()->second;
546     ReportThreadItem &thread = process.threads_.begin()->second;
547     ReportLibItem &lib = thread.libs_.begin()->second;
548 
549     ReportFuncItem &funca1 = lib.funcs_.at(0);
550     ReportFuncItem &funca2 = lib.funcs_.at(1);
551 
552     // only fist one have count
553     EXPECT_EQ(funca1.eventCount_, 10u);
554     EXPECT_EQ(funca1.subTreeEventCount_, 10u);
555     EXPECT_EQ(funca2.eventCount_, 0u);
556     EXPECT_EQ(funca2.subTreeEventCount_, 10u);
557     EXPECT_EQ(thread.callNode.subTreeEventCount_, 0u);
558     EXPECT_EQ(thread.callNodeReverse.subTreeEventCount_, 0u);
559 
560     json->UpdateCallNodeEventCount();
561     EXPECT_EQ(thread.callNode.subTreeEventCount_, 10u);
562     EXPECT_EQ(thread.callNodeReverse.subTreeEventCount_, 10u);
563 }
564 
565 /**
566  * @tc.name: ProcessSymbolsFiles
567  * @tc.desc:
568  * @tc.type: FUNC
569  */
570 HWTEST_F(ReportJsonFileTest, ProcessSymbolsFiles, TestSize.Level0)
571 {
572     VirtualRuntime virtualRuntime;
573     std::unique_ptr<ReportJsonFile> json =
574         std::make_unique<ReportJsonFile>(nullptr, virtualRuntime);
575     std::vector<std::unique_ptr<SymbolsFile>> symbolsFiles;
576 
577     std::string userSymbol = "user_symbol";
578     std::unique_ptr<SymbolsFile> user = SymbolsFile::CreateSymbolsFile(SYMBOL_ELF_FILE);
579     user->symbols_.emplace_back(0x1, 1u, "first_user_func", user->filePath_);
580     user->symbols_.emplace_back(0x2, 1u, "second_user_func", user->filePath_);
581     user->filePath_ = userSymbol;
582     symbolsFiles.emplace_back(std::move(user));
583 
584     std::string userSymbol2 = "user_symbol2";
585     std::unique_ptr<SymbolsFile> user2 = SymbolsFile::CreateSymbolsFile(SYMBOL_ELF_FILE);
586     user2->symbols_.emplace_back(0x1, 1u, "first_user2_func", user2->filePath_);
587     user2->symbols_.emplace_back(0x2, 1u, "second_user2_func", user2->filePath_);
588     user2->symbols_.emplace_back(0x3, 1u, "third_user2_func", user2->filePath_);
589     user2->filePath_ = userSymbol2;
590     symbolsFiles.emplace_back(std::move(user2));
591 
592     json->ProcessSymbolsFiles(symbolsFiles);
593     EXPECT_EQ(json->libList_.size(), 2u);
594     ASSERT_EQ(json->functionMap_.size(), 2u);
595     EXPECT_EQ(json->functionMap_[0].size(), 2u);
596     EXPECT_EQ(json->functionMap_[1].size(), 3u);
597 }
598 
599 /**
600  * @tc.name: GetFunctionID
601  * @tc.desc:
602  * @tc.type: FUNC
603  */
604 HWTEST_F(ReportJsonFileTest, GetFunctionID, TestSize.Level1)
605 {
606     StdoutRecord output;
607     VirtualRuntime virtualRuntime;
608     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
609     /*
610         3 lib : liba , libb and libc
611         5 func :
612             funca1 in liba
613             funca2 in liba
614             funcb1 in libb
615             funcc1 in libc
616     */
617     EXPECT_EQ(json->GetFunctionID(0, "funca1"), 0);
618     EXPECT_EQ(json->GetFunctionID(0, "funca2"), 1);
619     EXPECT_EQ(json->GetFunctionID(0, "funca3"), 4);
620     EXPECT_EQ(json->GetFunctionID(0, "funcb1"), 5);
621     EXPECT_EQ(json->GetFunctionID(1, "funcb1"), 2);
622     EXPECT_EQ(json->GetFunctionID(2, "funcc1"), 3);
623 }
624 
625 /**
626  * @tc.name: GetLibID
627  * @tc.desc:
628  * @tc.type: FUNC
629  */
630 HWTEST_F(ReportJsonFileTest, GetLibID, TestSize.Level2)
631 {
632     StdoutRecord output;
633     VirtualRuntime virtualRuntime;
634     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
635     /*
636         3 lib : liba , libb and libc
637         5 func :
638             funca1 in liba
639             funca2 in liba
640             funcb1 in libb
641             funcc1 in libc
642     */
643     EXPECT_EQ(json->GetLibID("liba"), 0);
644     EXPECT_EQ(json->GetLibID("libb"), 1);
645     EXPECT_EQ(json->GetLibID("libc"), 2);
646     EXPECT_EQ(json->GetLibID("libd"), -1);
647     EXPECT_EQ(json->GetLibID(""), -1);
648 }
649 
650 /**
651  * @tc.name: GetConfigIndex
652  * @tc.desc:
653  * @tc.type: FUNC
654  */
655 HWTEST_F(ReportJsonFileTest, GetConfigIndex, TestSize.Level2)
656 {
657     StdoutRecord output;
658     VirtualRuntime virtualRuntime;
659     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
660 
661     std::vector<uint64_t> ids = {4, 5};
662     json->reportConfigItems_.emplace(
663         ids, ReportConfigItem(json->reportConfigItems_.size(), "eventName2"));
664 
665     EXPECT_EQ(json->GetConfigIndex(1), 0u);
666     EXPECT_EQ(json->GetConfigIndex(2), 0u);
667     EXPECT_EQ(json->GetConfigIndex(3), 0u);
668     EXPECT_EQ(json->GetConfigIndex(4), 1u);
669     EXPECT_EQ(json->GetConfigIndex(5), 1u);
670     EXPECT_EQ(json->GetConfigIndex(6), 0u);
671 }
672 
673 /**
674  * @tc.name: GetConfigName
675  * @tc.desc:
676  * @tc.type: FUNC
677  */
678 HWTEST_F(ReportJsonFileTest, GetConfigName, TestSize.Level1)
679 {
680     StdoutRecord output;
681     VirtualRuntime virtualRuntime;
682     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
683 
684     std::vector<uint64_t> ids = {4, 5};
685     json->reportConfigItems_.emplace(
686         ids, ReportConfigItem(json->reportConfigItems_.size(), "eventName2"));
687 
688     EXPECT_STREQ(json->GetConfigName(1).c_str(), "eventName");
689     EXPECT_STREQ(json->GetConfigName(2).c_str(), "eventName");
690     EXPECT_STREQ(json->GetConfigName(3).c_str(), "eventName");
691     EXPECT_STREQ(json->GetConfigName(4).c_str(), "eventName2");
692     EXPECT_STREQ(json->GetConfigName(5).c_str(), "eventName2");
693     EXPECT_STREQ(json->GetConfigName(6).c_str(), "eventName");
694 }
695 
696 /**
697  * @tc.name: GetConfig
698  * @tc.desc:
699  * @tc.type: FUNC
700  */
701 HWTEST_F(ReportJsonFileTest, GetConfig, TestSize.Level2)
702 {
703     StdoutRecord output;
704     VirtualRuntime virtualRuntime;
705     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
706 
707     std::vector<uint64_t> ids = {4, 5};
708     json->reportConfigItems_.emplace(
709         ids, ReportConfigItem(json->reportConfigItems_.size(), "eventName2"));
710 
711     EXPECT_EQ(json->GetConfig(1).index_, 0);
712     EXPECT_EQ(json->GetConfig(2).index_, 0);
713     EXPECT_EQ(json->GetConfig(3).index_, 0);
714     EXPECT_EQ(json->GetConfig(4).index_, 1);
715     EXPECT_EQ(json->GetConfig(5).index_, 1);
716     EXPECT_EQ(json->GetConfig(6).index_, 0);
717 }
718 
719 /**
720  * @tc.name: ParseJson01
721  * @tc.desc: Test ParseJson function.
722  * @tc.type: FUNC
723 */
724 HWTEST_F(ReportJsonFileTest, ParseJson01, TestSize.Level2)
725 {
726     std::string testStr1 = "{\"test\":\"";
727     std::string testStr2 = "/data/local/tmp/\"test\"/test.so";
728     std::string testStr3 = "\"}";
729     std::string testStr = testStr1 + testStr2 + testStr3;
730     cJSON* root = cJSON_Parse(testStr.c_str());
731     EXPECT_FALSE(root != nullptr);
732     testStr2 = StringReplace(testStr2, "\"", "");
733     testStr = testStr1 + testStr2 + testStr3;
734     root = cJSON_Parse(testStr.c_str());
735     EXPECT_TRUE(root != nullptr);
736 }
737 
738 /**
739  * @tc.name: ParseJson02
740  * @tc.desc: Test ParseJson function.
741  * @tc.type: FUNC
742 */
743 HWTEST_F(ReportJsonFileTest, ParseJson02, TestSize.Level2)
744 {
745     std::string testStr1 = "{\"test\":\"";
746     std::string testStr2 = "/data/local/tmp/\\test/test.so";
747     std::string testStr3 = "\"}";
748     std::string testStr = testStr1 + testStr2 + testStr3;
749     cJSON* root = cJSON_Parse(testStr.c_str());
750     EXPECT_TRUE(root != nullptr);
751     testStr2 = StringReplace(testStr2, "\\", "\\\\");
752     testStr = testStr1 + testStr2 + testStr3;
753     root = cJSON_Parse(testStr.c_str());
754     EXPECT_TRUE(root != nullptr);
755 }
756 } // namespace HiPerf
757 } // namespace Developtools
758 } // namespace OHOS
759