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