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 <string> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" |
8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/run_loop.h" |
10 #include "net/base/address_list.h" | 12 #include "net/base/address_list.h" |
11 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.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" | |
15 #include "net/dns/mock_host_resolver.h" | 16 #include "net/dns/mock_host_resolver.h" |
16 #include "net/log/net_log.h" | 17 #include "net/log/net_log.h" |
17 #include "net/log/net_log_unittest.h" | 18 #include "net/log/net_log_unittest.h" |
18 #include "net/socket/client_socket_factory.h" | 19 #include "net/socket/client_socket_factory.h" |
19 #include "net/socket/tcp_listen_socket.h" | 20 #include "net/socket/tcp_client_socket.h" |
| 21 #include "net/socket/tcp_server_socket.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "testing/platform_test.h" | 23 #include "testing/platform_test.h" |
22 | 24 |
23 namespace net { | 25 namespace net { |
24 | 26 |
25 namespace { | 27 namespace { |
26 | 28 |
27 const char kServerReply[] = "HTTP/1.1 404 Not Found"; | 29 const char kServerReply[] = "HTTP/1.1 404 Not Found"; |
28 | 30 |
29 enum ClientSocketTestTypes { | 31 enum ClientSocketTestTypes { TCP, SCTP }; |
30 TCP, | |
31 SCTP | |
32 }; | |
33 | 32 |
34 } // namespace | 33 } // namespace |
35 | 34 |
36 class TransportClientSocketTest | 35 class TransportClientSocketTest |
37 : public StreamListenSocket::Delegate, | 36 : public ::testing::TestWithParam<ClientSocketTestTypes> { |
38 public ::testing::TestWithParam<ClientSocketTestTypes> { | |
39 public: | 37 public: |
40 TransportClientSocketTest() | 38 TransportClientSocketTest() |
41 : listen_port_(0), | 39 : listen_port_(0), |
42 socket_factory_(ClientSocketFactory::GetDefaultFactory()), | 40 socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
43 close_server_socket_on_next_send_(false) { | 41 close_server_socket_on_next_send_(false) {} |
44 } | |
45 | 42 |
46 virtual ~TransportClientSocketTest() { | 43 virtual ~TransportClientSocketTest() {} |
47 } | |
48 | |
49 // Implement StreamListenSocket::Delegate methods | |
50 void DidAccept(StreamListenSocket* server, | |
51 scoped_ptr<StreamListenSocket> connection) override { | |
52 connected_sock_.reset( | |
53 static_cast<TCPListenSocket*>(connection.release())); | |
54 } | |
55 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(); | |
61 } | |
62 void DidClose(StreamListenSocket* sock) override {} | |
63 | 44 |
64 // Testcase hooks | 45 // Testcase hooks |
65 void SetUp() override; | 46 void SetUp() override; |
66 | 47 |
67 void CloseServerSocket() { | 48 void CloseServerSocket() { |
68 // delete the connected_sock_, which will close it. | 49 // delete the connected_sock_, which will close it. |
69 connected_sock_.reset(); | 50 connected_sock_.reset(); |
70 } | 51 } |
71 | 52 |
72 void PauseServerReads() { | 53 void AcceptCallback(int res) { |
73 connected_sock_->PauseReads(); | 54 ASSERT_EQ(OK, res); |
74 } | 55 connect_loop_.Quit(); |
75 | |
76 void ResumeServerReads() { | |
77 connected_sock_->ResumeReads(); | |
78 } | 56 } |
79 | 57 |
80 int DrainClientSocket(IOBuffer* buf, | 58 int DrainClientSocket(IOBuffer* buf, |
81 uint32 buf_len, | 59 uint32 buf_len, |
82 uint32 bytes_to_read, | 60 uint32 bytes_to_read, |
83 TestCompletionCallback* callback); | 61 TestCompletionCallback* callback); |
84 | 62 |
85 void SendClientRequest(); | 63 // Establishes a connection to the server. |
| 64 void EstablishConnection(TestCompletionCallback* callback); |
| 65 |
| 66 // Sends a request from the client to the server socket. Makes the server read |
| 67 // the request and send a response. |
| 68 void SendRequestAndResponse(); |
| 69 |
| 70 // Makes |connected_sock_| to read |expected_bytes_read| bytes. Returns the |
| 71 // the data read as a string. |
| 72 std::string ReadServerData(int expected_bytes_read); |
| 73 |
| 74 // Sends server response. |
| 75 void SendServerResponse(); |
86 | 76 |
87 void set_close_server_socket_on_next_send(bool close) { | 77 void set_close_server_socket_on_next_send(bool close) { |
88 close_server_socket_on_next_send_ = close; | 78 close_server_socket_on_next_send_ = close; |
89 } | 79 } |
90 | 80 |
91 protected: | 81 protected: |
| 82 base::RunLoop connect_loop_; |
92 uint16 listen_port_; | 83 uint16 listen_port_; |
93 TestNetLog net_log_; | 84 TestNetLog net_log_; |
94 ClientSocketFactory* const socket_factory_; | 85 ClientSocketFactory* const socket_factory_; |
95 scoped_ptr<StreamSocket> sock_; | 86 scoped_ptr<StreamSocket> sock_; |
| 87 scoped_ptr<StreamSocket> connected_sock_; |
96 | 88 |
97 private: | 89 private: |
98 scoped_ptr<TCPListenSocket> listen_sock_; | 90 scoped_ptr<TCPServerSocket> listen_sock_; |
99 scoped_ptr<TCPListenSocket> connected_sock_; | |
100 bool close_server_socket_on_next_send_; | 91 bool close_server_socket_on_next_send_; |
101 }; | 92 }; |
102 | 93 |
103 void TransportClientSocketTest::SetUp() { | 94 void TransportClientSocketTest::SetUp() { |
104 ::testing::TestWithParam<ClientSocketTestTypes>::SetUp(); | 95 ::testing::TestWithParam<ClientSocketTestTypes>::SetUp(); |
105 | 96 |
106 // Find a free port to listen on | 97 // Open a server socket on an ephemeral port. |
107 scoped_ptr<TCPListenSocket> sock; | 98 listen_sock_.reset(new TCPServerSocket(NULL, NetLog::Source())); |
108 uint16 port; | 99 IPAddressNumber address; |
109 // Range of ports to listen on. Shouldn't need to try many. | 100 ParseIPLiteralToNumber("127.0.0.1", &address); |
110 const uint16 kMinPort = 10100; | 101 IPEndPoint local_address(address, 0); |
111 const uint16 kMaxPort = 10200; | 102 ASSERT_EQ(OK, listen_sock_->Listen(local_address, 1)); |
112 #if defined(OS_WIN) | 103 // Get the server's address (including the actual port number). |
113 EnsureWinsockInit(); | 104 ASSERT_EQ(OK, listen_sock_->GetLocalAddress(&local_address)); |
114 #endif | 105 listen_port_ = local_address.port(); |
115 for (port = kMinPort; port < kMaxPort; port++) { | 106 listen_sock_->Accept(&connected_sock_, |
116 sock = TCPListenSocket::CreateAndListen("127.0.0.1", port, this); | 107 base::Bind(&TransportClientSocketTest::AcceptCallback, |
117 if (sock.get()) | 108 base::Unretained(this))); |
118 break; | |
119 } | |
120 ASSERT_TRUE(sock.get() != NULL); | |
121 listen_sock_ = sock.Pass(); | |
122 listen_port_ = port; | |
123 | 109 |
124 AddressList addr; | 110 AddressList addr; |
125 // MockHostResolver resolves everything to 127.0.0.1. | 111 // MockHostResolver resolves everything to 127.0.0.1. |
126 scoped_ptr<HostResolver> resolver(new MockHostResolver()); | 112 scoped_ptr<HostResolver> resolver(new MockHostResolver()); |
127 HostResolver::RequestInfo info(HostPortPair("localhost", listen_port_)); | 113 HostResolver::RequestInfo info(HostPortPair("localhost", listen_port_)); |
128 TestCompletionCallback callback; | 114 TestCompletionCallback callback; |
129 int rv = resolver->Resolve( | 115 int rv = resolver->Resolve(info, DEFAULT_PRIORITY, &addr, callback.callback(), |
130 info, DEFAULT_PRIORITY, &addr, callback.callback(), NULL, BoundNetLog()); | 116 NULL, BoundNetLog()); |
131 CHECK_EQ(ERR_IO_PENDING, rv); | 117 CHECK_EQ(ERR_IO_PENDING, rv); |
132 rv = callback.WaitForResult(); | 118 rv = callback.WaitForResult(); |
133 CHECK_EQ(rv, OK); | 119 CHECK_EQ(rv, OK); |
134 sock_ = | 120 sock_ = socket_factory_->CreateTransportClientSocket(addr, &net_log_, |
135 socket_factory_->CreateTransportClientSocket(addr, | 121 NetLog::Source()); |
136 &net_log_, | |
137 NetLog::Source()); | |
138 } | 122 } |
139 | 123 |
140 int TransportClientSocketTest::DrainClientSocket( | 124 int TransportClientSocketTest::DrainClientSocket( |
141 IOBuffer* buf, uint32 buf_len, | 125 IOBuffer* buf, |
142 uint32 bytes_to_read, TestCompletionCallback* callback) { | 126 uint32 buf_len, |
| 127 uint32 bytes_to_read, |
| 128 TestCompletionCallback* callback) { |
143 int rv = OK; | 129 int rv = OK; |
144 uint32 bytes_read = 0; | 130 uint32 bytes_read = 0; |
145 | 131 |
146 while (bytes_read < bytes_to_read) { | 132 while (bytes_read < bytes_to_read) { |
147 rv = sock_->Read(buf, buf_len, callback->callback()); | 133 rv = sock_->Read(buf, buf_len, callback->callback()); |
148 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 134 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
149 | 135 rv = callback->GetResult(rv); |
150 if (rv == ERR_IO_PENDING) | 136 EXPECT_GT(rv, 0); |
151 rv = callback->WaitForResult(); | |
152 | |
153 EXPECT_GE(rv, 0); | |
154 bytes_read += rv; | 137 bytes_read += rv; |
155 } | 138 } |
156 | 139 |
157 return static_cast<int>(bytes_read); | 140 return static_cast<int>(bytes_read); |
158 } | 141 } |
159 | 142 |
160 void TransportClientSocketTest::SendClientRequest() { | 143 void TransportClientSocketTest::EstablishConnection( |
| 144 TestCompletionCallback* callback) { |
| 145 int rv = sock_->Connect(callback->callback()); |
| 146 // Wait for |listen_sock_| to accept a connection. |
| 147 connect_loop_.Run(); |
| 148 // Now wait for the client socket to accept the connection. |
| 149 EXPECT_EQ(OK, callback->GetResult(rv)); |
| 150 } |
| 151 |
| 152 void TransportClientSocketTest::SendRequestAndResponse() { |
| 153 // Send client request. |
161 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 154 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
162 scoped_refptr<IOBuffer> request_buffer( | 155 int request_len = strlen(request_text); |
163 new IOBuffer(arraysize(request_text) - 1)); | 156 scoped_refptr<DrainableIOBuffer> request_buffer( |
164 TestCompletionCallback callback; | 157 new DrainableIOBuffer(new IOBuffer(request_len), request_len)); |
165 int rv; | 158 memcpy(request_buffer->data(), request_text, request_len); |
166 | 159 |
167 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); | 160 int bytes_written = 0; |
168 rv = sock_->Write( | 161 while (request_buffer->BytesRemaining() > 0) { |
169 request_buffer.get(), arraysize(request_text) - 1, callback.callback()); | 162 TestCompletionCallback write_callback; |
170 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 163 int write_result = |
| 164 sock_->Write(request_buffer.get(), request_buffer->BytesRemaining(), |
| 165 write_callback.callback()); |
| 166 write_result = write_callback.GetResult(write_result); |
| 167 ASSERT_GT(write_result, 0); |
| 168 ASSERT_LE(bytes_written + write_result, request_len); |
| 169 request_buffer->DidConsume(write_result); |
| 170 bytes_written += write_result; |
| 171 } |
| 172 ASSERT_EQ(request_len, bytes_written); |
171 | 173 |
172 if (rv == ERR_IO_PENDING) | 174 // Confirm that the server receives what client sent. |
173 rv = callback.WaitForResult(); | 175 std::string data_received = ReadServerData(bytes_written); |
174 EXPECT_EQ(rv, static_cast<int>(arraysize(request_text) - 1)); | 176 ASSERT_TRUE(connected_sock_->IsConnectedAndIdle()); |
| 177 ASSERT_EQ(request_text, data_received); |
| 178 |
| 179 // Write server response. |
| 180 SendServerResponse(); |
| 181 } |
| 182 |
| 183 void TransportClientSocketTest::SendServerResponse() { |
| 184 // TODO(dkegel): this might not be long enough to tickle some bugs. |
| 185 int reply_len = strlen(kServerReply); |
| 186 scoped_refptr<DrainableIOBuffer> write_buffer( |
| 187 new DrainableIOBuffer(new IOBuffer(reply_len), reply_len)); |
| 188 memcpy(write_buffer->data(), kServerReply, reply_len); |
| 189 int bytes_written = 0; |
| 190 while (write_buffer->BytesRemaining() > 0) { |
| 191 TestCompletionCallback write_callback; |
| 192 int write_result = connected_sock_->Write(write_buffer.get(), |
| 193 write_buffer->BytesRemaining(), |
| 194 write_callback.callback()); |
| 195 write_result = write_callback.GetResult(write_result); |
| 196 ASSERT_GE(write_result, 0); |
| 197 ASSERT_LE(bytes_written + write_result, reply_len); |
| 198 write_buffer->DidConsume(write_result); |
| 199 bytes_written += write_result; |
| 200 } |
| 201 if (close_server_socket_on_next_send_) |
| 202 CloseServerSocket(); |
| 203 } |
| 204 |
| 205 std::string TransportClientSocketTest::ReadServerData(int expected_bytes_read) { |
| 206 int bytes_read = 0; |
| 207 scoped_refptr<IOBufferWithSize> read_buffer( |
| 208 new IOBufferWithSize(expected_bytes_read)); |
| 209 while (bytes_read < expected_bytes_read) { |
| 210 TestCompletionCallback read_callback; |
| 211 int rv = connected_sock_->Read(read_buffer.get(), |
| 212 expected_bytes_read - bytes_read, |
| 213 read_callback.callback()); |
| 214 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 215 rv = read_callback.GetResult(rv); |
| 216 EXPECT_GE(rv, 0); |
| 217 bytes_read += rv; |
| 218 } |
| 219 EXPECT_EQ(expected_bytes_read, bytes_read); |
| 220 return std::string(read_buffer->data(), bytes_read); |
175 } | 221 } |
176 | 222 |
177 // TODO(leighton): Add SCTP to this list when it is ready. | 223 // TODO(leighton): Add SCTP to this list when it is ready. |
178 INSTANTIATE_TEST_CASE_P(StreamSocket, | 224 INSTANTIATE_TEST_CASE_P(StreamSocket, |
179 TransportClientSocketTest, | 225 TransportClientSocketTest, |
180 ::testing::Values(TCP)); | 226 ::testing::Values(TCP)); |
181 | 227 |
182 TEST_P(TransportClientSocketTest, Connect) { | 228 TEST_P(TransportClientSocketTest, Connect) { |
183 TestCompletionCallback callback; | 229 TestCompletionCallback callback; |
184 EXPECT_FALSE(sock_->IsConnected()); | 230 EXPECT_FALSE(sock_->IsConnected()); |
185 | 231 |
186 int rv = sock_->Connect(callback.callback()); | 232 int rv = sock_->Connect(callback.callback()); |
| 233 // Wait for |listen_sock_| to accept a connection. |
| 234 connect_loop_.Run(); |
187 | 235 |
188 TestNetLog::CapturedEntryList net_log_entries; | 236 TestNetLog::CapturedEntryList net_log_entries; |
189 net_log_.GetEntries(&net_log_entries); | 237 net_log_.GetEntries(&net_log_entries); |
190 EXPECT_TRUE( | 238 EXPECT_TRUE( |
191 LogContainsBeginEvent(net_log_entries, 0, NetLog::TYPE_SOCKET_ALIVE)); | 239 LogContainsBeginEvent(net_log_entries, 0, NetLog::TYPE_SOCKET_ALIVE)); |
192 EXPECT_TRUE( | 240 EXPECT_TRUE( |
193 LogContainsBeginEvent(net_log_entries, 1, NetLog::TYPE_TCP_CONNECT)); | 241 LogContainsBeginEvent(net_log_entries, 1, NetLog::TYPE_TCP_CONNECT)); |
| 242 // Now wait for the client socket to accept the connection. |
194 if (rv != OK) { | 243 if (rv != OK) { |
195 ASSERT_EQ(rv, ERR_IO_PENDING); | 244 ASSERT_EQ(rv, ERR_IO_PENDING); |
196 rv = callback.WaitForResult(); | 245 rv = callback.WaitForResult(); |
197 EXPECT_EQ(rv, OK); | 246 EXPECT_EQ(rv, OK); |
198 } | 247 } |
199 | 248 |
200 EXPECT_TRUE(sock_->IsConnected()); | 249 EXPECT_TRUE(sock_->IsConnected()); |
201 net_log_.GetEntries(&net_log_entries); | 250 net_log_.GetEntries(&net_log_entries); |
202 EXPECT_TRUE( | 251 EXPECT_TRUE( |
203 LogContainsEndEvent(net_log_entries, -1, NetLog::TYPE_TCP_CONNECT)); | 252 LogContainsEndEvent(net_log_entries, -1, NetLog::TYPE_TCP_CONNECT)); |
204 | 253 |
205 sock_->Disconnect(); | 254 sock_->Disconnect(); |
206 EXPECT_FALSE(sock_->IsConnected()); | 255 EXPECT_FALSE(sock_->IsConnected()); |
207 } | 256 } |
208 | 257 |
209 TEST_P(TransportClientSocketTest, IsConnected) { | 258 TEST_P(TransportClientSocketTest, IsConnected) { |
210 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 259 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
211 TestCompletionCallback callback; | 260 TestCompletionCallback callback; |
212 uint32 bytes_read; | 261 uint32 bytes_read; |
213 | 262 |
214 EXPECT_FALSE(sock_->IsConnected()); | 263 EXPECT_FALSE(sock_->IsConnected()); |
215 EXPECT_FALSE(sock_->IsConnectedAndIdle()); | 264 EXPECT_FALSE(sock_->IsConnectedAndIdle()); |
216 int rv = sock_->Connect(callback.callback()); | 265 |
217 if (rv != OK) { | 266 EstablishConnection(&callback); |
218 ASSERT_EQ(rv, ERR_IO_PENDING); | 267 |
219 rv = callback.WaitForResult(); | |
220 EXPECT_EQ(rv, OK); | |
221 } | |
222 EXPECT_TRUE(sock_->IsConnected()); | 268 EXPECT_TRUE(sock_->IsConnected()); |
223 EXPECT_TRUE(sock_->IsConnectedAndIdle()); | 269 EXPECT_TRUE(sock_->IsConnectedAndIdle()); |
224 | 270 |
225 // Send the request and wait for the server to respond. | 271 // Send the request and wait for the server to respond. |
226 SendClientRequest(); | 272 SendRequestAndResponse(); |
227 | 273 |
228 // Drain a single byte so we know we've received some data. | 274 // Drain a single byte so we know we've received some data. |
229 bytes_read = DrainClientSocket(buf.get(), 1, 1, &callback); | 275 bytes_read = DrainClientSocket(buf.get(), 1, 1, &callback); |
230 ASSERT_EQ(bytes_read, 1u); | 276 ASSERT_EQ(bytes_read, 1u); |
231 | 277 |
232 // Socket should be considered connected, but not idle, due to | 278 // Socket should be considered connected, but not idle, due to |
233 // pending data. | 279 // pending data. |
234 EXPECT_TRUE(sock_->IsConnected()); | 280 EXPECT_TRUE(sock_->IsConnected()); |
235 EXPECT_FALSE(sock_->IsConnectedAndIdle()); | 281 EXPECT_FALSE(sock_->IsConnectedAndIdle()); |
236 | 282 |
237 bytes_read = DrainClientSocket( | 283 bytes_read = |
238 buf.get(), 4096, arraysize(kServerReply) - 2, &callback); | 284 DrainClientSocket(buf.get(), 4096, strlen(kServerReply) - 1, &callback); |
239 ASSERT_EQ(bytes_read, arraysize(kServerReply) - 2); | 285 ASSERT_EQ(bytes_read, strlen(kServerReply) - 1); |
240 | 286 |
241 // After draining the data, the socket should be back to connected | 287 // After draining the data, the socket should be back to connected |
242 // and idle. | 288 // and idle. |
243 EXPECT_TRUE(sock_->IsConnected()); | 289 EXPECT_TRUE(sock_->IsConnected()); |
244 EXPECT_TRUE(sock_->IsConnectedAndIdle()); | 290 EXPECT_TRUE(sock_->IsConnectedAndIdle()); |
245 | 291 |
246 // This time close the server socket immediately after the server response. | 292 // This time close the server socket immediately after the server response. |
247 set_close_server_socket_on_next_send(true); | 293 set_close_server_socket_on_next_send(true); |
248 SendClientRequest(); | 294 SendRequestAndResponse(); |
249 | 295 |
250 bytes_read = DrainClientSocket(buf.get(), 1, 1, &callback); | 296 bytes_read = DrainClientSocket(buf.get(), 1, 1, &callback); |
251 ASSERT_EQ(bytes_read, 1u); | 297 ASSERT_EQ(bytes_read, 1u); |
252 | 298 |
253 // As above because of data. | 299 // As above because of data. |
254 EXPECT_TRUE(sock_->IsConnected()); | 300 EXPECT_TRUE(sock_->IsConnected()); |
255 EXPECT_FALSE(sock_->IsConnectedAndIdle()); | 301 EXPECT_FALSE(sock_->IsConnectedAndIdle()); |
256 | 302 |
257 bytes_read = DrainClientSocket( | 303 bytes_read = |
258 buf.get(), 4096, arraysize(kServerReply) - 2, &callback); | 304 DrainClientSocket(buf.get(), 4096, strlen(kServerReply) - 1, &callback); |
259 ASSERT_EQ(bytes_read, arraysize(kServerReply) - 2); | 305 ASSERT_EQ(bytes_read, strlen(kServerReply) - 1); |
260 | 306 |
261 // Once the data is drained, the socket should now be seen as not | 307 // Once the data is drained, the socket should now be seen as not |
262 // connected. | 308 // connected. |
263 if (sock_->IsConnected()) { | 309 if (sock_->IsConnected()) { |
264 // In the unlikely event that the server's connection closure is not | 310 // In the unlikely event that the server's connection closure is not |
265 // processed in time, wait for the connection to be closed. | 311 // processed in time, wait for the connection to be closed. |
266 rv = sock_->Read(buf.get(), 4096, callback.callback()); | 312 int rv = sock_->Read(buf.get(), 4096, callback.callback()); |
267 EXPECT_EQ(0, callback.GetResult(rv)); | 313 EXPECT_EQ(0, callback.GetResult(rv)); |
268 EXPECT_FALSE(sock_->IsConnected()); | 314 EXPECT_FALSE(sock_->IsConnected()); |
269 } | 315 } |
270 EXPECT_FALSE(sock_->IsConnectedAndIdle()); | 316 EXPECT_FALSE(sock_->IsConnectedAndIdle()); |
271 } | 317 } |
272 | 318 |
273 TEST_P(TransportClientSocketTest, Read) { | 319 TEST_P(TransportClientSocketTest, Read) { |
274 TestCompletionCallback callback; | 320 TestCompletionCallback callback; |
275 int rv = sock_->Connect(callback.callback()); | 321 EstablishConnection(&callback); |
276 if (rv != OK) { | |
277 ASSERT_EQ(rv, ERR_IO_PENDING); | |
278 | 322 |
279 rv = callback.WaitForResult(); | 323 SendRequestAndResponse(); |
280 EXPECT_EQ(rv, OK); | |
281 } | |
282 SendClientRequest(); | |
283 | 324 |
284 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 325 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
285 uint32 bytes_read = DrainClientSocket( | 326 uint32 bytes_read = |
286 buf.get(), 4096, arraysize(kServerReply) - 1, &callback); | 327 DrainClientSocket(buf.get(), 4096, strlen(kServerReply), &callback); |
287 ASSERT_EQ(bytes_read, arraysize(kServerReply) - 1); | 328 ASSERT_EQ(bytes_read, strlen(kServerReply)); |
| 329 ASSERT_EQ(std::string(kServerReply), std::string(buf->data(), bytes_read)); |
288 | 330 |
289 // All data has been read now. Read once more to force an ERR_IO_PENDING, and | 331 // 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. | 332 // then close the server socket, and note the close. |
291 | 333 |
292 rv = sock_->Read(buf.get(), 4096, callback.callback()); | 334 int rv = sock_->Read(buf.get(), 4096, callback.callback()); |
293 ASSERT_EQ(ERR_IO_PENDING, rv); | 335 ASSERT_EQ(ERR_IO_PENDING, rv); |
294 CloseServerSocket(); | 336 CloseServerSocket(); |
295 EXPECT_EQ(0, callback.WaitForResult()); | 337 EXPECT_EQ(0, callback.WaitForResult()); |
296 } | 338 } |
297 | 339 |
298 TEST_P(TransportClientSocketTest, Read_SmallChunks) { | 340 TEST_P(TransportClientSocketTest, Read_SmallChunks) { |
299 TestCompletionCallback callback; | 341 TestCompletionCallback callback; |
300 int rv = sock_->Connect(callback.callback()); | 342 EstablishConnection(&callback); |
301 if (rv != OK) { | |
302 ASSERT_EQ(rv, ERR_IO_PENDING); | |
303 | 343 |
304 rv = callback.WaitForResult(); | 344 SendRequestAndResponse(); |
305 EXPECT_EQ(rv, OK); | |
306 } | |
307 SendClientRequest(); | |
308 | 345 |
309 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); | 346 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); |
310 uint32 bytes_read = 0; | 347 uint32 bytes_read = 0; |
311 while (bytes_read < arraysize(kServerReply) - 1) { | 348 while (bytes_read < strlen(kServerReply)) { |
312 rv = sock_->Read(buf.get(), 1, callback.callback()); | 349 int rv = sock_->Read(buf.get(), 1, callback.callback()); |
313 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 350 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
314 | 351 |
315 if (rv == ERR_IO_PENDING) | 352 rv = callback.GetResult(rv); |
316 rv = callback.WaitForResult(); | |
317 | 353 |
318 ASSERT_EQ(1, rv); | 354 ASSERT_EQ(1, rv); |
319 bytes_read += rv; | 355 bytes_read += rv; |
320 } | 356 } |
321 | 357 |
322 // All data has been read now. Read once more to force an ERR_IO_PENDING, and | 358 // 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. | 359 // then close the server socket, and note the close. |
324 | 360 |
325 rv = sock_->Read(buf.get(), 1, callback.callback()); | 361 int rv = sock_->Read(buf.get(), 1, callback.callback()); |
326 ASSERT_EQ(ERR_IO_PENDING, rv); | 362 ASSERT_EQ(ERR_IO_PENDING, rv); |
327 CloseServerSocket(); | 363 CloseServerSocket(); |
328 EXPECT_EQ(0, callback.WaitForResult()); | 364 EXPECT_EQ(0, callback.WaitForResult()); |
329 } | 365 } |
330 | 366 |
331 TEST_P(TransportClientSocketTest, Read_Interrupted) { | 367 TEST_P(TransportClientSocketTest, Read_Interrupted) { |
332 TestCompletionCallback callback; | 368 TestCompletionCallback callback; |
333 int rv = sock_->Connect(callback.callback()); | 369 EstablishConnection(&callback); |
334 if (rv != OK) { | |
335 ASSERT_EQ(ERR_IO_PENDING, rv); | |
336 | 370 |
337 rv = callback.WaitForResult(); | 371 SendRequestAndResponse(); |
338 EXPECT_EQ(rv, OK); | |
339 } | |
340 SendClientRequest(); | |
341 | 372 |
342 // Do a partial read and then exit. This test should not crash! | 373 // Do a partial read and then exit. This test should not crash! |
343 scoped_refptr<IOBuffer> buf(new IOBuffer(16)); | 374 scoped_refptr<IOBuffer> buf(new IOBuffer(16)); |
344 rv = sock_->Read(buf.get(), 16, callback.callback()); | 375 int rv = sock_->Read(buf.get(), 16, callback.callback()); |
345 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 376 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
346 | 377 |
347 if (rv == ERR_IO_PENDING) | 378 rv = callback.GetResult(rv); |
348 rv = callback.WaitForResult(); | |
349 | 379 |
350 EXPECT_NE(0, rv); | 380 EXPECT_NE(0, rv); |
351 } | 381 } |
352 | 382 |
353 TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_ReadFirst) { | 383 TEST_P(TransportClientSocketTest, FullDuplex_ReadFirst) { |
354 TestCompletionCallback callback; | 384 TestCompletionCallback callback; |
355 int rv = sock_->Connect(callback.callback()); | 385 EstablishConnection(&callback); |
356 if (rv != OK) { | |
357 ASSERT_EQ(rv, ERR_IO_PENDING); | |
358 | |
359 rv = callback.WaitForResult(); | |
360 EXPECT_EQ(rv, OK); | |
361 } | |
362 | 386 |
363 // Read first. There's no data, so it should return ERR_IO_PENDING. | 387 // Read first. There's no data, so it should return ERR_IO_PENDING. |
364 const int kBufLen = 4096; | 388 const int kBufLen = 4096; |
365 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); | 389 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); |
366 rv = sock_->Read(buf.get(), kBufLen, callback.callback()); | 390 int rv = sock_->Read(buf.get(), kBufLen, callback.callback()); |
367 EXPECT_EQ(ERR_IO_PENDING, rv); | 391 EXPECT_EQ(ERR_IO_PENDING, rv); |
368 | 392 |
369 PauseServerReads(); | |
370 const int kWriteBufLen = 64 * 1024; | 393 const int kWriteBufLen = 64 * 1024; |
371 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); | 394 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); |
372 char* request_data = request_buffer->data(); | 395 char* request_data = request_buffer->data(); |
373 memset(request_data, 'A', kWriteBufLen); | 396 memset(request_data, 'A', kWriteBufLen); |
374 TestCompletionCallback write_callback; | 397 TestCompletionCallback write_callback; |
375 | 398 |
| 399 int bytes_written = 0; |
376 while (true) { | 400 while (true) { |
377 rv = sock_->Write( | 401 rv = sock_->Write(request_buffer.get(), kWriteBufLen, |
378 request_buffer.get(), kWriteBufLen, write_callback.callback()); | 402 write_callback.callback()); |
379 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 403 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
380 | |
381 if (rv == ERR_IO_PENDING) { | 404 if (rv == ERR_IO_PENDING) { |
382 ResumeServerReads(); | 405 ReadServerData(bytes_written); |
| 406 SendServerResponse(); |
383 rv = write_callback.WaitForResult(); | 407 rv = write_callback.WaitForResult(); |
384 break; | 408 break; |
385 } | 409 } |
| 410 bytes_written += rv; |
386 } | 411 } |
387 | 412 |
388 // At this point, both read and write have returned ERR_IO_PENDING, and the | 413 // 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 | 414 // 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. | 415 // make sure that the socket can handle full duplex communications. |
391 | 416 |
392 rv = callback.WaitForResult(); | 417 rv = callback.WaitForResult(); |
393 EXPECT_GE(rv, 0); | 418 EXPECT_GE(rv, 0); |
394 } | 419 } |
395 | 420 |
396 TEST_P(TransportClientSocketTest, DISABLED_FullDuplex_WriteFirst) { | 421 TEST_P(TransportClientSocketTest, FullDuplex_WriteFirst) { |
397 TestCompletionCallback callback; | 422 TestCompletionCallback callback; |
398 int rv = sock_->Connect(callback.callback()); | 423 EstablishConnection(&callback); |
399 if (rv != OK) { | |
400 ASSERT_EQ(ERR_IO_PENDING, rv); | |
401 | 424 |
402 rv = callback.WaitForResult(); | |
403 EXPECT_EQ(OK, rv); | |
404 } | |
405 | |
406 PauseServerReads(); | |
407 const int kWriteBufLen = 64 * 1024; | 425 const int kWriteBufLen = 64 * 1024; |
408 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); | 426 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kWriteBufLen)); |
409 char* request_data = request_buffer->data(); | 427 char* request_data = request_buffer->data(); |
410 memset(request_data, 'A', kWriteBufLen); | 428 memset(request_data, 'A', kWriteBufLen); |
411 TestCompletionCallback write_callback; | 429 TestCompletionCallback write_callback; |
412 | 430 |
| 431 int bytes_written = 0; |
413 while (true) { | 432 while (true) { |
414 rv = sock_->Write( | 433 int rv = sock_->Write(request_buffer.get(), kWriteBufLen, |
415 request_buffer.get(), kWriteBufLen, write_callback.callback()); | 434 write_callback.callback()); |
416 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 435 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
417 | 436 |
418 if (rv == ERR_IO_PENDING) | 437 if (rv == ERR_IO_PENDING) |
419 break; | 438 break; |
| 439 bytes_written += rv; |
420 } | 440 } |
421 | 441 |
422 // Now we have the Write() blocked on ERR_IO_PENDING. It's time to force the | 442 // 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. | 443 // Read() to block on ERR_IO_PENDING too. |
424 | 444 |
425 const int kBufLen = 4096; | 445 const int kBufLen = 4096; |
426 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); | 446 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufLen)); |
427 while (true) { | 447 while (true) { |
428 rv = sock_->Read(buf.get(), kBufLen, callback.callback()); | 448 int rv = sock_->Read(buf.get(), kBufLen, callback.callback()); |
429 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 449 ASSERT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
430 if (rv == ERR_IO_PENDING) | 450 if (rv == ERR_IO_PENDING) |
431 break; | 451 break; |
432 } | 452 } |
433 | 453 |
434 // At this point, both read and write have returned ERR_IO_PENDING. Now we | 454 // 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 | 455 // run the write and read callbacks to make sure they can handle full duplex |
436 // communications. | 456 // communications. |
437 | 457 |
438 ResumeServerReads(); | 458 ReadServerData(bytes_written); |
439 rv = write_callback.WaitForResult(); | 459 SendServerResponse(); |
| 460 int rv = write_callback.WaitForResult(); |
440 EXPECT_GE(rv, 0); | 461 EXPECT_GE(rv, 0); |
441 | 462 |
442 // It's possible the read is blocked because it's already read all the data. | 463 // 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. | 464 // Close the server socket, so there will at least be a 0-byte read. |
444 CloseServerSocket(); | 465 CloseServerSocket(); |
445 | 466 |
446 rv = callback.WaitForResult(); | 467 rv = callback.WaitForResult(); |
447 EXPECT_GE(rv, 0); | 468 EXPECT_GE(rv, 0); |
448 } | 469 } |
449 | 470 |
450 } // namespace net | 471 } // namespace net |
OLD | NEW |