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