blob: b45cd9b8266e9bb8e6028e025e174e2ad5766e90 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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
Adam Lesinski838a6872015-05-01 13:14:05 -070017#include "MockResolver.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018#include "ResourceTable.h"
19#include "ResourceValues.h"
20#include "SourceXmlPullParser.h"
21#include "Util.h"
22#include "XmlFlattener.h"
23
24#include <androidfw/AssetManager.h>
25#include <androidfw/ResourceTypes.h>
26#include <gtest/gtest.h>
27#include <sstream>
28#include <string>
29
Adam Lesinskica2fc352015-04-03 12:08:26 -070030using namespace android;
31
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032namespace aapt {
33
34constexpr const char* kXmlPreamble = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
35
36class XmlFlattenerTest : public ::testing::Test {
37public:
38 virtual void SetUp() override {
Adam Lesinski838a6872015-05-01 13:14:05 -070039 std::shared_ptr<IResolver> resolver = std::make_shared<MockResolver>(
40 std::make_shared<ResourceTable>(),
Adam Lesinski24aad162015-04-24 19:19:30 -070041 std::map<ResourceName, ResourceId>({
42 { ResourceName{ u"android", ResourceType::kAttr, u"attr" },
43 ResourceId{ 0x01010000u } },
44 { ResourceName{ u"android", ResourceType::kId, u"id" },
45 ResourceId{ 0x01020000u } },
46 { ResourceName{ u"com.lib", ResourceType::kAttr, u"attr" },
47 ResourceId{ 0x01010001u } },
48 { ResourceName{ u"com.lib", ResourceType::kId, u"id" },
49 ResourceId{ 0x01020001u } }}));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050
Adam Lesinski24aad162015-04-24 19:19:30 -070051 mFlattener = std::make_shared<XmlFlattener>(nullptr, resolver);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052 }
53
Adam Lesinski24aad162015-04-24 19:19:30 -070054 ::testing::AssertionResult testFlatten(const std::string& in, ResXMLTree* outTree) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055 std::stringstream input(kXmlPreamble);
Adam Lesinski24aad162015-04-24 19:19:30 -070056 input << in << std::endl;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057 std::shared_ptr<XmlPullParser> xmlParser = std::make_shared<SourceXmlPullParser>(input);
58 BigBuffer outBuffer(1024);
Adam Lesinski24aad162015-04-24 19:19:30 -070059 XmlFlattener::Options xmlOptions;
60 xmlOptions.defaultPackage = u"android";
61 if (!mFlattener->flatten(Source{ "test" }, xmlParser, &outBuffer, xmlOptions)) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062 return ::testing::AssertionFailure();
63 }
64
65 std::unique_ptr<uint8_t[]> data = util::copy(outBuffer);
Adam Lesinskica2fc352015-04-03 12:08:26 -070066 if (outTree->setTo(data.get(), outBuffer.size(), true) != NO_ERROR) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067 return ::testing::AssertionFailure();
68 }
69 return ::testing::AssertionSuccess();
70 }
71
72 std::shared_ptr<XmlFlattener> mFlattener;
73};
74
75TEST_F(XmlFlattenerTest, ParseSimpleView) {
Adam Lesinski24aad162015-04-24 19:19:30 -070076 std::string input = "<View xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
77 " android:attr=\"@id/id\">\n"
78 "</View>";
Adam Lesinskica2fc352015-04-03 12:08:26 -070079 ResXMLTree tree;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080 ASSERT_TRUE(testFlatten(input, &tree));
81
Adam Lesinskica2fc352015-04-03 12:08:26 -070082 while (tree.next() != ResXMLTree::END_DOCUMENT) {
83 ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084 }
85}
86
Adam Lesinski24aad162015-04-24 19:19:30 -070087TEST_F(XmlFlattenerTest, ParseViewWithPackageAlias) {
88 std::string input = "<View xmlns:ns1=\"http://schemas.android.com/apk/res/android\"\n"
89 " xmlns:ns2=\"http://schemas.android.com/apk/res/android\"\n"
90 " ns1:attr=\"@ns2:id/id\">\n"
91 "</View>";
92 ResXMLTree tree;
93 ASSERT_TRUE(testFlatten(input, &tree));
94
95 while (tree.next() != ResXMLTree::END_DOCUMENT) {
96 ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
97 }
98}
99
100::testing::AssertionResult attributeNameAndValueEquals(ResXMLTree* tree, size_t index,
101 ResourceId nameId, ResourceId valueId) {
102 if (index >= tree->getAttributeCount()) {
103 return ::testing::AssertionFailure() << "index " << index << " is out of bounds ("
104 << tree->getAttributeCount() << ")";
105 }
106
107 if (tree->getAttributeNameResID(index) != nameId.id) {
108 return ::testing::AssertionFailure()
109 << "attribute at index " << index << " has ID "
110 << ResourceId{ (uint32_t) tree->getAttributeNameResID(index) }
111 << ". Expected ID " << nameId;
112 }
113
114 if (tree->getAttributeDataType(index) != Res_value::TYPE_REFERENCE) {
115 return ::testing::AssertionFailure() << "attribute at index " << index << " has value of "
116 << "type " << std::hex
117 << tree->getAttributeDataType(index) << std::dec
118 << ". Expected reference (" << std::hex
119 << Res_value::TYPE_REFERENCE << std::dec << ")";
120 }
121
122 if ((uint32_t) tree->getAttributeData(index) != valueId.id) {
123 return ::testing::AssertionFailure()
124 << "attribute at index " << index << " has value " << "with ID "
125 << ResourceId{ (uint32_t) tree->getAttributeData(index) }
126 << ". Expected ID " << valueId;
127 }
128 return ::testing::AssertionSuccess();
129}
130
131TEST_F(XmlFlattenerTest, ParseViewWithShadowedPackageAlias) {
132 std::string input = "<View xmlns:app=\"http://schemas.android.com/apk/res/android\"\n"
133 " app:attr=\"@app:id/id\">\n"
134 " <View xmlns:app=\"http://schemas.android.com/apk/res/com.lib\"\n"
135 " app:attr=\"@app:id/id\"/>\n"
136 "</View>";
137 ResXMLTree tree;
138 ASSERT_TRUE(testFlatten(input, &tree));
139
140 while (tree.next() != ResXMLTree::START_TAG) {
141 ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
142 ASSERT_NE(tree.getEventType(), ResXMLTree::END_DOCUMENT);
143 }
144
145 ASSERT_TRUE(attributeNameAndValueEquals(&tree, 0u, ResourceId{ 0x01010000u },
146 ResourceId{ 0x01020000u }));
147
148 while (tree.next() != ResXMLTree::START_TAG) {
149 ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
150 ASSERT_NE(tree.getEventType(), ResXMLTree::END_DOCUMENT);
151 }
152
153 ASSERT_TRUE(attributeNameAndValueEquals(&tree, 0u, ResourceId{ 0x01010001u },
154 ResourceId{ 0x01020001u }));
155}
156
157TEST_F(XmlFlattenerTest, ParseViewWithLocalPackageAndAliasOfTheSameName) {
158 std::string input = "<View xmlns:android=\"http://schemas.android.com/apk/res/com.lib\"\n"
159 " android:attr=\"@id/id\"/>";
160 ResXMLTree tree;
161 ASSERT_TRUE(testFlatten(input, &tree));
162
163 while (tree.next() != ResXMLTree::START_TAG) {
164 ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
165 ASSERT_NE(tree.getEventType(), ResXMLTree::END_DOCUMENT);
166 }
167
168 // We expect the 'android:attr' to be converted to 'com.lib:attr' due to the namespace
169 // assignment.
170 // However, we didn't give '@id/id' a package, so it should use the default package
171 // 'android', and not be converted from 'android' to 'com.lib'.
172 ASSERT_TRUE(attributeNameAndValueEquals(&tree, 0u, ResourceId{ 0x01010001u },
173 ResourceId{ 0x01020000u }));
174}
175
176/*
177 * The device ResXMLParser in libandroidfw differentiates between empty namespace and null
178 * namespace.
179 */
180TEST_F(XmlFlattenerTest, NoNamespaceIsNotTheSameAsEmptyNamespace) {
181 std::string input = "<View xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
182 " package=\"android\"/>";
183
184 ResXMLTree tree;
185 ASSERT_TRUE(testFlatten(input, &tree));
186
187 while (tree.next() != ResXMLTree::START_TAG) {
188 ASSERT_NE(tree.getEventType(), ResXMLTree::BAD_DOCUMENT);
189 ASSERT_NE(tree.getEventType(), ResXMLTree::END_DOCUMENT);
190 }
191
192 const StringPiece16 kPackage = u"package";
193 EXPECT_GE(tree.indexOfAttribute(nullptr, 0, kPackage.data(), kPackage.size()), 0);
194}
195
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800196} // namespace aapt