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 "libbpf_logger.h"
22 
23 using namespace testing::ext;
24 using namespace std;
25 namespace OHOS {
26 namespace Developtools {
27 namespace Hiebpf {
28 class LIBBPFLoggerTest : 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: MakeUnique
39  * @tc.desc: Test LIBBPFLogger MakeUnique
40  * @tc.type: FUNC
41  */
HWTEST_F(LIBBPFLoggerTest, MakeUnique, TestSize.Level1)42 HWTEST_F(LIBBPFLoggerTest, MakeUnique, TestSize.Level1)
43 {
44     auto logger = LIBBPFLogger::MakeUnique("", -1);
45     EXPECT_TRUE(logger == nullptr);
46 
47     logger = LIBBPFLogger::MakeUnique("stdout", 0);
48     EXPECT_TRUE(logger != nullptr);
49 }
50 
51 /**
52  * @tc.name: Printf
53  * @tc.desc: Test LIBBPFLogger Printf
54  * @tc.type: FUNC
55  */
HWTEST_F(LIBBPFLoggerTest, Printf, TestSize.Level1)56 HWTEST_F(LIBBPFLoggerTest, Printf, TestSize.Level1)
57 {
58     LIBBPFLogger logger(0);
59     va_list args;
60     auto ret = logger.Printf(1, nullptr, args);
61     EXPECT_EQ(ret, 0);
62 
63     char fmt[] = "text";
64     ret = logger.Printf(0, fmt, args);
65     EXPECT_EQ(ret, -1);
66 
67     ret = logger.OpenLogFile("stdout");
68     EXPECT_EQ(ret, 0);
69     ret = logger.Printf(0, fmt, args);
70     EXPECT_GE(ret, 0);
71 }
72 
73 /**
74  * @tc.name: OpenLogFile
75  * @tc.desc: Test LIBBPFLogger OpenLogFile
76  * @tc.type: FUNC
77  */
HWTEST_F(LIBBPFLoggerTest, OpenLogFile, TestSize.Level1)78 HWTEST_F(LIBBPFLoggerTest, OpenLogFile, TestSize.Level1)
79 {
80     LIBBPFLogger logger(0);
81     auto ret = logger.OpenLogFile("");
82     EXPECT_EQ(ret, -1);
83 
84     ret = logger.OpenLogFile("stdout");
85     EXPECT_EQ(ret, 0);
86 
87     std::string fileName = "/data/local/tmp/libebpf.log";
88     ret = logger.OpenLogFile(fileName);
89     ASSERT_EQ(ret, 0);
90     std::string cmd = "rm " + fileName;
91     system(cmd.c_str());
92 }
93 
94 /**
95  * @tc.name: GetLogFileName
96  * @tc.desc: Test LIBBPFLogger GetLogFileName
97  * @tc.type: FUNC
98  */
HWTEST_F(LIBBPFLoggerTest, GetLogFileName, TestSize.Level1)99 HWTEST_F(LIBBPFLoggerTest, GetLogFileName, TestSize.Level1)
100 {
101     LIBBPFLogger logger(0);
102     std::string fileName = logger.GetLogFileName();
103     EXPECT_STRNE(fileName.c_str(), "");
104 }
105 } // namespace Hiebpf
106 } // namespace Developtools
107 } // namespace OHOS
108