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 "webrtc/libjingle/xmllite/xmlnsstack.h" 12 13#include <iostream> 14#include <sstream> 15#include <string> 16 17#include "webrtc/libjingle/xmllite/xmlconstants.h" 18#include "webrtc/base/common.h" 19#include "webrtc/base/gunit.h" 20 21using buzz::NS_XML; 22using buzz::NS_XMLNS; 23using buzz::QName; 24using buzz::XmlnsStack; 25 26TEST(XmlnsStackTest, TestBuiltin) { 27 XmlnsStack stack; 28 29 EXPECT_EQ(std::string(NS_XML), stack.NsForPrefix("xml").first); 30 EXPECT_EQ(std::string(NS_XMLNS), stack.NsForPrefix("xmlns").first); 31 EXPECT_EQ("", stack.NsForPrefix("").first); 32 33 EXPECT_EQ("xml", stack.PrefixForNs(NS_XML, false).first); 34 EXPECT_EQ("xmlns", stack.PrefixForNs(NS_XMLNS, false).first); 35 EXPECT_EQ("", stack.PrefixForNs("", false).first); 36 EXPECT_EQ("", stack.PrefixForNs("", true).first); 37} 38 39TEST(XmlnsStackTest, TestNsForPrefix) { 40 XmlnsStack stack; 41 stack.AddXmlns("pre1", "ns1"); 42 stack.AddXmlns("pre2", "ns2"); 43 stack.AddXmlns("pre1", "ns3"); 44 stack.AddXmlns("", "ns4"); 45 46 EXPECT_EQ("ns3", stack.NsForPrefix("pre1").first); 47 EXPECT_TRUE(stack.NsForPrefix("pre1").second); 48 EXPECT_EQ("ns2", stack.NsForPrefix("pre2").first); 49 EXPECT_EQ("ns4", stack.NsForPrefix("").first); 50 EXPECT_EQ("", stack.NsForPrefix("pre3").first); 51 EXPECT_FALSE(stack.NsForPrefix("pre3").second); 52} 53 54TEST(XmlnsStackTest, TestPrefixForNs) { 55 XmlnsStack stack; 56 stack.AddXmlns("pre1", "ns1"); 57 stack.AddXmlns("pre2", "ns2"); 58 stack.AddXmlns("pre1", "ns3"); 59 stack.AddXmlns("pre3", "ns2"); 60 stack.AddXmlns("pre4", "ns4"); 61 stack.AddXmlns("", "ns4"); 62 63 EXPECT_EQ("", stack.PrefixForNs("ns1", false).first); 64 EXPECT_FALSE(stack.PrefixForNs("ns1", false).second); 65 EXPECT_EQ("", stack.PrefixForNs("ns1", true).first); 66 EXPECT_FALSE(stack.PrefixForNs("ns1", true).second); 67 EXPECT_EQ("pre3", stack.PrefixForNs("ns2", false).first); 68 EXPECT_TRUE(stack.PrefixForNs("ns2", false).second); 69 EXPECT_EQ("pre3", stack.PrefixForNs("ns2", true).first); 70 EXPECT_TRUE(stack.PrefixForNs("ns2", true).second); 71 EXPECT_EQ("pre1", stack.PrefixForNs("ns3", false).first); 72 EXPECT_EQ("pre1", stack.PrefixForNs("ns3", true).first); 73 EXPECT_EQ("", stack.PrefixForNs("ns4", false).first); 74 EXPECT_TRUE(stack.PrefixForNs("ns4", false).second); 75 EXPECT_EQ("pre4", stack.PrefixForNs("ns4", true).first); 76 EXPECT_EQ("", stack.PrefixForNs("ns5", false).first); 77 EXPECT_FALSE(stack.PrefixForNs("ns5", false).second); 78 EXPECT_EQ("", stack.PrefixForNs("ns5", true).first); 79 EXPECT_EQ("", stack.PrefixForNs("", false).first); 80 EXPECT_EQ("", stack.PrefixForNs("", true).first); 81 82 stack.AddXmlns("", "ns6"); 83 EXPECT_EQ("", stack.PrefixForNs("ns6", false).first); 84 EXPECT_TRUE(stack.PrefixForNs("ns6", false).second); 85 EXPECT_EQ("", stack.PrefixForNs("ns6", true).first); 86 EXPECT_FALSE(stack.PrefixForNs("ns6", true).second); 87} 88 89TEST(XmlnsStackTest, TestFrames) { 90 XmlnsStack stack; 91 stack.PushFrame(); 92 stack.AddXmlns("pre1", "ns1"); 93 stack.AddXmlns("pre2", "ns2"); 94 95 stack.PushFrame(); 96 stack.AddXmlns("pre1", "ns3"); 97 stack.AddXmlns("pre3", "ns2"); 98 stack.AddXmlns("pre4", "ns4"); 99 100 stack.PushFrame(); 101 stack.PushFrame(); 102 stack.AddXmlns("", "ns4"); 103 104 // basic test 105 EXPECT_EQ("ns3", stack.NsForPrefix("pre1").first); 106 EXPECT_EQ("ns2", stack.NsForPrefix("pre2").first); 107 EXPECT_EQ("ns2", stack.NsForPrefix("pre3").first); 108 EXPECT_EQ("ns4", stack.NsForPrefix("pre4").first); 109 EXPECT_EQ("", stack.NsForPrefix("pre5").first); 110 EXPECT_FALSE(stack.NsForPrefix("pre5").second); 111 EXPECT_EQ("ns4", stack.NsForPrefix("").first); 112 EXPECT_TRUE(stack.NsForPrefix("").second); 113 114 // pop the default xmlns definition 115 stack.PopFrame(); 116 EXPECT_EQ("ns3", stack.NsForPrefix("pre1").first); 117 EXPECT_EQ("ns2", stack.NsForPrefix("pre2").first); 118 EXPECT_EQ("ns2", stack.NsForPrefix("pre3").first); 119 EXPECT_EQ("ns4", stack.NsForPrefix("pre4").first); 120 EXPECT_EQ("", stack.NsForPrefix("pre5").first); 121 EXPECT_FALSE(stack.NsForPrefix("pre5").second); 122 EXPECT_EQ("", stack.NsForPrefix("").first); 123 EXPECT_TRUE(stack.NsForPrefix("").second); 124 125 // pop empty frame (nop) 126 stack.PopFrame(); 127 EXPECT_EQ("ns3", stack.NsForPrefix("pre1").first); 128 EXPECT_EQ("ns2", stack.NsForPrefix("pre2").first); 129 EXPECT_EQ("ns2", stack.NsForPrefix("pre3").first); 130 EXPECT_EQ("ns4", stack.NsForPrefix("pre4").first); 131 EXPECT_EQ("", stack.NsForPrefix("pre5").first); 132 EXPECT_FALSE(stack.NsForPrefix("pre5").second); 133 EXPECT_EQ("", stack.NsForPrefix("").first); 134 EXPECT_TRUE(stack.NsForPrefix("").second); 135 136 // pop frame with three defs 137 stack.PopFrame(); 138 EXPECT_EQ("ns1", stack.NsForPrefix("pre1").first); 139 EXPECT_EQ("ns2", stack.NsForPrefix("pre2").first); 140 EXPECT_EQ("", stack.NsForPrefix("pre3").first); 141 EXPECT_FALSE(stack.NsForPrefix("pre3").second); 142 EXPECT_EQ("", stack.NsForPrefix("pre4").first); 143 EXPECT_FALSE(stack.NsForPrefix("pre4").second); 144 EXPECT_EQ("", stack.NsForPrefix("pre5").first); 145 EXPECT_FALSE(stack.NsForPrefix("pre5").second); 146 EXPECT_EQ("", stack.NsForPrefix("").first); 147 EXPECT_TRUE(stack.NsForPrefix("").second); 148 149 // pop frame with last two defs 150 stack.PopFrame(); 151 EXPECT_FALSE(stack.NsForPrefix("pre1").second); 152 EXPECT_FALSE(stack.NsForPrefix("pre2").second); 153 EXPECT_FALSE(stack.NsForPrefix("pre3").second); 154 EXPECT_FALSE(stack.NsForPrefix("pre4").second); 155 EXPECT_FALSE(stack.NsForPrefix("pre5").second); 156 EXPECT_TRUE(stack.NsForPrefix("").second); 157 EXPECT_EQ("", stack.NsForPrefix("pre1").first); 158 EXPECT_EQ("", stack.NsForPrefix("pre2").first); 159 EXPECT_EQ("", stack.NsForPrefix("pre3").first); 160 EXPECT_EQ("", stack.NsForPrefix("pre4").first); 161 EXPECT_EQ("", stack.NsForPrefix("pre5").first); 162 EXPECT_EQ("", stack.NsForPrefix("").first); 163} 164 165TEST(XmlnsStackTest, TestAddNewPrefix) { 166 XmlnsStack stack; 167 168 // builtin namespaces cannot be added 169 EXPECT_FALSE(stack.AddNewPrefix("", true).second); 170 EXPECT_FALSE(stack.AddNewPrefix("", false).second); 171 EXPECT_FALSE(stack.AddNewPrefix(NS_XML, true).second); 172 EXPECT_FALSE(stack.AddNewPrefix(NS_XML, false).second); 173 EXPECT_FALSE(stack.AddNewPrefix(NS_XMLNS, true).second); 174 EXPECT_FALSE(stack.AddNewPrefix(NS_XMLNS, false).second); 175 176 // namespaces already added cannot be added again. 177 EXPECT_EQ("foo", stack.AddNewPrefix("http://a.b.com/foo.htm", true).first); 178 EXPECT_EQ("bare", stack.AddNewPrefix("http://a.b.com/bare", false).first); 179 EXPECT_EQ("z", stack.AddNewPrefix("z", false).first); 180 EXPECT_FALSE(stack.AddNewPrefix("http://a.b.com/foo.htm", true).second); 181 EXPECT_FALSE(stack.AddNewPrefix("http://a.b.com/bare", true).second); 182 EXPECT_FALSE(stack.AddNewPrefix("z", true).second); 183 EXPECT_FALSE(stack.AddNewPrefix("http://a.b.com/foo.htm", false).second); 184 EXPECT_FALSE(stack.AddNewPrefix("http://a.b.com/bare", false).second); 185 EXPECT_FALSE(stack.AddNewPrefix("z", false).second); 186 187 // default namespace usable by non-attributes only 188 stack.AddXmlns("", "http://my/default"); 189 EXPECT_FALSE(stack.AddNewPrefix("http://my/default", false).second); 190 EXPECT_EQ("def", stack.AddNewPrefix("http://my/default", true).first); 191 192 // namespace cannot start with 'xml' 193 EXPECT_EQ("ns", stack.AddNewPrefix("http://a.b.com/xmltest", true).first); 194 EXPECT_EQ("ns2", stack.AddNewPrefix("xmlagain", false).first); 195 196 // verify added namespaces are still defined 197 EXPECT_EQ("http://a.b.com/foo.htm", stack.NsForPrefix("foo").first); 198 EXPECT_TRUE(stack.NsForPrefix("foo").second); 199 EXPECT_EQ("http://a.b.com/bare", stack.NsForPrefix("bare").first); 200 EXPECT_TRUE(stack.NsForPrefix("bare").second); 201 EXPECT_EQ("z", stack.NsForPrefix("z").first); 202 EXPECT_TRUE(stack.NsForPrefix("z").second); 203 EXPECT_EQ("http://my/default", stack.NsForPrefix("").first); 204 EXPECT_TRUE(stack.NsForPrefix("").second); 205 EXPECT_EQ("http://my/default", stack.NsForPrefix("def").first); 206 EXPECT_TRUE(stack.NsForPrefix("def").second); 207 EXPECT_EQ("http://a.b.com/xmltest", stack.NsForPrefix("ns").first); 208 EXPECT_TRUE(stack.NsForPrefix("ns").second); 209 EXPECT_EQ("xmlagain", stack.NsForPrefix("ns2").first); 210 EXPECT_TRUE(stack.NsForPrefix("ns2").second); 211} 212 213TEST(XmlnsStackTest, TestFormatQName) { 214 XmlnsStack stack; 215 stack.AddXmlns("pre1", "ns1"); 216 stack.AddXmlns("pre2", "ns2"); 217 stack.AddXmlns("pre1", "ns3"); 218 stack.AddXmlns("", "ns4"); 219 220 EXPECT_EQ("zip", 221 stack.FormatQName(QName("ns1", "zip"), false)); // no match 222 EXPECT_EQ("pre2:abracadabra", 223 stack.FormatQName(QName("ns2", "abracadabra"), false)); 224 EXPECT_EQ("pre1:a", 225 stack.FormatQName(QName("ns3", "a"), false)); 226 EXPECT_EQ("simple", 227 stack.FormatQName(QName("ns4", "simple"), false)); 228 EXPECT_EQ("root", 229 stack.FormatQName(QName("", "root"), false)); // no match 230 231 EXPECT_EQ("zip", 232 stack.FormatQName(QName("ns1", "zip"), true)); // no match 233 EXPECT_EQ("pre2:abracadabra", 234 stack.FormatQName(QName("ns2", "abracadabra"), true)); 235 EXPECT_EQ("pre1:a", 236 stack.FormatQName(QName("ns3", "a"), true)); 237 EXPECT_EQ("simple", 238 stack.FormatQName(QName("ns4", "simple"), true)); // no match 239 EXPECT_EQ("root", 240 stack.FormatQName(QName("", "root"), true)); 241} 242