• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. 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 <fcntl.h>
17 #include <hwext/gtest-ext.h>
18 #include <hwext/gtest-tag.h>
19 #include <memory>
20 
21 #include "hilog_parser/pbreader_hilog_parser.h"
22 #include "hilog_plugin_result.pb.h"
23 #include "hilog_plugin_result.pbreader.h"
24 #include "parser/ptreader_parser/ptreader_parser.h"
25 #include "parser/common_types.h"
26 #include "trace_streamer_selector.h"
27 
28 using namespace testing::ext;
29 using namespace SysTuning::TraceStreamer;
30 
31 namespace SysTuning {
32 namespace TraceStreamer {
33 namespace HilogUnitTest {
34 const uint32_t PID = 2716;
35 const uint32_t TID = 1532;
36 const uint64_t LOG_ID = 1;
37 const uint32_t LOG_LEVEL_D = 68;
38 const uint64_t TV_SEC = 1632675525;
39 const uint64_t TV_NSEC = 996560700;
40 const std::string LOG_TAG = "HwMSDPMovementService";
41 const std::string LOG_CONTEXT = "handleGetSupportedModule";
42 const uint32_t PID_02 = 2532;
43 const uint32_t TID_02 = 1716;
44 const uint64_t LOG_ID_02 = 2;
45 const uint32_t LOG_LEVEL_E = 69;
46 const uint64_t TV_SEC_02 = 1632688888;
47 const uint64_t TV_NSEC_02 = 996588888;
48 const std::string LOG_TAG_02 = "ProfilerService";
49 const std::string LOG_CONTEXT_02 = "POST_RECV_MESSAGE method: /IProfilerService/CreateSession";
50 
51 class HilogParserTest : public ::testing::Test {
52 public:
SetUp()53     void SetUp()
54     {
55         stream_.InitFilter();
56     }
57 
TearDown()58     void TearDown() {}
59 
InitData(std::string & hilogData,bool isRepeatedData=false)60     void InitData(std::string &hilogData, bool isRepeatedData = false)
61     {
62         HilogInfo *hilogInfo = new HilogInfo();
63         HilogDetails *hilogDetails = new HilogDetails();
64         hilogDetails->set_tv_sec(TV_SEC);
65         hilogDetails->set_tv_nsec(TV_NSEC);
66         hilogDetails->set_pid(PID);
67         hilogDetails->set_tid(TID);
68         hilogDetails->set_level(LOG_LEVEL_D);
69         hilogDetails->set_tag(LOG_TAG);
70 
71         auto hilogLine = hilogInfo->add_info();
72         hilogLine->set_allocated_detail(hilogDetails);
73         hilogLine->set_context(LOG_CONTEXT);
74         hilogLine->set_id(LOG_ID);
75 
76         if (isRepeatedData) {
77             HilogDetails *hilogDetailsSecond = new HilogDetails();
78             hilogDetailsSecond->set_tv_sec(TV_SEC_02);
79             hilogDetailsSecond->set_tv_nsec(TV_NSEC_02);
80             hilogDetailsSecond->set_pid(PID_02);
81             hilogDetailsSecond->set_tid(TID_02);
82             hilogDetailsSecond->set_level(LOG_LEVEL_E);
83             hilogDetailsSecond->set_tag(LOG_TAG_02);
84 
85             auto hilogLineSecond = hilogInfo->add_info();
86             hilogLineSecond->set_allocated_detail(hilogDetailsSecond);
87             hilogLineSecond->set_context(LOG_CONTEXT_02);
88             hilogLineSecond->set_id(LOG_ID_02);
89         }
90 
91         hilogInfo->SerializeToString(&hilogData);
92     }
93 
94 public:
95     SysTuning::TraceStreamer::TraceStreamerSelector stream_ = {};
96 };
97 
98 /**
99  * @tc.name: ParseHilogInfoWithoutHilogLine
100  * @tc.desc: Parse a HilogInfo that does not contain any hiloglines
101  * @tc.type: FUNC
102  */
103 HWTEST_F(HilogParserTest, ParseHilogInfoWithoutHilogLine, TestSize.Level1)
104 {
105     TS_LOGI("test8-1");
106     HilogInfo *hilogInfo = new HilogInfo();
107     PbreaderHiLogParser htraceHiLogParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
108     std::string hilogData = "";
109     hilogInfo->SerializeToString(&hilogData);
110     ProtoReader::BytesView hilogInfoData(reinterpret_cast<const uint8_t *>(hilogData.data()), hilogData.size());
111     bool issplit = false;
112     htraceHiLogParser.Parse(hilogInfoData, issplit);
113     auto size = stream_.traceDataCache_->GetConstHilogData().Size();
114     EXPECT_FALSE(size);
115 }
116 
117 /**
118  * @tc.name: ParseHilogInfoWithOneHilogLine
119  * @tc.desc: Parse a HilogInfo with only one Hilogline
120  * @tc.type: FUNC
121  */
122 HWTEST_F(HilogParserTest, ParseHilogInfoWithOneHilogLine, TestSize.Level1)
123 {
124     TS_LOGI("test8-2");
125 
126     std::string hilogData = "";
127     InitData(hilogData);
128     PbreaderHiLogParser htraceHiLogParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
129     ProtoReader::BytesView hilogInfoData(reinterpret_cast<const uint8_t *>(hilogData.data()), hilogData.size());
130     bool issplit = false;
131     htraceHiLogParser.Parse(hilogInfoData, issplit);
132 
133     auto constHilogData = stream_.traceDataCache_->GetConstHilogData();
134     EXPECT_EQ(constHilogData.HilogLineSeqs()[0], LOG_ID);
135     EXPECT_EQ(constHilogData.TimeStampData()[0], (TV_NSEC + TV_SEC * SEC_TO_NS));
136     EXPECT_EQ(constHilogData.Pids()[0], PID);
137     EXPECT_EQ(constHilogData.Tids()[0], TID);
138 
139     auto iter = htraceHiLogParser.logLevelString_.find(LOG_LEVEL_D);
140     if (iter == htraceHiLogParser.logLevelString_.end()) {
141         EXPECT_FALSE(0);
142     }
143     auto &dataDict = stream_.traceDataCache_->dataDict_;
144     EXPECT_EQ(constHilogData.Levels()[0], dataDict.GetStringIndex(iter->second.c_str()));
145     EXPECT_EQ(constHilogData.Tags()[0], dataDict.GetStringIndex(LOG_TAG));
146     EXPECT_EQ(constHilogData.Contexts()[0], dataDict.GetStringIndex(LOG_CONTEXT));
147     EXPECT_EQ(stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_RECEIVED), 1);
148 }
149 
150 /**
151  * @tc.name: ParseHilogInfoWithMultipleHilogLine
152  * @tc.desc: Parse a HilogInfo with multiple Hiloglines
153  * @tc.type: FUNC
154  */
155 HWTEST_F(HilogParserTest, ParseHilogInfoWithMultipleHilogLine, TestSize.Level1)
156 {
157     TS_LOGI("test8-3");
158 
159     std::string hilogData = "";
160     InitData(hilogData, true);
161     PbreaderHiLogParser htraceHiLogParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
162     ProtoReader::BytesView hilogInfoData(reinterpret_cast<const uint8_t *>(hilogData.data()), hilogData.size());
163     bool issplit = false;
164     htraceHiLogParser.Parse(hilogInfoData, issplit);
165 
166     auto constHilogData = stream_.traceDataCache_->GetConstHilogData();
167     EXPECT_EQ(constHilogData.HilogLineSeqs()[0], LOG_ID);
168     EXPECT_EQ(constHilogData.HilogLineSeqs()[1], LOG_ID_02);
169     EXPECT_EQ(constHilogData.TimeStampData()[0], (TV_NSEC + TV_SEC * SEC_TO_NS));
170     EXPECT_EQ(constHilogData.TimeStampData()[1], (TV_NSEC_02 + TV_SEC_02 * SEC_TO_NS));
171     EXPECT_EQ(constHilogData.Pids()[0], PID);
172     EXPECT_EQ(constHilogData.Pids()[1], PID_02);
173     EXPECT_EQ(constHilogData.Tids()[0], TID);
174     EXPECT_EQ(constHilogData.Tids()[1], TID_02);
175 
176     auto iterFirst = htraceHiLogParser.logLevelString_.find(LOG_LEVEL_D);
177     if (iterFirst == htraceHiLogParser.logLevelString_.end()) {
178         EXPECT_FALSE(0);
179     }
180     auto &dataDict = stream_.traceDataCache_->dataDict_;
181     EXPECT_EQ(constHilogData.Levels()[0], dataDict.GetStringIndex(iterFirst->second.c_str()));
182 
183     auto iterSecond = htraceHiLogParser.logLevelString_.find(LOG_LEVEL_E);
184     if (iterSecond == htraceHiLogParser.logLevelString_.end()) {
185         EXPECT_FALSE(0);
186     }
187     EXPECT_EQ(constHilogData.Levels()[1], dataDict.GetStringIndex(iterSecond->second.c_str()));
188 
189     EXPECT_EQ(constHilogData.Tags()[0], dataDict.GetStringIndex(LOG_TAG));
190     EXPECT_EQ(constHilogData.Tags()[1], dataDict.GetStringIndex(LOG_TAG_02));
191     EXPECT_EQ(constHilogData.Contexts()[0], dataDict.GetStringIndex(LOG_CONTEXT));
192     EXPECT_EQ(constHilogData.Contexts()[1], dataDict.GetStringIndex(LOG_CONTEXT_02));
193     EXPECT_EQ(stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_RECEIVED), 2);
194 }
195 
196 /**
197  * @tc.name: ParseHilogInfoWithErrLevelHilogLine
198  * @tc.desc: Parse a HilogInfo with error level Hiloglines
199  * @tc.type: FUNC
200  */
201 HWTEST_F(HilogParserTest, ParseHilogInfoWithErrLevelHilogLine, TestSize.Level1)
202 {
203     TS_LOGI("test8-4");
204 
205     HilogDetails *hilogDetails = new HilogDetails();
206     hilogDetails->set_tv_sec(TV_SEC);
207     hilogDetails->set_tv_nsec(TV_NSEC);
208     hilogDetails->set_pid(PID);
209     hilogDetails->set_tid(TID);
210     hilogDetails->set_tag(LOG_TAG);
211 
212     HilogInfo *hilogInfo = new HilogInfo();
213     auto hilogLine = hilogInfo->add_info();
214     hilogLine->set_allocated_detail(hilogDetails);
215     hilogLine->set_context(LOG_CONTEXT);
216     hilogLine->set_id(LOG_ID);
217 
218     PbreaderHiLogParser htraceHiLogParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
219     std::string hilogData = "";
220     hilogInfo->SerializeToString(&hilogData);
221     ProtoReader::BytesView hilogInfoData(reinterpret_cast<const uint8_t *>(hilogData.data()), hilogData.size());
222     bool issplit = false;
223     htraceHiLogParser.Parse(hilogInfoData, issplit);
224 
225     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_RECEIVED);
226     EXPECT_TRUE(1 == eventCount);
227     eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_DATA_INVALID);
228     EXPECT_TRUE(1 == eventCount);
229 }
230 
231 /**
232  * @tc.name: ParseHilogInfoLostHilogLine
233  * @tc.desc: Parse a HilogInfo that lost a Hiloglines
234  * @tc.type: FUNC
235  */
236 HWTEST_F(HilogParserTest, ParseHilogInfoLostHilogLine, TestSize.Level1)
237 {
238     TS_LOGI("test8-5");
239 
240     HilogDetails *hilogDetails = new HilogDetails();
241     hilogDetails->set_tv_sec(TV_SEC);
242     hilogDetails->set_tv_nsec(TV_NSEC);
243     hilogDetails->set_pid(PID);
244     hilogDetails->set_tid(TID);
245     hilogDetails->set_level(LOG_LEVEL_D);
246     hilogDetails->set_tag(LOG_TAG);
247 
248     HilogInfo *hilogInfo = new HilogInfo();
249     auto hilogLine = hilogInfo->add_info();
250     hilogLine->set_allocated_detail(hilogDetails);
251     hilogLine->set_context(LOG_CONTEXT);
252     hilogLine->set_id(LOG_ID_02);
253 
254     PbreaderHiLogParser htraceHiLogParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
255     std::string hilogData = "";
256     hilogInfo->SerializeToString(&hilogData);
257     ProtoReader::BytesView hilogInfoData(reinterpret_cast<const uint8_t *>(hilogData.data()), hilogData.size());
258     bool issplit = false;
259     htraceHiLogParser.Parse(hilogInfoData, issplit);
260 
261     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_RECEIVED);
262     EXPECT_TRUE(1 == eventCount);
263     eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_DATA_LOST);
264     EXPECT_TRUE(1 == eventCount);
265 }
266 
267 /**
268  * @tc.name: ParseHilogInfoHasDuplicateHilogLine
269  * @tc.desc: Parse a HilogInfo has duplicate HilogLine
270  * @tc.type: FUNC
271  */
272 HWTEST_F(HilogParserTest, ParseHilogInfoHasDuplicateHilogLine, TestSize.Level1)
273 {
274     TS_LOGI("test8-6");
275 
276     HilogDetails *hilogDetails = new HilogDetails();
277     hilogDetails->set_tv_sec(TV_SEC);
278     hilogDetails->set_tv_nsec(TV_NSEC);
279     hilogDetails->set_pid(PID);
280     hilogDetails->set_tid(TID);
281     hilogDetails->set_level(LOG_LEVEL_D);
282     hilogDetails->set_tag(LOG_TAG);
283 
284     HilogInfo *hilogInfo = new HilogInfo();
285     auto hilogLineFirst = hilogInfo->add_info();
286     hilogLineFirst->set_allocated_detail(hilogDetails);
287     hilogLineFirst->set_context(LOG_CONTEXT);
288     hilogLineFirst->set_id(LOG_ID);
289     auto hilogLineSecond = hilogInfo->add_info();
290     hilogLineSecond->set_allocated_detail(hilogDetails);
291     hilogLineSecond->set_context(LOG_CONTEXT);
292     hilogLineSecond->set_id(LOG_ID);
293 
294     PbreaderHiLogParser htraceHiLogParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
295     std::string hilogData = "";
296     hilogInfo->SerializeToString(&hilogData);
297     ProtoReader::BytesView hilogInfoData(reinterpret_cast<const uint8_t *>(hilogData.data()), hilogData.size());
298     bool issplit = false;
299     htraceHiLogParser.Parse(hilogInfoData, issplit);
300 
301     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_RECEIVED);
302     EXPECT_TRUE(2 == eventCount);
303     eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_NOTMATCH);
304     EXPECT_TRUE(1 == eventCount);
305 }
306 
307 /**
308  * @tc.name: ParseTxtHilogInfo
309  * @tc.desc: Parse a text format HilogInfo
310  * @tc.type: FUNC
311  */
312 HWTEST_F(HilogParserTest, ParseTxtHilogInfo, TestSize.Level1)
313 {
314     TS_LOGI("test8-7");
315     constexpr size_t readSize = 1024;
316     constexpr uint32_t lineLength = 256;
317     char data[] = "08-07 11:04:45.947   523   640 E C04200/Root: <205>cannot find windowNode\n";
318 
319     std::unique_ptr<SysTuning::TraceStreamer::TraceStreamerSelector> ta =
320         std::make_unique<SysTuning::TraceStreamer::TraceStreamerSelector>();
321     ta->EnableMetaTable(false);
322 
323     std::unique_ptr<uint8_t[]> buf = std::make_unique<uint8_t[]>(readSize);
324     memcpy_s(buf.get(), readSize, data, sizeof(data));
325 
326     EXPECT_TRUE(ta->ParseTraceDataSegment(std::move(buf), sizeof(data), 0, 1));
327     ta->WaitForParserEnd();
328 
329     EXPECT_TRUE(ta->traceDataCache_->GetConstHilogData().HilogLineSeqs().size() == 1);
330 }
331 
332 /**
333  * @tc.name: ParseTxtHilogInfoWithTimeFormat
334  * @tc.desc: Parse a text format HilogInfo with different time format
335  * @tc.type: FUNC
336  */
337 HWTEST_F(HilogParserTest, ParseTxtHilogInfoWithTimeFormat, TestSize.Level1)
338 {
339     TS_LOGI("test8-7");
340     constexpr size_t readSize = 1024;
341     constexpr uint32_t lineLength = 256;
342     char data[] =
343         "08-07 11:04:45.947   523   640 E C04200/Root: <205>cannot find windowNode\n"
344         "CST 08-05 17:41:00.039   955   955 I C03900/Ace: [list_layout_algorithm.cpp(Measure)-(0)] child size is "
345         "empty\n"
346         "CST 2017-08-05 17:41:19.409   840   926 I C01560/WifiDeviceServiceImpl: thread work normally\n"
347         "1501926013.969  1585  1585 I C02d10/HiView-DOCDB: close ejdb success\n"
348         "2337.006   601   894 E C01200/Ces: [access_token_helper.cpp:(RecordSensitivePermissionUsage):52] permission "
349         "denied\n";
350 
351     std::unique_ptr<SysTuning::TraceStreamer::TraceStreamerSelector> ta =
352         std::make_unique<SysTuning::TraceStreamer::TraceStreamerSelector>();
353     ta->EnableMetaTable(false);
354 
355     std::unique_ptr<uint8_t[]> buf = std::make_unique<uint8_t[]>(readSize);
356     memcpy_s(buf.get(), readSize, data, sizeof(data));
357 
358     EXPECT_TRUE(ta->ParseTraceDataSegment(std::move(buf), sizeof(data), 0, 1));
359     ta->WaitForParserEnd();
360 
361     EXPECT_TRUE(ta->traceDataCache_->GetConstHilogData().HilogLineSeqs().size() == 5);
362 }
363 } // namespace HilogUnitTest
364 } // namespace TraceStreamer
365 } // namespace SysTuning