Skip to content

Commit e07a49b

Browse files
committed
add unit test of getBuildId()
Signed-off-by: Ruoyu Qiu <[email protected]>
1 parent 9393e97 commit e07a49b

File tree

3 files changed

+62
-23
lines changed

3 files changed

+62
-23
lines changed

llvm/test/tools/llvm-objdump/ELF/invalid-phdr.test

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,3 @@ FileHeader:
1515
Type: ET_EXEC
1616
Machine: EM_X86_64
1717
EPhOff: 0xffffff
18-
19-
20-
# RUN: yaml2obj --docnum=2 %s -o %t.note.err
21-
# RUN: llvm-objdump -D %t.note.err 2>&1 | \
22-
# RUN: FileCheck %s --check-prefix=PFILESIZE
23-
24-
# PFILESIZE: Disassembly of section .note.gnu.build-id:
25-
26-
--- !ELF
27-
FileHeader:
28-
Class: ELFCLASS64
29-
Data: ELFDATA2LSB
30-
Type: ET_EXEC
31-
Machine: EM_X86_64
32-
Sections:
33-
- Name: .note.gnu.build-id
34-
Type: SHT_NOTE
35-
Content: 040000000800000003000000474e5500abb50d82b6bdc861
36-
AddressAlign: 0x04
37-
ProgramHeaders:
38-
- Type: PT_NOTE
39-
FileSize: 0xffffffffffffffff
40-
Offset: 0x100
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===- BuildIDTest.cpp - Tests for getBuildID ----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Object/BuildID.h"
10+
#include "llvm/ADT/ArrayRef.h"
11+
#include "llvm/ADT/SmallString.h"
12+
#include "llvm/ADT/StringRef.h"
13+
#include "llvm/Object/ELFObjectFile.h"
14+
#include "llvm/ObjectYAML/yaml2obj.h"
15+
#include "llvm/Support/YAMLTraits.h"
16+
#include "llvm/Testing/Support/Error.h"
17+
18+
#include "gtest/gtest.h"
19+
20+
using namespace llvm;
21+
using namespace llvm::object;
22+
23+
template <class ELFT>
24+
static Expected<ELFObjectFile<ELFT>> toBinary(SmallVectorImpl<char> &Storage,
25+
StringRef Yaml) {
26+
raw_svector_ostream OS(Storage);
27+
yaml::Input YIn(Yaml);
28+
if (!yaml::convertYAML(YIn, OS, [](const Twine &Msg) {}))
29+
return createStringError(std::errc::invalid_argument,
30+
"unable to convert YAML");
31+
return ELFObjectFile<ELFT>::create(MemoryBufferRef(OS.str(), "dummyELF"));
32+
}
33+
34+
TEST(BuildIDTest, InvalidNoteFileSizeTest) {
35+
SmallString<0> Storage;
36+
Expected<ELFObjectFile<ELF64LE>> ElfOrErr = toBinary<ELF64LE>(Storage, R"(
37+
--- !ELF
38+
FileHeader:
39+
Class: ELFCLASS64
40+
Data: ELFDATA2LSB
41+
Type: ET_EXEC
42+
Machine: EM_X86_64
43+
Sections:
44+
- Name: .note.gnu.build-id
45+
Type: SHT_NOTE
46+
AddressAlign: 0x04
47+
Notes:
48+
- Name: "GNU"
49+
Desc: "abb50d82b6bdc861"
50+
Type: 3
51+
ProgramHeaders:
52+
- Type: PT_NOTE
53+
FileSize: 0xffffffffffffffff
54+
Offset: 0x100
55+
)");
56+
ASSERT_THAT_EXPECTED(ElfOrErr, Succeeded());
57+
BuildIDRef BuildID = getBuildID(&ElfOrErr.get());
58+
EXPECT_EQ(
59+
StringRef(reinterpret_cast<const char *>(BuildID.data()), BuildID.size()),
60+
"\xAB\xB5\x0D\x82\xB6\xBD\xC8\x61");
61+
}

llvm/unittests/Object/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
77

88
add_llvm_unittest(ObjectTests
99
ArchiveTest.cpp
10+
BuildIDTest.cpp
1011
COFFObjectFileTest.cpp
1112
DXContainerTest.cpp
1213
ELFObjectFileTest.cpp

0 commit comments

Comments
 (0)