Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 17 | #include "link/ReferenceLinker.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 19 | #include "test/Test.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 21 | using ::android::ResTable_map; |
| 22 | using ::testing::Eq; |
| 23 | using ::testing::IsNull; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 24 | using ::testing::NotNull; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 25 | |
| 26 | namespace aapt { |
| 27 | |
| 28 | TEST(ReferenceLinkerTest, LinkSimpleReferences) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 29 | std::unique_ptr<ResourceTable> table = |
| 30 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 31 | .SetPackageId("com.app.test", 0x7f) |
| 32 | .AddReference("com.app.test:string/foo", ResourceId(0x7f020000), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 33 | "com.app.test:string/bar") |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | // Test use of local reference (w/o package name). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | .AddReference("com.app.test:string/bar", ResourceId(0x7f020001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 37 | "string/baz") |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 38 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | .AddReference("com.app.test:string/baz", ResourceId(0x7f020002), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | "android:string/ok") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | std::unique_ptr<IAaptContext> context = |
| 44 | test::ContextBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | .SetCompilationPackage("com.app.test") |
| 46 | .SetPackageId(0x7f) |
| 47 | .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"}) |
| 48 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 50 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | test::StaticSymbolSourceBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | .AddPublicSymbol("android:string/ok", ResourceId(0x01040034)) |
| 53 | .Build()) |
| 54 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 55 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | ReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | ASSERT_TRUE(linker.Consume(context.get(), table.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 58 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 59 | Reference* ref = test::GetValue<Reference>(table.get(), "com.app.test:string/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 60 | ASSERT_THAT(ref, NotNull()); |
| 61 | ASSERT_TRUE(ref->id); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 62 | EXPECT_EQ(ResourceId(0x7f020001), ref->id.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | ref = test::GetValue<Reference>(table.get(), "com.app.test:string/bar"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 65 | ASSERT_THAT(ref, NotNull()); |
| 66 | ASSERT_TRUE(ref->id); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 67 | EXPECT_EQ(ResourceId(0x7f020002), ref->id.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 68 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | ref = test::GetValue<Reference>(table.get(), "com.app.test:string/baz"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 70 | ASSERT_THAT(ref, NotNull()); |
| 71 | ASSERT_TRUE(ref->id); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 72 | EXPECT_EQ(ResourceId(0x01040034), ref->id.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TEST(ReferenceLinkerTest, LinkStyleAttributes) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | std::unique_ptr<ResourceTable> table = |
| 77 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | .SetPackageId("com.app.test", 0x7f) |
| 79 | .AddValue("com.app.test:style/Theme", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | test::StyleBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | .SetParent("android:style/Theme.Material") |
| 82 | .AddItem("android:attr/foo", |
| 83 | ResourceUtils::TryParseColor("#ff00ff")) |
| 84 | .AddItem("android:attr/bar", {} /* placeholder */) |
| 85 | .Build()) |
| 86 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 87 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | { |
| 89 | // We need to fill in the value for the attribute android:attr/bar after we |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 90 | // build the table, because we need access to the string pool. |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 91 | Style* style = test::GetValue<Style>(table.get(), "com.app.test:style/Theme"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 92 | ASSERT_THAT(style, NotNull()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | style->entries.back().value = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 94 | util::make_unique<RawString>(table->string_pool.MakeRef("one|two")); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 96 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | std::unique_ptr<IAaptContext> context = |
| 98 | test::ContextBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 99 | .SetCompilationPackage("com.app.test") |
| 100 | .SetPackageId(0x7f) |
| 101 | .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"}) |
| 102 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | test::StaticSymbolSourceBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | .AddPublicSymbol("android:style/Theme.Material", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | ResourceId(0x01060000)) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 106 | .AddPublicSymbol("android:attr/foo", ResourceId(0x01010001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | test::AttributeBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | .SetTypeMask(ResTable_map::TYPE_COLOR) |
| 109 | .Build()) |
| 110 | .AddPublicSymbol("android:attr/bar", ResourceId(0x01010002), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 111 | test::AttributeBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | .SetTypeMask(ResTable_map::TYPE_FLAGS) |
| 113 | .AddItem("one", 0x01) |
| 114 | .AddItem("two", 0x02) |
| 115 | .Build()) |
| 116 | .Build()) |
| 117 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 118 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | ReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 120 | ASSERT_TRUE(linker.Consume(context.get(), table.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 121 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | Style* style = test::GetValue<Style>(table.get(), "com.app.test:style/Theme"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 123 | ASSERT_THAT(style, NotNull()); |
| 124 | ASSERT_TRUE(style->parent); |
| 125 | ASSERT_TRUE(style->parent.value().id); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 126 | EXPECT_EQ(ResourceId(0x01060000), style->parent.value().id.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | |
| 128 | ASSERT_EQ(2u, style->entries.size()); |
| 129 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 130 | ASSERT_TRUE(style->entries[0].key.id); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 131 | EXPECT_EQ(ResourceId(0x01010001), style->entries[0].key.id.value()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 132 | ASSERT_THAT(ValueCast<BinaryPrimitive>(style->entries[0].value.get()), NotNull()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 133 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 134 | ASSERT_TRUE(style->entries[1].key.id); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 135 | EXPECT_EQ(ResourceId(0x01010002), style->entries[1].key.id.value()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 136 | ASSERT_THAT(ValueCast<BinaryPrimitive>(style->entries[1].value.get()), NotNull()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST(ReferenceLinkerTest, LinkMangledReferencesAndAttributes) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | std::unique_ptr<IAaptContext> context = |
| 141 | test::ContextBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | .SetCompilationPackage("com.app.test") |
| 143 | .SetPackageId(0x7f) |
| 144 | .SetNameManglerPolicy( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 145 | NameManglerPolicy{"com.app.test", {"com.android.support"}}) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 146 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | test::StaticSymbolSourceBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 148 | .AddPublicSymbol("com.app.test:attr/com.android.support$foo", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | ResourceId(0x7f010000), |
| 150 | test::AttributeBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 151 | .SetTypeMask(ResTable_map::TYPE_COLOR) |
| 152 | .Build()) |
| 153 | .Build()) |
| 154 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 155 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | std::unique_ptr<ResourceTable> table = |
| 157 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 158 | .SetPackageId("com.app.test", 0x7f) |
| 159 | .AddValue("com.app.test:style/Theme", ResourceId(0x7f020000), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 160 | test::StyleBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 161 | .AddItem("com.android.support:attr/foo", |
| 162 | ResourceUtils::TryParseColor("#ff0000")) |
| 163 | .Build()) |
| 164 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 165 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | ReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 167 | ASSERT_TRUE(linker.Consume(context.get(), table.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 168 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | Style* style = test::GetValue<Style>(table.get(), "com.app.test:style/Theme"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 170 | ASSERT_THAT(style, NotNull()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | ASSERT_EQ(1u, style->entries.size()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 172 | ASSERT_TRUE(style->entries.front().key.id); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 173 | EXPECT_EQ(ResourceId(0x7f010000), style->entries.front().key.id.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 176 | TEST(ReferenceLinkerTest, FailToLinkPrivateSymbols) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 177 | std::unique_ptr<ResourceTable> table = |
| 178 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 179 | .SetPackageId("com.app.test", 0x7f) |
| 180 | .AddReference("com.app.test:string/foo", ResourceId(0x7f020000), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | "android:string/hidden") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | .Build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 183 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 184 | std::unique_ptr<IAaptContext> context = |
| 185 | test::ContextBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | .SetCompilationPackage("com.app.test") |
| 187 | .SetPackageId(0x7f) |
| 188 | .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"}) |
| 189 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 190 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 191 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | test::StaticSymbolSourceBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 193 | .AddSymbol("android:string/hidden", ResourceId(0x01040034)) |
| 194 | .Build()) |
| 195 | .Build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 196 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 197 | ReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 198 | ASSERT_FALSE(linker.Consume(context.get(), table.get())); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | TEST(ReferenceLinkerTest, FailToLinkPrivateMangledSymbols) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 202 | std::unique_ptr<ResourceTable> table = |
| 203 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 204 | .SetPackageId("com.app.test", 0x7f) |
| 205 | .AddReference("com.app.test:string/foo", ResourceId(0x7f020000), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 206 | "com.app.lib:string/hidden") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | .Build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 208 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | std::unique_ptr<IAaptContext> context = |
| 210 | test::ContextBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 | .SetCompilationPackage("com.app.test") |
| 212 | .SetPackageId(0x7f) |
| 213 | .SetNameManglerPolicy( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 214 | NameManglerPolicy{"com.app.test", {"com.app.lib"}}) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 215 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 216 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 217 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | test::StaticSymbolSourceBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 219 | .AddSymbol("com.app.test:string/com.app.lib$hidden", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 220 | ResourceId(0x7f040034)) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 221 | .Build()) |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 222 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 223 | .Build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 224 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 225 | ReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 226 | ASSERT_FALSE(linker.Consume(context.get(), table.get())); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | TEST(ReferenceLinkerTest, FailToLinkPrivateStyleAttributes) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 230 | std::unique_ptr<ResourceTable> table = |
| 231 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 232 | .SetPackageId("com.app.test", 0x7f) |
| 233 | .AddValue("com.app.test:style/Theme", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 234 | test::StyleBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 235 | .AddItem("android:attr/hidden", |
| 236 | ResourceUtils::TryParseColor("#ff00ff")) |
| 237 | .Build()) |
| 238 | .Build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 239 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 240 | std::unique_ptr<IAaptContext> context = |
| 241 | test::ContextBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 242 | .SetCompilationPackage("com.app.test") |
| 243 | .SetPackageId(0x7f) |
| 244 | .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"}) |
| 245 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 246 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 248 | test::StaticSymbolSourceBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | .AddSymbol("android:attr/hidden", ResourceId(0x01010001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 250 | test::AttributeBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 251 | .SetTypeMask(android::ResTable_map::TYPE_COLOR) |
| 252 | .Build()) |
| 253 | .Build()) |
| 254 | .Build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 255 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 256 | ReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 257 | ASSERT_FALSE(linker.Consume(context.get(), table.get())); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 260 | TEST(ReferenceLinkerTest, AppsWithSamePackageButDifferentIdAreVisibleNonPublic) { |
| 261 | NameMangler mangler(NameManglerPolicy{"com.app.test"}); |
| 262 | SymbolTable table(&mangler); |
| 263 | table.AppendSource(test::StaticSymbolSourceBuilder() |
| 264 | .AddSymbol("com.app.test:string/foo", ResourceId(0x7f010000)) |
| 265 | .Build()); |
| 266 | |
| 267 | std::string error; |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 268 | const CallSite call_site{"com.app.test"}; |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 269 | const SymbolTable::Symbol* symbol = ReferenceLinker::ResolveSymbolCheckVisibility( |
Chris Warrington | 481f027 | 2018-02-06 14:03:39 +0000 | [diff] [blame] | 270 | *test::BuildReference("com.app.test:string/foo"), call_site, &table, false, &error); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 271 | ASSERT_THAT(symbol, NotNull()); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 272 | EXPECT_TRUE(error.empty()); |
| 273 | } |
| 274 | |
| 275 | TEST(ReferenceLinkerTest, AppsWithDifferentPackageCanNotUseEachOthersAttribute) { |
| 276 | NameMangler mangler(NameManglerPolicy{"com.app.ext"}); |
| 277 | SymbolTable table(&mangler); |
| 278 | table.AppendSource(test::StaticSymbolSourceBuilder() |
| 279 | .AddSymbol("com.app.test:attr/foo", ResourceId(0x7f010000), |
| 280 | test::AttributeBuilder().Build()) |
| 281 | .AddPublicSymbol("com.app.test:attr/public_foo", ResourceId(0x7f010001), |
| 282 | test::AttributeBuilder().Build()) |
| 283 | .Build()); |
| 284 | |
| 285 | std::string error; |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 286 | const CallSite call_site{"com.app.ext"}; |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 287 | |
Chris Warrington | 481f027 | 2018-02-06 14:03:39 +0000 | [diff] [blame] | 288 | EXPECT_FALSE(ReferenceLinker::CompileXmlAttribute(*test::BuildReference("com.app.test:attr/foo"), |
| 289 | call_site, &table, false, &error)); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 290 | EXPECT_FALSE(error.empty()); |
| 291 | |
| 292 | error = ""; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 293 | ASSERT_TRUE(ReferenceLinker::CompileXmlAttribute( |
Chris Warrington | 481f027 | 2018-02-06 14:03:39 +0000 | [diff] [blame] | 294 | *test::BuildReference("com.app.test:attr/public_foo"), call_site, &table, false, &error)); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 295 | EXPECT_TRUE(error.empty()); |
| 296 | } |
| 297 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 298 | TEST(ReferenceLinkerTest, ReferenceWithNoPackageUsesCallSitePackage) { |
| 299 | NameMangler mangler(NameManglerPolicy{"com.app.test"}); |
| 300 | SymbolTable table(&mangler); |
| 301 | table.AppendSource(test::StaticSymbolSourceBuilder() |
| 302 | .AddSymbol("com.app.test:string/foo", ResourceId(0x7f010000)) |
| 303 | .AddSymbol("com.app.lib:string/foo", ResourceId(0x7f010001)) |
| 304 | .Build()); |
| 305 | |
Chris Warrington | 481f027 | 2018-02-06 14:03:39 +0000 | [diff] [blame] | 306 | const SymbolTable::Symbol* s = ReferenceLinker::ResolveSymbol( |
| 307 | *test::BuildReference("string/foo"), CallSite{"com.app.test"}, &table, false); |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 308 | ASSERT_THAT(s, NotNull()); |
| 309 | EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010000))); |
| 310 | |
| 311 | s = ReferenceLinker::ResolveSymbol(*test::BuildReference("string/foo"), CallSite{"com.app.lib"}, |
Chris Warrington | 481f027 | 2018-02-06 14:03:39 +0000 | [diff] [blame] | 312 | &table, false); |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 313 | ASSERT_THAT(s, NotNull()); |
| 314 | EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010001))); |
| 315 | |
| 316 | EXPECT_THAT(ReferenceLinker::ResolveSymbol(*test::BuildReference("string/foo"), |
Chris Warrington | 481f027 | 2018-02-06 14:03:39 +0000 | [diff] [blame] | 317 | CallSite{"com.app.bad"}, &table, false), |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 318 | IsNull()); |
| 319 | } |
| 320 | |
Chris Warrington | 481f027 | 2018-02-06 14:03:39 +0000 | [diff] [blame] | 321 | TEST(ReferenceLinkerTest, AutomaticNamespace) { |
| 322 | NameMangler mangler(NameManglerPolicy{"com.example.thislib"}); |
| 323 | SymbolTable table(&mangler); |
| 324 | table.AppendSource( |
| 325 | test::StaticSymbolSourceBuilder() |
| 326 | .AddSymbol("com.example.thislib:string/thislib_string", ResourceId(0x7f010006)) |
| 327 | .AddSymbol("com.example.thislib:string/explicit_override_string", ResourceId(0x7f010007)) |
| 328 | .Build()); |
| 329 | // Lib2 is higher priority than lib1 |
| 330 | table.AppendSource( |
| 331 | test::StaticSymbolSourceBuilder() |
| 332 | .AddSymbol("com.example.lib2:string/lib2_string", ResourceId(0x7f010003)) |
| 333 | .AddSymbol("com.example.lib2:string/explicit_override_string", ResourceId(0x7f010004)) |
| 334 | .AddSymbol("com.example.lib2:string/implicit_override_string", ResourceId(0x7f010005)) |
| 335 | .Build()); |
| 336 | table.AppendSource( |
| 337 | test::StaticSymbolSourceBuilder() |
| 338 | .AddSymbol("com.example.lib1:string/explicit_override_string", ResourceId(0x7f010001)) |
| 339 | .AddSymbol("com.example.lib1:string/implicit_override_string", ResourceId(0x7f010002)) |
| 340 | .Build()); |
| 341 | |
| 342 | // Sanity test: Local references are still fine. |
| 343 | const SymbolTable::Symbol* s = |
| 344 | ReferenceLinker::ResolveSymbol(*test::BuildReference("string/thislib_string"), |
| 345 | CallSite{"com.example.thislib"}, &table, true); |
| 346 | ASSERT_THAT(s, NotNull()); |
| 347 | EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010006))); |
| 348 | |
| 349 | // Local references are fine, even if clash with remote ones. |
| 350 | s = ReferenceLinker::ResolveSymbol(*test::BuildReference("string/explicit_override_string"), |
| 351 | CallSite{"com.example.thislib"}, &table, true); |
| 352 | ASSERT_THAT(s, NotNull()); |
| 353 | EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010007))); |
| 354 | |
| 355 | // An unqualified reference to lib2 is rewritten |
| 356 | s = ReferenceLinker::ResolveSymbol(*test::BuildReference("string/lib2_string"), |
| 357 | CallSite{"com.example.thislib"}, &table, true); |
| 358 | ASSERT_THAT(s, NotNull()); |
| 359 | EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010003))); |
| 360 | |
| 361 | // Qualified references are left alone. |
| 362 | s = ReferenceLinker::ResolveSymbol( |
| 363 | *test::BuildReference("com.example.lib2:string/explicit_override_string"), |
| 364 | CallSite{"com.example.thislib"}, &table, true); |
| 365 | ASSERT_THAT(s, NotNull()); |
| 366 | EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010004))); |
| 367 | |
| 368 | // Implicit overrides respect priority ordering. |
| 369 | s = ReferenceLinker::ResolveSymbol(*test::BuildReference("string/implicit_override_string"), |
| 370 | CallSite{"com.example.thislib"}, &table, true); |
| 371 | ASSERT_THAT(s, NotNull()); |
| 372 | EXPECT_THAT(s->id, Eq(make_value<ResourceId>(0x7f010005))); |
| 373 | |
| 374 | // |
| 375 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 376 | } // namespace aapt |