blob: c2842734eeaaec7a12bbffae503c3bdac35c99fe [file] [log] [blame]
Adam Lesinskiad4ad8c2015-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 Lesinskiad4ad8c2015-10-05 18:16:18 -070017#include "ziparchive/zip_writer.h"
Jiyong Parkcd997e62017-06-30 17:23:33 +090018#include "ziparchive/zip_archive.h"
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070019
Elliott Hughes4f713192015-12-04 22:00:26 -080020#include <android-base/test_utils.h>
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070021#include <gtest/gtest.h>
Christopher Ferris5e9f3d42016-01-19 10:33:03 -080022#include <time.h>
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070023#include <memory>
Christopher Ferrisa2a32b02015-11-04 17:54:32 -080024#include <vector>
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070025
Adam Lesinski537713b2017-03-16 13:23:51 -070026static ::testing::AssertionResult AssertFileEntryContentsEq(const std::string& expected,
27 ZipArchiveHandle handle,
28 ZipEntry* zip_entry);
29
Adam Lesinskiad4ad8c2015-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 Lesinski591fd392015-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 Lesinskiad4ad8c2015-10-05 18:16:18 -070058
Adam Lesinski591fd392015-10-06 15:23:46 -070059 ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070060
61 ZipArchiveHandle handle;
Adam Lesinski591fd392015-10-06 15:23:46 -070062 ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070063
64 ZipEntry data;
Adam Lesinski591fd392015-10-06 15:23:46 -070065 ASSERT_EQ(0, FindEntry(handle, ZipString("file.txt"), &data));
Adam Lesinski591fd392015-10-06 15:23:46 -070066 EXPECT_EQ(kCompressStored, data.method);
Adam Lesinskie2fa70b2017-03-29 16:10:11 -070067 EXPECT_EQ(0u, data.has_data_descriptor);
Adam Lesinski537713b2017-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 Lesinskiad4ad8c2015-10-05 18:16:18 -070071
72 CloseArchive(handle);
73}
74
75TEST_F(zipwriter, WriteUncompressedZipWithMultipleFiles) {
76 ZipWriter writer(file_);
77
Adam Lesinski591fd392015-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 Lesinskiad4ad8c2015-10-05 18:16:18 -070081
Adam Lesinski591fd392015-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 Lesinskiad4ad8c2015-10-05 18:16:18 -070085
Adam Lesinski591fd392015-10-06 15:23:46 -070086 ASSERT_EQ(0, writer.StartEntry("file/file2.txt", 0));
87 ASSERT_EQ(0, writer.FinishEntry());
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070088
Adam Lesinski591fd392015-10-06 15:23:46 -070089 ASSERT_EQ(0, writer.Finish());
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070090
Adam Lesinski591fd392015-10-06 15:23:46 -070091 ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070092
93 ZipArchiveHandle handle;
Adam Lesinski591fd392015-10-06 15:23:46 -070094 ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070095
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070096 ZipEntry data;
97
Adam Lesinski591fd392015-10-06 15:23:46 -070098 ASSERT_EQ(0, FindEntry(handle, ZipString("file.txt"), &data));
99 EXPECT_EQ(kCompressStored, data.method);
100 EXPECT_EQ(2u, data.compressed_length);
Adam Lesinski537713b2017-03-16 13:23:51 -0700101 ASSERT_EQ(2u, data.uncompressed_length);
102 ASSERT_TRUE(AssertFileEntryContentsEq("he", handle, &data));
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700103
Adam Lesinski591fd392015-10-06 15:23:46 -0700104 ASSERT_EQ(0, FindEntry(handle, ZipString("file/file.txt"), &data));
105 EXPECT_EQ(kCompressStored, data.method);
106 EXPECT_EQ(3u, data.compressed_length);
Adam Lesinski537713b2017-03-16 13:23:51 -0700107 ASSERT_EQ(3u, data.uncompressed_length);
108 ASSERT_TRUE(AssertFileEntryContentsEq("llo", handle, &data));
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700109
Adam Lesinski591fd392015-10-06 15:23:46 -0700110 ASSERT_EQ(0, FindEntry(handle, ZipString("file/file2.txt"), &data));
111 EXPECT_EQ(kCompressStored, data.method);
112 EXPECT_EQ(0u, data.compressed_length);
113 EXPECT_EQ(0u, data.uncompressed_length);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700114
115 CloseArchive(handle);
116}
117
Christopher Ferris5e9f3d42016-01-19 10:33:03 -0800118TEST_F(zipwriter, WriteUncompressedZipFileWithAlignedFlag) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700119 ZipWriter writer(file_);
120
Adam Lesinski591fd392015-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 Lesinskiad4ad8c2015-10-05 18:16:18 -0700125
Adam Lesinski591fd392015-10-06 15:23:46 -0700126 ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700127
128 ZipArchiveHandle handle;
Adam Lesinski591fd392015-10-06 15:23:46 -0700129 ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700130
131 ZipEntry data;
Adam Lesinski591fd392015-10-06 15:23:46 -0700132 ASSERT_EQ(0, FindEntry(handle, ZipString("align.txt"), &data));
133 EXPECT_EQ(0, data.offset & 0x03);
134
135 CloseArchive(handle);
136}
137
Yabin Cui8e6f7222016-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 Ferris5e9f3d42016-01-19 10:33:03 -0800150TEST_F(zipwriter, WriteUncompressedZipFileWithAlignedFlagAndTime) {
151 ZipWriter writer(file_);
152
Yabin Cui8e6f7222016-02-08 16:26:33 -0800153 struct tm tm = MakeTm();
Christopher Ferris5e9f3d42016-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;
166 ASSERT_EQ(0, FindEntry(handle, ZipString("align.txt"), &data));
167 EXPECT_EQ(0, data.offset & 0x03);
168
Elliott Hughes55fd2932017-05-28 22:59:04 -0700169 struct tm mod = data.GetModificationTime();
Christopher Ferris5e9f3d42016-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;
194 ASSERT_EQ(0, FindEntry(handle, ZipString("align.txt"), &data));
195 EXPECT_EQ(0, data.offset & 0xfff);
196
197 CloseArchive(handle);
198}
199
200TEST_F(zipwriter, WriteUncompressedZipFileWithAlignedValueAndTime) {
201 ZipWriter writer(file_);
202
Yabin Cui8e6f7222016-02-08 16:26:33 -0800203 struct tm tm = MakeTm();
Christopher Ferris5e9f3d42016-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;
216 ASSERT_EQ(0, FindEntry(handle, ZipString("align.txt"), &data));
217 EXPECT_EQ(0, data.offset & 0xfff);
218
Elliott Hughes55fd2932017-05-28 22:59:04 -0700219 struct tm mod = data.GetModificationTime();
Christopher Ferris5e9f3d42016-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 Lesinski591fd392015-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;
244 ASSERT_EQ(0, FindEntry(handle, ZipString("file.txt"), &data));
245 EXPECT_EQ(kCompressDeflated, data.method);
Adam Lesinski537713b2017-03-16 13:23:51 -0700246 ASSERT_EQ(4u, data.uncompressed_length);
247 ASSERT_TRUE(AssertFileEntryContentsEq("helo", handle, &data));
Adam Lesinski591fd392015-10-06 15:23:46 -0700248
249 CloseArchive(handle);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700250}
Christopher Ferrisa2a32b02015-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++) {
260 buffer[i] = i + prev;
261 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;
276 ASSERT_EQ(0, FindEntry(handle, ZipString("file.txt"), &data));
277 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);
282 ASSERT_EQ(0, ExtractToMemory(handle, &data, decompress.data(), decompress.size()));
283 EXPECT_EQ(0, memcmp(decompress.data(), buffer.data(), kBufSize))
284 << "Input buffer and output buffer are different.";
285
286 CloseArchive(handle);
287}
Christopher Ferris5e9f3d42016-01-19 10:33:03 -0800288
289TEST_F(zipwriter, CheckStartEntryErrors) {
290 ZipWriter writer(file_);
291
292 ASSERT_EQ(-5, writer.StartAlignedEntry("align.txt", ZipWriter::kAlign32, 4096));
293 ASSERT_EQ(-6, writer.StartAlignedEntry("align.txt", 0, 3));
294}
Adam Lesinski537713b2017-03-16 13:23:51 -0700295
296TEST_F(zipwriter, BackupRemovesTheLastFile) {
297 ZipWriter writer(file_);
298
299 const char* kKeepThis = "keep this";
300 const char* kDropThis = "drop this";
301 const char* kReplaceWithThis = "replace with this";
302
303 ZipWriter::FileEntry entry;
304 EXPECT_LT(writer.GetLastEntry(&entry), 0);
305
306 ASSERT_EQ(0, writer.StartEntry("keep.txt", 0));
307 ASSERT_EQ(0, writer.WriteBytes(kKeepThis, strlen(kKeepThis)));
308 ASSERT_EQ(0, writer.FinishEntry());
309
310 ASSERT_EQ(0, writer.GetLastEntry(&entry));
311 EXPECT_EQ("keep.txt", entry.path);
312
313 ASSERT_EQ(0, writer.StartEntry("drop.txt", 0));
314 ASSERT_EQ(0, writer.WriteBytes(kDropThis, strlen(kDropThis)));
315 ASSERT_EQ(0, writer.FinishEntry());
316
317 ASSERT_EQ(0, writer.GetLastEntry(&entry));
318 EXPECT_EQ("drop.txt", entry.path);
319
320 ASSERT_EQ(0, writer.DiscardLastEntry());
321
322 ASSERT_EQ(0, writer.GetLastEntry(&entry));
323 EXPECT_EQ("keep.txt", entry.path);
324
325 ASSERT_EQ(0, writer.StartEntry("replace.txt", 0));
326 ASSERT_EQ(0, writer.WriteBytes(kReplaceWithThis, strlen(kReplaceWithThis)));
327 ASSERT_EQ(0, writer.FinishEntry());
328
329 ASSERT_EQ(0, writer.GetLastEntry(&entry));
330 EXPECT_EQ("replace.txt", entry.path);
331
332 ASSERT_EQ(0, writer.Finish());
333
334 // Verify that "drop.txt" does not exist.
335
336 ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
337
338 ZipArchiveHandle handle;
339 ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
340
341 ZipEntry data;
342 ASSERT_EQ(0, FindEntry(handle, ZipString("keep.txt"), &data));
343 ASSERT_TRUE(AssertFileEntryContentsEq(kKeepThis, handle, &data));
344
345 ASSERT_NE(0, FindEntry(handle, ZipString("drop.txt"), &data));
346
347 ASSERT_EQ(0, FindEntry(handle, ZipString("replace.txt"), &data));
348 ASSERT_TRUE(AssertFileEntryContentsEq(kReplaceWithThis, handle, &data));
349
350 CloseArchive(handle);
351}
352
353TEST_F(zipwriter, TruncateFileAfterBackup) {
354 ZipWriter writer(file_);
355
356 const char* kSmall = "small";
357
358 ASSERT_EQ(0, writer.StartEntry("small.txt", 0));
359 ASSERT_EQ(0, writer.WriteBytes(kSmall, strlen(kSmall)));
360 ASSERT_EQ(0, writer.FinishEntry());
361
362 ASSERT_EQ(0, writer.StartEntry("large.txt", 0));
363 std::vector<uint8_t> data;
Jiyong Parkcd997e62017-06-30 17:23:33 +0900364 data.resize(1024 * 1024, 0xef);
Adam Lesinski537713b2017-03-16 13:23:51 -0700365 ASSERT_EQ(0, writer.WriteBytes(data.data(), data.size()));
366 ASSERT_EQ(0, writer.FinishEntry());
367
Adam Lesinskib02d6902017-03-23 11:57:05 -0700368 off_t before_len = ftello(file_);
Adam Lesinski537713b2017-03-16 13:23:51 -0700369
370 ZipWriter::FileEntry entry;
371 ASSERT_EQ(0, writer.GetLastEntry(&entry));
372 ASSERT_EQ(0, writer.DiscardLastEntry());
373
374 ASSERT_EQ(0, writer.Finish());
375
Adam Lesinskib02d6902017-03-23 11:57:05 -0700376 off_t after_len = ftello(file_);
Adam Lesinski537713b2017-03-16 13:23:51 -0700377
378 ASSERT_GT(before_len, after_len);
379}
380
381static ::testing::AssertionResult AssertFileEntryContentsEq(const std::string& expected,
382 ZipArchiveHandle handle,
383 ZipEntry* zip_entry) {
384 if (expected.size() != zip_entry->uncompressed_length) {
Jiyong Parkcd997e62017-06-30 17:23:33 +0900385 return ::testing::AssertionFailure()
386 << "uncompressed entry size " << zip_entry->uncompressed_length
387 << " does not match expected size " << expected.size();
Adam Lesinski537713b2017-03-16 13:23:51 -0700388 }
389
390 std::string actual;
391 actual.resize(expected.size());
392
393 uint8_t* buffer = reinterpret_cast<uint8_t*>(&*actual.begin());
394 if (ExtractToMemory(handle, zip_entry, buffer, actual.size()) != 0) {
395 return ::testing::AssertionFailure() << "failed to extract entry";
396 }
397
398 if (expected != actual) {
399 return ::testing::AssertionFailure() << "actual zip_entry data '" << actual
Jiyong Parkcd997e62017-06-30 17:23:33 +0900400 << "' does not match expected '" << expected << "'";
Adam Lesinski537713b2017-03-16 13:23:51 -0700401 }
402 return ::testing::AssertionSuccess();
403}