Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(616)

Side by Side Diff: remoting/host/heartbeat_sender_unittest.cc

Issue 734053003: Reporting of policy errors via host-offline-reason: part 2b (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@hor-nohoststatussender
Patch Set: Switched to base::Unretained in mock_callback.h Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/host/heartbeat_sender.cc ('k') | remoting/host/mock_callback.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/memory/weak_ptr.h"
Lambros 2014/11/18 21:57:55 Do you need weark_ptr.h ?
Łukasz Anforowicz 2014/11/18 22:16:54 I don't. Thanks for pointing this out (I've taken
10 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
11 #include "base/message_loop/message_loop_proxy.h" 12 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/run_loop.h" 13 #include "base/run_loop.h"
13 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
14 #include "remoting/base/constants.h" 15 #include "remoting/base/constants.h"
15 #include "remoting/base/rsa_key_pair.h" 16 #include "remoting/base/rsa_key_pair.h"
16 #include "remoting/base/test_rsa_key_pair.h" 17 #include "remoting/base/test_rsa_key_pair.h"
18 #include "remoting/host/mock_callback.h"
17 #include "remoting/signaling/iq_sender.h" 19 #include "remoting/signaling/iq_sender.h"
18 #include "remoting/signaling/mock_signal_strategy.h" 20 #include "remoting/signaling/mock_signal_strategy.h"
19 #include "testing/gmock/include/gmock/gmock.h" 21 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/libjingle/source/talk/xmpp/constants.h" 23 #include "third_party/libjingle/source/talk/xmpp/constants.h"
22 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" 24 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
23 25
24 using buzz::QName; 26 using buzz::QName;
25 using buzz::XmlElement; 27 using buzz::XmlElement;
26 28
27 using testing::_; 29 using testing::_;
28 using testing::DeleteArg; 30 using testing::DeleteArg;
29 using testing::DoAll; 31 using testing::DoAll;
30 using testing::Invoke; 32 using testing::Invoke;
31 using testing::NotNull; 33 using testing::NotNull;
32 using testing::Return; 34 using testing::Return;
33 using testing::SaveArg; 35 using testing::SaveArg;
34 36
35 namespace remoting { 37 namespace remoting {
36 38
37 namespace { 39 namespace {
38 40
39 const char kTestBotJid[] = "remotingunittest@bot.talk.google.com"; 41 const char kTestBotJid[] = "remotingunittest@bot.talk.google.com";
40 const char kHostId[] = "0"; 42 const char kHostId[] = "0";
41 const char kTestJid[] = "user@gmail.com/chromoting123"; 43 const char kTestJid[] = "user@gmail.com/chromoting123";
42 const char kStanzaId[] = "123"; 44 const char kStanzaId[] = "123";
43 const int kTestInterval = 123; 45 const int kTestInterval = 123;
44 46
45 class MockListener : public HeartbeatSender::Listener {
46 public:
47 // Overridden from HeartbeatSender::Listener
48 virtual void OnUnknownHostIdError() override {
49 NOTREACHED();
50 }
51
52 // Overridden from HeartbeatSender::Listener
53 MOCK_METHOD0(OnHeartbeatSuccessful, void());
54 };
55
56 } // namespace 47 } // namespace
57 48
58 ACTION_P(AddListener, list) { 49 ACTION_P(AddListener, list) {
59 list->insert(arg0); 50 list->insert(arg0);
60 } 51 }
61 ACTION_P(RemoveListener, list) { 52 ACTION_P(RemoveListener, list) {
62 EXPECT_TRUE(list->find(arg0) != list->end()); 53 EXPECT_TRUE(list->find(arg0) != list->end());
63 list->erase(arg0); 54 list->erase(arg0);
64 } 55 }
65 56
66 class HeartbeatSenderTest 57 class HeartbeatSenderTest
67 : public testing::Test { 58 : public testing::Test {
68 protected: 59 protected:
69 virtual void SetUp() override { 60 virtual void SetUp() override {
70 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair); 61 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair);
71 ASSERT_TRUE(key_pair_.get()); 62 ASSERT_TRUE(key_pair_.get());
72 63
73 EXPECT_CALL(signal_strategy_, GetState()) 64 EXPECT_CALL(signal_strategy_, GetState())
74 .WillOnce(Return(SignalStrategy::DISCONNECTED)); 65 .WillOnce(Return(SignalStrategy::DISCONNECTED));
75 EXPECT_CALL(signal_strategy_, AddListener(NotNull())) 66 EXPECT_CALL(signal_strategy_, AddListener(NotNull()))
76 .WillRepeatedly(AddListener(&signal_strategy_listeners_)); 67 .WillRepeatedly(AddListener(&signal_strategy_listeners_));
77 EXPECT_CALL(signal_strategy_, RemoveListener(NotNull())) 68 EXPECT_CALL(signal_strategy_, RemoveListener(NotNull()))
78 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_)); 69 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_));
79 EXPECT_CALL(signal_strategy_, GetLocalJid()) 70 EXPECT_CALL(signal_strategy_, GetLocalJid())
80 .WillRepeatedly(Return(kTestJid)); 71 .WillRepeatedly(Return(kTestJid));
72 EXPECT_CALL(mock_unknown_host_id_error_callback_, Run())
73 .Times(0);
81 74
82 heartbeat_sender_.reset(new HeartbeatSender( 75 heartbeat_sender_.reset(new HeartbeatSender(
83 &mock_listener_, kHostId, &signal_strategy_, key_pair_, kTestBotJid)); 76 mock_heartbeat_successful_callback_.GetCallback(),
77 mock_unknown_host_id_error_callback_.GetCallback(),
78 kHostId, &signal_strategy_, key_pair_, kTestBotJid));
84 } 79 }
85 80
86 virtual void TearDown() override { 81 virtual void TearDown() override {
87 heartbeat_sender_.reset(); 82 heartbeat_sender_.reset();
88 EXPECT_TRUE(signal_strategy_listeners_.empty()); 83 EXPECT_TRUE(signal_strategy_listeners_.empty());
89 } 84 }
90 85
91 void ValidateHeartbeatStanza(XmlElement* stanza, 86 void ValidateHeartbeatStanza(XmlElement* stanza,
92 const char* expected_sequence_id, 87 const char* expected_sequence_id,
93 const char* expected_host_offline_reason); 88 const char* expected_host_offline_reason);
94 89
95 void ProcessResponseWithInterval( 90 void ProcessResponseWithInterval(
96 bool is_offline_heartbeat_response, 91 bool is_offline_heartbeat_response,
97 int interval); 92 int interval);
98 93
99 base::MessageLoop message_loop_; 94 base::MessageLoop message_loop_;
100 MockSignalStrategy signal_strategy_; 95 MockSignalStrategy signal_strategy_;
101 MockListener mock_listener_; 96 MockClosure mock_heartbeat_successful_callback_;
97 MockClosure mock_unknown_host_id_error_callback_;
102 std::set<SignalStrategy::Listener*> signal_strategy_listeners_; 98 std::set<SignalStrategy::Listener*> signal_strategy_listeners_;
103 scoped_refptr<RsaKeyPair> key_pair_; 99 scoped_refptr<RsaKeyPair> key_pair_;
104 scoped_ptr<HeartbeatSender> heartbeat_sender_; 100 scoped_ptr<HeartbeatSender> heartbeat_sender_;
105 }; 101 };
106 102
107 // Call Start() followed by Stop(), and make sure a valid heartbeat is sent. 103 // Call Start() followed by Stop(), and make sure a valid heartbeat is sent.
108 TEST_F(HeartbeatSenderTest, DoSendStanza) { 104 TEST_F(HeartbeatSenderTest, DoSendStanza) {
109 XmlElement* sent_iq = NULL; 105 XmlElement* sent_iq = NULL;
110 EXPECT_CALL(signal_strategy_, GetLocalJid()) 106 EXPECT_CALL(signal_strategy_, GetLocalJid())
111 .WillRepeatedly(Return(kTestJid)); 107 .WillRepeatedly(Return(kTestJid));
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 result->AddElement(set_interval); 230 result->AddElement(set_interval);
235 231
236 set_interval->AddText(base::IntToString(interval)); 232 set_interval->AddText(base::IntToString(interval));
237 233
238 heartbeat_sender_->ProcessResponse( 234 heartbeat_sender_->ProcessResponse(
239 is_offline_heartbeat_response, NULL, response.get()); 235 is_offline_heartbeat_response, NULL, response.get());
240 } 236 }
241 237
242 // Verify that ProcessResponse parses set-interval result. 238 // Verify that ProcessResponse parses set-interval result.
243 TEST_F(HeartbeatSenderTest, ProcessResponseSetInterval) { 239 TEST_F(HeartbeatSenderTest, ProcessResponseSetInterval) {
244 EXPECT_CALL(mock_listener_, OnHeartbeatSuccessful()); 240 EXPECT_CALL(mock_heartbeat_successful_callback_, Run());
245 241
246 ProcessResponseWithInterval(false, kTestInterval); 242 ProcessResponseWithInterval(false, kTestInterval);
247 243
248 EXPECT_EQ(kTestInterval * 1000, heartbeat_sender_->interval_ms_); 244 EXPECT_EQ(kTestInterval * 1000, heartbeat_sender_->interval_ms_);
249 } 245 }
250 246
251 // Make sure SetHostOfflineReason sends a correct stanza. 247 // Make sure SetHostOfflineReason sends a correct stanza.
252 TEST_F(HeartbeatSenderTest, DoSetHostOfflineReason) { 248 TEST_F(HeartbeatSenderTest, DoSetHostOfflineReason) {
253 XmlElement* sent_iq = NULL; 249 XmlElement* sent_iq = NULL;
254 EXPECT_CALL(signal_strategy_, GetLocalJid()) 250 EXPECT_CALL(signal_strategy_, GetLocalJid())
(...skipping 13 matching lines...) Expand all
268 base::RunLoop().RunUntilIdle(); 264 base::RunLoop().RunUntilIdle();
269 265
270 scoped_ptr<XmlElement> stanza(sent_iq); 266 scoped_ptr<XmlElement> stanza(sent_iq);
271 ASSERT_TRUE(stanza != NULL); 267 ASSERT_TRUE(stanza != NULL);
272 ValidateHeartbeatStanza(stanza.get(), "0", "test_error"); 268 ValidateHeartbeatStanza(stanza.get(), "0", "test_error");
273 269
274 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED); 270 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED);
275 base::RunLoop().RunUntilIdle(); 271 base::RunLoop().RunUntilIdle();
276 } 272 }
277 273
278 static void MarkCallbackAsRun(bool* didCallbackRun) {
279 *didCallbackRun = true;
280 }
281
282 // Make sure SetHostOfflineReason triggers a callback when bot responds. 274 // Make sure SetHostOfflineReason triggers a callback when bot responds.
283 TEST_F(HeartbeatSenderTest, ProcessHostOfflineResponses) { 275 TEST_F(HeartbeatSenderTest, ProcessHostOfflineResponses) {
276 MockClosure mock_ack_callback;
277
284 EXPECT_CALL(signal_strategy_, GetLocalJid()) 278 EXPECT_CALL(signal_strategy_, GetLocalJid())
285 .WillRepeatedly(Return(kTestJid)); 279 .WillRepeatedly(Return(kTestJid));
286 EXPECT_CALL(signal_strategy_, GetNextId()) 280 EXPECT_CALL(signal_strategy_, GetNextId())
287 .WillOnce(Return(kStanzaId)); 281 .WillOnce(Return(kStanzaId));
288 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull())) 282 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull()))
289 .WillOnce(Return(true)); 283 .WillOnce(Return(true));
290 EXPECT_CALL(signal_strategy_, GetState()) 284 EXPECT_CALL(signal_strategy_, GetState())
291 .WillOnce(Return(SignalStrategy::DISCONNECTED)) 285 .WillOnce(Return(SignalStrategy::DISCONNECTED))
292 .WillRepeatedly(Return(SignalStrategy::CONNECTED)); 286 .WillRepeatedly(Return(SignalStrategy::CONNECTED));
293 EXPECT_CALL(mock_listener_, OnHeartbeatSuccessful()) 287 EXPECT_CALL(mock_heartbeat_successful_callback_, Run())
294 .WillRepeatedly(Return()); 288 .WillRepeatedly(Return());
295 289
296 bool didCallbackRun = false; 290 // Callback should not run, until response to offline-reason.
291 EXPECT_CALL(mock_ack_callback, Run()).Times(0);
292
297 heartbeat_sender_->SetHostOfflineReason( 293 heartbeat_sender_->SetHostOfflineReason(
298 "test_error", 294 "test_error",
299 base::Bind(MarkCallbackAsRun, base::Unretained(&didCallbackRun))); 295 mock_ack_callback.GetCallback());
300 ASSERT_FALSE(didCallbackRun);
301 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED); 296 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED);
302 base::RunLoop().RunUntilIdle(); 297 base::RunLoop().RunUntilIdle();
303 ASSERT_FALSE(didCallbackRun) << "Callback shouldn't run before ack from bot";
304 298
305 ProcessResponseWithInterval( 299 ProcessResponseWithInterval(
306 false /* <- this not a response to offline-reason*/, 300 false, // <- This is not a response to offline-reason.
307 kTestInterval); 301 kTestInterval);
308 base::RunLoop().RunUntilIdle(); 302 base::RunLoop().RunUntilIdle();
309 ASSERT_FALSE(didCallbackRun) << "Callback shouldn't run "
310 << "when getting a response to an earlier, non-offline stanza";
311 303
304 // Callback should run once, when we get response to offline-reason.
305 EXPECT_CALL(mock_ack_callback, Run()).Times(1);
306 ProcessResponseWithInterval(
307 true, // <- This is a response to offline-reason.
308 kTestInterval);
309 base::RunLoop().RunUntilIdle();
310
311 // When subsequent responses to offline-reason come,
312 // the callback should not be called again.
313 EXPECT_CALL(mock_ack_callback, Run()).Times(0);
312 ProcessResponseWithInterval(true, kTestInterval); 314 ProcessResponseWithInterval(true, kTestInterval);
313 base::RunLoop().RunUntilIdle(); 315 base::RunLoop().RunUntilIdle();
314 ASSERT_TRUE(didCallbackRun) << "Ack from bot should trigger the callback";
315
316 didCallbackRun = false;
317 ProcessResponseWithInterval(true, kTestInterval);
318 base::RunLoop().RunUntilIdle();
319 ASSERT_FALSE(didCallbackRun) << "Callback should only run once";
320 316
321 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED); 317 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED);
322 base::RunLoop().RunUntilIdle(); 318 base::RunLoop().RunUntilIdle();
323 } 319 }
324 320
325 // Validate a heartbeat stanza. 321 // Validate a heartbeat stanza.
326 void HeartbeatSenderTest::ValidateHeartbeatStanza( 322 void HeartbeatSenderTest::ValidateHeartbeatStanza(
327 XmlElement* stanza, 323 XmlElement* stanza,
328 const char* expected_sequence_id, 324 const char* expected_sequence_id,
329 const char* expected_host_offline_reason) { 325 const char* expected_host_offline_reason) {
(...skipping 21 matching lines...) Expand all
351 EXPECT_TRUE(heartbeat_stanza->NextNamed(signature_tag) == NULL); 347 EXPECT_TRUE(heartbeat_stanza->NextNamed(signature_tag) == NULL);
352 348
353 scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(kTestRsaKeyPair); 349 scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(kTestRsaKeyPair);
354 ASSERT_TRUE(key_pair.get()); 350 ASSERT_TRUE(key_pair.get());
355 std::string expected_signature = 351 std::string expected_signature =
356 key_pair->SignMessage(std::string(kTestJid) + ' ' + expected_sequence_id); 352 key_pair->SignMessage(std::string(kTestJid) + ' ' + expected_sequence_id);
357 EXPECT_EQ(expected_signature, signature->BodyText()); 353 EXPECT_EQ(expected_signature, signature->BodyText());
358 } 354 }
359 355
360 } // namespace remoting 356 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/heartbeat_sender.cc ('k') | remoting/host/mock_callback.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698