Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | // Copyright (c) 2011 Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | // Original author: Ted Mielczarek <ted.mielczarek@gmail.com> |
michael@0 | 31 | |
michael@0 | 32 | // synth_elf_unittest.cc: |
michael@0 | 33 | // Unittests for google_breakpad::synth_elf::ELF |
michael@0 | 34 | |
michael@0 | 35 | #include <elf.h> |
michael@0 | 36 | |
michael@0 | 37 | #include "breakpad_googletest_includes.h" |
michael@0 | 38 | #include "common/linux/elfutils.h" |
michael@0 | 39 | #include "common/linux/synth_elf.h" |
michael@0 | 40 | #include "common/using_std_string.h" |
michael@0 | 41 | |
michael@0 | 42 | using google_breakpad::ElfClass32; |
michael@0 | 43 | using google_breakpad::ElfClass64; |
michael@0 | 44 | using google_breakpad::synth_elf::ELF; |
michael@0 | 45 | using google_breakpad::synth_elf::Notes; |
michael@0 | 46 | using google_breakpad::synth_elf::Section; |
michael@0 | 47 | using google_breakpad::synth_elf::StringTable; |
michael@0 | 48 | using google_breakpad::synth_elf::SymbolTable; |
michael@0 | 49 | using google_breakpad::test_assembler::Endianness; |
michael@0 | 50 | using google_breakpad::test_assembler::kBigEndian; |
michael@0 | 51 | using google_breakpad::test_assembler::kLittleEndian; |
michael@0 | 52 | using google_breakpad::test_assembler::Label; |
michael@0 | 53 | using ::testing::Test; |
michael@0 | 54 | using ::testing::Types; |
michael@0 | 55 | |
michael@0 | 56 | class StringTableTest : public Test { |
michael@0 | 57 | public: |
michael@0 | 58 | StringTableTest() : table(kLittleEndian) {} |
michael@0 | 59 | |
michael@0 | 60 | StringTable table; |
michael@0 | 61 | }; |
michael@0 | 62 | |
michael@0 | 63 | TEST_F(StringTableTest, Empty) { |
michael@0 | 64 | EXPECT_EQ(1U, table.Size()); |
michael@0 | 65 | string contents; |
michael@0 | 66 | ASSERT_TRUE(table.GetContents(&contents)); |
michael@0 | 67 | const char* kExpectedContents = "\0"; |
michael@0 | 68 | EXPECT_EQ(0, memcmp(kExpectedContents, |
michael@0 | 69 | contents.c_str(), |
michael@0 | 70 | contents.size())); |
michael@0 | 71 | ASSERT_TRUE(table.empty_string.IsKnownConstant()); |
michael@0 | 72 | EXPECT_EQ(0U, table.empty_string.Value()); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | TEST_F(StringTableTest, Basic) { |
michael@0 | 76 | const string s1("table fills with strings"); |
michael@0 | 77 | const string s2("offsets preserved as labels"); |
michael@0 | 78 | const string s3("verified with tests"); |
michael@0 | 79 | const char* kExpectedContents = |
michael@0 | 80 | "\0table fills with strings\0" |
michael@0 | 81 | "offsets preserved as labels\0" |
michael@0 | 82 | "verified with tests\0"; |
michael@0 | 83 | Label l1(table.Add(s1)); |
michael@0 | 84 | Label l2(table.Add(s2)); |
michael@0 | 85 | Label l3(table.Add(s3)); |
michael@0 | 86 | string contents; |
michael@0 | 87 | ASSERT_TRUE(table.GetContents(&contents)); |
michael@0 | 88 | EXPECT_EQ(0, memcmp(kExpectedContents, |
michael@0 | 89 | contents.c_str(), |
michael@0 | 90 | contents.size())); |
michael@0 | 91 | // empty_string is at zero, other strings start at 1. |
michael@0 | 92 | ASSERT_TRUE(l1.IsKnownConstant()); |
michael@0 | 93 | EXPECT_EQ(1U, l1.Value()); |
michael@0 | 94 | // Each string has an extra byte for a trailing null. |
michael@0 | 95 | EXPECT_EQ(1 + s1.length() + 1, l2.Value()); |
michael@0 | 96 | EXPECT_EQ(1 + s1.length() + 1 + s2.length() + 1, l3.Value()); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | TEST_F(StringTableTest, Duplicates) { |
michael@0 | 100 | const string s1("string 1"); |
michael@0 | 101 | const string s2("string 2"); |
michael@0 | 102 | const string s3(""); |
michael@0 | 103 | const char* kExpectedContents = "\0string 1\0string 2\0"; |
michael@0 | 104 | Label l1(table.Add(s1)); |
michael@0 | 105 | Label l2(table.Add(s2)); |
michael@0 | 106 | // Adding strings twice should return the same Label. |
michael@0 | 107 | Label l3(table.Add(s3)); |
michael@0 | 108 | Label l4(table.Add(s2)); |
michael@0 | 109 | string contents; |
michael@0 | 110 | ASSERT_TRUE(table.GetContents(&contents)); |
michael@0 | 111 | EXPECT_EQ(0, memcmp(kExpectedContents, |
michael@0 | 112 | contents.c_str(), |
michael@0 | 113 | contents.size())); |
michael@0 | 114 | EXPECT_EQ(0U, table.empty_string.Value()); |
michael@0 | 115 | EXPECT_EQ(table.empty_string.Value(), l3.Value()); |
michael@0 | 116 | EXPECT_EQ(l2.Value(), l4.Value()); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | class SymbolTableTest : public Test {}; |
michael@0 | 120 | |
michael@0 | 121 | TEST_F(SymbolTableTest, Simple32) { |
michael@0 | 122 | StringTable table(kLittleEndian); |
michael@0 | 123 | SymbolTable syms(kLittleEndian, 4, table); |
michael@0 | 124 | |
michael@0 | 125 | const string kFuncName1 = "superfunc"; |
michael@0 | 126 | const uint32_t kFuncAddr1 = 0x10001000; |
michael@0 | 127 | const uint32_t kFuncSize1 = 0x10; |
michael@0 | 128 | const string kFuncName2 = "awesomefunc"; |
michael@0 | 129 | const uint32_t kFuncAddr2 = 0x20002000; |
michael@0 | 130 | const uint32_t kFuncSize2 = 0x2f; |
michael@0 | 131 | const string kFuncName3 = "megafunc"; |
michael@0 | 132 | const uint32_t kFuncAddr3 = 0x30003000; |
michael@0 | 133 | const uint32_t kFuncSize3 = 0x3c; |
michael@0 | 134 | |
michael@0 | 135 | syms.AddSymbol(kFuncName1, kFuncAddr1, kFuncSize1, |
michael@0 | 136 | ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), |
michael@0 | 137 | SHN_UNDEF + 1); |
michael@0 | 138 | syms.AddSymbol(kFuncName2, kFuncAddr2, kFuncSize2, |
michael@0 | 139 | ELF32_ST_INFO(STB_LOCAL, STT_FUNC), |
michael@0 | 140 | SHN_UNDEF + 2); |
michael@0 | 141 | syms.AddSymbol(kFuncName3, kFuncAddr3, kFuncSize3, |
michael@0 | 142 | ELF32_ST_INFO(STB_LOCAL, STT_FUNC), |
michael@0 | 143 | SHN_UNDEF + 3); |
michael@0 | 144 | |
michael@0 | 145 | const char kExpectedStringTable[] = "\0superfunc\0awesomefunc\0megafunc"; |
michael@0 | 146 | const size_t kExpectedStringTableSize = sizeof(kExpectedStringTable); |
michael@0 | 147 | EXPECT_EQ(kExpectedStringTableSize, table.Size()); |
michael@0 | 148 | string table_contents; |
michael@0 | 149 | table.GetContents(&table_contents); |
michael@0 | 150 | EXPECT_EQ(0, memcmp(kExpectedStringTable, |
michael@0 | 151 | table_contents.c_str(), |
michael@0 | 152 | table_contents.size())); |
michael@0 | 153 | |
michael@0 | 154 | const uint8_t kExpectedSymbolContents[] = { |
michael@0 | 155 | // Symbol 1 |
michael@0 | 156 | 0x01, 0x00, 0x00, 0x00, // name |
michael@0 | 157 | 0x00, 0x10, 0x00, 0x10, // value |
michael@0 | 158 | 0x10, 0x00, 0x00, 0x00, // size |
michael@0 | 159 | ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), // info |
michael@0 | 160 | 0x00, // other |
michael@0 | 161 | 0x01, 0x00, // shndx |
michael@0 | 162 | // Symbol 2 |
michael@0 | 163 | 0x0B, 0x00, 0x00, 0x00, // name |
michael@0 | 164 | 0x00, 0x20, 0x00, 0x20, // value |
michael@0 | 165 | 0x2f, 0x00, 0x00, 0x00, // size |
michael@0 | 166 | ELF32_ST_INFO(STB_LOCAL, STT_FUNC), // info |
michael@0 | 167 | 0x00, // other |
michael@0 | 168 | 0x02, 0x00, // shndx |
michael@0 | 169 | // Symbol 3 |
michael@0 | 170 | 0x17, 0x00, 0x00, 0x00, // name |
michael@0 | 171 | 0x00, 0x30, 0x00, 0x30, // value |
michael@0 | 172 | 0x3c, 0x00, 0x00, 0x00, // size |
michael@0 | 173 | ELF32_ST_INFO(STB_LOCAL, STT_FUNC), // info |
michael@0 | 174 | 0x00, // other |
michael@0 | 175 | 0x03, 0x00, // shndx |
michael@0 | 176 | }; |
michael@0 | 177 | const size_t kExpectedSymbolSize = sizeof(kExpectedSymbolContents); |
michael@0 | 178 | EXPECT_EQ(kExpectedSymbolSize, syms.Size()); |
michael@0 | 179 | |
michael@0 | 180 | string symbol_contents; |
michael@0 | 181 | syms.GetContents(&symbol_contents); |
michael@0 | 182 | EXPECT_EQ(0, memcmp(kExpectedSymbolContents, |
michael@0 | 183 | symbol_contents.c_str(), |
michael@0 | 184 | symbol_contents.size())); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | template<typename ElfClass> |
michael@0 | 188 | class BasicElf : public Test {}; |
michael@0 | 189 | |
michael@0 | 190 | // Doesn't seem worthwhile writing the tests to be endian-independent |
michael@0 | 191 | // when they're unlikely to ever be run on big-endian systems. |
michael@0 | 192 | #if defined(__i386__) || defined(__x86_64__) |
michael@0 | 193 | |
michael@0 | 194 | typedef Types<ElfClass32, ElfClass64> ElfClasses; |
michael@0 | 195 | |
michael@0 | 196 | TYPED_TEST_CASE(BasicElf, ElfClasses); |
michael@0 | 197 | |
michael@0 | 198 | TYPED_TEST(BasicElf, EmptyLE) { |
michael@0 | 199 | typedef typename TypeParam::Ehdr Ehdr; |
michael@0 | 200 | typedef typename TypeParam::Phdr Phdr; |
michael@0 | 201 | typedef typename TypeParam::Shdr Shdr; |
michael@0 | 202 | const size_t kStringTableSize = sizeof("\0.shstrtab"); |
michael@0 | 203 | const size_t kStringTableAlign = 4 - kStringTableSize % 4; |
michael@0 | 204 | const size_t kExpectedSize = sizeof(Ehdr) + |
michael@0 | 205 | // Two sections, SHT_NULL + the section header string table. |
michael@0 | 206 | 2 * sizeof(Shdr) + |
michael@0 | 207 | kStringTableSize + kStringTableAlign; |
michael@0 | 208 | |
michael@0 | 209 | // It doesn't really matter that the machine type is right for the class. |
michael@0 | 210 | ELF elf(EM_386, TypeParam::kClass, kLittleEndian); |
michael@0 | 211 | elf.Finish(); |
michael@0 | 212 | EXPECT_EQ(kExpectedSize, elf.Size()); |
michael@0 | 213 | |
michael@0 | 214 | string contents; |
michael@0 | 215 | ASSERT_TRUE(elf.GetContents(&contents)); |
michael@0 | 216 | ASSERT_EQ(kExpectedSize, contents.size()); |
michael@0 | 217 | const Ehdr* header = |
michael@0 | 218 | reinterpret_cast<const Ehdr*>(contents.data()); |
michael@0 | 219 | const uint8_t kIdent[] = { |
michael@0 | 220 | ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, |
michael@0 | 221 | TypeParam::kClass, ELFDATA2LSB, EV_CURRENT, ELFOSABI_SYSV, |
michael@0 | 222 | 0, 0, 0, 0, 0, 0, 0, 0 |
michael@0 | 223 | }; |
michael@0 | 224 | EXPECT_EQ(0, memcmp(kIdent, header->e_ident, sizeof(kIdent))); |
michael@0 | 225 | EXPECT_EQ(ET_EXEC, header->e_type); |
michael@0 | 226 | EXPECT_EQ(EM_386, header->e_machine); |
michael@0 | 227 | EXPECT_EQ(static_cast<unsigned int>(EV_CURRENT), header->e_version); |
michael@0 | 228 | EXPECT_EQ(0U, header->e_entry); |
michael@0 | 229 | EXPECT_EQ(0U, header->e_phoff); |
michael@0 | 230 | EXPECT_EQ(sizeof(Ehdr) + kStringTableSize + kStringTableAlign, |
michael@0 | 231 | header->e_shoff); |
michael@0 | 232 | EXPECT_EQ(0U, header->e_flags); |
michael@0 | 233 | EXPECT_EQ(sizeof(Ehdr), header->e_ehsize); |
michael@0 | 234 | EXPECT_EQ(sizeof(Phdr), header->e_phentsize); |
michael@0 | 235 | EXPECT_EQ(0, header->e_phnum); |
michael@0 | 236 | EXPECT_EQ(sizeof(Shdr), header->e_shentsize); |
michael@0 | 237 | EXPECT_EQ(2, header->e_shnum); |
michael@0 | 238 | EXPECT_EQ(1, header->e_shstrndx); |
michael@0 | 239 | |
michael@0 | 240 | const Shdr* shdr = |
michael@0 | 241 | reinterpret_cast<const Shdr*>(contents.data() + header->e_shoff); |
michael@0 | 242 | EXPECT_EQ(0U, shdr[0].sh_name); |
michael@0 | 243 | EXPECT_EQ(static_cast<unsigned int>(SHT_NULL), shdr[0].sh_type); |
michael@0 | 244 | EXPECT_EQ(0U, shdr[0].sh_flags); |
michael@0 | 245 | EXPECT_EQ(0U, shdr[0].sh_addr); |
michael@0 | 246 | EXPECT_EQ(0U, shdr[0].sh_offset); |
michael@0 | 247 | EXPECT_EQ(0U, shdr[0].sh_size); |
michael@0 | 248 | EXPECT_EQ(0U, shdr[0].sh_link); |
michael@0 | 249 | EXPECT_EQ(0U, shdr[0].sh_info); |
michael@0 | 250 | EXPECT_EQ(0U, shdr[0].sh_addralign); |
michael@0 | 251 | EXPECT_EQ(0U, shdr[0].sh_entsize); |
michael@0 | 252 | |
michael@0 | 253 | EXPECT_EQ(1U, shdr[1].sh_name); |
michael@0 | 254 | EXPECT_EQ(static_cast<unsigned int>(SHT_STRTAB), shdr[1].sh_type); |
michael@0 | 255 | EXPECT_EQ(0U, shdr[1].sh_flags); |
michael@0 | 256 | EXPECT_EQ(0U, shdr[1].sh_addr); |
michael@0 | 257 | EXPECT_EQ(sizeof(Ehdr), shdr[1].sh_offset); |
michael@0 | 258 | EXPECT_EQ(kStringTableSize, shdr[1].sh_size); |
michael@0 | 259 | EXPECT_EQ(0U, shdr[1].sh_link); |
michael@0 | 260 | EXPECT_EQ(0U, shdr[1].sh_info); |
michael@0 | 261 | EXPECT_EQ(0U, shdr[1].sh_addralign); |
michael@0 | 262 | EXPECT_EQ(0U, shdr[1].sh_entsize); |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | TYPED_TEST(BasicElf, BasicLE) { |
michael@0 | 266 | typedef typename TypeParam::Ehdr Ehdr; |
michael@0 | 267 | typedef typename TypeParam::Phdr Phdr; |
michael@0 | 268 | typedef typename TypeParam::Shdr Shdr; |
michael@0 | 269 | const size_t kStringTableSize = sizeof("\0.text\0.bss\0.shstrtab"); |
michael@0 | 270 | const size_t kStringTableAlign = 4 - kStringTableSize % 4; |
michael@0 | 271 | const size_t kExpectedSize = sizeof(Ehdr) + |
michael@0 | 272 | // Four sections, SHT_NULL + the section header string table + |
michael@0 | 273 | // 4096 bytes of the size-aligned .text section + one program header. |
michael@0 | 274 | sizeof(Phdr) + 4 * sizeof(Shdr) + 4096 + |
michael@0 | 275 | kStringTableSize + kStringTableAlign; |
michael@0 | 276 | |
michael@0 | 277 | // It doesn't really matter that the machine type is right for the class. |
michael@0 | 278 | ELF elf(EM_386, TypeParam::kClass, kLittleEndian); |
michael@0 | 279 | Section text(kLittleEndian); |
michael@0 | 280 | text.Append(4094, 0); |
michael@0 | 281 | int text_idx = elf.AddSection(".text", text, SHT_PROGBITS); |
michael@0 | 282 | Section bss(kLittleEndian); |
michael@0 | 283 | bss.Append(16, 0); |
michael@0 | 284 | int bss_idx = elf.AddSection(".bss", bss, SHT_NOBITS); |
michael@0 | 285 | elf.AddSegment(text_idx, bss_idx, PT_LOAD); |
michael@0 | 286 | elf.Finish(); |
michael@0 | 287 | EXPECT_EQ(kExpectedSize, elf.Size()); |
michael@0 | 288 | |
michael@0 | 289 | string contents; |
michael@0 | 290 | ASSERT_TRUE(elf.GetContents(&contents)); |
michael@0 | 291 | ASSERT_EQ(kExpectedSize, contents.size()); |
michael@0 | 292 | const Ehdr* header = |
michael@0 | 293 | reinterpret_cast<const Ehdr*>(contents.data()); |
michael@0 | 294 | const uint8_t kIdent[] = { |
michael@0 | 295 | ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, |
michael@0 | 296 | TypeParam::kClass, ELFDATA2LSB, EV_CURRENT, ELFOSABI_SYSV, |
michael@0 | 297 | 0, 0, 0, 0, 0, 0, 0, 0 |
michael@0 | 298 | }; |
michael@0 | 299 | EXPECT_EQ(0, memcmp(kIdent, header->e_ident, sizeof(kIdent))); |
michael@0 | 300 | EXPECT_EQ(ET_EXEC, header->e_type); |
michael@0 | 301 | EXPECT_EQ(EM_386, header->e_machine); |
michael@0 | 302 | EXPECT_EQ(static_cast<unsigned int>(EV_CURRENT), header->e_version); |
michael@0 | 303 | EXPECT_EQ(0U, header->e_entry); |
michael@0 | 304 | EXPECT_EQ(sizeof(Ehdr), header->e_phoff); |
michael@0 | 305 | EXPECT_EQ(sizeof(Ehdr) + sizeof(Phdr) + 4096 + kStringTableSize + |
michael@0 | 306 | kStringTableAlign, header->e_shoff); |
michael@0 | 307 | EXPECT_EQ(0U, header->e_flags); |
michael@0 | 308 | EXPECT_EQ(sizeof(Ehdr), header->e_ehsize); |
michael@0 | 309 | EXPECT_EQ(sizeof(Phdr), header->e_phentsize); |
michael@0 | 310 | EXPECT_EQ(1, header->e_phnum); |
michael@0 | 311 | EXPECT_EQ(sizeof(Shdr), header->e_shentsize); |
michael@0 | 312 | EXPECT_EQ(4, header->e_shnum); |
michael@0 | 313 | EXPECT_EQ(3, header->e_shstrndx); |
michael@0 | 314 | |
michael@0 | 315 | const Shdr* shdr = |
michael@0 | 316 | reinterpret_cast<const Shdr*>(contents.data() + header->e_shoff); |
michael@0 | 317 | EXPECT_EQ(0U, shdr[0].sh_name); |
michael@0 | 318 | EXPECT_EQ(static_cast<unsigned int>(SHT_NULL), shdr[0].sh_type); |
michael@0 | 319 | EXPECT_EQ(0U, shdr[0].sh_flags); |
michael@0 | 320 | EXPECT_EQ(0U, shdr[0].sh_addr); |
michael@0 | 321 | EXPECT_EQ(0U, shdr[0].sh_offset); |
michael@0 | 322 | EXPECT_EQ(0U, shdr[0].sh_size); |
michael@0 | 323 | EXPECT_EQ(0U, shdr[0].sh_link); |
michael@0 | 324 | EXPECT_EQ(0U, shdr[0].sh_info); |
michael@0 | 325 | EXPECT_EQ(0U, shdr[0].sh_addralign); |
michael@0 | 326 | EXPECT_EQ(0U, shdr[0].sh_entsize); |
michael@0 | 327 | |
michael@0 | 328 | EXPECT_EQ(1U, shdr[1].sh_name); |
michael@0 | 329 | EXPECT_EQ(static_cast<unsigned int>(SHT_PROGBITS), shdr[1].sh_type); |
michael@0 | 330 | EXPECT_EQ(0U, shdr[1].sh_flags); |
michael@0 | 331 | EXPECT_EQ(0U, shdr[1].sh_addr); |
michael@0 | 332 | EXPECT_EQ(sizeof(Ehdr) + sizeof(Phdr), shdr[1].sh_offset); |
michael@0 | 333 | EXPECT_EQ(4094U, shdr[1].sh_size); |
michael@0 | 334 | EXPECT_EQ(0U, shdr[1].sh_link); |
michael@0 | 335 | EXPECT_EQ(0U, shdr[1].sh_info); |
michael@0 | 336 | EXPECT_EQ(0U, shdr[1].sh_addralign); |
michael@0 | 337 | EXPECT_EQ(0U, shdr[1].sh_entsize); |
michael@0 | 338 | |
michael@0 | 339 | EXPECT_EQ(sizeof("\0.text"), shdr[2].sh_name); |
michael@0 | 340 | EXPECT_EQ(static_cast<unsigned int>(SHT_NOBITS), shdr[2].sh_type); |
michael@0 | 341 | EXPECT_EQ(0U, shdr[2].sh_flags); |
michael@0 | 342 | EXPECT_EQ(0U, shdr[2].sh_addr); |
michael@0 | 343 | EXPECT_EQ(0U, shdr[2].sh_offset); |
michael@0 | 344 | EXPECT_EQ(16U, shdr[2].sh_size); |
michael@0 | 345 | EXPECT_EQ(0U, shdr[2].sh_link); |
michael@0 | 346 | EXPECT_EQ(0U, shdr[2].sh_info); |
michael@0 | 347 | EXPECT_EQ(0U, shdr[2].sh_addralign); |
michael@0 | 348 | EXPECT_EQ(0U, shdr[2].sh_entsize); |
michael@0 | 349 | |
michael@0 | 350 | EXPECT_EQ(sizeof("\0.text\0.bss"), shdr[3].sh_name); |
michael@0 | 351 | EXPECT_EQ(static_cast<unsigned int>(SHT_STRTAB), shdr[3].sh_type); |
michael@0 | 352 | EXPECT_EQ(0U, shdr[3].sh_flags); |
michael@0 | 353 | EXPECT_EQ(0U, shdr[3].sh_addr); |
michael@0 | 354 | EXPECT_EQ(sizeof(Ehdr) + sizeof(Phdr) + 4096, shdr[3].sh_offset); |
michael@0 | 355 | EXPECT_EQ(kStringTableSize, shdr[3].sh_size); |
michael@0 | 356 | EXPECT_EQ(0U, shdr[3].sh_link); |
michael@0 | 357 | EXPECT_EQ(0U, shdr[3].sh_info); |
michael@0 | 358 | EXPECT_EQ(0U, shdr[3].sh_addralign); |
michael@0 | 359 | EXPECT_EQ(0U, shdr[3].sh_entsize); |
michael@0 | 360 | |
michael@0 | 361 | const Phdr* phdr = |
michael@0 | 362 | reinterpret_cast<const Phdr*>(contents.data() + header->e_phoff); |
michael@0 | 363 | EXPECT_EQ(static_cast<unsigned int>(PT_LOAD), phdr->p_type); |
michael@0 | 364 | EXPECT_EQ(sizeof(Ehdr) + sizeof(Phdr), phdr->p_offset); |
michael@0 | 365 | EXPECT_EQ(0U, phdr->p_vaddr); |
michael@0 | 366 | EXPECT_EQ(0U, phdr->p_paddr); |
michael@0 | 367 | EXPECT_EQ(4096U, phdr->p_filesz); |
michael@0 | 368 | EXPECT_EQ(4096U + 16U, phdr->p_memsz); |
michael@0 | 369 | EXPECT_EQ(0U, phdr->p_flags); |
michael@0 | 370 | EXPECT_EQ(0U, phdr->p_align); |
michael@0 | 371 | } |
michael@0 | 372 | |
michael@0 | 373 | class ElfNotesTest : public Test {}; |
michael@0 | 374 | |
michael@0 | 375 | TEST_F(ElfNotesTest, Empty) { |
michael@0 | 376 | Notes notes(kLittleEndian); |
michael@0 | 377 | string contents; |
michael@0 | 378 | ASSERT_TRUE(notes.GetContents(&contents)); |
michael@0 | 379 | EXPECT_EQ(0U, contents.size()); |
michael@0 | 380 | } |
michael@0 | 381 | |
michael@0 | 382 | TEST_F(ElfNotesTest, Notes) { |
michael@0 | 383 | Notes notes(kLittleEndian); |
michael@0 | 384 | notes.AddNote(1, "Linux", reinterpret_cast<const uint8_t *>("\x42\x02\0\0"), |
michael@0 | 385 | 4); |
michael@0 | 386 | notes.AddNote(2, "a", reinterpret_cast<const uint8_t *>("foobar"), |
michael@0 | 387 | sizeof("foobar") - 1); |
michael@0 | 388 | |
michael@0 | 389 | const uint8_t kExpectedNotesContents[] = { |
michael@0 | 390 | // Note 1 |
michael@0 | 391 | 0x06, 0x00, 0x00, 0x00, // name size, including terminating zero |
michael@0 | 392 | 0x04, 0x00, 0x00, 0x00, // desc size |
michael@0 | 393 | 0x01, 0x00, 0x00, 0x00, // type |
michael@0 | 394 | 'L', 'i', 'n', 'u', 'x', 0x00, 0x00, 0x00, // padded "Linux" |
michael@0 | 395 | 0x42, 0x02, 0x00, 0x00, // desc |
michael@0 | 396 | // Note 2 |
michael@0 | 397 | 0x02, 0x00, 0x00, 0x00, // name size |
michael@0 | 398 | 0x06, 0x00, 0x00, 0x00, // desc size |
michael@0 | 399 | 0x02, 0x00, 0x00, 0x00, // type |
michael@0 | 400 | 'a', 0x00, 0x00, 0x00, // padded "a" |
michael@0 | 401 | 'f', 'o', 'o', 'b', 'a', 'r', 0x00, 0x00, // padded "foobar" |
michael@0 | 402 | }; |
michael@0 | 403 | const size_t kExpectedNotesSize = sizeof(kExpectedNotesContents); |
michael@0 | 404 | EXPECT_EQ(kExpectedNotesSize, notes.Size()); |
michael@0 | 405 | |
michael@0 | 406 | string notes_contents; |
michael@0 | 407 | ASSERT_TRUE(notes.GetContents(¬es_contents)); |
michael@0 | 408 | EXPECT_EQ(0, memcmp(kExpectedNotesContents, |
michael@0 | 409 | notes_contents.data(), |
michael@0 | 410 | notes_contents.size())); |
michael@0 | 411 | } |
michael@0 | 412 | |
michael@0 | 413 | #endif // defined(__i386__) || defined(__x86_64__) |