| OLD | NEW |
| 1 // Copyright (c) 2010, Google Inc. | 1 // Copyright (c) 2010, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 AutoTempDir temp_dir; | 80 AutoTempDir temp_dir; |
| 81 string templ = temp_dir.path() + "/file-id-unittest"; | 81 string templ = temp_dir.path() + "/file-id-unittest"; |
| 82 char cmdline[4096]; | 82 char cmdline[4096]; |
| 83 sprintf(cmdline, "cp \"%s\" \"%s\"", exe_name, templ.c_str()); | 83 sprintf(cmdline, "cp \"%s\" \"%s\"", exe_name, templ.c_str()); |
| 84 ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline; | 84 ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline; |
| 85 sprintf(cmdline, "chmod u+w \"%s\"", templ.c_str()); | 85 sprintf(cmdline, "chmod u+w \"%s\"", templ.c_str()); |
| 86 ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline; | 86 ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline; |
| 87 sprintf(cmdline, "strip \"%s\"", templ.c_str()); | 87 sprintf(cmdline, "strip \"%s\"", templ.c_str()); |
| 88 ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline; | 88 ASSERT_EQ(0, system(cmdline)) << "Failed to execute: " << cmdline; |
| 89 | 89 |
| 90 uint8_t identifier1[sizeof(MDGUID)]; | 90 uint8_t identifier1[kMaxBuildID]; |
| 91 uint8_t identifier2[sizeof(MDGUID)]; | 91 size_t identifier1_length; |
| 92 uint8_t identifier2[kMaxBuildID]; |
| 93 size_t identifier2_length; |
| 92 FileID fileid1(exe_name); | 94 FileID fileid1(exe_name); |
| 93 EXPECT_TRUE(fileid1.ElfFileIdentifier(identifier1)); | 95 EXPECT_TRUE(fileid1.ElfFileIdentifier(identifier1, &identifier1_length)); |
| 94 FileID fileid2(templ.c_str()); | 96 FileID fileid2(templ.c_str()); |
| 95 EXPECT_TRUE(fileid2.ElfFileIdentifier(identifier2)); | 97 EXPECT_TRUE(fileid2.ElfFileIdentifier(identifier2, &identifier2_length)); |
| 96 char identifier_string1[37]; | 98 char identifier_string1[37]; |
| 97 char identifier_string2[37]; | 99 char identifier_string2[37]; |
| 98 FileID::ConvertIdentifierToString(identifier1, identifier_string1, | 100 FileID::ConvertIdentifierToString(identifier1, identifier_string1, |
| 99 37); | 101 37); |
| 100 FileID::ConvertIdentifierToString(identifier2, identifier_string2, | 102 FileID::ConvertIdentifierToString(identifier2, identifier_string2, |
| 101 37); | 103 37); |
| 102 EXPECT_STREQ(identifier_string1, identifier_string2); | 104 EXPECT_STREQ(identifier_string1, identifier_string2); |
| 103 } | 105 } |
| 104 #endif // !__ANDROID__ | 106 #endif // !__ANDROID__ |
| 105 | 107 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 118 | 120 |
| 119 vector<uint8_t> elfdata_v; | 121 vector<uint8_t> elfdata_v; |
| 120 uint8_t* elfdata; | 122 uint8_t* elfdata; |
| 121 }; | 123 }; |
| 122 | 124 |
| 123 typedef Types<ElfClass32, ElfClass64> ElfClasses; | 125 typedef Types<ElfClass32, ElfClass64> ElfClasses; |
| 124 | 126 |
| 125 TYPED_TEST_CASE(FileIDTest, ElfClasses); | 127 TYPED_TEST_CASE(FileIDTest, ElfClasses); |
| 126 | 128 |
| 127 TYPED_TEST(FileIDTest, ElfClass) { | 129 TYPED_TEST(FileIDTest, ElfClass) { |
| 128 uint8_t identifier[sizeof(MDGUID)]; | 130 uint8_t identifier[kMaxBuildID]; |
| 131 size_t identifier_length; |
| 129 const char expected_identifier_string[] = | 132 const char expected_identifier_string[] = |
| 130 "80808080-8080-0000-0000-008080808080"; | 133 "80808080-8080-0000-0000-008080808080"; |
| 131 char identifier_string[sizeof(expected_identifier_string)]; | 134 char identifier_string[sizeof(expected_identifier_string)]; |
| 132 const size_t kTextSectionSize = 128; | 135 const size_t kTextSectionSize = 128; |
| 133 | 136 |
| 134 ELF elf(EM_386, TypeParam::kClass, kLittleEndian); | 137 ELF elf(EM_386, TypeParam::kClass, kLittleEndian); |
| 135 Section text(kLittleEndian); | 138 Section text(kLittleEndian); |
| 136 for (size_t i = 0; i < kTextSectionSize; ++i) { | 139 for (size_t i = 0; i < kTextSectionSize; ++i) { |
| 137 text.D8(i * 3); | 140 text.D8(i * 3); |
| 138 } | 141 } |
| 139 elf.AddSection(".text", text, SHT_PROGBITS); | 142 elf.AddSection(".text", text, SHT_PROGBITS); |
| 140 elf.Finish(); | 143 elf.Finish(); |
| 141 this->GetElfContents(elf); | 144 this->GetElfContents(elf); |
| 142 | 145 |
| 143 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata, | 146 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata, |
| 144 identifier)); | 147 identifier, |
| 148 &identifier_length)); |
| 145 | 149 |
| 146 FileID::ConvertIdentifierToString(identifier, identifier_string, | 150 FileID::ConvertIdentifierToString(identifier, identifier_string, |
| 147 sizeof(identifier_string)); | 151 sizeof(identifier_string)); |
| 148 EXPECT_STREQ(expected_identifier_string, identifier_string); | 152 EXPECT_STREQ(expected_identifier_string, identifier_string); |
| 149 } | 153 } |
| 150 | 154 |
| 151 TYPED_TEST(FileIDTest, BuildID) { | 155 TYPED_TEST(FileIDTest, BuildID) { |
| 152 const uint8_t kExpectedIdentifier[sizeof(MDGUID)] = | 156 const uint8_t kExpectedIdentifier[kMaxBuildID] = |
| 153 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 157 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 154 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; | 158 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 159 0x10, 0x11, 0x12, 0x13}; |
| 155 char expected_identifier_string[] = | 160 char expected_identifier_string[] = |
| 156 "00000000-0000-0000-0000-000000000000"; | 161 "00000000-0000-0000-0000-000000000000"; |
| 157 FileID::ConvertIdentifierToString(kExpectedIdentifier, | 162 FileID::ConvertIdentifierToString(kExpectedIdentifier, |
| 158 expected_identifier_string, | 163 expected_identifier_string, |
| 159 sizeof(expected_identifier_string)); | 164 sizeof(expected_identifier_string)); |
| 160 | 165 |
| 161 uint8_t identifier[sizeof(MDGUID)]; | 166 uint8_t identifier[kMaxBuildID]; |
| 167 size_t identifier_length; |
| 162 char identifier_string[sizeof(expected_identifier_string)]; | 168 char identifier_string[sizeof(expected_identifier_string)]; |
| 163 | 169 |
| 164 ELF elf(EM_386, TypeParam::kClass, kLittleEndian); | 170 ELF elf(EM_386, TypeParam::kClass, kLittleEndian); |
| 165 Section text(kLittleEndian); | 171 Section text(kLittleEndian); |
| 166 text.Append(4096, 0); | 172 text.Append(4096, 0); |
| 167 elf.AddSection(".text", text, SHT_PROGBITS); | 173 elf.AddSection(".text", text, SHT_PROGBITS); |
| 168 Notes notes(kLittleEndian); | 174 Notes notes(kLittleEndian); |
| 169 notes.AddNote(NT_GNU_BUILD_ID, "GNU", kExpectedIdentifier, | 175 notes.AddNote(NT_GNU_BUILD_ID, "GNU", kExpectedIdentifier, |
| 170 sizeof(kExpectedIdentifier)); | 176 sizeof(kExpectedIdentifier)); |
| 171 elf.AddSection(".note.gnu.build-id", notes, SHT_NOTE); | 177 elf.AddSection(".note.gnu.build-id", notes, SHT_NOTE); |
| 172 elf.Finish(); | 178 elf.Finish(); |
| 173 this->GetElfContents(elf); | 179 this->GetElfContents(elf); |
| 174 | 180 |
| 175 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata, | 181 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata, |
| 176 identifier)); | 182 identifier, |
| 183 &identifier_length)); |
| 184 EXPECT_EQ(sizeof(kExpectedIdentifier), identifier_length); |
| 177 | 185 |
| 178 FileID::ConvertIdentifierToString(identifier, identifier_string, | 186 FileID::ConvertIdentifierToString(identifier, identifier_string, |
| 179 sizeof(identifier_string)); | 187 sizeof(identifier_string)); |
| 180 EXPECT_STREQ(expected_identifier_string, identifier_string); | 188 EXPECT_STREQ(expected_identifier_string, identifier_string); |
| 181 } | 189 } |
| 182 | 190 |
| 183 TYPED_TEST(FileIDTest, BuildIDPH) { | 191 TYPED_TEST(FileIDTest, BuildIDPH) { |
| 184 const uint8_t kExpectedIdentifier[sizeof(MDGUID)] = | 192 const uint8_t kExpectedIdentifier[kMaxBuildID] = |
| 185 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 193 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 186 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; | 194 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 195 0x10, 0x11, 0x12, 0x13}; |
| 187 char expected_identifier_string[] = | 196 char expected_identifier_string[] = |
| 188 "00000000-0000-0000-0000-000000000000"; | 197 "00000000-0000-0000-0000-000000000000"; |
| 189 FileID::ConvertIdentifierToString(kExpectedIdentifier, | 198 FileID::ConvertIdentifierToString(kExpectedIdentifier, |
| 190 expected_identifier_string, | 199 expected_identifier_string, |
| 191 sizeof(expected_identifier_string)); | 200 sizeof(expected_identifier_string)); |
| 192 | 201 |
| 193 uint8_t identifier[sizeof(MDGUID)]; | 202 uint8_t identifier[kMaxBuildID]; |
| 203 size_t identifier_length; |
| 194 char identifier_string[sizeof(expected_identifier_string)]; | 204 char identifier_string[sizeof(expected_identifier_string)]; |
| 195 | 205 |
| 196 ELF elf(EM_386, TypeParam::kClass, kLittleEndian); | 206 ELF elf(EM_386, TypeParam::kClass, kLittleEndian); |
| 197 Section text(kLittleEndian); | 207 Section text(kLittleEndian); |
| 198 text.Append(4096, 0); | 208 text.Append(4096, 0); |
| 199 elf.AddSection(".text", text, SHT_PROGBITS); | 209 elf.AddSection(".text", text, SHT_PROGBITS); |
| 200 Notes notes(kLittleEndian); | 210 Notes notes(kLittleEndian); |
| 201 notes.AddNote(0, "Linux", | 211 notes.AddNote(0, "Linux", |
| 202 reinterpret_cast<const uint8_t *>("\0x42\0x02\0\0"), 4); | 212 reinterpret_cast<const uint8_t *>("\0x42\0x02\0\0"), 4); |
| 203 notes.AddNote(NT_GNU_BUILD_ID, "GNU", kExpectedIdentifier, | 213 notes.AddNote(NT_GNU_BUILD_ID, "GNU", kExpectedIdentifier, |
| 204 sizeof(kExpectedIdentifier)); | 214 sizeof(kExpectedIdentifier)); |
| 205 int note_idx = elf.AddSection(".note", notes, SHT_NOTE); | 215 int note_idx = elf.AddSection(".note", notes, SHT_NOTE); |
| 206 elf.AddSegment(note_idx, note_idx, PT_NOTE); | 216 elf.AddSegment(note_idx, note_idx, PT_NOTE); |
| 207 elf.Finish(); | 217 elf.Finish(); |
| 208 this->GetElfContents(elf); | 218 this->GetElfContents(elf); |
| 209 | 219 |
| 210 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata, | 220 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata, |
| 211 identifier)); | 221 identifier, |
| 222 &identifier_length)); |
| 223 EXPECT_EQ(sizeof(kExpectedIdentifier), identifier_length); |
| 212 | 224 |
| 213 FileID::ConvertIdentifierToString(identifier, identifier_string, | 225 FileID::ConvertIdentifierToString(identifier, identifier_string, |
| 214 sizeof(identifier_string)); | 226 sizeof(identifier_string)); |
| 215 EXPECT_STREQ(expected_identifier_string, identifier_string); | 227 EXPECT_STREQ(expected_identifier_string, identifier_string); |
| 216 } | 228 } |
| 217 | 229 |
| 218 // Test to make sure two files with different text sections produce | 230 // Test to make sure two files with different text sections produce |
| 219 // different hashes when not using a build id. | 231 // different hashes when not using a build id. |
| 220 TYPED_TEST(FileIDTest, UniqueHashes) { | 232 TYPED_TEST(FileIDTest, UniqueHashes) { |
| 221 char identifier_string_1[] = | 233 char identifier_string_1[] = |
| 222 "00000000-0000-0000-0000-000000000000"; | 234 "00000000-0000-0000-0000-000000000000"; |
| 223 char identifier_string_2[] = | 235 char identifier_string_2[] = |
| 224 "00000000-0000-0000-0000-000000000000"; | 236 "00000000-0000-0000-0000-000000000000"; |
| 225 uint8_t identifier_1[sizeof(MDGUID)]; | 237 uint8_t identifier_1[kMaxBuildID]; |
| 226 uint8_t identifier_2[sizeof(MDGUID)]; | 238 size_t identifier_1_size; |
| 239 uint8_t identifier_2[kMaxBuildID]; |
| 240 size_t identifier_2_size; |
| 227 | 241 |
| 228 { | 242 { |
| 229 ELF elf1(EM_386, TypeParam::kClass, kLittleEndian); | 243 ELF elf1(EM_386, TypeParam::kClass, kLittleEndian); |
| 230 Section foo_1(kLittleEndian); | 244 Section foo_1(kLittleEndian); |
| 231 PopulateSection(&foo_1, 32, 5); | 245 PopulateSection(&foo_1, 32, 5); |
| 232 elf1.AddSection(".foo", foo_1, SHT_PROGBITS); | 246 elf1.AddSection(".foo", foo_1, SHT_PROGBITS); |
| 233 Section text_1(kLittleEndian); | 247 Section text_1(kLittleEndian); |
| 234 PopulateSection(&text_1, 4096, 17); | 248 PopulateSection(&text_1, 4096, 17); |
| 235 elf1.AddSection(".text", text_1, SHT_PROGBITS); | 249 elf1.AddSection(".text", text_1, SHT_PROGBITS); |
| 236 elf1.Finish(); | 250 elf1.Finish(); |
| 237 this->GetElfContents(elf1); | 251 this->GetElfContents(elf1); |
| 238 } | 252 } |
| 239 | 253 |
| 240 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata, | 254 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata, |
| 241 identifier_1)); | 255 identifier_1, |
| 256 &identifier_1_size)); |
| 242 FileID::ConvertIdentifierToString(identifier_1, identifier_string_1, | 257 FileID::ConvertIdentifierToString(identifier_1, identifier_string_1, |
| 243 sizeof(identifier_string_1)); | 258 sizeof(identifier_string_1)); |
| 244 | 259 |
| 245 { | 260 { |
| 246 ELF elf2(EM_386, TypeParam::kClass, kLittleEndian); | 261 ELF elf2(EM_386, TypeParam::kClass, kLittleEndian); |
| 247 Section text_2(kLittleEndian); | 262 Section text_2(kLittleEndian); |
| 248 Section foo_2(kLittleEndian); | 263 Section foo_2(kLittleEndian); |
| 249 PopulateSection(&foo_2, 32, 5); | 264 PopulateSection(&foo_2, 32, 5); |
| 250 elf2.AddSection(".foo", foo_2, SHT_PROGBITS); | 265 elf2.AddSection(".foo", foo_2, SHT_PROGBITS); |
| 251 PopulateSection(&text_2, 4096, 31); | 266 PopulateSection(&text_2, 4096, 31); |
| 252 elf2.AddSection(".text", text_2, SHT_PROGBITS); | 267 elf2.AddSection(".text", text_2, SHT_PROGBITS); |
| 253 elf2.Finish(); | 268 elf2.Finish(); |
| 254 this->GetElfContents(elf2); | 269 this->GetElfContents(elf2); |
| 255 } | 270 } |
| 256 | 271 |
| 257 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata, | 272 EXPECT_TRUE(FileID::ElfFileIdentifierFromMappedFile(this->elfdata, |
| 258 identifier_2)); | 273 identifier_2, |
| 274 &identifier_2_size)); |
| 259 FileID::ConvertIdentifierToString(identifier_2, identifier_string_2, | 275 FileID::ConvertIdentifierToString(identifier_2, identifier_string_2, |
| 260 sizeof(identifier_string_2)); | 276 sizeof(identifier_string_2)); |
| 261 | 277 |
| 262 EXPECT_STRNE(identifier_string_1, identifier_string_2); | 278 EXPECT_STRNE(identifier_string_1, identifier_string_2); |
| 263 } | 279 } |
| OLD | NEW |