blob: 6c717dcd84c80075cff85642b064226e4b3451f0 [file] [log] [blame]
Adam Lesinski75f3a552015-06-03 14:54:23 -07001/*
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 Lesinski467f1712015-11-16 17:35:44 -080017#include "xml/XmlDom.h"
Adam Lesinski75f3a552015-06-03 14:54:23 -070018
Adam Lesinski75f3a552015-06-03 14:54:23 -070019#include <string>
20
Adam Lesinski46708052017-09-29 14:49:15 -070021#include "format/binary/XmlFlattener.h"
Adam Lesinski00451162017-10-03 07:44:08 -070022#include "io/StringStream.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "test/Test.h"
24
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070025using ::aapt::io::StringInputStream;
Adam Lesinskibbf42972018-02-14 13:36:09 -080026using ::aapt::test::ValueEq;
Adam Lesinskifba0cf22017-06-29 17:53:36 -070027using ::testing::Eq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070028using ::testing::NotNull;
Adam Lesinskibbf42972018-02-14 13:36:09 -080029using ::testing::Pointee;
Adam Lesinskifba0cf22017-06-29 17:53:36 -070030using ::testing::SizeIs;
Adam Lesinski6b372992017-08-09 10:54:23 -070031using ::testing::StrEq;
Adam Lesinskia45893a2017-05-30 15:19:02 -070032
Adam Lesinski75f3a552015-06-03 14:54:23 -070033namespace aapt {
Adam Lesinski6b372992017-08-09 10:54:23 -070034namespace xml {
Adam Lesinski75f3a552015-06-03 14:54:23 -070035
Adam Lesinski75f3a552015-06-03 14:54:23 -070036TEST(XmlDomTest, Inflate) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070037 std::string input = R"(<?xml version="1.0" encoding="utf-8"?>
Adam Lesinskifba0cf22017-06-29 17:53:36 -070038 <Layout xmlns:android="http://schemas.android.com/apk/res/android"
39 android:layout_width="match_parent"
40 android:layout_height="wrap_content">
41 <TextView android:id="@+id/id"
42 android:layout_width="wrap_content"
43 android:layout_height="wrap_content" />
44 </Layout>)";
Adam Lesinski75f3a552015-06-03 14:54:23 -070045
Adam Lesinskice5e56e2016-10-21 17:56:45 -070046 StdErrDiagnostics diag;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070047 StringInputStream in(input);
Adam Lesinski6b372992017-08-09 10:54:23 -070048 std::unique_ptr<XmlResource> doc = Inflate(&in, &diag, Source("test.xml"));
Adam Lesinskia45893a2017-05-30 15:19:02 -070049 ASSERT_THAT(doc, NotNull());
Adam Lesinski75f3a552015-06-03 14:54:23 -070050
Adam Lesinski6b372992017-08-09 10:54:23 -070051 Element* el = doc->root.get();
52 EXPECT_THAT(el->namespace_decls, SizeIs(1u));
53 EXPECT_THAT(el->namespace_decls[0].uri, StrEq(xml::kSchemaAndroid));
54 EXPECT_THAT(el->namespace_decls[0].prefix, StrEq("android"));
Adam Lesinski75f3a552015-06-03 14:54:23 -070055}
56
Adam Lesinski0da683b2017-10-02 16:37:20 -070057TEST(XmlDomTest, BinaryInflate) {
58 std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
59 std::unique_ptr<XmlResource> doc = util::make_unique<XmlResource>();
60 doc->root = util::make_unique<Element>();
61 doc->root->name = "Layout";
62 doc->root->line_number = 2u;
63
Adam Lesinskibbf42972018-02-14 13:36:09 -080064 xml::Attribute attr;
65 attr.name = "text";
66 attr.namespace_uri = kSchemaAndroid;
67 attr.compiled_attribute = AaptAttribute(
68 aapt::Attribute(android::ResTable_map::TYPE_REFERENCE | android::ResTable_map::TYPE_STRING),
69 ResourceId(0x01010001u));
70 attr.value = "@string/foo";
71 attr.compiled_value = test::BuildReference("string/foo", ResourceId(0x7f010000u));
72 doc->root->attributes.push_back(std::move(attr));
73
Adam Lesinski0da683b2017-10-02 16:37:20 -070074 NamespaceDecl decl;
75 decl.uri = kSchemaAndroid;
76 decl.prefix = "android";
77 decl.line_number = 2u;
78 doc->root->namespace_decls.push_back(decl);
79
80 BigBuffer buffer(4096);
Adam Lesinskibbf42972018-02-14 13:36:09 -080081 XmlFlattenerOptions options;
82 options.keep_raw_values = true;
83 XmlFlattener flattener(&buffer, options);
Adam Lesinski0da683b2017-10-02 16:37:20 -070084 ASSERT_TRUE(flattener.Consume(context.get(), doc.get()));
85
86 auto block = util::Copy(buffer);
Adam Lesinski8780eb62017-10-31 17:44:39 -070087 std::unique_ptr<XmlResource> new_doc = Inflate(block.get(), buffer.size(), nullptr);
Adam Lesinski0da683b2017-10-02 16:37:20 -070088 ASSERT_THAT(new_doc, NotNull());
89
90 EXPECT_THAT(new_doc->root->name, StrEq("Layout"));
91 EXPECT_THAT(new_doc->root->line_number, Eq(2u));
Adam Lesinskibbf42972018-02-14 13:36:09 -080092
93 ASSERT_THAT(new_doc->root->attributes, SizeIs(1u));
94 EXPECT_THAT(new_doc->root->attributes[0].name, StrEq("text"));
95 EXPECT_THAT(new_doc->root->attributes[0].namespace_uri, StrEq(kSchemaAndroid));
96
97 // We only check that the resource ID was preserved. There is no where to encode the types that
98 // the Attribute accepts (eg: string|reference).
99 ASSERT_TRUE(new_doc->root->attributes[0].compiled_attribute);
100 EXPECT_THAT(new_doc->root->attributes[0].compiled_attribute.value().id,
Ryan Mitchell4382e442021-07-14 12:53:01 -0700101 Eq(ResourceId(0x01010001u)));
Adam Lesinskibbf42972018-02-14 13:36:09 -0800102
103 EXPECT_THAT(new_doc->root->attributes[0].value, StrEq("@string/foo"));
104 EXPECT_THAT(new_doc->root->attributes[0].compiled_value,
105 Pointee(ValueEq(Reference(ResourceId(0x7f010000u)))));
106
Adam Lesinski0da683b2017-10-02 16:37:20 -0700107 ASSERT_THAT(new_doc->root->namespace_decls, SizeIs(1u));
108 EXPECT_THAT(new_doc->root->namespace_decls[0].uri, StrEq(kSchemaAndroid));
109 EXPECT_THAT(new_doc->root->namespace_decls[0].prefix, StrEq("android"));
110 EXPECT_THAT(new_doc->root->namespace_decls[0].line_number, Eq(2u));
111}
112
Adam Lesinski48448e82017-04-26 15:13:52 -0700113// Escaping is handled after parsing of the values for resource-specific values.
114TEST(XmlDomTest, ForwardEscapes) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700115 std::unique_ptr<XmlResource> doc = test::BuildXmlDom(R"(
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700116 <element value="\?hello" pattern="\\d{5}">\\d{5}</element>)");
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800117
Adam Lesinski6b372992017-08-09 10:54:23 -0700118 Element* el = doc->root.get();
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800119
Adam Lesinski6b372992017-08-09 10:54:23 -0700120 Attribute* attr = el->FindAttribute({}, "pattern");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700121 ASSERT_THAT(attr, NotNull());
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700122 EXPECT_THAT(attr->value, Eq("\\\\d{5}"));
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800123
Adam Lesinski48448e82017-04-26 15:13:52 -0700124 attr = el->FindAttribute({}, "value");
Adam Lesinskia45893a2017-05-30 15:19:02 -0700125 ASSERT_THAT(attr, NotNull());
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700126 EXPECT_THAT(attr->value, Eq("\\?hello"));
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800127
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700128 ASSERT_THAT(el->children, SizeIs(1u));
129
Adam Lesinski6b372992017-08-09 10:54:23 -0700130 Text* text = xml::NodeCast<xml::Text>(el->children[0].get());
Adam Lesinskia45893a2017-05-30 15:19:02 -0700131 ASSERT_THAT(text, NotNull());
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700132 EXPECT_THAT(text->text, Eq("\\\\d{5}"));
133}
134
135TEST(XmlDomTest, XmlEscapeSequencesAreParsed) {
Adam Lesinski6b372992017-08-09 10:54:23 -0700136 std::unique_ptr<XmlResource> doc = test::BuildXmlDom(R"(<element value="&quot;" />)");
137 Attribute* attr = doc->root->FindAttribute({}, "value");
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700138 ASSERT_THAT(attr, NotNull());
Adam Lesinskifba0cf22017-06-29 17:53:36 -0700139 EXPECT_THAT(attr->value, Eq("\""));
Adam Lesinskiac6edc52017-03-02 19:31:28 -0800140}
141
Adam Lesinski6b372992017-08-09 10:54:23 -0700142class TestVisitor : public PackageAwareVisitor {
143 public:
144 using PackageAwareVisitor::Visit;
145
146 void Visit(Element* el) override {
147 if (el->name == "View1") {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700148 EXPECT_THAT(TransformPackageAlias("one"), Eq(ExtractedPackage{"com.one", false}));
Adam Lesinski6b372992017-08-09 10:54:23 -0700149 } else if (el->name == "View2") {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700150 EXPECT_THAT(TransformPackageAlias("one"), Eq(ExtractedPackage{"com.one", false}));
151 EXPECT_THAT(TransformPackageAlias("two"), Eq(ExtractedPackage{"com.two", false}));
Adam Lesinski6b372992017-08-09 10:54:23 -0700152 } else if (el->name == "View3") {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700153 EXPECT_THAT(TransformPackageAlias("one"), Eq(ExtractedPackage{"com.one", false}));
154 EXPECT_THAT(TransformPackageAlias("two"), Eq(ExtractedPackage{"com.two", false}));
155 EXPECT_THAT(TransformPackageAlias("three"), Eq(ExtractedPackage{"com.three", false}));
Adam Lesinskic9a29262018-03-01 20:04:00 -0800156 } else if (el->name == "View4") {
Ryan Mitchell4382e442021-07-14 12:53:01 -0700157 EXPECT_THAT(TransformPackageAlias("one"), Eq(ExtractedPackage{"com.one", false}));
158 EXPECT_THAT(TransformPackageAlias("two"), Eq(ExtractedPackage{"com.two", false}));
159 EXPECT_THAT(TransformPackageAlias("three"), Eq(ExtractedPackage{"com.three", false}));
160 EXPECT_THAT(TransformPackageAlias("four"), Eq(ExtractedPackage{"", true}));
Adam Lesinski6b372992017-08-09 10:54:23 -0700161 }
162 }
163};
164
165TEST(XmlDomTest, PackageAwareXmlVisitor) {
166 std::unique_ptr<XmlResource> doc = test::BuildXmlDom(R"(
167 <View1 xmlns:one="http://schemas.android.com/apk/res/com.one">
168 <View2 xmlns:two="http://schemas.android.com/apk/res/com.two">
Adam Lesinskic9a29262018-03-01 20:04:00 -0800169 <View3 xmlns:three="http://schemas.android.com/apk/res/com.three">
170 <View4 xmlns:four="http://schemas.android.com/apk/res-auto" />
171 </View3>
Adam Lesinski6b372992017-08-09 10:54:23 -0700172 </View2>
173 </View1>)");
174
Adam Lesinski6b372992017-08-09 10:54:23 -0700175 TestVisitor visitor;
176 doc->root->Accept(&visitor);
177}
178
179} // namespace xml
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180} // namespace aapt