blob: 16394b01280b789e9f8fb0b58b2dd78996a076ca [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>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070023
Elliott Hughes90a33692011-08-30 13:27:07 -070024#include "UniquePtr.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070025#include "common_test.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070026#include "os.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070027
28namespace art {
29
Brian Carlstromf734cf52011-08-17 16:28:14 -070030class ZipArchiveTest : public CommonTest {};
Brian Carlstromb0460ea2011-07-29 10:08:05 -070031
Brian Carlstromb0460ea2011-07-29 10:08:05 -070032TEST_F(ZipArchiveTest, FindAndExtract) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070033 std::string error_msg;
34 UniquePtr<ZipArchive> zip_archive(ZipArchive::Open(GetLibCoreDexFileName().c_str(), &error_msg));
35 ASSERT_TRUE(zip_archive.get() != false) << error_msg;
36 ASSERT_TRUE(error_msg.empty());
Narayan Kamath92572be2013-11-28 14:06:24 +000037 UniquePtr<ZipEntry> zip_entry(zip_archive->Find("classes.dex", &error_msg));
Elliott Hughes90a33692011-08-30 13:27:07 -070038 ASSERT_TRUE(zip_entry.get() != false);
Narayan Kamath92572be2013-11-28 14:06:24 +000039 ASSERT_TRUE(error_msg.empty());
Brian Carlstromb0460ea2011-07-29 10:08:05 -070040
Brian Carlstromdb4d5402011-08-09 12:18:28 -070041 ScratchFile tmp;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070042 ASSERT_NE(-1, tmp.GetFd());
Elliott Hughes76160052012-12-12 16:31:20 -080043 UniquePtr<File> file(new File(tmp.GetFd(), tmp.GetFilename()));
Elliott Hughes90a33692011-08-30 13:27:07 -070044 ASSERT_TRUE(file.get() != NULL);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070045 bool success = zip_entry->ExtractToFile(*file, &error_msg);
46 ASSERT_TRUE(success) << error_msg;
47 ASSERT_TRUE(error_msg.empty());
Elliott Hughes76160052012-12-12 16:31:20 -080048 file.reset(NULL);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070049
50 uint32_t computed_crc = crc32(0L, Z_NULL, 0);
Brian Carlstroma004aa92012-02-08 18:05:09 -080051 int fd = open(tmp.GetFilename().c_str(), O_RDONLY);
Brian Carlstromb0460ea2011-07-29 10:08:05 -070052 ASSERT_NE(-1, fd);
53 const size_t kBufSize = 32768;
54 uint8_t buf[kBufSize];
55 while (true) {
56 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buf, kBufSize));
57 if (bytes_read == 0) {
58 break;
59 }
60 computed_crc = crc32(computed_crc, buf, bytes_read);
61 }
62 EXPECT_EQ(zip_entry->GetCrc32(), computed_crc);
63}
64
65} // namespace art