1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 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 16 #include <gtest/gtest.h> 17 #include <memory> 18 #include <sys/types.h> 19 #include "dfx_maps.h" 20 21 using namespace OHOS::HiviewDFX; 22 using namespace testing::ext; 23 using namespace std; 24 25 namespace OHOS { 26 namespace HiviewDFX { 27 class DfxMapsTest : public testing::Test { 28 public: SetUpTestCase(void)29 static void SetUpTestCase(void) {} TearDownTestCase(void)30 static void TearDownTestCase(void) {} SetUp()31 void SetUp() {} TearDown()32 void TearDown() {} 33 }; 34 35 namespace { 36 /** 37 * @tc.name: DfxMapsTest001 38 * @tc.desc: test map Create 39 * @tc.type: FUNC 40 */ 41 HWTEST_F(DfxMapsTest, DfxMapsTest001, TestSize.Level2) 42 { 43 GTEST_LOG_(INFO) << "DfxMapsTest001: start."; 44 char mapBuf[] = "7658d38000-7658d40000 rw-p 00000000 00:00 0 /system/lib/libdfx_dumpcatcher.z.so"; 45 std::shared_ptr<DfxElfMap> map = DfxElfMap::Create(mapBuf, sizeof(mapBuf)); 46 EXPECT_EQ(true, map != nullptr); 47 bool ret = map->IsValidPath(); 48 EXPECT_EQ(true, ret); 49 GTEST_LOG_(INFO) << map->PrintMap(); 50 GTEST_LOG_(INFO) << "DfxMapsTest001: end."; 51 } 52 53 /** 54 * @tc.name: DfxMapsTest002 55 * @tc.desc: test maps Create 56 * @tc.type: FUNC 57 */ 58 HWTEST_F(DfxMapsTest, DfxMapsTest002, TestSize.Level2) 59 { 60 GTEST_LOG_(INFO) << "DfxMapsTest002: start."; 61 char testBuffer[] = "1000-2000 ---s 00000000 00:00 0\n\ 62 2000-3000 r--s 00000000 00:00 0\n\ 63 3000-4000 -w-s 00000000 00:00 0\n\ 64 4000-5000 --xp 00000000 00:00 0\n\ 65 5000-6000 rwxp 00000000 00:00 0\n"; 66 std::shared_ptr<DfxElfMaps> elfMaps = DfxElfMaps::Create(testBuffer); 67 EXPECT_EQ(true, elfMaps != nullptr); 68 auto maps = elfMaps->GetMaps(); 69 EXPECT_EQ(true, maps.size() == 5); 70 71 elfMaps = DfxElfMaps::CreateFromLocal(); 72 EXPECT_EQ(true, elfMaps != nullptr); 73 maps = elfMaps->GetMaps(); 74 EXPECT_EQ(true, maps.size() > 0); 75 GTEST_LOG_(INFO) << "DfxMapsTest002: end."; 76 } 77 78 /** 79 * @tc.name: DfxMapsTest003 80 * @tc.desc: test find map by addr 81 * @tc.type: FUNC 82 */ 83 HWTEST_F(DfxMapsTest, DfxMapsTest003, TestSize.Level2) 84 { 85 GTEST_LOG_(INFO) << "DfxMapsTest003: start."; 86 std::shared_ptr<DfxElfMaps> maps = DfxElfMaps::CreateFromLocal(); 87 std::shared_ptr<DfxElfMap> map = std::make_shared<DfxElfMap>(); 88 uintptr_t address = -1; 89 bool flag = maps->FindMapByAddr(address, map); 90 EXPECT_EQ(false, flag); 91 address = 1; 92 flag = maps->FindMapByAddr(address, map); 93 EXPECT_EQ(false, flag); 94 GTEST_LOG_(INFO) << "DfxMapsTest003: end."; 95 } 96 } 97 } // namespace HiviewDFX 98 } // namespace OHOS 99