summaryrefslogtreecommitdiffstats
path: root/tools/aapt2/XmlFlattener_test.cpp
blob: 8915d2478b64c9eec6f3dd33bad35432b31b97a2 (plain)
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
232
/*
 * 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 "MockResolver.h"
#include "ResourceTable.h"
#include "ResourceValues.h"
#include "Util.h"
#include "XmlFlattener.h"

#include <androidfw/AssetManager.h>
#include <androidfw/ResourceTypes.h>
#include <gtest/gtest.h>
#include <sstream>
#include <string>

using namespace android;

namespace aapt {
namespace xml {

constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

class XmlFlattenerTest : public ::testing::Test {
public:
    virtual void SetUp() override {
        mResolver = std::make_shared<MockResolver>(
                std::make_shared<ResourceTable>(),
                std::map<ResourceName, ResourceId>({
                        { ResourceName{ u"android", ResourceType::kAttr, u"attr" },
                          ResourceId{ 0x01010000u } },
                        { ResourceName{ u"android", ResourceType::kId, u"id" },
                          ResourceId{ 0x01020000u } },
                        { ResourceName{ u"com.lib", ResourceType::kAttr, u"attr" },
                          ResourceId{ 0x01010001u } },
                        { ResourceName{ u"com.lib", ResourceType::kId, u"id" },
                          ResourceId{ 0x01020001u } }}));
    }

    ::testing::AssertionResult testFlatten(const std::string& in, ResXMLTree* outTree) {
        std::stringstream input(kXmlPreamble);
        input << in << std::endl;

        SourceLogger logger(Source{ "test.xml" });
        std::unique_ptr<Node> root = inflate(&input, &logger);
        if (!root) {
            return ::testing::AssertionFailure();
        }

        BigBuffer outBuffer(1024);
        if (!flattenAndLink(Source{ "test.xml" }, root.get(), std::u16string(u"android"),
                    mResolver, {}, &outBuffer)) {
            return ::testing::AssertionFailure();
        }

        std::unique_ptr<uint8_t[]> data = util::copy(outBuffer);
        if (outTree->setTo(data.get(), outBuffer.size(), true) != NO_ERROR) {
            return ::testing::AssertionFailure();
        }
        return ::testing::AssertionSuccess();
    }

    std::shared_ptr<IResolver> mResolver;
};

TEST_F(XmlFlattenerTest, ParseSimpleView) {
    std::string input = R"EOF(
        <View xmlns:android="http://schemas.android.com/apk/res/android"
              android:attr="@id/id"
              class="str"
              style="@id/id">
        </View>
    )EOF";
    ResXMLTree tree;
    ASSERT_TRUE(testFlatten(input, &tree));

    while (tree.next() != ResXMLTree::START_TAG) {
        ASSERT_NE(tree.getEventType(), ResXMLTree::END_DOCUMENT);
        ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
    }

    const StringPiece16 androidNs = u"http://schemas.android.com/apk/res/android";
    const StringPiece16 attrName = u"attr";
    ssize_t idx = tree.indexOfAttribute(androidNs.data(), androidNs.size(), attrName.data(),
                                        attrName.size());
    ASSERT_GE(idx, 0);
    EXPECT_EQ(tree.getAttributeNameResID(idx), 0x01010000u);
    EXPECT_EQ(tree.getAttributeDataType(idx), android::Res_value::TYPE_REFERENCE);

    const StringPiece16 class16 = u"class";
    idx = tree.indexOfAttribute(nullptr, 0, class16.data(), class16.size());
    ASSERT_GE(idx, 0);
    EXPECT_EQ(tree.getAttributeNameResID(idx), 0u);
    EXPECT_EQ(tree.getAttributeDataType(idx), android::Res_value::TYPE_STRING);
    EXPECT_EQ(tree.getAttributeData(idx), tree.getAttributeValueStringID(idx));

    const StringPiece16 style16 = u"style";
    idx = tree.indexOfAttribute(nullptr, 0, style16.data(), style16.size());
    ASSERT_GE(idx, 0);
    EXPECT_EQ(tree.getAttributeNameResID(idx), 0u);
    EXPECT_EQ(tree.getAttributeDataType(idx), android::Res_value::TYPE_REFERENCE);
    EXPECT_EQ((uint32_t) tree.getAttributeData(idx), 0x01020000u);
    EXPECT_EQ(tree.getAttributeValueStringID(idx), -1);

    while (tree.next() != ResXMLTree::END_DOCUMENT) {
        ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
    }
}

TEST_F(XmlFlattenerTest, ParseViewWithPackageAlias) {
    std::string input = "<View xmlns:ns1=\"http://schemas.android.com/apk/res/android\"\n"
                        "      xmlns:ns2=\"http://schemas.android.com/apk/res/android\"\n"
                        "      ns1:attr=\"@ns2:id/id\">\n"
                        "</View>";
    ResXMLTree tree;
    ASSERT_TRUE(testFlatten(input, &tree));

    while (tree.next() != ResXMLTree::END_DOCUMENT) {
        ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
    }
}

::testing::AssertionResult attributeNameAndValueEquals(ResXMLTree* tree, size_t index,
                                                       ResourceId nameId, ResourceId valueId) {
    if (index >= tree->getAttributeCount()) {
        return ::testing::AssertionFailure() << "index " << index << " is out of bounds ("
                                             << tree->getAttributeCount() << ")";
    }

    if (tree->getAttributeNameResID(index) != nameId.id) {
        return ::testing::AssertionFailure()
                << "attribute at index " << index << " has ID "
                << ResourceId{ (uint32_t) tree->getAttributeNameResID(index) }
                << ". Expected ID " << nameId;
    }

    if (tree->getAttributeDataType(index) != Res_value::TYPE_REFERENCE) {
        return ::testing::AssertionFailure() << "attribute at index " << index << " has value of "
                                             << "type " << std::hex
                                             << tree->getAttributeDataType(index) << std::dec
                                             << ". Expected reference (" << std::hex
                                             << Res_value::TYPE_REFERENCE << std::dec << ")";
    }

    if ((uint32_t) tree->getAttributeData(index) != valueId.id) {
        return ::testing::AssertionFailure()
                << "attribute at index " << index << " has value " << "with ID "
                << ResourceId{ (uint32_t) tree->getAttributeData(index) }
                << ". Expected ID " << valueId;
    }
    return ::testing::AssertionSuccess();
}

TEST_F(XmlFlattenerTest, ParseViewWithShadowedPackageAlias) {
    std::string input = "<View xmlns:app=\"http://schemas.android.com/apk/res/android\"\n"
                        "      app:attr=\"@app:id/id\">\n"
                        "  <View xmlns:app=\"http://schemas.android.com/apk/res/com.lib\"\n"
                        "        app:attr=\"@app:id/id\"/>\n"
                        "</View>";
    ResXMLTree tree;
    ASSERT_TRUE(testFlatten(input, &tree));

    while (tree.next() != ResXMLTree::START_TAG) {
        ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
        ASSERT_NE(tree.getEventType(), ResXMLTree::END_DOCUMENT);
    }

    ASSERT_TRUE(attributeNameAndValueEquals(&tree, 0u, ResourceId{ 0x01010000u },
                                            ResourceId{ 0x01020000u }));

    while (tree.next() != ResXMLTree::START_TAG) {
        ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
        ASSERT_NE(tree.getEventType(), ResXMLTree::END_DOCUMENT);
    }

    ASSERT_TRUE(attributeNameAndValueEquals(&tree, 0u, ResourceId{ 0x01010001u },
                                            ResourceId{ 0x01020001u }));
}

TEST_F(XmlFlattenerTest, ParseViewWithLocalPackageAndAliasOfTheSameName) {
    std::string input = "<View xmlns:android=\"http://schemas.android.com/apk/res/com.lib\"\n"
                        "      android:attr=\"@id/id\"/>";
    ResXMLTree tree;
    ASSERT_TRUE(testFlatten(input, &tree));

    while (tree.next() != ResXMLTree::START_TAG) {
        ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
        ASSERT_NE(tree.getEventType(), ResXMLTree::END_DOCUMENT);
    }

    // We expect the 'android:attr' to be converted to 'com.lib:attr' due to the namespace
    // assignment.
    // However, we didn't give '@id/id' a package, so it should use the default package
    // 'android', and not be converted from 'android' to 'com.lib'.
    ASSERT_TRUE(attributeNameAndValueEquals(&tree, 0u, ResourceId{ 0x01010001u },
                                            ResourceId{ 0x01020000u }));
}

/*
 * The device ResXMLParser in libandroidfw differentiates between empty namespace and null
 * namespace.
 */
TEST_F(XmlFlattenerTest, NoNamespaceIsNotTheSameAsEmptyNamespace) {
    std::string input = "<View xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
                        "      package=\"android\"/>";

    ResXMLTree tree;
    ASSERT_TRUE(testFlatten(input, &tree));

    while (tree.next() != ResXMLTree::START_TAG) {
        ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
        ASSERT_NE(tree.getEventType(), ResXMLTree::END_DOCUMENT);
    }

    const StringPiece16 kPackage = u"package";
    EXPECT_GE(tree.indexOfAttribute(nullptr, 0, kPackage.data(), kPackage.size()), 0);
}

} // namespace xml
} // namespace aapt