| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/heartbeat_sender.h" | 5 #include "remoting/host/heartbeat_sender.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "remoting/base/constants.h" | 14 #include "remoting/base/constants.h" |
| 15 #include "remoting/base/rsa_key_pair.h" | 15 #include "remoting/base/rsa_key_pair.h" |
| 16 #include "remoting/base/test_rsa_key_pair.h" | 16 #include "remoting/base/test_rsa_key_pair.h" |
| 17 #include "remoting/host/mock_callback.h" | |
| 18 #include "remoting/signaling/iq_sender.h" | 17 #include "remoting/signaling/iq_sender.h" |
| 19 #include "remoting/signaling/mock_signal_strategy.h" | 18 #include "remoting/signaling/mock_signal_strategy.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 21 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 23 #include "third_party/webrtc/libjingle/xmpp/constants.h" | 22 #include "third_party/webrtc/libjingle/xmpp/constants.h" |
| 24 | 23 |
| 25 using buzz::QName; | 24 using buzz::QName; |
| 26 using buzz::XmlElement; | 25 using buzz::XmlElement; |
| 27 | 26 |
| 28 using testing::_; | 27 using testing::_; |
| 29 using testing::DeleteArg; | 28 using testing::DeleteArg; |
| 30 using testing::DoAll; | 29 using testing::DoAll; |
| 31 using testing::Invoke; | 30 using testing::Invoke; |
| 32 using testing::NotNull; | 31 using testing::NotNull; |
| 33 using testing::Return; | 32 using testing::Return; |
| 34 using testing::SaveArg; | 33 using testing::SaveArg; |
| 35 | 34 |
| 36 namespace remoting { | 35 namespace remoting { |
| 37 | 36 |
| 38 namespace { | 37 namespace { |
| 39 | 38 |
| 40 const char kTestBotJid[] = "remotingunittest@bot.talk.google.com"; | 39 const char kTestBotJid[] = "remotingunittest@bot.talk.google.com"; |
| 41 const char kHostId[] = "0"; | 40 const char kHostId[] = "0"; |
| 42 const char kTestJid[] = "user@gmail.com/chromoting123"; | 41 const char kTestJid[] = "user@gmail.com/chromoting123"; |
| 43 const char kStanzaId[] = "123"; | 42 const char kStanzaId[] = "123"; |
| 44 const int kTestInterval = 123; | 43 const int kTestInterval = 123; |
| 44 const base::TimeDelta kTestTimeout = base::TimeDelta::FromSeconds(123); |
| 45 | 45 |
| 46 } // namespace | 46 } // namespace |
| 47 | 47 |
| 48 ACTION_P(AddListener, list) { | 48 ACTION_P(AddListener, list) { |
| 49 list->insert(arg0); | 49 list->insert(arg0); |
| 50 } | 50 } |
| 51 ACTION_P(RemoveListener, list) { | 51 ACTION_P(RemoveListener, list) { |
| 52 EXPECT_TRUE(list->find(arg0) != list->end()); | 52 EXPECT_TRUE(list->find(arg0) != list->end()); |
| 53 list->erase(arg0); | 53 list->erase(arg0); |
| 54 } | 54 } |
| 55 | 55 |
| 56 class MockClosure { |
| 57 public: |
| 58 MOCK_CONST_METHOD0(Run, void()); |
| 59 }; |
| 60 |
| 61 class MockAckCallback { |
| 62 public: |
| 63 MOCK_CONST_METHOD1(Run, void(bool success)); |
| 64 }; |
| 65 |
| 56 class HeartbeatSenderTest | 66 class HeartbeatSenderTest |
| 57 : public testing::Test { | 67 : public testing::Test { |
| 58 protected: | 68 protected: |
| 59 void SetUp() override { | 69 void SetUp() override { |
| 60 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair); | 70 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair); |
| 61 ASSERT_TRUE(key_pair_.get()); | 71 ASSERT_TRUE(key_pair_.get()); |
| 62 | 72 |
| 63 EXPECT_CALL(signal_strategy_, GetState()) | 73 EXPECT_CALL(signal_strategy_, GetState()) |
| 64 .WillOnce(Return(SignalStrategy::DISCONNECTED)); | 74 .WillOnce(Return(SignalStrategy::DISCONNECTED)); |
| 65 EXPECT_CALL(signal_strategy_, AddListener(NotNull())) | 75 EXPECT_CALL(signal_strategy_, AddListener(NotNull())) |
| 66 .WillRepeatedly(AddListener(&signal_strategy_listeners_)); | 76 .WillRepeatedly(AddListener(&signal_strategy_listeners_)); |
| 67 EXPECT_CALL(signal_strategy_, RemoveListener(NotNull())) | 77 EXPECT_CALL(signal_strategy_, RemoveListener(NotNull())) |
| 68 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_)); | 78 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_)); |
| 69 EXPECT_CALL(signal_strategy_, GetLocalJid()) | 79 EXPECT_CALL(signal_strategy_, GetLocalJid()) |
| 70 .WillRepeatedly(Return(kTestJid)); | 80 .WillRepeatedly(Return(kTestJid)); |
| 71 EXPECT_CALL(mock_unknown_host_id_error_callback_, Run()) | 81 EXPECT_CALL(mock_unknown_host_id_error_callback_, Run()) |
| 72 .Times(0); | 82 .Times(0); |
| 73 | 83 |
| 74 heartbeat_sender_.reset(new HeartbeatSender( | 84 heartbeat_sender_.reset(new HeartbeatSender( |
| 75 mock_heartbeat_successful_callback_.GetCallback(), | 85 base::Bind(&MockClosure::Run, |
| 76 mock_unknown_host_id_error_callback_.GetCallback(), | 86 base::Unretained(&mock_heartbeat_successful_callback_)), |
| 87 base::Bind(&MockClosure::Run, |
| 88 base::Unretained(&mock_unknown_host_id_error_callback_)), |
| 77 kHostId, &signal_strategy_, key_pair_, kTestBotJid)); | 89 kHostId, &signal_strategy_, key_pair_, kTestBotJid)); |
| 78 } | 90 } |
| 79 | 91 |
| 80 void TearDown() override { | 92 void TearDown() override { |
| 81 heartbeat_sender_.reset(); | 93 heartbeat_sender_.reset(); |
| 82 EXPECT_TRUE(signal_strategy_listeners_.empty()); | 94 EXPECT_TRUE(signal_strategy_listeners_.empty()); |
| 83 } | 95 } |
| 84 | 96 |
| 85 void ValidateHeartbeatStanza(XmlElement* stanza, | 97 void ValidateHeartbeatStanza(XmlElement* stanza, |
| 86 const char* expected_sequence_id, | 98 const char* expected_sequence_id, |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 EXPECT_CALL(mock_heartbeat_successful_callback_, Run()); | 251 EXPECT_CALL(mock_heartbeat_successful_callback_, Run()); |
| 240 | 252 |
| 241 ProcessResponseWithInterval(false, kTestInterval); | 253 ProcessResponseWithInterval(false, kTestInterval); |
| 242 | 254 |
| 243 EXPECT_EQ(kTestInterval * 1000, heartbeat_sender_->interval_ms_); | 255 EXPECT_EQ(kTestInterval * 1000, heartbeat_sender_->interval_ms_); |
| 244 } | 256 } |
| 245 | 257 |
| 246 // Make sure SetHostOfflineReason sends a correct stanza. | 258 // Make sure SetHostOfflineReason sends a correct stanza. |
| 247 TEST_F(HeartbeatSenderTest, DoSetHostOfflineReason) { | 259 TEST_F(HeartbeatSenderTest, DoSetHostOfflineReason) { |
| 248 XmlElement* sent_iq = nullptr; | 260 XmlElement* sent_iq = nullptr; |
| 261 MockAckCallback mock_ack_callback; |
| 262 |
| 249 EXPECT_CALL(signal_strategy_, GetLocalJid()) | 263 EXPECT_CALL(signal_strategy_, GetLocalJid()) |
| 250 .WillRepeatedly(Return(kTestJid)); | 264 .WillRepeatedly(Return(kTestJid)); |
| 251 EXPECT_CALL(signal_strategy_, GetNextId()) | 265 EXPECT_CALL(signal_strategy_, GetNextId()) |
| 252 .WillOnce(Return(kStanzaId)); | 266 .WillOnce(Return(kStanzaId)); |
| 253 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull())) | 267 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull())) |
| 254 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true))); | 268 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true))); |
| 255 EXPECT_CALL(signal_strategy_, GetState()) | 269 EXPECT_CALL(signal_strategy_, GetState()) |
| 256 .WillOnce(Return(SignalStrategy::DISCONNECTED)) | 270 .WillOnce(Return(SignalStrategy::DISCONNECTED)) |
| 257 .WillRepeatedly(Return(SignalStrategy::CONNECTED)); | 271 .WillRepeatedly(Return(SignalStrategy::CONNECTED)); |
| 272 EXPECT_CALL(mock_ack_callback, Run(_)).Times(0); |
| 258 | 273 |
| 259 heartbeat_sender_->SetHostOfflineReason( | 274 heartbeat_sender_->SetHostOfflineReason( |
| 260 "test_error", | 275 "test_error", kTestTimeout, |
| 261 base::Bind(base::DoNothing)); | 276 base::Bind(&MockAckCallback::Run, base::Unretained(&mock_ack_callback))); |
| 262 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED); | 277 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED); |
| 263 base::RunLoop().RunUntilIdle(); | 278 base::RunLoop().RunUntilIdle(); |
| 264 | 279 |
| 265 scoped_ptr<XmlElement> stanza(sent_iq); | 280 scoped_ptr<XmlElement> stanza(sent_iq); |
| 266 ASSERT_TRUE(stanza != nullptr); | 281 ASSERT_TRUE(stanza != nullptr); |
| 267 ValidateHeartbeatStanza(stanza.get(), "0", "test_error"); | 282 ValidateHeartbeatStanza(stanza.get(), "0", "test_error"); |
| 268 | 283 |
| 269 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED); | 284 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED); |
| 270 base::RunLoop().RunUntilIdle(); | 285 base::RunLoop().RunUntilIdle(); |
| 271 } | 286 } |
| 272 | 287 |
| 273 // Make sure SetHostOfflineReason triggers a callback when bot responds. | 288 // Make sure SetHostOfflineReason triggers a callback when bot responds. |
| 274 TEST_F(HeartbeatSenderTest, ProcessHostOfflineResponses) { | 289 TEST_F(HeartbeatSenderTest, ProcessHostOfflineResponses) { |
| 275 MockClosure mock_ack_callback; | 290 MockAckCallback mock_ack_callback; |
| 276 | 291 |
| 277 EXPECT_CALL(signal_strategy_, GetLocalJid()) | 292 EXPECT_CALL(signal_strategy_, GetLocalJid()) |
| 278 .WillRepeatedly(Return(kTestJid)); | 293 .WillRepeatedly(Return(kTestJid)); |
| 279 EXPECT_CALL(signal_strategy_, GetNextId()) | 294 EXPECT_CALL(signal_strategy_, GetNextId()) |
| 280 .WillOnce(Return(kStanzaId)); | 295 .WillOnce(Return(kStanzaId)); |
| 281 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull())) | 296 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull())) |
| 282 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 297 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 283 EXPECT_CALL(signal_strategy_, GetState()) | 298 EXPECT_CALL(signal_strategy_, GetState()) |
| 284 .WillOnce(Return(SignalStrategy::DISCONNECTED)) | 299 .WillOnce(Return(SignalStrategy::DISCONNECTED)) |
| 285 .WillRepeatedly(Return(SignalStrategy::CONNECTED)); | 300 .WillRepeatedly(Return(SignalStrategy::CONNECTED)); |
| 286 EXPECT_CALL(mock_heartbeat_successful_callback_, Run()) | 301 EXPECT_CALL(mock_heartbeat_successful_callback_, Run()) |
| 287 .WillRepeatedly(Return()); | 302 .WillRepeatedly(Return()); |
| 288 | 303 |
| 289 // Callback should not run, until response to offline-reason. | 304 // Callback should not run, until response to offline-reason. |
| 290 EXPECT_CALL(mock_ack_callback, Run()).Times(0); | 305 EXPECT_CALL(mock_ack_callback, Run(_)).Times(0); |
| 291 | 306 |
| 292 heartbeat_sender_->SetHostOfflineReason( | 307 heartbeat_sender_->SetHostOfflineReason( |
| 293 "test_error", | 308 "test_error", kTestTimeout, |
| 294 mock_ack_callback.GetCallback()); | 309 base::Bind(&MockAckCallback::Run, base::Unretained(&mock_ack_callback))); |
| 295 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED); | 310 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED); |
| 296 base::RunLoop().RunUntilIdle(); | 311 base::RunLoop().RunUntilIdle(); |
| 297 | 312 |
| 298 ProcessResponseWithInterval( | 313 ProcessResponseWithInterval( |
| 299 false, // <- This is not a response to offline-reason. | 314 false, // <- This is not a response to offline-reason. |
| 300 kTestInterval); | 315 kTestInterval); |
| 301 base::RunLoop().RunUntilIdle(); | 316 base::RunLoop().RunUntilIdle(); |
| 302 | 317 |
| 303 // Callback should run once, when we get response to offline-reason. | 318 // Callback should run once, when we get response to offline-reason. |
| 304 EXPECT_CALL(mock_ack_callback, Run()).Times(1); | 319 EXPECT_CALL(mock_ack_callback, Run(true /* success */)).Times(1); |
| 305 ProcessResponseWithInterval( | 320 ProcessResponseWithInterval( |
| 306 true, // <- This is a response to offline-reason. | 321 true, // <- This is a response to offline-reason. |
| 307 kTestInterval); | 322 kTestInterval); |
| 308 base::RunLoop().RunUntilIdle(); | 323 base::RunLoop().RunUntilIdle(); |
| 309 | 324 |
| 310 // When subsequent responses to offline-reason come, | 325 // When subsequent responses to offline-reason come, |
| 311 // the callback should not be called again. | 326 // the callback should not be called again. |
| 312 EXPECT_CALL(mock_ack_callback, Run()).Times(0); | 327 EXPECT_CALL(mock_ack_callback, Run(_)).Times(0); |
| 313 ProcessResponseWithInterval(true, kTestInterval); | 328 ProcessResponseWithInterval(true, kTestInterval); |
| 314 base::RunLoop().RunUntilIdle(); | 329 base::RunLoop().RunUntilIdle(); |
| 315 | 330 |
| 316 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED); | 331 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED); |
| 317 base::RunLoop().RunUntilIdle(); | 332 base::RunLoop().RunUntilIdle(); |
| 318 } | 333 } |
| 319 | 334 |
| 320 // Validate a heartbeat stanza. | 335 // Validate a heartbeat stanza. |
| 321 void HeartbeatSenderTest::ValidateHeartbeatStanza( | 336 void HeartbeatSenderTest::ValidateHeartbeatStanza( |
| 322 XmlElement* stanza, | 337 XmlElement* stanza, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 346 EXPECT_TRUE(heartbeat_stanza->NextNamed(signature_tag) == nullptr); | 361 EXPECT_TRUE(heartbeat_stanza->NextNamed(signature_tag) == nullptr); |
| 347 | 362 |
| 348 scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(kTestRsaKeyPair); | 363 scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(kTestRsaKeyPair); |
| 349 ASSERT_TRUE(key_pair.get()); | 364 ASSERT_TRUE(key_pair.get()); |
| 350 std::string expected_signature = | 365 std::string expected_signature = |
| 351 key_pair->SignMessage(std::string(kTestJid) + ' ' + expected_sequence_id); | 366 key_pair->SignMessage(std::string(kTestJid) + ' ' + expected_sequence_id); |
| 352 EXPECT_EQ(expected_signature, signature->BodyText()); | 367 EXPECT_EQ(expected_signature, signature->BodyText()); |
| 353 } | 368 } |
| 354 | 369 |
| 355 } // namespace remoting | 370 } // namespace remoting |
| OLD | NEW |