File: TableFlattener_test.cpp

package info (click to toggle)
android-platform-frameworks-base 1%3A7.0.0%2Br33-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 137,116 kB
  • sloc: java: 704,370; cpp: 206,670; xml: 185,778; python: 2,626; ansic: 486; sh: 291; makefile: 48; sed: 19
file content (231 lines) | stat: -rw-r--r-- 9,828 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "flatten/TableFlattener.h"
#include "test/Builders.h"
#include "test/Context.h"
#include "unflatten/BinaryResourceParser.h"
#include "util/Util.h"


#include <gtest/gtest.h>

using namespace android;

namespace aapt {

class TableFlattenerTest : public ::testing::Test {
public:
    void SetUp() override {
        mContext = test::ContextBuilder()
                .setCompilationPackage(u"com.app.test")
                .setPackageId(0x7f)
                .build();
    }

    ::testing::AssertionResult flatten(ResourceTable* table, ResTable* outTable) {
        BigBuffer buffer(1024);
        TableFlattener flattener(&buffer);
        if (!flattener.consume(mContext.get(), table)) {
            return ::testing::AssertionFailure() << "failed to flatten ResourceTable";
        }

        std::unique_ptr<uint8_t[]> data = util::copy(buffer);
        if (outTable->add(data.get(), buffer.size(), -1, true) != NO_ERROR) {
            return ::testing::AssertionFailure() << "flattened ResTable is corrupt";
        }
        return ::testing::AssertionSuccess();
    }

    ::testing::AssertionResult flatten(ResourceTable* table, ResourceTable* outTable) {
        BigBuffer buffer(1024);
        TableFlattener flattener(&buffer);
        if (!flattener.consume(mContext.get(), table)) {
            return ::testing::AssertionFailure() << "failed to flatten ResourceTable";
        }

        std::unique_ptr<uint8_t[]> data = util::copy(buffer);
        BinaryResourceParser parser(mContext.get(), outTable, {}, data.get(), buffer.size());
        if (!parser.parse()) {
            return ::testing::AssertionFailure() << "flattened ResTable is corrupt";
        }
        return ::testing::AssertionSuccess();
    }

