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

Side by Side Diff: content/public/test/test_host_resolver.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
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/public/test/test_host_resolver.h"
6
7 #include "base/macros.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/threading/thread.h"
10 #include "content/browser/browser_thread_impl.h"
11 #include "content/browser/notification_service_impl.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/network_interfaces.h"
14 #include "net/dns/mock_host_resolver.h"
15
16 namespace content {
17
18 namespace {
19 // In many cases it may be not obvious that a test makes a real DNS lookup.
20 // We generally don't want to rely on external DNS servers for our tests,
21 // so this host resolver procedure catches external queries and returns a failed
22 // lookup result.
23 class LocalHostResolverProc : public net::HostResolverProc {
24 public:
25 LocalHostResolverProc() : HostResolverProc(NULL) {}
26
27 int Resolve(const std::string& host,
28 net::AddressFamily address_family,
29 net::HostResolverFlags host_resolver_flags,
30 net::AddressList* addrlist,
31 int* os_error) override {
32 const char* kLocalHostNames[] = {"localhost", "127.0.0.1", "::1"};
33 bool local = false;
34
35 if (host == net::GetHostName()) {
36 local = true;
37 } else {
38 for (size_t i = 0; i < arraysize(kLocalHostNames); i++)
39 if (host == kLocalHostNames[i]) {
40 local = true;
41 break;
42 }
43 }
44
45 // To avoid depending on external resources and to reduce (if not preclude)
46 // network interactions from tests, we simulate failure for non-local DNS
47 // queries, rather than perform them.
48 // If you really need to make an external DNS query, use
49 // net::RuleBasedHostResolverProc and its AllowDirectLookup method.
50 if (!local) {
51 DVLOG(1) << "To avoid external dependencies, simulating failure for "
52 "external DNS lookup of "
53 << host;
54 return net::ERR_NOT_IMPLEMENTED;
55 }
56
57 return ResolveUsingPrevious(host, address_family, host_resolver_flags,
58 addrlist, os_error);
59 }
60
61 private:
62 ~LocalHostResolverProc() override {}
63 };
64 }
65
66 TestHostResolver::TestHostResolver()
67 : local_resolver_(new LocalHostResolverProc()),
68 rule_based_resolver_(
69 new net::RuleBasedHostResolverProc(local_resolver_.get())),
70 scoped_local_host_resolver_proc_(
71 new net::ScopedDefaultHostResolverProc(rule_based_resolver_.get())) {
72 rule_based_resolver_->AddSimulatedFailure("wpad");
73 }
74
75 TestHostResolver::~TestHostResolver() {}
76
77 } // namespace content
OLDNEW
« no previous file with comments | « content/public/test/test_host_resolver.h ('k') | content/public/utility/content_utility_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698