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 <cstring>
1606f6ba60Sopenharmony_ci
1706f6ba60Sopenharmony_ci#include <gtest/gtest.h>
1806f6ba60Sopenharmony_ci
1906f6ba60Sopenharmony_ci#include "maps_info.h"
2006f6ba60Sopenharmony_ci
2106f6ba60Sopenharmony_ciusing namespace testing::ext;
2206f6ba60Sopenharmony_ci
2306f6ba60Sopenharmony_cinamespace OHOS {
2406f6ba60Sopenharmony_cinamespace Developtools {
2506f6ba60Sopenharmony_cinamespace Hiebpf {
2606f6ba60Sopenharmony_ciclass MapsInofTest : public ::testing::Test {
2706f6ba60Sopenharmony_cipublic:
2806f6ba60Sopenharmony_ci    static void SetUpTestCase() {};
2906f6ba60Sopenharmony_ci    static void TearDownTestCase() {};
3006f6ba60Sopenharmony_ci
3106f6ba60Sopenharmony_ci    void SetUp() {}
3206f6ba60Sopenharmony_ci    void TearDown() {}
3306f6ba60Sopenharmony_ci};
3406f6ba60Sopenharmony_ci
3506f6ba60Sopenharmony_ci/**
3606f6ba60Sopenharmony_ci * @tc.name: GetMaps
3706f6ba60Sopenharmony_ci * @tc.desc: Test framework
3806f6ba60Sopenharmony_ci * @tc.type: FUNC
3906f6ba60Sopenharmony_ci */
4006f6ba60Sopenharmony_ciHWTEST_F(MapsInofTest, GetMaps, TestSize.Level1)
4106f6ba60Sopenharmony_ci{
4206f6ba60Sopenharmony_ci    MapsInfo mapsInfo;
4306f6ba60Sopenharmony_ci    std::vector<MapsInfo::MapsItem> mapsItems;
4406f6ba60Sopenharmony_ci    mapsInfo.GetMaps(0, mapsItems);
4506f6ba60Sopenharmony_ci    ASSERT_EQ(mapsItems.size(), 0);
4606f6ba60Sopenharmony_ci    mapsInfo.GetMaps(-1, mapsItems);
4706f6ba60Sopenharmony_ci    ASSERT_EQ(mapsItems.size(), 0);
4806f6ba60Sopenharmony_ci
4906f6ba60Sopenharmony_ci    mapsInfo.GetMaps(1, mapsItems);
5006f6ba60Sopenharmony_ci    ASSERT_GE(mapsItems.size(), 1);
5106f6ba60Sopenharmony_ci    ASSERT_EQ(mapsItems[0].pid_, 1);
5206f6ba60Sopenharmony_ci    ASSERT_FALSE(mapsItems[0].fileName_.find("init") == std::string::npos);
5306f6ba60Sopenharmony_ci}
5406f6ba60Sopenharmony_ci
5506f6ba60Sopenharmony_ciHWTEST_F(MapsInofTest, Cache, TestSize.Level1)
5606f6ba60Sopenharmony_ci{
5706f6ba60Sopenharmony_ci    MapsInfo mapsInfo;
5806f6ba60Sopenharmony_ci    ASSERT_FALSE(mapsInfo.IsCached(1));
5906f6ba60Sopenharmony_ci    mapsInfo.CachePid(1);
6006f6ba60Sopenharmony_ci    ASSERT_TRUE(mapsInfo.IsCached(1));
6106f6ba60Sopenharmony_ci    mapsInfo.RemovePid(1);
6206f6ba60Sopenharmony_ci    ASSERT_FALSE(mapsInfo.IsCached(1));
6306f6ba60Sopenharmony_ci}
6406f6ba60Sopenharmony_ci
6506f6ba60Sopenharmony_ciHWTEST_F(MapsInofTest, GetBinary, TestSize.Level1)
6606f6ba60Sopenharmony_ci{
6706f6ba60Sopenharmony_ci    MapsInfo mapsInfo;
6806f6ba60Sopenharmony_ci    MapsInfo::MapsItem mapsItem;
6906f6ba60Sopenharmony_ci    const int testNum = 1000;
7006f6ba60Sopenharmony_ci    mapsItem.start_ = 0;
7106f6ba60Sopenharmony_ci    mapsItem.end_ = testNum;
7206f6ba60Sopenharmony_ci    mapsItem.offset_ = testNum;
7306f6ba60Sopenharmony_ci    mapsItem.pid_ = testNum;
7406f6ba60Sopenharmony_ci    mapsItem.fileName_ = "testGetBinary";
7506f6ba60Sopenharmony_ci
7606f6ba60Sopenharmony_ci    std::vector<uint8_t> buf;
7706f6ba60Sopenharmony_ci    uint32_t size = mapsInfo.GetBinary(mapsItem, buf);
7806f6ba60Sopenharmony_ci    const uint32_t fixLen = sizeof(mapsItem.start_) + sizeof(mapsItem.end_) +
7906f6ba60Sopenharmony_ci                      sizeof(mapsItem.offset_) + sizeof(mapsItem.pid_) + sizeof(uint32_t);
8006f6ba60Sopenharmony_ci    ASSERT_EQ(size, fixLen + mapsItem.fileName_.size() + 1);
8106f6ba60Sopenharmony_ci
8206f6ba60Sopenharmony_ci    uint8_t *p = buf.data();
8306f6ba60Sopenharmony_ci    uint64_t *start = (uint64_t *)p;
8406f6ba60Sopenharmony_ci    ASSERT_EQ(mapsItem.start_, *start);
8506f6ba60Sopenharmony_ci    p += sizeof(mapsItem.start_);
8606f6ba60Sopenharmony_ci
8706f6ba60Sopenharmony_ci    uint64_t *end = (uint64_t *)p;
8806f6ba60Sopenharmony_ci    ASSERT_EQ(mapsItem.end_, *end);
8906f6ba60Sopenharmony_ci    p += sizeof(mapsItem.end_);
9006f6ba60Sopenharmony_ci
9106f6ba60Sopenharmony_ci    uint32_t *offset = (uint32_t *)p;
9206f6ba60Sopenharmony_ci    ASSERT_EQ(mapsItem.offset_, *offset);
9306f6ba60Sopenharmony_ci    p += sizeof(mapsItem.offset_);
9406f6ba60Sopenharmony_ci
9506f6ba60Sopenharmony_ci    uint32_t *pid = (uint32_t *)p;
9606f6ba60Sopenharmony_ci    ASSERT_EQ(mapsItem.pid_, *pid);
9706f6ba60Sopenharmony_ci    p += sizeof(mapsItem.pid_);
9806f6ba60Sopenharmony_ci
9906f6ba60Sopenharmony_ci    uint32_t *fileNameLen = (uint32_t *)p;
10006f6ba60Sopenharmony_ci    ASSERT_EQ((uint32_t)(mapsItem.fileName_.size() + 1), *fileNameLen);
10106f6ba60Sopenharmony_ci    p += sizeof(uint32_t);
10206f6ba60Sopenharmony_ci
10306f6ba60Sopenharmony_ci    std::string fileName = (char *)p;
10406f6ba60Sopenharmony_ci    ASSERT_EQ(fileName, mapsItem.fileName_);
10506f6ba60Sopenharmony_ci}
10606f6ba60Sopenharmony_ci} // namespace Hiebpf
10706f6ba60Sopenharmony_ci} // namespace Developtools
10806f6ba60Sopenharmony_ci} // namespace OHOS
109