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 "elf_symbol_info.h" 2006f6ba60Sopenharmony_ci 2106f6ba60Sopenharmony_ciusing namespace testing::ext; 2206f6ba60Sopenharmony_ci 2306f6ba60Sopenharmony_cinamespace OHOS { 2406f6ba60Sopenharmony_cinamespace Developtools { 2506f6ba60Sopenharmony_cinamespace Hiebpf { 2606f6ba60Sopenharmony_ciclass ElfSymbolInfoTest : 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 const std::string TEST_FILE_64 = "/data/test/resources/testdata/elf64.stripped"; 3506f6ba60Sopenharmony_ci const std::string TEST_FILE_32 = "/data/test/resources/testdata/elf32.unstripped"; 3606f6ba60Sopenharmony_ci}; 3706f6ba60Sopenharmony_ci 3806f6ba60Sopenharmony_ci/** 3906f6ba60Sopenharmony_ci * @tc.name: GetSymbolTable 4006f6ba60Sopenharmony_ci * @tc.desc: Test framework 4106f6ba60Sopenharmony_ci * @tc.type: FUNC 4206f6ba60Sopenharmony_ci */ 4306f6ba60Sopenharmony_ciHWTEST_F(ElfSymbolInfoTest, GetSymbolTable32Unstripped, TestSize.Level1) 4406f6ba60Sopenharmony_ci{ 4506f6ba60Sopenharmony_ci ElfSymbolInfo elfSymbolInfo; 4606f6ba60Sopenharmony_ci ElfSymbolInfo::ElfSymbolTable elfSymbolTable; 4706f6ba60Sopenharmony_ci ASSERT_TRUE(elfSymbolInfo.GetSymbolTable(TEST_FILE_32, elfSymbolTable)); 4806f6ba60Sopenharmony_ci ASSERT_GT(elfSymbolTable.textOffset_, 0); 4906f6ba60Sopenharmony_ci ASSERT_GT(elfSymbolTable.symTable_.size(), 0); 5006f6ba60Sopenharmony_ci ASSERT_GT(elfSymbolTable.strTable_.size(), 0); 5106f6ba60Sopenharmony_ci 5206f6ba60Sopenharmony_ci const uint32_t symEntSize32 = 16; 5306f6ba60Sopenharmony_ci ASSERT_EQ(elfSymbolTable.symEntSize_, symEntSize32); 5406f6ba60Sopenharmony_ci 5506f6ba60Sopenharmony_ci ASSERT_EQ(elfSymbolTable.fileName_, TEST_FILE_32); 5606f6ba60Sopenharmony_ci} 5706f6ba60Sopenharmony_ci 5806f6ba60Sopenharmony_ciHWTEST_F(ElfSymbolInfoTest, GetSymbolTable64Stripped, TestSize.Level1) 5906f6ba60Sopenharmony_ci{ 6006f6ba60Sopenharmony_ci ElfSymbolInfo elfSymbolInfo; 6106f6ba60Sopenharmony_ci ElfSymbolInfo::ElfSymbolTable elfSymbolTable; 6206f6ba60Sopenharmony_ci ASSERT_TRUE(elfSymbolInfo.GetSymbolTable(TEST_FILE_64, elfSymbolTable)); 6306f6ba60Sopenharmony_ci ASSERT_GT(elfSymbolTable.textOffset_, 0); 6406f6ba60Sopenharmony_ci ASSERT_GT(elfSymbolTable.symTable_.size(), 0); 6506f6ba60Sopenharmony_ci ASSERT_GT(elfSymbolTable.strTable_.size(), 0); 6606f6ba60Sopenharmony_ci 6706f6ba60Sopenharmony_ci const uint32_t symEntSize64 = 24; 6806f6ba60Sopenharmony_ci ASSERT_EQ(elfSymbolTable.symEntSize_, symEntSize64); 6906f6ba60Sopenharmony_ci 7006f6ba60Sopenharmony_ci ASSERT_EQ(elfSymbolTable.fileName_, TEST_FILE_64); 7106f6ba60Sopenharmony_ci} 7206f6ba60Sopenharmony_ci 7306f6ba60Sopenharmony_ciHWTEST_F(ElfSymbolInfoTest, Cache, TestSize.Level1) 7406f6ba60Sopenharmony_ci{ 7506f6ba60Sopenharmony_ci ElfSymbolInfo elfSymbolInfo; 7606f6ba60Sopenharmony_ci ASSERT_FALSE(elfSymbolInfo.IsCached(TEST_FILE_64)); 7706f6ba60Sopenharmony_ci elfSymbolInfo.CacheFileName(TEST_FILE_64); 7806f6ba60Sopenharmony_ci ASSERT_TRUE(elfSymbolInfo.IsCached(TEST_FILE_64)); 7906f6ba60Sopenharmony_ci} 8006f6ba60Sopenharmony_ci 8106f6ba60Sopenharmony_ciHWTEST_F(ElfSymbolInfoTest, GetBinary, TestSize.Level1) 8206f6ba60Sopenharmony_ci{ 8306f6ba60Sopenharmony_ci ElfSymbolInfo elfSymbolInfo; 8406f6ba60Sopenharmony_ci ElfSymbolInfo::ElfSymbolTable elfSymbolTable; 8506f6ba60Sopenharmony_ci ASSERT_TRUE(elfSymbolInfo.GetSymbolTable(TEST_FILE_64, elfSymbolTable)); 8606f6ba60Sopenharmony_ci std::vector<uint8_t> buf; 8706f6ba60Sopenharmony_ci uint32_t size = elfSymbolInfo.GetBinary(elfSymbolTable, buf); 8806f6ba60Sopenharmony_ci const uint32_t binLen = sizeof(elfSymbolTable.textVaddr_) + 8906f6ba60Sopenharmony_ci sizeof(elfSymbolTable.textOffset_) + 9006f6ba60Sopenharmony_ci sizeof(uint32_t) + sizeof(uint32_t) + // strTabLen + symTabLen 9106f6ba60Sopenharmony_ci sizeof(uint32_t) + // fileNameLen 9206f6ba60Sopenharmony_ci sizeof(elfSymbolTable.symEntSize_) + 9306f6ba60Sopenharmony_ci elfSymbolTable.strTable_.size() + 9406f6ba60Sopenharmony_ci elfSymbolTable.symTable_.size() + 9506f6ba60Sopenharmony_ci elfSymbolTable.fileName_.size() + 1; 9606f6ba60Sopenharmony_ci ASSERT_EQ(size, binLen); 9706f6ba60Sopenharmony_ci 9806f6ba60Sopenharmony_ci uint8_t *p = buf.data() + binLen - elfSymbolTable.fileName_.size() - 1; 9906f6ba60Sopenharmony_ci std::string fileName = (char *)p; 10006f6ba60Sopenharmony_ci ASSERT_EQ(fileName, TEST_FILE_64); 10106f6ba60Sopenharmony_ci} 10206f6ba60Sopenharmony_ci} // namespace Hiebpf 10306f6ba60Sopenharmony_ci} // namespace Developtools 10406f6ba60Sopenharmony_ci} // namespace OHOS 105