blob: 72a91689e39277901980bc68da8c9689c8f7561a [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 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
Adam Lesinski467f1712015-11-16 17:35:44 -080017#include "link/ReferenceLinker.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinski64587af2016-02-18 18:33:06 -080019#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020
Adam Lesinski64587af2016-02-18 18:33:06 -080021using android::ResTable_map;
Adam Lesinskia45893a2017-05-30 15:19:02 -070022using ::testing::NotNull;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023
24namespace aapt {
25
26TEST(ReferenceLinkerTest, LinkSimpleReferences) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070027 std::unique_ptr<ResourceTable> table =
28 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029 .SetPackageId("com.app.test", 0x7f)
30 .AddReference("com.app.test:string/foo", ResourceId(0x7f020000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 "com.app.test:string/bar")
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 // Test use of local reference (w/o package name).
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 .AddReference("com.app.test:string/bar", ResourceId(0x7f020001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 "string/baz")
Adam Lesinski1ab598f2015-08-14 14:26:04 -070036
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037 .AddReference("com.app.test:string/baz", ResourceId(0x7f020002),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 "android:string/ok")
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 std::unique_ptr<IAaptContext> context =
42 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 .SetCompilationPackage("com.app.test")
44 .SetPackageId(0x7f)
45 .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"})
46 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 .AddPublicSymbol("android:string/ok", ResourceId(0x01040034))
51 .Build())
52 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 ASSERT_TRUE(linker.Consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070056
Adam Lesinskif34b6f42017-03-03 16:33:26 -080057 Reference* ref = test::GetValue<Reference>(table.get(), "com.app.test:string/foo");
Adam Lesinskia45893a2017-05-30 15:19:02 -070058 ASSERT_THAT(ref, NotNull());
59 ASSERT_TRUE(ref->id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -080060 EXPECT_EQ(ResourceId(0x7f020001), ref->id.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070061
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 ref = test::GetValue<Reference>(table.get(), "com.app.test:string/bar");
Adam Lesinskia45893a2017-05-30 15:19:02 -070063 ASSERT_THAT(ref, NotNull());
64 ASSERT_TRUE(ref->id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -080065 EXPECT_EQ(ResourceId(0x7f020002), ref->id.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070066
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 ref = test::GetValue<Reference>(table.get(), "com.app.test:string/baz");
Adam Lesinskia45893a2017-05-30 15:19:02 -070068 ASSERT_THAT(ref, NotNull());
69 ASSERT_TRUE(ref->id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -080070 EXPECT_EQ(ResourceId(0x01040034), ref->id.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071}
72
73TEST(ReferenceLinkerTest, LinkStyleAttributes) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 std::unique_ptr<ResourceTable> table =
75 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 .SetPackageId("com.app.test", 0x7f)
77 .AddValue("com.app.test:style/Theme",
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 test::StyleBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 .SetParent("android:style/Theme.Material")
80 .AddItem("android:attr/foo",
81 ResourceUtils::TryParseColor("#ff00ff"))
82 .AddItem("android:attr/bar", {} /* placeholder */)
83 .Build())
84 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 {
87 // We need to fill in the value for the attribute android:attr/bar after we
Adam Lesinskia45893a2017-05-30 15:19:02 -070088 // build the table, because we need access to the string pool.
Adam Lesinskif34b6f42017-03-03 16:33:26 -080089 Style* style = test::GetValue<Style>(table.get(), "com.app.test:style/Theme");
Adam Lesinskia45893a2017-05-30 15:19:02 -070090 ASSERT_THAT(style, NotNull());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 style->entries.back().value =
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 util::make_unique<RawString>(table->string_pool.MakeRef("one|two"));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 std::unique_ptr<IAaptContext> context =
96 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 .SetCompilationPackage("com.app.test")
98 .SetPackageId(0x7f)
99 .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"})
100 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 .AddPublicSymbol("android:style/Theme.Material",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 ResourceId(0x01060000))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104 .AddPublicSymbol("android:attr/foo", ResourceId(0x01010001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 .SetTypeMask(ResTable_map::TYPE_COLOR)
107 .Build())
108 .AddPublicSymbol("android:attr/bar", ResourceId(0x01010002),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 .SetTypeMask(ResTable_map::TYPE_FLAGS)
111 .AddItem("one", 0x01)
112 .AddItem("two", 0x02)
113 .Build())
114 .Build())
115 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 ASSERT_TRUE(linker.Consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 Style* style = test::GetValue<Style>(table.get(), "com.app.test:style/Theme");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700121 ASSERT_THAT(style, NotNull());
122 ASSERT_TRUE(style->parent);
123 ASSERT_TRUE(style->parent.value().id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800124 EXPECT_EQ(ResourceId(0x01060000), style->parent.value().id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125
126 ASSERT_EQ(2u, style->entries.size());
127
Adam Lesinskia45893a2017-05-30 15:19:02 -0700128 ASSERT_TRUE(style->entries[0].key.id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800129 EXPECT_EQ(ResourceId(0x01010001), style->entries[0].key.id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700130 ASSERT_THAT(ValueCast<BinaryPrimitive>(style->entries[0].value.get()), NotNull());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131
Adam Lesinskia45893a2017-05-30 15:19:02 -0700132 ASSERT_TRUE(style->entries[1].key.id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800133 EXPECT_EQ(ResourceId(0x01010002), style->entries[1].key.id.value());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700134 ASSERT_THAT(ValueCast<BinaryPrimitive>(style->entries[1].value.get()), NotNull());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135}
136
137TEST(ReferenceLinkerTest, LinkMangledReferencesAndAttributes) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 std::unique_ptr<IAaptContext> context =
139 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 .SetCompilationPackage("com.app.test")
141 .SetPackageId(0x7f)
142 .SetNameManglerPolicy(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 NameManglerPolicy{"com.app.test", {"com.android.support"}})
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 .AddPublicSymbol("com.app.test:attr/com.android.support$foo",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 ResourceId(0x7f010000),
148 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 .SetTypeMask(ResTable_map::TYPE_COLOR)
150 .Build())
151 .Build())
152 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 std::unique_ptr<ResourceTable> table =
155 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 .SetPackageId("com.app.test", 0x7f)
157 .AddValue("com.app.test:style/Theme", ResourceId(0x7f020000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 test::StyleBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 .AddItem("com.android.support:attr/foo",
160 ResourceUtils::TryParseColor("#ff0000"))
161 .Build())
162 .Build();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 ASSERT_TRUE(linker.Consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 Style* style = test::GetValue<Style>(table.get(), "com.app.test:style/Theme");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700168 ASSERT_THAT(style, NotNull());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 ASSERT_EQ(1u, style->entries.size());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700170 ASSERT_TRUE(style->entries.front().key.id);
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800171 EXPECT_EQ(ResourceId(0x7f010000), style->entries.front().key.id.value());
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172}
173
Adam Lesinski467f1712015-11-16 17:35:44 -0800174TEST(ReferenceLinkerTest, FailToLinkPrivateSymbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700175 std::unique_ptr<ResourceTable> table =
176 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 .SetPackageId("com.app.test", 0x7f)
178 .AddReference("com.app.test:string/foo", ResourceId(0x7f020000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 "android:string/hidden")
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800181
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 std::unique_ptr<IAaptContext> context =
183 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 .SetCompilationPackage("com.app.test")
185 .SetPackageId(0x7f)
186 .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"})
187 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 .AddSymbol("android:string/hidden", ResourceId(0x01040034))
192 .Build())
193 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800194
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700196 ASSERT_FALSE(linker.Consume(context.get(), table.get()));
Adam Lesinski467f1712015-11-16 17:35:44 -0800197}
198
199TEST(ReferenceLinkerTest, FailToLinkPrivateMangledSymbols) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 std::unique_ptr<ResourceTable> table =
201 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700202 .SetPackageId("com.app.test", 0x7f)
203 .AddReference("com.app.test:string/foo", ResourceId(0x7f020000),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 "com.app.lib:string/hidden")
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800206
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207 std::unique_ptr<IAaptContext> context =
208 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 .SetCompilationPackage("com.app.test")
210 .SetPackageId(0x7f)
211 .SetNameManglerPolicy(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 NameManglerPolicy{"com.app.test", {"com.app.lib"}})
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700213 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700215 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700217 .AddSymbol("com.app.test:string/com.app.lib$hidden",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 ResourceId(0x7f040034))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700219 .Build())
Adam Lesinski64587af2016-02-18 18:33:06 -0800220
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800222
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700224 ASSERT_FALSE(linker.Consume(context.get(), table.get()));
Adam Lesinski467f1712015-11-16 17:35:44 -0800225}
226
227TEST(ReferenceLinkerTest, FailToLinkPrivateStyleAttributes) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 std::unique_ptr<ResourceTable> table =
229 test::ResourceTableBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700230 .SetPackageId("com.app.test", 0x7f)
231 .AddValue("com.app.test:style/Theme",
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 test::StyleBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233 .AddItem("android:attr/hidden",
234 ResourceUtils::TryParseColor("#ff00ff"))
235 .Build())
236 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800237
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 std::unique_ptr<IAaptContext> context =
239 test::ContextBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700240 .SetCompilationPackage("com.app.test")
241 .SetPackageId(0x7f)
242 .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"})
243 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 util::make_unique<ResourceTableSymbolSource>(table.get()))
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 .AddSymbolSource(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 test::StaticSymbolSourceBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700247 .AddSymbol("android:attr/hidden", ResourceId(0x01010001),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700248 test::AttributeBuilder()
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700249 .SetTypeMask(android::ResTable_map::TYPE_COLOR)
250 .Build())
251 .Build())
252 .Build();
Adam Lesinski467f1712015-11-16 17:35:44 -0800253
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 ReferenceLinker linker;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 ASSERT_FALSE(linker.Consume(context.get(), table.get()));
Adam Lesinski467f1712015-11-16 17:35:44 -0800256}
257
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800258TEST(ReferenceLinkerTest, AppsWithSamePackageButDifferentIdAreVisibleNonPublic) {
259 NameMangler mangler(NameManglerPolicy{"com.app.test"});
260 SymbolTable table(&mangler);
261 table.AppendSource(test::StaticSymbolSourceBuilder()
262 .AddSymbol("com.app.test:string/foo", ResourceId(0x7f010000))
263 .Build());
264
265 std::string error;
266 const CallSite call_site{ResourceNameRef("com.app.test", ResourceType::kString, "foo")};
267 const SymbolTable::Symbol* symbol = ReferenceLinker::ResolveSymbolCheckVisibility(
268 *test::BuildReference("com.app.test:string/foo"), call_site, &table, &error);
Adam Lesinskia45893a2017-05-30 15:19:02 -0700269 ASSERT_THAT(symbol, NotNull());
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800270 EXPECT_TRUE(error.empty());
271}
272
273TEST(ReferenceLinkerTest, AppsWithDifferentPackageCanNotUseEachOthersAttribute) {
274 NameMangler mangler(NameManglerPolicy{"com.app.ext"});
275 SymbolTable table(&mangler);
276 table.AppendSource(test::StaticSymbolSourceBuilder()
277 .AddSymbol("com.app.test:attr/foo", ResourceId(0x7f010000),
278 test::AttributeBuilder().Build())
279 .AddPublicSymbol("com.app.test:attr/public_foo", ResourceId(0x7f010001),
280 test::AttributeBuilder().Build())
281 .Build());
282
283 std::string error;
284 const CallSite call_site{ResourceNameRef("com.app.ext", ResourceType::kLayout, "foo")};
285
Adam Lesinskia45893a2017-05-30 15:19:02 -0700286 EXPECT_FALSE(ReferenceLinker::CompileXmlAttribute(
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800287 *test::BuildReference("com.app.test:attr/foo"), call_site, &table, &error));
288 EXPECT_FALSE(error.empty());
289
290 error = "";
Adam Lesinskia45893a2017-05-30 15:19:02 -0700291 ASSERT_TRUE(ReferenceLinker::CompileXmlAttribute(
Adam Lesinskif34b6f42017-03-03 16:33:26 -0800292 *test::BuildReference("com.app.test:attr/public_foo"), call_site, &table, &error));
293 EXPECT_TRUE(error.empty());
294}
295
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296} // namespace aapt