100600bfbSopenharmony_ci/*
200600bfbSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
300600bfbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
400600bfbSopenharmony_ci * you may not use this file except in compliance with the License.
500600bfbSopenharmony_ci * You may obtain a copy of the License at
600600bfbSopenharmony_ci *
700600bfbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
800600bfbSopenharmony_ci *
900600bfbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1000600bfbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1100600bfbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1200600bfbSopenharmony_ci * See the License for the specific language governing permissions and
1300600bfbSopenharmony_ci * limitations under the License.
1400600bfbSopenharmony_ci */
1500600bfbSopenharmony_ci#include <gtest/gtest.h>
1600600bfbSopenharmony_ci#include <map>
1700600bfbSopenharmony_ci#include <unistd.h>
1800600bfbSopenharmony_ci#include <vector>
1900600bfbSopenharmony_ci#include "executor/memory/get_hardware_info.h"
2000600bfbSopenharmony_ci#include "executor/memory/get_process_info.h"
2100600bfbSopenharmony_ci#include "executor/memory/get_kernel_info.h"
2200600bfbSopenharmony_ci#include "executor/memory/memory_info.h"
2300600bfbSopenharmony_ci#include "executor/memory/memory_filter.h"
2400600bfbSopenharmony_ci#include "executor/memory/memory_util.h"
2500600bfbSopenharmony_ci#include "executor/memory/parse/parse_meminfo.h"
2600600bfbSopenharmony_ci#include "executor/memory/parse/parse_smaps_info.h"
2700600bfbSopenharmony_ci#include "executor/memory/parse/parse_smaps_rollup_info.h"
2800600bfbSopenharmony_ci#include "executor/memory/smaps_memory_info.h"
2900600bfbSopenharmony_ci
3000600bfbSopenharmony_ciusing namespace std;
3100600bfbSopenharmony_ciusing namespace testing::ext;
3200600bfbSopenharmony_cinamespace OHOS {
3300600bfbSopenharmony_cinamespace HiviewDFX {
3400600bfbSopenharmony_ciconst int INIT_PID = 1;
3500600bfbSopenharmony_ciconst uint64_t INVALID_PID = 0;
3600600bfbSopenharmony_ciconst int BUFFER_SIZE = 1024;
3700600bfbSopenharmony_cistring NULL_STR = "";
3800600bfbSopenharmony_ci
3900600bfbSopenharmony_ciusing ValueMap = std::map<std::string, uint64_t>;
4000600bfbSopenharmony_ciusing GroupMap = std::map<std::string, ValueMap>;
4100600bfbSopenharmony_ciusing StringMatrix = std::shared_ptr<std::vector<std::vector<std::string>>>;
4200600bfbSopenharmony_ci
4300600bfbSopenharmony_ciclass HidumperMemoryTest : public testing::Test {
4400600bfbSopenharmony_cipublic:
4500600bfbSopenharmony_ci    static void SetUpTestCase(void);
4600600bfbSopenharmony_ci    static void TearDownTestCase(void);
4700600bfbSopenharmony_ci    void SetUp();
4800600bfbSopenharmony_ci    void TearDown();
4900600bfbSopenharmony_ci};
5000600bfbSopenharmony_ci
5100600bfbSopenharmony_civoid HidumperMemoryTest::SetUpTestCase(void)
5200600bfbSopenharmony_ci{
5300600bfbSopenharmony_ci}
5400600bfbSopenharmony_civoid HidumperMemoryTest::TearDownTestCase(void)
5500600bfbSopenharmony_ci{
5600600bfbSopenharmony_ci}
5700600bfbSopenharmony_civoid HidumperMemoryTest::SetUp(void)
5800600bfbSopenharmony_ci{
5900600bfbSopenharmony_ci}
6000600bfbSopenharmony_civoid HidumperMemoryTest::TearDown(void)
6100600bfbSopenharmony_ci{
6200600bfbSopenharmony_ci}
6300600bfbSopenharmony_ci
6400600bfbSopenharmony_ci/**
6500600bfbSopenharmony_ci * @tc.name: MemoryParse001
6600600bfbSopenharmony_ci * @tc.desc: Test ParseMeminfo invalid ret.
6700600bfbSopenharmony_ci * @tc.type: FUNC
6800600bfbSopenharmony_ci */
6900600bfbSopenharmony_ciHWTEST_F(HidumperMemoryTest, MemoryParse001, TestSize.Level1)
7000600bfbSopenharmony_ci{
7100600bfbSopenharmony_ci    unique_ptr<OHOS::HiviewDFX::ParseMeminfo> parseMeminfo = make_unique<OHOS::HiviewDFX::ParseMeminfo>();
7200600bfbSopenharmony_ci    ValueMap result;
7300600bfbSopenharmony_ci    parseMeminfo->SetData("", result);
7400600bfbSopenharmony_ci    ASSERT_EQ(result.size(), 0);
7500600bfbSopenharmony_ci}
7600600bfbSopenharmony_ci
7700600bfbSopenharmony_ci/**
7800600bfbSopenharmony_ci * @tc.name: ParseSmapsInfo001
7900600bfbSopenharmony_ci * @tc.desc: Test parseSmapsInfo invalid ret.
8000600bfbSopenharmony_ci * @tc.type: FUNC
8100600bfbSopenharmony_ci */
8200600bfbSopenharmony_ciHWTEST_F(HidumperMemoryTest, ParseSmapsInfo001, TestSize.Level1)
8300600bfbSopenharmony_ci{
8400600bfbSopenharmony_ci    unique_ptr<OHOS::HiviewDFX::ParseSmapsInfo> parseSmapsInfo = make_unique<OHOS::HiviewDFX::ParseSmapsInfo>();
8500600bfbSopenharmony_ci    uint64_t pid = 0;
8600600bfbSopenharmony_ci    ASSERT_FALSE(parseSmapsInfo->GetHasPidValue("RSS", NULL_STR, pid));
8700600bfbSopenharmony_ci    ASSERT_FALSE(parseSmapsInfo->GetHasPidValue("PSS", NULL_STR, pid));
8800600bfbSopenharmony_ci    ASSERT_FALSE(parseSmapsInfo->GetHasPidValue("Size", NULL_STR, pid));
8900600bfbSopenharmony_ci    ASSERT_FALSE(parseSmapsInfo->GetSmapsValue(MemoryFilter::MemoryType::NOT_SPECIFIED_PID, NULL_STR,
9000600bfbSopenharmony_ci        NULL_STR, pid));
9100600bfbSopenharmony_ci    GroupMap groupMapResult;
9200600bfbSopenharmony_ci    std::vector<std::map<std::string, std::string>> vectMap;
9300600bfbSopenharmony_ci    parseSmapsInfo->memMap_.clear();
9400600bfbSopenharmony_ci    ASSERT_FALSE(parseSmapsInfo->ShowSmapsData(OHOS::HiviewDFX::MemoryFilter::MemoryType::NOT_SPECIFIED_PID,
9500600bfbSopenharmony_ci        0, groupMapResult, false, vectMap));
9600600bfbSopenharmony_ci    ASSERT_TRUE(parseSmapsInfo->ShowSmapsData(OHOS::HiviewDFX::MemoryFilter::MemoryType::NOT_SPECIFIED_PID,
9700600bfbSopenharmony_ci        INIT_PID, groupMapResult, true, vectMap));
9800600bfbSopenharmony_ci}
9900600bfbSopenharmony_ci
10000600bfbSopenharmony_ci/**
10100600bfbSopenharmony_ci * @tc.name: ParseSmapsRollupInfo001
10200600bfbSopenharmony_ci * @tc.desc: Test ParseSmapsRollupInfo invalid ret.
10300600bfbSopenharmony_ci * @tc.type: FUNC
10400600bfbSopenharmony_ci */
10500600bfbSopenharmony_ciHWTEST_F(HidumperMemoryTest, ParseSmapsRollupInfo001, TestSize.Level1)
10600600bfbSopenharmony_ci{
10700600bfbSopenharmony_ci    unique_ptr<OHOS::HiviewDFX::ParseSmapsRollupInfo> parseSmapsRollup =
10800600bfbSopenharmony_ci        make_unique<OHOS::HiviewDFX::ParseSmapsRollupInfo>();
10900600bfbSopenharmony_ci    MemInfoData::MemInfo memInfo;
11000600bfbSopenharmony_ci    parseSmapsRollup->GetValue("RSS", memInfo);
11100600bfbSopenharmony_ci    parseSmapsRollup->GetValue("PSS", memInfo);
11200600bfbSopenharmony_ci    parseSmapsRollup->GetValue("Size", memInfo);
11300600bfbSopenharmony_ci    ASSERT_TRUE(memInfo.rss == 0);
11400600bfbSopenharmony_ci}
11500600bfbSopenharmony_ci
11600600bfbSopenharmony_ci/**
11700600bfbSopenharmony_ci * @tc.name: SmapsMemoryInfo001
11800600bfbSopenharmony_ci * @tc.desc: Test SmapsMemoryInfo ret.
11900600bfbSopenharmony_ci * @tc.type: FUNC
12000600bfbSopenharmony_ci */
12100600bfbSopenharmony_ciHWTEST_F(HidumperMemoryTest, SmapsMemoryInfo001, TestSize.Level1)
12200600bfbSopenharmony_ci{
12300600bfbSopenharmony_ci    shared_ptr<OHOS::HiviewDFX::SmapsMemoryInfo> smapsMemoryInfo =
12400600bfbSopenharmony_ci        make_shared<OHOS::HiviewDFX::SmapsMemoryInfo>();
12500600bfbSopenharmony_ci    shared_ptr<vector<vector<string>>> result = make_shared<vector<vector<string>>>();
12600600bfbSopenharmony_ci    ASSERT_TRUE(smapsMemoryInfo->ShowMemorySmapsByPid(INIT_PID, result, true));
12700600bfbSopenharmony_ci    ASSERT_FALSE(smapsMemoryInfo->ShowMemorySmapsByPid(INVALID_PID, result, true));
12800600bfbSopenharmony_ci}
12900600bfbSopenharmony_ci
13000600bfbSopenharmony_ci/**
13100600bfbSopenharmony_ci * @tc.name: GetHardwareInfo001
13200600bfbSopenharmony_ci * @tc.desc: Test GetHardwareInfo ret.
13300600bfbSopenharmony_ci * @tc.type: FUNC
13400600bfbSopenharmony_ci */
13500600bfbSopenharmony_ciHWTEST_F(HidumperMemoryTest, GetHardwareInfo001, TestSize.Level1)
13600600bfbSopenharmony_ci{
13700600bfbSopenharmony_ci    unique_ptr<OHOS::HiviewDFX::GetHardwareInfo> getHardwareInfo =
13800600bfbSopenharmony_ci        make_unique<OHOS::HiviewDFX::GetHardwareInfo>();
13900600bfbSopenharmony_ci    getHardwareInfo->GetResverRegPath(NULL_STR);
14000600bfbSopenharmony_ci    vector<string> paths;
14100600bfbSopenharmony_ci    uint64_t value = getHardwareInfo->CalcHardware(paths);
14200600bfbSopenharmony_ci    ASSERT_TRUE(value == 0);
14300600bfbSopenharmony_ci    size_t groupSize = 0;
14400600bfbSopenharmony_ci    getHardwareInfo->GetGroupOfPaths(groupSize, groupSize, paths, paths);
14500600bfbSopenharmony_ci    ASSERT_TRUE(paths.size() == 0);
14600600bfbSopenharmony_ci}
14700600bfbSopenharmony_ci
14800600bfbSopenharmony_ci/**
14900600bfbSopenharmony_ci * @tc.name: MemoryInfo001
15000600bfbSopenharmony_ci * @tc.desc: Test MemoryInfo ret.
15100600bfbSopenharmony_ci * @tc.type: FUNC
15200600bfbSopenharmony_ci */
15300600bfbSopenharmony_ciHWTEST_F(HidumperMemoryTest, MemoryInfo001, TestSize.Level1)
15400600bfbSopenharmony_ci{
15500600bfbSopenharmony_ci    unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
15600600bfbSopenharmony_ci        make_unique<OHOS::HiviewDFX::MemoryInfo>();
15700600bfbSopenharmony_ci    int value = static_cast<int>(memoryInfo->GetProcValue(INVALID_PID, NULL_STR));
15800600bfbSopenharmony_ci    ASSERT_EQ(value, 0);
15900600bfbSopenharmony_ci    value = static_cast<int>(memoryInfo->GetVss(INVALID_PID));
16000600bfbSopenharmony_ci    ASSERT_EQ(value, 0);
16100600bfbSopenharmony_ci    MemInfoData::MemUsage usage;
16200600bfbSopenharmony_ci    OHOS::HiviewDFX::DmaInfo dmaInfo;
16300600bfbSopenharmony_ci    ASSERT_FALSE(memoryInfo->GetMemByProcessPid(INVALID_PID, usage));
16400600bfbSopenharmony_ci#ifdef HIDUMPER_MEMMGR_ENABLE
16500600bfbSopenharmony_ci    memoryInfo->GetProcessAdjLabel(INVALID_PID);
16600600bfbSopenharmony_ci#endif
16700600bfbSopenharmony_ci}
16800600bfbSopenharmony_ci
16900600bfbSopenharmony_ci/**
17000600bfbSopenharmony_ci * @tc.name: MemoryInfo002
17100600bfbSopenharmony_ci * @tc.desc: Test MemoryInfo ret.
17200600bfbSopenharmony_ci * @tc.type: FUNC
17300600bfbSopenharmony_ci */
17400600bfbSopenharmony_ciHWTEST_F(HidumperMemoryTest, MemoryInfo002, TestSize.Level1)
17500600bfbSopenharmony_ci{
17600600bfbSopenharmony_ci    unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
17700600bfbSopenharmony_ci        make_unique<OHOS::HiviewDFX::MemoryInfo>();
17800600bfbSopenharmony_ci    shared_ptr<vector<vector<string>>> result = make_shared<vector<vector<string>>>();
17900600bfbSopenharmony_ci    ValueMap memInfo;
18000600bfbSopenharmony_ci    memoryInfo->GetPurgTotal(memInfo, result);
18100600bfbSopenharmony_ci    ASSERT_TRUE(memInfo.size() == 0);
18200600bfbSopenharmony_ci}
18300600bfbSopenharmony_ci
18400600bfbSopenharmony_ci/**
18500600bfbSopenharmony_ci * @tc.name: GetProcessInfo001
18600600bfbSopenharmony_ci * @tc.desc: Test GetProcessInfo ret.
18700600bfbSopenharmony_ci * @tc.type: FUNC
18800600bfbSopenharmony_ci */
18900600bfbSopenharmony_ciHWTEST_F(HidumperMemoryTest, GetProcessInfo001, TestSize.Level1)
19000600bfbSopenharmony_ci{
19100600bfbSopenharmony_ci    unique_ptr<OHOS::HiviewDFX::GetProcessInfo> getProcessInfo = make_unique<OHOS::HiviewDFX::GetProcessInfo>();
19200600bfbSopenharmony_ci    GroupMap groupMap;
19300600bfbSopenharmony_ci    ValueMap memInfo;
19400600bfbSopenharmony_ci    uint64_t pid = 0;
19500600bfbSopenharmony_ci    memInfo.insert(pair<string, uint64_t>("Name", pid));
19600600bfbSopenharmony_ci    groupMap.insert(pair<string, ValueMap>("test", memInfo));
19700600bfbSopenharmony_ci    int value = static_cast<int>(getProcessInfo->GetProcess(groupMap));
19800600bfbSopenharmony_ci    ASSERT_EQ(value, 0);
19900600bfbSopenharmony_ci}
20000600bfbSopenharmony_ci
20100600bfbSopenharmony_ci/**
20200600bfbSopenharmony_ci * @tc.name: GetKernelInfo001
20300600bfbSopenharmony_ci * @tc.desc: Test GetKernelInfo ret.
20400600bfbSopenharmony_ci * @tc.type: FUNC
20500600bfbSopenharmony_ci */
20600600bfbSopenharmony_ciHWTEST_F(HidumperMemoryTest, GetKernelInfo001, TestSize.Level1)
20700600bfbSopenharmony_ci{
20800600bfbSopenharmony_ci    unique_ptr<OHOS::HiviewDFX::GetKernelInfo> getGetKernelInfo = make_unique<OHOS::HiviewDFX::GetKernelInfo>();
20900600bfbSopenharmony_ci    ValueMap memInfo;
21000600bfbSopenharmony_ci    uint64_t value = 0;
21100600bfbSopenharmony_ci    ASSERT_TRUE(getGetKernelInfo->GetKernel(memInfo, value));
21200600bfbSopenharmony_ci}
21300600bfbSopenharmony_ci
21400600bfbSopenharmony_ci
21500600bfbSopenharmony_ci/**
21600600bfbSopenharmony_ci * @tc.name: GraphicMemory001
21700600bfbSopenharmony_ci * @tc.desc: Test GetGraphicMemory.
21800600bfbSopenharmony_ci * @tc.type: FUNC
21900600bfbSopenharmony_ci */
22000600bfbSopenharmony_ciHWTEST_F(HidumperMemoryTest, GraphicMemory001, TestSize.Level1)
22100600bfbSopenharmony_ci{
22200600bfbSopenharmony_ci    shared_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
22300600bfbSopenharmony_ci        make_shared<OHOS::HiviewDFX::MemoryInfo>();
22400600bfbSopenharmony_ci    MemInfoData::GraphicsMemory graphicsMemory;
22500600bfbSopenharmony_ci    FILE* file = popen("pidof render_service", "r");
22600600bfbSopenharmony_ci    char buffer[BUFFER_SIZE];
22700600bfbSopenharmony_ci    if (file) {
22800600bfbSopenharmony_ci        if (fgets(buffer, sizeof(buffer), file) != nullptr) {};
22900600bfbSopenharmony_ci        pclose(file);
23000600bfbSopenharmony_ci    }
23100600bfbSopenharmony_ci    int pid = strtol(buffer, nullptr, 10);
23200600bfbSopenharmony_ci    int ret = memoryInfo->GetGraphicsMemory(pid, graphicsMemory, GraphicType::GRAPH);
23300600bfbSopenharmony_ci    ASSERT_TRUE(ret);
23400600bfbSopenharmony_ci    memoryInfo->GetGraphicsMemory(pid, graphicsMemory, GraphicType::GL);
23500600bfbSopenharmony_ci    ASSERT_TRUE(ret);
23600600bfbSopenharmony_ci}
23700600bfbSopenharmony_ci} // namespace HiviewDFX
23800600bfbSopenharmony_ci} // namespace OHOS