toolkit/crashreporter/google-breakpad/src/common/linux/file_id_unittest.cc

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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) 2010, 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 // Unit tests for FileID
michael@0 31
michael@0 32 #include <elf.h>
michael@0 33 #include <stdlib.h>
michael@0 34
michael@0 35 #include <string>
michael@0 36
michael@0 37 #include "common/linux/elfutils.h"
michael@0 38 #include "common/linux/file_id.h"
michael@0 39 #include "common/linux/safe_readlink.h"
michael@0 40 #include "common/linux/synth_elf.h"
michael@0 41 #include "common/test_assembler.h"
michael@0 42 #include "common/tests/auto_tempdir.h"
michael@0 43 #include "common/using_std_string.h"
michael@0 44 #include "breakpad_googletest_includes.h"
michael@0 45
michael@0 46 using namespace google_breakpad;
michael@0 47 using google_breakpad::ElfClass32;
michael@0 48 using google_breakpad::ElfClass64;
michael@0 49 using google_breakpad::SafeReadLink;
michael@0 50 using google_breakpad::synth_elf::ELF;
michael@0 51 using google_breakpad::synth_elf::Notes;
michael@0 52 using google_breakpad::test_assembler::kLittleEndian;
michael@0 53 using google_breakpad::test_assembler::Section;
michael@0 54 using ::testing::Types;
michael@0 55
michael@0 56 namespace {
michael@0 57
michael@0 58 // Simply calling Section::Append(size, byte) produces a uninteresting pattern
michael@0 59 // that tends to get hashed to 0000...0000. This populates the section with
michael@0 60 // data to produce better hashes.
michael@0 61 void PopulateSection(Section* section, int size, int prime_number) {
michael@0 62 for (int i = 0; i < size; i++)
michael@0 63 section->Append(1, (i % prime_number) % 256);
michael@0 64 }
michael@0 65
michael@0 66 } // namespace
michael@0 67
michael@0 68 TEST(FileIDStripTest, StripSelf) {
michael@0 69 // Calculate the File ID of this binary using
michael@0 70 // FileID::ElfFileIdentifier, then make a copy of this binary,
michael@0 71 // strip it, and ensure that the result is the same.
michael@0 72 char exe_name[PATH_MAX];
michael@0 73 ASSERT_TRUE(SafeReadLink("/proc/self/exe", exe_name));
michael@0 74
michael@0 75 // copy our binary to a temp file, and strip it
michael@0 76 AutoTempDir temp_dir;
michael@0 77 string templ = temp_dir.path() + "/file-id-unittest";
michael@0 78 char cmdline[4096];
michael@0 79 sprintf(cmdline, "cp \"%s\" \"%s\"", exe_name, templ.c_str());
michael@0 80 ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline;
michael@0 81 sprintf(cmdline, "chmod u+w \"%s\"", templ.c_str());
michael@0 82 ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline;
michael@0 83 sprintf(cmdline, "strip \"%s\"", templ.c_str());
michael@0 84 ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline;
michael@0 85
michael@0 86 uint8_t identifier1[sizeof(MDGUID)];
michael@0 87 uint8_t identifier2[sizeof(MDGUID)];
michael@0 88 FileID fileid1(exe_name);
michael@0 89 EXPECT_TRUE(fileid1.ElfFileIdentifier(identifier1));
michael@0 90 FileID fileid2(templ.c_str());
michael@0 91 EXPECT_TRUE(fileid2.ElfFileIdentifier(identifier2));
michael@0 92 char identifier_string1[37];
michael@0 93 char identifier_string2[37];
michael@0 94 FileID::ConvertIdentifierToString(identifier1, identifier_string1,
michael@0 95 37);
michael@0 96 FileID::ConvertIdentifierToString(identifier2, identifier_string2,
michael@0 97 37);
michael@0 98 EXPECT_STREQ(identifier_string1, identifier_string2);
michael@0 99 }
michael@0 100
michael@0 101 template<typename ElfClass>
michael@0 102 class FileIDTest : public testing::Test {
michael@0 103 public:
michael@0 104 void GetElfContents(ELF& elf) {
michael@0 105 string contents;
michael@0 106 ASSERT_TRUE(elf.GetContents(&contents));
michael@0 107 ASSERT_LT(0U, contents.size());
michael@0 108
michael@0 109 elfdata_v.clear();
michael@0 110 elfdata_v.insert(elfdata_v.begin(), contents.begin(), contents.end());
michael@0 111 elfdata = &elfdata_v[0];
michael@0 112 }
michael@0 113
michael@0 114 vector<uint8_t> elfdata_v;
michael@0 115 uint8_t* elfdata;
michael@0 116 };
michael@0 117
michael@0 118 typedef Types<ElfClass32, ElfClass64> ElfClasses;
michael@0 119
michael@0 120 TYPED_TEST_CASE(FileIDTest, ElfClasses);
michael@0 121
michael@0 122 TYPED_TEST(FileIDTest, ElfClass) {
michael@0 123 uint8_t identifier[sizeof(MDGUID)];
michael@0 124 const char expected_identifier_string[] =
michael@0 125 "80808080-8080-0000-0000-008080808080";
michael@0 126 char identifier_string[sizeof(expected_identifier_string)];
michael@0 127 const size_t kTextSectionSize = 128;
michael@0 128
michael@0 129 ELF elf(EM_386, TypeParam::kClass, kLittleEndian);
michael@0 130 Section text(kLittleEndian);
michael@0 131 for (size_t i = 0; i < kTextSectionSize; ++i) {
michael@0 132 text.D8(i * 3);
michael@0 133 }
michael@0 134 elf.AddSection(".text", text, SHT_PROGBITS);
michael@0 135 elf.Finish();
michael@0 136 this->GetElfContents(elf);
michael@0 137
michael@0 138 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata,
michael@0 139 identifier));
michael@0 140
michael@0 141 FileID::ConvertIdentifierToString(identifier, identifier_string,
michael@0 142 sizeof(identifier_string));
michael@0 143 EXPECT_STREQ(expected_identifier_string, identifier_string);
michael@0 144 }
michael@0 145
michael@0 146 TYPED_TEST(FileIDTest, BuildID) {
michael@0 147 const uint8_t kExpectedIdentifier[sizeof(MDGUID)] =
michael@0 148 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
michael@0 149 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
michael@0 150 char expected_identifier_string[] =
michael@0 151 "00000000-0000-0000-0000-000000000000";
michael@0 152 FileID::ConvertIdentifierToString(kExpectedIdentifier,
michael@0 153 expected_identifier_string,
michael@0 154 sizeof(expected_identifier_string));
michael@0 155
michael@0 156 uint8_t identifier[sizeof(MDGUID)];
michael@0 157 char identifier_string[sizeof(expected_identifier_string)];
michael@0 158
michael@0 159 ELF elf(EM_386, TypeParam::kClass, kLittleEndian);
michael@0 160 Section text(kLittleEndian);
michael@0 161 text.Append(4096, 0);
michael@0 162 elf.AddSection(".text", text, SHT_PROGBITS);
michael@0 163 Notes notes(kLittleEndian);
michael@0 164 notes.AddNote(NT_GNU_BUILD_ID, "GNU", kExpectedIdentifier,
michael@0 165 sizeof(kExpectedIdentifier));
michael@0 166 elf.AddSection(".note.gnu.build-id", notes, SHT_NOTE);
michael@0 167 elf.Finish();
michael@0 168 this->GetElfContents(elf);
michael@0 169
michael@0 170 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata,
michael@0 171 identifier));
michael@0 172
michael@0 173 FileID::ConvertIdentifierToString(identifier, identifier_string,
michael@0 174 sizeof(identifier_string));
michael@0 175 EXPECT_STREQ(expected_identifier_string, identifier_string);
michael@0 176 }
michael@0 177
michael@0 178 TYPED_TEST(FileIDTest, BuildIDPH) {
michael@0 179 const uint8_t kExpectedIdentifier[sizeof(MDGUID)] =
michael@0 180 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
michael@0 181 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
michael@0 182 char expected_identifier_string[] =
michael@0 183 "00000000-0000-0000-0000-000000000000";
michael@0 184 FileID::ConvertIdentifierToString(kExpectedIdentifier,
michael@0 185 expected_identifier_string,
michael@0 186 sizeof(expected_identifier_string));
michael@0 187
michael@0 188 uint8_t identifier[sizeof(MDGUID)];
michael@0 189 char identifier_string[sizeof(expected_identifier_string)];
michael@0 190
michael@0 191 ELF elf(EM_386, TypeParam::kClass, kLittleEndian);
michael@0 192 Section text(kLittleEndian);
michael@0 193 text.Append(4096, 0);
michael@0 194 elf.AddSection(".text", text, SHT_PROGBITS);
michael@0 195 Notes notes(kLittleEndian);
michael@0 196 notes.AddNote(0, "Linux",
michael@0 197 reinterpret_cast<const uint8_t *>("\0x42\0x02\0\0"), 4);
michael@0 198 notes.AddNote(NT_GNU_BUILD_ID, "GNU", kExpectedIdentifier,
michael@0 199 sizeof(kExpectedIdentifier));
michael@0 200 int note_idx = elf.AddSection(".note", notes, SHT_NOTE);
michael@0 201 elf.AddSegment(note_idx, note_idx, PT_NOTE);
michael@0 202 elf.Finish();
michael@0 203 this->GetElfContents(elf);
michael@0 204
michael@0 205 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata,
michael@0 206 identifier));
michael@0 207
michael@0 208 FileID::ConvertIdentifierToString(identifier, identifier_string,
michael@0 209 sizeof(identifier_string));
michael@0 210 EXPECT_STREQ(expected_identifier_string, identifier_string);
michael@0 211 }
michael@0 212
michael@0 213 // Test to make sure two files with different text sections produce
michael@0 214 // different hashes when not using a build id.
michael@0 215 TYPED_TEST(FileIDTest, UniqueHashes) {
michael@0 216 char identifier_string_1[] =
michael@0 217 "00000000-0000-0000-0000-000000000000";
michael@0 218 char identifier_string_2[] =
michael@0 219 "00000000-0000-0000-0000-000000000000";
michael@0 220 uint8_t identifier_1[sizeof(MDGUID)];
michael@0 221 uint8_t identifier_2[sizeof(MDGUID)];
michael@0 222
michael@0 223 {
michael@0 224 ELF elf1(EM_386, TypeParam::kClass, kLittleEndian);
michael@0 225 Section foo_1(kLittleEndian);
michael@0 226 PopulateSection(&foo_1, 32, 5);
michael@0 227 elf1.AddSection(".foo", foo_1, SHT_PROGBITS);
michael@0 228 Section text_1(kLittleEndian);
michael@0 229 PopulateSection(&text_1, 4096, 17);
michael@0 230 elf1.AddSection(".text", text_1, SHT_PROGBITS);
michael@0 231 elf1.Finish();
michael@0 232 this->GetElfContents(elf1);
michael@0 233 }
michael@0 234
michael@0 235 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata,
michael@0 236 identifier_1));
michael@0 237 FileID::ConvertIdentifierToString(identifier_1, identifier_string_1,
michael@0 238 sizeof(identifier_string_1));
michael@0 239
michael@0 240 {
michael@0 241 ELF elf2(EM_386, TypeParam::kClass, kLittleEndian);
michael@0 242 Section text_2(kLittleEndian);
michael@0 243 Section foo_2(kLittleEndian);
michael@0 244 PopulateSection(&foo_2, 32, 5);
michael@0 245 elf2.AddSection(".foo", foo_2, SHT_PROGBITS);
michael@0 246 PopulateSection(&text_2, 4096, 31);
michael@0 247 elf2.AddSection(".text", text_2, SHT_PROGBITS);
michael@0 248 elf2.Finish();
michael@0 249 this->GetElfContents(elf2);
michael@0 250 }
michael@0 251
michael@0 252 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata,
michael@0 253 identifier_2));
michael@0 254 FileID::ConvertIdentifierToString(identifier_2, identifier_string_2,
michael@0 255 sizeof(identifier_string_2));
michael@0 256
michael@0 257 EXPECT_STRNE(identifier_string_1, identifier_string_2);
michael@0 258 }

mercurial