summaryrefslogtreecommitdiffstats
path: root/net/base/mock_host_resolver.cc
blob: c6d31e9cb1aefa82c2aa9eb30358669d28497520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/mock_host_resolver.h"

#include "base/string_util.h"
#include "base/platform_thread.h"
#include "base/ref_counted.h"
#include "googleurl/src/url_canon_ip.h"
#include "net/base/net_errors.h"

namespace net {

namespace {
// Fills |addrlist| with a socket address for |host| which should be an
// IPv6 literal. Returns OK on success.
int ResolveIPV6LiteralUsingGURL(const std::string& host,
                                AddressList* addrlist) {
  // GURL expects the hostname to be surrounded with brackets.
  std::string host_brackets = "[" + host + "]";
  url_parse::Component host_comp(0, host_brackets.size());

  // Try parsing the hostname as an IPv6 literal.
  unsigned char ipv6_addr[16];  // 128 bits.
  bool ok = url_canon::IPv6AddressToNumber(host_brackets.data(),
                                           host_comp,
                                           ipv6_addr);
  if (!ok) {
    LOG(WARNING) << "Not an IPv6 literal: " << host;
    return ERR_UNEXPECTED;
  }

  *addrlist = AddressList::CreateIPv6Address(ipv6_addr);
  return OK;
}

}  // namespace

MockHostResolverBase::MockHostResolverBase(bool use_caching)
    : use_caching_(use_caching) {
  Reset(NULL);
}

int MockHostResolverBase::Resolve(const RequestInfo& info,
                                  AddressList* addresses,
                                  CompletionCallback* callback,
                                  RequestHandle* out_req,
                                  LoadLog* load_log) {
  if (synchronous_mode_) {
    callback = NULL;
    out_req = NULL;
  }
  return impl_->Resolve(info, addresses, callback, out_req, load_log);
}

void MockHostResolverBase::CancelRequest(RequestHandle req) {
  impl_->CancelRequest(req);
}

void MockHostResolverBase::AddObserver(Observer* observer) {
  impl_->AddObserver(observer);
}

void MockHostResolverBase::RemoveObserver(Observer* observer) {
  impl_->RemoveObserver(observer);
}

HostCache* MockHostResolverBase::GetHostCache() {
  return impl_->GetHostCache();
}

void MockHostResolverBase::Shutdown() {
  impl_->Shutdown();
}

void MockHostResolverBase::Reset(HostResolverProc* interceptor) {
  synchronous_mode_ = false;

  // At the root of the chain, map everything to localhost.
  scoped_refptr<RuleBasedHostResolverProc> catchall =
      new RuleBasedHostResolverProc(NULL);
  catchall->AddRule("*", "127.0.0.1");

  // Next add a rules-based layer the use controls.
  rules_ = new RuleBasedHostResolverProc(catchall);

  HostResolverProc* proc = rules_;

  // Lastly add the provided interceptor to the front of the chain.
  if (interceptor) {
    interceptor->set_previous_proc(proc);
    proc = interceptor;
  }

  int max_cache_entries = use_caching_ ? 100 : 0;
  int max_cache_age_ms = use_caching_ ? 60000 : 0;

  impl_ = new HostResolverImpl(proc, max_cache_entries, max_cache_age_ms);
}

//-----------------------------------------------------------------------------

struct RuleBasedHostResolverProc::Rule {
  enum ResolverType {
    kResolverTypeFail,
    kResolverTypeSystem,
    kResolverTypeIPV6Literal,
  };

  ResolverType resolver_type;
  std::string host_pattern;
  std::string replacement;
  int latency_ms;  // In milliseconds.

  Rule(ResolverType resolver_type,
       const std::string& host_pattern,
       const std::string& replacement,
       int latency_ms)
      : resolver_type(resolver_type),
        host_pattern(host_pattern),
        replacement(replacement),
        latency_ms(latency_ms) {}
};

RuleBasedHostResolverProc::RuleBasedHostResolverProc(HostResolverProc* previous)
    : HostResolverProc(previous) {
}

RuleBasedHostResolverProc::~RuleBasedHostResolverProc() {
}

void RuleBasedHostResolverProc::AddRule(const std::string& host_pattern,
                                        const std::string& replacement) {
  DCHECK(!replacement.empty());
  Rule rule(Rule::kResolverTypeSystem, host_pattern, replacement, 0);
  rules_.push_back(rule);
}

void RuleBasedHostResolverProc::AddIPv6Rule(const std::string& host_pattern,
                                            const std::string& ipv6_literal) {
  Rule rule(Rule::kResolverTypeIPV6Literal, host_pattern, ipv6_literal, 0);
  rules_.push_back(rule);
}

void RuleBasedHostResolverProc::AddRuleWithLatency(
    const std::string& host_pattern,
    const std::string& replacement,
    int latency_ms) {
  DCHECK(!replacement.empty());
  Rule rule(Rule::kResolverTypeSystem, host_pattern, replacement, latency_ms);
  rules_.push_back(rule);
}

void RuleBasedHostResolverProc::AllowDirectLookup(
    const std::string& host_pattern) {
  Rule rule(Rule::kResolverTypeSystem, host_pattern, "", 0);
  rules_.push_back(rule);
}

void RuleBasedHostResolverProc::AddSimulatedFailure(
    const std::string& host_pattern) {
  Rule rule(Rule::kResolverTypeFail, host_pattern, "", 0);
  rules_.push_back(rule);
}

int RuleBasedHostResolverProc::Resolve(const std::string& host,
                                       AddressFamily address_family,
                                       AddressList* addrlist) {
  RuleList::iterator r;
  for (r = rules_.begin(); r != rules_.end(); ++r) {
    if (MatchPattern(host, r->host_pattern)) {
      if (r->latency_ms != 0)
        PlatformThread::Sleep(r->latency_ms);

      // Remap to a new host.
      const std::string& effective_host =
          r->replacement.empty() ? host : r->replacement;

      // Apply the resolving function to the remapped hostname.
      switch (r->resolver_type) {
        case Rule::kResolverTypeFail:
          return ERR_NAME_NOT_RESOLVED;
        case Rule::kResolverTypeSystem:
          return SystemHostResolverProc(effective_host,
                                        address_family,
                                        addrlist);
        case Rule::kResolverTypeIPV6Literal:
          return ResolveIPV6LiteralUsingGURL(effective_host, addrlist);
        default:
          NOTREACHED();
          return ERR_UNEXPECTED;
      }
    }
  }
  return ResolveUsingPrevious(host, address_family, addrlist);
}

//-----------------------------------------------------------------------------

ScopedDefaultHostResolverProc::ScopedDefaultHostResolverProc(
    HostResolverProc* proc) : current_proc_(proc) {
  previous_proc_ = HostResolverProc::SetDefault(current_proc_);
  current_proc_->set_previous_proc(previous_proc_);
}

ScopedDefaultHostResolverProc::~ScopedDefaultHostResolverProc() {
  HostResolverProc* old_proc = HostResolverProc::SetDefault(previous_proc_);
  // The lifetimes of multiple instances must be nested.
  CHECK(old_proc == current_proc_);
}

void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) {
  current_proc_ = proc;
  previous_proc_ = HostResolverProc::SetDefault(current_proc_);
  current_proc_->set_previous_proc(previous_proc_);
}

}  // namespace net