    ::testing::AssertionResult exists(ResTable* table,
                                      const StringPiece16& expectedName,
                                      const ResourceId expectedId,
                                      const ConfigDescription& expectedConfig,
                                      const uint8_t expectedDataType, const uint32_t expectedData,
                                      const uint32_t expectedSpecFlags) {
        const ResourceName expectedResName = test::parseNameOrDie(expectedName);

        table->setParameters(&expectedConfig);

        ResTable_config config;
        Res_value val;
        uint32_t specFlags;
        if (table->getResource(expectedId.id, &val, false, 0, &specFlags, &config) < 0) {
            return ::testing::AssertionFailure() << "could not find resource with";
        }

        if (expectedDataType != val.dataType) {
            return ::testing::AssertionFailure()
                    << "expected data type "
                    << std::hex << (int) expectedDataType << " but got data type "
                    << (int) val.dataType << std::dec << " instead";
        }

        if (expectedData != val.data) {
            return ::testing::AssertionFailure()
                    << "expected data "
                    << std::hex << expectedData << " but got data "
                    << val.data << std::dec << " instead";
        }

        if (expectedSpecFlags != specFlags) {
            return ::testing::AssertionFailure()
                    << "expected specFlags "
                    << std::hex << expectedSpecFlags << " but got specFlags "
                    << specFlags << std::dec << " instead";
        }

        ResTable::resource_name actualName;
        if (!table->getResourceName(expectedId.id, false, &actualName)) {
            return ::testing::AssertionFailure() << "failed to find resource name";
        }

        StringPiece16 package16(actualName.package, actualName.packageLen);
        if (package16 != expectedResName.package) {
            return ::testing::AssertionFailure()
                    << "expected package '" << expectedResName.package << "' but got '"
                    << package16 << "'";
        }

        StringPiece16 type16(actualName.type, actualName.typeLen);
        if (type16 != toString(expectedResName.type)) {
            return ::testing::AssertionFailure()
                    << "expected type '" << expectedResName.type
                    << "' but got '" << type16 << "'";
        }

        StringPiece16 name16(actualName.name, actualName.nameLen);
        if (name16 != expectedResName.entry) {
            return ::testing::AssertionFailure()
                    << "expected name '" << expectedResName.entry
                    << "' but got '" << name16 << "'";
        }

        if (expectedConfig != config) {
            return ::testing::AssertionFailure()
                    << "expected config '" << expectedConfig << "' but got '"
                    << ConfigDescription(config) << "'";
        }
        return ::testing::AssertionSuccess();
    }

private:
    std::unique_ptr<IAaptContext> mContext;
};

TEST_F(TableFlattenerTest, FlattenFullyLinkedTable) {
    std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
            .setPackageId(u"com.app.test", 0x7f)
            .addSimple(u"@com.app.test:id/one", ResourceId(0x7f020000))
            .addSimple(u"@com.app.test:id/two", ResourceId(0x7f020001))
            .addValue(u"@com.app.test:id/three", ResourceId(0x7f020002),
                      test::buildReference(u"@com.app.test:id/one", ResourceId(0x7f020000)))
            .addValue(u"@com.app.test:integer/one", ResourceId(0x7f030000),
                      util::make_unique<BinaryPrimitive>(uint8_t(Res_value::TYPE_INT_DEC), 1u))
            .addValue(u"@com.app.test:integer/one", ResourceId(0x7f030000),
                      test::parseConfigOrDie("v1"),
                      util::make_unique<BinaryPrimitive>(uint8_t(Res_value::TYPE_INT_DEC), 2u))
            .addString(u"@com.app.test:string/test", ResourceId(0x7f040000), u"foo")
            .addString(u"@com.app.test:layout/bar", ResourceId(0x7f050000), u"res/layout/bar.xml")
            .build();

    ResTable resTable;
    ASSERT_TRUE(flatten(table.get(), &resTable));

    EXPECT_TRUE(exists(&resTable, u"@com.app.test:id/one", ResourceId(0x7f020000), {},
                       Res_value::TYPE_INT_BOOLEAN, 0u, 0u));

    EXPECT_TRUE(exists(&resTable, u"@com.app.test:id/two", ResourceId(0x7f020001), {},
                       Res_value::TYPE_INT_BOOLEAN, 0u, 0u));

    EXPECT_TRUE(exists(&resTable, u"@com.app.test:id/three", ResourceId(0x7f020002), {},
                       Res_value::TYPE_REFERENCE, 0x7f020000u, 0u));

    EXPECT_TRUE(exists(&resTable, u"@com.app.test:integer/one", ResourceId(0x7f030000),
                       {}, Res_value::TYPE_INT_DEC, 1u,
                       ResTable_config::CONFIG_VERSION));

    EXPECT_TRUE(exists(&resTable, u"@com.app.test:integer/one", ResourceId(0x7f030000),
                       test::parseConfigOrDie("v1"), Res_value::TYPE_INT_DEC, 2u,
                       ResTable_config::CONFIG_VERSION));

    StringPiece16 fooStr = u"foo";
    ssize_t idx = resTable.getTableStringBlock(0)->indexOfString(fooStr.data(), fooStr.size());
    ASSERT_GE(idx, 0);
    EXPECT_TRUE(exists(&resTable, u"@com.app.test:string/test", ResourceId(0x7f040000),
                       {}, Res_value::TYPE_STRING, (uint32_t) idx, 0u));

    StringPiece16 barPath = u"res/layout/bar.xml";
    idx = resTable.getTableStringBlock(0)->indexOfString(barPath.data(), barPath.size());
    ASSERT_GE(idx, 0);
    EXPECT_TRUE(exists(&resTable, u"@com.app.test:layout/bar", ResourceId(0x7f050000), {},
                       Res_value::TYPE_STRING, (uint32_t) idx, 0u));
}

TEST_F(TableFlattenerTest, FlattenEntriesWithGapsInIds) {
    std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
            .setPackageId(u"com.app.test", 0x7f)
            .addSimple(u"@com.app.test:id/one", ResourceId(0x7f020001))
            .addSimple(u"@com.app.test:id/three", ResourceId(0x7f020003))
            .build();

    ResTable resTable;
    ASSERT_TRUE(flatten(table.get(), &resTable));

    EXPECT_TRUE(exists(&resTable, u"@com.app.test:id/one", ResourceId(0x7f020001), {},
                       Res_value::TYPE_INT_BOOLEAN, 0u, 0u));
    EXPECT_TRUE(exists(&resTable, u"@com.app.test:id/three", ResourceId(0x7f020003), {},
                           Res_value::TYPE_INT_BOOLEAN, 0u, 0u));
}

TEST_F(TableFlattenerTest, FlattenMinMaxAttributes) {
    Attribute attr(false);
    attr.typeMask = android::ResTable_map::TYPE_INTEGER;
    attr.minInt = 10;
    attr.maxInt = 23;
    std::unique_ptr<ResourceTable> table = test::ResourceTableBuilder()
            .setPackageId(u"android", 0x01)
            .addValue(u"@android:attr/foo", ResourceId(0x01010000),
                      util::make_unique<Attribute>(attr))
            .build();

    ResourceTable result;
    ASSERT_TRUE(flatten(table.get(), &result));

    Attribute* actualAttr = test::getValue<Attribute>(&result, u"@android:attr/foo");
    ASSERT_NE(nullptr, actualAttr);
    EXPECT_EQ(attr.isWeak(), actualAttr->isWeak());
    EXPECT_EQ(attr.typeMask, actualAttr->typeMask);
    EXPECT_EQ(attr.minInt, actualAttr->minInt);
    EXPECT_EQ(attr.maxInt, actualAttr->maxInt);
}

} // namespace aapt