TableFlattener_test.cpp revision fb6312fe93a8544e6a95d1c619c8cea3940cbe1a
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#include "test/Test.h"
19#include "unflatten/BinaryResourceParser.h"
20#include "util/Util.h"
21
22using namespace android;
23
24namespace aapt {
25
26class TableFlattenerTest : public ::testing::Test {
27public:
28    void SetUp() override {
29        mContext = test::ContextBuilder()
30                .setCompilationPackage(u"com.app.test")
31                .setPackageId(0x7f)
32                .build();
33    }
34
35    ::testing::AssertionResult flatten(ResourceTable* table, ResTable* outTable) {
36        BigBuffer buffer(1024);
37        TableFlattener flattener(&buffer);
38        if (!flattener.consume(mContext.get(), table)) {
39            return ::testing::AssertionFailure() << "failed to flatten ResourceTable";
40        }
41
42        std::unique_ptr<uint8_t[]> data = util::copy(buffer);
43        if (outTable->add(data.get(), buffer.size(), -1, true) != NO_ERROR) {
44            return ::testing::AssertionFailure() << "flattened ResTable is corrupt";
45        }
46        return ::testing::AssertionSuccess();
47    }
48
49    ::testing::AssertionResult flatten(ResourceTable* table, ResourceTable* outTable) {
50        BigBuffer buffer(1024);
51        TableFlattener flattener(&buffer);
52        if (!flattener.consume(mContext.get(), table)) {
53            return ::testing::AssertionFailure() << "failed to flatten ResourceTable";
54        }
55
56        std::unique_ptr<uint8_t[]> data = util::copy(buffer);
57        BinaryResourceParser parser(mContext.get(), outTable, {}, data.get(), buffer.size());
58        if (!parser.parse()) {
59            return ::testing::AssertionFailure() << "flattened ResTable is corrupt";
60        }
61        return ::testing::AssertionSuccess();
62    }
63
64    ::testing::AssertionResult exists(ResTable* table,
65                                      const StringPiece16& expectedName,
66                                      const ResourceId expectedId,
67                                      const ConfigDescription& expectedConfig,
68                                      const uint8_t expectedDataType, const uint32_t expectedData,
69                                      const uint32_t expectedSpecFlags) {
70        const ResourceName expectedResName = test::parseNameOrDie(expectedName);
71
72        table->setParameters(&expectedConfig);
73
74        ResTable_config config;
75        Res_value val;
76        uint32_t specFlags;
77        if (table->getResource(expectedId.id, &val, false, 0, &specFlags, &config) < 0) {
78            return ::testing::AssertionFailure() << "could not find resource with";
79        }
80
81        if (expectedDataType != val.dataType) {
82            return ::testing::AssertionFailure()
83                    << "expected data type "
84                    << std::hex << (int) expectedDataType << " but got data type "
85                    << (int) val.dataType << std::dec << " instead";
86        }
87
88        if (expectedData != val.data) {
89            return ::testing::AssertionFailure()
90                    << "expected data "
91                    << std::hex << expectedData << " but got data "
92                    << val.data << std::dec << " instead";
93        }
94
95        if (expectedSpecFlags != specFlags) {
96            return ::testing::AssertionFailure()
97                    << "expected specFlags "
98                    << std::hex << expectedSpecFlags << " but got specFlags "
99                    << specFlags << std::dec << " instead";
100        }
101
102        ResTable::resource_name actualName;
103        if (!table->getResourceName(expectedId.id, false, &actualName)) {
104            return ::testing::AssertionFailure() << "failed to find resource name";
105        }
106
107        StringPiece16 package16(actualName.package, actualName.packageLen);
108        if (package16 != expectedResName.package) {
109            return ::testing::AssertionFailure()
110                    << "expected package '" << expectedResName.package << "' but got '"
111                    << package16 << "'";
112        }
113
114        StringPiece16 type16(actualName.type, actualName.typeLen);
115        if (type16 != toString(expectedResName.type)) {
116            return ::testing::AssertionFailure()
117                    << "expected type '" << expectedResName.type
118                    << "' but got '" << type16 << "'";
119        }
120
121        StringPiece16 name16(actualName.name, actualName.nameLen);
122        if (name16 != expectedResName.entry) {
123            return ::testing::AssertionFailure()
124                    << "expected name '" << expectedResName.entry
125                    << "' but got '" << name16 << "'";
126        }
127
128        if (expectedConfig != config) {
129            return ::testing::AssertionFailure()
130                    << "expected config '" << expectedConfig << "' but got '"
131                    << ConfigDescription(config) << "'";
132        }
133        return ::testing::AssertionSuccess();
134    }
135
136private:
137    std::unique_ptr<IAaptContext> mContext;
138};
139
140TEST_F(TableFlattenerTest, FlattenFullyLinkedTable) {
141    std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
142            .setPackageId(u"com.app.test", 0x7f)
143            .addSimple(u"@com.app.test:id/one", ResourceId(0x7f020000))
144            .addSimple(u"@com.app.test:id/two", ResourceId(0x7f020001))
145            .addValue(u"@com.app.test:id/three", ResourceId(0x7f020002),
146                      test::buildReference(u"@com.app.test:id/one", ResourceId(0x7f020000)))
147            .addValue(u"@com.app.test:integer/one", ResourceId(0x7f030000),
148                      util::make_unique<BinaryPrimitive>(uint8_t(Res_value::TYPE_INT_DEC), 1u))
149            .addValue(u"@com.app.test:integer/one", test::parseConfigOrDie("v1"),
150                      ResourceId(0x7f030000),
151                      util::make_unique<BinaryPrimitive>(uint8_t(Res_value::TYPE_INT_DEC), 2u))
152            .addString(u"@com.app.test:string/test", ResourceId(0x7f040000), u"foo")
153            .addString(u"@com.app.test:layout/bar", ResourceId(0x7f050000), u"res/layout/bar.xml")
154            .build();
155
156    ResTable resTable;
157    ASSERT_TRUE(flatten(table.get(), &resTable));
158
159    EXPECT_TRUE(exists(&resTable, u"@com.app.test:id/one", ResourceId(0x7f020000), {},
160                       Res_value::TYPE_INT_BOOLEAN, 0u, 0u));
161
162    EXPECT_TRUE(exists(&resTable, u"@com.app.test:id/two", ResourceId(0x7f020001), {},
163                       Res_value::TYPE_INT_BOOLEAN, 0u, 0u));
164
165    EXPECT_TRUE(exists(&resTable, u"@com.app.test:id/three", ResourceId(0x7f020002), {},
166                       Res_value::TYPE_REFERENCE, 0x7f020000u, 0u));
167
168    EXPECT_TRUE(exists(&resTable, u"@com.app.test:integer/one", ResourceId(0x7f030000),
169                       {}, Res_value::TYPE_INT_DEC, 1u,
170                       ResTable_config::CONFIG_VERSION));
171
172    EXPECT_TRUE(exists(&resTable, u"@com.app.test:integer/one", ResourceId(0x7f030000),
173                       test::parseConfigOrDie("v1"), Res_value::TYPE_INT_DEC, 2u,
174                       ResTable_config::CONFIG_VERSION));
175
176    StringPiece16 fooStr = u"foo";
177    ssize_t idx = resTable.getTableStringBlock(0)->indexOfString(fooStr.data(), fooStr.size());
178    ASSERT_GE(idx, 0);
179    EXPECT_TRUE(exists(&resTable, u"@com.app.test:string/test", ResourceId(0x7f040000),
180                       {}, Res_value::TYPE_STRING, (uint32_t) idx, 0u));
181
182    StringPiece16 barPath = u"res/layout/bar.xml";
183    idx = resTable.getTableStringBlock(0)->indexOfString(barPath.data(), barPath.size());
184    ASSERT_GE(idx, 0);
185    EXPECT_TRUE(exists(&resTable, u"@com.app.test:layout/bar", ResourceId(0x7f050000), {},
186                       Res_value::TYPE_STRING, (uint32_t) idx, 0u));
187}
188
189TEST_F(TableFlattenerTest, FlattenEntriesWithGapsInIds) {
190    std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
191            .setPackageId(u"com.app.test", 0x7f)
192            .addSimple(u"@com.app.test:id/one", ResourceId(0x7f020001))
193            .addSimple(u"@com.app.test:id/three", ResourceId(0x7f020003))
194            .build();
195
196    ResTable resTable;
197    ASSERT_TRUE(flatten(table.get(), &resTable));
198
199    EXPECT_TRUE(exists(&resTable, u"@com.app.test:id/one", ResourceId(0x7f020001), {},
200                       Res_value::TYPE_INT_BOOLEAN, 0u, 0u));
201    EXPECT_TRUE(exists(&resTable, u"@com.app.test:id/three", ResourceId(0x7f020003), {},
202                           Res_value::TYPE_INT_BOOLEAN, 0u, 0u));
203}
204
205TEST_F(TableFlattenerTest, FlattenMinMaxAttributes) {
206    Attribute attr(false);
207    attr.typeMask = android::ResTable_map::TYPE_INTEGER;
208    attr.minInt = 10;
209    attr.maxInt = 23;
210    std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
211            .setPackageId(u"android", 0x01)
212            .addValue(u"@android:attr/foo", ResourceId(0x01010000),
213                      util::make_unique<Attribute>(attr))
214            .build();
215
216    ResourceTable result;
217    ASSERT_TRUE(flatten(table.get(), &result));
218
219    Attribute* actualAttr = test::getValue<Attribute>(&result, u"@android:attr/foo");
220    ASSERT_NE(nullptr, actualAttr);
221    EXPECT_EQ(attr.isWeak(), actualAttr->isWeak());
222    EXPECT_EQ(attr.typeMask, actualAttr->typeMask);
223    EXPECT_EQ(attr.minInt, actualAttr->minInt);
224    EXPECT_EQ(attr.maxInt, actualAttr->maxInt);
225}
226
227} // namespace aapt
228