TableFlattener_test.cpp revision ceb9b2f80f853059233cdd29057f39a5960a74ae
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
27using namespace android;
28
29namespace aapt {
30
31class TableFlattenerTest : public ::testing::Test {
32 public:
33  void SetUp() override {
34    context_ = test::ContextBuilder()
35                   .SetCompilationPackage("com.app.test")
36                   .SetPackageId(0x7f)
37                   .Build();
38  }
39
40  ::testing::AssertionResult Flatten(IAaptContext* context, const TableFlattenerOptions& options,
41                                     ResourceTable* table, std::string* out_content) {
42    BigBuffer buffer(1024);
43    TableFlattener flattener(options, &buffer);
44    if (!flattener.Consume(context, table)) {
45      return ::testing::AssertionFailure() << "failed to flatten ResourceTable";
46    }
47    *out_content = buffer.to_string();
48    return ::testing::AssertionSuccess();
49  }
50
51  ::testing::AssertionResult Flatten(IAaptContext* context, const TableFlattenerOptions& options,
52                                     ResourceTable* table, ResTable* out_table) {
53    std::string content;
54    auto result = Flatten(context, options, table, &content);
55    if (!result) {
56      return result;
57    }
58
59    if (out_table->add(content.data(), content.size(), 1, true) != NO_ERROR) {
60      return ::testing::AssertionFailure() << "flattened ResTable is corrupt";
61    }
62    return ::testing::AssertionSuccess();
63  }
64
65  ::testing::AssertionResult Flatten(IAaptContext* context, const TableFlattenerOptions& options,
66                                     ResourceTable* table, ResourceTable* out_table) {
67    std::string content;
68    auto result = Flatten(context, options, table, &content);
69    if (!result) {
70      return result;
71    }
72
73    BinaryResourceParser parser(context, out_table, {}, content.data(), content.size());
74    if (!parser.Parse()) {
75      return ::testing::AssertionFailure() << "flattened ResTable is corrupt";
76    }
77    return ::testing::AssertionSuccess();
78  }
79
80  ::testing::AssertionResult Exists(ResTable* table,
81                                    const StringPiece& expected_name,
82                                    const ResourceId& expected_id,
83                                    const ConfigDescription& expected_config,
84                                    const uint8_t expected_data_type,
85                                    const uint32_t expected_data,
86                                    const uint32_t expected_spec_flags) {
87    const ResourceName expected_res_name = test::ParseNameOrDie(expected_name);
88
89    table->setParameters(&expected_config);
90
91    ResTable_config config;
92    Res_value val;
93    uint32_t spec_flags;
94    if (table->getResource(expected_id.id, &val, false, 0, &spec_flags,
95                           &config) < 0) {
96      return ::testing::AssertionFailure() << "could not find resource with";
97    }
98
99    if (expected_data_type != val.dataType) {
100      return ::testing::AssertionFailure()
101             << "expected data type " << std::hex << (int)expected_data_type
102             << " but got data type " << (int)val.dataType << std::dec
103             << " instead";
104    }
105
106    if (expected_data != val.data) {
107      return ::testing::AssertionFailure()
108             << "expected data " << std::hex << expected_data
109             << " but got data " << val.data << std::dec << " instead";
110    }
111
112    if (expected_spec_flags != spec_flags) {
113      return ::testing::AssertionFailure()
114             << "expected specFlags " << std::hex << expected_spec_flags
115             << " but got specFlags " << spec_flags << std::dec << " instead";
116    }
117
118    ResTable::resource_name actual_name;
119    if (!table->getResourceName(expected_id.id, false, &actual_name)) {
120      return ::testing::AssertionFailure() << "failed to find resource name";
121    }
122
123    Maybe<ResourceName> resName = ResourceUtils::ToResourceName(actual_name);
124    if (!resName) {
125      return ::testing::AssertionFailure()
126             << "expected name '" << expected_res_name << "' but got '"
127             << StringPiece16(actual_name.package, actual_name.packageLen)
128             << ":" << StringPiece16(actual_name.type, actual_name.typeLen)
129             << "/" << StringPiece16(actual_name.name, actual_name.nameLen)
130             << "'";
131    }
132
133    if (expected_config != config) {
134      return ::testing::AssertionFailure() << "expected config '"
135                                           << expected_config << "' but got '"
136                                           << ConfigDescription(config) << "'";
137    }
138    return ::testing::AssertionSuccess();
139  }
140
141 protected:
142  std::unique_ptr<IAaptContext> context_;
143};
144
145TEST_F(TableFlattenerTest, FlattenFullyLinkedTable) {
146  std::unique_ptr<ResourceTable> table =
147      test::ResourceTableBuilder()
148          .SetPackageId("com.app.test", 0x7f)
149          .AddSimple("com.app.test:id/one", ResourceId(0x7f020000))
150          .AddSimple("com.app.test:id/two", ResourceId(0x7f020001))
151          .AddValue("com.app.test:id/three", ResourceId(0x7f020002),
152                    test::BuildReference("com.app.test:id/one",
153                                         ResourceId(0x7f020000)))
154          .AddValue("com.app.test:integer/one", ResourceId(0x7f030000),
155                    util::make_unique<BinaryPrimitive>(
156                        uint8_t(Res_value::TYPE_INT_DEC), 1u))
157          .AddValue("com.app.test:integer/one", test::ParseConfigOrDie("v1"),
158                    ResourceId(0x7f030000),
159                    util::make_unique<BinaryPrimitive>(
160                        uint8_t(Res_value::TYPE_INT_DEC), 2u))
161          .AddString("com.app.test:string/test", ResourceId(0x7f040000), "foo")
162          .AddString("com.app.test:layout/bar", ResourceId(0x7f050000),
163                     "res/layout/bar.xml")
164          .Build();
165
166  ResTable res_table;
167  ASSERT_TRUE(Flatten(context_.get(), {}, table.get(), &res_table));
168
169  EXPECT_TRUE(Exists(&res_table, "com.app.test:id/one", ResourceId(0x7f020000),
170                     {}, Res_value::TYPE_INT_BOOLEAN, 0u, 0u));
171
172  EXPECT_TRUE(Exists(&res_table, "com.app.test:id/two", ResourceId(0x7f020001),
173                     {}, Res_value::TYPE_INT_BOOLEAN, 0u, 0u));
174
175  EXPECT_TRUE(Exists(&res_table, "com.app.test:id/three",
176                     ResourceId(0x7f020002), {}, Res_value::TYPE_REFERENCE,
177                     0x7f020000u, 0u));
178
179  EXPECT_TRUE(Exists(&res_table, "com.app.test:integer/one",
180                     ResourceId(0x7f030000), {}, Res_value::TYPE_INT_DEC, 1u,
181                     ResTable_config::CONFIG_VERSION));
182
183  EXPECT_TRUE(Exists(&res_table, "com.app.test:integer/one",
184                     ResourceId(0x7f030000), test::ParseConfigOrDie("v1"),
185                     Res_value::TYPE_INT_DEC, 2u,
186                     ResTable_config::CONFIG_VERSION));
187
188  std::u16string foo_str = u"foo";
189  ssize_t idx = res_table.getTableStringBlock(0)->indexOfString(foo_str.data(),
190                                                                foo_str.size());
191  ASSERT_GE(idx, 0);
192  EXPECT_TRUE(Exists(&res_table, "com.app.test:string/test",
193                     ResourceId(0x7f040000), {}, Res_value::TYPE_STRING,
194                     (uint32_t)idx, 0u));
195
196  std::u16string bar_path = u"res/layout/bar.xml";
197  idx = res_table.getTableStringBlock(0)->indexOfString(bar_path.data(),
198                                                        bar_path.size());
199  ASSERT_GE(idx, 0);
200  EXPECT_TRUE(Exists(&res_table, "com.app.test:layout/bar",
201                     ResourceId(0x7f050000), {}, Res_value::TYPE_STRING,
202                     (uint32_t)idx, 0u));
203}
204
205TEST_F(TableFlattenerTest, FlattenEntriesWithGapsInIds) {
206  std::unique_ptr<ResourceTable> table =
207      test::ResourceTableBuilder()
208          .SetPackageId("com.app.test", 0x7f)
209          .AddSimple("com.app.test:id/one", ResourceId(0x7f020001))
210          .AddSimple("com.app.test:id/three", ResourceId(0x7f020003))
211          .Build();
212
213  ResTable res_table;
214  ASSERT_TRUE(Flatten(context_.get(), {}, table.get(), &res_table));
215
216  EXPECT_TRUE(Exists(&res_table, "com.app.test:id/one", ResourceId(0x7f020001),
217                     {}, Res_value::TYPE_INT_BOOLEAN, 0u, 0u));
218  EXPECT_TRUE(Exists(&res_table, "com.app.test:id/three",
219                     ResourceId(0x7f020003), {}, Res_value::TYPE_INT_BOOLEAN,
220                     0u, 0u));
221}
222
223TEST_F(TableFlattenerTest, FlattenMinMaxAttributes) {
224  Attribute attr(false);
225  attr.type_mask = android::ResTable_map::TYPE_INTEGER;
226  attr.min_int = 10;
227  attr.max_int = 23;
228  std::unique_ptr<ResourceTable> table =
229      test::ResourceTableBuilder()
230          .SetPackageId("android", 0x01)
231          .AddValue("android:attr/foo", ResourceId(0x01010000),
232                    util::make_unique<Attribute>(attr))
233          .Build();
234
235  ResourceTable result;
236  ASSERT_TRUE(Flatten(context_.get(), {}, table.get(), &result));
237
238  Attribute* actualAttr =
239      test::GetValue<Attribute>(&result, "android:attr/foo");
240  ASSERT_NE(nullptr, actualAttr);
241  EXPECT_EQ(attr.IsWeak(), actualAttr->IsWeak());
242  EXPECT_EQ(attr.type_mask, actualAttr->type_mask);
243  EXPECT_EQ(attr.min_int, actualAttr->min_int);
244  EXPECT_EQ(attr.max_int, actualAttr->max_int);
245}
246
247static std::unique_ptr<ResourceTable> BuildTableWithSparseEntries(
248    IAaptContext* context, const ConfigDescription& sparse_config, float load) {
249  std::unique_ptr<ResourceTable> table =
250      test::ResourceTableBuilder()
251          .SetPackageId(context->GetCompilationPackage(), context->GetPackageId())
252          .Build();
253
254  // Add regular entries.
255  int stride = static_cast<int>(1.0f / load);
256  for (int i = 0; i < 100; i++) {
257    const ResourceName name = test::ParseNameOrDie(
258        base::StringPrintf("%s:string/foo_%d", context->GetCompilationPackage().data(), i));
259    const ResourceId resid(context->GetPackageId(), 0x02, static_cast<uint16_t>(i));
260    const auto value =
261        util::make_unique<BinaryPrimitive>(Res_value::TYPE_INT_DEC, static_cast<uint32_t>(i));
262    CHECK(table->AddResource(name, resid, ConfigDescription::DefaultConfig(), "",
263                             std::unique_ptr<Value>(value->Clone(nullptr)),
264                             context->GetDiagnostics()));
265
266    // Every few entries, write out a sparse_config value. This will give us the desired load.
267    if (i % stride == 0) {
268      CHECK(table->AddResource(name, resid, sparse_config, "",
269                               std::unique_ptr<Value>(value->Clone(nullptr)),
270                               context->GetDiagnostics()));
271    }
272  }
273  return table;
274}
275
276TEST_F(TableFlattenerTest, FlattenSparseEntryWithMinSdkO) {
277  std::unique_ptr<IAaptContext> context = test::ContextBuilder()
278                                              .SetCompilationPackage("android")
279                                              .SetPackageId(0x01)
280                                              .SetMinSdkVersion(SDK_O)
281                                              .Build();
282
283  const ConfigDescription sparse_config = test::ParseConfigOrDie("en-rGB");
284  auto table_in = BuildTableWithSparseEntries(context.get(), sparse_config, 0.25f);
285
286  TableFlattenerOptions options;
287  options.use_sparse_entries = true;
288
289  std::string no_sparse_contents;
290  ASSERT_TRUE(Flatten(context.get(), {}, table_in.get(), &no_sparse_contents));
291
292  std::string sparse_contents;
293  ASSERT_TRUE(Flatten(context.get(), options, table_in.get(), &sparse_contents));
294
295  EXPECT_GT(no_sparse_contents.size(), sparse_contents.size());
296
297  // Attempt to parse the sparse contents.
298
299  ResourceTable sparse_table;
300  BinaryResourceParser parser(context.get(), &sparse_table, Source("test.arsc"),
301                              sparse_contents.data(), sparse_contents.size());
302  ASSERT_TRUE(parser.Parse());
303
304  auto value = test::GetValueForConfig<BinaryPrimitive>(&sparse_table, "android:string/foo_0",
305                                                        sparse_config);
306  ASSERT_NE(nullptr, value);
307  EXPECT_EQ(0u, value->value.data);
308
309  ASSERT_EQ(nullptr, test::GetValueForConfig<BinaryPrimitive>(&sparse_table, "android:string/foo_1",
310                                                              sparse_config));
311
312  value = test::GetValueForConfig<BinaryPrimitive>(&sparse_table, "android:string/foo_4",
313                                                   sparse_config);
314  ASSERT_NE(nullptr, value);
315  EXPECT_EQ(4u, value->value.data);
316}
317
318TEST_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
340TEST_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
362TEST_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  AAPT_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
383TEST_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_NE(nullptr, dynamic_ref_table);
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}  // namespace aapt
415