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 | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 18 | #include "test/Test.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 19 | |
| 20 | namespace aapt { |
| 21 | |
| 22 | ::testing::AssertionResult verifyIds(ResourceTable* table); |
| 23 | |
| 24 | TEST(IdAssignerTest, AssignIds) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 25 | std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder() |
| 26 | .addSimple("android:attr/foo") |
| 27 | .addSimple("android:attr/bar") |
| 28 | .addSimple("android:id/foo") |
| 29 | .setPackageId("android", 0x01) |
| 30 | .build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 31 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 32 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().build(); |
| 33 | IdAssigner assigner; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | ASSERT_TRUE(assigner.consume(context.get(), table.get())); |
| 36 | ASSERT_TRUE(verifyIds(table.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | TEST(IdAssignerTest, AssignIdsWithReservedIds) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | std::unique_ptr<ResourceTable> table = |
| 41 | test::ResourceTableBuilder() |
| 42 | .addSimple("android:id/foo", ResourceId(0x01010000)) |
| 43 | .addSimple("android:dimen/two") |
| 44 | .addSimple("android:integer/three") |
| 45 | .addSimple("android:string/five") |
| 46 | .addSimple("android:attr/fun", ResourceId(0x01040000)) |
| 47 | .addSimple("android:attr/foo", ResourceId(0x01040006)) |
| 48 | .addSimple("android:attr/bar") |
| 49 | .addSimple("android:attr/baz") |
| 50 | .addSimple("app:id/biz") |
| 51 | .setPackageId("android", 0x01) |
| 52 | .setPackageId("app", 0x7f) |
| 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 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().build(); |
| 56 | IdAssigner assigner; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 57 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | ASSERT_TRUE(assigner.consume(context.get(), table.get())); |
| 59 | ASSERT_TRUE(verifyIds(table.get())); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 60 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | Maybe<ResourceTable::SearchResult> maybeResult; |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 62 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | // Expect to fill in the gaps between 0x0101XXXX and 0x0104XXXX. |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | maybeResult = table->findResource(test::parseNameOrDie("android:dimen/two")); |
| 66 | AAPT_ASSERT_TRUE(maybeResult); |
| 67 | EXPECT_EQ(make_value<uint8_t>(2), maybeResult.value().type->id); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 68 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | maybeResult = |
| 70 | table->findResource(test::parseNameOrDie("android:integer/three")); |
| 71 | AAPT_ASSERT_TRUE(maybeResult); |
| 72 | EXPECT_EQ(make_value<uint8_t>(3), maybeResult.value().type->id); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | // Expect to bypass the reserved 0x0104XXXX IDs and use the next 0x0105XXXX |
| 75 | // IDs. |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 76 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | maybeResult = |
| 78 | table->findResource(test::parseNameOrDie("android:string/five")); |
| 79 | AAPT_ASSERT_TRUE(maybeResult); |
| 80 | EXPECT_EQ(make_value<uint8_t>(5), maybeResult.value().type->id); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | // Expect to fill in the gaps between 0x01040000 and 0x01040006. |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | maybeResult = table->findResource(test::parseNameOrDie("android:attr/bar")); |
| 85 | AAPT_ASSERT_TRUE(maybeResult); |
| 86 | EXPECT_EQ(make_value<uint16_t>(1), maybeResult.value().entry->id); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 87 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | maybeResult = table->findResource(test::parseNameOrDie("android:attr/baz")); |
| 89 | AAPT_ASSERT_TRUE(maybeResult); |
| 90 | EXPECT_EQ(make_value<uint16_t>(2), maybeResult.value().entry->id); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | TEST(IdAssignerTest, FailWhenNonUniqueIdsAssigned) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 94 | std::unique_ptr<ResourceTable> table = |
| 95 | test::ResourceTableBuilder() |
| 96 | .addSimple("android:attr/foo", ResourceId(0x01040006)) |
| 97 | .addSimple("android:attr/bar", ResourceId(0x01040006)) |
| 98 | .setPackageId("android", 0x01) |
| 99 | .setPackageId("app", 0x7f) |
| 100 | .build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 101 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().build(); |
| 103 | IdAssigner assigner; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 104 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | ASSERT_FALSE(assigner.consume(context.get(), table.get())); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 108 | TEST(IdAssignerTest, AssignIdsWithIdMap) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder() |
| 110 | .addSimple("android:attr/foo") |
| 111 | .addSimple("android:attr/bar") |
| 112 | .setPackageId("android", 0x01) |
| 113 | .build(); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 114 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | std::unique_ptr<IAaptContext> context = test::ContextBuilder().build(); |
| 116 | std::unordered_map<ResourceName, ResourceId> idMap = { |
| 117 | {test::parseNameOrDie("android:attr/foo"), ResourceId(0x01010002)}}; |
| 118 | IdAssigner assigner(&idMap); |
| 119 | ASSERT_TRUE(assigner.consume(context.get(), table.get())); |
| 120 | ASSERT_TRUE(verifyIds(table.get())); |
| 121 | Maybe<ResourceTable::SearchResult> result = |
| 122 | table->findResource(test::parseNameOrDie("android:attr/foo")); |
| 123 | AAPT_ASSERT_TRUE(result); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 124 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 125 | const ResourceTable::SearchResult& searchResult = result.value(); |
| 126 | EXPECT_EQ(make_value<uint8_t>(0x01), searchResult.package->id); |
| 127 | EXPECT_EQ(make_value<uint8_t>(0x01), searchResult.type->id); |
| 128 | EXPECT_EQ(make_value<uint16_t>(0x0002), searchResult.entry->id); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | ::testing::AssertionResult verifyIds(ResourceTable* table) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | std::set<uint8_t> packageIds; |
| 133 | for (auto& package : table->packages) { |
| 134 | if (!package->id) { |
| 135 | return ::testing::AssertionFailure() << "package " << package->name |
| 136 | << " has no ID"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | if (!packageIds.insert(package->id.value()).second) { |
| 140 | return ::testing::AssertionFailure() |
| 141 | << "package " << package->name << " has non-unique ID " << std::hex |
| 142 | << (int)package->id.value() << std::dec; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 143 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | for (auto& package : table->packages) { |
| 147 | std::set<uint8_t> typeIds; |
| 148 | for (auto& type : package->types) { |
| 149 | if (!type->id) { |
| 150 | return ::testing::AssertionFailure() << "type " << type->type |
| 151 | << " of package " << package->name |
| 152 | << " has no ID"; |
| 153 | } |
| 154 | |
| 155 | if (!typeIds.insert(type->id.value()).second) { |
| 156 | return ::testing::AssertionFailure() |
| 157 | << "type " << type->type << " of package " << package->name |
| 158 | << " has non-unique ID " << std::hex << (int)type->id.value() |
| 159 | << std::dec; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | for (auto& type : package->types) { |
| 164 | std::set<uint16_t> entryIds; |
| 165 | for (auto& entry : type->entries) { |
| 166 | if (!entry->id) { |
| 167 | return ::testing::AssertionFailure() |
| 168 | << "entry " << entry->name << " of type " << type->type |
| 169 | << " of package " << package->name << " has no ID"; |
| 170 | } |
| 171 | |
| 172 | if (!entryIds.insert(entry->id.value()).second) { |
| 173 | return ::testing::AssertionFailure() |
| 174 | << "entry " << entry->name << " of type " << type->type |
| 175 | << " of package " << package->name << " has non-unique ID " |
| 176 | << std::hex << (int)entry->id.value() << std::dec; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | return ::testing::AssertionSuccess() << "all IDs are unique and assigned"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 184 | } // namespace aapt |