Index: net/socket/tcp_socket_unittest.cc |
diff --git a/net/socket/tcp_socket_unittest.cc b/net/socket/tcp_socket_unittest.cc |
index e20bdd875846b9f0bf6c189abdc6bfd19c249554..cd754ef79d13c145d21daef3fc927f3c28c5e054 100644 |
--- a/net/socket/tcp_socket_unittest.cc |
+++ b/net/socket/tcp_socket_unittest.cc |
@@ -4,10 +4,16 @@ |
#include "net/socket/tcp_socket.h" |
+#include <cstring> |
wtc
2013/09/06 21:55:42
Nit: can you <string.h>?
yzshen1
2013/09/09 23:14:48
Done.
|
+ |
#include <string> |
+#include <vector> |
+#include "base/memory/ref_counted.h" |
#include "base/memory/scoped_ptr.h" |
+#include "build/build_config.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/test_completion_callback.h" |
@@ -194,5 +200,71 @@ TEST_F(TCPSocketTest, AcceptIPv6) { |
EXPECT_EQ(OK, connect_callback.WaitForResult()); |
} |
+// TODO(yzshen): Enable it for other platforms once TCPSocketLibevent supports |
+// client socket operations. |
+#if defined(OS_WIN) |
+ |
+TEST_F(TCPSocketTest, ReadWrite) { |
+ ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
+ |
+ TestCompletionCallback connect_callback; |
+ TCPSocket connecting_socket(NULL, NetLog::Source()); |
+ int result = connecting_socket.Create(ADDRESS_FAMILY_IPV4); |
+ ASSERT_EQ(OK, result); |
+ connecting_socket.Connect(local_address_, connect_callback.callback()); |
+ |
+ TestCompletionCallback accept_callback; |
+ scoped_ptr<TCPSocket> accepted_socket; |
+ IPEndPoint accepted_address; |
+ result = socket_.Accept(&accepted_socket, &accepted_address, |
+ accept_callback.callback()); |
+ ASSERT_EQ(OK, accept_callback.GetResult(result)); |
+ |
+ ASSERT_TRUE(accepted_socket.get()); |
+ |
+ // Both sockets should be on the loopback network interface. |
+ EXPECT_EQ(accepted_address.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( |
wtc
2013/09/06 21:55:42
Remove all net:: because this file is in the net n
yzshen1
2013/09/09 23:14:48
Done.
|
+ new net::IOBufferWithSize(message.size() - bytes_written)); |
+ memmove(write_buffer->data(), message.data() + bytes_written, |
+ message.size() - bytes_written); |
+ |
+ TestCompletionCallback write_callback; |
+ int write_result = accepted_socket->Write( |
+ write_buffer.get(), write_buffer->size(), write_callback.callback()); |
+ write_result = write_callback.GetResult(write_result); |
+ ASSERT_TRUE(write_result >= 0); |
+ bytes_written += write_result; |
+ ASSERT_TRUE(bytes_written <= message.size()); |
+ } |
+ |
+ 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.get(), 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); |
+} |
+ |
+#endif |
+ |
} // namespace |
} // namespace net |