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
14using namespace llvm;
15
16static const TagNameMap emptyTagNameMap;
17
18// This class is used to test the common part of the ELF attribute section.
19class AttributeHeaderParser : 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
26public:
27 AttributeHeaderParser(ScopedPrinter *printer)
28 : ELFAttributeParser(printer, emptyTagNameMap, "test") {}
29 AttributeHeaderParser() : ELFAttributeParser(emptyTagNameMap, "test") {}
30};
31
32static 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
38TEST(AttributeHeaderParser, UnrecognizedFormatVersion) {
39 static const uint8_t bytes[] = {1};
40 testParseError(bytes, msg: "unrecognized format-version: 0x1");
41}
42
43TEST(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
48TEST(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
54TEST(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

source code of llvm/unittests/Support/ELFAttributeParserTest.cpp