Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 1 | #include "gmock/gmock.h" |
| 2 | #include "gtest/gtest.h" |
| 3 | |
| 4 | #include "ZipAlign.h" |
| 5 | |
weisu | eae4531 | 2021-12-13 17:56:12 +0000 | [diff] [blame] | 6 | #include <filesystem> |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 7 | #include <stdio.h> |
Fabien Sanglard | 6dfc6fb | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
| 10 | #include <android-base/file.h> |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 11 | |
| 12 | using namespace android; |
Fabien Sanglard | df73d1b | 2021-09-14 14:18:31 -0700 | [diff] [blame] | 13 | using namespace base; |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 14 | |
Fabien Sanglard | 8163cfa | 2022-10-10 22:19:47 +0000 | [diff] [blame^] | 15 | // This load the whole file to memory so be careful! |
| 16 | static bool sameContent(const std::string& path1, const std::string& path2) { |
| 17 | std::string f1; |
| 18 | if (!ReadFileToString(path1, &f1)) { |
| 19 | printf("Unable to read '%s' content: %m\n", path1.c_str()); |
| 20 | return false; |
| 21 | } |
| 22 | |
| 23 | std::string f2; |
| 24 | if (!ReadFileToString(path2, &f2)) { |
| 25 | printf("Unable to read '%s' content %m\n", path1.c_str()); |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | if (f1.size() != f2.size()) { |
| 30 | printf("File '%s' and '%s' are not the same\n", path1.c_str(), path2.c_str()); |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | return f1.compare(f2) == 0; |
| 35 | } |
| 36 | |
Fabien Sanglard | 6dfc6fb | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 37 | static std::string GetTestPath(const std::string& filename) { |
| 38 | static std::string test_data_dir = android::base::GetExecutableDirectory() + "/tests/data/"; |
| 39 | return test_data_dir + filename; |
| 40 | } |
| 41 | |
weisu | eae4531 | 2021-12-13 17:56:12 +0000 | [diff] [blame] | 42 | static std::string GetTempPath(const std::string& filename) { |
| 43 | std::filesystem::path temp_path = std::filesystem::path(testing::TempDir()); |
| 44 | temp_path += filename; |
| 45 | return temp_path.string(); |
| 46 | } |
| 47 | |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 48 | TEST(Align, Unaligned) { |
Fabien Sanglard | 6dfc6fb | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 49 | const std::string src = GetTestPath("unaligned.zip"); |
weisu | eae4531 | 2021-12-13 17:56:12 +0000 | [diff] [blame] | 50 | const std::string dst = GetTempPath("unaligned_out.zip"); |
Fabien Sanglard | 6dfc6fb | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 51 | |
Fabien Sanglard | 4b4b495 | 2020-11-05 18:36:56 -0800 | [diff] [blame] | 52 | int processed = process(src.c_str(), dst.c_str(), 4, true, false, 4096); |
| 53 | ASSERT_EQ(0, processed); |
| 54 | |
| 55 | int verified = verify(dst.c_str(), 4, true, false); |
| 56 | ASSERT_EQ(0, verified); |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 57 | } |
Fabien Sanglard | a720635 | 2020-10-20 15:47:10 -0700 | [diff] [blame] | 58 | |
Fabien Sanglard | df73d1b | 2021-09-14 14:18:31 -0700 | [diff] [blame] | 59 | TEST(Align, DoubleAligment) { |
| 60 | const std::string src = GetTestPath("unaligned.zip"); |
weisu | eae4531 | 2021-12-13 17:56:12 +0000 | [diff] [blame] | 61 | const std::string tmp = GetTempPath("da_aligned.zip"); |
| 62 | const std::string dst = GetTempPath("da_d_aligner.zip"); |
Fabien Sanglard | df73d1b | 2021-09-14 14:18:31 -0700 | [diff] [blame] | 63 | |
| 64 | int processed = process(src.c_str(), tmp.c_str(), 4, true, false, 4096); |
| 65 | ASSERT_EQ(0, processed); |
| 66 | |
| 67 | int verified = verify(tmp.c_str(), 4, true, false); |
| 68 | ASSERT_EQ(0, verified); |
| 69 | |
| 70 | // Align the result of the previous run. Essentially double aligning. |
| 71 | processed = process(tmp.c_str(), dst.c_str(), 4, true, false, 4096); |
| 72 | ASSERT_EQ(0, processed); |
| 73 | |
| 74 | verified = verify(dst.c_str(), 4, true, false); |
| 75 | ASSERT_EQ(0, verified); |
| 76 | |
| 77 | // Nothing should have changed between tmp and dst. |
| 78 | std::string tmp_content; |
| 79 | ASSERT_EQ(true, ReadFileToString(tmp, &tmp_content)); |
| 80 | |
| 81 | std::string dst_content; |
| 82 | ASSERT_EQ(true, ReadFileToString(dst, &dst_content)); |
| 83 | |
| 84 | ASSERT_EQ(tmp_content, dst_content); |
| 85 | } |
| 86 | |
Fabien Sanglard | a720635 | 2020-10-20 15:47:10 -0700 | [diff] [blame] | 87 | // Align a zip featuring a hole at the beginning. The |
| 88 | // hole in the archive is a delete entry in the Central |
| 89 | // Directory. |
| 90 | TEST(Align, Holes) { |
| 91 | const std::string src = GetTestPath("holes.zip"); |
weisu | eae4531 | 2021-12-13 17:56:12 +0000 | [diff] [blame] | 92 | const std::string dst = GetTempPath("holes_out.zip"); |
Fabien Sanglard | a720635 | 2020-10-20 15:47:10 -0700 | [diff] [blame] | 93 | |
| 94 | int processed = process(src.c_str(), dst.c_str(), 4, true, false, 4096); |
| 95 | ASSERT_EQ(0, processed); |
| 96 | |
| 97 | int verified = verify(dst.c_str(), 4, false, true); |
| 98 | ASSERT_EQ(0, verified); |
| 99 | } |
| 100 | |
| 101 | // Align a zip where LFH order and CD entries differ. |
| 102 | TEST(Align, DifferenteOrders) { |
| 103 | const std::string src = GetTestPath("diffOrders.zip"); |
weisu | eae4531 | 2021-12-13 17:56:12 +0000 | [diff] [blame] | 104 | const std::string dst = GetTempPath("diffOrders_out.zip"); |
Fabien Sanglard | a720635 | 2020-10-20 15:47:10 -0700 | [diff] [blame] | 105 | |
| 106 | int processed = process(src.c_str(), dst.c_str(), 4, true, false, 4096); |
| 107 | ASSERT_EQ(0, processed); |
| 108 | |
| 109 | int verified = verify(dst.c_str(), 4, false, true); |
| 110 | ASSERT_EQ(0, verified); |
| 111 | } |
Fabien Sanglard | 8163cfa | 2022-10-10 22:19:47 +0000 | [diff] [blame^] | 112 | |
| 113 | TEST(Align, DirectoryEntryDoNotRequireAlignment) { |
| 114 | const std::string src = GetTestPath("archiveWithOneDirectoryEntry.zip"); |
| 115 | int verified = verify(src.c_str(), 4, false, true); |
| 116 | ASSERT_EQ(0, verified); |
| 117 | } |
| 118 | |
| 119 | TEST(Align, DirectoryEntry) { |
| 120 | const std::string src = GetTestPath("archiveWithOneDirectoryEntry.zip"); |
| 121 | const std::string dst = GetTempPath("archiveWithOneDirectoryEntry_out.zip"); |
| 122 | |
| 123 | int processed = process(src.c_str(), dst.c_str(), 4, true, false, 4096); |
| 124 | ASSERT_EQ(0, processed); |
| 125 | ASSERT_EQ(true, sameContent(src, dst)); |
| 126 | |
| 127 | int verified = verify(dst.c_str(), 4, false, true); |
| 128 | ASSERT_EQ(0, verified); |
| 129 | } |