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/LoadedArsc.h"
18
19 #include "TestHelpers.h"
20 #include "data/basic/R.h"
21 #include "data/libclient/R.h"
22 #include "data/styles/R.h"
23
24 namespace app = com::android::app;
25 namespace basic = com::android::basic;
26 namespace libclient = com::android::libclient;
27
28 namespace android {
29
TEST(LoadedArscTest,LoadSinglePackageArsc)30 TEST(LoadedArscTest, LoadSinglePackageArsc) {
31 std::string contents;
32 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/styles/styles.apk", "resources.arsc",
33 &contents));
34
35 std::unique_ptr<const LoadedArsc> loaded_arsc =
36 LoadedArsc::Load(contents.data(), contents.size());
37 ASSERT_NE(nullptr, loaded_arsc);
38
39 const std::vector<std::unique_ptr<const LoadedPackage>>& packages = loaded_arsc->GetPackages();
40 ASSERT_EQ(1u, packages.size());
41 EXPECT_EQ(std::string("com.android.app"), packages[0]->GetPackageName());
42 EXPECT_EQ(0x7f, packages[0]->GetPackageId());
43
44 ResTable_config config;
45 memset(&config, 0, sizeof(config));
46 config.sdkVersion = 24;
47
48 LoadedArscEntry entry;
49 ResTable_config selected_config;
50 uint32_t flags;
51
52 ASSERT_TRUE(
53 loaded_arsc->FindEntry(app::R::string::string_one, config, &entry, &selected_config, &flags));
54 ASSERT_NE(nullptr, entry.entry);
55 }
56
TEST(LoadedArscTest,FindDefaultEntry)57 TEST(LoadedArscTest, FindDefaultEntry) {
58 std::string contents;
59 ASSERT_TRUE(
60 ReadFileFromZipToString(GetTestDataPath() + "/basic/basic.apk", "resources.arsc", &contents));
61
62 std::unique_ptr<const LoadedArsc> loaded_arsc =
63 LoadedArsc::Load(contents.data(), contents.size());
64 ASSERT_NE(nullptr, loaded_arsc);
65
66 ResTable_config desired_config;
67 memset(&desired_config, 0, sizeof(desired_config));
68 desired_config.language[0] = 'd';
69 desired_config.language[1] = 'e';
70
71 LoadedArscEntry entry;
72 ResTable_config selected_config;
73 uint32_t flags;
74
75 ASSERT_TRUE(loaded_arsc->FindEntry(basic::R::string::test1, desired_config, &entry,
76 &selected_config, &flags));
77 ASSERT_NE(nullptr, entry.entry);
78 }
79
TEST(LoadedArscTest,LoadSharedLibrary)80 TEST(LoadedArscTest, LoadSharedLibrary) {
81 std::string contents;
82 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/lib_one/lib_one.apk", "resources.arsc",
83 &contents));
84
85 std::unique_ptr<const LoadedArsc> loaded_arsc =
86 LoadedArsc::Load(contents.data(), contents.size());
87 ASSERT_NE(nullptr, loaded_arsc);
88
89 const auto& packages = loaded_arsc->GetPackages();
90 ASSERT_EQ(1u, packages.size());
91
92 EXPECT_TRUE(packages[0]->IsDynamic());
93 EXPECT_EQ(std::string("com.android.lib_one"), packages[0]->GetPackageName());
94 EXPECT_EQ(0, packages[0]->GetPackageId());
95
96 const auto& dynamic_pkg_map = packages[0]->GetDynamicPackageMap();
97
98 // The library has no dependencies.
99 ASSERT_TRUE(dynamic_pkg_map.empty());
100 }
101
TEST(LoadedArscTest,LoadAppLinkedAgainstSharedLibrary)102 TEST(LoadedArscTest, LoadAppLinkedAgainstSharedLibrary) {
103 std::string contents;
104 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/libclient/libclient.apk",
105 "resources.arsc", &contents));
106
107 std::unique_ptr<const LoadedArsc> loaded_arsc =
108 LoadedArsc::Load(contents.data(), contents.size());
109 ASSERT_NE(nullptr, loaded_arsc);
110
111 const auto& packages = loaded_arsc->GetPackages();
112 ASSERT_EQ(1u, packages.size());
113
114 EXPECT_FALSE(packages[0]->IsDynamic());
115 EXPECT_EQ(std::string("com.android.libclient"), packages[0]->GetPackageName());
116 EXPECT_EQ(0x7f, packages[0]->GetPackageId());
117
118 const auto& dynamic_pkg_map = packages[0]->GetDynamicPackageMap();
119
120 // The library has two dependencies.
121 ASSERT_EQ(2u, dynamic_pkg_map.size());
122
123 EXPECT_EQ(std::string("com.android.lib_one"), dynamic_pkg_map[0].package_name);
124 EXPECT_EQ(0x02, dynamic_pkg_map[0].package_id);
125
126 EXPECT_EQ(std::string("com.android.lib_two"), dynamic_pkg_map[1].package_name);
127 EXPECT_EQ(0x03, dynamic_pkg_map[1].package_id);
128 }
129
TEST(LoadedArscTest,LoadAppAsSharedLibrary)130 TEST(LoadedArscTest, LoadAppAsSharedLibrary) {
131 std::string contents;
132 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/appaslib/appaslib.apk",
133 "resources.arsc", &contents));
134
135 std::unique_ptr<const LoadedArsc> loaded_arsc = LoadedArsc::Load(
136 contents.data(), contents.size(), false /*system*/, true /*load_as_shared_library*/);
137 ASSERT_NE(nullptr, loaded_arsc);
138
139 const auto& packages = loaded_arsc->GetPackages();
140 ASSERT_EQ(1u, packages.size());
141
142 EXPECT_TRUE(packages[0]->IsDynamic());
143 EXPECT_EQ(0x7f, packages[0]->GetPackageId());
144 }
145
TEST(LoadedArscTest,LoadFeatureSplit)146 TEST(LoadedArscTest, LoadFeatureSplit) {
147 std::string contents;
148 ASSERT_TRUE(ReadFileFromZipToString(GetTestDataPath() + "/feature/feature.apk", "resources.arsc",
149 &contents));
150 std::unique_ptr<const LoadedArsc> loaded_arsc =
151 LoadedArsc::Load(contents.data(), contents.size());
152 ASSERT_NE(nullptr, loaded_arsc);
153
154 ResTable_config desired_config;
155 memset(&desired_config, 0, sizeof(desired_config));
156
157 LoadedArscEntry entry;
158 ResTable_config selected_config;
159 uint32_t flags;
160
161 ASSERT_TRUE(loaded_arsc->FindEntry(basic::R::string::test3, desired_config, &entry,
162 &selected_config, &flags));
163
164 size_t len;
165 const char16_t* type_name16 = entry.type_string_ref.string16(&len);
166 ASSERT_NE(nullptr, type_name16);
167 ASSERT_NE(0u, len);
168
169 size_t utf8_len = utf16_to_utf8_length(type_name16, len);
170 std::string type_name;
171 type_name.resize(utf8_len);
172 utf16_to_utf8(type_name16, len, &*type_name.begin(), utf8_len + 1);
173
174 EXPECT_EQ(std::string("string"), type_name);
175 }
176
177 // structs with size fields (like Res_value, ResTable_entry) should be
178 // backwards and forwards compatible (aka checking the size field against
179 // sizeof(Res_value) might not be backwards compatible.
TEST(LoadedArscTest,LoadingShouldBeForwardsAndBackwardsCompatible)180 TEST(LoadedArscTest, LoadingShouldBeForwardsAndBackwardsCompatible) { ASSERT_TRUE(false); }
181
182 } // namespace android
183