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#include <string>
16#include <thread>
17
18#include <gtest/gtest.h>
19
20#include "bpf_controller.h"
21
22using namespace testing::ext;
23
24namespace {
25constexpr int ROUND_COUNT = 1000;
26constexpr int BUF_SIZE = 512;
27const std::string FILE_NAME = "/data/local/tmp/hiebpf.txt";
28const std::string HIEBPF_FILE_NAME = "/data/local/tmp/hiebpf.data";
29constexpr int FILE_MODE = 0644;
30} // namespace
31
32namespace OHOS {
33namespace Developtools {
34namespace Hiebpf {
35class BpfControllerTest : public ::testing::Test {
36public:
37    static void SetUpTestCase() {};
38    static void TearDownTestCase()
39    {
40        if (access(FILE_NAME.c_str(), F_OK) == 0) {
41            std::string cmd = "rm " + FILE_NAME;
42            system(cmd.c_str());
43        }
44        if (access(HIEBPF_FILE_NAME.c_str(), F_OK) == 0) {
45            std::string cmd = "rm " + HIEBPF_FILE_NAME;
46            system(cmd.c_str());
47        }
48    }
49
50    void SetUp() {}
51    void TearDown() {}
52};
53
54/**
55 * @tc.name: Normal
56 * @tc.desc: Test framework
57 * @tc.type: FUNC
58 */
59HWTEST_F(BpfControllerTest, Normal, TestSize.Level1)
60{
61    BPFConfig cfg;
62    cfg.selectEventGroups_.insert(HiebpfEventGroup::FS_GROUP_ALL);
63    cfg.selectEventGroups_.insert(HiebpfEventGroup::MEM_GROUP_ALL);
64    cfg.selectEventGroups_.insert(HiebpfEventGroup::BIO_GROUP_ALL);
65    cfg.LIBBPFLogLevel_ = LIBBPF_NONE;
66    cfg.BPFLogLevel_ = BPF_LOG_NONE;
67    const uint32_t duration = 10;
68    cfg.traceDuration_ = duration;
69    std::unique_ptr<BPFController> pCtx = BPFController::MakeUnique(cfg);
70    ASSERT_TRUE(pCtx != nullptr);
71    std::thread threadContol([&pCtx]() {
72        ASSERT_EQ(pCtx->Start(), 0);
73    });
74    sleep(1);
75
76    // create fs/bio data
77    int fd = open(FILE_NAME.c_str(), O_RDWR | O_CREAT, FILE_MODE);
78    ASSERT_GT(fd, 0);
79    char buf[BUF_SIZE] = {0};
80    ASSERT_TRUE(memset_s(buf, sizeof(buf), '1', sizeof(buf)) == EOK);
81    off_t offset = 0;
82    for (int i = 0; i < ROUND_COUNT; i++) {
83        pwrite(fd, buf, sizeof(buf), offset);
84        fsync(fd);
85        pread(fd, buf, sizeof(buf), offset);
86        offset += sizeof(buf);
87    }
88    close(fd);
89
90    // create mem data
91    int num = 1;
92    if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
93        return;
94    }
95    pid_t pid = fork();
96    if (pid == 0) {
97        num++;
98        exit(0);
99    }
100
101    sleep(1);
102    pCtx->Stop();
103    if (threadContol.joinable()) {
104        threadContol.join();
105    }
106}
107} // namespace Hiebpf
108} // namespace Developtools
109} // namespace OHOS
110