1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#include "dfx_elf.h"
17800b99b8Sopenharmony_ci#include "dfx_maps.h"
18800b99b8Sopenharmony_ci#include <gtest/gtest.h>
19800b99b8Sopenharmony_ci#include <memory>
20800b99b8Sopenharmony_ci#include <sys/types.h>
21800b99b8Sopenharmony_ci
22800b99b8Sopenharmony_ciusing namespace OHOS::HiviewDFX;
23800b99b8Sopenharmony_ciusing namespace std;
24800b99b8Sopenharmony_ciusing namespace testing::ext;
25800b99b8Sopenharmony_ci
26800b99b8Sopenharmony_cinamespace OHOS {
27800b99b8Sopenharmony_cinamespace HiviewDFX {
28800b99b8Sopenharmony_cinamespace {
29800b99b8Sopenharmony_ciconst string INVALID_MAP_NAME = "/system/lib64/init/libinit_context111.z.so";
30800b99b8Sopenharmony_ci#ifdef __arm__
31800b99b8Sopenharmony_ciconst string MAPS_FILE = "/data/test/resource/testdata/testmaps_32";
32800b99b8Sopenharmony_ciconst string VALID_MAP_NAME = "/system/lib/init/libinit_context.z.so";
33800b99b8Sopenharmony_ciconst string VALID_MAP_ITEM = "f6d83000-f6d84000 r--p 00001000 b3:07 1892 /system/lib/init/libinit_context.z.so";
34800b99b8Sopenharmony_ciconst string INVALID_MAP_ITEM = "f6d83000-f6d84000 r--p 00001000 b3:07 1892 /system/lib/init/libinit_context111.z.so";
35800b99b8Sopenharmony_ciconst string VDSO_MAP_ITEM = "f7c21000-f7c22000 r-xp 00000000 00:00 0                                  [vdso]";
36800b99b8Sopenharmony_ciconst string ARK_HAP_MAP_NAME = "/system/app/SceneBoard/SceneBoard.hap";
37800b99b8Sopenharmony_ciconst string ARK_CODE_MAP_NAME = "[anon:ArkTS Code:libdialog.z.so/Dialog.js]";
38800b99b8Sopenharmony_ci#else
39800b99b8Sopenharmony_ciconst string MAPS_FILE = "/data/test/resource/testdata/testmaps_64";
40800b99b8Sopenharmony_ciconst string VALID_MAP_NAME = "/system/lib64/init/libinit_context.z.so";
41800b99b8Sopenharmony_ciconst string VALID_MAP_ITEM = "7f0ab40000-7f0ab41000 r--p 00000000 b3:07 1882 /system/lib64/init/libinit_context.z.so";
42800b99b8Sopenharmony_ciconst string INVALID_MAP_ITEM = "7f0ab40000-7f0ab41000 r--p 00000000 b3:07 1882 \
43800b99b8Sopenharmony_ci    /system/lib64/init/libinit_context111.z.so";
44800b99b8Sopenharmony_ciconst string VDSO_MAP_ITEM = "7f8b9df000-7f8b9e0000 r-xp 00000000 00:00 0                              [vdso]";
45800b99b8Sopenharmony_ciconst string ARK_HAP_MAP_NAME = "/system/app/SceneBoard/SceneBoard.hap";
46800b99b8Sopenharmony_ciconst string ARK_CODE_MAP_NAME = "[anon:ArkTS Code:libdialog.z.so/Dialog.js]";
47800b99b8Sopenharmony_ci#endif
48800b99b8Sopenharmony_ci}
49800b99b8Sopenharmony_ci
50800b99b8Sopenharmony_ciclass MapsTest : public testing::Test {
51800b99b8Sopenharmony_cipublic:
52800b99b8Sopenharmony_ci    static void SetUpTestCase(void) {}
53800b99b8Sopenharmony_ci    static void TearDownTestCase(void) {}
54800b99b8Sopenharmony_ci    void SetUp() { maps_ = DfxMaps::Create(getpid(), MAPS_FILE); }
55800b99b8Sopenharmony_ci    void TearDown() {}
56800b99b8Sopenharmony_ci
57800b99b8Sopenharmony_cipublic:
58800b99b8Sopenharmony_ci    std::shared_ptr<DfxMaps> maps_;
59800b99b8Sopenharmony_ci};
60800b99b8Sopenharmony_ci
61800b99b8Sopenharmony_cinamespace {
62800b99b8Sopenharmony_ci
63800b99b8Sopenharmony_ci/**
64800b99b8Sopenharmony_ci * @tc.name: FindMapByAddrTest001
65800b99b8Sopenharmony_ci * @tc.desc: test FindMapByAddr functions
66800b99b8Sopenharmony_ci * @tc.type: FUNC
67800b99b8Sopenharmony_ci */
68800b99b8Sopenharmony_ciHWTEST_F(MapsTest, FindMapByAddrTest001, TestSize.Level2)
69800b99b8Sopenharmony_ci{
70800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FindMapByAddrTest001: start.";
71800b99b8Sopenharmony_ci    maps_->Sort(true);
72800b99b8Sopenharmony_ci#ifdef __arm__
73800b99b8Sopenharmony_ci    uintptr_t validAddr = 0xf6d80000;
74800b99b8Sopenharmony_ci#else
75800b99b8Sopenharmony_ci    uintptr_t validAddr = 0x7f8b8f3001;
76800b99b8Sopenharmony_ci#endif
77800b99b8Sopenharmony_ci    const uintptr_t invalidAddr = 0xffffffff;
78800b99b8Sopenharmony_ci    std::shared_ptr<DfxMap> map = nullptr;
79800b99b8Sopenharmony_ci    EXPECT_EQ(true, maps_->FindMapByAddr(validAddr, map));
80800b99b8Sopenharmony_ci    EXPECT_EQ(false, maps_->FindMapByAddr(invalidAddr, map));
81800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FindMapByAddrTest001: end.";
82800b99b8Sopenharmony_ci}
83800b99b8Sopenharmony_ci
84800b99b8Sopenharmony_ci/**
85800b99b8Sopenharmony_ci * @tc.name: FindMapByFileInfoTest001
86800b99b8Sopenharmony_ci * @tc.desc: test FindMapByFileInfo functions
87800b99b8Sopenharmony_ci * @tc.type: FUNC
88800b99b8Sopenharmony_ci */
89800b99b8Sopenharmony_ciHWTEST_F(MapsTest, FindMapByFileInfoTest001, TestSize.Level2)
90800b99b8Sopenharmony_ci{
91800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FindMapByFileInfoTest001: start.";
92800b99b8Sopenharmony_ci    std::shared_ptr<DfxMap> map = nullptr;
93800b99b8Sopenharmony_ci    EXPECT_EQ(true, maps_->FindMapByFileInfo(VALID_MAP_NAME, 0, map));
94800b99b8Sopenharmony_ci    EXPECT_EQ(false, maps_->FindMapByFileInfo(INVALID_MAP_NAME, 0, map));
95800b99b8Sopenharmony_ci    const uint64_t invalidOffset = 0xffffffff;
96800b99b8Sopenharmony_ci    EXPECT_EQ(false, maps_->FindMapByFileInfo(VALID_MAP_NAME, invalidOffset, map));
97800b99b8Sopenharmony_ci    EXPECT_EQ(false, maps_->FindMapByFileInfo(INVALID_MAP_NAME, invalidOffset, map));
98800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FindMapByFileInfoTest001: end.";
99800b99b8Sopenharmony_ci}
100800b99b8Sopenharmony_ci
101800b99b8Sopenharmony_ci/**
102800b99b8Sopenharmony_ci * @tc.name: FindMapsByNameTest001
103800b99b8Sopenharmony_ci * @tc.desc: test FindMapsByName functions
104800b99b8Sopenharmony_ci * @tc.type: FUNC
105800b99b8Sopenharmony_ci */
106800b99b8Sopenharmony_ciHWTEST_F(MapsTest, FindMapsByNameTest001, TestSize.Level2)
107800b99b8Sopenharmony_ci{
108800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FindMapsByNameTest001: start.";
109800b99b8Sopenharmony_ci    std::vector<std::shared_ptr<DfxMap>> mapsV;
110800b99b8Sopenharmony_ci    EXPECT_EQ(true, maps_->FindMapsByName(VALID_MAP_NAME, mapsV));
111800b99b8Sopenharmony_ci    mapsV.clear();
112800b99b8Sopenharmony_ci    EXPECT_EQ(false, maps_->FindMapsByName(INVALID_MAP_NAME, mapsV));
113800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "FindMapsByNameTest001: end.";
114800b99b8Sopenharmony_ci}
115800b99b8Sopenharmony_ci
116800b99b8Sopenharmony_ci/**
117800b99b8Sopenharmony_ci * @tc.name: IsArkNameTest001
118800b99b8Sopenharmony_ci * @tc.desc: test IsArkExecutable functions
119800b99b8Sopenharmony_ci * @tc.type: FUNC
120800b99b8Sopenharmony_ci */
121800b99b8Sopenharmony_ciHWTEST_F(MapsTest, IsArkNameTest001, TestSize.Level2)
122800b99b8Sopenharmony_ci{
123800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "IsArkNameTest001: start.";
124800b99b8Sopenharmony_ci    std::shared_ptr<DfxMap> map = nullptr;
125800b99b8Sopenharmony_ci    map = std::make_shared<DfxMap>(0, 0, 0, "1", "anon:ArkTS Code");
126800b99b8Sopenharmony_ci    EXPECT_EQ(false, map->IsArkExecutable());
127800b99b8Sopenharmony_ci    map = std::make_shared<DfxMap>(0, 0, 0, "1", "/dev/zero");
128800b99b8Sopenharmony_ci    EXPECT_EQ(false, map->IsArkExecutable());
129800b99b8Sopenharmony_ci    map = std::make_shared<DfxMap>(0, 0, 0, 4, "[anon:ArkTS Code]");
130800b99b8Sopenharmony_ci    EXPECT_EQ(true, map->IsArkExecutable());
131800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "IsArkNameTest001: end.";
132800b99b8Sopenharmony_ci}
133800b99b8Sopenharmony_ci
134800b99b8Sopenharmony_ci/**
135800b99b8Sopenharmony_ci * @tc.name: GetRelPcTest
136800b99b8Sopenharmony_ci * @tc.desc: test getRelPc no elf
137800b99b8Sopenharmony_ci * @tc.type: FUNC
138800b99b8Sopenharmony_ci */
139800b99b8Sopenharmony_ciHWTEST_F(MapsTest, GetRelPcTest, TestSize.Level2)
140800b99b8Sopenharmony_ci{
141800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "GetRelPcTest: start.";
142800b99b8Sopenharmony_ci#ifdef __arm__
143800b99b8Sopenharmony_ci    uint64_t pc = 0xf6d83001;
144800b99b8Sopenharmony_ci    const uint64_t invalidOffset = 0x1001;
145800b99b8Sopenharmony_ci#else
146800b99b8Sopenharmony_ci    uint64_t pc = 0x7f0ab40016;
147800b99b8Sopenharmony_ci    const uint64_t invalidOffset = 0x16;
148800b99b8Sopenharmony_ci#endif
149800b99b8Sopenharmony_ci    shared_ptr<DfxMap> map = DfxMap::Create(INVALID_MAP_ITEM, sizeof(INVALID_MAP_ITEM));
150800b99b8Sopenharmony_ci    EXPECT_EQ(true, ((map->GetElf() == nullptr) && (map->GetRelPc(pc) == invalidOffset)));
151800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "GetRelPcTest: end.";
152800b99b8Sopenharmony_ci}
153800b99b8Sopenharmony_ci
154800b99b8Sopenharmony_ci/**
155800b99b8Sopenharmony_ci * @tc.name: ToStringTest
156800b99b8Sopenharmony_ci * @tc.desc: test ToString
157800b99b8Sopenharmony_ci * @tc.type: FUNC
158800b99b8Sopenharmony_ci */
159800b99b8Sopenharmony_ciHWTEST_F(MapsTest, ToStringTest, TestSize.Level2)
160800b99b8Sopenharmony_ci{
161800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "ToStringTest: start.";
162800b99b8Sopenharmony_ci    shared_ptr<DfxMap> map = DfxMap::Create(VALID_MAP_ITEM, sizeof(VALID_MAP_ITEM));
163800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << map->ToString();
164800b99b8Sopenharmony_ci    EXPECT_EQ(true, sizeof(map->ToString()) != 0);
165800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "ToStringTest: end.";
166800b99b8Sopenharmony_ci}
167800b99b8Sopenharmony_ci
168800b99b8Sopenharmony_ci/**
169800b99b8Sopenharmony_ci * @tc.name: CreateMapsTest
170800b99b8Sopenharmony_ci * @tc.desc: test create maps by pid
171800b99b8Sopenharmony_ci * @tc.type: FUNC
172800b99b8Sopenharmony_ci */
173800b99b8Sopenharmony_ciHWTEST_F(MapsTest, CreateMapsTest, TestSize.Level2)
174800b99b8Sopenharmony_ci{
175800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "CreateMapsTest: start.";
176800b99b8Sopenharmony_ci    std::shared_ptr<DfxMaps> dfxMaps = DfxMaps::Create(getpid());
177800b99b8Sopenharmony_ci    ASSERT_TRUE(dfxMaps != nullptr);
178800b99b8Sopenharmony_ci    auto maps = dfxMaps->GetMaps();
179800b99b8Sopenharmony_ci    for (auto map : maps) {
180800b99b8Sopenharmony_ci        std::cout << map->ToString();
181800b99b8Sopenharmony_ci    }
182800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "CreateMapsTest: end.";
183800b99b8Sopenharmony_ci}
184800b99b8Sopenharmony_ci
185800b99b8Sopenharmony_ci/**
186800b99b8Sopenharmony_ci * @tc.name: GetStackRangeTest
187800b99b8Sopenharmony_ci * @tc.desc: test GetStackRange
188800b99b8Sopenharmony_ci * @tc.type: FUNC
189800b99b8Sopenharmony_ci */
190800b99b8Sopenharmony_ciHWTEST_F(MapsTest, GetStackRangeTest, TestSize.Level2)
191800b99b8Sopenharmony_ci{
192800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "GetStackRangeTest: start.";
193800b99b8Sopenharmony_ci    uintptr_t bottom, top;
194800b99b8Sopenharmony_ci    ASSERT_TRUE(maps_->GetStackRange(bottom, top));
195800b99b8Sopenharmony_ci#ifdef __arm__
196800b99b8Sopenharmony_ci    EXPECT_EQ(bottom, 0xff860000);
197800b99b8Sopenharmony_ci    EXPECT_EQ(top, 0xff881000);
198800b99b8Sopenharmony_ci#else
199800b99b8Sopenharmony_ci    EXPECT_EQ(bottom, 0x7fe37db000);
200800b99b8Sopenharmony_ci    EXPECT_EQ(top, 0x7fe37fc000);
201800b99b8Sopenharmony_ci#endif
202800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "GetStackRangeTest: end.";
203800b99b8Sopenharmony_ci}
204800b99b8Sopenharmony_ci
205800b99b8Sopenharmony_ci/**
206800b99b8Sopenharmony_ci * @tc.name: IsArkExecutedMapTest
207800b99b8Sopenharmony_ci * @tc.desc: test IsArkExecutedMap
208800b99b8Sopenharmony_ci * @tc.type: FUNC
209800b99b8Sopenharmony_ci */
210800b99b8Sopenharmony_ciHWTEST_F(MapsTest, IsArkExecutedMapTest, TestSize.Level2)
211800b99b8Sopenharmony_ci{
212800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "IsArkExecutedMapTest: start.";
213800b99b8Sopenharmony_ci    uintptr_t addr;
214800b99b8Sopenharmony_ci#ifdef __arm__
215800b99b8Sopenharmony_ci    addr = 0xffff2001;
216800b99b8Sopenharmony_ci#else
217800b99b8Sopenharmony_ci    addr = 0x7fe37fd001;
218800b99b8Sopenharmony_ci#endif
219800b99b8Sopenharmony_ci    ASSERT_TRUE(maps_->IsArkExecutedMap(addr));
220800b99b8Sopenharmony_ci#ifdef __arm__
221800b99b8Sopenharmony_ci    addr = 0xffff1001;
222800b99b8Sopenharmony_ci#else
223800b99b8Sopenharmony_ci    addr = 0x7fe37fc001;
224800b99b8Sopenharmony_ci#endif
225800b99b8Sopenharmony_ci    ASSERT_FALSE(maps_->IsArkExecutedMap(addr));
226800b99b8Sopenharmony_ci    addr = 0x0;
227800b99b8Sopenharmony_ci    ASSERT_FALSE(maps_->IsArkExecutedMap(addr));
228800b99b8Sopenharmony_ci    maps_->Sort(false);
229800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "IsArkExecutedMapTest: end.";
230800b99b8Sopenharmony_ci}
231800b99b8Sopenharmony_ci
232800b99b8Sopenharmony_ci/**
233800b99b8Sopenharmony_ci * @tc.name: IsVdsoMapTest
234800b99b8Sopenharmony_ci * @tc.desc: test IsVdsoMap
235800b99b8Sopenharmony_ci * @tc.type: FUNC
236800b99b8Sopenharmony_ci */
237800b99b8Sopenharmony_ciHWTEST_F(MapsTest, IsVdsoMapTest, TestSize.Level2)
238800b99b8Sopenharmony_ci{
239800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "IsVdsoMapTest: start.";
240800b99b8Sopenharmony_ci    shared_ptr<DfxMap> map = DfxMap::Create(VDSO_MAP_ITEM, sizeof(VDSO_MAP_ITEM));
241800b99b8Sopenharmony_ci    ASSERT_TRUE(map->IsVdsoMap());
242800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "IsVdsoMapTest: end.";
243800b99b8Sopenharmony_ci}
244800b99b8Sopenharmony_ci
245800b99b8Sopenharmony_ci/**
246800b99b8Sopenharmony_ci * @tc.name: IsLegalMapItemTest
247800b99b8Sopenharmony_ci * @tc.desc: test IsLegalMapItem, IsArkHapMapItem, IsArkCodeMapItem
248800b99b8Sopenharmony_ci * @tc.type: FUNC
249800b99b8Sopenharmony_ci */
250800b99b8Sopenharmony_ciHWTEST_F(MapsTest, IsLegalMapItemTest, TestSize.Level2)
251800b99b8Sopenharmony_ci{
252800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "IsLegalMapItemTest: start.";
253800b99b8Sopenharmony_ci    ASSERT_TRUE(DfxMaps::IsArkHapMapItem(ARK_HAP_MAP_NAME));
254800b99b8Sopenharmony_ci    ASSERT_TRUE(DfxMaps::IsArkCodeMapItem(ARK_CODE_MAP_NAME));
255800b99b8Sopenharmony_ci    ASSERT_TRUE(DfxMaps::IsLegalMapItem(ARK_CODE_MAP_NAME));
256800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "IsLegalMapItemTest: end.";
257800b99b8Sopenharmony_ci}
258800b99b8Sopenharmony_ci}
259800b99b8Sopenharmony_ci} // namespace HiviewDFX
260800b99b8Sopenharmony_ci} // namespace OHOS