xref: /llvm-project/llvm/unittests/Support/ELFAttributeParserTest.cpp (revision 4a0ccfa865437fe29ef2ecb18152df7694dddb7f)
1581ba352SKai Wang //===----- unittests/ELFAttributeParserTest.cpp ---------------------------===//
2581ba352SKai Wang //
3581ba352SKai Wang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4581ba352SKai Wang // See https://llvm.org/LICENSE.txt for license information.
5581ba352SKai Wang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6581ba352SKai Wang //
7581ba352SKai Wang //===----------------------------------------------------------------------===//
8581ba352SKai Wang 
9581ba352SKai Wang #include "llvm/Support/ELFAttributeParser.h"
10581ba352SKai Wang #include "llvm/Support/ELFAttributes.h"
11581ba352SKai Wang #include "gtest/gtest.h"
12581ba352SKai Wang #include <string>
13581ba352SKai Wang 
14581ba352SKai Wang using namespace llvm;
15581ba352SKai Wang 
16581ba352SKai Wang static const TagNameMap emptyTagNameMap;
17581ba352SKai Wang 
18581ba352SKai Wang // This class is used to test the common part of the ELF attribute section.
19581ba352SKai Wang class AttributeHeaderParser : public ELFAttributeParser {
handler(uint64_t tag,bool & handled)2031eb8349SLogan Smith   Error handler(uint64_t tag, bool &handled) override {
21581ba352SKai Wang     // Treat all attributes as handled.
22581ba352SKai Wang     handled = true;
23581ba352SKai Wang     return Error::success();
24581ba352SKai Wang   }
25581ba352SKai Wang 
26581ba352SKai Wang public:
AttributeHeaderParser(ScopedPrinter * printer)27581ba352SKai Wang   AttributeHeaderParser(ScopedPrinter *printer)
28581ba352SKai Wang       : ELFAttributeParser(printer, emptyTagNameMap, "test") {}
AttributeHeaderParser()29581ba352SKai Wang   AttributeHeaderParser() : ELFAttributeParser(emptyTagNameMap, "test") {}
30581ba352SKai Wang };
31581ba352SKai Wang 
testParseError(ArrayRef<uint8_t> bytes,const char * msg)32581ba352SKai Wang static void testParseError(ArrayRef<uint8_t> bytes, const char *msg) {
33581ba352SKai Wang   AttributeHeaderParser parser;
34*4a0ccfa8SKazu Hirata   Error e = parser.parse(bytes, llvm::endianness::little);
35581ba352SKai Wang   EXPECT_STREQ(toString(std::move(e)).c_str(), msg);
36581ba352SKai Wang }
37581ba352SKai Wang 
TEST(AttributeHeaderParser,UnrecognizedFormatVersion)38581ba352SKai Wang TEST(AttributeHeaderParser, UnrecognizedFormatVersion) {
39581ba352SKai Wang   static const uint8_t bytes[] = {1};
40581ba352SKai Wang   testParseError(bytes, "unrecognized format-version: 0x1");
41581ba352SKai Wang }
42581ba352SKai Wang 
TEST(AttributeHeaderParser,InvalidSectionLength)43501522b5SKai Wang TEST(AttributeHeaderParser, InvalidSectionLength) {
44581ba352SKai Wang   static const uint8_t bytes[] = {'A', 3, 0, 0, 0};
45501522b5SKai Wang   testParseError(bytes, "invalid section length 3 at offset 0x1");
46581ba352SKai Wang }
47581ba352SKai Wang 
TEST(AttributeHeaderParser,UnrecognizedTag)48581ba352SKai Wang TEST(AttributeHeaderParser, UnrecognizedTag) {
49581ba352SKai Wang   static const uint8_t bytes[] = {'A', 14, 0, 0, 0, 't', 'e', 's',
50581ba352SKai Wang                                   't', 0,  4, 5, 0, 0,   0};
51581ba352SKai Wang   testParseError(bytes, "unrecognized tag 0x4 at offset 0xa");
52581ba352SKai Wang }
53581ba352SKai Wang 
TEST(AttributeHeaderParser,InvalidAttributeSize)54581ba352SKai Wang TEST(AttributeHeaderParser, InvalidAttributeSize) {
55581ba352SKai Wang   static const uint8_t bytes[] = {'A', 14, 0, 0, 0, 't', 'e', 's',
56581ba352SKai Wang                                   't', 0,  1, 4, 0, 0,   0};
57581ba352SKai Wang   testParseError(bytes, "invalid attribute size 4 at offset 0xa");
58581ba352SKai Wang }
59