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

Side by Side Diff: content/public/test/browser_test_base.cc

Issue 2837813004: Sync the browser test host resolver with the network process before a test runs. (Closed)
Patch Set: review comments Created 3 years, 7 months 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 | « content/public/test/browser_test_base.h ('k') | content/public/test/test_host_resolver.h » ('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 (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "content/public/test/browser_test_base.h" 5 #include "content/public/test/browser_test_base.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/debug/stack_trace.h" 11 #include "base/debug/stack_trace.h"
12 #include "base/feature_list.h" 12 #include "base/feature_list.h"
13 #include "base/i18n/icu_util.h" 13 #include "base/i18n/icu_util.h"
14 #include "base/location.h" 14 #include "base/location.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/sys_info.h" 18 #include "base/sys_info.h"
19 #include "base/test/test_timeouts.h" 19 #include "base/test/test_timeouts.h"
20 #include "base/threading/sequenced_worker_pool.h" 20 #include "base/threading/sequenced_worker_pool.h"
21 #include "base/threading/thread_restrictions.h" 21 #include "base/threading/thread_restrictions.h"
22 #include "build/build_config.h" 22 #include "build/build_config.h"
23 #include "content/browser/renderer_host/render_process_host_impl.h" 23 #include "content/browser/renderer_host/render_process_host_impl.h"
24 #include "content/browser/tracing/tracing_controller_impl.h" 24 #include "content/browser/tracing/tracing_controller_impl.h"
25 #include "content/public/app/content_main.h" 25 #include "content/public/app/content_main.h"
26 #include "content/public/browser/browser_thread.h" 26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/common/content_switches.h" 27 #include "content/public/common/content_switches.h"
28 #include "content/public/common/main_function_params.h" 28 #include "content/public/common/main_function_params.h"
29 #include "content/public/common/network_service_test.mojom.h"
30 #include "content/public/common/service_manager_connection.h"
31 #include "content/public/common/service_names.mojom.h"
29 #include "content/public/test/test_launcher.h" 32 #include "content/public/test/test_launcher.h"
30 #include "content/public/test/test_utils.h" 33 #include "content/public/test/test_utils.h"
31 #include "content/test/content_browser_sanity_checker.h" 34 #include "content/test/content_browser_sanity_checker.h"
32 #include "net/base/net_errors.h"
33 #include "net/base/network_interfaces.h"
34 #include "net/dns/mock_host_resolver.h" 35 #include "net/dns/mock_host_resolver.h"
35 #include "net/test/embedded_test_server/embedded_test_server.h" 36 #include "net/test/embedded_test_server/embedded_test_server.h"
37 #include "services/service_manager/public/cpp/connector.h"
36 #include "ui/base/platform_window_defaults.h" 38 #include "ui/base/platform_window_defaults.h"
37 #include "ui/base/test/material_design_controller_test_api.h" 39 #include "ui/base/test/material_design_controller_test_api.h"
38 #include "ui/compositor/compositor_switches.h" 40 #include "ui/compositor/compositor_switches.h"
39 #include "ui/gl/gl_implementation.h" 41 #include "ui/gl/gl_implementation.h"
40 #include "ui/gl/gl_switches.h" 42 #include "ui/gl/gl_switches.h"
41 43
42 #if defined(OS_POSIX) 44 #if defined(OS_POSIX)
43 #include "base/process/process_handle.h" 45 #include "base/process/process_handle.h"
44 #endif 46 #endif
45 47
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 _exit(128 + signal); 82 _exit(128 + signal);
81 } 83 }
82 #endif // defined(OS_POSIX) 84 #endif // defined(OS_POSIX)
83 85
84 void RunTaskOnRendererThread(const base::Closure& task, 86 void RunTaskOnRendererThread(const base::Closure& task,
85 const base::Closure& quit_task) { 87 const base::Closure& quit_task) {
86 task.Run(); 88 task.Run();
87 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_task); 89 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_task);
88 } 90 }
89 91
90 // In many cases it may be not obvious that a test makes a real DNS lookup.
91 // We generally don't want to rely on external DNS servers for our tests,
92 // so this host resolver procedure catches external queries and returns a failed
93 // lookup result.
94 class LocalHostResolverProc : public net::HostResolverProc {
95 public:
96 LocalHostResolverProc() : HostResolverProc(NULL) {}
97
98 int Resolve(const std::string& host,
99 net::AddressFamily address_family,
100 net::HostResolverFlags host_resolver_flags,
101 net::AddressList* addrlist,
102 int* os_error) override {
103 const char* kLocalHostNames[] = {"localhost", "127.0.0.1", "::1"};
104 bool local = false;
105
106 if (host == net::GetHostName()) {
107 local = true;
108 } else {
109 for (size_t i = 0; i < arraysize(kLocalHostNames); i++)
110 if (host == kLocalHostNames[i]) {
111 local = true;
112 break;
113 }
114 }
115
116 // To avoid depending on external resources and to reduce (if not preclude)
117 // network interactions from tests, we simulate failure for non-local DNS
118 // queries, rather than perform them.
119 // If you really need to make an external DNS query, use
120 // net::RuleBasedHostResolverProc and its AllowDirectLookup method.
121 if (!local) {
122 DVLOG(1) << "To avoid external dependencies, simulating failure for "
123 "external DNS lookup of " << host;
124 return net::ERR_NOT_IMPLEMENTED;
125 }
126
127 return ResolveUsingPrevious(host, address_family, host_resolver_flags,
128 addrlist, os_error);
129 }
130
131 private:
132 ~LocalHostResolverProc() override {}
133 };
134
135 void TraceStopTracingComplete(const base::Closure& quit, 92 void TraceStopTracingComplete(const base::Closure& quit,
136 const base::FilePath& file_path) { 93 const base::FilePath& file_path) {
137 LOG(ERROR) << "Tracing written to: " << file_path.value(); 94 LOG(ERROR) << "Tracing written to: " << file_path.value();
138 quit.Run(); 95 quit.Run();
139 } 96 }
140 97
141 } // namespace 98 } // namespace
142 99
143 extern int BrowserMain(const MainFunctionParams&); 100 extern int BrowserMain(const MainFunctionParams&);
144 101
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 #if defined(OS_CHROMEOS) 219 #if defined(OS_CHROMEOS)
263 // If the test is running on the chromeos envrionment (such as 220 // If the test is running on the chromeos envrionment (such as
264 // device or vm bots), we use hardware GL. 221 // device or vm bots), we use hardware GL.
265 if (base::SysInfo::IsRunningOnChromeOS()) 222 if (base::SysInfo::IsRunningOnChromeOS())
266 use_software_gl = false; 223 use_software_gl = false;
267 #endif 224 #endif
268 225
269 if (use_software_gl && !use_software_compositing_) 226 if (use_software_gl && !use_software_compositing_)
270 command_line->AppendSwitch(switches::kOverrideUseSoftwareGLForTests); 227 command_line->AppendSwitch(switches::kOverrideUseSoftwareGLForTests);
271 228
272 scoped_refptr<net::HostResolverProc> local_resolver = 229 test_host_resolver_.reset(new TestHostResolver);
273 new LocalHostResolverProc();
274 rule_based_resolver_ =
275 new net::RuleBasedHostResolverProc(local_resolver.get());
276 rule_based_resolver_->AddSimulatedFailure("wpad");
277 net::ScopedDefaultHostResolverProc scoped_local_host_resolver_proc(
278 rule_based_resolver_.get());
279 230
280 ContentBrowserSanityChecker scoped_enable_sanity_checks; 231 ContentBrowserSanityChecker scoped_enable_sanity_checks;
281 232
282 SetUpInProcessBrowserTestFixture(); 233 SetUpInProcessBrowserTestFixture();
283 234
284 // At this point, copy features to the command line, since BrowserMain will 235 // At this point, copy features to the command line, since BrowserMain will
285 // wipe out the current feature list. 236 // wipe out the current feature list.
286 std::string enabled_features; 237 std::string enabled_features;
287 std::string disabled_features; 238 std::string disabled_features;
288 if (base::FeatureList::GetInstance()) 239 if (base::FeatureList::GetInstance())
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 } 291 }
341 292
342 { 293 {
343 // This can be called from a posted task. Allow nested tasks here, because 294 // This can be called from a posted task. Allow nested tasks here, because
344 // otherwise the test body will have to do it in order to use RunLoop for 295 // otherwise the test body will have to do it in order to use RunLoop for
345 // waiting. 296 // waiting.
346 base::MessageLoop::ScopedNestableTaskAllower allow( 297 base::MessageLoop::ScopedNestableTaskAllower allow(
347 base::MessageLoop::current()); 298 base::MessageLoop::current());
348 PreRunTestOnMainThread(); 299 PreRunTestOnMainThread();
349 SetUpOnMainThread(); 300 SetUpOnMainThread();
301
302 // Tests would have added their host_resolver() rules by now, so copy them
303 // to the network process if it's in use.
304 InitializeNetworkProcess();
305
350 bool old_io_allowed_value = false; 306 bool old_io_allowed_value = false;
351 if (!disable_io_checks_) 307 if (!disable_io_checks_)
352 base::ThreadRestrictions::SetIOAllowed(false); 308 base::ThreadRestrictions::SetIOAllowed(false);
353 RunTestOnMainThread(); 309 RunTestOnMainThread();
354 if (!disable_io_checks_) 310 if (!disable_io_checks_)
355 base::ThreadRestrictions::SetIOAllowed(old_io_allowed_value); 311 base::ThreadRestrictions::SetIOAllowed(old_io_allowed_value);
356 TearDownOnMainThread(); 312 TearDownOnMainThread();
357 PostRunTestOnMainThread(); 313 PostRunTestOnMainThread();
358 } 314 }
359 315
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 void BrowserTestBase::UseSoftwareCompositing() { 365 void BrowserTestBase::UseSoftwareCompositing() {
410 use_software_compositing_ = true; 366 use_software_compositing_ = true;
411 } 367 }
412 368
413 bool BrowserTestBase::UsingSoftwareGL() const { 369 bool BrowserTestBase::UsingSoftwareGL() const {
414 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess(); 370 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
415 return cmd->GetSwitchValueASCII(switches::kUseGL) == 371 return cmd->GetSwitchValueASCII(switches::kUseGL) ==
416 gl::GetGLImplementationName(gl::GetSoftwareGLImplementation()); 372 gl::GetGLImplementationName(gl::GetSoftwareGLImplementation());
417 } 373 }
418 374
375 void BrowserTestBase::InitializeNetworkProcess() {
376 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
377 switches::kEnableNetworkService))
378 return;
379
380 net::RuleBasedHostResolverProc::RuleList rules = host_resolver()->GetRules();
381 std::vector<mojom::RulePtr> mojo_rules;
382 for (const auto& rule : rules) {
383 // For now, this covers all the rules used in content's tests.
384 // TODO(jam: expand this when we try to make browser_tests and
385 // components_browsertests work.
386 if (rule.resolver_type !=
387 net::RuleBasedHostResolverProc::Rule::kResolverTypeSystem ||
388 rule.address_family != net::AddressFamily::ADDRESS_FAMILY_UNSPECIFIED ||
389 !!rule.latency_ms || rule.replacement.empty())
390 continue;
391 mojom::RulePtr mojo_rule = mojom::Rule::New();
392 mojo_rule->host_pattern = rule.host_pattern;
393 mojo_rule->replacement = rule.replacement;
394 mojo_rules.push_back(std::move(mojo_rule));
395 }
396
397 if (mojo_rules.empty())
398 return;
399
400 mojom::NetworkServiceTestPtr network_service_test;
401 ServiceManagerConnection::GetForProcess()->GetConnector()->BindInterface(
402 mojom::kNetworkServiceName, &network_service_test);
403 network_service_test->AddRules(std::move(mojo_rules));
404
405 // TODO(jam): enable this once all access to host_resolver() is in
406 // SetUpOnMainThread or before. http://crbug.com/713847
407 // host_resolver()->DisableModifications();
408 }
409
419 } // namespace content 410 } // namespace content
OLDNEW
« no previous file with comments | « content/public/test/browser_test_base.h ('k') | content/public/test/test_host_resolver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698