blob: 07ad7ccda2a91e38d2d6630f47bfc95136c4361e [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");
Kalesh Singh55405b62023-08-17 09:44:45 -070051 int pageSize = 4096;
Fabien Sanglard6dfc6fb2020-10-22 17:58:12 -070052
Kalesh Singh55405b62023-08-17 09:44:45 -070053 int processed = process(src.c_str(), dst.c_str(), 4, true, false, false, pageSize);
Fabien Sanglard4b4b4952020-11-05 18:36:56 -080054 ASSERT_EQ(0, processed);
55
Kalesh Singh55405b62023-08-17 09:44:45 -070056 int verified = verify(dst.c_str(), 4, true, false, pageSize);
Fabien Sanglard4b4b4952020-11-05 18:36:56 -080057 ASSERT_EQ(0, verified);
Fabien Sanglard0f29f542020-10-22 17:58:12 -070058}
Fabien Sanglarda7206352020-10-20 15:47:10 -070059
Fabien Sanglarddf73d1b2021-09-14 14:18:31 -070060TEST(Align, DoubleAligment) {
61 const std::string src = GetTestPath("unaligned.zip");
weisueae45312021-12-13 17:56:12 +000062 const std::string tmp = GetTempPath("da_aligned.zip");
63 const std::string dst = GetTempPath("da_d_aligner.zip");
Kalesh Singh55405b62023-08-17 09:44:45 -070064 int pageSize = 4096;
Fabien Sanglarddf73d1b2021-09-14 14:18:31 -070065
Kalesh Singh55405b62023-08-17 09:44:45 -070066 int processed = process(src.c_str(), tmp.c_str(), 4, true, false, false, pageSize);
Fabien Sanglarddf73d1b2021-09-14 14:18:31 -070067 ASSERT_EQ(0, processed);
68
Kalesh Singh55405b62023-08-17 09:44:45 -070069 int verified = verify(tmp.c_str(), 4, true, false, pageSize);
Fabien Sanglarddf73d1b2021-09-14 14:18:31 -070070 ASSERT_EQ(0, verified);
71
72 // Align the result of the previous run. Essentially double aligning.
Kalesh Singh55405b62023-08-17 09:44:45 -070073 processed = process(tmp.c_str(), dst.c_str(), 4, true, false, false, pageSize);
Fabien Sanglarddf73d1b2021-09-14 14:18:31 -070074 ASSERT_EQ(0, processed);
75
Kalesh Singh55405b62023-08-17 09:44:45 -070076 verified = verify(dst.c_str(), 4, true, false, pageSize);
Fabien Sanglarddf73d1b2021-09-14 14:18:31 -070077 ASSERT_EQ(0, verified);
78
79 // Nothing should have changed between tmp and dst.
80 std::string tmp_content;
81 ASSERT_EQ(true, ReadFileToString(tmp, &tmp_content));
82
83 std::string dst_content;
84 ASSERT_EQ(true, ReadFileToString(dst, &dst_content));
85
86 ASSERT_EQ(tmp_content, dst_content);
87}
88
Fabien Sanglarda7206352020-10-20 15:47:10 -070089// Align a zip featuring a hole at the beginning. The
90// hole in the archive is a delete entry in the Central
91// Directory.
92TEST(Align, Holes) {
93 const std::string src = GetTestPath("holes.zip");
weisueae45312021-12-13 17:56:12 +000094 const std::string dst = GetTempPath("holes_out.zip");
Kalesh Singh55405b62023-08-17 09:44:45 -070095 int pageSize = 4096;
Fabien Sanglarda7206352020-10-20 15:47:10 -070096
Kalesh Singh55405b62023-08-17 09:44:45 -070097 int processed = process(src.c_str(), dst.c_str(), 4, true, false, true, pageSize);
Fabien Sanglarda7206352020-10-20 15:47:10 -070098 ASSERT_EQ(0, processed);
99
Kalesh Singh55405b62023-08-17 09:44:45 -0700100 int verified = verify(dst.c_str(), 4, false, true, pageSize);
Fabien Sanglarda7206352020-10-20 15:47:10 -0700101 ASSERT_EQ(0, verified);
102}
103
104// Align a zip where LFH order and CD entries differ.
105TEST(Align, DifferenteOrders) {
106 const std::string src = GetTestPath("diffOrders.zip");
weisueae45312021-12-13 17:56:12 +0000107 const std::string dst = GetTempPath("diffOrders_out.zip");
Kalesh Singh55405b62023-08-17 09:44:45 -0700108 int pageSize = 4096;
Fabien Sanglarda7206352020-10-20 15:47:10 -0700109
Kalesh Singh55405b62023-08-17 09:44:45 -0700110 int processed = process(src.c_str(), dst.c_str(), 4, true, false, true, pageSize);
Fabien Sanglarda7206352020-10-20 15:47:10 -0700111 ASSERT_EQ(0, processed);
112
Kalesh Singh55405b62023-08-17 09:44:45 -0700113 int verified = verify(dst.c_str(), 4, false, true, pageSize);
Fabien Sanglarda7206352020-10-20 15:47:10 -0700114 ASSERT_EQ(0, verified);
115}
Fabien Sanglard8163cfa2022-10-10 22:19:47 +0000116
117TEST(Align, DirectoryEntryDoNotRequireAlignment) {
118 const std::string src = GetTestPath("archiveWithOneDirectoryEntry.zip");
Kalesh Singh55405b62023-08-17 09:44:45 -0700119 int pageSize = 4096;
120 int verified = verify(src.c_str(), 4, false, true, pageSize);
Fabien Sanglard8163cfa2022-10-10 22:19:47 +0000121 ASSERT_EQ(0, verified);
122}
123
124TEST(Align, DirectoryEntry) {
125 const std::string src = GetTestPath("archiveWithOneDirectoryEntry.zip");
126 const std::string dst = GetTempPath("archiveWithOneDirectoryEntry_out.zip");
Kalesh Singh55405b62023-08-17 09:44:45 -0700127 int pageSize = 4096;
Fabien Sanglard8163cfa2022-10-10 22:19:47 +0000128
Kalesh Singh55405b62023-08-17 09:44:45 -0700129 int processed = process(src.c_str(), dst.c_str(), 4, true, false, true, pageSize);
Fabien Sanglard8163cfa2022-10-10 22:19:47 +0000130 ASSERT_EQ(0, processed);
131 ASSERT_EQ(true, sameContent(src, dst));
132
Kalesh Singh55405b62023-08-17 09:44:45 -0700133 int verified = verify(dst.c_str(), 4, false, true, pageSize);
134 ASSERT_EQ(0, verified);
135}
136
137class UncompressedSharedLibsTest : public ::testing::Test {
138 protected:
139 static void SetUpTestSuite() {
140 src = GetTestPath("apkWithUncompressedSharedLibs.zip");
141 dst = GetTempPath("apkWithUncompressedSharedLibs_out.zip");
142 }
143
144 static std::string src;
145 static std::string dst;
146};
147
148std::string UncompressedSharedLibsTest::src;
149std::string UncompressedSharedLibsTest::dst;
150
151TEST_F(UncompressedSharedLibsTest, Unaligned) {
152 int pageSize = 4096;
153
154 int processed = process(src.c_str(), dst.c_str(), 4, true, false, false, pageSize);
155 ASSERT_EQ(0, processed);
156
157 int verified = verify(dst.c_str(), 4, true, true, pageSize);
158 ASSERT_NE(0, verified); // .so's not page-aligned
159}
160
161TEST_F(UncompressedSharedLibsTest, AlignedPageSize4kB) {
162 int pageSize = 4096;
163
164 int processed = process(src.c_str(), dst.c_str(), 4, true, false, true, pageSize);
165 ASSERT_EQ(0, processed);
166
167 int verified = verify(dst.c_str(), 4, true, true, pageSize);
168 ASSERT_EQ(0, verified);
169}
170
171TEST_F(UncompressedSharedLibsTest, AlignedPageSize16kB) {
172 int pageSize = 16384;
173
174 int processed = process(src.c_str(), dst.c_str(), 4, true, false, true, pageSize);
175 ASSERT_EQ(0, processed);
176
177 int verified = verify(dst.c_str(), 4, true, true, pageSize);
178 ASSERT_EQ(0, verified);
179}
180
181TEST_F(UncompressedSharedLibsTest, AlignedPageSize64kB) {
182 int pageSize = 65536;
183
184 int processed = process(src.c_str(), dst.c_str(), 4, true, false, true, pageSize);
185 ASSERT_EQ(0, processed);
186
187 int verified = verify(dst.c_str(), 4, true, true, pageSize);
Fabien Sanglard8163cfa2022-10-10 22:19:47 +0000188 ASSERT_EQ(0, verified);
189}