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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
| 7 #include <map> |
| 8 #include <string> |
| 9 |
7 #include "base/at_exit.h" | 10 #include "base/at_exit.h" |
8 #include "base/command_line.h" | 11 #include "base/command_line.h" |
9 #include "base/macros.h" | 12 #include "base/macros.h" |
10 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
11 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
12 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
13 #include "base/test/mock_entropy_provider.h" | 16 #include "base/test/mock_entropy_provider.h" |
14 #include "build/build_config.h" | 17 #include "build/build_config.h" |
15 #include "chrome/browser/io_thread.h" | 18 #include "chrome/browser/io_thread.h" |
16 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
17 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
18 #include "components/policy/core/common/mock_policy_service.h" | 21 #include "components/policy/core/common/mock_policy_service.h" |
19 #include "components/prefs/pref_registry_simple.h" | 22 #include "components/prefs/pref_registry_simple.h" |
20 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
21 #include "components/prefs/testing_pref_service.h" | 24 #include "components/prefs/testing_pref_service.h" |
22 #include "components/proxy_config/pref_proxy_config_tracker_impl.h" | 25 #include "components/proxy_config/pref_proxy_config_tracker_impl.h" |
23 #include "components/proxy_config/proxy_config_pref_names.h" | 26 #include "components/proxy_config/proxy_config_pref_names.h" |
24 #include "components/variations/variations_associated_data.h" | 27 #include "components/variations/variations_associated_data.h" |
25 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
26 #include "content/public/test/test_browser_thread_bundle.h" | 29 #include "content/public/test/test_browser_thread_bundle.h" |
27 #include "extensions/features/features.h" | 30 #include "extensions/features/features.h" |
28 #include "net/cert_net/nss_ocsp.h" | 31 #include "net/cert_net/nss_ocsp.h" |
29 #include "net/http/http_auth_preferences.h" | 32 #include "net/http/http_auth_preferences.h" |
30 #include "net/http/http_auth_scheme.h" | 33 #include "net/http/http_auth_scheme.h" |
31 #include "net/http/http_network_session.h" | 34 #include "net/http/http_network_session.h" |
| 35 #include "net/nqe/effective_connection_type.h" |
| 36 #include "net/nqe/network_quality_estimator.h" |
32 #include "net/quic/chromium/quic_stream_factory.h" | 37 #include "net/quic/chromium/quic_stream_factory.h" |
33 #include "net/quic/core/quic_tag.h" | 38 #include "net/quic/core/quic_tag.h" |
34 #include "net/quic/core/quic_versions.h" | 39 #include "net/quic/core/quic_versions.h" |
35 #include "testing/gmock/include/gmock/gmock.h" | 40 #include "testing/gmock/include/gmock/gmock.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
37 | 42 |
38 #if BUILDFLAG(ENABLE_EXTENSIONS) | 43 #if BUILDFLAG(ENABLE_EXTENSIONS) |
39 #include "chrome/browser/extensions/event_router_forwarder.h" | 44 #include "chrome/browser/extensions/event_router_forwarder.h" |
40 #endif | 45 #endif |
41 | 46 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 EXPECT_EQ(expected, http_auth_preferences->NegotiateDisableCnameLookup()); | 80 EXPECT_EQ(expected, http_auth_preferences->NegotiateDisableCnameLookup()); |
76 } | 81 } |
77 | 82 |
78 void CheckNegotiateEnablePort(bool expected) { | 83 void CheckNegotiateEnablePort(bool expected) { |
79 auto* http_auth_preferences = | 84 auto* http_auth_preferences = |
80 IOThreadPeer::GetAuthPreferences(io_thread_.get()); | 85 IOThreadPeer::GetAuthPreferences(io_thread_.get()); |
81 ASSERT_NE(nullptr, http_auth_preferences); | 86 ASSERT_NE(nullptr, http_auth_preferences); |
82 EXPECT_EQ(expected, http_auth_preferences->NegotiateEnablePort()); | 87 EXPECT_EQ(expected, http_auth_preferences->NegotiateEnablePort()); |
83 } | 88 } |
84 | 89 |
| 90 void CheckEffectiveConnectionType(net::EffectiveConnectionType expected) { |
| 91 ASSERT_EQ(expected, |
| 92 io_thread_->globals() |
| 93 ->network_quality_estimator->GetEffectiveConnectionType()); |
| 94 } |
| 95 |
85 #if defined(OS_ANDROID) | 96 #if defined(OS_ANDROID) |
86 void CheckAuthAndroidNegoitateAccountType(std::string expected) { | 97 void CheckAuthAndroidNegoitateAccountType(std::string expected) { |
87 auto* http_auth_preferences = | 98 auto* http_auth_preferences = |
88 IOThreadPeer::GetAuthPreferences(io_thread_.get()); | 99 IOThreadPeer::GetAuthPreferences(io_thread_.get()); |
89 ASSERT_NE(nullptr, http_auth_preferences); | 100 ASSERT_NE(nullptr, http_auth_preferences); |
90 EXPECT_EQ(expected, | 101 EXPECT_EQ(expected, |
91 http_auth_preferences->AuthAndroidNegotiateAccountType()); | 102 http_auth_preferences->AuthAndroidNegotiateAccountType()); |
92 } | 103 } |
93 #endif | 104 #endif |
94 | 105 |
(...skipping 25 matching lines...) Expand all Loading... |
120 EXPECT_CALL(policy_service_, | 131 EXPECT_CALL(policy_service_, |
121 GetPolicies(policy::PolicyNamespace( | 132 GetPolicies(policy::PolicyNamespace( |
122 policy::POLICY_DOMAIN_CHROME, std::string()))) | 133 policy::POLICY_DOMAIN_CHROME, std::string()))) |
123 .WillRepeatedly(ReturnRef(policy_map_)); | 134 .WillRepeatedly(ReturnRef(policy_map_)); |
124 | 135 |
125 #if defined(OS_CHROMEOS) | 136 #if defined(OS_CHROMEOS) |
126 // Needed by IOThread constructor. | 137 // Needed by IOThread constructor. |
127 chromeos::DBusThreadManager::Initialize(); | 138 chromeos::DBusThreadManager::Initialize(); |
128 chromeos::NetworkHandler::Initialize(); | 139 chromeos::NetworkHandler::Initialize(); |
129 #endif | 140 #endif |
| 141 } |
| 142 |
| 143 void CreateThreads() { |
130 // The IOThread constructor registers the IOThread object with as the | 144 // The IOThread constructor registers the IOThread object with as the |
131 // BrowserThreadDelegate for the io thread. | 145 // BrowserThreadDelegate for the io thread. |
132 io_thread_.reset(new IOThread(&pref_service_, &policy_service_, nullptr, | 146 io_thread_.reset(new IOThread(&pref_service_, &policy_service_, nullptr, |
133 #if BUILDFLAG(ENABLE_EXTENSIONS) | 147 #if BUILDFLAG(ENABLE_EXTENSIONS) |
134 event_router_forwarder_.get() | 148 event_router_forwarder_.get() |
135 #else | 149 #else |
136 nullptr | 150 nullptr |
137 #endif | 151 #endif |
138 )); | 152 )); |
139 // Now that IOThread object is registered starting the threads will | 153 // Now that IOThread object is registered starting the threads will |
140 // call the IOThread::Init(). This sets up the environment needed for | 154 // call the IOThread::Init(). This sets up the environment needed for |
141 // these tests. | 155 // these tests. |
142 thread_bundle_.CreateThreads(); | 156 thread_bundle_.CreateThreads(); |
143 } | 157 } |
144 | 158 |
145 ~IOThreadTestWithIOThreadObject() override { | 159 ~IOThreadTestWithIOThreadObject() override { |
146 #if defined(USE_NSS_CERTS) | 160 #if defined(USE_NSS_CERTS) |
147 // Reset OCSPIOLoop thread checks, so that the test runner can run | 161 // Reset OCSPIOLoop thread checks, so that the test runner can run |
148 // futher tests in the same process. | 162 // futher tests in the same process. |
149 RunOnIOThreadBlocking(base::Bind(&net::ResetNSSHttpIOForTesting)); | 163 RunOnIOThreadBlocking(base::Bind(&net::ResetNSSHttpIOForTesting)); |
150 #endif | 164 #endif |
151 #if defined(OS_CHROMEOS) | 165 #if defined(OS_CHROMEOS) |
152 chromeos::NetworkHandler::Shutdown(); | 166 chromeos::NetworkHandler::Shutdown(); |
153 chromeos::DBusThreadManager::Shutdown(); | 167 chromeos::DBusThreadManager::Shutdown(); |
154 #endif | 168 #endif |
155 } | 169 } |
| 170 |
156 TestingPrefServiceSimple* pref_service() { return &pref_service_; } | 171 TestingPrefServiceSimple* pref_service() { return &pref_service_; } |
157 | 172 |
158 void RunOnIOThreadBlocking(const base::Closure& task) { | 173 void RunOnIOThreadBlocking(const base::Closure& task) { |
159 base::RunLoop run_loop; | 174 base::RunLoop run_loop; |
160 content::BrowserThread::PostTaskAndReply( | 175 content::BrowserThread::PostTaskAndReply( |
161 content::BrowserThread::IO, FROM_HERE, task, run_loop.QuitClosure()); | 176 content::BrowserThread::IO, FROM_HERE, task, run_loop.QuitClosure()); |
162 run_loop.Run(); | 177 run_loop.Run(); |
163 } | 178 } |
164 | 179 |
165 private: | 180 private: |
(...skipping 12 matching lines...) Expand all Loading... |
178 // the bundle is deleted first. | 193 // the bundle is deleted first. |
179 std::unique_ptr<IOThread> io_thread_; | 194 std::unique_ptr<IOThread> io_thread_; |
180 content::TestBrowserThreadBundle thread_bundle_; | 195 content::TestBrowserThreadBundle thread_bundle_; |
181 }; | 196 }; |
182 | 197 |
183 TEST_F(IOThreadTestWithIOThreadObject, UpdateNegotiateDisableCnameLookup) { | 198 TEST_F(IOThreadTestWithIOThreadObject, UpdateNegotiateDisableCnameLookup) { |
184 // This test uses the kDisableAuthNegotiateCnameLookup to check that | 199 // This test uses the kDisableAuthNegotiateCnameLookup to check that |
185 // the HttpAuthPreferences are correctly initialized and running on the | 200 // the HttpAuthPreferences are correctly initialized and running on the |
186 // IO thread. The other preferences are tested by the HttpAuthPreferences | 201 // IO thread. The other preferences are tested by the HttpAuthPreferences |
187 // unit tests. | 202 // unit tests. |
| 203 CreateThreads(); |
188 pref_service()->SetBoolean(prefs::kDisableAuthNegotiateCnameLookup, false); | 204 pref_service()->SetBoolean(prefs::kDisableAuthNegotiateCnameLookup, false); |
189 RunOnIOThreadBlocking( | 205 RunOnIOThreadBlocking( |
190 base::Bind(&IOThreadTestWithIOThreadObject::CheckCnameLookup, | 206 base::Bind(&IOThreadTestWithIOThreadObject::CheckCnameLookup, |
191 base::Unretained(this), false)); | 207 base::Unretained(this), false)); |
192 pref_service()->SetBoolean(prefs::kDisableAuthNegotiateCnameLookup, true); | 208 pref_service()->SetBoolean(prefs::kDisableAuthNegotiateCnameLookup, true); |
193 RunOnIOThreadBlocking( | 209 RunOnIOThreadBlocking( |
194 base::Bind(&IOThreadTestWithIOThreadObject::CheckCnameLookup, | 210 base::Bind(&IOThreadTestWithIOThreadObject::CheckCnameLookup, |
195 base::Unretained(this), true)); | 211 base::Unretained(this), true)); |
196 } | 212 } |
197 | 213 |
198 TEST_F(IOThreadTestWithIOThreadObject, UpdateEnableAuthNegotiatePort) { | 214 TEST_F(IOThreadTestWithIOThreadObject, UpdateEnableAuthNegotiatePort) { |
| 215 CreateThreads(); |
199 pref_service()->SetBoolean(prefs::kEnableAuthNegotiatePort, false); | 216 pref_service()->SetBoolean(prefs::kEnableAuthNegotiatePort, false); |
200 RunOnIOThreadBlocking( | 217 RunOnIOThreadBlocking( |
201 base::Bind(&IOThreadTestWithIOThreadObject::CheckNegotiateEnablePort, | 218 base::Bind(&IOThreadTestWithIOThreadObject::CheckNegotiateEnablePort, |
202 base::Unretained(this), false)); | 219 base::Unretained(this), false)); |
203 pref_service()->SetBoolean(prefs::kEnableAuthNegotiatePort, true); | 220 pref_service()->SetBoolean(prefs::kEnableAuthNegotiatePort, true); |
204 RunOnIOThreadBlocking( | 221 RunOnIOThreadBlocking( |
205 base::Bind(&IOThreadTestWithIOThreadObject::CheckNegotiateEnablePort, | 222 base::Bind(&IOThreadTestWithIOThreadObject::CheckNegotiateEnablePort, |
206 base::Unretained(this), true)); | 223 base::Unretained(this), true)); |
207 } | 224 } |
208 | 225 |
209 TEST_F(IOThreadTestWithIOThreadObject, UpdateServerWhitelist) { | 226 TEST_F(IOThreadTestWithIOThreadObject, UpdateServerWhitelist) { |
| 227 CreateThreads(); |
210 GURL url("http://test.example.com"); | 228 GURL url("http://test.example.com"); |
211 | 229 |
212 pref_service()->SetString(prefs::kAuthServerWhitelist, "xxx"); | 230 pref_service()->SetString(prefs::kAuthServerWhitelist, "xxx"); |
213 RunOnIOThreadBlocking( | 231 RunOnIOThreadBlocking( |
214 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanUseDefaultCredentials, | 232 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanUseDefaultCredentials, |
215 base::Unretained(this), false, url)); | 233 base::Unretained(this), false, url)); |
216 | 234 |
217 pref_service()->SetString(prefs::kAuthServerWhitelist, "*"); | 235 pref_service()->SetString(prefs::kAuthServerWhitelist, "*"); |
218 RunOnIOThreadBlocking( | 236 RunOnIOThreadBlocking( |
219 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanUseDefaultCredentials, | 237 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanUseDefaultCredentials, |
220 base::Unretained(this), true, url)); | 238 base::Unretained(this), true, url)); |
221 } | 239 } |
222 | 240 |
223 TEST_F(IOThreadTestWithIOThreadObject, UpdateDelegateWhitelist) { | 241 TEST_F(IOThreadTestWithIOThreadObject, UpdateDelegateWhitelist) { |
| 242 CreateThreads(); |
224 GURL url("http://test.example.com"); | 243 GURL url("http://test.example.com"); |
225 | 244 |
226 pref_service()->SetString(prefs::kAuthNegotiateDelegateWhitelist, ""); | 245 pref_service()->SetString(prefs::kAuthNegotiateDelegateWhitelist, ""); |
227 RunOnIOThreadBlocking( | 246 RunOnIOThreadBlocking( |
228 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanDelegate, | 247 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanDelegate, |
229 base::Unretained(this), false, url)); | 248 base::Unretained(this), false, url)); |
230 | 249 |
231 pref_service()->SetString(prefs::kAuthNegotiateDelegateWhitelist, "*"); | 250 pref_service()->SetString(prefs::kAuthNegotiateDelegateWhitelist, "*"); |
232 RunOnIOThreadBlocking( | 251 RunOnIOThreadBlocking( |
233 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanDelegate, | 252 base::Bind(&IOThreadTestWithIOThreadObject::CheckCanDelegate, |
234 base::Unretained(this), true, url)); | 253 base::Unretained(this), true, url)); |
235 } | 254 } |
236 | 255 |
237 #if defined(OS_ANDROID) | 256 #if defined(OS_ANDROID) |
238 // AuthAndroidNegotiateAccountType is only used on Android. | 257 // AuthAndroidNegotiateAccountType is only used on Android. |
239 TEST_F(IOThreadTestWithIOThreadObject, UpdateAuthAndroidNegotiateAccountType) { | 258 TEST_F(IOThreadTestWithIOThreadObject, UpdateAuthAndroidNegotiateAccountType) { |
| 259 CreateThreads(); |
240 pref_service()->SetString(prefs::kAuthAndroidNegotiateAccountType, "acc1"); | 260 pref_service()->SetString(prefs::kAuthAndroidNegotiateAccountType, "acc1"); |
241 RunOnIOThreadBlocking(base::Bind( | 261 RunOnIOThreadBlocking(base::Bind( |
242 &IOThreadTestWithIOThreadObject::CheckAuthAndroidNegoitateAccountType, | 262 &IOThreadTestWithIOThreadObject::CheckAuthAndroidNegoitateAccountType, |
243 base::Unretained(this), "acc1")); | 263 base::Unretained(this), "acc1")); |
244 pref_service()->SetString(prefs::kAuthAndroidNegotiateAccountType, "acc2"); | 264 pref_service()->SetString(prefs::kAuthAndroidNegotiateAccountType, "acc2"); |
245 RunOnIOThreadBlocking(base::Bind( | 265 RunOnIOThreadBlocking(base::Bind( |
246 &IOThreadTestWithIOThreadObject::CheckAuthAndroidNegoitateAccountType, | 266 &IOThreadTestWithIOThreadObject::CheckAuthAndroidNegoitateAccountType, |
247 base::Unretained(this), "acc2")); | 267 base::Unretained(this), "acc2")); |
248 } | 268 } |
249 #endif | 269 #endif |
250 | 270 |
| 271 TEST_F(IOThreadTestWithIOThreadObject, ForceECTFromCommandLine) { |
| 272 CreateThreads(); |
| 273 base::CommandLine::Init(0, nullptr); |
| 274 ASSERT_TRUE(base::CommandLine::InitializedForCurrentProcess()); |
| 275 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 276 "--force-effective-connection-type", "Slow-2G"); |
| 277 |
| 278 RunOnIOThreadBlocking(base::Bind( |
| 279 &IOThreadTestWithIOThreadObject::CheckEffectiveConnectionType, |
| 280 base::Unretained(this), net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); |
| 281 } |
| 282 |
| 283 TEST_F(IOThreadTestWithIOThreadObject, ForceECTUsingFieldTrial) { |
| 284 base::CommandLine::Init(0, nullptr); |
| 285 ASSERT_TRUE(base::CommandLine::InitializedForCurrentProcess()); |
| 286 |
| 287 variations::testing::ClearAllVariationParams(); |
| 288 std::map<std::string, std::string> variation_params; |
| 289 variation_params["force_effective_connection_type"] = "2G"; |
| 290 ASSERT_TRUE(variations::AssociateVariationParams( |
| 291 "NetworkQualityEstimator", "Enabled", variation_params)); |
| 292 |
| 293 base::FieldTrialList field_trial_list(nullptr); |
| 294 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("NetworkQualityEstimator", |
| 295 "Enabled")); |
| 296 |
| 297 // Create threads after the field trial has been set. |
| 298 CreateThreads(); |
| 299 |
| 300 RunOnIOThreadBlocking( |
| 301 base::Bind(&IOThreadTestWithIOThreadObject::CheckEffectiveConnectionType, |
| 302 base::Unretained(this), net::EFFECTIVE_CONNECTION_TYPE_2G)); |
| 303 } |
| 304 |
| 305 TEST_F(IOThreadTestWithIOThreadObject, ECTFromCommandLineOverridesFieldTrial) { |
| 306 base::CommandLine::Init(0, nullptr); |
| 307 ASSERT_TRUE(base::CommandLine::InitializedForCurrentProcess()); |
| 308 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 309 "--force-effective-connection-type", "Slow-2G"); |
| 310 |
| 311 variations::testing::ClearAllVariationParams(); |
| 312 std::map<std::string, std::string> variation_params; |
| 313 variation_params["force_effective_connection_type"] = "2G"; |
| 314 ASSERT_TRUE(variations::AssociateVariationParams( |
| 315 "NetworkQualityEstimator", "Enabled", variation_params)); |
| 316 |
| 317 base::FieldTrialList field_trial_list(nullptr); |
| 318 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("NetworkQualityEstimator", |
| 319 "Enabled")); |
| 320 |
| 321 // Create threads after the field trial has been set. |
| 322 CreateThreads(); |
| 323 RunOnIOThreadBlocking(base::Bind( |
| 324 &IOThreadTestWithIOThreadObject::CheckEffectiveConnectionType, |
| 325 base::Unretained(this), net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G)); |
| 326 } |
| 327 |
251 class ConfigureParamsFromFieldTrialsAndCommandLineTest | 328 class ConfigureParamsFromFieldTrialsAndCommandLineTest |
252 : public ::testing::Test { | 329 : public ::testing::Test { |
253 public: | 330 public: |
254 ConfigureParamsFromFieldTrialsAndCommandLineTest() | 331 ConfigureParamsFromFieldTrialsAndCommandLineTest() |
255 : command_line_(base::CommandLine::NO_PROGRAM), | 332 : command_line_(base::CommandLine::NO_PROGRAM), |
256 is_quic_allowed_by_policy_(true) {} | 333 is_quic_allowed_by_policy_(true) {} |
257 | 334 |
258 protected: | 335 protected: |
259 void ConfigureParamsFromFieldTrialsAndCommandLine() { | 336 void ConfigureParamsFromFieldTrialsAndCommandLine() { |
260 IOThreadPeer::ConfigureParamsFromFieldTrialsAndCommandLine( | 337 IOThreadPeer::ConfigureParamsFromFieldTrialsAndCommandLine( |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 command_line_.AppendSwitchASCII("testing-fixed-http-port", "42"); | 493 command_line_.AppendSwitchASCII("testing-fixed-http-port", "42"); |
417 command_line_.AppendSwitchASCII("testing-fixed-https-port", "137"); | 494 command_line_.AppendSwitchASCII("testing-fixed-https-port", "137"); |
418 | 495 |
419 ConfigureParamsFromFieldTrialsAndCommandLine(); | 496 ConfigureParamsFromFieldTrialsAndCommandLine(); |
420 | 497 |
421 EXPECT_EQ(42u, params_.testing_fixed_http_port); | 498 EXPECT_EQ(42u, params_.testing_fixed_http_port); |
422 EXPECT_EQ(137u, params_.testing_fixed_https_port); | 499 EXPECT_EQ(137u, params_.testing_fixed_https_port); |
423 } | 500 } |
424 | 501 |
425 } // namespace test | 502 } // namespace test |
OLD | NEW |