blob: 4a1a49b9734fa2e58848058d2144b8a57d7dba18 [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 <android-base/test_utils.h>
25#include <gtest/gtest.h>
26#include <otautil/SysUtil.h>
27#include <otautil/ZipUtil.h>
28#include <ziparchive/zip_archive.h>
29
Tao Bao0dfb7532016-11-04 15:15:52 -070030#include "common/test_constants.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070031
Tao Bao0dfb7532016-11-04 15:15:52 -070032TEST(ZipTest, ExtractPackageRecursive) {
33 std::string zip_path = from_testdata_base("ziptest_valid.zip");
34 ZipArchiveHandle handle;
35 ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle));
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070036
Tao Bao0dfb7532016-11-04 15:15:52 -070037 // Extract the whole package into a temp directory.
38 TemporaryDir td;
39 ASSERT_NE(nullptr, td.path);
40 ExtractPackageRecursive(handle, "", td.path, nullptr, nullptr);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070041
Tao Bao0dfb7532016-11-04 15:15:52 -070042 // Make sure all the files are extracted correctly.
43 std::string path(td.path);
Tao Baoa3ece962016-12-21 18:44:20 -080044 ASSERT_EQ(0, access((path + "/a.txt").c_str(), F_OK));
45 ASSERT_EQ(0, access((path + "/b.txt").c_str(), F_OK));
46 ASSERT_EQ(0, access((path + "/b/c.txt").c_str(), F_OK));
47 ASSERT_EQ(0, access((path + "/b/d.txt").c_str(), F_OK));
Tao Bao0dfb7532016-11-04 15:15:52 -070048
49 // The content of the file is the same as expected.
50 std::string content1;
51 ASSERT_TRUE(android::base::ReadFileToString(path + "/a.txt", &content1));
52 ASSERT_EQ(kATxtContents, content1);
53
54 std::string content2;
55 ASSERT_TRUE(android::base::ReadFileToString(path + "/b/d.txt", &content2));
Tao Baoa3ece962016-12-21 18:44:20 -080056 ASSERT_EQ(kDTxtContents, content2);
57
58 CloseArchive(handle);
59
60 // Clean up.
61 ASSERT_EQ(0, unlink((path + "/a.txt").c_str()));
62 ASSERT_EQ(0, unlink((path + "/b.txt").c_str()));
63 ASSERT_EQ(0, unlink((path + "/b/c.txt").c_str()));
64 ASSERT_EQ(0, unlink((path + "/b/d.txt").c_str()));
65 ASSERT_EQ(0, rmdir((path + "/b").c_str()));
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070066}
67
Tao Bao0dfb7532016-11-04 15:15:52 -070068TEST(ZipTest, OpenFromMemory) {
69 MemMapping map;
70 std::string zip_path = from_testdata_base("ziptest_dummy-update.zip");
71 ASSERT_EQ(0, sysMapFile(zip_path.c_str(), &map));
72
73 // Map an update package into memory and open the archive from there.
74 ZipArchiveHandle handle;
75 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_path.c_str(), &handle));
76
77 static constexpr const char* BINARY_PATH = "META-INF/com/google/android/update-binary";
78 ZipString binary_path(BINARY_PATH);
79 ZipEntry binary_entry;
80 // Make sure the package opens correctly and its entry can be read.
81 ASSERT_EQ(0, FindEntry(handle, binary_path, &binary_entry));
82
83 TemporaryFile tmp_binary;
84 ASSERT_NE(-1, tmp_binary.fd);
85 ASSERT_EQ(0, ExtractEntryToFile(handle, &binary_entry, tmp_binary.fd));
86
Tao Baoa3ece962016-12-21 18:44:20 -080087 CloseArchive(handle);
Tao Bao0dfb7532016-11-04 15:15:52 -070088 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070089}
90