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