diff options
author | dkegel@google.com <dkegel@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-13 23:51:16 +0000 |
---|---|---|
committer | dkegel@google.com <dkegel@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-13 23:51:16 +0000 |
commit | 10dd1a30fbaee0315e78d1752cbf1fa8e22c99e4 (patch) | |
tree | 517dc914e5a3f6de3b38420850815928da43edb1 /net | |
parent | a71d61f08f078f9d60cf96d668ab07dec7ce611b (diff) | |
download | chromium_src-10dd1a30fbaee0315e78d1752cbf1fa8e22c99e4.zip chromium_src-10dd1a30fbaee0315e78d1752cbf1fa8e22c99e4.tar.gz chromium_src-10dd1a30fbaee0315e78d1752cbf1fa8e22c99e4.tar.bz2 |
Rewrite old unit tests to avoid connecting to google.com; fixes TODO(darin)
fix typo in listen_socket.h include guard
I would have liked to use scoped_refptr for
listen_sock_ and connected_sock_, but that
crashes on the mac (possibly because
the sockets need to be closed before
TCPClientSocketTest is destroyed;
that will be easier once ListenSocket is
refactored, but that's beyond the scope of
this change).
Review URL: http://codereview.chromium.org/18001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7984 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/listen_socket.h | 7 | ||||
-rw-r--r-- | net/base/tcp_client_socket_unittest.cc | 62 |
2 files changed, 55 insertions, 14 deletions
diff --git a/net/base/listen_socket.h b/net/base/listen_socket.h index e405940..7921b8f 100644 --- a/net/base/listen_socket.h +++ b/net/base/listen_socket.h @@ -8,8 +8,8 @@ // happen in that loop's thread always and that all other methods (including // constructors and destructors) should also be called from the same thread. -#ifndef NET_BASE_SOCKET_H_ -#define NET_BASE_SOCKET_H_ +#ifndef NET_BASE_LISTEN_SOCKET_H_ +#define NET_BASE_LISTEN_SOCKET_H_ #if defined(OS_WIN) #include <winsock2.h> @@ -109,4 +109,5 @@ class ListenSocket : public base::RefCountedThreadSafe<ListenSocket>, DISALLOW_EVIL_CONSTRUCTORS(ListenSocket); }; -#endif // NET_BASE_SOCKET_H_ +#endif // NET_BASE_LISTEN_SOCKET_H_ + diff --git a/net/base/tcp_client_socket_unittest.cc b/net/base/tcp_client_socket_unittest.cc index 18e6c21..976ff1e 100644 --- a/net/base/tcp_client_socket_unittest.cc +++ b/net/base/tcp_client_socket_unittest.cc @@ -1,34 +1,74 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2006-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. #include "net/base/address_list.h" #include "net/base/host_resolver.h" +#include "net/base/listen_socket.h" #include "net/base/net_errors.h" -#include "net/base/scoped_host_mapper.h" #include "net/base/tcp_client_socket.h" #include "net/base/test_completion_callback.h" +#include "net/base/winsock_init.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" -class TCPClientSocketTest : public PlatformTest { +class TCPClientSocketTest + : public PlatformTest, public ListenSocket::ListenSocketDelegate { public: TCPClientSocketTest() { - // TODO(darin): kill this exception once we have a way to test out the - // TCPClientSocket class using loopback connections. - host_mapper_.AddRule("www.google.com", "www.google.com"); } + + // Implement ListenSocketDelegate methods + virtual void DidAccept(ListenSocket* server, ListenSocket* connection) { + connected_sock_ = connection; + } + virtual void DidRead(ListenSocket*, const std::string& s) { + // TODO(dkegel): this might not be long enough to tickle some bugs. + connected_sock_->Send(std::string("HTTP/1.1 404 Not Found"), true); + // Close socket by destroying it, else read test below will hang. + connected_sock_ = NULL; + } + virtual void DidClose(ListenSocket* sock) {} + + // Testcase hooks + virtual void SetUp(); + + protected: + int listen_port_; + private: - net::ScopedHostMapper host_mapper_; + scoped_refptr<ListenSocket> listen_sock_; + scoped_refptr<ListenSocket> connected_sock_; }; +void TCPClientSocketTest::SetUp() { + PlatformTest::SetUp(); + + // Find a free port to listen on + ListenSocket *sock = NULL; + int port; + // Range of ports to listen on. Shouldn't need to try many. + static const int kMinPort = 10100; + static const int kMaxPort = 10200; +#if defined(OS_WIN) + net::EnsureWinsockInit(); +#endif + for (port = kMinPort; port < kMaxPort; port++) { + sock = ListenSocket::Listen("127.0.0.1", port, this); + if (sock) + break; + } + ASSERT_TRUE(sock != NULL); + listen_sock_ = sock; + listen_port_ = port; +} TEST_F(TCPClientSocketTest, Connect) { net::AddressList addr; net::HostResolver resolver; TestCompletionCallback callback; - int rv = resolver.Resolve("www.google.com", 80, &addr, NULL); + int rv = resolver.Resolve("localhost", listen_port_, &addr, NULL); EXPECT_EQ(rv, net::OK); net::TCPClientSocket sock(addr); @@ -54,7 +94,7 @@ TEST_F(TCPClientSocketTest, Read) { net::HostResolver resolver; TestCompletionCallback callback; - int rv = resolver.Resolve("www.google.com", 80, &addr, &callback); + int rv = resolver.Resolve("localhost", listen_port_, &addr, &callback); EXPECT_EQ(rv, net::ERR_IO_PENDING); rv = callback.WaitForResult(); @@ -98,7 +138,7 @@ TEST_F(TCPClientSocketTest, Read_SmallChunks) { net::HostResolver resolver; TestCompletionCallback callback; - int rv = resolver.Resolve("www.google.com", 80, &addr, NULL); + int rv = resolver.Resolve("localhost", listen_port_, &addr, NULL); EXPECT_EQ(rv, net::OK); net::TCPClientSocket sock(addr); @@ -139,7 +179,7 @@ TEST_F(TCPClientSocketTest, Read_Interrupted) { net::HostResolver resolver; TestCompletionCallback callback; - int rv = resolver.Resolve("www.google.com", 80, &addr, NULL); + int rv = resolver.Resolve("localhost", listen_port_, &addr, NULL); EXPECT_EQ(rv, net::OK); net::TCPClientSocket sock(addr); |