summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
Diffstat (limited to 'net')
-rw-r--r--net/base/listen_socket.h7
-rw-r--r--net/base/tcp_client_socket_unittest.cc62
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);