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

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: Rebasing... Created 6 years 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/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"
17 #include "remoting/signaling/iq_sender.h" 18 #include "remoting/signaling/iq_sender.h"
18 #include "remoting/signaling/mock_signal_strategy.h" 19 #include "remoting/signaling/mock_signal_strategy.h"
19 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" 22 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
22 #include "third_party/webrtc/libjingle/xmpp/constants.h" 23 #include "third_party/webrtc/libjingle/xmpp/constants.h"
23 24
24 using buzz::QName; 25 using buzz::QName;
25 using buzz::XmlElement; 26 using buzz::XmlElement;
26 27
27 using testing::_; 28 using testing::_;
28 using testing::DeleteArg; 29 using testing::DeleteArg;
29 using testing::DoAll; 30 using testing::DoAll;
30 using testing::Invoke; 31 using testing::Invoke;
31 using testing::NotNull; 32 using testing::NotNull;
32 using testing::Return; 33 using testing::Return;
33 using testing::SaveArg; 34 using testing::SaveArg;
34 35
35 namespace remoting { 36 namespace remoting {
36 37
37 namespace { 38 namespace {
38 39
39 const char kTestBotJid[] = "remotingunittest@bot.talk.google.com"; 40 const char kTestBotJid[] = "remotingunittest@bot.talk.google.com";
40 const char kHostId[] = "0"; 41 const char kHostId[] = "0";
41 const char kTestJid[] = "user@gmail.com/chromoting123"; 42 const char kTestJid[] = "user@gmail.com/chromoting123";
42 const char kStanzaId[] = "123"; 43 const char kStanzaId[] = "123";
43 const int kTestInterval = 123; 44 const int kTestInterval = 123;
44 45
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 46 } // namespace
57 47
58 ACTION_P(AddListener, list) { 48 ACTION_P(AddListener, list) {
59 list->insert(arg0); 49 list->insert(arg0);
60 } 50 }
61 ACTION_P(RemoveListener, list) { 51 ACTION_P(RemoveListener, list) {
62 EXPECT_TRUE(list->find(arg0) != list->end()); 52 EXPECT_TRUE(list->find(arg0) != list->end());
63 list->erase(arg0); 53 list->erase(arg0);
64 } 54 }
65 55
66 class HeartbeatSenderTest 56 class HeartbeatSenderTest
67 : public testing::Test { 57 : public testing::Test {
68 protected: 58 protected:
69 void SetUp() override { 59 void SetUp() override {
70 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair); 60 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair);
71 ASSERT_TRUE(key_pair_.get()); 61 ASSERT_TRUE(key_pair_.get());
72 62
73 EXPECT_CALL(signal_strategy_, GetState()) 63 EXPECT_CALL(signal_strategy_, GetState())
74 .WillOnce(Return(SignalStrategy::DISCONNECTED)); 64 .WillOnce(Return(SignalStrategy::DISCONNECTED));
75 EXPECT_CALL(signal_strategy_, AddListener(NotNull())) 65 EXPECT_CALL(signal_strategy_, AddListener(NotNull()))
76 .WillRepeatedly(AddListener(&signal_strategy_listeners_)); 66 .WillRepeatedly(AddListener(&signal_strategy_listeners_));
77 EXPECT_CALL(signal_strategy_, RemoveListener(NotNull())) 67 EXPECT_CALL(signal_strategy_, RemoveListener(NotNull()))
78 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_)); 68 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_));
79 EXPECT_CALL(signal_strategy_, GetLocalJid()) 69 EXPECT_CALL(signal_strategy_, GetLocalJid())
80 .WillRepeatedly(Return(kTestJid)); 70 .WillRepeatedly(Return(kTestJid));
71 EXPECT_CALL(mock_unknown_host_id_error_callback_, Run())
72 .Times(0);
81 73
82 heartbeat_sender_.reset(new HeartbeatSender( 74 heartbeat_sender_.reset(new HeartbeatSender(
83 &mock_listener_, kHostId, &signal_strategy_, key_pair_, kTestBotJid)); 75 mock_heartbeat_successful_callback_.GetCallback(),
76 mock_unknown_host_id_error_callback_.GetCallback(),
77 kHostId, &signal_strategy_, key_pair_, kTestBotJid));
84 } 78 }
85 79
86 void TearDown() override { 80 void TearDown() override {
87 heartbeat_sender_.reset(); 81 heartbeat_sender_.reset();
88 EXPECT_TRUE(signal_strategy_listeners_.empty()); 82 EXPECT_TRUE(signal_strategy_listeners_.empty());
89 } 83 }
90 84
91 void ValidateHeartbeatStanza(XmlElement* stanza, 85 void ValidateHeartbeatStanza(XmlElement* stanza,
92 const char* expected_sequence_id, 86 const char* expected_sequence_id,
93 const char* expected_host_offline_reason); 87 const char* expected_host_offline_reason);
94 88
95 void ProcessResponseWithInterval( 89 void ProcessResponseWithInterval(
96 bool is_offline_heartbeat_response, 90 bool is_offline_heartbeat_response,
97 int interval); 91 int interval);
98 92
99 base::MessageLoop message_loop_; 93 base::MessageLoop message_loop_;
100 MockSignalStrategy signal_strategy_; 94 MockSignalStrategy signal_strategy_;
101 MockListener mock_listener_; 95 MockClosure mock_heartbeat_successful_callback_;
96 MockClosure mock_unknown_host_id_error_callback_;
102 std::set<SignalStrategy::Listener*> signal_strategy_listeners_; 97 std::set<SignalStrategy::Listener*> signal_strategy_listeners_;
103 scoped_refptr<RsaKeyPair> key_pair_; 98 scoped_refptr<RsaKeyPair> key_pair_;
104 scoped_ptr<HeartbeatSender> heartbeat_sender_; 99 scoped_ptr<HeartbeatSender> heartbeat_sender_;
105 }; 100 };
106 101
107 // Call Start() followed by Stop(), and make sure a valid heartbeat is sent. 102 // Call Start() followed by Stop(), and make sure a valid heartbeat is sent.
108 TEST_F(HeartbeatSenderTest, DoSendStanza) { 103 TEST_F(HeartbeatSenderTest, DoSendStanza) {
109 XmlElement* sent_iq = NULL; 104 XmlElement* sent_iq = NULL;
110 EXPECT_CALL(signal_strategy_, GetLocalJid()) 105 EXPECT_CALL(signal_strategy_, GetLocalJid())
111 .WillRepeatedly(Return(kTestJid)); 106 .WillRepeatedly(Return(kTestJid));
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 result->AddElement(set_interval); 229 result->AddElement(set_interval);
235 230
236 set_interval->AddText(base::IntToString(interval)); 231 set_interval->AddText(base::IntToString(interval));
237 232
238 heartbeat_sender_->ProcessResponse( 233 heartbeat_sender_->ProcessResponse(
239 is_offline_heartbeat_response, NULL, response.get()); 234 is_offline_heartbeat_response, NULL, response.get());
240 } 235 }
241 236
242 // Verify that ProcessResponse parses set-interval result. 237 // Verify that ProcessResponse parses set-interval result.
243 TEST_F(HeartbeatSenderTest, ProcessResponseSetInterval) { 238 TEST_F(HeartbeatSenderTest, ProcessResponseSetInterval) {
244 EXPECT_CALL(mock_listener_, OnHeartbeatSuccessful()); 239 EXPECT_CALL(mock_heartbeat_successful_callback_, Run());
245 240
246 ProcessResponseWithInterval(false, kTestInterval); 241 ProcessResponseWithInterval(false, kTestInterval);
247 242
248 EXPECT_EQ(kTestInterval * 1000, heartbeat_sender_->interval_ms_); 243 EXPECT_EQ(kTestInterval * 1000, heartbeat_sender_->interval_ms_);
249 } 244 }
250 245
251 // Make sure SetHostOfflineReason sends a correct stanza. 246 // Make sure SetHostOfflineReason sends a correct stanza.
252 TEST_F(HeartbeatSenderTest, DoSetHostOfflineReason) { 247 TEST_F(HeartbeatSenderTest, DoSetHostOfflineReason) {
253 XmlElement* sent_iq = NULL; 248 XmlElement* sent_iq = NULL;
254 EXPECT_CALL(signal_strategy_, GetLocalJid()) 249 EXPECT_CALL(signal_strategy_, GetLocalJid())
(...skipping 13 matching lines...) Expand all
268 base::RunLoop().RunUntilIdle(); 263 base::RunLoop().RunUntilIdle();
269 264
270 scoped_ptr<XmlElement> stanza(sent_iq); 265 scoped_ptr<XmlElement> stanza(sent_iq);
271 ASSERT_TRUE(stanza != NULL); 266 ASSERT_TRUE(stanza != NULL);
272 ValidateHeartbeatStanza(stanza.get(), "0", "test_error"); 267 ValidateHeartbeatStanza(stanza.get(), "0", "test_error");
273 268
274 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED); 269 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED);
275 base::RunLoop().RunUntilIdle(); 270 base::RunLoop().RunUntilIdle();
276 } 271 }
277 272
278 static void MarkCallbackAsRun(bool* didCallbackRun) {
279 *didCallbackRun = true;
280 }
281
282 // Make sure SetHostOfflineReason triggers a callback when bot responds. 273 // Make sure SetHostOfflineReason triggers a callback when bot responds.
283 TEST_F(HeartbeatSenderTest, ProcessHostOfflineResponses) { 274 TEST_F(HeartbeatSenderTest, ProcessHostOfflineResponses) {
275 MockClosure mock_ack_callback;
276
284 EXPECT_CALL(signal_strategy_, GetLocalJid()) 277 EXPECT_CALL(signal_strategy_, GetLocalJid())
285 .WillRepeatedly(Return(kTestJid)); 278 .WillRepeatedly(Return(kTestJid));
286 EXPECT_CALL(signal_strategy_, GetNextId()) 279 EXPECT_CALL(signal_strategy_, GetNextId())
287 .WillOnce(Return(kStanzaId)); 280 .WillOnce(Return(kStanzaId));
288 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull())) 281 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull()))
289 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); 282 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
290 EXPECT_CALL(signal_strategy_, GetState()) 283 EXPECT_CALL(signal_strategy_, GetState())
291 .WillOnce(Return(SignalStrategy::DISCONNECTED)) 284 .WillOnce(Return(SignalStrategy::DISCONNECTED))
292 .WillRepeatedly(Return(SignalStrategy::CONNECTED)); 285 .WillRepeatedly(Return(SignalStrategy::CONNECTED));
293 EXPECT_CALL(mock_listener_, OnHeartbeatSuccessful()) 286 EXPECT_CALL(mock_heartbeat_successful_callback_, Run())
294 .WillRepeatedly(Return()); 287 .WillRepeatedly(Return());
295 288
296 bool didCallbackRun = false; 289 // Callback should not run, until response to offline-reason.
290 EXPECT_CALL(mock_ack_callback, Run()).Times(0);
291
297 heartbeat_sender_->SetHostOfflineReason( 292 heartbeat_sender_->SetHostOfflineReason(
298 "test_error", 293 "test_error",
299 base::Bind(MarkCallbackAsRun, base::Unretained(&didCallbackRun))); 294 mock_ack_callback.GetCallback());
300 ASSERT_FALSE(didCallbackRun);
301 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED); 295 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::CONNECTED);
302 base::RunLoop().RunUntilIdle(); 296 base::RunLoop().RunUntilIdle();
303 ASSERT_FALSE(didCallbackRun) << "Callback shouldn't run before ack from bot";
304 297
305 ProcessResponseWithInterval( 298 ProcessResponseWithInterval(
306 false /* <- this not a response to offline-reason*/, 299 false, // <- This is not a response to offline-reason.
307 kTestInterval); 300 kTestInterval);
308 base::RunLoop().RunUntilIdle(); 301 base::RunLoop().RunUntilIdle();
309 ASSERT_FALSE(didCallbackRun) << "Callback shouldn't run "
310 << "when getting a response to an earlier, non-offline stanza";
311 302
303 // Callback should run once, when we get response to offline-reason.
304 EXPECT_CALL(mock_ack_callback, Run()).Times(1);
305 ProcessResponseWithInterval(
306 true, // <- This is a response to offline-reason.
307 kTestInterval);
308 base::RunLoop().RunUntilIdle();
309
310 // When subsequent responses to offline-reason come,
311 // the callback should not be called again.
312 EXPECT_CALL(mock_ack_callback, Run()).Times(0);
312 ProcessResponseWithInterval(true, kTestInterval); 313 ProcessResponseWithInterval(true, kTestInterval);
313 base::RunLoop().RunUntilIdle(); 314 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 315
321 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED); 316 heartbeat_sender_->OnSignalStrategyStateChange(SignalStrategy::DISCONNECTED);
322 base::RunLoop().RunUntilIdle(); 317 base::RunLoop().RunUntilIdle();
323 } 318 }
324 319
325 // Validate a heartbeat stanza. 320 // Validate a heartbeat stanza.
326 void HeartbeatSenderTest::ValidateHeartbeatStanza( 321 void HeartbeatSenderTest::ValidateHeartbeatStanza(
327 XmlElement* stanza, 322 XmlElement* stanza,
328 const char* expected_sequence_id, 323 const char* expected_sequence_id,
329 const char* expected_host_offline_reason) { 324 const char* expected_host_offline_reason) {
(...skipping 21 matching lines...) Expand all
351 EXPECT_TRUE(heartbeat_stanza->NextNamed(signature_tag) == NULL); 346 EXPECT_TRUE(heartbeat_stanza->NextNamed(signature_tag) == NULL);
352 347
353 scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(kTestRsaKeyPair); 348 scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(kTestRsaKeyPair);
354 ASSERT_TRUE(key_pair.get()); 349 ASSERT_TRUE(key_pair.get());
355 std::string expected_signature = 350 std::string expected_signature =
356 key_pair->SignMessage(std::string(kTestJid) + ' ' + expected_sequence_id); 351 key_pair->SignMessage(std::string(kTestJid) + ' ' + expected_sequence_id);
357 EXPECT_EQ(expected_signature, signature->BodyText()); 352 EXPECT_EQ(expected_signature, signature->BodyText());
358 } 353 }
359 354
360 } // namespace remoting 355 } // 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