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

Unified Diff: net/base/mock_host_resolver.h

Issue 149511: Refactorings surrounding HostResolver:... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Merge in socks5_client_socket_unittest.cc Created 11 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/base/host_resolver_unittest.cc ('k') | net/base/mock_host_resolver.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/mock_host_resolver.h
===================================================================
--- net/base/mock_host_resolver.h (revision 20435)
+++ net/base/mock_host_resolver.h (working copy)
@@ -1,34 +1,30 @@
-// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
+// 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.
-#ifndef NET_BASE_HOST_RESOLVER_UNITTEST_H_
-#define NET_BASE_HOST_RESOLVER_UNITTEST_H_
+#ifndef NET_BASE_MOCK_HOST_RESOLVER_H_
+#define NET_BASE_MOCK_HOST_RESOLVER_H_
-#ifdef UNIT_TEST
-
#include <list>
-#include "base/string_util.h"
-#include "base/platform_thread.h"
-#include "base/ref_counted.h"
#include "base/waitable_event.h"
-#include "net/base/host_resolver.h"
-#include "net/base/net_errors.h"
+#include "net/base/host_resolver_impl.h"
+#include "net/base/host_resolver_proc.h"
namespace net {
+class RuleBasedHostResolverProc;
+
// In most cases, it is important that unit tests avoid making actual DNS
// queries since the resulting tests can be flaky, especially if the network is
// unreliable for some reason. To simplify writing tests that avoid making
-// actual DNS queries, the following helper class may be used:
+// actual DNS queries, pass a MockHostResolver as the HostResolver dependency.
+// The socket addresses returned can be configured using the
+// RuleBasedHostResolverProc:
//
-// scoped_refptr<RuleBasedHostMapper> host_mapper = new RuleBasedHostMapper();
-// host_mapper->AddRule("foo.com", "1.2.3.4");
-// host_mapper->AddRule("bar.com", "2.3.4.5");
+// host_resolver->rules()->AddRule("foo.com", "1.2.3.4");
+// host_resolver->rules()->AddRule("bar.com", "2.3.4.5");
//
-// Don't forget to actually set your mapper, probably with ScopedHostMapper!
-//
// The above rules define a static mapping from hostnames to IP address
// literals. The first parameter to AddRule specifies a host pattern to match
// against, and the second parameter indicates what value should be used to
@@ -38,133 +34,110 @@
//
// Replacement doesn't have to be string representing an IP address. It can
// re-map one hostname to another as well.
-class RuleBasedHostMapper : public HostMapper {
+class MockHostResolver : public HostResolver {
public:
+ // Creates a MockHostResolver that does NOT cache entries
+ // (the HostResolverProc will be called for every lookup). If you need
+ // caching behavior, call Reset() with non-zero cache size.
+ MockHostResolver();
+
+ virtual ~MockHostResolver() {}
+
+ // HostResolver methods:
+ virtual int Resolve(const RequestInfo& info, AddressList* addresses,
+ CompletionCallback* callback, RequestHandle* out_req);
+ virtual void CancelRequest(RequestHandle req);
+ virtual void AddObserver(Observer* observer);
+ virtual void RemoveObserver(Observer* observer);
+ // TODO(eroman): temp hack for http://crbug.com/15513
+ virtual void Shutdown();
+
+ RuleBasedHostResolverProc* rules() { return rules_; }
+
+ // Resets the mock.
+ void Reset(HostResolverProc* interceptor,
+ int max_cache_entries,
+ int max_cache_age_ms);
+
+ private:
+ scoped_refptr<HostResolverImpl> impl_;
+ scoped_refptr<RuleBasedHostResolverProc> rules_;
+};
+
+// RuleBasedHostResolverProc applies a set of rules to map a host string to
+// a replacement host string. It then uses the system host resolver to return
+// a socket address. Generally the replacement should be an IPv4 literal so
+// there is no network dependency.
+class RuleBasedHostResolverProc : public HostResolverProc {
+ public:
+ explicit RuleBasedHostResolverProc(HostResolverProc* previous);
+ ~RuleBasedHostResolverProc();
+
// Any hostname matching the given pattern will be replaced with the given
// replacement value. Usually, replacement should be an IP address literal.
- void AddRule(const char* host_pattern, const char* replacement) {
- rules_.push_back(Rule(host_pattern, replacement));
- }
+ void AddRule(const std::string& host_pattern,
+ const std::string& replacement);
- void AddRuleWithLatency(const char* host_pattern, const char* replacement,
- int latency) {
- rules_.push_back(Rule(host_pattern, replacement, latency));
- }
+ void AddRuleWithLatency(const std::string& host_pattern,
+ const std::string& replacement,
+ int latency_ms);
// Make sure that |host| will not be re-mapped or even processed by underlying
- // host mappers. It can also be a pattern.
- void AllowDirectLookup(const char* host) {
- rules_.push_back(Rule(host, "", true));
- }
+ // host resolver procedures. It can also be a pattern.
+ void AllowDirectLookup(const std::string& host);
// Simulate a lookup failure for |host| (it also can be a pattern).
- void AddSimulatedFailure(const char* host) {
- AddRule(host, "");
- }
+ void AddSimulatedFailure(const std::string& host);
- virtual std::string Map(const std::string& host) {
- RuleList::iterator r;
- for (r = rules_.begin(); r != rules_.end(); ++r) {
- if (MatchPattern(host, r->host_pattern)) {
- if (r->latency != 0) {
- PlatformThread::Sleep(r->latency);
- r->latency = 1; // Simulate cache warmup.
- }
- return r->direct ? host : r->replacement;
- }
- }
+ // HostResolverProc methods:
+ virtual int Resolve(const std::string& host, AddressList* addrlist);
- return MapUsingPrevious(host);
- }
-
private:
- struct Rule {
- std::string host_pattern;
- std::string replacement;
- int latency; // in milliseconds
- bool direct; // if true, don't mangle hostname and ignore replacement
- Rule(const char* h, const char* r)
- : host_pattern(h),
- replacement(r),
- latency(0),
- direct(false) {}
- Rule(const char* h, const char* r, const int l)
- : host_pattern(h),
- replacement(r),
- latency(l),
- direct(false) {}
- Rule(const char* h, const char* r, const bool d)
- : host_pattern(h),
- replacement(r),
- latency(0),
- direct(d) {}
- };
+ struct Rule;
typedef std::list<Rule> RuleList;
RuleList rules_;
};
-// Using WaitingHostMapper you can simulate very long lookups, for example
-// to test code which cancels a request. Example usage:
-//
-// scoped_refptr<WaitingHostMapper> mapper = new WaitingHostMapper();
-// ScopedHostMapper scoped_mapper(mapper.get());
-//
-// (start the lookup asynchronously)
-// (cancel the lookup)
-//
-// mapper->Signal();
-class WaitingHostMapper : public HostMapper {
+// Using WaitingHostResolverProc you can simulate very long lookups.
+class WaitingHostResolverProc : public HostResolverProc {
public:
- WaitingHostMapper() : event_(false, false) {
- }
+ explicit WaitingHostResolverProc(HostResolverProc* previous)
+ : HostResolverProc(previous), event_(false, false) {}
void Signal() {
event_.Signal();
}
- private:
- virtual std::string Map(const std::string& host) {
+ // HostResolverProc methods:
+ virtual int Resolve(const std::string& host, AddressList* addrlist) {
event_.Wait();
- return MapUsingPrevious(host);
+ return ResolveUsingPrevious(host, addrlist);
}
base::WaitableEvent event_;
};
-// This class sets the HostMapper for a particular scope. If there are multiple
-// ScopedHostMappers in existence, then the last one allocated will be used.
-// However, if it does not provide a matching rule, then it should delegate
-// to the previously set HostMapper (see SetHostMapper). This is true for all
-// mappers defined in this file. If no HostMapper matches a given hostname, then
-// the hostname will be unmodified.
-class ScopedHostMapper {
+// This class sets the HostResolverProc for a particular scope. If there are
+// multiple ScopedDefaultHostResolverProc in existence, then the last one
+// allocated will be used. However, if it does not provide a matching rule,
+// then it should delegate to the previously set HostResolverProc.
+//
+// NOTE: Only use this as a catch-all safety net. Individual tests should use
+// MockHostResolver.
+class ScopedDefaultHostResolverProc {
public:
- ScopedHostMapper(HostMapper* mapper) : current_host_mapper_(mapper) {
- previous_host_mapper_ = SetHostMapper(current_host_mapper_.get());
- current_host_mapper_->set_previous_mapper(previous_host_mapper_.get());
- }
+ ScopedDefaultHostResolverProc() {}
+ explicit ScopedDefaultHostResolverProc(HostResolverProc* proc);
- ScopedHostMapper() {}
+ ~ScopedDefaultHostResolverProc();
- ~ScopedHostMapper() {
- HostMapper* old_mapper = SetHostMapper(previous_host_mapper_.get());
- // The lifetimes of multiple instances must be nested.
- CHECK(old_mapper == current_host_mapper_.get());
- }
+ void Init(HostResolverProc* proc);
- void Init(HostMapper* mapper) {
- current_host_mapper_ = mapper;
- previous_host_mapper_ = SetHostMapper(current_host_mapper_.get());
- current_host_mapper_->set_previous_mapper(previous_host_mapper_.get());
- }
-
private:
- scoped_refptr<HostMapper> current_host_mapper_;
- scoped_refptr<HostMapper> previous_host_mapper_;
+ scoped_refptr<HostResolverProc> current_proc_;
+ scoped_refptr<HostResolverProc> previous_proc_;
};
} // namespace net
-#endif // UNIT_TEST
-
-#endif // NET_BASE_HOST_RESOLVER_UNITTEST_H_
+#endif // NET_BASE_MOCK_HOST_RESOLVER_H_
« no previous file with comments | « net/base/host_resolver_unittest.cc ('k') | net/base/mock_host_resolver.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698