blob: 19f4ee0a33ab3c2480c4d354d6385f0e6c5dcd8e [file] [log] [blame]
Brian Carlstromb0460ea2011-07-29 10:08:05 -07001// 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 Carlstromdb4d5402011-08-09 12:18:28 -07008#include "os.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -07009#include "zip_archive.h"
10#include "gtest/gtest.h"
11
12namespace art {
13
Brian Carlstromf734cf52011-08-17 16:28:14 -070014class ZipArchiveTest : public CommonTest {};
Brian Carlstromb0460ea2011-07-29 10:08:05 -070015
Brian Carlstromb0460ea2011-07-29 10:08:05 -070016TEST_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 Carlstromdb4d5402011-08-09 12:18:28 -070022 ScratchFile tmp;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070023 ASSERT_NE(-1, tmp.GetFd());
Brian Carlstromdb4d5402011-08-09 12:18:28 -070024 scoped_ptr<File> file(OS::FileFromFd(tmp.GetFilename(), tmp.GetFd()));
25 ASSERT_TRUE(file != NULL);
26 bool success = zip_entry->Extract(*file);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070027 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