blob: f42e76326fc8875c1d38fe0786d5eb830679cc25 [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>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070025#include <ziparchive/zip_archive.h>
26
Tao Bao0dfb7532016-11-04 15:15:52 -070027#include "common/test_constants.h"
Tao Bao17054c02018-05-03 22:41:23 -070028#include "otautil/sysutil.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070029
Tom Marshall981118e2017-10-25 20:27:08 +020030TEST(ZipTest, ExtractPackageRecursive) {
31 std::string zip_path = from_testdata_base("ziptest_valid.zip");
32 ZipArchiveHandle handle;
33 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
34
35 // Extract the whole package into a temp directory.
36 TemporaryDir td;
37 ASSERT_NE(nullptr, td.path);
38 ExtractPackageRecursive(handle, "", td.path, nullptr, nullptr);
39
40 // Make sure all the files are extracted correctly.
41 std::string path(td.path);
42 ASSERT_EQ(0, access((path + "/a.txt").c_str(), F_OK));
43 ASSERT_EQ(0, access((path + "/b.txt").c_str(), F_OK));
44 ASSERT_EQ(0, access((path + "/b/c.txt").c_str(), F_OK));
45 ASSERT_EQ(0, access((path + "/b/d.txt").c_str(), F_OK));
46
47 // The content of the file is the same as expected.
48 std::string content1;
49 ASSERT_TRUE(android::base::ReadFileToString(path + "/a.txt", &content1));
50 ASSERT_EQ(kATxtContents, content1);
51
52 std::string content2;
53 ASSERT_TRUE(android::base::ReadFileToString(path + "/b/d.txt", &content2));
54 ASSERT_EQ(kDTxtContents, content2);
55
56 CloseArchive(handle);
57
58 // Clean up.
59 ASSERT_EQ(0, unlink((path + "/a.txt").c_str()));
60 ASSERT_EQ(0, unlink((path + "/b.txt").c_str()));
61 ASSERT_EQ(0, unlink((path + "/b/c.txt").c_str()));
62 ASSERT_EQ(0, unlink((path + "/b/d.txt").c_str()));
63 ASSERT_EQ(0, rmdir((path + "/b").c_str()));
64}
65
Tao Bao0dfb7532016-11-04 15:15:52 -070066TEST(ZipTest, OpenFromMemory) {
Tao Bao0dfb7532016-11-04 15:15:52 -070067 std::string zip_path = from_testdata_base("ziptest_dummy-update.zip");
Tao Baob656a152017-04-18 23:54:29 -070068 MemMapping map;
69 ASSERT_TRUE(map.MapFile(zip_path));
Tao Bao0dfb7532016-11-04 15:15:52 -070070
71 // Map an update package into memory and open the archive from there.
72 ZipArchiveHandle handle;
73 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_path.c_str(), &handle));
74
75 static constexpr const char* BINARY_PATH = "META-INF/com/google/android/update-binary";
Tao Bao0dfb7532016-11-04 15:15:52 -070076 ZipEntry binary_entry;
77 // Make sure the package opens correctly and its entry can be read.
Elliott Hughesa86dddb2019-05-03 22:52:37 -070078 ASSERT_EQ(0, FindEntry(handle, BINARY_PATH, &binary_entry));
Tao Bao0dfb7532016-11-04 15:15:52 -070079
80 TemporaryFile tmp_binary;
81 ASSERT_NE(-1, tmp_binary.fd);
82 ASSERT_EQ(0, ExtractEntryToFile(handle, &binary_entry, tmp_binary.fd));
83
Tao Baoa3ece962016-12-21 18:44:20 -080084 CloseArchive(handle);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070085}
86