Skip to content

Commit 4e83f8a

Browse files
committed
add test cases for BuildIDTest.
Signed-off-by: Ruoyu Qiu <[email protected]>
1 parent e07a49b commit 4e83f8a

File tree

1 file changed

+60
-6
lines changed

1 file changed

+60
-6
lines changed

llvm/unittests/Object/BuildIDTest.cpp

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/Object/BuildID.h"
10-
#include "llvm/ADT/ArrayRef.h"
1110
#include "llvm/ADT/SmallString.h"
1211
#include "llvm/ADT/StringRef.h"
1312
#include "llvm/Object/ELFObjectFile.h"
@@ -31,7 +30,61 @@ static Expected<ELFObjectFile<ELFT>> toBinary(SmallVectorImpl<char> &Storage,
3130
return ELFObjectFile<ELFT>::create(MemoryBufferRef(OS.str(), "dummyELF"));
3231
}
3332

33+
static StringRef optionalSectionHeaderELF(bool WithSec) {
34+
static std::string WithSection(R"(
35+
--- !ELF
36+
FileHeader:
37+
Class: ELFCLASS64
38+
Data: ELFDATA2LSB
39+
Type: ET_EXEC
40+
Machine: EM_X86_64
41+
ProgramHeaders:
42+
- Type: PT_NOTE
43+
FileSize: 0xffffffffffffffff
44+
FirstSec: .note.gnu.build-id
45+
LastSec: .note.gnu.build-id
46+
Sections:
47+
- Name: .note.gnu.build-id
48+
Type: SHT_NOTE
49+
AddressAlign: 0x04
50+
Notes:
51+
- Name: "GNU"
52+
Desc: "abb50d82b6bdc861"
53+
Type: 3
54+
)");
55+
static std::string WithoutSection(WithSection + R"(
56+
- Type: SectionHeaderTable
57+
NoHeaders: true
58+
)");
59+
if (WithSec)
60+
return WithSection;
61+
else
62+
return WithoutSection;
63+
}
64+
3465
TEST(BuildIDTest, InvalidNoteFileSizeTest) {
66+
SmallString<0> Storage;
67+
Expected<ELFObjectFile<ELF64LE>> ElfOrErr =
68+
toBinary<ELF64LE>(Storage, optionalSectionHeaderELF(true));
69+
ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded());
70+
BuildIDRef BuildID = getBuildID(&ElfOrErr.get());
71+
EXPECT_EQ(
72+
StringRef(reinterpret_cast<const char *>(BuildID.data()), BuildID.size()),
73+
"\xAB\xB5\x0D\x82\xB6\xBD\xC8\x61");
74+
}
75+
76+
TEST(BuildIDTest, OnlyInvalidProgramHeader) {
77+
SmallString<0> Storage;
78+
Expected<ELFObjectFile<ELF64LE>> ElfOrErr =
79+
toBinary<ELF64LE>(Storage, optionalSectionHeaderELF(false));
80+
ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded());
81+
BuildIDRef BuildID = getBuildID(&ElfOrErr.get());
82+
EXPECT_EQ(
83+
StringRef(reinterpret_cast<const char *>(BuildID.data()), BuildID.size()),
84+
"\xAB\xB5\x0D\x82\xB6\xBD\xC8\x61");
85+
}
86+
87+
TEST(BuildIDTest, InvalidSectionHeader) {
3588
SmallString<0> Storage;
3689
Expected<ELFObjectFile<ELF64LE>> ElfOrErr = toBinary<ELF64LE>(Storage, R"(
3790
--- !ELF
@@ -40,22 +93,23 @@ TEST(BuildIDTest, InvalidNoteFileSizeTest) {
4093
Data: ELFDATA2LSB
4194
Type: ET_EXEC
4295
Machine: EM_X86_64
96+
ProgramHeaders:
97+
- Type: PT_NOTE
98+
FirstSec: .note.gnu.build-id
99+
LastSec: .note.gnu.build-id
43100
Sections:
44101
- Name: .note.gnu.build-id
45102
Type: SHT_NOTE
46103
AddressAlign: 0x04
104+
Offset: 0x8000
47105
Notes:
48106
- Name: "GNU"
49107
Desc: "abb50d82b6bdc861"
50108
Type: 3
51-
ProgramHeaders:
52-
- Type: PT_NOTE
53-
FileSize: 0xffffffffffffffff
54-
Offset: 0x100
55109
)");
56110
ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded());
57111
BuildIDRef BuildID = getBuildID(&ElfOrErr.get());
58112
EXPECT_EQ(
59113
StringRef(reinterpret_cast<const char *>(BuildID.data()), BuildID.size()),
60114
"\xAB\xB5\x0D\x82\xB6\xBD\xC8\x61");
61-
}
115+
}

0 commit comments

Comments
 (0)