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