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

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: Respond to cbentzel@'s comments. Created 5 years 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
« no previous file with comments | « chrome/browser/io_thread.cc ('k') | chrome/browser/ui/login/login_prompt.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
10 #include "base/run_loop.h"
7 #include "base/test/mock_entropy_provider.h" 11 #include "base/test/mock_entropy_provider.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/browser/browser_thread.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "net/http/http_auth_preferences.h"
22 #include "net/http/http_auth_scheme.h"
11 #include "net/http/http_network_session.h" 23 #include "net/http/http_network_session.h"
12 #include "net/http/http_server_properties_impl.h" 24 #include "net/http/http_server_properties_impl.h"
13 #include "net/quic/quic_protocol.h" 25 #include "net/quic/quic_protocol.h"
14 #include "net/quic/quic_stream_factory.h" 26 #include "net/quic/quic_stream_factory.h"
15 #include "testing/gmock/include/gmock/gmock.h" 27 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
17 29
30 #if defined(ENABLE_EXTENSIONS)
31 #include "chrome/browser/extensions/event_router_forwarder.h"
32 #endif
33
34 #if defined(OS_CHROMEOS)
35 #include "chromeos/dbus/dbus_thread_manager.h"
36 #include "chromeos/network/network_handler.h"
37 #endif
38
18 namespace test { 39 namespace test {
19 40
20 using ::testing::ElementsAre; 41 using ::testing::ElementsAre;
42 using ::testing::ReturnRef;
21 43
44 // Class used for accessing IOThread methods (friend of IOThread).
22 class IOThreadPeer { 45 class IOThreadPeer {
23 public: 46 public:
24 static void ConfigureQuicGlobals( 47 static void ConfigureQuicGlobals(
25 const base::CommandLine& command_line, 48 const base::CommandLine& command_line,
26 base::StringPiece quic_trial_group, 49 base::StringPiece quic_trial_group,
27 const std::map<std::string, std::string>& quic_trial_params, 50 const std::map<std::string, std::string>& quic_trial_params,
28 bool is_quic_allowed_by_policy, 51 bool is_quic_allowed_by_policy,
29 IOThread::Globals* globals) { 52 IOThread::Globals* globals) {
30 IOThread::ConfigureQuicGlobals(command_line, quic_trial_group, 53 IOThread::ConfigureQuicGlobals(command_line, quic_trial_group,
31 quic_trial_params, is_quic_allowed_by_policy, 54 quic_trial_params, is_quic_allowed_by_policy,
(...skipping 12 matching lines...) Expand all
44 static void ConfigureNPNGlobals(base::StringPiece npn_trial_group, 67 static void ConfigureNPNGlobals(base::StringPiece npn_trial_group,
45 IOThread::Globals* globals) { 68 IOThread::Globals* globals) {
46 IOThread::ConfigureNPNGlobals(npn_trial_group, globals); 69 IOThread::ConfigureNPNGlobals(npn_trial_group, globals);
47 } 70 }
48 71
49 static void InitializeNetworkSessionParamsFromGlobals( 72 static void InitializeNetworkSessionParamsFromGlobals(
50 const IOThread::Globals& globals, 73 const IOThread::Globals& globals,
51 net::HttpNetworkSession::Params* params) { 74 net::HttpNetworkSession::Params* params) {
52 IOThread::InitializeNetworkSessionParamsFromGlobals(globals, params); 75 IOThread::InitializeNetworkSessionParamsFromGlobals(globals, params);
53 } 76 }
77
78 static void InitIOThread(IOThread* io_thread) { io_thread->Init(); }
79
80 static void CleanUpIOThread(IOThread* io_thread) { io_thread->CleanUp(); }
81
82 static net::HttpAuthPreferences* GetAuthPreferences(IOThread* io_thread) {
83 return io_thread->globals()->http_auth_preferences.get();
84 }
54 }; 85 };
55 86
56 class IOThreadTest : public testing::Test { 87 class IOThreadTest : public testing::Test {
57 public: 88 public:
58 IOThreadTest() 89 IOThreadTest()
59 : command_line_(base::CommandLine::NO_PROGRAM), 90 : command_line_(base::CommandLine::NO_PROGRAM),
60 is_quic_allowed_by_policy_(true) { 91 is_quic_allowed_by_policy_(true) {
61 globals_.http_server_properties.reset(new net::HttpServerPropertiesImpl()); 92 globals_.http_server_properties.reset(new net::HttpServerPropertiesImpl());
62 } 93 }
63 94
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 TEST_F(IOThreadTest, QuicDisallowedByPolicy) { 559 TEST_F(IOThreadTest, QuicDisallowedByPolicy) {
529 command_line_.AppendSwitch(switches::kEnableQuic); 560 command_line_.AppendSwitch(switches::kEnableQuic);
530 is_quic_allowed_by_policy_ = false; 561 is_quic_allowed_by_policy_ = false;
531 ConfigureQuicGlobals(); 562 ConfigureQuicGlobals();
532 563
533 net::HttpNetworkSession::Params params; 564 net::HttpNetworkSession::Params params;
534 InitializeNetworkSessionParams(&params); 565 InitializeNetworkSessionParams(&params);
535 EXPECT_FALSE(params.enable_quic); 566 EXPECT_FALSE(params.enable_quic);
536 } 567 }
537 568
569 class IOThreadTestWithIOThreadObject : public testing::Test {
570 public:
571 // These functions need to be public, since it is difficult to bind to
572 // protected functions in a test (the code would need to explicitly contain
573 // the name of the actual test class).
574 void CheckCnameLookup(bool expected) {
575 auto http_auth_preferences =
576 IOThreadPeer::GetAuthPreferences(io_thread_.get());
577 ASSERT_NE(nullptr, http_auth_preferences);
578 EXPECT_EQ(expected, http_auth_preferences->NegotiateDisableCnameLookup());
579 }
580
581 void CheckNegotiateEnablePort(bool expected) {
582 auto http_auth_preferences =
583 IOThreadPeer::GetAuthPreferences(io_thread_.get());
584 ASSERT_NE(nullptr, http_auth_preferences);
585 EXPECT_EQ(expected, http_auth_preferences->NegotiateEnablePort());
586 }
587
588 #if defined(OS_ANDROID)
589 void CheckAuthAndroidNegoitateAccountType(std::string expected) {
590 auto http_auth_preferences =
591 IOThreadPeer::GetAuthPreferences(io_thread_.get());
592 ASSERT_NE(nullptr, http_auth_preferences);
593 EXPECT_EQ(expected,
594 http_auth_preferences->AuthAndroidNegotiateAccountType());
595 }
596 #endif
597
598 void CheckCanUseDefaultCredentials(bool expected, const GURL& url) {
599 auto http_auth_preferences =
600 IOThreadPeer::GetAuthPreferences(io_thread_.get());
601 EXPECT_EQ(expected, http_auth_preferences->CanUseDefaultCredentials(url));
602 }
603
604 void CheckCanDelegate(bool expected, const GURL& url) {
605 auto http_auth_preferences =
606 IOThreadPeer::GetAuthPreferences(io_thread_.get());
607 EXPECT_EQ(expected, http_auth_preferences->CanDelegate(url));
608 }
609
610 protected:
611 IOThreadTestWithIOThreadObject()
612 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD) {
613 #if defined(ENABLE_EXTENSIONS)
614 event_router_forwarder_ = new extensions::EventRouterForwarder;
615 #endif
616 PrefRegistrySimple* pref_registry = pref_service_.registry();
617 IOThread::RegisterPrefs(pref_registry);
618 PrefProxyConfigTrackerImpl::RegisterPrefs(pref_registry);
619 ssl_config::SSLConfigServiceManager::RegisterPrefs(pref_registry);
620
621 // Set up default function behaviour.
622 EXPECT_CALL(policy_service_,
623 GetPolicies(policy::PolicyNamespace(
624 policy::POLICY_DOMAIN_CHROME, std::string())))
625 .WillRepeatedly(ReturnRef(policy_map_));
626
627 #if defined(OS_CHROMEOS)
628 // Needed by IOThread constructor.
629 chromeos::DBusThreadManager::Initialize();
630 chromeos::NetworkHandler::Initialize();
631 #endif
632 io_thread_.reset(new IOThread(&pref_service_, &policy_service_, nullptr,
633 #if defined(ENABLE_EXTENSIONS)
634 event_router_forwarder_.get()
635 #else
636 nullptr
637 #endif
638 ));
639 // Init must be run on the IO thread.
640 RunOnIOThreadBlocking(
641 base::Bind(&IOThreadPeer::InitIOThread, io_thread_.get()));
642 }
643
644 ~IOThreadTestWithIOThreadObject() override {
645 RunOnIOThreadBlocking(
646 base::Bind(&IOThreadPeer::CleanUpIOThread, io_thread_.get()));
647 #if defined(OS_CHROMEOS)
648 chromeos::NetworkHandler::Shutdown();
649 chromeos::DBusThreadManager::Shutdown();
650 #endif
651 }
652 TestingPrefServiceSimple* pref_service() { return &pref_service_; }
653
654 void RunOnIOThreadBlocking(const base::Closure& task) {
655 base::RunLoop run_loop;
656 content::BrowserThread::PostTaskAndReply(
657 content::BrowserThread::IO, FROM_HERE, task, run_loop.QuitClosure());
658 run_loop.Run();
659 }
660
661 private:
662 content::TestBrowserThreadBundle thread_bundle_;
663 TestingPrefServiceSimple pref_service_;
664 #if defined(ENABLE_EXTENSIONS)
665 scoped_refptr<extensions::EventRouterForwarder> event_router_forwarder_;
666 #endif
667 policy::PolicyMap policy_map_;
668 policy::MockPolicyService policy_service_;
669 scoped_ptr<IOThread> io_thread_;
670 };
671
672 TEST_F(IOThreadTestWithIOThreadObject, UpdateNegotiateDisableCnameLookup) {
673 // This test uses the kDisableAuthNegotiateCnameLookup to check that
674 // the HttpAuthPreferences are correctly initialized and running on the
675 // IO thread. The other preferences are tested by the HttpAuthPreferences
676 // unit tests.
677 pref_service()->SetBoolean(prefs::kDisableAuthNegotiateCnameLookup, false);
678 RunOnIOThreadBlocking(
679 base::Bind(&IOThreadTestWithIOThreadObject::CheckCnameLookup,
680 base::Unretained(this), false));
681 pref_service()->SetBoolean(prefs::kDisableAuthNegotiateCnameLookup, true);
682 RunOnIOThreadBlocking(
683 base::Bind(&IOThreadTestWithIOThreadObject::CheckCnameLookup,
684 base::Unretained(this), true));
685 }
686
687 TEST_F(IOThreadTestWithIOThreadObject, UpdateEnableAuthNegotiatePort) {
688 pref_service()->SetBoolean(prefs::kEnableAuthNegotiatePort, false);
689 RunOnIOThreadBlocking(
690 base::Bind(&IOThreadTestWithIOThreadObject::CheckNegotiateEnablePort,
691 base::Unretained(this), false));
692 pref_service()->SetBoolean(prefs::kEnableAuthNegotiatePort, true);
693 RunOnIOThreadBlocking(
694 base::Bind(&IOThreadTestWithIOThreadObject::CheckNegotiateEnablePort,
695 base::Unretained(this), true));
696 }
697
698 TEST_F(IOThreadTestWithIOThreadObject, UpdateServerWhitelist) {
699 GURL url("http://test.example.com");
700
701 pref_service()->SetString(prefs::kAuthServerWhitelist, "xxx");
702 RunOnIOThreadBlocking(
703 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanUseDefaultCredentials,
704 base::Unretained(this), false, url));
705
706 pref_service()->SetString(prefs::kAuthServerWhitelist, "*");
707 RunOnIOThreadBlocking(
708 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanUseDefaultCredentials,
709 base::Unretained(this), true, url));
710 }
711
712 TEST_F(IOThreadTestWithIOThreadObject, UpdateDelegateWhitelist) {
713 GURL url("http://test.example.com");
714
715 pref_service()->SetString(prefs::kAuthNegotiateDelegateWhitelist, "");
716 RunOnIOThreadBlocking(
717 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanDelegate,
718 base::Unretained(this), false, url));
719
720 pref_service()->SetString(prefs::kAuthNegotiateDelegateWhitelist, "*");
721 RunOnIOThreadBlocking(
722 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanDelegate,
723 base::Unretained(this), true, url));
724 }
725
726 #if defined(OS_ANDROID)
727 // AuthAndroidNegotiateAccountType is only used on Android.
728 TEST_F(IOThreadTestWithIOThreadObject, UpdateAuthAndroidNegotiateAccountType) {
729 pref_service()->SetString(prefs::kAuthAndroidNegotiateAccountType, "acc1");
730 RunOnIOThreadBlocking(base::Bind(
731 &IOThreadTestWithIOThreadObject::CheckAuthAndroidNegoitateAccountType,
732 base::Unretained(this), "acc1"));
733 pref_service()->SetString(prefs::kAuthAndroidNegotiateAccountType, "acc2");
734 RunOnIOThreadBlocking(base::Bind(
735 &IOThreadTestWithIOThreadObject::CheckAuthAndroidNegoitateAccountType,
736 base::Unretained(this), "acc2"));
737 }
738 #endif
739
538 } // namespace test 740 } // namespace test
OLDNEW
« no previous file with comments | « chrome/browser/io_thread.cc ('k') | chrome/browser/ui/login/login_prompt.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698