| 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/json/json_writer.h" |
| 7 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 9 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "components/policy/core/common/fake_async_policy_loader.h" |
| 10 #include "policy/policy_constants.h" | 12 #include "policy/policy_constants.h" |
| 11 #include "remoting/host/dns_blackhole_checker.h" | 13 #include "remoting/host/dns_blackhole_checker.h" |
| 12 #include "remoting/host/policy_hack/fake_policy_watcher.h" | |
| 13 #include "remoting/host/policy_hack/mock_policy_callback.h" | 14 #include "remoting/host/policy_hack/mock_policy_callback.h" |
| 15 #include "remoting/host/policy_hack/policy_service_watcher.h" |
| 14 #include "remoting/host/policy_hack/policy_watcher.h" | 16 #include "remoting/host/policy_hack/policy_watcher.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 19 |
| 18 namespace remoting { | 20 namespace remoting { |
| 19 namespace policy_hack { | 21 namespace policy_hack { |
| 20 | 22 |
| 21 class PolicyWatcherTest : public testing::Test { | 23 class PolicyWatcherTest : public testing::Test { |
| 22 public: | 24 public: |
| 23 PolicyWatcherTest() : message_loop_(base::MessageLoop::TYPE_IO) {} | 25 PolicyWatcherTest() : message_loop_(base::MessageLoop::TYPE_IO) {} |
| 24 | 26 |
| 25 void SetUp() override { | 27 void SetUp() override { |
| 26 message_loop_proxy_ = base::MessageLoopProxy::current(); | 28 message_loop_proxy_ = base::MessageLoopProxy::current(); |
| 27 policy_updated_callback_ = base::Bind( | 29 policy_updated_callback_ = base::Bind( |
| 28 &MockPolicyCallback::OnPolicyUpdate, | 30 &MockPolicyCallback::OnPolicyUpdate, |
| 29 base::Unretained(&mock_policy_callback_)); | 31 base::Unretained(&mock_policy_callback_)); |
| 30 policy_error_callback_ = base::Bind( | 32 policy_error_callback_ = base::Bind( |
| 31 &MockPolicyCallback::OnPolicyError, | 33 &MockPolicyCallback::OnPolicyError, |
| 32 base::Unretained(&mock_policy_callback_)); | 34 base::Unretained(&mock_policy_callback_)); |
| 33 policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_)); | 35 |
| 36 scoped_ptr<policy::FakeAsyncPolicyLoader> policy_loader( |
| 37 new policy::FakeAsyncPolicyLoader(message_loop_proxy_)); |
| 38 // retaining a pointer to keep control over policy contents. |
| 39 policy_loader_ = policy_loader.get(); |
| 40 policy_watcher_ = PolicyServiceWatcher::CreateFromPolicyLoader( |
| 41 message_loop_proxy_, policy_loader.Pass()); |
| 42 |
| 34 nat_true_.SetBoolean(policy::key::kRemoteAccessHostFirewallTraversal, true); | 43 nat_true_.SetBoolean(policy::key::kRemoteAccessHostFirewallTraversal, true); |
| 35 nat_false_.SetBoolean(policy::key::kRemoteAccessHostFirewallTraversal, | 44 nat_false_.SetBoolean(policy::key::kRemoteAccessHostFirewallTraversal, |
| 36 false); | 45 false); |
| 37 nat_one_.SetInteger(policy::key::kRemoteAccessHostFirewallTraversal, 1); | 46 nat_one_.SetInteger(policy::key::kRemoteAccessHostFirewallTraversal, 1); |
| 38 domain_empty_.SetString(policy::key::kRemoteAccessHostDomain, | 47 domain_empty_.SetString(policy::key::kRemoteAccessHostDomain, |
| 39 std::string()); | 48 std::string()); |
| 40 domain_full_.SetString(policy::key::kRemoteAccessHostDomain, kHostDomain); | 49 domain_full_.SetString(policy::key::kRemoteAccessHostDomain, kHostDomain); |
| 41 SetDefaults(nat_true_others_default_); | 50 SetDefaults(nat_true_others_default_); |
| 42 nat_true_others_default_.SetBoolean( | 51 nat_true_others_default_.SetBoolean( |
| 43 policy::key::kRemoteAccessHostFirewallTraversal, true); | 52 policy::key::kRemoteAccessHostFirewallTraversal, true); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 base::RunLoop().RunUntilIdle(); | 125 base::RunLoop().RunUntilIdle(); |
| 117 } | 126 } |
| 118 | 127 |
| 119 void StopWatching() { | 128 void StopWatching() { |
| 120 EXPECT_CALL(*this, PostPolicyWatcherShutdown()).Times(1); | 129 EXPECT_CALL(*this, PostPolicyWatcherShutdown()).Times(1); |
| 121 policy_watcher_->StopWatching(base::Bind( | 130 policy_watcher_->StopWatching(base::Bind( |
| 122 &PolicyWatcherTest::PostPolicyWatcherShutdown, base::Unretained(this))); | 131 &PolicyWatcherTest::PostPolicyWatcherShutdown, base::Unretained(this))); |
| 123 base::RunLoop().RunUntilIdle(); | 132 base::RunLoop().RunUntilIdle(); |
| 124 } | 133 } |
| 125 | 134 |
| 135 void SetPolicies(const base::DictionaryValue& dict) { |
| 136 // Copy |dict| into |policy_bundle|. |
| 137 policy::PolicyNamespace policy_namespace = |
| 138 policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string()); |
| 139 policy::PolicyBundle policy_bundle; |
| 140 policy::PolicyMap& policy_map = policy_bundle.Get(policy_namespace); |
| 141 policy_map.LoadFrom(&dict, policy::POLICY_LEVEL_MANDATORY, |
| 142 policy::POLICY_SCOPE_MACHINE); |
| 143 |
| 144 // Simulate a policy file/registry/preference update. |
| 145 policy_loader_->SetPolicies(policy_bundle); |
| 146 policy_loader_->PostReloadOnBackgroundThread(true /* force reload asap */); |
| 147 base::RunLoop().RunUntilIdle(); |
| 148 } |
| 149 |
| 150 void SignalTransientErrorForTest() { |
| 151 policy_watcher_->SignalTransientPolicyError(); |
| 152 } |
| 153 |
| 126 MOCK_METHOD0(PostPolicyWatcherShutdown, void()); | 154 MOCK_METHOD0(PostPolicyWatcherShutdown, void()); |
| 127 | 155 |
| 128 static const char* kHostDomain; | 156 static const char* kHostDomain; |
| 129 static const char* kPortRange; | 157 static const char* kPortRange; |
| 130 base::MessageLoop message_loop_; | 158 base::MessageLoop message_loop_; |
| 131 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 159 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 132 MockPolicyCallback mock_policy_callback_; | 160 MockPolicyCallback mock_policy_callback_; |
| 133 PolicyWatcher::PolicyUpdatedCallback policy_updated_callback_; | 161 PolicyWatcher::PolicyUpdatedCallback policy_updated_callback_; |
| 134 PolicyWatcher::PolicyErrorCallback policy_error_callback_; | 162 PolicyWatcher::PolicyErrorCallback policy_error_callback_; |
| 135 scoped_ptr<FakePolicyWatcher> policy_watcher_; | 163 policy::FakeAsyncPolicyLoader* policy_loader_; |
| 164 scoped_ptr<PolicyWatcher> policy_watcher_; |
| 136 base::DictionaryValue empty_; | 165 base::DictionaryValue empty_; |
| 137 base::DictionaryValue nat_true_; | 166 base::DictionaryValue nat_true_; |
| 138 base::DictionaryValue nat_false_; | 167 base::DictionaryValue nat_false_; |
| 139 base::DictionaryValue nat_one_; | 168 base::DictionaryValue nat_one_; |
| 140 base::DictionaryValue domain_empty_; | 169 base::DictionaryValue domain_empty_; |
| 141 base::DictionaryValue domain_full_; | 170 base::DictionaryValue domain_full_; |
| 142 base::DictionaryValue nat_true_others_default_; | 171 base::DictionaryValue nat_true_others_default_; |
| 143 base::DictionaryValue nat_false_others_default_; | 172 base::DictionaryValue nat_false_others_default_; |
| 144 base::DictionaryValue domain_empty_others_default_; | 173 base::DictionaryValue domain_empty_others_default_; |
| 145 base::DictionaryValue domain_full_others_default_; | 174 base::DictionaryValue domain_full_others_default_; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 #if !defined(NDEBUG) | 211 #if !defined(NDEBUG) |
| 183 dict.SetString(policy::key::kRemoteAccessHostDebugOverridePolicies, ""); | 212 dict.SetString(policy::key::kRemoteAccessHostDebugOverridePolicies, ""); |
| 184 #endif | 213 #endif |
| 185 } | 214 } |
| 186 }; | 215 }; |
| 187 | 216 |
| 188 const char* PolicyWatcherTest::kHostDomain = "google.com"; | 217 const char* PolicyWatcherTest::kHostDomain = "google.com"; |
| 189 const char* PolicyWatcherTest::kPortRange = "12400-12409"; | 218 const char* PolicyWatcherTest::kPortRange = "12400-12409"; |
| 190 | 219 |
| 191 MATCHER_P(IsPolicies, dict, "") { | 220 MATCHER_P(IsPolicies, dict, "") { |
| 192 return arg->Equals(dict); | 221 bool equal = arg->Equals(dict); |
| 222 if (!equal) { |
| 223 std::string actual_value; |
| 224 base::JSONWriter::WriteWithOptions( |
| 225 arg, base::JSONWriter::OPTIONS_PRETTY_PRINT, &actual_value); |
| 226 |
| 227 std::string expected_value; |
| 228 base::JSONWriter::WriteWithOptions( |
| 229 dict, base::JSONWriter::OPTIONS_PRETTY_PRINT, &expected_value); |
| 230 |
| 231 *result_listener << "Policies are not equal. "; |
| 232 *result_listener << "Expected policy: " << expected_value << ". "; |
| 233 *result_listener << "Actual policy: " << actual_value << "."; |
| 234 } |
| 235 return equal; |
| 193 } | 236 } |
| 194 | 237 |
| 195 TEST_F(PolicyWatcherTest, None) { | 238 TEST_F(PolicyWatcherTest, None) { |
| 196 EXPECT_CALL(mock_policy_callback_, | 239 EXPECT_CALL(mock_policy_callback_, |
| 197 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | 240 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 198 | 241 |
| 242 SetPolicies(empty_); |
| 199 StartWatching(); | 243 StartWatching(); |
| 200 policy_watcher_->SetPolicies(&empty_); | |
| 201 StopWatching(); | 244 StopWatching(); |
| 202 } | 245 } |
| 203 | 246 |
| 204 TEST_F(PolicyWatcherTest, NatTrue) { | 247 TEST_F(PolicyWatcherTest, NatTrue) { |
| 205 EXPECT_CALL(mock_policy_callback_, | 248 EXPECT_CALL(mock_policy_callback_, |
| 206 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | 249 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 207 | 250 |
| 251 SetPolicies(nat_true_); |
| 208 StartWatching(); | 252 StartWatching(); |
| 209 policy_watcher_->SetPolicies(&nat_true_); | |
| 210 StopWatching(); | 253 StopWatching(); |
| 211 } | 254 } |
| 212 | 255 |
| 213 TEST_F(PolicyWatcherTest, NatFalse) { | 256 TEST_F(PolicyWatcherTest, NatFalse) { |
| 214 EXPECT_CALL(mock_policy_callback_, | 257 EXPECT_CALL(mock_policy_callback_, |
| 215 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_))); | 258 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_))); |
| 216 | 259 |
| 260 SetPolicies(nat_false_); |
| 217 StartWatching(); | 261 StartWatching(); |
| 218 policy_watcher_->SetPolicies(&nat_false_); | |
| 219 StopWatching(); | 262 StopWatching(); |
| 220 } | 263 } |
| 221 | 264 |
| 222 TEST_F(PolicyWatcherTest, NatOne) { | 265 TEST_F(PolicyWatcherTest, NatOne) { |
| 223 EXPECT_CALL(mock_policy_callback_, | 266 EXPECT_CALL(mock_policy_callback_, |
| 224 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_))); | 267 OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_))); |
| 225 | 268 |
| 269 SetPolicies(nat_one_); |
| 226 StartWatching(); | 270 StartWatching(); |
| 227 policy_watcher_->SetPolicies(&nat_one_); | |
| 228 StopWatching(); | 271 StopWatching(); |
| 229 } | 272 } |
| 230 | 273 |
| 231 TEST_F(PolicyWatcherTest, DomainEmpty) { | 274 TEST_F(PolicyWatcherTest, DomainEmpty) { |
| 232 EXPECT_CALL(mock_policy_callback_, | 275 EXPECT_CALL(mock_policy_callback_, |
| 233 OnPolicyUpdatePtr(IsPolicies(&domain_empty_others_default_))); | 276 OnPolicyUpdatePtr(IsPolicies(&domain_empty_others_default_))); |
| 234 | 277 |
| 278 SetPolicies(domain_empty_); |
| 235 StartWatching(); | 279 StartWatching(); |
| 236 policy_watcher_->SetPolicies(&domain_empty_); | |
| 237 StopWatching(); | 280 StopWatching(); |
| 238 } | 281 } |
| 239 | 282 |
| 240 TEST_F(PolicyWatcherTest, DomainFull) { | 283 TEST_F(PolicyWatcherTest, DomainFull) { |
| 241 EXPECT_CALL(mock_policy_callback_, | 284 EXPECT_CALL(mock_policy_callback_, |
| 242 OnPolicyUpdatePtr(IsPolicies(&domain_full_others_default_))); | 285 OnPolicyUpdatePtr(IsPolicies(&domain_full_others_default_))); |
| 243 | 286 |
| 287 SetPolicies(domain_full_); |
| 244 StartWatching(); | 288 StartWatching(); |
| 245 policy_watcher_->SetPolicies(&domain_full_); | |
| 246 StopWatching(); | 289 StopWatching(); |
| 247 } | 290 } |
| 248 | 291 |
| 249 TEST_F(PolicyWatcherTest, NatNoneThenTrue) { | 292 TEST_F(PolicyWatcherTest, NatNoneThenTrue) { |
| 250 EXPECT_CALL(mock_policy_callback_, | 293 EXPECT_CALL(mock_policy_callback_, |
| 251 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | 294 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 252 | 295 |
| 296 SetPolicies(empty_); |
| 253 StartWatching(); | 297 StartWatching(); |
| 254 policy_watcher_->SetPolicies(&empty_); | 298 SetPolicies(nat_true_); |
| 255 policy_watcher_->SetPolicies(&nat_true_); | |
| 256 StopWatching(); | 299 StopWatching(); |
| 257 } | 300 } |
| 258 | 301 |
| 259 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrue) { | 302 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrue) { |
| 260 EXPECT_CALL(mock_policy_callback_, | 303 EXPECT_CALL(mock_policy_callback_, |
| 261 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | 304 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 262 | 305 |
| 306 SetPolicies(empty_); |
| 263 StartWatching(); | 307 StartWatching(); |
| 264 policy_watcher_->SetPolicies(&empty_); | 308 SetPolicies(nat_true_); |
| 265 policy_watcher_->SetPolicies(&nat_true_); | 309 SetPolicies(nat_true_); |
| 266 policy_watcher_->SetPolicies(&nat_true_); | |
| 267 StopWatching(); | 310 StopWatching(); |
| 268 } | 311 } |
| 269 | 312 |
| 270 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrueThenFalse) { | 313 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrueThenFalse) { |
| 271 testing::InSequence sequence; | 314 testing::InSequence sequence; |
| 272 EXPECT_CALL(mock_policy_callback_, | 315 EXPECT_CALL(mock_policy_callback_, |
| 273 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | 316 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 274 EXPECT_CALL(mock_policy_callback_, | 317 EXPECT_CALL(mock_policy_callback_, |
| 275 OnPolicyUpdatePtr(IsPolicies(&nat_false_))); | 318 OnPolicyUpdatePtr(IsPolicies(&nat_false_))); |
| 276 | 319 |
| 320 SetPolicies(empty_); |
| 277 StartWatching(); | 321 StartWatching(); |
| 278 policy_watcher_->SetPolicies(&empty_); | 322 SetPolicies(nat_true_); |
| 279 policy_watcher_->SetPolicies(&nat_true_); | 323 SetPolicies(nat_true_); |
| 280 policy_watcher_->SetPolicies(&nat_true_); | 324 SetPolicies(nat_false_); |
| 281 policy_watcher_->SetPolicies(&nat_false_); | |
| 282 StopWatching(); | 325 StopWatching(); |
| 283 } | 326 } |
| 284 | 327 |
| 285 TEST_F(PolicyWatcherTest, NatNoneThenFalse) { | 328 TEST_F(PolicyWatcherTest, NatNoneThenFalse) { |
| 286 testing::InSequence sequence; | 329 testing::InSequence sequence; |
| 287 EXPECT_CALL(mock_policy_callback_, | 330 EXPECT_CALL(mock_policy_callback_, |
| 288 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | 331 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 289 EXPECT_CALL(mock_policy_callback_, | 332 EXPECT_CALL(mock_policy_callback_, |
| 290 OnPolicyUpdatePtr(IsPolicies(&nat_false_))); | 333 OnPolicyUpdatePtr(IsPolicies(&nat_false_))); |
| 291 | 334 |
| 335 SetPolicies(empty_); |
| 292 StartWatching(); | 336 StartWatching(); |
| 293 policy_watcher_->SetPolicies(&empty_); | 337 SetPolicies(nat_false_); |
| 294 policy_watcher_->SetPolicies(&nat_false_); | |
| 295 StopWatching(); | 338 StopWatching(); |
| 296 } | 339 } |
| 297 | 340 |
| 298 TEST_F(PolicyWatcherTest, NatNoneThenFalseThenTrue) { | 341 TEST_F(PolicyWatcherTest, NatNoneThenFalseThenTrue) { |
| 299 testing::InSequence sequence; | 342 testing::InSequence sequence; |
| 300 EXPECT_CALL(mock_policy_callback_, | 343 EXPECT_CALL(mock_policy_callback_, |
| 301 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | 344 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 302 EXPECT_CALL(mock_policy_callback_, | 345 EXPECT_CALL(mock_policy_callback_, |
| 303 OnPolicyUpdatePtr(IsPolicies(&nat_false_))); | 346 OnPolicyUpdatePtr(IsPolicies(&nat_false_))); |
| 304 EXPECT_CALL(mock_policy_callback_, | 347 EXPECT_CALL(mock_policy_callback_, |
| 305 OnPolicyUpdatePtr(IsPolicies(&nat_true_))); | 348 OnPolicyUpdatePtr(IsPolicies(&nat_true_))); |
| 306 | 349 |
| 350 SetPolicies(empty_); |
| 307 StartWatching(); | 351 StartWatching(); |
| 308 policy_watcher_->SetPolicies(&empty_); | 352 SetPolicies(nat_false_); |
| 309 policy_watcher_->SetPolicies(&nat_false_); | 353 SetPolicies(nat_true_); |
| 310 policy_watcher_->SetPolicies(&nat_true_); | |
| 311 StopWatching(); | 354 StopWatching(); |
| 312 } | 355 } |
| 313 | 356 |
| 314 TEST_F(PolicyWatcherTest, ChangeOneRepeatedlyThenTwo) { | 357 TEST_F(PolicyWatcherTest, ChangeOneRepeatedlyThenTwo) { |
| 315 testing::InSequence sequence; | 358 testing::InSequence sequence; |
| 316 EXPECT_CALL(mock_policy_callback_, | 359 EXPECT_CALL(mock_policy_callback_, |
| 317 OnPolicyUpdatePtr(IsPolicies( | 360 OnPolicyUpdatePtr(IsPolicies( |
| 318 &nat_true_domain_empty_others_default_))); | 361 &nat_true_domain_empty_others_default_))); |
| 319 EXPECT_CALL(mock_policy_callback_, | 362 EXPECT_CALL(mock_policy_callback_, |
| 320 OnPolicyUpdatePtr(IsPolicies(&domain_full_))); | 363 OnPolicyUpdatePtr(IsPolicies(&domain_full_))); |
| 321 EXPECT_CALL(mock_policy_callback_, | 364 EXPECT_CALL(mock_policy_callback_, |
| 322 OnPolicyUpdatePtr(IsPolicies(&nat_false_))); | 365 OnPolicyUpdatePtr(IsPolicies(&nat_false_))); |
| 323 EXPECT_CALL(mock_policy_callback_, | 366 EXPECT_CALL(mock_policy_callback_, |
| 324 OnPolicyUpdatePtr(IsPolicies(&domain_empty_))); | 367 OnPolicyUpdatePtr(IsPolicies(&domain_empty_))); |
| 325 EXPECT_CALL(mock_policy_callback_, | 368 EXPECT_CALL(mock_policy_callback_, |
| 326 OnPolicyUpdatePtr(IsPolicies(&nat_true_domain_full_))); | 369 OnPolicyUpdatePtr(IsPolicies(&nat_true_domain_full_))); |
| 327 | 370 |
| 371 SetPolicies(nat_true_domain_empty_); |
| 328 StartWatching(); | 372 StartWatching(); |
| 329 policy_watcher_->SetPolicies(&nat_true_domain_empty_); | 373 SetPolicies(nat_true_domain_full_); |
| 330 policy_watcher_->SetPolicies(&nat_true_domain_full_); | 374 SetPolicies(nat_false_domain_full_); |
| 331 policy_watcher_->SetPolicies(&nat_false_domain_full_); | 375 SetPolicies(nat_false_domain_empty_); |
| 332 policy_watcher_->SetPolicies(&nat_false_domain_empty_); | 376 SetPolicies(nat_true_domain_full_); |
| 333 policy_watcher_->SetPolicies(&nat_true_domain_full_); | |
| 334 StopWatching(); | 377 StopWatching(); |
| 335 } | 378 } |
| 336 | 379 |
| 337 TEST_F(PolicyWatcherTest, FilterUnknownPolicies) { | 380 TEST_F(PolicyWatcherTest, FilterUnknownPolicies) { |
| 338 testing::InSequence sequence; | 381 testing::InSequence sequence; |
| 339 EXPECT_CALL(mock_policy_callback_, | 382 EXPECT_CALL(mock_policy_callback_, |
| 340 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | 383 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 341 | 384 |
| 385 SetPolicies(empty_); |
| 342 StartWatching(); | 386 StartWatching(); |
| 343 policy_watcher_->SetPolicies(&empty_); | 387 SetPolicies(unknown_policies_); |
| 344 policy_watcher_->SetPolicies(&unknown_policies_); | 388 SetPolicies(empty_); |
| 345 policy_watcher_->SetPolicies(&empty_); | |
| 346 StopWatching(); | 389 StopWatching(); |
| 347 } | 390 } |
| 348 | 391 |
| 349 TEST_F(PolicyWatcherTest, DebugOverrideNatPolicy) { | 392 TEST_F(PolicyWatcherTest, DebugOverrideNatPolicy) { |
| 350 #if !defined(NDEBUG) | 393 #if !defined(NDEBUG) |
| 351 EXPECT_CALL(mock_policy_callback_, | 394 EXPECT_CALL( |
| 395 mock_policy_callback_, |
| 352 OnPolicyUpdatePtr(IsPolicies(&nat_false_overridden_others_default_))); | 396 OnPolicyUpdatePtr(IsPolicies(&nat_false_overridden_others_default_))); |
| 353 #else | 397 #else |
| 354 EXPECT_CALL(mock_policy_callback_, | 398 EXPECT_CALL(mock_policy_callback_, |
| 355 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | 399 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 356 #endif | 400 #endif |
| 357 | 401 |
| 402 SetPolicies(nat_true_and_overridden_); |
| 358 StartWatching(); | 403 StartWatching(); |
| 359 policy_watcher_->SetPolicies(&nat_true_and_overridden_); | |
| 360 StopWatching(); | 404 StopWatching(); |
| 361 } | 405 } |
| 362 | 406 |
| 363 TEST_F(PolicyWatcherTest, PairingFalseThenTrue) { | 407 TEST_F(PolicyWatcherTest, PairingFalseThenTrue) { |
| 364 testing::InSequence sequence; | 408 testing::InSequence sequence; |
| 365 EXPECT_CALL(mock_policy_callback_, | 409 EXPECT_CALL(mock_policy_callback_, |
| 366 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | 410 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 367 EXPECT_CALL(mock_policy_callback_, | 411 EXPECT_CALL(mock_policy_callback_, |
| 368 OnPolicyUpdatePtr(IsPolicies(&pairing_false_))); | 412 OnPolicyUpdatePtr(IsPolicies(&pairing_false_))); |
| 369 EXPECT_CALL(mock_policy_callback_, | 413 EXPECT_CALL(mock_policy_callback_, |
| 370 OnPolicyUpdatePtr(IsPolicies(&pairing_true_))); | 414 OnPolicyUpdatePtr(IsPolicies(&pairing_true_))); |
| 371 | 415 |
| 416 SetPolicies(empty_); |
| 372 StartWatching(); | 417 StartWatching(); |
| 373 policy_watcher_->SetPolicies(&empty_); | 418 SetPolicies(pairing_false_); |
| 374 policy_watcher_->SetPolicies(&pairing_false_); | 419 SetPolicies(pairing_true_); |
| 375 policy_watcher_->SetPolicies(&pairing_true_); | |
| 376 StopWatching(); | 420 StopWatching(); |
| 377 } | 421 } |
| 378 | 422 |
| 379 TEST_F(PolicyWatcherTest, GnubbyAuth) { | 423 TEST_F(PolicyWatcherTest, GnubbyAuth) { |
| 380 testing::InSequence sequence; | 424 testing::InSequence sequence; |
| 381 EXPECT_CALL(mock_policy_callback_, | 425 EXPECT_CALL(mock_policy_callback_, |
| 382 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | 426 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 383 EXPECT_CALL(mock_policy_callback_, | 427 EXPECT_CALL(mock_policy_callback_, |
| 384 OnPolicyUpdatePtr(IsPolicies(&gnubby_auth_false_))); | 428 OnPolicyUpdatePtr(IsPolicies(&gnubby_auth_false_))); |
| 385 EXPECT_CALL(mock_policy_callback_, | 429 EXPECT_CALL(mock_policy_callback_, |
| 386 OnPolicyUpdatePtr(IsPolicies(&gnubby_auth_true_))); | 430 OnPolicyUpdatePtr(IsPolicies(&gnubby_auth_true_))); |
| 387 | 431 |
| 432 SetPolicies(empty_); |
| 388 StartWatching(); | 433 StartWatching(); |
| 389 policy_watcher_->SetPolicies(&empty_); | 434 SetPolicies(gnubby_auth_false_); |
| 390 policy_watcher_->SetPolicies(&gnubby_auth_false_); | 435 SetPolicies(gnubby_auth_true_); |
| 391 policy_watcher_->SetPolicies(&gnubby_auth_true_); | |
| 392 StopWatching(); | 436 StopWatching(); |
| 393 } | 437 } |
| 394 | 438 |
| 395 TEST_F(PolicyWatcherTest, Relay) { | 439 TEST_F(PolicyWatcherTest, Relay) { |
| 396 testing::InSequence sequence; | 440 testing::InSequence sequence; |
| 397 EXPECT_CALL(mock_policy_callback_, | 441 EXPECT_CALL(mock_policy_callback_, |
| 398 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | 442 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 399 EXPECT_CALL(mock_policy_callback_, | 443 EXPECT_CALL(mock_policy_callback_, |
| 400 OnPolicyUpdatePtr(IsPolicies(&relay_false_))); | 444 OnPolicyUpdatePtr(IsPolicies(&relay_false_))); |
| 401 EXPECT_CALL(mock_policy_callback_, | 445 EXPECT_CALL(mock_policy_callback_, |
| 402 OnPolicyUpdatePtr(IsPolicies(&relay_true_))); | 446 OnPolicyUpdatePtr(IsPolicies(&relay_true_))); |
| 403 | 447 |
| 448 SetPolicies(empty_); |
| 404 StartWatching(); | 449 StartWatching(); |
| 405 policy_watcher_->SetPolicies(&empty_); | 450 SetPolicies(relay_false_); |
| 406 policy_watcher_->SetPolicies(&relay_false_); | 451 SetPolicies(relay_true_); |
| 407 policy_watcher_->SetPolicies(&relay_true_); | |
| 408 StopWatching(); | 452 StopWatching(); |
| 409 } | 453 } |
| 410 | 454 |
| 411 TEST_F(PolicyWatcherTest, UdpPortRange) { | 455 TEST_F(PolicyWatcherTest, UdpPortRange) { |
| 412 testing::InSequence sequence; | 456 testing::InSequence sequence; |
| 413 EXPECT_CALL(mock_policy_callback_, | 457 EXPECT_CALL(mock_policy_callback_, |
| 414 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | 458 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); |
| 415 EXPECT_CALL(mock_policy_callback_, | 459 EXPECT_CALL(mock_policy_callback_, |
| 416 OnPolicyUpdatePtr(IsPolicies(&port_range_full_))); | 460 OnPolicyUpdatePtr(IsPolicies(&port_range_full_))); |
| 417 EXPECT_CALL(mock_policy_callback_, | 461 EXPECT_CALL(mock_policy_callback_, |
| 418 OnPolicyUpdatePtr(IsPolicies(&port_range_empty_))); | 462 OnPolicyUpdatePtr(IsPolicies(&port_range_empty_))); |
| 419 | 463 |
| 464 SetPolicies(empty_); |
| 420 StartWatching(); | 465 StartWatching(); |
| 421 policy_watcher_->SetPolicies(&empty_); | 466 SetPolicies(port_range_full_); |
| 422 policy_watcher_->SetPolicies(&port_range_full_); | 467 SetPolicies(port_range_empty_); |
| 423 policy_watcher_->SetPolicies(&port_range_empty_); | |
| 424 StopWatching(); | 468 StopWatching(); |
| 425 } | 469 } |
| 426 | 470 |
| 427 const int kMaxTransientErrorRetries = 5; | 471 const int kMaxTransientErrorRetries = 5; |
| 428 | 472 |
| 429 TEST_F(PolicyWatcherTest, SingleTransientErrorDoesntTriggerErrorCallback) { | 473 TEST_F(PolicyWatcherTest, SingleTransientErrorDoesntTriggerErrorCallback) { |
| 430 EXPECT_CALL(mock_policy_callback_, OnPolicyErrorPtr()).Times(0); | 474 EXPECT_CALL(mock_policy_callback_, OnPolicyErrorPtr()).Times(0); |
| 431 | 475 |
| 432 StartWatching(); | 476 StartWatching(); |
| 433 policy_watcher_->SignalTransientErrorForTest(); | 477 SignalTransientErrorForTest(); |
| 434 StopWatching(); | 478 StopWatching(); |
| 435 } | 479 } |
| 436 | 480 |
| 437 TEST_F(PolicyWatcherTest, MultipleTransientErrorsTriggerErrorCallback) { | 481 TEST_F(PolicyWatcherTest, MultipleTransientErrorsTriggerErrorCallback) { |
| 438 EXPECT_CALL(mock_policy_callback_, OnPolicyErrorPtr()); | 482 EXPECT_CALL(mock_policy_callback_, OnPolicyErrorPtr()); |
| 439 | 483 |
| 440 StartWatching(); | 484 StartWatching(); |
| 441 for (int i = 0; i < kMaxTransientErrorRetries; i++) { | 485 for (int i = 0; i < kMaxTransientErrorRetries; i++) { |
| 442 policy_watcher_->SignalTransientErrorForTest(); | 486 SignalTransientErrorForTest(); |
| 443 } | 487 } |
| 444 StopWatching(); | 488 StopWatching(); |
| 445 } | 489 } |
| 446 | 490 |
| 447 TEST_F(PolicyWatcherTest, PolicyUpdateResetsTransientErrorsCounter) { | 491 TEST_F(PolicyWatcherTest, PolicyUpdateResetsTransientErrorsCounter) { |
| 448 testing::InSequence s; | 492 testing::InSequence s; |
| 449 EXPECT_CALL(mock_policy_callback_, | 493 EXPECT_CALL(mock_policy_callback_, OnPolicyUpdatePtr(testing::_)); |
| 450 OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_))); | |
| 451 EXPECT_CALL(mock_policy_callback_, OnPolicyErrorPtr()).Times(0); | 494 EXPECT_CALL(mock_policy_callback_, OnPolicyErrorPtr()).Times(0); |
| 452 | 495 |
| 453 StartWatching(); | 496 StartWatching(); |
| 454 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { | 497 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { |
| 455 policy_watcher_->SignalTransientErrorForTest(); | 498 SignalTransientErrorForTest(); |
| 456 } | 499 } |
| 457 policy_watcher_->SetPolicies(&nat_true_); | 500 SetPolicies(nat_true_); |
| 458 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { | 501 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { |
| 459 policy_watcher_->SignalTransientErrorForTest(); | 502 SignalTransientErrorForTest(); |
| 460 } | 503 } |
| 461 StopWatching(); | 504 StopWatching(); |
| 462 } | 505 } |
| 463 | 506 |
| 464 // Unit tests cannot instantiate PolicyWatcher on ChromeOS | 507 // Unit tests cannot instantiate PolicyWatcher on ChromeOS |
| 465 // (as this requires running inside a browser process). | 508 // (as this requires running inside a browser process). |
| 466 #ifndef OS_CHROMEOS | 509 #ifndef OS_CHROMEOS |
| 467 | 510 |
| 468 namespace { | 511 namespace { |
| 469 | 512 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 run_loop.RunUntilIdle(); | 566 run_loop.RunUntilIdle(); |
| 524 } | 567 } |
| 525 | 568 |
| 526 // Today, the only verification offered by this test is: | 569 // Today, the only verification offered by this test is: |
| 527 // - Manual verification of policy values dumped by OnPolicyUpdatedDumpPolicy | 570 // - Manual verification of policy values dumped by OnPolicyUpdatedDumpPolicy |
| 528 // - Automated verification that nothing crashed | 571 // - Automated verification that nothing crashed |
| 529 } | 572 } |
| 530 | 573 |
| 531 #endif | 574 #endif |
| 532 | 575 |
| 533 // TODO(lukasza): We should consider adding a test against a | |
| 534 // MockConfigurationPolicyProvider. | |
| 535 | |
| 536 } // namespace policy_hack | 576 } // namespace policy_hack |
| 537 } // namespace remoting | 577 } // namespace remoting |
| OLD | NEW |