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