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