blob: aded30cd8601d356a181fdd6bc3c22fdbd6dda91 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Brian Carlstromb0460ea2011-07-29 10:08:05 -070016
Elliott Hughes90a33692011-08-30 13:27:07 -070017#include "zip_archive.h"
18
Brian Carlstromb0460ea2011-07-29 10:08:05 -070019#include <fcntl.h>
20#include <sys/stat.h>
21#include <sys/types.h>
Narayan Kamath92572be2013-11-28 14:06:24 +000022#include <zlib.h>
Ian Rogers700a4022014-05-19 16:49:03 -070023#include <memory>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070024
Ian Rogerse63db272014-07-15 15:36:11 -070025#include "base/unix_file/fd_file.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080026#include "common_runtime_test.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070027#include "os.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070028
29namespace art {
30
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080031class ZipArchiveTest : public CommonRuntimeTest {};
Brian Carlstromb0460ea2011-07-29 10:08:05 -070032
Brian Carlstromb0460ea2011-07-29 10:08:05 -070033TEST_F(ZipArchiveTest, FindAndExtract) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070034 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070035 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(GetLibCoreDexFileName().c_str(), &error_msg));
Ian Rogersb48b9eb2014-02-28 16:20:21 -080036 ASSERT_TRUE(zip_archive.get() != nullptr) << error_msg;
Ian Rogers8d31bbd2013-10-13 10:44:14 -070037 ASSERT_TRUE(error_msg.empty());
Ian Rogers700a4022014-05-19 16:49:03 -070038 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find("classes.dex", &error_msg));
Ian Rogersb48b9eb2014-02-28 16:20:21 -080039 ASSERT_TRUE(zip_entry.get() != nullptr);
Narayan Kamath92572be2013-11-28 14:06:24 +000040 ASSERT_TRUE(error_msg.empty());
Brian Carlstromb0460ea2011-07-29 10:08:05 -070041
Brian Carlstromdb4d5402011-08-09 12:18:28 -070042 ScratchFile tmp;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070043 ASSERT_NE(-1, tmp.GetFd());
Andreas Gampe4303ba92014-11-06 01:00:46 -080044 std::unique_ptr<File> file(new File(tmp.GetFd(), tmp.GetFilename(), false));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070045 ASSERT_TRUE(file.get() != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070046 bool success = zip_entry->ExtractToFile(*file, &error_msg);
47 ASSERT_TRUE(success) << error_msg;
48 ASSERT_TRUE(error_msg.empty());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070049 file.reset(nullptr);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070050
51 uint32_t computed_crc = crc32(0L, Z_NULL, 0);
Brian Carlstroma004aa92012-02-08 18:05:09 -080052 int fd = open(tmp.GetFilename().c_str(), O_RDONLY);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070053 ASSERT_NE(-1, fd);
54 const size_t kBufSize = 32768;
55 uint8_t buf[kBufSize];
56 while (true) {
57 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buf, kBufSize));
58 if (bytes_read == 0) {
59 break;
60 }
61 computed_crc = crc32(computed_crc, buf, bytes_read);
62 }
63 EXPECT_EQ(zip_entry->GetCrc32(), computed_crc);
64}
65
66} // namespace art