Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <memory> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <android-base/file.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 25 | #include <android-base/test_utils.h> |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <otautil/SysUtil.h> |
| 28 | #include <otautil/ZipUtil.h> |
| 29 | #include <ziparchive/zip_archive.h> |
| 30 | |
Tao Bao | 0dfb753 | 2016-11-04 15:15:52 -0700 | [diff] [blame] | 31 | #include "common/test_constants.h" |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 32 | |
Tao Bao | 0dfb753 | 2016-11-04 15:15:52 -0700 | [diff] [blame] | 33 | TEST(ZipTest, ExtractPackageRecursive) { |
| 34 | std::string zip_path = from_testdata_base("ziptest_valid.zip"); |
| 35 | ZipArchiveHandle handle; |
| 36 | ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle)); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 37 | |
Tao Bao | 0dfb753 | 2016-11-04 15:15:52 -0700 | [diff] [blame] | 38 | // Extract the whole package into a temp directory. |
| 39 | TemporaryDir td; |
| 40 | ASSERT_NE(nullptr, td.path); |
| 41 | ExtractPackageRecursive(handle, "", td.path, nullptr, nullptr); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 42 | |
Tao Bao | 0dfb753 | 2016-11-04 15:15:52 -0700 | [diff] [blame] | 43 | // Make sure all the files are extracted correctly. |
| 44 | std::string path(td.path); |
| 45 | ASSERT_EQ(0, access((path + "/a.txt").c_str(), O_RDONLY)); |
| 46 | ASSERT_EQ(0, access((path + "/b.txt").c_str(), O_RDONLY)); |
| 47 | ASSERT_EQ(0, access((path + "/b/c.txt").c_str(), O_RDONLY)); |
| 48 | ASSERT_EQ(0, access((path + "/b/d.txt").c_str(), O_RDONLY)); |
| 49 | |
| 50 | // The content of the file is the same as expected. |
| 51 | std::string content1; |
| 52 | ASSERT_TRUE(android::base::ReadFileToString(path + "/a.txt", &content1)); |
| 53 | ASSERT_EQ(kATxtContents, content1); |
| 54 | |
| 55 | std::string content2; |
| 56 | ASSERT_TRUE(android::base::ReadFileToString(path + "/b/d.txt", &content2)); |
| 57 | ASSERT_EQ(kBTxtContents, content2); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Tao Bao | 0dfb753 | 2016-11-04 15:15:52 -0700 | [diff] [blame] | 60 | TEST(ZipTest, OpenFromMemory) { |
| 61 | MemMapping map; |
| 62 | std::string zip_path = from_testdata_base("ziptest_dummy-update.zip"); |
| 63 | ASSERT_EQ(0, sysMapFile(zip_path.c_str(), &map)); |
| 64 | |
| 65 | // Map an update package into memory and open the archive from there. |
| 66 | ZipArchiveHandle handle; |
| 67 | ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_path.c_str(), &handle)); |
| 68 | |
| 69 | static constexpr const char* BINARY_PATH = "META-INF/com/google/android/update-binary"; |
| 70 | ZipString binary_path(BINARY_PATH); |
| 71 | ZipEntry binary_entry; |
| 72 | // Make sure the package opens correctly and its entry can be read. |
| 73 | ASSERT_EQ(0, FindEntry(handle, binary_path, &binary_entry)); |
| 74 | |
| 75 | TemporaryFile tmp_binary; |
| 76 | ASSERT_NE(-1, tmp_binary.fd); |
| 77 | ASSERT_EQ(0, ExtractEntryToFile(handle, &binary_entry, tmp_binary.fd)); |
| 78 | |
| 79 | sysReleaseMap(&map); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 80 | } |
| 81 | |