• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "test/Test.h"
18 
19 namespace aapt {
20 
21 using ArchiveTest = TestDirectoryFixture;
22 
23 constexpr size_t kTestDataLength = 100;
24 
25 class TestData : public io::MallocData {
26  public:
TestData(std::unique_ptr<uint8_t[]> & data,size_t size)27   TestData(std::unique_ptr<uint8_t[]>& data, size_t size)
28       : MallocData(std::move(data), size) {}
29 
HadError() const30   bool HadError() const override { return !error_.empty(); }
31 
GetError() const32   std::string GetError() const override { return error_; }
33 
34   std::string error_;
35 };
36 
MakeTestArray()37 std::unique_ptr<uint8_t[]> MakeTestArray() {
38   auto array = std::make_unique<uint8_t[]>(kTestDataLength);
39   for (int index = 0; index < kTestDataLength; ++index) {
40     array[index] = static_cast<uint8_t>(rand());
41   }
42   return array;
43 }
44 
MakeDirectoryWriter(const std::string & output_path)45 std::unique_ptr<IArchiveWriter> MakeDirectoryWriter(const std::string& output_path) {
46   file::mkdirs(output_path);
47 
48   StdErrDiagnostics diag;
49   return CreateDirectoryArchiveWriter(&diag, output_path);
50 }
51 
MakeZipFileWriter(const std::string & output_path)52 std::unique_ptr<IArchiveWriter> MakeZipFileWriter(const std::string& output_path) {
53   file::mkdirs(file::GetStem(output_path).to_string());
54   std::remove(output_path.c_str());
55 
56   StdErrDiagnostics diag;
57   return CreateZipFileArchiveWriter(&diag, output_path);
58 }
59 
VerifyDirectory(const std::string & path,const std::string & file,const uint8_t array[])60 void VerifyDirectory(const std::string& path, const std::string& file, const uint8_t array[]) {
61   std::string file_path = file::BuildPath({path, file});
62   auto buffer = std::make_unique<char[]>(kTestDataLength);
63   std::ifstream stream(file_path);
64   stream.read(buffer.get(), kTestDataLength);
65 
66   for (int index = 0; index < kTestDataLength; ++index) {
67     ASSERT_EQ(array[index], static_cast<uint8_t>(buffer[index]));
68   }
69 }
70 
VerifyZipFile(const std::string & output_path,const std::string & file,const uint8_t array[])71 void VerifyZipFile(const std::string& output_path, const std::string& file, const uint8_t array[]) {
72   std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(output_path, nullptr);
73   std::unique_ptr<io::InputStream> stream = zip->FindFile(file)->OpenInputStream();
74 
75   std::vector<uint8_t> buffer;
76   const void* data;
77   size_t size;
78 
79   while (stream->Next(&data, &size)) {
80     auto pointer = static_cast<const uint8_t*>(data);
81     buffer.insert(buffer.end(), pointer, pointer + size);
82   }
83 
84   for (int index = 0; index < kTestDataLength; ++index) {
85     ASSERT_EQ(array[index], buffer[index]);
86   }
87 }
88 
TEST_F(ArchiveTest,DirectoryWriteEntrySuccess)89 TEST_F(ArchiveTest, DirectoryWriteEntrySuccess) {
90   std::string output_path = GetTestPath("output");
91   std::unique_ptr<IArchiveWriter> writer = MakeDirectoryWriter(output_path);
92   std::unique_ptr<uint8_t[]> data1 = MakeTestArray();
93   std::unique_ptr<uint8_t[]> data2 = MakeTestArray();
94 
95   ASSERT_TRUE(writer->StartEntry("test1", 0));
96   ASSERT_TRUE(writer->Write(static_cast<const void*>(data1.get()), kTestDataLength));
97   ASSERT_TRUE(writer->FinishEntry());
98   ASSERT_FALSE(writer->HadError());
99 
100   ASSERT_TRUE(writer->StartEntry("test2", 0));
101   ASSERT_TRUE(writer->Write(static_cast<const void*>(data2.get()), kTestDataLength));
102   ASSERT_TRUE(writer->FinishEntry());
103   ASSERT_FALSE(writer->HadError());
104 
105   writer.reset();
106 
107   VerifyDirectory(output_path, "test1", data1.get());
108   VerifyDirectory(output_path, "test2", data2.get());
109 }
110 
TEST_F(ArchiveTest,DirectoryWriteFileSuccess)111 TEST_F(ArchiveTest, DirectoryWriteFileSuccess) {
112   std::string output_path = GetTestPath("output");
113   std::unique_ptr<IArchiveWriter> writer = MakeDirectoryWriter(output_path);
114 
115   std::unique_ptr<uint8_t[]> data1 = MakeTestArray();
116   auto data1_copy = std::make_unique<uint8_t[]>(kTestDataLength);
117   std::copy(data1.get(), data1.get() + kTestDataLength, data1_copy.get());
118 
119   std::unique_ptr<uint8_t[]> data2 = MakeTestArray();
120   auto data2_copy = std::make_unique<uint8_t[]>(kTestDataLength);
121   std::copy(data2.get(), data2.get() + kTestDataLength, data2_copy.get());
122 
123   auto input1 = std::make_unique<TestData>(data1_copy, kTestDataLength);
124   auto input2 = std::make_unique<TestData>(data2_copy, kTestDataLength);
125 
126   ASSERT_TRUE(writer->WriteFile("test1", 0, input1.get()));
127   ASSERT_FALSE(writer->HadError());
128   ASSERT_TRUE(writer->WriteFile("test2", 0, input2.get()));
129   ASSERT_FALSE(writer->HadError());
130 
131   writer.reset();
132 
133   VerifyDirectory(output_path, "test1", data1.get());
134   VerifyDirectory(output_path, "test2", data2.get());
135 }
136 
TEST_F(ArchiveTest,DirectoryWriteFileError)137 TEST_F(ArchiveTest, DirectoryWriteFileError) {
138   std::string output_path = GetTestPath("output");
139   std::unique_ptr<IArchiveWriter> writer = MakeDirectoryWriter(output_path);
140   std::unique_ptr<uint8_t[]> data = MakeTestArray();
141   auto input = std::make_unique<TestData>(data, kTestDataLength);
142   input->error_ = "DirectoryWriteFileError";
143 
144   ASSERT_FALSE(writer->WriteFile("test", 0, input.get()));
145   ASSERT_TRUE(writer->HadError());
146   ASSERT_EQ("DirectoryWriteFileError", writer->GetError());
147 }
148 
TEST_F(ArchiveTest,ZipFileWriteEntrySuccess)149 TEST_F(ArchiveTest, ZipFileWriteEntrySuccess) {
150   std::string output_path = GetTestPath("output.apk");
151   std::unique_ptr<IArchiveWriter> writer = MakeZipFileWriter(output_path);
152   std::unique_ptr<uint8_t[]> data1 = MakeTestArray();
153   std::unique_ptr<uint8_t[]> data2 = MakeTestArray();
154 
155   ASSERT_TRUE(writer->StartEntry("test1", 0));
156   ASSERT_TRUE(writer->Write(static_cast<const void*>(data1.get()), kTestDataLength));
157   ASSERT_TRUE(writer->FinishEntry());
158   ASSERT_FALSE(writer->HadError());
159 
160   ASSERT_TRUE(writer->StartEntry("test2", 0));
161   ASSERT_TRUE(writer->Write(static_cast<const void*>(data2.get()), kTestDataLength));
162   ASSERT_TRUE(writer->FinishEntry());
163   ASSERT_FALSE(writer->HadError());
164 
165   writer.reset();
166 
167   VerifyZipFile(output_path, "test1", data1.get());
168   VerifyZipFile(output_path, "test2", data2.get());
169 }
170 
TEST_F(ArchiveTest,ZipFileWriteFileSuccess)171 TEST_F(ArchiveTest, ZipFileWriteFileSuccess) {
172   std::string output_path = GetTestPath("output.apk");
173   std::unique_ptr<IArchiveWriter> writer = MakeZipFileWriter(output_path);
174 
175   std::unique_ptr<uint8_t[]> data1 = MakeTestArray();
176   auto data1_copy = std::make_unique<uint8_t[]>(kTestDataLength);
177   std::copy(data1.get(), data1.get() + kTestDataLength, data1_copy.get());
178 
179   std::unique_ptr<uint8_t[]> data2 = MakeTestArray();
180   auto data2_copy = std::make_unique<uint8_t[]>(kTestDataLength);
181   std::copy(data2.get(), data2.get() + kTestDataLength, data2_copy.get());
182 
183   auto input1 = std::make_unique<TestData>(data1_copy, kTestDataLength);
184   auto input2 = std::make_unique<TestData>(data2_copy, kTestDataLength);
185 
186   ASSERT_TRUE(writer->WriteFile("test1", 0, input1.get()));
187   ASSERT_FALSE(writer->HadError());
188   ASSERT_TRUE(writer->WriteFile("test2", 0, input2.get()));
189   ASSERT_FALSE(writer->HadError());
190 
191   writer.reset();
192 
193   VerifyZipFile(output_path, "test1", data1.get());
194   VerifyZipFile(output_path, "test2", data2.get());
195 }
196 
TEST_F(ArchiveTest,ZipFileWriteFileError)197 TEST_F(ArchiveTest, ZipFileWriteFileError) {
198   std::string output_path = GetTestPath("output.apk");
199   std::unique_ptr<IArchiveWriter> writer = MakeZipFileWriter(output_path);
200   std::unique_ptr<uint8_t[]> data = MakeTestArray();
201   auto input = std::make_unique<TestData>(data, kTestDataLength);
202   input->error_ = "ZipFileWriteFileError";
203 
204   ASSERT_FALSE(writer->WriteFile("test", 0, input.get()));
205   ASSERT_TRUE(writer->HadError());
206   ASSERT_EQ("ZipFileWriteFileError", writer->GetError());
207 }
208 
209 }  // namespace aapt
210