OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/metrics/field_trial.h" | 6 #include "base/metrics/field_trial.h" |
7 #include "base/prefs/pref_registry_simple.h" | |
8 #include "base/prefs/pref_service.h" | |
9 #include "base/prefs/testing_pref_service.h" | |
10 #include "base/run_loop.h" | |
7 #include "base/test/mock_entropy_provider.h" | 11 #include "base/test/mock_entropy_provider.h" |
12 #include "chrome/browser/extensions/event_router_forwarder.h" | |
asanka
2015/11/25 19:00:09
Extensions code including dependencies should be c
aberent
2015/11/26 15:58:00
Done.
| |
8 #include "chrome/browser/io_thread.h" | 13 #include "chrome/browser/io_thread.h" |
9 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/common/pref_names.h" | |
10 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s.h" | 16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s.h" |
17 #include "components/policy/core/common/mock_policy_service.h" | |
18 #include "components/proxy_config/pref_proxy_config_tracker_impl.h" | |
19 #include "components/proxy_config/proxy_config_pref_names.h" | |
20 #include "content/public/browser/browser_thread.h" | |
21 #include "content/public/test/test_browser_thread_bundle.h" | |
22 #include "net/http/http_auth_preferences.h" | |
23 #include "net/http/http_auth_scheme.h" | |
11 #include "net/http/http_network_session.h" | 24 #include "net/http/http_network_session.h" |
12 #include "net/http/http_server_properties_impl.h" | 25 #include "net/http/http_server_properties_impl.h" |
13 #include "net/quic/quic_protocol.h" | 26 #include "net/quic/quic_protocol.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
16 | 29 |
30 #if defined(OS_CHROMEOS) | |
31 #include "chromeos/dbus/dbus_thread_manager.h" | |
32 #include "chromeos/network/network_handler.h" | |
33 #endif | |
34 | |
17 namespace test { | 35 namespace test { |
18 | 36 |
19 using ::testing::ElementsAre; | 37 using ::testing::ElementsAre; |
38 using ::testing::ReturnRef; | |
20 | 39 |
40 // Class used for accessing IOThread methods (friend of IOThread). | |
21 class IOThreadPeer { | 41 class IOThreadPeer { |
22 public: | 42 public: |
23 static void ConfigureQuicGlobals( | 43 static void ConfigureQuicGlobals( |
24 const base::CommandLine& command_line, | 44 const base::CommandLine& command_line, |
25 base::StringPiece quic_trial_group, | 45 base::StringPiece quic_trial_group, |
26 const std::map<std::string, std::string>& quic_trial_params, | 46 const std::map<std::string, std::string>& quic_trial_params, |
27 bool is_quic_allowed_by_policy, | 47 bool is_quic_allowed_by_policy, |
28 IOThread::Globals* globals) { | 48 IOThread::Globals* globals) { |
29 IOThread::ConfigureQuicGlobals(command_line, quic_trial_group, | 49 IOThread::ConfigureQuicGlobals(command_line, quic_trial_group, |
30 quic_trial_params, is_quic_allowed_by_policy, | 50 quic_trial_params, is_quic_allowed_by_policy, |
(...skipping 12 matching lines...) Expand all Loading... | |
43 static void ConfigureNPNGlobals(base::StringPiece npn_trial_group, | 63 static void ConfigureNPNGlobals(base::StringPiece npn_trial_group, |
44 IOThread::Globals* globals) { | 64 IOThread::Globals* globals) { |
45 IOThread::ConfigureNPNGlobals(npn_trial_group, globals); | 65 IOThread::ConfigureNPNGlobals(npn_trial_group, globals); |
46 } | 66 } |
47 | 67 |
48 static void InitializeNetworkSessionParamsFromGlobals( | 68 static void InitializeNetworkSessionParamsFromGlobals( |
49 const IOThread::Globals& globals, | 69 const IOThread::Globals& globals, |
50 net::HttpNetworkSession::Params* params) { | 70 net::HttpNetworkSession::Params* params) { |
51 IOThread::InitializeNetworkSessionParamsFromGlobals(globals, params); | 71 IOThread::InitializeNetworkSessionParamsFromGlobals(globals, params); |
52 } | 72 } |
73 | |
74 static void InitIOThread(IOThread* io_thread) { io_thread->Init(); } | |
75 | |
76 static void CleanUpIOThread(IOThread* io_thread) { io_thread->CleanUp(); } | |
77 | |
78 static net::HttpAuthPreferences* GetAuthPreferences(IOThread* io_thread) { | |
79 return io_thread->globals()->http_auth_preferences.get(); | |
80 } | |
53 }; | 81 }; |
54 | 82 |
55 class IOThreadTest : public testing::Test { | 83 class IOThreadTest : public testing::Test { |
56 public: | 84 public: |
57 IOThreadTest() | 85 IOThreadTest() |
58 : command_line_(base::CommandLine::NO_PROGRAM), | 86 : command_line_(base::CommandLine::NO_PROGRAM), |
59 is_quic_allowed_by_policy_(true) { | 87 is_quic_allowed_by_policy_(true) { |
60 globals_.http_server_properties.reset(new net::HttpServerPropertiesImpl()); | 88 globals_.http_server_properties.reset(new net::HttpServerPropertiesImpl()); |
61 } | 89 } |
62 | 90 |
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
516 TEST_F(IOThreadTest, QuicDisallowedByPolicy) { | 544 TEST_F(IOThreadTest, QuicDisallowedByPolicy) { |
517 command_line_.AppendSwitch(switches::kEnableQuic); | 545 command_line_.AppendSwitch(switches::kEnableQuic); |
518 is_quic_allowed_by_policy_ = false; | 546 is_quic_allowed_by_policy_ = false; |
519 ConfigureQuicGlobals(); | 547 ConfigureQuicGlobals(); |
520 | 548 |
521 net::HttpNetworkSession::Params params; | 549 net::HttpNetworkSession::Params params; |
522 InitializeNetworkSessionParams(¶ms); | 550 InitializeNetworkSessionParams(¶ms); |
523 EXPECT_FALSE(params.enable_quic); | 551 EXPECT_FALSE(params.enable_quic); |
524 } | 552 } |
525 | 553 |
554 class IOThreadTestWithIOThreadObject : public testing::Test { | |
555 public: | |
556 // These functions need to be public, since it is difficult to bind to | |
557 // protected functions in a test (the code would need to explicitly contain | |
558 // the name of the actual test class). | |
559 void CheckCnameLookup(bool expected) { | |
560 auto http_auth_preferences = | |
561 IOThreadPeer::GetAuthPreferences(io_thread_.get()); | |
562 ASSERT_NE(nullptr, http_auth_preferences); | |
563 EXPECT_EQ(expected, | |
564 http_auth_preferences->negotiate_disable_cname_lookup()); | |
565 } | |
566 | |
567 void CheckNegotiateEnablePort(bool expected) { | |
568 auto http_auth_preferences = | |
569 IOThreadPeer::GetAuthPreferences(io_thread_.get()); | |
570 ASSERT_NE(nullptr, http_auth_preferences); | |
571 EXPECT_EQ(expected, http_auth_preferences->negotiate_enable_port()); | |
572 } | |
573 | |
574 #if defined(OS_ANDROID) | |
575 void CheckAuthAndroidNegoitateAccountType(std::string expected) { | |
576 auto http_auth_preferences = | |
577 IOThreadPeer::GetAuthPreferences(io_thread_.get()); | |
578 ASSERT_NE(nullptr, http_auth_preferences); | |
579 EXPECT_EQ(expected, | |
580 http_auth_preferences->auth_android_negotiate_account_type()); | |
581 } | |
582 #endif | |
583 | |
584 void CheckCanUseDefaultCredentials(bool expected, const GURL& url) { | |
585 auto http_auth_preferences = | |
586 IOThreadPeer::GetAuthPreferences(io_thread_.get()); | |
587 EXPECT_EQ(expected, http_auth_preferences->CanUseDefaultCredentials(url)); | |
588 } | |
589 | |
590 void CheckCanDelegate(bool expected, const GURL& url) { | |
591 auto http_auth_preferences = | |
592 IOThreadPeer::GetAuthPreferences(io_thread_.get()); | |
593 EXPECT_EQ(expected, http_auth_preferences->CanDelegate(url)); | |
594 } | |
595 | |
596 protected: | |
597 IOThreadTestWithIOThreadObject() | |
598 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) { | |
599 #if defined(ENABLE_EXTENSIONS) | |
asanka
2015/11/25 19:00:09
Let's move initialization / teardown into SetUp()/
aberent
2015/11/26 15:58:00
Why? The googletest documentation definitely recom
asanka
2015/12/01 05:30:03
Suggestion retracted.
The suggestion was for cons
| |
600 event_router_forwarder_ = new extensions::EventRouterForwarder; | |
601 #endif | |
602 PrefRegistrySimple* pref_registry = pref_service_.registry(); | |
603 IOThread::RegisterPrefs(pref_registry); | |
604 PrefProxyConfigTrackerImpl::RegisterPrefs(pref_registry); | |
605 ssl_config::SSLConfigServiceManager::RegisterPrefs(pref_registry); | |
606 | |
607 // Set up default function behaviour. | |
608 EXPECT_CALL(policy_service_, | |
609 GetPolicies(policy::PolicyNamespace( | |
610 policy::POLICY_DOMAIN_CHROME, std::string()))) | |
611 .WillRepeatedly(ReturnRef(policy_map_)); | |
612 | |
613 #if defined(OS_CHROMEOS) | |
614 // Needed by IOThread constructor. | |
615 chromeos::DBusThreadManager::Initialize(); | |
616 chromeos::NetworkHandler::Initialize(); | |
617 #endif | |
618 io_thread_.reset(new IOThread(&pref_service_, &policy_service_, nullptr, | |
619 event_router_forwarder_.get())); | |
620 // Init must be run on the IO thread. | |
621 RunOnIOThreadBlocking( | |
622 base::Bind(&IOThreadPeer::InitIOThread, io_thread_.get())); | |
623 } | |
624 | |
625 ~IOThreadTestWithIOThreadObject() override { | |
626 RunOnIOThreadBlocking( | |
627 base::Bind(&IOThreadPeer::CleanUpIOThread, io_thread_.get())); | |
628 #if defined(OS_CHROMEOS) | |
629 chromeos::NetworkHandler::Shutdown(); | |
630 chromeos::DBusThreadManager::Shutdown(); | |
631 #endif | |
632 } | |
633 TestingPrefServiceSimple* pref_service() { return &pref_service_; } | |
634 | |
635 IOThread* io_thread() { return io_thread_.get(); } | |
asanka
2015/11/25 19:00:09
Remove this accessor since it can't be safely used
aberent
2015/11/26 15:58:00
Done.
| |
636 | |
637 void RunOnIOThreadBlocking(base::Closure task) { | |
638 base::RunLoop run_loop; | |
639 content::BrowserThread::PostTaskAndReply( | |
640 content::BrowserThread::IO, FROM_HERE, task, run_loop.QuitClosure()); | |
641 run_loop.Run(); | |
642 } | |
643 | |
644 private: | |
645 content::TestBrowserThreadBundle thread_bundle_; | |
646 TestingPrefServiceSimple pref_service_; | |
647 scoped_refptr<extensions::EventRouterForwarder> event_router_forwarder_; | |
648 policy::PolicyMap policy_map_; | |
649 policy::MockPolicyService policy_service_; | |
650 scoped_ptr<IOThread> io_thread_; | |
651 IOThread::Globals globals_; | |
652 }; | |
653 | |
654 TEST_F(IOThreadTestWithIOThreadObject, UpdateNegotiateDisableCnameLookup) { | |
655 // This test uses the kDisableAuthNegotiateCnameLookup to check that | |
656 // the HttpAuthPreferences are correctly initialized and running on the | |
657 // IO thread. The other preferences are tested by the HttpAuthPreferences | |
658 // unit tests. | |
659 pref_service()->SetBoolean(prefs::kDisableAuthNegotiateCnameLookup, false); | |
660 RunOnIOThreadBlocking( | |
661 base::Bind(&IOThreadTestWithIOThreadObject::CheckCnameLookup, | |
662 base::Unretained(this), false)); | |
663 pref_service()->SetBoolean(prefs::kDisableAuthNegotiateCnameLookup, true); | |
664 RunOnIOThreadBlocking( | |
665 base::Bind(&IOThreadTestWithIOThreadObject::CheckCnameLookup, | |
666 base::Unretained(this), true)); | |
667 } | |
668 | |
669 TEST_F(IOThreadTestWithIOThreadObject, UpdateEnableAuthNegotiatePort) { | |
670 pref_service()->SetBoolean(prefs::kEnableAuthNegotiatePort, false); | |
671 RunOnIOThreadBlocking( | |
672 base::Bind(&IOThreadTestWithIOThreadObject::CheckNegotiateEnablePort, | |
673 base::Unretained(this), false)); | |
674 pref_service()->SetBoolean(prefs::kEnableAuthNegotiatePort, true); | |
675 RunOnIOThreadBlocking( | |
676 base::Bind(&IOThreadTestWithIOThreadObject::CheckNegotiateEnablePort, | |
677 base::Unretained(this), true)); | |
678 } | |
679 | |
680 TEST_F(IOThreadTestWithIOThreadObject, UpdateServerWhitelist) { | |
681 GURL url("http://test.example.com"); | |
682 | |
683 pref_service()->SetString(prefs::kAuthServerWhitelist, ""); | |
684 RunOnIOThreadBlocking( | |
685 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanUseDefaultCredentials, | |
686 base::Unretained(this), false, url)); | |
687 | |
688 pref_service()->SetString(prefs::kAuthServerWhitelist, "*"); | |
689 RunOnIOThreadBlocking( | |
690 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanUseDefaultCredentials, | |
691 base::Unretained(this), true, url)); | |
692 } | |
693 | |
694 TEST_F(IOThreadTestWithIOThreadObject, UpdateDelegateWhitelist) { | |
695 GURL url("http://test.example.com"); | |
696 | |
697 pref_service()->SetString(prefs::kAuthNegotiateDelegateWhitelist, ""); | |
698 RunOnIOThreadBlocking( | |
699 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanDelegate, | |
700 base::Unretained(this), false, url)); | |
701 | |
702 pref_service()->SetString(prefs::kAuthNegotiateDelegateWhitelist, "*"); | |
703 RunOnIOThreadBlocking( | |
704 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanDelegate, | |
705 base::Unretained(this), true, url)); | |
706 } | |
707 | |
708 #if defined(OS_ANDROID) | |
709 // AuthAndroidNegotiateAccountType is only used on Android. | |
710 TEST_F(IOThreadTestWithIOThreadObject, UpdateAuthAndroidNegotiateAccountType) { | |
711 pref_service()->SetString(prefs::kAuthAndroidNegotiateAccountType, "acc1"); | |
712 RunOnIOThreadBlocking(base::Bind( | |
713 &IOThreadTestWithIOThreadObject::CheckAuthAndroidNegoitateAccountType, | |
714 base::Unretained(this), "acc1")); | |
715 pref_service()->SetString(prefs::kAuthAndroidNegotiateAccountType, "acc2"); | |
716 RunOnIOThreadBlocking(base::Bind( | |
717 &IOThreadTestWithIOThreadObject::CheckAuthAndroidNegoitateAccountType, | |
718 base::Unretained(this), "acc2")); | |
719 } | |
720 #endif | |
721 | |
526 } // namespace test | 722 } // namespace test |
OLD | NEW |