Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -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 "androidfw/ApkAssets.h" |
| 18 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 19 | #include "android-base/file.h" |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 20 | #include "android-base/test_utils.h" |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 21 | #include "android-base/unique_fd.h" |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 22 | #include "androidfw/Util.h" |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 23 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 24 | #include "TestHelpers.h" |
| 25 | #include "data/basic/R.h" |
| 26 | |
Adam Lesinski | 441500b | 2017-11-13 17:52:25 -0800 | [diff] [blame] | 27 | using ::android::base::unique_fd; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 28 | using ::com::android::basic::R; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 29 | using ::testing::Eq; |
| 30 | using ::testing::Ge; |
| 31 | using ::testing::NotNull; |
| 32 | using ::testing::SizeIs; |
| 33 | using ::testing::StrEq; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | TEST(ApkAssetsTest, LoadApk) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 38 | std::unique_ptr<const ApkAssets> loaded_apk = |
| 39 | ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk"); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 40 | ASSERT_THAT(loaded_apk, NotNull()); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 41 | |
| 42 | const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 43 | ASSERT_THAT(loaded_arsc, NotNull()); |
| 44 | ASSERT_THAT(loaded_arsc->GetPackageById(0x7fu), NotNull()); |
| 45 | ASSERT_THAT(loaded_apk->Open("res/layout/main.xml"), NotNull()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Adam Lesinski | 441500b | 2017-11-13 17:52:25 -0800 | [diff] [blame] | 48 | TEST(ApkAssetsTest, LoadApkFromFd) { |
| 49 | const std::string path = GetTestDataPath() + "/basic/basic.apk"; |
| 50 | unique_fd fd(::open(path.c_str(), O_RDONLY | O_BINARY)); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 51 | ASSERT_THAT(fd.get(), Ge(0)); |
Adam Lesinski | 441500b | 2017-11-13 17:52:25 -0800 | [diff] [blame] | 52 | |
| 53 | std::unique_ptr<const ApkAssets> loaded_apk = |
| 54 | ApkAssets::LoadFromFd(std::move(fd), path, false /*system*/, false /*force_shared_lib*/); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 55 | ASSERT_THAT(loaded_apk, NotNull()); |
Adam Lesinski | 441500b | 2017-11-13 17:52:25 -0800 | [diff] [blame] | 56 | |
| 57 | const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 58 | ASSERT_THAT(loaded_arsc, NotNull()); |
| 59 | ASSERT_THAT(loaded_arsc->GetPackageById(0x7fu), NotNull()); |
| 60 | ASSERT_THAT(loaded_apk->Open("res/layout/main.xml"), NotNull()); |
Adam Lesinski | 441500b | 2017-11-13 17:52:25 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 63 | TEST(ApkAssetsTest, LoadApkAsSharedLibrary) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 64 | std::unique_ptr<const ApkAssets> loaded_apk = |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 65 | ApkAssets::Load(GetTestDataPath() + "/appaslib/appaslib.apk"); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 66 | ASSERT_THAT(loaded_apk, NotNull()); |
| 67 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 68 | const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 69 | ASSERT_THAT(loaded_arsc, NotNull()); |
| 70 | ASSERT_THAT(loaded_arsc->GetPackages(), SizeIs(1u)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 71 | EXPECT_FALSE(loaded_arsc->GetPackages()[0]->IsDynamic()); |
| 72 | |
| 73 | loaded_apk = ApkAssets::LoadAsSharedLibrary(GetTestDataPath() + "/appaslib/appaslib.apk"); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 74 | ASSERT_THAT(loaded_apk, NotNull()); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 75 | |
| 76 | loaded_arsc = loaded_apk->GetLoadedArsc(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 77 | ASSERT_THAT(loaded_arsc, NotNull()); |
| 78 | ASSERT_THAT(loaded_arsc->GetPackages(), SizeIs(1u)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 79 | EXPECT_TRUE(loaded_arsc->GetPackages()[0]->IsDynamic()); |
| 80 | } |
| 81 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 82 | TEST(ApkAssetsTest, LoadApkWithIdmap) { |
| 83 | std::string contents; |
| 84 | ResTable target_table; |
| 85 | const std::string target_path = GetTestDataPath() + "/basic/basic.apk"; |
| 86 | ASSERT_TRUE(ReadFileFromZipToString(target_path, "resources.arsc", &contents)); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 87 | ASSERT_THAT(target_table.add(contents.data(), contents.size(), 0, true /*copyData*/), |
| 88 | Eq(NO_ERROR)); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 89 | |
| 90 | ResTable overlay_table; |
| 91 | const std::string overlay_path = GetTestDataPath() + "/overlay/overlay.apk"; |
| 92 | ASSERT_TRUE(ReadFileFromZipToString(overlay_path, "resources.arsc", &contents)); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 93 | ASSERT_THAT(overlay_table.add(contents.data(), contents.size(), 0, true /*copyData*/), |
| 94 | Eq(NO_ERROR)); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 95 | |
| 96 | util::unique_cptr<void> idmap_data; |
| 97 | void* temp_data; |
| 98 | size_t idmap_len; |
| 99 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 100 | ASSERT_THAT(target_table.createIdmap(overlay_table, 0u, 0u, target_path.c_str(), |
| 101 | overlay_path.c_str(), &temp_data, &idmap_len), |
| 102 | Eq(NO_ERROR)); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 103 | idmap_data.reset(temp_data); |
| 104 | |
| 105 | TemporaryFile tf; |
| 106 | ASSERT_TRUE(base::WriteFully(tf.fd, idmap_data.get(), idmap_len)); |
| 107 | close(tf.fd); |
| 108 | |
| 109 | // Open something so that the destructor of TemporaryFile closes a valid fd. |
| 110 | tf.fd = open("/dev/null", O_WRONLY); |
| 111 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 112 | ASSERT_THAT(ApkAssets::LoadOverlay(tf.path), NotNull()); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 115 | TEST(ApkAssetsTest, CreateAndDestroyAssetKeepsApkAssetsOpen) { |
| 116 | std::unique_ptr<const ApkAssets> loaded_apk = |
| 117 | ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk"); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 118 | ASSERT_THAT(loaded_apk, NotNull()); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 119 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 120 | { ASSERT_THAT(loaded_apk->Open("res/layout/main.xml", Asset::ACCESS_BUFFER), NotNull()); } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 121 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 122 | { ASSERT_THAT(loaded_apk->Open("res/layout/main.xml", Asset::ACCESS_BUFFER), NotNull()); } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | TEST(ApkAssetsTest, OpenUncompressedAssetFd) { |
| 126 | std::unique_ptr<const ApkAssets> loaded_apk = |
| 127 | ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk"); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 128 | ASSERT_THAT(loaded_apk, NotNull()); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 129 | |
| 130 | auto asset = loaded_apk->Open("assets/uncompressed.txt", Asset::ACCESS_UNKNOWN); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 131 | ASSERT_THAT(asset, NotNull()); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 132 | |
| 133 | off64_t start, length; |
Adam Lesinski | 441500b | 2017-11-13 17:52:25 -0800 | [diff] [blame] | 134 | unique_fd fd(asset->openFileDescriptor(&start, &length)); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 135 | ASSERT_THAT(fd.get(), Ge(0)); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 136 | |
| 137 | lseek64(fd.get(), start, SEEK_SET); |
| 138 | |
| 139 | std::string buffer; |
| 140 | buffer.resize(length); |
| 141 | ASSERT_TRUE(base::ReadFully(fd.get(), &*buffer.begin(), length)); |
| 142 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame^] | 143 | EXPECT_THAT(buffer, StrEq("This should be uncompressed.\n\n")); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 146 | } // namespace android |