| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <memory> | 5 #include <memory> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "net/base/address_list.h" | 11 #include "net/base/address_list.h" |
| 12 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 13 #include "net/base/ip_address.h" | 13 #include "net/base/ip_address.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
| 16 #include "net/dns/mock_host_resolver.h" | 16 #include "net/dns/mock_host_resolver.h" |
| 17 #include "net/log/net_log.h" | 17 #include "net/log/net_log.h" |
| 18 #include "net/log/test_net_log.h" | 18 #include "net/log/test_net_log.h" |
| 19 #include "net/log/test_net_log_entry.h" | 19 #include "net/log/test_net_log_entry.h" |
| 20 #include "net/log/test_net_log_util.h" | 20 #include "net/log/test_net_log_util.h" |
| 21 #include "net/socket/client_socket_factory.h" | 21 #include "net/socket/client_socket_factory.h" |
| 22 #include "net/socket/tcp_client_socket.h" | 22 #include "net/socket/tcp_client_socket.h" |
| 23 #include "net/socket/tcp_server_socket.h" | 23 #include "net/socket/tcp_server_socket.h" |
| 24 #include "net/test/gtest_util.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "testing/platform_test.h" | 27 #include "testing/platform_test.h" |
| 26 | 28 |
| 29 using net::test::IsError; |
| 30 using net::test::IsOk; |
| 31 |
| 27 namespace net { | 32 namespace net { |
| 28 | 33 |
| 29 namespace { | 34 namespace { |
| 30 | 35 |
| 31 const char kServerReply[] = "HTTP/1.1 404 Not Found"; | 36 const char kServerReply[] = "HTTP/1.1 404 Not Found"; |
| 32 | 37 |
| 33 enum ClientSocketTestTypes { TCP, SCTP }; | 38 enum ClientSocketTestTypes { TCP, SCTP }; |
| 34 | 39 |
| 35 } // namespace | 40 } // namespace |
| 36 | 41 |
| 37 class TransportClientSocketTest | 42 class TransportClientSocketTest |
| 38 : public ::testing::TestWithParam<ClientSocketTestTypes> { | 43 : public ::testing::TestWithParam<ClientSocketTestTypes> { |
| 39 public: | 44 public: |
| 40 TransportClientSocketTest() | 45 TransportClientSocketTest() |
| 41 : listen_port_(0), | 46 : listen_port_(0), |
| 42 socket_factory_(ClientSocketFactory::GetDefaultFactory()), | 47 socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
| 43 close_server_socket_on_next_send_(false) {} | 48 close_server_socket_on_next_send_(false) {} |
| 44 | 49 |
| 45 virtual ~TransportClientSocketTest() {} | 50 virtual ~TransportClientSocketTest() {} |
| 46 | 51 |
| 47 // Testcase hooks | 52 // Testcase hooks |
| 48 void SetUp() override; | 53 void SetUp() override; |
| 49 | 54 |
| 50 void CloseServerSocket() { | 55 void CloseServerSocket() { |
| 51 // delete the connected_sock_, which will close it. | 56 // delete the connected_sock_, which will close it. |
| 52 connected_sock_.reset(); | 57 connected_sock_.reset(); |
| 53 } | 58 } |
| 54 | 59 |
| 55 void AcceptCallback(int res) { | 60 void AcceptCallback(int res) { |
| 56 ASSERT_EQ(OK, res); | 61 ASSERT_THAT(res, IsOk()); |
| 57 connect_loop_.Quit(); | 62 connect_loop_.Quit(); |
| 58 } | 63 } |
| 59 | 64 |
| 60 int DrainClientSocket(IOBuffer* buf, | 65 int DrainClientSocket(IOBuffer* buf, |
| 61 uint32_t buf_len, | 66 uint32_t buf_len, |
| 62 uint32_t bytes_to_read, | 67 uint32_t bytes_to_read, |
| 63 TestCompletionCallback* callback); | 68 TestCompletionCallback* callback); |
| 64 | 69 |
| 65 // Establishes a connection to the server. | 70 // Establishes a connection to the server. |
| 66 void EstablishConnection(TestCompletionCallback* callback); | 71 void EstablishConnection(TestCompletionCallback* callback); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 92 std::unique_ptr<TCPServerSocket> listen_sock_; | 97 std::unique_ptr<TCPServerSocket> listen_sock_; |
| 93 bool close_server_socket_on_next_send_; | 98 bool close_server_socket_on_next_send_; |
| 94 }; | 99 }; |
| 95 | 100 |
| 96 void TransportClientSocketTest::SetUp() { | 101 void TransportClientSocketTest::SetUp() { |
| 97 ::testing::TestWithParam<ClientSocketTestTypes>::SetUp(); | 102 ::testing::TestWithParam<ClientSocketTestTypes>::SetUp(); |
| 98 | 103 |
| 99 // Open a server socket on an ephemeral port. | 104 // Open a server socket on an ephemeral port. |
| 100 listen_sock_.reset(new TCPServerSocket(NULL, NetLog::Source())); | 105 listen_sock_.reset(new TCPServerSocket(NULL, NetLog::Source())); |
| 101 IPEndPoint local_address(IPAddress::IPv4Localhost(), 0); | 106 IPEndPoint local_address(IPAddress::IPv4Localhost(), 0); |
| 102 ASSERT_EQ(OK, listen_sock_->Listen(local_address, 1)); | 107 ASSERT_THAT(listen_sock_->Listen(local_address, 1), IsOk()); |
| 103 // Get the server's address (including the actual port number). | 108 // Get the server's address (including the actual port number). |
| 104 ASSERT_EQ(OK, listen_sock_->GetLocalAddress(&local_address)); | 109 ASSERT_THAT(listen_sock_->GetLocalAddress(&local_address), IsOk()); |
| 105 listen_port_ = local_address.port(); | 110 listen_port_ = local_address.port(); |
| 106 listen_sock_->Accept(&connected_sock_, | 111 listen_sock_->Accept(&connected_sock_, |
| 107 base::Bind(&TransportClientSocketTest::AcceptCallback, | 112 base::Bind(&TransportClientSocketTest::AcceptCallback, |
| 108 base::Unretained(this))); | 113 base::Unretained(this))); |
| 109 | 114 |
| 110 AddressList addr; | 115 AddressList addr; |
| 111 // MockHostResolver resolves everything to 127.0.0.1. | 116 // MockHostResolver resolves everything to 127.0.0.1. |
| 112 std::unique_ptr<HostResolver> resolver(new MockHostResolver()); | 117 std::unique_ptr<HostResolver> resolver(new MockHostResolver()); |
| 113 HostResolver::RequestInfo info(HostPortPair("localhost", listen_port_)); | 118 HostResolver::RequestInfo info(HostPortPair("localhost", listen_port_)); |
| 114 TestCompletionCallback callback; | 119 TestCompletionCallback callback; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 139 | 144 |
| 140 return static_cast<int>(bytes_read); | 145 return static_cast<int>(bytes_read); |
| 141 } | 146 } |
| 142 | 147 |
| 143 void TransportClientSocketTest::EstablishConnection( | 148 void TransportClientSocketTest::EstablishConnection( |
| 144 TestCompletionCallback* callback) { | 149 TestCompletionCallback* callback) { |
| 145 int rv = sock_->Connect(callback->callback()); | 150 int rv = sock_->Connect(callback->callback()); |
| 146 // Wait for |listen_sock_| to accept a connection. | 151 // Wait for |listen_sock_| to accept a connection. |
| 147 connect_loop_.Run(); | 152 connect_loop_.Run(); |
| 148 // Now wait for the client socket to accept the connection. | 153 // Now wait for the client socket to accept the connection. |
| 149 EXPECT_EQ(OK, callback->GetResult(rv)); | 154 EXPECT_THAT(callback->GetResult(rv), IsOk()); |
| 150 } | 155 } |
| 151 | 156 |
| 152 void TransportClientSocketTest::SendRequestAndResponse() { | 157 void TransportClientSocketTest::SendRequestAndResponse() { |
| 153 // Send client request. | 158 // Send client request. |
| 154 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 159 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 155 int request_len = strlen(request_text); | 160 int request_len = strlen(request_text); |
| 156 scoped_refptr<DrainableIOBuffer> request_buffer( | 161 scoped_refptr<DrainableIOBuffer> request_buffer( |
| 157 new DrainableIOBuffer(new IOBuffer(request_len), request_len)); | 162 new DrainableIOBuffer(new IOBuffer(request_len), request_len)); |
| 158 memcpy(request_buffer->data(), request_text, request_len); | 163 memcpy(request_buffer->data(), request_text, request_len); |
| 159 | 164 |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 330 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
| 326 uint32_t bytes_read = | 331 uint32_t bytes_read = |
| 327 DrainClientSocket(buf.get(), 4096, strlen(kServerReply), &callback); | 332 DrainClientSocket(buf.get(), 4096, strlen(kServerReply), &callback); |
| 328 ASSERT_EQ(bytes_read, strlen(kServerReply)); | 333 ASSERT_EQ(bytes_read, strlen(kServerReply)); |
| 329 ASSERT_EQ(std::string(kServerReply), std::string(buf->data(), bytes_read)); | 334 ASSERT_EQ(std::string(kServerReply), std::string(buf->data(), bytes_read)); |
| 330 | 335 |
| 331 // All data has been read now. Read once more to force an ERR_IO_PENDING, and | 336 // All data has been read now. Read once more to force an ERR_IO_PENDING, and |
| 332 // then close the server socket, and note the close. | 337 // then close the server socket, and note the close. |
| 333 | 338 |
| 334 int rv = sock_->Read(buf.get(), 4096, callback.callback()); | 339 int rv = sock_->Read(buf.get(), 4096, callback.callback()); |
| 335 ASSERT_EQ(ERR_IO_PENDING, rv); | 340 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 336 CloseServerSocket(); | 341 CloseServerSocket(); |
| 337 EXPECT_EQ(0, callback.WaitForResult()); | 342 EXPECT_EQ(0, callback.WaitForResult()); |
| 338 } | 343 } |
| 339 | 344 |
| 340 TEST_P(TransportClientSocketTest, Read_SmallChunks) { | 345 TEST_P(TransportClientSocketTest, Read_SmallChunks) { |
| 341 TestCompletionCallback callback; | 346 TestCompletionCallback callback; |
| 342 EstablishConnection(&callback); | 347 EstablishConnection(&callback); |
| 343 | 348 |
| 344 SendRequestAndResponse(); | 349 SendRequestAndResponse(); |
| 345 | 350 |
| 346 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); | 351 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); |
| 347 uint32_t bytes_read = 0; | 352 uint32_t bytes_read = 0; |
| 348 while (bytes_read < strlen(kServerReply)) { | 353 while (bytes_read < strlen(kServerReply)) { |
| 349 int rv = sock_->Read(buf.get(), 1, callback.callback()); | 354 int rv = sock_->Read(buf.get(), 1, callback.callback()); |
| 350 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 355 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 351 | 356 |
| 352 rv = callback.GetResult(rv); | 357 rv = callback.GetResult(rv); |
| 353 | 358 |
| 354 ASSERT_EQ(1, rv); | 359 ASSERT_EQ(1, rv); |
| 355 bytes_read += rv; | 360 bytes_read += rv; |
| 356 } | 361 } |
| 357 | 362 |
| 358 // All data has been read now. Read once more to force an ERR_IO_PENDING, and | 363 // All data has been read now. Read once more to force an ERR_IO_PENDING, and |
| 359 // then close the server socket, and note the close. | 364 // then close the server socket, and note the close. |
| 360 | 365 |
| 361 int rv = sock_->Read(buf.get(), 1, callback.callback()); | 366 int rv = sock_->Read(buf.get(), 1, callback.callback()); |
| 362 ASSERT_EQ(ERR_IO_PENDING, rv); | 367 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 363 CloseServerSocket(); | 368 CloseServerSocket(); |
| 364 EXPECT_EQ(0, callback.WaitForResult()); | 369 EXPECT_EQ(0, callback.WaitForResult()); |
| 365 } | 370 } |
| 366 | 371 |
| 367 TEST_P(TransportClientSocketTest, Read_Interrupted) { | 372 TEST_P(TransportClientSocketTest, Read_Interrupted) { |
| 368 TestCompletionCallback callback; | 373 TestCompletionCallback callback; |
| 369 EstablishConnection(&callback); | 374 EstablishConnection(&callback); |
| 370 | 375 |
| 371 SendRequestAndResponse(); | 376 SendRequestAndResponse(); |
| 372 | 377 |
| 373 // Do a partial read and then exit. This test should not crash! | 378 // Do a partial read and then exit. This test should not crash! |
| 374 scoped_refptr<IOBuffer> buf(new IOBuffer(16)); | 379 scoped_refptr<IOBuffer> buf(new IOBuffer(16)); |
| 375 int rv = sock_->Read(buf.get(), 16, callback.callback()); | 380 int rv = sock_->Read(buf.get(), 16, callback.callback()); |
| 376 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 381 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 377 | 382 |
| 378 rv = callback.GetResult(rv); | 383 rv = callback.GetResult(rv); |
| 379 | 384 |
| 380 EXPECT_NE(0, rv); | 385 EXPECT_NE(0, rv); |
| 381 } | 386 } |
| 382 | 387 |
| 383 TEST_P(TransportClientSocketTest, FullDuplex_ReadFirst) { | 388 TEST_P(TransportClientSocketTest, FullDuplex_ReadFirst) { |
| 384 TestCompletionCallback callback; | 389 TestCompletionCallback callback; |
| 385 EstablishConnection(&callback); | 390 EstablishConnection(&callback); |
| 386 | 391 |
| 387 // Read first. There's no data, so it should return ERR_IO_PENDING. | 392 // Read first. There's no data, so it should return ERR_IO_PENDING. |
| 388 const int kBufLen = 4096; | 393 const int kBufLen = 4096; |
| 389 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); | 394 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); |
| 390 int rv = sock_->Read(buf.get(), kBufLen, callback.callback()); | 395 int rv = sock_->Read(buf.get(), kBufLen, callback.callback()); |
| 391 EXPECT_EQ(ERR_IO_PENDING, rv); | 396 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 392 | 397 |
| 393 const int kWriteBufLen = 64 * 1024; | 398 const int kWriteBufLen = 64 * 1024; |
| 394 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); | 399 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); |
| 395 char* request_data = request_buffer->data(); | 400 char* request_data = request_buffer->data(); |
| 396 memset(request_data, 'A', kWriteBufLen); | 401 memset(request_data, 'A', kWriteBufLen); |
| 397 TestCompletionCallback write_callback; | 402 TestCompletionCallback write_callback; |
| 398 | 403 |
| 399 int bytes_written = 0; | 404 int bytes_written = 0; |
| 400 while (true) { | 405 while (true) { |
| 401 rv = sock_->Write(request_buffer.get(), kWriteBufLen, | 406 rv = sock_->Write(request_buffer.get(), kWriteBufLen, |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 | 468 |
| 464 // It's possible the read is blocked because it's already read all the data. | 469 // It's possible the read is blocked because it's already read all the data. |
| 465 // Close the server socket, so there will at least be a 0-byte read. | 470 // Close the server socket, so there will at least be a 0-byte read. |
| 466 CloseServerSocket(); | 471 CloseServerSocket(); |
| 467 | 472 |
| 468 rv = callback.WaitForResult(); | 473 rv = callback.WaitForResult(); |
| 469 EXPECT_GE(rv, 0); | 474 EXPECT_GE(rv, 0); |
| 470 } | 475 } |
| 471 | 476 |
| 472 } // namespace net | 477 } // namespace net |
| OLD | NEW |