[email protected] | 7370de6e | 2012-02-22 08:04:18 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/socket/tcp_server_socket.h" |
| 6 | |
[email protected] | e57a716 | 2011-06-15 04:14:23 | [diff] [blame] | 7 | #include <string> |
[email protected] | 7370de6e | 2012-02-22 08:04:18 | [diff] [blame] | 8 | #include <vector> |
[email protected] | e57a716 | 2011-06-15 04:14:23 | [diff] [blame] | 9 | |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
[email protected] | 7370de6e | 2012-02-22 08:04:18 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
[email protected] | e57a716 | 2011-06-15 04:14:23 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 13 | #include "net/base/address_list.h" |
[email protected] | 7370de6e | 2012-02-22 08:04:18 | [diff] [blame] | 14 | #include "net/base/io_buffer.h" |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame^] | 15 | #include "net/base/ip_address.h" |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 16 | #include "net/base/ip_endpoint.h" |
| 17 | #include "net/base/net_errors.h" |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 18 | #include "net/base/test_completion_callback.h" |
| 19 | #include "net/socket/tcp_client_socket.h" |
| 20 | #include "testing/gtest/include/gtest/gtest.h" |
| 21 | #include "testing/platform_test.h" |
| 22 | |
| 23 | namespace net { |
| 24 | |
| 25 | namespace { |
| 26 | const int kListenBacklog = 5; |
| 27 | |
| 28 | class TCPServerSocketTest : public PlatformTest { |
| 29 | protected: |
| 30 | TCPServerSocketTest() |
| 31 | : socket_(NULL, NetLog::Source()) { |
| 32 | } |
| 33 | |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 34 | void SetUpIPv4() { |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame^] | 35 | IPEndPoint address(IPAddress::IPv4Localhost(), 0); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 36 | ASSERT_EQ(OK, socket_.Listen(address, kListenBacklog)); |
| 37 | ASSERT_EQ(OK, socket_.GetLocalAddress(&local_address_)); |
| 38 | } |
| 39 | |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 40 | void SetUpIPv6(bool* success) { |
| 41 | *success = false; |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame^] | 42 | IPEndPoint address(IPAddress::IPv6Localhost(), 0); |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 43 | if (socket_.Listen(address, kListenBacklog) != 0) { |
| 44 | LOG(ERROR) << "Failed to listen on ::1 - probably because IPv6 is " |
| 45 | "disabled. Skipping the test"; |
| 46 | return; |
| 47 | } |
| 48 | ASSERT_EQ(OK, socket_.GetLocalAddress(&local_address_)); |
| 49 | *success = true; |
| 50 | } |
| 51 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 52 | static IPEndPoint GetPeerAddress(StreamSocket* socket) { |
[email protected] | a352869 | 2012-06-08 00:11:42 | [diff] [blame] | 53 | IPEndPoint address; |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 54 | EXPECT_EQ(OK, socket->GetPeerAddress(&address)); |
[email protected] | a352869 | 2012-06-08 00:11:42 | [diff] [blame] | 55 | return address; |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 56 | } |
| 57 | |
[email protected] | fe89ea7 | 2011-05-12 02:02:40 | [diff] [blame] | 58 | AddressList local_address_list() const { |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 59 | return AddressList(local_address_); |
[email protected] | fe89ea7 | 2011-05-12 02:02:40 | [diff] [blame] | 60 | } |
| 61 | |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 62 | TCPServerSocket socket_; |
| 63 | IPEndPoint local_address_; |
| 64 | }; |
| 65 | |
| 66 | TEST_F(TCPServerSocketTest, Accept) { |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 67 | ASSERT_NO_FATAL_FAILURE(SetUpIPv4()); |
| 68 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 69 | TestCompletionCallback connect_callback; |
[email protected] | fe89ea7 | 2011-05-12 02:02:40 | [diff] [blame] | 70 | TCPClientSocket connecting_socket(local_address_list(), |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 71 | NULL, NetLog::Source()); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 72 | connecting_socket.Connect(connect_callback.callback()); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 73 | |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 74 | TestCompletionCallback accept_callback; |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 75 | scoped_ptr<StreamSocket> accepted_socket; |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 76 | int result = socket_.Accept(&accepted_socket, accept_callback.callback()); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 77 | if (result == ERR_IO_PENDING) |
| 78 | result = accept_callback.WaitForResult(); |
| 79 | ASSERT_EQ(OK, result); |
| 80 | |
| 81 | ASSERT_TRUE(accepted_socket.get() != NULL); |
| 82 | |
| 83 | // Both sockets should be on the loopback network interface. |
| 84 | EXPECT_EQ(GetPeerAddress(accepted_socket.get()).address(), |
| 85 | local_address_.address()); |
| 86 | |
| 87 | EXPECT_EQ(OK, connect_callback.WaitForResult()); |
| 88 | } |
| 89 | |
| 90 | // Test Accept() callback. |
| 91 | TEST_F(TCPServerSocketTest, AcceptAsync) { |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 92 | ASSERT_NO_FATAL_FAILURE(SetUpIPv4()); |
| 93 | |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 94 | TestCompletionCallback accept_callback; |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 95 | scoped_ptr<StreamSocket> accepted_socket; |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 96 | |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 97 | ASSERT_EQ(ERR_IO_PENDING, |
| 98 | socket_.Accept(&accepted_socket, accept_callback.callback())); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 99 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 100 | TestCompletionCallback connect_callback; |
[email protected] | fe89ea7 | 2011-05-12 02:02:40 | [diff] [blame] | 101 | TCPClientSocket connecting_socket(local_address_list(), |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 102 | NULL, NetLog::Source()); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 103 | connecting_socket.Connect(connect_callback.callback()); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 104 | |
| 105 | EXPECT_EQ(OK, connect_callback.WaitForResult()); |
| 106 | EXPECT_EQ(OK, accept_callback.WaitForResult()); |
| 107 | |
| 108 | EXPECT_TRUE(accepted_socket != NULL); |
| 109 | |
| 110 | // Both sockets should be on the loopback network interface. |
| 111 | EXPECT_EQ(GetPeerAddress(accepted_socket.get()).address(), |
| 112 | local_address_.address()); |
| 113 | } |
| 114 | |
| 115 | // Accept two connections simultaneously. |
| 116 | TEST_F(TCPServerSocketTest, Accept2Connections) { |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 117 | ASSERT_NO_FATAL_FAILURE(SetUpIPv4()); |
| 118 | |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 119 | TestCompletionCallback accept_callback; |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 120 | scoped_ptr<StreamSocket> accepted_socket; |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 121 | |
| 122 | ASSERT_EQ(ERR_IO_PENDING, |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 123 | socket_.Accept(&accepted_socket, accept_callback.callback())); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 124 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 125 | TestCompletionCallback connect_callback; |
[email protected] | fe89ea7 | 2011-05-12 02:02:40 | [diff] [blame] | 126 | TCPClientSocket connecting_socket(local_address_list(), |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 127 | NULL, NetLog::Source()); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 128 | connecting_socket.Connect(connect_callback.callback()); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 129 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 130 | TestCompletionCallback connect_callback2; |
[email protected] | fe89ea7 | 2011-05-12 02:02:40 | [diff] [blame] | 131 | TCPClientSocket connecting_socket2(local_address_list(), |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 132 | NULL, NetLog::Source()); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 133 | connecting_socket2.Connect(connect_callback2.callback()); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 134 | |
| 135 | EXPECT_EQ(OK, accept_callback.WaitForResult()); |
| 136 | |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 137 | TestCompletionCallback accept_callback2; |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 138 | scoped_ptr<StreamSocket> accepted_socket2; |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 139 | int result = socket_.Accept(&accepted_socket2, accept_callback2.callback()); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 140 | if (result == ERR_IO_PENDING) |
| 141 | result = accept_callback2.WaitForResult(); |
| 142 | ASSERT_EQ(OK, result); |
| 143 | |
| 144 | EXPECT_EQ(OK, connect_callback.WaitForResult()); |
| 145 | |
| 146 | EXPECT_TRUE(accepted_socket != NULL); |
| 147 | EXPECT_TRUE(accepted_socket2 != NULL); |
| 148 | EXPECT_NE(accepted_socket.get(), accepted_socket2.get()); |
| 149 | |
| 150 | EXPECT_EQ(GetPeerAddress(accepted_socket.get()).address(), |
| 151 | local_address_.address()); |
| 152 | EXPECT_EQ(GetPeerAddress(accepted_socket2.get()).address(), |
| 153 | local_address_.address()); |
| 154 | } |
| 155 | |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 156 | TEST_F(TCPServerSocketTest, AcceptIPv6) { |
| 157 | bool initialized = false; |
| 158 | ASSERT_NO_FATAL_FAILURE(SetUpIPv6(&initialized)); |
| 159 | if (!initialized) |
| 160 | return; |
| 161 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 162 | TestCompletionCallback connect_callback; |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 163 | TCPClientSocket connecting_socket(local_address_list(), |
| 164 | NULL, NetLog::Source()); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 165 | connecting_socket.Connect(connect_callback.callback()); |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 166 | |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 167 | TestCompletionCallback accept_callback; |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 168 | scoped_ptr<StreamSocket> accepted_socket; |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 169 | int result = socket_.Accept(&accepted_socket, accept_callback.callback()); |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 170 | if (result == ERR_IO_PENDING) |
| 171 | result = accept_callback.WaitForResult(); |
| 172 | ASSERT_EQ(OK, result); |
| 173 | |
| 174 | ASSERT_TRUE(accepted_socket.get() != NULL); |
| 175 | |
| 176 | // Both sockets should be on the loopback network interface. |
| 177 | EXPECT_EQ(GetPeerAddress(accepted_socket.get()).address(), |
| 178 | local_address_.address()); |
| 179 | |
| 180 | EXPECT_EQ(OK, connect_callback.WaitForResult()); |
| 181 | } |
| 182 | |
[email protected] | 7370de6e | 2012-02-22 08:04:18 | [diff] [blame] | 183 | TEST_F(TCPServerSocketTest, AcceptIO) { |
| 184 | ASSERT_NO_FATAL_FAILURE(SetUpIPv4()); |
| 185 | |
| 186 | TestCompletionCallback connect_callback; |
| 187 | TCPClientSocket connecting_socket(local_address_list(), |
| 188 | NULL, NetLog::Source()); |
| 189 | connecting_socket.Connect(connect_callback.callback()); |
| 190 | |
| 191 | TestCompletionCallback accept_callback; |
| 192 | scoped_ptr<StreamSocket> accepted_socket; |
| 193 | int result = socket_.Accept(&accepted_socket, accept_callback.callback()); |
| 194 | ASSERT_EQ(OK, accept_callback.GetResult(result)); |
| 195 | |
| 196 | ASSERT_TRUE(accepted_socket.get() != NULL); |
| 197 | |
| 198 | // Both sockets should be on the loopback network interface. |
| 199 | EXPECT_EQ(GetPeerAddress(accepted_socket.get()).address(), |
| 200 | local_address_.address()); |
| 201 | |
| 202 | EXPECT_EQ(OK, connect_callback.WaitForResult()); |
| 203 | |
| 204 | const std::string message("test message"); |
| 205 | std::vector<char> buffer(message.size()); |
| 206 | |
| 207 | size_t bytes_written = 0; |
| 208 | while (bytes_written < message.size()) { |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 209 | scoped_refptr<IOBufferWithSize> write_buffer( |
| 210 | new IOBufferWithSize(message.size() - bytes_written)); |
[email protected] | 7370de6e | 2012-02-22 08:04:18 | [diff] [blame] | 211 | memmove(write_buffer->data(), message.data(), message.size()); |
| 212 | |
| 213 | TestCompletionCallback write_callback; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 214 | int write_result = accepted_socket->Write( |
| 215 | write_buffer.get(), write_buffer->size(), write_callback.callback()); |
[email protected] | 7370de6e | 2012-02-22 08:04:18 | [diff] [blame] | 216 | write_result = write_callback.GetResult(write_result); |
| 217 | ASSERT_TRUE(write_result >= 0); |
| 218 | ASSERT_TRUE(bytes_written + write_result <= message.size()); |
| 219 | bytes_written += write_result; |
| 220 | } |
| 221 | |
| 222 | size_t bytes_read = 0; |
| 223 | while (bytes_read < message.size()) { |
[email protected] | fa6ce92 | 2014-07-17 04:27:04 | [diff] [blame] | 224 | scoped_refptr<IOBufferWithSize> read_buffer( |
| 225 | new IOBufferWithSize(message.size() - bytes_read)); |
[email protected] | 7370de6e | 2012-02-22 08:04:18 | [diff] [blame] | 226 | TestCompletionCallback read_callback; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 227 | int read_result = connecting_socket.Read( |
| 228 | read_buffer.get(), read_buffer->size(), read_callback.callback()); |
[email protected] | 7370de6e | 2012-02-22 08:04:18 | [diff] [blame] | 229 | read_result = read_callback.GetResult(read_result); |
| 230 | ASSERT_TRUE(read_result >= 0); |
| 231 | ASSERT_TRUE(bytes_read + read_result <= message.size()); |
| 232 | memmove(&buffer[bytes_read], read_buffer->data(), read_result); |
| 233 | bytes_read += read_result; |
| 234 | } |
| 235 | |
| 236 | std::string received_message(buffer.begin(), buffer.end()); |
| 237 | ASSERT_EQ(message, received_message); |
| 238 | } |
| 239 | |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 240 | } // namespace |
| 241 | |
| 242 | } // namespace net |