| OLD | NEW |
| (Empty) |
| 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/xmlelement.h" | |
| 15 #include "webrtc/libjingle/xmpp/xmppstanzaparser.h" | |
| 16 #include "webrtc/base/common.h" | |
| 17 #include "webrtc/base/gunit.h" | |
| 18 | |
| 19 using buzz::QName; | |
| 20 using buzz::XmlElement; | |
| 21 using buzz::XmppStanzaParser; | |
| 22 using buzz::XmppStanzaParseHandler; | |
| 23 | |
| 24 class XmppStanzaParserTestHandler : public XmppStanzaParseHandler { | |
| 25 public: | |
| 26 virtual void StartStream(const XmlElement * element) { | |
| 27 ss_ << "START" << element->Str(); | |
| 28 } | |
| 29 virtual void Stanza(const XmlElement * element) { | |
| 30 ss_ << "STANZA" << element->Str(); | |
| 31 } | |
| 32 virtual void EndStream() { | |
| 33 ss_ << "END"; | |
| 34 } | |
| 35 virtual void XmlError() { | |
| 36 ss_ << "ERROR"; | |
| 37 } | |
| 38 | |
| 39 std::string Str() { | |
| 40 return ss_.str(); | |
| 41 } | |
| 42 | |
| 43 std::string StrClear() { | |
| 44 std::string result = ss_.str(); | |
| 45 ss_.str(""); | |
| 46 return result; | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 std::stringstream ss_; | |
| 51 }; | |
| 52 | |
| 53 | |
| 54 TEST(XmppStanzaParserTest, TestTrivial) { | |
| 55 XmppStanzaParserTestHandler handler; | |
| 56 XmppStanzaParser parser(&handler); | |
| 57 std::string fragment; | |
| 58 | |
| 59 fragment = "<trivial/>"; | |
| 60 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 61 EXPECT_EQ("START<trivial/>END", handler.StrClear()); | |
| 62 } | |
| 63 | |
| 64 TEST(XmppStanzaParserTest, TestStanzaAtATime) { | |
| 65 XmppStanzaParserTestHandler handler; | |
| 66 XmppStanzaParser parser(&handler); | |
| 67 std::string fragment; | |
| 68 | |
| 69 fragment = "<stream:stream id='abc' xmlns='j:c' xmlns:stream='str'>"; | |
| 70 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 71 EXPECT_EQ("START<stream:stream id=\"abc\" xmlns=\"j:c\" " | |
| 72 "xmlns:stream=\"str\"/>", handler.StrClear()); | |
| 73 | |
| 74 fragment = "<message type='foo'><body>hello</body></message>"; | |
| 75 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 76 EXPECT_EQ("STANZA<c:message type=\"foo\" xmlns:c=\"j:c\">" | |
| 77 "<c:body>hello</c:body></c:message>", handler.StrClear()); | |
| 78 | |
| 79 fragment = " SOME TEXT TO IGNORE "; | |
| 80 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 81 EXPECT_EQ("", handler.StrClear()); | |
| 82 | |
| 83 fragment = "<iq type='set' id='123'><abc xmlns='def'/></iq>"; | |
| 84 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 85 EXPECT_EQ("STANZA<c:iq type=\"set\" id=\"123\" xmlns:c=\"j:c\">" | |
| 86 "<abc xmlns=\"def\"/></c:iq>", handler.StrClear()); | |
| 87 | |
| 88 fragment = "</stream:stream>"; | |
| 89 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 90 EXPECT_EQ("END", handler.StrClear()); | |
| 91 } | |
| 92 | |
| 93 TEST(XmppStanzaParserTest, TestFragmentedStanzas) { | |
| 94 XmppStanzaParserTestHandler handler; | |
| 95 XmppStanzaParser parser(&handler); | |
| 96 std::string fragment; | |
| 97 | |
| 98 fragment = "<stream:stream id='abc' xmlns='j:c' xml"; | |
| 99 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 100 EXPECT_EQ("", handler.StrClear()); | |
| 101 | |
| 102 fragment = "ns:stream='str'><message type='foo'><body>hel"; | |
| 103 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 104 EXPECT_EQ("START<stream:stream id=\"abc\" xmlns=\"j:c\" " | |
| 105 "xmlns:stream=\"str\"/>", handler.StrClear()); | |
| 106 | |
| 107 fragment = "lo</body></message> IGNORE ME <iq type='set' id='123'>" | |
| 108 "<abc xmlns='def'/></iq></st"; | |
| 109 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 110 EXPECT_EQ("STANZA<c:message type=\"foo\" xmlns:c=\"j:c\">" | |
| 111 "<c:body>hello</c:body></c:message>STANZA<c:iq type=\"set\" id=\"123\" " | |
| 112 "xmlns:c=\"j:c\"><abc xmlns=\"def\"/></c:iq>", handler.StrClear()); | |
| 113 | |
| 114 fragment = "ream:stream>"; | |
| 115 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 116 EXPECT_EQ("END", handler.StrClear()); | |
| 117 } | |
| 118 | |
| 119 TEST(XmppStanzaParserTest, TestReset) { | |
| 120 XmppStanzaParserTestHandler handler; | |
| 121 XmppStanzaParser parser(&handler); | |
| 122 std::string fragment; | |
| 123 | |
| 124 fragment = "<stream:stream id='abc' xmlns='j:c' xml"; | |
| 125 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 126 EXPECT_EQ("", handler.StrClear()); | |
| 127 | |
| 128 parser.Reset(); | |
| 129 fragment = "<stream:stream id='abc' xmlns='j:c' xml"; | |
| 130 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 131 EXPECT_EQ("", handler.StrClear()); | |
| 132 | |
| 133 fragment = "ns:stream='str'><message type='foo'><body>hel"; | |
| 134 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 135 EXPECT_EQ("START<stream:stream id=\"abc\" xmlns=\"j:c\" " | |
| 136 "xmlns:stream=\"str\"/>", handler.StrClear()); | |
| 137 parser.Reset(); | |
| 138 | |
| 139 fragment = "<stream:stream id='abc' xmlns='j:c' xmlns:stream='str'>"; | |
| 140 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 141 EXPECT_EQ("START<stream:stream id=\"abc\" xmlns=\"j:c\" " | |
| 142 "xmlns:stream=\"str\"/>", handler.StrClear()); | |
| 143 | |
| 144 fragment = "<message type='foo'><body>hello</body></message>"; | |
| 145 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 146 EXPECT_EQ("STANZA<c:message type=\"foo\" xmlns:c=\"j:c\">" | |
| 147 "<c:body>hello</c:body></c:message>", handler.StrClear()); | |
| 148 } | |
| 149 | |
| 150 TEST(XmppStanzaParserTest, TestError) { | |
| 151 XmppStanzaParserTestHandler handler; | |
| 152 XmppStanzaParser parser(&handler); | |
| 153 std::string fragment; | |
| 154 | |
| 155 fragment = "<-foobar/>"; | |
| 156 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 157 EXPECT_EQ("ERROR", handler.StrClear()); | |
| 158 | |
| 159 parser.Reset(); | |
| 160 fragment = "<stream:stream/>"; | |
| 161 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 162 EXPECT_EQ("ERROR", handler.StrClear()); | |
| 163 parser.Reset(); | |
| 164 | |
| 165 fragment = "ns:stream='str'><message type='foo'><body>hel"; | |
| 166 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 167 EXPECT_EQ("ERROR", handler.StrClear()); | |
| 168 parser.Reset(); | |
| 169 | |
| 170 fragment = "<stream:stream xmlns:stream='st' xmlns='jc'>" | |
| 171 "<foo/><bar><st:foobar/></bar>"; | |
| 172 parser.Parse(fragment.c_str(), fragment.length(), false); | |
| 173 EXPECT_EQ("START<stream:stream xmlns:stream=\"st\" xmlns=\"jc\"/>STANZA" | |
| 174 "<jc:foo xmlns:jc=\"jc\"/>ERROR", handler.StrClear()); | |
| 175 } | |
| OLD | NEW |