OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 14 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
14 #include "components/policy/core/browser/browser_policy_connector.h" | 17 #include "components/policy/core/browser/browser_policy_connector.h" |
15 #include "components/policy/core/common/mock_configuration_policy_provider.h" | 18 #include "components/policy/core/common/mock_configuration_policy_provider.h" |
16 #include "components/policy/core/common/policy_map.h" | 19 #include "components/policy/core/common/policy_map.h" |
17 #include "components/policy/core/common/policy_types.h" | 20 #include "components/policy/core/common/policy_types.h" |
18 #include "components/policy/policy_constants.h" | 21 #include "components/policy/policy_constants.h" |
19 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/test/browser_test.h" | 23 #include "content/public/test/browser_test.h" |
21 #include "net/http/http_transaction_factory.h" | 24 #include "net/http/http_transaction_factory.h" |
22 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
23 #include "net/url_request/url_request_context_getter.h" | 26 #include "net/url_request/url_request_context_getter.h" |
24 | 27 |
25 namespace { | 28 namespace { |
26 | 29 |
27 void VerifyQuicEnabledStatus(net::URLRequestContextGetter* getter, | 30 bool IsQuicEnabled(net::URLRequestContextGetter* context) { |
28 bool quic_enabled_expected, | 31 return context->GetURLRequestContext() |
29 const base::Closure& done_callback) { | 32 ->http_transaction_factory() |
30 net::URLRequestContext* context = getter->GetURLRequestContext(); | 33 ->GetSession() |
31 bool quic_enabled = context->http_transaction_factory()->GetSession()-> | 34 ->IsQuicEnabled(); |
32 params().enable_quic; | 35 } |
33 EXPECT_EQ(quic_enabled_expected, quic_enabled); | 36 |
| 37 void VerifyQuicEnabledStatus( |
| 38 net::URLRequestContextGetter* system_context, |
| 39 scoped_refptr<net::URLRequestContextGetter> safe_browsing_context, |
| 40 net::URLRequestContextGetter* profile_context, |
| 41 bool quic_enabled_expected, |
| 42 const base::Closure& done_callback) { |
| 43 bool quic_enabled_system = IsQuicEnabled(system_context); |
| 44 EXPECT_EQ(quic_enabled_expected, quic_enabled_system); |
| 45 |
| 46 bool quic_enabled_safe_browsing = IsQuicEnabled(safe_browsing_context.get()); |
| 47 EXPECT_EQ(quic_enabled_expected, quic_enabled_safe_browsing); |
| 48 |
| 49 bool quic_enabled_profile = IsQuicEnabled(profile_context); |
| 50 EXPECT_EQ(quic_enabled_expected, quic_enabled_profile); |
34 | 51 |
35 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 52 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
36 done_callback); | 53 done_callback); |
37 } | 54 } |
38 | 55 |
39 void VerifyQuicEnabledStatusInIOThread(bool quic_enabled_expected) { | 56 void VerifyQuicEnabledStatusInIOThread(Profile* profile, |
| 57 bool quic_enabled_expected) { |
40 base::RunLoop run_loop; | 58 base::RunLoop run_loop; |
| 59 net::URLRequestContextGetter* system_context = |
| 60 g_browser_process->system_request_context(); |
| 61 scoped_refptr<net::URLRequestContextGetter> safe_browsing_context = |
| 62 g_browser_process->safe_browsing_service()->url_request_context(); |
| 63 net::URLRequestContextGetter* profile_context = profile->GetRequestContext(); |
| 64 |
41 content::BrowserThread::PostTask( | 65 content::BrowserThread::PostTask( |
42 content::BrowserThread::IO, FROM_HERE, | 66 content::BrowserThread::IO, FROM_HERE, |
43 base::Bind(VerifyQuicEnabledStatus, | 67 base::Bind(VerifyQuicEnabledStatus, base::RetainedRef(system_context), |
44 base::RetainedRef(g_browser_process->system_request_context()), | 68 safe_browsing_context, base::RetainedRef(profile_context), |
45 quic_enabled_expected, run_loop.QuitClosure())); | 69 quic_enabled_expected, run_loop.QuitClosure())); |
46 run_loop.Run(); | 70 run_loop.Run(); |
47 } | 71 } |
48 | 72 |
49 } // namespace | 73 } // namespace |
50 | 74 |
51 namespace policy { | 75 namespace policy { |
52 | 76 |
53 // The tests are based on the assumption that command line flag kEnableQuic | 77 // The tests are based on the assumption that command line flag kEnableQuic |
54 // guarantees that QUIC protocol is enabled which is the case at the moment | 78 // guarantees that QUIC protocol is enabled which is the case at the moment |
55 // when these are being written. | 79 // when these are being written. |
56 class QuicAllowedPolicyTestBase: public InProcessBrowserTest { | 80 class QuicAllowedPolicyTestBase: public InProcessBrowserTest { |
57 public: | 81 public: |
58 QuicAllowedPolicyTestBase() : InProcessBrowserTest(){} | 82 QuicAllowedPolicyTestBase() : InProcessBrowserTest() {} |
59 | 83 |
60 protected: | 84 protected: |
61 void SetUpInProcessBrowserTestFixture() override { | 85 void SetUpInProcessBrowserTestFixture() override { |
62 base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableQuic); | 86 base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnableQuic); |
63 EXPECT_CALL(provider_, IsInitializationComplete(testing::_)) | 87 EXPECT_CALL(provider_, IsInitializationComplete(testing::_)) |
64 .WillRepeatedly(testing::Return(true)); | 88 .WillRepeatedly(testing::Return(true)); |
65 | 89 |
66 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | 90 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); |
67 PolicyMap values; | 91 PolicyMap values; |
68 GetQuicAllowedPolicy(&values); | 92 GetQuicAllowedPolicy(&values); |
69 provider_.UpdateChromePolicy(values); | 93 provider_.UpdateChromePolicy(values); |
70 } | 94 } |
71 | 95 |
72 virtual void GetQuicAllowedPolicy(PolicyMap* values) = 0; | 96 virtual void GetQuicAllowedPolicy(PolicyMap* values) = 0; |
73 | 97 |
| 98 MockConfigurationPolicyProvider provider_; |
| 99 |
74 private: | 100 private: |
75 MockConfigurationPolicyProvider provider_; | |
76 DISALLOW_COPY_AND_ASSIGN(QuicAllowedPolicyTestBase); | 101 DISALLOW_COPY_AND_ASSIGN(QuicAllowedPolicyTestBase); |
77 }; | 102 }; |
78 | 103 |
79 // Policy QuicAllowed set to false. | 104 // Policy QuicAllowed set to false. |
80 class QuicAllowedPolicyIsFalse: public QuicAllowedPolicyTestBase { | 105 class QuicAllowedPolicyIsFalse: public QuicAllowedPolicyTestBase { |
81 public: | 106 public: |
82 QuicAllowedPolicyIsFalse() : QuicAllowedPolicyTestBase(){} | 107 QuicAllowedPolicyIsFalse() : QuicAllowedPolicyTestBase() {} |
83 | 108 |
84 protected: | 109 protected: |
85 void GetQuicAllowedPolicy(PolicyMap* values) override { | 110 void GetQuicAllowedPolicy(PolicyMap* values) override { |
86 values->Set(key::kQuicAllowed, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | 111 values->Set(key::kQuicAllowed, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, |
87 POLICY_SOURCE_CLOUD, | 112 POLICY_SOURCE_CLOUD, |
88 base::MakeUnique<base::FundamentalValue>(false), nullptr); | 113 base::MakeUnique<base::FundamentalValue>(false), nullptr); |
89 } | 114 } |
90 | 115 |
91 private: | 116 private: |
92 DISALLOW_COPY_AND_ASSIGN(QuicAllowedPolicyIsFalse); | 117 DISALLOW_COPY_AND_ASSIGN(QuicAllowedPolicyIsFalse); |
93 }; | 118 }; |
94 | 119 |
95 IN_PROC_BROWSER_TEST_F(QuicAllowedPolicyIsFalse, QuicDisallowed) { | 120 IN_PROC_BROWSER_TEST_F(QuicAllowedPolicyIsFalse, QuicDisallowed) { |
96 VerifyQuicEnabledStatusInIOThread(false); | 121 VerifyQuicEnabledStatusInIOThread(browser()->profile(), false); |
97 } | 122 } |
98 | 123 |
99 // Policy QuicAllowed set to true. | 124 // Policy QuicAllowed set to true. |
100 class QuicAllowedPolicyIsTrue: public QuicAllowedPolicyTestBase { | 125 class QuicAllowedPolicyIsTrue: public QuicAllowedPolicyTestBase { |
101 public: | 126 public: |
102 QuicAllowedPolicyIsTrue() : QuicAllowedPolicyTestBase(){} | 127 QuicAllowedPolicyIsTrue() : QuicAllowedPolicyTestBase() {} |
103 | 128 |
104 protected: | 129 protected: |
105 void GetQuicAllowedPolicy(PolicyMap* values) override { | 130 void GetQuicAllowedPolicy(PolicyMap* values) override { |
106 values->Set(key::kQuicAllowed, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, | 131 values->Set(key::kQuicAllowed, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE, |
107 POLICY_SOURCE_CLOUD, | 132 POLICY_SOURCE_CLOUD, |
108 base::MakeUnique<base::FundamentalValue>(true), nullptr); | 133 base::MakeUnique<base::FundamentalValue>(true), nullptr); |
109 } | 134 } |
110 | 135 |
111 private: | 136 private: |
112 DISALLOW_COPY_AND_ASSIGN(QuicAllowedPolicyIsTrue); | 137 DISALLOW_COPY_AND_ASSIGN(QuicAllowedPolicyIsTrue); |
113 }; | 138 }; |
114 | 139 |
115 IN_PROC_BROWSER_TEST_F(QuicAllowedPolicyIsTrue, QuicAllowed) { | 140 IN_PROC_BROWSER_TEST_F(QuicAllowedPolicyIsTrue, QuicAllowed) { |
116 VerifyQuicEnabledStatusInIOThread(true); | 141 VerifyQuicEnabledStatusInIOThread(browser()->profile(), true); |
117 } | 142 } |
118 | 143 |
119 // Policy QuicAllowed is not set. | 144 // Policy QuicAllowed is not set. |
120 class QuicAllowedPolicyIsNotSet: public QuicAllowedPolicyTestBase { | 145 class QuicAllowedPolicyIsNotSet: public QuicAllowedPolicyTestBase { |
121 public: | 146 public: |
122 QuicAllowedPolicyIsNotSet() : QuicAllowedPolicyTestBase(){} | 147 QuicAllowedPolicyIsNotSet() : QuicAllowedPolicyTestBase() {} |
123 | 148 |
124 protected: | 149 protected: |
125 void GetQuicAllowedPolicy(PolicyMap* values) override { | 150 void GetQuicAllowedPolicy(PolicyMap* values) override { |
126 } | 151 } |
127 | 152 |
128 private: | 153 private: |
129 DISALLOW_COPY_AND_ASSIGN(QuicAllowedPolicyIsNotSet); | 154 DISALLOW_COPY_AND_ASSIGN(QuicAllowedPolicyIsNotSet); |
130 }; | 155 }; |
131 | 156 |
132 IN_PROC_BROWSER_TEST_F(QuicAllowedPolicyIsNotSet, NoQuicRegulations) { | 157 IN_PROC_BROWSER_TEST_F(QuicAllowedPolicyIsNotSet, NoQuicRegulations) { |
133 VerifyQuicEnabledStatusInIOThread(true); | 158 VerifyQuicEnabledStatusInIOThread(browser()->profile(), true); |
134 } | 159 } |
135 | 160 |
| 161 // Policy QuicAllowed is set after IOThread initialization. |
| 162 class QuicAllowedPolicyIsSetToFalseAfterInit |
| 163 : public QuicAllowedPolicyTestBase { |
| 164 public: |
| 165 QuicAllowedPolicyIsSetToFalseAfterInit() : QuicAllowedPolicyTestBase() {} |
| 166 |
| 167 protected: |
| 168 void GetQuicAllowedPolicy(PolicyMap* values) override {} |
| 169 |
| 170 private: |
| 171 DISALLOW_COPY_AND_ASSIGN(QuicAllowedPolicyIsSetToFalseAfterInit); |
| 172 }; |
| 173 |
| 174 IN_PROC_BROWSER_TEST_F(QuicAllowedPolicyIsSetToFalseAfterInit, QuicDisallowed) { |
| 175 PolicyMap values; |
| 176 values.Set(key::kQuicAllowed, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 177 POLICY_SOURCE_CLOUD, |
| 178 base::MakeUnique<base::FundamentalValue>(false), nullptr); |
| 179 provider_.UpdateChromePolicy(values); |
| 180 base::RunLoop().RunUntilIdle(); |
| 181 |
| 182 VerifyQuicEnabledStatusInIOThread(browser()->profile(), false); |
| 183 } |
136 } // namespace policy | 184 } // namespace policy |
OLD | NEW |