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 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 26 public: 27 AttributeHeaderParser(ScopedPrinter *printer) 28 : ELFAttributeParser(printer, emptyTagNameMap, "test") {} 29 AttributeHeaderParser() : ELFAttributeParser(emptyTagNameMap, "test") {} 30 }; 31 32 static void testParseError(ArrayRef<uint8_t> bytes, const char *msg) { 33 AttributeHeaderParser parser; 34 Error e = parser.parse(bytes, support::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, "unrecognized format-version: 0x1"); 41 } 42 43 TEST(AttributeHeaderParser, InvalidSectionLength) { 44 static const uint8_t bytes[] = {'A', 3, 0, 0, 0}; 45 testParseError(bytes, "invalid section length 3 at offset 0x1"); 46 } 47 48 TEST(AttributeHeaderParser, UnrecognizedVendorName) { 49 static const uint8_t bytes[] = {'A', 7, 0, 0, 0, 'x', 'y', 0}; 50 testParseError(bytes, "unrecognized vendor-name: xy"); 51 } 52 53 TEST(AttributeHeaderParser, UnrecognizedTag) { 54 static const uint8_t bytes[] = {'A', 14, 0, 0, 0, 't', 'e', 's', 55 't', 0, 4, 5, 0, 0, 0}; 56 testParseError(bytes, "unrecognized tag 0x4 at offset 0xa"); 57 } 58 59 TEST(AttributeHeaderParser, InvalidAttributeSize) { 60 static const uint8_t bytes[] = {'A', 14, 0, 0, 0, 't', 'e', 's', 61 't', 0, 1, 4, 0, 0, 0}; 62 testParseError(bytes, "invalid attribute size 4 at offset 0xa"); 63 } 64