1 // Copyright (c) 2012 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_client_socket.h"
7 #include "base/basictypes.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "net/base/address_list.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/net_log.h"
14 #include "net/base/net_log_unittest.h"
15 #include "net/base/test_completion_callback.h"
16 #include "net/base/winsock_init.h"
17 #include "net/dns/mock_host_resolver.h"
18 #include "net/socket/client_socket_factory.h"
19 #include "net/socket/tcp_listen_socket.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "testing/platform_test.h"
27 const char kServerReply
[] = "HTTP/1.1 404 Not Found";
29 enum ClientSocketTestTypes
{
36 class TransportClientSocketTest
37 : public StreamListenSocket::Delegate
,
38 public ::testing::TestWithParam
<ClientSocketTestTypes
> {
40 TransportClientSocketTest()
42 socket_factory_(ClientSocketFactory::GetDefaultFactory()),
43 close_server_socket_on_next_send_(false) {
46 virtual ~TransportClientSocketTest() {
49 // Implement StreamListenSocket::Delegate methods
50 virtual void DidAccept(StreamListenSocket
* server
,
51 scoped_ptr
<StreamListenSocket
> connection
) OVERRIDE
{
52 connected_sock_
.reset(
53 static_cast<TCPListenSocket
*>(connection
.release()));
55 virtual void DidRead(StreamListenSocket
*, const char* str
, int len
) OVERRIDE
{
56 // TODO(dkegel): this might not be long enough to tickle some bugs.
57 connected_sock_
->Send(kServerReply
, arraysize(kServerReply
) - 1,
58 false /* Don't append line feed */);
59 if (close_server_socket_on_next_send_
)
62 virtual void DidClose(StreamListenSocket
* sock
) OVERRIDE
{}
67 void CloseServerSocket() {
68 // delete the connected_sock_, which will close it.
69 connected_sock_
.reset();
72 void PauseServerReads() {
73 connected_sock_
->PauseReads();
76 void ResumeServerReads() {
77 connected_sock_
->ResumeReads();
80 int DrainClientSocket(IOBuffer
* buf
,
83 TestCompletionCallback
* callback
);
85 void SendClientRequest();
87 void set_close_server_socket_on_next_send(bool close
) {
88 close_server_socket_on_next_send_
= close
;
93 CapturingNetLog net_log_
;
94 ClientSocketFactory
* const socket_factory_
;
95 scoped_ptr
<StreamSocket
> sock_
;
98 scoped_ptr
<TCPListenSocket
> listen_sock_
;
99 scoped_ptr
<TCPListenSocket
> connected_sock_
;
100 bool close_server_socket_on_next_send_
;
103 void TransportClientSocketTest::SetUp() {
104 ::testing::TestWithParam
<ClientSocketTestTypes
>::SetUp();
106 // Find a free port to listen on
107 scoped_ptr
<TCPListenSocket
> sock
;
109 // Range of ports to listen on. Shouldn't need to try many.
110 const int kMinPort
= 10100;
111 const int kMaxPort
= 10200;
115 for (port
= kMinPort
; port
< kMaxPort
; port
++) {
116 sock
= TCPListenSocket::CreateAndListen("127.0.0.1", port
, this);
120 ASSERT_TRUE(sock
.get() != NULL
);
121 listen_sock_
= sock
.Pass();
125 // MockHostResolver resolves everything to 127.0.0.1.
126 scoped_ptr
<HostResolver
> resolver(new MockHostResolver());
127 HostResolver::RequestInfo
info(HostPortPair("localhost", listen_port_
));
128 TestCompletionCallback callback
;
129 int rv
= resolver
->Resolve(
130 info
, DEFAULT_PRIORITY
, &addr
, callback
.callback(), NULL
, BoundNetLog());
131 CHECK_EQ(ERR_IO_PENDING
, rv
);
132 rv
= callback
.WaitForResult();
135 socket_factory_
->CreateTransportClientSocket(addr
,
140 int TransportClientSocketTest::DrainClientSocket(
141 IOBuffer
* buf
, uint32 buf_len
,
142 uint32 bytes_to_read
, TestCompletionCallback
* callback
) {
144 uint32 bytes_read
= 0;
146 while (bytes_read
< bytes_to_read
) {
147 rv
= sock_
->Read(buf
, buf_len
, callback
->callback());
148 EXPECT_TRUE(rv
>= 0 || rv
== ERR_IO_PENDING
);
150 if (rv
== ERR_IO_PENDING
)
151 rv
= callback
->WaitForResult();
157 return static_cast<int>(bytes_read
);
160 void TransportClientSocketTest::SendClientRequest() {
161 const char request_text
[] = "GET / HTTP/1.0\r\n\r\n";
162 scoped_refptr
<IOBuffer
> request_buffer(
163 new IOBuffer(arraysize(request_text
) - 1));
164 TestCompletionCallback callback
;
167 memcpy(request_buffer
->data(), request_text
, arraysize(request_text
) - 1);
169 request_buffer
.get(), arraysize(request_text
) - 1, callback
.callback());
170 EXPECT_TRUE(rv
>= 0 || rv
== ERR_IO_PENDING
);
172 if (rv
== ERR_IO_PENDING
)
173 rv
= callback
.WaitForResult();
174 EXPECT_EQ(rv
, static_cast<int>(arraysize(request_text
) - 1));
177 // TODO(leighton): Add SCTP to this list when it is ready.
178 INSTANTIATE_TEST_CASE_P(StreamSocket
,
179 TransportClientSocketTest
,
180 ::testing::Values(TCP
));
182 TEST_P(TransportClientSocketTest
, Connect
) {
183 TestCompletionCallback callback
;
184 EXPECT_FALSE(sock_
->IsConnected());
186 int rv
= sock_
->Connect(callback
.callback());
188 net::CapturingNetLog::CapturedEntryList net_log_entries
;
189 net_log_
.GetEntries(&net_log_entries
);
190 EXPECT_TRUE(net::LogContainsBeginEvent(
191 net_log_entries
, 0, net::NetLog::TYPE_SOCKET_ALIVE
));
192 EXPECT_TRUE(net::LogContainsBeginEvent(
193 net_log_entries
, 1, net::NetLog::TYPE_TCP_CONNECT
));
195 ASSERT_EQ(rv
, ERR_IO_PENDING
);
196 rv
= callback
.WaitForResult();
200 EXPECT_TRUE(sock_
->IsConnected());
201 net_log_
.GetEntries(&net_log_entries
);
202 EXPECT_TRUE(net::LogContainsEndEvent(
203 net_log_entries
, -1, net::NetLog::TYPE_TCP_CONNECT
));
206 EXPECT_FALSE(sock_
->IsConnected());
209 TEST_P(TransportClientSocketTest
, IsConnected
) {
210 scoped_refptr
<IOBuffer
> buf(new IOBuffer(4096));
211 TestCompletionCallback callback
;
214 EXPECT_FALSE(sock_
->IsConnected());
215 EXPECT_FALSE(sock_
->IsConnectedAndIdle());
216 int rv
= sock_
->Connect(callback
.callback());
218 ASSERT_EQ(rv
, ERR_IO_PENDING
);
219 rv
= callback
.WaitForResult();
222 EXPECT_TRUE(sock_
->IsConnected());
223 EXPECT_TRUE(sock_
->IsConnectedAndIdle());
225 // Send the request and wait for the server to respond.
228 // Drain a single byte so we know we've received some data.
229 bytes_read
= DrainClientSocket(buf
.get(), 1, 1, &callback
);
230 ASSERT_EQ(bytes_read
, 1u);
232 // Socket should be considered connected, but not idle, due to
234 EXPECT_TRUE(sock_
->IsConnected());
235 EXPECT_FALSE(sock_
->IsConnectedAndIdle());
237 bytes_read
= DrainClientSocket(
238 buf
.get(), 4096, arraysize(kServerReply
) - 2, &callback
);
239 ASSERT_EQ(bytes_read
, arraysize(kServerReply
) - 2);
241 // After draining the data, the socket should be back to connected
243 EXPECT_TRUE(sock_
->IsConnected());
244 EXPECT_TRUE(sock_
->IsConnectedAndIdle());
246 // This time close the server socket immediately after the server response.
247 set_close_server_socket_on_next_send(true);
250 bytes_read
= DrainClientSocket(buf
.get(), 1, 1, &callback
);
251 ASSERT_EQ(bytes_read
, 1u);
253 // As above because of data.
254 EXPECT_TRUE(sock_
->IsConnected());
255 EXPECT_FALSE(sock_
->IsConnectedAndIdle());
257 bytes_read
= DrainClientSocket(
258 buf
.get(), 4096, arraysize(kServerReply
) - 2, &callback
);
259 ASSERT_EQ(bytes_read
, arraysize(kServerReply
) - 2);
261 // Once the data is drained, the socket should now be seen as not
263 if (sock_
->IsConnected()) {
264 // In the unlikely event that the server's connection closure is not
265 // processed in time, wait for the connection to be closed.
266 rv
= sock_
->Read(buf
.get(), 4096, callback
.callback());
267 EXPECT_EQ(0, callback
.GetResult(rv
));
268 EXPECT_FALSE(sock_
->IsConnected());
270 EXPECT_FALSE(sock_
->IsConnectedAndIdle());
273 TEST_P(TransportClientSocketTest
, Read
) {
274 TestCompletionCallback callback
;
275 int rv
= sock_
->Connect(callback
.callback());
277 ASSERT_EQ(rv
, ERR_IO_PENDING
);
279 rv
= callback
.WaitForResult();
284 scoped_refptr
<IOBuffer
> buf(new IOBuffer(4096));
285 uint32 bytes_read
= DrainClientSocket(
286 buf
.get(), 4096, arraysize(kServerReply
) - 1, &callback
);
287 ASSERT_EQ(bytes_read
, arraysize(kServerReply
) - 1);
289 // All data has been read now. Read once more to force an ERR_IO_PENDING, and
290 // then close the server socket, and note the close.
292 rv
= sock_
->Read(buf
.get(), 4096, callback
.callback());
293 ASSERT_EQ(ERR_IO_PENDING
, rv
);
295 EXPECT_EQ(0, callback
.WaitForResult());
298 TEST_P(TransportClientSocketTest
, Read_SmallChunks
) {
299 TestCompletionCallback callback
;
300 int rv
= sock_
->Connect(callback
.callback());
302 ASSERT_EQ(rv
, ERR_IO_PENDING
);
304 rv
= callback
.WaitForResult();
309 scoped_refptr
<IOBuffer
> buf(new IOBuffer(1));
310 uint32 bytes_read
= 0;
311 while (bytes_read
< arraysize(kServerReply
) - 1) {
312 rv
= sock_
->Read(buf
.get(), 1, callback
.callback());
313 EXPECT_TRUE(rv
>= 0 || rv
== ERR_IO_PENDING
);
315 if (rv
== ERR_IO_PENDING
)
316 rv
= callback
.WaitForResult();
322 // All data has been read now. Read once more to force an ERR_IO_PENDING, and
323 // then close the server socket, and note the close.
325 rv
= sock_
->Read(buf
.get(), 1, callback
.callback());
326 ASSERT_EQ(ERR_IO_PENDING
, rv
);
328 EXPECT_EQ(0, callback
.WaitForResult());
331 TEST_P(TransportClientSocketTest
, Read_Interrupted
) {
332 TestCompletionCallback callback
;
333 int rv
= sock_
->Connect(callback
.callback());
335 ASSERT_EQ(ERR_IO_PENDING
, rv
);
337 rv
= callback
.WaitForResult();
342 // Do a partial read and then exit. This test should not crash!
343 scoped_refptr
<IOBuffer
> buf(new IOBuffer(16));
344 rv
= sock_
->Read(buf
.get(), 16, callback
.callback());
345 EXPECT_TRUE(rv
>= 0 || rv
== ERR_IO_PENDING
);
347 if (rv
== ERR_IO_PENDING
)
348 rv
= callback
.WaitForResult();
353 TEST_P(TransportClientSocketTest
, DISABLED_FullDuplex_ReadFirst
) {
354 TestCompletionCallback callback
;
355 int rv
= sock_
->Connect(callback
.callback());
357 ASSERT_EQ(rv
, ERR_IO_PENDING
);
359 rv
= callback
.WaitForResult();
363 // Read first. There's no data, so it should return ERR_IO_PENDING.
364 const int kBufLen
= 4096;
365 scoped_refptr
<IOBuffer
> buf(new IOBuffer(kBufLen
));
366 rv
= sock_
->Read(buf
.get(), kBufLen
, callback
.callback());
367 EXPECT_EQ(ERR_IO_PENDING
, rv
);
370 const int kWriteBufLen
= 64 * 1024;
371 scoped_refptr
<IOBuffer
> request_buffer(new IOBuffer(kWriteBufLen
));
372 char* request_data
= request_buffer
->data();
373 memset(request_data
, 'A', kWriteBufLen
);
374 TestCompletionCallback write_callback
;
378 request_buffer
.get(), kWriteBufLen
, write_callback
.callback());
379 ASSERT_TRUE(rv
>= 0 || rv
== ERR_IO_PENDING
);
381 if (rv
== ERR_IO_PENDING
) {
383 rv
= write_callback
.WaitForResult();
388 // At this point, both read and write have returned ERR_IO_PENDING, and the
389 // write callback has executed. We wait for the read callback to run now to
390 // make sure that the socket can handle full duplex communications.
392 rv
= callback
.WaitForResult();
396 TEST_P(TransportClientSocketTest
, DISABLED_FullDuplex_WriteFirst
) {
397 TestCompletionCallback callback
;
398 int rv
= sock_
->Connect(callback
.callback());
400 ASSERT_EQ(ERR_IO_PENDING
, rv
);
402 rv
= callback
.WaitForResult();
407 const int kWriteBufLen
= 64 * 1024;
408 scoped_refptr
<IOBuffer
> request_buffer(new IOBuffer(kWriteBufLen
));
409 char* request_data
= request_buffer
->data();
410 memset(request_data
, 'A', kWriteBufLen
);
411 TestCompletionCallback write_callback
;
415 request_buffer
.get(), kWriteBufLen
, write_callback
.callback());
416 ASSERT_TRUE(rv
>= 0 || rv
== ERR_IO_PENDING
);
418 if (rv
== ERR_IO_PENDING
)
422 // Now we have the Write() blocked on ERR_IO_PENDING. It's time to force the
423 // Read() to block on ERR_IO_PENDING too.
425 const int kBufLen
= 4096;
426 scoped_refptr
<IOBuffer
> buf(new IOBuffer(kBufLen
));
428 rv
= sock_
->Read(buf
.get(), kBufLen
, callback
.callback());
429 ASSERT_TRUE(rv
>= 0 || rv
== ERR_IO_PENDING
);
430 if (rv
== ERR_IO_PENDING
)
434 // At this point, both read and write have returned ERR_IO_PENDING. Now we
435 // run the write and read callbacks to make sure they can handle full duplex
439 rv
= write_callback
.WaitForResult();
442 // It's possible the read is blocked because it's already read all the data.
443 // Close the server socket, so there will at least be a 0-byte read.
446 rv
= callback
.WaitForResult();