blob: 09be4455b8ad4ee12614771ef3f475132e924c38 [file] [log] [blame]
Fabien Sanglard0f29f542020-10-22 17:58:12 -07001#include "gmock/gmock.h"
2#include "gtest/gtest.h"
3
4#include "ZipAlign.h"
5
weisueae45312021-12-13 17:56:12 +00006#include <filesystem>
Fabien Sanglard0f29f542020-10-22 17:58:12 -07007#include <stdio.h>
Fabien Sanglard6dfc6fb2020-10-22 17:58:12 -07008#include <string>
9
10#include <android-base/file.h>
Fabien Sanglard0f29f542020-10-22 17:58:12 -070011
12using namespace android;
Fabien Sanglarddf73d1b2021-09-14 14:18:31 -070013using namespace base;
Fabien Sanglard0f29f542020-10-22 17:58:12 -070014
Fabien Sanglard8163cfa2022-10-10 22:19:47 +000015// This load the whole file to memory so be careful!
16static 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 Sanglard6dfc6fb2020-10-22 17:58:12 -070037static 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
weisueae45312021-12-13 17:56:12 +000042static 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 Sanglard0f29f542020-10-22 17:58:12 -070048TEST(Align, Unaligned) {
Fabien Sanglard6dfc6fb2020-10-22 17:58:12 -070049 const std::string src = GetTestPath("unaligned.zip");
weisueae45312021-12-13 17:56:12 +000050 const std::string dst = GetTempPath("unaligned_out.zip");
Fabien Sanglard6dfc6fb2020-10-22 17:58:12 -070051
Kalesh Singh7e0aa042023-08-17 14:21:06 -070052 int processed = process(src.c_str(), dst.c_str(), 4, true, false, false);
Fabien Sanglard4b4b4952020-11-05 18:36:56 -080053 ASSERT_EQ(0, processed);
54
55 int verified = verify(dst.c_str(), 4, true, false);
56 ASSERT_EQ(0, verified);
Fabien Sanglard0f29f542020-10-22 17:58:12 -070057}
Fabien Sanglarda7206352020-10-20 15:47:10 -070058
Fabien Sanglarddf73d1b2021-09-14 14:18:31 -070059TEST(Align, DoubleAligment) {
60 const std::string src = GetTestPath("unaligned.zip");
weisueae45312021-12-13 17:56:12 +000061 const std::string tmp = GetTempPath("da_aligned.zip");
62 const std::string dst = GetTempPath("da_d_aligner.zip");
Fabien Sanglarddf73d1b2021-09-14 14:18:31 -070063
Kalesh Singh7e0aa042023-08-17 14:21:06 -070064 int processed = process(src.c_str(), tmp.c_str(), 4, true, false, false);
Fabien Sanglarddf73d1b2021-09-14 14:18:31 -070065 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.
Kalesh Singh7e0aa042023-08-17 14:21:06 -070071 processed = process(tmp.c_str(), dst.c_str(), 4, true, false, false);
Fabien Sanglarddf73d1b2021-09-14 14:18:31 -070072 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 Sanglarda7206352020-10-20 15:47:10 -070087// Align a zip featuring a hole at the beginning. The
88// hole in the archive is a delete entry in the Central
89// Directory.
90TEST(Align, Holes) {
91 const std::string src = GetTestPath("holes.zip");
weisueae45312021-12-13 17:56:12 +000092 const std::string dst = GetTempPath("holes_out.zip");
Fabien Sanglarda7206352020-10-20 15:47:10 -070093
Kalesh Singh7e0aa042023-08-17 14:21:06 -070094 int processed = process(src.c_str(), dst.c_str(), 4, true, false, true);
Fabien Sanglarda7206352020-10-20 15:47:10 -070095 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.
102TEST(Align, DifferenteOrders) {
103 const std::string src = GetTestPath("diffOrders.zip");
weisueae45312021-12-13 17:56:12 +0000104 const std::string dst = GetTempPath("diffOrders_out.zip");
Fabien Sanglarda7206352020-10-20 15:47:10 -0700105
Kalesh Singh7e0aa042023-08-17 14:21:06 -0700106 int processed = process(src.c_str(), dst.c_str(), 4, true, false, true);
Fabien Sanglarda7206352020-10-20 15:47:10 -0700107 ASSERT_EQ(0, processed);
108
109 int verified = verify(dst.c_str(), 4, false, true);
110 ASSERT_EQ(0, verified);
111}
Fabien Sanglard8163cfa2022-10-10 22:19:47 +0000112
113TEST(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
119TEST(Align, DirectoryEntry) {
120 const std::string src = GetTestPath("archiveWithOneDirectoryEntry.zip");
121 const std::string dst = GetTempPath("archiveWithOneDirectoryEntry_out.zip");
122
Kalesh Singh7e0aa042023-08-17 14:21:06 -0700123 int processed = process(src.c_str(), dst.c_str(), 4, true, false, true);
Fabien Sanglard8163cfa2022-10-10 22:19:47 +0000124 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}