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 #include <algorithm> | |
davidben
2017/03/02 20:47:03
Nit: Swap this and the blank line below.
xunjieli
2017/03/02 22:30:20
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 Loading... | |
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 Loading... | |
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 Loading... | |
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 Loading... | |
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 17 matching lines...) Expand all Loading... | |
398 void BlockWrite(); | 446 void BlockWrite(); |
399 void UnblockWrite(); | 447 void UnblockWrite(); |
400 | 448 |
401 // Waits for the blocked Write() call to be scheduled. | 449 // Waits for the blocked Write() call to be scheduled. |
402 void WaitForWrite(); | 450 void WaitForWrite(); |
403 | 451 |
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 |
456 // Handles async completion of ReadIfReady(). | |
457 void CompleteReadIfReady(scoped_refptr<IOBuffer> buffer, int rv); | |
458 | |
408 // Finishes the current read. | 459 // Finishes the current read. |
409 void ReturnReadResult(); | 460 void ReturnReadResult(); |
410 | 461 |
411 // True if read callbacks are blocked. | 462 // True if read callbacks are blocked. |
412 bool should_block_read_ = false; | 463 bool should_block_read_ = false; |
413 | 464 |
465 // Used to buffer result returned by a completed ReadIfReady(). | |
466 std::string read_if_ready_buf_; | |
467 | |
468 // Non-null if there is a pending ReadIfReady(). | |
469 CompletionCallback read_if_ready_callback_; | |
470 | |
414 // The buffer for the pending read, or NULL if not consumed. | 471 // The buffer for the pending read, or NULL if not consumed. |
415 scoped_refptr<IOBuffer> pending_read_buf_; | 472 scoped_refptr<IOBuffer> pending_read_buf_; |
416 | 473 |
417 // The size of the pending read buffer, or -1 if not set. | 474 // The size of the pending read buffer, or -1 if not set. |
418 int pending_read_buf_len_ = -1; | 475 int pending_read_buf_len_ = -1; |
419 | 476 |
420 // The user callback for the pending read call. | 477 // The user callback for the pending read call. |
421 CompletionCallback pending_read_callback_; | 478 CompletionCallback pending_read_callback_; |
422 | 479 |
423 // The result for the blocked read callback, or ERR_IO_PENDING if not | 480 // The result for the blocked read callback, or ERR_IO_PENDING if not |
(...skipping 20 matching lines...) Expand all Loading... | |
444 }; | 501 }; |
445 | 502 |
446 int FakeBlockingStreamSocket::Read(IOBuffer* buf, | 503 int FakeBlockingStreamSocket::Read(IOBuffer* buf, |
447 int len, | 504 int len, |
448 const CompletionCallback& callback) { | 505 const CompletionCallback& callback) { |
449 DCHECK(!pending_read_buf_); | 506 DCHECK(!pending_read_buf_); |
450 DCHECK(pending_read_callback_.is_null()); | 507 DCHECK(pending_read_callback_.is_null()); |
451 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); | 508 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); |
452 DCHECK(!callback.is_null()); | 509 DCHECK(!callback.is_null()); |
453 | 510 |
454 int rv = transport_->Read(buf, len, base::Bind( | 511 int rv = transport_->Read( |
455 &FakeBlockingStreamSocket::OnReadCompleted, base::Unretained(this))); | 512 buf, len, base::Bind(&FakeBlockingStreamSocket::OnReadCompleted, |
513 base::Unretained(this))); | |
456 if (rv == ERR_IO_PENDING || should_block_read_) { | 514 if (rv == ERR_IO_PENDING || should_block_read_) { |
457 // Save the callback to be called later. | 515 // Save the callback to be called later. |
458 pending_read_buf_ = buf; | 516 pending_read_buf_ = buf; |
459 pending_read_buf_len_ = len; | 517 pending_read_buf_len_ = len; |
460 pending_read_callback_ = callback; | 518 pending_read_callback_ = callback; |
461 // Save the read result. | 519 // Save the read result. |
462 if (rv != ERR_IO_PENDING) { | 520 if (rv != ERR_IO_PENDING) { |
463 OnReadCompleted(rv); | 521 OnReadCompleted(rv); |
464 rv = ERR_IO_PENDING; | 522 rv = ERR_IO_PENDING; |
465 } | 523 } |
466 } | 524 } |
467 return rv; | 525 return rv; |
468 } | 526 } |
469 | 527 |
528 int FakeBlockingStreamSocket::ReadIfReady(IOBuffer* buf, | |
529 int len, | |
530 const CompletionCallback& callback) { | |
531 if (!read_if_ready_buf_.empty()) { | |
532 // This is possible if the caller calls ReadIfReady() with a smaller buffer | |
533 // size than previously or if BlockReadResult() is called after | |
534 // ReadIfReady() reports there is data available but before the data is | |
535 // drained. | |
davidben
2017/03/02 20:47:03
The comment should probably be clearer that we're
xunjieli
2017/03/02 22:30:21
Done.
| |
536 CHECK(!should_block_read_); | |
537 CHECK_GE(len, (int)read_if_ready_buf_.size()); | |
davidben
2017/03/02 20:47:03
Nit: static_cast<int>(read_if_ready_buf_.size())
xunjieli
2017/03/02 22:30:20
Done.
| |
538 int rv = read_if_ready_buf_.size(); | |
539 memcpy(buf->data(), read_if_ready_buf_.data(), rv); | |
540 read_if_ready_buf_.clear(); | |
541 return rv; | |
542 } | |
543 scoped_refptr<IOBuffer> buf_copy = new IOBuffer(len); | |
544 int rv = Read(buf_copy.get(), len, | |
545 base::Bind(&FakeBlockingStreamSocket::CompleteReadIfReady, | |
546 base::Unretained(this), buf_copy)); | |
547 if (rv > 0) | |
548 memcpy(buf->data(), buf_copy->data(), rv); | |
549 if (rv == ERR_IO_PENDING) | |
550 read_if_ready_callback_ = callback; | |
551 return rv; | |
552 } | |
553 | |
470 int FakeBlockingStreamSocket::Write(IOBuffer* buf, | 554 int FakeBlockingStreamSocket::Write(IOBuffer* buf, |
471 int len, | 555 int len, |
472 const CompletionCallback& callback) { | 556 const CompletionCallback& callback) { |
473 DCHECK(buf); | 557 DCHECK(buf); |
474 DCHECK_LE(0, len); | 558 DCHECK_LE(0, len); |
475 | 559 |
476 if (!should_block_write_) | 560 if (!should_block_write_) |
477 return transport_->Write(buf, len, callback); | 561 return transport_->Write(buf, len, callback); |
478 | 562 |
479 // Schedule the write, but do nothing. | 563 // Schedule the write, but do nothing. |
(...skipping 20 matching lines...) Expand all Loading... | |
500 DCHECK(should_block_read_); | 584 DCHECK(should_block_read_); |
501 should_block_read_ = false; | 585 should_block_read_ = false; |
502 | 586 |
503 // If the operation has since completed, return the result to the caller. | 587 // If the operation has since completed, return the result to the caller. |
504 if (pending_read_result_ != ERR_IO_PENDING) | 588 if (pending_read_result_ != ERR_IO_PENDING) |
505 ReturnReadResult(); | 589 ReturnReadResult(); |
506 } | 590 } |
507 | 591 |
508 bool FakeBlockingStreamSocket::ReplaceReadResult(const std::string& data) { | 592 bool FakeBlockingStreamSocket::ReplaceReadResult(const std::string& data) { |
509 DCHECK(should_block_read_); | 593 DCHECK(should_block_read_); |
510 DCHECK_NE(ERR_IO_PENDING, pending_read_result_); | |
511 DCHECK(pending_read_buf_); | 594 DCHECK(pending_read_buf_); |
512 DCHECK_NE(-1, pending_read_buf_len_); | 595 DCHECK_NE(-1, pending_read_buf_len_); |
513 | 596 |
514 if (static_cast<size_t>(pending_read_buf_len_) < data.size()) | 597 if (static_cast<size_t>(pending_read_buf_len_) < data.size()) |
515 return false; | 598 return false; |
516 | 599 |
600 DCHECK_NE(ERR_IO_PENDING, pending_read_result_); | |
601 DCHECK(pending_read_buf_); | |
602 DCHECK_NE(-1, pending_read_buf_len_); | |
davidben
2017/03/02 20:47:02
601 and 602 are already checked a few lines above.
xunjieli
2017/03/02 22:30:21
Done.
| |
517 memcpy(pending_read_buf_->data(), data.data(), data.size()); | 603 memcpy(pending_read_buf_->data(), data.data(), data.size()); |
518 pending_read_result_ = data.size(); | 604 pending_read_result_ = data.size(); |
519 return true; | 605 return true; |
520 } | 606 } |
521 | 607 |
522 void FakeBlockingStreamSocket::WaitForReadResult() { | 608 void FakeBlockingStreamSocket::WaitForReadResult() { |
523 DCHECK(should_block_read_); | 609 DCHECK(should_block_read_); |
524 DCHECK(!read_loop_); | 610 DCHECK(!read_loop_); |
525 | 611 |
526 if (pending_read_result_ != ERR_IO_PENDING) | 612 if (pending_read_result_ != ERR_IO_PENDING) |
(...skipping 50 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 void SetUp() override { | |
962 if (GetParam()) | |
963 scoped_feature_list_.InitAndEnableFeature(Socket::kReadIfReadyExperiment); | |
964 } | |
965 | |
966 // Convienient wrapper to call Read()/ReadIfReady() depending on GetParam(). | |
967 int Read(StreamSocket* socket, | |
968 IOBuffer* buf, | |
969 int buf_len, | |
970 const CompletionCallback& callback) { | |
971 if (GetParam()) | |
972 return socket->ReadIfReady(buf, buf_len, callback); | |
973 return socket->Read(buf, buf_len, callback); | |
974 } | |
975 | |
976 // Wait for Read()/ReadIfReady() to complete. | |
977 int WaitForReadCompletion(StreamSocket* socket, | |
978 IOBuffer* buf, | |
979 int buf_len, | |
980 TestCompletionCallback* callback, | |
981 int rv) { | |
982 if (!GetParam()) | |
983 return callback->GetResult(rv); | |
984 while (rv == ERR_IO_PENDING) { | |
985 rv = callback->GetResult(rv); | |
986 if (rv != OK) | |
987 return rv; | |
988 rv = socket->ReadIfReady(buf, buf_len, callback->callback()); | |
989 } | |
990 return rv; | |
991 } | |
992 | |
993 // Calls Read()/ReadIfReady() and waits for it to return data. | |
994 int ReadAndWaitForCompletion(StreamSocket* socket, | |
995 IOBuffer* buf, | |
996 int buf_len) { | |
997 TestCompletionCallback callback; | |
998 int rv = Read(socket, buf, buf_len, callback.callback()); | |
999 return WaitForReadCompletion(socket, buf, buf_len, &callback, rv); | |
1000 } | |
1001 | |
1002 private: | |
1003 base::test::ScopedFeatureList scoped_feature_list_; | |
1004 }; | |
1005 | |
1006 INSTANTIATE_TEST_CASE_P(/* no prefix */, | |
1007 SSLClientSocketReadTest, | |
1008 ::testing::Bool()); | |
1009 | |
861 // Verifies the correctness of GetSSLCertRequestInfo. | 1010 // Verifies the correctness of GetSSLCertRequestInfo. |
862 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest { | 1011 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest { |
863 protected: | 1012 protected: |
864 // Creates a test server with the given SSLOptions, connects to it and returns | 1013 // Creates a test server with the given SSLOptions, connects to it and returns |
865 // the SSLCertRequestInfo reported by the socket. | 1014 // the SSLCertRequestInfo reported by the socket. |
866 scoped_refptr<SSLCertRequestInfo> GetCertRequest( | 1015 scoped_refptr<SSLCertRequestInfo> GetCertRequest( |
867 SpawnedTestServer::SSLOptions ssl_options) { | 1016 SpawnedTestServer::SSLOptions ssl_options) { |
868 SpawnedTestServer spawned_test_server(SpawnedTestServer::TYPE_HTTPS, | 1017 SpawnedTestServer spawned_test_server(SpawnedTestServer::TYPE_HTTPS, |
869 ssl_options, base::FilePath()); | 1018 ssl_options, base::FilePath()); |
870 if (!spawned_test_server.Start()) | 1019 if (!spawned_test_server.Start()) |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1199 EXPECT_FALSE(sock_->IsConnected()); | 1348 EXPECT_FALSE(sock_->IsConnected()); |
1200 } | 1349 } |
1201 | 1350 |
1202 // TODO(wtc): Add unit tests for IsConnectedAndIdle: | 1351 // TODO(wtc): Add unit tests for IsConnectedAndIdle: |
1203 // - Server closes an SSL connection (with a close_notify alert message). | 1352 // - Server closes an SSL connection (with a close_notify alert message). |
1204 // - Server closes the underlying TCP connection directly. | 1353 // - Server closes the underlying TCP connection directly. |
1205 // - Server sends data unexpectedly. | 1354 // - Server sends data unexpectedly. |
1206 | 1355 |
1207 // Tests that the socket can be read from successfully. Also test that a peer's | 1356 // Tests that the socket can be read from successfully. Also test that a peer's |
1208 // close_notify alert is successfully processed without error. | 1357 // close_notify alert is successfully processed without error. |
1209 TEST_F(SSLClientSocketTest, Read) { | 1358 TEST_P(SSLClientSocketReadTest, Read) { |
1210 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1359 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1211 | 1360 |
1212 TestCompletionCallback callback; | 1361 TestCompletionCallback callback; |
1213 std::unique_ptr<StreamSocket> transport( | 1362 std::unique_ptr<StreamSocket> transport( |
1214 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1363 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1215 EXPECT_EQ(0, transport->GetTotalReceivedBytes()); | 1364 EXPECT_EQ(0, transport->GetTotalReceivedBytes()); |
1216 | 1365 |
1217 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1366 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1218 EXPECT_THAT(rv, IsOk()); | 1367 EXPECT_THAT(rv, IsOk()); |
1219 | 1368 |
(...skipping 15 matching lines...) Expand all Loading... | |
1235 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); | 1384 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
1236 | 1385 |
1237 rv = callback.GetResult(sock->Write( | 1386 rv = callback.GetResult(sock->Write( |
1238 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); | 1387 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); |
1239 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); | 1388 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
1240 | 1389 |
1241 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1390 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1242 int64_t unencrypted_bytes_read = 0; | 1391 int64_t unencrypted_bytes_read = 0; |
1243 int64_t network_bytes_read_during_handshake = sock->GetTotalReceivedBytes(); | 1392 int64_t network_bytes_read_during_handshake = sock->GetTotalReceivedBytes(); |
1244 do { | 1393 do { |
1245 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | 1394 rv = ReadAndWaitForCompletion(sock.get(), buf.get(), 4096); |
1246 EXPECT_GE(rv, 0); | 1395 EXPECT_GE(rv, 0); |
1247 if (rv >= 0) { | 1396 if (rv >= 0) { |
1248 unencrypted_bytes_read += rv; | 1397 unencrypted_bytes_read += rv; |
1249 } | 1398 } |
1250 } while (rv > 0); | 1399 } while (rv > 0); |
1251 EXPECT_GT(unencrypted_bytes_read, 0); | 1400 EXPECT_GT(unencrypted_bytes_read, 0); |
1252 // Reading the payload should increase the number of bytes on network layer. | 1401 // Reading the payload should increase the number of bytes on network layer. |
1253 EXPECT_GT(sock->GetTotalReceivedBytes(), network_bytes_read_during_handshake); | 1402 EXPECT_GT(sock->GetTotalReceivedBytes(), network_bytes_read_during_handshake); |
1254 // Number of bytes received on the network after the handshake should be | 1403 // Number of bytes received on the network after the handshake should be |
1255 // higher than the number of encrypted bytes read. | 1404 // higher than the number of encrypted bytes read. |
(...skipping 30 matching lines...) Expand all Loading... | |
1286 | 1435 |
1287 rv = callback.GetResult(sock->Connect(callback.callback())); | 1436 rv = callback.GetResult(sock->Connect(callback.callback())); |
1288 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1437 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
1289 EXPECT_FALSE(sock->IsConnected()); | 1438 EXPECT_FALSE(sock->IsConnected()); |
1290 } | 1439 } |
1291 | 1440 |
1292 // Tests that the SSLClientSocket properly handles when the underlying transport | 1441 // Tests that the SSLClientSocket properly handles when the underlying transport |
1293 // synchronously returns an error code - such as if an intermediary terminates | 1442 // synchronously returns an error code - such as if an intermediary terminates |
1294 // the socket connection uncleanly. | 1443 // the socket connection uncleanly. |
1295 // This is a regression test for http://crbug.com/238536 | 1444 // This is a regression test for http://crbug.com/238536 |
1296 TEST_F(SSLClientSocketTest, Read_WithSynchronousError) { | 1445 TEST_P(SSLClientSocketReadTest, Read_WithSynchronousError) { |
1297 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1446 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1298 | 1447 |
1299 TestCompletionCallback callback; | 1448 TestCompletionCallback callback; |
1300 std::unique_ptr<StreamSocket> real_transport( | 1449 std::unique_ptr<StreamSocket> real_transport( |
1301 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1450 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1302 std::unique_ptr<SynchronousErrorStreamSocket> transport( | 1451 std::unique_ptr<SynchronousErrorStreamSocket> transport( |
1303 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1452 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1304 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1453 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1305 EXPECT_THAT(rv, IsOk()); | 1454 EXPECT_THAT(rv, IsOk()); |
1306 | 1455 |
(...skipping 21 matching lines...) Expand all Loading... | |
1328 EXPECT_EQ(kRequestTextSize, rv); | 1477 EXPECT_EQ(kRequestTextSize, rv); |
1329 | 1478 |
1330 // Simulate an unclean/forcible shutdown. | 1479 // Simulate an unclean/forcible shutdown. |
1331 raw_transport->SetNextReadError(ERR_CONNECTION_RESET); | 1480 raw_transport->SetNextReadError(ERR_CONNECTION_RESET); |
1332 | 1481 |
1333 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1482 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1334 | 1483 |
1335 // Note: This test will hang if this bug has regressed. Simply checking that | 1484 // 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 | 1485 // rv != ERR_IO_PENDING is insufficient, as ERR_IO_PENDING is a legitimate |
1337 // result when using a dedicated task runner for NSS. | 1486 // result when using a dedicated task runner for NSS. |
1338 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | 1487 rv = Read(sock.get(), buf.get(), 4096, callback.callback()); |
1339 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1488 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
davidben
2017/03/02 20:47:03
ReadAndWaitForCompletion?
xunjieli
2017/03/02 22:30:21
Done.
| |
1340 } | 1489 } |
1341 | 1490 |
1342 // Tests that the SSLClientSocket properly handles when the underlying transport | 1491 // Tests that the SSLClientSocket properly handles when the underlying transport |
1343 // asynchronously returns an error code while writing data - such as if an | 1492 // asynchronously returns an error code while writing data - such as if an |
1344 // intermediary terminates the socket connection uncleanly. | 1493 // intermediary terminates the socket connection uncleanly. |
1345 // This is a regression test for http://crbug.com/249848 | 1494 // This is a regression test for http://crbug.com/249848 |
1346 TEST_F(SSLClientSocketTest, Write_WithSynchronousError) { | 1495 TEST_F(SSLClientSocketTest, Write_WithSynchronousError) { |
1347 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1496 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1348 | 1497 |
1349 TestCompletionCallback callback; | 1498 TestCompletionCallback callback; |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1463 int old_write_count = raw_counting_socket->write_count(); | 1612 int old_write_count = raw_counting_socket->write_count(); |
1464 base::RunLoop loop; | 1613 base::RunLoop loop; |
1465 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 1614 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
1466 FROM_HERE, loop.QuitClosure(), base::TimeDelta::FromMilliseconds(100)); | 1615 FROM_HERE, loop.QuitClosure(), base::TimeDelta::FromMilliseconds(100)); |
1467 loop.Run(); | 1616 loop.Run(); |
1468 EXPECT_EQ(old_write_count, raw_counting_socket->write_count()); | 1617 EXPECT_EQ(old_write_count, raw_counting_socket->write_count()); |
1469 } | 1618 } |
1470 | 1619 |
1471 // Test the full duplex mode, with Read and Write pending at the same time. | 1620 // 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. | 1621 // This test also serves as a regression test for http://crbug.com/29815. |
1473 TEST_F(SSLClientSocketTest, Read_FullDuplex) { | 1622 TEST_P(SSLClientSocketReadTest, Read_FullDuplex) { |
1474 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1623 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1475 | 1624 |
1476 int rv; | 1625 int rv; |
1477 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 1626 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
1478 EXPECT_THAT(rv, IsOk()); | 1627 EXPECT_THAT(rv, IsOk()); |
1479 | 1628 |
1480 // Issue a "hanging" Read first. | 1629 // Issue a "hanging" Read first. |
1481 TestCompletionCallback callback; | 1630 TestCompletionCallback callback; |
1482 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1631 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1483 rv = sock_->Read(buf.get(), 4096, callback.callback()); | 1632 rv = Read(sock_.get(), buf.get(), 4096, callback.callback()); |
1484 // We haven't written the request, so there should be no response yet. | 1633 // We haven't written the request, so there should be no response yet. |
1485 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); | 1634 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
1486 | 1635 |
1487 // Write the request. | 1636 // Write the request. |
1488 // The request is padded with a User-Agent header to a size that causes the | 1637 // 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. | 1638 // memio circular buffer (4k bytes) in SSLClientSocketNSS to wrap around. |
1490 // This tests the fix for http://crbug.com/29815. | 1639 // This tests the fix for http://crbug.com/29815. |
1491 std::string request_text = "GET / HTTP/1.1\r\nUser-Agent: long browser name "; | 1640 std::string request_text = "GET / HTTP/1.1\r\nUser-Agent: long browser name "; |
1492 for (int i = 0; i < 3770; ++i) | 1641 for (int i = 0; i < 3770; ++i) |
1493 request_text.push_back('*'); | 1642 request_text.push_back('*'); |
1494 request_text.append("\r\n\r\n"); | 1643 request_text.append("\r\n\r\n"); |
1495 scoped_refptr<IOBuffer> request_buffer(new StringIOBuffer(request_text)); | 1644 scoped_refptr<IOBuffer> request_buffer(new StringIOBuffer(request_text)); |
1496 | 1645 |
1497 TestCompletionCallback callback2; // Used for Write only. | 1646 TestCompletionCallback callback2; // Used for Write only. |
1498 rv = callback2.GetResult(sock_->Write( | 1647 rv = callback2.GetResult(sock_->Write( |
1499 request_buffer.get(), request_text.size(), callback2.callback())); | 1648 request_buffer.get(), request_text.size(), callback2.callback())); |
1500 EXPECT_EQ(static_cast<int>(request_text.size()), rv); | 1649 EXPECT_EQ(static_cast<int>(request_text.size()), rv); |
1501 | 1650 |
1502 // Now get the Read result. | 1651 // Now get the Read result. |
1503 rv = callback.WaitForResult(); | 1652 rv = WaitForReadCompletion(sock_.get(), buf.get(), 4096, &callback, rv); |
1504 EXPECT_GT(rv, 0); | 1653 EXPECT_GT(rv, 0); |
1505 } | 1654 } |
1506 | 1655 |
1507 // Attempts to Read() and Write() from an SSLClientSocketNSS in full duplex | 1656 // Attempts to Read() and Write() from an SSLClientSocketNSS in full duplex |
1508 // mode when the underlying transport is blocked on sending data. When the | 1657 // 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 | 1658 // 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() | 1659 // Read() and Write() callbacks. If the socket is deleted by the Read() |
1511 // callback, the Write() callback should not be invoked. | 1660 // callback, the Write() callback should not be invoked. |
1512 // Regression test for http://crbug.com/232633 | 1661 // Regression test for http://crbug.com/232633 |
1513 TEST_F(SSLClientSocketTest, Read_DeleteWhilePendingFullDuplex) { | 1662 TEST_P(SSLClientSocketReadTest, Read_DeleteWhilePendingFullDuplex) { |
1514 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1663 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1515 | 1664 |
1516 TestCompletionCallback callback; | 1665 TestCompletionCallback callback; |
1517 std::unique_ptr<StreamSocket> real_transport( | 1666 std::unique_ptr<StreamSocket> real_transport( |
1518 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1667 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1519 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer | 1668 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer |
1520 // is retained in order to configure additional errors. | 1669 // is retained in order to configure additional errors. |
1521 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( | 1670 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( |
1522 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1671 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1523 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); | 1672 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 | 1701 // ... but have those errors returned asynchronously. Because the Write() will |
1553 // return first, this will trigger the error. | 1702 // return first, this will trigger the error. |
1554 raw_transport->BlockReadResult(); | 1703 raw_transport->BlockReadResult(); |
1555 raw_transport->BlockWrite(); | 1704 raw_transport->BlockWrite(); |
1556 | 1705 |
1557 // Enqueue a Read() before calling Write(), which should "hang" due to | 1706 // Enqueue a Read() before calling Write(), which should "hang" due to |
1558 // the ERR_IO_PENDING caused by SetReadShouldBlock() and thus return. | 1707 // the ERR_IO_PENDING caused by SetReadShouldBlock() and thus return. |
1559 SSLClientSocket* raw_sock = sock.get(); | 1708 SSLClientSocket* raw_sock = sock.get(); |
1560 DeleteSocketCallback read_callback(sock.release()); | 1709 DeleteSocketCallback read_callback(sock.release()); |
1561 scoped_refptr<IOBuffer> read_buf(new IOBuffer(4096)); | 1710 scoped_refptr<IOBuffer> read_buf(new IOBuffer(4096)); |
1562 rv = raw_sock->Read(read_buf.get(), 4096, read_callback.callback()); | 1711 rv = Read(raw_sock, read_buf.get(), 4096, read_callback.callback()); |
1563 | 1712 |
1564 // Ensure things didn't complete synchronously, otherwise |sock| is invalid. | 1713 // Ensure things didn't complete synchronously, otherwise |sock| is invalid. |
1565 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); | 1714 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
1566 ASSERT_FALSE(read_callback.have_result()); | 1715 ASSERT_FALSE(read_callback.have_result()); |
1567 | 1716 |
1568 // Attempt to write the remaining data. OpenSSL will return that its blocked | 1717 // Attempt to write the remaining data. OpenSSL will return that its blocked |
1569 // because the underlying transport is blocked. | 1718 // because the underlying transport is blocked. |
1570 rv = raw_sock->Write(request_buffer.get(), | 1719 rv = raw_sock->Write(request_buffer.get(), |
1571 request_buffer->BytesRemaining(), | 1720 request_buffer->BytesRemaining(), |
1572 callback.callback()); | 1721 callback.callback()); |
1573 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); | 1722 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
1574 ASSERT_FALSE(callback.have_result()); | 1723 ASSERT_FALSE(callback.have_result()); |
1575 | 1724 |
1576 // Now unblock Write(), which will invoke OnSendComplete and (eventually) | 1725 // Now unblock Write(), which will invoke OnSendComplete and (eventually) |
1577 // call the Read() callback, deleting the socket and thus aborting calling | 1726 // call the Read() callback, deleting the socket and thus aborting calling |
1578 // the Write() callback. | 1727 // the Write() callback. |
1579 raw_transport->UnblockWrite(); | 1728 raw_transport->UnblockWrite(); |
1580 | 1729 |
1730 // |read_callback| deletes |sock| so if ReadIfReady() is used, we will get OK | |
1731 // asynchronously but can't continue reading because the socket is gone. | |
1581 rv = read_callback.WaitForResult(); | 1732 rv = read_callback.WaitForResult(); |
1582 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1733 if (GetParam()) { |
1734 EXPECT_THAT(rv, IsOk()); | |
1735 } else { | |
1736 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | |
1737 } | |
1583 | 1738 |
1584 // The Write callback should not have been called. | 1739 // The Write callback should not have been called. |
1585 EXPECT_FALSE(callback.have_result()); | 1740 EXPECT_FALSE(callback.have_result()); |
1586 } | 1741 } |
1587 | 1742 |
1588 // Tests that the SSLClientSocket does not crash if data is received on the | 1743 // 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 | 1744 // transport socket after a failing write. This can occur if we have a Write |
1590 // error in a SPDY socket. | 1745 // error in a SPDY socket. |
1591 // Regression test for http://crbug.com/335557 | 1746 // Regression test for http://crbug.com/335557 |
1592 TEST_F(SSLClientSocketTest, Read_WithWriteError) { | 1747 TEST_P(SSLClientSocketReadTest, Read_WithWriteError) { |
1593 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1748 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1594 | 1749 |
1595 TestCompletionCallback callback; | 1750 TestCompletionCallback callback; |
1596 std::unique_ptr<StreamSocket> real_transport( | 1751 std::unique_ptr<StreamSocket> real_transport( |
1597 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1752 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1598 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer | 1753 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer |
1599 // is retained in order to configure additional errors. | 1754 // is retained in order to configure additional errors. |
1600 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( | 1755 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( |
1601 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1756 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1602 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); | 1757 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); |
(...skipping 24 matching lines...) Expand all Loading... | |
1627 memcpy(request_buffer->data(), request_text, kRequestTextSize); | 1782 memcpy(request_buffer->data(), request_text, kRequestTextSize); |
1628 | 1783 |
1629 rv = callback.GetResult( | 1784 rv = callback.GetResult( |
1630 sock->Write(request_buffer.get(), kRequestTextSize, callback.callback())); | 1785 sock->Write(request_buffer.get(), kRequestTextSize, callback.callback())); |
1631 EXPECT_EQ(kRequestTextSize, rv); | 1786 EXPECT_EQ(kRequestTextSize, rv); |
1632 | 1787 |
1633 // Start a hanging read. | 1788 // Start a hanging read. |
1634 TestCompletionCallback read_callback; | 1789 TestCompletionCallback read_callback; |
1635 raw_transport->BlockReadResult(); | 1790 raw_transport->BlockReadResult(); |
1636 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1791 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1637 rv = sock->Read(buf.get(), 4096, read_callback.callback()); | 1792 rv = Read(sock.get(), buf.get(), 4096, read_callback.callback()); |
1638 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 1793 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
1639 | 1794 |
1640 // Perform another write, but have it fail. Write a request larger than the | 1795 // 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 | 1796 // internal socket buffers so that the request hits the underlying transport |
1642 // socket and detects the error. | 1797 // socket and detects the error. |
1643 std::string long_request_text = | 1798 std::string long_request_text = |
1644 "GET / HTTP/1.1\r\nUser-Agent: long browser name "; | 1799 "GET / HTTP/1.1\r\nUser-Agent: long browser name "; |
1645 long_request_text.append(20 * 1024, '*'); | 1800 long_request_text.append(20 * 1024, '*'); |
1646 long_request_text.append("\r\n\r\n"); | 1801 long_request_text.append("\r\n\r\n"); |
1647 scoped_refptr<DrainableIOBuffer> long_request_buffer(new DrainableIOBuffer( | 1802 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 | 1814 // Abort if the entire input is ever consumed. The input is larger than |
1660 // the SSLClientSocket's write buffers. | 1815 // the SSLClientSocket's write buffers. |
1661 ASSERT_LT(0, long_request_buffer->BytesRemaining()); | 1816 ASSERT_LT(0, long_request_buffer->BytesRemaining()); |
1662 } | 1817 } |
1663 } while (rv > 0); | 1818 } while (rv > 0); |
1664 | 1819 |
1665 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1820 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
1666 | 1821 |
1667 // At this point the Read result is available. Transport write errors are | 1822 // At this point the Read result is available. Transport write errors are |
1668 // surfaced through Writes. See https://crbug.com/249848. | 1823 // surfaced through Writes. See https://crbug.com/249848. |
1669 rv = read_callback.WaitForResult(); | 1824 rv = WaitForReadCompletion(sock.get(), buf.get(), 4096, &read_callback, rv); |
1670 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1825 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
1671 | 1826 |
1672 // Release the read. This does not cause a crash. | 1827 // Release the read. This does not cause a crash. |
1673 raw_transport->UnblockReadResult(); | 1828 raw_transport->UnblockReadResult(); |
1674 base::RunLoop().RunUntilIdle(); | 1829 base::RunLoop().RunUntilIdle(); |
1675 } | 1830 } |
1676 | 1831 |
1677 // Tests that SSLClientSocket fails the handshake if the underlying | 1832 // Tests that SSLClientSocket fails the handshake if the underlying |
1678 // transport is cleanly closed. | 1833 // transport is cleanly closed. |
1679 TEST_F(SSLClientSocketTest, Connect_WithZeroReturn) { | 1834 TEST_F(SSLClientSocketTest, Connect_WithZeroReturn) { |
(...skipping 15 matching lines...) Expand all Loading... | |
1695 raw_transport->SetNextReadError(0); | 1850 raw_transport->SetNextReadError(0); |
1696 | 1851 |
1697 rv = callback.GetResult(sock->Connect(callback.callback())); | 1852 rv = callback.GetResult(sock->Connect(callback.callback())); |
1698 EXPECT_THAT(rv, IsError(ERR_CONNECTION_CLOSED)); | 1853 EXPECT_THAT(rv, IsError(ERR_CONNECTION_CLOSED)); |
1699 EXPECT_FALSE(sock->IsConnected()); | 1854 EXPECT_FALSE(sock->IsConnected()); |
1700 } | 1855 } |
1701 | 1856 |
1702 // Tests that SSLClientSocket returns a Read of size 0 if the underlying socket | 1857 // 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. | 1858 // is cleanly closed, but the peer does not send close_notify. |
1704 // This is a regression test for https://crbug.com/422246 | 1859 // This is a regression test for https://crbug.com/422246 |
1705 TEST_F(SSLClientSocketTest, Read_WithZeroReturn) { | 1860 TEST_P(SSLClientSocketReadTest, Read_WithZeroReturn) { |
1706 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1861 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1707 | 1862 |
1708 TestCompletionCallback callback; | 1863 TestCompletionCallback callback; |
1709 std::unique_ptr<StreamSocket> real_transport( | 1864 std::unique_ptr<StreamSocket> real_transport( |
1710 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1865 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1711 std::unique_ptr<SynchronousErrorStreamSocket> transport( | 1866 std::unique_ptr<SynchronousErrorStreamSocket> transport( |
1712 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1867 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1713 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1868 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1714 EXPECT_THAT(rv, IsOk()); | 1869 EXPECT_THAT(rv, IsOk()); |
1715 | 1870 |
1716 // Disable TLS False Start to ensure the handshake has completed. | 1871 // Disable TLS False Start to ensure the handshake has completed. |
1717 SSLConfig ssl_config; | 1872 SSLConfig ssl_config; |
1718 ssl_config.false_start_enabled = false; | 1873 ssl_config.false_start_enabled = false; |
1719 | 1874 |
1720 SynchronousErrorStreamSocket* raw_transport = transport.get(); | 1875 SynchronousErrorStreamSocket* raw_transport = transport.get(); |
1721 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( | 1876 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
1722 std::move(transport), spawned_test_server()->host_port_pair(), | 1877 std::move(transport), spawned_test_server()->host_port_pair(), |
1723 ssl_config)); | 1878 ssl_config)); |
1724 | 1879 |
1725 rv = callback.GetResult(sock->Connect(callback.callback())); | 1880 rv = callback.GetResult(sock->Connect(callback.callback())); |
1726 EXPECT_THAT(rv, IsOk()); | 1881 EXPECT_THAT(rv, IsOk()); |
1727 EXPECT_TRUE(sock->IsConnected()); | 1882 EXPECT_TRUE(sock->IsConnected()); |
1728 | 1883 |
1729 raw_transport->SetNextReadError(0); | 1884 raw_transport->SetNextReadError(0); |
1730 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1885 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1731 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | 1886 rv = ReadAndWaitForCompletion(sock.get(), buf.get(), 4096); |
1732 EXPECT_EQ(0, rv); | 1887 EXPECT_EQ(0, rv); |
1733 } | 1888 } |
1734 | 1889 |
1735 // Tests that SSLClientSocket cleanly returns a Read of size 0 if the | 1890 // Tests that SSLClientSocket cleanly returns a Read of size 0 if the |
1736 // underlying socket is cleanly closed asynchronously. | 1891 // underlying socket is cleanly closed asynchronously. |
1737 // This is a regression test for https://crbug.com/422246 | 1892 // This is a regression test for https://crbug.com/422246 |
1738 TEST_F(SSLClientSocketTest, Read_WithAsyncZeroReturn) { | 1893 TEST_P(SSLClientSocketReadTest, Read_WithAsyncZeroReturn) { |
1739 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1894 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1740 | 1895 |
1741 TestCompletionCallback callback; | 1896 TestCompletionCallback callback; |
1742 std::unique_ptr<StreamSocket> real_transport( | 1897 std::unique_ptr<StreamSocket> real_transport( |
1743 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1898 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1744 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( | 1899 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( |
1745 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1900 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1746 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); | 1901 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); |
1747 std::unique_ptr<FakeBlockingStreamSocket> transport( | 1902 std::unique_ptr<FakeBlockingStreamSocket> transport( |
1748 new FakeBlockingStreamSocket(std::move(error_socket))); | 1903 new FakeBlockingStreamSocket(std::move(error_socket))); |
1749 FakeBlockingStreamSocket* raw_transport = transport.get(); | 1904 FakeBlockingStreamSocket* raw_transport = transport.get(); |
1750 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1905 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1751 EXPECT_THAT(rv, IsOk()); | 1906 EXPECT_THAT(rv, IsOk()); |
1752 | 1907 |
1753 // Disable TLS False Start to ensure the handshake has completed. | 1908 // Disable TLS False Start to ensure the handshake has completed. |
1754 SSLConfig ssl_config; | 1909 SSLConfig ssl_config; |
1755 ssl_config.false_start_enabled = false; | 1910 ssl_config.false_start_enabled = false; |
1756 | 1911 |
1757 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( | 1912 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
1758 std::move(transport), spawned_test_server()->host_port_pair(), | 1913 std::move(transport), spawned_test_server()->host_port_pair(), |
1759 ssl_config)); | 1914 ssl_config)); |
1760 | 1915 |
1761 rv = callback.GetResult(sock->Connect(callback.callback())); | 1916 rv = callback.GetResult(sock->Connect(callback.callback())); |
1762 EXPECT_THAT(rv, IsOk()); | 1917 EXPECT_THAT(rv, IsOk()); |
1763 EXPECT_TRUE(sock->IsConnected()); | 1918 EXPECT_TRUE(sock->IsConnected()); |
1764 | 1919 |
1765 raw_error_socket->SetNextReadError(0); | 1920 raw_error_socket->SetNextReadError(0); |
1766 raw_transport->BlockReadResult(); | 1921 raw_transport->BlockReadResult(); |
1767 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1922 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1768 rv = sock->Read(buf.get(), 4096, callback.callback()); | 1923 TestCompletionCallback read_callback; |
1924 rv = Read(sock.get(), buf.get(), 4096, read_callback.callback()); | |
1769 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 1925 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
1770 | 1926 |
1771 raw_transport->UnblockReadResult(); | 1927 raw_transport->UnblockReadResult(); |
1772 rv = callback.GetResult(rv); | 1928 rv = WaitForReadCompletion(sock.get(), buf.get(), 4096, &read_callback, rv); |
1773 EXPECT_EQ(0, rv); | 1929 EXPECT_EQ(0, rv); |
1774 } | 1930 } |
1775 | 1931 |
1776 // Tests that fatal alerts from the peer are processed. This is a regression | 1932 // Tests that fatal alerts from the peer are processed. This is a regression |
1777 // test for https://crbug.com/466303. | 1933 // test for https://crbug.com/466303. |
1778 TEST_F(SSLClientSocketTest, Read_WithFatalAlert) { | 1934 TEST_P(SSLClientSocketReadTest, Read_WithFatalAlert) { |
1779 SpawnedTestServer::SSLOptions ssl_options; | 1935 SpawnedTestServer::SSLOptions ssl_options; |
1780 ssl_options.alert_after_handshake = true; | 1936 ssl_options.alert_after_handshake = true; |
1781 ASSERT_TRUE(StartTestServer(ssl_options)); | 1937 ASSERT_TRUE(StartTestServer(ssl_options)); |
1782 | 1938 |
1783 int rv; | 1939 int rv; |
1784 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 1940 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
1785 EXPECT_THAT(rv, IsOk()); | 1941 EXPECT_THAT(rv, IsOk()); |
1786 | 1942 |
1787 // Receive the fatal alert. | 1943 // Receive the fatal alert. |
1788 TestCompletionCallback callback; | 1944 TestCompletionCallback callback; |
1789 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1945 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1790 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, callback.GetResult(sock_->Read( | 1946 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, |
1791 buf.get(), 4096, callback.callback()))); | 1947 ReadAndWaitForCompletion(sock_.get(), buf.get(), 4096)); |
1792 } | 1948 } |
1793 | 1949 |
1794 TEST_F(SSLClientSocketTest, Read_SmallChunks) { | 1950 TEST_P(SSLClientSocketReadTest, Read_SmallChunks) { |
1795 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1951 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1796 | 1952 |
1797 int rv; | 1953 int rv; |
1798 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 1954 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
1799 EXPECT_THAT(rv, IsOk()); | 1955 EXPECT_THAT(rv, IsOk()); |
1800 | 1956 |
1801 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 1957 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
1802 scoped_refptr<IOBuffer> request_buffer( | 1958 scoped_refptr<IOBuffer> request_buffer( |
1803 new IOBuffer(arraysize(request_text) - 1)); | 1959 new IOBuffer(arraysize(request_text) - 1)); |
1804 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); | 1960 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
1805 | 1961 |
1806 TestCompletionCallback callback; | 1962 TestCompletionCallback callback; |
1807 rv = callback.GetResult(sock_->Write( | 1963 rv = callback.GetResult(sock_->Write( |
1808 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); | 1964 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); |
1809 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); | 1965 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
1810 | 1966 |
1811 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); | 1967 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); |
1812 do { | 1968 do { |
1813 rv = callback.GetResult(sock_->Read(buf.get(), 1, callback.callback())); | 1969 rv = ReadAndWaitForCompletion(sock_.get(), buf.get(), 1); |
1814 EXPECT_GE(rv, 0); | 1970 EXPECT_GE(rv, 0); |
1815 } while (rv > 0); | 1971 } while (rv > 0); |
1816 } | 1972 } |
1817 | 1973 |
1818 TEST_F(SSLClientSocketTest, Read_ManySmallRecords) { | 1974 TEST_P(SSLClientSocketReadTest, Read_ManySmallRecords) { |
1819 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1975 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1820 | 1976 |
1821 TestCompletionCallback callback; | 1977 TestCompletionCallback callback; |
1822 | 1978 |
1823 std::unique_ptr<StreamSocket> real_transport( | 1979 std::unique_ptr<StreamSocket> real_transport( |
1824 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1980 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1825 std::unique_ptr<ReadBufferingStreamSocket> transport( | 1981 std::unique_ptr<ReadBufferingStreamSocket> transport( |
1826 new ReadBufferingStreamSocket(std::move(real_transport))); | 1982 new ReadBufferingStreamSocket(std::move(real_transport))); |
1827 ReadBufferingStreamSocket* raw_transport = transport.get(); | 1983 ReadBufferingStreamSocket* raw_transport = transport.get(); |
1828 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1984 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 | 2007 // 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 | 2008 // many smaller SSL records (the TestServer writes along 1350 byte |
1853 // plaintext boundaries), although there may also be a few records that are | 2009 // plaintext boundaries), although there may also be a few records that are |
1854 // smaller or larger, due to timing and SSL False Start. | 2010 // 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 | 2011 // 15K was chosen because 15K is smaller than the 17K (max) read issued by |
1856 // the SSLClientSocket implementation, and larger than the minimum amount | 2012 // the SSLClientSocket implementation, and larger than the minimum amount |
1857 // of ciphertext necessary to contain the 8K of plaintext requested below. | 2013 // of ciphertext necessary to contain the 8K of plaintext requested below. |
1858 raw_transport->SetBufferSize(15000); | 2014 raw_transport->SetBufferSize(15000); |
1859 | 2015 |
1860 scoped_refptr<IOBuffer> buffer(new IOBuffer(8192)); | 2016 scoped_refptr<IOBuffer> buffer(new IOBuffer(8192)); |
1861 rv = callback.GetResult(sock->Read(buffer.get(), 8192, callback.callback())); | 2017 rv = ReadAndWaitForCompletion(sock.get(), buffer.get(), 8192); |
1862 ASSERT_EQ(rv, 8192); | 2018 ASSERT_EQ(rv, 8192); |
1863 } | 2019 } |
1864 | 2020 |
1865 TEST_F(SSLClientSocketTest, Read_Interrupted) { | 2021 TEST_P(SSLClientSocketReadTest, Read_Interrupted) { |
1866 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 2022 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1867 | 2023 |
1868 int rv; | 2024 int rv; |
1869 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 2025 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
1870 EXPECT_THAT(rv, IsOk()); | 2026 EXPECT_THAT(rv, IsOk()); |
1871 | 2027 |
1872 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 2028 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
1873 scoped_refptr<IOBuffer> request_buffer( | 2029 scoped_refptr<IOBuffer> request_buffer( |
1874 new IOBuffer(arraysize(request_text) - 1)); | 2030 new IOBuffer(arraysize(request_text) - 1)); |
1875 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); | 2031 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
1876 | 2032 |
1877 TestCompletionCallback callback; | 2033 TestCompletionCallback callback; |
1878 rv = callback.GetResult(sock_->Write( | 2034 rv = callback.GetResult(sock_->Write( |
1879 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); | 2035 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); |
1880 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); | 2036 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
1881 | 2037 |
1882 // Do a partial read and then exit. This test should not crash! | 2038 // Do a partial read and then exit. This test should not crash! |
1883 scoped_refptr<IOBuffer> buf(new IOBuffer(512)); | 2039 scoped_refptr<IOBuffer> buf(new IOBuffer(512)); |
1884 rv = callback.GetResult(sock_->Read(buf.get(), 512, callback.callback())); | 2040 rv = ReadAndWaitForCompletion(sock_.get(), buf.get(), 512); |
1885 EXPECT_GT(rv, 0); | 2041 EXPECT_GT(rv, 0); |
1886 } | 2042 } |
1887 | 2043 |
1888 TEST_F(SSLClientSocketTest, Read_FullLogging) { | 2044 TEST_P(SSLClientSocketReadTest, Read_FullLogging) { |
1889 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 2045 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1890 | 2046 |
1891 TestCompletionCallback callback; | 2047 TestCompletionCallback callback; |
1892 TestNetLog log; | 2048 TestNetLog log; |
1893 log.SetCaptureMode(NetLogCaptureMode::IncludeSocketBytes()); | 2049 log.SetCaptureMode(NetLogCaptureMode::IncludeSocketBytes()); |
1894 std::unique_ptr<StreamSocket> transport( | 2050 std::unique_ptr<StreamSocket> transport( |
1895 new TCPClientSocket(addr(), NULL, &log, NetLogSource())); | 2051 new TCPClientSocket(addr(), NULL, &log, NetLogSource())); |
1896 int rv = callback.GetResult(transport->Connect(callback.callback())); | 2052 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1897 EXPECT_THAT(rv, IsOk()); | 2053 EXPECT_THAT(rv, IsOk()); |
1898 | 2054 |
(...skipping 15 matching lines...) Expand all Loading... | |
1914 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); | 2070 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
1915 | 2071 |
1916 TestNetLogEntry::List entries; | 2072 TestNetLogEntry::List entries; |
1917 log.GetEntries(&entries); | 2073 log.GetEntries(&entries); |
1918 size_t last_index = ExpectLogContainsSomewhereAfter( | 2074 size_t last_index = ExpectLogContainsSomewhereAfter( |
1919 entries, 5, NetLogEventType::SSL_SOCKET_BYTES_SENT, | 2075 entries, 5, NetLogEventType::SSL_SOCKET_BYTES_SENT, |
1920 NetLogEventPhase::NONE); | 2076 NetLogEventPhase::NONE); |
1921 | 2077 |
1922 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 2078 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1923 for (;;) { | 2079 for (;;) { |
1924 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | 2080 rv = ReadAndWaitForCompletion(sock.get(), buf.get(), 4096); |
1925 EXPECT_GE(rv, 0); | 2081 EXPECT_GE(rv, 0); |
1926 if (rv <= 0) | 2082 if (rv <= 0) |
1927 break; | 2083 break; |
1928 | 2084 |
1929 log.GetEntries(&entries); | 2085 log.GetEntries(&entries); |
1930 last_index = ExpectLogContainsSomewhereAfter( | 2086 last_index = ExpectLogContainsSomewhereAfter( |
1931 entries, last_index + 1, NetLogEventType::SSL_SOCKET_BYTES_RECEIVED, | 2087 entries, last_index + 1, NetLogEventType::SSL_SOCKET_BYTES_RECEIVED, |
1932 NetLogEventPhase::NONE); | 2088 NetLogEventPhase::NONE); |
1933 } | 2089 } |
1934 } | 2090 } |
(...skipping 1651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3586 // Replace it with an alert. | 3742 // Replace it with an alert. |
3587 raw_transport->ReplaceReadResult( | 3743 raw_transport->ReplaceReadResult( |
3588 FormatTLS12Alert(49 /* AlertDescription.access_denied */)); | 3744 FormatTLS12Alert(49 /* AlertDescription.access_denied */)); |
3589 raw_transport->UnblockReadResult(); | 3745 raw_transport->UnblockReadResult(); |
3590 | 3746 |
3591 rv = callback.GetResult(rv); | 3747 rv = callback.GetResult(rv); |
3592 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT)); | 3748 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT)); |
3593 } | 3749 } |
3594 | 3750 |
3595 // Basic test for dumping memory stats. | 3751 // Basic test for dumping memory stats. |
3596 TEST_F(SSLClientSocketTest, DumpMemoryStats) { | 3752 TEST_P(SSLClientSocketReadTest, DumpMemoryStats) { |
3597 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 3753 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
3598 | 3754 |
3599 int rv; | 3755 int rv; |
3600 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 3756 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
3601 EXPECT_THAT(rv, IsOk()); | 3757 EXPECT_THAT(rv, IsOk()); |
3602 StreamSocket::SocketMemoryStats stats; | 3758 StreamSocket::SocketMemoryStats stats; |
3603 sock_->DumpMemoryStats(&stats); | 3759 sock_->DumpMemoryStats(&stats); |
3604 EXPECT_EQ(0u, stats.buffer_size); | 3760 EXPECT_EQ(0u, stats.buffer_size); |
3605 EXPECT_EQ(1u, stats.cert_count); | 3761 EXPECT_EQ(1u, stats.cert_count); |
3606 EXPECT_LT(0u, stats.serialized_cert_size); | 3762 EXPECT_LT(0u, stats.serialized_cert_size); |
3607 EXPECT_EQ(stats.serialized_cert_size, stats.total_size); | 3763 EXPECT_EQ(stats.serialized_cert_size, stats.total_size); |
3608 | 3764 |
3609 // Read the response without writing a request, so the read will be pending. | 3765 // Read the response without writing a request, so the read will be pending. |
3610 TestCompletionCallback read_callback; | 3766 TestCompletionCallback read_callback; |
3611 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 3767 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
3612 rv = sock_->Read(buf.get(), 4096, read_callback.callback()); | 3768 rv = Read(sock_.get(), buf.get(), 4096, read_callback.callback()); |
3613 EXPECT_EQ(ERR_IO_PENDING, rv); | 3769 EXPECT_EQ(ERR_IO_PENDING, rv); |
3614 | 3770 |
3615 // Dump memory again and check that |buffer_size| contain the read buffer. | 3771 // Dump memory again and check that |buffer_size| contain the read buffer. |
3616 StreamSocket::SocketMemoryStats stats2; | 3772 StreamSocket::SocketMemoryStats stats2; |
3617 sock_->DumpMemoryStats(&stats2); | 3773 sock_->DumpMemoryStats(&stats2); |
3618 EXPECT_EQ(17 * 1024u, stats2.buffer_size); | 3774 |
3775 bool buffer_released = false; | |
3776 if (GetParam()) { | |
3777 // TODO(xunjieli): crbug.com/690915. Implement ReadIfReady() for windows. | |
3778 #if defined(OS_POSIX) | |
3779 buffer_released = true; | |
3780 #endif | |
3781 } | |
3782 if (buffer_released) { | |
3783 EXPECT_EQ(0u, stats2.buffer_size); | |
3784 EXPECT_EQ(stats.serialized_cert_size, stats2.total_size); | |
3785 } else { | |
3786 EXPECT_EQ(17 * 1024u, stats2.buffer_size); | |
3787 EXPECT_LT(17 * 1024u, stats2.total_size); | |
3788 } | |
3619 EXPECT_EQ(1u, stats2.cert_count); | 3789 EXPECT_EQ(1u, stats2.cert_count); |
3620 EXPECT_LT(0u, stats2.serialized_cert_size); | 3790 EXPECT_LT(0u, stats2.serialized_cert_size); |
3621 EXPECT_LT(17 * 1024u, stats2.total_size); | |
3622 } | 3791 } |
3623 | 3792 |
3624 } // namespace net | 3793 } // namespace net |
OLD | NEW |