1 // Copyright (c) 2012 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/host/heartbeat_sender.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "remoting/base/constants.h"
15 #include "remoting/base/rsa_key_pair.h"
16 #include "remoting/base/test_rsa_key_pair.h"
17 #include "remoting/signaling/iq_sender.h"
18 #include "remoting/signaling/mock_signal_strategy.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/libjingle/source/talk/xmpp/constants.h"
22 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
25 using buzz::XmlElement;
28 using testing::DeleteArg;
30 using testing::Invoke;
31 using testing::NotNull;
32 using testing::Return;
33 using testing::SaveArg;
39 const char kTestBotJid[] = "remotingunittest@bot.talk.google.com";
40 const char kHostId[] = "0";
41 const char kTestJid[] = "user@gmail.com/chromoting123";
42 const char kStanzaId[] = "123";
44 class MockListener : public HeartbeatSender::Listener {
46 // Overridden from HeartbeatSender::Listener
47 virtual void OnUnknownHostIdError() OVERRIDE {
51 // Overridden from HeartbeatSender::Listener
52 MOCK_METHOD0(OnHeartbeatSuccessful, void());
57 ACTION_P(AddListener, list) {
60 ACTION_P(RemoveListener, list) {
61 EXPECT_TRUE(list->find(arg0) != list->end());
65 class HeartbeatSenderTest
66 : public testing::Test {
68 virtual void SetUp() OVERRIDE {
69 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair);
70 ASSERT_TRUE(key_pair_.get());
72 EXPECT_CALL(signal_strategy_, GetState())
73 .WillOnce(Return(SignalStrategy::DISCONNECTED));
74 EXPECT_CALL(signal_strategy_, AddListener(NotNull()))
75 .WillRepeatedly(AddListener(&signal_strategy_listeners_));
76 EXPECT_CALL(signal_strategy_, RemoveListener(NotNull()))
77 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_));
78 EXPECT_CALL(signal_strategy_, GetLocalJid())
79 .WillRepeatedly(Return(kTestJid));
81 heartbeat_sender_.reset(new HeartbeatSender(
82 &mock_listener_, kHostId, &signal_strategy_, key_pair_, kTestBotJid));
85 virtual void TearDown() OVERRIDE {
86 heartbeat_sender_.reset();
87 EXPECT_TRUE(signal_strategy_listeners_.empty());
90 void ValidateHeartbeatStanza(XmlElement* stanza,
91 const char* expectedSequenceId);
93 base::MessageLoop message_loop_;
94 MockSignalStrategy signal_strategy_;
95 MockListener mock_listener_;
96 std::set<SignalStrategy::Listener*> signal_strategy_listeners_;
97 scoped_refptr<RsaKeyPair> key_pair_;
98 scoped_ptr<HeartbeatSender> heartbeat_sender_;
101 // Call Start() followed by Stop(), and make sure a valid heartbeat is sent.
102 TEST_F(HeartbeatSenderTest, DoSendStanza) {
103 XmlElement* sent_iq = NULL;
104 EXPECT_CALL(signal_strategy_, GetLocalJid())
105 .WillRepeatedly(Return(kTestJid));
106 EXPECT_CALL(signal_strategy_, GetNextId())
107 .WillOnce(Return(kStanzaId));
108 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull()))
109 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true)));
111 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED);
112 base::RunLoop().RunUntilIdle();
114 scoped_ptr<XmlElement> stanza(sent_iq);
115 ASSERT_TRUE(stanza != NULL);
116 ValidateHeartbeatStanza(stanza.get(), "0");
118 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED);
119 base::RunLoop().RunUntilIdle();
122 // Call Start() followed by Stop(), twice, and make sure two valid heartbeats
123 // are sent, with the correct sequence IDs.
124 TEST_F(HeartbeatSenderTest, DoSendStanzaTwice) {
125 XmlElement* sent_iq = NULL;
126 EXPECT_CALL(signal_strategy_, GetLocalJid())
127 .WillRepeatedly(Return(kTestJid));
128 EXPECT_CALL(signal_strategy_, GetNextId())
129 .WillOnce(Return(kStanzaId));
130 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull()))
131 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true)));
133 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED);
134 base::RunLoop().RunUntilIdle();
136 scoped_ptr<XmlElement> stanza(sent_iq);
137 ASSERT_TRUE(stanza != NULL);
138 ValidateHeartbeatStanza(stanza.get(), "0");
140 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED);
141 base::RunLoop().RunUntilIdle();
143 EXPECT_CALL(signal_strategy_, GetLocalJid())
144 .WillRepeatedly(Return(kTestJid));
145 EXPECT_CALL(signal_strategy_, GetNextId())
146 .WillOnce(Return(kStanzaId + 1));
147 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull()))
148 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true)));
150 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED);
151 base::RunLoop().RunUntilIdle();
153 scoped_ptr<XmlElement> stanza2(sent_iq);
154 ValidateHeartbeatStanza(stanza2.get(), "1");
156 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED);
157 base::RunLoop().RunUntilIdle();
160 // Call Start() followed by Stop(), make sure a valid Iq stanza is sent,
161 // reply with an expected sequence ID, and make sure two valid heartbeats
162 // are sent, with the correct sequence IDs.
163 TEST_F(HeartbeatSenderTest, DoSendStanzaWithExpectedSequenceId) {
164 XmlElement* sent_iq = NULL;
165 EXPECT_CALL(signal_strategy_, GetLocalJid())
166 .WillRepeatedly(Return(kTestJid));
167 EXPECT_CALL(signal_strategy_, GetNextId())
168 .WillOnce(Return(kStanzaId));
169 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull()))
170 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true)));
172 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED);
173 base::RunLoop().RunUntilIdle();
175 scoped_ptr<XmlElement> stanza(sent_iq);
176 ASSERT_TRUE(stanza != NULL);
177 ValidateHeartbeatStanza(stanza.get(), "0");
179 XmlElement* sent_iq2 = NULL;
180 EXPECT_CALL(signal_strategy_, GetLocalJid())
181 .WillRepeatedly(Return(kTestJid));
182 EXPECT_CALL(signal_strategy_, GetNextId())
183 .WillOnce(Return(kStanzaId + 1));
184 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull()))
185 .WillOnce(DoAll(SaveArg<0>(&sent_iq2), Return(true)));
186 EXPECT_CALL(mock_listener_, OnHeartbeatSuccessful());
188 scoped_ptr<XmlElement> response(new XmlElement(buzz::QN_IQ));
189 response->AddAttr(QName(std::string(), "type"), "result");
191 new XmlElement(QName(kChromotingXmlNamespace, "heartbeat-result"));
192 response->AddElement(result);
193 XmlElement* expected_sequence_id = new XmlElement(
194 QName(kChromotingXmlNamespace, "expected-sequence-id"));
195 result->AddElement(expected_sequence_id);
196 const int kExpectedSequenceId = 456;
197 expected_sequence_id->AddText(base::IntToString(kExpectedSequenceId));
198 heartbeat_sender_->ProcessResponse(NULL, response.get());
199 base::RunLoop().RunUntilIdle();
201 scoped_ptr<XmlElement> stanza2(sent_iq2);
202 ASSERT_TRUE(stanza2 != NULL);
203 ValidateHeartbeatStanza(stanza2.get(),
204 base::IntToString(kExpectedSequenceId).c_str());
206 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED);
207 base::RunLoop().RunUntilIdle();
210 // Verify that ProcessResponse parses set-interval result.
211 TEST_F(HeartbeatSenderTest, ProcessResponseSetInterval) {
212 EXPECT_CALL(mock_listener_, OnHeartbeatSuccessful());
214 scoped_ptr<XmlElement> response(new XmlElement(buzz::QN_IQ));
215 response->AddAttr(QName(std::string(), "type"), "result");
217 XmlElement* result = new XmlElement(
218 QName(kChromotingXmlNamespace, "heartbeat-result"));
219 response->AddElement(result);
221 XmlElement* set_interval = new XmlElement(
222 QName(kChromotingXmlNamespace, "set-interval"));
223 result->AddElement(set_interval);
225 const int kTestInterval = 123;
226 set_interval->AddText(base::IntToString(kTestInterval));
228 heartbeat_sender_->ProcessResponse(NULL, response.get());
230 EXPECT_EQ(kTestInterval * 1000, heartbeat_sender_->interval_ms_);
233 // Validate a heartbeat stanza.
234 void HeartbeatSenderTest::ValidateHeartbeatStanza(
235 XmlElement* stanza, const char* expectedSequenceId) {
236 EXPECT_EQ(stanza->Attr(buzz::QName(std::string(), "to")),
237 std::string(kTestBotJid));
238 EXPECT_EQ(stanza->Attr(buzz::QName(std::string(), "type")), "set");
239 XmlElement* heartbeat_stanza =
240 stanza->FirstNamed(QName(kChromotingXmlNamespace, "heartbeat"));
241 ASSERT_TRUE(heartbeat_stanza != NULL);
242 EXPECT_EQ(expectedSequenceId, heartbeat_stanza->Attr(
243 buzz::QName(kChromotingXmlNamespace, "sequence-id")));
244 EXPECT_EQ(std::string(kHostId),
245 heartbeat_stanza->Attr(QName(kChromotingXmlNamespace, "hostid")));
247 QName signature_tag(kChromotingXmlNamespace, "signature");
248 XmlElement* signature = heartbeat_stanza->FirstNamed(signature_tag);
249 ASSERT_TRUE(signature != NULL);
250 EXPECT_TRUE(heartbeat_stanza->NextNamed(signature_tag) == NULL);
252 scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(kTestRsaKeyPair);
253 ASSERT_TRUE(key_pair.get());
254 std::string expected_signature =
255 key_pair->SignMessage(std::string(kTestJid) + ' ' + expectedSequenceId);
256 EXPECT_EQ(expected_signature, signature->BodyText());
259 } // namespace remoting