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