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

Side by Side Diff: net/base/scoped_host_mapper.h

Issue 18775: Add more unit tests for net/base/host_resolver. (Closed)
Patch Set: add fixes, and actually use previous mapper Created 11 years, 10 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 | « net/base/run_all_unittests.cc ('k') | net/base/ssl_client_socket_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2006-2008 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 // This file defines ScopedHostMapper, which is a helper class for writing
6 // tests that use HostResolver either directly or indirectly.
7 //
8 // In most cases, it is important that unit tests avoid making actual DNS
9 // queries since the resulting tests can be flaky, especially if the network is
10 // unreliable for some reason. To simplify writing tests that avoid making
11 // actual DNS queries, the following helper class may be used:
12 //
13 // ScopedHostMapper scoped_host_mapper;
14 // scoped_host_mapper.AddRule("foo.com", "1.2.3.4");
15 // scoped_host_mapper.AddRule("bar.com", "2.3.4.5");
16 // ...
17 //
18 // The above rules define a static mapping from hostnames to IP address
19 // literals. The first parameter to AddRule specifies a host pattern to match
20 // against, and the second parameter indicates what value should be used to
21 // replace the given hostname. So, the following is also supported:
22 //
23 // scoped_host_mapper.AddRule("*.com", "127.0.0.1");
24 //
25 // If there are multiple ScopedHostMappers in existence, then the last one
26 // allocated will be used. However, if it does not provide a matching rule,
27 // then it will delegate to the previously set HostMapper (see SetHostMapper).
28 // Finally, if no HostMapper matches a given hostname, then the hostname will
29 // be unmodified.
30 //
31 // IMPORTANT: ScopedHostMapper is only designed to be used on a single thread,
32 // and it is a requirement that the lifetimes of multiple instances be nested.
33
34 #ifndef NET_BASE_SCOPED_HOST_MAPPER_H_
35 #define NET_BASE_SCOPED_HOST_MAPPER_H_
36
37 #ifdef UNIT_TEST
38
39 #include <list>
40
41 #include "base/string_util.h"
42 #include "net/base/host_resolver.h"
43 #include "net/base/net_errors.h"
44
45 namespace net {
46
47 // This class sets the HostMapper for a particular scope.
48 class ScopedHostMapper : public HostMapper {
49 public:
50 ScopedHostMapper() {
51 previous_host_mapper_ = SetHostMapper(this);
52 }
53
54 ~ScopedHostMapper() {
55 HostMapper* old_mapper = SetHostMapper(previous_host_mapper_);
56 // The lifetimes of multiple instances must be nested.
57 CHECK(old_mapper == this);
58 }
59
60 // Any hostname matching the given pattern will be replaced with the given
61 // replacement value. Usually, replacement should be an IP address literal.
62 void AddRule(const char* host_pattern, const char* replacement) {
63 rules_.push_back(Rule(host_pattern, replacement));
64 }
65
66 private:
67 std::string Map(const std::string& host) {
68 RuleList::const_iterator r;
69 for (r = rules_.begin(); r != rules_.end(); ++r) {
70 if (MatchPattern(host, r->host_pattern))
71 return r->replacement;
72 }
73 return previous_host_mapper_ ? previous_host_mapper_->Map(host) : host;
74 }
75
76 struct Rule {
77 std::string host_pattern;
78 std::string replacement;
79 Rule(const char* h, const char* r) : host_pattern(h), replacement(r) {}
80 };
81 typedef std::list<Rule> RuleList;
82
83 HostMapper* previous_host_mapper_;
84 RuleList rules_;
85 };
86
87 } // namespace net
88
89 #endif // UNIT_TEST
90
91 #endif // NET_BASE_SCOPED_HOST_MAPPER_H_
OLDNEW
« no previous file with comments | « net/base/run_all_unittests.cc ('k') | net/base/ssl_client_socket_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698