Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: third_party/android_platform/relocation_packer/src/elf_file_unittest.cc

Issue 1027823002: Port Android relocation packer to chromium build (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "elf_file.h" 5 #include "elf_file.h"
6 6
7 #include <limits.h> 7 #include <limits.h>
8 #include <stdio.h> 8 #include <stdio.h>
9 #include <unistd.h> 9 #include <unistd.h>
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 #include "debug.h" 12 #include "debug.h"
13 #include "elf_traits.h" 13 #include "elf_traits.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "gtest/gtest.h"
15
16 // Macro stringification.
17 // https://gcc.gnu.org/onlinedocs/cpp/Stringification.html
18 #define XSTR(S) STR(S)
19 #define STR(S) #S
20 15
21 namespace { 16 namespace {
22 17
23 void GetDataFilePath(const char* name, std::string* path) { 18 void GetDataFilePath(const char* name, std::string* path) {
24 std::string data_dir; 19 std::string data_dir;
25 20
26 const char* bindir = getenv("bindir"); 21 const char* bindir = getenv("bindir");
27 if (bindir) { 22 if (bindir) {
28 data_dir = std::string(bindir); 23 data_dir = std::string(bindir);
29 } else { 24 } else {
30 // Test data is in the gyp INTERMEDIATE_DIR subdirectory of the directory
31 // that contains the current binary.
32 char path[PATH_MAX]; 25 char path[PATH_MAX];
33 memset(path, 0, sizeof(path)); 26 memset(path, 0, sizeof(path));
34 ASSERT_NE(-1, readlink("/proc/self/exe", path, sizeof(path) - 1)); 27 ASSERT_NE(-1, readlink("/proc/self/exe", path, sizeof(path) - 1));
35 28
36 data_dir = std::string(path); 29 data_dir = std::string(path);
37 size_t pos = data_dir.rfind('/'); 30 size_t pos = data_dir.rfind('/');
38 ASSERT_NE(std::string::npos, pos); 31 ASSERT_NE(std::string::npos, pos);
39 32
40 data_dir.erase(pos + 1); 33 data_dir.erase(pos);
41 data_dir += std::string(XSTR(INTERMEDIATE_DIR));
42 } 34 }
43 35
44 *path = data_dir + "/" + name; 36 *path = data_dir + "/" + name;
45 } 37 }
46 38
47 void OpenRelocsTestFile(const char* name, FILE** stream) { 39 void OpenRelocsTestFile(const char* name, FILE** stream) {
48 std::string path; 40 std::string path;
49 GetDataFilePath(name, &path); 41 GetDataFilePath(name, &path);
50 42
51 FILE* testfile = fopen(path.c_str(), "rb"); 43 FILE* testfile = fopen(path.c_str(), "rb");
52 ASSERT_FALSE(testfile == NULL); 44 ASSERT_FALSE(testfile == NULL) << "Error opening '" << path << "'";
53 45
54 FILE* temporary = tmpfile(); 46 FILE* temporary = tmpfile();
55 ASSERT_FALSE(temporary == NULL); 47 ASSERT_FALSE(temporary == NULL);
56 48
57 static const size_t buffer_size = 4096; 49 static const size_t buffer_size = 4096;
58 unsigned char buffer[buffer_size]; 50 unsigned char buffer[buffer_size];
59 51
60 size_t bytes; 52 size_t bytes;
61 do { 53 do {
62 bytes = fread(buffer, 1, sizeof(buffer), testfile); 54 bytes = fread(buffer, 1, sizeof(buffer), testfile);
63 ASSERT_EQ(bytes, fwrite(buffer, 1, bytes, temporary)); 55 ASSERT_EQ(bytes, fwrite(buffer, 1, bytes, temporary));
64 } while (bytes > 0); 56 } while (bytes > 0);
65 57
66 ASSERT_EQ(0, fclose(testfile)); 58 ASSERT_EQ(0, fclose(testfile));
67 ASSERT_EQ(0, fseek(temporary, 0, SEEK_SET)); 59 ASSERT_EQ(0, fseek(temporary, 0, SEEK_SET));
68 ASSERT_EQ(0, lseek(fileno(temporary), 0, SEEK_SET)); 60 ASSERT_EQ(0, lseek(fileno(temporary), 0, SEEK_SET));
69 61
70 *stream = temporary; 62 *stream = temporary;
71 } 63 }
72 64
73 void OpenRelocsTestFiles(FILE** relocs_so, FILE** packed_relocs_so) { 65 void OpenRelocsTestFiles(const std::string& arch, FILE** relocs_so, FILE** packe d_relocs_so) {
74 const char* arch = NULL;
75 if (ELF::kMachine == EM_ARM) {
76 arch = "arm32";
77 } else if (ELF::kMachine == EM_AARCH64) {
78 arch = "arm64";
79 }
80 ASSERT_FALSE(arch == NULL);
81
82 const std::string base = std::string("elf_file_unittest_relocs_") + arch; 66 const std::string base = std::string("elf_file_unittest_relocs_") + arch;
83 const std::string relocs = base + ".so"; 67 const std::string relocs = base + ".so";
84 const std::string packed_relocs = base + "_packed.so"; 68 const std::string packed_relocs = base + "_packed.so";
85 69
86 OpenRelocsTestFile(relocs.c_str(), relocs_so); 70 OpenRelocsTestFile(relocs.c_str(), relocs_so);
87 OpenRelocsTestFile(packed_relocs.c_str(), packed_relocs_so); 71 OpenRelocsTestFile(packed_relocs.c_str(), packed_relocs_so);
88 } 72 }
89 73
90 void CloseRelocsTestFile(FILE* temporary) { 74 void CloseRelocsTestFile(FILE* temporary) {
91 fclose(temporary); 75 fclose(temporary);
(...skipping 16 matching lines...) Expand all
108 size_t first_read = fread(first_buffer, 1, sizeof(first_buffer), first); 92 size_t first_read = fread(first_buffer, 1, sizeof(first_buffer), first);
109 size_t second_read = fread(second_buffer, 1, sizeof(second_buffer), second); 93 size_t second_read = fread(second_buffer, 1, sizeof(second_buffer), second);
110 94
111 EXPECT_EQ(first_read, second_read); 95 EXPECT_EQ(first_read, second_read);
112 EXPECT_EQ(0, memcmp(first_buffer, second_buffer, first_read)); 96 EXPECT_EQ(0, memcmp(first_buffer, second_buffer, first_read));
113 } while (!feof(first) && !feof(second)); 97 } while (!feof(first) && !feof(second));
114 98
115 EXPECT_TRUE(feof(first) && feof(second)); 99 EXPECT_TRUE(feof(first) && feof(second));
116 } 100 }
117 101
118 } // namespace 102 template <typename ELF>
103 static void ProcessUnpack(FILE* relocs_so, FILE* packed_relocs_so) {
104 relocation_packer::ElfFile<ELF> elf_file(fileno(packed_relocs_so));
119 105
120 namespace relocation_packer { 106 // Ensure packing fails (already packed).
107 EXPECT_FALSE(elf_file.PackRelocations());
121 108
122 TEST(ElfFile, PackRelocations) { 109 // Unpack golden relocations, and check files are now identical.
123 ASSERT_NE(EV_NONE, elf_version(EV_CURRENT)); 110 EXPECT_TRUE(elf_file.UnpackRelocations());
111 CheckFileContentsEqual(packed_relocs_so, relocs_so);
112
113 CloseRelocsTestFiles(relocs_so, packed_relocs_so);
114 }
115
116 static void RunUnpackRelocationsTestFor(const std::string& arch) {
117 ASSERT_NE(static_cast<uint32_t>(EV_NONE), elf_version(EV_CURRENT));
124 118
125 FILE* relocs_so = NULL; 119 FILE* relocs_so = NULL;
126 FILE* packed_relocs_so = NULL; 120 FILE* packed_relocs_so = NULL;
127 OpenRelocsTestFiles(&relocs_so, &packed_relocs_so); 121 OpenRelocsTestFiles(arch, &relocs_so, &packed_relocs_so);
128 if (HasFatalFailure())
129 return;
130 122
131 ElfFile elf_file(fileno(relocs_so)); 123 if (relocs_so != NULL && packed_relocs_so != NULL) {
124 // lets detect elf class
125 ASSERT_EQ(0, fseek(relocs_so, EI_CLASS, SEEK_SET))
126 << "Invalid file length: " << strerror(errno);
127 uint8_t elf_class = 0;
128 ASSERT_EQ(1U, fread(&elf_class, 1, 1, relocs_so));
129 ASSERT_EQ(0, fseek(relocs_so, 0, SEEK_SET));
130 if (elf_class == ELFCLASS32) {
131 ProcessUnpack<ELF32_traits>(relocs_so, packed_relocs_so);
132 } else {
133 ProcessUnpack<ELF64_traits>(relocs_so, packed_relocs_so);
134 }
135 }
136 }
137
138 template <typename ELF>
139 static void ProcessPack(FILE* relocs_so, FILE* packed_relocs_so) {
140 relocation_packer::ElfFile<ELF> elf_file(fileno(relocs_so));
132 141
133 // Ensure unpacking fails (not packed). 142 // Ensure unpacking fails (not packed).
134 EXPECT_FALSE(elf_file.UnpackRelocations()); 143 EXPECT_FALSE(elf_file.UnpackRelocations());
135 144
136 // Pack relocations, and check files are now identical. 145 // Pack relocations, and check files are now identical.
137 EXPECT_TRUE(elf_file.PackRelocations()); 146 EXPECT_TRUE(elf_file.PackRelocations());
138 CheckFileContentsEqual(relocs_so, packed_relocs_so); 147 CheckFileContentsEqual(relocs_so, packed_relocs_so);
139 148
140 CloseRelocsTestFiles(relocs_so, packed_relocs_so); 149 CloseRelocsTestFiles(relocs_so, packed_relocs_so);
141 } 150 }
142 151
143 TEST(ElfFile, UnpackRelocations) { 152 static void RunPackRelocationsTestFor(const std::string& arch) {
144 ASSERT_NE(EV_NONE, elf_version(EV_CURRENT)); 153 ASSERT_NE(static_cast<uint32_t>(EV_NONE), elf_version(EV_CURRENT));
145 154
146 FILE* relocs_so = NULL; 155 FILE* relocs_so = NULL;
147 FILE* packed_relocs_so = NULL; 156 FILE* packed_relocs_so = NULL;
148 OpenRelocsTestFiles(&relocs_so, &packed_relocs_so); 157 OpenRelocsTestFiles(arch, &relocs_so, &packed_relocs_so);
149 if (HasFatalFailure())
150 return;
151 158
152 ElfFile elf_file(fileno(packed_relocs_so)); 159 if (relocs_so != NULL && packed_relocs_so != NULL) {
160 // lets detect elf class
161 ASSERT_EQ(0, fseek(packed_relocs_so, EI_CLASS, SEEK_SET))
162 << "Invalid file length: " << strerror(errno);
163 uint8_t elf_class = 0;
164 ASSERT_EQ(1U, fread(&elf_class, 1, 1, packed_relocs_so));
165 fseek(packed_relocs_so, 0, SEEK_SET);
166 if (elf_class == ELFCLASS32) {
167 ProcessPack<ELF32_traits>(relocs_so, packed_relocs_so);
168 } else {
169 ProcessPack<ELF64_traits>(relocs_so, packed_relocs_so);
170 }
171 }
172 }
153 173
154 // Ensure packing fails (already packed). 174 } // namespace
155 EXPECT_FALSE(elf_file.PackRelocations());
156 175
157 // Unpack golden relocations, and check files are now identical. 176 namespace relocation_packer {
158 EXPECT_TRUE(elf_file.UnpackRelocations());
159 CheckFileContentsEqual(packed_relocs_so, relocs_so);
160 177
161 CloseRelocsTestFiles(relocs_so, packed_relocs_so); 178 TEST(ElfFile, PackRelocations) {
179 RunPackRelocationsTestFor("arm32");
180 RunPackRelocationsTestFor("arm64");
181 }
182
183 TEST(ElfFile, UnpackRelocations) {
184 RunUnpackRelocationsTestFor("arm32");
185 RunUnpackRelocationsTestFor("arm64");
162 } 186 }
163 187
164 } // namespace relocation_packer 188 } // namespace relocation_packer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698