// Copyright 2015 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "remoting/signaling/xmpp_stream_parser.h" #include "base/bind.h" #include "base/memory/scoped_vector.h" #include "base/message_loop/message_loop.h" #include "base/run_loop.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" namespace remoting { class XmppStreamParserTest : public testing::Test { public: XmppStreamParserTest() : error_(false) {} void SetUp() override { parser_.reset(new remoting::XmppStreamParser()); parser_->SetCallbacks( base::Bind(&XmppStreamParserTest::OnStanza, base::Unretained(this)), base::Bind(&XmppStreamParserTest::OnError, base::Unretained(this))); } void TearDown() override { parser_.reset(); base::RunLoop().RunUntilIdle(); } void OnStanza(scoped_ptr stanza) { received_stanzas_.push_back(stanza.Pass()); } void OnError() { error_ = true; } protected: base::MessageLoop message_loop_; scoped_ptr parser_; ScopedVector received_stanzas_; bool error_; }; TEST_F(XmppStreamParserTest, ParseXmppStream) { parser_->AppendData("text"); EXPECT_EQ(received_stanzas_[0]->Str(), "text"); }; TEST_F(XmppStreamParserTest, HandleMultipleIncomingStanzas) { parser_->AppendData("textmore text"); EXPECT_EQ(received_stanzas_[0]->Str(), "text"); EXPECT_EQ(received_stanzas_[1]->Str(), "more text"); }; TEST_F(XmppStreamParserTest, IgnoreWhitespaceBetweenStanzas) { parser_->AppendData(" text"); EXPECT_EQ(received_stanzas_[0]->Str(), "text"); }; TEST_F(XmppStreamParserTest, AssembleMessagesFromChunks) { parser_->AppendData("AppendData("q>"); // Split one UTF-8 sequence into two chunks std::string data = "😃"; parser_->AppendData(data.substr(0, 2)); parser_->AppendData(data.substr(2)); parser_->AppendData(""); EXPECT_EQ(received_stanzas_[0]->Str(), "😃"); }; TEST_F(XmppStreamParserTest, StopParsingOnErrors) { parser_->AppendData("text"); EXPECT_TRUE(error_); EXPECT_TRUE(received_stanzas_.empty()); }; TEST_F(XmppStreamParserTest, FailOnInvalidStreamHeader) { parser_->AppendData(""); EXPECT_TRUE(error_); }; TEST_F(XmppStreamParserTest, FailOnLooseText) { parser_->AppendData("stream<"); EXPECT_TRUE(error_); }; } // namespace remoting