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/ssl_client_socket.h" | 5 #include "net/socket/ssl_client_socket.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
| 10 #include <algorithm> |
10 #include <utility> | 11 #include <utility> |
11 | 12 |
12 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
13 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
14 #include "base/location.h" | 15 #include "base/location.h" |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
17 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
18 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
19 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 21 #include "base/test/scoped_feature_list.h" |
20 #include "base/test/scoped_task_scheduler.h" | 22 #include "base/test/scoped_task_scheduler.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
22 #include "base/time/time.h" | 24 #include "base/time/time.h" |
23 #include "base/values.h" | 25 #include "base/values.h" |
24 #include "net/base/address_list.h" | 26 #include "net/base/address_list.h" |
25 #include "net/base/io_buffer.h" | 27 #include "net/base/io_buffer.h" |
26 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
27 #include "net/base/test_completion_callback.h" | 29 #include "net/base/test_completion_callback.h" |
28 #include "net/cert/asn1_util.h" | 30 #include "net/cert/asn1_util.h" |
29 #include "net/cert/ct_policy_enforcer.h" | 31 #include "net/cert/ct_policy_enforcer.h" |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 int64_t GetTotalReceivedBytes() const override { | 137 int64_t GetTotalReceivedBytes() const override { |
136 return transport_->GetTotalReceivedBytes(); | 138 return transport_->GetTotalReceivedBytes(); |
137 } | 139 } |
138 | 140 |
139 // Socket implementation: | 141 // Socket implementation: |
140 int Read(IOBuffer* buf, | 142 int Read(IOBuffer* buf, |
141 int buf_len, | 143 int buf_len, |
142 const CompletionCallback& callback) override { | 144 const CompletionCallback& callback) override { |
143 return transport_->Read(buf, buf_len, callback); | 145 return transport_->Read(buf, buf_len, callback); |
144 } | 146 } |
| 147 int ReadIfReady(IOBuffer* buf, |
| 148 int buf_len, |
| 149 const CompletionCallback& callback) override { |
| 150 return transport_->ReadIfReady(buf, buf_len, callback); |
| 151 } |
145 int Write(IOBuffer* buf, | 152 int Write(IOBuffer* buf, |
146 int buf_len, | 153 int buf_len, |
147 const CompletionCallback& callback) override { | 154 const CompletionCallback& callback) override { |
148 return transport_->Write(buf, buf_len, callback); | 155 return transport_->Write(buf, buf_len, callback); |
149 } | 156 } |
150 int SetReceiveBufferSize(int32_t size) override { | 157 int SetReceiveBufferSize(int32_t size) override { |
151 return transport_->SetReceiveBufferSize(size); | 158 return transport_->SetReceiveBufferSize(size); |
152 } | 159 } |
153 int SetSendBufferSize(int32_t size) override { | 160 int SetSendBufferSize(int32_t size) override { |
154 return transport_->SetSendBufferSize(size); | 161 return transport_->SetSendBufferSize(size); |
(...skipping 12 matching lines...) Expand all Loading... |
167 class ReadBufferingStreamSocket : public WrappedStreamSocket { | 174 class ReadBufferingStreamSocket : public WrappedStreamSocket { |
168 public: | 175 public: |
169 explicit ReadBufferingStreamSocket(std::unique_ptr<StreamSocket> transport); | 176 explicit ReadBufferingStreamSocket(std::unique_ptr<StreamSocket> transport); |
170 ~ReadBufferingStreamSocket() override {} | 177 ~ReadBufferingStreamSocket() override {} |
171 | 178 |
172 // Socket implementation: | 179 // Socket implementation: |
173 int Read(IOBuffer* buf, | 180 int Read(IOBuffer* buf, |
174 int buf_len, | 181 int buf_len, |
175 const CompletionCallback& callback) override; | 182 const CompletionCallback& callback) override; |
176 | 183 |
| 184 int ReadIfReady(IOBuffer* buf, |
| 185 int buf_len, |
| 186 const CompletionCallback& callback) override; |
| 187 |
177 // Sets the internal buffer to |size|. This must not be greater than | 188 // Sets the internal buffer to |size|. This must not be greater than |
178 // the largest value supplied to Read() - that is, it does not handle | 189 // the largest value supplied to Read() - that is, it does not handle |
179 // having "leftovers" at the end of Read(). | 190 // having "leftovers" at the end of Read(). |
180 // Each call to Read() will be prevented from completion until at least | 191 // Each call to Read() will be prevented from completion until at least |
181 // |size| data has been read. | 192 // |size| data has been read. |
182 // Set to 0 to turn off buffering, causing Read() to transparently | 193 // Set to 0 to turn off buffering, causing Read() to transparently |
183 // read via the underlying transport. | 194 // read via the underlying transport. |
184 void SetBufferSize(int size); | 195 void SetBufferSize(int size); |
185 | 196 |
186 private: | 197 private: |
(...skipping 16 matching lines...) Expand all Loading... |
203 CompletionCallback user_read_callback_; | 214 CompletionCallback user_read_callback_; |
204 }; | 215 }; |
205 | 216 |
206 ReadBufferingStreamSocket::ReadBufferingStreamSocket( | 217 ReadBufferingStreamSocket::ReadBufferingStreamSocket( |
207 std::unique_ptr<StreamSocket> transport) | 218 std::unique_ptr<StreamSocket> transport) |
208 : WrappedStreamSocket(std::move(transport)), | 219 : WrappedStreamSocket(std::move(transport)), |
209 read_buffer_(new GrowableIOBuffer()), | 220 read_buffer_(new GrowableIOBuffer()), |
210 buffer_size_(0) {} | 221 buffer_size_(0) {} |
211 | 222 |
212 void ReadBufferingStreamSocket::SetBufferSize(int size) { | 223 void ReadBufferingStreamSocket::SetBufferSize(int size) { |
213 DCHECK(!user_read_buf_.get()); | 224 DCHECK(!user_read_buf_); |
214 buffer_size_ = size; | 225 buffer_size_ = size; |
215 read_buffer_->SetCapacity(size); | 226 read_buffer_->SetCapacity(size); |
216 } | 227 } |
217 | 228 |
218 int ReadBufferingStreamSocket::Read(IOBuffer* buf, | 229 int ReadBufferingStreamSocket::Read(IOBuffer* buf, |
219 int buf_len, | 230 int buf_len, |
220 const CompletionCallback& callback) { | 231 const CompletionCallback& callback) { |
| 232 DCHECK(!user_read_buf_); |
221 if (buffer_size_ == 0) | 233 if (buffer_size_ == 0) |
222 return transport_->Read(buf, buf_len, callback); | 234 return transport_->Read(buf, buf_len, callback); |
| 235 int rv = ReadIfReady(buf, buf_len, callback); |
| 236 if (rv == ERR_IO_PENDING) |
| 237 user_read_buf_ = buf; |
| 238 return rv; |
| 239 } |
| 240 |
| 241 int ReadBufferingStreamSocket::ReadIfReady(IOBuffer* buf, |
| 242 int buf_len, |
| 243 const CompletionCallback& callback) { |
| 244 DCHECK(!user_read_buf_); |
| 245 if (buffer_size_ == 0) |
| 246 return transport_->ReadIfReady(buf, buf_len, callback); |
| 247 |
| 248 if (read_buffer_->RemainingCapacity() == 0) { |
| 249 memcpy(buf->data(), read_buffer_->StartOfBuffer(), |
| 250 read_buffer_->capacity()); |
| 251 read_buffer_->set_offset(0); |
| 252 return read_buffer_->capacity(); |
| 253 } |
223 | 254 |
224 if (buf_len < buffer_size_) | 255 if (buf_len < buffer_size_) |
225 return ERR_UNEXPECTED; | 256 return ERR_UNEXPECTED; |
226 | 257 |
227 state_ = STATE_READ; | 258 state_ = STATE_READ; |
228 user_read_buf_ = buf; | 259 int rv = DoLoop(OK); |
229 int result = DoLoop(OK); | 260 if (rv == ERR_IO_PENDING) |
230 if (result == ERR_IO_PENDING) | |
231 user_read_callback_ = callback; | 261 user_read_callback_ = callback; |
232 else | 262 return rv; |
233 user_read_buf_ = NULL; | |
234 return result; | |
235 } | 263 } |
236 | 264 |
237 int ReadBufferingStreamSocket::DoLoop(int result) { | 265 int ReadBufferingStreamSocket::DoLoop(int result) { |
238 int rv = result; | 266 int rv = result; |
239 do { | 267 do { |
240 State current_state = state_; | 268 State current_state = state_; |
241 state_ = STATE_NONE; | 269 state_ = STATE_NONE; |
242 switch (current_state) { | 270 switch (current_state) { |
243 case STATE_READ: | 271 case STATE_READ: |
244 rv = DoRead(); | 272 rv = DoRead(); |
245 break; | 273 break; |
246 case STATE_READ_COMPLETE: | 274 case STATE_READ_COMPLETE: |
247 rv = DoReadComplete(rv); | 275 rv = DoReadComplete(rv); |
248 break; | 276 break; |
249 case STATE_NONE: | 277 case STATE_NONE: |
250 default: | 278 default: |
251 NOTREACHED() << "Unexpected state: " << current_state; | 279 NOTREACHED() << "Unexpected state: " << current_state; |
252 rv = ERR_UNEXPECTED; | 280 rv = ERR_UNEXPECTED; |
253 break; | 281 break; |
254 } | 282 } |
255 } while (rv != ERR_IO_PENDING && state_ != STATE_NONE); | 283 } while (rv != ERR_IO_PENDING && state_ != STATE_NONE); |
256 return rv; | 284 return rv; |
257 } | 285 } |
258 | 286 |
259 int ReadBufferingStreamSocket::DoRead() { | 287 int ReadBufferingStreamSocket::DoRead() { |
260 state_ = STATE_READ_COMPLETE; | 288 state_ = STATE_READ_COMPLETE; |
261 int rv = | 289 return transport_->Read( |
262 transport_->Read(read_buffer_.get(), | 290 read_buffer_.get(), read_buffer_->RemainingCapacity(), |
263 read_buffer_->RemainingCapacity(), | 291 base::Bind(&ReadBufferingStreamSocket::OnReadCompleted, |
264 base::Bind(&ReadBufferingStreamSocket::OnReadCompleted, | 292 base::Unretained(this))); |
265 base::Unretained(this))); | |
266 return rv; | |
267 } | 293 } |
268 | 294 |
269 int ReadBufferingStreamSocket::DoReadComplete(int result) { | 295 int ReadBufferingStreamSocket::DoReadComplete(int result) { |
270 state_ = STATE_NONE; | 296 state_ = STATE_NONE; |
| 297 |
271 if (result <= 0) | 298 if (result <= 0) |
272 return result; | 299 return result; |
273 | 300 |
274 read_buffer_->set_offset(read_buffer_->offset() + result); | 301 read_buffer_->set_offset(read_buffer_->offset() + result); |
275 if (read_buffer_->RemainingCapacity() > 0) { | 302 if (read_buffer_->RemainingCapacity() > 0) { |
276 state_ = STATE_READ; | 303 state_ = STATE_READ; |
277 return OK; | 304 return OK; |
278 } | 305 } |
279 | 306 |
| 307 // If ReadIfReady() is called by the user and this is an asynchronous |
| 308 // completion, notify the user that read can be retried. |
| 309 if (user_read_buf_ == nullptr) |
| 310 return OK; |
| 311 |
280 memcpy(user_read_buf_->data(), | 312 memcpy(user_read_buf_->data(), |
281 read_buffer_->StartOfBuffer(), | 313 read_buffer_->StartOfBuffer(), |
282 read_buffer_->capacity()); | 314 read_buffer_->capacity()); |
283 read_buffer_->set_offset(0); | 315 read_buffer_->set_offset(0); |
284 return read_buffer_->capacity(); | 316 return read_buffer_->capacity(); |
285 } | 317 } |
286 | 318 |
287 void ReadBufferingStreamSocket::OnReadCompleted(int result) { | 319 void ReadBufferingStreamSocket::OnReadCompleted(int result) { |
| 320 DCHECK_NE(ERR_IO_PENDING, result); |
| 321 DCHECK(user_read_callback_); |
| 322 |
288 result = DoLoop(result); | 323 result = DoLoop(result); |
289 if (result == ERR_IO_PENDING) | 324 if (result == ERR_IO_PENDING) |
290 return; | 325 return; |
291 | 326 user_read_buf_ = nullptr; |
292 user_read_buf_ = NULL; | |
293 base::ResetAndReturn(&user_read_callback_).Run(result); | 327 base::ResetAndReturn(&user_read_callback_).Run(result); |
294 } | 328 } |
295 | 329 |
296 // Simulates synchronously receiving an error during Read() or Write() | 330 // Simulates synchronously receiving an error during Read() or Write() |
297 class SynchronousErrorStreamSocket : public WrappedStreamSocket { | 331 class SynchronousErrorStreamSocket : public WrappedStreamSocket { |
298 public: | 332 public: |
299 explicit SynchronousErrorStreamSocket(std::unique_ptr<StreamSocket> transport) | 333 explicit SynchronousErrorStreamSocket(std::unique_ptr<StreamSocket> transport) |
300 : WrappedStreamSocket(std::move(transport)) {} | 334 : WrappedStreamSocket(std::move(transport)) {} |
301 ~SynchronousErrorStreamSocket() override {} | 335 ~SynchronousErrorStreamSocket() override {} |
302 | 336 |
303 // Socket implementation: | 337 // Socket implementation: |
304 int Read(IOBuffer* buf, | 338 int Read(IOBuffer* buf, |
305 int buf_len, | 339 int buf_len, |
306 const CompletionCallback& callback) override; | 340 const CompletionCallback& callback) override; |
| 341 int ReadIfReady(IOBuffer* buf, |
| 342 int buf_len, |
| 343 const CompletionCallback& callback) override; |
307 int Write(IOBuffer* buf, | 344 int Write(IOBuffer* buf, |
308 int buf_len, | 345 int buf_len, |
309 const CompletionCallback& callback) override; | 346 const CompletionCallback& callback) override; |
310 | 347 |
311 // Sets the next Read() call and all future calls to return |error|. | 348 // Sets the next Read() call and all future calls to return |error|. |
312 // If there is already a pending asynchronous read, the configured error | 349 // If there is already a pending asynchronous read, the configured error |
313 // will not be returned until that asynchronous read has completed and Read() | 350 // will not be returned until that asynchronous read has completed and Read() |
314 // is called again. | 351 // is called again. |
315 void SetNextReadError(int error) { | 352 void SetNextReadError(int error) { |
316 DCHECK_GE(0, error); | 353 DCHECK_GE(0, error); |
(...skipping 22 matching lines...) Expand all Loading... |
339 }; | 376 }; |
340 | 377 |
341 int SynchronousErrorStreamSocket::Read(IOBuffer* buf, | 378 int SynchronousErrorStreamSocket::Read(IOBuffer* buf, |
342 int buf_len, | 379 int buf_len, |
343 const CompletionCallback& callback) { | 380 const CompletionCallback& callback) { |
344 if (have_read_error_) | 381 if (have_read_error_) |
345 return pending_read_error_; | 382 return pending_read_error_; |
346 return transport_->Read(buf, buf_len, callback); | 383 return transport_->Read(buf, buf_len, callback); |
347 } | 384 } |
348 | 385 |
| 386 int SynchronousErrorStreamSocket::ReadIfReady( |
| 387 IOBuffer* buf, |
| 388 int buf_len, |
| 389 const CompletionCallback& callback) { |
| 390 if (have_read_error_) |
| 391 return pending_read_error_; |
| 392 return transport_->ReadIfReady(buf, buf_len, callback); |
| 393 } |
| 394 |
349 int SynchronousErrorStreamSocket::Write(IOBuffer* buf, | 395 int SynchronousErrorStreamSocket::Write(IOBuffer* buf, |
350 int buf_len, | 396 int buf_len, |
351 const CompletionCallback& callback) { | 397 const CompletionCallback& callback) { |
352 if (have_write_error_) | 398 if (have_write_error_) |
353 return pending_write_error_; | 399 return pending_write_error_; |
354 return transport_->Write(buf, buf_len, callback); | 400 return transport_->Write(buf, buf_len, callback); |
355 } | 401 } |
356 | 402 |
357 // FakeBlockingStreamSocket wraps an existing StreamSocket and simulates the | 403 // FakeBlockingStreamSocket wraps an existing StreamSocket and simulates the |
358 // underlying transport needing to complete things asynchronously in a | 404 // underlying transport needing to complete things asynchronously in a |
359 // deterministic manner (e.g.: independent of the TestServer and the OS's | 405 // deterministic manner (e.g.: independent of the TestServer and the OS's |
360 // semantics). | 406 // semantics). |
361 class FakeBlockingStreamSocket : public WrappedStreamSocket { | 407 class FakeBlockingStreamSocket : public WrappedStreamSocket { |
362 public: | 408 public: |
363 explicit FakeBlockingStreamSocket(std::unique_ptr<StreamSocket> transport) | 409 explicit FakeBlockingStreamSocket(std::unique_ptr<StreamSocket> transport) |
364 : WrappedStreamSocket(std::move(transport)) {} | 410 : WrappedStreamSocket(std::move(transport)) {} |
365 ~FakeBlockingStreamSocket() override {} | 411 ~FakeBlockingStreamSocket() override {} |
366 | 412 |
367 // Socket implementation: | 413 // Socket implementation: |
368 int Read(IOBuffer* buf, | 414 int Read(IOBuffer* buf, |
369 int buf_len, | 415 int buf_len, |
370 const CompletionCallback& callback) override; | 416 const CompletionCallback& callback) override; |
| 417 int ReadIfReady(IOBuffer* buf, |
| 418 int buf_len, |
| 419 const CompletionCallback& callback) override; |
371 int Write(IOBuffer* buf, | 420 int Write(IOBuffer* buf, |
372 int buf_len, | 421 int buf_len, |
373 const CompletionCallback& callback) override; | 422 const CompletionCallback& callback) override; |
374 | 423 |
375 int pending_read_result() const { return pending_read_result_; } | 424 int pending_read_result() const { return pending_read_result_; } |
376 IOBuffer* pending_read_buf() const { return pending_read_buf_.get(); } | 425 IOBuffer* pending_read_buf() const { return pending_read_buf_.get(); } |
377 | 426 |
378 // Blocks read results on the socket. Reads will not complete until | 427 // Blocks read results on the socket. Reads will not complete until |
379 // UnblockReadResult() has been called and a result is ready from the | 428 // UnblockReadResult() has been called and a result is ready from the |
380 // underlying transport. Note: if BlockReadResult() is called while there is a | 429 // underlying transport. Note: if BlockReadResult() is called while there is a |
(...skipping 17 matching lines...) Expand all Loading... |
398 void BlockWrite(); | 447 void BlockWrite(); |
399 void UnblockWrite(); | 448 void UnblockWrite(); |
400 | 449 |
401 // Waits for the blocked Write() call to be scheduled. | 450 // Waits for the blocked Write() call to be scheduled. |
402 void WaitForWrite(); | 451 void WaitForWrite(); |
403 | 452 |
404 private: | 453 private: |
405 // Handles completion from the underlying transport read. | 454 // Handles completion from the underlying transport read. |
406 void OnReadCompleted(int result); | 455 void OnReadCompleted(int result); |
407 | 456 |
| 457 // Handles async completion of ReadIfReady(). |
| 458 void CompleteReadIfReady(scoped_refptr<IOBuffer> buffer, int rv); |
| 459 |
408 // Finishes the current read. | 460 // Finishes the current read. |
409 void ReturnReadResult(); | 461 void ReturnReadResult(); |
410 | 462 |
411 // True if read callbacks are blocked. | 463 // True if read callbacks are blocked. |
412 bool should_block_read_ = false; | 464 bool should_block_read_ = false; |
413 | 465 |
| 466 // Used to buffer result returned by a completed ReadIfReady(). |
| 467 std::string read_if_ready_buf_; |
| 468 |
| 469 // Non-null if there is a pending ReadIfReady(). |
| 470 CompletionCallback read_if_ready_callback_; |
| 471 |
414 // The buffer for the pending read, or NULL if not consumed. | 472 // The buffer for the pending read, or NULL if not consumed. |
415 scoped_refptr<IOBuffer> pending_read_buf_; | 473 scoped_refptr<IOBuffer> pending_read_buf_; |
416 | 474 |
417 // The size of the pending read buffer, or -1 if not set. | 475 // The size of the pending read buffer, or -1 if not set. |
418 int pending_read_buf_len_ = -1; | 476 int pending_read_buf_len_ = -1; |
419 | 477 |
420 // The user callback for the pending read call. | 478 // The user callback for the pending read call. |
421 CompletionCallback pending_read_callback_; | 479 CompletionCallback pending_read_callback_; |
422 | 480 |
423 // The result for the blocked read callback, or ERR_IO_PENDING if not | 481 // The result for the blocked read callback, or ERR_IO_PENDING if not |
(...skipping 20 matching lines...) Expand all Loading... |
444 }; | 502 }; |
445 | 503 |
446 int FakeBlockingStreamSocket::Read(IOBuffer* buf, | 504 int FakeBlockingStreamSocket::Read(IOBuffer* buf, |
447 int len, | 505 int len, |
448 const CompletionCallback& callback) { | 506 const CompletionCallback& callback) { |
449 DCHECK(!pending_read_buf_); | 507 DCHECK(!pending_read_buf_); |
450 DCHECK(pending_read_callback_.is_null()); | 508 DCHECK(pending_read_callback_.is_null()); |
451 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); | 509 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); |
452 DCHECK(!callback.is_null()); | 510 DCHECK(!callback.is_null()); |
453 | 511 |
454 int rv = transport_->Read(buf, len, base::Bind( | 512 int rv = |
455 &FakeBlockingStreamSocket::OnReadCompleted, base::Unretained(this))); | 513 transport_->Read(buf, len, |
| 514 base::Bind(&FakeBlockingStreamSocket::OnReadCompleted, |
| 515 base::Unretained(this))); |
456 if (rv == ERR_IO_PENDING || should_block_read_) { | 516 if (rv == ERR_IO_PENDING || should_block_read_) { |
457 // Save the callback to be called later. | 517 // Save the callback to be called later. |
458 pending_read_buf_ = buf; | 518 pending_read_buf_ = buf; |
459 pending_read_buf_len_ = len; | 519 pending_read_buf_len_ = len; |
460 pending_read_callback_ = callback; | 520 pending_read_callback_ = callback; |
461 // Save the read result. | 521 // Save the read result. |
462 if (rv != ERR_IO_PENDING) { | 522 if (rv != ERR_IO_PENDING) { |
463 OnReadCompleted(rv); | 523 OnReadCompleted(rv); |
464 rv = ERR_IO_PENDING; | 524 rv = ERR_IO_PENDING; |
465 } | 525 } |
466 } | 526 } |
467 return rv; | 527 return rv; |
468 } | 528 } |
469 | 529 |
| 530 int FakeBlockingStreamSocket::ReadIfReady(IOBuffer* buf, |
| 531 int len, |
| 532 const CompletionCallback& callback) { |
| 533 if (!read_if_ready_buf_.empty()) { |
| 534 // If ReadIfReady() is used, asynchronous reads with a large enough buffer |
| 535 // and no BlockReadResult() are supported by this class. Explicitly check |
| 536 // that |should_block_read_| doesn't apply and |len| is greater than the |
| 537 // size of the buffered data. |
| 538 CHECK(!should_block_read_); |
| 539 CHECK_GE(len, static_cast<int>(read_if_ready_buf_.size())); |
| 540 int rv = read_if_ready_buf_.size(); |
| 541 memcpy(buf->data(), read_if_ready_buf_.data(), rv); |
| 542 read_if_ready_buf_.clear(); |
| 543 return rv; |
| 544 } |
| 545 scoped_refptr<IOBuffer> buf_copy = new IOBuffer(len); |
| 546 int rv = Read(buf_copy.get(), len, |
| 547 base::Bind(&FakeBlockingStreamSocket::CompleteReadIfReady, |
| 548 base::Unretained(this), buf_copy)); |
| 549 if (rv > 0) |
| 550 memcpy(buf->data(), buf_copy->data(), rv); |
| 551 if (rv == ERR_IO_PENDING) |
| 552 read_if_ready_callback_ = callback; |
| 553 return rv; |
| 554 } |
| 555 |
470 int FakeBlockingStreamSocket::Write(IOBuffer* buf, | 556 int FakeBlockingStreamSocket::Write(IOBuffer* buf, |
471 int len, | 557 int len, |
472 const CompletionCallback& callback) { | 558 const CompletionCallback& callback) { |
473 DCHECK(buf); | 559 DCHECK(buf); |
474 DCHECK_LE(0, len); | 560 DCHECK_LE(0, len); |
475 | 561 |
476 if (!should_block_write_) | 562 if (!should_block_write_) |
477 return transport_->Write(buf, len, callback); | 563 return transport_->Write(buf, len, callback); |
478 | 564 |
479 // Schedule the write, but do nothing. | 565 // Schedule the write, but do nothing. |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 if (should_block_read_) { | 663 if (should_block_read_) { |
578 // Defer the result until UnblockReadResult is called. | 664 // Defer the result until UnblockReadResult is called. |
579 if (read_loop_) | 665 if (read_loop_) |
580 read_loop_->Quit(); | 666 read_loop_->Quit(); |
581 return; | 667 return; |
582 } | 668 } |
583 | 669 |
584 ReturnReadResult(); | 670 ReturnReadResult(); |
585 } | 671 } |
586 | 672 |
| 673 void FakeBlockingStreamSocket::CompleteReadIfReady(scoped_refptr<IOBuffer> buf, |
| 674 int rv) { |
| 675 DCHECK(read_if_ready_callback_); |
| 676 DCHECK(read_if_ready_buf_.empty()); |
| 677 DCHECK(!should_block_read_); |
| 678 if (rv > 0) |
| 679 read_if_ready_buf_ = std::string(buf->data(), buf->data() + rv); |
| 680 base::ResetAndReturn(&read_if_ready_callback_).Run(rv > 0 ? OK : rv); |
| 681 } |
| 682 |
587 void FakeBlockingStreamSocket::ReturnReadResult() { | 683 void FakeBlockingStreamSocket::ReturnReadResult() { |
588 int result = pending_read_result_; | 684 int result = pending_read_result_; |
589 pending_read_result_ = ERR_IO_PENDING; | 685 pending_read_result_ = ERR_IO_PENDING; |
590 pending_read_buf_ = nullptr; | 686 pending_read_buf_ = nullptr; |
591 pending_read_buf_len_ = -1; | 687 pending_read_buf_len_ = -1; |
592 base::ResetAndReturn(&pending_read_callback_).Run(result); | 688 base::ResetAndReturn(&pending_read_callback_).Run(result); |
593 } | 689 } |
594 | 690 |
595 // CountingStreamSocket wraps an existing StreamSocket and maintains a count of | 691 // CountingStreamSocket wraps an existing StreamSocket and maintains a count of |
596 // reads and writes on the socket. | 692 // reads and writes on the socket. |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
851 SSLClientSocketContext context_; | 947 SSLClientSocketContext context_; |
852 std::unique_ptr<SSLClientSocket> sock_; | 948 std::unique_ptr<SSLClientSocket> sock_; |
853 TestNetLog log_; | 949 TestNetLog log_; |
854 | 950 |
855 private: | 951 private: |
856 std::unique_ptr<SpawnedTestServer> spawned_test_server_; | 952 std::unique_ptr<SpawnedTestServer> spawned_test_server_; |
857 TestCompletionCallback callback_; | 953 TestCompletionCallback callback_; |
858 AddressList addr_; | 954 AddressList addr_; |
859 }; | 955 }; |
860 | 956 |
| 957 // If GetParam(), try ReadIfReady() and fall back to Read() if needed. |
| 958 class SSLClientSocketReadTest : public SSLClientSocketTest, |
| 959 public ::testing::WithParamInterface<bool> { |
| 960 protected: |
| 961 SSLClientSocketReadTest() |
| 962 : SSLClientSocketTest(), read_if_ready_enabled_(GetParam()) {} |
| 963 |
| 964 void SetUp() override { |
| 965 if (read_if_ready_enabled()) |
| 966 scoped_feature_list_.InitAndEnableFeature(Socket::kReadIfReadyExperiment); |
| 967 } |
| 968 |
| 969 // Convienient wrapper to call Read()/ReadIfReady() depending on whether |
| 970 // ReadyIfReady() is enabled. |
| 971 int Read(StreamSocket* socket, |
| 972 IOBuffer* buf, |
| 973 int buf_len, |
| 974 const CompletionCallback& callback) { |
| 975 if (read_if_ready_enabled()) |
| 976 return socket->ReadIfReady(buf, buf_len, callback); |
| 977 return socket->Read(buf, buf_len, callback); |
| 978 } |
| 979 |
| 980 // Wait for Read()/ReadIfReady() to complete. |
| 981 int WaitForReadCompletion(StreamSocket* socket, |
| 982 IOBuffer* buf, |
| 983 int buf_len, |
| 984 TestCompletionCallback* callback, |
| 985 int rv) { |
| 986 if (!read_if_ready_enabled()) |
| 987 return callback->GetResult(rv); |
| 988 while (rv == ERR_IO_PENDING) { |
| 989 rv = callback->GetResult(rv); |
| 990 if (rv != OK) |
| 991 return rv; |
| 992 rv = socket->ReadIfReady(buf, buf_len, callback->callback()); |
| 993 } |
| 994 return rv; |
| 995 } |
| 996 |
| 997 // Calls Read()/ReadIfReady() and waits for it to return data. |
| 998 int ReadAndWaitForCompletion(StreamSocket* socket, |
| 999 IOBuffer* buf, |
| 1000 int buf_len) { |
| 1001 TestCompletionCallback callback; |
| 1002 int rv = Read(socket, buf, buf_len, callback.callback()); |
| 1003 return WaitForReadCompletion(socket, buf, buf_len, &callback, rv); |
| 1004 } |
| 1005 |
| 1006 bool read_if_ready_enabled() const { return read_if_ready_enabled_; } |
| 1007 |
| 1008 private: |
| 1009 base::test::ScopedFeatureList scoped_feature_list_; |
| 1010 const bool read_if_ready_enabled_; |
| 1011 }; |
| 1012 |
| 1013 INSTANTIATE_TEST_CASE_P(/* no prefix */, |
| 1014 SSLClientSocketReadTest, |
| 1015 ::testing::Bool()); |
| 1016 |
861 // Verifies the correctness of GetSSLCertRequestInfo. | 1017 // Verifies the correctness of GetSSLCertRequestInfo. |
862 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest { | 1018 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest { |
863 protected: | 1019 protected: |
864 // Creates a test server with the given SSLOptions, connects to it and returns | 1020 // Creates a test server with the given SSLOptions, connects to it and returns |
865 // the SSLCertRequestInfo reported by the socket. | 1021 // the SSLCertRequestInfo reported by the socket. |
866 scoped_refptr<SSLCertRequestInfo> GetCertRequest( | 1022 scoped_refptr<SSLCertRequestInfo> GetCertRequest( |
867 SpawnedTestServer::SSLOptions ssl_options) { | 1023 SpawnedTestServer::SSLOptions ssl_options) { |
868 SpawnedTestServer spawned_test_server(SpawnedTestServer::TYPE_HTTPS, | 1024 SpawnedTestServer spawned_test_server(SpawnedTestServer::TYPE_HTTPS, |
869 ssl_options, base::FilePath()); | 1025 ssl_options, base::FilePath()); |
870 if (!spawned_test_server.Start()) | 1026 if (!spawned_test_server.Start()) |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1199 EXPECT_FALSE(sock_->IsConnected()); | 1355 EXPECT_FALSE(sock_->IsConnected()); |
1200 } | 1356 } |
1201 | 1357 |
1202 // TODO(wtc): Add unit tests for IsConnectedAndIdle: | 1358 // TODO(wtc): Add unit tests for IsConnectedAndIdle: |
1203 // - Server closes an SSL connection (with a close_notify alert message). | 1359 // - Server closes an SSL connection (with a close_notify alert message). |
1204 // - Server closes the underlying TCP connection directly. | 1360 // - Server closes the underlying TCP connection directly. |
1205 // - Server sends data unexpectedly. | 1361 // - Server sends data unexpectedly. |
1206 | 1362 |
1207 // Tests that the socket can be read from successfully. Also test that a peer's | 1363 // Tests that the socket can be read from successfully. Also test that a peer's |
1208 // close_notify alert is successfully processed without error. | 1364 // close_notify alert is successfully processed without error. |
1209 TEST_F(SSLClientSocketTest, Read) { | 1365 TEST_P(SSLClientSocketReadTest, Read) { |
1210 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1366 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1211 | 1367 |
1212 TestCompletionCallback callback; | 1368 TestCompletionCallback callback; |
1213 std::unique_ptr<StreamSocket> transport( | 1369 std::unique_ptr<StreamSocket> transport( |
1214 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1370 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1215 EXPECT_EQ(0, transport->GetTotalReceivedBytes()); | 1371 EXPECT_EQ(0, transport->GetTotalReceivedBytes()); |
1216 | 1372 |
1217 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1373 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1218 EXPECT_THAT(rv, IsOk()); | 1374 EXPECT_THAT(rv, IsOk()); |
1219 | 1375 |
(...skipping 15 matching lines...) Expand all Loading... |
1235 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); | 1391 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
1236 | 1392 |
1237 rv = callback.GetResult(sock->Write( | 1393 rv = callback.GetResult(sock->Write( |
1238 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); | 1394 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); |
1239 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); | 1395 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
1240 | 1396 |
1241 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1397 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1242 int64_t unencrypted_bytes_read = 0; | 1398 int64_t unencrypted_bytes_read = 0; |
1243 int64_t network_bytes_read_during_handshake = sock->GetTotalReceivedBytes(); | 1399 int64_t network_bytes_read_during_handshake = sock->GetTotalReceivedBytes(); |
1244 do { | 1400 do { |
1245 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | 1401 rv = ReadAndWaitForCompletion(sock.get(), buf.get(), 4096); |
1246 EXPECT_GE(rv, 0); | 1402 EXPECT_GE(rv, 0); |
1247 if (rv >= 0) { | 1403 if (rv >= 0) { |
1248 unencrypted_bytes_read += rv; | 1404 unencrypted_bytes_read += rv; |
1249 } | 1405 } |
1250 } while (rv > 0); | 1406 } while (rv > 0); |
1251 EXPECT_GT(unencrypted_bytes_read, 0); | 1407 EXPECT_GT(unencrypted_bytes_read, 0); |
1252 // Reading the payload should increase the number of bytes on network layer. | 1408 // Reading the payload should increase the number of bytes on network layer. |
1253 EXPECT_GT(sock->GetTotalReceivedBytes(), network_bytes_read_during_handshake); | 1409 EXPECT_GT(sock->GetTotalReceivedBytes(), network_bytes_read_during_handshake); |
1254 // Number of bytes received on the network after the handshake should be | 1410 // Number of bytes received on the network after the handshake should be |
1255 // higher than the number of encrypted bytes read. | 1411 // higher than the number of encrypted bytes read. |
(...skipping 30 matching lines...) Expand all Loading... |
1286 | 1442 |
1287 rv = callback.GetResult(sock->Connect(callback.callback())); | 1443 rv = callback.GetResult(sock->Connect(callback.callback())); |
1288 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1444 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
1289 EXPECT_FALSE(sock->IsConnected()); | 1445 EXPECT_FALSE(sock->IsConnected()); |
1290 } | 1446 } |
1291 | 1447 |
1292 // Tests that the SSLClientSocket properly handles when the underlying transport | 1448 // Tests that the SSLClientSocket properly handles when the underlying transport |
1293 // synchronously returns an error code - such as if an intermediary terminates | 1449 // synchronously returns an error code - such as if an intermediary terminates |
1294 // the socket connection uncleanly. | 1450 // the socket connection uncleanly. |
1295 // This is a regression test for http://crbug.com/238536 | 1451 // This is a regression test for http://crbug.com/238536 |
1296 TEST_F(SSLClientSocketTest, Read_WithSynchronousError) { | 1452 TEST_P(SSLClientSocketReadTest, Read_WithSynchronousError) { |
1297 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1453 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1298 | 1454 |
1299 TestCompletionCallback callback; | 1455 TestCompletionCallback callback; |
1300 std::unique_ptr<StreamSocket> real_transport( | 1456 std::unique_ptr<StreamSocket> real_transport( |
1301 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1457 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1302 std::unique_ptr<SynchronousErrorStreamSocket> transport( | 1458 std::unique_ptr<SynchronousErrorStreamSocket> transport( |
1303 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1459 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1304 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1460 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1305 EXPECT_THAT(rv, IsOk()); | 1461 EXPECT_THAT(rv, IsOk()); |
1306 | 1462 |
(...skipping 21 matching lines...) Expand all Loading... |
1328 EXPECT_EQ(kRequestTextSize, rv); | 1484 EXPECT_EQ(kRequestTextSize, rv); |
1329 | 1485 |
1330 // Simulate an unclean/forcible shutdown. | 1486 // Simulate an unclean/forcible shutdown. |
1331 raw_transport->SetNextReadError(ERR_CONNECTION_RESET); | 1487 raw_transport->SetNextReadError(ERR_CONNECTION_RESET); |
1332 | 1488 |
1333 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1489 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1334 | 1490 |
1335 // Note: This test will hang if this bug has regressed. Simply checking that | 1491 // Note: This test will hang if this bug has regressed. Simply checking that |
1336 // rv != ERR_IO_PENDING is insufficient, as ERR_IO_PENDING is a legitimate | 1492 // rv != ERR_IO_PENDING is insufficient, as ERR_IO_PENDING is a legitimate |
1337 // result when using a dedicated task runner for NSS. | 1493 // result when using a dedicated task runner for NSS. |
1338 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | 1494 rv = ReadAndWaitForCompletion(sock.get(), buf.get(), 4096); |
1339 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1495 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
1340 } | 1496 } |
1341 | 1497 |
1342 // Tests that the SSLClientSocket properly handles when the underlying transport | 1498 // Tests that the SSLClientSocket properly handles when the underlying transport |
1343 // asynchronously returns an error code while writing data - such as if an | 1499 // asynchronously returns an error code while writing data - such as if an |
1344 // intermediary terminates the socket connection uncleanly. | 1500 // intermediary terminates the socket connection uncleanly. |
1345 // This is a regression test for http://crbug.com/249848 | 1501 // This is a regression test for http://crbug.com/249848 |
1346 TEST_F(SSLClientSocketTest, Write_WithSynchronousError) { | 1502 TEST_F(SSLClientSocketTest, Write_WithSynchronousError) { |
1347 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1503 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1348 | 1504 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1463 int old_write_count = raw_counting_socket->write_count(); | 1619 int old_write_count = raw_counting_socket->write_count(); |
1464 base::RunLoop loop; | 1620 base::RunLoop loop; |
1465 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 1621 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
1466 FROM_HERE, loop.QuitClosure(), base::TimeDelta::FromMilliseconds(100)); | 1622 FROM_HERE, loop.QuitClosure(), base::TimeDelta::FromMilliseconds(100)); |
1467 loop.Run(); | 1623 loop.Run(); |
1468 EXPECT_EQ(old_write_count, raw_counting_socket->write_count()); | 1624 EXPECT_EQ(old_write_count, raw_counting_socket->write_count()); |
1469 } | 1625 } |
1470 | 1626 |
1471 // Test the full duplex mode, with Read and Write pending at the same time. | 1627 // Test the full duplex mode, with Read and Write pending at the same time. |
1472 // This test also serves as a regression test for http://crbug.com/29815. | 1628 // This test also serves as a regression test for http://crbug.com/29815. |
1473 TEST_F(SSLClientSocketTest, Read_FullDuplex) { | 1629 TEST_P(SSLClientSocketReadTest, Read_FullDuplex) { |
1474 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1630 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1475 | 1631 |
1476 int rv; | 1632 int rv; |
1477 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 1633 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
1478 EXPECT_THAT(rv, IsOk()); | 1634 EXPECT_THAT(rv, IsOk()); |
1479 | 1635 |
1480 // Issue a "hanging" Read first. | 1636 // Issue a "hanging" Read first. |
1481 TestCompletionCallback callback; | 1637 TestCompletionCallback callback; |
1482 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1638 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1483 rv = sock_->Read(buf.get(), 4096, callback.callback()); | 1639 rv = Read(sock_.get(), buf.get(), 4096, callback.callback()); |
1484 // We haven't written the request, so there should be no response yet. | 1640 // We haven't written the request, so there should be no response yet. |
1485 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); | 1641 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
1486 | 1642 |
1487 // Write the request. | 1643 // Write the request. |
1488 // The request is padded with a User-Agent header to a size that causes the | 1644 // The request is padded with a User-Agent header to a size that causes the |
1489 // memio circular buffer (4k bytes) in SSLClientSocketNSS to wrap around. | 1645 // memio circular buffer (4k bytes) in SSLClientSocketNSS to wrap around. |
1490 // This tests the fix for http://crbug.com/29815. | 1646 // This tests the fix for http://crbug.com/29815. |
1491 std::string request_text = "GET / HTTP/1.1\r\nUser-Agent: long browser name "; | 1647 std::string request_text = "GET / HTTP/1.1\r\nUser-Agent: long browser name "; |
1492 for (int i = 0; i < 3770; ++i) | 1648 for (int i = 0; i < 3770; ++i) |
1493 request_text.push_back('*'); | 1649 request_text.push_back('*'); |
1494 request_text.append("\r\n\r\n"); | 1650 request_text.append("\r\n\r\n"); |
1495 scoped_refptr<IOBuffer> request_buffer(new StringIOBuffer(request_text)); | 1651 scoped_refptr<IOBuffer> request_buffer(new StringIOBuffer(request_text)); |
1496 | 1652 |
1497 TestCompletionCallback callback2; // Used for Write only. | 1653 TestCompletionCallback callback2; // Used for Write only. |
1498 rv = callback2.GetResult(sock_->Write( | 1654 rv = callback2.GetResult(sock_->Write( |
1499 request_buffer.get(), request_text.size(), callback2.callback())); | 1655 request_buffer.get(), request_text.size(), callback2.callback())); |
1500 EXPECT_EQ(static_cast<int>(request_text.size()), rv); | 1656 EXPECT_EQ(static_cast<int>(request_text.size()), rv); |
1501 | 1657 |
1502 // Now get the Read result. | 1658 // Now get the Read result. |
1503 rv = callback.WaitForResult(); | 1659 rv = WaitForReadCompletion(sock_.get(), buf.get(), 4096, &callback, rv); |
1504 EXPECT_GT(rv, 0); | 1660 EXPECT_GT(rv, 0); |
1505 } | 1661 } |
1506 | 1662 |
1507 // Attempts to Read() and Write() from an SSLClientSocketNSS in full duplex | 1663 // Attempts to Read() and Write() from an SSLClientSocketNSS in full duplex |
1508 // mode when the underlying transport is blocked on sending data. When the | 1664 // mode when the underlying transport is blocked on sending data. When the |
1509 // underlying transport completes due to an error, it should invoke both the | 1665 // underlying transport completes due to an error, it should invoke both the |
1510 // Read() and Write() callbacks. If the socket is deleted by the Read() | 1666 // Read() and Write() callbacks. If the socket is deleted by the Read() |
1511 // callback, the Write() callback should not be invoked. | 1667 // callback, the Write() callback should not be invoked. |
1512 // Regression test for http://crbug.com/232633 | 1668 // Regression test for http://crbug.com/232633 |
1513 TEST_F(SSLClientSocketTest, Read_DeleteWhilePendingFullDuplex) { | 1669 TEST_P(SSLClientSocketReadTest, Read_DeleteWhilePendingFullDuplex) { |
1514 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1670 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1515 | 1671 |
1516 TestCompletionCallback callback; | 1672 TestCompletionCallback callback; |
1517 std::unique_ptr<StreamSocket> real_transport( | 1673 std::unique_ptr<StreamSocket> real_transport( |
1518 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1674 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1519 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer | 1675 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer |
1520 // is retained in order to configure additional errors. | 1676 // is retained in order to configure additional errors. |
1521 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( | 1677 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( |
1522 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1678 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1523 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); | 1679 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); |
(...skipping 28 matching lines...) Expand all Loading... |
1552 // ... but have those errors returned asynchronously. Because the Write() will | 1708 // ... but have those errors returned asynchronously. Because the Write() will |
1553 // return first, this will trigger the error. | 1709 // return first, this will trigger the error. |
1554 raw_transport->BlockReadResult(); | 1710 raw_transport->BlockReadResult(); |
1555 raw_transport->BlockWrite(); | 1711 raw_transport->BlockWrite(); |
1556 | 1712 |
1557 // Enqueue a Read() before calling Write(), which should "hang" due to | 1713 // Enqueue a Read() before calling Write(), which should "hang" due to |
1558 // the ERR_IO_PENDING caused by SetReadShouldBlock() and thus return. | 1714 // the ERR_IO_PENDING caused by SetReadShouldBlock() and thus return. |
1559 SSLClientSocket* raw_sock = sock.get(); | 1715 SSLClientSocket* raw_sock = sock.get(); |
1560 DeleteSocketCallback read_callback(sock.release()); | 1716 DeleteSocketCallback read_callback(sock.release()); |
1561 scoped_refptr<IOBuffer> read_buf(new IOBuffer(4096)); | 1717 scoped_refptr<IOBuffer> read_buf(new IOBuffer(4096)); |
1562 rv = raw_sock->Read(read_buf.get(), 4096, read_callback.callback()); | 1718 rv = Read(raw_sock, read_buf.get(), 4096, read_callback.callback()); |
1563 | 1719 |
1564 // Ensure things didn't complete synchronously, otherwise |sock| is invalid. | 1720 // Ensure things didn't complete synchronously, otherwise |sock| is invalid. |
1565 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); | 1721 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
1566 ASSERT_FALSE(read_callback.have_result()); | 1722 ASSERT_FALSE(read_callback.have_result()); |
1567 | 1723 |
1568 // Attempt to write the remaining data. OpenSSL will return that its blocked | 1724 // Attempt to write the remaining data. OpenSSL will return that its blocked |
1569 // because the underlying transport is blocked. | 1725 // because the underlying transport is blocked. |
1570 rv = raw_sock->Write(request_buffer.get(), | 1726 rv = raw_sock->Write(request_buffer.get(), |
1571 request_buffer->BytesRemaining(), | 1727 request_buffer->BytesRemaining(), |
1572 callback.callback()); | 1728 callback.callback()); |
1573 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); | 1729 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
1574 ASSERT_FALSE(callback.have_result()); | 1730 ASSERT_FALSE(callback.have_result()); |
1575 | 1731 |
1576 // Now unblock Write(), which will invoke OnSendComplete and (eventually) | 1732 // Now unblock Write(), which will invoke OnSendComplete and (eventually) |
1577 // call the Read() callback, deleting the socket and thus aborting calling | 1733 // call the Read() callback, deleting the socket and thus aborting calling |
1578 // the Write() callback. | 1734 // the Write() callback. |
1579 raw_transport->UnblockWrite(); | 1735 raw_transport->UnblockWrite(); |
1580 | 1736 |
| 1737 // |read_callback| deletes |sock| so if ReadIfReady() is used, we will get OK |
| 1738 // asynchronously but can't continue reading because the socket is gone. |
1581 rv = read_callback.WaitForResult(); | 1739 rv = read_callback.WaitForResult(); |
1582 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1740 if (read_if_ready_enabled()) { |
| 1741 EXPECT_THAT(rv, IsOk()); |
| 1742 } else { |
| 1743 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
| 1744 } |
1583 | 1745 |
1584 // The Write callback should not have been called. | 1746 // The Write callback should not have been called. |
1585 EXPECT_FALSE(callback.have_result()); | 1747 EXPECT_FALSE(callback.have_result()); |
1586 } | 1748 } |
1587 | 1749 |
1588 // Tests that the SSLClientSocket does not crash if data is received on the | 1750 // Tests that the SSLClientSocket does not crash if data is received on the |
1589 // transport socket after a failing write. This can occur if we have a Write | 1751 // transport socket after a failing write. This can occur if we have a Write |
1590 // error in a SPDY socket. | 1752 // error in a SPDY socket. |
1591 // Regression test for http://crbug.com/335557 | 1753 // Regression test for http://crbug.com/335557 |
1592 TEST_F(SSLClientSocketTest, Read_WithWriteError) { | 1754 TEST_P(SSLClientSocketReadTest, Read_WithWriteError) { |
1593 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1755 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1594 | 1756 |
1595 TestCompletionCallback callback; | 1757 TestCompletionCallback callback; |
1596 std::unique_ptr<StreamSocket> real_transport( | 1758 std::unique_ptr<StreamSocket> real_transport( |
1597 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1759 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1598 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer | 1760 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer |
1599 // is retained in order to configure additional errors. | 1761 // is retained in order to configure additional errors. |
1600 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( | 1762 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( |
1601 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1763 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1602 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); | 1764 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); |
(...skipping 24 matching lines...) Expand all Loading... |
1627 memcpy(request_buffer->data(), request_text, kRequestTextSize); | 1789 memcpy(request_buffer->data(), request_text, kRequestTextSize); |
1628 | 1790 |
1629 rv = callback.GetResult( | 1791 rv = callback.GetResult( |
1630 sock->Write(request_buffer.get(), kRequestTextSize, callback.callback())); | 1792 sock->Write(request_buffer.get(), kRequestTextSize, callback.callback())); |
1631 EXPECT_EQ(kRequestTextSize, rv); | 1793 EXPECT_EQ(kRequestTextSize, rv); |
1632 | 1794 |
1633 // Start a hanging read. | 1795 // Start a hanging read. |
1634 TestCompletionCallback read_callback; | 1796 TestCompletionCallback read_callback; |
1635 raw_transport->BlockReadResult(); | 1797 raw_transport->BlockReadResult(); |
1636 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1798 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1637 rv = sock->Read(buf.get(), 4096, read_callback.callback()); | 1799 rv = Read(sock.get(), buf.get(), 4096, read_callback.callback()); |
1638 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 1800 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
1639 | 1801 |
1640 // Perform another write, but have it fail. Write a request larger than the | 1802 // Perform another write, but have it fail. Write a request larger than the |
1641 // internal socket buffers so that the request hits the underlying transport | 1803 // internal socket buffers so that the request hits the underlying transport |
1642 // socket and detects the error. | 1804 // socket and detects the error. |
1643 std::string long_request_text = | 1805 std::string long_request_text = |
1644 "GET / HTTP/1.1\r\nUser-Agent: long browser name "; | 1806 "GET / HTTP/1.1\r\nUser-Agent: long browser name "; |
1645 long_request_text.append(20 * 1024, '*'); | 1807 long_request_text.append(20 * 1024, '*'); |
1646 long_request_text.append("\r\n\r\n"); | 1808 long_request_text.append("\r\n\r\n"); |
1647 scoped_refptr<DrainableIOBuffer> long_request_buffer(new DrainableIOBuffer( | 1809 scoped_refptr<DrainableIOBuffer> long_request_buffer(new DrainableIOBuffer( |
(...skipping 11 matching lines...) Expand all Loading... |
1659 // Abort if the entire input is ever consumed. The input is larger than | 1821 // Abort if the entire input is ever consumed. The input is larger than |
1660 // the SSLClientSocket's write buffers. | 1822 // the SSLClientSocket's write buffers. |
1661 ASSERT_LT(0, long_request_buffer->BytesRemaining()); | 1823 ASSERT_LT(0, long_request_buffer->BytesRemaining()); |
1662 } | 1824 } |
1663 } while (rv > 0); | 1825 } while (rv > 0); |
1664 | 1826 |
1665 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1827 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
1666 | 1828 |
1667 // At this point the Read result is available. Transport write errors are | 1829 // At this point the Read result is available. Transport write errors are |
1668 // surfaced through Writes. See https://crbug.com/249848. | 1830 // surfaced through Writes. See https://crbug.com/249848. |
1669 rv = read_callback.WaitForResult(); | 1831 rv = WaitForReadCompletion(sock.get(), buf.get(), 4096, &read_callback, rv); |
1670 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1832 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
1671 | 1833 |
1672 // Release the read. This does not cause a crash. | 1834 // Release the read. This does not cause a crash. |
1673 raw_transport->UnblockReadResult(); | 1835 raw_transport->UnblockReadResult(); |
1674 base::RunLoop().RunUntilIdle(); | 1836 base::RunLoop().RunUntilIdle(); |
1675 } | 1837 } |
1676 | 1838 |
1677 // Tests that SSLClientSocket fails the handshake if the underlying | 1839 // Tests that SSLClientSocket fails the handshake if the underlying |
1678 // transport is cleanly closed. | 1840 // transport is cleanly closed. |
1679 TEST_F(SSLClientSocketTest, Connect_WithZeroReturn) { | 1841 TEST_F(SSLClientSocketTest, Connect_WithZeroReturn) { |
(...skipping 15 matching lines...) Expand all Loading... |
1695 raw_transport->SetNextReadError(0); | 1857 raw_transport->SetNextReadError(0); |
1696 | 1858 |
1697 rv = callback.GetResult(sock->Connect(callback.callback())); | 1859 rv = callback.GetResult(sock->Connect(callback.callback())); |
1698 EXPECT_THAT(rv, IsError(ERR_CONNECTION_CLOSED)); | 1860 EXPECT_THAT(rv, IsError(ERR_CONNECTION_CLOSED)); |
1699 EXPECT_FALSE(sock->IsConnected()); | 1861 EXPECT_FALSE(sock->IsConnected()); |
1700 } | 1862 } |
1701 | 1863 |
1702 // Tests that SSLClientSocket returns a Read of size 0 if the underlying socket | 1864 // Tests that SSLClientSocket returns a Read of size 0 if the underlying socket |
1703 // is cleanly closed, but the peer does not send close_notify. | 1865 // is cleanly closed, but the peer does not send close_notify. |
1704 // This is a regression test for https://crbug.com/422246 | 1866 // This is a regression test for https://crbug.com/422246 |
1705 TEST_F(SSLClientSocketTest, Read_WithZeroReturn) { | 1867 TEST_P(SSLClientSocketReadTest, Read_WithZeroReturn) { |
1706 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1868 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1707 | 1869 |
1708 TestCompletionCallback callback; | 1870 TestCompletionCallback callback; |
1709 std::unique_ptr<StreamSocket> real_transport( | 1871 std::unique_ptr<StreamSocket> real_transport( |
1710 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1872 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1711 std::unique_ptr<SynchronousErrorStreamSocket> transport( | 1873 std::unique_ptr<SynchronousErrorStreamSocket> transport( |
1712 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1874 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1713 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1875 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1714 EXPECT_THAT(rv, IsOk()); | 1876 EXPECT_THAT(rv, IsOk()); |
1715 | 1877 |
1716 // Disable TLS False Start to ensure the handshake has completed. | 1878 // Disable TLS False Start to ensure the handshake has completed. |
1717 SSLConfig ssl_config; | 1879 SSLConfig ssl_config; |
1718 ssl_config.false_start_enabled = false; | 1880 ssl_config.false_start_enabled = false; |
1719 | 1881 |
1720 SynchronousErrorStreamSocket* raw_transport = transport.get(); | 1882 SynchronousErrorStreamSocket* raw_transport = transport.get(); |
1721 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( | 1883 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
1722 std::move(transport), spawned_test_server()->host_port_pair(), | 1884 std::move(transport), spawned_test_server()->host_port_pair(), |
1723 ssl_config)); | 1885 ssl_config)); |
1724 | 1886 |
1725 rv = callback.GetResult(sock->Connect(callback.callback())); | 1887 rv = callback.GetResult(sock->Connect(callback.callback())); |
1726 EXPECT_THAT(rv, IsOk()); | 1888 EXPECT_THAT(rv, IsOk()); |
1727 EXPECT_TRUE(sock->IsConnected()); | 1889 EXPECT_TRUE(sock->IsConnected()); |
1728 | 1890 |
1729 raw_transport->SetNextReadError(0); | 1891 raw_transport->SetNextReadError(0); |
1730 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1892 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1731 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | 1893 rv = ReadAndWaitForCompletion(sock.get(), buf.get(), 4096); |
1732 EXPECT_EQ(0, rv); | 1894 EXPECT_EQ(0, rv); |
1733 } | 1895 } |
1734 | 1896 |
1735 // Tests that SSLClientSocket cleanly returns a Read of size 0 if the | 1897 // Tests that SSLClientSocket cleanly returns a Read of size 0 if the |
1736 // underlying socket is cleanly closed asynchronously. | 1898 // underlying socket is cleanly closed asynchronously. |
1737 // This is a regression test for https://crbug.com/422246 | 1899 // This is a regression test for https://crbug.com/422246 |
1738 TEST_F(SSLClientSocketTest, Read_WithAsyncZeroReturn) { | 1900 TEST_P(SSLClientSocketReadTest, Read_WithAsyncZeroReturn) { |
1739 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1901 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1740 | 1902 |
1741 TestCompletionCallback callback; | 1903 TestCompletionCallback callback; |
1742 std::unique_ptr<StreamSocket> real_transport( | 1904 std::unique_ptr<StreamSocket> real_transport( |
1743 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1905 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1744 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( | 1906 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( |
1745 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1907 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1746 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); | 1908 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); |
1747 std::unique_ptr<FakeBlockingStreamSocket> transport( | 1909 std::unique_ptr<FakeBlockingStreamSocket> transport( |
1748 new FakeBlockingStreamSocket(std::move(error_socket))); | 1910 new FakeBlockingStreamSocket(std::move(error_socket))); |
1749 FakeBlockingStreamSocket* raw_transport = transport.get(); | 1911 FakeBlockingStreamSocket* raw_transport = transport.get(); |
1750 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1912 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1751 EXPECT_THAT(rv, IsOk()); | 1913 EXPECT_THAT(rv, IsOk()); |
1752 | 1914 |
1753 // Disable TLS False Start to ensure the handshake has completed. | 1915 // Disable TLS False Start to ensure the handshake has completed. |
1754 SSLConfig ssl_config; | 1916 SSLConfig ssl_config; |
1755 ssl_config.false_start_enabled = false; | 1917 ssl_config.false_start_enabled = false; |
1756 | 1918 |
1757 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( | 1919 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
1758 std::move(transport), spawned_test_server()->host_port_pair(), | 1920 std::move(transport), spawned_test_server()->host_port_pair(), |
1759 ssl_config)); | 1921 ssl_config)); |
1760 | 1922 |
1761 rv = callback.GetResult(sock->Connect(callback.callback())); | 1923 rv = callback.GetResult(sock->Connect(callback.callback())); |
1762 EXPECT_THAT(rv, IsOk()); | 1924 EXPECT_THAT(rv, IsOk()); |
1763 EXPECT_TRUE(sock->IsConnected()); | 1925 EXPECT_TRUE(sock->IsConnected()); |
1764 | 1926 |
1765 raw_error_socket->SetNextReadError(0); | 1927 raw_error_socket->SetNextReadError(0); |
1766 raw_transport->BlockReadResult(); | 1928 raw_transport->BlockReadResult(); |
1767 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1929 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1768 rv = sock->Read(buf.get(), 4096, callback.callback()); | 1930 TestCompletionCallback read_callback; |
| 1931 rv = Read(sock.get(), buf.get(), 4096, read_callback.callback()); |
1769 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 1932 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
1770 | 1933 |
1771 raw_transport->UnblockReadResult(); | 1934 raw_transport->UnblockReadResult(); |
1772 rv = callback.GetResult(rv); | 1935 rv = WaitForReadCompletion(sock.get(), buf.get(), 4096, &read_callback, rv); |
1773 EXPECT_EQ(0, rv); | 1936 EXPECT_EQ(0, rv); |
1774 } | 1937 } |
1775 | 1938 |
1776 // Tests that fatal alerts from the peer are processed. This is a regression | 1939 // Tests that fatal alerts from the peer are processed. This is a regression |
1777 // test for https://crbug.com/466303. | 1940 // test for https://crbug.com/466303. |
1778 TEST_F(SSLClientSocketTest, Read_WithFatalAlert) { | 1941 TEST_P(SSLClientSocketReadTest, Read_WithFatalAlert) { |
1779 SpawnedTestServer::SSLOptions ssl_options; | 1942 SpawnedTestServer::SSLOptions ssl_options; |
1780 ssl_options.alert_after_handshake = true; | 1943 ssl_options.alert_after_handshake = true; |
1781 ASSERT_TRUE(StartTestServer(ssl_options)); | 1944 ASSERT_TRUE(StartTestServer(ssl_options)); |
1782 | 1945 |
1783 int rv; | 1946 int rv; |
1784 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 1947 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
1785 EXPECT_THAT(rv, IsOk()); | 1948 EXPECT_THAT(rv, IsOk()); |
1786 | 1949 |
1787 // Receive the fatal alert. | 1950 // Receive the fatal alert. |
1788 TestCompletionCallback callback; | 1951 TestCompletionCallback callback; |
1789 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1952 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1790 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, callback.GetResult(sock_->Read( | 1953 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, |
1791 buf.get(), 4096, callback.callback()))); | 1954 ReadAndWaitForCompletion(sock_.get(), buf.get(), 4096)); |
1792 } | 1955 } |
1793 | 1956 |
1794 TEST_F(SSLClientSocketTest, Read_SmallChunks) { | 1957 TEST_P(SSLClientSocketReadTest, Read_SmallChunks) { |
1795 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1958 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1796 | 1959 |
1797 int rv; | 1960 int rv; |
1798 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 1961 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
1799 EXPECT_THAT(rv, IsOk()); | 1962 EXPECT_THAT(rv, IsOk()); |
1800 | 1963 |
1801 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 1964 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
1802 scoped_refptr<IOBuffer> request_buffer( | 1965 scoped_refptr<IOBuffer> request_buffer( |
1803 new IOBuffer(arraysize(request_text) - 1)); | 1966 new IOBuffer(arraysize(request_text) - 1)); |
1804 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); | 1967 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
1805 | 1968 |
1806 TestCompletionCallback callback; | 1969 TestCompletionCallback callback; |
1807 rv = callback.GetResult(sock_->Write( | 1970 rv = callback.GetResult(sock_->Write( |
1808 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); | 1971 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); |
1809 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); | 1972 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
1810 | 1973 |
1811 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); | 1974 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); |
1812 do { | 1975 do { |
1813 rv = callback.GetResult(sock_->Read(buf.get(), 1, callback.callback())); | 1976 rv = ReadAndWaitForCompletion(sock_.get(), buf.get(), 1); |
1814 EXPECT_GE(rv, 0); | 1977 EXPECT_GE(rv, 0); |
1815 } while (rv > 0); | 1978 } while (rv > 0); |
1816 } | 1979 } |
1817 | 1980 |
1818 TEST_F(SSLClientSocketTest, Read_ManySmallRecords) { | 1981 TEST_P(SSLClientSocketReadTest, Read_ManySmallRecords) { |
1819 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1982 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1820 | 1983 |
1821 TestCompletionCallback callback; | 1984 TestCompletionCallback callback; |
1822 | 1985 |
1823 std::unique_ptr<StreamSocket> real_transport( | 1986 std::unique_ptr<StreamSocket> real_transport( |
1824 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1987 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1825 std::unique_ptr<ReadBufferingStreamSocket> transport( | 1988 std::unique_ptr<ReadBufferingStreamSocket> transport( |
1826 new ReadBufferingStreamSocket(std::move(real_transport))); | 1989 new ReadBufferingStreamSocket(std::move(real_transport))); |
1827 ReadBufferingStreamSocket* raw_transport = transport.get(); | 1990 ReadBufferingStreamSocket* raw_transport = transport.get(); |
1828 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1991 int rv = callback.GetResult(transport->Connect(callback.callback())); |
(...skipping 22 matching lines...) Expand all Loading... |
1851 // of SSL data is buffered first. The 15K of buffered data is made up of | 2014 // of SSL data is buffered first. The 15K of buffered data is made up of |
1852 // many smaller SSL records (the TestServer writes along 1350 byte | 2015 // many smaller SSL records (the TestServer writes along 1350 byte |
1853 // plaintext boundaries), although there may also be a few records that are | 2016 // plaintext boundaries), although there may also be a few records that are |
1854 // smaller or larger, due to timing and SSL False Start. | 2017 // smaller or larger, due to timing and SSL False Start. |
1855 // 15K was chosen because 15K is smaller than the 17K (max) read issued by | 2018 // 15K was chosen because 15K is smaller than the 17K (max) read issued by |
1856 // the SSLClientSocket implementation, and larger than the minimum amount | 2019 // the SSLClientSocket implementation, and larger than the minimum amount |
1857 // of ciphertext necessary to contain the 8K of plaintext requested below. | 2020 // of ciphertext necessary to contain the 8K of plaintext requested below. |
1858 raw_transport->SetBufferSize(15000); | 2021 raw_transport->SetBufferSize(15000); |
1859 | 2022 |
1860 scoped_refptr<IOBuffer> buffer(new IOBuffer(8192)); | 2023 scoped_refptr<IOBuffer> buffer(new IOBuffer(8192)); |
1861 rv = callback.GetResult(sock->Read(buffer.get(), 8192, callback.callback())); | 2024 rv = ReadAndWaitForCompletion(sock.get(), buffer.get(), 8192); |
1862 ASSERT_EQ(rv, 8192); | 2025 ASSERT_EQ(rv, 8192); |
1863 } | 2026 } |
1864 | 2027 |
1865 TEST_F(SSLClientSocketTest, Read_Interrupted) { | 2028 TEST_P(SSLClientSocketReadTest, Read_Interrupted) { |
1866 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 2029 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1867 | 2030 |
1868 int rv; | 2031 int rv; |
1869 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 2032 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
1870 EXPECT_THAT(rv, IsOk()); | 2033 EXPECT_THAT(rv, IsOk()); |
1871 | 2034 |
1872 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 2035 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
1873 scoped_refptr<IOBuffer> request_buffer( | 2036 scoped_refptr<IOBuffer> request_buffer( |
1874 new IOBuffer(arraysize(request_text) - 1)); | 2037 new IOBuffer(arraysize(request_text) - 1)); |
1875 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); | 2038 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
1876 | 2039 |
1877 TestCompletionCallback callback; | 2040 TestCompletionCallback callback; |
1878 rv = callback.GetResult(sock_->Write( | 2041 rv = callback.GetResult(sock_->Write( |
1879 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); | 2042 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); |
1880 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); | 2043 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
1881 | 2044 |
1882 // Do a partial read and then exit. This test should not crash! | 2045 // Do a partial read and then exit. This test should not crash! |
1883 scoped_refptr<IOBuffer> buf(new IOBuffer(512)); | 2046 scoped_refptr<IOBuffer> buf(new IOBuffer(512)); |
1884 rv = callback.GetResult(sock_->Read(buf.get(), 512, callback.callback())); | 2047 rv = ReadAndWaitForCompletion(sock_.get(), buf.get(), 512); |
1885 EXPECT_GT(rv, 0); | 2048 EXPECT_GT(rv, 0); |
1886 } | 2049 } |
1887 | 2050 |
1888 TEST_F(SSLClientSocketTest, Read_FullLogging) { | 2051 TEST_P(SSLClientSocketReadTest, Read_FullLogging) { |
1889 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 2052 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1890 | 2053 |
1891 TestCompletionCallback callback; | 2054 TestCompletionCallback callback; |
1892 TestNetLog log; | 2055 TestNetLog log; |
1893 log.SetCaptureMode(NetLogCaptureMode::IncludeSocketBytes()); | 2056 log.SetCaptureMode(NetLogCaptureMode::IncludeSocketBytes()); |
1894 std::unique_ptr<StreamSocket> transport( | 2057 std::unique_ptr<StreamSocket> transport( |
1895 new TCPClientSocket(addr(), NULL, &log, NetLogSource())); | 2058 new TCPClientSocket(addr(), NULL, &log, NetLogSource())); |
1896 int rv = callback.GetResult(transport->Connect(callback.callback())); | 2059 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1897 EXPECT_THAT(rv, IsOk()); | 2060 EXPECT_THAT(rv, IsOk()); |
1898 | 2061 |
(...skipping 15 matching lines...) Expand all Loading... |
1914 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); | 2077 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
1915 | 2078 |
1916 TestNetLogEntry::List entries; | 2079 TestNetLogEntry::List entries; |
1917 log.GetEntries(&entries); | 2080 log.GetEntries(&entries); |
1918 size_t last_index = ExpectLogContainsSomewhereAfter( | 2081 size_t last_index = ExpectLogContainsSomewhereAfter( |
1919 entries, 5, NetLogEventType::SSL_SOCKET_BYTES_SENT, | 2082 entries, 5, NetLogEventType::SSL_SOCKET_BYTES_SENT, |
1920 NetLogEventPhase::NONE); | 2083 NetLogEventPhase::NONE); |
1921 | 2084 |
1922 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 2085 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1923 for (;;) { | 2086 for (;;) { |
1924 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | 2087 rv = ReadAndWaitForCompletion(sock.get(), buf.get(), 4096); |
1925 EXPECT_GE(rv, 0); | 2088 EXPECT_GE(rv, 0); |
1926 if (rv <= 0) | 2089 if (rv <= 0) |
1927 break; | 2090 break; |
1928 | 2091 |
1929 log.GetEntries(&entries); | 2092 log.GetEntries(&entries); |
1930 last_index = ExpectLogContainsSomewhereAfter( | 2093 last_index = ExpectLogContainsSomewhereAfter( |
1931 entries, last_index + 1, NetLogEventType::SSL_SOCKET_BYTES_RECEIVED, | 2094 entries, last_index + 1, NetLogEventType::SSL_SOCKET_BYTES_RECEIVED, |
1932 NetLogEventPhase::NONE); | 2095 NetLogEventPhase::NONE); |
1933 } | 2096 } |
1934 } | 2097 } |
(...skipping 1651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3586 // Replace it with an alert. | 3749 // Replace it with an alert. |
3587 raw_transport->ReplaceReadResult( | 3750 raw_transport->ReplaceReadResult( |
3588 FormatTLS12Alert(49 /* AlertDescription.access_denied */)); | 3751 FormatTLS12Alert(49 /* AlertDescription.access_denied */)); |
3589 raw_transport->UnblockReadResult(); | 3752 raw_transport->UnblockReadResult(); |
3590 | 3753 |
3591 rv = callback.GetResult(rv); | 3754 rv = callback.GetResult(rv); |
3592 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT)); | 3755 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT)); |
3593 } | 3756 } |
3594 | 3757 |
3595 // Basic test for dumping memory stats. | 3758 // Basic test for dumping memory stats. |
3596 TEST_F(SSLClientSocketTest, DumpMemoryStats) { | 3759 TEST_P(SSLClientSocketReadTest, DumpMemoryStats) { |
3597 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 3760 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
3598 | 3761 |
3599 int rv; | 3762 int rv; |
3600 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 3763 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
3601 EXPECT_THAT(rv, IsOk()); | 3764 EXPECT_THAT(rv, IsOk()); |
3602 StreamSocket::SocketMemoryStats stats; | 3765 StreamSocket::SocketMemoryStats stats; |
3603 sock_->DumpMemoryStats(&stats); | 3766 sock_->DumpMemoryStats(&stats); |
3604 EXPECT_EQ(0u, stats.buffer_size); | 3767 EXPECT_EQ(0u, stats.buffer_size); |
3605 EXPECT_EQ(1u, stats.cert_count); | 3768 EXPECT_EQ(1u, stats.cert_count); |
3606 EXPECT_LT(0u, stats.cert_size); | 3769 EXPECT_LT(0u, stats.cert_size); |
3607 EXPECT_EQ(stats.cert_size, stats.total_size); | 3770 EXPECT_EQ(stats.cert_size, stats.total_size); |
3608 | 3771 |
3609 // Read the response without writing a request, so the read will be pending. | 3772 // Read the response without writing a request, so the read will be pending. |
3610 TestCompletionCallback read_callback; | 3773 TestCompletionCallback read_callback; |
3611 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 3774 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
3612 rv = sock_->Read(buf.get(), 4096, read_callback.callback()); | 3775 rv = Read(sock_.get(), buf.get(), 4096, read_callback.callback()); |
3613 EXPECT_EQ(ERR_IO_PENDING, rv); | 3776 EXPECT_EQ(ERR_IO_PENDING, rv); |
3614 | 3777 |
3615 // Dump memory again and check that |buffer_size| contain the read buffer. | 3778 // Dump memory again and check that |buffer_size| contain the read buffer. |
3616 StreamSocket::SocketMemoryStats stats2; | 3779 StreamSocket::SocketMemoryStats stats2; |
3617 sock_->DumpMemoryStats(&stats2); | 3780 sock_->DumpMemoryStats(&stats2); |
3618 EXPECT_EQ(17 * 1024u, stats2.buffer_size); | 3781 |
| 3782 bool buffer_released = false; |
| 3783 if (read_if_ready_enabled()) { |
| 3784 // TODO(xunjieli): https://crbug.com/690915. Implement for windows. |
| 3785 #if defined(OS_POSIX) |
| 3786 buffer_released = true; |
| 3787 #endif |
| 3788 } |
| 3789 if (buffer_released) { |
| 3790 EXPECT_EQ(0u, stats2.buffer_size); |
| 3791 EXPECT_EQ(stats.cert_size, stats2.total_size); |
| 3792 } else { |
| 3793 EXPECT_EQ(17 * 1024u, stats2.buffer_size); |
| 3794 EXPECT_LT(17 * 1024u, stats2.total_size); |
| 3795 } |
3619 EXPECT_EQ(1u, stats2.cert_count); | 3796 EXPECT_EQ(1u, stats2.cert_count); |
3620 EXPECT_LT(0u, stats2.cert_size); | 3797 EXPECT_LT(0u, stats2.cert_size); |
3621 EXPECT_LT(17 * 1024u, stats2.total_size); | |
3622 } | 3798 } |
3623 | 3799 |
3624 } // namespace net | 3800 } // namespace net |
OLD | NEW |