Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: chrome/browser/io_thread_unittest.cc

Issue 1414313002: Allow dynamic updating of authentication policies (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix missing Chromeos includes Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
7 #include "base/test/mock_entropy_provider.h" 10 #include "base/test/mock_entropy_provider.h"
11 #include "chrome/browser/extensions/event_router_forwarder.h"
8 #include "chrome/browser/io_thread.h" 12 #include "chrome/browser/io_thread.h"
9 #include "chrome/common/chrome_switches.h" 13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/pref_names.h"
10 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s.h" 15 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param s.h"
16 #include "components/policy/core/common/mock_policy_service.h"
17 #include "components/proxy_config/pref_proxy_config_tracker_impl.h"
18 #include "components/proxy_config/proxy_config_pref_names.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "net/http/http_auth_handler_negotiate.h"
11 #include "net/http/http_network_session.h" 21 #include "net/http/http_network_session.h"
12 #include "net/http/http_server_properties_impl.h" 22 #include "net/http/http_server_properties_impl.h"
13 #include "net/quic/quic_protocol.h" 23 #include "net/quic/quic_protocol.h"
14 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
16 26
27 #if defined(OS_CHROMEOS)
28 #include "chromeos/dbus/dbus_thread_manager.h"
29 #include "chromeos/network/network_handler.h"
30 #endif
31
32
17 namespace test { 33 namespace test {
18 34
19 using ::testing::ElementsAre; 35 using ::testing::ElementsAre;
36 using ::testing::ReturnRef;
20 37
38 // Class used for accessing IOThread methods (friend of IOThread). Creating
39 // an IOThreadPeer creates and initializes an IOThread instance set up for
40 // testing, deleting it cleanly closes down and deletes the IOThread instance.
21 class IOThreadPeer { 41 class IOThreadPeer {
asanka 2015/11/04 16:05:44 Shall we maintain IOThreadPeer as a pure peer? It
aberent 2015/11/06 13:57:34 Done.
22 public: 42 public:
43 IOThreadPeer() {
44 #if defined(ENABLE_EXTENSIONS)
45 event_router_forwarder_ = new extensions::EventRouterForwarder;
46 #endif
47 PrefRegistrySimple* pref_registry = pref_service_.registry();
48 IOThread::RegisterPrefs(pref_registry);
49 PrefProxyConfigTrackerImpl::RegisterPrefs(pref_registry);
50 ssl_config::SSLConfigServiceManager::RegisterPrefs(pref_registry);
51
52 // Set up default function behaviour.
53 EXPECT_CALL(policy_service_,
54 GetPolicies(policy::PolicyNamespace(
55 policy::POLICY_DOMAIN_CHROME, std::string())))
56 .WillRepeatedly(ReturnRef(policy_map_));
57
58 #if defined(OS_CHROMEOS)
59 // Needed by IOThread constructor.
60 chromeos::DBusThreadManager::Initialize();
61 chromeos::NetworkHandler::Initialize();
62 #endif
63 io_thread_.reset(new IOThread(&pref_service_, &policy_service_, nullptr,
64 event_router_forwarder_.get()));
65 // Sadly the test can't call IOThread::Init here, since on Mac the IOThread
66 // constructor has to run on the UI thread, and will DCHECK if it doesn't;
67 // but on all platforms IOThread::Init will DCHECK if it isn't on the IO
68 // Thread.
69 io_thread_->SetGlobalsForTesting(&globals_);
70 io_thread_->CreateDefaultAuthHandlerFactory();
71 }
72
73 ~IOThreadPeer() {
74 io_thread_->SetGlobalsForTesting(nullptr);
75 #if defined(OS_CHROMEOS)
76 chromeos::NetworkHandler::Shutdown();
77 chromeos::DBusThreadManager::Shutdown();
78 #endif
79 }
80
23 static void ConfigureQuicGlobals( 81 static void ConfigureQuicGlobals(
24 const base::CommandLine& command_line, 82 const base::CommandLine& command_line,
25 base::StringPiece quic_trial_group, 83 base::StringPiece quic_trial_group,
26 const std::map<std::string, std::string>& quic_trial_params, 84 const std::map<std::string, std::string>& quic_trial_params,
27 bool is_quic_allowed_by_policy, 85 bool is_quic_allowed_by_policy,
28 IOThread::Globals* globals) { 86 IOThread::Globals* globals) {
29 IOThread::ConfigureQuicGlobals(command_line, quic_trial_group, 87 IOThread::ConfigureQuicGlobals(command_line, quic_trial_group,
30 quic_trial_params, is_quic_allowed_by_policy, 88 quic_trial_params, is_quic_allowed_by_policy,
31 globals); 89 globals);
32 } 90 }
33 91
34 static void ConfigureSpdyGlobals( 92 static void ConfigureSpdyGlobals(
35 const base::CommandLine& command_line, 93 const base::CommandLine& command_line,
36 base::StringPiece spdy_trial_group, 94 base::StringPiece spdy_trial_group,
37 const std::map<std::string, std::string>& spdy_trial_params, 95 const std::map<std::string, std::string>& spdy_trial_params,
38 IOThread::Globals* globals) { 96 IOThread::Globals* globals) {
39 IOThread::ConfigureSpdyGlobals(command_line, spdy_trial_group, 97 IOThread::ConfigureSpdyGlobals(command_line, spdy_trial_group,
40 spdy_trial_params, globals); 98 spdy_trial_params, globals);
41 } 99 }
42 100
43 static void InitializeNetworkSessionParamsFromGlobals( 101 static void InitializeNetworkSessionParamsFromGlobals(
44 const IOThread::Globals& globals, 102 const IOThread::Globals& globals,
45 net::HttpNetworkSession::Params* params) { 103 net::HttpNetworkSession::Params* params) {
46 IOThread::InitializeNetworkSessionParamsFromGlobals(globals, params); 104 IOThread::InitializeNetworkSessionParamsFromGlobals(globals, params);
47 } 105 }
106
107 IOThread* io_thread() { return io_thread_.get(); }
108
109 TestingPrefServiceSimple* pref_service() { return &pref_service_; }
110
111 private:
112 content::TestBrowserThreadBundle thread_bundle_;
113 TestingPrefServiceSimple pref_service_;
114 scoped_refptr<extensions::EventRouterForwarder> event_router_forwarder_;
115 policy::PolicyMap policy_map_;
116 policy::MockPolicyService policy_service_;
117 scoped_ptr<IOThread> io_thread_;
118 IOThread::Globals globals_;
119
120 DISALLOW_COPY_AND_ASSIGN(IOThreadPeer);
48 }; 121 };
49 122
50 class IOThreadTest : public testing::Test { 123 class IOThreadTest : public testing::Test {
51 public: 124 public:
52 IOThreadTest() 125 IOThreadTest()
53 : command_line_(base::CommandLine::NO_PROGRAM), 126 : command_line_(base::CommandLine::NO_PROGRAM),
54 is_quic_allowed_by_policy_(true) { 127 is_quic_allowed_by_policy_(true) {
55 globals_.http_server_properties.reset(new net::HttpServerPropertiesImpl()); 128 globals_.http_server_properties.reset(new net::HttpServerPropertiesImpl());
56 } 129 }
57 130
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 TEST_F(IOThreadTest, QuicDisallowedByPolicy) { 564 TEST_F(IOThreadTest, QuicDisallowedByPolicy) {
492 command_line_.AppendSwitch(switches::kEnableQuic); 565 command_line_.AppendSwitch(switches::kEnableQuic);
493 is_quic_allowed_by_policy_ = false; 566 is_quic_allowed_by_policy_ = false;
494 ConfigureQuicGlobals(); 567 ConfigureQuicGlobals();
495 568
496 net::HttpNetworkSession::Params params; 569 net::HttpNetworkSession::Params params;
497 InitializeNetworkSessionParams(&params); 570 InitializeNetworkSessionParams(&params);
498 EXPECT_FALSE(params.enable_quic); 571 EXPECT_FALSE(params.enable_quic);
499 } 572 }
500 573
574 TEST_F(IOThreadTest, UpdateNegotiateDisableCnameLookup) {
575 IOThreadPeer peer;
asanka 2015/11/04 16:05:44 Exercising the thread restrictions is important gi
aberent 2015/11/06 13:57:34 Done.
576
577 auto factory = static_cast<net::HttpAuthHandlerRegistryFactory*>(
578 peer.io_thread()->globals()->http_auth_handler_factory.get());
579
580 auto negotiate_factory = static_cast<net::HttpAuthHandlerNegotiate::Factory*>(
581 factory->GetSchemeFactory(net::kNegotiateAuthScheme));
582 // If we don't have a negotiate factory (net wasn't built with Kerberos) the
583 // policy does nothing so we can't test anything.
584 if (!negotiate_factory)
585 return;
586
587 peer.pref_service()->SetBoolean(prefs::kDisableAuthNegotiateCnameLookup,
588 false);
589 EXPECT_FALSE(negotiate_factory->disable_cname_lookup());
590 peer.pref_service()->SetBoolean(prefs::kDisableAuthNegotiateCnameLookup,
591 true);
592 EXPECT_TRUE(negotiate_factory->disable_cname_lookup());
593 }
594
595 TEST_F(IOThreadTest, UpdateEnableAuthNegotiatePort) {
596 IOThreadPeer peer;
597
598 auto factory = static_cast<net::HttpAuthHandlerRegistryFactory*>(
599 peer.io_thread()->globals()->http_auth_handler_factory.get());
600
601 auto negotiate_factory = static_cast<net::HttpAuthHandlerNegotiate::Factory*>(
602 factory->GetSchemeFactory(net::kNegotiateAuthScheme));
603 // If we don't have a negotiate factory (net wasn't built with Kerberos) the
604 // policy does nothing so we can't test anything.
605 if (!negotiate_factory)
606 return;
607
608 peer.pref_service()->SetBoolean(prefs::kEnableAuthNegotiatePort, false);
609 EXPECT_FALSE(negotiate_factory->use_port());
610 peer.pref_service()->SetBoolean(prefs::kEnableAuthNegotiatePort, true);
611 EXPECT_TRUE(negotiate_factory->use_port());
612 }
613
614 TEST_F(IOThreadTest, UpdateServerWhitelist) {
615 IOThreadPeer peer;
616
617 GURL url("http://test.example.com");
618
619 peer.pref_service()->SetString(prefs::kAuthServerWhitelist, "");
620 net::URLSecurityManager* security_manager =
621 peer.io_thread()->globals()->url_security_manager.get();
622 EXPECT_FALSE(security_manager->CanUseDefaultCredentials(url));
623
624 peer.pref_service()->SetString(prefs::kAuthServerWhitelist, "*");
625 security_manager = peer.io_thread()->globals()->url_security_manager.get();
626 EXPECT_TRUE(security_manager->CanUseDefaultCredentials(url));
627 }
628
629 TEST_F(IOThreadTest, UpdateDelegateWhitelist) {
630 IOThreadPeer peer;
631
632 GURL url("http://test.example.com");
633
634 peer.pref_service()->SetString(prefs::kAuthNegotiateDelegateWhitelist, "");
635 net::URLSecurityManager* security_manager =
636 peer.io_thread()->globals()->url_security_manager.get();
637 EXPECT_FALSE(security_manager->CanDelegate(url));
638
639 peer.pref_service()->SetString(prefs::kAuthNegotiateDelegateWhitelist, "*");
640 security_manager = peer.io_thread()->globals()->url_security_manager.get();
641 EXPECT_TRUE(security_manager->CanDelegate(url));
642 }
643
644 #if defined(OS_ANDROID)
645 // AuthAndroidNegotiateAccountType is only used on Android.
646 TEST_F(IOThreadTest, UpdateAuthAndroidNegotiateAccountType) {
647 IOThreadPeer peer;
648
649 auto factory = static_cast<net::HttpAuthHandlerRegistryFactory*>(
650 peer.io_thread()->globals()->http_auth_handler_factory.get());
651
652 auto negotiate_factory = static_cast<net::HttpAuthHandlerNegotiate::Factory*>(
653 factory->GetSchemeFactory(net::kNegotiateAuthScheme));
654 // If we don't have a negotiate factory (net wasn't built with Kerberos) the
655 // policy does nothing so we can't test anything.
656 if (!negotiate_factory)
657 return;
658
659 peer.pref_service()->SetString(prefs::kAuthAndroidNegotiateAccountType,
660 "acc1");
661 EXPECT_EQ("acc1", *(negotiate_factory->library()));
662 peer.pref_service()->SetString(prefs::kAuthAndroidNegotiateAccountType,
663 "acc2");
664 EXPECT_EQ("acc2", *(negotiate_factory->library()));
665 }
666 #endif
667
501 } // namespace test 668 } // namespace test
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698