summaryrefslogtreecommitdiffstats
path: root/net/socket/tcp_client_socket_unittest.cc
diff options
context:
space:
mode:
authormbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-12 15:24:04 +0000
committermbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-12 15:24:04 +0000
commita01076ab38b57005e040da62d6dd37074c38a8cf (patch)
tree062b8e5ee5c5ab3d0026e7a734df5cb81c848669 /net/socket/tcp_client_socket_unittest.cc
parent1c62e33701298c0964af870476100d03558b5e92 (diff)
downloadchromium_src-a01076ab38b57005e040da62d6dd37074c38a8cf.zip
chromium_src-a01076ab38b57005e040da62d6dd37074c38a8cf.tar.gz
chromium_src-a01076ab38b57005e040da62d6dd37074c38a8cf.tar.bz2
Rework the tcp_client_socket_unittest to be a generic
transport_client_socket_test. This allows us to create new tests which test against all transports. For now, that is just TCP. Soon we'll add SCTP. BUG=none TEST=none Review URL: http://codereview.chromium.org/6822022 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@81251 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/socket/tcp_client_socket_unittest.cc')
-rw-r--r--net/socket/tcp_client_socket_unittest.cc355
1 files changed, 0 insertions, 355 deletions
diff --git a/net/socket/tcp_client_socket_unittest.cc b/net/socket/tcp_client_socket_unittest.cc
deleted file mode 100644
index 2ed9441..0000000
--- a/net/socket/tcp_client_socket_unittest.cc
+++ /dev/null
@@ -1,355 +0,0 @@
-// Copyright (c) 2010 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/socket/tcp_client_socket.h"
-
-#include "base/basictypes.h"
-#include "net/base/address_list.h"
-#include "net/base/host_resolver.h"
-#include "net/base/io_buffer.h"
-#include "net/base/listen_socket.h"
-#include "net/base/net_log.h"
-#include "net/base/net_log_unittest.h"
-#include "net/base/net_errors.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"
-
-namespace net {
-
-namespace {
-
-const char kServerReply[] = "HTTP/1.1 404 Not Found";
-
-class TCPClientSocketTest
- : public PlatformTest, public ListenSocket::ListenSocketDelegate {
- public:
- TCPClientSocketTest()
- : listen_port_(0),
- net_log_(CapturingNetLog::kUnbounded) {
- }
-
- // Implement ListenSocketDelegate methods
- virtual void DidAccept(ListenSocket* server, ListenSocket* connection) {
- connected_sock_ = connection;
- }
- virtual void DidRead(ListenSocket*, const char* str, int len) {
- // TODO(dkegel): this might not be long enough to tickle some bugs.
- connected_sock_->Send(kServerReply,
- arraysize(kServerReply) - 1,
- false /* don't append line feed */);
- }
- virtual void DidClose(ListenSocket* sock) {}
-
- // Testcase hooks
- virtual void SetUp();
-
- void CloseServerSocket() {
- // delete the connected_sock_, which will close it.
- connected_sock_ = NULL;
- }
-
- void PauseServerReads() {
- connected_sock_->PauseReads();
- }
-
- void ResumeServerReads() {
- connected_sock_->ResumeReads();
- }
-
- protected:
- int listen_port_;
- CapturingNetLog net_log_;
- scoped_ptr<TCPClientSocket> sock_;
-
- private:
- 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.
- const int kMinPort = 10100;
- const int kMaxPort = 10200;
-#if defined(OS_WIN)
- 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;
-
- AddressList addr;
- scoped_ptr<HostResolver> resolver(
- CreateSystemHostResolver(HostResolver::kDefaultParallelism,
- NULL, NULL));
- HostResolver::RequestInfo info(HostPortPair("localhost", listen_port_));
- int rv = resolver->Resolve(info, &addr, NULL, NULL, BoundNetLog());
- CHECK_EQ(rv, OK);
- sock_.reset(new TCPClientSocket(addr, &net_log_, NetLog::Source()));
-}
-
-TEST_F(TCPClientSocketTest, Connect) {
- TestCompletionCallback callback;
- EXPECT_FALSE(sock_->IsConnected());
-
- int rv = sock_->Connect(&callback);
-
- net::CapturingNetLog::EntryList net_log_entries;
- net_log_.GetEntries(&net_log_entries);
- EXPECT_TRUE(net::LogContainsBeginEvent(
- net_log_entries, 0, net::NetLog::TYPE_SOCKET_ALIVE));
- EXPECT_TRUE(net::LogContainsBeginEvent(
- net_log_entries, 1, net::NetLog::TYPE_TCP_CONNECT));
- if (rv != OK) {
- ASSERT_EQ(rv, ERR_IO_PENDING);
- rv = callback.WaitForResult();
- EXPECT_EQ(rv, OK);
- }
-
- EXPECT_TRUE(sock_->IsConnected());
- net_log_.GetEntries(&net_log_entries);
- EXPECT_TRUE(net::LogContainsEndEvent(
- net_log_entries, -1, net::NetLog::TYPE_TCP_CONNECT));
-
- sock_->Disconnect();
- EXPECT_FALSE(sock_->IsConnected());
-}
-
-// TODO(wtc): Add unit tests for IsConnectedAndIdle:
-// - Server closes a connection.
-// - Server sends data unexpectedly.
-
-TEST_F(TCPClientSocketTest, Read) {
- TestCompletionCallback callback;
- int rv = sock_->Connect(&callback);
- if (rv != OK) {
- ASSERT_EQ(rv, ERR_IO_PENDING);
-
- rv = callback.WaitForResult();
- EXPECT_EQ(rv, OK);
- }
-
- const char request_text[] = "GET / HTTP/1.0\r\n\r\n";
- scoped_refptr<IOBuffer> request_buffer(
- new IOBuffer(arraysize(request_text) - 1));
- memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1);
-
- rv = sock_->Write(request_buffer, arraysize(request_text) - 1, &callback);
- EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING);
-
- if (rv == ERR_IO_PENDING) {
- rv = callback.WaitForResult();
- EXPECT_EQ(rv, static_cast<int>(arraysize(request_text) - 1));
- }
-
- scoped_refptr<IOBuffer> buf(new IOBuffer(4096));
- uint32 bytes_read = 0;
- while (bytes_read < arraysize(kServerReply) - 1) {
- rv = sock_->Read(buf, 4096, &callback);
- EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING);
-
- if (rv == ERR_IO_PENDING)
- rv = callback.WaitForResult();
-
- ASSERT_GE(rv, 0);
- bytes_read += rv;
- }
-
- // All data has been read now. Read once more to force an ERR_IO_PENDING, and
- // then close the server socket, and note the close.
-
- rv = sock_->Read(buf, 4096, &callback);
- ASSERT_EQ(ERR_IO_PENDING, rv);
- CloseServerSocket();
- EXPECT_EQ(0, callback.WaitForResult());
-}
-
-TEST_F(TCPClientSocketTest, Read_SmallChunks) {
- TestCompletionCallback callback;
- int rv = sock_->Connect(&callback);
- if (rv != OK) {
- ASSERT_EQ(rv, ERR_IO_PENDING);
-
- rv = callback.WaitForResult();
- EXPECT_EQ(rv, OK);
- }
-
- const char request_text[] = "GET / HTTP/1.0\r\n\r\n";
- scoped_refptr<IOBuffer> request_buffer(
- new IOBuffer(arraysize(request_text) - 1));
- memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1);
-
- rv = sock_->Write(request_buffer, arraysize(request_text) - 1, &callback);
- EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING);
-
- if (rv == ERR_IO_PENDING) {
- rv = callback.WaitForResult();
- EXPECT_EQ(rv, static_cast<int>(arraysize(request_text) - 1));
- }
-
- scoped_refptr<IOBuffer> buf(new IOBuffer(1));
- uint32 bytes_read = 0;
- while (bytes_read < arraysize(kServerReply) - 1) {
- rv = sock_->Read(buf, 1, &callback);
- EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING);
-
- if (rv == ERR_IO_PENDING)
- rv = callback.WaitForResult();
-
- ASSERT_EQ(1, rv);
- bytes_read += rv;
- }
-
- // All data has been read now. Read once more to force an ERR_IO_PENDING, and
- // then close the server socket, and note the close.
-
- rv = sock_->Read(buf, 1, &callback);
- ASSERT_EQ(ERR_IO_PENDING, rv);
- CloseServerSocket();
- EXPECT_EQ(0, callback.WaitForResult());
-}
-
-TEST_F(TCPClientSocketTest, Read_Interrupted) {
- TestCompletionCallback callback;
- int rv = sock_->Connect(&callback);
- if (rv != OK) {
- ASSERT_EQ(ERR_IO_PENDING, rv);
-
- rv = callback.WaitForResult();
- EXPECT_EQ(rv, OK);
- }
-
- const char request_text[] = "GET / HTTP/1.0\r\n\r\n";
- scoped_refptr<IOBuffer> request_buffer(
- new IOBuffer(arraysize(request_text) - 1));
- memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1);
-
- rv = sock_->Write(request_buffer, arraysize(request_text) - 1, &callback);
- EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING);
-
- if (rv == ERR_IO_PENDING) {
- rv = callback.WaitForResult();
- EXPECT_EQ(rv, static_cast<int>(arraysize(request_text) - 1));
- }
-
- // Do a partial read and then exit. This test should not crash!
- scoped_refptr<IOBuffer> buf(new IOBuffer(16));
- rv = sock_->Read(buf, 16, &callback);
- EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING);
-
- if (rv == ERR_IO_PENDING)
- rv = callback.WaitForResult();
-
- EXPECT_NE(0, rv);
-}
-
-TEST_F(TCPClientSocketTest, DISABLED_FullDuplex_ReadFirst) {
- TestCompletionCallback callback;
- int rv = sock_->Connect(&callback);
- if (rv != OK) {
- ASSERT_EQ(rv, ERR_IO_PENDING);
-
- rv = callback.WaitForResult();
- EXPECT_EQ(rv, OK);
- }
-
- // Read first. There's no data, so it should return ERR_IO_PENDING.
- const int kBufLen = 4096;
- scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen));
- rv = sock_->Read(buf, kBufLen, &callback);
- EXPECT_EQ(ERR_IO_PENDING, rv);
-
- PauseServerReads();
- const int kWriteBufLen = 64 * 1024;
- scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen));
- char* request_data = request_buffer->data();
- memset(request_data, 'A', kWriteBufLen);
- TestCompletionCallback write_callback;
-
- while (true) {
- rv = sock_->Write(request_buffer, kWriteBufLen, &write_callback);
- ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING);
-
- if (rv == ERR_IO_PENDING) {
- ResumeServerReads();
- rv = write_callback.WaitForResult();
- break;
- }
- }
-
- // At this point, both read and write have returned ERR_IO_PENDING, and the
- // write callback has executed. We wait for the read callback to run now to
- // make sure that the socket can handle full duplex communications.
-
- rv = callback.WaitForResult();
- EXPECT_GE(rv, 0);
-}
-
-TEST_F(TCPClientSocketTest, DISABLED_FullDuplex_WriteFirst) {
- TestCompletionCallback callback;
- int rv = sock_->Connect(&callback);
- if (rv != OK) {
- ASSERT_EQ(ERR_IO_PENDING, rv);
-
- rv = callback.WaitForResult();
- EXPECT_EQ(OK, rv);
- }
-
- PauseServerReads();
- const int kWriteBufLen = 64 * 1024;
- scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen));
- char* request_data = request_buffer->data();
- memset(request_data, 'A', kWriteBufLen);
- TestCompletionCallback write_callback;
-
- while (true) {
- rv = sock_->Write(request_buffer, kWriteBufLen, &write_callback);
- ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING);
-
- if (rv == ERR_IO_PENDING)
- break;
- }
-
- // Now we have the Write() blocked on ERR_IO_PENDING. It's time to force the
- // Read() to block on ERR_IO_PENDING too.
-
- const int kBufLen = 4096;
- scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen));
- while (true) {
- rv = sock_->Read(buf, kBufLen, &callback);
- ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING);
- if (rv == ERR_IO_PENDING)
- break;
- }
-
- // At this point, both read and write have returned ERR_IO_PENDING. Now we
- // run the write and read callbacks to make sure they can handle full duplex
- // communications.
-
- ResumeServerReads();
- rv = write_callback.WaitForResult();
- EXPECT_GE(rv, 0);
-
- // It's possible the read is blocked because it's already read all the data.
- // Close the server socket, so there will at least be a 0-byte read.
- CloseServerSocket();
-
- rv = callback.WaitForResult();
- EXPECT_GE(rv, 0);
-}
-
-} // namespace
-
-} // namespace net