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 "bpf_log_reader.h"
22 
23 using namespace testing::ext;
24 using namespace std;
25 namespace {
26 const std::string FILE_NAME = "/data/local/tmp/libebpf.log";
27 } // namespace
28 
29 namespace OHOS {
30 namespace Developtools {
31 namespace Hiebpf {
32 class BPFLogReaderTest : public ::testing::Test {
33 public:
SetUpTestCase()34     static void SetUpTestCase() {};
TearDownTestCase()35     static void TearDownTestCase()
36     {
37         if (access(FILE_NAME.c_str(), F_OK) == 0) {
38             std::string cmd = "rm " + FILE_NAME;
39             system(cmd.c_str());
40         }
41     }
42 
SetUp()43     void SetUp() {}
TearDown()44     void TearDown() {}
45 };
46 
47 /**
48  * @tc.name: MakeUnique
49  * @tc.desc: Test BPFLogReader MakeUnique
50  * @tc.type: FUNC
51  */
HWTEST_F(BPFLogReaderTest, MakeUnique, TestSize.Level1)52 HWTEST_F(BPFLogReaderTest, MakeUnique, TestSize.Level1)
53 {
54     auto logger = BPFLogReader::MakeUnique("");
55     EXPECT_TRUE(logger == nullptr);
56 
57     logger = BPFLogReader::MakeUnique("stdout");
58     EXPECT_TRUE(logger != nullptr);
59 
60     logger = BPFLogReader::MakeUnique(FILE_NAME);
61     EXPECT_TRUE(logger != nullptr);
62 }
63 
64 /**
65  * @tc.name: OpenLogFile
66  * @tc.desc: Test BPFLogReader OpenLogFile
67  * @tc.type: FUNC
68  */
HWTEST_F(BPFLogReaderTest, OpenLogFile, TestSize.Level1)69 HWTEST_F(BPFLogReaderTest, OpenLogFile, TestSize.Level1)
70 {
71     BPFLogReader logger;
72     auto ret = logger.OpenLogFile("");
73     EXPECT_EQ(ret, -1);
74 
75     ret = logger.OpenLogFile("stdout");
76     EXPECT_EQ(ret, 0);
77 
78     ret = logger.OpenLogFile(FILE_NAME);
79     EXPECT_EQ(ret, 0);
80 }
81 
82 /**
83  * @tc.name: GetLogFileName
84  * @tc.desc: Test BPFLogReader GetLogFileName
85  * @tc.type: FUNC
86  */
HWTEST_F(BPFLogReaderTest, GetLogFileName, TestSize.Level1)87 HWTEST_F(BPFLogReaderTest, GetLogFileName, TestSize.Level1)
88 {
89     BPFLogReader logger;
90     std::string fileName = logger.GetLogFileName();
91     EXPECT_STRNE(fileName.c_str(), "");
92 }
93 
94 /**
95  * @tc.name: Start
96  * @tc.desc: Test BPFLogReader Start
97  * @tc.type: FUNC
98  */
HWTEST_F(BPFLogReaderTest, Start, TestSize.Level1)99 HWTEST_F(BPFLogReaderTest, Start, TestSize.Level1)
100 {
101     BPFLogReader logger;
102     auto ret = logger.Start();
103     EXPECT_EQ(ret, 0);
104 
105     sleep(1);
106 
107     ret = logger.Stop();
108     EXPECT_EQ(ret, 0);
109 }
110 } // namespace Hiebpf
111 } // namespace Developtools
112 } // namespace OHOS
113