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

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

Issue 2593063003: Add Socket::ReadIfReady() (Closed)
Patch Set: Self Created 3 years, 10 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
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/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 #include <algorithm>
davidben 2017/02/10 23:33:48 Nit: Swap this and the blank line below.
xunjieli 2017/02/13 20:28:18 Done.
9 10
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/stl_util.h"
22 #include "base/test/scoped_feature_list.h"
20 #include "base/test/scoped_task_scheduler.h" 23 #include "base/test/scoped_task_scheduler.h"
21 #include "base/threading/thread_task_runner_handle.h" 24 #include "base/threading/thread_task_runner_handle.h"
22 #include "base/time/time.h" 25 #include "base/time/time.h"
23 #include "base/values.h" 26 #include "base/values.h"
24 #include "net/base/address_list.h" 27 #include "net/base/address_list.h"
25 #include "net/base/io_buffer.h" 28 #include "net/base/io_buffer.h"
26 #include "net/base/net_errors.h" 29 #include "net/base/net_errors.h"
27 #include "net/base/test_completion_callback.h" 30 #include "net/base/test_completion_callback.h"
28 #include "net/cert/asn1_util.h" 31 #include "net/cert/asn1_util.h"
29 #include "net/cert/ct_policy_enforcer.h" 32 #include "net/cert/ct_policy_enforcer.h"
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 int64_t GetTotalReceivedBytes() const override { 138 int64_t GetTotalReceivedBytes() const override {
136 return transport_->GetTotalReceivedBytes(); 139 return transport_->GetTotalReceivedBytes();
137 } 140 }
138 141
139 // Socket implementation: 142 // Socket implementation:
140 int Read(IOBuffer* buf, 143 int Read(IOBuffer* buf,
141 int buf_len, 144 int buf_len,
142 const CompletionCallback& callback) override { 145 const CompletionCallback& callback) override {
143 return transport_->Read(buf, buf_len, callback); 146 return transport_->Read(buf, buf_len, callback);
144 } 147 }
148 int ReadIfReady(IOBuffer* buf,
149 int buf_len,
150 const CompletionCallback& callback) override {
151 return transport_->ReadIfReady(buf, buf_len, callback);
152 }
145 int Write(IOBuffer* buf, 153 int Write(IOBuffer* buf,
146 int buf_len, 154 int buf_len,
147 const CompletionCallback& callback) override { 155 const CompletionCallback& callback) override {
148 return transport_->Write(buf, buf_len, callback); 156 return transport_->Write(buf, buf_len, callback);
149 } 157 }
150 int SetReceiveBufferSize(int32_t size) override { 158 int SetReceiveBufferSize(int32_t size) override {
151 return transport_->SetReceiveBufferSize(size); 159 return transport_->SetReceiveBufferSize(size);
152 } 160 }
153 int SetSendBufferSize(int32_t size) override { 161 int SetSendBufferSize(int32_t size) override {
154 return transport_->SetSendBufferSize(size); 162 return transport_->SetSendBufferSize(size);
(...skipping 12 matching lines...) Expand all
167 class ReadBufferingStreamSocket : public WrappedStreamSocket { 175 class ReadBufferingStreamSocket : public WrappedStreamSocket {
168 public: 176 public:
169 explicit ReadBufferingStreamSocket(std::unique_ptr<StreamSocket> transport); 177 explicit ReadBufferingStreamSocket(std::unique_ptr<StreamSocket> transport);
170 ~ReadBufferingStreamSocket() override {} 178 ~ReadBufferingStreamSocket() override {}
171 179
172 // Socket implementation: 180 // Socket implementation:
173 int Read(IOBuffer* buf, 181 int Read(IOBuffer* buf,
174 int buf_len, 182 int buf_len,
175 const CompletionCallback& callback) override; 183 const CompletionCallback& callback) override;
176 184
185 int ReadIfReady(IOBuffer* buf,
186 int buf_len,
187 const CompletionCallback& callback) override;
188
177 // Sets the internal buffer to |size|. This must not be greater than 189 // 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 190 // the largest value supplied to Read() - that is, it does not handle
179 // having "leftovers" at the end of Read(). 191 // having "leftovers" at the end of Read().
180 // Each call to Read() will be prevented from completion until at least 192 // Each call to Read() will be prevented from completion until at least
181 // |size| data has been read. 193 // |size| data has been read.
182 // Set to 0 to turn off buffering, causing Read() to transparently 194 // Set to 0 to turn off buffering, causing Read() to transparently
183 // read via the underlying transport. 195 // read via the underlying transport.
184 void SetBufferSize(int size); 196 void SetBufferSize(int size);
185 197
186 private: 198 private:
(...skipping 16 matching lines...) Expand all
203 CompletionCallback user_read_callback_; 215 CompletionCallback user_read_callback_;
204 }; 216 };
205 217
206 ReadBufferingStreamSocket::ReadBufferingStreamSocket( 218 ReadBufferingStreamSocket::ReadBufferingStreamSocket(
207 std::unique_ptr<StreamSocket> transport) 219 std::unique_ptr<StreamSocket> transport)
208 : WrappedStreamSocket(std::move(transport)), 220 : WrappedStreamSocket(std::move(transport)),
209 read_buffer_(new GrowableIOBuffer()), 221 read_buffer_(new GrowableIOBuffer()),
210 buffer_size_(0) {} 222 buffer_size_(0) {}
211 223
212 void ReadBufferingStreamSocket::SetBufferSize(int size) { 224 void ReadBufferingStreamSocket::SetBufferSize(int size) {
213 DCHECK(!user_read_buf_.get()); 225 DCHECK(!user_read_buf_);
214 buffer_size_ = size; 226 buffer_size_ = size;
215 read_buffer_->SetCapacity(size); 227 read_buffer_->SetCapacity(size);
216 } 228 }
217 229
218 int ReadBufferingStreamSocket::Read(IOBuffer* buf, 230 int ReadBufferingStreamSocket::Read(IOBuffer* buf,
219 int buf_len, 231 int buf_len,
220 const CompletionCallback& callback) { 232 const CompletionCallback& callback) {
233 DCHECK(!user_read_buf_);
221 if (buffer_size_ == 0) 234 if (buffer_size_ == 0)
222 return transport_->Read(buf, buf_len, callback); 235 return transport_->Read(buf, buf_len, callback);
236 int rv = ReadIfReady(buf, buf_len, callback);
237 if (rv == ERR_IO_PENDING)
238 user_read_buf_ = buf;
239 return rv;
240 }
241
242 int ReadBufferingStreamSocket::ReadIfReady(IOBuffer* buf,
243 int buf_len,
244 const CompletionCallback& callback) {
245 DCHECK(!user_read_buf_);
246 if (buffer_size_ == 0)
247 return transport_->ReadIfReady(buf, buf_len, callback);
248
249 if (read_buffer_->RemainingCapacity() == 0) {
250 memcpy(buf->data(), read_buffer_->StartOfBuffer(),
251 read_buffer_->capacity());
252 read_buffer_->set_offset(0);
253 return read_buffer_->capacity();
254 }
223 255
224 if (buf_len < buffer_size_) 256 if (buf_len < buffer_size_)
225 return ERR_UNEXPECTED; 257 return ERR_UNEXPECTED;
226
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 used.
308 if (user_read_buf_ == nullptr)
309 return OK;
310
280 memcpy(user_read_buf_->data(), 311 memcpy(user_read_buf_->data(),
281 read_buffer_->StartOfBuffer(), 312 read_buffer_->StartOfBuffer(),
282 read_buffer_->capacity()); 313 read_buffer_->capacity());
283 read_buffer_->set_offset(0); 314 read_buffer_->set_offset(0);
284 return read_buffer_->capacity(); 315 return read_buffer_->capacity();
285 } 316 }
286 317
287 void ReadBufferingStreamSocket::OnReadCompleted(int result) { 318 void ReadBufferingStreamSocket::OnReadCompleted(int result) {
319 DCHECK_NE(ERR_IO_PENDING, result);
320 DCHECK(user_read_callback_);
321
288 result = DoLoop(result); 322 result = DoLoop(result);
289 if (result == ERR_IO_PENDING) 323 if (result == ERR_IO_PENDING)
290 return; 324 return;
291 325 user_read_buf_ = nullptr;
292 user_read_buf_ = NULL;
293 base::ResetAndReturn(&user_read_callback_).Run(result); 326 base::ResetAndReturn(&user_read_callback_).Run(result);
294 } 327 }
295 328
296 // Simulates synchronously receiving an error during Read() or Write() 329 // Simulates synchronously receiving an error during Read() or Write()
297 class SynchronousErrorStreamSocket : public WrappedStreamSocket { 330 class SynchronousErrorStreamSocket : public WrappedStreamSocket {
298 public: 331 public:
299 explicit SynchronousErrorStreamSocket(std::unique_ptr<StreamSocket> transport) 332 explicit SynchronousErrorStreamSocket(std::unique_ptr<StreamSocket> transport)
300 : WrappedStreamSocket(std::move(transport)) {} 333 : WrappedStreamSocket(std::move(transport)) {}
301 ~SynchronousErrorStreamSocket() override {} 334 ~SynchronousErrorStreamSocket() override {}
302 335
303 // Socket implementation: 336 // Socket implementation:
304 int Read(IOBuffer* buf, 337 int Read(IOBuffer* buf,
305 int buf_len, 338 int buf_len,
306 const CompletionCallback& callback) override; 339 const CompletionCallback& callback) override;
340 int ReadIfReady(IOBuffer* buf,
341 int buf_len,
342 const CompletionCallback& callback) override;
307 int Write(IOBuffer* buf, 343 int Write(IOBuffer* buf,
308 int buf_len, 344 int buf_len,
309 const CompletionCallback& callback) override; 345 const CompletionCallback& callback) override;
310 346
311 // Sets the next Read() call and all future calls to return |error|. 347 // Sets the next Read() call and all future calls to return |error|.
312 // If there is already a pending asynchronous read, the configured error 348 // If there is already a pending asynchronous read, the configured error
313 // will not be returned until that asynchronous read has completed and Read() 349 // will not be returned until that asynchronous read has completed and Read()
314 // is called again. 350 // is called again.
315 void SetNextReadError(int error) { 351 void SetNextReadError(int error) {
316 DCHECK_GE(0, error); 352 DCHECK_GE(0, error);
(...skipping 22 matching lines...) Expand all
339 }; 375 };
340 376
341 int SynchronousErrorStreamSocket::Read(IOBuffer* buf, 377 int SynchronousErrorStreamSocket::Read(IOBuffer* buf,
342 int buf_len, 378 int buf_len,
343 const CompletionCallback& callback) { 379 const CompletionCallback& callback) {
344 if (have_read_error_) 380 if (have_read_error_)
345 return pending_read_error_; 381 return pending_read_error_;
346 return transport_->Read(buf, buf_len, callback); 382 return transport_->Read(buf, buf_len, callback);
347 } 383 }
348 384
385 int SynchronousErrorStreamSocket::ReadIfReady(
386 IOBuffer* buf,
387 int buf_len,
388 const CompletionCallback& callback) {
389 if (have_read_error_)
390 return pending_read_error_;
391 return transport_->ReadIfReady(buf, buf_len, callback);
392 }
393
349 int SynchronousErrorStreamSocket::Write(IOBuffer* buf, 394 int SynchronousErrorStreamSocket::Write(IOBuffer* buf,
350 int buf_len, 395 int buf_len,
351 const CompletionCallback& callback) { 396 const CompletionCallback& callback) {
352 if (have_write_error_) 397 if (have_write_error_)
353 return pending_write_error_; 398 return pending_write_error_;
354 return transport_->Write(buf, buf_len, callback); 399 return transport_->Write(buf, buf_len, callback);
355 } 400 }
356 401
357 // FakeBlockingStreamSocket wraps an existing StreamSocket and simulates the 402 // FakeBlockingStreamSocket wraps an existing StreamSocket and simulates the
358 // underlying transport needing to complete things asynchronously in a 403 // underlying transport needing to complete things asynchronously in a
359 // deterministic manner (e.g.: independent of the TestServer and the OS's 404 // deterministic manner (e.g.: independent of the TestServer and the OS's
360 // semantics). 405 // semantics).
361 class FakeBlockingStreamSocket : public WrappedStreamSocket { 406 class FakeBlockingStreamSocket : public WrappedStreamSocket {
362 public: 407 public:
363 explicit FakeBlockingStreamSocket(std::unique_ptr<StreamSocket> transport) 408 explicit FakeBlockingStreamSocket(std::unique_ptr<StreamSocket> transport)
364 : WrappedStreamSocket(std::move(transport)) {} 409 : WrappedStreamSocket(std::move(transport)) {}
365 ~FakeBlockingStreamSocket() override {} 410 ~FakeBlockingStreamSocket() override {}
366 411
367 // Socket implementation: 412 // Socket implementation:
368 int Read(IOBuffer* buf, 413 int Read(IOBuffer* buf,
369 int buf_len, 414 int buf_len,
370 const CompletionCallback& callback) override; 415 const CompletionCallback& callback) override;
416 int ReadIfReady(IOBuffer* buf,
417 int buf_len,
418 const CompletionCallback& callback) override;
371 int Write(IOBuffer* buf, 419 int Write(IOBuffer* buf,
372 int buf_len, 420 int buf_len,
373 const CompletionCallback& callback) override; 421 const CompletionCallback& callback) override;
374 422
375 int pending_read_result() const { return pending_read_result_; } 423 int pending_read_result() const { return pending_read_result_; }
376 IOBuffer* pending_read_buf() const { return pending_read_buf_.get(); } 424 IOBuffer* pending_read_buf() const { return pending_read_buf_.get(); }
377 425
378 // Blocks read results on the socket. Reads will not complete until 426 // Blocks read results on the socket. Reads will not complete until
379 // UnblockReadResult() has been called and a result is ready from the 427 // UnblockReadResult() has been called and a result is ready from the
380 // underlying transport. Note: if BlockReadResult() is called while there is a 428 // underlying transport. Note: if BlockReadResult() is called while there is a
(...skipping 23 matching lines...) Expand all
404 private: 452 private:
405 // Handles completion from the underlying transport read. 453 // Handles completion from the underlying transport read.
406 void OnReadCompleted(int result); 454 void OnReadCompleted(int result);
407 455
408 // Finishes the current read. 456 // Finishes the current read.
409 void ReturnReadResult(); 457 void ReturnReadResult();
410 458
411 // True if read callbacks are blocked. 459 // True if read callbacks are blocked.
412 bool should_block_read_ = false; 460 bool should_block_read_ = false;
413 461
462 // Used to buffer result returned by a synchronously completed ReadIfReady()
463 // which needs to be blocked.
464 std::string blocked_sync_read_if_ready_data_;
davidben 2017/02/10 23:33:48 Hah! Writing into a buffer like that hadn't occure
xunjieli 2017/02/13 20:28:18 Acknowledged.
465 // True if a synchronous ReadIfReady() has been blocked. Reset to false when
466 // the data from the blocked sync ReadIfReady() is consumed.
467 bool has_blocked_sync_read_if_ready_ = false;
468
414 // The buffer for the pending read, or NULL if not consumed. 469 // The buffer for the pending read, or NULL if not consumed.
415 scoped_refptr<IOBuffer> pending_read_buf_; 470 scoped_refptr<IOBuffer> pending_read_buf_;
416 471
417 // The size of the pending read buffer, or -1 if not set. 472 // The size of the pending read buffer, or -1 if not set.
418 int pending_read_buf_len_ = -1; 473 int pending_read_buf_len_ = -1;
419 474
420 // The user callback for the pending read call. 475 // The user callback for the pending read call.
421 CompletionCallback pending_read_callback_; 476 CompletionCallback pending_read_callback_;
422 477
423 // The result for the blocked read callback, or ERR_IO_PENDING if not 478 // The result for the blocked read callback, or ERR_IO_PENDING if not
(...skipping 20 matching lines...) Expand all
444 }; 499 };
445 500
446 int FakeBlockingStreamSocket::Read(IOBuffer* buf, 501 int FakeBlockingStreamSocket::Read(IOBuffer* buf,
447 int len, 502 int len,
448 const CompletionCallback& callback) { 503 const CompletionCallback& callback) {
449 DCHECK(!pending_read_buf_); 504 DCHECK(!pending_read_buf_);
450 DCHECK(pending_read_callback_.is_null()); 505 DCHECK(pending_read_callback_.is_null());
451 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); 506 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_);
452 DCHECK(!callback.is_null()); 507 DCHECK(!callback.is_null());
453 508
454 int rv = transport_->Read(buf, len, base::Bind( 509 int rv = transport_->Read(
455 &FakeBlockingStreamSocket::OnReadCompleted, base::Unretained(this))); 510 buf, len, base::Bind(&FakeBlockingStreamSocket::OnReadCompleted,
511 base::Unretained(this)));
456 if (rv == ERR_IO_PENDING || should_block_read_) { 512 if (rv == ERR_IO_PENDING || should_block_read_) {
457 // Save the callback to be called later. 513 // Save the callback to be called later.
458 pending_read_buf_ = buf; 514 pending_read_buf_ = buf;
459 pending_read_buf_len_ = len; 515 pending_read_buf_len_ = len;
460 pending_read_callback_ = callback; 516 pending_read_callback_ = callback;
461 // Save the read result. 517 // Save the read result.
462 if (rv != ERR_IO_PENDING) { 518 if (rv != ERR_IO_PENDING) {
463 OnReadCompleted(rv); 519 OnReadCompleted(rv);
464 rv = ERR_IO_PENDING; 520 rv = ERR_IO_PENDING;
465 } 521 }
466 } 522 }
467 return rv; 523 return rv;
468 } 524 }
469 525
526 int FakeBlockingStreamSocket::ReadIfReady(IOBuffer* buf,
527 int len,
528 const CompletionCallback& callback) {
529 if (has_blocked_sync_read_if_ready_) {
davidben 2017/02/10 23:33:48 I think this doesn't quite work if should_block_re
xunjieli 2017/02/13 20:28:18 Acknowledged.
530 DCHECK(!pending_read_callback_);
531 DCHECK(pending_read_buf_);
532 DCHECK_NE(ERR_IO_PENDING, pending_read_result_);
533 if (pending_read_result_ > 0) {
534 DCHECK_LE(0, pending_read_buf_len_);
535 int bytes_read = std::min(len, pending_read_buf_len_);
536 memcpy(buf->data(), pending_read_buf_->data(), bytes_read);
537 blocked_sync_read_if_ready_data_.erase(0, bytes_read);
538 if (!blocked_sync_read_if_ready_data_.empty()) {
539 pending_read_buf_ =
540 new StringIOBuffer(blocked_sync_read_if_ready_data_);
541 pending_read_buf_len_ = blocked_sync_read_if_ready_data_.length();
542 pending_read_result_ = pending_read_buf_len_;
543 return bytes_read;
544 }
545 }
546 int rv = pending_read_result_;
547 has_blocked_sync_read_if_ready_ = false;
548 pending_read_buf_ = nullptr;
549 pending_read_buf_len_ = -1;
550 pending_read_result_ = ERR_IO_PENDING;
551 return rv;
552 }
553 DCHECK(!pending_read_buf_);
554 DCHECK(pending_read_callback_.is_null());
555 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_);
556 DCHECK(!callback.is_null());
557
558 int rv = transport_->ReadIfReady(
559 buf, len, base::Bind(&FakeBlockingStreamSocket::OnReadCompleted,
560 base::Unretained(this)));
561 if (rv == ERR_IO_PENDING) {
562 pending_read_callback_ = callback;
563 return rv;
564 }
565 if (should_block_read_) {
566 // Save the data to be called later.
567 if (rv >= 0)
568 blocked_sync_read_if_ready_data_.append(buf->data(), rv);
569 has_blocked_sync_read_if_ready_ = true;
570 // Save the callback to be called later.
571 pending_read_buf_ = new StringIOBuffer(blocked_sync_read_if_ready_data_);
davidben 2017/02/10 23:33:48 Do you need both the std::string and the StringIOB
xunjieli 2017/02/13 20:28:19 Done. Neat! I somehow though StringIOBuffer doesn'
572 pending_read_buf_len_ = len;
573 pending_read_callback_ = callback;
574 OnReadCompleted(rv);
575 rv = ERR_IO_PENDING;
576 }
577 return rv;
578 }
579
470 int FakeBlockingStreamSocket::Write(IOBuffer* buf, 580 int FakeBlockingStreamSocket::Write(IOBuffer* buf,
471 int len, 581 int len,
472 const CompletionCallback& callback) { 582 const CompletionCallback& callback) {
473 DCHECK(buf); 583 DCHECK(buf);
474 DCHECK_LE(0, len); 584 DCHECK_LE(0, len);
475 585
476 if (!should_block_write_) 586 if (!should_block_write_)
477 return transport_->Write(buf, len, callback); 587 return transport_->Write(buf, len, callback);
478 588
479 // Schedule the write, but do nothing. 589 // Schedule the write, but do nothing.
(...skipping 20 matching lines...) Expand all
500 DCHECK(should_block_read_); 610 DCHECK(should_block_read_);
501 should_block_read_ = false; 611 should_block_read_ = false;
502 612
503 // If the operation has since completed, return the result to the caller. 613 // If the operation has since completed, return the result to the caller.
504 if (pending_read_result_ != ERR_IO_PENDING) 614 if (pending_read_result_ != ERR_IO_PENDING)
505 ReturnReadResult(); 615 ReturnReadResult();
506 } 616 }
507 617
508 bool FakeBlockingStreamSocket::ReplaceReadResult(const std::string& data) { 618 bool FakeBlockingStreamSocket::ReplaceReadResult(const std::string& data) {
509 DCHECK(should_block_read_); 619 DCHECK(should_block_read_);
510 DCHECK_NE(ERR_IO_PENDING, pending_read_result_);
511 DCHECK(pending_read_buf_); 620 DCHECK(pending_read_buf_);
512 DCHECK_NE(-1, pending_read_buf_len_); 621 DCHECK_NE(-1, pending_read_buf_len_);
513 622
514 if (static_cast<size_t>(pending_read_buf_len_) < data.size()) 623 if (static_cast<size_t>(pending_read_buf_len_) < data.size())
515 return false; 624 return false;
516 625
626 if (has_blocked_sync_read_if_ready_)
627 blocked_sync_read_if_ready_data_ = data;
628
629 DCHECK_NE(ERR_IO_PENDING, pending_read_result_);
630 DCHECK(pending_read_buf_);
631 DCHECK_NE(-1, pending_read_buf_len_);
517 memcpy(pending_read_buf_->data(), data.data(), data.size()); 632 memcpy(pending_read_buf_->data(), data.data(), data.size());
518 pending_read_result_ = data.size(); 633 pending_read_result_ = data.size();
519 return true; 634 return true;
520 } 635 }
521 636
522 void FakeBlockingStreamSocket::WaitForReadResult() { 637 void FakeBlockingStreamSocket::WaitForReadResult() {
523 DCHECK(should_block_read_); 638 DCHECK(should_block_read_);
524 DCHECK(!read_loop_); 639 DCHECK(!read_loop_);
525 640
526 if (pending_read_result_ != ERR_IO_PENDING) 641 if (pending_read_result_ != ERR_IO_PENDING)
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 if (read_loop_) 694 if (read_loop_)
580 read_loop_->Quit(); 695 read_loop_->Quit();
581 return; 696 return;
582 } 697 }
583 698
584 ReturnReadResult(); 699 ReturnReadResult();
585 } 700 }
586 701
587 void FakeBlockingStreamSocket::ReturnReadResult() { 702 void FakeBlockingStreamSocket::ReturnReadResult() {
588 int result = pending_read_result_; 703 int result = pending_read_result_;
704 if (has_blocked_sync_read_if_ready_) {
705 base::ResetAndReturn(&pending_read_callback_).Run(result > 0 ? OK : result);
706 return;
707 }
589 pending_read_result_ = ERR_IO_PENDING; 708 pending_read_result_ = ERR_IO_PENDING;
590 pending_read_buf_ = nullptr; 709 pending_read_buf_ = nullptr;
591 pending_read_buf_len_ = -1; 710 pending_read_buf_len_ = -1;
592 base::ResetAndReturn(&pending_read_callback_).Run(result); 711 base::ResetAndReturn(&pending_read_callback_).Run(result);
593 } 712 }
594 713
595 // CountingStreamSocket wraps an existing StreamSocket and maintains a count of 714 // CountingStreamSocket wraps an existing StreamSocket and maintains a count of
596 // reads and writes on the socket. 715 // reads and writes on the socket.
597 class CountingStreamSocket : public WrappedStreamSocket { 716 class CountingStreamSocket : public WrappedStreamSocket {
598 public: 717 public:
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
851 SSLClientSocketContext context_; 970 SSLClientSocketContext context_;
852 std::unique_ptr<SSLClientSocket> sock_; 971 std::unique_ptr<SSLClientSocket> sock_;
853 TestNetLog log_; 972 TestNetLog log_;
854 973
855 private: 974 private:
856 std::unique_ptr<SpawnedTestServer> spawned_test_server_; 975 std::unique_ptr<SpawnedTestServer> spawned_test_server_;
857 TestCompletionCallback callback_; 976 TestCompletionCallback callback_;
858 AddressList addr_; 977 AddressList addr_;
859 }; 978 };
860 979
980 // If GetParam(), try ReadIfReady() and fall back to Read() if needed.
981 class SSLClientSocketReadTest : public SSLClientSocketTest,
982 public ::testing::WithParamInterface<bool> {
983 protected:
984 void SetUp() override {
985 if (GetParam())
986 scoped_feature_list_.InitAndEnableFeature(Socket::kReadIfReadyExperiment);
987 }
988
989 // Convienient wrapper to call Read() or ReadIfReady() depending on
990 // GetParam().
991 int Read(StreamSocket* socket,
992 IOBuffer* buf,
993 int buf_len,
994 const CompletionCallback& callback) {
995 if (GetParam())
996 return socket->ReadIfReady(buf, buf_len, callback);
997 return socket->Read(buf, buf_len, callback);
998 }
999
1000 // Calls Read()/ReadIfReady() and waits for it to return data.
1001 int ReadAndWaitForCompletion(StreamSocket* socket,
1002 IOBuffer* buf,
1003 int buf_len) {
1004 TestCompletionCallback callback;
1005 if (GetParam()) {
1006 int rv = socket->ReadIfReady(buf, buf_len, callback.callback());
1007 if (rv == ERR_IO_PENDING) {
1008 rv = callback.GetResult(rv);
1009 if (rv == OK) {
1010 rv = socket->ReadIfReady(buf, buf_len, callback.callback());
1011 DCHECK_NE(ERR_IO_PENDING, rv);
1012 }
1013 }
1014 return rv;
davidben 2017/02/10 23:33:48 It's legal for ReadIfReady to signal OK before it'
xunjieli 2017/02/13 20:28:18 Done.
1015 }
1016 return callback.GetResult(socket->Read(buf, buf_len, callback.callback()));
1017 }
1018
1019 private:
1020 base::test::ScopedFeatureList scoped_feature_list_;
1021 };
1022
1023 INSTANTIATE_TEST_CASE_P(/* no prefix */,
1024 SSLClientSocketReadTest,
1025 ::testing::Bool());
1026
861 // Verifies the correctness of GetSSLCertRequestInfo. 1027 // Verifies the correctness of GetSSLCertRequestInfo.
862 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest { 1028 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest {
863 protected: 1029 protected:
864 // Creates a test server with the given SSLOptions, connects to it and returns 1030 // Creates a test server with the given SSLOptions, connects to it and returns
865 // the SSLCertRequestInfo reported by the socket. 1031 // the SSLCertRequestInfo reported by the socket.
866 scoped_refptr<SSLCertRequestInfo> GetCertRequest( 1032 scoped_refptr<SSLCertRequestInfo> GetCertRequest(
867 SpawnedTestServer::SSLOptions ssl_options) { 1033 SpawnedTestServer::SSLOptions ssl_options) {
868 SpawnedTestServer spawned_test_server(SpawnedTestServer::TYPE_HTTPS, 1034 SpawnedTestServer spawned_test_server(SpawnedTestServer::TYPE_HTTPS,
869 ssl_options, base::FilePath()); 1035 ssl_options, base::FilePath());
870 if (!spawned_test_server.Start()) 1036 if (!spawned_test_server.Start())
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
1199 EXPECT_FALSE(sock_->IsConnected()); 1365 EXPECT_FALSE(sock_->IsConnected());
1200 } 1366 }
1201 1367
1202 // TODO(wtc): Add unit tests for IsConnectedAndIdle: 1368 // TODO(wtc): Add unit tests for IsConnectedAndIdle:
1203 // - Server closes an SSL connection (with a close_notify alert message). 1369 // - Server closes an SSL connection (with a close_notify alert message).
1204 // - Server closes the underlying TCP connection directly. 1370 // - Server closes the underlying TCP connection directly.
1205 // - Server sends data unexpectedly. 1371 // - Server sends data unexpectedly.
1206 1372
1207 // Tests that the socket can be read from successfully. Also test that a peer's 1373 // Tests that the socket can be read from successfully. Also test that a peer's
1208 // close_notify alert is successfully processed without error. 1374 // close_notify alert is successfully processed without error.
1209 TEST_F(SSLClientSocketTest, Read) { 1375 TEST_P(SSLClientSocketReadTest, Read) {
1210 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 1376 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
1211 1377
1212 TestCompletionCallback callback; 1378 TestCompletionCallback callback;
1213 std::unique_ptr<StreamSocket> transport( 1379 std::unique_ptr<StreamSocket> transport(
1214 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); 1380 new TCPClientSocket(addr(), NULL, NULL, NetLogSource()));
1215 EXPECT_EQ(0, transport->GetTotalReceivedBytes()); 1381 EXPECT_EQ(0, transport->GetTotalReceivedBytes());
1216 1382
1217 int rv = callback.GetResult(transport->Connect(callback.callback())); 1383 int rv = callback.GetResult(transport->Connect(callback.callback()));
1218 EXPECT_THAT(rv, IsOk()); 1384 EXPECT_THAT(rv, IsOk());
1219 1385
(...skipping 15 matching lines...) Expand all
1235 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); 1401 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1);
1236 1402
1237 rv = callback.GetResult(sock->Write( 1403 rv = callback.GetResult(sock->Write(
1238 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); 1404 request_buffer.get(), arraysize(request_text) - 1, callback.callback()));
1239 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); 1405 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv);
1240 1406
1241 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); 1407 scoped_refptr<IOBuffer> buf(new IOBuffer(4096));
1242 int64_t unencrypted_bytes_read = 0; 1408 int64_t unencrypted_bytes_read = 0;
1243 int64_t network_bytes_read_during_handshake = sock->GetTotalReceivedBytes(); 1409 int64_t network_bytes_read_during_handshake = sock->GetTotalReceivedBytes();
1244 do { 1410 do {
1245 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); 1411 rv = ReadAndWaitForCompletion(sock.get(), buf.get(), 4096);
1246 EXPECT_GE(rv, 0); 1412 EXPECT_GE(rv, 0);
1247 if (rv >= 0) { 1413 if (rv >= 0) {
1248 unencrypted_bytes_read += rv; 1414 unencrypted_bytes_read += rv;
1249 } 1415 }
1250 } while (rv > 0); 1416 } while (rv > 0);
1251 EXPECT_GT(unencrypted_bytes_read, 0); 1417 EXPECT_GT(unencrypted_bytes_read, 0);
1252 // Reading the payload should increase the number of bytes on network layer. 1418 // Reading the payload should increase the number of bytes on network layer.
1253 EXPECT_GT(sock->GetTotalReceivedBytes(), network_bytes_read_during_handshake); 1419 EXPECT_GT(sock->GetTotalReceivedBytes(), network_bytes_read_during_handshake);
1254 // Number of bytes received on the network after the handshake should be 1420 // Number of bytes received on the network after the handshake should be
1255 // higher than the number of encrypted bytes read. 1421 // higher than the number of encrypted bytes read.
(...skipping 30 matching lines...) Expand all
1286 1452
1287 rv = callback.GetResult(sock->Connect(callback.callback())); 1453 rv = callback.GetResult(sock->Connect(callback.callback()));
1288 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); 1454 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET));
1289 EXPECT_FALSE(sock->IsConnected()); 1455 EXPECT_FALSE(sock->IsConnected());
1290 } 1456 }
1291 1457
1292 // Tests that the SSLClientSocket properly handles when the underlying transport 1458 // Tests that the SSLClientSocket properly handles when the underlying transport
1293 // synchronously returns an error code - such as if an intermediary terminates 1459 // synchronously returns an error code - such as if an intermediary terminates
1294 // the socket connection uncleanly. 1460 // the socket connection uncleanly.
1295 // This is a regression test for http://crbug.com/238536 1461 // This is a regression test for http://crbug.com/238536
1296 TEST_F(SSLClientSocketTest, Read_WithSynchronousError) { 1462 TEST_P(SSLClientSocketReadTest, Read_WithSynchronousError) {
1297 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 1463 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
1298 1464
1299 TestCompletionCallback callback; 1465 TestCompletionCallback callback;
1300 std::unique_ptr<StreamSocket> real_transport( 1466 std::unique_ptr<StreamSocket> real_transport(
1301 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); 1467 new TCPClientSocket(addr(), NULL, NULL, NetLogSource()));
1302 std::unique_ptr<SynchronousErrorStreamSocket> transport( 1468 std::unique_ptr<SynchronousErrorStreamSocket> transport(
1303 new SynchronousErrorStreamSocket(std::move(real_transport))); 1469 new SynchronousErrorStreamSocket(std::move(real_transport)));
1304 int rv = callback.GetResult(transport->Connect(callback.callback())); 1470 int rv = callback.GetResult(transport->Connect(callback.callback()));
1305 EXPECT_THAT(rv, IsOk()); 1471 EXPECT_THAT(rv, IsOk());
1306 1472
(...skipping 21 matching lines...) Expand all
1328 EXPECT_EQ(kRequestTextSize, rv); 1494 EXPECT_EQ(kRequestTextSize, rv);
1329 1495
1330 // Simulate an unclean/forcible shutdown. 1496 // Simulate an unclean/forcible shutdown.
1331 raw_transport->SetNextReadError(ERR_CONNECTION_RESET); 1497 raw_transport->SetNextReadError(ERR_CONNECTION_RESET);
1332 1498
1333 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); 1499 scoped_refptr<IOBuffer> buf(new IOBuffer(4096));
1334 1500
1335 // Note: This test will hang if this bug has regressed. Simply checking that 1501 // 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 1502 // rv != ERR_IO_PENDING is insufficient, as ERR_IO_PENDING is a legitimate
1337 // result when using a dedicated task runner for NSS. 1503 // result when using a dedicated task runner for NSS.
1338 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); 1504 rv = Read(sock.get(), buf.get(), 4096, callback.callback());
1339 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); 1505 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET));
1340 } 1506 }
1341 1507
1342 // Tests that the SSLClientSocket properly handles when the underlying transport 1508 // Tests that the SSLClientSocket properly handles when the underlying transport
1343 // asynchronously returns an error code while writing data - such as if an 1509 // asynchronously returns an error code while writing data - such as if an
1344 // intermediary terminates the socket connection uncleanly. 1510 // intermediary terminates the socket connection uncleanly.
1345 // This is a regression test for http://crbug.com/249848 1511 // This is a regression test for http://crbug.com/249848
1346 TEST_F(SSLClientSocketTest, Write_WithSynchronousError) { 1512 TEST_F(SSLClientSocketTest, Write_WithSynchronousError) {
1347 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 1513 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
1348 1514
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
1463 int old_write_count = raw_counting_socket->write_count(); 1629 int old_write_count = raw_counting_socket->write_count();
1464 base::RunLoop loop; 1630 base::RunLoop loop;
1465 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 1631 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
1466 FROM_HERE, loop.QuitClosure(), base::TimeDelta::FromMilliseconds(100)); 1632 FROM_HERE, loop.QuitClosure(), base::TimeDelta::FromMilliseconds(100));
1467 loop.Run(); 1633 loop.Run();
1468 EXPECT_EQ(old_write_count, raw_counting_socket->write_count()); 1634 EXPECT_EQ(old_write_count, raw_counting_socket->write_count());
1469 } 1635 }
1470 1636
1471 // Test the full duplex mode, with Read and Write pending at the same time. 1637 // 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. 1638 // This test also serves as a regression test for http://crbug.com/29815.
1473 TEST_F(SSLClientSocketTest, Read_FullDuplex) { 1639 TEST_P(SSLClientSocketReadTest, Read_FullDuplex) {
1474 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 1640 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
1475 1641
1476 int rv; 1642 int rv;
1477 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); 1643 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv));
1478 EXPECT_THAT(rv, IsOk()); 1644 EXPECT_THAT(rv, IsOk());
1479 1645
1480 // Issue a "hanging" Read first. 1646 // Issue a "hanging" Read first.
1481 TestCompletionCallback callback; 1647 TestCompletionCallback callback;
1482 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); 1648 scoped_refptr<IOBuffer> buf(new IOBuffer(4096));
1483 rv = sock_->Read(buf.get(), 4096, callback.callback()); 1649 rv = Read(sock_.get(), buf.get(), 4096, callback.callback());
1484 // We haven't written the request, so there should be no response yet. 1650 // We haven't written the request, so there should be no response yet.
1485 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); 1651 ASSERT_THAT(rv, IsError(ERR_IO_PENDING));
1486 1652
1487 // Write the request. 1653 // Write the request.
1488 // The request is padded with a User-Agent header to a size that causes the 1654 // 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. 1655 // memio circular buffer (4k bytes) in SSLClientSocketNSS to wrap around.
1490 // This tests the fix for http://crbug.com/29815. 1656 // This tests the fix for http://crbug.com/29815.
1491 std::string request_text = "GET / HTTP/1.1\r\nUser-Agent: long browser name "; 1657 std::string request_text = "GET / HTTP/1.1\r\nUser-Agent: long browser name ";
1492 for (int i = 0; i < 3770; ++i) 1658 for (int i = 0; i < 3770; ++i)
1493 request_text.push_back('*'); 1659 request_text.push_back('*');
1494 request_text.append("\r\n\r\n"); 1660 request_text.append("\r\n\r\n");
1495 scoped_refptr<IOBuffer> request_buffer(new StringIOBuffer(request_text)); 1661 scoped_refptr<IOBuffer> request_buffer(new StringIOBuffer(request_text));
1496 1662
1497 TestCompletionCallback callback2; // Used for Write only. 1663 TestCompletionCallback callback2; // Used for Write only.
1498 rv = callback2.GetResult(sock_->Write( 1664 rv = callback2.GetResult(sock_->Write(
1499 request_buffer.get(), request_text.size(), callback2.callback())); 1665 request_buffer.get(), request_text.size(), callback2.callback()));
1500 EXPECT_EQ(static_cast<int>(request_text.size()), rv); 1666 EXPECT_EQ(static_cast<int>(request_text.size()), rv);
1501 1667
1502 // Now get the Read result. 1668 // Now get the Read result.
1503 rv = callback.WaitForResult(); 1669 rv = callback.WaitForResult();
1670 if (GetParam()) {
1671 EXPECT_THAT(rv, IsOk());
1672 rv = Read(sock_.get(), buf.get(), 4096, callback.callback());
davidben 2017/02/10 23:33:48 Given the issue with ReadIfReady always needing to
xunjieli 2017/02/13 20:28:18 Done.
1673 }
1504 EXPECT_GT(rv, 0); 1674 EXPECT_GT(rv, 0);
1505 } 1675 }
1506 1676
1507 // Attempts to Read() and Write() from an SSLClientSocketNSS in full duplex 1677 // Attempts to Read() and Write() from an SSLClientSocketNSS in full duplex
1508 // mode when the underlying transport is blocked on sending data. When the 1678 // 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 1679 // 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() 1680 // Read() and Write() callbacks. If the socket is deleted by the Read()
1511 // callback, the Write() callback should not be invoked. 1681 // callback, the Write() callback should not be invoked.
1512 // Regression test for http://crbug.com/232633 1682 // Regression test for http://crbug.com/232633
1513 TEST_F(SSLClientSocketTest, Read_DeleteWhilePendingFullDuplex) { 1683 TEST_P(SSLClientSocketReadTest, Read_DeleteWhilePendingFullDuplex) {
1514 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 1684 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
1515 1685
1516 TestCompletionCallback callback; 1686 TestCompletionCallback callback;
1517 std::unique_ptr<StreamSocket> real_transport( 1687 std::unique_ptr<StreamSocket> real_transport(
1518 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); 1688 new TCPClientSocket(addr(), NULL, NULL, NetLogSource()));
1519 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer 1689 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer
1520 // is retained in order to configure additional errors. 1690 // is retained in order to configure additional errors.
1521 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( 1691 std::unique_ptr<SynchronousErrorStreamSocket> error_socket(
1522 new SynchronousErrorStreamSocket(std::move(real_transport))); 1692 new SynchronousErrorStreamSocket(std::move(real_transport)));
1523 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); 1693 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get();
(...skipping 28 matching lines...) Expand all
1552 // ... but have those errors returned asynchronously. Because the Write() will 1722 // ... but have those errors returned asynchronously. Because the Write() will
1553 // return first, this will trigger the error. 1723 // return first, this will trigger the error.
1554 raw_transport->BlockReadResult(); 1724 raw_transport->BlockReadResult();
1555 raw_transport->BlockWrite(); 1725 raw_transport->BlockWrite();
1556 1726
1557 // Enqueue a Read() before calling Write(), which should "hang" due to 1727 // Enqueue a Read() before calling Write(), which should "hang" due to
1558 // the ERR_IO_PENDING caused by SetReadShouldBlock() and thus return. 1728 // the ERR_IO_PENDING caused by SetReadShouldBlock() and thus return.
1559 SSLClientSocket* raw_sock = sock.get(); 1729 SSLClientSocket* raw_sock = sock.get();
1560 DeleteSocketCallback read_callback(sock.release()); 1730 DeleteSocketCallback read_callback(sock.release());
1561 scoped_refptr<IOBuffer> read_buf(new IOBuffer(4096)); 1731 scoped_refptr<IOBuffer> read_buf(new IOBuffer(4096));
1562 rv = raw_sock->Read(read_buf.get(), 4096, read_callback.callback()); 1732 rv = Read(raw_sock, read_buf.get(), 4096, read_callback.callback());
1563 1733
1564 // Ensure things didn't complete synchronously, otherwise |sock| is invalid. 1734 // Ensure things didn't complete synchronously, otherwise |sock| is invalid.
1565 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); 1735 ASSERT_THAT(rv, IsError(ERR_IO_PENDING));
1566 ASSERT_FALSE(read_callback.have_result()); 1736 ASSERT_FALSE(read_callback.have_result());
1567 1737
1568 // Attempt to write the remaining data. OpenSSL will return that its blocked 1738 // Attempt to write the remaining data. OpenSSL will return that its blocked
1569 // because the underlying transport is blocked. 1739 // because the underlying transport is blocked.
1570 rv = raw_sock->Write(request_buffer.get(), 1740 rv = raw_sock->Write(request_buffer.get(),
1571 request_buffer->BytesRemaining(), 1741 request_buffer->BytesRemaining(),
1572 callback.callback()); 1742 callback.callback());
1573 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); 1743 ASSERT_THAT(rv, IsError(ERR_IO_PENDING));
1574 ASSERT_FALSE(callback.have_result()); 1744 ASSERT_FALSE(callback.have_result());
1575 1745
1576 // Now unblock Write(), which will invoke OnSendComplete and (eventually) 1746 // Now unblock Write(), which will invoke OnSendComplete and (eventually)
1577 // call the Read() callback, deleting the socket and thus aborting calling 1747 // call the Read() callback, deleting the socket and thus aborting calling
1578 // the Write() callback. 1748 // the Write() callback.
1579 raw_transport->UnblockWrite(); 1749 raw_transport->UnblockWrite();
1580 1750
1581 rv = read_callback.WaitForResult(); 1751 rv = read_callback.WaitForResult();
1582 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); 1752 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET));
davidben 2017/02/10 23:33:48 You'll probably need WaitForReadResult here, once
xunjieli 2017/02/13 20:28:18 Actually this particularly one deletes the socket
1583 1753
1584 // The Write callback should not have been called. 1754 // The Write callback should not have been called.
1585 EXPECT_FALSE(callback.have_result()); 1755 EXPECT_FALSE(callback.have_result());
1586 } 1756 }
1587 1757
1588 // Tests that the SSLClientSocket does not crash if data is received on the 1758 // 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 1759 // transport socket after a failing write. This can occur if we have a Write
1590 // error in a SPDY socket. 1760 // error in a SPDY socket.
1591 // Regression test for http://crbug.com/335557 1761 // Regression test for http://crbug.com/335557
1592 TEST_F(SSLClientSocketTest, Read_WithWriteError) { 1762 TEST_P(SSLClientSocketReadTest, Read_WithWriteError) {
1593 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 1763 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
1594 1764
1595 TestCompletionCallback callback; 1765 TestCompletionCallback callback;
1596 std::unique_ptr<StreamSocket> real_transport( 1766 std::unique_ptr<StreamSocket> real_transport(
1597 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); 1767 new TCPClientSocket(addr(), NULL, NULL, NetLogSource()));
1598 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer 1768 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer
1599 // is retained in order to configure additional errors. 1769 // is retained in order to configure additional errors.
1600 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( 1770 std::unique_ptr<SynchronousErrorStreamSocket> error_socket(
1601 new SynchronousErrorStreamSocket(std::move(real_transport))); 1771 new SynchronousErrorStreamSocket(std::move(real_transport)));
1602 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); 1772 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get();
(...skipping 24 matching lines...) Expand all
1627 memcpy(request_buffer->data(), request_text, kRequestTextSize); 1797 memcpy(request_buffer->data(), request_text, kRequestTextSize);
1628 1798
1629 rv = callback.GetResult( 1799 rv = callback.GetResult(
1630 sock->Write(request_buffer.get(), kRequestTextSize, callback.callback())); 1800 sock->Write(request_buffer.get(), kRequestTextSize, callback.callback()));
1631 EXPECT_EQ(kRequestTextSize, rv); 1801 EXPECT_EQ(kRequestTextSize, rv);
1632 1802
1633 // Start a hanging read. 1803 // Start a hanging read.
1634 TestCompletionCallback read_callback; 1804 TestCompletionCallback read_callback;
1635 raw_transport->BlockReadResult(); 1805 raw_transport->BlockReadResult();
1636 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); 1806 scoped_refptr<IOBuffer> buf(new IOBuffer(4096));
1637 rv = sock->Read(buf.get(), 4096, read_callback.callback()); 1807 rv = Read(sock.get(), buf.get(), 4096, read_callback.callback());
1638 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); 1808 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
1639 1809
1640 // Perform another write, but have it fail. Write a request larger than the 1810 // 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 1811 // internal socket buffers so that the request hits the underlying transport
1642 // socket and detects the error. 1812 // socket and detects the error.
1643 std::string long_request_text = 1813 std::string long_request_text =
1644 "GET / HTTP/1.1\r\nUser-Agent: long browser name "; 1814 "GET / HTTP/1.1\r\nUser-Agent: long browser name ";
1645 long_request_text.append(20 * 1024, '*'); 1815 long_request_text.append(20 * 1024, '*');
1646 long_request_text.append("\r\n\r\n"); 1816 long_request_text.append("\r\n\r\n");
1647 scoped_refptr<DrainableIOBuffer> long_request_buffer(new DrainableIOBuffer( 1817 scoped_refptr<DrainableIOBuffer> long_request_buffer(new DrainableIOBuffer(
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1695 raw_transport->SetNextReadError(0); 1865 raw_transport->SetNextReadError(0);
1696 1866
1697 rv = callback.GetResult(sock->Connect(callback.callback())); 1867 rv = callback.GetResult(sock->Connect(callback.callback()));
1698 EXPECT_THAT(rv, IsError(ERR_CONNECTION_CLOSED)); 1868 EXPECT_THAT(rv, IsError(ERR_CONNECTION_CLOSED));
1699 EXPECT_FALSE(sock->IsConnected()); 1869 EXPECT_FALSE(sock->IsConnected());
1700 } 1870 }
1701 1871
1702 // Tests that SSLClientSocket returns a Read of size 0 if the underlying socket 1872 // 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. 1873 // is cleanly closed, but the peer does not send close_notify.
1704 // This is a regression test for https://crbug.com/422246 1874 // This is a regression test for https://crbug.com/422246
1705 TEST_F(SSLClientSocketTest, Read_WithZeroReturn) { 1875 TEST_P(SSLClientSocketReadTest, Read_WithZeroReturn) {
1706 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 1876 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
1707 1877
1708 TestCompletionCallback callback; 1878 TestCompletionCallback callback;
1709 std::unique_ptr<StreamSocket> real_transport( 1879 std::unique_ptr<StreamSocket> real_transport(
1710 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); 1880 new TCPClientSocket(addr(), NULL, NULL, NetLogSource()));
1711 std::unique_ptr<SynchronousErrorStreamSocket> transport( 1881 std::unique_ptr<SynchronousErrorStreamSocket> transport(
1712 new SynchronousErrorStreamSocket(std::move(real_transport))); 1882 new SynchronousErrorStreamSocket(std::move(real_transport)));
1713 int rv = callback.GetResult(transport->Connect(callback.callback())); 1883 int rv = callback.GetResult(transport->Connect(callback.callback()));
1714 EXPECT_THAT(rv, IsOk()); 1884 EXPECT_THAT(rv, IsOk());
1715 1885
1716 // Disable TLS False Start to ensure the handshake has completed. 1886 // Disable TLS False Start to ensure the handshake has completed.
1717 SSLConfig ssl_config; 1887 SSLConfig ssl_config;
1718 ssl_config.false_start_enabled = false; 1888 ssl_config.false_start_enabled = false;
1719 1889
1720 SynchronousErrorStreamSocket* raw_transport = transport.get(); 1890 SynchronousErrorStreamSocket* raw_transport = transport.get();
1721 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( 1891 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
1722 std::move(transport), spawned_test_server()->host_port_pair(), 1892 std::move(transport), spawned_test_server()->host_port_pair(),
1723 ssl_config)); 1893 ssl_config));
1724 1894
1725 rv = callback.GetResult(sock->Connect(callback.callback())); 1895 rv = callback.GetResult(sock->Connect(callback.callback()));
1726 EXPECT_THAT(rv, IsOk()); 1896 EXPECT_THAT(rv, IsOk());
1727 EXPECT_TRUE(sock->IsConnected()); 1897 EXPECT_TRUE(sock->IsConnected());
1728 1898
1729 raw_transport->SetNextReadError(0); 1899 raw_transport->SetNextReadError(0);
1730 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); 1900 scoped_refptr<IOBuffer> buf(new IOBuffer(4096));
1731 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); 1901 rv = ReadAndWaitForCompletion(sock.get(), buf.get(), 4096);
1732 EXPECT_EQ(0, rv); 1902 EXPECT_EQ(0, rv);
1733 } 1903 }
1734 1904
1735 // Tests that SSLClientSocket cleanly returns a Read of size 0 if the 1905 // Tests that SSLClientSocket cleanly returns a Read of size 0 if the
1736 // underlying socket is cleanly closed asynchronously. 1906 // underlying socket is cleanly closed asynchronously.
1737 // This is a regression test for https://crbug.com/422246 1907 // This is a regression test for https://crbug.com/422246
1738 TEST_F(SSLClientSocketTest, Read_WithAsyncZeroReturn) { 1908 TEST_P(SSLClientSocketReadTest, Read_WithAsyncZeroReturn) {
1739 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 1909 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
1740 1910
1741 TestCompletionCallback callback; 1911 TestCompletionCallback callback;
1742 std::unique_ptr<StreamSocket> real_transport( 1912 std::unique_ptr<StreamSocket> real_transport(
1743 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); 1913 new TCPClientSocket(addr(), NULL, NULL, NetLogSource()));
1744 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( 1914 std::unique_ptr<SynchronousErrorStreamSocket> error_socket(
1745 new SynchronousErrorStreamSocket(std::move(real_transport))); 1915 new SynchronousErrorStreamSocket(std::move(real_transport)));
1746 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); 1916 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get();
1747 std::unique_ptr<FakeBlockingStreamSocket> transport( 1917 std::unique_ptr<FakeBlockingStreamSocket> transport(
1748 new FakeBlockingStreamSocket(std::move(error_socket))); 1918 new FakeBlockingStreamSocket(std::move(error_socket)));
1749 FakeBlockingStreamSocket* raw_transport = transport.get(); 1919 FakeBlockingStreamSocket* raw_transport = transport.get();
1750 int rv = callback.GetResult(transport->Connect(callback.callback())); 1920 int rv = callback.GetResult(transport->Connect(callback.callback()));
1751 EXPECT_THAT(rv, IsOk()); 1921 EXPECT_THAT(rv, IsOk());
1752 1922
1753 // Disable TLS False Start to ensure the handshake has completed. 1923 // Disable TLS False Start to ensure the handshake has completed.
1754 SSLConfig ssl_config; 1924 SSLConfig ssl_config;
1755 ssl_config.false_start_enabled = false; 1925 ssl_config.false_start_enabled = false;
1756 1926
1757 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( 1927 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket(
1758 std::move(transport), spawned_test_server()->host_port_pair(), 1928 std::move(transport), spawned_test_server()->host_port_pair(),
1759 ssl_config)); 1929 ssl_config));
1760 1930
1761 rv = callback.GetResult(sock->Connect(callback.callback())); 1931 rv = callback.GetResult(sock->Connect(callback.callback()));
1762 EXPECT_THAT(rv, IsOk()); 1932 EXPECT_THAT(rv, IsOk());
1763 EXPECT_TRUE(sock->IsConnected()); 1933 EXPECT_TRUE(sock->IsConnected());
1764 1934
1765 raw_error_socket->SetNextReadError(0); 1935 raw_error_socket->SetNextReadError(0);
1766 raw_transport->BlockReadResult(); 1936 raw_transport->BlockReadResult();
1767 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); 1937 scoped_refptr<IOBuffer> buf(new IOBuffer(4096));
1768 rv = sock->Read(buf.get(), 4096, callback.callback()); 1938 TestCompletionCallback read_callback;
1939 rv = Read(sock.get(), buf.get(), 4096, read_callback.callback());
1769 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); 1940 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
1770 1941
1771 raw_transport->UnblockReadResult(); 1942 raw_transport->UnblockReadResult();
1772 rv = callback.GetResult(rv); 1943 rv = read_callback.GetResult(rv);
1944 if (GetParam()) {
1945 EXPECT_EQ(OK, rv);
1946 rv = Read(sock.get(), buf.get(), 4096, read_callback.callback());
1947 }
1773 EXPECT_EQ(0, rv); 1948 EXPECT_EQ(0, rv);
1774 } 1949 }
1775 1950
1776 // Tests that fatal alerts from the peer are processed. This is a regression 1951 // Tests that fatal alerts from the peer are processed. This is a regression
1777 // test for https://crbug.com/466303. 1952 // test for https://crbug.com/466303.
1778 TEST_F(SSLClientSocketTest, Read_WithFatalAlert) { 1953 TEST_P(SSLClientSocketReadTest, Read_WithFatalAlert) {
1779 SpawnedTestServer::SSLOptions ssl_options; 1954 SpawnedTestServer::SSLOptions ssl_options;
1780 ssl_options.alert_after_handshake = true; 1955 ssl_options.alert_after_handshake = true;
1781 ASSERT_TRUE(StartTestServer(ssl_options)); 1956 ASSERT_TRUE(StartTestServer(ssl_options));
1782 1957
1783 int rv; 1958 int rv;
1784 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); 1959 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv));
1785 EXPECT_THAT(rv, IsOk()); 1960 EXPECT_THAT(rv, IsOk());
1786 1961
1787 // Receive the fatal alert. 1962 // Receive the fatal alert.
1788 TestCompletionCallback callback; 1963 TestCompletionCallback callback;
1789 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); 1964 scoped_refptr<IOBuffer> buf(new IOBuffer(4096));
1790 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, callback.GetResult(sock_->Read( 1965 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR,
1791 buf.get(), 4096, callback.callback()))); 1966 ReadAndWaitForCompletion(sock_.get(), buf.get(), 4096));
1792 } 1967 }
1793 1968
1794 TEST_F(SSLClientSocketTest, Read_SmallChunks) { 1969 TEST_P(SSLClientSocketReadTest, Read_SmallChunks) {
1795 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 1970 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
1796 1971
1797 int rv; 1972 int rv;
1798 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); 1973 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv));
1799 EXPECT_THAT(rv, IsOk()); 1974 EXPECT_THAT(rv, IsOk());
1800 1975
1801 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; 1976 const char request_text[] = "GET / HTTP/1.0\r\n\r\n";
1802 scoped_refptr<IOBuffer> request_buffer( 1977 scoped_refptr<IOBuffer> request_buffer(
1803 new IOBuffer(arraysize(request_text) - 1)); 1978 new IOBuffer(arraysize(request_text) - 1));
1804 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); 1979 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1);
1805 1980
1806 TestCompletionCallback callback; 1981 TestCompletionCallback callback;
1807 rv = callback.GetResult(sock_->Write( 1982 rv = callback.GetResult(sock_->Write(
1808 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); 1983 request_buffer.get(), arraysize(request_text) - 1, callback.callback()));
1809 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); 1984 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv);
1810 1985
1811 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); 1986 scoped_refptr<IOBuffer> buf(new IOBuffer(1));
1812 do { 1987 do {
1813 rv = callback.GetResult(sock_->Read(buf.get(), 1, callback.callback())); 1988 rv = ReadAndWaitForCompletion(sock_.get(), buf.get(), 1);
1814 EXPECT_GE(rv, 0); 1989 EXPECT_GE(rv, 0);
1815 } while (rv > 0); 1990 } while (rv > 0);
1816 } 1991 }
1817 1992
1818 TEST_F(SSLClientSocketTest, Read_ManySmallRecords) { 1993 TEST_P(SSLClientSocketReadTest, Read_ManySmallRecords) {
1819 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 1994 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
1820 1995
1821 TestCompletionCallback callback; 1996 TestCompletionCallback callback;
1822 1997
1823 std::unique_ptr<StreamSocket> real_transport( 1998 std::unique_ptr<StreamSocket> real_transport(
1824 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); 1999 new TCPClientSocket(addr(), NULL, NULL, NetLogSource()));
1825 std::unique_ptr<ReadBufferingStreamSocket> transport( 2000 std::unique_ptr<ReadBufferingStreamSocket> transport(
1826 new ReadBufferingStreamSocket(std::move(real_transport))); 2001 new ReadBufferingStreamSocket(std::move(real_transport)));
1827 ReadBufferingStreamSocket* raw_transport = transport.get(); 2002 ReadBufferingStreamSocket* raw_transport = transport.get();
1828 int rv = callback.GetResult(transport->Connect(callback.callback())); 2003 int rv = callback.GetResult(transport->Connect(callback.callback()));
(...skipping 22 matching lines...) Expand all
1851 // of SSL data is buffered first. The 15K of buffered data is made up of 2026 // 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 2027 // many smaller SSL records (the TestServer writes along 1350 byte
1853 // plaintext boundaries), although there may also be a few records that are 2028 // plaintext boundaries), although there may also be a few records that are
1854 // smaller or larger, due to timing and SSL False Start. 2029 // 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 2030 // 15K was chosen because 15K is smaller than the 17K (max) read issued by
1856 // the SSLClientSocket implementation, and larger than the minimum amount 2031 // the SSLClientSocket implementation, and larger than the minimum amount
1857 // of ciphertext necessary to contain the 8K of plaintext requested below. 2032 // of ciphertext necessary to contain the 8K of plaintext requested below.
1858 raw_transport->SetBufferSize(15000); 2033 raw_transport->SetBufferSize(15000);
1859 2034
1860 scoped_refptr<IOBuffer> buffer(new IOBuffer(8192)); 2035 scoped_refptr<IOBuffer> buffer(new IOBuffer(8192));
1861 rv = callback.GetResult(sock->Read(buffer.get(), 8192, callback.callback())); 2036 rv = ReadAndWaitForCompletion(sock.get(), buffer.get(), 8192);
1862 ASSERT_EQ(rv, 8192); 2037 ASSERT_EQ(rv, 8192);
1863 } 2038 }
1864 2039
1865 TEST_F(SSLClientSocketTest, Read_Interrupted) { 2040 TEST_P(SSLClientSocketReadTest, Read_Interrupted) {
1866 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 2041 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
1867 2042
1868 int rv; 2043 int rv;
1869 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); 2044 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv));
1870 EXPECT_THAT(rv, IsOk()); 2045 EXPECT_THAT(rv, IsOk());
1871 2046
1872 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; 2047 const char request_text[] = "GET / HTTP/1.0\r\n\r\n";
1873 scoped_refptr<IOBuffer> request_buffer( 2048 scoped_refptr<IOBuffer> request_buffer(
1874 new IOBuffer(arraysize(request_text) - 1)); 2049 new IOBuffer(arraysize(request_text) - 1));
1875 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); 2050 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1);
1876 2051
1877 TestCompletionCallback callback; 2052 TestCompletionCallback callback;
1878 rv = callback.GetResult(sock_->Write( 2053 rv = callback.GetResult(sock_->Write(
1879 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); 2054 request_buffer.get(), arraysize(request_text) - 1, callback.callback()));
1880 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); 2055 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv);
1881 2056
1882 // Do a partial read and then exit. This test should not crash! 2057 // Do a partial read and then exit. This test should not crash!
1883 scoped_refptr<IOBuffer> buf(new IOBuffer(512)); 2058 scoped_refptr<IOBuffer> buf(new IOBuffer(512));
1884 rv = callback.GetResult(sock_->Read(buf.get(), 512, callback.callback())); 2059 rv = ReadAndWaitForCompletion(sock_.get(), buf.get(), 512);
1885 EXPECT_GT(rv, 0); 2060 EXPECT_GT(rv, 0);
1886 } 2061 }
1887 2062
1888 TEST_F(SSLClientSocketTest, Read_FullLogging) { 2063 TEST_P(SSLClientSocketReadTest, Read_FullLogging) {
1889 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 2064 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
1890 2065
1891 TestCompletionCallback callback; 2066 TestCompletionCallback callback;
1892 TestNetLog log; 2067 TestNetLog log;
1893 log.SetCaptureMode(NetLogCaptureMode::IncludeSocketBytes()); 2068 log.SetCaptureMode(NetLogCaptureMode::IncludeSocketBytes());
1894 std::unique_ptr<StreamSocket> transport( 2069 std::unique_ptr<StreamSocket> transport(
1895 new TCPClientSocket(addr(), NULL, &log, NetLogSource())); 2070 new TCPClientSocket(addr(), NULL, &log, NetLogSource()));
1896 int rv = callback.GetResult(transport->Connect(callback.callback())); 2071 int rv = callback.GetResult(transport->Connect(callback.callback()));
1897 EXPECT_THAT(rv, IsOk()); 2072 EXPECT_THAT(rv, IsOk());
1898 2073
(...skipping 15 matching lines...) Expand all
1914 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); 2089 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv);
1915 2090
1916 TestNetLogEntry::List entries; 2091 TestNetLogEntry::List entries;
1917 log.GetEntries(&entries); 2092 log.GetEntries(&entries);
1918 size_t last_index = ExpectLogContainsSomewhereAfter( 2093 size_t last_index = ExpectLogContainsSomewhereAfter(
1919 entries, 5, NetLogEventType::SSL_SOCKET_BYTES_SENT, 2094 entries, 5, NetLogEventType::SSL_SOCKET_BYTES_SENT,
1920 NetLogEventPhase::NONE); 2095 NetLogEventPhase::NONE);
1921 2096
1922 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); 2097 scoped_refptr<IOBuffer> buf(new IOBuffer(4096));
1923 for (;;) { 2098 for (;;) {
1924 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); 2099 rv = ReadAndWaitForCompletion(sock.get(), buf.get(), 4096);
1925 EXPECT_GE(rv, 0); 2100 EXPECT_GE(rv, 0);
1926 if (rv <= 0) 2101 if (rv <= 0)
1927 break; 2102 break;
1928 2103
1929 log.GetEntries(&entries); 2104 log.GetEntries(&entries);
1930 last_index = ExpectLogContainsSomewhereAfter( 2105 last_index = ExpectLogContainsSomewhereAfter(
1931 entries, last_index + 1, NetLogEventType::SSL_SOCKET_BYTES_RECEIVED, 2106 entries, last_index + 1, NetLogEventType::SSL_SOCKET_BYTES_RECEIVED,
1932 NetLogEventPhase::NONE); 2107 NetLogEventPhase::NONE);
1933 } 2108 }
1934 } 2109 }
(...skipping 1651 matching lines...) Expand 10 before | Expand all | Expand 10 after
3586 // Replace it with an alert. 3761 // Replace it with an alert.
3587 raw_transport->ReplaceReadResult( 3762 raw_transport->ReplaceReadResult(
3588 FormatTLS12Alert(49 /* AlertDescription.access_denied */)); 3763 FormatTLS12Alert(49 /* AlertDescription.access_denied */));
3589 raw_transport->UnblockReadResult(); 3764 raw_transport->UnblockReadResult();
3590 3765
3591 rv = callback.GetResult(rv); 3766 rv = callback.GetResult(rv);
3592 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT)); 3767 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT));
3593 } 3768 }
3594 3769
3595 // Basic test for dumping memory stats. 3770 // Basic test for dumping memory stats.
3596 TEST_F(SSLClientSocketTest, DumpMemoryStats) { 3771 TEST_P(SSLClientSocketReadTest, DumpMemoryStats) {
3597 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); 3772 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions()));
3598 3773
3599 int rv; 3774 int rv;
3600 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); 3775 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv));
3601 EXPECT_THAT(rv, IsOk()); 3776 EXPECT_THAT(rv, IsOk());
3602 StreamSocket::SocketMemoryStats stats; 3777 StreamSocket::SocketMemoryStats stats;
3603 sock_->DumpMemoryStats(&stats); 3778 sock_->DumpMemoryStats(&stats);
3604 EXPECT_EQ(0u, stats.buffer_size); 3779 EXPECT_EQ(0u, stats.buffer_size);
3605 EXPECT_EQ(1u, stats.cert_count); 3780 EXPECT_EQ(1u, stats.cert_count);
3606 EXPECT_LT(0u, stats.serialized_cert_size); 3781 EXPECT_LT(0u, stats.serialized_cert_size);
3607 EXPECT_EQ(stats.serialized_cert_size, stats.total_size); 3782 EXPECT_EQ(stats.serialized_cert_size, stats.total_size);
3608 3783
3609 // Read the response without writing a request, so the read will be pending. 3784 // Read the response without writing a request, so the read will be pending.
3610 TestCompletionCallback read_callback; 3785 TestCompletionCallback read_callback;
3611 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); 3786 scoped_refptr<IOBuffer> buf(new IOBuffer(4096));
3612 rv = sock_->Read(buf.get(), 4096, read_callback.callback()); 3787 rv = Read(sock_.get(), buf.get(), 4096, read_callback.callback());
3613 EXPECT_EQ(ERR_IO_PENDING, rv); 3788 EXPECT_EQ(ERR_IO_PENDING, rv);
3614 3789
3615 // Dump memory again and check that |buffer_size| contain the read buffer. 3790 // Dump memory again and check that |buffer_size| contain the read buffer.
3616 StreamSocket::SocketMemoryStats stats2; 3791 StreamSocket::SocketMemoryStats stats2;
3617 sock_->DumpMemoryStats(&stats2); 3792 sock_->DumpMemoryStats(&stats2);
3618 EXPECT_EQ(17 * 1024u, stats2.buffer_size); 3793
3794 bool buffer_released = false;
3795 if (GetParam()) {
3796 #if defined(OS_POSIX)
3797 buffer_released = true;
davidben 2017/02/10 23:33:48 Probably wants a comment for what's happening here
xunjieli 2017/02/13 20:28:18 Done.
3798 #endif
3799 }
3800 if (buffer_released) {
3801 EXPECT_EQ(0u, stats2.buffer_size);
3802 EXPECT_EQ(stats.serialized_cert_size, stats2.total_size);
3803 } else {
3804 EXPECT_EQ(17 * 1024u, stats2.buffer_size);
3805 EXPECT_LT(17 * 1024u, stats2.total_size);
3806 }
3619 EXPECT_EQ(1u, stats2.cert_count); 3807 EXPECT_EQ(1u, stats2.cert_count);
3620 EXPECT_LT(0u, stats2.serialized_cert_size); 3808 EXPECT_LT(0u, stats2.serialized_cert_size);
3621 EXPECT_LT(17 * 1024u, stats2.total_size);
3622 } 3809 }
3623 3810
3624 } // namespace net 3811 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698