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