summaryrefslogtreecommitdiffstats
path: root/remoting/signaling/xmpp_stream_parser_unittest.cc
blob: a76e02c013a91e02b73c0a539b2f4339b0aa8b49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// 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<buzz::XmlElement> stanza) {
    received_stanzas_.push_back(stanza.Pass());
  }

  void OnError() {
    error_ = true;
  }

 protected:
  base::MessageLoop message_loop_;

  scoped_ptr<XmppStreamParser> parser_;
  ScopedVector<buzz::XmlElement> received_stanzas_;
  bool error_;
};

TEST_F(XmppStreamParserTest, ParseXmppStream) {
  parser_->AppendData("<stream><iq>text</iq>");
  EXPECT_EQ(received_stanzas_[0]->Str(), "<iq>text</iq>");
};

TEST_F(XmppStreamParserTest, HandleMultipleIncomingStanzas) {
  parser_->AppendData("<stream><iq>text</iq><iq>more text</iq>");
  EXPECT_EQ(received_stanzas_[0]->Str(), "<iq>text</iq>");
  EXPECT_EQ(received_stanzas_[1]->Str(), "<iq>more text</iq>");
};

TEST_F(XmppStreamParserTest, IgnoreWhitespaceBetweenStanzas) {
  parser_->AppendData("<stream> <iq>text</iq>");
  EXPECT_EQ(received_stanzas_[0]->Str(), "<iq>text</iq>");
};

TEST_F(XmppStreamParserTest, AssembleMessagesFromChunks) {
  parser_->AppendData("<stream><i");
  parser_->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("</iq>");

  EXPECT_EQ(received_stanzas_[0]->Str(), "<iq>😃</iq>");
};

TEST_F(XmppStreamParserTest, StopParsingOnErrors) {
  parser_->AppendData("<stream><invalidtag p!='a'></invalidtag><iq>text</iq>");
  EXPECT_TRUE(error_);
  EXPECT_TRUE(received_stanzas_.empty());
};

TEST_F(XmppStreamParserTest, FailOnInvalidStreamHeader) {
  parser_->AppendData("<stream p!='a'>");
  EXPECT_TRUE(error_);
};

TEST_F(XmppStreamParserTest, FailOnLooseText) {
  parser_->AppendData("stream<");
  EXPECT_TRUE(error_);
};

}  // namespace remoting