• 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 #include <string>
21 
22 #include "parser/bytrace_parser/bytrace_parser.h"
23 #include "parser/common_types.h"
24 #include "securec.h"
25 #include "trace_streamer_selector.h"
26 
27 using namespace testing::ext;
28 using namespace SysTuning::TraceStreamer;
29 
30 namespace SysTuning {
31 namespace TraceStreamer {
32 class BytraceParserTest : public ::testing::Test {
33 public:
SetUp()34     void SetUp()
35     {
36         stream_.InitFilter();
37     }
38 
TearDown()39     void TearDown() {}
40 
41 public:
42     SysTuning::TraceStreamer::TraceStreamerSelector stream_ = {};
43     const std::string dbPath_ = "/data/resource/out.db";
44 };
45 
46 /**
47  * @tc.name: ParseNoData
48  * @tc.desc: Test ParseTraceDataSegment interface Parse empty memory
49  * @tc.type: FUNC
50  */
51 HWTEST_F(BytraceParserTest, ParseNoData, TestSize.Level1)
52 {
53     TS_LOGI("test1-1");
54     auto buf = std::make_unique<uint8_t[]>(1);
55     BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
56     printf("xxx\n");
57     bytraceParser.ParseTraceDataSegment(std::move(buf), 1);
58     printf("xxx2\n");
59     bytraceParser.WaitForParserEnd();
60     printf("xxx3\n");
61     EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0);
62     EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0);
63     EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 0);
64 }
65 
66 /**
67  * @tc.name: ParseNoDataWhithLineFlag
68  * @tc.desc: Test ParseTraceDataSegment interface Parse "\n"
69  * @tc.type: FUNC
70  */
71 HWTEST_F(BytraceParserTest, ParseNoDataWhithLineFlag, TestSize.Level1)
72 {
73     TS_LOGI("test1-2");
74     constexpr uint32_t bufSize = 1024;
75     auto buf = std::make_unique<uint8_t[]>(bufSize);
76     if (memcpy_s(buf.get(), bufSize, " \n", strlen(" \n"))) {
77         EXPECT_TRUE(false);
78         return;
79     }
80     BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
81     bytraceParser.ParseTraceDataSegment(std::move(buf), bufSize);
82     bytraceParser.WaitForParserEnd();
83     stream_.traceDataCache_->ExportDatabase(dbPath_);
84     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
85     EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0);
86     EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0);
87     EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 1);
88 }
89 
90 /**
91  * @tc.name: ParseInvalidData
92  * @tc.desc: Test ParseTraceDataSegment interface Parse invalid string
93  * @tc.type: FUNC
94  */
95 HWTEST_F(BytraceParserTest, ParseInvalidData, TestSize.Level1)
96 {
97     TS_LOGI("test1-3");
98     constexpr uint32_t bufSize = 1024;
99     auto buf = std::make_unique<uint8_t[]>(bufSize);
100     if (memcpy_s(buf.get(), bufSize, "0123456789\n", strlen("0123456789\n"))) {
101         EXPECT_TRUE(false);
102         return;
103     }
104     BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
105     bytraceParser.ParseTraceDataSegment(std::move(buf), bufSize);
106     bytraceParser.WaitForParserEnd();
107     stream_.traceDataCache_->ExportDatabase(dbPath_);
108     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
109     EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0);
110     EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0);
111     EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 1);
112 }
113 
114 /**
115  * @tc.name: ParseComment
116  * @tc.desc: Test ParseTraceDataSegment interface Parse Multiline data
117  * @tc.type: FUNC
118  */
119 HWTEST_F(BytraceParserTest, ParseComment, TestSize.Level1)
120 {
121     TS_LOGI("test1-4");
122     constexpr uint32_t bufSize = 1024;
123     auto buf = std::make_unique<uint8_t[]>(bufSize);
124     if (memcpy_s(buf.get(), bufSize, "TRACE: \n# tracer: nop \n# \n", strlen("TRACE: \n# tracer: nop \n# \n"))) {
125         EXPECT_TRUE(false);
126         return;
127     }
128     BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
129     bytraceParser.ParseTraceDataSegment(std::move(buf), bufSize);
130     bytraceParser.WaitForParserEnd();
131     stream_.traceDataCache_->ExportDatabase(dbPath_);
132     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
133     EXPECT_TRUE(bytraceParser.TraceCommentLines() == 2);
134     EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0);
135     EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 1);
136 }
137 
138 /**
139  * @tc.name: ParseInvalidLines
140  * @tc.desc: Test ParseTraceDataSegment interface Parse Multiline Invalid data
141  * @tc.type: FUNC
142  */
143 HWTEST_F(BytraceParserTest, ParseInvalidLines, TestSize.Level1)
144 {
145     TS_LOGI("test1-5");
146     constexpr uint32_t bufSize = 1024;
147     auto buf = std::make_unique<uint8_t[]>(bufSize);
148     if (memcpy_s(buf.get(), bufSize, "\nafafda\n", strlen("\nafafda\n"))) {
149         EXPECT_TRUE(false);
150         return;
151     }
152     BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
153     bytraceParser.ParseTraceDataSegment(std::move(buf), bufSize);
154     bytraceParser.WaitForParserEnd();
155     stream_.traceDataCache_->ExportDatabase(dbPath_);
156     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
157     EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0);
158     EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0);
159     EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 2);
160 }
161 
162 /**
163  * @tc.name: ParseNormal
164  * @tc.desc: Test ParseTraceDataItem interface Parse normal data
165  * @tc.type: FUNC
166  */
167 HWTEST_F(BytraceParserTest, ParseNormal, TestSize.Level1)
168 {
169     TS_LOGI("test1-6");
170     std::string str(
171         "ACCS0-2716  ( 2519) [000] ...1 168758.662861: binder_transaction: \
172         transaction=25137708 dest_node=4336 dest_proc=924 dest_thread=0 reply=0 flags=0x10 code=0x3\n");
173     BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
174     bytraceParser.ParseTraceDataItem(str);
175     bytraceParser.WaitForParserEnd();
176 
177     EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0);
178     EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 1);
179     EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 0);
180 }
181 
182 /**
183  * @tc.name: LineParser_abnormal_pid_err
184  * @tc.desc: Test ParseTraceDataItem interface Parse data with error pid
185  * @tc.type: FUNC
186  */
187 HWTEST_F(BytraceParserTest, LineParser_abnormal_pid_err, TestSize.Level1)
188 {
189     TS_LOGI("test1-7");
190     std::string str(
191         "ACCS0-27X6  ( 2519) [000] ...1 168758.662861: binder_transaction: \
192         transaction=25137708 dest_node=4336 dest_proc=924 dest_thread=0 reply=0 flags=0x10 code=0x3\n");
193     BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
194     bytraceParser.ParseTraceDataItem(str);
195     bytraceParser.WaitForParserEnd();
196 
197     EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0);
198     EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0);
199     EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 1);
200 }
201 
202 /**
203  * @tc.name: LineParserWithInvalidCpu
204  * @tc.desc: Test ParseTraceDataItem interface Parse data with invalid cpu
205  * @tc.type: FUNC
206  */
207 HWTEST_F(BytraceParserTest, LineParserWithInvalidCpu, TestSize.Level1)
208 {
209     TS_LOGI("test1-8");
210     std::string str(
211         "ACCS0-2716  ( 2519) [00X] ...1 168758.662861: binder_transaction: \
212         transaction=25137708 dest_node=4336 dest_proc=924 dest_thread=0 reply=0 flags=0x10 code=0x3\n");
213     BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
214     bytraceParser.ParseTraceDataItem(str);
215     bytraceParser.WaitForParserEnd();
216 
217     EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0);
218     EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0);
219     EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 1);
220 }
221 
222 /**
223  * @tc.name: LineParserWithInvalidTs
224  * @tc.desc: Test ParseTraceDataItem interface Parse data with invalid ts
225  * @tc.type: FUNC
226  */
227 HWTEST_F(BytraceParserTest, LineParserWithInvalidTs, TestSize.Level1)
228 {
229     TS_LOGI("test1-9");
230     std::string str(
231         "ACCS0-2716  ( 2519) [000] ...1 168758.662X61: binder_transaction: \
232         transaction=25137708 dest_node=4336 dest_proc=924 dest_thread=0 reply=0 flags=0x10 code=0x3\n");
233     BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
234     bytraceParser.ParseTraceDataItem(str);
235     bytraceParser.WaitForParserEnd();
236 
237     EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0);
238     EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0);
239     EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 1);
240 }
241 } // namespace TraceStreamer
242 } // namespace SysTuning
243