* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include <gtest/gtest.h>
#include "mockcpp/mockcpp.hpp"
#include "dump_ELF.h"
using namespace Adx;
class DUMP_ELF_UTEST : public testing::Test {
protected:
virtual void SetUp() {}
virtual void TearDown() {
GlobalMockObject::verify();
}
};
TEST_F(DUMP_ELF_UTEST, TEST_ELF_CREATE_AND_SAVE) {
ELF::DumpELF coreFile;
ELF::SectionPtr sec = coreFile.AddSection(ASCEND_SHTYPE_GLOBAL, std::string("test"));
std::string data("test");
sec->SetData(data);
system("mkdir -p /tmp/adump_ELF_utest");
coreFile.Save("/tmp/adump_ELF_utest/test.elf");
EXPECT_EQ(0, system("readelf /tmp/adump_ELF_utest/test.elf -t"));
EXPECT_EQ(0, system("readelf /tmp/adump_ELF_utest/test.elf -p test"));
system("rm -r /tmp/adump_ELF_utest");
}
TEST_F(DUMP_ELF_UTEST, TEST_SECTION_ADDRALIGN) {
ELF::Section sec(SHT_NULL);
sec.SetAddrAlign(8U);
EXPECT_EQ(sec.GetAddrAlign(), 8U);
sec.SetAddrAlign(16U);
EXPECT_EQ(sec.GetAddrAlign(), 16U);
}
TEST_F(DUMP_ELF_UTEST, TEST_ADDSECTION_DUPLICATE_NAME) {
ELF::DumpELF coreFile;
ELF::SectionPtr sec1 = coreFile.AddSection(ASCEND_SHTYPE_GLOBAL, "same_name");
EXPECT_NE(sec1, nullptr);
ELF::SectionPtr sec2 = coreFile.AddSection(ASCEND_SHTYPE_GLOBAL, "same_name");
EXPECT_NE(sec2, nullptr);
EXPECT_NE(sec1.get(), sec2.get());
}
TEST_F(DUMP_ELF_UTEST, TEST_GETSECTION_OUT_OF_RANGE) {
ELF::DumpELF coreFile;
ELF::SectionPtr sec = coreFile.GetSectionByIndex(9999U);
EXPECT_EQ(sec, nullptr);
}
TEST_F(DUMP_ELF_UTEST, TEST_ELF_SAVE_ON_EXISTING_FILE) {
system("mkdir -p /tmp/adump_ELF_exist_test");
system("touch /tmp/adump_ELF_exist_test/existing.elf");
ELF::DumpELF coreFile;
coreFile.Save("/tmp/adump_ELF_exist_test/existing.elf");
system("rm -rf /tmp/adump_ELF_exist_test");
EXPECT_TRUE(true);
}