Home | History | Annotate | Download | only in flatten
      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"
     18 
     19 #include "android-base/stringprintf.h"
     20 
     21 #include "ResourceUtils.h"
     22 #include "SdkConstants.h"
     23 #include "test/Test.h"
     24 #include "unflatten/BinaryResourceParser.h"
     25 #include "util/Util.h"
     26 
     27 using namespace android;
     28 
     29 using ::testing::IsNull;
     30 using ::testing::NotNull;
     31 
     32 namespace aapt {
     33 
     34 class TableFlattenerTest : public ::testing::Test {
     35  public:
     36   void SetUp() override {
     37     context_ = test::ContextBuilder()
     38                    .SetCompilationPackage("com.app.test")
     39                    .SetPackageId(0x7f)
     40                    .Build();
     41   }
     42 
     43   ::testing::AssertionResult Flatten(IAaptContext* context, const TableFlattenerOptions& options,
     44                                      ResourceTable* table, std::string* out_content) {
     45     BigBuffer buffer(1024);
     46     TableFlattener flattener(options, &buffer);
     47     if (!flattener.Consume(context, table)) {
     48       return ::testing::AssertionFailure() << "failed to flatten ResourceTable";
     49     }
     50     *out_content = buffer.to_string();
     51     return ::testing::AssertionSuccess();
     52   }
     53 
     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 
     62     if (out_table->add(content.data(), content.size(), 1, true) != NO_ERROR) {
     63       return ::testing::AssertionFailure() << "flattened ResTable is corrupt";
     64     }
     65     return ::testing::AssertionSuccess();
     66   }
     67 
     68   ::testing::AssertionResult Flatten(IAaptContext* context, const TableFlattenerOptions& options,
     69                                      ResourceTable* table, ResourceTable* out_table) {
     70     std::string content;
     71     auto result = Flatten(context, options, table, &content);
     72     if (!result) {
     73       return result;
     74     }
     75 
     76     BinaryResourceParser parser(context, out_table, {}, content.data(), content.size());
     77     if (!parser.Parse()) {
     78       return ::testing::AssertionFailure() << "flattened ResTable is corrupt";
     79     }
     80     return ::testing::AssertionSuccess();
     81   }
     82 
     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);
     91 
     92     table->setParameters(&expected_config);
     93 
     94     ResTable_config config;
     95     Res_value val;
     96     uint32_t spec_flags;
     97     if (table->getResource(expected_id.id, &val, false, 0, &spec_flags,
     98                            &config) < 0) {
     99       return ::testing::AssertionFailure() << "could not find resource with";
    100     }
    101 
    102     if (expected_data_type != val.dataType) {
    103       return ::testing::AssertionFailure()
    104              << "expected data type " << std::hex << (int)expected_data_type
    105              << " but got data type " << (int)val.dataType << std::dec
    106              << " instead";
    107     }
    108 
    109     if (expected_data != val.data) {
    110       return ::testing::AssertionFailure()
    111              << "expected data " << std::hex << expected_data
    112              << " but got data " << val.data << std::dec << " instead";
    113     }
    114 
    115     if (expected_spec_flags != spec_flags) {
    116       return ::testing::AssertionFailure()
    117              << "expected specFlags " << std::hex << expected_spec_flags
    118              << " but got specFlags " << spec_flags << std::dec << " instead";
    119     }
    120 
    121     ResTable::resource_name actual_name;
    122     if (!table->getResourceName(expected_id.id, false, &actual_name)) {
    123       return ::testing::AssertionFailure() << "failed to find resource name";
    124     }
    125 
    126     Maybe<ResourceName> resName = ResourceUtils::ToResourceName(actual_name);
    127     if (!resName) {
    128       return ::testing::AssertionFailure()
    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              << "'";
    134     }
    135 
    136     if (expected_config != config) {
    137       return ::testing::AssertionFailure() << "expected config '"
    138                                            << expected_config << "' but got '"
    139                                            << ConfigDescription(config) << "'";
    140     }
    141     return ::testing::AssertionSuccess();
    142   }
    143 
    144  protected:
    145   std::unique_ptr<IAaptContext> context_;
    146 };
    147 
    148 TEST_F(TableFlattenerTest, FlattenFullyLinkedTable) {
    149   std::unique_ptr<ResourceTable> table =
    150       test::ResourceTableBuilder()
    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",
    156                                          ResourceId(0x7f020000)))
    157           .AddValue("com.app.test:integer/one", ResourceId(0x7f030000),
    158                     util::make_unique<BinaryPrimitive>(
    159                         uint8_t(Res_value::TYPE_INT_DEC), 1u))
    160           .AddValue("com.app.test:integer/one", test::ParseConfigOrDie("v1"),
    161                     ResourceId(0x7f030000),
    162                     util::make_unique<BinaryPrimitive>(
    163                         uint8_t(Res_value::TYPE_INT_DEC), 2u))
    164           .AddString("com.app.test:string/test", ResourceId(0x7f040000), "foo")
    165           .AddString("com.app.test:layout/bar", ResourceId(0x7f050000),
    166                      "res/layout/bar.xml")
    167           .Build();
    168 
    169   ResTable res_table;
    170   ASSERT_TRUE(Flatten(context_.get(), {}, table.get(), &res_table));
    171 
    172   EXPECT_TRUE(Exists(&res_table, "com.app.test:id/one", ResourceId(0x7f020000),
    173                      {}, Res_value::TYPE_INT_BOOLEAN, 0u, 0u));
    174 
    175   EXPECT_TRUE(Exists(&res_table, "com.app.test:id/two", ResourceId(0x7f020001),
    176                      {}, Res_value::TYPE_INT_BOOLEAN, 0u, 0u));
    177 
    178   EXPECT_TRUE(Exists(&res_table, "com.app.test:id/three",
    179                      ResourceId(0x7f020002), {}, Res_value::TYPE_REFERENCE,
    180                      0x7f020000u, 0u));
    181 
    182   EXPECT_TRUE(Exists(&res_table, "com.app.test:integer/one",
    183                      ResourceId(0x7f030000), {}, Res_value::TYPE_INT_DEC, 1u,
    184                      ResTable_config::CONFIG_VERSION));
    185 
    186   EXPECT_TRUE(Exists(&res_table, "com.app.test:integer/one",
    187                      ResourceId(0x7f030000), test::ParseConfigOrDie("v1"),
    188                      Res_value::TYPE_INT_DEC, 2u,
    189                      ResTable_config::CONFIG_VERSION));
    190 
    191   std::u16string foo_str = u"foo";
    192   ssize_t idx = res_table.getTableStringBlock(0)->indexOfString(foo_str.data(),
    193                                                                 foo_str.size());
    194   ASSERT_GE(idx, 0);
    195   EXPECT_TRUE(Exists(&res_table, "com.app.test:string/test",
    196                      ResourceId(0x7f040000), {}, Res_value::TYPE_STRING,
    197                      (uint32_t)idx, 0u));
    198 
    199   std::u16string bar_path = u"res/layout/bar.xml";
    200   idx = res_table.getTableStringBlock(0)->indexOfString(bar_path.data(),
    201                                                         bar_path.size());
    202   ASSERT_GE(idx, 0);
    203   EXPECT_TRUE(Exists(&res_table, "com.app.test:layout/bar",
    204                      ResourceId(0x7f050000), {}, Res_value::TYPE_STRING,
    205                      (uint32_t)idx, 0u));
    206 }
    207 
    208 TEST_F(TableFlattenerTest, FlattenEntriesWithGapsInIds) {
    209   std::unique_ptr<ResourceTable> table =
    210       test::ResourceTableBuilder()
    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();
    215 
    216   ResTable res_table;
    217   ASSERT_TRUE(Flatten(context_.get(), {}, table.get(), &res_table));
    218 
    219   EXPECT_TRUE(Exists(&res_table, "com.app.test:id/one", ResourceId(0x7f020001),
    220                      {}, Res_value::TYPE_INT_BOOLEAN, 0u, 0u));
    221   EXPECT_TRUE(Exists(&res_table, "com.app.test:id/three",
    222                      ResourceId(0x7f020003), {}, Res_value::TYPE_INT_BOOLEAN,
    223                      0u, 0u));
    224 }
    225 
    226 TEST_F(TableFlattenerTest, FlattenMinMaxAttributes) {
    227   Attribute attr(false);
    228   attr.type_mask = android::ResTable_map::TYPE_INTEGER;
    229   attr.min_int = 10;
    230   attr.max_int = 23;
    231   std::unique_ptr<ResourceTable> table =
    232       test::ResourceTableBuilder()
    233           .SetPackageId("android", 0x01)
    234           .AddValue("android:attr/foo", ResourceId(0x01010000),
    235                     util::make_unique<Attribute>(attr))
    236           .Build();
    237 
    238   ResourceTable result;
    239   ASSERT_TRUE(Flatten(context_.get(), {}, table.get(), &result));
    240 
    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);
    247 }
    248 
    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);
    308   ASSERT_THAT(value, NotNull());
    309   EXPECT_EQ(0u, value->value.data);
    310 
    311   ASSERT_THAT(test::GetValueForConfig<BinaryPrimitive>(&sparse_table, "android:string/foo_1", sparse_config), IsNull());
    312 
    313   value = test::GetValueForConfig<BinaryPrimitive>(&sparse_table, "android:string/foo_4", sparse_config);
    314   ASSERT_THAT(value, NotNull());
    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 
    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"));
    375   ASSERT_TRUE(search_result);
    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);
    401   ASSERT_THAT(dynamic_ref_table, NotNull());
    402 
    403   const KeyedVector<String16, uint8_t>& entries = dynamic_ref_table->entries();
    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 
    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);
    426   ASSERT_THAT(dynamic_ref_table, NotNull());
    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 
    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 
    470 }  // namespace aapt
    471