blob: b617446b8c723afadc27c322ab6b737e9f9273e8 [file] [log] [blame]
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -07001/*
2 * Copyright (C) 2016 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 */
16
17#include <errno.h>
18#include <fcntl.h>
19#include <pthread.h>
20#include <unistd.h>
21
22#include <memory>
23#include <vector>
24
25#include <android-base/file.h>
26#include <android-base/stringprintf.h>
27#include <android-base/unique_fd.h>
28#include <android-base/test_utils.h>
29#include <gtest/gtest.h>
30#include <otautil/SysUtil.h>
31#include <otautil/ZipUtil.h>
32#include <ziparchive/zip_archive.h>
33
34static const std::string DATA_PATH(getenv("ANDROID_DATA"));
35static const std::string TESTDATA_PATH("/recovery/testdata/");
36
37static const std::vector<uint8_t> kATxtContents {
38 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
39 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
40 '\n'
41};
42
43static const std::vector<uint8_t> kBTxtContents {
44 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
45 '\n'
46};
47
48TEST(otazip, ExtractPackageRecursive) {
49 TemporaryDir td;
50 ASSERT_NE(td.path, nullptr);
51 ZipArchiveHandle handle;
52 std::string zip_path = DATA_PATH + TESTDATA_PATH + "/ziptest_valid.zip";
53 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
54 // Extract the whole package into a temp directory.
55 ExtractPackageRecursive(handle, "", td.path, nullptr, nullptr);
56 // Make sure all the files are extracted correctly.
57 std::string path(td.path);
58 android::base::unique_fd fd(open((path + "/a.txt").c_str(), O_RDONLY));
59 ASSERT_NE(fd, -1);
60 std::vector<uint8_t> read_data;
61 read_data.resize(kATxtContents.size());
62 // The content of the file is the same as expected.
63 ASSERT_TRUE(android::base::ReadFully(fd.get(), read_data.data(), read_data.size()));
64 ASSERT_EQ(0, memcmp(read_data.data(), kATxtContents.data(), kATxtContents.size()));
65
66 fd.reset(open((path + "/b.txt").c_str(), O_RDONLY));
67 ASSERT_NE(fd, -1);
68 fd.reset(open((path + "/b/c.txt").c_str(), O_RDONLY));
69 ASSERT_NE(fd, -1);
70 fd.reset(open((path + "/b/d.txt").c_str(), O_RDONLY));
71 ASSERT_NE(fd, -1);
72 read_data.resize(kBTxtContents.size());
73 ASSERT_TRUE(android::base::ReadFully(fd.get(), read_data.data(), read_data.size()));
74 ASSERT_EQ(0, memcmp(read_data.data(), kBTxtContents.data(), kBTxtContents.size()));
75}
76
77TEST(otazip, OpenFromMemory) {
78 MemMapping map;
79 std::string zip_path = DATA_PATH + TESTDATA_PATH + "/ziptest_dummy-update.zip";
80 ASSERT_EQ(0, sysMapFile(zip_path.c_str(), &map));
81 // Map an update package into memory and open the archive from there.
82 ZipArchiveHandle handle;
83 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_path.c_str(), &handle));
84 static constexpr const char* BINARY_PATH = "META-INF/com/google/android/update-binary";
85 ZipString binary_path(BINARY_PATH);
86 ZipEntry binary_entry;
87 // Make sure the package opens correctly and its entry can be read.
88 ASSERT_EQ(0, FindEntry(handle, binary_path, &binary_entry));
89 TemporaryFile tmp_binary;
90 ASSERT_NE(-1, tmp_binary.fd);
91 ASSERT_EQ(0, ExtractEntryToFile(handle, &binary_entry, tmp_binary.fd));
92}
93