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 "policy/policy_constants.h" | 10 #include "policy/policy_constants.h" |
11 #include "remoting/host/dns_blackhole_checker.h" | 11 #include "remoting/host/dns_blackhole_checker.h" |
12 #include "remoting/host/policy_hack/fake_policy_watcher.h" | 12 #include "remoting/host/policy_hack/fake_policy_watcher.h" |
13 #include "remoting/host/policy_hack/mock_policy_callback.h" | 13 #include "remoting/host/policy_hack/mock_policy_callback.h" |
14 #include "remoting/host/policy_hack/policy_watcher.h" | 14 #include "remoting/host/policy_hack/policy_watcher.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 namespace remoting { | 18 namespace remoting { |
19 namespace policy_hack { | 19 namespace policy_hack { |
20 | 20 |
21 class PolicyWatcherTest : public testing::Test { | 21 class PolicyWatcherTest : public testing::Test { |
22 public: | 22 public: |
23 PolicyWatcherTest() { | 23 PolicyWatcherTest() : message_loop_(base::MessageLoop::TYPE_IO) {} |
24 } | |
25 | 24 |
26 void SetUp() override { | 25 void SetUp() override { |
27 message_loop_proxy_ = base::MessageLoopProxy::current(); | 26 message_loop_proxy_ = base::MessageLoopProxy::current(); |
28 policy_updated_callback_ = base::Bind( | 27 policy_updated_callback_ = base::Bind( |
29 &MockPolicyCallback::OnPolicyUpdate, | 28 &MockPolicyCallback::OnPolicyUpdate, |
30 base::Unretained(&mock_policy_callback_)); | 29 base::Unretained(&mock_policy_callback_)); |
31 policy_error_callback_ = base::Bind( | 30 policy_error_callback_ = base::Bind( |
32 &MockPolicyCallback::OnPolicyError, | 31 &MockPolicyCallback::OnPolicyError, |
33 base::Unretained(&mock_policy_callback_)); | 32 base::Unretained(&mock_policy_callback_)); |
34 policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_)); | 33 policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_)); |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { | 454 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { |
456 policy_watcher_->SignalTransientErrorForTest(); | 455 policy_watcher_->SignalTransientErrorForTest(); |
457 } | 456 } |
458 policy_watcher_->SetPolicies(&nat_true_); | 457 policy_watcher_->SetPolicies(&nat_true_); |
459 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { | 458 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { |
460 policy_watcher_->SignalTransientErrorForTest(); | 459 policy_watcher_->SignalTransientErrorForTest(); |
461 } | 460 } |
462 StopWatching(); | 461 StopWatching(); |
463 } | 462 } |
464 | 463 |
| 464 // Unit tests cannot instantiate PolicyWatcher on ChromeOS |
| 465 // (as this requires running inside a browser process). |
| 466 #ifndef OS_CHROMEOS |
| 467 |
| 468 namespace { |
| 469 |
| 470 void OnPolicyUpdatedDumpPolicy(scoped_ptr<base::DictionaryValue> policies) { |
| 471 VLOG(1) << "OnPolicyUpdated callback received the following policies:"; |
| 472 |
| 473 for (base::DictionaryValue::Iterator iter(*policies); !iter.IsAtEnd(); |
| 474 iter.Advance()) { |
| 475 switch (iter.value().GetType()) { |
| 476 case base::Value::Type::TYPE_STRING: { |
| 477 std::string value; |
| 478 CHECK(iter.value().GetAsString(&value)); |
| 479 VLOG(1) << iter.key() << " = " |
| 480 << "string: " << '"' << value << '"'; |
| 481 break; |
| 482 } |
| 483 case base::Value::Type::TYPE_BOOLEAN: { |
| 484 bool value; |
| 485 CHECK(iter.value().GetAsBoolean(&value)); |
| 486 VLOG(1) << iter.key() << " = " |
| 487 << "boolean: " << (value ? "True" : "False"); |
| 488 break; |
| 489 } |
| 490 default: { |
| 491 VLOG(1) << iter.key() << " = " |
| 492 << "unrecognized type"; |
| 493 break; |
| 494 } |
| 495 } |
| 496 } |
| 497 } |
| 498 |
| 499 } // anonymous namespace |
| 500 |
| 501 // To dump policy contents, run unit tests with the following flags: |
| 502 // out/Debug/remoting_unittests --gtest_filter=*TestRealChromotingPolicy* -v=1 |
| 503 TEST_F(PolicyWatcherTest, TestRealChromotingPolicy) { |
| 504 scoped_refptr<base::SingleThreadTaskRunner> task_runner = |
| 505 base::MessageLoop::current()->task_runner(); |
| 506 scoped_ptr<PolicyWatcher> policy_watcher( |
| 507 PolicyWatcher::Create(nullptr, task_runner)); |
| 508 |
| 509 { |
| 510 base::RunLoop run_loop; |
| 511 policy_watcher->StartWatching(base::Bind(OnPolicyUpdatedDumpPolicy), |
| 512 base::Bind(base::DoNothing)); |
| 513 run_loop.RunUntilIdle(); |
| 514 } |
| 515 |
| 516 { |
| 517 base::RunLoop run_loop; |
| 518 PolicyWatcher* raw_policy_watcher = policy_watcher.release(); |
| 519 raw_policy_watcher->StopWatching( |
| 520 base::Bind(base::IgnoreResult( |
| 521 &base::SequencedTaskRunner::DeleteSoon<PolicyWatcher>), |
| 522 task_runner, FROM_HERE, raw_policy_watcher)); |
| 523 run_loop.RunUntilIdle(); |
| 524 } |
| 525 |
| 526 // Today, the only verification offered by this test is: |
| 527 // - Manual verification of policy values dumped by OnPolicyUpdatedDumpPolicy |
| 528 // - Automated verification that nothing crashed |
| 529 } |
| 530 |
| 531 #endif |
| 532 |
| 533 // TODO(lukasza): We should consider adding a test against a |
| 534 // MockConfigurationPolicyProvider. |
| 535 |
465 } // namespace policy_hack | 536 } // namespace policy_hack |
466 } // namespace remoting | 537 } // namespace remoting |
OLD | NEW |