1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2022. 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 <cstdio>
17 #include <string>
18
19 #include <gtest/gtest.h>
20
21 #include "hhlog.h"
22
23 using namespace testing::ext;
24 using namespace std;
25 namespace OHOS {
26 namespace Developtools {
27 namespace Hiebpf {
28 class HHLoggerTest : public ::testing::Test {
29 public:
SetUpTestCase()30 static void SetUpTestCase() {};
TearDownTestCase()31 static void TearDownTestCase() {};
32
SetUp()33 void SetUp() {}
TearDown()34 void TearDown() {}
35 };
36
37 /**
38 * @tc.name: GetLogFileName
39 * @tc.desc: Test HHLogger GetLogFileName
40 * @tc.type: FUNC
41 */
HWTEST_F(HHLoggerTest, GetLogFileName, TestSize.Level1)42 HWTEST_F(HHLoggerTest, GetLogFileName, TestSize.Level1)
43 {
44 std::string fileName = HHLogger::GetInstance().GetLogFileName();
45 EXPECT_STRNE(fileName.c_str(), "");
46 }
47
48 /**
49 * @tc.name: GetFormatTime
50 * @tc.desc: Test HHLogger GetFormatTime
51 * @tc.type: FUNC
52 */
HWTEST_F(HHLoggerTest, GetFormatTime, TestSize.Level1)53 HWTEST_F(HHLoggerTest, GetFormatTime, TestSize.Level1)
54 {
55 std::string formatTime = HHLogger::GetInstance().GetFormatTime();
56 EXPECT_STRNE(formatTime.c_str(), "");
57 }
58
59 /**
60 * @tc.name: UpdateTimer
61 * @tc.desc: Test HHLogger UpdateTimer
62 * @tc.type: FUNC
63 */
HWTEST_F(HHLoggerTest, UpdateTimer, TestSize.Level1)64 HWTEST_F(HHLoggerTest, UpdateTimer, TestSize.Level1)
65 {
66 int ret = HHLogger::GetInstance().UpdateTimer();
67 EXPECT_EQ(ret, 0);
68 }
69
70 /**
71 * @tc.name: InitLogger
72 * @tc.desc: Test HHLogger InitLogger
73 * @tc.type: FUNC
74 */
HWTEST_F(HHLoggerTest, InitLogger, TestSize.Level1)75 HWTEST_F(HHLoggerTest, InitLogger, TestSize.Level1)
76 {
77 auto ret = HHLogger::GetInstance().InitLogger(HHLOG_DEBUG, "");
78 EXPECT_EQ(ret, -1);
79
80 ret = HHLogger::GetInstance().InitLogger(HHLOG_DEBUG, "stdout");
81 EXPECT_EQ(ret, 0);
82
83 std::string fileName = "/data/local/tmp/libebpf.log";
84 ret = HHLogger::GetInstance().InitLogger(HHLOG_DEBUG, fileName);
85 ASSERT_EQ(ret, 0);
86 std::string cmd = "rm " + fileName;
87 system(cmd.c_str());
88 }
89
90 /**
91 * @tc.name: Start
92 * @tc.desc: Test HHLogger Start
93 * @tc.type: FUNC
94 */
HWTEST_F(HHLoggerTest, Start, TestSize.Level1)95 HWTEST_F(HHLoggerTest, Start, TestSize.Level1)
96 {
97 HHLogger logger;
98 auto ret = logger.Start(HHLOG_NONE);
99 EXPECT_EQ(ret, 0);
100 ret = logger.GetLogLevel();
101 EXPECT_EQ(ret, HHLOG_NONE);
102
103 ret = logger.Start(HHLOG_DEBUG, "");
104 EXPECT_EQ(ret, -1);
105
106 ret = logger.Start(HHLOG_DEBUG, "stdout");
107 EXPECT_EQ(ret, 0);
108 HHLOGD(true, HHLOG_DEBUG, "this is test");
109 sleep(1);
110
111 ret = logger.GetLogLevel();
112 EXPECT_EQ(ret, 0);
113 auto isStop = logger.IsStopped();
114 EXPECT_FALSE(isStop);
115 }
116 } // namespace Hiebpf
117 } // namespace Developtools
118 } // namespace OHOS
119