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 19 #include "android-base/file.h" 20 #include "android-base/test_utils.h" 21 #include "android-base/unique_fd.h" 22 #include "androidfw/Util.h" 23 24 #include "TestHelpers.h" 25 #include "data/basic/R.h" 26 27 using ::android::base::unique_fd; 28 using ::com::android::basic::R; 29 using ::testing::Eq; 30 using ::testing::Ge; 31 using ::testing::NotNull; 32 using ::testing::SizeIs; 33 using ::testing::StrEq; 34 35 namespace android { 36 37 TEST(ApkAssetsTest, LoadApk) { 38 std::unique_ptr<const ApkAssets> loaded_apk = 39 ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk"); 40 ASSERT_THAT(loaded_apk, NotNull()); 41 42 const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc(); 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()); 46 } 47 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)); 51 ASSERT_THAT(fd.get(), Ge(0)); 52 53 std::unique_ptr<const ApkAssets> loaded_apk = 54 ApkAssets::LoadFromFd(std::move(fd), path, false /*system*/, false /*force_shared_lib*/); 55 ASSERT_THAT(loaded_apk, NotNull()); 56 57 const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc(); 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()); 61 } 62 63 TEST(ApkAssetsTest, LoadApkAsSharedLibrary) { 64 std::unique_ptr<const ApkAssets> loaded_apk = 65 ApkAssets::Load(GetTestDataPath() + "/appaslib/appaslib.apk"); 66 ASSERT_THAT(loaded_apk, NotNull()); 67 68 const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc(); 69 ASSERT_THAT(loaded_arsc, NotNull()); 70 ASSERT_THAT(loaded_arsc->GetPackages(), SizeIs(1u)); 71 EXPECT_FALSE(loaded_arsc->GetPackages()[0]->IsDynamic()); 72 73 loaded_apk = ApkAssets::LoadAsSharedLibrary(GetTestDataPath() + "/appaslib/appaslib.apk"); 74 ASSERT_THAT(loaded_apk, NotNull()); 75 76 loaded_arsc = loaded_apk->GetLoadedArsc(); 77 ASSERT_THAT(loaded_arsc, NotNull()); 78 ASSERT_THAT(loaded_arsc->GetPackages(), SizeIs(1u)); 79 EXPECT_TRUE(loaded_arsc->GetPackages()[0]->IsDynamic()); 80 } 81 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)); 87 ASSERT_THAT(target_table.add(contents.data(), contents.size(), 0, true /*copyData*/), 88 Eq(NO_ERROR)); 89 90 ResTable overlay_table; 91 const std::string overlay_path = GetTestDataPath() + "/overlay/overlay.apk"; 92 ASSERT_TRUE(ReadFileFromZipToString(overlay_path, "resources.arsc", &contents)); 93 ASSERT_THAT(overlay_table.add(contents.data(), contents.size(), 0, true /*copyData*/), 94 Eq(NO_ERROR)); 95 96 util::unique_cptr<void> idmap_data; 97 void* temp_data; 98 size_t idmap_len; 99 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)); 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 112 ASSERT_THAT(ApkAssets::LoadOverlay(tf.path), NotNull()); 113 } 114 115 TEST(ApkAssetsTest, CreateAndDestroyAssetKeepsApkAssetsOpen) { 116 std::unique_ptr<const ApkAssets> loaded_apk = 117 ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk"); 118 ASSERT_THAT(loaded_apk, NotNull()); 119 120 { ASSERT_THAT(loaded_apk->Open("res/layout/main.xml", Asset::ACCESS_BUFFER), NotNull()); } 121 122 { ASSERT_THAT(loaded_apk->Open("res/layout/main.xml", Asset::ACCESS_BUFFER), NotNull()); } 123 } 124 125 TEST(ApkAssetsTest, OpenUncompressedAssetFd) { 126 std::unique_ptr<const ApkAssets> loaded_apk = 127 ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk"); 128 ASSERT_THAT(loaded_apk, NotNull()); 129 130 auto asset = loaded_apk->Open("assets/uncompressed.txt", Asset::ACCESS_UNKNOWN); 131 ASSERT_THAT(asset, NotNull()); 132 133 off64_t start, length; 134 unique_fd fd(asset->openFileDescriptor(&start, &length)); 135 ASSERT_THAT(fd.get(), Ge(0)); 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 143 EXPECT_THAT(buffer, StrEq("This should be uncompressed.\n\n")); 144 } 145 146 } // namespace android 147