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