blob: ba5844bbfbb1a37cdefa7ead91829626390dbc39 [file] [log] [blame]
Adam Lesinski7ad11102016-10-28 16:39:15 -07001/*
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 Lesinskid1ecd7a2017-01-23 12:58:11 -080019#include "android-base/file.h"
Adam Lesinski970bd8d2017-09-25 13:21:55 -070020#include "android-base/test_utils.h"
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -080021#include "android-base/unique_fd.h"
Adam Lesinski970bd8d2017-09-25 13:21:55 -070022#include "androidfw/Util.h"
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -080023
Adam Lesinski7ad11102016-10-28 16:39:15 -070024#include "TestHelpers.h"
25#include "data/basic/R.h"
26
Adam Lesinski441500b2017-11-13 17:52:25 -080027using ::android::base::unique_fd;
Adam Lesinski970bd8d2017-09-25 13:21:55 -070028using ::com::android::basic::R;
Adam Lesinski7ad11102016-10-28 16:39:15 -070029
30namespace android {
31
32TEST(ApkAssetsTest, LoadApk) {
Adam Lesinski0c405242017-01-13 20:47:26 -080033 std::unique_ptr<const ApkAssets> loaded_apk =
34 ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
Adam Lesinski7ad11102016-10-28 16:39:15 -070035 ASSERT_NE(nullptr, loaded_apk);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070036
37 const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc();
38 ASSERT_NE(nullptr, loaded_arsc);
39
40 const LoadedPackage* loaded_package = loaded_arsc->GetPackageForId(0x7f010000);
41 ASSERT_NE(nullptr, loaded_package);
42 EXPECT_TRUE(loaded_package->IsVerified());
Adam Lesinski7ad11102016-10-28 16:39:15 -070043
44 std::unique_ptr<Asset> asset = loaded_apk->Open("res/layout/main.xml");
45 ASSERT_NE(nullptr, asset);
46}
47
Adam Lesinski441500b2017-11-13 17:52:25 -080048TEST(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_GE(fd.get(), 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_NE(nullptr, loaded_apk);
56
57 const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc();
58 ASSERT_NE(nullptr, loaded_arsc);
59
60 const LoadedPackage* loaded_package = loaded_arsc->GetPackageForId(0x7f010000);
61 ASSERT_NE(nullptr, loaded_package);
62 EXPECT_TRUE(loaded_package->IsVerified());
63
64 std::unique_ptr<Asset> asset = loaded_apk->Open("res/layout/main.xml");
65 ASSERT_NE(nullptr, asset);
66}
67
Adam Lesinskida431a22016-12-29 16:08:16 -050068TEST(ApkAssetsTest, LoadApkAsSharedLibrary) {
Adam Lesinski0c405242017-01-13 20:47:26 -080069 std::unique_ptr<const ApkAssets> loaded_apk =
Adam Lesinskida431a22016-12-29 16:08:16 -050070 ApkAssets::Load(GetTestDataPath() + "/appaslib/appaslib.apk");
71 ASSERT_NE(nullptr, loaded_apk);
72 const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc();
73 ASSERT_NE(nullptr, loaded_arsc);
74 ASSERT_EQ(1u, loaded_arsc->GetPackages().size());
75 EXPECT_FALSE(loaded_arsc->GetPackages()[0]->IsDynamic());
76
77 loaded_apk = ApkAssets::LoadAsSharedLibrary(GetTestDataPath() + "/appaslib/appaslib.apk");
78 ASSERT_NE(nullptr, loaded_apk);
79
80 loaded_arsc = loaded_apk->GetLoadedArsc();
81 ASSERT_NE(nullptr, loaded_arsc);
82 ASSERT_EQ(1u, loaded_arsc->GetPackages().size());
83 EXPECT_TRUE(loaded_arsc->GetPackages()[0]->IsDynamic());
84}
85
Adam Lesinski970bd8d2017-09-25 13:21:55 -070086TEST(ApkAssetsTest, LoadApkWithIdmap) {
87 std::string contents;
88 ResTable target_table;
89 const std::string target_path = GetTestDataPath() + "/basic/basic.apk";
90 ASSERT_TRUE(ReadFileFromZipToString(target_path, "resources.arsc", &contents));
91 ASSERT_EQ(NO_ERROR, target_table.add(contents.data(), contents.size(), 0, true /*copyData*/));
92
93 ResTable overlay_table;
94 const std::string overlay_path = GetTestDataPath() + "/overlay/overlay.apk";
95 ASSERT_TRUE(ReadFileFromZipToString(overlay_path, "resources.arsc", &contents));
96 ASSERT_EQ(NO_ERROR, overlay_table.add(contents.data(), contents.size(), 0, true /*copyData*/));
97
98 util::unique_cptr<void> idmap_data;
99 void* temp_data;
100 size_t idmap_len;
101
102 ASSERT_EQ(NO_ERROR, target_table.createIdmap(overlay_table, 0u, 0u, target_path.c_str(),
103 overlay_path.c_str(), &temp_data, &idmap_len));
104 idmap_data.reset(temp_data);
105
106 TemporaryFile tf;
107 ASSERT_TRUE(base::WriteFully(tf.fd, idmap_data.get(), idmap_len));
108 close(tf.fd);
109
110 // Open something so that the destructor of TemporaryFile closes a valid fd.
111 tf.fd = open("/dev/null", O_WRONLY);
112
113 std::unique_ptr<const ApkAssets> loaded_overlay_apk = ApkAssets::LoadOverlay(tf.path);
114 ASSERT_NE(nullptr, loaded_overlay_apk);
115}
116
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700117TEST(ApkAssetsTest, LoadUnverifiableApk) {
118 std::unique_ptr<const ApkAssets> loaded_apk =
119 ApkAssets::Load(GetTestDataPath() + "/unverified/unverified.apk");
120 ASSERT_NE(nullptr, loaded_apk);
121
122 const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc();
123 ASSERT_NE(nullptr, loaded_arsc);
124
125 const LoadedPackage* loaded_package = loaded_arsc->GetPackageForId(0x7f010000);
126 ASSERT_NE(nullptr, loaded_package);
127
128 EXPECT_FALSE(loaded_package->IsVerified());
129}
130
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800131TEST(ApkAssetsTest, CreateAndDestroyAssetKeepsApkAssetsOpen) {
132 std::unique_ptr<const ApkAssets> loaded_apk =
133 ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
134 ASSERT_NE(nullptr, loaded_apk);
135
136 {
137 std::unique_ptr<Asset> assets = loaded_apk->Open("res/layout/main.xml", Asset::ACCESS_BUFFER);
138 ASSERT_NE(nullptr, assets);
139 }
140
141 {
142 std::unique_ptr<Asset> assets = loaded_apk->Open("res/layout/main.xml", Asset::ACCESS_BUFFER);
143 ASSERT_NE(nullptr, assets);
144 }
145}
146
147TEST(ApkAssetsTest, OpenUncompressedAssetFd) {
148 std::unique_ptr<const ApkAssets> loaded_apk =
149 ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
150 ASSERT_NE(nullptr, loaded_apk);
151
152 auto asset = loaded_apk->Open("assets/uncompressed.txt", Asset::ACCESS_UNKNOWN);
153 ASSERT_NE(nullptr, asset);
154
155 off64_t start, length;
Adam Lesinski441500b2017-11-13 17:52:25 -0800156 unique_fd fd(asset->openFileDescriptor(&start, &length));
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800157 EXPECT_GE(fd.get(), 0);
158
159 lseek64(fd.get(), start, SEEK_SET);
160
161 std::string buffer;
162 buffer.resize(length);
163 ASSERT_TRUE(base::ReadFully(fd.get(), &*buffer.begin(), length));
164
165 EXPECT_EQ("This should be uncompressed.\n\n", buffer);
166}
167
Adam Lesinski7ad11102016-10-28 16:39:15 -0700168} // namespace android