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 | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 21 | using android::ResTable_map; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 22 | using ::testing::NotNull; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | |
| 24 | namespace aapt { |
| 25 | |
| 26 | TEST(ReferenceLinkerTest, LinkSimpleReferences) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 27 | std::unique_ptr<ResourceTable> table = |
| 28 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 29 | .SetPackageId("com.app.test", 0x7f) |
| 30 | .AddReference("com.app.test:string/foo", ResourceId(0x7f020000), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 31 | "com.app.test:string/bar") |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 33 | // Test use of local reference (w/o package name). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | .AddReference("com.app.test:string/bar", ResourceId(0x7f020001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | "string/baz") |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | .AddReference("com.app.test:string/baz", ResourceId(0x7f020002), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | "android:string/ok") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | std::unique_ptr<IAaptContext> context = |
| 42 | test::ContextBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | .SetCompilationPackage("com.app.test") |
| 44 | .SetPackageId(0x7f) |
| 45 | .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"}) |
| 46 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 47 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | test::StaticSymbolSourceBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 50 | .AddPublicSymbol("android:string/ok", ResourceId(0x01040034)) |
| 51 | .Build()) |
| 52 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 53 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | ReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | ASSERT_TRUE(linker.Consume(context.get(), table.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 56 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 57 | Reference* ref = test::GetValue<Reference>(table.get(), "com.app.test:string/foo"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 58 | ASSERT_THAT(ref, NotNull()); |
| 59 | ASSERT_TRUE(ref->id); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 60 | EXPECT_EQ(ResourceId(0x7f020001), ref->id.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | ref = test::GetValue<Reference>(table.get(), "com.app.test:string/bar"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 63 | ASSERT_THAT(ref, NotNull()); |
| 64 | ASSERT_TRUE(ref->id); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 65 | EXPECT_EQ(ResourceId(0x7f020002), ref->id.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 66 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | ref = test::GetValue<Reference>(table.get(), "com.app.test:string/baz"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 68 | ASSERT_THAT(ref, NotNull()); |
| 69 | ASSERT_TRUE(ref->id); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 70 | EXPECT_EQ(ResourceId(0x01040034), ref->id.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TEST(ReferenceLinkerTest, LinkStyleAttributes) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | std::unique_ptr<ResourceTable> table = |
| 75 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | .SetPackageId("com.app.test", 0x7f) |
| 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 | .SetPackageId("com.app.test", 0x7f) |
| 157 | .AddValue("com.app.test:style/Theme", ResourceId(0x7f020000), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | test::StyleBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 159 | .AddItem("com.android.support:attr/foo", |
| 160 | ResourceUtils::TryParseColor("#ff0000")) |
| 161 | .Build()) |
| 162 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 163 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 164 | ReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | ASSERT_TRUE(linker.Consume(context.get(), table.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 166 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 167 | Style* style = test::GetValue<Style>(table.get(), "com.app.test:style/Theme"); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 168 | ASSERT_THAT(style, NotNull()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 169 | ASSERT_EQ(1u, style->entries.size()); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 170 | ASSERT_TRUE(style->entries.front().key.id); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 171 | EXPECT_EQ(ResourceId(0x7f010000), style->entries.front().key.id.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 174 | TEST(ReferenceLinkerTest, FailToLinkPrivateSymbols) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 175 | std::unique_ptr<ResourceTable> table = |
| 176 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | .SetPackageId("com.app.test", 0x7f) |
| 178 | .AddReference("com.app.test:string/foo", ResourceId(0x7f020000), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | "android:string/hidden") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 180 | .Build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 181 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | std::unique_ptr<IAaptContext> context = |
| 183 | test::ContextBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | .SetCompilationPackage("com.app.test") |
| 185 | .SetPackageId(0x7f) |
| 186 | .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"}) |
| 187 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 188 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 189 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 190 | test::StaticSymbolSourceBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 191 | .AddSymbol("android:string/hidden", ResourceId(0x01040034)) |
| 192 | .Build()) |
| 193 | .Build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 194 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 195 | ReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 196 | ASSERT_FALSE(linker.Consume(context.get(), table.get())); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | TEST(ReferenceLinkerTest, FailToLinkPrivateMangledSymbols) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 200 | std::unique_ptr<ResourceTable> table = |
| 201 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 202 | .SetPackageId("com.app.test", 0x7f) |
| 203 | .AddReference("com.app.test:string/foo", ResourceId(0x7f020000), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 204 | "com.app.lib:string/hidden") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 205 | .Build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 206 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 207 | std::unique_ptr<IAaptContext> context = |
| 208 | test::ContextBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 209 | .SetCompilationPackage("com.app.test") |
| 210 | .SetPackageId(0x7f) |
| 211 | .SetNameManglerPolicy( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 212 | NameManglerPolicy{"com.app.test", {"com.app.lib"}}) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 213 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 214 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
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 | test::StaticSymbolSourceBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 217 | .AddSymbol("com.app.test:string/com.app.lib$hidden", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | ResourceId(0x7f040034)) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 219 | .Build()) |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 220 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 221 | .Build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 222 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 223 | ReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 224 | ASSERT_FALSE(linker.Consume(context.get(), table.get())); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | TEST(ReferenceLinkerTest, FailToLinkPrivateStyleAttributes) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 228 | std::unique_ptr<ResourceTable> table = |
| 229 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 230 | .SetPackageId("com.app.test", 0x7f) |
| 231 | .AddValue("com.app.test:style/Theme", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 232 | test::StyleBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 233 | .AddItem("android:attr/hidden", |
| 234 | ResourceUtils::TryParseColor("#ff00ff")) |
| 235 | .Build()) |
| 236 | .Build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 237 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 238 | std::unique_ptr<IAaptContext> context = |
| 239 | test::ContextBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 240 | .SetCompilationPackage("com.app.test") |
| 241 | .SetPackageId(0x7f) |
| 242 | .SetNameManglerPolicy(NameManglerPolicy{"com.app.test"}) |
| 243 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 244 | util::make_unique<ResourceTableSymbolSource>(table.get())) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 245 | .AddSymbolSource( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 246 | test::StaticSymbolSourceBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 247 | .AddSymbol("android:attr/hidden", ResourceId(0x01010001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 248 | test::AttributeBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | .SetTypeMask(android::ResTable_map::TYPE_COLOR) |
| 250 | .Build()) |
| 251 | .Build()) |
| 252 | .Build(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 253 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 254 | ReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 255 | ASSERT_FALSE(linker.Consume(context.get(), table.get())); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 258 | TEST(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 Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 269 | ASSERT_THAT(symbol, NotNull()); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 270 | EXPECT_TRUE(error.empty()); |
| 271 | } |
| 272 | |
| 273 | TEST(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 Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 286 | EXPECT_FALSE(ReferenceLinker::CompileXmlAttribute( |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 287 | *test::BuildReference("com.app.test:attr/foo"), call_site, &table, &error)); |
| 288 | EXPECT_FALSE(error.empty()); |
| 289 | |
| 290 | error = ""; |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 291 | ASSERT_TRUE(ReferenceLinker::CompileXmlAttribute( |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 292 | *test::BuildReference("com.app.test:attr/public_foo"), call_site, &table, &error)); |
| 293 | EXPECT_TRUE(error.empty()); |
| 294 | } |
| 295 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 296 | } // namespace aapt |