| 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" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 : listen_port_(0), | 41 : listen_port_(0), |
| 42 socket_factory_(ClientSocketFactory::GetDefaultFactory()), | 42 socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
| 43 close_server_socket_on_next_send_(false) { | 43 close_server_socket_on_next_send_(false) { |
| 44 } | 44 } |
| 45 | 45 |
| 46 virtual ~TransportClientSocketTest() { | 46 virtual ~TransportClientSocketTest() { |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Implement StreamListenSocket::Delegate methods | 49 // Implement StreamListenSocket::Delegate methods |
| 50 virtual void DidAccept(StreamListenSocket* server, | 50 virtual void DidAccept(StreamListenSocket* server, |
| 51 StreamListenSocket* connection) OVERRIDE { | 51 scoped_ptr<StreamListenSocket> connection) OVERRIDE { |
| 52 connected_sock_ = reinterpret_cast<TCPListenSocket*>(connection); | 52 connected_sock_.reset( |
| 53 static_cast<TCPListenSocket*>(connection.release())); |
| 53 } | 54 } |
| 54 virtual void DidRead(StreamListenSocket*, const char* str, int len) OVERRIDE { | 55 virtual void DidRead(StreamListenSocket*, const char* str, int len) OVERRIDE { |
| 55 // TODO(dkegel): this might not be long enough to tickle some bugs. | 56 // TODO(dkegel): this might not be long enough to tickle some bugs. |
| 56 connected_sock_->Send(kServerReply, arraysize(kServerReply) - 1, | 57 connected_sock_->Send(kServerReply, arraysize(kServerReply) - 1, |
| 57 false /* Don't append line feed */); | 58 false /* Don't append line feed */); |
| 58 if (close_server_socket_on_next_send_) | 59 if (close_server_socket_on_next_send_) |
| 59 CloseServerSocket(); | 60 CloseServerSocket(); |
| 60 } | 61 } |
| 61 virtual void DidClose(StreamListenSocket* sock) OVERRIDE {} | 62 virtual void DidClose(StreamListenSocket* sock) OVERRIDE {} |
| 62 | 63 |
| 63 // Testcase hooks | 64 // Testcase hooks |
| 64 virtual void SetUp(); | 65 virtual void SetUp(); |
| 65 | 66 |
| 66 void CloseServerSocket() { | 67 void CloseServerSocket() { |
| 67 // delete the connected_sock_, which will close it. | 68 // delete the connected_sock_, which will close it. |
| 68 connected_sock_ = NULL; | 69 connected_sock_.reset(); |
| 69 } | 70 } |
| 70 | 71 |
| 71 void PauseServerReads() { | 72 void PauseServerReads() { |
| 72 connected_sock_->PauseReads(); | 73 connected_sock_->PauseReads(); |
| 73 } | 74 } |
| 74 | 75 |
| 75 void ResumeServerReads() { | 76 void ResumeServerReads() { |
| 76 connected_sock_->ResumeReads(); | 77 connected_sock_->ResumeReads(); |
| 77 } | 78 } |
| 78 | 79 |
| 79 int DrainClientSocket(IOBuffer* buf, | 80 int DrainClientSocket(IOBuffer* buf, |
| 80 uint32 buf_len, | 81 uint32 buf_len, |
| 81 uint32 bytes_to_read, | 82 uint32 bytes_to_read, |
| 82 TestCompletionCallback* callback); | 83 TestCompletionCallback* callback); |
| 83 | 84 |
| 84 void SendClientRequest(); | 85 void SendClientRequest(); |
| 85 | 86 |
| 86 void set_close_server_socket_on_next_send(bool close) { | 87 void set_close_server_socket_on_next_send(bool close) { |
| 87 close_server_socket_on_next_send_ = close; | 88 close_server_socket_on_next_send_ = close; |
| 88 } | 89 } |
| 89 | 90 |
| 90 protected: | 91 protected: |
| 91 int listen_port_; | 92 int listen_port_; |
| 92 CapturingNetLog net_log_; | 93 CapturingNetLog net_log_; |
| 93 ClientSocketFactory* const socket_factory_; | 94 ClientSocketFactory* const socket_factory_; |
| 94 scoped_ptr<StreamSocket> sock_; | 95 scoped_ptr<StreamSocket> sock_; |
| 95 | 96 |
| 96 private: | 97 private: |
| 97 scoped_refptr<TCPListenSocket> listen_sock_; | 98 scoped_ptr<TCPListenSocket> listen_sock_; |
| 98 scoped_refptr<TCPListenSocket> connected_sock_; | 99 scoped_ptr<TCPListenSocket> connected_sock_; |
| 99 bool close_server_socket_on_next_send_; | 100 bool close_server_socket_on_next_send_; |
| 100 }; | 101 }; |
| 101 | 102 |
| 102 void TransportClientSocketTest::SetUp() { | 103 void TransportClientSocketTest::SetUp() { |
| 103 ::testing::TestWithParam<ClientSocketTestTypes>::SetUp(); | 104 ::testing::TestWithParam<ClientSocketTestTypes>::SetUp(); |
| 104 | 105 |
| 105 // Find a free port to listen on | 106 // Find a free port to listen on |
| 106 scoped_refptr<TCPListenSocket> sock; | 107 scoped_ptr<TCPListenSocket> sock; |
| 107 int port; | 108 int port; |
| 108 // Range of ports to listen on. Shouldn't need to try many. | 109 // Range of ports to listen on. Shouldn't need to try many. |
| 109 const int kMinPort = 10100; | 110 const int kMinPort = 10100; |
| 110 const int kMaxPort = 10200; | 111 const int kMaxPort = 10200; |
| 111 #if defined(OS_WIN) | 112 #if defined(OS_WIN) |
| 112 EnsureWinsockInit(); | 113 EnsureWinsockInit(); |
| 113 #endif | 114 #endif |
| 114 for (port = kMinPort; port < kMaxPort; port++) { | 115 for (port = kMinPort; port < kMaxPort; port++) { |
| 115 sock = TCPListenSocket::CreateAndListen("127.0.0.1", port, this); | 116 sock = TCPListenSocket::CreateAndListen("127.0.0.1", port, this); |
| 116 if (sock.get()) | 117 if (sock.get()) |
| 117 break; | 118 break; |
| 118 } | 119 } |
| 119 ASSERT_TRUE(sock.get() != NULL); | 120 ASSERT_TRUE(sock.get() != NULL); |
| 120 listen_sock_ = sock; | 121 listen_sock_ = sock.Pass(); |
| 121 listen_port_ = port; | 122 listen_port_ = port; |
| 122 | 123 |
| 123 AddressList addr; | 124 AddressList addr; |
| 124 // MockHostResolver resolves everything to 127.0.0.1. | 125 // MockHostResolver resolves everything to 127.0.0.1. |
| 125 scoped_ptr<HostResolver> resolver(new MockHostResolver()); | 126 scoped_ptr<HostResolver> resolver(new MockHostResolver()); |
| 126 HostResolver::RequestInfo info(HostPortPair("localhost", listen_port_)); | 127 HostResolver::RequestInfo info(HostPortPair("localhost", listen_port_)); |
| 127 TestCompletionCallback callback; | 128 TestCompletionCallback callback; |
| 128 int rv = resolver->Resolve( | 129 int rv = resolver->Resolve( |
| 129 info, DEFAULT_PRIORITY, &addr, callback.callback(), NULL, BoundNetLog()); | 130 info, DEFAULT_PRIORITY, &addr, callback.callback(), NULL, BoundNetLog()); |
| 130 CHECK_EQ(ERR_IO_PENDING, rv); | 131 CHECK_EQ(ERR_IO_PENDING, rv); |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 | 441 |
| 441 // It's possible the read is blocked because it's already read all the data. | 442 // It's possible the read is blocked because it's already read all the data. |
| 442 // Close the server socket, so there will at least be a 0-byte read. | 443 // Close the server socket, so there will at least be a 0-byte read. |
| 443 CloseServerSocket(); | 444 CloseServerSocket(); |
| 444 | 445 |
| 445 rv = callback.WaitForResult(); | 446 rv = callback.WaitForResult(); |
| 446 EXPECT_GE(rv, 0); | 447 EXPECT_GE(rv, 0); |
| 447 } | 448 } |
| 448 | 449 |
| 449 } // namespace net | 450 } // namespace net |
| OLD | NEW |