• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 // This load the whole file to memory so be careful!
sameContent(const std::string & path1,const std::string & path2)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 
GetTestPath(const std::string & filename)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 
GetTempPath(const std::string & filename)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 
TEST(Align,Unaligned)48 TEST(Align, Unaligned) {
49   const std::string src = GetTestPath("unaligned.zip");
50   const std::string dst = GetTempPath("unaligned_out.zip");
51 
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);
57 }
58 
TEST(Align,DoubleAligment)59 TEST(Align, DoubleAligment) {
60   const std::string src = GetTestPath("unaligned.zip");
61   const std::string tmp = GetTempPath("da_aligned.zip");
62   const std::string dst = GetTempPath("da_d_aligner.zip");
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 
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.
TEST(Align,Holes)90 TEST(Align, Holes) {
91   const std::string src = GetTestPath("holes.zip");
92   const std::string dst = GetTempPath("holes_out.zip");
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.
TEST(Align,DifferenteOrders)102 TEST(Align, DifferenteOrders) {
103   const std::string src = GetTestPath("diffOrders.zip");
104   const std::string dst = GetTempPath("diffOrders_out.zip");
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 }
112 
TEST(Align,DirectoryEntryDoNotRequireAlignment)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 
TEST(Align,DirectoryEntry)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 }
130