1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/socket/tcp_socket.h"
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "net/base/address_list.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/base/net_errors.h"
19 #include "net/base/test_completion_callback.h"
20 #include "net/socket/tcp_client_socket.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "testing/platform_test.h"
27 const int kListenBacklog
= 5;
29 class TCPSocketTest
: public PlatformTest
{
31 TCPSocketTest() : socket_(NULL
, NetLog::Source()) {
34 void SetUpListenIPv4() {
36 ParseAddress("127.0.0.1", 0, &address
);
38 ASSERT_EQ(OK
, socket_
.Open(ADDRESS_FAMILY_IPV4
));
39 ASSERT_EQ(OK
, socket_
.Bind(address
));
40 ASSERT_EQ(OK
, socket_
.Listen(kListenBacklog
));
41 ASSERT_EQ(OK
, socket_
.GetLocalAddress(&local_address_
));
44 void SetUpListenIPv6(bool* success
) {
47 ParseAddress("::1", 0, &address
);
49 if (socket_
.Open(ADDRESS_FAMILY_IPV6
) != OK
||
50 socket_
.Bind(address
) != OK
||
51 socket_
.Listen(kListenBacklog
) != OK
) {
52 LOG(ERROR
) << "Failed to listen on ::1 - probably because IPv6 is "
53 "disabled. Skipping the test";
56 ASSERT_EQ(OK
, socket_
.GetLocalAddress(&local_address_
));
60 void ParseAddress(const std::string
& ip_str
,
62 IPEndPoint
* address
) {
63 IPAddressNumber ip_number
;
64 bool rv
= ParseIPLiteralToNumber(ip_str
, &ip_number
);
67 *address
= IPEndPoint(ip_number
, port
);
70 void TestAcceptAsync() {
71 TestCompletionCallback accept_callback
;
72 scoped_ptr
<TCPSocket
> accepted_socket
;
73 IPEndPoint accepted_address
;
74 ASSERT_EQ(ERR_IO_PENDING
,
75 socket_
.Accept(&accepted_socket
, &accepted_address
,
76 accept_callback
.callback()));
78 TestCompletionCallback connect_callback
;
79 TCPClientSocket
connecting_socket(local_address_list(),
80 NULL
, NetLog::Source());
81 connecting_socket
.Connect(connect_callback
.callback());
83 EXPECT_EQ(OK
, connect_callback
.WaitForResult());
84 EXPECT_EQ(OK
, accept_callback
.WaitForResult());
86 EXPECT_TRUE(accepted_socket
.get());
88 // Both sockets should be on the loopback network interface.
89 EXPECT_EQ(accepted_address
.address(), local_address_
.address());
92 AddressList
local_address_list() const {
93 return AddressList(local_address_
);
97 IPEndPoint local_address_
;
100 // Test listening and accepting with a socket bound to an IPv4 address.
101 TEST_F(TCPSocketTest
, Accept
) {
102 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
104 TestCompletionCallback connect_callback
;
105 // TODO(yzshen): Switch to use TCPSocket when it supports client socket
107 TCPClientSocket
connecting_socket(local_address_list(),
108 NULL
, NetLog::Source());
109 connecting_socket
.Connect(connect_callback
.callback());
111 TestCompletionCallback accept_callback
;
112 scoped_ptr
<TCPSocket
> accepted_socket
;
113 IPEndPoint accepted_address
;
114 int result
= socket_
.Accept(&accepted_socket
, &accepted_address
,
115 accept_callback
.callback());
116 if (result
== ERR_IO_PENDING
)
117 result
= accept_callback
.WaitForResult();
118 ASSERT_EQ(OK
, result
);
120 EXPECT_TRUE(accepted_socket
.get());
122 // Both sockets should be on the loopback network interface.
123 EXPECT_EQ(accepted_address
.address(), local_address_
.address());
125 EXPECT_EQ(OK
, connect_callback
.WaitForResult());
128 // Test Accept() callback.
129 TEST_F(TCPSocketTest
, AcceptAsync
) {
130 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
135 // Test Accept() for AdoptListenSocket.
136 TEST_F(TCPSocketTest
, AcceptForAdoptedListenSocket
) {
137 // Create a socket to be used with AdoptListenSocket.
138 SOCKET existing_socket
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
139 ASSERT_EQ(OK
, socket_
.AdoptListenSocket(existing_socket
));
142 ParseAddress("127.0.0.1", 0, &address
);
143 SockaddrStorage storage
;
144 ASSERT_TRUE(address
.ToSockAddr(storage
.addr
, &storage
.addr_len
));
145 ASSERT_EQ(0, bind(existing_socket
, storage
.addr
, storage
.addr_len
));
147 ASSERT_EQ(OK
, socket_
.Listen(kListenBacklog
));
148 ASSERT_EQ(OK
, socket_
.GetLocalAddress(&local_address_
));
154 // Accept two connections simultaneously.
155 TEST_F(TCPSocketTest
, Accept2Connections
) {
156 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
158 TestCompletionCallback accept_callback
;
159 scoped_ptr
<TCPSocket
> accepted_socket
;
160 IPEndPoint accepted_address
;
162 ASSERT_EQ(ERR_IO_PENDING
,
163 socket_
.Accept(&accepted_socket
, &accepted_address
,
164 accept_callback
.callback()));
166 TestCompletionCallback connect_callback
;
167 TCPClientSocket
connecting_socket(local_address_list(),
168 NULL
, NetLog::Source());
169 connecting_socket
.Connect(connect_callback
.callback());
171 TestCompletionCallback connect_callback2
;
172 TCPClientSocket
connecting_socket2(local_address_list(),
173 NULL
, NetLog::Source());
174 connecting_socket2
.Connect(connect_callback2
.callback());
176 EXPECT_EQ(OK
, accept_callback
.WaitForResult());
178 TestCompletionCallback accept_callback2
;
179 scoped_ptr
<TCPSocket
> accepted_socket2
;
180 IPEndPoint accepted_address2
;
182 int result
= socket_
.Accept(&accepted_socket2
, &accepted_address2
,
183 accept_callback2
.callback());
184 if (result
== ERR_IO_PENDING
)
185 result
= accept_callback2
.WaitForResult();
186 ASSERT_EQ(OK
, result
);
188 EXPECT_EQ(OK
, connect_callback
.WaitForResult());
189 EXPECT_EQ(OK
, connect_callback2
.WaitForResult());
191 EXPECT_TRUE(accepted_socket
.get());
192 EXPECT_TRUE(accepted_socket2
.get());
193 EXPECT_NE(accepted_socket
.get(), accepted_socket2
.get());
195 EXPECT_EQ(accepted_address
.address(), local_address_
.address());
196 EXPECT_EQ(accepted_address2
.address(), local_address_
.address());
199 // Test listening and accepting with a socket bound to an IPv6 address.
200 TEST_F(TCPSocketTest
, AcceptIPv6
) {
201 bool initialized
= false;
202 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv6(&initialized
));
206 TestCompletionCallback connect_callback
;
207 TCPClientSocket
connecting_socket(local_address_list(),
208 NULL
, NetLog::Source());
209 connecting_socket
.Connect(connect_callback
.callback());
211 TestCompletionCallback accept_callback
;
212 scoped_ptr
<TCPSocket
> accepted_socket
;
213 IPEndPoint accepted_address
;
214 int result
= socket_
.Accept(&accepted_socket
, &accepted_address
,
215 accept_callback
.callback());
216 if (result
== ERR_IO_PENDING
)
217 result
= accept_callback
.WaitForResult();
218 ASSERT_EQ(OK
, result
);
220 EXPECT_TRUE(accepted_socket
.get());
222 // Both sockets should be on the loopback network interface.
223 EXPECT_EQ(accepted_address
.address(), local_address_
.address());
225 EXPECT_EQ(OK
, connect_callback
.WaitForResult());
228 TEST_F(TCPSocketTest
, ReadWrite
) {
229 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4());
231 TestCompletionCallback connect_callback
;
232 TCPSocket
connecting_socket(NULL
, NetLog::Source());
233 int result
= connecting_socket
.Open(ADDRESS_FAMILY_IPV4
);
234 ASSERT_EQ(OK
, result
);
235 connecting_socket
.Connect(local_address_
, connect_callback
.callback());
237 TestCompletionCallback accept_callback
;
238 scoped_ptr
<TCPSocket
> accepted_socket
;
239 IPEndPoint accepted_address
;
240 result
= socket_
.Accept(&accepted_socket
, &accepted_address
,
241 accept_callback
.callback());
242 ASSERT_EQ(OK
, accept_callback
.GetResult(result
));
244 ASSERT_TRUE(accepted_socket
.get());
246 // Both sockets should be on the loopback network interface.
247 EXPECT_EQ(accepted_address
.address(), local_address_
.address());
249 EXPECT_EQ(OK
, connect_callback
.WaitForResult());
251 const std::string
message("test message");
252 std::vector
<char> buffer(message
.size());
254 size_t bytes_written
= 0;
255 while (bytes_written
< message
.size()) {
256 scoped_refptr
<IOBufferWithSize
> write_buffer(
257 new IOBufferWithSize(message
.size() - bytes_written
));
258 memmove(write_buffer
->data(), message
.data() + bytes_written
,
259 message
.size() - bytes_written
);
261 TestCompletionCallback write_callback
;
262 int write_result
= accepted_socket
->Write(
263 write_buffer
.get(), write_buffer
->size(), write_callback
.callback());
264 write_result
= write_callback
.GetResult(write_result
);
265 ASSERT_TRUE(write_result
>= 0);
266 bytes_written
+= write_result
;
267 ASSERT_TRUE(bytes_written
<= message
.size());
270 size_t bytes_read
= 0;
271 while (bytes_read
< message
.size()) {
272 scoped_refptr
<IOBufferWithSize
> read_buffer(
273 new IOBufferWithSize(message
.size() - bytes_read
));
274 TestCompletionCallback read_callback
;
275 int read_result
= connecting_socket
.Read(
276 read_buffer
.get(), read_buffer
->size(), read_callback
.callback());
277 read_result
= read_callback
.GetResult(read_result
);
278 ASSERT_TRUE(read_result
>= 0);
279 ASSERT_TRUE(bytes_read
+ read_result
<= message
.size());
280 memmove(&buffer
[bytes_read
], read_buffer
->data(), read_result
);
281 bytes_read
+= read_result
;
284 std::string
received_message(buffer
.begin(), buffer
.end());
285 ASSERT_EQ(message
, received_message
);