| 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 "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 "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
| 11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/base/net_log.h" | 13 #include "net/base/net_log.h" |
| 14 #include "net/base/net_log_unittest.h" | 14 #include "net/base/net_log_unittest.h" |
| 15 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
| 16 #include "net/base/winsock_init.h" | 16 #include "net/base/winsock_init.h" |
| 17 #include "net/dns/mock_host_resolver.h" | 17 #include "net/dns/mock_host_resolver.h" |
| 18 #include "net/socket/client_socket_factory.h" | 18 #include "net/socket/client_socket_factory.h" |
| 19 #include "net/socket/tcp_listen_socket.h" | 19 #include "net/socket/tcp_listen_socket.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "testing/platform_test.h" | 21 #include "testing/platform_test.h" |
| 22 | 22 |
| 23 namespace net { | 23 namespace net { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const char kServerReply[] = "HTTP/1.1 404 Not Found"; | 27 const char kServerReply[] = "HTTP/1.1 404 Not Found"; |
| 28 | 28 |
| 29 enum ClientSocketTestTypes { | 29 enum ClientSocketTestTypes { TCP, SCTP }; |
| 30 TCP, | |
| 31 SCTP | |
| 32 }; | |
| 33 | 30 |
| 34 } // namespace | 31 } // namespace |
| 35 | 32 |
| 36 class TransportClientSocketTest | 33 class TransportClientSocketTest |
| 37 : public StreamListenSocket::Delegate, | 34 : public StreamListenSocket::Delegate, |
| 38 public ::testing::TestWithParam<ClientSocketTestTypes> { | 35 public ::testing::TestWithParam<ClientSocketTestTypes> { |
| 39 public: | 36 public: |
| 40 TransportClientSocketTest() | 37 TransportClientSocketTest() |
| 41 : listen_port_(0), | 38 : listen_port_(0), |
| 42 socket_factory_(ClientSocketFactory::GetDefaultFactory()), | 39 socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
| 43 close_server_socket_on_next_send_(false) { | 40 close_server_socket_on_next_send_(false) {} |
| 44 } | |
| 45 | 41 |
| 46 virtual ~TransportClientSocketTest() { | 42 virtual ~TransportClientSocketTest() {} |
| 47 } | |
| 48 | 43 |
| 49 // Implement StreamListenSocket::Delegate methods | 44 // Implement StreamListenSocket::Delegate methods |
| 50 virtual void DidAccept(StreamListenSocket* server, | 45 virtual void DidAccept(StreamListenSocket* server, |
| 51 scoped_ptr<StreamListenSocket> connection) OVERRIDE { | 46 scoped_ptr<StreamListenSocket> connection) OVERRIDE { |
| 52 connected_sock_.reset( | 47 connected_sock_.reset(static_cast<TCPListenSocket*>(connection.release())); |
| 53 static_cast<TCPListenSocket*>(connection.release())); | |
| 54 } | 48 } |
| 55 virtual void DidRead(StreamListenSocket*, const char* str, int len) OVERRIDE { | 49 virtual void DidRead(StreamListenSocket*, const char* str, int len) OVERRIDE { |
| 56 // TODO(dkegel): this might not be long enough to tickle some bugs. | 50 // TODO(dkegel): this might not be long enough to tickle some bugs. |
| 57 connected_sock_->Send(kServerReply, arraysize(kServerReply) - 1, | 51 connected_sock_->Send(kServerReply, |
| 52 arraysize(kServerReply) - 1, |
| 58 false /* Don't append line feed */); | 53 false /* Don't append line feed */); |
| 59 if (close_server_socket_on_next_send_) | 54 if (close_server_socket_on_next_send_) |
| 60 CloseServerSocket(); | 55 CloseServerSocket(); |
| 61 } | 56 } |
| 62 virtual void DidClose(StreamListenSocket* sock) OVERRIDE {} | 57 virtual void DidClose(StreamListenSocket* sock) OVERRIDE {} |
| 63 | 58 |
| 64 // Testcase hooks | 59 // Testcase hooks |
| 65 virtual void SetUp(); | 60 virtual void SetUp(); |
| 66 | 61 |
| 67 void CloseServerSocket() { | 62 void CloseServerSocket() { |
| 68 // delete the connected_sock_, which will close it. | 63 // delete the connected_sock_, which will close it. |
| 69 connected_sock_.reset(); | 64 connected_sock_.reset(); |
| 70 } | 65 } |
| 71 | 66 |
| 72 void PauseServerReads() { | 67 void PauseServerReads() { connected_sock_->PauseReads(); } |
| 73 connected_sock_->PauseReads(); | |
| 74 } | |
| 75 | 68 |
| 76 void ResumeServerReads() { | 69 void ResumeServerReads() { connected_sock_->ResumeReads(); } |
| 77 connected_sock_->ResumeReads(); | |
| 78 } | |
| 79 | 70 |
| 80 int DrainClientSocket(IOBuffer* buf, | 71 int DrainClientSocket(IOBuffer* buf, |
| 81 uint32 buf_len, | 72 uint32 buf_len, |
| 82 uint32 bytes_to_read, | 73 uint32 bytes_to_read, |
| 83 TestCompletionCallback* callback); | 74 TestCompletionCallback* callback); |
| 84 | 75 |
| 85 void SendClientRequest(); | 76 void SendClientRequest(); |
| 86 | 77 |
| 87 void set_close_server_socket_on_next_send(bool close) { | 78 void set_close_server_socket_on_next_send(bool close) { |
| 88 close_server_socket_on_next_send_ = close; | 79 close_server_socket_on_next_send_ = close; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 AddressList addr; | 115 AddressList addr; |
| 125 // MockHostResolver resolves everything to 127.0.0.1. | 116 // MockHostResolver resolves everything to 127.0.0.1. |
| 126 scoped_ptr<HostResolver> resolver(new MockHostResolver()); | 117 scoped_ptr<HostResolver> resolver(new MockHostResolver()); |
| 127 HostResolver::RequestInfo info(HostPortPair("localhost", listen_port_)); | 118 HostResolver::RequestInfo info(HostPortPair("localhost", listen_port_)); |
| 128 TestCompletionCallback callback; | 119 TestCompletionCallback callback; |
| 129 int rv = resolver->Resolve( | 120 int rv = resolver->Resolve( |
| 130 info, DEFAULT_PRIORITY, &addr, callback.callback(), NULL, BoundNetLog()); | 121 info, DEFAULT_PRIORITY, &addr, callback.callback(), NULL, BoundNetLog()); |
| 131 CHECK_EQ(ERR_IO_PENDING, rv); | 122 CHECK_EQ(ERR_IO_PENDING, rv); |
| 132 rv = callback.WaitForResult(); | 123 rv = callback.WaitForResult(); |
| 133 CHECK_EQ(rv, OK); | 124 CHECK_EQ(rv, OK); |
| 134 sock_ = | 125 sock_ = socket_factory_->CreateTransportClientSocket( |
| 135 socket_factory_->CreateTransportClientSocket(addr, | 126 addr, &net_log_, NetLog::Source()); |
| 136 &net_log_, | |
| 137 NetLog::Source()); | |
| 138 } | 127 } |
| 139 | 128 |
| 140 int TransportClientSocketTest::DrainClientSocket( | 129 int TransportClientSocketTest::DrainClientSocket( |
| 141 IOBuffer* buf, uint32 buf_len, | 130 IOBuffer* buf, |
| 142 uint32 bytes_to_read, TestCompletionCallback* callback) { | 131 uint32 buf_len, |
| 132 uint32 bytes_to_read, |
| 133 TestCompletionCallback* callback) { |
| 143 int rv = OK; | 134 int rv = OK; |
| 144 uint32 bytes_read = 0; | 135 uint32 bytes_read = 0; |
| 145 | 136 |
| 146 while (bytes_read < bytes_to_read) { | 137 while (bytes_read < bytes_to_read) { |
| 147 rv = sock_->Read(buf, buf_len, callback->callback()); | 138 rv = sock_->Read(buf, buf_len, callback->callback()); |
| 148 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 139 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 149 | 140 |
| 150 if (rv == ERR_IO_PENDING) | 141 if (rv == ERR_IO_PENDING) |
| 151 rv = callback->WaitForResult(); | 142 rv = callback->WaitForResult(); |
| 152 | 143 |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 | 432 |
| 442 // It's possible the read is blocked because it's already read all the data. | 433 // 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. | 434 // Close the server socket, so there will at least be a 0-byte read. |
| 444 CloseServerSocket(); | 435 CloseServerSocket(); |
| 445 | 436 |
| 446 rv = callback.WaitForResult(); | 437 rv = callback.WaitForResult(); |
| 447 EXPECT_GE(rv, 0); | 438 EXPECT_GE(rv, 0); |
| 448 } | 439 } |
| 449 | 440 |
| 450 } // namespace net | 441 } // namespace net |
| OLD | NEW |