You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
6.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <string>
  28. #include <sstream>
  29. #include <iostream>
  30. #include "talk/base/common.h"
  31. #include "talk/base/gunit.h"
  32. #include "talk/xmllite/xmlbuilder.h"
  33. #include "talk/xmllite/xmlelement.h"
  34. #include "talk/xmllite/xmlparser.h"
  35. using buzz::XmlBuilder;
  36. using buzz::XmlElement;
  37. using buzz::XmlParser;
  38. TEST(XmlBuilderTest, TestTrivial) {
  39. XmlBuilder builder;
  40. XmlParser::ParseXml(&builder, "<testing/>");
  41. EXPECT_EQ("<testing/>", builder.BuiltElement()->Str());
  42. }
  43. TEST(XmlBuilderTest, TestAttributes1) {
  44. XmlBuilder builder;
  45. XmlParser::ParseXml(&builder, "<testing a='b'/>");
  46. EXPECT_EQ("<testing a=\"b\"/>", builder.BuiltElement()->Str());
  47. }
  48. TEST(XmlBuilderTest, TestAttributes2) {
  49. XmlBuilder builder;
  50. XmlParser::ParseXml(&builder, "<testing e='' long='some text'/>");
  51. EXPECT_EQ("<testing e=\"\" long=\"some text\"/>",
  52. builder.BuiltElement()->Str());
  53. }
  54. TEST(XmlBuilderTest, TestNesting1) {
  55. XmlBuilder builder;
  56. XmlParser::ParseXml(&builder,
  57. "<top><first/><second><third></third></second></top>");
  58. EXPECT_EQ("<top><first/><second><third/></second></top>",
  59. builder.BuiltElement()->Str());
  60. }
  61. TEST(XmlBuilderTest, TestNesting2) {
  62. XmlBuilder builder;
  63. XmlParser::ParseXml(&builder,
  64. "<top><fifth><deeper><and><deeper/></and><sibling><leaf/>"
  65. "</sibling></deeper></fifth><first/><second><third></third>"
  66. "</second></top>");
  67. EXPECT_EQ("<top><fifth><deeper><and><deeper/></and><sibling><leaf/>"
  68. "</sibling></deeper></fifth><first/><second><third/>"
  69. "</second></top>", builder.BuiltElement()->Str());
  70. }
  71. TEST(XmlBuilderTest, TestQuoting1) {
  72. XmlBuilder builder;
  73. XmlParser::ParseXml(&builder, "<testing a='>'/>");
  74. EXPECT_EQ("<testing a=\"&gt;\"/>", builder.BuiltElement()->Str());
  75. }
  76. TEST(XmlBuilderTest, TestQuoting2) {
  77. XmlBuilder builder;
  78. XmlParser::ParseXml(&builder, "<testing a='&lt;>&amp;&quot;'/>");
  79. EXPECT_EQ("<testing a=\"&lt;&gt;&amp;&quot;\"/>",
  80. builder.BuiltElement()->Str());
  81. }
  82. TEST(XmlBuilderTest, TestQuoting3) {
  83. XmlBuilder builder;
  84. XmlParser::ParseXml(&builder, "<testing a='so &quot;important&quot;'/>");
  85. EXPECT_EQ("<testing a=\"so &quot;important&quot;\"/>",
  86. builder.BuiltElement()->Str());
  87. }
  88. TEST(XmlBuilderTest, TestQuoting4) {
  89. XmlBuilder builder;
  90. XmlParser::ParseXml(&builder, "<testing a='&quot;important&quot;, yes'/>");
  91. EXPECT_EQ("<testing a=\"&quot;important&quot;, yes\"/>",
  92. builder.BuiltElement()->Str());
  93. }
  94. TEST(XmlBuilderTest, TestQuoting5) {
  95. XmlBuilder builder;
  96. XmlParser::ParseXml(&builder,
  97. "<testing a='&lt;what is &quot;important&quot;&gt;'/>");
  98. EXPECT_EQ("<testing a=\"&lt;what is &quot;important&quot;&gt;\"/>",
  99. builder.BuiltElement()->Str());
  100. }
  101. TEST(XmlBuilderTest, TestText1) {
  102. XmlBuilder builder;
  103. XmlParser::ParseXml(&builder, "<testing>></testing>");
  104. EXPECT_EQ("<testing>&gt;</testing>", builder.BuiltElement()->Str());
  105. }
  106. TEST(XmlBuilderTest, TestText2) {
  107. XmlBuilder builder;
  108. XmlParser::ParseXml(&builder, "<testing>&lt;>&amp;&quot;</testing>");
  109. EXPECT_EQ("<testing>&lt;&gt;&amp;\"</testing>",
  110. builder.BuiltElement()->Str());
  111. }
  112. TEST(XmlBuilderTest, TestText3) {
  113. XmlBuilder builder;
  114. XmlParser::ParseXml(&builder, "<testing>so &lt;important&gt;</testing>");
  115. EXPECT_EQ("<testing>so &lt;important&gt;</testing>",
  116. builder.BuiltElement()->Str());
  117. }
  118. TEST(XmlBuilderTest, TestText4) {
  119. XmlBuilder builder;
  120. XmlParser::ParseXml(&builder, "<testing>&lt;important&gt;, yes</testing>");
  121. EXPECT_EQ("<testing>&lt;important&gt;, yes</testing>",
  122. builder.BuiltElement()->Str());
  123. }
  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. XmlBuilder builder;
  133. XmlParser::ParseXml(&builder, "<testing xmlns='foo'/>");
  134. EXPECT_EQ("<testing xmlns=\"foo\"/>", builder.BuiltElement()->Str());
  135. }
  136. TEST(XmlBuilderTest, TestNamespace2) {
  137. XmlBuilder builder;
  138. XmlParser::ParseXml(&builder, "<testing xmlns:a='foo' a:b='c'/>");
  139. EXPECT_EQ("<testing xmlns:a=\"foo\" a:b=\"c\"/>",
  140. builder.BuiltElement()->Str());
  141. }
  142. TEST(XmlBuilderTest, TestNamespace3) {
  143. XmlBuilder builder;
  144. XmlParser::ParseXml(&builder, "<testing xmlns:a=''/>");
  145. EXPECT_TRUE(NULL == builder.BuiltElement());
  146. }
  147. TEST(XmlBuilderTest, TestNamespace4) {
  148. XmlBuilder builder;
  149. XmlParser::ParseXml(&builder, "<testing a:b='c'/>");
  150. EXPECT_TRUE(NULL == builder.BuiltElement());
  151. }
  152. TEST(XmlBuilderTest, TestAttrCollision1) {
  153. XmlBuilder builder;
  154. XmlParser::ParseXml(&builder, "<testing a='first' a='second'/>");
  155. EXPECT_TRUE(NULL == builder.BuiltElement());
  156. }
  157. TEST(XmlBuilderTest, TestAttrCollision2) {
  158. XmlBuilder builder;
  159. XmlParser::ParseXml(&builder,
  160. "<testing xmlns:a='foo' xmlns:b='foo' a:x='c' b:x='d'/>");
  161. EXPECT_TRUE(NULL == builder.BuiltElement());
  162. }
  163. TEST(XmlBuilderTest, TestAttrCollision3) {
  164. XmlBuilder builder;
  165. XmlParser::ParseXml(&builder,
  166. "<testing xmlns:a='foo'><nested xmlns:b='foo' a:x='c' b:x='d'/>"
  167. "</testing>");
  168. EXPECT_TRUE(NULL == builder.BuiltElement());
  169. }