106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#include <string>
1606f6ba60Sopenharmony_ci#include <thread>
1706f6ba60Sopenharmony_ci
1806f6ba60Sopenharmony_ci#include <gtest/gtest.h>
1906f6ba60Sopenharmony_ci
2006f6ba60Sopenharmony_ci#include "bpf_controller.h"
2106f6ba60Sopenharmony_ci
2206f6ba60Sopenharmony_ciusing namespace testing::ext;
2306f6ba60Sopenharmony_ci
2406f6ba60Sopenharmony_cinamespace {
2506f6ba60Sopenharmony_ciconstexpr int ROUND_COUNT = 1000;
2606f6ba60Sopenharmony_ciconstexpr int BUF_SIZE = 512;
2706f6ba60Sopenharmony_ciconst std::string FILE_NAME = "/data/local/tmp/hiebpf.txt";
2806f6ba60Sopenharmony_ciconst std::string HIEBPF_FILE_NAME = "/data/local/tmp/hiebpf.data";
2906f6ba60Sopenharmony_ciconstexpr int FILE_MODE = 0644;
3006f6ba60Sopenharmony_ci} // namespace
3106f6ba60Sopenharmony_ci
3206f6ba60Sopenharmony_cinamespace OHOS {
3306f6ba60Sopenharmony_cinamespace Developtools {
3406f6ba60Sopenharmony_cinamespace Hiebpf {
3506f6ba60Sopenharmony_ciclass BpfControllerTest : public ::testing::Test {
3606f6ba60Sopenharmony_cipublic:
3706f6ba60Sopenharmony_ci    static void SetUpTestCase() {};
3806f6ba60Sopenharmony_ci    static void TearDownTestCase()
3906f6ba60Sopenharmony_ci    {
4006f6ba60Sopenharmony_ci        if (access(FILE_NAME.c_str(), F_OK) == 0) {
4106f6ba60Sopenharmony_ci            std::string cmd = "rm " + FILE_NAME;
4206f6ba60Sopenharmony_ci            system(cmd.c_str());
4306f6ba60Sopenharmony_ci        }
4406f6ba60Sopenharmony_ci        if (access(HIEBPF_FILE_NAME.c_str(), F_OK) == 0) {
4506f6ba60Sopenharmony_ci            std::string cmd = "rm " + HIEBPF_FILE_NAME;
4606f6ba60Sopenharmony_ci            system(cmd.c_str());
4706f6ba60Sopenharmony_ci        }
4806f6ba60Sopenharmony_ci    }
4906f6ba60Sopenharmony_ci
5006f6ba60Sopenharmony_ci    void SetUp() {}
5106f6ba60Sopenharmony_ci    void TearDown() {}
5206f6ba60Sopenharmony_ci};
5306f6ba60Sopenharmony_ci
5406f6ba60Sopenharmony_ci/**
5506f6ba60Sopenharmony_ci * @tc.name: Normal
5606f6ba60Sopenharmony_ci * @tc.desc: Test framework
5706f6ba60Sopenharmony_ci * @tc.type: FUNC
5806f6ba60Sopenharmony_ci */
5906f6ba60Sopenharmony_ciHWTEST_F(BpfControllerTest, Normal, TestSize.Level1)
6006f6ba60Sopenharmony_ci{
6106f6ba60Sopenharmony_ci    BPFConfig cfg;
6206f6ba60Sopenharmony_ci    cfg.selectEventGroups_.insert(HiebpfEventGroup::FS_GROUP_ALL);
6306f6ba60Sopenharmony_ci    cfg.selectEventGroups_.insert(HiebpfEventGroup::MEM_GROUP_ALL);
6406f6ba60Sopenharmony_ci    cfg.selectEventGroups_.insert(HiebpfEventGroup::BIO_GROUP_ALL);
6506f6ba60Sopenharmony_ci    cfg.LIBBPFLogLevel_ = LIBBPF_NONE;
6606f6ba60Sopenharmony_ci    cfg.BPFLogLevel_ = BPF_LOG_NONE;
6706f6ba60Sopenharmony_ci    const uint32_t duration = 10;
6806f6ba60Sopenharmony_ci    cfg.traceDuration_ = duration;
6906f6ba60Sopenharmony_ci    std::unique_ptr<BPFController> pCtx = BPFController::MakeUnique(cfg);
7006f6ba60Sopenharmony_ci    ASSERT_TRUE(pCtx != nullptr);
7106f6ba60Sopenharmony_ci    std::thread threadContol([&pCtx]() {
7206f6ba60Sopenharmony_ci        ASSERT_EQ(pCtx->Start(), 0);
7306f6ba60Sopenharmony_ci    });
7406f6ba60Sopenharmony_ci    sleep(1);
7506f6ba60Sopenharmony_ci
7606f6ba60Sopenharmony_ci    // create fs/bio data
7706f6ba60Sopenharmony_ci    int fd = open(FILE_NAME.c_str(), O_RDWR | O_CREAT, FILE_MODE);
7806f6ba60Sopenharmony_ci    ASSERT_GT(fd, 0);
7906f6ba60Sopenharmony_ci    char buf[BUF_SIZE] = {0};
8006f6ba60Sopenharmony_ci    ASSERT_TRUE(memset_s(buf, sizeof(buf), '1', sizeof(buf)) == EOK);
8106f6ba60Sopenharmony_ci    off_t offset = 0;
8206f6ba60Sopenharmony_ci    for (int i = 0; i < ROUND_COUNT; i++) {
8306f6ba60Sopenharmony_ci        pwrite(fd, buf, sizeof(buf), offset);
8406f6ba60Sopenharmony_ci        fsync(fd);
8506f6ba60Sopenharmony_ci        pread(fd, buf, sizeof(buf), offset);
8606f6ba60Sopenharmony_ci        offset += sizeof(buf);
8706f6ba60Sopenharmony_ci    }
8806f6ba60Sopenharmony_ci    close(fd);
8906f6ba60Sopenharmony_ci
9006f6ba60Sopenharmony_ci    // create mem data
9106f6ba60Sopenharmony_ci    int num = 1;
9206f6ba60Sopenharmony_ci    if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
9306f6ba60Sopenharmony_ci        return;
9406f6ba60Sopenharmony_ci    }
9506f6ba60Sopenharmony_ci    pid_t pid = fork();
9606f6ba60Sopenharmony_ci    if (pid == 0) {
9706f6ba60Sopenharmony_ci        num++;
9806f6ba60Sopenharmony_ci        exit(0);
9906f6ba60Sopenharmony_ci    }
10006f6ba60Sopenharmony_ci
10106f6ba60Sopenharmony_ci    sleep(1);
10206f6ba60Sopenharmony_ci    pCtx->Stop();
10306f6ba60Sopenharmony_ci    if (threadContol.joinable()) {
10406f6ba60Sopenharmony_ci        threadContol.join();
10506f6ba60Sopenharmony_ci    }
10606f6ba60Sopenharmony_ci}
10706f6ba60Sopenharmony_ci} // namespace Hiebpf
10806f6ba60Sopenharmony_ci} // namespace Developtools
10906f6ba60Sopenharmony_ci} // namespace OHOS
110