1 #include "gmock/gmock.h"
2 #include "gtest/gtest.h"
3
4 #include "ZipAlign.h"
5
6 #include <filesystem>
7 #include <stdio.h>
8 #include <string>
9
10 #include <android-base/file.h>
11
12 using namespace android;
13 using namespace base;
14
GetTestPath(const std::string & filename)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
GetTempPath(const std::string & filename)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
TEST(Align,Unaligned)26 TEST(Align, Unaligned) {
27 const std::string src = GetTestPath("unaligned.zip");
28 const std::string dst = GetTempPath("unaligned_out.zip");
29
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);
35 }
36
TEST(Align,DoubleAligment)37 TEST(Align, DoubleAligment) {
38 const std::string src = GetTestPath("unaligned.zip");
39 const std::string tmp = GetTempPath("da_aligned.zip");
40 const std::string dst = GetTempPath("da_d_aligner.zip");
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
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.
TEST(Align,Holes)68 TEST(Align, Holes) {
69 const std::string src = GetTestPath("holes.zip");
70 const std::string dst = GetTempPath("holes_out.zip");
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.
TEST(Align,DifferenteOrders)80 TEST(Align, DifferenteOrders) {
81 const std::string src = GetTestPath("diffOrders.zip");
82 const std::string dst = GetTempPath("diffOrders_out.zip");
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 }
90