Refactor BasicElf synth_elf unitest

A=Mike Hommey <mh@glandium.org>
R=ted at https://breakpad.appspot.com/544002/

git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@1136 4c0a9323-5329-0410-9bdc-e9ce6186880e
This commit is contained in:
ted.mielczarek@gmail.com 2013-03-29 15:06:37 +00:00
parent 637c392d1c
commit 8de2cd2d22

View file

@ -35,9 +35,12 @@
#include <elf.h>
#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<typename ElfClass>
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<ElfClass32, ElfClass64> 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<const Elf32_Ehdr*>(contents.data());
const Ehdr* header =
reinterpret_cast<const Ehdr*>(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<unsigned int>(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<const Elf64_Ehdr*>(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<unsigned int>(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);
}