Revert 233414 "x11: Move XInput2 availability information out of..."
[chromium-blink-merge.git] / net / socket / transport_client_socket_unittest.cc
blob5548b27b995999dc76daebc8d418230e4c4dace8
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"
23 namespace net {
25 namespace {
27 const char kServerReply[] = "HTTP/1.1 404 Not Found";
29 enum ClientSocketTestTypes {
30 TCP,
31 SCTP
34 } // namespace
36 class TransportClientSocketTest
37 : public StreamListenSocket::Delegate,
38 public ::testing::TestWithParam<ClientSocketTestTypes> {
39 public:
40 TransportClientSocketTest()
41 : listen_port_(0),
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_)
60 CloseServerSocket();
62 virtual void DidClose(StreamListenSocket* sock) OVERRIDE {}
64 // Testcase hooks
65 virtual void SetUp();
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,
81 uint32 buf_len,
82 uint32 bytes_to_read,
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;
91 protected:
92 int listen_port_;
93 CapturingNetLog net_log_;
94 ClientSocketFactory* const socket_factory_;
95 scoped_ptr<StreamSocket> sock_;
97 private:
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;
108 int port;
109 // Range of ports to listen on. Shouldn't need to try many.
110 const int kMinPort = 10100;
111 const int kMaxPort = 10200;
112 #if defined(OS_WIN)
113 EnsureWinsockInit();
114 #endif
115 for (port = kMinPort; port < kMaxPort; port++) {
116 sock = TCPListenSocket::CreateAndListen("127.0.0.1", port, this);
117 if (sock.get())
118 break;
120 ASSERT_TRUE(sock.get() != NULL);
121 listen_sock_ = sock.Pass();
122 listen_port_ = port;
124 AddressList addr;
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();
133 CHECK_EQ(rv, OK);
134 sock_ =
135 socket_factory_->CreateTransportClientSocket(addr,
136 &net_log_,
137 NetLog::Source());
140 int TransportClientSocketTest::DrainClientSocket(
141 IOBuffer* buf, uint32 buf_len,
142 uint32 bytes_to_read, TestCompletionCallback* callback) {
143 int rv = OK;
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();
153 EXPECT_GE(rv, 0);
154 bytes_read += rv;
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;
165 int rv;
167 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1);
168 rv = sock_->Write(
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));
194 if (rv != OK) {
195 ASSERT_EQ(rv, ERR_IO_PENDING);
196 rv = callback.WaitForResult();
197 EXPECT_EQ(rv, OK);
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));
205 sock_->Disconnect();
206 EXPECT_FALSE(sock_->IsConnected());
209 TEST_P(TransportClientSocketTest, IsConnected) {
210 scoped_refptr<IOBuffer> buf(new IOBuffer(4096));
211 TestCompletionCallback callback;
212 uint32 bytes_read;
214 EXPECT_FALSE(sock_->IsConnected());
215 EXPECT_FALSE(sock_->IsConnectedAndIdle());
216 int rv = sock_->Connect(callback.callback());
217 if (rv != OK) {
218 ASSERT_EQ(rv, ERR_IO_PENDING);
219 rv = callback.WaitForResult();
220 EXPECT_EQ(rv, OK);
222 EXPECT_TRUE(sock_->IsConnected());
223 EXPECT_TRUE(sock_->IsConnectedAndIdle());
225 // Send the request and wait for the server to respond.
226 SendClientRequest();
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
233 // pending data.
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
242 // and idle.
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);
248 SendClientRequest();
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
262 // connected.
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());
276 if (rv != OK) {
277 ASSERT_EQ(rv, ERR_IO_PENDING);
279 rv = callback.WaitForResult();
280 EXPECT_EQ(rv, OK);
282 SendClientRequest();
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);
294 CloseServerSocket();
295 EXPECT_EQ(0, callback.WaitForResult());
298 TEST_P(TransportClientSocketTest, Read_SmallChunks) {
299 TestCompletionCallback callback;
300 int rv = sock_->Connect(callback.callback());
301 if (rv != OK) {
302 ASSERT_EQ(rv, ERR_IO_PENDING);
304 rv = callback.WaitForResult();
305 EXPECT_EQ(rv, OK);
307 SendClientRequest();
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();
318 ASSERT_EQ(1, rv);
319 bytes_read += rv;
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);
327 CloseServerSocket();
328 EXPECT_EQ(0, callback.WaitForResult());
331 TEST_P(TransportClientSocketTest, Read_Interrupted) {
332 TestCompletionCallback callback;
333 int rv = sock_->Connect(callback.callback());
334 if (rv != OK) {
335 ASSERT_EQ(ERR_IO_PENDING, rv);
337 rv = callback.WaitForResult();
338 EXPECT_EQ(rv, OK);
340 SendClientRequest();
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();
350 EXPECT_NE(0, rv);
353 TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_ReadFirst) {
354 TestCompletionCallback callback;
355 int rv = sock_->Connect(callback.callback());
356 if (rv != OK) {
357 ASSERT_EQ(rv, ERR_IO_PENDING);
359 rv = callback.WaitForResult();
360 EXPECT_EQ(rv, OK);
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);
369 PauseServerReads();
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;
376 while (true) {
377 rv = sock_->Write(
378 request_buffer.get(), kWriteBufLen, write_callback.callback());
379 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING);
381 if (rv == ERR_IO_PENDING) {
382 ResumeServerReads();
383 rv = write_callback.WaitForResult();
384 break;
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();
393 EXPECT_GE(rv, 0);
396 TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_WriteFirst) {
397 TestCompletionCallback callback;
398 int rv = sock_->Connect(callback.callback());
399 if (rv != OK) {
400 ASSERT_EQ(ERR_IO_PENDING, rv);
402 rv = callback.WaitForResult();
403 EXPECT_EQ(OK, rv);
406 PauseServerReads();
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;
413 while (true) {
414 rv = sock_->Write(
415 request_buffer.get(), kWriteBufLen, write_callback.callback());
416 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING);
418 if (rv == ERR_IO_PENDING)
419 break;
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));
427 while (true) {
428 rv = sock_->Read(buf.get(), kBufLen, callback.callback());
429 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING);
430 if (rv == ERR_IO_PENDING)
431 break;
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
436 // communications.
438 ResumeServerReads();
439 rv = write_callback.WaitForResult();
440 EXPECT_GE(rv, 0);
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.
444 CloseServerSocket();
446 rv = callback.WaitForResult();
447 EXPECT_GE(rv, 0);
450 } // namespace net