OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/ssl_client_socket.h" | 5 #include "net/socket/ssl_client_socket.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/location.h" | 14 #include "base/location.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/metrics/field_trial.h" | |
17 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
18 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
20 #include "base/test/mock_entropy_provider.h" | |
19 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
20 #include "base/time/time.h" | 22 #include "base/time/time.h" |
21 #include "base/values.h" | 23 #include "base/values.h" |
22 #include "net/base/address_list.h" | 24 #include "net/base/address_list.h" |
23 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
24 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
25 #include "net/base/test_completion_callback.h" | 27 #include "net/base/test_completion_callback.h" |
26 #include "net/cert/asn1_util.h" | 28 #include "net/cert/asn1_util.h" |
27 #include "net/cert/ct_policy_enforcer.h" | 29 #include "net/cert/ct_policy_enforcer.h" |
28 #include "net/cert/ct_policy_status.h" | 30 #include "net/cert/ct_policy_status.h" |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 int64_t GetTotalReceivedBytes() const override { | 135 int64_t GetTotalReceivedBytes() const override { |
134 return transport_->GetTotalReceivedBytes(); | 136 return transport_->GetTotalReceivedBytes(); |
135 } | 137 } |
136 | 138 |
137 // Socket implementation: | 139 // Socket implementation: |
138 int Read(IOBuffer* buf, | 140 int Read(IOBuffer* buf, |
139 int buf_len, | 141 int buf_len, |
140 const CompletionCallback& callback) override { | 142 const CompletionCallback& callback) override { |
141 return transport_->Read(buf, buf_len, callback); | 143 return transport_->Read(buf, buf_len, callback); |
142 } | 144 } |
145 int ReadIfReady(IOBuffer* buf, | |
146 int buf_len, | |
147 const CompletionCallback& callback) override { | |
148 return transport_->ReadIfReady(buf, buf_len, callback); | |
149 } | |
143 int Write(IOBuffer* buf, | 150 int Write(IOBuffer* buf, |
144 int buf_len, | 151 int buf_len, |
145 const CompletionCallback& callback) override { | 152 const CompletionCallback& callback) override { |
146 return transport_->Write(buf, buf_len, callback); | 153 return transport_->Write(buf, buf_len, callback); |
147 } | 154 } |
148 int SetReceiveBufferSize(int32_t size) override { | 155 int SetReceiveBufferSize(int32_t size) override { |
149 return transport_->SetReceiveBufferSize(size); | 156 return transport_->SetReceiveBufferSize(size); |
150 } | 157 } |
151 int SetSendBufferSize(int32_t size) override { | 158 int SetSendBufferSize(int32_t size) override { |
152 return transport_->SetSendBufferSize(size); | 159 return transport_->SetSendBufferSize(size); |
(...skipping 12 matching lines...) Expand all Loading... | |
165 class ReadBufferingStreamSocket : public WrappedStreamSocket { | 172 class ReadBufferingStreamSocket : public WrappedStreamSocket { |
166 public: | 173 public: |
167 explicit ReadBufferingStreamSocket(std::unique_ptr<StreamSocket> transport); | 174 explicit ReadBufferingStreamSocket(std::unique_ptr<StreamSocket> transport); |
168 ~ReadBufferingStreamSocket() override {} | 175 ~ReadBufferingStreamSocket() override {} |
169 | 176 |
170 // Socket implementation: | 177 // Socket implementation: |
171 int Read(IOBuffer* buf, | 178 int Read(IOBuffer* buf, |
172 int buf_len, | 179 int buf_len, |
173 const CompletionCallback& callback) override; | 180 const CompletionCallback& callback) override; |
174 | 181 |
182 int ReadIfReady(IOBuffer* buf, | |
183 int buf_len, | |
184 const CompletionCallback& callback) override; | |
185 | |
175 // Sets the internal buffer to |size|. This must not be greater than | 186 // Sets the internal buffer to |size|. This must not be greater than |
176 // the largest value supplied to Read() - that is, it does not handle | 187 // the largest value supplied to Read() - that is, it does not handle |
177 // having "leftovers" at the end of Read(). | 188 // having "leftovers" at the end of Read(). |
178 // Each call to Read() will be prevented from completion until at least | 189 // Each call to Read() will be prevented from completion until at least |
179 // |size| data has been read. | 190 // |size| data has been read. |
180 // Set to 0 to turn off buffering, causing Read() to transparently | 191 // Set to 0 to turn off buffering, causing Read() to transparently |
181 // read via the underlying transport. | 192 // read via the underlying transport. |
182 void SetBufferSize(int size); | 193 void SetBufferSize(int size); |
183 | 194 |
184 private: | 195 private: |
185 enum State { | 196 enum State { |
186 STATE_NONE, | 197 STATE_NONE, |
187 STATE_READ, | 198 STATE_READ, |
188 STATE_READ_COMPLETE, | 199 STATE_READ_COMPLETE, |
189 }; | 200 }; |
190 | 201 |
191 int DoLoop(int result); | 202 // Routine shared between Read() and ReadIfReady(). |
192 int DoRead(); | 203 int ReadCommon(IOBuffer* buf, |
193 int DoReadComplete(int result); | 204 int buf_len, |
194 void OnReadCompleted(int result); | 205 const CompletionCallback& callback, |
206 bool use_read_if_ready); | |
207 int DoLoop(int result, bool use_read_if_ready); | |
208 int DoRead(bool use_read_if_ready); | |
209 int DoReadComplete(int result, bool use_read_if_ready); | |
210 void OnReadCompleted(bool use_read_if_ready, int result); | |
195 | 211 |
196 State state_; | 212 State state_; |
197 scoped_refptr<GrowableIOBuffer> read_buffer_; | 213 scoped_refptr<GrowableIOBuffer> read_buffer_; |
198 int buffer_size_; | 214 int buffer_size_; |
199 | 215 |
200 scoped_refptr<IOBuffer> user_read_buf_; | 216 scoped_refptr<IOBuffer> user_read_buf_; |
201 CompletionCallback user_read_callback_; | 217 CompletionCallback user_read_callback_; |
202 }; | 218 }; |
203 | 219 |
204 ReadBufferingStreamSocket::ReadBufferingStreamSocket( | 220 ReadBufferingStreamSocket::ReadBufferingStreamSocket( |
205 std::unique_ptr<StreamSocket> transport) | 221 std::unique_ptr<StreamSocket> transport) |
206 : WrappedStreamSocket(std::move(transport)), | 222 : WrappedStreamSocket(std::move(transport)), |
207 read_buffer_(new GrowableIOBuffer()), | 223 read_buffer_(new GrowableIOBuffer()), |
208 buffer_size_(0) {} | 224 buffer_size_(0) {} |
209 | 225 |
210 void ReadBufferingStreamSocket::SetBufferSize(int size) { | 226 void ReadBufferingStreamSocket::SetBufferSize(int size) { |
211 DCHECK(!user_read_buf_.get()); | 227 DCHECK(!user_read_buf_.get()); |
212 buffer_size_ = size; | 228 buffer_size_ = size; |
213 read_buffer_->SetCapacity(size); | 229 read_buffer_->SetCapacity(size); |
214 } | 230 } |
215 | 231 |
216 int ReadBufferingStreamSocket::Read(IOBuffer* buf, | 232 int ReadBufferingStreamSocket::Read(IOBuffer* buf, |
217 int buf_len, | 233 int buf_len, |
218 const CompletionCallback& callback) { | 234 const CompletionCallback& callback) { |
219 if (buffer_size_ == 0) | 235 if (buffer_size_ == 0) |
220 return transport_->Read(buf, buf_len, callback); | 236 return transport_->Read(buf, buf_len, callback); |
237 return ReadCommon(buf, buf_len, callback, false); | |
238 } | |
239 | |
240 int ReadBufferingStreamSocket::ReadIfReady(IOBuffer* buf, | |
241 int buf_len, | |
242 const CompletionCallback& callback) { | |
davidben
2017/02/01 22:25:58
Similar comment. I think you can structure this li
xunjieli
2017/02/03 16:35:33
Done.
| |
243 if (buffer_size_ == 0) | |
244 return transport_->ReadIfReady(buf, buf_len, callback); | |
245 if (read_buffer_->RemainingCapacity() == 0) { | |
246 memcpy(buf->data(), read_buffer_->StartOfBuffer(), | |
247 read_buffer_->capacity()); | |
248 read_buffer_->set_offset(0); | |
249 return read_buffer_->capacity(); | |
250 } | |
251 int rv = ReadCommon(buf, buf_len, callback, true); | |
252 user_read_buf_ = nullptr; | |
253 return rv; | |
254 } | |
255 | |
256 int ReadBufferingStreamSocket::ReadCommon(IOBuffer* buf, | |
257 int buf_len, | |
258 const CompletionCallback& callback, | |
259 bool use_read_if_ready) { | |
260 DCHECK(!user_read_buf_); | |
221 | 261 |
222 if (buf_len < buffer_size_) | 262 if (buf_len < buffer_size_) |
223 return ERR_UNEXPECTED; | 263 return ERR_UNEXPECTED; |
224 | 264 |
225 state_ = STATE_READ; | 265 state_ = STATE_READ; |
226 user_read_buf_ = buf; | 266 user_read_buf_ = buf; |
227 int result = DoLoop(OK); | 267 int result = DoLoop(OK, use_read_if_ready); |
228 if (result == ERR_IO_PENDING) | 268 if (result == ERR_IO_PENDING) |
229 user_read_callback_ = callback; | 269 user_read_callback_ = callback; |
230 else | 270 else |
231 user_read_buf_ = NULL; | 271 user_read_buf_ = nullptr; |
232 return result; | 272 return result; |
233 } | 273 } |
234 | 274 |
235 int ReadBufferingStreamSocket::DoLoop(int result) { | 275 int ReadBufferingStreamSocket::DoLoop(int result, bool use_read_if_ready) { |
236 int rv = result; | 276 int rv = result; |
237 do { | 277 do { |
238 State current_state = state_; | 278 State current_state = state_; |
239 state_ = STATE_NONE; | 279 state_ = STATE_NONE; |
240 switch (current_state) { | 280 switch (current_state) { |
241 case STATE_READ: | 281 case STATE_READ: |
242 rv = DoRead(); | 282 rv = DoRead(use_read_if_ready); |
243 break; | 283 break; |
244 case STATE_READ_COMPLETE: | 284 case STATE_READ_COMPLETE: |
245 rv = DoReadComplete(rv); | 285 rv = DoReadComplete(rv, use_read_if_ready); |
246 break; | 286 break; |
247 case STATE_NONE: | 287 case STATE_NONE: |
248 default: | 288 default: |
249 NOTREACHED() << "Unexpected state: " << current_state; | 289 NOTREACHED() << "Unexpected state: " << current_state; |
250 rv = ERR_UNEXPECTED; | 290 rv = ERR_UNEXPECTED; |
251 break; | 291 break; |
252 } | 292 } |
253 } while (rv != ERR_IO_PENDING && state_ != STATE_NONE); | 293 } while (rv != ERR_IO_PENDING && state_ != STATE_NONE); |
254 return rv; | 294 return rv; |
255 } | 295 } |
256 | 296 |
257 int ReadBufferingStreamSocket::DoRead() { | 297 int ReadBufferingStreamSocket::DoRead(bool use_read_if_ready) { |
258 state_ = STATE_READ_COMPLETE; | 298 state_ = STATE_READ_COMPLETE; |
259 int rv = | 299 if (use_read_if_ready) { |
260 transport_->Read(read_buffer_.get(), | 300 return transport_->ReadIfReady( |
261 read_buffer_->RemainingCapacity(), | 301 read_buffer_.get(), read_buffer_->RemainingCapacity(), |
262 base::Bind(&ReadBufferingStreamSocket::OnReadCompleted, | 302 base::Bind(&ReadBufferingStreamSocket::OnReadCompleted, |
263 base::Unretained(this))); | 303 base::Unretained(this), true)); |
264 return rv; | 304 } else { |
305 return transport_->Read( | |
306 read_buffer_.get(), read_buffer_->RemainingCapacity(), | |
307 base::Bind(&ReadBufferingStreamSocket::OnReadCompleted, | |
308 base::Unretained(this), false)); | |
309 } | |
265 } | 310 } |
266 | 311 |
267 int ReadBufferingStreamSocket::DoReadComplete(int result) { | 312 int ReadBufferingStreamSocket::DoReadComplete(int result, |
313 bool use_read_if_ready) { | |
268 state_ = STATE_NONE; | 314 state_ = STATE_NONE; |
269 if (result <= 0) | 315 if (result <= 0) |
270 return result; | 316 return result; |
271 | 317 |
272 read_buffer_->set_offset(read_buffer_->offset() + result); | 318 read_buffer_->set_offset(read_buffer_->offset() + result); |
273 if (read_buffer_->RemainingCapacity() > 0) { | 319 if (read_buffer_->RemainingCapacity() > 0) { |
274 state_ = STATE_READ; | 320 state_ = STATE_READ; |
275 return OK; | 321 return OK; |
276 } | 322 } |
277 | 323 |
324 if (use_read_if_ready) | |
325 return OK; | |
326 | |
278 memcpy(user_read_buf_->data(), | 327 memcpy(user_read_buf_->data(), |
279 read_buffer_->StartOfBuffer(), | 328 read_buffer_->StartOfBuffer(), |
280 read_buffer_->capacity()); | 329 read_buffer_->capacity()); |
281 read_buffer_->set_offset(0); | 330 read_buffer_->set_offset(0); |
282 return read_buffer_->capacity(); | 331 return read_buffer_->capacity(); |
283 } | 332 } |
284 | 333 |
285 void ReadBufferingStreamSocket::OnReadCompleted(int result) { | 334 void ReadBufferingStreamSocket::OnReadCompleted(bool use_read_if_ready, |
286 result = DoLoop(result); | 335 int result) { |
336 DCHECK_NE(ERR_IO_PENDING, result); | |
337 | |
338 if (use_read_if_ready && result == 0) | |
339 state_ = STATE_READ; | |
340 | |
341 result = DoLoop(result, use_read_if_ready); | |
287 if (result == ERR_IO_PENDING) | 342 if (result == ERR_IO_PENDING) |
288 return; | 343 return; |
289 | |
290 user_read_buf_ = NULL; | 344 user_read_buf_ = NULL; |
291 base::ResetAndReturn(&user_read_callback_).Run(result); | 345 base::ResetAndReturn(&user_read_callback_).Run(result); |
292 } | 346 } |
293 | 347 |
294 // Simulates synchronously receiving an error during Read() or Write() | 348 // Simulates synchronously receiving an error during Read() or Write() |
295 class SynchronousErrorStreamSocket : public WrappedStreamSocket { | 349 class SynchronousErrorStreamSocket : public WrappedStreamSocket { |
296 public: | 350 public: |
297 explicit SynchronousErrorStreamSocket(std::unique_ptr<StreamSocket> transport) | 351 explicit SynchronousErrorStreamSocket(std::unique_ptr<StreamSocket> transport) |
298 : WrappedStreamSocket(std::move(transport)) {} | 352 : WrappedStreamSocket(std::move(transport)) {} |
299 ~SynchronousErrorStreamSocket() override {} | 353 ~SynchronousErrorStreamSocket() override {} |
300 | 354 |
301 // Socket implementation: | 355 // Socket implementation: |
302 int Read(IOBuffer* buf, | 356 int Read(IOBuffer* buf, |
303 int buf_len, | 357 int buf_len, |
304 const CompletionCallback& callback) override; | 358 const CompletionCallback& callback) override; |
359 int ReadIfReady(IOBuffer* buf, | |
360 int buf_len, | |
361 const CompletionCallback& callback) override; | |
305 int Write(IOBuffer* buf, | 362 int Write(IOBuffer* buf, |
306 int buf_len, | 363 int buf_len, |
307 const CompletionCallback& callback) override; | 364 const CompletionCallback& callback) override; |
308 | 365 |
309 // Sets the next Read() call and all future calls to return |error|. | 366 // Sets the next Read() call and all future calls to return |error|. |
310 // If there is already a pending asynchronous read, the configured error | 367 // If there is already a pending asynchronous read, the configured error |
311 // will not be returned until that asynchronous read has completed and Read() | 368 // will not be returned until that asynchronous read has completed and Read() |
312 // is called again. | 369 // is called again. |
313 void SetNextReadError(int error) { | 370 void SetNextReadError(int error) { |
314 DCHECK_GE(0, error); | 371 DCHECK_GE(0, error); |
(...skipping 22 matching lines...) Expand all Loading... | |
337 }; | 394 }; |
338 | 395 |
339 int SynchronousErrorStreamSocket::Read(IOBuffer* buf, | 396 int SynchronousErrorStreamSocket::Read(IOBuffer* buf, |
340 int buf_len, | 397 int buf_len, |
341 const CompletionCallback& callback) { | 398 const CompletionCallback& callback) { |
342 if (have_read_error_) | 399 if (have_read_error_) |
343 return pending_read_error_; | 400 return pending_read_error_; |
344 return transport_->Read(buf, buf_len, callback); | 401 return transport_->Read(buf, buf_len, callback); |
345 } | 402 } |
346 | 403 |
404 int SynchronousErrorStreamSocket::ReadIfReady( | |
405 IOBuffer* buf, | |
406 int buf_len, | |
407 const CompletionCallback& callback) { | |
408 if (have_read_error_) | |
409 return pending_read_error_; | |
410 return transport_->ReadIfReady(buf, buf_len, callback); | |
411 } | |
412 | |
347 int SynchronousErrorStreamSocket::Write(IOBuffer* buf, | 413 int SynchronousErrorStreamSocket::Write(IOBuffer* buf, |
348 int buf_len, | 414 int buf_len, |
349 const CompletionCallback& callback) { | 415 const CompletionCallback& callback) { |
350 if (have_write_error_) | 416 if (have_write_error_) |
351 return pending_write_error_; | 417 return pending_write_error_; |
352 return transport_->Write(buf, buf_len, callback); | 418 return transport_->Write(buf, buf_len, callback); |
353 } | 419 } |
354 | 420 |
355 // FakeBlockingStreamSocket wraps an existing StreamSocket and simulates the | 421 // FakeBlockingStreamSocket wraps an existing StreamSocket and simulates the |
356 // underlying transport needing to complete things asynchronously in a | 422 // underlying transport needing to complete things asynchronously in a |
357 // deterministic manner (e.g.: independent of the TestServer and the OS's | 423 // deterministic manner (e.g.: independent of the TestServer and the OS's |
358 // semantics). | 424 // semantics). |
359 class FakeBlockingStreamSocket : public WrappedStreamSocket { | 425 class FakeBlockingStreamSocket : public WrappedStreamSocket { |
360 public: | 426 public: |
361 explicit FakeBlockingStreamSocket(std::unique_ptr<StreamSocket> transport) | 427 explicit FakeBlockingStreamSocket(std::unique_ptr<StreamSocket> transport) |
362 : WrappedStreamSocket(std::move(transport)) {} | 428 : WrappedStreamSocket(std::move(transport)) {} |
363 ~FakeBlockingStreamSocket() override {} | 429 ~FakeBlockingStreamSocket() override {} |
364 | 430 |
365 // Socket implementation: | 431 // Socket implementation: |
366 int Read(IOBuffer* buf, | 432 int Read(IOBuffer* buf, |
367 int buf_len, | 433 int buf_len, |
368 const CompletionCallback& callback) override; | 434 const CompletionCallback& callback) override; |
435 int ReadIfReady(IOBuffer* buf, | |
436 int buf_len, | |
437 const CompletionCallback& callback) override; | |
369 int Write(IOBuffer* buf, | 438 int Write(IOBuffer* buf, |
370 int buf_len, | 439 int buf_len, |
371 const CompletionCallback& callback) override; | 440 const CompletionCallback& callback) override; |
372 | 441 |
373 int pending_read_result() const { return pending_read_result_; } | 442 int pending_read_result() const { return pending_read_result_; } |
374 IOBuffer* pending_read_buf() const { return pending_read_buf_.get(); } | 443 IOBuffer* pending_read_buf() const { return pending_read_buf_.get(); } |
375 | 444 |
376 // Blocks read results on the socket. Reads will not complete until | 445 // Blocks read results on the socket. Reads will not complete until |
377 // UnblockReadResult() has been called and a result is ready from the | 446 // UnblockReadResult() has been called and a result is ready from the |
378 // underlying transport. Note: if BlockReadResult() is called while there is a | 447 // underlying transport. Note: if BlockReadResult() is called while there is a |
(...skipping 14 matching lines...) Expand all Loading... | |
393 // is a pending asynchronous write, it is NOT blocked. For purposes of | 462 // is a pending asynchronous write, it is NOT blocked. For purposes of |
394 // blocking writes, data is considered to have reached the underlying | 463 // blocking writes, data is considered to have reached the underlying |
395 // transport as soon as Write() is called. | 464 // transport as soon as Write() is called. |
396 void BlockWrite(); | 465 void BlockWrite(); |
397 void UnblockWrite(); | 466 void UnblockWrite(); |
398 | 467 |
399 // Waits for the blocked Write() call to be scheduled. | 468 // Waits for the blocked Write() call to be scheduled. |
400 void WaitForWrite(); | 469 void WaitForWrite(); |
401 | 470 |
402 private: | 471 private: |
472 // Shared between Read() and ReadIfReady(). If |use_read_if_ready| is true, | |
473 // ReadIfReady() will be used. | |
474 int ReadCommon(IOBuffer* buf, | |
475 int buf_len, | |
476 const CompletionCallback& callback, | |
477 bool use_read_if_ready); | |
478 | |
403 // Handles completion from the underlying transport read. | 479 // Handles completion from the underlying transport read. |
404 void OnReadCompleted(int result); | 480 void OnReadCompleted(int result); |
405 | 481 |
406 // Finishes the current read. | 482 // Finishes the current read. |
407 void ReturnReadResult(); | 483 void ReturnReadResult(); |
408 | 484 |
409 // True if read callbacks are blocked. | 485 // True if read callbacks are blocked. |
410 bool should_block_read_ = false; | 486 bool should_block_read_ = false; |
411 | 487 |
412 // The buffer for the pending read, or NULL if not consumed. | 488 // The buffer for the pending read, or NULL if not consumed. |
(...skipping 24 matching lines...) Expand all Loading... | |
437 // The length for the pending write, or -1 if not scheduled. | 513 // The length for the pending write, or -1 if not scheduled. |
438 int pending_write_len_ = -1; | 514 int pending_write_len_ = -1; |
439 | 515 |
440 // WaitForWrite() wait loop. | 516 // WaitForWrite() wait loop. |
441 std::unique_ptr<base::RunLoop> write_loop_; | 517 std::unique_ptr<base::RunLoop> write_loop_; |
442 }; | 518 }; |
443 | 519 |
444 int FakeBlockingStreamSocket::Read(IOBuffer* buf, | 520 int FakeBlockingStreamSocket::Read(IOBuffer* buf, |
445 int len, | 521 int len, |
446 const CompletionCallback& callback) { | 522 const CompletionCallback& callback) { |
447 DCHECK(!pending_read_buf_); | 523 return ReadCommon(buf, len, callback, false); |
448 DCHECK(pending_read_callback_.is_null()); | 524 } |
449 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); | |
450 DCHECK(!callback.is_null()); | |
451 | 525 |
452 int rv = transport_->Read(buf, len, base::Bind( | 526 int FakeBlockingStreamSocket::ReadIfReady(IOBuffer* buf, |
453 &FakeBlockingStreamSocket::OnReadCompleted, base::Unretained(this))); | 527 int len, |
454 if (rv == ERR_IO_PENDING || should_block_read_) { | 528 const CompletionCallback& callback) { |
455 // Save the callback to be called later. | 529 int rv = ReadCommon(buf, len, callback, true); |
456 pending_read_buf_ = buf; | 530 pending_read_buf_ = nullptr; |
457 pending_read_buf_len_ = len; | 531 pending_read_buf_len_ = 0; |
458 pending_read_callback_ = callback; | |
459 // Save the read result. | |
460 if (rv != ERR_IO_PENDING) { | |
461 OnReadCompleted(rv); | |
462 rv = ERR_IO_PENDING; | |
463 } | |
464 } | |
465 return rv; | 532 return rv; |
466 } | 533 } |
467 | 534 |
468 int FakeBlockingStreamSocket::Write(IOBuffer* buf, | 535 int FakeBlockingStreamSocket::Write(IOBuffer* buf, |
469 int len, | 536 int len, |
470 const CompletionCallback& callback) { | 537 const CompletionCallback& callback) { |
471 DCHECK(buf); | 538 DCHECK(buf); |
472 DCHECK_LE(0, len); | 539 DCHECK_LE(0, len); |
473 | 540 |
474 if (!should_block_write_) | 541 if (!should_block_write_) |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
559 DCHECK(!write_loop_); | 626 DCHECK(!write_loop_); |
560 | 627 |
561 if (pending_write_buf_.get()) | 628 if (pending_write_buf_.get()) |
562 return; | 629 return; |
563 write_loop_.reset(new base::RunLoop); | 630 write_loop_.reset(new base::RunLoop); |
564 write_loop_->Run(); | 631 write_loop_->Run(); |
565 write_loop_.reset(); | 632 write_loop_.reset(); |
566 DCHECK(pending_write_buf_.get()); | 633 DCHECK(pending_write_buf_.get()); |
567 } | 634 } |
568 | 635 |
636 int FakeBlockingStreamSocket::ReadCommon(IOBuffer* buf, | |
637 int len, | |
638 const CompletionCallback& callback, | |
639 bool use_read_if_ready) { | |
640 DCHECK(!pending_read_buf_); | |
641 DCHECK(pending_read_callback_.is_null()); | |
642 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); | |
643 DCHECK(!callback.is_null()); | |
644 | |
645 int rv; | |
646 if (!use_read_if_ready) { | |
647 rv = transport_->Read(buf, len, | |
648 base::Bind(&FakeBlockingStreamSocket::OnReadCompleted, | |
649 base::Unretained(this))); | |
650 } else { | |
651 rv = transport_->ReadIfReady( | |
652 buf, len, base::Bind(&FakeBlockingStreamSocket::OnReadCompleted, | |
653 base::Unretained(this))); | |
davidben
2017/02/01 22:25:57
Hrm. This one's a little interesting. If ReadIfRea
xunjieli
2017/02/03 16:35:33
Ah, sorry I missed that. Good catch! How about we
| |
654 } | |
655 if (rv == ERR_IO_PENDING || should_block_read_) { | |
656 // Save the callback to be called later. | |
657 pending_read_buf_ = buf; | |
658 pending_read_buf_len_ = len; | |
659 pending_read_callback_ = callback; | |
660 // Save the read result. | |
661 if (rv != ERR_IO_PENDING) { | |
662 OnReadCompleted(rv); | |
663 rv = ERR_IO_PENDING; | |
664 } | |
665 } | |
666 return rv; | |
667 } | |
668 | |
569 void FakeBlockingStreamSocket::OnReadCompleted(int result) { | 669 void FakeBlockingStreamSocket::OnReadCompleted(int result) { |
570 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); | 670 DCHECK_EQ(ERR_IO_PENDING, pending_read_result_); |
571 DCHECK(!pending_read_callback_.is_null()); | 671 DCHECK(!pending_read_callback_.is_null()); |
572 | 672 |
573 pending_read_result_ = result; | 673 pending_read_result_ = result; |
574 | 674 |
575 if (should_block_read_) { | 675 if (should_block_read_) { |
576 // Defer the result until UnblockReadResult is called. | 676 // Defer the result until UnblockReadResult is called. |
577 if (read_loop_) | 677 if (read_loop_) |
578 read_loop_->Quit(); | 678 read_loop_->Quit(); |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
849 SSLClientSocketContext context_; | 949 SSLClientSocketContext context_; |
850 std::unique_ptr<SSLClientSocket> sock_; | 950 std::unique_ptr<SSLClientSocket> sock_; |
851 TestNetLog log_; | 951 TestNetLog log_; |
852 | 952 |
853 private: | 953 private: |
854 std::unique_ptr<SpawnedTestServer> spawned_test_server_; | 954 std::unique_ptr<SpawnedTestServer> spawned_test_server_; |
855 TestCompletionCallback callback_; | 955 TestCompletionCallback callback_; |
856 AddressList addr_; | 956 AddressList addr_; |
857 }; | 957 }; |
858 | 958 |
959 // If GetParam(), try ReadIfReady() and fall back to Read() if needed. | |
960 class SSLClientSocketReadTest : public SSLClientSocketTest, | |
961 public ::testing::WithParamInterface<bool> { | |
962 protected: | |
963 void SetUp() override { | |
964 if (GetParam()) { | |
965 field_trial_list_ = base::MakeUnique<base::FieldTrialList>( | |
966 base::MakeUnique<base::MockEntropyProvider>()); | |
967 base::FieldTrialList::CreateFieldTrial(Socket::kReadIfReadyTrialName, | |
968 "enable"); | |
969 } | |
970 } | |
971 | |
972 // Convienient wrapper to call Read() or ReadIfReady() depending on | |
973 // GetParam(). | |
974 int Read(StreamSocket* socket, | |
975 IOBuffer* buf, | |
976 int buf_len, | |
977 const CompletionCallback& callback) { | |
978 if (GetParam()) { | |
979 int rv = socket->ReadIfReady(buf, buf_len, callback); | |
980 if (rv != ERR_READ_IF_READY_NOT_IMPLEMENTED) | |
981 return rv; | |
davidben
2017/02/01 22:25:58
Since this is test code, do we want to have this c
xunjieli
2017/02/03 16:35:33
Done.
| |
982 } | |
983 return socket->Read(buf, buf_len, callback); | |
984 } | |
985 | |
986 // Calls Read()/ReadIfReady() and waits for it to return data. | |
987 int ReadAndWaitForCompletion(StreamSocket* socket, | |
988 IOBuffer* buf, | |
989 int buf_len, | |
990 TestCompletionCallback* callback) { | |
davidben
2017/02/01 22:25:58
Since you pass in and wait on callback in the same
xunjieli
2017/02/03 16:35:33
Done.
| |
991 if (GetParam()) { | |
992 int rv = socket->ReadIfReady(buf, buf_len, callback->callback()); | |
993 if (rv == ERR_IO_PENDING) { | |
994 rv = callback->GetResult(rv); | |
995 if (rv == OK) { | |
996 rv = socket->ReadIfReady(buf, buf_len, callback->callback()); | |
997 DCHECK_NE(ERR_IO_PENDING, rv); | |
998 } | |
999 } | |
1000 if (rv != ERR_READ_IF_READY_NOT_IMPLEMENTED) | |
davidben
2017/02/01 22:25:58
Ditto re ERR_READ_IF_READY_NOT_IMPLEMENTED.
xunjieli
2017/02/03 16:35:33
Done.
| |
1001 return rv; | |
1002 } | |
1003 return callback->GetResult( | |
1004 socket->Read(buf, buf_len, callback->callback())); | |
1005 } | |
1006 | |
1007 private: | |
1008 std::unique_ptr<base::FieldTrialList> field_trial_list_; | |
1009 }; | |
1010 | |
1011 INSTANTIATE_TEST_CASE_P(/* no prefix */, | |
1012 SSLClientSocketReadTest, | |
1013 ::testing::Bool()); | |
1014 | |
859 // Verifies the correctness of GetSSLCertRequestInfo. | 1015 // Verifies the correctness of GetSSLCertRequestInfo. |
860 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest { | 1016 class SSLClientSocketCertRequestInfoTest : public SSLClientSocketTest { |
861 protected: | 1017 protected: |
862 // Creates a test server with the given SSLOptions, connects to it and returns | 1018 // Creates a test server with the given SSLOptions, connects to it and returns |
863 // the SSLCertRequestInfo reported by the socket. | 1019 // the SSLCertRequestInfo reported by the socket. |
864 scoped_refptr<SSLCertRequestInfo> GetCertRequest( | 1020 scoped_refptr<SSLCertRequestInfo> GetCertRequest( |
865 SpawnedTestServer::SSLOptions ssl_options) { | 1021 SpawnedTestServer::SSLOptions ssl_options) { |
866 SpawnedTestServer spawned_test_server(SpawnedTestServer::TYPE_HTTPS, | 1022 SpawnedTestServer spawned_test_server(SpawnedTestServer::TYPE_HTTPS, |
867 ssl_options, base::FilePath()); | 1023 ssl_options, base::FilePath()); |
868 if (!spawned_test_server.Start()) | 1024 if (!spawned_test_server.Start()) |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1193 EXPECT_FALSE(sock_->IsConnected()); | 1349 EXPECT_FALSE(sock_->IsConnected()); |
1194 } | 1350 } |
1195 | 1351 |
1196 // TODO(wtc): Add unit tests for IsConnectedAndIdle: | 1352 // TODO(wtc): Add unit tests for IsConnectedAndIdle: |
1197 // - Server closes an SSL connection (with a close_notify alert message). | 1353 // - Server closes an SSL connection (with a close_notify alert message). |
1198 // - Server closes the underlying TCP connection directly. | 1354 // - Server closes the underlying TCP connection directly. |
1199 // - Server sends data unexpectedly. | 1355 // - Server sends data unexpectedly. |
1200 | 1356 |
1201 // Tests that the socket can be read from successfully. Also test that a peer's | 1357 // Tests that the socket can be read from successfully. Also test that a peer's |
1202 // close_notify alert is successfully processed without error. | 1358 // close_notify alert is successfully processed without error. |
1203 TEST_F(SSLClientSocketTest, Read) { | 1359 TEST_P(SSLClientSocketReadTest, Read) { |
1204 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1360 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1205 | 1361 |
1206 TestCompletionCallback callback; | 1362 TestCompletionCallback callback; |
1207 std::unique_ptr<StreamSocket> transport( | 1363 std::unique_ptr<StreamSocket> transport( |
1208 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1364 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1209 EXPECT_EQ(0, transport->GetTotalReceivedBytes()); | 1365 EXPECT_EQ(0, transport->GetTotalReceivedBytes()); |
1210 | 1366 |
1211 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1367 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1212 EXPECT_THAT(rv, IsOk()); | 1368 EXPECT_THAT(rv, IsOk()); |
1213 | 1369 |
(...skipping 15 matching lines...) Expand all Loading... | |
1229 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); | 1385 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
1230 | 1386 |
1231 rv = callback.GetResult(sock->Write( | 1387 rv = callback.GetResult(sock->Write( |
1232 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); | 1388 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); |
1233 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); | 1389 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
1234 | 1390 |
1235 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1391 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1236 int64_t unencrypted_bytes_read = 0; | 1392 int64_t unencrypted_bytes_read = 0; |
1237 int64_t network_bytes_read_during_handshake = sock->GetTotalReceivedBytes(); | 1393 int64_t network_bytes_read_during_handshake = sock->GetTotalReceivedBytes(); |
1238 do { | 1394 do { |
1239 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | 1395 TestCompletionCallback read_callback; |
1396 rv = ReadAndWaitForCompletion(sock.get(), buf.get(), 4096, &read_callback); | |
1240 EXPECT_GE(rv, 0); | 1397 EXPECT_GE(rv, 0); |
1241 if (rv >= 0) { | 1398 if (rv >= 0) { |
1242 unencrypted_bytes_read += rv; | 1399 unencrypted_bytes_read += rv; |
1243 } | 1400 } |
1244 } while (rv > 0); | 1401 } while (rv > 0); |
1245 EXPECT_GT(unencrypted_bytes_read, 0); | 1402 EXPECT_GT(unencrypted_bytes_read, 0); |
1246 // Reading the payload should increase the number of bytes on network layer. | 1403 // Reading the payload should increase the number of bytes on network layer. |
1247 EXPECT_GT(sock->GetTotalReceivedBytes(), network_bytes_read_during_handshake); | 1404 EXPECT_GT(sock->GetTotalReceivedBytes(), network_bytes_read_during_handshake); |
1248 // Number of bytes received on the network after the handshake should be | 1405 // Number of bytes received on the network after the handshake should be |
1249 // higher than the number of encrypted bytes read. | 1406 // higher than the number of encrypted bytes read. |
(...skipping 30 matching lines...) Expand all Loading... | |
1280 | 1437 |
1281 rv = callback.GetResult(sock->Connect(callback.callback())); | 1438 rv = callback.GetResult(sock->Connect(callback.callback())); |
1282 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1439 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
1283 EXPECT_FALSE(sock->IsConnected()); | 1440 EXPECT_FALSE(sock->IsConnected()); |
1284 } | 1441 } |
1285 | 1442 |
1286 // Tests that the SSLClientSocket properly handles when the underlying transport | 1443 // Tests that the SSLClientSocket properly handles when the underlying transport |
1287 // synchronously returns an error code - such as if an intermediary terminates | 1444 // synchronously returns an error code - such as if an intermediary terminates |
1288 // the socket connection uncleanly. | 1445 // the socket connection uncleanly. |
1289 // This is a regression test for http://crbug.com/238536 | 1446 // This is a regression test for http://crbug.com/238536 |
1290 TEST_F(SSLClientSocketTest, Read_WithSynchronousError) { | 1447 TEST_P(SSLClientSocketReadTest, Read_WithSynchronousError) { |
1291 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1448 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1292 | 1449 |
1293 TestCompletionCallback callback; | 1450 TestCompletionCallback callback; |
1294 std::unique_ptr<StreamSocket> real_transport( | 1451 std::unique_ptr<StreamSocket> real_transport( |
1295 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1452 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1296 std::unique_ptr<SynchronousErrorStreamSocket> transport( | 1453 std::unique_ptr<SynchronousErrorStreamSocket> transport( |
1297 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1454 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1298 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1455 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1299 EXPECT_THAT(rv, IsOk()); | 1456 EXPECT_THAT(rv, IsOk()); |
1300 | 1457 |
(...skipping 21 matching lines...) Expand all Loading... | |
1322 EXPECT_EQ(kRequestTextSize, rv); | 1479 EXPECT_EQ(kRequestTextSize, rv); |
1323 | 1480 |
1324 // Simulate an unclean/forcible shutdown. | 1481 // Simulate an unclean/forcible shutdown. |
1325 raw_transport->SetNextReadError(ERR_CONNECTION_RESET); | 1482 raw_transport->SetNextReadError(ERR_CONNECTION_RESET); |
1326 | 1483 |
1327 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1484 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1328 | 1485 |
1329 // Note: This test will hang if this bug has regressed. Simply checking that | 1486 // Note: This test will hang if this bug has regressed. Simply checking that |
1330 // rv != ERR_IO_PENDING is insufficient, as ERR_IO_PENDING is a legitimate | 1487 // rv != ERR_IO_PENDING is insufficient, as ERR_IO_PENDING is a legitimate |
1331 // result when using a dedicated task runner for NSS. | 1488 // result when using a dedicated task runner for NSS. |
1332 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | 1489 rv = Read(sock.get(), buf.get(), 4096, callback.callback()); |
1333 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1490 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
1334 } | 1491 } |
1335 | 1492 |
1336 // Tests that the SSLClientSocket properly handles when the underlying transport | 1493 // Tests that the SSLClientSocket properly handles when the underlying transport |
1337 // asynchronously returns an error code while writing data - such as if an | 1494 // asynchronously returns an error code while writing data - such as if an |
1338 // intermediary terminates the socket connection uncleanly. | 1495 // intermediary terminates the socket connection uncleanly. |
1339 // This is a regression test for http://crbug.com/249848 | 1496 // This is a regression test for http://crbug.com/249848 |
1340 TEST_F(SSLClientSocketTest, Write_WithSynchronousError) { | 1497 TEST_F(SSLClientSocketTest, Write_WithSynchronousError) { |
1341 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1498 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1342 | 1499 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1457 int old_write_count = raw_counting_socket->write_count(); | 1614 int old_write_count = raw_counting_socket->write_count(); |
1458 base::RunLoop loop; | 1615 base::RunLoop loop; |
1459 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 1616 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
1460 FROM_HERE, loop.QuitClosure(), base::TimeDelta::FromMilliseconds(100)); | 1617 FROM_HERE, loop.QuitClosure(), base::TimeDelta::FromMilliseconds(100)); |
1461 loop.Run(); | 1618 loop.Run(); |
1462 EXPECT_EQ(old_write_count, raw_counting_socket->write_count()); | 1619 EXPECT_EQ(old_write_count, raw_counting_socket->write_count()); |
1463 } | 1620 } |
1464 | 1621 |
1465 // Test the full duplex mode, with Read and Write pending at the same time. | 1622 // Test the full duplex mode, with Read and Write pending at the same time. |
1466 // This test also serves as a regression test for http://crbug.com/29815. | 1623 // This test also serves as a regression test for http://crbug.com/29815. |
1467 TEST_F(SSLClientSocketTest, Read_FullDuplex) { | 1624 TEST_P(SSLClientSocketReadTest, Read_FullDuplex) { |
1468 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1625 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1469 | 1626 |
1470 int rv; | 1627 int rv; |
1471 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 1628 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
1472 EXPECT_THAT(rv, IsOk()); | 1629 EXPECT_THAT(rv, IsOk()); |
1473 | 1630 |
1474 // Issue a "hanging" Read first. | 1631 // Issue a "hanging" Read first. |
1475 TestCompletionCallback callback; | 1632 TestCompletionCallback callback; |
1476 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1633 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1477 rv = sock_->Read(buf.get(), 4096, callback.callback()); | 1634 rv = Read(sock_.get(), buf.get(), 4096, callback.callback()); |
1478 // We haven't written the request, so there should be no response yet. | 1635 // We haven't written the request, so there should be no response yet. |
1479 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); | 1636 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
1480 | 1637 |
1481 // Write the request. | 1638 // Write the request. |
1482 // The request is padded with a User-Agent header to a size that causes the | 1639 // The request is padded with a User-Agent header to a size that causes the |
1483 // memio circular buffer (4k bytes) in SSLClientSocketNSS to wrap around. | 1640 // memio circular buffer (4k bytes) in SSLClientSocketNSS to wrap around. |
1484 // This tests the fix for http://crbug.com/29815. | 1641 // This tests the fix for http://crbug.com/29815. |
1485 std::string request_text = "GET / HTTP/1.1\r\nUser-Agent: long browser name "; | 1642 std::string request_text = "GET / HTTP/1.1\r\nUser-Agent: long browser name "; |
1486 for (int i = 0; i < 3770; ++i) | 1643 for (int i = 0; i < 3770; ++i) |
1487 request_text.push_back('*'); | 1644 request_text.push_back('*'); |
1488 request_text.append("\r\n\r\n"); | 1645 request_text.append("\r\n\r\n"); |
1489 scoped_refptr<IOBuffer> request_buffer(new StringIOBuffer(request_text)); | 1646 scoped_refptr<IOBuffer> request_buffer(new StringIOBuffer(request_text)); |
1490 | 1647 |
1491 TestCompletionCallback callback2; // Used for Write only. | 1648 TestCompletionCallback callback2; // Used for Write only. |
1492 rv = callback2.GetResult(sock_->Write( | 1649 rv = callback2.GetResult(sock_->Write( |
1493 request_buffer.get(), request_text.size(), callback2.callback())); | 1650 request_buffer.get(), request_text.size(), callback2.callback())); |
1494 EXPECT_EQ(static_cast<int>(request_text.size()), rv); | 1651 EXPECT_EQ(static_cast<int>(request_text.size()), rv); |
1495 | 1652 |
1496 // Now get the Read result. | 1653 // Now get the Read result. |
1497 rv = callback.WaitForResult(); | 1654 rv = callback.WaitForResult(); |
1655 if (GetParam()) { | |
1656 EXPECT_THAT(rv, IsOk()); | |
1657 rv = Read(sock_.get(), buf.get(), 4096, callback.callback()); | |
1658 } | |
1498 EXPECT_GT(rv, 0); | 1659 EXPECT_GT(rv, 0); |
1499 } | 1660 } |
1500 | 1661 |
1501 // Attempts to Read() and Write() from an SSLClientSocketNSS in full duplex | 1662 // Attempts to Read() and Write() from an SSLClientSocketNSS in full duplex |
1502 // mode when the underlying transport is blocked on sending data. When the | 1663 // mode when the underlying transport is blocked on sending data. When the |
1503 // underlying transport completes due to an error, it should invoke both the | 1664 // underlying transport completes due to an error, it should invoke both the |
1504 // Read() and Write() callbacks. If the socket is deleted by the Read() | 1665 // Read() and Write() callbacks. If the socket is deleted by the Read() |
1505 // callback, the Write() callback should not be invoked. | 1666 // callback, the Write() callback should not be invoked. |
1506 // Regression test for http://crbug.com/232633 | 1667 // Regression test for http://crbug.com/232633 |
1507 TEST_F(SSLClientSocketTest, Read_DeleteWhilePendingFullDuplex) { | 1668 TEST_P(SSLClientSocketReadTest, Read_DeleteWhilePendingFullDuplex) { |
1508 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1669 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1509 | 1670 |
1510 TestCompletionCallback callback; | 1671 TestCompletionCallback callback; |
1511 std::unique_ptr<StreamSocket> real_transport( | 1672 std::unique_ptr<StreamSocket> real_transport( |
1512 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1673 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1513 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer | 1674 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer |
1514 // is retained in order to configure additional errors. | 1675 // is retained in order to configure additional errors. |
1515 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( | 1676 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( |
1516 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1677 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1517 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); | 1678 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); |
(...skipping 28 matching lines...) Expand all Loading... | |
1546 // ... but have those errors returned asynchronously. Because the Write() will | 1707 // ... but have those errors returned asynchronously. Because the Write() will |
1547 // return first, this will trigger the error. | 1708 // return first, this will trigger the error. |
1548 raw_transport->BlockReadResult(); | 1709 raw_transport->BlockReadResult(); |
1549 raw_transport->BlockWrite(); | 1710 raw_transport->BlockWrite(); |
1550 | 1711 |
1551 // Enqueue a Read() before calling Write(), which should "hang" due to | 1712 // Enqueue a Read() before calling Write(), which should "hang" due to |
1552 // the ERR_IO_PENDING caused by SetReadShouldBlock() and thus return. | 1713 // the ERR_IO_PENDING caused by SetReadShouldBlock() and thus return. |
1553 SSLClientSocket* raw_sock = sock.get(); | 1714 SSLClientSocket* raw_sock = sock.get(); |
1554 DeleteSocketCallback read_callback(sock.release()); | 1715 DeleteSocketCallback read_callback(sock.release()); |
1555 scoped_refptr<IOBuffer> read_buf(new IOBuffer(4096)); | 1716 scoped_refptr<IOBuffer> read_buf(new IOBuffer(4096)); |
1556 rv = raw_sock->Read(read_buf.get(), 4096, read_callback.callback()); | 1717 rv = Read(raw_sock, read_buf.get(), 4096, read_callback.callback()); |
1557 | 1718 |
1558 // Ensure things didn't complete synchronously, otherwise |sock| is invalid. | 1719 // Ensure things didn't complete synchronously, otherwise |sock| is invalid. |
1559 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); | 1720 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
1560 ASSERT_FALSE(read_callback.have_result()); | 1721 ASSERT_FALSE(read_callback.have_result()); |
1561 | 1722 |
1562 // Attempt to write the remaining data. OpenSSL will return that its blocked | 1723 // Attempt to write the remaining data. OpenSSL will return that its blocked |
1563 // because the underlying transport is blocked. | 1724 // because the underlying transport is blocked. |
1564 rv = raw_sock->Write(request_buffer.get(), | 1725 rv = raw_sock->Write(request_buffer.get(), |
1565 request_buffer->BytesRemaining(), | 1726 request_buffer->BytesRemaining(), |
1566 callback.callback()); | 1727 callback.callback()); |
1567 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); | 1728 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
1568 ASSERT_FALSE(callback.have_result()); | 1729 ASSERT_FALSE(callback.have_result()); |
1569 | 1730 |
1570 // Now unblock Write(), which will invoke OnSendComplete and (eventually) | 1731 // Now unblock Write(), which will invoke OnSendComplete and (eventually) |
1571 // call the Read() callback, deleting the socket and thus aborting calling | 1732 // call the Read() callback, deleting the socket and thus aborting calling |
1572 // the Write() callback. | 1733 // the Write() callback. |
1573 raw_transport->UnblockWrite(); | 1734 raw_transport->UnblockWrite(); |
1574 | 1735 |
1575 rv = read_callback.WaitForResult(); | 1736 rv = read_callback.WaitForResult(); |
1576 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1737 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
1577 | 1738 |
1578 // The Write callback should not have been called. | 1739 // The Write callback should not have been called. |
1579 EXPECT_FALSE(callback.have_result()); | 1740 EXPECT_FALSE(callback.have_result()); |
1580 } | 1741 } |
1581 | 1742 |
1582 // 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 |
1583 // 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 |
1584 // error in a SPDY socket. | 1745 // error in a SPDY socket. |
1585 // Regression test for http://crbug.com/335557 | 1746 // Regression test for http://crbug.com/335557 |
1586 TEST_F(SSLClientSocketTest, Read_WithWriteError) { | 1747 TEST_P(SSLClientSocketReadTest, Read_WithWriteError) { |
1587 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1748 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1588 | 1749 |
1589 TestCompletionCallback callback; | 1750 TestCompletionCallback callback; |
1590 std::unique_ptr<StreamSocket> real_transport( | 1751 std::unique_ptr<StreamSocket> real_transport( |
1591 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1752 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1592 // 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 |
1593 // is retained in order to configure additional errors. | 1754 // is retained in order to configure additional errors. |
1594 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( | 1755 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( |
1595 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1756 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1596 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); | 1757 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); |
(...skipping 24 matching lines...) Expand all Loading... | |
1621 memcpy(request_buffer->data(), request_text, kRequestTextSize); | 1782 memcpy(request_buffer->data(), request_text, kRequestTextSize); |
1622 | 1783 |
1623 rv = callback.GetResult( | 1784 rv = callback.GetResult( |
1624 sock->Write(request_buffer.get(), kRequestTextSize, callback.callback())); | 1785 sock->Write(request_buffer.get(), kRequestTextSize, callback.callback())); |
1625 EXPECT_EQ(kRequestTextSize, rv); | 1786 EXPECT_EQ(kRequestTextSize, rv); |
1626 | 1787 |
1627 // Start a hanging read. | 1788 // Start a hanging read. |
1628 TestCompletionCallback read_callback; | 1789 TestCompletionCallback read_callback; |
1629 raw_transport->BlockReadResult(); | 1790 raw_transport->BlockReadResult(); |
1630 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1791 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1631 rv = sock->Read(buf.get(), 4096, read_callback.callback()); | 1792 rv = Read(sock.get(), buf.get(), 4096, read_callback.callback()); |
1632 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 1793 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
1633 | 1794 |
1634 // 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 |
1635 // internal socket buffers so that the request hits the underlying transport | 1796 // internal socket buffers so that the request hits the underlying transport |
1636 // socket and detects the error. | 1797 // socket and detects the error. |
1637 std::string long_request_text = | 1798 std::string long_request_text = |
1638 "GET / HTTP/1.1\r\nUser-Agent: long browser name "; | 1799 "GET / HTTP/1.1\r\nUser-Agent: long browser name "; |
1639 long_request_text.append(20 * 1024, '*'); | 1800 long_request_text.append(20 * 1024, '*'); |
1640 long_request_text.append("\r\n\r\n"); | 1801 long_request_text.append("\r\n\r\n"); |
1641 scoped_refptr<DrainableIOBuffer> long_request_buffer(new DrainableIOBuffer( | 1802 scoped_refptr<DrainableIOBuffer> long_request_buffer(new DrainableIOBuffer( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1689 raw_transport->SetNextReadError(0); | 1850 raw_transport->SetNextReadError(0); |
1690 | 1851 |
1691 rv = callback.GetResult(sock->Connect(callback.callback())); | 1852 rv = callback.GetResult(sock->Connect(callback.callback())); |
1692 EXPECT_THAT(rv, IsError(ERR_CONNECTION_CLOSED)); | 1853 EXPECT_THAT(rv, IsError(ERR_CONNECTION_CLOSED)); |
1693 EXPECT_FALSE(sock->IsConnected()); | 1854 EXPECT_FALSE(sock->IsConnected()); |
1694 } | 1855 } |
1695 | 1856 |
1696 // 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 |
1697 // is cleanly closed, but the peer does not send close_notify. | 1858 // is cleanly closed, but the peer does not send close_notify. |
1698 // This is a regression test for https://crbug.com/422246 | 1859 // This is a regression test for https://crbug.com/422246 |
1699 TEST_F(SSLClientSocketTest, Read_WithZeroReturn) { | 1860 TEST_P(SSLClientSocketReadTest, Read_WithZeroReturn) { |
1700 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1861 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1701 | 1862 |
1702 TestCompletionCallback callback; | 1863 TestCompletionCallback callback; |
1703 std::unique_ptr<StreamSocket> real_transport( | 1864 std::unique_ptr<StreamSocket> real_transport( |
1704 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1865 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1705 std::unique_ptr<SynchronousErrorStreamSocket> transport( | 1866 std::unique_ptr<SynchronousErrorStreamSocket> transport( |
1706 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1867 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1707 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1868 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1708 EXPECT_THAT(rv, IsOk()); | 1869 EXPECT_THAT(rv, IsOk()); |
1709 | 1870 |
1710 // Disable TLS False Start to ensure the handshake has completed. | 1871 // Disable TLS False Start to ensure the handshake has completed. |
1711 SSLConfig ssl_config; | 1872 SSLConfig ssl_config; |
1712 ssl_config.false_start_enabled = false; | 1873 ssl_config.false_start_enabled = false; |
1713 | 1874 |
1714 SynchronousErrorStreamSocket* raw_transport = transport.get(); | 1875 SynchronousErrorStreamSocket* raw_transport = transport.get(); |
1715 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( | 1876 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
1716 std::move(transport), spawned_test_server()->host_port_pair(), | 1877 std::move(transport), spawned_test_server()->host_port_pair(), |
1717 ssl_config)); | 1878 ssl_config)); |
1718 | 1879 |
1719 rv = callback.GetResult(sock->Connect(callback.callback())); | 1880 rv = callback.GetResult(sock->Connect(callback.callback())); |
1720 EXPECT_THAT(rv, IsOk()); | 1881 EXPECT_THAT(rv, IsOk()); |
1721 EXPECT_TRUE(sock->IsConnected()); | 1882 EXPECT_TRUE(sock->IsConnected()); |
1722 | 1883 |
1723 raw_transport->SetNextReadError(0); | 1884 raw_transport->SetNextReadError(0); |
1724 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1885 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1725 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | 1886 rv = ReadAndWaitForCompletion(sock.get(), buf.get(), 4096, &callback); |
1726 EXPECT_EQ(0, rv); | 1887 EXPECT_EQ(0, rv); |
1727 } | 1888 } |
1728 | 1889 |
1729 // 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 |
1730 // underlying socket is cleanly closed asynchronously. | 1891 // underlying socket is cleanly closed asynchronously. |
1731 // This is a regression test for https://crbug.com/422246 | 1892 // This is a regression test for https://crbug.com/422246 |
1732 TEST_F(SSLClientSocketTest, Read_WithAsyncZeroReturn) { | 1893 TEST_P(SSLClientSocketReadTest, Read_WithAsyncZeroReturn) { |
1733 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1894 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1734 | 1895 |
1735 TestCompletionCallback callback; | 1896 TestCompletionCallback callback; |
1736 std::unique_ptr<StreamSocket> real_transport( | 1897 std::unique_ptr<StreamSocket> real_transport( |
1737 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1898 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1738 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( | 1899 std::unique_ptr<SynchronousErrorStreamSocket> error_socket( |
1739 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1900 new SynchronousErrorStreamSocket(std::move(real_transport))); |
1740 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); | 1901 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); |
1741 std::unique_ptr<FakeBlockingStreamSocket> transport( | 1902 std::unique_ptr<FakeBlockingStreamSocket> transport( |
1742 new FakeBlockingStreamSocket(std::move(error_socket))); | 1903 new FakeBlockingStreamSocket(std::move(error_socket))); |
1743 FakeBlockingStreamSocket* raw_transport = transport.get(); | 1904 FakeBlockingStreamSocket* raw_transport = transport.get(); |
1744 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1905 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1745 EXPECT_THAT(rv, IsOk()); | 1906 EXPECT_THAT(rv, IsOk()); |
1746 | 1907 |
1747 // Disable TLS False Start to ensure the handshake has completed. | 1908 // Disable TLS False Start to ensure the handshake has completed. |
1748 SSLConfig ssl_config; | 1909 SSLConfig ssl_config; |
1749 ssl_config.false_start_enabled = false; | 1910 ssl_config.false_start_enabled = false; |
1750 | 1911 |
1751 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( | 1912 std::unique_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
1752 std::move(transport), spawned_test_server()->host_port_pair(), | 1913 std::move(transport), spawned_test_server()->host_port_pair(), |
1753 ssl_config)); | 1914 ssl_config)); |
1754 | 1915 |
1755 rv = callback.GetResult(sock->Connect(callback.callback())); | 1916 rv = callback.GetResult(sock->Connect(callback.callback())); |
1756 EXPECT_THAT(rv, IsOk()); | 1917 EXPECT_THAT(rv, IsOk()); |
1757 EXPECT_TRUE(sock->IsConnected()); | 1918 EXPECT_TRUE(sock->IsConnected()); |
1758 | 1919 |
1759 raw_error_socket->SetNextReadError(0); | 1920 raw_error_socket->SetNextReadError(0); |
1760 raw_transport->BlockReadResult(); | 1921 raw_transport->BlockReadResult(); |
1761 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1922 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1762 rv = sock->Read(buf.get(), 4096, callback.callback()); | 1923 TestCompletionCallback read_callback; |
1924 rv = Read(sock.get(), buf.get(), 4096, read_callback.callback()); | |
1763 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 1925 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
1764 | 1926 |
1765 raw_transport->UnblockReadResult(); | 1927 raw_transport->UnblockReadResult(); |
1766 rv = callback.GetResult(rv); | 1928 rv = read_callback.GetResult(rv); |
1929 if (GetParam()) { | |
1930 EXPECT_EQ(OK, rv); | |
1931 rv = Read(sock.get(), buf.get(), 4096, read_callback.callback()); | |
1932 } | |
1767 EXPECT_EQ(0, rv); | 1933 EXPECT_EQ(0, rv); |
1768 } | 1934 } |
1769 | 1935 |
1770 // Tests that fatal alerts from the peer are processed. This is a regression | 1936 // Tests that fatal alerts from the peer are processed. This is a regression |
1771 // test for https://crbug.com/466303. | 1937 // test for https://crbug.com/466303. |
1772 TEST_F(SSLClientSocketTest, Read_WithFatalAlert) { | 1938 TEST_P(SSLClientSocketReadTest, Read_WithFatalAlert) { |
1773 SpawnedTestServer::SSLOptions ssl_options; | 1939 SpawnedTestServer::SSLOptions ssl_options; |
1774 ssl_options.alert_after_handshake = true; | 1940 ssl_options.alert_after_handshake = true; |
1775 ASSERT_TRUE(StartTestServer(ssl_options)); | 1941 ASSERT_TRUE(StartTestServer(ssl_options)); |
1776 | 1942 |
1777 int rv; | 1943 int rv; |
1778 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 1944 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
1779 EXPECT_THAT(rv, IsOk()); | 1945 EXPECT_THAT(rv, IsOk()); |
1780 | 1946 |
1781 // Receive the fatal alert. | 1947 // Receive the fatal alert. |
1782 TestCompletionCallback callback; | 1948 TestCompletionCallback callback; |
1783 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1949 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1784 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, callback.GetResult(sock_->Read( | 1950 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, |
1785 buf.get(), 4096, callback.callback()))); | 1951 ReadAndWaitForCompletion(sock_.get(), buf.get(), 4096, &callback)); |
1786 } | 1952 } |
1787 | 1953 |
1788 TEST_F(SSLClientSocketTest, Read_SmallChunks) { | 1954 TEST_P(SSLClientSocketReadTest, Read_SmallChunks) { |
1789 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1955 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1790 | 1956 |
1791 int rv; | 1957 int rv; |
1792 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 1958 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
1793 EXPECT_THAT(rv, IsOk()); | 1959 EXPECT_THAT(rv, IsOk()); |
1794 | 1960 |
1795 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 1961 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
1796 scoped_refptr<IOBuffer> request_buffer( | 1962 scoped_refptr<IOBuffer> request_buffer( |
1797 new IOBuffer(arraysize(request_text) - 1)); | 1963 new IOBuffer(arraysize(request_text) - 1)); |
1798 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); | 1964 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
1799 | 1965 |
1800 TestCompletionCallback callback; | 1966 TestCompletionCallback callback; |
1801 rv = callback.GetResult(sock_->Write( | 1967 rv = callback.GetResult(sock_->Write( |
1802 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); | 1968 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); |
1803 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); | 1969 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
1804 | 1970 |
1805 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); | 1971 scoped_refptr<IOBuffer> buf(new IOBuffer(1)); |
1806 do { | 1972 do { |
1807 rv = callback.GetResult(sock_->Read(buf.get(), 1, callback.callback())); | 1973 rv = ReadAndWaitForCompletion(sock_.get(), buf.get(), 1, &callback); |
1808 EXPECT_GE(rv, 0); | 1974 EXPECT_GE(rv, 0); |
1809 } while (rv > 0); | 1975 } while (rv > 0); |
1810 } | 1976 } |
1811 | 1977 |
1812 TEST_F(SSLClientSocketTest, Read_ManySmallRecords) { | 1978 TEST_P(SSLClientSocketReadTest, Read_ManySmallRecords) { |
1813 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1979 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1814 | 1980 |
1815 TestCompletionCallback callback; | 1981 TestCompletionCallback callback; |
1816 | 1982 |
1817 std::unique_ptr<StreamSocket> real_transport( | 1983 std::unique_ptr<StreamSocket> real_transport( |
1818 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1984 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
1819 std::unique_ptr<ReadBufferingStreamSocket> transport( | 1985 std::unique_ptr<ReadBufferingStreamSocket> transport( |
1820 new ReadBufferingStreamSocket(std::move(real_transport))); | 1986 new ReadBufferingStreamSocket(std::move(real_transport))); |
1821 ReadBufferingStreamSocket* raw_transport = transport.get(); | 1987 ReadBufferingStreamSocket* raw_transport = transport.get(); |
1822 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1988 int rv = callback.GetResult(transport->Connect(callback.callback())); |
(...skipping 22 matching lines...) Expand all Loading... | |
1845 // of SSL data is buffered first. The 15K of buffered data is made up of | 2011 // of SSL data is buffered first. The 15K of buffered data is made up of |
1846 // many smaller SSL records (the TestServer writes along 1350 byte | 2012 // many smaller SSL records (the TestServer writes along 1350 byte |
1847 // plaintext boundaries), although there may also be a few records that are | 2013 // plaintext boundaries), although there may also be a few records that are |
1848 // smaller or larger, due to timing and SSL False Start. | 2014 // smaller or larger, due to timing and SSL False Start. |
1849 // 15K was chosen because 15K is smaller than the 17K (max) read issued by | 2015 // 15K was chosen because 15K is smaller than the 17K (max) read issued by |
1850 // the SSLClientSocket implementation, and larger than the minimum amount | 2016 // the SSLClientSocket implementation, and larger than the minimum amount |
1851 // of ciphertext necessary to contain the 8K of plaintext requested below. | 2017 // of ciphertext necessary to contain the 8K of plaintext requested below. |
1852 raw_transport->SetBufferSize(15000); | 2018 raw_transport->SetBufferSize(15000); |
1853 | 2019 |
1854 scoped_refptr<IOBuffer> buffer(new IOBuffer(8192)); | 2020 scoped_refptr<IOBuffer> buffer(new IOBuffer(8192)); |
1855 rv = callback.GetResult(sock->Read(buffer.get(), 8192, callback.callback())); | 2021 rv = ReadAndWaitForCompletion(sock.get(), buffer.get(), 8192, &callback); |
1856 ASSERT_EQ(rv, 8192); | 2022 ASSERT_EQ(rv, 8192); |
1857 } | 2023 } |
1858 | 2024 |
1859 TEST_F(SSLClientSocketTest, Read_Interrupted) { | 2025 TEST_P(SSLClientSocketReadTest, Read_Interrupted) { |
1860 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 2026 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1861 | 2027 |
1862 int rv; | 2028 int rv; |
1863 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 2029 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
1864 EXPECT_THAT(rv, IsOk()); | 2030 EXPECT_THAT(rv, IsOk()); |
1865 | 2031 |
1866 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 2032 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
1867 scoped_refptr<IOBuffer> request_buffer( | 2033 scoped_refptr<IOBuffer> request_buffer( |
1868 new IOBuffer(arraysize(request_text) - 1)); | 2034 new IOBuffer(arraysize(request_text) - 1)); |
1869 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); | 2035 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
1870 | 2036 |
1871 TestCompletionCallback callback; | 2037 TestCompletionCallback callback; |
1872 rv = callback.GetResult(sock_->Write( | 2038 rv = callback.GetResult(sock_->Write( |
1873 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); | 2039 request_buffer.get(), arraysize(request_text) - 1, callback.callback())); |
1874 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); | 2040 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
1875 | 2041 |
1876 // Do a partial read and then exit. This test should not crash! | 2042 // Do a partial read and then exit. This test should not crash! |
1877 scoped_refptr<IOBuffer> buf(new IOBuffer(512)); | 2043 scoped_refptr<IOBuffer> buf(new IOBuffer(512)); |
1878 rv = callback.GetResult(sock_->Read(buf.get(), 512, callback.callback())); | 2044 rv = ReadAndWaitForCompletion(sock_.get(), buf.get(), 512, &callback); |
1879 EXPECT_GT(rv, 0); | 2045 EXPECT_GT(rv, 0); |
1880 } | 2046 } |
1881 | 2047 |
1882 TEST_F(SSLClientSocketTest, Read_FullLogging) { | 2048 TEST_P(SSLClientSocketReadTest, Read_FullLogging) { |
1883 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 2049 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
1884 | 2050 |
1885 TestCompletionCallback callback; | 2051 TestCompletionCallback callback; |
1886 TestNetLog log; | 2052 TestNetLog log; |
1887 log.SetCaptureMode(NetLogCaptureMode::IncludeSocketBytes()); | 2053 log.SetCaptureMode(NetLogCaptureMode::IncludeSocketBytes()); |
1888 std::unique_ptr<StreamSocket> transport( | 2054 std::unique_ptr<StreamSocket> transport( |
1889 new TCPClientSocket(addr(), NULL, &log, NetLogSource())); | 2055 new TCPClientSocket(addr(), NULL, &log, NetLogSource())); |
1890 int rv = callback.GetResult(transport->Connect(callback.callback())); | 2056 int rv = callback.GetResult(transport->Connect(callback.callback())); |
1891 EXPECT_THAT(rv, IsOk()); | 2057 EXPECT_THAT(rv, IsOk()); |
1892 | 2058 |
(...skipping 15 matching lines...) Expand all Loading... | |
1908 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); | 2074 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
1909 | 2075 |
1910 TestNetLogEntry::List entries; | 2076 TestNetLogEntry::List entries; |
1911 log.GetEntries(&entries); | 2077 log.GetEntries(&entries); |
1912 size_t last_index = ExpectLogContainsSomewhereAfter( | 2078 size_t last_index = ExpectLogContainsSomewhereAfter( |
1913 entries, 5, NetLogEventType::SSL_SOCKET_BYTES_SENT, | 2079 entries, 5, NetLogEventType::SSL_SOCKET_BYTES_SENT, |
1914 NetLogEventPhase::NONE); | 2080 NetLogEventPhase::NONE); |
1915 | 2081 |
1916 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 2082 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1917 for (;;) { | 2083 for (;;) { |
1918 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | 2084 rv = ReadAndWaitForCompletion(sock.get(), buf.get(), 4096, &callback); |
1919 EXPECT_GE(rv, 0); | 2085 EXPECT_GE(rv, 0); |
1920 if (rv <= 0) | 2086 if (rv <= 0) |
1921 break; | 2087 break; |
1922 | 2088 |
1923 log.GetEntries(&entries); | 2089 log.GetEntries(&entries); |
1924 last_index = ExpectLogContainsSomewhereAfter( | 2090 last_index = ExpectLogContainsSomewhereAfter( |
1925 entries, last_index + 1, NetLogEventType::SSL_SOCKET_BYTES_RECEIVED, | 2091 entries, last_index + 1, NetLogEventType::SSL_SOCKET_BYTES_RECEIVED, |
1926 NetLogEventPhase::NONE); | 2092 NetLogEventPhase::NONE); |
1927 } | 2093 } |
1928 } | 2094 } |
(...skipping 1652 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3581 raw_transport->ReplaceReadResult( | 3747 raw_transport->ReplaceReadResult( |
3582 FormatTLS12Alert(49 /* AlertDescription.access_denied */)); | 3748 FormatTLS12Alert(49 /* AlertDescription.access_denied */)); |
3583 raw_transport->UnblockReadResult(); | 3749 raw_transport->UnblockReadResult(); |
3584 | 3750 |
3585 rv = callback.GetResult(rv); | 3751 rv = callback.GetResult(rv); |
3586 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT)); | 3752 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT)); |
3587 } | 3753 } |
3588 | 3754 |
3589 // Basic test for dumping memory stats. | 3755 // Basic test for dumping memory stats. |
3590 TEST_F(SSLClientSocketTest, DumpMemoryStats) { | 3756 TEST_F(SSLClientSocketTest, DumpMemoryStats) { |
3757 std::unique_ptr<base::FieldTrialList> field_trial_list = | |
3758 base::MakeUnique<base::FieldTrialList>( | |
3759 base::MakeUnique<base::MockEntropyProvider>()); | |
3760 | |
3761 base::FieldTrialList::CreateFieldTrial(Socket::kReadIfReadyTrialName, | |
3762 "enable"); | |
3591 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 3763 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
3592 | 3764 |
3593 int rv; | 3765 int rv; |
3594 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); | 3766 ASSERT_TRUE(CreateAndConnectSSLClientSocket(SSLConfig(), &rv)); |
3595 EXPECT_THAT(rv, IsOk()); | 3767 EXPECT_THAT(rv, IsOk()); |
3596 StreamSocket::SocketMemoryStats stats; | 3768 StreamSocket::SocketMemoryStats stats; |
3597 sock_->DumpMemoryStats(&stats); | 3769 sock_->DumpMemoryStats(&stats); |
3598 EXPECT_EQ(0u, stats.buffer_size); | 3770 EXPECT_EQ(0u, stats.buffer_size); |
3599 EXPECT_EQ(1u, stats.cert_count); | 3771 EXPECT_EQ(1u, stats.cert_count); |
3600 EXPECT_LT(0u, stats.serialized_cert_size); | 3772 EXPECT_LT(0u, stats.serialized_cert_size); |
3601 EXPECT_EQ(stats.serialized_cert_size, stats.total_size); | 3773 EXPECT_EQ(stats.serialized_cert_size, stats.total_size); |
3602 | 3774 |
3603 // Read the response without writing a request, so the read will be pending. | 3775 // Read the response without writing a request, so the read will be pending. |
3604 TestCompletionCallback read_callback; | 3776 TestCompletionCallback read_callback; |
3605 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 3777 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
3778 #if defined(OS_POSIX) | |
davidben
2017/02/01 22:25:58
Not a TEST_P?
xunjieli
2017/02/03 16:35:33
Done.
| |
3779 rv = sock_->ReadIfReady(buf.get(), 4096, read_callback.callback()); | |
3780 #else | |
3606 rv = sock_->Read(buf.get(), 4096, read_callback.callback()); | 3781 rv = sock_->Read(buf.get(), 4096, read_callback.callback()); |
3782 #endif | |
3607 EXPECT_EQ(ERR_IO_PENDING, rv); | 3783 EXPECT_EQ(ERR_IO_PENDING, rv); |
3608 | 3784 |
3609 // Dump memory again and check that |buffer_size| contain the read buffer. | 3785 // Dump memory again and check that |buffer_size| contain the read buffer. |
3610 StreamSocket::SocketMemoryStats stats2; | 3786 StreamSocket::SocketMemoryStats stats2; |
3611 sock_->DumpMemoryStats(&stats2); | 3787 sock_->DumpMemoryStats(&stats2); |
3788 | |
3789 #if defined(OS_POSIX) | |
3790 EXPECT_EQ(0u, stats2.buffer_size); | |
3791 EXPECT_EQ(stats.serialized_cert_size, stats2.total_size); | |
3792 #else | |
3612 EXPECT_EQ(17 * 1024u, stats2.buffer_size); | 3793 EXPECT_EQ(17 * 1024u, stats2.buffer_size); |
3794 EXPECT_LT(17 * 1024u, stats2.total_size); | |
3795 #endif | |
3613 EXPECT_EQ(1u, stats2.cert_count); | 3796 EXPECT_EQ(1u, stats2.cert_count); |
3614 EXPECT_LT(0u, stats2.serialized_cert_size); | 3797 EXPECT_LT(0u, stats2.serialized_cert_size); |
3615 EXPECT_LT(17 * 1024u, stats2.total_size); | |
3616 } | 3798 } |
3617 | 3799 |
3618 } // namespace net | 3800 } // namespace net |
OLD | NEW |