1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 <cstring>
17 #include <dlfcn.h>
18 #include <fcntl.h>
19 #include <gtest/gtest.h>
20 #include <cinttypes>
21 #include <cstdio>
22 #include <ctime>
23 #include <unistd.h>
24
25 #include "hilog_plugin.h"
26 #include "plugin_module_api.h"
27
28 using namespace testing::ext;
29
30 namespace {
31 const std::string DEFAULT_RECORD_FILE("/data/local/tmp/");
32 const int FILE_NAME_LEN = 5;
33 const int US_PER_S = 1000000;
34 const int BASE_YEAR = 1900;
35 const int DEFAULT_WAIT = 10;
36 const int TIME_HOUR_WIDTH = 5;
37 constexpr int BUF_MAX_LEN = 32;
38 uint64_t g_testId;
39 std::vector<HilogInfo> g_proto;
40
41 class HilogPluginTest : public ::testing::Test {
42 public:
SetUpTestCase()43 static void SetUpTestCase() {};
TearDownTestCase()44 static void TearDownTestCase() {};
45
SetUp()46 void SetUp() {}
TearDown()47 void TearDown() {}
48 };
49
WriteFunc(WriterStruct* writer, const void* data, size_t size)50 long WriteFunc(WriterStruct* writer, const void* data, size_t size)
51 {
52 if (writer == nullptr || data == nullptr || size <= 0) {
53 return -1;
54 }
55
56 HilogInfo info;
57 if (info.ParseFromArray(data, size) <= 0) {
58 return -1;
59 }
60 g_proto.push_back(info);
61 return 0;
62 }
63
FlushFunc(WriterStruct* writer)64 bool FlushFunc(WriterStruct* writer)
65 {
66 if (writer == nullptr) {
67 return false;
68 }
69 return true;
70 }
71
PluginStart(HilogPlugin& plugin, HilogConfig& config)72 bool PluginStart(HilogPlugin& plugin, HilogConfig& config)
73 {
74 // serialize
75 int size = config.ByteSizeLong();
76 std::vector<uint8_t> configData(size);
77 int ret = config.SerializeToArray(configData.data(), configData.size());
78 CHECK_TRUE(ret > 0, false, "HilogPluginTest: SerializeToArray fail!!!");
79 PROFILER_LOG_INFO(LOG_CORE, "HilogPluginTest: SerializeToArray success");
80
81 // start
82 ret = plugin.Start(configData.data(), configData.size());
83 CHECK_TRUE(ret == 0, false, "HilogPluginTest: start plugin fail!!!");
84 PROFILER_LOG_INFO(LOG_CORE, "HilogPluginTest: Start success");
85
86 return true;
87 }
88
RecordFileExist(std::string& file)89 bool RecordFileExist(std::string& file)
90 {
91 char name[FILE_NAME_LEN] = {0};
92 time_t nSeconds;
93 struct tm* pTM;
94
95 nSeconds = time(nullptr);
96 pTM = localtime(&nSeconds);
97 if (snprintf_s(name, sizeof(name), sizeof(name) - 1, "%04d", pTM->tm_year + BASE_YEAR) < 0) {
98 PROFILER_LOG_ERROR(LOG_CORE, "%s:snprintf_s error", __func__);
99 }
100 if (access(DEFAULT_RECORD_FILE.c_str(), F_OK) != 0) {
101 return false;
102 }
103
104 std::string cmd = "find /data/local/tmp -name \"" + std::string(name) + "*\"";
105 char buff[BUF_MAX_LEN] = {0};
106 std::unique_ptr<FILE, int (*)(FILE*)> fp(popen(cmd.c_str(), "r"), pclose);
107 if (!fp) {
108 PROFILER_LOG_ERROR(LOG_CORE, "%s:: popen error", __func__);
109 return false;
110 }
111
112 char* pRet = fgets(buff, BUF_MAX_LEN - 1, fp.get());
113 CHECK_NOTNULL(pRet, false, "FileCache: fgets Failed, errno(%d)", errno);
114 buff[BUF_MAX_LEN - 1] = '\0';
115 if (strlen(buff)) {
116 file = std::string(buff);
117 return true;
118 }
119 return false;
120 }
121
GetSec(HilogPlugin& plugin, const char* data)122 uint64_t GetSec(HilogPlugin& plugin, const char* data)
123 {
124 time_t nSeconds = time(nullptr);
125 if (nSeconds == 0) {
126 const int bufSize = 256;
127 char buf[bufSize] = { 0 };
128 strerror_r(errno, buf, bufSize);
129 PROFILER_LOG_ERROR(LOG_CORE, "GetSec: get time failed!, errno(%d:%s)", errno, buf);
130 return 0;
131 }
132
133 struct tm* pTM = localtime(&nSeconds);
134 if (pTM == nullptr) {
135 const int bufSize = 256;
136 char buf[bufSize] = { 0 };
137 strerror_r(errno, buf, bufSize);
138 PROFILER_LOG_ERROR(LOG_CORE, "GetSec: get localtime failed!, errno(%d:%s)", errno, buf);
139 return 0;
140 }
141
142 struct tm tmTime = {0};
143 tmTime.tm_year = pTM->tm_year;
144 strptime(data, "%m-%d %H:%M:%S", &tmTime);
145 const char* pTmp = data + TIME_HOUR_WIDTH;
146 long fixHour;
147 CHECK_TRUE(plugin.StringToL(pTmp, fixHour), 0, "%s:strtol fixHour failed", __func__);
148 if (static_cast<int>(fixHour) != tmTime.tm_hour) { // hours since midnight - [0, 23]
149 PROFILER_LOG_INFO(LOG_CORE, "GetSec: hour(%d) <==> fix hour(%ld)!", tmTime.tm_hour, fixHour);
150 tmTime.tm_hour = fixHour;
151 }
152
153 return mktime(&tmTime);
154 }
155
156 /**
157 * @tc.name: hilog plugin
158 * @tc.desc: Test valid level cmd
159 * @tc.type: FUNC
160 */
HWTEST_F(HilogPluginTest, TestValidLevelCmd, TestSize.Level1)161 HWTEST_F(HilogPluginTest, TestValidLevelCmd, TestSize.Level1)
162 {
163 HilogConfig config;
164 HilogPlugin plugin;
165
166 config.set_log_level(Level::LEVEL_UNSPECIFIED);
167 plugin.SetConfig(config);
168 EXPECT_STREQ(plugin.GetlevelCmd().c_str(), "");
169 }
170
171 /**
172 * @tc.name: hilog plugin
173 * @tc.desc: Test e level cmd
174 * @tc.type: FUNC
175 */
HWTEST_F(HilogPluginTest, TestELevelCmd, TestSize.Level1)176 HWTEST_F(HilogPluginTest, TestELevelCmd, TestSize.Level1)
177 {
178 HilogConfig config;
179 HilogPlugin plugin;
180
181 config.set_log_level(Level::ERROR);
182 plugin.SetConfig(config);
183 EXPECT_STREQ(plugin.GetlevelCmd().c_str(), "E");
184 }
185
186 /**
187 * @tc.name: hilog plugin
188 * @tc.desc: Test i level cmd
189 * @tc.type: FUNC
190 */
HWTEST_F(HilogPluginTest, TestILevelCmd, TestSize.Level1)191 HWTEST_F(HilogPluginTest, TestILevelCmd, TestSize.Level1)
192 {
193 HilogConfig config;
194 HilogPlugin plugin;
195
196 config.set_log_level(Level::INFO);
197 plugin.SetConfig(config);
198 EXPECT_STREQ(plugin.GetlevelCmd().c_str(), "I");
199 }
200
201 /**
202 * @tc.name: hilog plugin
203 * @tc.desc: Test d level cmd
204 * @tc.type: FUNC
205 */
HWTEST_F(HilogPluginTest, TestDLevelCmd, TestSize.Level1)206 HWTEST_F(HilogPluginTest, TestDLevelCmd, TestSize.Level1)
207 {
208 HilogConfig config;
209 HilogPlugin plugin;
210
211 config.set_log_level(Level::DEBUG);
212 plugin.SetConfig(config);
213 EXPECT_STREQ(plugin.GetlevelCmd().c_str(), "D");
214 }
215
216 /**
217 * @tc.name: hilog plugin
218 * @tc.desc: Test w level cmd
219 * @tc.type: FUNC
220 */
HWTEST_F(HilogPluginTest, TestWLevelCmd, TestSize.Level1)221 HWTEST_F(HilogPluginTest, TestWLevelCmd, TestSize.Level1)
222 {
223 HilogConfig config;
224 HilogPlugin plugin;
225
226 config.set_log_level(Level::WARN);
227 plugin.SetConfig(config);
228 EXPECT_STREQ(plugin.GetlevelCmd().c_str(), "W");
229 }
230
231 /**
232 * @tc.name: hilog plugin
233 * @tc.desc: Test nullptr param
234 * @tc.type: FUNC
235 */
HWTEST_F(HilogPluginTest, TestNullptrLogLine, TestSize.Level1)236 HWTEST_F(HilogPluginTest, TestNullptrLogLine, TestSize.Level1)
237 {
238 HilogConfig config;
239 HilogPlugin plugin;
240 HilogLine info;
241 const char* data = nullptr;
242
243 plugin.SetConfig(config);
244 plugin.ParseLogLineInfo(data, 0, info);
245 EXPECT_EQ(info.mutable_detail()->tv_sec(), (uint64_t)0);
246 EXPECT_EQ(info.mutable_detail()->tv_nsec(), (uint64_t)0);
247 EXPECT_EQ(info.mutable_detail()->pid(), (uint32_t)0);
248 EXPECT_EQ(info.mutable_detail()->tid(), (uint32_t)0);
249 EXPECT_EQ(info.mutable_detail()->level(), (uint32_t)0);
250 EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
251 EXPECT_STREQ(info.context().c_str(), "");
252 }
253
254 /**
255 * @tc.name: hilog plugin
256 * @tc.desc: Test invalid ParseLogLineInfo
257 * @tc.type: FUNC
258 */
HWTEST_F(HilogPluginTest, TestInvalidLogLine1, TestSize.Level1)259 HWTEST_F(HilogPluginTest, TestInvalidLogLine1, TestSize.Level1)
260 {
261 HilogConfig config;
262 HilogPlugin plugin;
263 HilogLine info;
264 const char* data = "08-30 16:47:16.522";
265
266 plugin.SetConfig(config);
267 plugin.ParseLogLineInfo(data, strlen(data), info);
268 EXPECT_EQ(info.mutable_detail()->tv_sec(), (uint64_t)0);
269 EXPECT_EQ(info.mutable_detail()->tv_nsec(), (uint64_t)0);
270 EXPECT_EQ(info.mutable_detail()->pid(), (uint32_t)0);
271 EXPECT_EQ(info.mutable_detail()->tid(), (uint32_t)0);
272 EXPECT_EQ(info.mutable_detail()->level(), (uint32_t)0);
273 EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
274 EXPECT_STREQ(info.context().c_str(), "");
275 }
276
277 /**
278 * @tc.name: hilog plugin
279 * @tc.desc: Test invalid ParseLogLineInfo
280 * @tc.type: FUNC
281 */
HWTEST_F(HilogPluginTest, TestInvalidLogLine2, TestSize.Level1)282 HWTEST_F(HilogPluginTest, TestInvalidLogLine2, TestSize.Level1)
283 {
284 HilogConfig config;
285 HilogPlugin plugin;
286 HilogLine info;
287 const char* data = "08-30 16:47:16.149566200\n";
288 uint64_t sec = GetSec(plugin, data);
289 uint64_t nsec = 149566200;
290
291 plugin.SetConfig(config);
292 plugin.ParseLogLineInfo(data, strlen(data), info);
293 EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
294 EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
295 EXPECT_EQ(info.mutable_detail()->pid(), (uint32_t)0);
296 EXPECT_EQ(info.mutable_detail()->tid(), (uint32_t)0);
297 EXPECT_EQ(info.mutable_detail()->level(), (uint32_t)0);
298 EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
299 EXPECT_STREQ(info.context().c_str(), "");
300 }
301
302 /**
303 * @tc.name: hilog plugin
304 * @tc.desc: Test invalid ParseLogLineInfo
305 * @tc.type: FUNC
306 */
HWTEST_F(HilogPluginTest, TestInvalidLogLine3, TestSize.Level1)307 HWTEST_F(HilogPluginTest, TestInvalidLogLine3, TestSize.Level1)
308 {
309 HilogConfig config;
310 HilogPlugin plugin;
311 HilogLine info;
312 const char* data = "08-30 16:47:16.149566200 27953 \n";
313 uint64_t sec = GetSec(plugin, data);
314 uint64_t nsec = 149566200;
315 uint32_t target = 27953;
316
317 plugin.SetConfig(config);
318 plugin.ParseLogLineInfo(data, strlen(data), info);
319 EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
320 EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
321 EXPECT_EQ(info.mutable_detail()->pid(), target);
322 EXPECT_EQ(info.mutable_detail()->tid(), (uint32_t)0);
323 EXPECT_EQ(info.mutable_detail()->level(), (uint32_t)0);
324 EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
325 EXPECT_STREQ(info.context().c_str(), "");
326 }
327
328 /**
329 * @tc.name: hilog plugin
330 * @tc.desc: Test invalid ParseLogLineInfo
331 * @tc.type: FUNC
332 */
HWTEST_F(HilogPluginTest, TestInvalidLogLine4, TestSize.Level1)333 HWTEST_F(HilogPluginTest, TestInvalidLogLine4, TestSize.Level1)
334 {
335 HilogConfig config;
336 HilogPlugin plugin;
337 HilogLine info;
338 const char* data = "08-30 16:47:16.149566200 27953 31750 \n";
339 uint64_t sec = GetSec(plugin, data);
340 uint64_t nsec = 149566200;
341 uint32_t target[] = {27953, 31750};
342
343 plugin.SetConfig(config);
344 plugin.ParseLogLineInfo(data, strlen(data), info);
345 EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
346 EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
347 EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
348 EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
349 EXPECT_EQ(info.mutable_detail()->level(), (uint32_t)0);
350 EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
351 EXPECT_STREQ(info.context().c_str(), "");
352 }
353
354 /**
355 * @tc.name: hilog plugin
356 * @tc.desc: Test invalid ParseLogLineInfo
357 * @tc.type: FUNC
358 */
HWTEST_F(HilogPluginTest, TestInvalidLogLine5, TestSize.Level1)359 HWTEST_F(HilogPluginTest, TestInvalidLogLine5, TestSize.Level1)
360 {
361 HilogConfig config;
362 HilogPlugin plugin;
363 HilogLine info;
364 const char* data = "08-30 16:47:16.149566200 27953 31750 I\n";
365 uint64_t sec = GetSec(plugin, data);
366 uint64_t nsec = 149566200;
367 uint32_t target[] = {27953, 31750};
368
369 plugin.SetConfig(config);
370 plugin.ParseLogLineInfo(data, strlen(data), info);
371 EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
372 EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
373 EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
374 EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
375 EXPECT_EQ(info.mutable_detail()->level(), 'I');
376 EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
377 EXPECT_STREQ(info.context().c_str(), "");
378 }
379
380 /**
381 * @tc.name: hilog plugin
382 * @tc.desc: Test invalid ParseLogLineInfo
383 * @tc.type: FUNC
384 */
HWTEST_F(HilogPluginTest, TestInvalidLogLine6, TestSize.Level1)385 HWTEST_F(HilogPluginTest, TestInvalidLogLine6, TestSize.Level1)
386 {
387 HilogConfig config;
388 HilogPlugin plugin;
389 HilogLine info;
390 const char* data = "08-30 16:47:16.522 27953 31750 I 00B00 \n";
391 uint64_t sec = GetSec(plugin, data);
392 uint64_t nsec = 522;
393 uint32_t target[] = {27953, 31750};
394
395 plugin.SetConfig(config);
396 plugin.ParseLogLineInfo(data, strlen(data), info);
397 EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
398 EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
399 EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
400 EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
401 EXPECT_EQ(info.mutable_detail()->level(), 'I');
402 EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
403 EXPECT_STREQ(info.context().c_str(), "");
404 }
405
406 /**
407 * @tc.name: hilog plugin
408 * @tc.desc: Test invalid ParseLogLineInfo
409 * @tc.type: FUNC
410 */
HWTEST_F(HilogPluginTest, TestInvalidLogLine7, TestSize.Level1)411 HWTEST_F(HilogPluginTest, TestInvalidLogLine7, TestSize.Level1)
412 {
413 HilogConfig config;
414 HilogPlugin plugin;
415 HilogLine info;
416 const char* data = "08-30 16:47:16.522 27953 31750 I 00B00/ \n";
417 uint64_t sec = GetSec(plugin, data);
418 uint64_t nsec = 522;
419 uint32_t target[] = {27953, 31750};
420
421 plugin.SetConfig(config);
422 plugin.ParseLogLineInfo(data, strlen(data), info);
423 EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
424 EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
425 EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
426 EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
427 EXPECT_EQ(info.mutable_detail()->level(), 'I');
428 EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
429 EXPECT_STREQ(info.context().c_str(), "");
430 }
431
432 /**
433 * @tc.name: hilog plugin
434 * @tc.desc: Test invalid ParseLogLineInfo
435 * @tc.type: FUNC
436 */
HWTEST_F(HilogPluginTest, TestInvalidLogLine8, TestSize.Level1)437 HWTEST_F(HilogPluginTest, TestInvalidLogLine8, TestSize.Level1)
438 {
439 HilogConfig config;
440 HilogPlugin plugin;
441 HilogLine info;
442 const char* data = "08-30 16:47:16.522 27953 31750 I 00B00/HwMSDPMovementImpl: \n";
443 uint64_t sec = GetSec(plugin, data);
444 uint64_t nsec = 522;
445 uint32_t target[] = {27953, 31750};
446
447 plugin.SetConfig(config);
448 plugin.ParseLogLineInfo(data, strlen(data), info);
449 EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
450 EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
451 EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
452 EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
453 EXPECT_EQ(info.mutable_detail()->level(), 'I');
454 EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "HwMSDPMovementImpl");
455 EXPECT_STREQ(info.context().c_str(), "");
456 }
457
458 /**
459 * @tc.name: hilog plugin
460 * @tc.desc: Test invalid ParseLogLineInfo
461 * @tc.type: FUNC
462 */
HWTEST_F(HilogPluginTest, TestInvalidLogLine9, TestSize.Level1)463 HWTEST_F(HilogPluginTest, TestInvalidLogLine9, TestSize.Level1)
464 {
465 HilogConfig config;
466 HilogPlugin plugin;
467 HilogLine info;
468 const char* data = "08-30 16:47:16.522 27953 31750 I chatty :\n";
469 uint64_t sec = GetSec(plugin, data);
470 uint64_t nsec = 522;
471 uint32_t target[] = {27953, 31750};
472
473 plugin.SetConfig(config);
474 plugin.ParseLogLineInfo(data, strlen(data), info);
475 EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
476 EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
477 EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
478 EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
479 EXPECT_EQ(info.mutable_detail()->level(), 'I');
480 EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "chatty ");
481 EXPECT_STREQ(info.context().c_str(), "");
482 }
483
484 /**
485 * @tc.name: hilog plugin
486 * @tc.desc: Test invalid ParseLogLineInfo
487 * @tc.type: FUNC
488 */
HWTEST_F(HilogPluginTest, TestInvalidLogLine, TestSize.Level1)489 HWTEST_F(HilogPluginTest, TestInvalidLogLine, TestSize.Level1)
490 {
491 HilogConfig config;
492 HilogPlugin plugin;
493 HilogLine info;
494 const char* data = "08-30 16:47:16.522 27953 31750 I 00B00/HwMSDPMovementImpl: mSupportedModule= 0\n";
495 uint64_t sec = GetSec(plugin, data);
496 uint64_t nsec = 522;
497 uint32_t target[] = {27953, 31750};
498
499 plugin.SetConfig(config);
500 plugin.ParseLogLineInfo(data, strlen(data), info);
501 EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
502 EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
503 EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
504 EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
505 EXPECT_EQ(info.mutable_detail()->level(), 'I');
506 EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "HwMSDPMovementImpl");
507 EXPECT_STREQ(info.context().c_str(), "mSupportedModule= 0");
508 }
509
510 /**
511 * @tc.name: hilog plugin
512 * @tc.desc: Test ParseLogLineInfo
513 * @tc.type: FUNC
514 */
HWTEST_F(HilogPluginTest, TestParseLogLine1, TestSize.Level1)515 HWTEST_F(HilogPluginTest, TestParseLogLine1, TestSize.Level1)
516 {
517 HilogConfig config;
518 HilogPlugin plugin;
519 HilogLine info;
520 const char* data = "08-30 16:47:16.149566200 27953 31750 I 00B00/HwMSDPMovementImpl: mSupportedModule= 0\n";
521 uint64_t sec = GetSec(plugin, data);
522 uint64_t nsec = 149566200;
523 uint32_t target[] = {27953, 31750};
524
525 plugin.SetConfig(config);
526 plugin.ParseLogLineInfo(data, strlen(data), info);
527 EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
528 EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
529 EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
530 EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
531 EXPECT_EQ(info.mutable_detail()->level(), 'I');
532 EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "HwMSDPMovementImpl");
533 EXPECT_STREQ(info.context().c_str(), "mSupportedModule= 0");
534 }
535
536 /**
537 * @tc.name: hilog plugin
538 * @tc.desc: Test ParseLogLineInfo
539 * @tc.type: FUNC
540 */
HWTEST_F(HilogPluginTest, TestParseLogLine2, TestSize.Level1)541 HWTEST_F(HilogPluginTest, TestParseLogLine2, TestSize.Level1)
542 {
543 HilogConfig config;
544 HilogPlugin plugin;
545 HilogLine info;
546 const char* data =
547 "08-30 16:47:16.149566200 27953 31750 E chatty : uid=10194(com.zh.heaptest) identical 2 lines\n";
548 uint64_t sec = GetSec(plugin, data);
549 uint64_t nsec = 149566200;
550 uint32_t target[] = {27953, 31750};
551
552 plugin.SetConfig(config);
553 plugin.ParseLogLineInfo(data, strlen(data), info);
554 EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
555 EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
556 EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
557 EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
558 EXPECT_EQ(info.mutable_detail()->level(), 'E');
559 EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "chatty ");
560 EXPECT_STREQ(info.context().c_str(), "uid=10194(com.zh.heaptest) identical 2 lines");
561 }
562
563 /**
564 * @tc.name: hilog plugin
565 * @tc.desc: Test FindFirstNum
566 * @tc.type: FUNC
567 */
HWTEST_F(HilogPluginTest, TestFindFirstNum1, TestSize.Level1)568 HWTEST_F(HilogPluginTest, TestFindFirstNum1, TestSize.Level1)
569 {
570 HilogPlugin plugin;
571 char data[] = {'a', 'b', '\0', '0'};
572 char* cptr = data;
573
574 EXPECT_FALSE(plugin.FindFirstNum(&cptr));
575 }
576
577 /**
578 * @tc.name: hilog plugin
579 * @tc.desc: Test FindFirstNum
580 * @tc.type: FUNC
581 */
HWTEST_F(HilogPluginTest, TestFindFirstNum2, TestSize.Level1)582 HWTEST_F(HilogPluginTest, TestFindFirstNum2, TestSize.Level1)
583 {
584 HilogPlugin plugin;
585 char data[] = {'a', 'b', '1', '\0', '0'};
586 char* cptr = data;
587
588 EXPECT_TRUE(plugin.FindFirstNum(&cptr));
589 EXPECT_STREQ(cptr, "1");
590 }
591
592 /**
593 * @tc.name: hilog plugin
594 * @tc.desc: Test FindFirstNum
595 * @tc.type: FUNC
596 */
HWTEST_F(HilogPluginTest, TestFindFirstNum3, TestSize.Level1)597 HWTEST_F(HilogPluginTest, TestFindFirstNum3, TestSize.Level1)
598 {
599 HilogPlugin plugin;
600 char data[] = {'a', 'b', '1', 'c', '\n', '\0'};
601 char* cptr = data;
602
603 EXPECT_TRUE(plugin.FindFirstNum(&cptr));
604 EXPECT_STREQ(cptr, "1c\n");
605 }
606
607 /**
608 * @tc.name: hilog plugin
609 * @tc.desc: Test FindFirstNum
610 * @tc.type: FUNC
611 */
HWTEST_F(HilogPluginTest, TestFindFirstNum4, TestSize.Level1)612 HWTEST_F(HilogPluginTest, TestFindFirstNum4, TestSize.Level1)
613 {
614 HilogPlugin plugin;
615 char data[] = {'a', 'b', '1', 'c', '\0', '0'};
616 char* cptr = data;
617
618 EXPECT_TRUE(plugin.FindFirstNum(&cptr));
619 EXPECT_STREQ(cptr, "1c");
620 }
621
622 /**
623 * @tc.name: hilog plugin
624 * @tc.desc: Test FindFirstNum
625 * @tc.type: FUNC
626 */
HWTEST_F(HilogPluginTest, TestFindFirstNum5, TestSize.Level1)627 HWTEST_F(HilogPluginTest, TestFindFirstNum5, TestSize.Level1)
628 {
629 HilogPlugin plugin;
630 char data[] = {'a', 'b', '\n', '0'};
631 char* cptr = data;
632
633 EXPECT_FALSE(plugin.FindFirstNum(&cptr));
634 }
635
636 /**
637 * @tc.name: hilog plugin
638 * @tc.desc: Test FindFirstSpace
639 * @tc.type: FUNC
640 */
HWTEST_F(HilogPluginTest, TestFindFirstSpace1, TestSize.Level1)641 HWTEST_F(HilogPluginTest, TestFindFirstSpace1, TestSize.Level1)
642 {
643 HilogPlugin plugin;
644 char data[] = {'a', 'b', '1', '\0', ' '};
645 char* cptr = data;
646
647 EXPECT_FALSE(plugin.FindFirstSpace(&cptr));
648 }
649
650 /**
651 * @tc.name: hilog plugin
652 * @tc.desc: Test FindFirstSpace
653 * @tc.type: FUNC
654 */
HWTEST_F(HilogPluginTest, TestFindFirstSpace2, TestSize.Level1)655 HWTEST_F(HilogPluginTest, TestFindFirstSpace2, TestSize.Level1)
656 {
657 HilogPlugin plugin;
658 char data[] = {'a', 'b', ' ', '\0', '0'};
659 char* cptr = data;
660
661 EXPECT_TRUE(plugin.FindFirstSpace(&cptr));
662 EXPECT_STREQ(cptr, " ");
663 }
664
665 /**
666 * @tc.name: hilog plugin
667 * @tc.desc: Test FindFirstSpace
668 * @tc.type: FUNC
669 */
HWTEST_F(HilogPluginTest, TestFindFirstSpace3, TestSize.Level1)670 HWTEST_F(HilogPluginTest, TestFindFirstSpace3, TestSize.Level1)
671 {
672 HilogPlugin plugin;
673 char data[] = {'\n', 'a', ' ', '\0', '0'};
674 char* cptr = data;
675
676 EXPECT_FALSE(plugin.FindFirstSpace(&cptr));
677 }
678
679 /**
680 * @tc.name: hilog plugin
681 * @tc.desc: Test FindFirstSpace
682 * @tc.type: FUNC
683 */
HWTEST_F(HilogPluginTest, TestFindFirstSpace4, TestSize.Level1)684 HWTEST_F(HilogPluginTest, TestFindFirstSpace4, TestSize.Level1)
685 {
686 HilogPlugin plugin;
687 char data[] = {'a', 'b', ' ', ' ', '\0', '0'};
688 char* cptr = data;
689
690 EXPECT_TRUE(plugin.FindFirstSpace(&cptr));
691 EXPECT_STREQ(cptr, " ");
692 }
693
694 /**
695 * @tc.name: hilog plugin
696 * @tc.desc: Test FindFirstSpace
697 * @tc.type: FUNC
698 */
HWTEST_F(HilogPluginTest, TestFindFirstSpace5, TestSize.Level1)699 HWTEST_F(HilogPluginTest, TestFindFirstSpace5, TestSize.Level1)
700 {
701 HilogPlugin plugin;
702 char data[] = {'.', 'b', ' ', ' ', 'c', '\0', '0'};
703 char* cptr = data;
704
705 EXPECT_TRUE(plugin.FindFirstSpace(&cptr));
706 EXPECT_STREQ(cptr, " c");
707 }
708
709 /**
710 * @tc.name: hilog plugin
711 * @tc.desc: Test RemoveSpaces
712 * @tc.type: FUNC
713 */
HWTEST_F(HilogPluginTest, TestRemoveSpaces1, TestSize.Level1)714 HWTEST_F(HilogPluginTest, TestRemoveSpaces1, TestSize.Level1)
715 {
716 HilogPlugin plugin;
717 char data[] = {' ', '\0', 'a'};
718 char* cptr = data;
719
720 EXPECT_FALSE(plugin.RemoveSpaces(&cptr));
721 }
722
723 /**
724 * @tc.name: hilog plugin
725 * @tc.desc: Test RemoveSpaces
726 * @tc.type: FUNC
727 */
HWTEST_F(HilogPluginTest, TestRemoveSpaces2, TestSize.Level1)728 HWTEST_F(HilogPluginTest, TestRemoveSpaces2, TestSize.Level1)
729 {
730 HilogPlugin plugin;
731 char data[] = {' ', ' ', 'a', '\0'};
732 char* cptr = data;
733
734 EXPECT_TRUE(plugin.RemoveSpaces(&cptr));
735 EXPECT_STREQ(cptr, "a");
736 }
737
738 /**
739 * @tc.name: hilog plugin
740 * @tc.desc: Test RemoveSpaces
741 * @tc.type: FUNC
742 */
HWTEST_F(HilogPluginTest, TestRemoveSpaces3, TestSize.Level1)743 HWTEST_F(HilogPluginTest, TestRemoveSpaces3, TestSize.Level1)
744 {
745 HilogPlugin plugin;
746 char data[] = {' ', 'a', 'b', '\0'};
747 char* cptr = data;
748
749 EXPECT_TRUE(plugin.RemoveSpaces(&cptr));
750 EXPECT_STREQ(cptr, "ab");
751 }
752
753 /**
754 * @tc.name: hilog plugin
755 * @tc.desc: Test RemoveSpaces
756 * @tc.type: FUNC
757 */
HWTEST_F(HilogPluginTest, TestRemoveSpaces4, TestSize.Level1)758 HWTEST_F(HilogPluginTest, TestRemoveSpaces4, TestSize.Level1)
759 {
760 HilogPlugin plugin;
761 char data[] = {'\n', ' ', '\0'};
762 char* cptr = data;
763
764 EXPECT_FALSE(plugin.RemoveSpaces(&cptr));
765 }
766
767 /**
768 * @tc.name: hilog plugin
769 * @tc.desc: Test RemoveSpaces
770 * @tc.type: FUNC
771 */
HWTEST_F(HilogPluginTest, TestRemoveSpaces5, TestSize.Level1)772 HWTEST_F(HilogPluginTest, TestRemoveSpaces5, TestSize.Level1)
773 {
774 HilogPlugin plugin;
775 char data[] = {'a', 'b', ' ', 'c', 'd', '\0'};
776 char* cptr = data;
777
778 EXPECT_TRUE(plugin.RemoveSpaces(&cptr));
779 EXPECT_STREQ(cptr, "ab cd");
780 }
781
782 /**
783 * @tc.name: hilog plugin
784 * @tc.desc: Test write
785 * @tc.type: FUNC
786 */
HWTEST_F(HilogPluginTest, TestFileOperation, TestSize.Level1)787 HWTEST_F(HilogPluginTest, TestFileOperation, TestSize.Level1)
788 {
789 char buff[FILE_NAME_LEN] = {0};
790 std::string testPath = "/data/local/tmp/uttestdir/";
791 std::string cmd = std::string("mkdir ") + testPath;
792 system(cmd.c_str());
793 FileCache file(testPath);
794 char writeByte[] = "1234";
795 int32_t writeLen = static_cast<int32_t>(strlen(writeByte));
796
797 ASSERT_TRUE(file.Open("test.txt"));
798 ASSERT_EQ(file.Write(writeByte, writeLen), writeLen);
799 ASSERT_GT(FILE_NAME_LEN, writeLen);
800 ASSERT_EQ(file.Read(buff), writeLen);
801 ASSERT_TRUE(file.Close());
802 cmd = std::string("rm -rf ") + testPath;
803 system(cmd.c_str());
804 }
805
806 /**
807 * @tc.name: hilog plugin
808 * @tc.desc: start fail test
809 * @tc.type: FUNC
810 */
HWTEST_F(HilogPluginTest, TestStartFail, TestSize.Level1)811 HWTEST_F(HilogPluginTest, TestStartFail, TestSize.Level1)
812 {
813 HilogConfig config;
814 HilogPlugin plugin;
815 WriterStruct writer = {WriteFunc, FlushFunc};
816
817 g_testId = 1;
818 g_proto.erase(g_proto.begin(), g_proto.end());
819 // set config
820 config.set_need_record(true);
821
822 // test plugin process
823 plugin.SetWriter(&writer);
824
825 // serialize
826 int size = config.ByteSizeLong();
827 ASSERT_GT(size, 0);
828 std::vector<uint8_t> configData(size);
829 ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0);
830
831 // start
832 EXPECT_NE(plugin.Start(configData.data(), size - 1), 0);
833 }
834
835 /**
836 * @tc.name: hilog plugin
837 * @tc.desc: Framework test
838 * @tc.type: FUNC
839 */
HWTEST_F(HilogPluginTest, TestFramework, TestSize.Level1)840 HWTEST_F(HilogPluginTest, TestFramework, TestSize.Level1)
841 {
842 std::string path = std::string("libhilogplugin.z.so");
843 void* handle = dlopen(path.c_str(), RTLD_LAZY);
844 EXPECT_NE(handle, nullptr);
845 PluginModuleStruct* plugin = reinterpret_cast<PluginModuleStruct*>(dlsym(handle, "g_pluginModule"));
846 EXPECT_NE(plugin, nullptr);
847 EXPECT_STREQ(plugin->name, "hilog-plugin");
848
849 g_testId = 1;
850 g_proto.erase(g_proto.begin(), g_proto.end());
851
852 // set config
853 HilogConfig config;
854 config.set_need_record(true);
855 int size = config.ByteSizeLong();
856 ASSERT_GT(size, 0);
857 std::vector<uint8_t> configData(size);
858 ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0);
859
860 // test framework process
861 WriterStruct writer = {WriteFunc, FlushFunc};
862 std::vector<uint8_t> dataBuffer(plugin->resultBufferSizeHint);
863 EXPECT_EQ(plugin->callbacks->onRegisterWriterStruct(&writer), 0);
864 EXPECT_EQ(plugin->callbacks->onPluginSessionStart(configData.data(), configData.size()), 0);
865 usleep(US_PER_S * DEFAULT_WAIT); // 10s
866 EXPECT_EQ(plugin->callbacks->onPluginSessionStop(), 0);
867
868 // test proto data
869 int protoSize = g_proto.size();
870 ASSERT_GT(protoSize, 0);
871 g_testId = 1;
872 for (int i = 0; i < protoSize; i++) {
873 HilogInfo info = g_proto[i];
874 for (int j = 0; j < info.info_size(); j++) {
875 EXPECT_EQ(info.info(j).id(), g_testId);
876 g_testId++;
877 }
878 }
879 }
880
881 /**
882 * @tc.name: hilog plugin
883 * @tc.desc: Test default cmd
884 * @tc.type: FUNC
885 */
HWTEST_F(HilogPluginTest, TestDefaultCmd, TestSize.Level1)886 HWTEST_F(HilogPluginTest, TestDefaultCmd, TestSize.Level1)
887 {
888 HilogConfig config;
889 HilogPlugin plugin;
890 WriterStruct writer = {WriteFunc, FlushFunc};
891
892 g_testId = 1;
893 g_proto.erase(g_proto.begin(), g_proto.end());
894
895 // test plugin process
896 plugin.SetWriter(&writer);
897 EXPECT_TRUE(PluginStart(plugin, config));
898 usleep(US_PER_S * DEFAULT_WAIT); // 10s
899 EXPECT_EQ(plugin.Stop(), 0);
900
901 // test proto data
902 int size = g_proto.size();
903 ASSERT_GT(size, 0);
904 for (int i = 0; i < size; i++) {
905 HilogInfo info = g_proto[i];
906 for (int j = 0; j < info.info_size(); j++) {
907 EXPECT_EQ(info.info(j).id(), g_testId);
908 g_testId++;
909 }
910 }
911 }
912
913 /**
914 * @tc.name: hilog plugin
915 * @tc.desc: Test cmd
916 * @tc.type: FUNC
917 */
HWTEST_F(HilogPluginTest, TestFullCmd, TestSize.Level1)918 HWTEST_F(HilogPluginTest, TestFullCmd, TestSize.Level1)
919 {
920 std::string oldFile = "";
921 if (RecordFileExist(oldFile)) {
922 std::string cmd = std::string("rm ") + oldFile;
923 system(cmd.c_str());
924 }
925
926 HilogConfig config;
927 HilogPlugin plugin;
928 WriterStruct writer = {WriteFunc, FlushFunc};
929
930 g_testId = 1;
931 g_proto.erase(g_proto.begin(), g_proto.end());
932 // set config
933 config.set_need_record(true);
934
935 // test plugin process
936 plugin.SetWriter(&writer);
937 EXPECT_TRUE(PluginStart(plugin, config));
938 usleep(US_PER_S * DEFAULT_WAIT); // 10s
939
940 // test proto data
941 int size = g_proto.size();
942 ASSERT_GT(size, 0);
943 for (int i = 0; i < size; i++) {
944 HilogInfo info = g_proto[i];
945 for (int j = 0; j < info.info_size(); j++) {
946 EXPECT_EQ(info.info(j).id(), g_testId);
947 g_testId++;
948 }
949 }
950 // test record file
951 std::string file;
952 ASSERT_TRUE(RecordFileExist(file));
953 file = std::string("rm ") + file;
954 system(file.c_str());
955 EXPECT_EQ(plugin.Stop(), 0);
956 }
957 } // namespace
958