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