Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Set to 1 to enable the dummy test that dumps test data to /tmp. | |
| 6 #define DEBUG_TEST_DATA 0 | |
| 7 | |
| 8 #include "relocation_packer_elf_file.h" | |
| 9 | |
| 10 #include <stdio.h> | |
| 11 #include <vector> | |
| 12 #include "relocation_packer_debug.h" | |
| 13 #include "relocation_packer_elf_file_unittest_relocs.so.h" | |
| 14 #include "relocation_packer_elf_file_unittest_relocs_golden.so.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 FILE* MaterializeRelocsTestFile(unsigned char* data, size_t length) { | |
| 20 FILE* temporary = tmpfile(); | |
| 21 if (temporary) | |
| 22 fwrite(data, 1, length, temporary); | |
| 23 return temporary; | |
| 24 } | |
| 25 | |
| 26 bool MaterializeRelocsTestFiles(FILE** relocs_so, FILE** packed_relocs_so) { | |
| 27 *relocs_so = MaterializeRelocsTestFile( | |
| 28 relocation_packer_elf_file_unittest_relocs_so, | |
| 29 relocation_packer_elf_file_unittest_relocs_so_len); | |
| 30 | |
| 31 *packed_relocs_so = MaterializeRelocsTestFile( | |
| 32 relocation_packer_elf_file_unittest_relocs_golden_so, | |
| 33 relocation_packer_elf_file_unittest_relocs_golden_so_len); | |
| 34 | |
| 35 return *relocs_so && *packed_relocs_so; | |
|
rmcilroy
2014/06/02 15:16:35
Couldn't you just include these both as binary fil
simonb (inactive)
2014/06/04 16:40:35
Done.
| |
| 36 } | |
| 37 | |
| 38 void DematerializeRelocsTestFile(FILE* temporary) { | |
| 39 fclose(temporary); | |
|
rmcilroy
2014/06/02 15:16:35
delete the temp files?
simonb (inactive)
2014/06/04 16:40:35
No need. From tmpfile(1): "The file will be autom
| |
| 40 } | |
| 41 | |
| 42 void DematerializeRelocsTestFiles(FILE* relocs_so, FILE* packed_relocs_so) { | |
| 43 DematerializeRelocsTestFile(relocs_so); | |
| 44 DematerializeRelocsTestFile(packed_relocs_so); | |
| 45 } | |
| 46 | |
| 47 bool FileContentsEqual(FILE* first, FILE* second) { | |
| 48 if (fseek(first, 0, SEEK_SET) || fseek(second, 0, SEEK_SET)) | |
| 49 return false; | |
| 50 | |
| 51 static const size_t buffer_size = 4096; | |
| 52 unsigned char first_buffer[buffer_size]; | |
| 53 unsigned char second_buffer[buffer_size]; | |
| 54 | |
| 55 do { | |
| 56 size_t first_read = fread(first_buffer, 1, sizeof(first_buffer), first); | |
| 57 size_t second_read = fread(second_buffer, 1, sizeof(second_buffer), second); | |
| 58 | |
| 59 if (first_read != second_read) | |
| 60 return false; | |
| 61 if (memcmp(first_buffer, second_buffer, first_read)) | |
| 62 return false; | |
| 63 } while (!feof(first) && !feof(second)); | |
| 64 | |
| 65 return feof(first) && feof(second); | |
| 66 } | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 namespace relocation_packer { | |
| 71 | |
| 72 #if DEBUG_TEST_DATA | |
|
rmcilroy
2014/06/02 15:16:35
This is never defined - can we just remove this te
simonb (inactive)
2014/06/04 16:40:35
Done.
| |
| 73 TEST(ElfFile, ExtractTestData) { | |
| 74 // Dummy test to dump data files to /tmp for debugging, always passes. | |
| 75 FILE* stream = fopen("/tmp/relocs.so", "wb"); | |
| 76 if (stream) { | |
| 77 fwrite(relocation_packer_elf_file_unittest_relocs_so, | |
| 78 relocation_packer_elf_file_unittest_relocs_so_len, | |
| 79 1, | |
| 80 stream); | |
| 81 fclose(stream); | |
| 82 } | |
| 83 | |
| 84 stream = fopen("/tmp/packed_relocs.so", "wb"); | |
| 85 if (stream) { | |
| 86 fwrite(relocation_packer_elf_file_unittest_relocs_golden_so, | |
| 87 relocation_packer_elf_file_unittest_relocs_golden_so_len, | |
| 88 1, | |
| 89 stream); | |
| 90 fclose(stream); | |
| 91 } | |
| 92 } | |
| 93 #endif | |
| 94 | |
| 95 TEST(ElfFile, PackRelocations) { | |
| 96 ASSERT_NE(EV_NONE, elf_version(EV_CURRENT)); | |
| 97 | |
| 98 FILE* relocs_so = NULL; | |
| 99 FILE* packed_relocs_so = NULL; | |
| 100 ASSERT_TRUE(MaterializeRelocsTestFiles(&relocs_so, &packed_relocs_so)); | |
| 101 ASSERT_FALSE(FileContentsEqual(relocs_so, packed_relocs_so)); | |
| 102 | |
| 103 ASSERT_EQ(0, lseek(fileno(relocs_so), 0, SEEK_SET)); | |
|
rmcilroy
2014/06/02 15:16:35
How about doing the lseek at the end of FileConten
simonb (inactive)
2014/06/04 16:40:35
Can't call ASSERT_EQ from a namespace-local functi
| |
| 104 | |
| 105 ElfFile elf_file; | |
| 106 EXPECT_TRUE(elf_file.Load(fileno(relocs_so))); | |
| 107 | |
| 108 // Ensure unpacking fails (not packed). | |
| 109 EXPECT_FALSE(elf_file.UnpackRelocations()); | |
| 110 | |
| 111 // Pack relocations. | |
| 112 EXPECT_TRUE(elf_file.PackRelocations()); | |
| 113 elf_file.Flush(); | |
| 114 | |
| 115 // Compare packed relocations to golden. | |
| 116 EXPECT_TRUE(FileContentsEqual(relocs_so, packed_relocs_so)); | |
| 117 | |
| 118 DematerializeRelocsTestFiles(relocs_so, packed_relocs_so); | |
| 119 } | |
| 120 | |
| 121 TEST(ElfFile, UnpackRelocations) { | |
| 122 ASSERT_NE(EV_NONE, elf_version(EV_CURRENT)); | |
| 123 | |
| 124 FILE* relocs_so = NULL; | |
| 125 FILE* packed_relocs_so = NULL; | |
| 126 ASSERT_TRUE(MaterializeRelocsTestFiles(&relocs_so, &packed_relocs_so)); | |
| 127 ASSERT_FALSE(FileContentsEqual(relocs_so, packed_relocs_so)); | |
| 128 | |
| 129 ASSERT_EQ(0, lseek(fileno(packed_relocs_so), 0, SEEK_SET)); | |
| 130 | |
| 131 ElfFile elf_file; | |
| 132 EXPECT_TRUE(elf_file.Load(fileno(packed_relocs_so))); | |
| 133 | |
| 134 // Ensure packing fails (already packed). | |
| 135 EXPECT_FALSE(elf_file.PackRelocations()); | |
| 136 | |
| 137 // Unpack golden relocations. | |
| 138 EXPECT_TRUE(elf_file.UnpackRelocations()); | |
| 139 elf_file.Flush(); | |
| 140 | |
| 141 // Compare unpacked relocations to originals. | |
| 142 EXPECT_TRUE(FileContentsEqual(packed_relocs_so, relocs_so)); | |
| 143 | |
| 144 DematerializeRelocsTestFiles(relocs_so, packed_relocs_so); | |
| 145 } | |
| 146 | |
| 147 } // namespace relocation_packer | |
| OLD | NEW |