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