blob: 2766ce127cf0ff3d08eae39a635987a4e013b216 [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 Lesinskida431a22016-12-29 16:08:16 -050035 EXPECT_NE(nullptr, loaded_apk->GetLoadedArsc());
Adam Lesinski7ad11102016-10-28 16:39:15 -070036
37 std::unique_ptr<Asset> asset = loaded_apk->Open("res/layout/main.xml");
38 ASSERT_NE(nullptr, asset);
39}
40
Adam Lesinskida431a22016-12-29 16:08:16 -050041TEST(ApkAssetsTest, LoadApkAsSharedLibrary) {
Adam Lesinski0c405242017-01-13 20:47:26 -080042 std::unique_ptr<const ApkAssets> loaded_apk =
Adam Lesinskida431a22016-12-29 16:08:16 -050043 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 Lesinski970bd8d2017-09-25 13:21:55 -070059TEST(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 Lesinskid1ecd7a2017-01-23 12:58:11 -080090TEST(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
106TEST(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 Lesinski7ad11102016-10-28 16:39:15 -0700127} // namespace android