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+ }
0 commit comments