• 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 <fcntl.h>
17 #include <hwext/gtest-ext.h>
18 #include <hwext/gtest-tag.h>
19 #include <memory>
20 
21 #include "htrace_hilog_parser.h"
22 #include "parser/bytrace_parser/bytrace_parser.h"
23 #include "parser/common_types.h"
24 #include "trace_streamer_selector.h"
25 
26 using namespace testing::ext;
27 using namespace SysTuning::TraceStreamer;
28 
29 namespace SysTuning {
30 namespace TraceStreamer {
31 class HilogParserTest : public ::testing::Test {
32 public:
SetUp()33     void SetUp()
34     {
35         stream_.InitFilter();
36     }
37 
TearDown()38     void TearDown() {}
39 
40 public:
41     SysTuning::TraceStreamer::TraceStreamerSelector stream_ = {};
42 };
43 
44 /**
45  * @tc.name: ParseHilogInfoWithoutHilogLine
46  * @tc.desc: Parse a HilogInfo that does not contain any hiloglines
47  * @tc.type: FUNC
48  */
49 HWTEST_F(HilogParserTest, ParseHilogInfoWithoutHilogLine, TestSize.Level1)
50 {
51     TS_LOGI("test8-1");
52     HilogInfo* hilogInfo = new HilogInfo();
53     HtraceHiLogParser htraceHiLogParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
54     htraceHiLogParser.Parse(*hilogInfo);
55     auto size = stream_.traceDataCache_->GetConstHilogData().Size();
56     EXPECT_FALSE(size);
57 }
58 
59 /**
60  * @tc.name: ParseHilogInfoWithOneHilogLine
61  * @tc.desc: Parse a HilogInfo with only one Hilogline
62  * @tc.type: FUNC
63  */
64 HWTEST_F(HilogParserTest, ParseHilogInfoWithOneHilogLine, TestSize.Level1)
65 {
66     TS_LOGI("test8-2");
67     const uint64_t TV_SEC = 1632675525;
68     const uint64_t TV_NSEC = 996560700;
69     const std::string LOG_TAG = "HwMSDPMovementService";
70     const std::string LOG_CONTEXT = "handleGetSupportedModule";
71     const uint32_t LOG_LEVEL_D = 68;
72     const uint32_t PID = 2716;
73     const uint32_t TID = 1532;
74     const uint64_t LOG_ID = 1;
75 
76     HilogDetails* hilogDetails = new HilogDetails();
77     hilogDetails->set_tv_sec(TV_SEC);
78     hilogDetails->set_tv_nsec(TV_NSEC);
79     hilogDetails->set_pid(PID);
80     hilogDetails->set_tid(TID);
81     hilogDetails->set_level(LOG_LEVEL_D);
82     hilogDetails->set_tag(LOG_TAG);
83 
84     HilogInfo* hilogInfo = new HilogInfo();
85     auto hilogLine = hilogInfo->add_info();
86     hilogLine->set_allocated_detail(hilogDetails);
87     hilogLine->set_context(LOG_CONTEXT);
88     hilogLine->set_id(LOG_ID);
89 
90     HtraceHiLogParser htraceHiLogParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
91     htraceHiLogParser.Parse(*hilogInfo);
92 
93     auto seq = stream_.traceDataCache_->GetConstHilogData().HilogLineSeqs()[0];
94     EXPECT_EQ(seq, LOG_ID);
95 
96     auto timestamp = stream_.traceDataCache_->GetConstHilogData().TimeStamData()[0];
97     EXPECT_EQ(timestamp, (TV_NSEC + TV_SEC * SEC_TO_NS));
98 
99     auto pid = stream_.traceDataCache_->GetConstHilogData().Pids()[0];
100     EXPECT_EQ(pid, PID);
101 
102     auto tid = stream_.traceDataCache_->GetConstHilogData().Tids()[0];
103     EXPECT_EQ(tid, TID);
104 
105     auto level = stream_.traceDataCache_->GetConstHilogData().Levels()[0];
106     auto iter = htraceHiLogParser.logLevelString_.find(LOG_LEVEL_D);
107     if (iter == htraceHiLogParser.logLevelString_.end()) {
108         EXPECT_FALSE(0);
109     }
110     DataIndex levelDIndex = stream_.traceDataCache_->dataDict_.GetStringIndex(iter->second.c_str());
111     EXPECT_EQ(level, levelDIndex);
112 
113     auto readTagIndex = stream_.traceDataCache_->GetConstHilogData().Tags()[0];
114     DataIndex writeTagIndex = stream_.traceDataCache_->dataDict_.GetStringIndex(LOG_TAG);
115     EXPECT_EQ(readTagIndex, writeTagIndex);
116 
117     auto readContextIndex = stream_.traceDataCache_->GetConstHilogData().Contexts()[0];
118     DataIndex writeContextIndex = stream_.traceDataCache_->dataDict_.GetStringIndex(LOG_CONTEXT);
119     EXPECT_EQ(readContextIndex, writeContextIndex);
120 
121     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_RECEIVED);
122     EXPECT_TRUE(1 == eventCount);
123 }
124 
125 /**
126  * @tc.name: ParseHilogInfoWithMultipleHilogLine
127  * @tc.desc: Parse a HilogInfo with multiple Hiloglines
128  * @tc.type: FUNC
129  */
130 HWTEST_F(HilogParserTest, ParseHilogInfoWithMultipleHilogLine, TestSize.Level1)
131 {
132     TS_LOGI("test8-3");
133     const uint64_t TV_SEC_01 = 1632675525;
134     const uint64_t TV_NSEC_01 = 996560700;
135     const uint32_t PID_01 = 2716;
136     const uint32_t TID_01 = 1532;
137     const uint32_t LOG_LEVEL_D = 68;
138     const std::string LOG_TAG_01 = "HwMSDPMovementService";
139     const std::string LOG_CONTEXT_01 = "handleGetSupportedModule";
140     const uint64_t LOG_ID_01 = 1;
141 
142     HilogDetails* hilogDetailsFirst = new HilogDetails();
143     hilogDetailsFirst->set_tv_sec(TV_SEC_01);
144     hilogDetailsFirst->set_tv_nsec(TV_NSEC_01);
145     hilogDetailsFirst->set_pid(PID_01);
146     hilogDetailsFirst->set_tid(TID_01);
147     hilogDetailsFirst->set_level(LOG_LEVEL_D);
148     hilogDetailsFirst->set_tag(LOG_TAG_01);
149 
150     const uint64_t TV_SEC_02 = 1632688888;
151     const uint64_t TV_NSEC_02 = 996588888;
152     const uint32_t PID_02 = 2532;
153     const uint32_t TID_02 = 1716;
154     const uint32_t LOG_LEVEL_E = 69;
155     const std::string LOG_TAG_02 = "ProfilerService";
156     const std::string LOG_CONTEXT_02 = "POST_RECV_MESSAGE method: /IProfilerService/CreateSession";
157     const uint64_t LOG_ID_02 = 2;
158 
159     HilogDetails* hilogDetailsSecond = new HilogDetails();
160     hilogDetailsSecond->set_tv_sec(TV_SEC_02);
161     hilogDetailsSecond->set_tv_nsec(TV_NSEC_02);
162     hilogDetailsSecond->set_pid(PID_02);
163     hilogDetailsSecond->set_tid(TID_02);
164     hilogDetailsSecond->set_level(LOG_LEVEL_E);
165     hilogDetailsSecond->set_tag(LOG_TAG_02);
166 
167     HilogInfo* hilogInfo = new HilogInfo();
168     auto hilogLineFirst = hilogInfo->add_info();
169     hilogLineFirst->set_allocated_detail(hilogDetailsFirst);
170     hilogLineFirst->set_context(LOG_CONTEXT_01);
171     hilogLineFirst->set_id(LOG_ID_01);
172 
173     auto hilogLineSecond = hilogInfo->add_info();
174     hilogLineSecond->set_allocated_detail(hilogDetailsSecond);
175     hilogLineSecond->set_context(LOG_CONTEXT_02);
176     hilogLineSecond->set_id(LOG_ID_02);
177 
178     HtraceHiLogParser htraceHiLogParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
179     htraceHiLogParser.Parse(*hilogInfo);
180 
181     auto seqFirst = stream_.traceDataCache_->GetConstHilogData().HilogLineSeqs()[0];
182     auto seqSecond = stream_.traceDataCache_->GetConstHilogData().HilogLineSeqs()[1];
183     EXPECT_EQ(seqFirst, LOG_ID_01);
184     EXPECT_EQ(seqSecond, LOG_ID_02);
185 
186     auto timestampFirst = stream_.traceDataCache_->GetConstHilogData().TimeStamData()[0];
187     auto timestampSecond = stream_.traceDataCache_->GetConstHilogData().TimeStamData()[1];
188     EXPECT_EQ(timestampFirst, (TV_NSEC_01 + TV_SEC_01 * SEC_TO_NS));
189     EXPECT_EQ(timestampSecond, (TV_NSEC_02 + TV_SEC_02 * SEC_TO_NS));
190 
191     auto pidFirst = stream_.traceDataCache_->GetConstHilogData().Pids()[0];
192     auto pidSecond = stream_.traceDataCache_->GetConstHilogData().Pids()[1];
193     EXPECT_EQ(pidFirst, PID_01);
194     EXPECT_EQ(pidSecond, PID_02);
195 
196     auto tidFirst = stream_.traceDataCache_->GetConstHilogData().Tids()[0];
197     auto tidSecond = stream_.traceDataCache_->GetConstHilogData().Tids()[1];
198     EXPECT_EQ(tidFirst, TID_01);
199     EXPECT_EQ(tidSecond, TID_02);
200 
201     auto levelFirst = stream_.traceDataCache_->GetConstHilogData().Levels()[0];
202     auto iterFirst = htraceHiLogParser.logLevelString_.find(LOG_LEVEL_D);
203     if (iterFirst == htraceHiLogParser.logLevelString_.end()) {
204         EXPECT_FALSE(0);
205     }
206     DataIndex levelDIndex = stream_.traceDataCache_->dataDict_.GetStringIndex(iterFirst->second.c_str());
207     EXPECT_EQ(levelFirst, levelDIndex);
208 
209     auto levelSecond = stream_.traceDataCache_->GetConstHilogData().Levels()[1];
210     auto iterSecond = htraceHiLogParser.logLevelString_.find(LOG_LEVEL_E);
211     if (iterSecond == htraceHiLogParser.logLevelString_.end()) {
212         EXPECT_FALSE(0);
213     }
214     DataIndex levelEIndex = stream_.traceDataCache_->dataDict_.GetStringIndex(iterSecond->second.c_str());
215     EXPECT_EQ(levelSecond, levelEIndex);
216 
217     auto readTagIndexFirst = stream_.traceDataCache_->GetConstHilogData().Tags()[0];
218     auto readTagIndexSecond = stream_.traceDataCache_->GetConstHilogData().Tags()[1];
219     DataIndex writeTagIndexFirst = stream_.traceDataCache_->dataDict_.GetStringIndex(LOG_TAG_01);
220     DataIndex writeTagIndexSecond = stream_.traceDataCache_->dataDict_.GetStringIndex(LOG_TAG_02);
221     EXPECT_EQ(readTagIndexFirst, writeTagIndexFirst);
222     EXPECT_EQ(readTagIndexSecond, writeTagIndexSecond);
223 
224     auto readContextIndexFirst = stream_.traceDataCache_->GetConstHilogData().Contexts()[0];
225     auto readContextIndexSecond = stream_.traceDataCache_->GetConstHilogData().Contexts()[1];
226     DataIndex writeContextIndexFirst = stream_.traceDataCache_->dataDict_.GetStringIndex(LOG_CONTEXT_01);
227     DataIndex writeContextIndexSecond = stream_.traceDataCache_->dataDict_.GetStringIndex(LOG_CONTEXT_02);
228     EXPECT_EQ(readContextIndexFirst, writeContextIndexFirst);
229     EXPECT_EQ(readContextIndexSecond, writeContextIndexSecond);
230 
231     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_RECEIVED);
232     EXPECT_TRUE(2 == eventCount);
233 }
234 
235 /**
236  * @tc.name: ParseHilogInfoWithErrLevelHilogLine
237  * @tc.desc: Parse a HilogInfo with error level Hiloglines
238  * @tc.type: FUNC
239  */
240 HWTEST_F(HilogParserTest, ParseHilogInfoWithErrLevelHilogLine, TestSize.Level1)
241 {
242     TS_LOGI("test8-4");
243     const uint64_t TV_SEC = 1632675525;
244     const uint64_t TV_NSEC = 996560700;
245     const std::string LOG_TAG = "HwMSDPMovementService";
246     const std::string LOG_CONTEXT = "handleGetSupportedModule";
247     const uint32_t LOG_LEVEL_ILLEGAL = 0;
248     const uint32_t PID = 2716;
249     const uint32_t TID = 1532;
250     const uint64_t LOG_ID = 1;
251 
252     HilogDetails* hilogDetails = new HilogDetails();
253     hilogDetails->set_tv_sec(TV_SEC);
254     hilogDetails->set_tv_nsec(TV_NSEC);
255     hilogDetails->set_pid(PID);
256     hilogDetails->set_tid(TID);
257     hilogDetails->set_level(LOG_LEVEL_ILLEGAL);
258     hilogDetails->set_tag(LOG_TAG);
259 
260     HilogInfo* hilogInfo = new HilogInfo();
261     auto hilogLine = hilogInfo->add_info();
262     hilogLine->set_allocated_detail(hilogDetails);
263     hilogLine->set_context(LOG_CONTEXT);
264     hilogLine->set_id(LOG_ID);
265 
266     HtraceHiLogParser htraceHiLogParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
267     htraceHiLogParser.Parse(*hilogInfo);
268 
269     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_RECEIVED);
270     EXPECT_TRUE(1 == eventCount);
271     eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_DATA_INVALID);
272     EXPECT_TRUE(1 == eventCount);
273 }
274 
275 /**
276  * @tc.name: ParseHilogInfoLostHilogLine
277  * @tc.desc: Parse a HilogInfo that lost a Hiloglines
278  * @tc.type: FUNC
279  */
280 HWTEST_F(HilogParserTest, ParseHilogInfoLostHilogLine, TestSize.Level1)
281 {
282     TS_LOGI("test8-5");
283     const uint64_t TV_SEC = 1632675525;
284     const uint64_t TV_NSEC = 996560700;
285     const std::string LOG_TAG = "HwMSDPMovementService";
286     const std::string LOG_CONTEXT = "handleGetSupportedModule";
287     const uint32_t LOG_LEVEL_D = 68;
288     const uint32_t PID = 2716;
289     const uint32_t TID = 1532;
290     const uint64_t LOG_ID = 2;
291 
292     HilogDetails* hilogDetails = new HilogDetails();
293     hilogDetails->set_tv_sec(TV_SEC);
294     hilogDetails->set_tv_nsec(TV_NSEC);
295     hilogDetails->set_pid(PID);
296     hilogDetails->set_tid(TID);
297     hilogDetails->set_level(LOG_LEVEL_D);
298     hilogDetails->set_tag(LOG_TAG);
299 
300     HilogInfo* hilogInfo = new HilogInfo();
301     auto hilogLine = hilogInfo->add_info();
302     hilogLine->set_allocated_detail(hilogDetails);
303     hilogLine->set_context(LOG_CONTEXT);
304     hilogLine->set_id(LOG_ID);
305 
306     HtraceHiLogParser htraceHiLogParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
307     htraceHiLogParser.Parse(*hilogInfo);
308 
309     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_RECEIVED);
310     EXPECT_TRUE(1 == eventCount);
311     eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_DATA_LOST);
312     EXPECT_TRUE(1 == eventCount);
313 }
314 
315 /**
316  * @tc.name: ParseHilogInfoHasDuplicateHilogLine
317  * @tc.desc: Parse a HilogInfo has duplicate HilogLine
318  * @tc.type: FUNC
319  */
320 HWTEST_F(HilogParserTest, ParseHilogInfoHasDuplicateHilogLine, TestSize.Level1)
321 {
322     TS_LOGI("test8-6");
323     const uint64_t TV_SEC = 1632675525;
324     const uint64_t TV_NSEC = 996560700;
325     const std::string LOG_TAG = "HwMSDPMovementService";
326     const std::string LOG_CONTEXT = "handleGetSupportedModule";
327     const uint32_t LOG_LEVEL_D = 68;
328     const uint32_t PID = 2716;
329     const uint32_t TID = 1532;
330     const uint64_t LOG_ID = 1;
331 
332     HilogDetails* hilogDetails = new HilogDetails();
333     hilogDetails->set_tv_sec(TV_SEC);
334     hilogDetails->set_tv_nsec(TV_NSEC);
335     hilogDetails->set_pid(PID);
336     hilogDetails->set_tid(TID);
337     hilogDetails->set_level(LOG_LEVEL_D);
338     hilogDetails->set_tag(LOG_TAG);
339 
340     HilogInfo* hilogInfo = new HilogInfo();
341     auto hilogLineFirst = hilogInfo->add_info();
342     hilogLineFirst->set_allocated_detail(hilogDetails);
343     hilogLineFirst->set_context(LOG_CONTEXT);
344     hilogLineFirst->set_id(LOG_ID);
345     auto hilogLineSecond = hilogInfo->add_info();
346     hilogLineSecond->set_allocated_detail(hilogDetails);
347     hilogLineSecond->set_context(LOG_CONTEXT);
348     hilogLineSecond->set_id(LOG_ID);
349 
350     HtraceHiLogParser htraceHiLogParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
351     htraceHiLogParser.Parse(*hilogInfo);
352 
353     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_RECEIVED);
354     EXPECT_TRUE(2 == eventCount);
355     eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_HILOG, STAT_EVENT_NOTMATCH);
356     EXPECT_TRUE(1 == eventCount);
357 }
358 } // namespace TraceStreamer
359 } // namespace SysTuning