1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. 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 #include <cstring> 16 17 #include <gtest/gtest.h> 18 19 #include "maps_info.h" 20 21 using namespace testing::ext; 22 23 namespace OHOS { 24 namespace Developtools { 25 namespace Hiebpf { 26 class MapsInofTest : public ::testing::Test { 27 public: SetUpTestCase()28 static void SetUpTestCase() {}; TearDownTestCase()29 static void TearDownTestCase() {}; 30 SetUp()31 void SetUp() {} TearDown()32 void TearDown() {} 33 }; 34 35 /** 36 * @tc.name: GetMaps 37 * @tc.desc: Test framework 38 * @tc.type: FUNC 39 */ 40 HWTEST_F(MapsInofTest, GetMaps, TestSize.Level1) 41 { 42 MapsInfo mapsInfo; 43 std::vector<MapsInfo::MapsItem> mapsItems; 44 mapsInfo.GetMaps(0, mapsItems); 45 ASSERT_EQ(mapsItems.size(), 0); 46 mapsInfo.GetMaps(-1, mapsItems); 47 ASSERT_EQ(mapsItems.size(), 0); 48 49 mapsInfo.GetMaps(1, mapsItems); 50 ASSERT_GE(mapsItems.size(), 1); 51 ASSERT_EQ(mapsItems[0].pid_, 1); 52 ASSERT_FALSE(mapsItems[0].fileName_.find("init") == std::string::npos); 53 } 54 55 HWTEST_F(MapsInofTest, Cache, TestSize.Level1) 56 { 57 MapsInfo mapsInfo; 58 ASSERT_FALSE(mapsInfo.IsCached(1)); 59 mapsInfo.CachePid(1); 60 ASSERT_TRUE(mapsInfo.IsCached(1)); 61 mapsInfo.RemovePid(1); 62 ASSERT_FALSE(mapsInfo.IsCached(1)); 63 } 64 65 HWTEST_F(MapsInofTest, GetBinary, TestSize.Level1) 66 { 67 MapsInfo mapsInfo; 68 MapsInfo::MapsItem mapsItem; 69 const int testNum = 1000; 70 mapsItem.start_ = 0; 71 mapsItem.end_ = testNum; 72 mapsItem.offset_ = testNum; 73 mapsItem.pid_ = testNum; 74 mapsItem.fileName_ = "testGetBinary"; 75 76 std::vector<uint8_t> buf; 77 uint32_t size = mapsInfo.GetBinary(mapsItem, buf); 78 const uint32_t fixLen = sizeof(mapsItem.start_) + sizeof(mapsItem.end_) + 79 sizeof(mapsItem.offset_) + sizeof(mapsItem.pid_) + sizeof(uint32_t); 80 ASSERT_EQ(size, fixLen + mapsItem.fileName_.size() + 1); 81 82 uint8_t *p = buf.data(); 83 uint64_t *start = (uint64_t *)p; 84 ASSERT_EQ(mapsItem.start_, *start); 85 p += sizeof(mapsItem.start_); 86 87 uint64_t *end = (uint64_t *)p; 88 ASSERT_EQ(mapsItem.end_, *end); 89 p += sizeof(mapsItem.end_); 90 91 uint32_t *offset = (uint32_t *)p; 92 ASSERT_EQ(mapsItem.offset_, *offset); 93 p += sizeof(mapsItem.offset_); 94 95 uint32_t *pid = (uint32_t *)p; 96 ASSERT_EQ(mapsItem.pid_, *pid); 97 p += sizeof(mapsItem.pid_); 98 99 uint32_t *fileNameLen = (uint32_t *)p; 100 ASSERT_EQ((uint32_t)(mapsItem.fileName_.size() + 1), *fileNameLen); 101 p += sizeof(uint32_t); 102 103 std::string fileName = (char *)p; 104 ASSERT_EQ(fileName, mapsItem.fileName_); 105 } 106 } // namespace Hiebpf 107 } // namespace Developtools 108 } // namespace OHOS 109