1 | //===----- unittests/ELFAttributeParserTest.cpp ---------------------------===// |
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/Support/ELFAttributeParser.h" |
10 | #include "llvm/Support/ELFAttributes.h" |
11 | #include "gtest/gtest.h" |
12 | #include <string> |
13 | |
14 | using namespace llvm; |
15 | |
16 | static const TagNameMap emptyTagNameMap; |
17 | |
18 | // This class is used to test the common part of the ELF attribute section. |
19 | class : public ELFAttributeParser { |
20 | Error handler(uint64_t tag, bool &handled) override { |
21 | // Treat all attributes as handled. |
22 | handled = true; |
23 | return Error::success(); |
24 | } |
25 | |
26 | public: |
27 | (ScopedPrinter *printer) |
28 | : ELFAttributeParser(printer, emptyTagNameMap, "test" ) {} |
29 | () : ELFAttributeParser(emptyTagNameMap, "test" ) {} |
30 | }; |
31 | |
32 | static void testParseError(ArrayRef<uint8_t> bytes, const char *msg) { |
33 | AttributeHeaderParser parser; |
34 | Error e = parser.parse(section: bytes, endian: llvm::endianness::little); |
35 | EXPECT_STREQ(toString(std::move(e)).c_str(), msg); |
36 | } |
37 | |
38 | TEST(AttributeHeaderParser, UnrecognizedFormatVersion) { |
39 | static const uint8_t bytes[] = {1}; |
40 | testParseError(bytes, msg: "unrecognized format-version: 0x1" ); |
41 | } |
42 | |
43 | TEST(AttributeHeaderParser, InvalidSectionLength) { |
44 | static const uint8_t bytes[] = {'A', 3, 0, 0, 0}; |
45 | testParseError(bytes, msg: "invalid section length 3 at offset 0x1" ); |
46 | } |
47 | |
48 | TEST(AttributeHeaderParser, UnrecognizedTag) { |
49 | static const uint8_t bytes[] = {'A', 14, 0, 0, 0, 't', 'e', 's', |
50 | 't', 0, 4, 5, 0, 0, 0}; |
51 | testParseError(bytes, msg: "unrecognized tag 0x4 at offset 0xa" ); |
52 | } |
53 | |
54 | TEST(AttributeHeaderParser, InvalidAttributeSize) { |
55 | static const uint8_t bytes[] = {'A', 14, 0, 0, 0, 't', 'e', 's', |
56 | 't', 0, 1, 4, 0, 0, 0}; |
57 | testParseError(bytes, msg: "invalid attribute size 4 at offset 0xa" ); |
58 | } |
59 | |