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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "remoting/host/dns_blackhole_checker.h" | 10 #include "remoting/host/dns_blackhole_checker.h" |
11 #include "remoting/host/policy_hack/fake_policy_watcher.h" | 11 #include "remoting/host/policy_hack/fake_policy_watcher.h" |
12 #include "remoting/host/policy_hack/mock_policy_callback.h" | 12 #include "remoting/host/policy_hack/mock_policy_callback.h" |
13 #include "remoting/host/policy_hack/policy_watcher.h" | 13 #include "remoting/host/policy_hack/policy_watcher.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace remoting { | 17 namespace remoting { |
18 namespace policy_hack { | 18 namespace policy_hack { |
19 | 19 |
20 class PolicyWatcherTest : public testing::Test { | 20 class PolicyWatcherTest : public testing::Test { |
21 public: | 21 public: |
22 PolicyWatcherTest() { | 22 PolicyWatcherTest() { |
23 } | 23 } |
24 | 24 |
25 void SetUp() override { | 25 void SetUp() override { |
26 message_loop_proxy_ = base::MessageLoopProxy::current(); | 26 message_loop_proxy_ = base::MessageLoopProxy::current(); |
27 policy_callback_ = base::Bind(&MockPolicyCallback::OnPolicyUpdate, | 27 policy_updated_callback_ = base::Bind( |
28 base::Unretained(&mock_policy_callback_)); | 28 &MockPolicyCallback::OnPolicyUpdate, |
| 29 base::Unretained(&mock_policy_callback_)); |
| 30 policy_error_callback_ = base::Bind( |
| 31 &MockPolicyCallback::OnPolicyError, |
| 32 base::Unretained(&mock_policy_callback_)); |
29 policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_)); | 33 policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_)); |
30 nat_true_.SetBoolean(PolicyWatcher::kNatPolicyName, true); | 34 nat_true_.SetBoolean(PolicyWatcher::kNatPolicyName, true); |
31 nat_false_.SetBoolean(PolicyWatcher::kNatPolicyName, false); | 35 nat_false_.SetBoolean(PolicyWatcher::kNatPolicyName, false); |
32 nat_one_.SetInteger(PolicyWatcher::kNatPolicyName, 1); | 36 nat_one_.SetInteger(PolicyWatcher::kNatPolicyName, 1); |
33 domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName, | 37 domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName, |
34 std::string()); | 38 std::string()); |
35 domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName, kHostDomain); | 39 domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName, kHostDomain); |
36 SetDefaults(nat_true_others_default_); | 40 SetDefaults(nat_true_others_default_); |
37 nat_true_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, true); | 41 nat_true_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, true); |
38 SetDefaults(nat_false_others_default_); | 42 SetDefaults(nat_false_others_default_); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 nat_false_overridden_others_default_.SetBoolean( | 91 nat_false_overridden_others_default_.SetBoolean( |
88 PolicyWatcher::kNatPolicyName, false); | 92 PolicyWatcher::kNatPolicyName, false); |
89 nat_false_overridden_others_default_.SetString( | 93 nat_false_overridden_others_default_.SetString( |
90 PolicyWatcher::kHostDebugOverridePoliciesName, | 94 PolicyWatcher::kHostDebugOverridePoliciesName, |
91 kOverrideNatTraversalToFalse); | 95 kOverrideNatTraversalToFalse); |
92 #endif | 96 #endif |
93 } | 97 } |
94 | 98 |
95 protected: | 99 protected: |
96 void StartWatching() { | 100 void StartWatching() { |
97 policy_watcher_->StartWatching(policy_callback_); | 101 policy_watcher_->StartWatching( |
| 102 policy_updated_callback_, |
| 103 policy_error_callback_); |
98 base::RunLoop().RunUntilIdle(); | 104 base::RunLoop().RunUntilIdle(); |
99 } | 105 } |
100 | 106 |
101 void StopWatching() { | 107 void StopWatching() { |
102 EXPECT_CALL(*this, PostPolicyWatcherShutdown()).Times(1); | 108 EXPECT_CALL(*this, PostPolicyWatcherShutdown()).Times(1); |
103 policy_watcher_->StopWatching(base::Bind( | 109 policy_watcher_->StopWatching(base::Bind( |
104 &PolicyWatcherTest::PostPolicyWatcherShutdown, base::Unretained(this))); | 110 &PolicyWatcherTest::PostPolicyWatcherShutdown, base::Unretained(this))); |
105 base::RunLoop().RunUntilIdle(); | 111 base::RunLoop().RunUntilIdle(); |
106 } | 112 } |
107 | 113 |
108 MOCK_METHOD0(PostPolicyWatcherShutdown, void()); | 114 MOCK_METHOD0(PostPolicyWatcherShutdown, void()); |
109 | 115 |
110 static const char* kHostDomain; | 116 static const char* kHostDomain; |
111 static const char* kPortRange; | 117 static const char* kPortRange; |
112 base::MessageLoop message_loop_; | 118 base::MessageLoop message_loop_; |
113 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 119 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
114 MockPolicyCallback mock_policy_callback_; | 120 MockPolicyCallback mock_policy_callback_; |
115 PolicyWatcher::PolicyCallback policy_callback_; | 121 PolicyWatcher::PolicyUpdatedCallback policy_updated_callback_; |
| 122 PolicyWatcher::PolicyErrorCallback policy_error_callback_; |
116 scoped_ptr<FakePolicyWatcher> policy_watcher_; | 123 scoped_ptr<FakePolicyWatcher> policy_watcher_; |
117 base::DictionaryValue empty_; | 124 base::DictionaryValue empty_; |
118 base::DictionaryValue nat_true_; | 125 base::DictionaryValue nat_true_; |
119 base::DictionaryValue nat_false_; | 126 base::DictionaryValue nat_false_; |
120 base::DictionaryValue nat_one_; | 127 base::DictionaryValue nat_one_; |
121 base::DictionaryValue domain_empty_; | 128 base::DictionaryValue domain_empty_; |
122 base::DictionaryValue domain_full_; | 129 base::DictionaryValue domain_full_; |
123 base::DictionaryValue nat_true_others_default_; | 130 base::DictionaryValue nat_true_others_default_; |
124 base::DictionaryValue nat_false_others_default_; | 131 base::DictionaryValue nat_false_others_default_; |
125 base::DictionaryValue domain_empty_others_default_; | 132 base::DictionaryValue domain_empty_others_default_; |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 EXPECT_CALL(mock_policy_callback_, | 404 EXPECT_CALL(mock_policy_callback_, |
398 OnPolicyUpdatePtr(IsPolicies(&port_range_empty_))); | 405 OnPolicyUpdatePtr(IsPolicies(&port_range_empty_))); |
399 | 406 |
400 StartWatching(); | 407 StartWatching(); |
401 policy_watcher_->SetPolicies(&empty_); | 408 policy_watcher_->SetPolicies(&empty_); |
402 policy_watcher_->SetPolicies(&port_range_full_); | 409 policy_watcher_->SetPolicies(&port_range_full_); |
403 policy_watcher_->SetPolicies(&port_range_empty_); | 410 policy_watcher_->SetPolicies(&port_range_empty_); |
404 StopWatching(); | 411 StopWatching(); |
405 } | 412 } |
406 | 413 |
| 414 const int kMaxTransientErrorRetries = 5; |
| 415 |
| 416 TEST_F(PolicyWatcherTest, SingleTransientErrorDoesntTriggerErrorCallback) { |
| 417 EXPECT_CALL(mock_policy_callback_, OnPolicyErrorPtr()).Times(0); |
| 418 |
| 419 StartWatching(); |
| 420 policy_watcher_->SignalTransientErrorForTest(); |
| 421 StopWatching(); |
| 422 } |
| 423 |
| 424 TEST_F(PolicyWatcherTest, MultipleTransientErrorsTriggerErrorCallback) { |
| 425 EXPECT_CALL(mock_policy_callback_, OnPolicyErrorPtr()); |
| 426 |
| 427 StartWatching(); |
| 428 for (int i = 0; i < kMaxTransientErrorRetries; i++) { |
| 429 policy_watcher_->SignalTransientErrorForTest(); |
| 430 } |
| 431 StopWatching(); |
| 432 } |
| 433 |
| 434 TEST_F(PolicyWatcherTest, PolicyUpdateResetsTransientErrorsCounter) { |
| 435 testing::InSequence s; |
| 436 EXPECT_CALL(mock_policy_callback_, |
| 437 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 438 EXPECT_CALL(mock_policy_callback_, OnPolicyErrorPtr()).Times(0); |
| 439 |
| 440 StartWatching(); |
| 441 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { |
| 442 policy_watcher_->SignalTransientErrorForTest(); |
| 443 } |
| 444 policy_watcher_->SetPolicies(&nat_true_); |
| 445 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { |
| 446 policy_watcher_->SignalTransientErrorForTest(); |
| 447 } |
| 448 StopWatching(); |
| 449 } |
| 450 |
407 } // namespace policy_hack | 451 } // namespace policy_hack |
408 } // namespace remoting | 452 } // namespace remoting |
OLD | NEW |