Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 3 | #include "zip_archive.h" |
| 4 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 5 | #include <fcntl.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <sys/types.h> |
| 8 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 9 | #include "UniquePtr.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 10 | #include "common_test.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 11 | #include "gtest/gtest.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 12 | #include "os.h" |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 13 | |
| 14 | namespace art { |
| 15 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 16 | class ZipArchiveTest : public CommonTest {}; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 17 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 18 | TEST_F(ZipArchiveTest, FindAndExtract) { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 19 | UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(GetLibCoreDexFileName())); |
| 20 | ASSERT_TRUE(zip_archive.get() != false); |
| 21 | UniquePtr<ZipEntry> zip_entry(zip_archive->Find("classes.dex")); |
| 22 | ASSERT_TRUE(zip_entry.get() != false); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 23 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 24 | ScratchFile tmp; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 25 | ASSERT_NE(-1, tmp.GetFd()); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 26 | UniquePtr<File> file(OS::FileFromFd(tmp.GetFilename(), tmp.GetFd())); |
| 27 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 28 | bool success = zip_entry->Extract(*file); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 29 | ASSERT_TRUE(success); |
| 30 | close(tmp.GetFd()); |
| 31 | |
| 32 | uint32_t computed_crc = crc32(0L, Z_NULL, 0); |
| 33 | int fd = open(tmp.GetFilename(), O_RDONLY); |
| 34 | ASSERT_NE(-1, fd); |
| 35 | const size_t kBufSize = 32768; |
| 36 | uint8_t buf[kBufSize]; |
| 37 | while (true) { |
| 38 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buf, kBufSize)); |
| 39 | if (bytes_read == 0) { |
| 40 | break; |
| 41 | } |
| 42 | computed_crc = crc32(computed_crc, buf, bytes_read); |
| 43 | } |
| 44 | EXPECT_EQ(zip_entry->GetCrc32(), computed_crc); |
| 45 | } |
| 46 | |
| 47 | } // namespace art |