blob: 903c9dbf79bd8430c05b35abddfe63f41bab9778 [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>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070018#include <unistd.h>
19
20#include <memory>
21#include <vector>
22
23#include <android-base/file.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070024#include <gtest/gtest.h>
Michael Bestasddc7b5a2019-09-19 21:42:12 +030025#include <otautil/ziputil.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070026#include <ziparchive/zip_archive.h>
27
Tao Bao0dfb7532016-11-04 15:15:52 -070028#include "common/test_constants.h"
Tao Bao17054c02018-05-03 22:41:23 -070029#include "otautil/sysutil.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070030
Michael Bestasddc7b5a2019-09-19 21:42:12 +030031TEST(ZipTest, ExtractPackageRecursive) {
32 std::string zip_path = from_testdata_base("ziptest_valid.zip");
33 ZipArchiveHandle handle;
34 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
35
36 // Extract the whole package into a temp directory.
37 TemporaryDir td;
38 ASSERT_NE(nullptr, td.path);
39 ExtractPackageRecursive(handle, "", td.path, nullptr, nullptr);
40
41 // Make sure all the files are extracted correctly.
42 std::string path(td.path);
43 ASSERT_EQ(0, access((path + "/a.txt").c_str(), F_OK));
44 ASSERT_EQ(0, access((path + "/b.txt").c_str(), F_OK));
45 ASSERT_EQ(0, access((path + "/b/c.txt").c_str(), F_OK));
46 ASSERT_EQ(0, access((path + "/b/d.txt").c_str(), F_OK));
47
48 // The content of the file is the same as expected.
49 std::string content1;
50 ASSERT_TRUE(android::base::ReadFileToString(path + "/a.txt", &content1));
51 ASSERT_EQ(kATxtContents, content1);
52
53 std::string content2;
54 ASSERT_TRUE(android::base::ReadFileToString(path + "/b/d.txt", &content2));
55 ASSERT_EQ(kDTxtContents, content2);
56
57 CloseArchive(handle);
58
59 // Clean up.
60 ASSERT_EQ(0, unlink((path + "/a.txt").c_str()));
61 ASSERT_EQ(0, unlink((path + "/b.txt").c_str()));
62 ASSERT_EQ(0, unlink((path + "/b/c.txt").c_str()));
63 ASSERT_EQ(0, unlink((path + "/b/d.txt").c_str()));
64 ASSERT_EQ(0, rmdir((path + "/b").c_str()));
65}
66
Tao Bao0dfb7532016-11-04 15:15:52 -070067TEST(ZipTest, OpenFromMemory) {
Tianjie78d15142020-07-22 17:40:09 -070068 std::string zip_path = from_testdata_base("ziptest_fake-update.zip");
Tao Baob656a152017-04-18 23:54:29 -070069 MemMapping map;
70 ASSERT_TRUE(map.MapFile(zip_path));
Tao Bao0dfb7532016-11-04 15:15:52 -070071
72 // Map an update package into memory and open the archive from there.
73 ZipArchiveHandle handle;
74 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_path.c_str(), &handle));
75
76 static constexpr const char* BINARY_PATH = "META-INF/com/google/android/update-binary";
Kelvin Zhang4f811302020-09-16 14:06:12 -040077 ZipEntry64 binary_entry;
Tao Bao0dfb7532016-11-04 15:15:52 -070078 // Make sure the package opens correctly and its entry can be read.
Elliott Hughesa86dddb2019-05-03 22:52:37 -070079 ASSERT_EQ(0, FindEntry(handle, BINARY_PATH, &binary_entry));
Tao Bao0dfb7532016-11-04 15:15:52 -070080
81 TemporaryFile tmp_binary;
82 ASSERT_NE(-1, tmp_binary.fd);
83 ASSERT_EQ(0, ExtractEntryToFile(handle, &binary_entry, tmp_binary.fd));
84
Tao Baoa3ece962016-12-21 18:44:20 -080085 CloseArchive(handle);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070086}
87