/* * libjingle * Copyright 2004, Google Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include "talk/base/common.h" #include "talk/base/gunit.h" #include "talk/xmllite/qname.h" #include "talk/xmllite/xmlparser.h" using buzz::QName; using buzz::XmlParser; using buzz::XmlParseContext; using buzz::XmlParseHandler; class XmlParserTestHandler : public XmlParseHandler { public: virtual void StartElement(XmlParseContext * pctx, const char * name, const char ** atts) { ss_ << "START (" << pctx->ResolveQName(name, false).Merged(); while (*atts) { ss_ << ", " << pctx->ResolveQName(*atts, true).Merged() << "='" << *(atts+1) << "'"; atts += 2; } ss_ << ") "; } virtual void EndElement(XmlParseContext * pctx, const char * name) { UNUSED(pctx); UNUSED(name); ss_ << "END "; } virtual void CharacterData(XmlParseContext * pctx, const char * text, int len) { UNUSED(pctx); ss_ << "TEXT (" << std::string(text, len) << ") "; } virtual void Error(XmlParseContext * pctx, XML_Error code) { UNUSED(pctx); ss_ << "ERROR (" << static_cast(code) << ") "; } virtual ~XmlParserTestHandler() { } std::string Str() { return ss_.str(); } std::string StrClear() { std::string result = ss_.str(); ss_.str(""); return result; } private: std::stringstream ss_; }; TEST(XmlParserTest, TestTrivial) { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, ""); EXPECT_EQ("START (testing) END ", handler.Str()); } TEST(XmlParserTest, TestAttributes) { { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, ""); EXPECT_EQ("START (testing, a='b') END ", handler.Str()); } { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, ""); EXPECT_EQ("START (testing, e='', long='some text') END ", handler.Str()); } } TEST(XmlParserTest, TestNesting) { { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, ""); EXPECT_EQ("START (top) START (first) END START (second) START (third) " "END END END ", handler.Str()); } { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, "" "" ""); EXPECT_EQ("START (top) START (fifth) START (deeper) START (and) START " "(deeper) END END START (sibling) START (leaf) END END END " "END START (first) END START (second) START (third) END END END ", handler.Str()); } } TEST(XmlParserTest, TestXmlDecl) { { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, ""); EXPECT_EQ("START (testing) END ", handler.Str()); } { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, ""); EXPECT_EQ("START (testing) END ", handler.Str()); } { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, "" ""); EXPECT_EQ("START (testing) END ", handler.Str()); } } TEST(XmlParserTest, TestNamespace) { { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, ""); EXPECT_EQ("START (my-namespace:top, xmlns='my-namespace', a='b') END ", handler.Str()); } { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, ""); EXPECT_EQ("START (my-namespace:top, " "http://www.w3.org/2000/xmlns/:foo='my-namespace', " "a='b', my-namespace:c='d') END ", handler.Str()); } { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, "" ""); EXPECT_EQ("START (top) START (my-namespace:nested, xmlns='my-namespace') " "START (my-namespace:leaf) END END START (sibling) END END ", handler.Str()); } } TEST(XmlParserTest, TestIncremental) { XmlParserTestHandler handler; XmlParser parser(&handler); std::string fragment; fragment = " garbage "); EXPECT_EQ("START (top) END ERROR (9) ", handler.Str()); } { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, "<-hm->"); EXPECT_EQ("ERROR (4) ", handler.Str()); } { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, "&foobar;"); EXPECT_EQ("START (hello) ERROR (11) ", handler.Str()); } { XmlParserTestHandler handler; XmlParser::ParseXml(&handler, ""); EXPECT_EQ("ERROR (3) ", handler.Str()); } { // XmlParser requires utf-8 XmlParserTestHandler handler; XmlParser::ParseXml(&handler, ""); EXPECT_EQ("ERROR (19) ", handler.Str()); } { // XmlParser requires version 1.0 XmlParserTestHandler handler; XmlParser::ParseXml(&handler, ""); EXPECT_EQ("ERROR (2) ", handler.Str()); } { // XmlParser requires standalone documents XmlParserTestHandler handler; XmlParser::ParseXml(&handler, ""); EXPECT_EQ("ERROR (2) ", handler.Str()); } { // XmlParser doesn't like empty namespace URIs XmlParserTestHandler handler; XmlParser::ParseXml(&handler, ""); EXPECT_EQ("ERROR (2) ", handler.Str()); } }