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

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

Issue 45026: Prevent making real DNS lookups by chrome tests. (Closed)
Patch Set: simplified Created 11 years, 9 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/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
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 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 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 #ifndef NET_BASE_HOST_RESOLVER_UNITTEST_H_ 5 #ifndef NET_BASE_HOST_RESOLVER_UNITTEST_H_
6 #define NET_BASE_HOST_RESOLVER_UNITTEST_H_ 6 #define NET_BASE_HOST_RESOLVER_UNITTEST_H_
7 7
8 #ifdef UNIT_TEST 8 #ifdef UNIT_TEST
9 9
10 #include <list> 10 #include <list>
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 // replacement value. Usually, replacement should be an IP address literal. 44 // replacement value. Usually, replacement should be an IP address literal.
45 void AddRule(const char* host_pattern, const char* replacement) { 45 void AddRule(const char* host_pattern, const char* replacement) {
46 rules_.push_back(Rule(host_pattern, replacement)); 46 rules_.push_back(Rule(host_pattern, replacement));
47 } 47 }
48 48
49 void AddRuleWithLatency(const char* host_pattern, const char* replacement, 49 void AddRuleWithLatency(const char* host_pattern, const char* replacement,
50 int latency) { 50 int latency) {
51 rules_.push_back(Rule(host_pattern, replacement, latency)); 51 rules_.push_back(Rule(host_pattern, replacement, latency));
52 } 52 }
53 53
54 private: 54 // Make sure that |host| will not be re-mapped or even processed by underlying
55 std::string Map(const std::string& host) { 55 // host mappers. It can also be a pattern.
56 void AllowDirectLookup(const char* host) {
57 rules_.push_back(Rule(host, "", true));
58 }
59
60 // Simulate a lookup failure for |host| (it also can be a pattern).
61 void AddSimulatedFailure(const char* host) {
62 AddRule(host, "");
63 }
64
65 virtual std::string Map(const std::string& host) {
56 RuleList::iterator r; 66 RuleList::iterator r;
57 for (r = rules_.begin(); r != rules_.end(); ++r) { 67 for (r = rules_.begin(); r != rules_.end(); ++r) {
58 if (MatchPattern(host, r->host_pattern)) { 68 if (MatchPattern(host, r->host_pattern)) {
59 if (r->latency != 0) { 69 if (r->latency != 0) {
60 PlatformThread::Sleep(r->latency); 70 PlatformThread::Sleep(r->latency);
61 r->latency = 1; // Simulate cache warmup. 71 r->latency = 1; // Simulate cache warmup.
62 } 72 }
63 return r->replacement; 73 return r->direct ? host : r->replacement;
64 } 74 }
65 } 75 }
76
66 return MapUsingPrevious(host); 77 return MapUsingPrevious(host);
67 } 78 }
68 79
80 private:
69 struct Rule { 81 struct Rule {
70 std::string host_pattern; 82 std::string host_pattern;
71 std::string replacement; 83 std::string replacement;
72 int latency; // in milliseconds 84 int latency; // in milliseconds
85 bool direct; // if true, don't mangle hostname and ignore replacement
73 Rule(const char* h, const char* r) 86 Rule(const char* h, const char* r)
74 : host_pattern(h), 87 : host_pattern(h),
75 replacement(r), 88 replacement(r),
76 latency(0) {} 89 latency(0),
90 direct(false) {}
77 Rule(const char* h, const char* r, const int l) 91 Rule(const char* h, const char* r, const int l)
78 : host_pattern(h), 92 : host_pattern(h),
79 replacement(r), 93 replacement(r),
80 latency(l) {} 94 latency(l),
95 direct(false) {}
96 Rule(const char* h, const char* r, const bool d)
97 : host_pattern(h),
98 replacement(r),
99 latency(0),
100 direct(d) {}
81 }; 101 };
82 typedef std::list<Rule> RuleList; 102 typedef std::list<Rule> RuleList;
83
84 RuleList rules_; 103 RuleList rules_;
85 }; 104 };
86 105
87 // Using WaitingHostMapper you can simulate very long lookups, for example 106 // Using WaitingHostMapper you can simulate very long lookups, for example
88 // to test code which cancels a request. Example usage: 107 // to test code which cancels a request. Example usage:
89 // 108 //
90 // scoped_refptr<WaitingHostMapper> mapper = new WaitingHostMapper(); 109 // scoped_refptr<WaitingHostMapper> mapper = new WaitingHostMapper();
91 // ScopedHostMapper scoped_mapper(mapper.get()); 110 // ScopedHostMapper scoped_mapper(mapper.get());
92 // 111 //
93 // (start the lookup asynchronously) 112 // (start the lookup asynchronously)
94 // (cancel the lookup) 113 // (cancel the lookup)
95 // 114 //
96 // mapper->Signal(); 115 // mapper->Signal();
97 class WaitingHostMapper : public HostMapper { 116 class WaitingHostMapper : public HostMapper {
98 public: 117 public:
99 WaitingHostMapper() : event_(false, false) { 118 WaitingHostMapper() : event_(false, false) {
100 } 119 }
101 120
102 void Signal() { 121 void Signal() {
103 event_.Signal(); 122 event_.Signal();
104 } 123 }
105 124
106 private: 125 private:
107 std::string Map(const std::string& host) { 126 virtual std::string Map(const std::string& host) {
108 event_.Wait(); 127 event_.Wait();
109 return MapUsingPrevious(host); 128 return MapUsingPrevious(host);
110 } 129 }
111 130
112 base::WaitableEvent event_; 131 base::WaitableEvent event_;
113 }; 132 };
114 133
115 // This class sets the HostMapper for a particular scope. If there are multiple 134 // 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. 135 // 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 136 // However, if it does not provide a matching rule, then it should delegate
(...skipping 24 matching lines...) Expand all
142 private: 161 private:
143 scoped_refptr<HostMapper> current_host_mapper_; 162 scoped_refptr<HostMapper> current_host_mapper_;
144 scoped_refptr<HostMapper> previous_host_mapper_; 163 scoped_refptr<HostMapper> previous_host_mapper_;
145 }; 164 };
146 165
147 } // namespace net 166 } // namespace net
148 167
149 #endif // UNIT_TEST 168 #endif // UNIT_TEST
150 169
151 #endif // NET_BASE_HOST_RESOLVER_UNITTEST_H_ 170 #endif // NET_BASE_HOST_RESOLVER_UNITTEST_H_
OLDNEW
« no previous file with comments | « net/base/host_resolver.cc ('k') | net/base/ssl_client_socket_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698