summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoreroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-06 18:25:19 +0000
committereroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-06 18:25:19 +0000
commitb11279e2abfc427e30f0ccaa8e265f5ebbb63b3c (patch)
tree88a3814a2a1c9e8ad675f6c27875bc04f8942038
parent182d4f35a1b22d141ea12a7ba286440e5797bd36 (diff)
downloadchromium_src-b11279e2abfc427e30f0ccaa8e265f5ebbb63b3c.zip
chromium_src-b11279e2abfc427e30f0ccaa8e265f5ebbb63b3c.tar.gz
chromium_src-b11279e2abfc427e30f0ccaa8e265f5ebbb63b3c.tar.bz2
Add unit-tests for SingleRequestHostResolver.
To facilitate this, moved it out of host_resolver.cc to its own set of files. BUG=84261 Review URL: http://codereview.chromium.org/6993015 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@88006 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/net/predictor.cc1
-rw-r--r--content/browser/renderer_host/pepper_message_filter.cc1
-rw-r--r--jingle/notifier/communicator/xmpp_connection_generator.h1
-rw-r--r--net/base/host_resolver.cc61
-rw-r--r--net/base/host_resolver.h42
-rw-r--r--net/base/single_request_host_resolver.cc72
-rw-r--r--net/base/single_request_host_resolver.h57
-rw-r--r--net/base/single_request_host_resolver_unittest.cc122
-rw-r--r--net/curvecp/test_client.cc1
-rw-r--r--net/ftp/ftp_network_transaction.h1
-rw-r--r--net/http/http_auth_handler_negotiate.cc1
-rw-r--r--net/net.gyp3
-rw-r--r--net/socket/socks_client_socket.h1
-rw-r--r--net/socket/transport_client_socket_pool.h1
14 files changed, 262 insertions, 103 deletions
diff --git a/chrome/browser/net/predictor.cc b/chrome/browser/net/predictor.cc
index 013a268..bf3e0ba 100644
--- a/chrome/browser/net/predictor.cc
+++ b/chrome/browser/net/predictor.cc
@@ -22,6 +22,7 @@
#include "net/base/host_resolver.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
+#include "net/base/single_request_host_resolver.h"
using base::TimeDelta;
diff --git a/content/browser/renderer_host/pepper_message_filter.cc b/content/browser/renderer_host/pepper_message_filter.cc
index 9381ed6..46d0095 100644
--- a/content/browser/renderer_host/pepper_message_filter.cc
+++ b/content/browser/renderer_host/pepper_message_filter.cc
@@ -18,6 +18,7 @@
#include "net/base/net_errors.h"
#include "net/base/host_port_pair.h"
#include "net/base/host_resolver.h"
+#include "net/base/single_request_host_resolver.h"
#include "net/url_request/url_request_context.h"
#include "ppapi/c/private/ppb_flash_net_connector.h"
#include "ppapi/proxy/ppapi_messages.h"
diff --git a/jingle/notifier/communicator/xmpp_connection_generator.h b/jingle/notifier/communicator/xmpp_connection_generator.h
index 38e964a..e37e1f8 100644
--- a/jingle/notifier/communicator/xmpp_connection_generator.h
+++ b/jingle/notifier/communicator/xmpp_connection_generator.h
@@ -12,6 +12,7 @@
#include "net/base/completion_callback.h"
#include "net/base/host_resolver.h"
#include "net/base/net_log.h"
+#include "net/base/single_request_host_resolver.h"
#include "jingle/notifier/base/server_information.h"
namespace talk_base {
diff --git a/net/base/host_resolver.cc b/net/base/host_resolver.cc
index cd17c47..bf1c11a 100644
--- a/net/base/host_resolver.cc
+++ b/net/base/host_resolver.cc
@@ -4,10 +4,6 @@
#include "net/base/host_resolver.h"
-#include "base/compiler_specific.h"
-#include "base/logging.h"
-#include "net/base/net_errors.h"
-
namespace net {
HostResolver::RequestInfo::RequestInfo(const HostPortPair& host_port_pair)
@@ -34,61 +30,4 @@ HostResolverImpl* HostResolver::GetAsHostResolverImpl() {
HostResolver::HostResolver() {
}
-SingleRequestHostResolver::SingleRequestHostResolver(HostResolver* resolver)
- : resolver_(resolver),
- cur_request_(NULL),
- cur_request_callback_(NULL),
- ALLOW_THIS_IN_INITIALIZER_LIST(
- callback_(this, &SingleRequestHostResolver::OnResolveCompletion)) {
- DCHECK(resolver_ != NULL);
-}
-
-SingleRequestHostResolver::~SingleRequestHostResolver() {
- Cancel();
-}
-
-int SingleRequestHostResolver::Resolve(const HostResolver::RequestInfo& info,
- AddressList* addresses,
- CompletionCallback* callback,
- const BoundNetLog& net_log) {
- DCHECK(!cur_request_ && !cur_request_callback_) << "resolver already in use";
-
- HostResolver::RequestHandle request = NULL;
-
- // We need to be notified of completion before |callback| is called, so that
- // we can clear out |cur_request_*|.
- CompletionCallback* transient_callback = callback ? &callback_ : NULL;
-
- int rv = resolver_->Resolve(
- info, addresses, transient_callback, &request, net_log);
-
- if (rv == ERR_IO_PENDING) {
- // Cleared in OnResolveCompletion().
- cur_request_ = request;
- cur_request_callback_ = callback;
- }
-
- return rv;
-}
-
-void SingleRequestHostResolver::Cancel() {
- if (cur_request_) {
- resolver_->CancelRequest(cur_request_);
- cur_request_ = NULL;
- }
-}
-
-void SingleRequestHostResolver::OnResolveCompletion(int result) {
- DCHECK(cur_request_ && cur_request_callback_);
-
- CompletionCallback* callback = cur_request_callback_;
-
- // Clear the outstanding request information.
- cur_request_ = NULL;
- cur_request_callback_ = NULL;
-
- // Call the user's original callback.
- callback->Run(result);
-}
-
} // namespace net
diff --git a/net/base/host_resolver.h b/net/base/host_resolver.h
index 9850617..fd5cf6d 100644
--- a/net/base/host_resolver.h
+++ b/net/base/host_resolver.h
@@ -199,48 +199,6 @@ class NET_API HostResolver {
DISALLOW_COPY_AND_ASSIGN(HostResolver);
};
-// This class represents the task of resolving a hostname (or IP address
-// literal) to an AddressList object. It wraps HostResolver to resolve only a
-// single hostname at a time and cancels this request when going out of scope.
-class NET_API SingleRequestHostResolver {
- public:
- // |resolver| must remain valid for the lifetime of |this|.
- explicit SingleRequestHostResolver(HostResolver* resolver);
-
- // If a completion callback is pending when the resolver is destroyed, the
- // host resolution is cancelled, and the completion callback will not be
- // called.
- ~SingleRequestHostResolver();
-
- // Resolves the given hostname (or IP address literal), filling out the
- // |addresses| object upon success. See HostResolver::Resolve() for details.
- int Resolve(const HostResolver::RequestInfo& info,
- AddressList* addresses,
- CompletionCallback* callback,
- const BoundNetLog& net_log);
-
- // Cancels the in-progress request, if any. This prevents the callback
- // from being invoked. Resolve() can be called again after cancelling.
- void Cancel();
-
- private:
- // Callback for when the request to |resolver_| completes, so we dispatch
- // to the user's callback.
- void OnResolveCompletion(int result);
-
- // The actual host resolver that will handle the request.
- HostResolver* const resolver_;
-
- // The current request (if any).
- HostResolver::RequestHandle cur_request_;
- CompletionCallback* cur_request_callback_;
-
- // Completion callback for when request to |resolver_| completes.
- CompletionCallbackImpl<SingleRequestHostResolver> callback_;
-
- DISALLOW_COPY_AND_ASSIGN(SingleRequestHostResolver);
-};
-
// Creates a HostResolver implementation that queries the underlying system.
// (Except if a unit-test has changed the global HostResolverProc using
// ScopedHostResolverProc to intercept requests to the system).
diff --git a/net/base/single_request_host_resolver.cc b/net/base/single_request_host_resolver.cc
new file mode 100644
index 0000000..d3f7758
--- /dev/null
+++ b/net/base/single_request_host_resolver.cc
@@ -0,0 +1,72 @@
+// Copyright (c) 2011 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.
+
+#include "net/base/single_request_host_resolver.h"
+
+#include "base/compiler_specific.h"
+#include "base/logging.h"
+#include "net/base/net_errors.h"
+
+namespace net {
+
+SingleRequestHostResolver::SingleRequestHostResolver(HostResolver* resolver)
+ : resolver_(resolver),
+ cur_request_(NULL),
+ cur_request_callback_(NULL),
+ ALLOW_THIS_IN_INITIALIZER_LIST(
+ callback_(this, &SingleRequestHostResolver::OnResolveCompletion)) {
+ DCHECK(resolver_ != NULL);
+}
+
+SingleRequestHostResolver::~SingleRequestHostResolver() {
+ Cancel();
+}
+
+int SingleRequestHostResolver::Resolve(const HostResolver::RequestInfo& info,
+ AddressList* addresses,
+ CompletionCallback* callback,
+ const BoundNetLog& net_log) {
+ DCHECK(!cur_request_callback_) << "resolver already in use";
+
+ HostResolver::RequestHandle request = NULL;
+
+ // We need to be notified of completion before |callback| is called, so that
+ // we can clear out |cur_request_*|.
+ CompletionCallback* transient_callback = callback ? &callback_ : NULL;
+
+ int rv = resolver_->Resolve(
+ info, addresses, transient_callback, &request, net_log);
+
+ if (rv == ERR_IO_PENDING) {
+ DCHECK(callback);
+ // Cleared in OnResolveCompletion().
+ cur_request_ = request;
+ cur_request_callback_ = callback;
+ }
+
+ return rv;
+}
+
+void SingleRequestHostResolver::Cancel() {
+ if (cur_request_callback_) {
+ resolver_->CancelRequest(cur_request_);
+ cur_request_ = NULL;
+ cur_request_callback_ = NULL;
+ }
+}
+
+void SingleRequestHostResolver::OnResolveCompletion(int result) {
+ DCHECK(cur_request_ && cur_request_callback_);
+
+ CompletionCallback* callback = cur_request_callback_;
+
+ // Clear the outstanding request information.
+ cur_request_ = NULL;
+ cur_request_callback_ = NULL;
+
+ // Call the user's original callback.
+ callback->Run(result);
+}
+
+} // namespace net
diff --git a/net/base/single_request_host_resolver.h b/net/base/single_request_host_resolver.h
new file mode 100644
index 0000000..6b9ec9b
--- /dev/null
+++ b/net/base/single_request_host_resolver.h
@@ -0,0 +1,57 @@
+// Copyright (c) 2011 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_SINGLE_REQUEST_HOST_RESOLVER_H_
+#define NET_BASE_SINGLE_REQUEST_HOST_RESOLVER_H_
+#pragma once
+
+#include "net/base/host_resolver.h"
+
+namespace net {
+
+// This class represents the task of resolving a hostname (or IP address
+// literal) to an AddressList object. It wraps HostResolver to resolve only a
+// single hostname at a time and cancels this request when going out of scope.
+class NET_API SingleRequestHostResolver {
+ public:
+ // |resolver| must remain valid for the lifetime of |this|.
+ explicit SingleRequestHostResolver(HostResolver* resolver);
+
+ // If a completion callback is pending when the resolver is destroyed, the
+ // host resolution is cancelled, and the completion callback will not be
+ // called.
+ ~SingleRequestHostResolver();
+
+ // Resolves the given hostname (or IP address literal), filling out the
+ // |addresses| object upon success. See HostResolver::Resolve() for details.
+ int Resolve(const HostResolver::RequestInfo& info,
+ AddressList* addresses,
+ CompletionCallback* callback,
+ const BoundNetLog& net_log);
+
+ // Cancels the in-progress request, if any. This prevents the callback
+ // from being invoked. Resolve() can be called again after cancelling.
+ void Cancel();
+
+ private:
+ // Callback for when the request to |resolver_| completes, so we dispatch
+ // to the user's callback.
+ void OnResolveCompletion(int result);
+
+ // The actual host resolver that will handle the request.
+ HostResolver* const resolver_;
+
+ // The current request (if any).
+ HostResolver::RequestHandle cur_request_;
+ CompletionCallback* cur_request_callback_;
+
+ // Completion callback for when request to |resolver_| completes.
+ CompletionCallbackImpl<SingleRequestHostResolver> callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(SingleRequestHostResolver);
+};
+
+} // namespace net
+
+#endif // NET_BASE_SINGLE_REQUEST_HOST_RESOLVER_H_
diff --git a/net/base/single_request_host_resolver_unittest.cc b/net/base/single_request_host_resolver_unittest.cc
new file mode 100644
index 0000000..7a3a3a6
--- /dev/null
+++ b/net/base/single_request_host_resolver_unittest.cc
@@ -0,0 +1,122 @@
+// Copyright (c) 2011 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.
+
+#include "net/base/single_request_host_resolver.h"
+
+#include "net/base/mock_host_resolver.h"
+#include "net/base/net_errors.h"
+#include "net/base/test_completion_callback.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace net {
+
+namespace {
+
+// Helper class used by SingleRequestHostResolverTest.Cancel test.
+// It checks that only one request is outstanding at a time, and that
+// it is cancelled before the class is destroyed.
+class HangingHostResolver : public HostResolver {
+ public:
+ HangingHostResolver() : outstanding_request_(NULL) {}
+
+ virtual ~HangingHostResolver() {
+ EXPECT_TRUE(!has_outstanding_request());
+ }
+
+ bool has_outstanding_request() const {
+ return outstanding_request_ != NULL;
+ }
+
+ virtual int Resolve(const RequestInfo& info,
+ AddressList* addresses,
+ CompletionCallback* callback,
+ RequestHandle* out_req,
+ const BoundNetLog& net_log) {
+ EXPECT_FALSE(has_outstanding_request());
+ outstanding_request_ = reinterpret_cast<RequestHandle>(0x1234);
+ *out_req = outstanding_request_;
+
+ // Never complete this request! Caller is expected to cancel it
+ // before destroying the resolver.
+ return ERR_IO_PENDING;
+ }
+
+ virtual void CancelRequest(RequestHandle req) {
+ EXPECT_TRUE(has_outstanding_request());
+ EXPECT_EQ(req, outstanding_request_);
+ outstanding_request_ = NULL;
+ }
+
+ virtual void AddObserver(Observer* observer) {
+ FAIL();
+ }
+
+ virtual void RemoveObserver(Observer* observer) {
+ FAIL();
+ }
+
+ private:
+ RequestHandle outstanding_request_;
+
+ DISALLOW_COPY_AND_ASSIGN(HangingHostResolver);
+};
+
+// Test that a regular end-to-end lookup returns the expected result.
+TEST(SingleRequestHostResolverTest, NormalResolve) {
+ // Create a host resolver dependency that returns address "199.188.1.166"
+ // for resolutions of "watsup".
+ MockHostResolver resolver;
+ resolver.rules()->AddIPLiteralRule("watsup", "199.188.1.166", "");
+
+ SingleRequestHostResolver single_request_resolver(&resolver);
+
+ // Resolve "watsup:90" using our SingleRequestHostResolver.
+ AddressList addrlist;
+ TestCompletionCallback callback;
+ HostResolver::RequestInfo request(HostPortPair("watsup", 90));
+ int rv = single_request_resolver.Resolve(
+ request, &addrlist, &callback, BoundNetLog());
+ EXPECT_EQ(ERR_IO_PENDING, rv);
+ EXPECT_EQ(OK, callback.WaitForResult());
+
+ // Verify that the result is what we specified in the MockHostResolver.
+ EXPECT_EQ("199.188.1.166", NetAddressToString(addrlist.head()));
+}
+
+// Test that the Cancel() method cancels any outstanding request.
+TEST(SingleRequestHostResolverTest, Cancel) {
+ HangingHostResolver resolver;
+
+ {
+ SingleRequestHostResolver single_request_resolver(&resolver);
+
+ // Resolve "watsup:90" using our SingleRequestHostResolver.
+ AddressList addrlist;
+ TestCompletionCallback callback;
+ HostResolver::RequestInfo request(HostPortPair("watsup", 90));
+ int rv = single_request_resolver.Resolve(
+ request, &addrlist, &callback, BoundNetLog());
+ EXPECT_EQ(ERR_IO_PENDING, rv);
+ EXPECT_TRUE(resolver.has_outstanding_request());
+ }
+
+ // Now that the SingleRequestHostResolver has been destroyed, the
+ // in-progress request should have been aborted.
+ EXPECT_FALSE(resolver.has_outstanding_request());
+}
+
+// Test that the Cancel() method is a no-op when there is no outstanding
+// request.
+TEST(SingleRequestHostResolverTest, CancelWhileNoPendingRequest) {
+ HangingHostResolver resolver;
+ SingleRequestHostResolver single_request_resolver(&resolver);
+ single_request_resolver.Cancel();
+
+ // To pass, HangingHostResolver should not have received a cancellation
+ // request (since there is nothing to cancel). If it does, it will crash.
+}
+
+} // namespace
+
+} // namespace net
diff --git a/net/curvecp/test_client.cc b/net/curvecp/test_client.cc
index 6239ed5..9eced0d 100644
--- a/net/curvecp/test_client.cc
+++ b/net/curvecp/test_client.cc
@@ -15,6 +15,7 @@
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
+#include "net/base/single_request_host_resolver.h"
#include "net/curvecp/curvecp_client_socket.h"
namespace net {
diff --git a/net/ftp/ftp_network_transaction.h b/net/ftp/ftp_network_transaction.h
index 5589728..8500d27 100644
--- a/net/ftp/ftp_network_transaction.h
+++ b/net/ftp/ftp_network_transaction.h
@@ -15,6 +15,7 @@
#include "net/base/address_list.h"
#include "net/base/host_resolver.h"
#include "net/base/net_log.h"
+#include "net/base/single_request_host_resolver.h"
#include "net/ftp/ftp_ctrl_response_buffer.h"
#include "net/ftp/ftp_response_info.h"
#include "net/ftp/ftp_transaction.h"
diff --git a/net/http/http_auth_handler_negotiate.cc b/net/http/http_auth_handler_negotiate.cc
index 51a0e24..52a2b1e 100644
--- a/net/http/http_auth_handler_negotiate.cc
+++ b/net/http/http_auth_handler_negotiate.cc
@@ -11,6 +11,7 @@
#include "net/base/address_family.h"
#include "net/base/host_resolver.h"
#include "net/base/net_errors.h"
+#include "net/base/single_request_host_resolver.h"
#include "net/http/http_auth_filter.h"
#include "net/http/url_security_manager.h"
diff --git a/net/net.gyp b/net/net.gyp
index b73eead..4864049 100644
--- a/net/net.gyp
+++ b/net/net.gyp
@@ -181,6 +181,8 @@
'base/sdch_filter.h',
'base/sdch_manager.cc',
'base/sdch_manager.h',
+ 'base/single_request_host_resolver.cc',
+ 'base/single_request_host_resolver.h',
'base/ssl_cert_request_info.cc',
'base/ssl_cert_request_info.h',
'base/ssl_cipher_suite_names.cc',
@@ -871,6 +873,7 @@
'base/registry_controlled_domain_unittest.cc',
'base/run_all_unittests.cc',
'base/sdch_filter_unittest.cc',
+ 'base/single_request_host_resolver_unittest.cc',
'base/ssl_cipher_suite_names_unittest.cc',
'base/ssl_client_auth_cache_unittest.cc',
'base/ssl_config_service_unittest.cc',
diff --git a/net/socket/socks_client_socket.h b/net/socket/socks_client_socket.h
index c881f67..7c4ba35 100644
--- a/net/socket/socks_client_socket.h
+++ b/net/socket/socks_client_socket.h
@@ -18,6 +18,7 @@
#include "net/base/host_resolver.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
+#include "net/base/single_request_host_resolver.h"
#include "net/socket/stream_socket.h"
namespace net {
diff --git a/net/socket/transport_client_socket_pool.h b/net/socket/transport_client_socket_pool.h
index 33b60bc..dab1679 100644
--- a/net/socket/transport_client_socket_pool.h
+++ b/net/socket/transport_client_socket_pool.h
@@ -15,6 +15,7 @@
#include "base/timer.h"
#include "net/base/host_port_pair.h"
#include "net/base/host_resolver.h"
+#include "net/base/single_request_host_resolver.h"
#include "net/socket/client_socket_pool_base.h"
#include "net/socket/client_socket_pool_histograms.h"
#include "net/socket/client_socket_pool.h"