Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: net/socket/transport_client_socket_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698