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/chromotingABC123";
42 const char kTestJidNormalized
[] = "user@gmail.com/chromotingABC123";
43 const char kStanzaId
[] = "123";
44 const int kTestInterval
= 123;
45 const base::TimeDelta kTestTimeout
= base::TimeDelta::FromSeconds(123);
49 ACTION_P(AddListener
, list
) {
52 ACTION_P(RemoveListener
, list
) {
53 EXPECT_TRUE(list
->find(arg0
) != list
->end());
59 MOCK_CONST_METHOD0(Run
, void());
62 class MockAckCallback
{
64 MOCK_CONST_METHOD1(Run
, void(bool success
));
67 class HeartbeatSenderTest
68 : public testing::Test
{
70 void SetUp() override
{
71 key_pair_
= RsaKeyPair::FromString(kTestRsaKeyPair
);
72 ASSERT_TRUE(key_pair_
.get());
74 EXPECT_CALL(signal_strategy_
, GetState())
75 .WillOnce(Return(SignalStrategy::DISCONNECTED
));
76 EXPECT_CALL(signal_strategy_
, AddListener(NotNull()))
77 .WillRepeatedly(AddListener(&signal_strategy_listeners_
));
78 EXPECT_CALL(signal_strategy_
, RemoveListener(NotNull()))
79 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_
));
80 EXPECT_CALL(signal_strategy_
, GetLocalJid())
81 .WillRepeatedly(Return(kTestJid
));
82 EXPECT_CALL(mock_unknown_host_id_error_callback_
, Run())
85 heartbeat_sender_
.reset(new HeartbeatSender(
86 base::Bind(&MockClosure::Run
,
87 base::Unretained(&mock_heartbeat_successful_callback_
)),
88 base::Bind(&MockClosure::Run
,
89 base::Unretained(&mock_unknown_host_id_error_callback_
)),
90 kHostId
, &signal_strategy_
, key_pair_
, kTestBotJid
));
93 void TearDown() override
{
94 heartbeat_sender_
.reset();
95 EXPECT_TRUE(signal_strategy_listeners_
.empty());
98 void ValidateHeartbeatStanza(XmlElement
* stanza
,
99 const char* expected_sequence_id
,
100 const char* expected_host_offline_reason
);
102 void ProcessResponseWithInterval(
103 bool is_offline_heartbeat_response
,
106 base::MessageLoop message_loop_
;
107 MockSignalStrategy signal_strategy_
;
108 MockClosure mock_heartbeat_successful_callback_
;
109 MockClosure mock_unknown_host_id_error_callback_
;
110 std::set
<SignalStrategy::Listener
*> signal_strategy_listeners_
;
111 scoped_refptr
<RsaKeyPair
> key_pair_
;
112 scoped_ptr
<HeartbeatSender
> heartbeat_sender_
;
115 // Call Start() followed by Stop(), and make sure a valid heartbeat is sent.
116 TEST_F(HeartbeatSenderTest
, DoSendStanza
) {
117 XmlElement
* sent_iq
= nullptr;
118 EXPECT_CALL(signal_strategy_
, GetLocalJid())
119 .WillRepeatedly(Return(kTestJid
));
120 EXPECT_CALL(signal_strategy_
, GetNextId())
121 .WillOnce(Return(kStanzaId
));
122 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
123 .WillOnce(DoAll(SaveArg
<0>(&sent_iq
), Return(true)));
124 EXPECT_CALL(signal_strategy_
, GetState())
125 .WillRepeatedly(Return(SignalStrategy::CONNECTED
));
127 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::CONNECTED
);
128 base::RunLoop().RunUntilIdle();
130 scoped_ptr
<XmlElement
> stanza(sent_iq
);
131 ASSERT_TRUE(stanza
!= nullptr);
132 ValidateHeartbeatStanza(stanza
.get(), "0", nullptr);
134 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED
);
135 base::RunLoop().RunUntilIdle();
138 // Call Start() followed by Stop(), twice, and make sure two valid heartbeats
139 // are sent, with the correct sequence IDs.
140 TEST_F(HeartbeatSenderTest
, DoSendStanzaTwice
) {
141 XmlElement
* sent_iq
= nullptr;
142 EXPECT_CALL(signal_strategy_
, GetLocalJid())
143 .WillRepeatedly(Return(kTestJid
));
144 EXPECT_CALL(signal_strategy_
, GetNextId())
145 .WillOnce(Return(kStanzaId
));
146 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
147 .WillOnce(DoAll(SaveArg
<0>(&sent_iq
), Return(true)));
148 EXPECT_CALL(signal_strategy_
, GetState())
149 .WillRepeatedly(Return(SignalStrategy::CONNECTED
));
151 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::CONNECTED
);
152 base::RunLoop().RunUntilIdle();
154 scoped_ptr
<XmlElement
> stanza(sent_iq
);
155 ASSERT_TRUE(stanza
!= nullptr);
156 ValidateHeartbeatStanza(stanza
.get(), "0", nullptr);
158 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED
);
159 base::RunLoop().RunUntilIdle();
161 EXPECT_CALL(signal_strategy_
, GetLocalJid())
162 .WillRepeatedly(Return(kTestJid
));
163 EXPECT_CALL(signal_strategy_
, GetNextId())
164 .WillOnce(Return(kStanzaId
+ 1));
165 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
166 .WillOnce(DoAll(SaveArg
<0>(&sent_iq
), Return(true)));
168 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::CONNECTED
);
169 base::RunLoop().RunUntilIdle();
171 scoped_ptr
<XmlElement
> stanza2(sent_iq
);
172 ValidateHeartbeatStanza(stanza2
.get(), "1", nullptr);
174 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED
);
175 base::RunLoop().RunUntilIdle();
178 // Call Start() followed by Stop(), make sure a valid Iq stanza is sent,
179 // reply with an expected sequence ID, and make sure two valid heartbeats
180 // are sent, with the correct sequence IDs.
181 TEST_F(HeartbeatSenderTest
, DoSendStanzaWithExpectedSequenceId
) {
182 XmlElement
* sent_iq
= nullptr;
183 EXPECT_CALL(signal_strategy_
, GetLocalJid())
184 .WillRepeatedly(Return(kTestJid
));
185 EXPECT_CALL(signal_strategy_
, GetNextId())
186 .WillOnce(Return(kStanzaId
));
187 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
188 .WillOnce(DoAll(SaveArg
<0>(&sent_iq
), Return(true)));
189 EXPECT_CALL(signal_strategy_
, GetState())
190 .WillRepeatedly(Return(SignalStrategy::CONNECTED
));
192 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::CONNECTED
);
193 base::RunLoop().RunUntilIdle();
195 scoped_ptr
<XmlElement
> stanza(sent_iq
);
196 ASSERT_TRUE(stanza
!= nullptr);
197 ValidateHeartbeatStanza(stanza
.get(), "0", nullptr);
199 XmlElement
* sent_iq2
= nullptr;
200 EXPECT_CALL(signal_strategy_
, GetLocalJid())
201 .WillRepeatedly(Return(kTestJid
));
202 EXPECT_CALL(signal_strategy_
, GetNextId())
203 .WillOnce(Return(kStanzaId
+ 1));
204 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
205 .WillOnce(DoAll(SaveArg
<0>(&sent_iq2
), Return(true)));
207 scoped_ptr
<XmlElement
> response(new XmlElement(buzz::QN_IQ
));
208 response
->AddAttr(QName(std::string(), "type"), "result");
210 new XmlElement(QName(kChromotingXmlNamespace
, "heartbeat-result"));
211 response
->AddElement(result
);
212 XmlElement
* expected_sequence_id
= new XmlElement(
213 QName(kChromotingXmlNamespace
, "expected-sequence-id"));
214 result
->AddElement(expected_sequence_id
);
215 const int kExpectedSequenceId
= 456;
216 expected_sequence_id
->AddText(base::IntToString(kExpectedSequenceId
));
217 heartbeat_sender_
->ProcessResponse(false, nullptr, response
.get());
218 base::RunLoop().RunUntilIdle();
220 scoped_ptr
<XmlElement
> stanza2(sent_iq2
);
221 ASSERT_TRUE(stanza2
!= nullptr);
222 ValidateHeartbeatStanza(stanza2
.get(),
223 base::IntToString(kExpectedSequenceId
).c_str(),
226 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED
);
227 base::RunLoop().RunUntilIdle();
230 void HeartbeatSenderTest::ProcessResponseWithInterval(
231 bool is_offline_heartbeat_response
,
233 scoped_ptr
<XmlElement
> response(new XmlElement(buzz::QN_IQ
));
234 response
->AddAttr(QName(std::string(), "type"), "result");
236 XmlElement
* result
= new XmlElement(
237 QName(kChromotingXmlNamespace
, "heartbeat-result"));
238 response
->AddElement(result
);
240 XmlElement
* set_interval
= new XmlElement(
241 QName(kChromotingXmlNamespace
, "set-interval"));
242 result
->AddElement(set_interval
);
244 set_interval
->AddText(base::IntToString(interval
));
246 heartbeat_sender_
->ProcessResponse(
247 is_offline_heartbeat_response
, nullptr, response
.get());
250 // Verify that ProcessResponse parses set-interval result.
251 TEST_F(HeartbeatSenderTest
, ProcessResponseSetInterval
) {
252 EXPECT_CALL(mock_heartbeat_successful_callback_
, Run());
254 ProcessResponseWithInterval(false, kTestInterval
);
256 EXPECT_EQ(kTestInterval
* 1000, heartbeat_sender_
->interval_ms_
);
259 // Make sure SetHostOfflineReason sends a correct stanza.
260 TEST_F(HeartbeatSenderTest
, DoSetHostOfflineReason
) {
261 XmlElement
* sent_iq
= nullptr;
262 MockAckCallback mock_ack_callback
;
264 EXPECT_CALL(signal_strategy_
, GetLocalJid())
265 .WillRepeatedly(Return(kTestJid
));
266 EXPECT_CALL(signal_strategy_
, GetNextId())
267 .WillOnce(Return(kStanzaId
));
268 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
269 .WillOnce(DoAll(SaveArg
<0>(&sent_iq
), Return(true)));
270 EXPECT_CALL(signal_strategy_
, GetState())
271 .WillOnce(Return(SignalStrategy::DISCONNECTED
))
272 .WillRepeatedly(Return(SignalStrategy::CONNECTED
));
273 EXPECT_CALL(mock_ack_callback
, Run(_
)).Times(0);
275 heartbeat_sender_
->SetHostOfflineReason(
276 "test_error", kTestTimeout
,
277 base::Bind(&MockAckCallback::Run
, base::Unretained(&mock_ack_callback
)));
278 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::CONNECTED
);
279 base::RunLoop().RunUntilIdle();
281 scoped_ptr
<XmlElement
> stanza(sent_iq
);
282 ASSERT_TRUE(stanza
!= nullptr);
283 ValidateHeartbeatStanza(stanza
.get(), "0", "test_error");
285 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED
);
286 base::RunLoop().RunUntilIdle();
289 // Make sure SetHostOfflineReason triggers a callback when bot responds.
290 TEST_F(HeartbeatSenderTest
, ProcessHostOfflineResponses
) {
291 MockAckCallback mock_ack_callback
;
293 EXPECT_CALL(signal_strategy_
, GetLocalJid())
294 .WillRepeatedly(Return(kTestJid
));
295 EXPECT_CALL(signal_strategy_
, GetNextId())
296 .WillOnce(Return(kStanzaId
));
297 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
298 .WillOnce(DoAll(DeleteArg
<0>(), Return(true)));
299 EXPECT_CALL(signal_strategy_
, GetState())
300 .WillOnce(Return(SignalStrategy::DISCONNECTED
))
301 .WillRepeatedly(Return(SignalStrategy::CONNECTED
));
302 EXPECT_CALL(mock_heartbeat_successful_callback_
, Run())
303 .WillRepeatedly(Return());
305 // Callback should not run, until response to offline-reason.
306 EXPECT_CALL(mock_ack_callback
, Run(_
)).Times(0);
308 heartbeat_sender_
->SetHostOfflineReason(
309 "test_error", kTestTimeout
,
310 base::Bind(&MockAckCallback::Run
, base::Unretained(&mock_ack_callback
)));
311 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::CONNECTED
);
312 base::RunLoop().RunUntilIdle();
314 ProcessResponseWithInterval(
315 false, // <- This is not a response to offline-reason.
317 base::RunLoop().RunUntilIdle();
319 // Callback should run once, when we get response to offline-reason.
320 EXPECT_CALL(mock_ack_callback
, Run(true /* success */)).Times(1);
321 ProcessResponseWithInterval(
322 true, // <- This is a response to offline-reason.
324 base::RunLoop().RunUntilIdle();
326 // When subsequent responses to offline-reason come,
327 // the callback should not be called again.
328 EXPECT_CALL(mock_ack_callback
, Run(_
)).Times(0);
329 ProcessResponseWithInterval(true, kTestInterval
);
330 base::RunLoop().RunUntilIdle();
332 heartbeat_sender_
->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED
);
333 base::RunLoop().RunUntilIdle();
336 // Validate a heartbeat stanza.
337 void HeartbeatSenderTest::ValidateHeartbeatStanza(
339 const char* expected_sequence_id
,
340 const char* expected_host_offline_reason
) {
341 EXPECT_EQ(stanza
->Attr(buzz::QName(std::string(), "to")),
342 std::string(kTestBotJid
));
343 EXPECT_EQ(stanza
->Attr(buzz::QName(std::string(), "type")), "set");
344 XmlElement
* heartbeat_stanza
=
345 stanza
->FirstNamed(QName(kChromotingXmlNamespace
, "heartbeat"));
346 ASSERT_TRUE(heartbeat_stanza
!= nullptr);
347 EXPECT_EQ(expected_sequence_id
, heartbeat_stanza
->Attr(
348 buzz::QName(kChromotingXmlNamespace
, "sequence-id")));
349 if (expected_host_offline_reason
== nullptr) {
350 EXPECT_FALSE(heartbeat_stanza
->HasAttr(
351 buzz::QName(kChromotingXmlNamespace
, "host-offline-reason")));
353 EXPECT_EQ(expected_host_offline_reason
, heartbeat_stanza
->Attr(
354 buzz::QName(kChromotingXmlNamespace
, "host-offline-reason")));
356 EXPECT_EQ(std::string(kHostId
),
357 heartbeat_stanza
->Attr(QName(kChromotingXmlNamespace
, "hostid")));
359 QName
signature_tag(kChromotingXmlNamespace
, "signature");
360 XmlElement
* signature
= heartbeat_stanza
->FirstNamed(signature_tag
);
361 ASSERT_TRUE(signature
!= nullptr);
362 EXPECT_TRUE(heartbeat_stanza
->NextNamed(signature_tag
) == nullptr);
364 scoped_refptr
<RsaKeyPair
> key_pair
= RsaKeyPair::FromString(kTestRsaKeyPair
);
365 ASSERT_TRUE(key_pair
.get());
366 std::string expected_signature
= key_pair
->SignMessage(
367 std::string(kTestJidNormalized
) + ' ' + expected_sequence_id
);
368 EXPECT_EQ(expected_signature
, signature
->BodyText());
371 } // namespace remoting