blob: d65d93f9af1ab93853ec9641a1f5d0fafa0dccc1 [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 Lesinski970bd8d2017-09-25 13:21:55 -070027using ::com::android::basic::R;
Adam Lesinski7ad11102016-10-28 16:39:15 -070028
29namespace android {
30
31TEST(ApkAssetsTest, LoadApk) {
Adam Lesinski0c405242017-01-13 20:47:26 -080032 std::unique_ptr<const ApkAssets> loaded_apk =
33 ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
Adam Lesinski7ad11102016-10-28 16:39:15 -070034 ASSERT_NE(nullptr, loaded_apk);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070035
36 const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc();
37 ASSERT_NE(nullptr, loaded_arsc);
38
39 const LoadedPackage* loaded_package = loaded_arsc->GetPackageForId(0x7f010000);
40 ASSERT_NE(nullptr, loaded_package);
41 EXPECT_TRUE(loaded_package->IsVerified());
Adam Lesinski7ad11102016-10-28 16:39:15 -070042
43 std::unique_ptr<Asset> asset = loaded_apk->Open("res/layout/main.xml");
44 ASSERT_NE(nullptr, asset);
45}
46
Adam Lesinskida431a22016-12-29 16:08:16 -050047TEST(ApkAssetsTest, LoadApkAsSharedLibrary) {
Adam Lesinski0c405242017-01-13 20:47:26 -080048 std::unique_ptr<const ApkAssets> loaded_apk =
Adam Lesinskida431a22016-12-29 16:08:16 -050049 ApkAssets::Load(GetTestDataPath() + "/appaslib/appaslib.apk");
50 ASSERT_NE(nullptr, loaded_apk);
51 const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc();
52 ASSERT_NE(nullptr, loaded_arsc);
53 ASSERT_EQ(1u, loaded_arsc->GetPackages().size());
54 EXPECT_FALSE(loaded_arsc->GetPackages()[0]->IsDynamic());
55
56 loaded_apk = ApkAssets::LoadAsSharedLibrary(GetTestDataPath() + "/appaslib/appaslib.apk");
57 ASSERT_NE(nullptr, loaded_apk);
58
59 loaded_arsc = loaded_apk->GetLoadedArsc();
60 ASSERT_NE(nullptr, loaded_arsc);
61 ASSERT_EQ(1u, loaded_arsc->GetPackages().size());
62 EXPECT_TRUE(loaded_arsc->GetPackages()[0]->IsDynamic());
63}
64
Adam Lesinski970bd8d2017-09-25 13:21:55 -070065TEST(ApkAssetsTest, LoadApkWithIdmap) {
66 std::string contents;
67 ResTable target_table;
68 const std::string target_path = GetTestDataPath() + "/basic/basic.apk";
69 ASSERT_TRUE(ReadFileFromZipToString(target_path, "resources.arsc", &contents));
70 ASSERT_EQ(NO_ERROR, target_table.add(contents.data(), contents.size(), 0, true /*copyData*/));
71
72 ResTable overlay_table;
73 const std::string overlay_path = GetTestDataPath() + "/overlay/overlay.apk";
74 ASSERT_TRUE(ReadFileFromZipToString(overlay_path, "resources.arsc", &contents));
75 ASSERT_EQ(NO_ERROR, overlay_table.add(contents.data(), contents.size(), 0, true /*copyData*/));
76
77 util::unique_cptr<void> idmap_data;
78 void* temp_data;
79 size_t idmap_len;
80
81 ASSERT_EQ(NO_ERROR, target_table.createIdmap(overlay_table, 0u, 0u, target_path.c_str(),
82 overlay_path.c_str(), &temp_data, &idmap_len));
83 idmap_data.reset(temp_data);
84
85 TemporaryFile tf;
86 ASSERT_TRUE(base::WriteFully(tf.fd, idmap_data.get(), idmap_len));
87 close(tf.fd);
88
89 // Open something so that the destructor of TemporaryFile closes a valid fd.
90 tf.fd = open("/dev/null", O_WRONLY);
91
92 std::unique_ptr<const ApkAssets> loaded_overlay_apk = ApkAssets::LoadOverlay(tf.path);
93 ASSERT_NE(nullptr, loaded_overlay_apk);
94}
95
Adam Lesinski1a1e9c22017-10-13 15:45:34 -070096TEST(ApkAssetsTest, LoadUnverifiableApk) {
97 std::unique_ptr<const ApkAssets> loaded_apk =
98 ApkAssets::Load(GetTestDataPath() + "/unverified/unverified.apk");
99 ASSERT_NE(nullptr, loaded_apk);
100
101 const LoadedArsc* loaded_arsc = loaded_apk->GetLoadedArsc();
102 ASSERT_NE(nullptr, loaded_arsc);
103
104 const LoadedPackage* loaded_package = loaded_arsc->GetPackageForId(0x7f010000);
105 ASSERT_NE(nullptr, loaded_package);
106
107 EXPECT_FALSE(loaded_package->IsVerified());
108}
109
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800110TEST(ApkAssetsTest, CreateAndDestroyAssetKeepsApkAssetsOpen) {
111 std::unique_ptr<const ApkAssets> loaded_apk =
112 ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
113 ASSERT_NE(nullptr, loaded_apk);
114
115 {
116 std::unique_ptr<Asset> assets = loaded_apk->Open("res/layout/main.xml", Asset::ACCESS_BUFFER);
117 ASSERT_NE(nullptr, assets);
118 }
119
120 {
121 std::unique_ptr<Asset> assets = loaded_apk->Open("res/layout/main.xml", Asset::ACCESS_BUFFER);
122 ASSERT_NE(nullptr, assets);
123 }
124}
125
126TEST(ApkAssetsTest, OpenUncompressedAssetFd) {
127 std::unique_ptr<const ApkAssets> loaded_apk =
128 ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
129 ASSERT_NE(nullptr, loaded_apk);
130
131 auto asset = loaded_apk->Open("assets/uncompressed.txt", Asset::ACCESS_UNKNOWN);
132 ASSERT_NE(nullptr, asset);
133
134 off64_t start, length;
135 base::unique_fd fd(asset->openFileDescriptor(&start, &length));
136 EXPECT_GE(fd.get(), 0);
137
138 lseek64(fd.get(), start, SEEK_SET);
139
140 std::string buffer;
141 buffer.resize(length);
142 ASSERT_TRUE(base::ReadFully(fd.get(), &*buffer.begin(), length));
143
144 EXPECT_EQ("This should be uncompressed.\n\n", buffer);
145}
146
Adam Lesinski7ad11102016-10-28 16:39:15 -0700147} // namespace android