blob: 663776645990572f28a36ea465c10bbc0409e538 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "compile/IdAssigner.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070019#include "test/Test.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020
21namespace aapt {
22
Ryan Mitchell1d008d12021-03-19 14:54:17 -070023struct 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 Lesinskice5e56e2016-10-21 17:56:45 -070030::testing::AssertionResult VerifyIds(ResourceTable* table);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070031
Ryan Mitchell1d008d12021-03-19 14:54:17 -070032TEST_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 Lesinskicacb28f2016-10-19 12:18:14 -070038 IdAssigner assigner;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 ASSERT_TRUE(assigner.Consume(context.get(), table.get()));
41 ASSERT_TRUE(VerifyIds(table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042}
43
Ryan Mitchell1d008d12021-03-19 14:54:17 -070044TEST_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 Lesinski1ab598f2015-08-14 14:26:04 -070055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 IdAssigner assigner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 ASSERT_TRUE(assigner.Consume(context.get(), table.get()));
58 ASSERT_TRUE(VerifyIds(table.get()));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070059
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 Maybe<ResourceTable::SearchResult> maybe_result;
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070061
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 // Expect to fill in the gaps between 0x0101XXXX and 0x0104XXXX.
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070063
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 maybe_result = table->FindResource(test::ParseNameOrDie("android:dimen/two"));
Adam Lesinskia45893a2017-05-30 15:19:02 -070065 ASSERT_TRUE(maybe_result);
Ryan Mitchell1d008d12021-03-19 14:54:17 -070066 EXPECT_EQ(make_value<ResourceId>(0x01020000), maybe_result.value().entry->id);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070067
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 maybe_result =
69 table->FindResource(test::ParseNameOrDie("android:integer/three"));
Adam Lesinskia45893a2017-05-30 15:19:02 -070070 ASSERT_TRUE(maybe_result);
Ryan Mitchell1d008d12021-03-19 14:54:17 -070071 EXPECT_EQ(make_value<ResourceId>(0x01030000), maybe_result.value().entry->id);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070072
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 // Expect to bypass the reserved 0x0104XXXX IDs and use the next 0x0105XXXX
74 // IDs.
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070075
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 maybe_result =
77 table->FindResource(test::ParseNameOrDie("android:string/five"));
Adam Lesinskia45893a2017-05-30 15:19:02 -070078 ASSERT_TRUE(maybe_result);
Ryan Mitchell1d008d12021-03-19 14:54:17 -070079 EXPECT_EQ(make_value<ResourceId>(0x01050000), maybe_result.value().entry->id);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070080
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 // Expect to fill in the gaps between 0x01040000 and 0x01040006.
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070082
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 maybe_result = table->FindResource(test::ParseNameOrDie("android:attr/bar"));
Adam Lesinskia45893a2017-05-30 15:19:02 -070084 ASSERT_TRUE(maybe_result);
Ryan Mitchell1d008d12021-03-19 14:54:17 -070085 EXPECT_EQ(make_value<ResourceId>(0x01040001), maybe_result.value().entry->id);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -070086
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 maybe_result = table->FindResource(test::ParseNameOrDie("android:attr/baz"));
Adam Lesinskia45893a2017-05-30 15:19:02 -070088 ASSERT_TRUE(maybe_result);
Ryan Mitchell1d008d12021-03-19 14:54:17 -070089 EXPECT_EQ(make_value<ResourceId>(0x01040002), maybe_result.value().entry->id);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070090}
91
Ryan Mitchell1d008d12021-03-19 14:54:17 -070092TEST_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 Lesinskicacb28f2016-10-19 12:18:14 -070097 IdAssigner assigner;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 ASSERT_FALSE(assigner.Consume(context.get(), table.get()));
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099}
100
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700101TEST_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
110TEST_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
119TEST_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 Mitchell1d008d12021-03-19 14:54:17 -0700132TEST_F(IdAssignerTests, AssignIdsWithIdMap) {
133 auto table = test::ResourceTableBuilder()
134 .AddSimple("android:attr/foo")
135 .AddSimple("android:attr/bar")
136 .Build();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 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 Mitchell1d008d12021-03-19 14:54:17 -0700142 auto result = table->FindResource(test::ParseNameOrDie("android:attr/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700143 ASSERT_TRUE(result);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 const ResourceTable::SearchResult& search_result = result.value();
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700146 EXPECT_EQ(make_value<ResourceId>(0x01010002), search_result.entry->id);
147}
148
149TEST_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
161TEST_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
173TEST_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 Lesinskibf0bd0f2016-06-01 15:31:50 -0700185}
186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187::testing::AssertionResult VerifyIds(ResourceTable* table) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700188 std::set<ResourceId> seen_ids;
189 for (auto& package : table->packages) {
190 for (auto& type : package->types) {
191 for (auto& entry : type->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 if (!entry->id) {
193 return ::testing::AssertionFailure()
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700194 << "resource " << ResourceNameRef(package->name, type->type, entry->name)
195 << " has no ID";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 }
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700197 if (!seen_ids.insert(entry->id.value()).second) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 return ::testing::AssertionFailure()
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700199 << "resource " << ResourceNameRef(package->name, type->type, entry->name)
200 << " has a non-unique ID" << std::hex << entry->id.value() << std::dec;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 }
202 }
203 }
204 }
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700205
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 return ::testing::AssertionSuccess() << "all IDs are unique and assigned";
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700207}
208
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209} // namespace aapt