diff options
author | ygorshenin@chromium.org <ygorshenin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-22 08:04:18 +0000 |
---|---|---|
committer | ygorshenin@chromium.org <ygorshenin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-22 08:04:18 +0000 |
commit | 7370de6e193be7266476c8de1600449f1f1e58c0 (patch) | |
tree | 6351b5fcf7f00e5784694817955b2cf4025615fb | |
parent | e501f31e7829da99f69724baaea8f4c3bf57adb6 (diff) | |
download | chromium_src-7370de6e193be7266476c8de1600449f1f1e58c0.zip chromium_src-7370de6e193be7266476c8de1600449f1f1e58c0.tar.gz chromium_src-7370de6e193be7266476c8de1600449f1f1e58c0.tar.bz2 |
While calling IO methods on accepted TCP sockets we run into problem that AssertEventNotSignaled is triggered in TCPClientSocketWin::{Read|Write} methods. This CL fixes this issue and adds test.
BUG=99602
TEST=TCPServerSocketTest
Review URL: https://chromiumcodereview.appspot.com/9413007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@122998 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | net/socket/tcp_client_socket_win.cc | 13 | ||||
-rw-r--r-- | net/socket/tcp_server_socket_unittest.cc | 64 |
2 files changed, 70 insertions, 7 deletions
diff --git a/net/socket/tcp_client_socket_win.cc b/net/socket/tcp_client_socket_win.cc index b5921b9..196522d 100644 --- a/net/socket/tcp_client_socket_win.cc +++ b/net/socket/tcp_client_socket_win.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -256,6 +256,9 @@ TCPClientSocketWin::Core::Core( slow_start_throttle_(kInitialSlowStartThrottle) { memset(&read_overlapped_, 0, sizeof(read_overlapped_)); memset(&write_overlapped_, 0, sizeof(write_overlapped_)); + + read_overlapped_.hEvent = WSACreateEvent(); + write_overlapped_.hEvent = WSACreateEvent(); } TCPClientSocketWin::Core::~Core() { @@ -342,7 +345,10 @@ int TCPClientSocketWin::AdoptSocket(SOCKET socket) { return MapSystemError(error); socket_ = socket; + SetNonBlocking(socket_); + core_ = new Core(this); + current_ai_ = addresses_.head(); use_history_.set_was_ever_connected(); @@ -474,15 +480,10 @@ int TCPClientSocketWin::DoConnect() { DCHECK(!core_); core_ = new Core(this); - - // WSACreateEvent creates a manual-reset event object. - core_->read_overlapped_.hEvent = WSACreateEvent(); // WSAEventSelect sets the socket to non-blocking mode as a side effect. // Our connect() and recv() calls require that the socket be non-blocking. WSAEventSelect(socket_, core_->read_overlapped_.hEvent, FD_CONNECT); - core_->write_overlapped_.hEvent = WSACreateEvent(); - connect_start_time_ = base::TimeTicks::Now(); if (!connect(socket_, ai->ai_addr, static_cast<int>(ai->ai_addrlen))) { // Connected without waiting! diff --git a/net/socket/tcp_server_socket_unittest.cc b/net/socket/tcp_server_socket_unittest.cc index 2ccc70e..9307297 100644 --- a/net/socket/tcp_server_socket_unittest.cc +++ b/net/socket/tcp_server_socket_unittest.cc @@ -1,14 +1,17 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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_server_socket.h" #include <string> +#include <vector> #include "base/compiler_specific.h" +#include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "net/base/address_list.h" +#include "net/base/io_buffer.h" #include "net/base/ip_endpoint.h" #include "net/base/net_errors.h" #include "net/base/sys_addrinfo.h" @@ -191,6 +194,65 @@ TEST_F(TCPServerSocketTest, AcceptIPv6) { EXPECT_EQ(OK, connect_callback.WaitForResult()); } +TEST_F(TCPServerSocketTest, AcceptIO) { + ASSERT_NO_FATAL_FAILURE(SetUpIPv4()); + + TestCompletionCallback connect_callback; + TCPClientSocket connecting_socket(local_address_list(), + NULL, NetLog::Source()); + connecting_socket.Connect(connect_callback.callback()); + + TestCompletionCallback accept_callback; + scoped_ptr<StreamSocket> accepted_socket; + int result = socket_.Accept(&accepted_socket, accept_callback.callback()); + ASSERT_EQ(OK, accept_callback.GetResult(result)); + + ASSERT_TRUE(accepted_socket.get() != NULL); + + // Both sockets should be on the loopback network interface. + EXPECT_EQ(GetPeerAddress(accepted_socket.get()).address(), + local_address_.address()); + + EXPECT_EQ(OK, connect_callback.WaitForResult()); + + const std::string message("test message"); + std::vector<char> buffer(message.size()); + + size_t bytes_written = 0; + while (bytes_written < message.size()) { + scoped_refptr<net::IOBufferWithSize> write_buffer( + new net::IOBufferWithSize(message.size() - bytes_written)); + memmove(write_buffer->data(), message.data(), message.size()); + + TestCompletionCallback write_callback; + int write_result = accepted_socket->Write(write_buffer, + write_buffer->size(), + write_callback.callback()); + write_result = write_callback.GetResult(write_result); + ASSERT_TRUE(write_result >= 0); + ASSERT_TRUE(bytes_written + write_result <= message.size()); + bytes_written += write_result; + } + + size_t bytes_read = 0; + while (bytes_read < message.size()) { + scoped_refptr<net::IOBufferWithSize> read_buffer( + new net::IOBufferWithSize(message.size() - bytes_read)); + TestCompletionCallback read_callback; + int read_result = connecting_socket.Read(read_buffer, + read_buffer->size(), + read_callback.callback()); + read_result = read_callback.GetResult(read_result); + ASSERT_TRUE(read_result >= 0); + ASSERT_TRUE(bytes_read + read_result <= message.size()); + memmove(&buffer[bytes_read], read_buffer->data(), read_result); + bytes_read += read_result; + } + + std::string received_message(buffer.begin(), buffer.end()); + ASSERT_EQ(message, received_message); +} + } // namespace } // namespace net |