1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <iostream>
12 #include <sstream>
13 #include <string>
14 #include "webrtc/libjingle/xmllite/xmlbuilder.h"
15 #include "webrtc/libjingle/xmllite/xmlelement.h"
16 #include "webrtc/libjingle/xmllite/xmlparser.h"
17 #include "webrtc/base/common.h"
18 #include "webrtc/base/gunit.h"
19 
20 using buzz::XmlBuilder;
21 using buzz::XmlElement;
22 using buzz::XmlParser;
23 
TEST(XmlBuilderTest,TestTrivial)24 TEST(XmlBuilderTest, TestTrivial) {
25   XmlBuilder builder;
26   XmlParser::ParseXml(&builder, "<testing/>");
27   EXPECT_EQ("<testing/>", builder.BuiltElement()->Str());
28 }
29 
TEST(XmlBuilderTest,TestAttributes1)30 TEST(XmlBuilderTest, TestAttributes1) {
31   XmlBuilder builder;
32   XmlParser::ParseXml(&builder, "<testing a='b'/>");
33   EXPECT_EQ("<testing a=\"b\"/>", builder.BuiltElement()->Str());
34 }
35 
TEST(XmlBuilderTest,TestAttributes2)36 TEST(XmlBuilderTest, TestAttributes2) {
37   XmlBuilder builder;
38   XmlParser::ParseXml(&builder, "<testing e='' long='some text'/>");
39   EXPECT_EQ("<testing e=\"\" long=\"some text\"/>",
40       builder.BuiltElement()->Str());
41 }
42 
TEST(XmlBuilderTest,TestNesting1)43 TEST(XmlBuilderTest, TestNesting1) {
44   XmlBuilder builder;
45   XmlParser::ParseXml(&builder,
46       "<top><first/><second><third></third></second></top>");
47   EXPECT_EQ("<top><first/><second><third/></second></top>",
48       builder.BuiltElement()->Str());
49 }
50 
TEST(XmlBuilderTest,TestNesting2)51 TEST(XmlBuilderTest, TestNesting2) {
52   XmlBuilder builder;
53   XmlParser::ParseXml(&builder,
54     "<top><fifth><deeper><and><deeper/></and><sibling><leaf/>"
55     "</sibling></deeper></fifth><first/><second><third></third>"
56     "</second></top>");
57   EXPECT_EQ("<top><fifth><deeper><and><deeper/></and><sibling><leaf/>"
58     "</sibling></deeper></fifth><first/><second><third/>"
59     "</second></top>", builder.BuiltElement()->Str());
60 }
61 
TEST(XmlBuilderTest,TestQuoting1)62 TEST(XmlBuilderTest, TestQuoting1) {
63   XmlBuilder builder;
64   XmlParser::ParseXml(&builder, "<testing a='>'/>");
65   EXPECT_EQ("<testing a=\"&gt;\"/>", builder.BuiltElement()->Str());
66 }
67 
TEST(XmlBuilderTest,TestQuoting2)68 TEST(XmlBuilderTest, TestQuoting2) {
69   XmlBuilder builder;
70   XmlParser::ParseXml(&builder, "<testing a='&lt;>&amp;&quot;'/>");
71   EXPECT_EQ("<testing a=\"&lt;&gt;&amp;&quot;\"/>",
72       builder.BuiltElement()->Str());
73 }
74 
TEST(XmlBuilderTest,TestQuoting3)75 TEST(XmlBuilderTest, TestQuoting3) {
76   XmlBuilder builder;
77   XmlParser::ParseXml(&builder, "<testing a='so &quot;important&quot;'/>");
78   EXPECT_EQ("<testing a=\"so &quot;important&quot;\"/>",
79       builder.BuiltElement()->Str());
80 }
81 
TEST(XmlBuilderTest,TestQuoting4)82 TEST(XmlBuilderTest, TestQuoting4) {
83   XmlBuilder builder;
84   XmlParser::ParseXml(&builder, "<testing a='&quot;important&quot;, yes'/>");
85   EXPECT_EQ("<testing a=\"&quot;important&quot;, yes\"/>",
86       builder.BuiltElement()->Str());
87 }
88 
TEST(XmlBuilderTest,TestQuoting5)89 TEST(XmlBuilderTest, TestQuoting5) {
90   XmlBuilder builder;
91   XmlParser::ParseXml(&builder,
92       "<testing a='&lt;what is &quot;important&quot;&gt;'/>");
93   EXPECT_EQ("<testing a=\"&lt;what is &quot;important&quot;&gt;\"/>",
94       builder.BuiltElement()->Str());
95 }
96 
TEST(XmlBuilderTest,TestText1)97 TEST(XmlBuilderTest, TestText1) {
98   XmlBuilder builder;
99   XmlParser::ParseXml(&builder, "<testing>></testing>");
100   EXPECT_EQ("<testing>&gt;</testing>", builder.BuiltElement()->Str());
101 }
102 
TEST(XmlBuilderTest,TestText2)103 TEST(XmlBuilderTest, TestText2) {
104   XmlBuilder builder;
105   XmlParser::ParseXml(&builder, "<testing>&lt;>&amp;&quot;</testing>");
106   EXPECT_EQ("<testing>&lt;&gt;&amp;\"</testing>",
107       builder.BuiltElement()->Str());
108 }
109 
TEST(XmlBuilderTest,TestText3)110 TEST(XmlBuilderTest, TestText3) {
111   XmlBuilder builder;
112   XmlParser::ParseXml(&builder, "<testing>so &lt;important&gt;</testing>");
113   EXPECT_EQ("<testing>so &lt;important&gt;</testing>",
114       builder.BuiltElement()->Str());
115 }
116 
TEST(XmlBuilderTest,TestText4)117 TEST(XmlBuilderTest, TestText4) {
118   XmlBuilder builder;
119   XmlParser::ParseXml(&builder, "<testing>&lt;important&gt;, yes</testing>");
120   EXPECT_EQ("<testing>&lt;important&gt;, yes</testing>",
121       builder.BuiltElement()->Str());
122 }
123 
TEST(XmlBuilderTest,TestText5)124 TEST(XmlBuilderTest, TestText5) {
125   XmlBuilder builder;
126   XmlParser::ParseXml(&builder,
127       "<testing>importance &amp;&lt;important&gt;&amp;</testing>");
128   EXPECT_EQ("<testing>importance &amp;&lt;important&gt;&amp;</testing>",
129       builder.BuiltElement()->Str());
130 }
131 
TEST(XmlBuilderTest,TestNamespace1)132 TEST(XmlBuilderTest, TestNamespace1) {
133   XmlBuilder builder;
134   XmlParser::ParseXml(&builder, "<testing xmlns='foo'/>");
135   EXPECT_EQ("<testing xmlns=\"foo\"/>", builder.BuiltElement()->Str());
136 }
137 
TEST(XmlBuilderTest,TestNamespace2)138 TEST(XmlBuilderTest, TestNamespace2) {
139   XmlBuilder builder;
140   XmlParser::ParseXml(&builder, "<testing xmlns:a='foo' a:b='c'/>");
141   EXPECT_EQ("<testing xmlns:a=\"foo\" a:b=\"c\"/>",
142       builder.BuiltElement()->Str());
143 }
144 
TEST(XmlBuilderTest,TestNamespace3)145 TEST(XmlBuilderTest, TestNamespace3) {
146   XmlBuilder builder;
147   XmlParser::ParseXml(&builder, "<testing xmlns:a=''/>");
148   EXPECT_TRUE(NULL == builder.BuiltElement());
149 }
150 
TEST(XmlBuilderTest,TestNamespace4)151 TEST(XmlBuilderTest, TestNamespace4) {
152   XmlBuilder builder;
153   XmlParser::ParseXml(&builder, "<testing a:b='c'/>");
154   EXPECT_TRUE(NULL == builder.BuiltElement());
155 }
156 
TEST(XmlBuilderTest,TestAttrCollision1)157 TEST(XmlBuilderTest, TestAttrCollision1) {
158   XmlBuilder builder;
159   XmlParser::ParseXml(&builder, "<testing a='first' a='second'/>");
160   EXPECT_TRUE(NULL == builder.BuiltElement());
161 }
162 
TEST(XmlBuilderTest,TestAttrCollision2)163 TEST(XmlBuilderTest, TestAttrCollision2) {
164   XmlBuilder builder;
165   XmlParser::ParseXml(&builder,
166       "<testing xmlns:a='foo' xmlns:b='foo' a:x='c' b:x='d'/>");
167   EXPECT_TRUE(NULL == builder.BuiltElement());
168 }
169 
TEST(XmlBuilderTest,TestAttrCollision3)170 TEST(XmlBuilderTest, TestAttrCollision3) {
171   XmlBuilder builder;
172   XmlParser::ParseXml(&builder,
173       "<testing xmlns:a='foo'><nested xmlns:b='foo' a:x='c' b:x='d'/>"
174       "</testing>");
175   EXPECT_TRUE(NULL == builder.BuiltElement());
176 }
177 
178