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 "flatten/TableFlattener.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
| 20 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 21 | #include "ResourceUtils.h" |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 22 | #include "SdkConstants.h" |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 23 | #include "test/Test.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 24 | #include "unflatten/BinaryResourceParser.h" |
| 25 | #include "util/Util.h" |
| 26 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 27 | using namespace android; |
| 28 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 29 | using ::testing::IsNull; |
| 30 | using ::testing::NotNull; |
| 31 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | namespace aapt { |
| 33 | |
| 34 | class TableFlattenerTest : public ::testing::Test { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | public: |
| 36 | void SetUp() override { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | context_ = test::ContextBuilder() |
| 38 | .SetCompilationPackage("com.app.test") |
| 39 | .SetPackageId(0x7f) |
| 40 | .Build(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 43 | ::testing::AssertionResult Flatten(IAaptContext* context, const TableFlattenerOptions& options, |
| 44 | ResourceTable* table, std::string* out_content) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 45 | BigBuffer buffer(1024); |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 46 | TableFlattener flattener(options, &buffer); |
| 47 | if (!flattener.Consume(context, table)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | return ::testing::AssertionFailure() << "failed to flatten ResourceTable"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 49 | } |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 50 | *out_content = buffer.to_string(); |
| 51 | return ::testing::AssertionSuccess(); |
| 52 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 53 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 54 | ::testing::AssertionResult Flatten(IAaptContext* context, const TableFlattenerOptions& options, |
| 55 | ResourceTable* table, ResTable* out_table) { |
| 56 | std::string content; |
| 57 | auto result = Flatten(context, options, table, &content); |
| 58 | if (!result) { |
| 59 | return result; |
| 60 | } |
| 61 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 62 | if (out_table->add(content.data(), content.size(), 1, true) != NO_ERROR) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | return ::testing::AssertionFailure() << "flattened ResTable is corrupt"; |
| 64 | } |
| 65 | return ::testing::AssertionSuccess(); |
| 66 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 67 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 68 | ::testing::AssertionResult Flatten(IAaptContext* context, const TableFlattenerOptions& options, |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 69 | ResourceTable* table, ResourceTable* out_table) { |
| 70 | std::string content; |
| 71 | auto result = Flatten(context, options, table, &content); |
| 72 | if (!result) { |
| 73 | return result; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 76 | BinaryResourceParser parser(context, out_table, {}, content.data(), content.size()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | if (!parser.Parse()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | return ::testing::AssertionFailure() << "flattened ResTable is corrupt"; |
| 79 | } |
| 80 | return ::testing::AssertionSuccess(); |
| 81 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 82 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | ::testing::AssertionResult Exists(ResTable* table, |
| 84 | const StringPiece& expected_name, |
| 85 | const ResourceId& expected_id, |
| 86 | const ConfigDescription& expected_config, |
| 87 | const uint8_t expected_data_type, |
| 88 | const uint32_t expected_data, |
| 89 | const uint32_t expected_spec_flags) { |
| 90 | const ResourceName expected_res_name = test::ParseNameOrDie(expected_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 92 | table->setParameters(&expected_config); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | |
| 94 | ResTable_config config; |
| 95 | Res_value val; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | uint32_t spec_flags; |
| 97 | if (table->getResource(expected_id.id, &val, false, 0, &spec_flags, |
| 98 | &config) < 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 99 | return ::testing::AssertionFailure() << "could not find resource with"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | if (expected_data_type != val.dataType) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 103 | return ::testing::AssertionFailure() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | << "expected data type " << std::hex << (int)expected_data_type |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | << " but got data type " << (int)val.dataType << std::dec |
| 106 | << " instead"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 109 | if (expected_data != val.data) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | return ::testing::AssertionFailure() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 111 | << "expected data " << std::hex << expected_data |
| 112 | << " but got data " << val.data << std::dec << " instead"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | if (expected_spec_flags != spec_flags) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 116 | return ::testing::AssertionFailure() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 117 | << "expected specFlags " << std::hex << expected_spec_flags |
| 118 | << " but got specFlags " << spec_flags << std::dec << " instead"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | ResTable::resource_name actual_name; |
| 122 | if (!table->getResourceName(expected_id.id, false, &actual_name)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | return ::testing::AssertionFailure() << "failed to find resource name"; |
| 124 | } |
| 125 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 126 | Maybe<ResourceName> resName = ResourceUtils::ToResourceName(actual_name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 127 | if (!resName) { |
| 128 | return ::testing::AssertionFailure() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 129 | << "expected name '" << expected_res_name << "' but got '" |
| 130 | << StringPiece16(actual_name.package, actual_name.packageLen) |
| 131 | << ":" << StringPiece16(actual_name.type, actual_name.typeLen) |
| 132 | << "/" << StringPiece16(actual_name.name, actual_name.nameLen) |
| 133 | << "'"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 136 | if (expected_config != config) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | return ::testing::AssertionFailure() << "expected config '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | << expected_config << "' but got '" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | << ConfigDescription(config) << "'"; |
| 140 | } |
| 141 | return ::testing::AssertionSuccess(); |
| 142 | } |
| 143 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 144 | protected: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 145 | std::unique_ptr<IAaptContext> context_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | TEST_F(TableFlattenerTest, FlattenFullyLinkedTable) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | std::unique_ptr<ResourceTable> table = |
| 150 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 151 | .SetPackageId("com.app.test", 0x7f) |
| 152 | .AddSimple("com.app.test:id/one", ResourceId(0x7f020000)) |
| 153 | .AddSimple("com.app.test:id/two", ResourceId(0x7f020001)) |
| 154 | .AddValue("com.app.test:id/three", ResourceId(0x7f020002), |
| 155 | test::BuildReference("com.app.test:id/one", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 156 | ResourceId(0x7f020000))) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 157 | .AddValue("com.app.test:integer/one", ResourceId(0x7f030000), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | util::make_unique<BinaryPrimitive>( |
| 159 | uint8_t(Res_value::TYPE_INT_DEC), 1u)) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 160 | .AddValue("com.app.test:integer/one", test::ParseConfigOrDie("v1"), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | ResourceId(0x7f030000), |
| 162 | util::make_unique<BinaryPrimitive>( |
| 163 | uint8_t(Res_value::TYPE_INT_DEC), 2u)) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 164 | .AddString("com.app.test:string/test", ResourceId(0x7f040000), "foo") |
| 165 | .AddString("com.app.test:layout/bar", ResourceId(0x7f050000), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | "res/layout/bar.xml") |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 167 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 168 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | ResTable res_table; |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 170 | ASSERT_TRUE(Flatten(context_.get(), {}, table.get(), &res_table)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 171 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 172 | EXPECT_TRUE(Exists(&res_table, "com.app.test:id/one", ResourceId(0x7f020000), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 173 | {}, Res_value::TYPE_INT_BOOLEAN, 0u, 0u)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 174 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 175 | EXPECT_TRUE(Exists(&res_table, "com.app.test:id/two", ResourceId(0x7f020001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | {}, Res_value::TYPE_INT_BOOLEAN, 0u, 0u)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 177 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 178 | EXPECT_TRUE(Exists(&res_table, "com.app.test:id/three", |
| 179 | ResourceId(0x7f020002), {}, Res_value::TYPE_REFERENCE, |
| 180 | 0x7f020000u, 0u)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 181 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | EXPECT_TRUE(Exists(&res_table, "com.app.test:integer/one", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 183 | ResourceId(0x7f030000), {}, Res_value::TYPE_INT_DEC, 1u, |
| 184 | ResTable_config::CONFIG_VERSION)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 185 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 186 | EXPECT_TRUE(Exists(&res_table, "com.app.test:integer/one", |
| 187 | ResourceId(0x7f030000), test::ParseConfigOrDie("v1"), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 188 | Res_value::TYPE_INT_DEC, 2u, |
| 189 | ResTable_config::CONFIG_VERSION)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 190 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 191 | std::u16string foo_str = u"foo"; |
| 192 | ssize_t idx = res_table.getTableStringBlock(0)->indexOfString(foo_str.data(), |
| 193 | foo_str.size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 194 | ASSERT_GE(idx, 0); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 195 | EXPECT_TRUE(Exists(&res_table, "com.app.test:string/test", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | ResourceId(0x7f040000), {}, Res_value::TYPE_STRING, |
| 197 | (uint32_t)idx, 0u)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 198 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 199 | std::u16string bar_path = u"res/layout/bar.xml"; |
| 200 | idx = res_table.getTableStringBlock(0)->indexOfString(bar_path.data(), |
| 201 | bar_path.size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 202 | ASSERT_GE(idx, 0); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 203 | EXPECT_TRUE(Exists(&res_table, "com.app.test:layout/bar", |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 204 | ResourceId(0x7f050000), {}, Res_value::TYPE_STRING, |
| 205 | (uint32_t)idx, 0u)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | TEST_F(TableFlattenerTest, FlattenEntriesWithGapsInIds) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | std::unique_ptr<ResourceTable> table = |
| 210 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 211 | .SetPackageId("com.app.test", 0x7f) |
| 212 | .AddSimple("com.app.test:id/one", ResourceId(0x7f020001)) |
| 213 | .AddSimple("com.app.test:id/three", ResourceId(0x7f020003)) |
| 214 | .Build(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 215 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 216 | ResTable res_table; |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 217 | ASSERT_TRUE(Flatten(context_.get(), {}, table.get(), &res_table)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 218 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 219 | EXPECT_TRUE(Exists(&res_table, "com.app.test:id/one", ResourceId(0x7f020001), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 220 | {}, Res_value::TYPE_INT_BOOLEAN, 0u, 0u)); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 221 | EXPECT_TRUE(Exists(&res_table, "com.app.test:id/three", |
| 222 | ResourceId(0x7f020003), {}, Res_value::TYPE_INT_BOOLEAN, |
| 223 | 0u, 0u)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 226 | TEST_F(TableFlattenerTest, FlattenMinMaxAttributes) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 227 | Attribute attr(false); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 228 | attr.type_mask = android::ResTable_map::TYPE_INTEGER; |
| 229 | attr.min_int = 10; |
| 230 | attr.max_int = 23; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 231 | std::unique_ptr<ResourceTable> table = |
| 232 | test::ResourceTableBuilder() |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 233 | .SetPackageId("android", 0x01) |
| 234 | .AddValue("android:attr/foo", ResourceId(0x01010000), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 235 | util::make_unique<Attribute>(attr)) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 236 | .Build(); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 237 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 238 | ResourceTable result; |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 239 | ASSERT_TRUE(Flatten(context_.get(), {}, table.get(), &result)); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 240 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 241 | Attribute* actual_attr = test::GetValue<Attribute>(&result, "android:attr/foo"); |
| 242 | ASSERT_THAT(actual_attr, NotNull()); |
| 243 | EXPECT_EQ(attr.IsWeak(), actual_attr->IsWeak()); |
| 244 | EXPECT_EQ(attr.type_mask, actual_attr->type_mask); |
| 245 | EXPECT_EQ(attr.min_int, actual_attr->min_int); |
| 246 | EXPECT_EQ(attr.max_int, actual_attr->max_int); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 249 | static std::unique_ptr<ResourceTable> BuildTableWithSparseEntries( |
| 250 | IAaptContext* context, const ConfigDescription& sparse_config, float load) { |
| 251 | std::unique_ptr<ResourceTable> table = |
| 252 | test::ResourceTableBuilder() |
| 253 | .SetPackageId(context->GetCompilationPackage(), context->GetPackageId()) |
| 254 | .Build(); |
| 255 | |
| 256 | // Add regular entries. |
| 257 | int stride = static_cast<int>(1.0f / load); |
| 258 | for (int i = 0; i < 100; i++) { |
| 259 | const ResourceName name = test::ParseNameOrDie( |
| 260 | base::StringPrintf("%s:string/foo_%d", context->GetCompilationPackage().data(), i)); |
| 261 | const ResourceId resid(context->GetPackageId(), 0x02, static_cast<uint16_t>(i)); |
| 262 | const auto value = |
| 263 | util::make_unique<BinaryPrimitive>(Res_value::TYPE_INT_DEC, static_cast<uint32_t>(i)); |
| 264 | CHECK(table->AddResource(name, resid, ConfigDescription::DefaultConfig(), "", |
| 265 | std::unique_ptr<Value>(value->Clone(nullptr)), |
| 266 | context->GetDiagnostics())); |
| 267 | |
| 268 | // Every few entries, write out a sparse_config value. This will give us the desired load. |
| 269 | if (i % stride == 0) { |
| 270 | CHECK(table->AddResource(name, resid, sparse_config, "", |
| 271 | std::unique_ptr<Value>(value->Clone(nullptr)), |
| 272 | context->GetDiagnostics())); |
| 273 | } |
| 274 | } |
| 275 | return table; |
| 276 | } |
| 277 | |
| 278 | TEST_F(TableFlattenerTest, FlattenSparseEntryWithMinSdkO) { |
| 279 | std::unique_ptr<IAaptContext> context = test::ContextBuilder() |
| 280 | .SetCompilationPackage("android") |
| 281 | .SetPackageId(0x01) |
| 282 | .SetMinSdkVersion(SDK_O) |
| 283 | .Build(); |
| 284 | |
| 285 | const ConfigDescription sparse_config = test::ParseConfigOrDie("en-rGB"); |
| 286 | auto table_in = BuildTableWithSparseEntries(context.get(), sparse_config, 0.25f); |
| 287 | |
| 288 | TableFlattenerOptions options; |
| 289 | options.use_sparse_entries = true; |
| 290 | |
| 291 | std::string no_sparse_contents; |
| 292 | ASSERT_TRUE(Flatten(context.get(), {}, table_in.get(), &no_sparse_contents)); |
| 293 | |
| 294 | std::string sparse_contents; |
| 295 | ASSERT_TRUE(Flatten(context.get(), options, table_in.get(), &sparse_contents)); |
| 296 | |
| 297 | EXPECT_GT(no_sparse_contents.size(), sparse_contents.size()); |
| 298 | |
| 299 | // Attempt to parse the sparse contents. |
| 300 | |
| 301 | ResourceTable sparse_table; |
| 302 | BinaryResourceParser parser(context.get(), &sparse_table, Source("test.arsc"), |
| 303 | sparse_contents.data(), sparse_contents.size()); |
| 304 | ASSERT_TRUE(parser.Parse()); |
| 305 | |
| 306 | auto value = test::GetValueForConfig<BinaryPrimitive>(&sparse_table, "android:string/foo_0", |
| 307 | sparse_config); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 308 | ASSERT_THAT(value, NotNull()); |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 309 | EXPECT_EQ(0u, value->value.data); |
| 310 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 311 | ASSERT_THAT(test::GetValueForConfig<BinaryPrimitive>(&sparse_table, "android:string/foo_1", sparse_config), IsNull()); |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 312 | |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 313 | value = test::GetValueForConfig<BinaryPrimitive>(&sparse_table, "android:string/foo_4", sparse_config); |
| 314 | ASSERT_THAT(value, NotNull()); |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 315 | EXPECT_EQ(4u, value->value.data); |
| 316 | } |
| 317 | |
| 318 | TEST_F(TableFlattenerTest, FlattenSparseEntryWithConfigSdkVersionO) { |
| 319 | std::unique_ptr<IAaptContext> context = test::ContextBuilder() |
| 320 | .SetCompilationPackage("android") |
| 321 | .SetPackageId(0x01) |
| 322 | .SetMinSdkVersion(SDK_LOLLIPOP) |
| 323 | .Build(); |
| 324 | |
| 325 | const ConfigDescription sparse_config = test::ParseConfigOrDie("en-rGB-v26"); |
| 326 | auto table_in = BuildTableWithSparseEntries(context.get(), sparse_config, 0.25f); |
| 327 | |
| 328 | TableFlattenerOptions options; |
| 329 | options.use_sparse_entries = true; |
| 330 | |
| 331 | std::string no_sparse_contents; |
| 332 | ASSERT_TRUE(Flatten(context.get(), {}, table_in.get(), &no_sparse_contents)); |
| 333 | |
| 334 | std::string sparse_contents; |
| 335 | ASSERT_TRUE(Flatten(context.get(), options, table_in.get(), &sparse_contents)); |
| 336 | |
| 337 | EXPECT_GT(no_sparse_contents.size(), sparse_contents.size()); |
| 338 | } |
| 339 | |
| 340 | TEST_F(TableFlattenerTest, DoNotUseSparseEntryForDenseConfig) { |
| 341 | std::unique_ptr<IAaptContext> context = test::ContextBuilder() |
| 342 | .SetCompilationPackage("android") |
| 343 | .SetPackageId(0x01) |
| 344 | .SetMinSdkVersion(SDK_O) |
| 345 | .Build(); |
| 346 | |
| 347 | const ConfigDescription sparse_config = test::ParseConfigOrDie("en-rGB"); |
| 348 | auto table_in = BuildTableWithSparseEntries(context.get(), sparse_config, 0.80f); |
| 349 | |
| 350 | TableFlattenerOptions options; |
| 351 | options.use_sparse_entries = true; |
| 352 | |
| 353 | std::string no_sparse_contents; |
| 354 | ASSERT_TRUE(Flatten(context.get(), {}, table_in.get(), &no_sparse_contents)); |
| 355 | |
| 356 | std::string sparse_contents; |
| 357 | ASSERT_TRUE(Flatten(context.get(), options, table_in.get(), &sparse_contents)); |
| 358 | |
| 359 | EXPECT_EQ(no_sparse_contents.size(), sparse_contents.size()); |
| 360 | } |
| 361 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 362 | TEST_F(TableFlattenerTest, FlattenSharedLibrary) { |
| 363 | std::unique_ptr<IAaptContext> context = |
| 364 | test::ContextBuilder().SetCompilationPackage("lib").SetPackageId(0x00).Build(); |
| 365 | std::unique_ptr<ResourceTable> table = |
| 366 | test::ResourceTableBuilder() |
| 367 | .SetPackageId("lib", 0x00) |
| 368 | .AddValue("lib:id/foo", ResourceId(0x00010000), util::make_unique<Id>()) |
| 369 | .Build(); |
| 370 | ResourceTable result; |
| 371 | ASSERT_TRUE(Flatten(context.get(), {}, table.get(), &result)); |
| 372 | |
| 373 | Maybe<ResourceTable::SearchResult> search_result = |
| 374 | result.FindResource(test::ParseNameOrDie("lib:id/foo")); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 375 | ASSERT_TRUE(search_result); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 376 | EXPECT_EQ(0x00u, search_result.value().package->id.value()); |
| 377 | |
| 378 | auto iter = result.included_packages_.find(0x00); |
| 379 | ASSERT_NE(result.included_packages_.end(), iter); |
| 380 | EXPECT_EQ("lib", iter->second); |
| 381 | } |
| 382 | |
| 383 | TEST_F(TableFlattenerTest, FlattenTableReferencingSharedLibraries) { |
| 384 | std::unique_ptr<IAaptContext> context = |
| 385 | test::ContextBuilder().SetCompilationPackage("app").SetPackageId(0x7f).Build(); |
| 386 | std::unique_ptr<ResourceTable> table = |
| 387 | test::ResourceTableBuilder() |
| 388 | .SetPackageId("app", 0x7f) |
| 389 | .AddValue("app:id/foo", ResourceId(0x7f010000), |
| 390 | test::BuildReference("lib_one:id/foo", ResourceId(0x02010000))) |
| 391 | .AddValue("app:id/bar", ResourceId(0x7f010001), |
| 392 | test::BuildReference("lib_two:id/bar", ResourceId(0x03010000))) |
| 393 | .Build(); |
| 394 | table->included_packages_[0x02] = "lib_one"; |
| 395 | table->included_packages_[0x03] = "lib_two"; |
| 396 | |
| 397 | ResTable result; |
| 398 | ASSERT_TRUE(Flatten(context.get(), {}, table.get(), &result)); |
| 399 | |
| 400 | const DynamicRefTable* dynamic_ref_table = result.getDynamicRefTableForCookie(1); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 401 | ASSERT_THAT(dynamic_ref_table, NotNull()); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 402 | |
Adam Lesinski | 4ca5697 | 2017-04-26 21:49:53 -0700 | [diff] [blame] | 403 | const KeyedVector<String16, uint8_t>& entries = dynamic_ref_table->entries(); |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 404 | |
| 405 | ssize_t idx = entries.indexOfKey(android::String16("lib_one")); |
| 406 | ASSERT_GE(idx, 0); |
| 407 | EXPECT_EQ(0x02u, entries.valueAt(idx)); |
| 408 | |
| 409 | idx = entries.indexOfKey(android::String16("lib_two")); |
| 410 | ASSERT_GE(idx, 0); |
| 411 | EXPECT_EQ(0x03u, entries.valueAt(idx)); |
| 412 | } |
| 413 | |
Adam Lesinski | 4ca5697 | 2017-04-26 21:49:53 -0700 | [diff] [blame] | 414 | TEST_F(TableFlattenerTest, PackageWithNonStandardIdHasDynamicRefTable) { |
| 415 | std::unique_ptr<IAaptContext> context = |
| 416 | test::ContextBuilder().SetCompilationPackage("app").SetPackageId(0x80).Build(); |
| 417 | std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder() |
| 418 | .SetPackageId("app", 0x80) |
| 419 | .AddSimple("app:id/foo", ResourceId(0x80010000)) |
| 420 | .Build(); |
| 421 | |
| 422 | ResTable result; |
| 423 | ASSERT_TRUE(Flatten(context.get(), {}, table.get(), &result)); |
| 424 | |
| 425 | const DynamicRefTable* dynamic_ref_table = result.getDynamicRefTableForCookie(1); |
Adam Lesinski | a45893a | 2017-05-30 15:19:02 -0700 | [diff] [blame] | 426 | ASSERT_THAT(dynamic_ref_table, NotNull()); |
Adam Lesinski | 4ca5697 | 2017-04-26 21:49:53 -0700 | [diff] [blame] | 427 | |
| 428 | const KeyedVector<String16, uint8_t>& entries = dynamic_ref_table->entries(); |
| 429 | ssize_t idx = entries.indexOfKey(android::String16("app")); |
| 430 | ASSERT_GE(idx, 0); |
| 431 | EXPECT_EQ(0x80u, entries.valueAt(idx)); |
| 432 | } |
| 433 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 434 | TEST_F(TableFlattenerTest, LongPackageNameIsTruncated) { |
| 435 | std::string kPackageName(256, 'F'); |
| 436 | |
| 437 | std::unique_ptr<IAaptContext> context = |
| 438 | test::ContextBuilder().SetCompilationPackage(kPackageName).SetPackageId(0x7f).Build(); |
| 439 | std::unique_ptr<ResourceTable> table = |
| 440 | test::ResourceTableBuilder() |
| 441 | .SetPackageId(kPackageName, 0x7f) |
| 442 | .AddSimple(kPackageName + ":id/foo", ResourceId(0x7f010000)) |
| 443 | .Build(); |
| 444 | |
| 445 | ResTable result; |
| 446 | ASSERT_TRUE(Flatten(context.get(), {}, table.get(), &result)); |
| 447 | |
| 448 | ASSERT_EQ(1u, result.getBasePackageCount()); |
| 449 | EXPECT_EQ(127u, result.getBasePackageName(0).size()); |
| 450 | } |
| 451 | |
| 452 | TEST_F(TableFlattenerTest, LongSharedLibraryPackageNameIsIllegal) { |
| 453 | std::string kPackageName(256, 'F'); |
| 454 | |
| 455 | std::unique_ptr<IAaptContext> context = test::ContextBuilder() |
| 456 | .SetCompilationPackage(kPackageName) |
| 457 | .SetPackageId(0x7f) |
| 458 | .SetPackageType(PackageType::kSharedLib) |
| 459 | .Build(); |
| 460 | std::unique_ptr<ResourceTable> table = |
| 461 | test::ResourceTableBuilder() |
| 462 | .SetPackageId(kPackageName, 0x7f) |
| 463 | .AddSimple(kPackageName + ":id/foo", ResourceId(0x7f010000)) |
| 464 | .Build(); |
| 465 | |
| 466 | ResTable result; |
| 467 | ASSERT_FALSE(Flatten(context.get(), {}, table.get(), &result)); |
| 468 | } |
| 469 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 470 | } // namespace aapt |