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 "xml/XmlDom.h"
18 
19 #include <string>
20 
21 #include "format/binary/XmlFlattener.h"
22 #include "io/StringStream.h"
23 #include "test/Test.h"
24 
25 using ::aapt::io::StringInputStream;
26 using ::aapt::test::ValueEq;
27 using ::testing::Eq;
28 using ::testing::NotNull;
29 using ::testing::Pointee;
30 using ::testing::SizeIs;
31 using ::testing::StrEq;
32 
33 namespace aapt {
34 namespace xml {
35 
TEST(XmlDomTest,Inflate)36 TEST(XmlDomTest, Inflate) {
37   std::string input = R"(<?xml version="1.0" encoding="utf-8"?>
38       <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>)";
45 
46   StdErrDiagnostics diag;
47   StringInputStream in(input);
48   std::unique_ptr<XmlResource> doc = Inflate(&in, &diag, Source("test.xml"));
49   ASSERT_THAT(doc, NotNull());
50 
51   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"));
55 }
56 
57 TEST(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 
64   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 
74   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);
81   XmlFlattenerOptions options;
82   options.keep_raw_values = true;
83   XmlFlattener flattener(&buffer, options);
84   ASSERT_TRUE(flattener.Consume(context.get(), doc.get()));
85 
86   auto block = util::Copy(buffer);
87   std::unique_ptr<XmlResource> new_doc = Inflate(block.get(), buffer.size(), nullptr);
88   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));
92 
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,
101               Eq(make_value(ResourceId(0x01010001u))));
102 
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 
107   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 
113 // Escaping is handled after parsing of the values for resource-specific values.
114 TEST(XmlDomTest, ForwardEscapes) {
115   std::unique_ptr<XmlResource> doc = test::BuildXmlDom(R"(
116       <element value="\?hello" pattern="\\d{5}">\\d{5}</element>)");
117 
118   Element* el = doc->root.get();
119 
120   Attribute* attr = el->FindAttribute({}, "pattern");
121   ASSERT_THAT(attr, NotNull());
122   EXPECT_THAT(attr->value, Eq("\\\\d{5}"));
123 
124   attr = el->FindAttribute({}, "value");
125   ASSERT_THAT(attr, NotNull());
126   EXPECT_THAT(attr->value, Eq("\\?hello"));
127 
128   ASSERT_THAT(el->children, SizeIs(1u));
129 
130   Text* text = xml::NodeCast<xml::Text>(el->children[0].get());
131   ASSERT_THAT(text, NotNull());
132   EXPECT_THAT(text->text, Eq("\\\\d{5}"));
133 }
134 
135 TEST(XmlDomTest, XmlEscapeSequencesAreParsed) {
136   std::unique_ptr<XmlResource> doc = test::BuildXmlDom(R"(<element value="&quot;" />)");
137   Attribute* attr = doc->root->FindAttribute({}, "value");
138   ASSERT_THAT(attr, NotNull());
139   EXPECT_THAT(attr->value, Eq("\""));
140 }
141 
142 class TestVisitor : public PackageAwareVisitor {
143  public:
144   using PackageAwareVisitor::Visit;
145 
Visit(Element * el)146   void Visit(Element* el) override {
147     if (el->name == "View1") {
148       EXPECT_THAT(TransformPackageAlias("one"), Eq(make_value(ExtractedPackage{"com.one", false})));
149     } else if (el->name == "View2") {
150       EXPECT_THAT(TransformPackageAlias("one"), Eq(make_value(ExtractedPackage{"com.one", false})));
151       EXPECT_THAT(TransformPackageAlias("two"), Eq(make_value(ExtractedPackage{"com.two", false})));
152     } else if (el->name == "View3") {
153       EXPECT_THAT(TransformPackageAlias("one"), Eq(make_value(ExtractedPackage{"com.one", false})));
154       EXPECT_THAT(TransformPackageAlias("two"), Eq(make_value(ExtractedPackage{"com.two", false})));
155       EXPECT_THAT(TransformPackageAlias("three"),
156                   Eq(make_value(ExtractedPackage{"com.three", false})));
157     } else if (el->name == "View4") {
158       EXPECT_THAT(TransformPackageAlias("one"), Eq(make_value(ExtractedPackage{"com.one", false})));
159       EXPECT_THAT(TransformPackageAlias("two"), Eq(make_value(ExtractedPackage{"com.two", false})));
160       EXPECT_THAT(TransformPackageAlias("three"),
161                   Eq(make_value(ExtractedPackage{"com.three", false})));
162       EXPECT_THAT(TransformPackageAlias("four"), Eq(make_value(ExtractedPackage{"", true})));
163     }
164   }
165 };
166 
TEST(XmlDomTest,PackageAwareXmlVisitor)167 TEST(XmlDomTest, PackageAwareXmlVisitor) {
168   std::unique_ptr<XmlResource> doc = test::BuildXmlDom(R"(
169       <View1 xmlns:one="http://schemas.android.com/apk/res/com.one">
170         <View2 xmlns:two="http://schemas.android.com/apk/res/com.two">
171           <View3 xmlns:three="http://schemas.android.com/apk/res/com.three">
172             <View4 xmlns:four="http://schemas.android.com/apk/res-auto" />
173           </View3>
174         </View2>
175       </View1>)");
176 
177   TestVisitor visitor;
178   doc->root->Accept(&visitor);
179 }
180 
181 }  // namespace xml
182 }  // namespace aapt
183