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 <iostream> | |
6 | |
5 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
6 #include "base/bind.h" | 8 #include "base/bind.h" |
7 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
9 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
10 #include "remoting/host/dns_blackhole_checker.h" | 12 #include "remoting/host/dns_blackhole_checker.h" |
11 #include "remoting/host/policy_hack/fake_policy_watcher.h" | 13 #include "remoting/host/policy_hack/fake_policy_watcher.h" |
12 #include "remoting/host/policy_hack/mock_policy_callback.h" | 14 #include "remoting/host/policy_hack/mock_policy_callback.h" |
13 #include "remoting/host/policy_hack/policy_watcher.h" | 15 #include "remoting/host/policy_hack/policy_watcher.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 namespace remoting { | 19 namespace remoting { |
18 namespace policy_hack { | 20 namespace policy_hack { |
19 | 21 |
20 class PolicyWatcherTest : public testing::Test { | 22 class PolicyWatcherTest : public testing::Test { |
21 public: | 23 public: |
22 PolicyWatcherTest() { | 24 PolicyWatcherTest() : message_loop_(base::MessageLoop::TYPE_IO) {} |
23 } | |
24 | 25 |
25 void SetUp() override { | 26 void SetUp() override { |
26 message_loop_proxy_ = base::MessageLoopProxy::current(); | 27 message_loop_proxy_ = base::MessageLoopProxy::current(); |
27 policy_updated_callback_ = base::Bind( | 28 policy_updated_callback_ = base::Bind( |
28 &MockPolicyCallback::OnPolicyUpdate, | 29 &MockPolicyCallback::OnPolicyUpdate, |
29 base::Unretained(&mock_policy_callback_)); | 30 base::Unretained(&mock_policy_callback_)); |
30 policy_error_callback_ = base::Bind( | 31 policy_error_callback_ = base::Bind( |
31 &MockPolicyCallback::OnPolicyError, | 32 &MockPolicyCallback::OnPolicyError, |
32 base::Unretained(&mock_policy_callback_)); | 33 base::Unretained(&mock_policy_callback_)); |
33 policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_)); | 34 policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_)); |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
441 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { | 442 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { |
442 policy_watcher_->SignalTransientErrorForTest(); | 443 policy_watcher_->SignalTransientErrorForTest(); |
443 } | 444 } |
444 policy_watcher_->SetPolicies(&nat_true_); | 445 policy_watcher_->SetPolicies(&nat_true_); |
445 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { | 446 for (int i = 0; i < (kMaxTransientErrorRetries - 1); i++) { |
446 policy_watcher_->SignalTransientErrorForTest(); | 447 policy_watcher_->SignalTransientErrorForTest(); |
447 } | 448 } |
448 StopWatching(); | 449 StopWatching(); |
449 } | 450 } |
450 | 451 |
452 // Unit tests cannot instantiate PolicyWatcher on ChromeOS | |
453 // (as this requires running inside a browser process). | |
Mattias Nissler (ping if slow)
2015/01/06 09:06:12
You could create a fake PolicyService and test aga
Łukasz Anforowicz
2015/01/07 17:54:15
If PolicyServiceWatcher is here for the long term,
Mattias Nissler (ping if slow)
2015/01/08 09:58:36
OK, fair enough.
| |
454 #ifndef OS_CHROMEOS | |
455 | |
456 namespace { | |
457 | |
458 void OnPolicyUpdatedDumpPolicy(scoped_ptr<base::DictionaryValue> policies) { | |
459 if (!VLOG_IS_ON(1)) { | |
460 return; | |
461 } | |
462 | |
463 std::cout << "OnPolicyUpdated callback received the following policies:" | |
464 << std::endl; | |
465 | |
466 for (base::DictionaryValue::Iterator iter(*policies); !iter.IsAtEnd(); | |
467 iter.Advance()) { | |
468 std::cout << iter.key() << " = "; | |
469 switch (iter.value().GetType()) { | |
470 case base::Value::Type::TYPE_STRING: { | |
471 std::string value; | |
472 CHECK(iter.value().GetAsString(&value)); | |
473 std::cout << "string: " << '"' << value << '"'; | |
474 break; | |
475 } | |
476 case base::Value::Type::TYPE_BOOLEAN: { | |
477 bool value; | |
478 CHECK(iter.value().GetAsBoolean(&value)); | |
479 std::cout << "boolean: " << (value ? "True" : "False"); | |
480 break; | |
481 } | |
482 default: { | |
483 std::cout << "unrecognized type"; | |
484 break; | |
485 } | |
486 } | |
487 std::cout << std::endl; | |
488 } | |
489 } | |
490 | |
491 } // anonymous namespace | |
492 | |
493 // To dump policy contents, run unit tests with the following flags: | |
494 // --gtest_filter=*TestRealChromotingPolicy* -v=1 | |
495 TEST_F(PolicyWatcherTest, TestRealChromotingPolicy) { | |
496 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | |
497 base::MessageLoop::current()->task_runner(); | |
498 scoped_ptr<PolicyWatcher> policy_watcher( | |
499 PolicyWatcher::Create(nullptr, task_runner)); | |
500 | |
501 { | |
502 base::RunLoop run_loop; | |
503 policy_watcher->StartWatching(base::Bind(OnPolicyUpdatedDumpPolicy), | |
504 base::Bind(base::DoNothing)); | |
505 run_loop.RunUntilIdle(); | |
506 } | |
507 | |
508 { | |
509 base::RunLoop run_loop; | |
510 PolicyWatcher* raw_policy_watcher = policy_watcher.release(); | |
511 raw_policy_watcher->StopWatching( | |
512 base::Bind(base::IgnoreResult( | |
513 &base::SequencedTaskRunner::DeleteSoon<PolicyWatcher>), | |
514 task_runner, FROM_HERE, raw_policy_watcher)); | |
515 run_loop.RunUntilIdle(); | |
516 } | |
517 | |
518 // Today, the only verification offered by this test is: | |
519 // - Manual verification of policy values dumped by OnPolicyUpdatedDumpPolicy | |
520 // - Automated verification that nothing crashed | |
Mattias Nissler (ping if slow)
2015/01/06 09:06:12
It may make sense to add a test that tests PolicyS
Łukasz Anforowicz
2015/01/07 17:54:15
Yes, but see my other comment for explanation why
Mattias Nissler (ping if slow)
2015/01/08 09:58:36
OK, might still want to document proposed improvem
Łukasz Anforowicz
2015/01/08 23:09:26
I thought about it and the test against a MockConf
| |
521 } | |
522 | |
523 #endif | |
524 | |
451 } // namespace policy_hack | 525 } // namespace policy_hack |
452 } // namespace remoting | 526 } // namespace remoting |
OLD | NEW |