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 | 6dfc6fb | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 15 | static std::string GetTestPath(const std::string& filename) { |
| 16 | static std::string test_data_dir = android::base::GetExecutableDirectory() + "/tests/data/"; |
| 17 | return test_data_dir + filename; |
| 18 | } |
| 19 | |
weisu | eae4531 | 2021-12-13 17:56:12 +0000 | [diff] [blame] | 20 | static std::string GetTempPath(const std::string& filename) { |
| 21 | std::filesystem::path temp_path = std::filesystem::path(testing::TempDir()); |
| 22 | temp_path += filename; |
| 23 | return temp_path.string(); |
| 24 | } |
| 25 | |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 26 | TEST(Align, Unaligned) { |
Fabien Sanglard | 6dfc6fb | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 27 | const std::string src = GetTestPath("unaligned.zip"); |
weisu | eae4531 | 2021-12-13 17:56:12 +0000 | [diff] [blame] | 28 | const std::string dst = GetTempPath("unaligned_out.zip"); |
Fabien Sanglard | 6dfc6fb | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 29 | |
Fabien Sanglard | 4b4b495 | 2020-11-05 18:36:56 -0800 | [diff] [blame] | 30 | int processed = process(src.c_str(), dst.c_str(), 4, true, false, 4096); |
| 31 | ASSERT_EQ(0, processed); |
| 32 | |
| 33 | int verified = verify(dst.c_str(), 4, true, false); |
| 34 | ASSERT_EQ(0, verified); |
Fabien Sanglard | 0f29f54 | 2020-10-22 17:58:12 -0700 | [diff] [blame] | 35 | } |
Fabien Sanglard | a720635 | 2020-10-20 15:47:10 -0700 | [diff] [blame] | 36 | |
Fabien Sanglard | df73d1b | 2021-09-14 14:18:31 -0700 | [diff] [blame] | 37 | TEST(Align, DoubleAligment) { |
| 38 | const std::string src = GetTestPath("unaligned.zip"); |
weisu | eae4531 | 2021-12-13 17:56:12 +0000 | [diff] [blame] | 39 | const std::string tmp = GetTempPath("da_aligned.zip"); |
| 40 | const std::string dst = GetTempPath("da_d_aligner.zip"); |
Fabien Sanglard | df73d1b | 2021-09-14 14:18:31 -0700 | [diff] [blame] | 41 | |
| 42 | int processed = process(src.c_str(), tmp.c_str(), 4, true, false, 4096); |
| 43 | ASSERT_EQ(0, processed); |
| 44 | |
| 45 | int verified = verify(tmp.c_str(), 4, true, false); |
| 46 | ASSERT_EQ(0, verified); |
| 47 | |
| 48 | // Align the result of the previous run. Essentially double aligning. |
| 49 | processed = process(tmp.c_str(), dst.c_str(), 4, true, false, 4096); |
| 50 | ASSERT_EQ(0, processed); |
| 51 | |
| 52 | verified = verify(dst.c_str(), 4, true, false); |
| 53 | ASSERT_EQ(0, verified); |
| 54 | |
| 55 | // Nothing should have changed between tmp and dst. |
| 56 | std::string tmp_content; |
| 57 | ASSERT_EQ(true, ReadFileToString(tmp, &tmp_content)); |
| 58 | |
| 59 | std::string dst_content; |
| 60 | ASSERT_EQ(true, ReadFileToString(dst, &dst_content)); |
| 61 | |
| 62 | ASSERT_EQ(tmp_content, dst_content); |
| 63 | } |
| 64 | |
Fabien Sanglard | a720635 | 2020-10-20 15:47:10 -0700 | [diff] [blame] | 65 | // Align a zip featuring a hole at the beginning. The |
| 66 | // hole in the archive is a delete entry in the Central |
| 67 | // Directory. |
| 68 | TEST(Align, Holes) { |
| 69 | const std::string src = GetTestPath("holes.zip"); |
weisu | eae4531 | 2021-12-13 17:56:12 +0000 | [diff] [blame] | 70 | const std::string dst = GetTempPath("holes_out.zip"); |
Fabien Sanglard | a720635 | 2020-10-20 15:47:10 -0700 | [diff] [blame] | 71 | |
| 72 | int processed = process(src.c_str(), dst.c_str(), 4, true, false, 4096); |
| 73 | ASSERT_EQ(0, processed); |
| 74 | |
| 75 | int verified = verify(dst.c_str(), 4, false, true); |
| 76 | ASSERT_EQ(0, verified); |
| 77 | } |
| 78 | |
| 79 | // Align a zip where LFH order and CD entries differ. |
| 80 | TEST(Align, DifferenteOrders) { |
| 81 | const std::string src = GetTestPath("diffOrders.zip"); |
weisu | eae4531 | 2021-12-13 17:56:12 +0000 | [diff] [blame] | 82 | const std::string dst = GetTempPath("diffOrders_out.zip"); |
Fabien Sanglard | a720635 | 2020-10-20 15:47:10 -0700 | [diff] [blame] | 83 | |
| 84 | int processed = process(src.c_str(), dst.c_str(), 4, true, false, 4096); |
| 85 | ASSERT_EQ(0, processed); |
| 86 | |
| 87 | int verified = verify(dst.c_str(), 4, false, true); |
| 88 | ASSERT_EQ(0, verified); |
| 89 | } |