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