henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
| 28 | #include <string> |
| 29 | #include <sstream> |
| 30 | #include <iostream> |
| 31 | #include "talk/base/common.h" |
| 32 | #include "talk/base/gunit.h" |
| 33 | #include "talk/xmllite/xmlbuilder.h" |
| 34 | #include "talk/xmllite/xmlelement.h" |
| 35 | #include "talk/xmllite/xmlparser.h" |
| 36 | |
| 37 | using buzz::XmlBuilder; |
| 38 | using buzz::XmlElement; |
| 39 | using buzz::XmlParser; |
| 40 | |
| 41 | TEST(XmlBuilderTest, TestTrivial) { |
| 42 | XmlBuilder builder; |
| 43 | XmlParser::ParseXml(&builder, "<testing/>"); |
| 44 | EXPECT_EQ("<testing/>", builder.BuiltElement()->Str()); |
| 45 | } |
| 46 | |
| 47 | TEST(XmlBuilderTest, TestAttributes1) { |
| 48 | XmlBuilder builder; |
| 49 | XmlParser::ParseXml(&builder, "<testing a='b'/>"); |
| 50 | EXPECT_EQ("<testing a=\"b\"/>", builder.BuiltElement()->Str()); |
| 51 | } |
| 52 | |
| 53 | TEST(XmlBuilderTest, TestAttributes2) { |
| 54 | XmlBuilder builder; |
| 55 | XmlParser::ParseXml(&builder, "<testing e='' long='some text'/>"); |
| 56 | EXPECT_EQ("<testing e=\"\" long=\"some text\"/>", |
| 57 | builder.BuiltElement()->Str()); |
| 58 | } |
| 59 | |
| 60 | TEST(XmlBuilderTest, TestNesting1) { |
| 61 | XmlBuilder builder; |
| 62 | XmlParser::ParseXml(&builder, |
| 63 | "<top><first/><second><third></third></second></top>"); |
| 64 | EXPECT_EQ("<top><first/><second><third/></second></top>", |
| 65 | builder.BuiltElement()->Str()); |
| 66 | } |
| 67 | |
| 68 | TEST(XmlBuilderTest, TestNesting2) { |
| 69 | XmlBuilder builder; |
| 70 | XmlParser::ParseXml(&builder, |
| 71 | "<top><fifth><deeper><and><deeper/></and><sibling><leaf/>" |
| 72 | "</sibling></deeper></fifth><first/><second><third></third>" |
| 73 | "</second></top>"); |
| 74 | EXPECT_EQ("<top><fifth><deeper><and><deeper/></and><sibling><leaf/>" |
| 75 | "</sibling></deeper></fifth><first/><second><third/>" |
| 76 | "</second></top>", builder.BuiltElement()->Str()); |
| 77 | } |
| 78 | |
| 79 | TEST(XmlBuilderTest, TestQuoting1) { |
| 80 | XmlBuilder builder; |
| 81 | XmlParser::ParseXml(&builder, "<testing a='>'/>"); |
| 82 | EXPECT_EQ("<testing a=\">\"/>", builder.BuiltElement()->Str()); |
| 83 | } |
| 84 | |
| 85 | TEST(XmlBuilderTest, TestQuoting2) { |
| 86 | XmlBuilder builder; |
| 87 | XmlParser::ParseXml(&builder, "<testing a='<>&"'/>"); |
| 88 | EXPECT_EQ("<testing a=\"<>&"\"/>", |
| 89 | builder.BuiltElement()->Str()); |
| 90 | } |
| 91 | |
| 92 | TEST(XmlBuilderTest, TestQuoting3) { |
| 93 | XmlBuilder builder; |
| 94 | XmlParser::ParseXml(&builder, "<testing a='so "important"'/>"); |
| 95 | EXPECT_EQ("<testing a=\"so "important"\"/>", |
| 96 | builder.BuiltElement()->Str()); |
| 97 | } |
| 98 | |
| 99 | TEST(XmlBuilderTest, TestQuoting4) { |
| 100 | XmlBuilder builder; |
| 101 | XmlParser::ParseXml(&builder, "<testing a='"important", yes'/>"); |
| 102 | EXPECT_EQ("<testing a=\""important", yes\"/>", |
| 103 | builder.BuiltElement()->Str()); |
| 104 | } |
| 105 | |
| 106 | TEST(XmlBuilderTest, TestQuoting5) { |
| 107 | XmlBuilder builder; |
| 108 | XmlParser::ParseXml(&builder, |
| 109 | "<testing a='<what is "important">'/>"); |
| 110 | EXPECT_EQ("<testing a=\"<what is "important">\"/>", |
| 111 | builder.BuiltElement()->Str()); |
| 112 | } |
| 113 | |
| 114 | TEST(XmlBuilderTest, TestText1) { |
| 115 | XmlBuilder builder; |
| 116 | XmlParser::ParseXml(&builder, "<testing>></testing>"); |
| 117 | EXPECT_EQ("<testing>></testing>", builder.BuiltElement()->Str()); |
| 118 | } |
| 119 | |
| 120 | TEST(XmlBuilderTest, TestText2) { |
| 121 | XmlBuilder builder; |
| 122 | XmlParser::ParseXml(&builder, "<testing><>&"</testing>"); |
| 123 | EXPECT_EQ("<testing><>&\"</testing>", |
| 124 | builder.BuiltElement()->Str()); |
| 125 | } |
| 126 | |
| 127 | TEST(XmlBuilderTest, TestText3) { |
| 128 | XmlBuilder builder; |
| 129 | XmlParser::ParseXml(&builder, "<testing>so <important></testing>"); |
| 130 | EXPECT_EQ("<testing>so <important></testing>", |
| 131 | builder.BuiltElement()->Str()); |
| 132 | } |
| 133 | |
| 134 | TEST(XmlBuilderTest, TestText4) { |
| 135 | XmlBuilder builder; |
| 136 | XmlParser::ParseXml(&builder, "<testing><important>, yes</testing>"); |
| 137 | EXPECT_EQ("<testing><important>, yes</testing>", |
| 138 | builder.BuiltElement()->Str()); |
| 139 | } |
| 140 | |
| 141 | TEST(XmlBuilderTest, TestText5) { |
| 142 | XmlBuilder builder; |
| 143 | XmlParser::ParseXml(&builder, |
| 144 | "<testing>importance &<important>&</testing>"); |
| 145 | EXPECT_EQ("<testing>importance &<important>&</testing>", |
| 146 | builder.BuiltElement()->Str()); |
| 147 | } |
| 148 | |
| 149 | TEST(XmlBuilderTest, TestNamespace1) { |
| 150 | XmlBuilder builder; |
| 151 | XmlParser::ParseXml(&builder, "<testing xmlns='foo'/>"); |
| 152 | EXPECT_EQ("<testing xmlns=\"foo\"/>", builder.BuiltElement()->Str()); |
| 153 | } |
| 154 | |
| 155 | TEST(XmlBuilderTest, TestNamespace2) { |
| 156 | XmlBuilder builder; |
| 157 | XmlParser::ParseXml(&builder, "<testing xmlns:a='foo' a:b='c'/>"); |
| 158 | EXPECT_EQ("<testing xmlns:a=\"foo\" a:b=\"c\"/>", |
| 159 | builder.BuiltElement()->Str()); |
| 160 | } |
| 161 | |
| 162 | TEST(XmlBuilderTest, TestNamespace3) { |
| 163 | XmlBuilder builder; |
| 164 | XmlParser::ParseXml(&builder, "<testing xmlns:a=''/>"); |
| 165 | EXPECT_TRUE(NULL == builder.BuiltElement()); |
| 166 | } |
| 167 | |
| 168 | TEST(XmlBuilderTest, TestNamespace4) { |
| 169 | XmlBuilder builder; |
| 170 | XmlParser::ParseXml(&builder, "<testing a:b='c'/>"); |
| 171 | EXPECT_TRUE(NULL == builder.BuiltElement()); |
| 172 | } |
| 173 | |
| 174 | TEST(XmlBuilderTest, TestAttrCollision1) { |
| 175 | XmlBuilder builder; |
| 176 | XmlParser::ParseXml(&builder, "<testing a='first' a='second'/>"); |
| 177 | EXPECT_TRUE(NULL == builder.BuiltElement()); |
| 178 | } |
| 179 | |
| 180 | TEST(XmlBuilderTest, TestAttrCollision2) { |
| 181 | XmlBuilder builder; |
| 182 | XmlParser::ParseXml(&builder, |
| 183 | "<testing xmlns:a='foo' xmlns:b='foo' a:x='c' b:x='d'/>"); |
| 184 | EXPECT_TRUE(NULL == builder.BuiltElement()); |
| 185 | } |
| 186 | |
| 187 | TEST(XmlBuilderTest, TestAttrCollision3) { |
| 188 | XmlBuilder builder; |
| 189 | XmlParser::ParseXml(&builder, |
| 190 | "<testing xmlns:a='foo'><nested xmlns:b='foo' a:x='c' b:x='d'/>" |
| 191 | "</testing>"); |
| 192 | EXPECT_TRUE(NULL == builder.BuiltElement()); |
| 193 | } |
| 194 | |