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

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

Powered by Google App Engine
This is Rietveld 408576698