Index: tools/relocation_packer/src/relocation_packer_elf_file_unittest.cc |
diff --git a/tools/relocation_packer/src/relocation_packer_elf_file_unittest.cc b/tools/relocation_packer/src/relocation_packer_elf_file_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f40b14da6d6a2dac3d540e2d846709f87f1cc55c |
--- /dev/null |
+++ b/tools/relocation_packer/src/relocation_packer_elf_file_unittest.cc |
@@ -0,0 +1,147 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Set to 1 to enable the dummy test that dumps test data to /tmp. |
+#define DEBUG_TEST_DATA 0 |
+ |
+#include "relocation_packer_elf_file.h" |
+ |
+#include <stdio.h> |
+#include <vector> |
+#include "relocation_packer_debug.h" |
+#include "relocation_packer_elf_file_unittest_relocs.so.h" |
+#include "relocation_packer_elf_file_unittest_relocs_golden.so.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+FILE* MaterializeRelocsTestFile(unsigned char* data, size_t length) { |
+ FILE* temporary = tmpfile(); |
+ if (temporary) |
+ fwrite(data, 1, length, temporary); |
+ return temporary; |
+} |
+ |
+bool MaterializeRelocsTestFiles(FILE** relocs_so, FILE** packed_relocs_so) { |
+ *relocs_so = MaterializeRelocsTestFile( |
+ relocation_packer_elf_file_unittest_relocs_so, |
+ relocation_packer_elf_file_unittest_relocs_so_len); |
+ |
+ *packed_relocs_so = MaterializeRelocsTestFile( |
+ relocation_packer_elf_file_unittest_relocs_golden_so, |
+ relocation_packer_elf_file_unittest_relocs_golden_so_len); |
+ |
+ 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.
|
+} |
+ |
+void DematerializeRelocsTestFile(FILE* temporary) { |
+ 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
|
+} |
+ |
+void DematerializeRelocsTestFiles(FILE* relocs_so, FILE* packed_relocs_so) { |
+ DematerializeRelocsTestFile(relocs_so); |
+ DematerializeRelocsTestFile(packed_relocs_so); |
+} |
+ |
+bool FileContentsEqual(FILE* first, FILE* second) { |
+ if (fseek(first, 0, SEEK_SET) || fseek(second, 0, SEEK_SET)) |
+ return false; |
+ |
+ static const size_t buffer_size = 4096; |
+ unsigned char first_buffer[buffer_size]; |
+ unsigned char second_buffer[buffer_size]; |
+ |
+ do { |
+ size_t first_read = fread(first_buffer, 1, sizeof(first_buffer), first); |
+ size_t second_read = fread(second_buffer, 1, sizeof(second_buffer), second); |
+ |
+ if (first_read != second_read) |
+ return false; |
+ if (memcmp(first_buffer, second_buffer, first_read)) |
+ return false; |
+ } while (!feof(first) && !feof(second)); |
+ |
+ return feof(first) && feof(second); |
+} |
+ |
+} // namespace |
+ |
+namespace relocation_packer { |
+ |
+#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.
|
+TEST(ElfFile, ExtractTestData) { |
+ // Dummy test to dump data files to /tmp for debugging, always passes. |
+ FILE* stream = fopen("/tmp/relocs.so", "wb"); |
+ if (stream) { |
+ fwrite(relocation_packer_elf_file_unittest_relocs_so, |
+ relocation_packer_elf_file_unittest_relocs_so_len, |
+ 1, |
+ stream); |
+ fclose(stream); |
+ } |
+ |
+ stream = fopen("/tmp/packed_relocs.so", "wb"); |
+ if (stream) { |
+ fwrite(relocation_packer_elf_file_unittest_relocs_golden_so, |
+ relocation_packer_elf_file_unittest_relocs_golden_so_len, |
+ 1, |
+ stream); |
+ fclose(stream); |
+ } |
+} |
+#endif |
+ |
+TEST(ElfFile, PackRelocations) { |
+ ASSERT_NE(EV_NONE, elf_version(EV_CURRENT)); |
+ |
+ FILE* relocs_so = NULL; |
+ FILE* packed_relocs_so = NULL; |
+ ASSERT_TRUE(MaterializeRelocsTestFiles(&relocs_so, &packed_relocs_so)); |
+ ASSERT_FALSE(FileContentsEqual(relocs_so, packed_relocs_so)); |
+ |
+ 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
|
+ |
+ ElfFile elf_file; |
+ EXPECT_TRUE(elf_file.Load(fileno(relocs_so))); |
+ |
+ // Ensure unpacking fails (not packed). |
+ EXPECT_FALSE(elf_file.UnpackRelocations()); |
+ |
+ // Pack relocations. |
+ EXPECT_TRUE(elf_file.PackRelocations()); |
+ elf_file.Flush(); |
+ |
+ // Compare packed relocations to golden. |
+ EXPECT_TRUE(FileContentsEqual(relocs_so, packed_relocs_so)); |
+ |
+ DematerializeRelocsTestFiles(relocs_so, packed_relocs_so); |
+} |
+ |
+TEST(ElfFile, UnpackRelocations) { |
+ ASSERT_NE(EV_NONE, elf_version(EV_CURRENT)); |
+ |
+ FILE* relocs_so = NULL; |
+ FILE* packed_relocs_so = NULL; |
+ ASSERT_TRUE(MaterializeRelocsTestFiles(&relocs_so, &packed_relocs_so)); |
+ ASSERT_FALSE(FileContentsEqual(relocs_so, packed_relocs_so)); |
+ |
+ ASSERT_EQ(0, lseek(fileno(packed_relocs_so), 0, SEEK_SET)); |
+ |
+ ElfFile elf_file; |
+ EXPECT_TRUE(elf_file.Load(fileno(packed_relocs_so))); |
+ |
+ // Ensure packing fails (already packed). |
+ EXPECT_FALSE(elf_file.PackRelocations()); |
+ |
+ // Unpack golden relocations. |
+ EXPECT_TRUE(elf_file.UnpackRelocations()); |
+ elf_file.Flush(); |
+ |
+ // Compare unpacked relocations to originals. |
+ EXPECT_TRUE(FileContentsEqual(packed_relocs_so, relocs_so)); |
+ |
+ DematerializeRelocsTestFiles(relocs_so, packed_relocs_so); |
+} |
+ |
+} // namespace relocation_packer |