// Copyright 2004 Google Inc. All Rights Reserved #include #include #include #include "webrtc/libjingle/xmllite/xmlelement.h" #include "webrtc/libjingle/xmpp/xmppstanzaparser.h" #include "webrtc/base/common.h" #include "webrtc/base/gunit.h" using buzz::QName; using buzz::XmlElement; using buzz::XmppStanzaParser; using buzz::XmppStanzaParseHandler; class XmppStanzaParserTestHandler : public XmppStanzaParseHandler { public: virtual void StartStream(const XmlElement * element) { ss_ << "START" << element->Str(); } virtual void Stanza(const XmlElement * element) { ss_ << "STANZA" << element->Str(); } virtual void EndStream() { ss_ << "END"; } virtual void XmlError() { ss_ << "ERROR"; } std::string Str() { return ss_.str(); } std::string StrClear() { std::string result = ss_.str(); ss_.str(""); return result; } private: std::stringstream ss_; }; TEST(XmppStanzaParserTest, TestTrivial) { XmppStanzaParserTestHandler handler; XmppStanzaParser parser(&handler); std::string fragment; fragment = ""; parser.Parse(fragment.c_str(), fragment.length(), false); EXPECT_EQ("STARTEND", handler.StrClear()); } TEST(XmppStanzaParserTest, TestStanzaAtATime) { XmppStanzaParserTestHandler handler; XmppStanzaParser parser(&handler); std::string fragment; fragment = ""; parser.Parse(fragment.c_str(), fragment.length(), false); EXPECT_EQ("START", handler.StrClear()); fragment = "hello"; parser.Parse(fragment.c_str(), fragment.length(), false); EXPECT_EQ("STANZA" "hello", handler.StrClear()); fragment = " SOME TEXT TO IGNORE "; parser.Parse(fragment.c_str(), fragment.length(), false); EXPECT_EQ("", handler.StrClear()); fragment = ""; parser.Parse(fragment.c_str(), fragment.length(), false); EXPECT_EQ("STANZA" "", handler.StrClear()); fragment = ""; parser.Parse(fragment.c_str(), fragment.length(), false); EXPECT_EQ("END", handler.StrClear()); } TEST(XmppStanzaParserTest, TestFragmentedStanzas) { XmppStanzaParserTestHandler handler; XmppStanzaParser parser(&handler); std::string fragment; fragment = "", handler.StrClear()); fragment = "lo IGNORE ME " "" "helloSTANZA", handler.StrClear()); fragment = "ream:stream>"; parser.Parse(fragment.c_str(), fragment.length(), false); EXPECT_EQ("END", handler.StrClear()); } TEST(XmppStanzaParserTest, TestReset) { XmppStanzaParserTestHandler handler; XmppStanzaParser parser(&handler); std::string fragment; fragment = "", handler.StrClear()); parser.Reset(); fragment = ""; parser.Parse(fragment.c_str(), fragment.length(), false); EXPECT_EQ("START", handler.StrClear()); fragment = "hello"; parser.Parse(fragment.c_str(), fragment.length(), false); EXPECT_EQ("STANZA" "hello", handler.StrClear()); } TEST(XmppStanzaParserTest, TestError) { XmppStanzaParserTestHandler handler; XmppStanzaParser parser(&handler); std::string fragment; fragment = "<-foobar/>"; parser.Parse(fragment.c_str(), fragment.length(), false); EXPECT_EQ("ERROR", handler.StrClear()); parser.Reset(); fragment = ""; parser.Parse(fragment.c_str(), fragment.length(), false); EXPECT_EQ("ERROR", handler.StrClear()); parser.Reset(); fragment = "ns:stream='str'>hel"; parser.Parse(fragment.c_str(), fragment.length(), false); EXPECT_EQ("ERROR", handler.StrClear()); parser.Reset(); fragment = "" ""; parser.Parse(fragment.c_str(), fragment.length(), false); EXPECT_EQ("STARTSTANZA" "ERROR", handler.StrClear()); }