| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket/tcp_client_socket.h" | 5 #include "net/socket/tcp_client_socket.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "net/base/address_list.h" | 8 #include "net/base/address_list.h" |
| 9 #include "net/base/host_resolver.h" | 9 #include "net/base/host_resolver.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| 11 #include "net/base/listen_socket.h" | 11 #include "net/base/listen_socket.h" |
| 12 #include "net/base/load_log.h" |
| 13 #include "net/base/load_log_unittest.h" |
| 12 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 13 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
| 14 #include "net/base/winsock_init.h" | 16 #include "net/base/winsock_init.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "testing/platform_test.h" | 18 #include "testing/platform_test.h" |
| 17 | 19 |
| 18 namespace net { | 20 namespace net { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 HostResolver::RequestInfo info("localhost", listen_port_); | 92 HostResolver::RequestInfo info("localhost", listen_port_); |
| 91 int rv = resolver->Resolve(info, &addr, NULL, NULL, NULL); | 93 int rv = resolver->Resolve(info, &addr, NULL, NULL, NULL); |
| 92 CHECK(rv == OK); | 94 CHECK(rv == OK); |
| 93 sock_.reset(new TCPClientSocket(addr)); | 95 sock_.reset(new TCPClientSocket(addr)); |
| 94 } | 96 } |
| 95 | 97 |
| 96 TEST_F(TCPClientSocketTest, Connect) { | 98 TEST_F(TCPClientSocketTest, Connect) { |
| 97 TestCompletionCallback callback; | 99 TestCompletionCallback callback; |
| 98 EXPECT_FALSE(sock_->IsConnected()); | 100 EXPECT_FALSE(sock_->IsConnected()); |
| 99 | 101 |
| 100 int rv = sock_->Connect(&callback); | 102 scoped_refptr<LoadLog> log(new LoadLog); |
| 103 int rv = sock_->Connect(&callback, log); |
| 104 EXPECT_TRUE(net::LogContains( |
| 105 *log, 0, net::LoadLog::TYPE_TCP_CONNECT, net::LoadLog::PHASE_BEGIN)); |
| 101 if (rv != OK) { | 106 if (rv != OK) { |
| 102 ASSERT_EQ(rv, ERR_IO_PENDING); | 107 ASSERT_EQ(rv, ERR_IO_PENDING); |
| 108 EXPECT_FALSE(net::LogContains( |
| 109 *log, -1, net::LoadLog::TYPE_TCP_CONNECT, net::LoadLog::PHASE_END)); |
| 103 | 110 |
| 104 rv = callback.WaitForResult(); | 111 rv = callback.WaitForResult(); |
| 105 EXPECT_EQ(rv, OK); | 112 EXPECT_EQ(rv, OK); |
| 106 } | 113 } |
| 107 | 114 |
| 108 EXPECT_TRUE(sock_->IsConnected()); | 115 EXPECT_TRUE(sock_->IsConnected()); |
| 116 EXPECT_TRUE(net::LogContains( |
| 117 *log, -1, net::LoadLog::TYPE_TCP_CONNECT, net::LoadLog::PHASE_END)); |
| 109 | 118 |
| 110 sock_->Disconnect(); | 119 sock_->Disconnect(); |
| 111 EXPECT_FALSE(sock_->IsConnected()); | 120 EXPECT_FALSE(sock_->IsConnected()); |
| 112 } | 121 } |
| 113 | 122 |
| 114 // TODO(wtc): Add unit tests for IsConnectedAndIdle: | 123 // TODO(wtc): Add unit tests for IsConnectedAndIdle: |
| 115 // - Server closes a connection. | 124 // - Server closes a connection. |
| 116 // - Server sends data unexpectedly. | 125 // - Server sends data unexpectedly. |
| 117 | 126 |
| 118 TEST_F(TCPClientSocketTest, Read) { | 127 TEST_F(TCPClientSocketTest, Read) { |
| 119 TestCompletionCallback callback; | 128 TestCompletionCallback callback; |
| 120 int rv = sock_->Connect(&callback); | 129 int rv = sock_->Connect(&callback, NULL); |
| 121 if (rv != OK) { | 130 if (rv != OK) { |
| 122 ASSERT_EQ(rv, ERR_IO_PENDING); | 131 ASSERT_EQ(rv, ERR_IO_PENDING); |
| 123 | 132 |
| 124 rv = callback.WaitForResult(); | 133 rv = callback.WaitForResult(); |
| 125 EXPECT_EQ(rv, OK); | 134 EXPECT_EQ(rv, OK); |
| 126 } | 135 } |
| 127 | 136 |
| 128 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 137 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 129 scoped_refptr<IOBuffer> request_buffer = | 138 scoped_refptr<IOBuffer> request_buffer = |
| 130 new IOBuffer(arraysize(request_text) - 1); | 139 new IOBuffer(arraysize(request_text) - 1); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 155 // then close the server socket, and note the close. | 164 // then close the server socket, and note the close. |
| 156 | 165 |
| 157 rv = sock_->Read(buf, 4096, &callback); | 166 rv = sock_->Read(buf, 4096, &callback); |
| 158 ASSERT_EQ(ERR_IO_PENDING, rv); | 167 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 159 CloseServerSocket(); | 168 CloseServerSocket(); |
| 160 EXPECT_EQ(0, callback.WaitForResult()); | 169 EXPECT_EQ(0, callback.WaitForResult()); |
| 161 } | 170 } |
| 162 | 171 |
| 163 TEST_F(TCPClientSocketTest, Read_SmallChunks) { | 172 TEST_F(TCPClientSocketTest, Read_SmallChunks) { |
| 164 TestCompletionCallback callback; | 173 TestCompletionCallback callback; |
| 165 int rv = sock_->Connect(&callback); | 174 int rv = sock_->Connect(&callback, NULL); |
| 166 if (rv != OK) { | 175 if (rv != OK) { |
| 167 ASSERT_EQ(rv, ERR_IO_PENDING); | 176 ASSERT_EQ(rv, ERR_IO_PENDING); |
| 168 | 177 |
| 169 rv = callback.WaitForResult(); | 178 rv = callback.WaitForResult(); |
| 170 EXPECT_EQ(rv, OK); | 179 EXPECT_EQ(rv, OK); |
| 171 } | 180 } |
| 172 | 181 |
| 173 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 182 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 174 scoped_refptr<IOBuffer> request_buffer = | 183 scoped_refptr<IOBuffer> request_buffer = |
| 175 new IOBuffer(arraysize(request_text) - 1); | 184 new IOBuffer(arraysize(request_text) - 1); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 200 // then close the server socket, and note the close. | 209 // then close the server socket, and note the close. |
| 201 | 210 |
| 202 rv = sock_->Read(buf, 1, &callback); | 211 rv = sock_->Read(buf, 1, &callback); |
| 203 ASSERT_EQ(ERR_IO_PENDING, rv); | 212 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 204 CloseServerSocket(); | 213 CloseServerSocket(); |
| 205 EXPECT_EQ(0, callback.WaitForResult()); | 214 EXPECT_EQ(0, callback.WaitForResult()); |
| 206 } | 215 } |
| 207 | 216 |
| 208 TEST_F(TCPClientSocketTest, Read_Interrupted) { | 217 TEST_F(TCPClientSocketTest, Read_Interrupted) { |
| 209 TestCompletionCallback callback; | 218 TestCompletionCallback callback; |
| 210 int rv = sock_->Connect(&callback); | 219 int rv = sock_->Connect(&callback, NULL); |
| 211 if (rv != OK) { | 220 if (rv != OK) { |
| 212 ASSERT_EQ(ERR_IO_PENDING, rv); | 221 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 213 | 222 |
| 214 rv = callback.WaitForResult(); | 223 rv = callback.WaitForResult(); |
| 215 EXPECT_EQ(rv, OK); | 224 EXPECT_EQ(rv, OK); |
| 216 } | 225 } |
| 217 | 226 |
| 218 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 227 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 219 scoped_refptr<IOBuffer> request_buffer = | 228 scoped_refptr<IOBuffer> request_buffer = |
| 220 new IOBuffer(arraysize(request_text) - 1); | 229 new IOBuffer(arraysize(request_text) - 1); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 234 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 243 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 235 | 244 |
| 236 if (rv == ERR_IO_PENDING) | 245 if (rv == ERR_IO_PENDING) |
| 237 rv = callback.WaitForResult(); | 246 rv = callback.WaitForResult(); |
| 238 | 247 |
| 239 EXPECT_NE(0, rv); | 248 EXPECT_NE(0, rv); |
| 240 } | 249 } |
| 241 | 250 |
| 242 TEST_F(TCPClientSocketTest, DISABLED_FullDuplex_ReadFirst) { | 251 TEST_F(TCPClientSocketTest, DISABLED_FullDuplex_ReadFirst) { |
| 243 TestCompletionCallback callback; | 252 TestCompletionCallback callback; |
| 244 int rv = sock_->Connect(&callback); | 253 int rv = sock_->Connect(&callback, NULL); |
| 245 if (rv != OK) { | 254 if (rv != OK) { |
| 246 ASSERT_EQ(rv, ERR_IO_PENDING); | 255 ASSERT_EQ(rv, ERR_IO_PENDING); |
| 247 | 256 |
| 248 rv = callback.WaitForResult(); | 257 rv = callback.WaitForResult(); |
| 249 EXPECT_EQ(rv, OK); | 258 EXPECT_EQ(rv, OK); |
| 250 } | 259 } |
| 251 | 260 |
| 252 // Read first. There's no data, so it should return ERR_IO_PENDING. | 261 // Read first. There's no data, so it should return ERR_IO_PENDING. |
| 253 const int kBufLen = 4096; | 262 const int kBufLen = 4096; |
| 254 scoped_refptr<IOBuffer> buf = new IOBuffer(kBufLen); | 263 scoped_refptr<IOBuffer> buf = new IOBuffer(kBufLen); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 276 // At this point, both read and write have returned ERR_IO_PENDING, and the | 285 // At this point, both read and write have returned ERR_IO_PENDING, and the |
| 277 // write callback has executed. We wait for the read callback to run now to | 286 // write callback has executed. We wait for the read callback to run now to |
| 278 // make sure that the socket can handle full duplex communications. | 287 // make sure that the socket can handle full duplex communications. |
| 279 | 288 |
| 280 rv = callback.WaitForResult(); | 289 rv = callback.WaitForResult(); |
| 281 EXPECT_GE(rv, 0); | 290 EXPECT_GE(rv, 0); |
| 282 } | 291 } |
| 283 | 292 |
| 284 TEST_F(TCPClientSocketTest, DISABLED_FullDuplex_WriteFirst) { | 293 TEST_F(TCPClientSocketTest, DISABLED_FullDuplex_WriteFirst) { |
| 285 TestCompletionCallback callback; | 294 TestCompletionCallback callback; |
| 286 int rv = sock_->Connect(&callback); | 295 int rv = sock_->Connect(&callback, NULL); |
| 287 if (rv != OK) { | 296 if (rv != OK) { |
| 288 ASSERT_EQ(ERR_IO_PENDING, rv); | 297 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 289 | 298 |
| 290 rv = callback.WaitForResult(); | 299 rv = callback.WaitForResult(); |
| 291 EXPECT_EQ(OK, rv); | 300 EXPECT_EQ(OK, rv); |
| 292 } | 301 } |
| 293 | 302 |
| 294 PauseServerReads(); | 303 PauseServerReads(); |
| 295 const int kWriteBufLen = 64 * 1024; | 304 const int kWriteBufLen = 64 * 1024; |
| 296 scoped_refptr<IOBuffer> request_buffer = new IOBuffer(kWriteBufLen); | 305 scoped_refptr<IOBuffer> request_buffer = new IOBuffer(kWriteBufLen); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 // Close the server socket, so there will at least be a 0-byte read. | 339 // Close the server socket, so there will at least be a 0-byte read. |
| 331 CloseServerSocket(); | 340 CloseServerSocket(); |
| 332 | 341 |
| 333 rv = callback.WaitForResult(); | 342 rv = callback.WaitForResult(); |
| 334 EXPECT_GE(rv, 0); | 343 EXPECT_GE(rv, 0); |
| 335 } | 344 } |
| 336 | 345 |
| 337 } // namespace | 346 } // namespace |
| 338 | 347 |
| 339 } // namespace net | 348 } // namespace net |
| OLD | NEW |