blob: c3da23c7915b619be1965bba85f60cd61c689d48 [file] [log] [blame]
Adam Lesinski622f3042015-10-05 18:16:18 -07001/*
2 * Copyright (C) 2015 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
Adam Lesinski622f3042015-10-05 18:16:18 -070017#include "ziparchive/zip_writer.h"
Jiyong Park6821cc82017-06-30 17:23:33 +090018#include "ziparchive/zip_archive.h"
Adam Lesinski622f3042015-10-05 18:16:18 -070019
Elliott Hughes53039d62015-12-04 22:00:26 -080020#include <android-base/test_utils.h>
Adam Lesinski622f3042015-10-05 18:16:18 -070021#include <gtest/gtest.h>
Christopher Ferris72082ab2016-01-19 10:33:03 -080022#include <time.h>
Adam Lesinski622f3042015-10-05 18:16:18 -070023#include <memory>
Christopher Ferrisfcc081f2015-11-04 17:54:32 -080024#include <vector>
Adam Lesinski622f3042015-10-05 18:16:18 -070025
Adam Lesinski2e216de2017-03-16 13:23:51 -070026static ::testing::AssertionResult AssertFileEntryContentsEq(const std::string& expected,
27 ZipArchiveHandle handle,
28 ZipEntry* zip_entry);
29
Adam Lesinski622f3042015-10-05 18:16:18 -070030struct zipwriter : public ::testing::Test {
31 TemporaryFile* temp_file_;
32 int fd_;
33 FILE* file_;
34
35 void SetUp() override {
36 temp_file_ = new TemporaryFile();
37 fd_ = temp_file_->fd;
38 file_ = fdopen(fd_, "w");
39 ASSERT_NE(file_, nullptr);
40 }
41
42 void TearDown() override {
43 fclose(file_);
44 delete temp_file_;
45 }
46};
47
48TEST_F(zipwriter, WriteUncompressedZipWithOneFile) {
49 ZipWriter writer(file_);
50
51 const char* expected = "hello";
52
Adam Lesinskic322b432015-10-06 15:23:46 -070053 ASSERT_EQ(0, writer.StartEntry("file.txt", 0));
54 ASSERT_EQ(0, writer.WriteBytes("he", 2));
55 ASSERT_EQ(0, writer.WriteBytes("llo", 3));
56 ASSERT_EQ(0, writer.FinishEntry());
57 ASSERT_EQ(0, writer.Finish());
Adam Lesinski622f3042015-10-05 18:16:18 -070058
Adam Lesinskic322b432015-10-06 15:23:46 -070059 ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
Adam Lesinski622f3042015-10-05 18:16:18 -070060
61 ZipArchiveHandle handle;
Adam Lesinskic322b432015-10-06 15:23:46 -070062 ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
Adam Lesinski622f3042015-10-05 18:16:18 -070063
64 ZipEntry data;
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -070065 ASSERT_EQ(0, FindEntry(handle, "file.txt", &data));
Adam Lesinskic322b432015-10-06 15:23:46 -070066 EXPECT_EQ(kCompressStored, data.method);
Adam Lesinskif61aaf42017-03-29 16:10:11 -070067 EXPECT_EQ(0u, data.has_data_descriptor);
Adam Lesinski2e216de2017-03-16 13:23:51 -070068 EXPECT_EQ(strlen(expected), data.compressed_length);
69 ASSERT_EQ(strlen(expected), data.uncompressed_length);
70 ASSERT_TRUE(AssertFileEntryContentsEq(expected, handle, &data));
Adam Lesinski622f3042015-10-05 18:16:18 -070071
72 CloseArchive(handle);
73}
74
75TEST_F(zipwriter, WriteUncompressedZipWithMultipleFiles) {
76 ZipWriter writer(file_);
77
Adam Lesinskic322b432015-10-06 15:23:46 -070078 ASSERT_EQ(0, writer.StartEntry("file.txt", 0));
79 ASSERT_EQ(0, writer.WriteBytes("he", 2));
80 ASSERT_EQ(0, writer.FinishEntry());
Adam Lesinski622f3042015-10-05 18:16:18 -070081
Adam Lesinskic322b432015-10-06 15:23:46 -070082 ASSERT_EQ(0, writer.StartEntry("file/file.txt", 0));
83 ASSERT_EQ(0, writer.WriteBytes("llo", 3));
84 ASSERT_EQ(0, writer.FinishEntry());
Adam Lesinski622f3042015-10-05 18:16:18 -070085
Adam Lesinskic322b432015-10-06 15:23:46 -070086 ASSERT_EQ(0, writer.StartEntry("file/file2.txt", 0));
87 ASSERT_EQ(0, writer.FinishEntry());
Adam Lesinski622f3042015-10-05 18:16:18 -070088
Adam Lesinskic322b432015-10-06 15:23:46 -070089 ASSERT_EQ(0, writer.Finish());
Adam Lesinski622f3042015-10-05 18:16:18 -070090
Adam Lesinskic322b432015-10-06 15:23:46 -070091 ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
Adam Lesinski622f3042015-10-05 18:16:18 -070092
93 ZipArchiveHandle handle;
Adam Lesinskic322b432015-10-06 15:23:46 -070094 ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
Adam Lesinski622f3042015-10-05 18:16:18 -070095
Adam Lesinski622f3042015-10-05 18:16:18 -070096 ZipEntry data;
97
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -070098 ASSERT_EQ(0, FindEntry(handle, "file.txt", &data));
Adam Lesinskic322b432015-10-06 15:23:46 -070099 EXPECT_EQ(kCompressStored, data.method);
100 EXPECT_EQ(2u, data.compressed_length);
Adam Lesinski2e216de2017-03-16 13:23:51 -0700101 ASSERT_EQ(2u, data.uncompressed_length);
102 ASSERT_TRUE(AssertFileEntryContentsEq("he", handle, &data));
Adam Lesinski622f3042015-10-05 18:16:18 -0700103
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -0700104 ASSERT_EQ(0, FindEntry(handle, "file/file.txt", &data));
Adam Lesinskic322b432015-10-06 15:23:46 -0700105 EXPECT_EQ(kCompressStored, data.method);
106 EXPECT_EQ(3u, data.compressed_length);
Adam Lesinski2e216de2017-03-16 13:23:51 -0700107 ASSERT_EQ(3u, data.uncompressed_length);
108 ASSERT_TRUE(AssertFileEntryContentsEq("llo", handle, &data));
Adam Lesinski622f3042015-10-05 18:16:18 -0700109
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -0700110 ASSERT_EQ(0, FindEntry(handle, "file/file2.txt", &data));
Adam Lesinskic322b432015-10-06 15:23:46 -0700111 EXPECT_EQ(kCompressStored, data.method);
112 EXPECT_EQ(0u, data.compressed_length);
113 EXPECT_EQ(0u, data.uncompressed_length);
Adam Lesinski622f3042015-10-05 18:16:18 -0700114
115 CloseArchive(handle);
116}
117
Christopher Ferris72082ab2016-01-19 10:33:03 -0800118TEST_F(zipwriter, WriteUncompressedZipFileWithAlignedFlag) {
Adam Lesinski622f3042015-10-05 18:16:18 -0700119 ZipWriter writer(file_);
120
Adam Lesinskic322b432015-10-06 15:23:46 -0700121 ASSERT_EQ(0, writer.StartEntry("align.txt", ZipWriter::kAlign32));
122 ASSERT_EQ(0, writer.WriteBytes("he", 2));
123 ASSERT_EQ(0, writer.FinishEntry());
124 ASSERT_EQ(0, writer.Finish());
Adam Lesinski622f3042015-10-05 18:16:18 -0700125
Adam Lesinskic322b432015-10-06 15:23:46 -0700126 ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
Adam Lesinski622f3042015-10-05 18:16:18 -0700127
128 ZipArchiveHandle handle;
Adam Lesinskic322b432015-10-06 15:23:46 -0700129 ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
Adam Lesinski622f3042015-10-05 18:16:18 -0700130
131 ZipEntry data;
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -0700132 ASSERT_EQ(0, FindEntry(handle, "align.txt", &data));
Adam Lesinskic322b432015-10-06 15:23:46 -0700133 EXPECT_EQ(0, data.offset & 0x03);
134
135 CloseArchive(handle);
136}
137
Yabin Cui6f71fb72016-02-08 16:26:33 -0800138static struct tm MakeTm() {
139 struct tm tm;
140 memset(&tm, 0, sizeof(struct tm));
141 tm.tm_year = 2001 - 1900;
142 tm.tm_mon = 1;
143 tm.tm_mday = 12;
144 tm.tm_hour = 18;
145 tm.tm_min = 30;
146 tm.tm_sec = 20;
147 return tm;
148}
149
Christopher Ferris72082ab2016-01-19 10:33:03 -0800150TEST_F(zipwriter, WriteUncompressedZipFileWithAlignedFlagAndTime) {
151 ZipWriter writer(file_);
152
Yabin Cui6f71fb72016-02-08 16:26:33 -0800153 struct tm tm = MakeTm();
Christopher Ferris72082ab2016-01-19 10:33:03 -0800154 time_t time = mktime(&tm);
155 ASSERT_EQ(0, writer.StartEntryWithTime("align.txt", ZipWriter::kAlign32, time));
156 ASSERT_EQ(0, writer.WriteBytes("he", 2));
157 ASSERT_EQ(0, writer.FinishEntry());
158 ASSERT_EQ(0, writer.Finish());
159
160 ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
161
162 ZipArchiveHandle handle;
163 ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
164
165 ZipEntry data;
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -0700166 ASSERT_EQ(0, FindEntry(handle, "align.txt", &data));
Christopher Ferris72082ab2016-01-19 10:33:03 -0800167 EXPECT_EQ(0, data.offset & 0x03);
168
Elliott Hughes6702cef2017-05-28 22:59:04 -0700169 struct tm mod = data.GetModificationTime();
Christopher Ferris72082ab2016-01-19 10:33:03 -0800170 EXPECT_EQ(tm.tm_sec, mod.tm_sec);
171 EXPECT_EQ(tm.tm_min, mod.tm_min);
172 EXPECT_EQ(tm.tm_hour, mod.tm_hour);
173 EXPECT_EQ(tm.tm_mday, mod.tm_mday);
174 EXPECT_EQ(tm.tm_mon, mod.tm_mon);
175 EXPECT_EQ(tm.tm_year, mod.tm_year);
176
177 CloseArchive(handle);
178}
179
180TEST_F(zipwriter, WriteUncompressedZipFileWithAlignedValue) {
181 ZipWriter writer(file_);
182
183 ASSERT_EQ(0, writer.StartAlignedEntry("align.txt", 0, 4096));
184 ASSERT_EQ(0, writer.WriteBytes("he", 2));
185 ASSERT_EQ(0, writer.FinishEntry());
186 ASSERT_EQ(0, writer.Finish());
187
188 ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
189
190 ZipArchiveHandle handle;
191 ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
192
193 ZipEntry data;
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -0700194 ASSERT_EQ(0, FindEntry(handle, "align.txt", &data));
Christopher Ferris72082ab2016-01-19 10:33:03 -0800195 EXPECT_EQ(0, data.offset & 0xfff);
196
197 CloseArchive(handle);
198}
199
200TEST_F(zipwriter, WriteUncompressedZipFileWithAlignedValueAndTime) {
201 ZipWriter writer(file_);
202
Yabin Cui6f71fb72016-02-08 16:26:33 -0800203 struct tm tm = MakeTm();
Christopher Ferris72082ab2016-01-19 10:33:03 -0800204 time_t time = mktime(&tm);
205 ASSERT_EQ(0, writer.StartAlignedEntryWithTime("align.txt", 0, time, 4096));
206 ASSERT_EQ(0, writer.WriteBytes("he", 2));
207 ASSERT_EQ(0, writer.FinishEntry());
208 ASSERT_EQ(0, writer.Finish());
209
210 ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
211
212 ZipArchiveHandle handle;
213 ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
214
215 ZipEntry data;
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -0700216 ASSERT_EQ(0, FindEntry(handle, "align.txt", &data));
Christopher Ferris72082ab2016-01-19 10:33:03 -0800217 EXPECT_EQ(0, data.offset & 0xfff);
218
Elliott Hughes6702cef2017-05-28 22:59:04 -0700219 struct tm mod = data.GetModificationTime();
Christopher Ferris72082ab2016-01-19 10:33:03 -0800220 EXPECT_EQ(tm.tm_sec, mod.tm_sec);
221 EXPECT_EQ(tm.tm_min, mod.tm_min);
222 EXPECT_EQ(tm.tm_hour, mod.tm_hour);
223 EXPECT_EQ(tm.tm_mday, mod.tm_mday);
224 EXPECT_EQ(tm.tm_mon, mod.tm_mon);
225 EXPECT_EQ(tm.tm_year, mod.tm_year);
226
227 CloseArchive(handle);
228}
229
Adam Lesinskic322b432015-10-06 15:23:46 -0700230TEST_F(zipwriter, WriteCompressedZipWithOneFile) {
231 ZipWriter writer(file_);
232
233 ASSERT_EQ(0, writer.StartEntry("file.txt", ZipWriter::kCompress));
234 ASSERT_EQ(0, writer.WriteBytes("helo", 4));
235 ASSERT_EQ(0, writer.FinishEntry());
236 ASSERT_EQ(0, writer.Finish());
237
238 ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
239
240 ZipArchiveHandle handle;
241 ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
242
243 ZipEntry data;
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -0700244 ASSERT_EQ(0, FindEntry(handle, "file.txt", &data));
Adam Lesinskic322b432015-10-06 15:23:46 -0700245 EXPECT_EQ(kCompressDeflated, data.method);
Adam Lesinski2e216de2017-03-16 13:23:51 -0700246 ASSERT_EQ(4u, data.uncompressed_length);
247 ASSERT_TRUE(AssertFileEntryContentsEq("helo", handle, &data));
Adam Lesinskic322b432015-10-06 15:23:46 -0700248
249 CloseArchive(handle);
Adam Lesinski622f3042015-10-05 18:16:18 -0700250}
Christopher Ferrisfcc081f2015-11-04 17:54:32 -0800251
252TEST_F(zipwriter, WriteCompressedZipFlushFull) {
253 // This exact data will cause the Finish() to require multiple calls
254 // to deflate() because the ZipWriter buffer isn't big enough to hold
255 // the entire compressed data buffer.
256 constexpr size_t kBufSize = 10000000;
257 std::vector<uint8_t> buffer(kBufSize);
258 size_t prev = 1;
259 for (size_t i = 0; i < kBufSize; i++) {
Andreas Gampe5a5ffb52019-04-05 13:48:02 -0700260 buffer[i] = static_cast<uint8_t>(i + prev);
Christopher Ferrisfcc081f2015-11-04 17:54:32 -0800261 prev = i;
262 }
263
264 ZipWriter writer(file_);
265 ASSERT_EQ(0, writer.StartEntry("file.txt", ZipWriter::kCompress));
266 ASSERT_EQ(0, writer.WriteBytes(buffer.data(), buffer.size()));
267 ASSERT_EQ(0, writer.FinishEntry());
268 ASSERT_EQ(0, writer.Finish());
269
270 ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
271
272 ZipArchiveHandle handle;
273 ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
274
275 ZipEntry data;
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -0700276 ASSERT_EQ(0, FindEntry(handle, "file.txt", &data));
Christopher Ferrisfcc081f2015-11-04 17:54:32 -0800277 EXPECT_EQ(kCompressDeflated, data.method);
278 EXPECT_EQ(kBufSize, data.uncompressed_length);
279
280 std::vector<uint8_t> decompress(kBufSize);
281 memset(decompress.data(), 0, kBufSize);
Andreas Gampe5a5ffb52019-04-05 13:48:02 -0700282 ASSERT_EQ(0, ExtractToMemory(handle, &data, decompress.data(),
283 static_cast<uint32_t>(decompress.size())));
Christopher Ferrisfcc081f2015-11-04 17:54:32 -0800284 EXPECT_EQ(0, memcmp(decompress.data(), buffer.data(), kBufSize))
285 << "Input buffer and output buffer are different.";
286
287 CloseArchive(handle);
288}
Christopher Ferris72082ab2016-01-19 10:33:03 -0800289
290TEST_F(zipwriter, CheckStartEntryErrors) {
291 ZipWriter writer(file_);
292
293 ASSERT_EQ(-5, writer.StartAlignedEntry("align.txt", ZipWriter::kAlign32, 4096));
294 ASSERT_EQ(-6, writer.StartAlignedEntry("align.txt", 0, 3));
295}
Adam Lesinski2e216de2017-03-16 13:23:51 -0700296
297TEST_F(zipwriter, BackupRemovesTheLastFile) {
298 ZipWriter writer(file_);
299
300 const char* kKeepThis = "keep this";
301 const char* kDropThis = "drop this";
302 const char* kReplaceWithThis = "replace with this";
303
304 ZipWriter::FileEntry entry;
305 EXPECT_LT(writer.GetLastEntry(&entry), 0);
306
307 ASSERT_EQ(0, writer.StartEntry("keep.txt", 0));
308 ASSERT_EQ(0, writer.WriteBytes(kKeepThis, strlen(kKeepThis)));
309 ASSERT_EQ(0, writer.FinishEntry());
310
311 ASSERT_EQ(0, writer.GetLastEntry(&entry));
312 EXPECT_EQ("keep.txt", entry.path);
313
314 ASSERT_EQ(0, writer.StartEntry("drop.txt", 0));
315 ASSERT_EQ(0, writer.WriteBytes(kDropThis, strlen(kDropThis)));
316 ASSERT_EQ(0, writer.FinishEntry());
317
318 ASSERT_EQ(0, writer.GetLastEntry(&entry));
319 EXPECT_EQ("drop.txt", entry.path);
320
321 ASSERT_EQ(0, writer.DiscardLastEntry());
322
323 ASSERT_EQ(0, writer.GetLastEntry(&entry));
324 EXPECT_EQ("keep.txt", entry.path);
325
326 ASSERT_EQ(0, writer.StartEntry("replace.txt", 0));
327 ASSERT_EQ(0, writer.WriteBytes(kReplaceWithThis, strlen(kReplaceWithThis)));
328 ASSERT_EQ(0, writer.FinishEntry());
329
330 ASSERT_EQ(0, writer.GetLastEntry(&entry));
331 EXPECT_EQ("replace.txt", entry.path);
332
333 ASSERT_EQ(0, writer.Finish());
334
335 // Verify that "drop.txt" does not exist.
336
337 ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
338
339 ZipArchiveHandle handle;
340 ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
341
342 ZipEntry data;
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -0700343 ASSERT_EQ(0, FindEntry(handle, "keep.txt", &data));
Adam Lesinski2e216de2017-03-16 13:23:51 -0700344 ASSERT_TRUE(AssertFileEntryContentsEq(kKeepThis, handle, &data));
345
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -0700346 ASSERT_NE(0, FindEntry(handle, "drop.txt", &data));
Adam Lesinski2e216de2017-03-16 13:23:51 -0700347
Elliott Hughes0b4bf2f2019-05-03 22:38:44 -0700348 ASSERT_EQ(0, FindEntry(handle, "replace.txt", &data));
Adam Lesinski2e216de2017-03-16 13:23:51 -0700349 ASSERT_TRUE(AssertFileEntryContentsEq(kReplaceWithThis, handle, &data));
350
351 CloseArchive(handle);
352}
353
354TEST_F(zipwriter, TruncateFileAfterBackup) {
355 ZipWriter writer(file_);
356
357 const char* kSmall = "small";
358
359 ASSERT_EQ(0, writer.StartEntry("small.txt", 0));
360 ASSERT_EQ(0, writer.WriteBytes(kSmall, strlen(kSmall)));
361 ASSERT_EQ(0, writer.FinishEntry());
362
363 ASSERT_EQ(0, writer.StartEntry("large.txt", 0));
364 std::vector<uint8_t> data;
Jiyong Park6821cc82017-06-30 17:23:33 +0900365 data.resize(1024 * 1024, 0xef);
Adam Lesinski2e216de2017-03-16 13:23:51 -0700366 ASSERT_EQ(0, writer.WriteBytes(data.data(), data.size()));
367 ASSERT_EQ(0, writer.FinishEntry());
368
Adam Lesinski48bd4852017-03-23 11:57:05 -0700369 off_t before_len = ftello(file_);
Adam Lesinski2e216de2017-03-16 13:23:51 -0700370
371 ZipWriter::FileEntry entry;
372 ASSERT_EQ(0, writer.GetLastEntry(&entry));
373 ASSERT_EQ(0, writer.DiscardLastEntry());
374
375 ASSERT_EQ(0, writer.Finish());
376
Adam Lesinski48bd4852017-03-23 11:57:05 -0700377 off_t after_len = ftello(file_);
Adam Lesinski2e216de2017-03-16 13:23:51 -0700378
379 ASSERT_GT(before_len, after_len);
380}
381
382static ::testing::AssertionResult AssertFileEntryContentsEq(const std::string& expected,
383 ZipArchiveHandle handle,
384 ZipEntry* zip_entry) {
385 if (expected.size() != zip_entry->uncompressed_length) {
Jiyong Park6821cc82017-06-30 17:23:33 +0900386 return ::testing::AssertionFailure()
387 << "uncompressed entry size " << zip_entry->uncompressed_length
388 << " does not match expected size " << expected.size();
Adam Lesinski2e216de2017-03-16 13:23:51 -0700389 }
390
391 std::string actual;
392 actual.resize(expected.size());
393
394 uint8_t* buffer = reinterpret_cast<uint8_t*>(&*actual.begin());
Andreas Gampe5a5ffb52019-04-05 13:48:02 -0700395 if (ExtractToMemory(handle, zip_entry, buffer, static_cast<uint32_t>(actual.size())) != 0) {
Adam Lesinski2e216de2017-03-16 13:23:51 -0700396 return ::testing::AssertionFailure() << "failed to extract entry";
397 }
398
399 if (expected != actual) {
400 return ::testing::AssertionFailure() << "actual zip_entry data '" << actual
Jiyong Park6821cc82017-06-30 17:23:33 +0900401 << "' does not match expected '" << expected << "'";
Adam Lesinski2e216de2017-03-16 13:23:51 -0700402 }
403 return ::testing::AssertionSuccess();
404}