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 | |
| 17 | #include "compile/IdAssigner.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 19 | #include "test/Test.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | |
| 21 | namespace aapt { |
| 22 | |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 23 | struct IdAssignerTests : public ::testing::Test { |
| 24 | void SetUp() override { |
| 25 | context = test::ContextBuilder().SetCompilationPackage("android").SetPackageId(0x01).Build(); |
| 26 | } |
| 27 | std::unique_ptr<IAaptContext> context; |
| 28 | }; |
| 29 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | ::testing::AssertionResult VerifyIds(ResourceTable* table); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 32 | TEST_F(IdAssignerTests, AssignIds) { |
| 33 | auto table = test::ResourceTableBuilder() |
| 34 | .AddSimple("android:attr/foo") |
| 35 | .AddSimple("android:attr/bar") |
| 36 | .AddSimple("android:id/foo") |
| 37 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | IdAssigner assigner; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 40 | ASSERT_TRUE(assigner.Consume(context.get(), table.get())); |
| 41 | ASSERT_TRUE(VerifyIds(table.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 44 | TEST_F(IdAssignerTests, AssignIdsWithReservedIds) { |
| 45 | auto table = test::ResourceTableBuilder() |
| 46 | .AddSimple("android:id/foo", ResourceId(0x01010000)) |
| 47 | .AddSimple("android:dimen/two") |
| 48 | .AddSimple("android:integer/three") |
| 49 | .AddSimple("android:string/five") |
| 50 | .AddSimple("android:attr/fun", ResourceId(0x01040000)) |
| 51 | .AddSimple("android:attr/foo", ResourceId(0x01040006)) |
| 52 | .AddSimple("android:attr/bar") |
| 53 | .AddSimple("android:attr/baz") |
| 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 | IdAssigner assigner; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | ASSERT_TRUE(assigner.Consume(context.get(), table.get())); |
| 58 | ASSERT_TRUE(VerifyIds(table.get())); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 59 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | Maybe<ResourceTable::SearchResult> maybe_result; |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 62 | // Expect to fill in the gaps between 0x0101XXXX and 0x0104XXXX. |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | maybe_result = table->FindResource(test::ParseNameOrDie("android:dimen/two")); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 65 | ASSERT_TRUE(maybe_result); |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 66 | EXPECT_EQ(make_value<ResourceId>(0x01020000), maybe_result.value().entry->id); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 67 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | maybe_result = |
| 69 | table->FindResource(test::ParseNameOrDie("android:integer/three")); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 70 | ASSERT_TRUE(maybe_result); |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 71 | EXPECT_EQ(make_value<ResourceId>(0x01030000), maybe_result.value().entry->id); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 72 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | // Expect to bypass the reserved 0x0104XXXX IDs and use the next 0x0105XXXX |
| 74 | // IDs. |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | maybe_result = |
| 77 | table->FindResource(test::ParseNameOrDie("android:string/five")); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(maybe_result); |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 79 | EXPECT_EQ(make_value<ResourceId>(0x01050000), maybe_result.value().entry->id); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 81 | // Expect to fill in the gaps between 0x01040000 and 0x01040006. |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 82 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | maybe_result = table->FindResource(test::ParseNameOrDie("android:attr/bar")); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 84 | ASSERT_TRUE(maybe_result); |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 85 | EXPECT_EQ(make_value<ResourceId>(0x01040001), maybe_result.value().entry->id); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 86 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | maybe_result = table->FindResource(test::ParseNameOrDie("android:attr/baz")); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 88 | ASSERT_TRUE(maybe_result); |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 89 | EXPECT_EQ(make_value<ResourceId>(0x01040002), maybe_result.value().entry->id); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 92 | TEST_F(IdAssignerTests, FailWhenNonUniqueIdsAssigned) { |
| 93 | auto table = test::ResourceTableBuilder() |
| 94 | .AddSimple("android:attr/foo", ResourceId(0x01040006)) |
| 95 | .AddSimple("android:attr/bar", ResourceId(0x01040006)) |
| 96 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | IdAssigner assigner; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | ASSERT_FALSE(assigner.Consume(context.get(), table.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame^] | 101 | TEST_F(IdAssignerTests, FailWhenNonUniqueTypeIdsAssigned) { |
| 102 | auto table = test::ResourceTableBuilder() |
| 103 | .AddSimple("android:string/foo", ResourceId(0x01040000)) |
| 104 | .AddSimple("android:attr/bar", ResourceId(0x01040006)) |
| 105 | .Build(); |
| 106 | IdAssigner assigner; |
| 107 | ASSERT_FALSE(assigner.Consume(context.get(), table.get())); |
| 108 | } |
| 109 | |
| 110 | TEST_F(IdAssignerTests, FailWhenTypeHasTwoNonStagedIds) { |
| 111 | auto table = test::ResourceTableBuilder() |
| 112 | .AddSimple("android:attr/foo", ResourceId(0x01050000)) |
| 113 | .AddSimple("android:attr/bar", ResourceId(0x01040006)) |
| 114 | .Build(); |
| 115 | IdAssigner assigner; |
| 116 | ASSERT_FALSE(assigner.Consume(context.get(), table.get())); |
| 117 | } |
| 118 | |
| 119 | TEST_F(IdAssignerTests, FailWhenTypeHasTwoNonStagedIdsRegardlessOfStagedId) { |
| 120 | auto table = test::ResourceTableBuilder() |
| 121 | .AddSimple("android:attr/foo", ResourceId(0x01050000)) |
| 122 | .AddSimple("android:attr/bar", ResourceId(0x01ff0006)) |
| 123 | .Add(NewResourceBuilder("android:attr/staged_baz") |
| 124 | .SetId(0x01ff0000) |
| 125 | .SetVisibility({.staged_api = true}) |
| 126 | .Build()) |
| 127 | .Build(); |
| 128 | IdAssigner assigner; |
| 129 | ASSERT_FALSE(assigner.Consume(context.get(), table.get())); |
| 130 | } |
| 131 | |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 132 | TEST_F(IdAssignerTests, AssignIdsWithIdMap) { |
| 133 | auto table = test::ResourceTableBuilder() |
| 134 | .AddSimple("android:attr/foo") |
| 135 | .AddSimple("android:attr/bar") |
| 136 | .Build(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 137 | std::unordered_map<ResourceName, ResourceId> id_map = { |
| 138 | {test::ParseNameOrDie("android:attr/foo"), ResourceId(0x01010002)}}; |
| 139 | IdAssigner assigner(&id_map); |
| 140 | ASSERT_TRUE(assigner.Consume(context.get(), table.get())); |
| 141 | ASSERT_TRUE(VerifyIds(table.get())); |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 142 | auto result = table->FindResource(test::ParseNameOrDie("android:attr/foo")); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 143 | ASSERT_TRUE(result); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 144 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 145 | const ResourceTable::SearchResult& search_result = result.value(); |
Ryan Mitchell | 1d008d1 | 2021-03-19 14:54:17 -0700 | [diff] [blame] | 146 | EXPECT_EQ(make_value<ResourceId>(0x01010002), search_result.entry->id); |
| 147 | } |
| 148 | |
| 149 | TEST_F(IdAssignerTests, UseAllEntryIds) { |
| 150 | ResourceTable table; |
| 151 | const size_t max_entry_id = std::numeric_limits<uint16_t>::max(); |
| 152 | for (size_t i = 0; i <= max_entry_id; i++) { |
| 153 | ASSERT_TRUE( |
| 154 | table.AddResource(NewResourceBuilder("android:attr/res" + std::to_string(i)).Build(), |
| 155 | context->GetDiagnostics())); |
| 156 | } |
| 157 | IdAssigner assigner; |
| 158 | ASSERT_TRUE(assigner.Consume(context.get(), &table)); |
| 159 | } |
| 160 | |
| 161 | TEST_F(IdAssignerTests, ExaustEntryIds) { |
| 162 | ResourceTable table; |
| 163 | const size_t max_entry_id = std::numeric_limits<uint16_t>::max() + 1u; |
| 164 | for (size_t i = 0; i <= max_entry_id; i++) { |
| 165 | ASSERT_TRUE( |
| 166 | table.AddResource(NewResourceBuilder("android:attr/res" + std::to_string(i)).Build(), |
| 167 | context->GetDiagnostics())); |
| 168 | } |
| 169 | IdAssigner assigner; |
| 170 | ASSERT_FALSE(assigner.Consume(context.get(), &table)); |
| 171 | } |
| 172 | |
| 173 | TEST_F(IdAssignerTests, ExaustEntryIdsLastIdIsPublic) { |
| 174 | ResourceTable table; |
| 175 | ASSERT_TRUE(table.AddResource(NewResourceBuilder("android:attr/res").SetId(0x0101ffff).Build(), |
| 176 | context->GetDiagnostics())); |
| 177 | const size_t max_entry_id = std::numeric_limits<uint16_t>::max(); |
| 178 | for (size_t i = 0; i <= max_entry_id; i++) { |
| 179 | ASSERT_TRUE( |
| 180 | table.AddResource(NewResourceBuilder("android:attr/res" + std::to_string(i)).Build(), |
| 181 | context->GetDiagnostics())); |
| 182 | } |
| 183 | IdAssigner assigner; |
| 184 | ASSERT_FALSE(assigner.Consume(context.get(), &table)); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | ::testing::AssertionResult VerifyIds(ResourceTable* table) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame^] | 188 | std::set<ResourceId> seen_ids; |
| 189 | for (auto& package : table->packages) { |
| 190 | for (auto& type : package->types) { |
| 191 | for (auto& entry : type->entries) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | if (!entry->id) { |
| 193 | return ::testing::AssertionFailure() |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame^] | 194 | << "resource " << ResourceNameRef(package->name, type->type, entry->name) |
| 195 | << " has no ID"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | } |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame^] | 197 | if (!seen_ids.insert(entry->id.value()).second) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 198 | return ::testing::AssertionFailure() |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame^] | 199 | << "resource " << ResourceNameRef(package->name, type->type, entry->name) |
| 200 | << " has a non-unique ID" << std::hex << entry->id.value() << std::dec; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame^] | 205 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 206 | return ::testing::AssertionSuccess() << "all IDs are unique and assigned"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | } // namespace aapt |