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