1 // Copyright 2015 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.
5 #include "remoting/signaling/xmpp_stream_parser.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
16 class XmppStreamParserTest
: public testing::Test
{
18 XmppStreamParserTest() : error_(false) {}
20 void SetUp() override
{
21 parser_
.reset(new remoting::XmppStreamParser());
22 parser_
->SetCallbacks(
23 base::Bind(&XmppStreamParserTest::OnStanza
, base::Unretained(this)),
24 base::Bind(&XmppStreamParserTest::OnError
, base::Unretained(this)));
27 void TearDown() override
{
29 base::RunLoop().RunUntilIdle();
32 void OnStanza(scoped_ptr
<buzz::XmlElement
> stanza
) {
33 received_stanzas_
.push_back(stanza
.Pass());
41 base::MessageLoop message_loop_
;
43 scoped_ptr
<XmppStreamParser
> parser_
;
44 ScopedVector
<buzz::XmlElement
> received_stanzas_
;
48 TEST_F(XmppStreamParserTest
, ParseXmppStream
) {
49 parser_
->AppendData("<stream><iq>text</iq>");
50 EXPECT_EQ(received_stanzas_
[0]->Str(), "<iq>text</iq>");
53 TEST_F(XmppStreamParserTest
, HandleMultipleIncomingStanzas
) {
54 parser_
->AppendData("<stream><iq>text</iq><iq>more text</iq>");
55 EXPECT_EQ(received_stanzas_
[0]->Str(), "<iq>text</iq>");
56 EXPECT_EQ(received_stanzas_
[1]->Str(), "<iq>more text</iq>");
59 TEST_F(XmppStreamParserTest
, IgnoreWhitespaceBetweenStanzas
) {
60 parser_
->AppendData("<stream> <iq>text</iq>");
61 EXPECT_EQ(received_stanzas_
[0]->Str(), "<iq>text</iq>");
64 TEST_F(XmppStreamParserTest
, AssembleMessagesFromChunks
) {
65 parser_
->AppendData("<stream><i");
66 parser_
->AppendData("q>");
68 // Split one UTF-8 sequence into two chunks
69 std::string data
= "😃";
70 parser_
->AppendData(data
.substr(0, 2));
71 parser_
->AppendData(data
.substr(2));
73 parser_
->AppendData("</iq>");
75 EXPECT_EQ(received_stanzas_
[0]->Str(), "<iq>😃</iq>");
78 TEST_F(XmppStreamParserTest
, StopParsingOnErrors
) {
79 parser_
->AppendData("<stream><invalidtag p!='a'></invalidtag><iq>text</iq>");
81 EXPECT_TRUE(received_stanzas_
.empty());
84 TEST_F(XmppStreamParserTest
, FailOnInvalidStreamHeader
) {
85 parser_
->AppendData("<stream p!='a'>");
89 TEST_F(XmppStreamParserTest
, FailOnLooseText
) {
90 parser_
->AppendData("stream<");
94 } // namespace remoting