diff --git a/src/common/linux/synth_elf_unittest.cc b/src/common/linux/synth_elf_unittest.cc index 34a00cab..7aa252e3 100644 --- a/src/common/linux/synth_elf_unittest.cc +++ b/src/common/linux/synth_elf_unittest.cc @@ -35,9 +35,12 @@ #include #include "breakpad_googletest_includes.h" +#include "common/linux/elfutils.h" #include "common/linux/synth_elf.h" #include "common/using_std_string.h" +using google_breakpad::ElfClass32; +using google_breakpad::ElfClass64; using google_breakpad::synth_elf::ELF; using google_breakpad::synth_elf::StringTable; using google_breakpad::synth_elf::SymbolTable; @@ -46,6 +49,7 @@ using google_breakpad::test_assembler::kBigEndian; using google_breakpad::test_assembler::kLittleEndian; using google_breakpad::test_assembler::Label; using ::testing::Test; +using ::testing::Types; class StringTableTest : public Test { public: @@ -178,32 +182,41 @@ TEST_F(SymbolTableTest, Simple32) { symbol_contents.size())); } +template class BasicElf : public Test {}; // Doesn't seem worthwhile writing the tests to be endian-independent // when they're unlikely to ever be run on big-endian systems. #if defined(__i386__) || defined(__x86_64__) -TEST_F(BasicElf, EmptyLE32) { +typedef Types ElfClasses; + +TYPED_TEST_CASE(BasicElf, ElfClasses); + +TYPED_TEST(BasicElf, EmptyLE) { + typedef typename TypeParam::Ehdr Ehdr; + typedef typename TypeParam::Phdr Phdr; + typedef typename TypeParam::Shdr Shdr; const size_t kStringTableSize = sizeof("\0.shstrtab"); const size_t kStringTableAlign = 4 - kStringTableSize % 4; - const size_t kExpectedSize = sizeof(Elf32_Ehdr) + + const size_t kExpectedSize = sizeof(Ehdr) + // Two sections, SHT_NULL + the section header string table. - 2 * sizeof(Elf32_Shdr) + + 2 * sizeof(Shdr) + kStringTableSize + kStringTableAlign; - ELF elf(EM_386, ELFCLASS32, kLittleEndian); + // It doesn't really matter that the machine type is right for the class. + ELF elf(EM_386, TypeParam::kClass, kLittleEndian); elf.Finish(); EXPECT_EQ(kExpectedSize, elf.Size()); string contents; ASSERT_TRUE(elf.GetContents(&contents)); ASSERT_EQ(kExpectedSize, contents.size()); - const Elf32_Ehdr* header = - reinterpret_cast(contents.data()); + const Ehdr* header = + reinterpret_cast(contents.data()); const uint8_t kIdent[] = { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, - ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ELFOSABI_SYSV, + TypeParam::kClass, ELFDATA2LSB, EV_CURRENT, ELFOSABI_SYSV, 0, 0, 0, 0, 0, 0, 0, 0 }; EXPECT_EQ(0, memcmp(kIdent, header->e_ident, sizeof(kIdent))); @@ -212,52 +225,13 @@ TEST_F(BasicElf, EmptyLE32) { EXPECT_EQ(static_cast(EV_CURRENT), header->e_version); EXPECT_EQ(0U, header->e_entry); EXPECT_EQ(0U, header->e_phoff); - EXPECT_EQ(sizeof(Elf32_Ehdr) + kStringTableSize + kStringTableAlign, + EXPECT_EQ(sizeof(Ehdr) + kStringTableSize + kStringTableAlign, header->e_shoff); EXPECT_EQ(0U, header->e_flags); - EXPECT_EQ(sizeof(Elf32_Ehdr), header->e_ehsize); - EXPECT_EQ(sizeof(Elf32_Phdr), header->e_phentsize); + EXPECT_EQ(sizeof(Ehdr), header->e_ehsize); + EXPECT_EQ(sizeof(Phdr), header->e_phentsize); EXPECT_EQ(0, header->e_phnum); - EXPECT_EQ(sizeof(Elf32_Shdr), header->e_shentsize); - EXPECT_EQ(2, header->e_shnum); - EXPECT_EQ(1, header->e_shstrndx); -} - -TEST_F(BasicElf, EmptyLE64) { - const size_t kStringTableSize = sizeof("\0.shstrtab"); - const size_t kStringTableAlign = 4 - kStringTableSize % 4; - const size_t kExpectedSize = sizeof(Elf64_Ehdr) + - // Two sections, SHT_NULL + the section header string table. - 2 * sizeof(Elf64_Shdr) + - kStringTableSize + kStringTableAlign; - - ELF elf(EM_X86_64, ELFCLASS64, kLittleEndian); - elf.Finish(); - EXPECT_EQ(kExpectedSize, elf.Size()); - - string contents; - ASSERT_TRUE(elf.GetContents(&contents)); - ASSERT_EQ(kExpectedSize, contents.size()); - const Elf64_Ehdr* header = - reinterpret_cast(contents.data()); - const uint8_t kIdent[] = { - ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, - ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ELFOSABI_SYSV, - 0, 0, 0, 0, 0, 0, 0, 0 - }; - EXPECT_EQ(0, memcmp(kIdent, header->e_ident, sizeof(kIdent))); - EXPECT_EQ(ET_EXEC, header->e_type); - EXPECT_EQ(EM_X86_64, header->e_machine); - EXPECT_EQ(static_cast(EV_CURRENT), header->e_version); - EXPECT_EQ(0U, header->e_entry); - EXPECT_EQ(0U, header->e_phoff); - EXPECT_EQ(sizeof(Elf64_Ehdr) + kStringTableSize + kStringTableAlign, - header->e_shoff); - EXPECT_EQ(0U, header->e_flags); - EXPECT_EQ(sizeof(Elf64_Ehdr), header->e_ehsize); - EXPECT_EQ(sizeof(Elf64_Phdr), header->e_phentsize); - EXPECT_EQ(0, header->e_phnum); - EXPECT_EQ(sizeof(Elf64_Shdr), header->e_shentsize); + EXPECT_EQ(sizeof(Shdr), header->e_shentsize); EXPECT_EQ(2, header->e_shnum); EXPECT_EQ(1, header->e_shstrndx); }