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

Unified Diff: net/base/mock_host_resolver.cc

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/mock_host_resolver.h ('k') | net/base/run_all_unittests.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/mock_host_resolver.cc
===================================================================
--- net/base/mock_host_resolver.cc (revision 20435)
+++ net/base/mock_host_resolver.cc (working copy)
@@ -1,170 +1,154 @@
-// 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_
+#include "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"
namespace net {
-// 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:
-//
-// 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");
-//
-// 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
-// replace the given hostname. So, the following is also supported:
-//
-// host_mapper->AddRule("*.com", "127.0.0.1");
-//
-// 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 {
- public:
- // 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));
- }
+MockHostResolver::MockHostResolver() {
+ Reset(NULL, 0, 0);
+}
- void AddRuleWithLatency(const char* host_pattern, const char* replacement,
- int latency) {
- rules_.push_back(Rule(host_pattern, replacement, latency));
- }
+int MockHostResolver::Resolve(const RequestInfo& info,
+ AddressList* addresses,
+ CompletionCallback* callback,
+ RequestHandle* out_req) {
+ return impl_->Resolve(info, addresses, callback, out_req);
+}
- // 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));
- }
+void MockHostResolver::CancelRequest(RequestHandle req) {
+ impl_->CancelRequest(req);
+}
- // Simulate a lookup failure for |host| (it also can be a pattern).
- void AddSimulatedFailure(const char* host) {
- AddRule(host, "");
- }
+void MockHostResolver::AddObserver(Observer* observer) {
+ impl_->AddObserver(observer);
+}
- 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;
- }
- }
+void MockHostResolver::RemoveObserver(Observer* observer) {
+ impl_->RemoveObserver(observer);
+}
- return MapUsingPrevious(host);
- }
+void MockHostResolver::Shutdown() {
+ impl_->Shutdown();
+}
- 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) {}
- };
- typedef std::list<Rule> RuleList;
- RuleList rules_;
-};
+void MockHostResolver::Reset(HostResolverProc* interceptor,
+ int max_cache_entries,
+ int max_cache_age_ms) {
+ // At the root of the chain, map everything to localhost.
+ scoped_refptr<RuleBasedHostResolverProc> catchall =
+ new RuleBasedHostResolverProc(NULL);
+ catchall->AddRule("*", "127.0.0.1");
-// 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 {
- public:
- WaitingHostMapper() : event_(false, false) {
- }
+ // Next add a rules-based layer the use controls.
+ rules_ = new RuleBasedHostResolverProc(catchall);
- void Signal() {
- event_.Signal();
- }
+ HostResolverProc* proc = rules_;
- private:
- virtual std::string Map(const std::string& host) {
- event_.Wait();
- return MapUsingPrevious(host);
+ // Lastly add the provided interceptor to the front of the chain.
+ if (interceptor) {
+ interceptor->set_previous_proc(proc);
+ proc = interceptor;
}
- base::WaitableEvent event_;
+ impl_ = new HostResolverImpl(proc, max_cache_entries, max_cache_age_ms);
+}
+
+//-----------------------------------------------------------------------------
+
+struct RuleBasedHostResolverProc::Rule {
+ std::string host_pattern;
+ std::string replacement;
+ int latency_ms; // In milliseconds.
+ bool direct; // if true, don't mangle hostname and ignore replacement
+ Rule(const std::string& host_pattern, const std::string& replacement)
+ : host_pattern(host_pattern),
+ replacement(replacement),
+ latency_ms(0),
+ direct(false) {}
+ Rule(const std::string& host_pattern, const std::string& replacement,
+ const int latency_ms)
+ : host_pattern(host_pattern),
+ replacement(replacement),
+ latency_ms(latency_ms),
+ direct(false) {}
+ Rule(const std::string& host_pattern, const std::string& replacement,
+ const bool direct)
+ : host_pattern(host_pattern),
+ replacement(replacement),
+ latency_ms(0),
+ direct(direct) {}
};
-// 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 {
- 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());
- }
+RuleBasedHostResolverProc::RuleBasedHostResolverProc(HostResolverProc* previous)
+ : HostResolverProc(previous) {
+}
- ScopedHostMapper() {}
+RuleBasedHostResolverProc::~RuleBasedHostResolverProc() {
+}
- ~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 RuleBasedHostResolverProc::AddRule(const std::string& host_pattern,
+ const std::string& replacement) {
+ rules_.push_back(Rule(host_pattern, replacement));
+}
- 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());
+void RuleBasedHostResolverProc::AddRuleWithLatency(
+ const std::string& host_pattern,
+ const std::string& replacement, int latency_ms) {
+ rules_.push_back(Rule(host_pattern, replacement, latency_ms));
+}
+
+void RuleBasedHostResolverProc::AllowDirectLookup(const std::string& host) {
+ rules_.push_back(Rule(host, "", true));
+}
+
+void RuleBasedHostResolverProc::AddSimulatedFailure(const std::string& host) {
+ AddRule(host, "");
+}
+
+int RuleBasedHostResolverProc::Resolve(const std::string& host,
+ 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);
+ // Hmm, this seems unecessary.
+ r->latency_ms = 1;
+ }
+ const std::string& effective_host = r->direct ? host : r->replacement;
+ if (effective_host.empty())
+ return ERR_NAME_NOT_RESOLVED;
+ return SystemHostResolverProc(effective_host, addrlist);
+ }
}
+ return ResolveUsingPrevious(host, addrlist);
+}
- private:
- scoped_refptr<HostMapper> current_host_mapper_;
- scoped_refptr<HostMapper> previous_host_mapper_;
-};
+//-----------------------------------------------------------------------------
-} // namespace net
+ScopedDefaultHostResolverProc::ScopedDefaultHostResolverProc(
+ HostResolverProc* proc) : current_proc_(proc) {
+ previous_proc_ = HostResolverProc::SetDefault(current_proc_);
+ current_proc_->set_previous_proc(previous_proc_);
+}
-#endif // UNIT_TEST
+ScopedDefaultHostResolverProc::~ScopedDefaultHostResolverProc() {
+ HostResolverProc* old_proc = HostResolverProc::SetDefault(previous_proc_);
+ // The lifetimes of multiple instances must be nested.
+ CHECK(old_proc == current_proc_);
+}
-#endif // NET_BASE_HOST_RESOLVER_UNITTEST_H_
+void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) {
+ current_proc_ = proc;
+ previous_proc_ = HostResolverProc::SetDefault(current_proc_);
+ current_proc_->set_previous_proc(previous_proc_);
+}
+
+} // namespace net
« no previous file with comments | « net/base/mock_host_resolver.h ('k') | net/base/run_all_unittests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698