blob: ce45b7c1df048a90325073db51d679a02b682054 [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
Ryan Mitchell4382e442021-07-14 12:53:01 -070060 std::optional<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 Mitchell4382e442021-07-14 12:53:01 -070066 EXPECT_EQ(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 Mitchell4382e442021-07-14 12:53:01 -070071 EXPECT_EQ(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 Mitchell4382e442021-07-14 12:53:01 -070079 EXPECT_EQ(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 Mitchell4382e442021-07-14 12:53:01 -070085 EXPECT_EQ(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 Mitchell4382e442021-07-14 12:53:01 -070089 EXPECT_EQ(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) {
Brandon Liu4cb46242023-02-16 02:55:56 +0000120 auto table =
121 test::ResourceTableBuilder()
122 .AddSimple("android:attr/foo", ResourceId(0x01050000))
123 .AddSimple("android:attr/bar", ResourceId(0x01ff0006))
124 .Add(NewResourceBuilder("android:attr/staged_baz")
125 .SetId(0x01ff0000)
126 .SetVisibility({.staged_api = true, .level = Visibility::Level::kPublic})
127 .Build())
128 .Build();
129 IdAssigner assigner;
130 ASSERT_FALSE(assigner.Consume(context.get(), table.get()));
131}
132
133TEST_F(IdAssignerTests, FailWhenTypeHaveBothStagedAndNonStagedIds) {
134 auto table =
135 test::ResourceTableBuilder()
136 .AddSimple("android:attr/foo", ResourceId(0x01010000))
137 .Add(NewResourceBuilder("android:bool/staged_baz")
138 .SetId(0x01010001)
139 .SetVisibility({.staged_api = true, .level = Visibility::Level::kPublic})
140 .Build())
141 .Build();
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700142 IdAssigner assigner;
143 ASSERT_FALSE(assigner.Consume(context.get(), table.get()));
144}
145
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700146TEST_F(IdAssignerTests, AssignIdsWithIdMap) {
147 auto table = test::ResourceTableBuilder()
148 .AddSimple("android:attr/foo")
149 .AddSimple("android:attr/bar")
150 .Build();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 std::unordered_map<ResourceName, ResourceId> id_map = {
152 {test::ParseNameOrDie("android:attr/foo"), ResourceId(0x01010002)}};
153 IdAssigner assigner(&id_map);
154 ASSERT_TRUE(assigner.Consume(context.get(), table.get()));
155 ASSERT_TRUE(VerifyIds(table.get()));
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700156 auto result = table->FindResource(test::ParseNameOrDie("android:attr/foo"));
Adam Lesinskia45893a2017-05-30 15:19:02 -0700157 ASSERT_TRUE(result);
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700158
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 const ResourceTable::SearchResult& search_result = result.value();
Ryan Mitchell4382e442021-07-14 12:53:01 -0700160 EXPECT_EQ(0x01010002, search_result.entry->id);
Ryan Mitchell1d008d12021-03-19 14:54:17 -0700161}
162
163TEST_F(IdAssignerTests, UseAllEntryIds) {
164 ResourceTable table;
165 const size_t max_entry_id = std::numeric_limits<uint16_t>::max();
166 for (size_t i = 0; i <= max_entry_id; i++) {
167 ASSERT_TRUE(
168 table.AddResource(NewResourceBuilder("android:attr/res" + std::to_string(i)).Build(),
169 context->GetDiagnostics()));
170 }
171 IdAssigner assigner;
172 ASSERT_TRUE(assigner.Consume(context.get(), &table));
173}
174
175TEST_F(IdAssignerTests, ExaustEntryIds) {
176 ResourceTable table;
177 const size_t max_entry_id = std::numeric_limits<uint16_t>::max() + 1u;
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));
185}
186
187TEST_F(IdAssignerTests, ExaustEntryIdsLastIdIsPublic) {
188 ResourceTable table;
189 ASSERT_TRUE(table.AddResource(NewResourceBuilder("android:attr/res").SetId(0x0101ffff).Build(),
190 context->GetDiagnostics()));
191 const size_t max_entry_id = std::numeric_limits<uint16_t>::max();
192 for (size_t i = 0; i <= max_entry_id; i++) {
193 ASSERT_TRUE(
194 table.AddResource(NewResourceBuilder("android:attr/res" + std::to_string(i)).Build(),
195 context->GetDiagnostics()));
196 }
197 IdAssigner assigner;
198 ASSERT_FALSE(assigner.Consume(context.get(), &table));
Adam Lesinskibf0bd0f2016-06-01 15:31:50 -0700199}
200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201::testing::AssertionResult VerifyIds(ResourceTable* table) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700202 std::set<ResourceId> seen_ids;
203 for (auto& package : table->packages) {
204 for (auto& type : package->types) {
205 for (auto& entry : type->entries) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 if (!entry->id) {
207 return ::testing::AssertionFailure()
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000208 << "resource " << ResourceNameRef(package->name, type->named_type, entry->name)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700209 << " has no ID";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 }
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700211 if (!seen_ids.insert(entry->id.value()).second) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700212 return ::testing::AssertionFailure()
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000213 << "resource " << ResourceNameRef(package->name, type->named_type, entry->name)
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700214 << " has a non-unique ID" << std::hex << entry->id.value() << std::dec;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 }
216 }
217 }
218 }
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700219
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700220 return ::testing::AssertionSuccess() << "all IDs are unique and assigned";
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700221}
222
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223} // namespace aapt