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