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 "string_help.h" 25 #include "trace_streamer_selector.h" 26 27 using namespace testing::ext; 28 using namespace SysTuning::TraceStreamer; 29 using namespace SysTuning::base; 30 31 namespace SysTuning { 32 namespace TraceStreamer { 33 class BytraceParserTest : public ::testing::Test { 34 public: SetUp()35 void SetUp() 36 { 37 stream_.InitFilter(); 38 } 39 TearDown()40 void TearDown() {} 41 42 public: 43 SysTuning::TraceStreamer::TraceStreamerSelector stream_ = {}; 44 const std::string dbPath_ = "../../test/resource/out.db"; 45 }; 46 47 /** 48 * @tc.name: ParseNoData 49 * @tc.desc: Test ParseTraceDataSegment interface Parse empty memory 50 * @tc.type: FUNC 51 */ 52 HWTEST_F(BytraceParserTest, ParseNoData, TestSize.Level1) 53 { 54 TS_LOGI("test2-1"); 55 auto buf = std::make_unique<uint8_t[]>(1); 56 BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 57 bytraceParser.ParseTraceDataSegment(std::move(buf), 1); 58 bytraceParser.WaitForParserEnd(); 59 EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0); 60 EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0); 61 EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 0); 62 } 63 64 /** 65 * @tc.name: ParseNoDataWhithLineFlag 66 * @tc.desc: Test ParseTraceDataSegment interface Parse "\n" 67 * @tc.type: FUNC 68 */ 69 HWTEST_F(BytraceParserTest, ParseNoDataWhithLineFlag, TestSize.Level1) 70 { 71 TS_LOGI("test2-2"); 72 constexpr uint32_t bufSize = 1024; 73 auto buf = std::make_unique<uint8_t[]>(bufSize); 74 auto realBufSize = strlen(" \n"); 75 if (memcpy_s(buf.get(), bufSize, " \n", realBufSize)) { 76 EXPECT_TRUE(false); 77 return; 78 } 79 BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 80 bytraceParser.ParseTraceDataSegment(std::move(buf), realBufSize); 81 bytraceParser.WaitForParserEnd(); 82 stream_.traceDataCache_->ExportDatabase(dbPath_); 83 EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0); 84 EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0); 85 EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0); 86 EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 1); 87 } 88 89 /** 90 * @tc.name: ParseInvalidData 91 * @tc.desc: Test ParseTraceDataSegment interface Parse invalid string 92 * @tc.type: FUNC 93 */ 94 HWTEST_F(BytraceParserTest, ParseInvalidData, TestSize.Level1) 95 { 96 TS_LOGI("test2-3"); 97 constexpr uint32_t bufSize = 1024; 98 auto buf = std::make_unique<uint8_t[]>(bufSize); 99 auto realBufSize = strlen("0123456789\n"); 100 if (memcpy_s(buf.get(), bufSize, "0123456789\n", realBufSize)) { 101 EXPECT_TRUE(false); 102 return; 103 } 104 BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 105 bytraceParser.ParseTraceDataSegment(std::move(buf), realBufSize); 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("test2-4"); 122 constexpr uint32_t bufSize = 1024; 123 auto buf = std::make_unique<uint8_t[]>(bufSize); 124 auto realBufSize = strlen("TRACE: \n# tracer: nop \n# \n"); 125 if (memcpy_s(buf.get(), bufSize, "TRACE: \n# tracer: nop \n# \n", realBufSize)) { 126 EXPECT_TRUE(false); 127 return; 128 } 129 BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 130 bytraceParser.ParseTraceDataSegment(std::move(buf), realBufSize); 131 bytraceParser.WaitForParserEnd(); 132 stream_.traceDataCache_->ExportDatabase(dbPath_); 133 EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0); 134 EXPECT_TRUE(bytraceParser.TraceCommentLines() == 2); 135 EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0); 136 EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 1); 137 } 138 139 /** 140 * @tc.name: ParseInvalidLines 141 * @tc.desc: Test ParseTraceDataSegment interface Parse Multiline Invalid data 142 * @tc.type: FUNC 143 */ 144 HWTEST_F(BytraceParserTest, ParseInvalidLines, TestSize.Level1) 145 { 146 TS_LOGI("test2-5"); 147 constexpr uint32_t bufSize = 1024; 148 auto buf = std::make_unique<uint8_t[]>(bufSize); 149 auto realBufSize = strlen(" \nafafda\n"); 150 if (memcpy_s(buf.get(), bufSize, " \nafafda\n", realBufSize)) { 151 EXPECT_TRUE(false); 152 return; 153 } 154 BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 155 bytraceParser.ParseTraceDataSegment(std::move(buf), realBufSize); 156 bytraceParser.WaitForParserEnd(); 157 stream_.traceDataCache_->ExportDatabase(dbPath_); 158 EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0); 159 EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0); 160 EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0); 161 EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 2); 162 } 163 164 /** 165 * @tc.name: ParseNormal 166 * @tc.desc: Test ParseTraceDataItem interface Parse normal data 167 * @tc.type: FUNC 168 */ 169 HWTEST_F(BytraceParserTest, ParseNormal, TestSize.Level1) 170 { 171 TS_LOGI("test2-6"); 172 std::string str( 173 "ACCS0-2716 ( 2519) [000] ...1 168758.662861: binder_transaction: \ 174 transaction=25137708 dest_node=4336 dest_proc=924 dest_thread=0 reply=0 flags=0x10 code=0x3 \n"); 175 BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 176 bytraceParser.ParseTraceDataItem(str); 177 bytraceParser.WaitForParserEnd(); 178 179 EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0); 180 EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 1); 181 EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 0); 182 } 183 184 /** 185 * @tc.name: LineParser_abnormal_pid_err 186 * @tc.desc: Test ParseTraceDataItem interface Parse data with error pid 187 * @tc.type: FUNC 188 */ 189 HWTEST_F(BytraceParserTest, LineParser_abnormal_pid_err, TestSize.Level1) 190 { 191 TS_LOGI("test2-7"); 192 std::string str( 193 "ACCS0-27X6 ( 2519) [000] ...1 168758.662861: binder_transaction: \ 194 transaction=25137708 dest_node=4336 dest_proc=924 dest_thread=0 reply=0 flags=0x10 code=0x3 \n"); 195 BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 196 bytraceParser.ParseTraceDataItem(str); 197 bytraceParser.WaitForParserEnd(); 198 199 EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0); 200 EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0); 201 EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 1); 202 } 203 204 /** 205 * @tc.name: LineParserWithInvalidCpu 206 * @tc.desc: Test ParseTraceDataItem interface Parse data with invalid cpu 207 * @tc.type: FUNC 208 */ 209 HWTEST_F(BytraceParserTest, LineParserWithInvalidCpu, TestSize.Level1) 210 { 211 TS_LOGI("test2-8"); 212 std::string str( 213 "ACCS0-2716 ( 2519) [00X] ...1 168758.662861: binder_transaction: \ 214 transaction=25137708 dest_node=4336 dest_proc=924 dest_thread=0 reply=0 flags=0x10 code=0x3 \n"); 215 BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 216 bytraceParser.ParseTraceDataItem(str); 217 bytraceParser.WaitForParserEnd(); 218 219 EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0); 220 EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0); 221 EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 1); 222 } 223 224 /** 225 * @tc.name: LineParserWithInvalidTs 226 * @tc.desc: Test ParseTraceDataItem interface Parse data with invalid ts 227 * @tc.type: FUNC 228 */ 229 HWTEST_F(BytraceParserTest, LineParserWithInvalidTs, TestSize.Level1) 230 { 231 TS_LOGI("test2-9"); 232 std::string str( 233 "ACCS0-2716 ( 2519) [000] ...1 168758.662X61: binder_transaction: \ 234 transaction=25137708 dest_node=4336 dest_proc=924 dest_thread=0 reply=0 flags=0x10 code=0x3 \n"); 235 BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 236 bytraceParser.ParseTraceDataItem(str); 237 bytraceParser.WaitForParserEnd(); 238 239 EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0); 240 EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 0); 241 EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 1); 242 } 243 244 /** 245 * @tc.name: NomalHtmlBytraceFile 246 * @tc.desc: Test ParseTraceDataItem interface Parse normal html format bytrace data 247 * @tc.type: FUNC 248 */ 249 HWTEST_F(BytraceParserTest, NomalHtmlBytraceFile, TestSize.Level1) 250 { 251 TS_LOGI("test2-10"); 252 constexpr uint32_t bufSize = 1024; 253 auto buf = std::make_unique<uint8_t[]>(bufSize); 254 char realBuf[] = 255 "<!DOCTYPE html>\r\n<html>\r\n<script class=\"trace-data\" type=\"application/text\">\r\n" 256 "ACCS0-2716 ( 2519) [000] ...1 168758.662861: binder_transaction: " 257 "transaction=25137708 dest_node=4336 dest_proc=924 dest_thread=0 reply=0 flags=0x10 code=0x3 \r\n" 258 "</script>\r\n</html>\n"; 259 auto realBufSize = sizeof(realBuf); 260 261 if (memcpy_s(buf.get(), bufSize, realBuf, realBufSize)) { 262 EXPECT_TRUE(false); 263 return; 264 } 265 BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 266 bytraceParser.ParseTraceDataSegment(std::move(buf), realBufSize); 267 bytraceParser.WaitForParserEnd(); 268 stream_.traceDataCache_->ExportDatabase(dbPath_); 269 EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0); 270 EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0); 271 EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 1); 272 EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 0); 273 } 274 275 /** 276 * @tc.name: MultiScriptHtmlBytraceFile 277 * @tc.desc: Test ParseTraceDataItem interface Parse html format bytrace data with multiple script section 278 * @tc.type: FUNC 279 */ 280 HWTEST_F(BytraceParserTest, MultiScriptHtmlBytraceFile, TestSize.Level1) 281 { 282 TS_LOGI("test2-11"); 283 constexpr uint32_t bufSize = 1024; 284 auto buf = std::make_unique<uint8_t[]>(bufSize); 285 char realBuf[] = 286 "<!DOCTYPE html>\r\n<html>\r\n<script class=\"trace-data\" type=\"application/text\">\r\n" 287 "ACCS0-2716 ( 2519) [000] ...1 168758.662861: binder_transaction: " 288 "transaction=25137708 dest_node=4336 dest_proc=924 dest_thread=0 reply=0 flags=0x10 code=0x3 \r\n" 289 "</script>\r\n<script class=\"trace-data\" type=\"application/text\">\r\n" 290 "{\"traceEvents\": [{\"category\": \"process_argv\", \"name\": \"process_argv\", \"args\": " 291 "{\"argv\": [\"c:\\\\platform-tools\\\\systrace\\\\systrace.py\", \"--from-file\", \"d:\\\\trace_output\", " 292 "\"-o\", \"output.html\"]}, " 293 "\"pid\": 18892, \"ts\": 13988802989.6, \"tid\": 4464, \"ph\": \"M\"}], \"metadata\": {\"clock-domain\": " 294 "\"SYSTRACE\"}} </script>" 295 "</html>\n"; 296 auto realBufSize = sizeof(realBuf); 297 if (memcpy_s(buf.get(), bufSize, realBuf, realBufSize)) { 298 EXPECT_TRUE(false); 299 return; 300 } 301 BytraceParser bytraceParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 302 bytraceParser.ParseTraceDataSegment(std::move(buf), realBufSize); 303 bytraceParser.WaitForParserEnd(); 304 stream_.traceDataCache_->ExportDatabase(dbPath_); 305 EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0); 306 EXPECT_TRUE(bytraceParser.TraceCommentLines() == 0); 307 EXPECT_TRUE(bytraceParser.ParsedTraceValidLines() == 1); 308 EXPECT_TRUE(bytraceParser.ParsedTraceInvalidLines() == 1); 309 } 310 311 } // namespace TraceStreamer 312 } // namespace SysTuning 313