• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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     PROFILER_LOG_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     PROFILER_LOG_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         PROFILER_LOG_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         PROFILER_LOG_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         PROFILER_LOG_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         PROFILER_LOG_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         PROFILER_LOG_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 level cmd
165  * @tc.type: FUNC
166  */
167 HWTEST_F(HilogPluginTest, TestValidLevelCmd, TestSize.Level1)
168 {
169     HilogConfig config;
170     HilogPlugin plugin;
171 
172     config.set_log_level(Level::LEVEL_UNSPECIFIED);
173     plugin.SetConfig(config);
174     EXPECT_STREQ(plugin.GetlevelCmd().c_str(), "");
175 }
176 
177 /**
178  * @tc.name: hilog plugin
179  * @tc.desc: Test e level cmd
180  * @tc.type: FUNC
181  */
182 HWTEST_F(HilogPluginTest, TestELevelCmd, TestSize.Level1)
183 {
184     HilogConfig config;
185     HilogPlugin plugin;
186 
187     config.set_log_level(Level::ERROR);
188     plugin.SetConfig(config);
189     EXPECT_STREQ(plugin.GetlevelCmd().c_str(), "E");
190 }
191 
192 /**
193  * @tc.name: hilog plugin
194  * @tc.desc: Test i level cmd
195  * @tc.type: FUNC
196  */
197 HWTEST_F(HilogPluginTest, TestILevelCmd, TestSize.Level1)
198 {
199     HilogConfig config;
200     HilogPlugin plugin;
201 
202     config.set_log_level(Level::INFO);
203     plugin.SetConfig(config);
204     EXPECT_STREQ(plugin.GetlevelCmd().c_str(), "I");
205 }
206 
207 /**
208  * @tc.name: hilog plugin
209  * @tc.desc: Test d level cmd
210  * @tc.type: FUNC
211  */
212 HWTEST_F(HilogPluginTest, TestDLevelCmd, TestSize.Level1)
213 {
214     HilogConfig config;
215     HilogPlugin plugin;
216 
217     config.set_log_level(Level::DEBUG);
218     plugin.SetConfig(config);
219     EXPECT_STREQ(plugin.GetlevelCmd().c_str(), "D");
220 }
221 
222 /**
223  * @tc.name: hilog plugin
224  * @tc.desc: Test w level cmd
225  * @tc.type: FUNC
226  */
227 HWTEST_F(HilogPluginTest, TestWLevelCmd, TestSize.Level1)
228 {
229     HilogConfig config;
230     HilogPlugin plugin;
231 
232     config.set_log_level(Level::WARN);
233     plugin.SetConfig(config);
234     EXPECT_STREQ(plugin.GetlevelCmd().c_str(), "W");
235 }
236 
237 /**
238  * @tc.name: hilog plugin
239  * @tc.desc: Test nullptr param
240  * @tc.type: FUNC
241  */
242 HWTEST_F(HilogPluginTest, TestNullptrLogLine, TestSize.Level1)
243 {
244     HilogConfig config;
245     HilogPlugin plugin;
246     HilogLine info;
247     const char* data = nullptr;
248 
249     plugin.SetConfig(config);
250     plugin.ParseLogLineInfo(data, 0, info);
251     EXPECT_EQ(info.mutable_detail()->tv_sec(), (uint64_t)0);
252     EXPECT_EQ(info.mutable_detail()->tv_nsec(), (uint64_t)0);
253     EXPECT_EQ(info.mutable_detail()->pid(), (uint32_t)0);
254     EXPECT_EQ(info.mutable_detail()->tid(), (uint32_t)0);
255     EXPECT_EQ(info.mutable_detail()->level(), (uint32_t)0);
256     EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
257     EXPECT_STREQ(info.context().c_str(), "");
258 }
259 
260 /**
261  * @tc.name: hilog plugin
262  * @tc.desc: Test invalid ParseLogLineInfo
263  * @tc.type: FUNC
264  */
265 HWTEST_F(HilogPluginTest, TestInvalidLogLine1, TestSize.Level1)
266 {
267     HilogConfig config;
268     HilogPlugin plugin;
269     HilogLine info;
270     const char* data = "08-30 16:47:16.522";
271 
272     plugin.SetConfig(config);
273     plugin.ParseLogLineInfo(data, strlen(data), info);
274     EXPECT_EQ(info.mutable_detail()->tv_sec(), (uint64_t)0);
275     EXPECT_EQ(info.mutable_detail()->tv_nsec(), (uint64_t)0);
276     EXPECT_EQ(info.mutable_detail()->pid(), (uint32_t)0);
277     EXPECT_EQ(info.mutable_detail()->tid(), (uint32_t)0);
278     EXPECT_EQ(info.mutable_detail()->level(), (uint32_t)0);
279     EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
280     EXPECT_STREQ(info.context().c_str(), "");
281 }
282 
283 /**
284  * @tc.name: hilog plugin
285  * @tc.desc: Test invalid ParseLogLineInfo
286  * @tc.type: FUNC
287  */
288 HWTEST_F(HilogPluginTest, TestInvalidLogLine2, TestSize.Level1)
289 {
290     HilogConfig config;
291     HilogPlugin plugin;
292     HilogLine info;
293     const char* data = "08-30 16:47:16.149566200\n";
294     uint64_t sec = GetSec(plugin, data);
295     uint64_t nsec = 149566200;
296 
297     plugin.SetConfig(config);
298     plugin.ParseLogLineInfo(data, strlen(data), info);
299     EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
300     EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
301     EXPECT_EQ(info.mutable_detail()->pid(), (uint32_t)0);
302     EXPECT_EQ(info.mutable_detail()->tid(), (uint32_t)0);
303     EXPECT_EQ(info.mutable_detail()->level(), (uint32_t)0);
304     EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
305     EXPECT_STREQ(info.context().c_str(), "");
306 }
307 
308 /**
309  * @tc.name: hilog plugin
310  * @tc.desc: Test invalid ParseLogLineInfo
311  * @tc.type: FUNC
312  */
313 HWTEST_F(HilogPluginTest, TestInvalidLogLine3, TestSize.Level1)
314 {
315     HilogConfig config;
316     HilogPlugin plugin;
317     HilogLine info;
318     const char* data = "08-30 16:47:16.149566200 27953 \n";
319     uint64_t sec = GetSec(plugin, data);
320     uint64_t nsec = 149566200;
321     uint32_t target = 27953;
322 
323     plugin.SetConfig(config);
324     plugin.ParseLogLineInfo(data, strlen(data), info);
325     EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
326     EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
327     EXPECT_EQ(info.mutable_detail()->pid(), target);
328     EXPECT_EQ(info.mutable_detail()->tid(), (uint32_t)0);
329     EXPECT_EQ(info.mutable_detail()->level(), (uint32_t)0);
330     EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
331     EXPECT_STREQ(info.context().c_str(), "");
332 }
333 
334 /**
335  * @tc.name: hilog plugin
336  * @tc.desc: Test invalid ParseLogLineInfo
337  * @tc.type: FUNC
338  */
339 HWTEST_F(HilogPluginTest, TestInvalidLogLine4, TestSize.Level1)
340 {
341     HilogConfig config;
342     HilogPlugin plugin;
343     HilogLine info;
344     const char* data = "08-30 16:47:16.149566200 27953 31750 \n";
345     uint64_t sec = GetSec(plugin, data);
346     uint64_t nsec = 149566200;
347     uint32_t target[] = {27953, 31750};
348 
349     plugin.SetConfig(config);
350     plugin.ParseLogLineInfo(data, strlen(data), info);
351     EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
352     EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
353     EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
354     EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
355     EXPECT_EQ(info.mutable_detail()->level(), (uint32_t)0);
356     EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
357     EXPECT_STREQ(info.context().c_str(), "");
358 }
359 
360 /**
361  * @tc.name: hilog plugin
362  * @tc.desc: Test invalid ParseLogLineInfo
363  * @tc.type: FUNC
364  */
365 HWTEST_F(HilogPluginTest, TestInvalidLogLine5, TestSize.Level1)
366 {
367     HilogConfig config;
368     HilogPlugin plugin;
369     HilogLine info;
370     const char* data = "08-30 16:47:16.149566200 27953 31750 I\n";
371     uint64_t sec = GetSec(plugin, data);
372     uint64_t nsec = 149566200;
373     uint32_t target[] = {27953, 31750};
374 
375     plugin.SetConfig(config);
376     plugin.ParseLogLineInfo(data, strlen(data), info);
377     EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
378     EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
379     EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
380     EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
381     EXPECT_EQ(info.mutable_detail()->level(), 'I');
382     EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
383     EXPECT_STREQ(info.context().c_str(), "");
384 }
385 
386 /**
387  * @tc.name: hilog plugin
388  * @tc.desc: Test invalid ParseLogLineInfo
389  * @tc.type: FUNC
390  */
391 HWTEST_F(HilogPluginTest, TestInvalidLogLine6, TestSize.Level1)
392 {
393     HilogConfig config;
394     HilogPlugin plugin;
395     HilogLine info;
396     const char* data = "08-30 16:47:16.522 27953 31750 I 00B00 \n";
397     uint64_t sec = GetSec(plugin, data);
398     uint64_t nsec = 522;
399     uint32_t target[] = {27953, 31750};
400 
401     plugin.SetConfig(config);
402     plugin.ParseLogLineInfo(data, strlen(data), info);
403     EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
404     EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
405     EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
406     EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
407     EXPECT_EQ(info.mutable_detail()->level(), 'I');
408     EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
409     EXPECT_STREQ(info.context().c_str(), "");
410 }
411 
412 /**
413  * @tc.name: hilog plugin
414  * @tc.desc: Test invalid ParseLogLineInfo
415  * @tc.type: FUNC
416  */
417 HWTEST_F(HilogPluginTest, TestInvalidLogLine7, TestSize.Level1)
418 {
419     HilogConfig config;
420     HilogPlugin plugin;
421     HilogLine info;
422     const char* data = "08-30 16:47:16.522 27953 31750 I 00B00/ \n";
423     uint64_t sec = GetSec(plugin, data);
424     uint64_t nsec = 522;
425     uint32_t target[] = {27953, 31750};
426 
427     plugin.SetConfig(config);
428     plugin.ParseLogLineInfo(data, strlen(data), info);
429     EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
430     EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
431     EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
432     EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
433     EXPECT_EQ(info.mutable_detail()->level(), 'I');
434     EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "");
435     EXPECT_STREQ(info.context().c_str(), "");
436 }
437 
438 /**
439  * @tc.name: hilog plugin
440  * @tc.desc: Test invalid ParseLogLineInfo
441  * @tc.type: FUNC
442  */
443 HWTEST_F(HilogPluginTest, TestInvalidLogLine8, TestSize.Level1)
444 {
445     HilogConfig config;
446     HilogPlugin plugin;
447     HilogLine info;
448     const char* data = "08-30 16:47:16.522 27953 31750 I 00B00/HwMSDPMovementImpl: \n";
449     uint64_t sec = GetSec(plugin, data);
450     uint64_t nsec = 522;
451     uint32_t target[] = {27953, 31750};
452 
453     plugin.SetConfig(config);
454     plugin.ParseLogLineInfo(data, strlen(data), info);
455     EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
456     EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
457     EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
458     EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
459     EXPECT_EQ(info.mutable_detail()->level(), 'I');
460     EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "HwMSDPMovementImpl");
461     EXPECT_STREQ(info.context().c_str(), "");
462 }
463 
464 /**
465  * @tc.name: hilog plugin
466  * @tc.desc: Test invalid ParseLogLineInfo
467  * @tc.type: FUNC
468  */
469 HWTEST_F(HilogPluginTest, TestInvalidLogLine9, TestSize.Level1)
470 {
471     HilogConfig config;
472     HilogPlugin plugin;
473     HilogLine info;
474     const char* data = "08-30 16:47:16.522 27953 31750 I chatty  :\n";
475     uint64_t sec = GetSec(plugin, data);
476     uint64_t nsec = 522;
477     uint32_t target[] = {27953, 31750};
478 
479     plugin.SetConfig(config);
480     plugin.ParseLogLineInfo(data, strlen(data), info);
481     EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
482     EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
483     EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
484     EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
485     EXPECT_EQ(info.mutable_detail()->level(), 'I');
486     EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "chatty  ");
487     EXPECT_STREQ(info.context().c_str(), "");
488 }
489 
490 /**
491  * @tc.name: hilog plugin
492  * @tc.desc: Test invalid ParseLogLineInfo
493  * @tc.type: FUNC
494  */
495 HWTEST_F(HilogPluginTest, TestInvalidLogLine, TestSize.Level1)
496 {
497     HilogConfig config;
498     HilogPlugin plugin;
499     HilogLine info;
500     const char* data = "08-30 16:47:16.522 27953 31750 I 00B00/HwMSDPMovementImpl: mSupportedModule= 0\n";
501     uint64_t sec = GetSec(plugin, data);
502     uint64_t nsec = 522;
503     uint32_t target[] = {27953, 31750};
504 
505     plugin.SetConfig(config);
506     plugin.ParseLogLineInfo(data, strlen(data), info);
507     EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
508     EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
509     EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
510     EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
511     EXPECT_EQ(info.mutable_detail()->level(), 'I');
512     EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "HwMSDPMovementImpl");
513     EXPECT_STREQ(info.context().c_str(), "mSupportedModule= 0");
514 }
515 
516 /**
517  * @tc.name: hilog plugin
518  * @tc.desc: Test ParseLogLineInfo
519  * @tc.type: FUNC
520  */
521 HWTEST_F(HilogPluginTest, TestParseLogLine1, TestSize.Level1)
522 {
523     HilogConfig config;
524     HilogPlugin plugin;
525     HilogLine info;
526     const char* data = "08-30 16:47:16.149566200 27953 31750 I 00B00/HwMSDPMovementImpl: mSupportedModule= 0\n";
527     uint64_t sec = GetSec(plugin, data);
528     uint64_t nsec = 149566200;
529     uint32_t target[] = {27953, 31750};
530 
531     plugin.SetConfig(config);
532     plugin.ParseLogLineInfo(data, strlen(data), info);
533     EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
534     EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
535     EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
536     EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
537     EXPECT_EQ(info.mutable_detail()->level(), 'I');
538     EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "HwMSDPMovementImpl");
539     EXPECT_STREQ(info.context().c_str(), "mSupportedModule= 0");
540 }
541 
542 /**
543  * @tc.name: hilog plugin
544  * @tc.desc: Test ParseLogLineInfo
545  * @tc.type: FUNC
546  */
547 HWTEST_F(HilogPluginTest, TestParseLogLine2, TestSize.Level1)
548 {
549     HilogConfig config;
550     HilogPlugin plugin;
551     HilogLine info;
552     const char* data =
553         "08-30 16:47:16.149566200 27953 31750 E chatty  : uid=10194(com.zh.heaptest) identical 2 lines\n";
554     uint64_t sec = GetSec(plugin, data);
555     uint64_t nsec = 149566200;
556     uint32_t target[] = {27953, 31750};
557 
558     plugin.SetConfig(config);
559     plugin.ParseLogLineInfo(data, strlen(data), info);
560     EXPECT_EQ(info.mutable_detail()->tv_sec(), sec);
561     EXPECT_EQ(info.mutable_detail()->tv_nsec(), nsec);
562     EXPECT_EQ(info.mutable_detail()->pid(), target[0]);
563     EXPECT_EQ(info.mutable_detail()->tid(), target[1]);
564     EXPECT_EQ(info.mutable_detail()->level(), 'E');
565     EXPECT_STREQ(info.mutable_detail()->tag().c_str(), "chatty  ");
566     EXPECT_STREQ(info.context().c_str(), "uid=10194(com.zh.heaptest) identical 2 lines");
567 }
568 
569 /**
570  * @tc.name: hilog plugin
571  * @tc.desc: Test FindFirstNum
572  * @tc.type: FUNC
573  */
574 HWTEST_F(HilogPluginTest, TestFindFirstNum1, TestSize.Level1)
575 {
576     HilogPlugin plugin;
577     char data[] = {'a', 'b', '\0', '0'};
578     char* cptr = data;
579 
580     EXPECT_FALSE(plugin.FindFirstNum(&cptr));
581 }
582 
583 /**
584  * @tc.name: hilog plugin
585  * @tc.desc: Test FindFirstNum
586  * @tc.type: FUNC
587  */
588 HWTEST_F(HilogPluginTest, TestFindFirstNum2, TestSize.Level1)
589 {
590     HilogPlugin plugin;
591     char data[] = {'a', 'b', '1', '\0', '0'};
592     char* cptr = data;
593 
594     EXPECT_TRUE(plugin.FindFirstNum(&cptr));
595     EXPECT_STREQ(cptr, "1");
596 }
597 
598 /**
599  * @tc.name: hilog plugin
600  * @tc.desc: Test FindFirstNum
601  * @tc.type: FUNC
602  */
603 HWTEST_F(HilogPluginTest, TestFindFirstNum3, TestSize.Level1)
604 {
605     HilogPlugin plugin;
606     char data[] = {'a', 'b', '1', 'c', '\n', '\0'};
607     char* cptr = data;
608 
609     EXPECT_TRUE(plugin.FindFirstNum(&cptr));
610     EXPECT_STREQ(cptr, "1c\n");
611 }
612 
613 /**
614  * @tc.name: hilog plugin
615  * @tc.desc: Test FindFirstNum
616  * @tc.type: FUNC
617  */
618 HWTEST_F(HilogPluginTest, TestFindFirstNum4, TestSize.Level1)
619 {
620     HilogPlugin plugin;
621     char data[] = {'a', 'b', '1', 'c', '\0', '0'};
622     char* cptr = data;
623 
624     EXPECT_TRUE(plugin.FindFirstNum(&cptr));
625     EXPECT_STREQ(cptr, "1c");
626 }
627 
628 /**
629  * @tc.name: hilog plugin
630  * @tc.desc: Test FindFirstNum
631  * @tc.type: FUNC
632  */
633 HWTEST_F(HilogPluginTest, TestFindFirstNum5, TestSize.Level1)
634 {
635     HilogPlugin plugin;
636     char data[] = {'a', 'b', '\n', '0'};
637     char* cptr = data;
638 
639     EXPECT_FALSE(plugin.FindFirstNum(&cptr));
640 }
641 
642 /**
643  * @tc.name: hilog plugin
644  * @tc.desc: Test FindFirstSpace
645  * @tc.type: FUNC
646  */
647 HWTEST_F(HilogPluginTest, TestFindFirstSpace1, TestSize.Level1)
648 {
649     HilogPlugin plugin;
650     char data[] = {'a', 'b', '1', '\0', ' '};
651     char* cptr = data;
652 
653     EXPECT_FALSE(plugin.FindFirstSpace(&cptr));
654 }
655 
656 /**
657  * @tc.name: hilog plugin
658  * @tc.desc: Test FindFirstSpace
659  * @tc.type: FUNC
660  */
661 HWTEST_F(HilogPluginTest, TestFindFirstSpace2, TestSize.Level1)
662 {
663     HilogPlugin plugin;
664     char data[] = {'a', 'b', ' ', '\0', '0'};
665     char* cptr = data;
666 
667     EXPECT_TRUE(plugin.FindFirstSpace(&cptr));
668     EXPECT_STREQ(cptr, " ");
669 }
670 
671 /**
672  * @tc.name: hilog plugin
673  * @tc.desc: Test FindFirstSpace
674  * @tc.type: FUNC
675  */
676 HWTEST_F(HilogPluginTest, TestFindFirstSpace3, TestSize.Level1)
677 {
678     HilogPlugin plugin;
679     char data[] = {'\n', 'a', ' ', '\0', '0'};
680     char* cptr = data;
681 
682     EXPECT_FALSE(plugin.FindFirstSpace(&cptr));
683 }
684 
685 /**
686  * @tc.name: hilog plugin
687  * @tc.desc: Test FindFirstSpace
688  * @tc.type: FUNC
689  */
690 HWTEST_F(HilogPluginTest, TestFindFirstSpace4, TestSize.Level1)
691 {
692     HilogPlugin plugin;
693     char data[] = {'a', 'b', ' ', ' ', '\0', '0'};
694     char* cptr = data;
695 
696     EXPECT_TRUE(plugin.FindFirstSpace(&cptr));
697     EXPECT_STREQ(cptr, "  ");
698 }
699 
700 /**
701  * @tc.name: hilog plugin
702  * @tc.desc: Test FindFirstSpace
703  * @tc.type: FUNC
704  */
705 HWTEST_F(HilogPluginTest, TestFindFirstSpace5, TestSize.Level1)
706 {
707     HilogPlugin plugin;
708     char data[] = {'.', 'b', ' ', ' ', 'c', '\0', '0'};
709     char* cptr = data;
710 
711     EXPECT_TRUE(plugin.FindFirstSpace(&cptr));
712     EXPECT_STREQ(cptr, "  c");
713 }
714 
715 /**
716  * @tc.name: hilog plugin
717  * @tc.desc: Test RemoveSpaces
718  * @tc.type: FUNC
719  */
720 HWTEST_F(HilogPluginTest, TestRemoveSpaces1, TestSize.Level1)
721 {
722     HilogPlugin plugin;
723     char data[] = {' ', '\0', 'a'};
724     char* cptr = data;
725 
726     EXPECT_FALSE(plugin.RemoveSpaces(&cptr));
727 }
728 
729 /**
730  * @tc.name: hilog plugin
731  * @tc.desc: Test RemoveSpaces
732  * @tc.type: FUNC
733  */
734 HWTEST_F(HilogPluginTest, TestRemoveSpaces2, TestSize.Level1)
735 {
736     HilogPlugin plugin;
737     char data[] = {' ', ' ', 'a', '\0'};
738     char* cptr = data;
739 
740     EXPECT_TRUE(plugin.RemoveSpaces(&cptr));
741     EXPECT_STREQ(cptr, "a");
742 }
743 
744 /**
745  * @tc.name: hilog plugin
746  * @tc.desc: Test RemoveSpaces
747  * @tc.type: FUNC
748  */
749 HWTEST_F(HilogPluginTest, TestRemoveSpaces3, TestSize.Level1)
750 {
751     HilogPlugin plugin;
752     char data[] = {' ', 'a', 'b', '\0'};
753     char* cptr = data;
754 
755     EXPECT_TRUE(plugin.RemoveSpaces(&cptr));
756     EXPECT_STREQ(cptr, "ab");
757 }
758 
759 /**
760  * @tc.name: hilog plugin
761  * @tc.desc: Test RemoveSpaces
762  * @tc.type: FUNC
763  */
764 HWTEST_F(HilogPluginTest, TestRemoveSpaces4, TestSize.Level1)
765 {
766     HilogPlugin plugin;
767     char data[] = {'\n', ' ', '\0'};
768     char* cptr = data;
769 
770     EXPECT_FALSE(plugin.RemoveSpaces(&cptr));
771 }
772 
773 /**
774  * @tc.name: hilog plugin
775  * @tc.desc: Test RemoveSpaces
776  * @tc.type: FUNC
777  */
778 HWTEST_F(HilogPluginTest, TestRemoveSpaces5, TestSize.Level1)
779 {
780     HilogPlugin plugin;
781     char data[] = {'a', 'b', ' ', 'c', 'd', '\0'};
782     char* cptr = data;
783 
784     EXPECT_TRUE(plugin.RemoveSpaces(&cptr));
785     EXPECT_STREQ(cptr, "ab cd");
786 }
787 
788 /**
789  * @tc.name: hilog plugin
790  * @tc.desc: Test write
791  * @tc.type: FUNC
792  */
793 HWTEST_F(HilogPluginTest, TestFileOperation, TestSize.Level1)
794 {
795     char buff[FILE_NAME_LEN] = {0};
796     std::string testPath = "/data/local/tmp/uttestdir/";
797     std::string cmd = std::string("mkdir ") + testPath;
798     system(cmd.c_str());
799     FileCache file(testPath);
800     char writeByte[] = "1234";
801     int32_t writeLen = static_cast<int32_t>(strlen(writeByte));
802 
803     ASSERT_TRUE(file.Open("test.txt"));
804     ASSERT_EQ(file.Write(writeByte, writeLen), writeLen);
805     ASSERT_GT(FILE_NAME_LEN, writeLen);
806     ASSERT_EQ(file.Read(buff), writeLen);
807     ASSERT_TRUE(file.Close());
808     cmd = std::string("rm -rf ") + testPath;
809     system(cmd.c_str());
810 }
811 
812 /**
813  * @tc.name: hilog plugin
814  * @tc.desc: start fail test
815  * @tc.type: FUNC
816  */
817 HWTEST_F(HilogPluginTest, TestStartFail, TestSize.Level1)
818 {
819     HilogConfig config;
820     HilogPlugin plugin;
821     WriterStruct writer = {WriteFunc, FlushFunc};
822 
823     g_testId = 1;
824     g_proto.erase(g_proto.begin(), g_proto.end());
825     // set config
826     config.set_need_record(true);
827 
828     // test plugin process
829     plugin.SetWriter(&writer);
830 
831     // serialize
832     int size = config.ByteSizeLong();
833     ASSERT_GT(size, 0);
834     std::vector<uint8_t> configData(size);
835     ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0);
836 
837     // start
838     EXPECT_NE(plugin.Start(configData.data(), size - 1), 0);
839 }
840 
841 /**
842  * @tc.name: hilog plugin
843  * @tc.desc: Framework test
844  * @tc.type: FUNC
845  */
846 HWTEST_F(HilogPluginTest, TestFramework, TestSize.Level1)
847 {
848     std::string path = DEFAULT_TEST_PATH + std::string("libhilogplugin.z.so");
849     void* handle = dlopen(path.c_str(), RTLD_LAZY);
850     EXPECT_NE(handle, nullptr);
851     PluginModuleStruct* plugin = reinterpret_cast<PluginModuleStruct*>(dlsym(handle, "g_pluginModule"));
852     EXPECT_NE(plugin, nullptr);
853     EXPECT_STREQ(plugin->name, "hilog-plugin");
854 
855     g_testId = 1;
856     g_proto.erase(g_proto.begin(), g_proto.end());
857 
858     // set config
859     HilogConfig config;
860     config.set_need_record(true);
861     int size = config.ByteSizeLong();
862     ASSERT_GT(size, 0);
863     std::vector<uint8_t> configData(size);
864     ASSERT_GT(config.SerializeToArray(configData.data(), configData.size()), 0);
865 
866     // test framework process
867     WriterStruct writer = {WriteFunc, FlushFunc};
868     std::vector<uint8_t> dataBuffer(plugin->resultBufferSizeHint);
869     EXPECT_EQ(plugin->callbacks->onRegisterWriterStruct(&writer), 0);
870     EXPECT_EQ(plugin->callbacks->onPluginSessionStart(configData.data(), configData.size()), 0);
871     usleep(US_PER_S * DEFAULT_WAIT); // 10s
872     EXPECT_EQ(plugin->callbacks->onPluginSessionStop(), 0);
873 
874     // test proto data
875     int protoSize = g_proto.size();
876     ASSERT_GT(protoSize, 0);
877     g_testId = 1;
878     for (int i = 0; i < protoSize; i++) {
879         HilogInfo info = g_proto[i];
880         for (int j = 0; j < info.info_size(); j++) {
881             EXPECT_EQ(info.info(j).id(), g_testId);
882             g_testId++;
883         }
884     }
885 }
886 
887 /**
888  * @tc.name: hilog plugin
889  * @tc.desc: Test default cmd
890  * @tc.type: FUNC
891  */
892 HWTEST_F(HilogPluginTest, TestDefaultCmd, TestSize.Level1)
893 {
894     HilogConfig config;
895     HilogPlugin plugin;
896     WriterStruct writer = {WriteFunc, FlushFunc};
897 
898     g_testId = 1;
899     g_proto.erase(g_proto.begin(), g_proto.end());
900 
901     // test plugin process
902     plugin.SetWriter(&writer);
903     EXPECT_TRUE(PluginStart(plugin, config));
904     usleep(US_PER_S * DEFAULT_WAIT); // 10s
905     EXPECT_EQ(plugin.Stop(), 0);
906 
907     // test proto data
908     int size = g_proto.size();
909     ASSERT_GT(size, 0);
910     for (int i = 0; i < size; i++) {
911         HilogInfo info = g_proto[i];
912         for (int j = 0; j < info.info_size(); j++) {
913             EXPECT_EQ(info.info(j).id(), g_testId);
914             g_testId++;
915         }
916     }
917 }
918 
919 /**
920  * @tc.name: hilog plugin
921  * @tc.desc: Test cmd
922  * @tc.type: FUNC
923  */
924 HWTEST_F(HilogPluginTest, TestFullCmd, TestSize.Level1)
925 {
926     std::string oldFile = "";
927     if (RecordFileExist(oldFile)) {
928         std::string cmd = std::string("rm ") + oldFile;
929         system(cmd.c_str());
930     }
931 
932     HilogConfig config;
933     HilogPlugin plugin;
934     WriterStruct writer = {WriteFunc, FlushFunc};
935 
936     g_testId = 1;
937     g_proto.erase(g_proto.begin(), g_proto.end());
938     // set config
939     config.set_need_record(true);
940 
941     // test plugin process
942     plugin.SetWriter(&writer);
943     EXPECT_TRUE(PluginStart(plugin, config));
944     usleep(US_PER_S * DEFAULT_WAIT); // 10s
945 
946     // test proto data
947     int size = g_proto.size();
948     ASSERT_GT(size, 0);
949     for (int i = 0; i < size; i++) {
950         HilogInfo info = g_proto[i];
951         for (int j = 0; j < info.info_size(); j++) {
952             EXPECT_EQ(info.info(j).id(), g_testId);
953             g_testId++;
954         }
955     }
956     // test record file
957     std::string file;
958     ASSERT_TRUE(RecordFileExist(file));
959     file = std::string("rm ") + file;
960     system(file.c_str());
961     EXPECT_EQ(plugin.Stop(), 0);
962 }
963 } // namespace
964