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/webrtc/libjingle/xmllite/xmlelement.h"
22 #include "third_party/webrtc/libjingle/xmpp/constants.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";
43 const int kTestInterval
= 123;
44 const base::TimeDelta kTestTimeout
= base::TimeDelta::FromSeconds(123);
48 ACTION_P(AddListener
, list
) {
51 ACTION_P(RemoveListener
, list
) {
52 EXPECT_TRUE(list
->find(arg0
) != list
->end());
58 MOCK_CONST_METHOD0(Run
, void());
61 class MockAckCallback
{
63 MOCK_CONST_METHOD1(Run
, void(bool success
));
66 class HeartbeatSenderTest
67 : public testing::Test
{
69 void SetUp() override
{
70 key_pair_
= RsaKeyPair::FromString(kTestRsaKeyPair
);
71 ASSERT_TRUE(key_pair_
.get());
73 EXPECT_CALL(signal_strategy_
, GetState())
74 .WillOnce(Return(SignalStrategy::DISCONNECTED
));
75 EXPECT_CALL(signal_strategy_
, AddListener(NotNull()))
76 .WillRepeatedly(AddListener(&signal_strategy_listeners_
));
77 EXPECT_CALL(signal_strategy_
, RemoveListener(NotNull()))
78 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_
));
79 EXPECT_CALL(signal_strategy_
, GetLocalJid())
80 .WillRepeatedly(Return(kTestJid
));
81 EXPECT_CALL(mock_unknown_host_id_error_callback_
, Run())
84 heartbeat_sender_
.reset(new HeartbeatSender(
85 base::Bind(&MockClosure::Run
,
86 base::Unretained(&mock_heartbeat_successful_callback_
)),
87 base::Bind(&MockClosure::Run
,
88 base::Unretained(&mock_unknown_host_id_error_callback_
)),
89 kHostId
, &signal_strategy_
, key_pair_
, kTestBotJid
));
92 void TearDown() override
{
93 heartbeat_sender_
.reset();
94 EXPECT_TRUE(signal_strategy_listeners_
.empty());
97 void ValidateHeartbeatStanza(XmlElement
* stanza
,
98 const char* expected_sequence_id
,
99 const char* expected_host_offline_reason
);
101 void ProcessResponseWithInterval(
102 bool is_offline_heartbeat_response
,
105 base::MessageLoop message_loop_
;
106 MockSignalStrategy signal_strategy_
;
107 MockClosure mock_heartbeat_successful_callback_
;
108 MockClosure mock_unknown_host_id_error_callback_
;
109 std::set
<SignalStrategy::Listener
*> signal_strategy_listeners_
;
110 scoped_refptr
<RsaKeyPair
> key_pair_
;
111 scoped_ptr
<HeartbeatSender
> heartbeat_sender_
;
114 // Call Start() followed by Stop(), and make sure a valid heartbeat is sent.
115 TEST_F(HeartbeatSenderTest
, DoSendStanza
) {
116 XmlElement
* sent_iq
= nullptr;
117 EXPECT_CALL(signal_strategy_
, GetLocalJid())
118 .WillRepeatedly(Return(kTestJid
));
119 EXPECT_CALL(signal_strategy_
, GetNextId())
120 .WillOnce(Return(kStanzaId
));
121 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
122 .WillOnce(DoAll(SaveArg
<0>(&sent_iq
), Return(true)));
123 EXPECT_CALL(signal_strategy_
, GetState())
124 .WillRepeatedly(Return(SignalStrategy::CONNECTED
));
126 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::CONNECTED
);
127 base::RunLoop().RunUntilIdle();
129 scoped_ptr
<XmlElement
> stanza(sent_iq
);
130 ASSERT_TRUE(stanza
!= nullptr);
131 ValidateHeartbeatStanza(stanza
.get(), "0", nullptr);
133 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED
);
134 base::RunLoop().RunUntilIdle();
137 // Call Start() followed by Stop(), twice, and make sure two valid heartbeats
138 // are sent, with the correct sequence IDs.
139 TEST_F(HeartbeatSenderTest
, DoSendStanzaTwice
) {
140 XmlElement
* sent_iq
= nullptr;
141 EXPECT_CALL(signal_strategy_
, GetLocalJid())
142 .WillRepeatedly(Return(kTestJid
));
143 EXPECT_CALL(signal_strategy_
, GetNextId())
144 .WillOnce(Return(kStanzaId
));
145 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
146 .WillOnce(DoAll(SaveArg
<0>(&sent_iq
), Return(true)));
147 EXPECT_CALL(signal_strategy_
, GetState())
148 .WillRepeatedly(Return(SignalStrategy::CONNECTED
));
150 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::CONNECTED
);
151 base::RunLoop().RunUntilIdle();
153 scoped_ptr
<XmlElement
> stanza(sent_iq
);
154 ASSERT_TRUE(stanza
!= nullptr);
155 ValidateHeartbeatStanza(stanza
.get(), "0", nullptr);
157 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED
);
158 base::RunLoop().RunUntilIdle();
160 EXPECT_CALL(signal_strategy_
, GetLocalJid())
161 .WillRepeatedly(Return(kTestJid
));
162 EXPECT_CALL(signal_strategy_
, GetNextId())
163 .WillOnce(Return(kStanzaId
+ 1));
164 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
165 .WillOnce(DoAll(SaveArg
<0>(&sent_iq
), Return(true)));
167 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::CONNECTED
);
168 base::RunLoop().RunUntilIdle();
170 scoped_ptr
<XmlElement
> stanza2(sent_iq
);
171 ValidateHeartbeatStanza(stanza2
.get(), "1", nullptr);
173 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED
);
174 base::RunLoop().RunUntilIdle();
177 // Call Start() followed by Stop(), make sure a valid Iq stanza is sent,
178 // reply with an expected sequence ID, and make sure two valid heartbeats
179 // are sent, with the correct sequence IDs.
180 TEST_F(HeartbeatSenderTest
, DoSendStanzaWithExpectedSequenceId
) {
181 XmlElement
* sent_iq
= nullptr;
182 EXPECT_CALL(signal_strategy_
, GetLocalJid())
183 .WillRepeatedly(Return(kTestJid
));
184 EXPECT_CALL(signal_strategy_
, GetNextId())
185 .WillOnce(Return(kStanzaId
));
186 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
187 .WillOnce(DoAll(SaveArg
<0>(&sent_iq
), Return(true)));
188 EXPECT_CALL(signal_strategy_
, GetState())
189 .WillRepeatedly(Return(SignalStrategy::CONNECTED
));
191 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::CONNECTED
);
192 base::RunLoop().RunUntilIdle();
194 scoped_ptr
<XmlElement
> stanza(sent_iq
);
195 ASSERT_TRUE(stanza
!= nullptr);
196 ValidateHeartbeatStanza(stanza
.get(), "0", nullptr);
198 XmlElement
* sent_iq2
= nullptr;
199 EXPECT_CALL(signal_strategy_
, GetLocalJid())
200 .WillRepeatedly(Return(kTestJid
));
201 EXPECT_CALL(signal_strategy_
, GetNextId())
202 .WillOnce(Return(kStanzaId
+ 1));
203 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
204 .WillOnce(DoAll(SaveArg
<0>(&sent_iq2
), Return(true)));
206 scoped_ptr
<XmlElement
> response(new XmlElement(buzz::QN_IQ
));
207 response
->AddAttr(QName(std::string(), "type"), "result");
209 new XmlElement(QName(kChromotingXmlNamespace
, "heartbeat-result"));
210 response
->AddElement(result
);
211 XmlElement
* expected_sequence_id
= new XmlElement(
212 QName(kChromotingXmlNamespace
, "expected-sequence-id"));
213 result
->AddElement(expected_sequence_id
);
214 const int kExpectedSequenceId
= 456;
215 expected_sequence_id
->AddText(base::IntToString(kExpectedSequenceId
));
216 heartbeat_sender_
->ProcessResponse(false, nullptr, response
.get());
217 base::RunLoop().RunUntilIdle();
219 scoped_ptr
<XmlElement
> stanza2(sent_iq2
);
220 ASSERT_TRUE(stanza2
!= nullptr);
221 ValidateHeartbeatStanza(stanza2
.get(),
222 base::IntToString(kExpectedSequenceId
).c_str(),
225 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED
);
226 base::RunLoop().RunUntilIdle();
229 void HeartbeatSenderTest::ProcessResponseWithInterval(
230 bool is_offline_heartbeat_response
,
232 scoped_ptr
<XmlElement
> response(new XmlElement(buzz::QN_IQ
));
233 response
->AddAttr(QName(std::string(), "type"), "result");
235 XmlElement
* result
= new XmlElement(
236 QName(kChromotingXmlNamespace
, "heartbeat-result"));
237 response
->AddElement(result
);
239 XmlElement
* set_interval
= new XmlElement(
240 QName(kChromotingXmlNamespace
, "set-interval"));
241 result
->AddElement(set_interval
);
243 set_interval
->AddText(base::IntToString(interval
));
245 heartbeat_sender_
->ProcessResponse(
246 is_offline_heartbeat_response
, nullptr, response
.get());
249 // Verify that ProcessResponse parses set-interval result.
250 TEST_F(HeartbeatSenderTest
, ProcessResponseSetInterval
) {
251 EXPECT_CALL(mock_heartbeat_successful_callback_
, Run());
253 ProcessResponseWithInterval(false, kTestInterval
);
255 EXPECT_EQ(kTestInterval
* 1000, heartbeat_sender_
->interval_ms_
);
258 // Make sure SetHostOfflineReason sends a correct stanza.
259 TEST_F(HeartbeatSenderTest
, DoSetHostOfflineReason
) {
260 XmlElement
* sent_iq
= nullptr;
261 MockAckCallback mock_ack_callback
;
263 EXPECT_CALL(signal_strategy_
, GetLocalJid())
264 .WillRepeatedly(Return(kTestJid
));
265 EXPECT_CALL(signal_strategy_
, GetNextId())
266 .WillOnce(Return(kStanzaId
));
267 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
268 .WillOnce(DoAll(SaveArg
<0>(&sent_iq
), Return(true)));
269 EXPECT_CALL(signal_strategy_
, GetState())
270 .WillOnce(Return(SignalStrategy::DISCONNECTED
))
271 .WillRepeatedly(Return(SignalStrategy::CONNECTED
));
272 EXPECT_CALL(mock_ack_callback
, Run(_
)).Times(0);
274 heartbeat_sender_
->SetHostOfflineReason(
275 "test_error", kTestTimeout
,
276 base::Bind(&MockAckCallback::Run
, base::Unretained(&mock_ack_callback
)));
277 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::CONNECTED
);
278 base::RunLoop().RunUntilIdle();
280 scoped_ptr
<XmlElement
> stanza(sent_iq
);
281 ASSERT_TRUE(stanza
!= nullptr);
282 ValidateHeartbeatStanza(stanza
.get(), "0", "test_error");
284 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED
);
285 base::RunLoop().RunUntilIdle();
288 // Make sure SetHostOfflineReason triggers a callback when bot responds.
289 TEST_F(HeartbeatSenderTest
, ProcessHostOfflineResponses
) {
290 MockAckCallback mock_ack_callback
;
292 EXPECT_CALL(signal_strategy_
, GetLocalJid())
293 .WillRepeatedly(Return(kTestJid
));
294 EXPECT_CALL(signal_strategy_
, GetNextId())
295 .WillOnce(Return(kStanzaId
));
296 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
297 .WillOnce(DoAll(DeleteArg
<0>(), Return(true)));
298 EXPECT_CALL(signal_strategy_
, GetState())
299 .WillOnce(Return(SignalStrategy::DISCONNECTED
))
300 .WillRepeatedly(Return(SignalStrategy::CONNECTED
));
301 EXPECT_CALL(mock_heartbeat_successful_callback_
, Run())
302 .WillRepeatedly(Return());
304 // Callback should not run, until response to offline-reason.
305 EXPECT_CALL(mock_ack_callback
, Run(_
)).Times(0);
307 heartbeat_sender_
->SetHostOfflineReason(
308 "test_error", kTestTimeout
,
309 base::Bind(&MockAckCallback::Run
, base::Unretained(&mock_ack_callback
)));
310 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::CONNECTED
);
311 base::RunLoop().RunUntilIdle();
313 ProcessResponseWithInterval(
314 false, // <- This is not a response to offline-reason.
316 base::RunLoop().RunUntilIdle();
318 // Callback should run once, when we get response to offline-reason.
319 EXPECT_CALL(mock_ack_callback
, Run(true /* success */)).Times(1);
320 ProcessResponseWithInterval(
321 true, // <- This is a response to offline-reason.
323 base::RunLoop().RunUntilIdle();
325 // When subsequent responses to offline-reason come,
326 // the callback should not be called again.
327 EXPECT_CALL(mock_ack_callback
, Run(_
)).Times(0);
328 ProcessResponseWithInterval(true, kTestInterval
);
329 base::RunLoop().RunUntilIdle();
331 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED
);
332 base::RunLoop().RunUntilIdle();
335 // Validate a heartbeat stanza.
336 void HeartbeatSenderTest::ValidateHeartbeatStanza(
338 const char* expected_sequence_id
,
339 const char* expected_host_offline_reason
) {
340 EXPECT_EQ(stanza
->Attr(buzz::QName(std::string(), "to")),
341 std::string(kTestBotJid
));
342 EXPECT_EQ(stanza
->Attr(buzz::QName(std::string(), "type")), "set");
343 XmlElement
* heartbeat_stanza
=
344 stanza
->FirstNamed(QName(kChromotingXmlNamespace
, "heartbeat"));
345 ASSERT_TRUE(heartbeat_stanza
!= nullptr);
346 EXPECT_EQ(expected_sequence_id
, heartbeat_stanza
->Attr(
347 buzz::QName(kChromotingXmlNamespace
, "sequence-id")));
348 if (expected_host_offline_reason
== nullptr) {
349 EXPECT_FALSE(heartbeat_stanza
->HasAttr(
350 buzz::QName(kChromotingXmlNamespace
, "host-offline-reason")));
352 EXPECT_EQ(expected_host_offline_reason
, heartbeat_stanza
->Attr(
353 buzz::QName(kChromotingXmlNamespace
, "host-offline-reason")));
355 EXPECT_EQ(std::string(kHostId
),
356 heartbeat_stanza
->Attr(QName(kChromotingXmlNamespace
, "hostid")));
358 QName
signature_tag(kChromotingXmlNamespace
, "signature");
359 XmlElement
* signature
= heartbeat_stanza
->FirstNamed(signature_tag
);
360 ASSERT_TRUE(signature
!= nullptr);
361 EXPECT_TRUE(heartbeat_stanza
->NextNamed(signature_tag
) == nullptr);
363 scoped_refptr
<RsaKeyPair
> key_pair
= RsaKeyPair::FromString(kTestRsaKeyPair
);
364 ASSERT_TRUE(key_pair
.get());
365 std::string expected_signature
=
366 key_pair
->SignMessage(std::string(kTestJid
) + ' ' + expected_sequence_id
);
367 EXPECT_EQ(expected_signature
, signature
->BodyText());
370 } // namespace remoting