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> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 25 | #include <ziparchive/zip_archive.h> |
| 26 | |
Tao Bao | 0dfb753 | 2016-11-04 15:15:52 -0700 | [diff] [blame] | 27 | #include "common/test_constants.h" |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 28 | #include "otautil/sysutil.h" |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 29 | |
Tao Bao | 0dfb753 | 2016-11-04 15:15:52 -0700 | [diff] [blame] | 30 | TEST(ZipTest, OpenFromMemory) { |
Tianjie | 78d1514 | 2020-07-22 17:40:09 -0700 | [diff] [blame] | 31 | std::string zip_path = from_testdata_base("ziptest_fake-update.zip"); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 32 | MemMapping map; |
| 33 | ASSERT_TRUE(map.MapFile(zip_path)); |
Tao Bao | 0dfb753 | 2016-11-04 15:15:52 -0700 | [diff] [blame] | 34 | |
| 35 | // Map an update package into memory and open the archive from there. |
| 36 | ZipArchiveHandle handle; |
| 37 | ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_path.c_str(), &handle)); |
| 38 | |
| 39 | static constexpr const char* BINARY_PATH = "META-INF/com/google/android/update-binary"; |
Kelvin Zhang | 4f81130 | 2020-09-16 14:06:12 -0400 | [diff] [blame] | 40 | ZipEntry64 binary_entry; |
Tao Bao | 0dfb753 | 2016-11-04 15:15:52 -0700 | [diff] [blame] | 41 | // Make sure the package opens correctly and its entry can be read. |
Elliott Hughes | a86dddb | 2019-05-03 22:52:37 -0700 | [diff] [blame] | 42 | ASSERT_EQ(0, FindEntry(handle, BINARY_PATH, &binary_entry)); |
Tao Bao | 0dfb753 | 2016-11-04 15:15:52 -0700 | [diff] [blame] | 43 | |
| 44 | TemporaryFile tmp_binary; |
| 45 | ASSERT_NE(-1, tmp_binary.fd); |
| 46 | ASSERT_EQ(0, ExtractEntryToFile(handle, &binary_entry, tmp_binary.fd)); |
| 47 | |
Tao Bao | a3ece96 | 2016-12-21 18:44:20 -0800 | [diff] [blame] | 48 | CloseArchive(handle); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 49 | } |
| 50 | |