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

Side by Side Diff: net/base/host_resolver_unittest.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/host_resolver.cc ('k') | net/base/host_resolver_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-2009 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 #ifndef NET_BASE_HOST_RESOLVER_UNITTEST_H_
6 #define NET_BASE_HOST_RESOLVER_UNITTEST_H_
7
8 #ifdef UNIT_TEST
9
10 #include <list>
11
12 #include "base/string_util.h"
13 #include "base/platform_thread.h"
14 #include "base/ref_counted.h"
15 #include "base/waitable_event.h"
16 #include "net/base/host_resolver.h"
17 #include "net/base/net_errors.h"
18
19 namespace net {
20
21 // In most cases, it is important that unit tests avoid making actual DNS
22 // queries since the resulting tests can be flaky, especially if the network is
23 // unreliable for some reason. To simplify writing tests that avoid making
24 // actual DNS queries, the following helper class may be used:
25 //
26 // scoped_refptr<RuleBasedHostMapper> host_mapper = new RuleBasedHostMapper();
27 // host_mapper->AddRule("foo.com", "1.2.3.4");
28 // host_mapper->AddRule("bar.com", "2.3.4.5");
29 //
30 // Don't forget to actually set your mapper, probably with ScopedHostMapper!
31 //
32 // The above rules define a static mapping from hostnames to IP address
33 // literals. The first parameter to AddRule specifies a host pattern to match
34 // against, and the second parameter indicates what value should be used to
35 // replace the given hostname. So, the following is also supported:
36 //
37 // host_mapper->AddRule("*.com", "127.0.0.1");
38 //
39 // Replacement doesn't have to be string representing an IP address. It can
40 // re-map one hostname to another as well.
41 class RuleBasedHostMapper : public HostMapper {
42 public:
43 // Any hostname matching the given pattern will be replaced with the given
44 // replacement value. Usually, replacement should be an IP address literal.
45 void AddRule(const char* host_pattern, const char* replacement) {
46 rules_.push_back(Rule(host_pattern, replacement));
47 }
48
49 void AddRuleWithLatency(const char* host_pattern, const char* replacement,
50 int latency) {
51 rules_.push_back(Rule(host_pattern, replacement, latency));
52 }
53
54 private:
55 std::string Map(const std::string& host) {
56 RuleList::iterator r;
57 for (r = rules_.begin(); r != rules_.end(); ++r) {
58 if (MatchPattern(host, r->host_pattern)) {
59 if (r->latency != 0) {
60 PlatformThread::Sleep(r->latency);
61 r->latency = 1; // Simulate cache warmup.
62 }
63 return r->replacement;
64 }
65 }
66 return MapUsingPrevious(host);
67 }
68
69 struct Rule {
70 std::string host_pattern;
71 std::string replacement;
72 int latency; // in milliseconds
73 Rule(const char* h, const char* r)
74 : host_pattern(h),
75 replacement(r),
76 latency(0) {}
77 Rule(const char* h, const char* r, const int l)
78 : host_pattern(h),
79 replacement(r),
80 latency(l) {}
81 };
82 typedef std::list<Rule> RuleList;
83
84 RuleList rules_;
85 };
86
87 // Using WaitingHostMapper you can simulate very long lookups, for example
88 // to test code which cancels a request. Example usage:
89 //
90 // scoped_refptr<WaitingHostMapper> mapper = new WaitingHostMapper();
91 // ScopedHostMapper scoped_mapper(mapper.get());
92 //
93 // (start the lookup asynchronously)
94 // (cancel the lookup)
95 //
96 // mapper->Signal();
97 class WaitingHostMapper : public HostMapper {
98 public:
99 WaitingHostMapper() : event_(false, false) {
100 }
101
102 void Signal() {
103 event_.Signal();
104 }
105
106 private:
107 std::string Map(const std::string& host) {
108 event_.Wait();
109 return MapUsingPrevious(host);
110 }
111
112 base::WaitableEvent event_;
113 };
114
115 // This class sets the HostMapper for a particular scope. If there are multiple
116 // ScopedHostMappers in existence, then the last one allocated will be used.
117 // However, if it does not provide a matching rule, then it should delegate
118 // to the previously set HostMapper (see SetHostMapper). This is true for all
119 // mappers defined in this file. If no HostMapper matches a given hostname, then
120 // the hostname will be unmodified.
121 class ScopedHostMapper {
122 public:
123 ScopedHostMapper(HostMapper* mapper) : current_host_mapper_(mapper) {
124 previous_host_mapper_ = SetHostMapper(current_host_mapper_.get());
125 current_host_mapper_->set_previous_mapper(previous_host_mapper_.get());
126 }
127
128 ~ScopedHostMapper() {
129 HostMapper* old_mapper = SetHostMapper(previous_host_mapper_.get());
130 // The lifetimes of multiple instances must be nested.
131 CHECK(old_mapper == current_host_mapper_.get());
132 }
133
134 private:
135 scoped_refptr<HostMapper> current_host_mapper_;
136 scoped_refptr<HostMapper> previous_host_mapper_;
137 };
138
139 } // namespace net
140
141 #endif // UNIT_TEST
142
143 #endif // NET_BASE_HOST_RESOLVER_UNITTEST_H_
OLDNEW
« no previous file with comments | « net/base/host_resolver.cc ('k') | net/base/host_resolver_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698