David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/socket/ssl_client_socket.h" |
| 6 | |
| 7 | #include "net/base/address_list.h" |
| 8 | #include "net/base/cert_test_util.h" |
| 9 | #include "net/base/host_resolver.h" |
| 10 | #include "net/base/io_buffer.h" |
| 11 | #include "net/base/mock_cert_verifier.h" |
| 12 | #include "net/base/net_errors.h" |
| 13 | #include "net/base/net_log.h" |
| 14 | #include "net/base/net_log_unittest.h" |
| 15 | #include "net/base/ssl_config_service.h" |
| 16 | #include "net/base/test_completion_callback.h" |
| 17 | #include "net/base/test_data_directory.h" |
| 18 | #include "net/base/test_root_certs.h" |
| 19 | #include "net/socket/client_socket_factory.h" |
| 20 | #include "net/socket/client_socket_handle.h" |
| 21 | #include "net/socket/socket_test_util.h" |
| 22 | #include "net/socket/tcp_client_socket.h" |
| 23 | #include "net/test/test_server.h" |
| 24 | #include "testing/gtest/include/gtest/gtest.h" |
| 25 | #include "testing/platform_test.h" |
| 26 | |
| 27 | //----------------------------------------------------------------------------- |
| 28 | |
| 29 | const net::SSLConfig kDefaultSSLConfig; |
| 30 | |
| 31 | // WrappedStreamSocket is a base class that wraps an existing StreamSocket, |
| 32 | // forwarding the Socket and StreamSocket interfaces to the underlying |
| 33 | // transport. |
| 34 | // This is to provide a common base class for subclasses to override specific |
| 35 | // StreamSocket methods for testing, while still communicating with a 'real' |
| 36 | // StreamSocket. |
| 37 | class WrappedStreamSocket : public net::StreamSocket { |
| 38 | public: |
| 39 | explicit WrappedStreamSocket(scoped_ptr<net::StreamSocket> transport) |
| 40 | : transport_(transport.Pass()) { |
| 41 | } |
| 42 | virtual ~WrappedStreamSocket() {} |
| 43 | |
| 44 | // StreamSocket implementation: |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 45 | virtual int Connect(const net::CompletionCallback& callback) override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 46 | return transport_->Connect(callback); |
| 47 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 48 | virtual void Disconnect() override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 49 | transport_->Disconnect(); |
| 50 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 51 | virtual bool IsConnected() const override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 52 | return transport_->IsConnected(); |
| 53 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 54 | virtual bool IsConnectedAndIdle() const override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 55 | return transport_->IsConnectedAndIdle(); |
| 56 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 57 | virtual int GetPeerAddress(net::IPEndPoint* address) const override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 58 | return transport_->GetPeerAddress(address); |
| 59 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 60 | virtual int GetLocalAddress(net::IPEndPoint* address) const override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 61 | return transport_->GetLocalAddress(address); |
| 62 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 63 | virtual const net::BoundNetLog& NetLog() const override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 64 | return transport_->NetLog(); |
| 65 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 66 | virtual void SetSubresourceSpeculation() override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 67 | transport_->SetSubresourceSpeculation(); |
| 68 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 69 | virtual void SetOmniboxSpeculation() override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 70 | transport_->SetOmniboxSpeculation(); |
| 71 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 72 | virtual bool WasEverUsed() const override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 73 | return transport_->WasEverUsed(); |
| 74 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 75 | virtual bool UsingTCPFastOpen() const override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 76 | return transport_->UsingTCPFastOpen(); |
| 77 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 78 | virtual bool WasNpnNegotiated() const override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 79 | return transport_->WasNpnNegotiated(); |
| 80 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 81 | virtual net::NextProto GetNegotiatedProtocol() const override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 82 | return transport_->GetNegotiatedProtocol(); |
| 83 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 84 | virtual bool GetSSLInfo(net::SSLInfo* ssl_info) override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 85 | return transport_->GetSSLInfo(ssl_info); |
| 86 | } |
| 87 | |
| 88 | // Socket implementation: |
| 89 | virtual int Read(net::IOBuffer* buf, int buf_len, |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 90 | const net::CompletionCallback& callback) override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 91 | return transport_->Read(buf, buf_len, callback); |
| 92 | } |
| 93 | virtual int Write(net::IOBuffer* buf, int buf_len, |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 94 | const net::CompletionCallback& callback) override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 95 | return transport_->Write(buf, buf_len, callback); |
| 96 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 97 | virtual bool SetReceiveBufferSize(int32 size) override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 98 | return transport_->SetReceiveBufferSize(size); |
| 99 | } |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 100 | virtual bool SetSendBufferSize(int32 size) override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 101 | return transport_->SetSendBufferSize(size); |
| 102 | } |
| 103 | |
| 104 | protected: |
| 105 | scoped_ptr<net::StreamSocket> transport_; |
| 106 | }; |
| 107 | |
| 108 | // ReadBufferingStreamSocket is a wrapper for an existing StreamSocket that |
| 109 | // will ensure a certain amount of data is internally buffered before |
| 110 | // satisfying a Read() request. It exists to mimic OS-level internal |
| 111 | // buffering, but in a way to guarantee that X number of bytes will be |
| 112 | // returned to callers of Read(), regardless of how quickly the OS receives |
| 113 | // them from the TestServer. |
| 114 | class ReadBufferingStreamSocket : public WrappedStreamSocket { |
| 115 | public: |
| 116 | explicit ReadBufferingStreamSocket(scoped_ptr<net::StreamSocket> transport); |
| 117 | virtual ~ReadBufferingStreamSocket() {} |
| 118 | |
| 119 | // Socket implementation: |
| 120 | virtual int Read(net::IOBuffer* buf, int buf_len, |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 121 | const net::CompletionCallback& callback) override; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 122 | |
| 123 | // Sets the internal buffer to |size|. This must not be greater than |
| 124 | // the largest value supplied to Read() - that is, it does not handle |
| 125 | // having "leftovers" at the end of Read(). |
| 126 | // Each call to Read() will be prevented from completion until at least |
| 127 | // |size| data has been read. |
| 128 | // Set to 0 to turn off buffering, causing Read() to transparently |
| 129 | // read via the underlying transport. |
| 130 | void SetBufferSize(int size); |
| 131 | |
| 132 | private: |
| 133 | enum State { |
| 134 | STATE_NONE, |
| 135 | STATE_READ, |
| 136 | STATE_READ_COMPLETE, |
| 137 | }; |
| 138 | |
| 139 | int DoLoop(int result); |
| 140 | int DoRead(); |
| 141 | int DoReadComplete(int result); |
| 142 | void OnReadCompleted(int result); |
| 143 | |
| 144 | State state_; |
| 145 | scoped_refptr<net::GrowableIOBuffer> read_buffer_; |
| 146 | int buffer_size_; |
| 147 | |
| 148 | scoped_refptr<net::IOBuffer> user_read_buf_; |
| 149 | net::CompletionCallback user_read_callback_; |
| 150 | }; |
| 151 | |
| 152 | ReadBufferingStreamSocket::ReadBufferingStreamSocket( |
| 153 | scoped_ptr<net::StreamSocket> transport) |
| 154 | : WrappedStreamSocket(transport.Pass()), |
| 155 | read_buffer_(new net::GrowableIOBuffer()), |
| 156 | buffer_size_(0) { |
| 157 | } |
| 158 | |
| 159 | void ReadBufferingStreamSocket::SetBufferSize(int size) { |
| 160 | DCHECK(!user_read_buf_); |
| 161 | buffer_size_ = size; |
| 162 | read_buffer_->SetCapacity(size); |
| 163 | } |
| 164 | |
| 165 | int ReadBufferingStreamSocket::Read(net::IOBuffer* buf, |
| 166 | int buf_len, |
| 167 | const net::CompletionCallback& callback) { |
| 168 | if (buffer_size_ == 0) |
| 169 | return transport_->Read(buf, buf_len, callback); |
| 170 | |
| 171 | if (buf_len < buffer_size_) |
| 172 | return net::ERR_UNEXPECTED; |
| 173 | |
| 174 | state_ = STATE_READ; |
| 175 | user_read_buf_ = buf; |
| 176 | int result = DoLoop(net::OK); |
| 177 | if (result == net::ERR_IO_PENDING) |
| 178 | user_read_callback_ = callback; |
| 179 | else |
| 180 | user_read_buf_ = NULL; |
| 181 | return result; |
| 182 | } |
| 183 | |
| 184 | int ReadBufferingStreamSocket::DoLoop(int result) { |
| 185 | int rv = result; |
| 186 | do { |
| 187 | State current_state = state_; |
| 188 | state_ = STATE_NONE; |
| 189 | switch (current_state) { |
| 190 | case STATE_READ: |
| 191 | rv = DoRead(); |
| 192 | break; |
| 193 | case STATE_READ_COMPLETE: |
| 194 | rv = DoReadComplete(rv); |
| 195 | break; |
| 196 | case STATE_NONE: |
| 197 | default: |
| 198 | NOTREACHED() << "Unexpected state: " << current_state; |
| 199 | rv = net::ERR_UNEXPECTED; |
| 200 | break; |
| 201 | } |
| 202 | } while (rv != net::ERR_IO_PENDING && state_ != STATE_NONE); |
| 203 | return rv; |
| 204 | } |
| 205 | |
| 206 | int ReadBufferingStreamSocket::DoRead() { |
| 207 | state_ = STATE_READ_COMPLETE; |
| 208 | int rv = transport_->Read( |
| 209 | read_buffer_, |
| 210 | read_buffer_->RemainingCapacity(), |
| 211 | base::Bind(&ReadBufferingStreamSocket::OnReadCompleted, |
| 212 | base::Unretained(this))); |
| 213 | return rv; |
| 214 | } |
| 215 | |
| 216 | int ReadBufferingStreamSocket::DoReadComplete(int result) { |
| 217 | state_ = STATE_NONE; |
| 218 | if (result <= 0) |
| 219 | return result; |
| 220 | |
| 221 | read_buffer_->set_offset(read_buffer_->offset() + result); |
| 222 | if (read_buffer_->RemainingCapacity() > 0) { |
| 223 | state_ = STATE_READ; |
| 224 | return net::OK; |
| 225 | } |
| 226 | |
| 227 | memcpy(user_read_buf_->data(), read_buffer_->StartOfBuffer(), |
| 228 | read_buffer_->capacity()); |
| 229 | read_buffer_->set_offset(0); |
| 230 | return read_buffer_->capacity(); |
| 231 | } |
| 232 | |
| 233 | void ReadBufferingStreamSocket::OnReadCompleted(int result) { |
| 234 | result = DoLoop(result); |
| 235 | if (result == net::ERR_IO_PENDING) |
| 236 | return; |
| 237 | |
| 238 | user_read_buf_ = NULL; |
| 239 | base::ResetAndReturn(&user_read_callback_).Run(result); |
| 240 | } |
| 241 | |
| 242 | // Simulates synchronously receiving an error during Read() or Write() |
| 243 | class SynchronousErrorStreamSocket : public WrappedStreamSocket { |
| 244 | public: |
| 245 | explicit SynchronousErrorStreamSocket(scoped_ptr<StreamSocket> transport); |
| 246 | virtual ~SynchronousErrorStreamSocket() {} |
| 247 | |
| 248 | // Socket implementation: |
| 249 | virtual int Read(net::IOBuffer* buf, int buf_len, |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 250 | const net::CompletionCallback& callback) override; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 251 | virtual int Write(net::IOBuffer* buf, int buf_len, |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 252 | const net::CompletionCallback& callback) override; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 253 | |
| 254 | // Sets the the next Read() call to return |error|. |
| 255 | // If there is already a pending asynchronous read, the configured error |
| 256 | // will not be returned until that asynchronous read has completed and Read() |
| 257 | // is called again. |
| 258 | void SetNextReadError(net::Error error) { |
| 259 | DCHECK_GE(0, error); |
| 260 | have_read_error_ = true; |
| 261 | pending_read_error_ = error; |
| 262 | } |
| 263 | |
| 264 | // Sets the the next Write() call to return |error|. |
| 265 | // If there is already a pending asynchronous write, the configured error |
| 266 | // will not be returned until that asynchronous write has completed and |
| 267 | // Write() is called again. |
| 268 | void SetNextWriteError(net::Error error) { |
| 269 | DCHECK_GE(0, error); |
| 270 | have_write_error_ = true; |
| 271 | pending_write_error_ = error; |
| 272 | } |
| 273 | |
| 274 | private: |
| 275 | bool have_read_error_; |
| 276 | int pending_read_error_; |
| 277 | |
| 278 | bool have_write_error_; |
| 279 | int pending_write_error_; |
| 280 | |
| 281 | DISALLOW_COPY_AND_ASSIGN(SynchronousErrorStreamSocket); |
| 282 | }; |
| 283 | |
| 284 | SynchronousErrorStreamSocket::SynchronousErrorStreamSocket( |
| 285 | scoped_ptr<StreamSocket> transport) |
| 286 | : WrappedStreamSocket(transport.Pass()), |
| 287 | have_read_error_(false), |
| 288 | pending_read_error_(net::OK), |
| 289 | have_write_error_(false), |
| 290 | pending_write_error_(net::OK) { |
| 291 | } |
| 292 | |
| 293 | int SynchronousErrorStreamSocket::Read( |
| 294 | net::IOBuffer* buf, |
| 295 | int buf_len, |
| 296 | const net::CompletionCallback& callback) { |
| 297 | if (have_read_error_) { |
| 298 | have_read_error_ = false; |
| 299 | return pending_read_error_; |
| 300 | } |
| 301 | return transport_->Read(buf, buf_len, callback); |
| 302 | } |
| 303 | |
| 304 | int SynchronousErrorStreamSocket::Write( |
| 305 | net::IOBuffer* buf, |
| 306 | int buf_len, |
| 307 | const net::CompletionCallback& callback) { |
| 308 | if (have_write_error_) { |
| 309 | have_write_error_ = false; |
| 310 | return pending_write_error_; |
| 311 | } |
| 312 | return transport_->Write(buf, buf_len, callback); |
| 313 | } |
| 314 | |
| 315 | // FakeBlockingStreamSocket wraps an existing StreamSocket and simulates the |
| 316 | // underlying transport needing to complete things asynchronously in a |
| 317 | // deterministic manner (e.g.: independent of the TestServer and the OS's |
| 318 | // semantics). |
| 319 | class FakeBlockingStreamSocket : public WrappedStreamSocket { |
| 320 | public: |
| 321 | explicit FakeBlockingStreamSocket(scoped_ptr<StreamSocket> transport); |
| 322 | virtual ~FakeBlockingStreamSocket() {} |
| 323 | |
| 324 | // Socket implementation: |
| 325 | virtual int Read(net::IOBuffer* buf, int buf_len, |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 326 | const net::CompletionCallback& callback) override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 327 | return read_state_.RunWrappedFunction(buf, buf_len, callback); |
| 328 | } |
| 329 | virtual int Write(net::IOBuffer* buf, int buf_len, |
Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 330 | const net::CompletionCallback& callback) override { |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 331 | return write_state_.RunWrappedFunction(buf, buf_len, callback); |
| 332 | } |
| 333 | |
| 334 | // Causes the next call to Read() to return ERR_IO_PENDING, not completing |
| 335 | // (invoking the callback) until UnblockRead() has been called and the |
| 336 | // underlying transport has completed. |
| 337 | void SetNextReadShouldBlock() { read_state_.SetShouldBlock(); } |
| 338 | void UnblockRead() { read_state_.Unblock(); } |
| 339 | |
| 340 | // Causes the next call to Write() to return ERR_IO_PENDING, not completing |
| 341 | // (invoking the callback) until UnblockWrite() has been called and the |
| 342 | // underlying transport has completed. |
| 343 | void SetNextWriteShouldBlock() { write_state_.SetShouldBlock(); } |
| 344 | void UnblockWrite() { write_state_.Unblock(); } |
| 345 | |
| 346 | private: |
| 347 | // Tracks the state for simulating a blocking Read/Write operation. |
| 348 | class BlockingState { |
| 349 | public: |
| 350 | // Wrapper for the underlying Socket function to call (ie: Read/Write). |
| 351 | typedef base::Callback< |
| 352 | int(net::IOBuffer*, int, |
| 353 | const net::CompletionCallback&)> WrappedSocketFunction; |
| 354 | |
| 355 | explicit BlockingState(const WrappedSocketFunction& function); |
| 356 | ~BlockingState() {} |
| 357 | |
| 358 | // Sets the next call to RunWrappedFunction() to block, returning |
| 359 | // ERR_IO_PENDING and not invoking the user callback until Unblock() is |
| 360 | // called. |
| 361 | void SetShouldBlock(); |
| 362 | |
| 363 | // Unblocks the currently blocked pending function, invoking the user |
| 364 | // callback if the results are immediately available. |
| 365 | // Note: It's not valid to call this unless SetShouldBlock() has been |
| 366 | // called beforehand. |
| 367 | void Unblock(); |
| 368 | |
| 369 | // Performs the wrapped socket function on the underlying transport. If |
| 370 | // configured to block via SetShouldBlock(), then |user_callback| will not |
| 371 | // be invoked until Unblock() has been called. |
| 372 | int RunWrappedFunction(net::IOBuffer* buf, int len, |
| 373 | const net::CompletionCallback& user_callback); |
| 374 | |
| 375 | private: |
| 376 | // Handles completion from the underlying wrapped socket function. |
| 377 | void OnCompleted(int result); |
| 378 | |
| 379 | WrappedSocketFunction wrapped_function_; |
| 380 | bool should_block_; |
| 381 | bool have_result_; |
| 382 | int pending_result_; |
| 383 | net::CompletionCallback user_callback_; |
| 384 | }; |
| 385 | |
| 386 | BlockingState read_state_; |
| 387 | BlockingState write_state_; |
| 388 | |
| 389 | DISALLOW_COPY_AND_ASSIGN(FakeBlockingStreamSocket); |
| 390 | }; |
| 391 | |
| 392 | FakeBlockingStreamSocket::FakeBlockingStreamSocket( |
| 393 | scoped_ptr<StreamSocket> transport) |
| 394 | : WrappedStreamSocket(transport.Pass()), |
| 395 | read_state_(base::Bind(&Socket::Read, |
| 396 | base::Unretained(transport_.get()))), |
| 397 | write_state_(base::Bind(&Socket::Write, |
| 398 | base::Unretained(transport_.get()))) { |
| 399 | } |
| 400 | |
| 401 | FakeBlockingStreamSocket::BlockingState::BlockingState( |
| 402 | const WrappedSocketFunction& function) |
| 403 | : wrapped_function_(function), |
| 404 | should_block_(false), |
| 405 | have_result_(false), |
| 406 | pending_result_(net::OK) { |
| 407 | } |
| 408 | |
| 409 | void FakeBlockingStreamSocket::BlockingState::SetShouldBlock() { |
| 410 | DCHECK(!should_block_); |
| 411 | should_block_ = true; |
| 412 | } |
| 413 | |
| 414 | void FakeBlockingStreamSocket::BlockingState::Unblock() { |
| 415 | DCHECK(should_block_); |
| 416 | should_block_ = false; |
| 417 | |
| 418 | // If the operation is still pending in the underlying transport, immediately |
| 419 | // return - OnCompleted() will handle invoking the callback once the transport |
| 420 | // has completed. |
| 421 | if (!have_result_) |
| 422 | return; |
| 423 | |
| 424 | have_result_ = false; |
| 425 | |
| 426 | base::ResetAndReturn(&user_callback_).Run(pending_result_); |
| 427 | } |
| 428 | |
| 429 | int FakeBlockingStreamSocket::BlockingState::RunWrappedFunction( |
| 430 | net::IOBuffer* buf, |
| 431 | int len, |
| 432 | const net::CompletionCallback& callback) { |
| 433 | |
| 434 | // The callback to be called by the underlying transport. Either forward |
| 435 | // directly to the user's callback if not set to block, or intercept it with |
| 436 | // OnCompleted so that the user's callback is not invoked until Unblock() is |
| 437 | // called. |
| 438 | net::CompletionCallback transport_callback = |
| 439 | !should_block_ ? callback : base::Bind(&BlockingState::OnCompleted, |
| 440 | base::Unretained(this)); |
| 441 | int rv = wrapped_function_.Run(buf, len, transport_callback); |
| 442 | if (should_block_) { |
| 443 | user_callback_ = callback; |
| 444 | // May have completed synchronously. |
| 445 | have_result_ = (rv != net::ERR_IO_PENDING); |
| 446 | pending_result_ = rv; |
| 447 | return net::ERR_IO_PENDING; |
| 448 | } |
| 449 | |
| 450 | return rv; |
| 451 | } |
| 452 | |
| 453 | void FakeBlockingStreamSocket::BlockingState::OnCompleted(int result) { |
| 454 | if (should_block_) { |
| 455 | // Store the result so that the callback can be invoked once Unblock() is |
| 456 | // called. |
| 457 | have_result_ = true; |
| 458 | pending_result_ = result; |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | // Otherwise, the Unblock() function was called before the underlying |
| 463 | // transport completed, so run the user's callback immediately. |
| 464 | base::ResetAndReturn(&user_callback_).Run(result); |
| 465 | } |
| 466 | |
| 467 | // CompletionCallback that will delete the associated net::StreamSocket when |
| 468 | // the callback is invoked. |
| 469 | class DeleteSocketCallback : public net::TestCompletionCallbackBase { |
| 470 | public: |
| 471 | explicit DeleteSocketCallback(net::StreamSocket* socket) |
| 472 | : socket_(socket), |
| 473 | callback_(base::Bind(&DeleteSocketCallback::OnComplete, |
| 474 | base::Unretained(this))) { |
| 475 | } |
| 476 | virtual ~DeleteSocketCallback() {} |
| 477 | |
| 478 | const net::CompletionCallback& callback() const { return callback_; } |
| 479 | |
| 480 | private: |
| 481 | void OnComplete(int result) { |
| 482 | if (socket_) { |
| 483 | delete socket_; |
| 484 | socket_ = NULL; |
| 485 | } else { |
| 486 | ADD_FAILURE() << "Deleting socket twice"; |
| 487 | } |
| 488 | SetResult(result); |
| 489 | } |
| 490 | |
| 491 | net::StreamSocket* socket_; |
| 492 | net::CompletionCallback callback_; |
| 493 | |
| 494 | DISALLOW_COPY_AND_ASSIGN(DeleteSocketCallback); |
| 495 | }; |
| 496 | |
| 497 | } // namespace |
| 498 | |
| 499 | class SSLClientSocketTest : public PlatformTest { |
| 500 | public: |
| 501 | SSLClientSocketTest() |
| 502 | : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()), |
| 503 | cert_verifier_(new net::MockCertVerifier) { |
| 504 | cert_verifier_->set_default_result(net::OK); |
| 505 | context_.cert_verifier = cert_verifier_.get(); |
| 506 | } |
| 507 | |
| 508 | protected: |
| 509 | net::SSLClientSocket* CreateSSLClientSocket( |
| 510 | net::StreamSocket* transport_socket, |
| 511 | const net::HostPortPair& host_and_port, |
| 512 | const net::SSLConfig& ssl_config) { |
| 513 | return socket_factory_->CreateSSLClientSocket(transport_socket, |
| 514 | host_and_port, |
| 515 | ssl_config, |
| 516 | context_); |
| 517 | } |
| 518 | |
| 519 | net::ClientSocketFactory* socket_factory_; |
| 520 | scoped_ptr<net::MockCertVerifier> cert_verifier_; |
| 521 | net::SSLClientSocketContext context_; |
| 522 | }; |
| 523 | |
| 524 | //----------------------------------------------------------------------------- |
| 525 | |
| 526 | // LogContainsSSLConnectEndEvent returns true if the given index in the given |
| 527 | // log is an SSL connect end event. The NSS sockets will cork in an attempt to |
| 528 | // merge the first application data record with the Finished message when false |
| 529 | // starting. However, in order to avoid the server timing out the handshake, |
| 530 | // they'll give up waiting for application data and send the Finished after a |
| 531 | // timeout. This means that an SSL connect end event may appear as a socket |
| 532 | // write. |
| 533 | static bool LogContainsSSLConnectEndEvent( |
| 534 | const net::CapturingNetLog::CapturedEntryList& log, int i) { |
| 535 | return net::LogContainsEndEvent(log, i, net::NetLog::TYPE_SSL_CONNECT) || |
| 536 | net::LogContainsEvent(log, i, net::NetLog::TYPE_SOCKET_BYTES_SENT, |
| 537 | net::NetLog::PHASE_NONE); |
| 538 | }; |
| 539 | |
| 540 | TEST_F(SSLClientSocketTest, Connect) { |
| 541 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 542 | net::TestServer::kLocalhost, |
| 543 | FilePath()); |
| 544 | ASSERT_TRUE(test_server.Start()); |
| 545 | |
| 546 | net::AddressList addr; |
| 547 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 548 | |
| 549 | net::TestCompletionCallback callback; |
| 550 | net::CapturingNetLog log; |
| 551 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 552 | addr, &log, net::NetLog::Source()); |
| 553 | int rv = transport->Connect(callback.callback()); |
| 554 | if (rv == net::ERR_IO_PENDING) |
| 555 | rv = callback.WaitForResult(); |
| 556 | EXPECT_EQ(net::OK, rv); |
| 557 | |
| 558 | scoped_ptr<net::SSLClientSocket> sock( |
| 559 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 560 | kDefaultSSLConfig)); |
| 561 | |
| 562 | EXPECT_FALSE(sock->IsConnected()); |
| 563 | |
| 564 | rv = sock->Connect(callback.callback()); |
| 565 | |
| 566 | net::CapturingNetLog::CapturedEntryList entries; |
| 567 | log.GetEntries(&entries); |
| 568 | EXPECT_TRUE(net::LogContainsBeginEvent( |
| 569 | entries, 5, net::NetLog::TYPE_SSL_CONNECT)); |
| 570 | if (rv == net::ERR_IO_PENDING) |
| 571 | rv = callback.WaitForResult(); |
| 572 | EXPECT_EQ(net::OK, rv); |
| 573 | EXPECT_TRUE(sock->IsConnected()); |
| 574 | log.GetEntries(&entries); |
| 575 | EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); |
| 576 | |
| 577 | sock->Disconnect(); |
| 578 | EXPECT_FALSE(sock->IsConnected()); |
| 579 | } |
| 580 | |
| 581 | TEST_F(SSLClientSocketTest, ConnectExpired) { |
| 582 | net::TestServer::SSLOptions ssl_options( |
| 583 | net::TestServer::SSLOptions::CERT_EXPIRED); |
| 584 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 585 | ssl_options, |
| 586 | FilePath()); |
| 587 | ASSERT_TRUE(test_server.Start()); |
| 588 | |
| 589 | cert_verifier_->set_default_result(net::ERR_CERT_DATE_INVALID); |
| 590 | |
| 591 | net::AddressList addr; |
| 592 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 593 | |
| 594 | net::TestCompletionCallback callback; |
| 595 | net::CapturingNetLog log; |
| 596 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 597 | addr, &log, net::NetLog::Source()); |
| 598 | int rv = transport->Connect(callback.callback()); |
| 599 | if (rv == net::ERR_IO_PENDING) |
| 600 | rv = callback.WaitForResult(); |
| 601 | EXPECT_EQ(net::OK, rv); |
| 602 | |
| 603 | scoped_ptr<net::SSLClientSocket> sock( |
| 604 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 605 | kDefaultSSLConfig)); |
| 606 | |
| 607 | EXPECT_FALSE(sock->IsConnected()); |
| 608 | |
| 609 | rv = sock->Connect(callback.callback()); |
| 610 | |
| 611 | net::CapturingNetLog::CapturedEntryList entries; |
| 612 | log.GetEntries(&entries); |
| 613 | EXPECT_TRUE(net::LogContainsBeginEvent( |
| 614 | entries, 5, net::NetLog::TYPE_SSL_CONNECT)); |
| 615 | if (rv == net::ERR_IO_PENDING) |
| 616 | rv = callback.WaitForResult(); |
| 617 | |
| 618 | EXPECT_EQ(net::ERR_CERT_DATE_INVALID, rv); |
| 619 | |
| 620 | // Rather than testing whether or not the underlying socket is connected, |
| 621 | // test that the handshake has finished. This is because it may be |
| 622 | // desirable to disconnect the socket before showing a user prompt, since |
| 623 | // the user may take indefinitely long to respond. |
| 624 | log.GetEntries(&entries); |
| 625 | EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); |
| 626 | } |
| 627 | |
| 628 | TEST_F(SSLClientSocketTest, ConnectMismatched) { |
| 629 | net::TestServer::SSLOptions ssl_options( |
| 630 | net::TestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 631 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 632 | ssl_options, |
| 633 | FilePath()); |
| 634 | ASSERT_TRUE(test_server.Start()); |
| 635 | |
| 636 | cert_verifier_->set_default_result(net::ERR_CERT_COMMON_NAME_INVALID); |
| 637 | |
| 638 | net::AddressList addr; |
| 639 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 640 | |
| 641 | net::TestCompletionCallback callback; |
| 642 | net::CapturingNetLog log; |
| 643 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 644 | addr, &log, net::NetLog::Source()); |
| 645 | int rv = transport->Connect(callback.callback()); |
| 646 | if (rv == net::ERR_IO_PENDING) |
| 647 | rv = callback.WaitForResult(); |
| 648 | EXPECT_EQ(net::OK, rv); |
| 649 | |
| 650 | scoped_ptr<net::SSLClientSocket> sock( |
| 651 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 652 | kDefaultSSLConfig)); |
| 653 | |
| 654 | EXPECT_FALSE(sock->IsConnected()); |
| 655 | |
| 656 | rv = sock->Connect(callback.callback()); |
| 657 | |
| 658 | net::CapturingNetLog::CapturedEntryList entries; |
| 659 | log.GetEntries(&entries); |
| 660 | EXPECT_TRUE(net::LogContainsBeginEvent( |
| 661 | entries, 5, net::NetLog::TYPE_SSL_CONNECT)); |
| 662 | if (rv == net::ERR_IO_PENDING) |
| 663 | rv = callback.WaitForResult(); |
| 664 | |
| 665 | EXPECT_EQ(net::ERR_CERT_COMMON_NAME_INVALID, rv); |
| 666 | |
| 667 | // Rather than testing whether or not the underlying socket is connected, |
| 668 | // test that the handshake has finished. This is because it may be |
| 669 | // desirable to disconnect the socket before showing a user prompt, since |
| 670 | // the user may take indefinitely long to respond. |
| 671 | log.GetEntries(&entries); |
| 672 | EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); |
| 673 | } |
| 674 | |
| 675 | // Attempt to connect to a page which requests a client certificate. It should |
| 676 | // return an error code on connect. |
| 677 | TEST_F(SSLClientSocketTest, ConnectClientAuthCertRequested) { |
| 678 | net::TestServer::SSLOptions ssl_options; |
| 679 | ssl_options.request_client_certificate = true; |
| 680 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 681 | ssl_options, |
| 682 | FilePath()); |
| 683 | ASSERT_TRUE(test_server.Start()); |
| 684 | |
| 685 | net::AddressList addr; |
| 686 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 687 | |
| 688 | net::TestCompletionCallback callback; |
| 689 | net::CapturingNetLog log; |
| 690 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 691 | addr, &log, net::NetLog::Source()); |
| 692 | int rv = transport->Connect(callback.callback()); |
| 693 | if (rv == net::ERR_IO_PENDING) |
| 694 | rv = callback.WaitForResult(); |
| 695 | EXPECT_EQ(net::OK, rv); |
| 696 | |
| 697 | scoped_ptr<net::SSLClientSocket> sock( |
| 698 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 699 | kDefaultSSLConfig)); |
| 700 | |
| 701 | EXPECT_FALSE(sock->IsConnected()); |
| 702 | |
| 703 | rv = sock->Connect(callback.callback()); |
| 704 | |
| 705 | net::CapturingNetLog::CapturedEntryList entries; |
| 706 | log.GetEntries(&entries); |
| 707 | EXPECT_TRUE(net::LogContainsBeginEvent( |
| 708 | entries, 5, net::NetLog::TYPE_SSL_CONNECT)); |
| 709 | if (rv == net::ERR_IO_PENDING) |
| 710 | rv = callback.WaitForResult(); |
| 711 | |
| 712 | log.GetEntries(&entries); |
| 713 | // Because we prematurely kill the handshake at CertificateRequest, |
| 714 | // the server may still send data (notably the ServerHelloDone) |
| 715 | // after the error is returned. As a result, the SSL_CONNECT may not |
| 716 | // be the last entry. See http://crbug.com/54445. We use |
| 717 | // ExpectLogContainsSomewhere instead of |
| 718 | // LogContainsSSLConnectEndEvent to avoid assuming, e.g., only one |
| 719 | // extra read instead of two. This occurs before the handshake ends, |
| 720 | // so the corking logic of LogContainsSSLConnectEndEvent isn't |
| 721 | // necessary. |
| 722 | // |
| 723 | // TODO(davidben): When SSL_RestartHandshakeAfterCertReq in NSS is |
| 724 | // fixed and we can respond to the first CertificateRequest |
| 725 | // without closing the socket, add a unit test for sending the |
| 726 | // certificate. This test may still be useful as we'll want to close |
| 727 | // the socket on a timeout if the user takes a long time to pick a |
| 728 | // cert. Related bug: https://bugzilla.mozilla.org/show_bug.cgi?id=542832 |
| 729 | net::ExpectLogContainsSomewhere( |
| 730 | entries, 0, net::NetLog::TYPE_SSL_CONNECT, net::NetLog::PHASE_END); |
| 731 | EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); |
| 732 | EXPECT_FALSE(sock->IsConnected()); |
| 733 | } |
| 734 | |
| 735 | // Connect to a server requesting optional client authentication. Send it a |
| 736 | // null certificate. It should allow the connection. |
| 737 | // |
| 738 | // TODO(davidben): Also test providing an actual certificate. |
| 739 | TEST_F(SSLClientSocketTest, ConnectClientAuthSendNullCert) { |
| 740 | net::TestServer::SSLOptions ssl_options; |
| 741 | ssl_options.request_client_certificate = true; |
| 742 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 743 | ssl_options, |
| 744 | FilePath()); |
| 745 | ASSERT_TRUE(test_server.Start()); |
| 746 | |
| 747 | net::AddressList addr; |
| 748 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 749 | |
| 750 | net::TestCompletionCallback callback; |
| 751 | net::CapturingNetLog log; |
| 752 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 753 | addr, &log, net::NetLog::Source()); |
| 754 | int rv = transport->Connect(callback.callback()); |
| 755 | if (rv == net::ERR_IO_PENDING) |
| 756 | rv = callback.WaitForResult(); |
| 757 | EXPECT_EQ(net::OK, rv); |
| 758 | |
| 759 | net::SSLConfig ssl_config = kDefaultSSLConfig; |
| 760 | ssl_config.send_client_cert = true; |
| 761 | ssl_config.client_cert = NULL; |
| 762 | |
| 763 | scoped_ptr<net::SSLClientSocket> sock( |
| 764 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 765 | ssl_config)); |
| 766 | |
| 767 | EXPECT_FALSE(sock->IsConnected()); |
| 768 | |
| 769 | // Our test server accepts certificate-less connections. |
| 770 | // TODO(davidben): Add a test which requires them and verify the error. |
| 771 | rv = sock->Connect(callback.callback()); |
| 772 | |
| 773 | net::CapturingNetLog::CapturedEntryList entries; |
| 774 | log.GetEntries(&entries); |
| 775 | EXPECT_TRUE(net::LogContainsBeginEvent( |
| 776 | entries, 5, net::NetLog::TYPE_SSL_CONNECT)); |
| 777 | if (rv == net::ERR_IO_PENDING) |
| 778 | rv = callback.WaitForResult(); |
| 779 | |
| 780 | EXPECT_EQ(net::OK, rv); |
| 781 | EXPECT_TRUE(sock->IsConnected()); |
| 782 | log.GetEntries(&entries); |
| 783 | EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); |
| 784 | |
| 785 | // We responded to the server's certificate request with a Certificate |
| 786 | // message with no client certificate in it. ssl_info.client_cert_sent |
| 787 | // should be false in this case. |
| 788 | net::SSLInfo ssl_info; |
| 789 | sock->GetSSLInfo(&ssl_info); |
| 790 | EXPECT_FALSE(ssl_info.client_cert_sent); |
| 791 | |
| 792 | sock->Disconnect(); |
| 793 | EXPECT_FALSE(sock->IsConnected()); |
| 794 | } |
| 795 | |
| 796 | // TODO(wtc): Add unit tests for IsConnectedAndIdle: |
| 797 | // - Server closes an SSL connection (with a close_notify alert message). |
| 798 | // - Server closes the underlying TCP connection directly. |
| 799 | // - Server sends data unexpectedly. |
| 800 | |
| 801 | TEST_F(SSLClientSocketTest, Read) { |
| 802 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 803 | net::TestServer::kLocalhost, |
| 804 | FilePath()); |
| 805 | ASSERT_TRUE(test_server.Start()); |
| 806 | |
| 807 | net::AddressList addr; |
| 808 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 809 | |
| 810 | net::TestCompletionCallback callback; |
| 811 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 812 | addr, NULL, net::NetLog::Source()); |
| 813 | int rv = transport->Connect(callback.callback()); |
| 814 | if (rv == net::ERR_IO_PENDING) |
| 815 | rv = callback.WaitForResult(); |
| 816 | EXPECT_EQ(net::OK, rv); |
| 817 | |
| 818 | scoped_ptr<net::SSLClientSocket> sock( |
| 819 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 820 | kDefaultSSLConfig)); |
| 821 | |
| 822 | rv = sock->Connect(callback.callback()); |
| 823 | if (rv == net::ERR_IO_PENDING) |
| 824 | rv = callback.WaitForResult(); |
| 825 | EXPECT_EQ(net::OK, rv); |
| 826 | EXPECT_TRUE(sock->IsConnected()); |
| 827 | |
| 828 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 829 | scoped_refptr<net::IOBuffer> request_buffer( |
| 830 | new net::IOBuffer(arraysize(request_text) - 1)); |
| 831 | memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 832 | |
| 833 | rv = sock->Write(request_buffer, arraysize(request_text) - 1, |
| 834 | callback.callback()); |
| 835 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 836 | |
| 837 | if (rv == net::ERR_IO_PENDING) |
| 838 | rv = callback.WaitForResult(); |
| 839 | EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
| 840 | |
| 841 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(4096)); |
| 842 | for (;;) { |
| 843 | rv = sock->Read(buf, 4096, callback.callback()); |
| 844 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 845 | |
| 846 | if (rv == net::ERR_IO_PENDING) |
| 847 | rv = callback.WaitForResult(); |
| 848 | |
| 849 | EXPECT_GE(rv, 0); |
| 850 | if (rv <= 0) |
| 851 | break; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | // Tests that the SSLClientSocket properly handles when the underlying transport |
| 856 | // synchronously returns an error code - such as if an intermediary terminates |
| 857 | // the socket connection uncleanly. |
| 858 | // This is a regression test for http://crbug.com/238536 |
| 859 | TEST_F(SSLClientSocketTest, Read_WithSynchronousError) { |
| 860 | net::SpawnedTestServer test_server(net::SpawnedTestServer::TYPE_HTTPS, |
| 861 | net::SpawnedTestServer::kLocalhost, |
| 862 | base::FilePath()); |
| 863 | ASSERT_TRUE(test_server.Start()); |
| 864 | |
| 865 | net::AddressList addr; |
| 866 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 867 | |
| 868 | net::TestCompletionCallback callback; |
| 869 | scoped_ptr<net::StreamSocket> real_transport(new net::TCPClientSocket( |
| 870 | addr, NULL, net::NetLog::Source())); |
| 871 | SynchronousErrorStreamSocket* transport = new SynchronousErrorStreamSocket( |
| 872 | real_transport.Pass()); |
| 873 | int rv = callback.GetResult(transport->Connect(callback.callback())); |
| 874 | EXPECT_EQ(net::OK, rv); |
| 875 | |
| 876 | // Disable TLS False Start to avoid handshake non-determinism. |
| 877 | net::SSLConfig ssl_config; |
| 878 | ssl_config.false_start_enabled = false; |
| 879 | |
| 880 | scoped_ptr<net::SSLClientSocket> sock( |
| 881 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 882 | ssl_config)); |
| 883 | |
| 884 | rv = callback.GetResult(sock->Connect(callback.callback())); |
| 885 | EXPECT_EQ(net::OK, rv); |
| 886 | EXPECT_TRUE(sock->IsConnected()); |
| 887 | |
| 888 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 889 | static const int kRequestTextSize = |
| 890 | static_cast<int>(arraysize(request_text) - 1); |
| 891 | scoped_refptr<net::IOBuffer> request_buffer( |
| 892 | new net::IOBuffer(kRequestTextSize)); |
| 893 | memcpy(request_buffer->data(), request_text, kRequestTextSize); |
| 894 | |
| 895 | rv = callback.GetResult(sock->Write(request_buffer, kRequestTextSize, |
| 896 | callback.callback())); |
| 897 | EXPECT_EQ(kRequestTextSize, rv); |
| 898 | |
| 899 | // Simulate an unclean/forcible shutdown. |
| 900 | transport->SetNextReadError(net::ERR_CONNECTION_RESET); |
| 901 | |
| 902 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(4096)); |
| 903 | |
| 904 | // Note: This test will hang if this bug has regressed. Simply checking that |
| 905 | // rv != ERR_IO_PENDING is insufficient, as ERR_IO_PENDING is a legitimate |
| 906 | // result when using a dedicated task runner for NSS. |
| 907 | rv = callback.GetResult(sock->Read(buf, 4096, callback.callback())); |
| 908 | |
| 909 | #if !defined(USE_OPENSSL) |
| 910 | // NSS records the error exactly |
| 911 | EXPECT_EQ(net::ERR_CONNECTION_RESET, rv); |
| 912 | #else |
| 913 | // OpenSSL treats any errors as a simple EOF. |
| 914 | EXPECT_EQ(0, rv); |
| 915 | #endif |
| 916 | } |
| 917 | |
| 918 | // Test the full duplex mode, with Read and Write pending at the same time. |
| 919 | // This test also serves as a regression test for http://crbug.com/29815. |
| 920 | TEST_F(SSLClientSocketTest, Read_FullDuplex) { |
| 921 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 922 | net::TestServer::kLocalhost, |
| 923 | FilePath()); |
| 924 | ASSERT_TRUE(test_server.Start()); |
| 925 | |
| 926 | net::AddressList addr; |
| 927 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 928 | |
| 929 | net::TestCompletionCallback callback; // Used for everything except Write. |
| 930 | |
| 931 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 932 | addr, NULL, net::NetLog::Source()); |
| 933 | int rv = transport->Connect(callback.callback()); |
| 934 | if (rv == net::ERR_IO_PENDING) |
| 935 | rv = callback.WaitForResult(); |
| 936 | EXPECT_EQ(net::OK, rv); |
| 937 | |
| 938 | scoped_ptr<net::SSLClientSocket> sock( |
| 939 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 940 | kDefaultSSLConfig)); |
| 941 | |
| 942 | rv = sock->Connect(callback.callback()); |
| 943 | if (rv == net::ERR_IO_PENDING) |
| 944 | rv = callback.WaitForResult(); |
| 945 | EXPECT_EQ(net::OK, rv); |
| 946 | EXPECT_TRUE(sock->IsConnected()); |
| 947 | |
| 948 | // Issue a "hanging" Read first. |
| 949 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(4096)); |
| 950 | rv = sock->Read(buf, 4096, callback.callback()); |
| 951 | // We haven't written the request, so there should be no response yet. |
| 952 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 953 | |
| 954 | // Write the request. |
| 955 | // The request is padded with a User-Agent header to a size that causes the |
| 956 | // memio circular buffer (4k bytes) in SSLClientSocketNSS to wrap around. |
| 957 | // This tests the fix for http://crbug.com/29815. |
| 958 | std::string request_text = "GET / HTTP/1.1\r\nUser-Agent: long browser name "; |
| 959 | for (int i = 0; i < 3770; ++i) |
| 960 | request_text.push_back('*'); |
| 961 | request_text.append("\r\n\r\n"); |
| 962 | scoped_refptr<net::IOBuffer> request_buffer( |
| 963 | new net::StringIOBuffer(request_text)); |
| 964 | |
| 965 | net::TestCompletionCallback callback2; // Used for Write only. |
| 966 | rv = sock->Write(request_buffer, request_text.size(), callback2.callback()); |
| 967 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 968 | |
| 969 | if (rv == net::ERR_IO_PENDING) |
| 970 | rv = callback2.WaitForResult(); |
| 971 | EXPECT_EQ(static_cast<int>(request_text.size()), rv); |
| 972 | |
| 973 | // Now get the Read result. |
| 974 | rv = callback.WaitForResult(); |
| 975 | EXPECT_GT(rv, 0); |
| 976 | } |
| 977 | |
| 978 | // Attempts to Read() and Write() from an SSLClientSocketNSS in full duplex |
| 979 | // mode when the underlying transport is blocked on sending data. When the |
| 980 | // underlying transport completes due to an error, it should invoke both the |
| 981 | // Read() and Write() callbacks. If the socket is deleted by the Read() |
| 982 | // callback, the Write() callback should not be invoked. |
| 983 | // Regression test for http://crbug.com/232633 |
| 984 | TEST_F(SSLClientSocketTest, Read_DeleteWhilePendingFullDuplex) { |
| 985 | net::SpawnedTestServer test_server(net::SpawnedTestServer::TYPE_HTTPS, |
| 986 | net::SpawnedTestServer::kLocalhost, |
| 987 | base::FilePath()); |
| 988 | ASSERT_TRUE(test_server.Start()); |
| 989 | |
| 990 | net::AddressList addr; |
| 991 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 992 | |
| 993 | net::TestCompletionCallback callback; |
| 994 | scoped_ptr<net::StreamSocket> real_transport(new net::TCPClientSocket( |
| 995 | addr, NULL, net::NetLog::Source())); |
| 996 | // Note: |error_socket|'s ownership is handed to |transport|, but the pointer |
| 997 | // is retained in order to configure additional errors. |
| 998 | SynchronousErrorStreamSocket* error_socket = new SynchronousErrorStreamSocket( |
| 999 | real_transport.Pass()); |
| 1000 | FakeBlockingStreamSocket* transport = new FakeBlockingStreamSocket( |
| 1001 | scoped_ptr<net::StreamSocket>(error_socket)); |
| 1002 | |
| 1003 | int rv = callback.GetResult(transport->Connect(callback.callback())); |
| 1004 | EXPECT_EQ(net::OK, rv); |
| 1005 | |
| 1006 | // Disable TLS False Start to avoid handshake non-determinism. |
| 1007 | net::SSLConfig ssl_config; |
| 1008 | ssl_config.false_start_enabled = false; |
| 1009 | |
| 1010 | net::SSLClientSocket* sock( |
| 1011 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 1012 | ssl_config)); |
| 1013 | |
| 1014 | rv = callback.GetResult(sock->Connect(callback.callback())); |
| 1015 | EXPECT_EQ(net::OK, rv); |
| 1016 | EXPECT_TRUE(sock->IsConnected()); |
| 1017 | |
| 1018 | std::string request_text = "GET / HTTP/1.1\r\nUser-Agent: long browser name "; |
| 1019 | request_text.append(20 * 1024, '*'); |
| 1020 | request_text.append("\r\n\r\n"); |
| 1021 | scoped_refptr<net::DrainableIOBuffer> request_buffer( |
| 1022 | new net::DrainableIOBuffer(new net::StringIOBuffer(request_text), |
| 1023 | request_text.size())); |
| 1024 | |
| 1025 | // Simulate errors being returned from the underlying Read() and Write() ... |
| 1026 | error_socket->SetNextReadError(net::ERR_CONNECTION_RESET); |
| 1027 | error_socket->SetNextWriteError(net::ERR_CONNECTION_RESET); |
| 1028 | // ... but have those errors returned asynchronously. Because the Write() will |
| 1029 | // return first, this will trigger the error. |
| 1030 | transport->SetNextReadShouldBlock(); |
| 1031 | transport->SetNextWriteShouldBlock(); |
| 1032 | |
| 1033 | // Enqueue a Read() before calling Write(), which should "hang" due to |
| 1034 | // the ERR_IO_PENDING caused by SetReadShouldBlock() and thus return. |
| 1035 | DeleteSocketCallback read_callback(sock); |
| 1036 | scoped_refptr<net::IOBuffer> read_buf(new net::IOBuffer(4096)); |
| 1037 | rv = sock->Read(read_buf, 4096, read_callback.callback()); |
| 1038 | |
| 1039 | // Ensure things didn't complete synchronously, otherwise |sock| is invalid. |
| 1040 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 1041 | ASSERT_FALSE(read_callback.have_result()); |
| 1042 | |
| 1043 | #if !defined(USE_OPENSSL) |
| 1044 | // NSS follows a pattern where a call to PR_Write will only consume as |
| 1045 | // much data as it can encode into application data records before the |
| 1046 | // internal memio buffer is full, which should only fill if writing a large |
| 1047 | // amount of data and the underlying transport is blocked. Once this happens, |
| 1048 | // NSS will return (total size of all application data records it wrote) - 1, |
| 1049 | // with the caller expected to resume with the remaining unsent data. |
| 1050 | // |
| 1051 | // This causes SSLClientSocketNSS::Write to return that it wrote some data |
| 1052 | // before it will return ERR_IO_PENDING, so make an extra call to Write() to |
| 1053 | // get the socket in the state needed for the test below. |
| 1054 | // |
| 1055 | // This is not needed for OpenSSL, because for OpenSSL, |
| 1056 | // SSL_MODE_ENABLE_PARTIAL_WRITE is not specified - thus |
| 1057 | // SSLClientSocketOpenSSL::Write() will not return until all of |
| 1058 | // |request_buffer| has been written to the underlying BIO (although not |
| 1059 | // necessarily the underlying transport). |
| 1060 | rv = callback.GetResult(sock->Write(request_buffer, |
| 1061 | request_buffer->BytesRemaining(), |
| 1062 | callback.callback())); |
| 1063 | ASSERT_LT(0, rv); |
| 1064 | request_buffer->DidConsume(rv); |
| 1065 | |
| 1066 | // Guard to ensure that |request_buffer| was larger than all of the internal |
| 1067 | // buffers (transport, memio, NSS) along the way - otherwise the next call |
| 1068 | // to Write() will crash with an invalid buffer. |
| 1069 | ASSERT_LT(0, request_buffer->BytesRemaining()); |
| 1070 | #endif |
| 1071 | |
| 1072 | // Attempt to write the remaining data. NSS will not be able to consume the |
| 1073 | // application data because the internal buffers are full, while OpenSSL will |
| 1074 | // return that its blocked because the underlying transport is blocked. |
| 1075 | rv = sock->Write(request_buffer, request_buffer->BytesRemaining(), |
| 1076 | callback.callback()); |
| 1077 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 1078 | ASSERT_FALSE(callback.have_result()); |
| 1079 | |
| 1080 | // Now unblock Write(), which will invoke OnSendComplete and (eventually) |
| 1081 | // call the Read() callback, deleting the socket and thus aborting calling |
| 1082 | // the Write() callback. |
| 1083 | transport->UnblockWrite(); |
| 1084 | |
| 1085 | rv = read_callback.WaitForResult(); |
| 1086 | |
| 1087 | #if !defined(USE_OPENSSL) |
| 1088 | // NSS records the error exactly. |
| 1089 | EXPECT_EQ(net::ERR_CONNECTION_RESET, rv); |
| 1090 | #else |
| 1091 | // OpenSSL treats any errors as a simple EOF. |
| 1092 | EXPECT_EQ(0, rv); |
| 1093 | #endif |
| 1094 | |
| 1095 | // The Write callback should not have been called. |
| 1096 | EXPECT_FALSE(callback.have_result()); |
| 1097 | } |
| 1098 | |
| 1099 | TEST_F(SSLClientSocketTest, Read_SmallChunks) { |
| 1100 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 1101 | net::TestServer::kLocalhost, |
| 1102 | FilePath()); |
| 1103 | ASSERT_TRUE(test_server.Start()); |
| 1104 | |
| 1105 | net::AddressList addr; |
| 1106 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 1107 | |
| 1108 | net::TestCompletionCallback callback; |
| 1109 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 1110 | addr, NULL, net::NetLog::Source()); |
| 1111 | int rv = transport->Connect(callback.callback()); |
| 1112 | if (rv == net::ERR_IO_PENDING) |
| 1113 | rv = callback.WaitForResult(); |
| 1114 | EXPECT_EQ(net::OK, rv); |
| 1115 | |
| 1116 | scoped_ptr<net::SSLClientSocket> sock( |
| 1117 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 1118 | kDefaultSSLConfig)); |
| 1119 | |
| 1120 | rv = sock->Connect(callback.callback()); |
| 1121 | if (rv == net::ERR_IO_PENDING) |
| 1122 | rv = callback.WaitForResult(); |
| 1123 | EXPECT_EQ(net::OK, rv); |
| 1124 | |
| 1125 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 1126 | scoped_refptr<net::IOBuffer> request_buffer( |
| 1127 | new net::IOBuffer(arraysize(request_text) - 1)); |
| 1128 | memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 1129 | |
| 1130 | rv = sock->Write(request_buffer, arraysize(request_text) - 1, |
| 1131 | callback.callback()); |
| 1132 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 1133 | |
| 1134 | if (rv == net::ERR_IO_PENDING) |
| 1135 | rv = callback.WaitForResult(); |
| 1136 | EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
| 1137 | |
| 1138 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(1)); |
| 1139 | for (;;) { |
| 1140 | rv = sock->Read(buf, 1, callback.callback()); |
| 1141 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 1142 | |
| 1143 | if (rv == net::ERR_IO_PENDING) |
| 1144 | rv = callback.WaitForResult(); |
| 1145 | |
| 1146 | EXPECT_GE(rv, 0); |
| 1147 | if (rv <= 0) |
| 1148 | break; |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | TEST_F(SSLClientSocketTest, Read_Interrupted) { |
| 1153 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 1154 | net::TestServer::kLocalhost, |
| 1155 | FilePath()); |
| 1156 | ASSERT_TRUE(test_server.Start()); |
| 1157 | |
| 1158 | net::AddressList addr; |
| 1159 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 1160 | |
| 1161 | net::TestCompletionCallback callback; |
| 1162 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 1163 | addr, NULL, net::NetLog::Source()); |
| 1164 | int rv = transport->Connect(callback.callback()); |
| 1165 | if (rv == net::ERR_IO_PENDING) |
| 1166 | rv = callback.WaitForResult(); |
| 1167 | EXPECT_EQ(net::OK, rv); |
| 1168 | |
| 1169 | scoped_ptr<net::SSLClientSocket> sock( |
| 1170 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 1171 | kDefaultSSLConfig)); |
| 1172 | |
| 1173 | rv = sock->Connect(callback.callback()); |
| 1174 | if (rv == net::ERR_IO_PENDING) |
| 1175 | rv = callback.WaitForResult(); |
| 1176 | EXPECT_EQ(net::OK, rv); |
| 1177 | |
| 1178 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 1179 | scoped_refptr<net::IOBuffer> request_buffer( |
| 1180 | new net::IOBuffer(arraysize(request_text) - 1)); |
| 1181 | memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 1182 | |
| 1183 | rv = sock->Write(request_buffer, arraysize(request_text) - 1, |
| 1184 | callback.callback()); |
| 1185 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 1186 | |
| 1187 | if (rv == net::ERR_IO_PENDING) |
| 1188 | rv = callback.WaitForResult(); |
| 1189 | EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
| 1190 | |
| 1191 | // Do a partial read and then exit. This test should not crash! |
| 1192 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(512)); |
| 1193 | rv = sock->Read(buf, 512, callback.callback()); |
| 1194 | EXPECT_TRUE(rv > 0 || rv == net::ERR_IO_PENDING); |
| 1195 | |
| 1196 | if (rv == net::ERR_IO_PENDING) |
| 1197 | rv = callback.WaitForResult(); |
| 1198 | |
| 1199 | EXPECT_GT(rv, 0); |
| 1200 | } |
| 1201 | |
| 1202 | TEST_F(SSLClientSocketTest, Read_FullLogging) { |
| 1203 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 1204 | net::TestServer::kLocalhost, |
| 1205 | FilePath()); |
| 1206 | ASSERT_TRUE(test_server.Start()); |
| 1207 | |
| 1208 | net::AddressList addr; |
| 1209 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 1210 | |
| 1211 | net::TestCompletionCallback callback; |
| 1212 | net::CapturingNetLog log; |
| 1213 | log.SetLogLevel(net::NetLog::LOG_ALL); |
| 1214 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 1215 | addr, &log, net::NetLog::Source()); |
| 1216 | int rv = transport->Connect(callback.callback()); |
| 1217 | if (rv == net::ERR_IO_PENDING) |
| 1218 | rv = callback.WaitForResult(); |
| 1219 | EXPECT_EQ(net::OK, rv); |
| 1220 | |
| 1221 | scoped_ptr<net::SSLClientSocket> sock( |
| 1222 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 1223 | kDefaultSSLConfig)); |
| 1224 | |
| 1225 | rv = sock->Connect(callback.callback()); |
| 1226 | if (rv == net::ERR_IO_PENDING) |
| 1227 | rv = callback.WaitForResult(); |
| 1228 | EXPECT_EQ(net::OK, rv); |
| 1229 | EXPECT_TRUE(sock->IsConnected()); |
| 1230 | |
| 1231 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 1232 | scoped_refptr<net::IOBuffer> request_buffer( |
| 1233 | new net::IOBuffer(arraysize(request_text) - 1)); |
| 1234 | memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 1235 | |
| 1236 | rv = sock->Write(request_buffer, arraysize(request_text) - 1, |
| 1237 | callback.callback()); |
| 1238 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 1239 | |
| 1240 | if (rv == net::ERR_IO_PENDING) |
| 1241 | rv = callback.WaitForResult(); |
| 1242 | EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
| 1243 | |
| 1244 | net::CapturingNetLog::CapturedEntryList entries; |
| 1245 | log.GetEntries(&entries); |
| 1246 | size_t last_index = net::ExpectLogContainsSomewhereAfter( |
| 1247 | entries, 5, net::NetLog::TYPE_SSL_SOCKET_BYTES_SENT, |
| 1248 | net::NetLog::PHASE_NONE); |
| 1249 | |
| 1250 | scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(4096)); |
| 1251 | for (;;) { |
| 1252 | rv = sock->Read(buf, 4096, callback.callback()); |
| 1253 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 1254 | |
| 1255 | if (rv == net::ERR_IO_PENDING) |
| 1256 | rv = callback.WaitForResult(); |
| 1257 | |
| 1258 | EXPECT_GE(rv, 0); |
| 1259 | if (rv <= 0) |
| 1260 | break; |
| 1261 | |
| 1262 | log.GetEntries(&entries); |
| 1263 | last_index = net::ExpectLogContainsSomewhereAfter( |
| 1264 | entries, last_index + 1, net::NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, |
| 1265 | net::NetLog::PHASE_NONE); |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | // Regression test for http://crbug.com/42538 |
| 1270 | TEST_F(SSLClientSocketTest, PrematureApplicationData) { |
| 1271 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 1272 | net::TestServer::kLocalhost, |
| 1273 | FilePath()); |
| 1274 | ASSERT_TRUE(test_server.Start()); |
| 1275 | |
| 1276 | net::AddressList addr; |
| 1277 | net::TestCompletionCallback callback; |
| 1278 | |
| 1279 | static const unsigned char application_data[] = { |
| 1280 | 0x17, 0x03, 0x01, 0x00, 0x4a, 0x02, 0x00, 0x00, 0x46, 0x03, 0x01, 0x4b, |
| 1281 | 0xc2, 0xf8, 0xb2, 0xc1, 0x56, 0x42, 0xb9, 0x57, 0x7f, 0xde, 0x87, 0x46, |
| 1282 | 0xf7, 0xa3, 0x52, 0x42, 0x21, 0xf0, 0x13, 0x1c, 0x9c, 0x83, 0x88, 0xd6, |
| 1283 | 0x93, 0x0c, 0xf6, 0x36, 0x30, 0x05, 0x7e, 0x20, 0xb5, 0xb5, 0x73, 0x36, |
| 1284 | 0x53, 0x83, 0x0a, 0xfc, 0x17, 0x63, 0xbf, 0xa0, 0xe4, 0x42, 0x90, 0x0d, |
| 1285 | 0x2f, 0x18, 0x6d, 0x20, 0xd8, 0x36, 0x3f, 0xfc, 0xe6, 0x01, 0xfa, 0x0f, |
| 1286 | 0xa5, 0x75, 0x7f, 0x09, 0x00, 0x04, 0x00, 0x16, 0x03, 0x01, 0x11, 0x57, |
| 1287 | 0x0b, 0x00, 0x11, 0x53, 0x00, 0x11, 0x50, 0x00, 0x06, 0x22, 0x30, 0x82, |
| 1288 | 0x06, 0x1e, 0x30, 0x82, 0x05, 0x06, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, |
| 1289 | 0x0a |
| 1290 | }; |
| 1291 | |
| 1292 | // All reads and writes complete synchronously (async=false). |
| 1293 | net::MockRead data_reads[] = { |
| 1294 | net::MockRead(net::SYNCHRONOUS, |
| 1295 | reinterpret_cast<const char*>(application_data), |
| 1296 | arraysize(application_data)), |
| 1297 | net::MockRead(net::SYNCHRONOUS, net::OK), |
| 1298 | }; |
| 1299 | |
| 1300 | net::StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 1301 | NULL, 0); |
| 1302 | |
| 1303 | net::StreamSocket* transport = |
| 1304 | new net::MockTCPClientSocket(addr, NULL, &data); |
| 1305 | int rv = transport->Connect(callback.callback()); |
| 1306 | if (rv == net::ERR_IO_PENDING) |
| 1307 | rv = callback.WaitForResult(); |
| 1308 | EXPECT_EQ(net::OK, rv); |
| 1309 | |
| 1310 | scoped_ptr<net::SSLClientSocket> sock( |
| 1311 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 1312 | kDefaultSSLConfig)); |
| 1313 | |
| 1314 | rv = sock->Connect(callback.callback()); |
| 1315 | if (rv == net::ERR_IO_PENDING) |
| 1316 | rv = callback.WaitForResult(); |
| 1317 | EXPECT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv); |
| 1318 | } |
| 1319 | |
| 1320 | // TODO(rsleevi): Not implemented for Schannel. As Schannel is only used when |
| 1321 | // performing client authentication, it will not be tested here. |
| 1322 | TEST_F(SSLClientSocketTest, CipherSuiteDisables) { |
| 1323 | // Rather than exhaustively disabling every RC4 ciphersuite defined at |
| 1324 | // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml, |
| 1325 | // only disabling those cipher suites that the test server actually |
| 1326 | // implements. |
| 1327 | const uint16 kCiphersToDisable[] = { |
| 1328 | 0x0005, // TLS_RSA_WITH_RC4_128_SHA |
| 1329 | }; |
| 1330 | |
| 1331 | net::TestServer::SSLOptions ssl_options; |
| 1332 | // Enable only RC4 on the test server. |
| 1333 | ssl_options.bulk_ciphers = |
| 1334 | net::TestServer::SSLOptions::BULK_CIPHER_RC4; |
| 1335 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 1336 | ssl_options, |
| 1337 | FilePath()); |
| 1338 | ASSERT_TRUE(test_server.Start()); |
| 1339 | |
| 1340 | net::AddressList addr; |
| 1341 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 1342 | |
| 1343 | net::TestCompletionCallback callback; |
| 1344 | net::CapturingNetLog log; |
| 1345 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 1346 | addr, &log, net::NetLog::Source()); |
| 1347 | int rv = transport->Connect(callback.callback()); |
| 1348 | if (rv == net::ERR_IO_PENDING) |
| 1349 | rv = callback.WaitForResult(); |
| 1350 | EXPECT_EQ(net::OK, rv); |
| 1351 | |
| 1352 | net::SSLConfig ssl_config; |
| 1353 | for (size_t i = 0; i < arraysize(kCiphersToDisable); ++i) |
| 1354 | ssl_config.disabled_cipher_suites.push_back(kCiphersToDisable[i]); |
| 1355 | |
| 1356 | scoped_ptr<net::SSLClientSocket> sock( |
| 1357 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 1358 | ssl_config)); |
| 1359 | |
| 1360 | EXPECT_FALSE(sock->IsConnected()); |
| 1361 | |
| 1362 | rv = sock->Connect(callback.callback()); |
| 1363 | net::CapturingNetLog::CapturedEntryList entries; |
| 1364 | log.GetEntries(&entries); |
| 1365 | EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1366 | entries, 5, net::NetLog::TYPE_SSL_CONNECT)); |
| 1367 | |
| 1368 | // NSS has special handling that maps a handshake_failure alert received |
| 1369 | // immediately after a client_hello to be a mismatched cipher suite error, |
| 1370 | // leading to ERR_SSL_VERSION_OR_CIPHER_MISMATCH. When using OpenSSL or |
| 1371 | // Secure Transport (OS X), the handshake_failure is bubbled up without any |
| 1372 | // interpretation, leading to ERR_SSL_PROTOCOL_ERROR. Either way, a failure |
| 1373 | // indicates that no cipher suite was negotiated with the test server. |
| 1374 | if (rv == net::ERR_IO_PENDING) |
| 1375 | rv = callback.WaitForResult(); |
| 1376 | EXPECT_TRUE(rv == net::ERR_SSL_VERSION_OR_CIPHER_MISMATCH || |
| 1377 | rv == net::ERR_SSL_PROTOCOL_ERROR); |
| 1378 | // The exact ordering differs between SSLClientSocketNSS (which issues an |
| 1379 | // extra read) and SSLClientSocketMac (which does not). Just make sure the |
| 1380 | // error appears somewhere in the log. |
| 1381 | log.GetEntries(&entries); |
| 1382 | net::ExpectLogContainsSomewhere(entries, 0, |
| 1383 | net::NetLog::TYPE_SSL_HANDSHAKE_ERROR, |
| 1384 | net::NetLog::PHASE_NONE); |
| 1385 | |
| 1386 | // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 1387 | // the socket when it encounters an error, whereas other implementations |
| 1388 | // leave it connected. |
| 1389 | // Because this an error that the test server is mutually aware of, as opposed |
| 1390 | // to being an error such as a certificate name mismatch, which is |
| 1391 | // client-only, the exact index of the SSL connect end depends on how |
| 1392 | // quickly the test server closes the underlying socket. If the test server |
| 1393 | // closes before the IO message loop pumps messages, there may be a 0-byte |
| 1394 | // Read event in the NetLog due to TCPClientSocket picking up the EOF. As a |
| 1395 | // result, the SSL connect end event will be the second-to-last entry, |
| 1396 | // rather than the last entry. |
| 1397 | EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1) || |
| 1398 | LogContainsSSLConnectEndEvent(entries, -2)); |
| 1399 | } |
| 1400 | |
| 1401 | // When creating an SSLClientSocket, it is allowed to pass in a |
| 1402 | // ClientSocketHandle that is not obtained from a client socket pool. |
| 1403 | // Here we verify that such a simple ClientSocketHandle, not associated with any |
| 1404 | // client socket pool, can be destroyed safely. |
| 1405 | TEST_F(SSLClientSocketTest, ClientSocketHandleNotFromPool) { |
| 1406 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 1407 | net::TestServer::kLocalhost, |
| 1408 | FilePath()); |
| 1409 | ASSERT_TRUE(test_server.Start()); |
| 1410 | |
| 1411 | net::AddressList addr; |
| 1412 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 1413 | |
| 1414 | net::TestCompletionCallback callback; |
| 1415 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 1416 | addr, NULL, net::NetLog::Source()); |
| 1417 | int rv = transport->Connect(callback.callback()); |
| 1418 | if (rv == net::ERR_IO_PENDING) |
| 1419 | rv = callback.WaitForResult(); |
| 1420 | EXPECT_EQ(net::OK, rv); |
| 1421 | |
| 1422 | net::ClientSocketHandle* socket_handle = new net::ClientSocketHandle(); |
| 1423 | socket_handle->set_socket(transport); |
| 1424 | |
| 1425 | scoped_ptr<net::SSLClientSocket> sock( |
| 1426 | socket_factory_->CreateSSLClientSocket( |
| 1427 | socket_handle, test_server.host_port_pair(), kDefaultSSLConfig, |
| 1428 | context_)); |
| 1429 | |
| 1430 | EXPECT_FALSE(sock->IsConnected()); |
| 1431 | rv = sock->Connect(callback.callback()); |
| 1432 | if (rv == net::ERR_IO_PENDING) |
| 1433 | rv = callback.WaitForResult(); |
| 1434 | EXPECT_EQ(net::OK, rv); |
| 1435 | } |
| 1436 | |
| 1437 | // Verifies that SSLClientSocket::ExportKeyingMaterial return a success |
| 1438 | // code and different keying label results in different keying material. |
| 1439 | TEST_F(SSLClientSocketTest, ExportKeyingMaterial) { |
| 1440 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 1441 | net::TestServer::kLocalhost, |
| 1442 | FilePath()); |
| 1443 | ASSERT_TRUE(test_server.Start()); |
| 1444 | |
| 1445 | net::AddressList addr; |
| 1446 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 1447 | |
| 1448 | net::TestCompletionCallback callback; |
| 1449 | |
| 1450 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 1451 | addr, NULL, net::NetLog::Source()); |
| 1452 | int rv = transport->Connect(callback.callback()); |
| 1453 | if (rv == net::ERR_IO_PENDING) |
| 1454 | rv = callback.WaitForResult(); |
| 1455 | EXPECT_EQ(net::OK, rv); |
| 1456 | |
| 1457 | scoped_ptr<net::SSLClientSocket> sock( |
| 1458 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 1459 | kDefaultSSLConfig)); |
| 1460 | |
| 1461 | rv = sock->Connect(callback.callback()); |
| 1462 | if (rv == net::ERR_IO_PENDING) |
| 1463 | rv = callback.WaitForResult(); |
| 1464 | EXPECT_EQ(net::OK, rv); |
| 1465 | EXPECT_TRUE(sock->IsConnected()); |
| 1466 | |
| 1467 | const int kKeyingMaterialSize = 32; |
| 1468 | const char* kKeyingLabel1 = "client-socket-test-1"; |
| 1469 | const char* kKeyingContext = ""; |
| 1470 | unsigned char client_out1[kKeyingMaterialSize]; |
| 1471 | memset(client_out1, 0, sizeof(client_out1)); |
| 1472 | rv = sock->ExportKeyingMaterial(kKeyingLabel1, false, kKeyingContext, |
| 1473 | client_out1, sizeof(client_out1)); |
| 1474 | EXPECT_EQ(rv, net::OK); |
| 1475 | |
| 1476 | const char* kKeyingLabel2 = "client-socket-test-2"; |
| 1477 | unsigned char client_out2[kKeyingMaterialSize]; |
| 1478 | memset(client_out2, 0, sizeof(client_out2)); |
| 1479 | rv = sock->ExportKeyingMaterial(kKeyingLabel2, false, kKeyingContext, |
| 1480 | client_out2, sizeof(client_out2)); |
| 1481 | EXPECT_EQ(rv, net::OK); |
| 1482 | EXPECT_NE(memcmp(client_out1, client_out2, kKeyingMaterialSize), 0); |
| 1483 | } |
| 1484 | |
| 1485 | // Verifies that SSLClientSocket::ClearSessionCache can be called without |
| 1486 | // explicit NSS initialization. |
| 1487 | TEST(SSLClientSocket, ClearSessionCache) { |
| 1488 | net::SSLClientSocket::ClearSessionCache(); |
| 1489 | } |
| 1490 | |
| 1491 | // This tests that SSLInfo contains a properly re-constructed certificate |
| 1492 | // chain. That, in turn, verifies that GetSSLInfo is giving us the chain as |
| 1493 | // verified, not the chain as served by the server. (They may be different.) |
| 1494 | // |
| 1495 | // CERT_CHAIN_WRONG_ROOT is redundant-server-chain.pem. It contains A |
| 1496 | // (end-entity) -> B -> C, and C is signed by D. redundant-validated-chain.pem |
| 1497 | // contains a chain of A -> B -> C2, where C2 is the same public key as C, but |
| 1498 | // a self-signed root. Such a situation can occur when a new root (C2) is |
| 1499 | // cross-certified by an old root (D) and has two different versions of its |
| 1500 | // floating around. Servers may supply C2 as an intermediate, but the |
| 1501 | // SSLClientSocket should return the chain that was verified, from |
| 1502 | // verify_result, instead. |
| 1503 | TEST_F(SSLClientSocketTest, VerifyReturnChainProperlyOrdered) { |
| 1504 | // By default, cause the CertVerifier to treat all certificates as |
| 1505 | // expired. |
| 1506 | cert_verifier_->set_default_result(net::ERR_CERT_DATE_INVALID); |
| 1507 | |
| 1508 | // We will expect SSLInfo to ultimately contain this chain. |
| 1509 | net::CertificateList certs = CreateCertificateListFromFile( |
| 1510 | net::GetTestCertsDirectory(), "redundant-validated-chain.pem", |
| 1511 | net::X509Certificate::FORMAT_AUTO); |
| 1512 | ASSERT_EQ(3U, certs.size()); |
| 1513 | |
| 1514 | net::X509Certificate::OSCertHandles temp_intermediates; |
| 1515 | temp_intermediates.push_back(certs[1]->os_cert_handle()); |
| 1516 | temp_intermediates.push_back(certs[2]->os_cert_handle()); |
| 1517 | |
| 1518 | net::CertVerifyResult verify_result; |
| 1519 | verify_result.verified_cert = |
| 1520 | net::X509Certificate::CreateFromHandle(certs[0]->os_cert_handle(), |
| 1521 | temp_intermediates); |
| 1522 | |
| 1523 | // Add a rule that maps the server cert (A) to the chain of A->B->C2 |
| 1524 | // rather than A->B->C. |
| 1525 | cert_verifier_->AddResultForCert(certs[0], verify_result, net::OK); |
| 1526 | |
| 1527 | // Load and install the root for the validated chain. |
| 1528 | scoped_refptr<net::X509Certificate> root_cert = |
| 1529 | net::ImportCertFromFile(net::GetTestCertsDirectory(), |
| 1530 | "redundant-validated-chain-root.pem"); |
| 1531 | ASSERT_NE(static_cast<net::X509Certificate*>(NULL), root_cert); |
| 1532 | net::ScopedTestRoot scoped_root(root_cert); |
| 1533 | |
| 1534 | // Set up a test server with CERT_CHAIN_WRONG_ROOT. |
| 1535 | net::TestServer::SSLOptions ssl_options( |
| 1536 | net::TestServer::SSLOptions::CERT_CHAIN_WRONG_ROOT); |
| 1537 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, |
| 1538 | ssl_options, |
| 1539 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 1540 | ASSERT_TRUE(test_server.Start()); |
| 1541 | |
| 1542 | net::AddressList addr; |
| 1543 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 1544 | |
| 1545 | net::TestCompletionCallback callback; |
| 1546 | net::CapturingNetLog log; |
| 1547 | net::StreamSocket* transport = new net::TCPClientSocket( |
| 1548 | addr, &log, net::NetLog::Source()); |
| 1549 | int rv = transport->Connect(callback.callback()); |
| 1550 | if (rv == net::ERR_IO_PENDING) |
| 1551 | rv = callback.WaitForResult(); |
| 1552 | EXPECT_EQ(net::OK, rv); |
| 1553 | |
| 1554 | scoped_ptr<net::SSLClientSocket> sock( |
| 1555 | CreateSSLClientSocket(transport, test_server.host_port_pair(), |
| 1556 | kDefaultSSLConfig)); |
| 1557 | EXPECT_FALSE(sock->IsConnected()); |
| 1558 | rv = sock->Connect(callback.callback()); |
| 1559 | |
| 1560 | net::CapturingNetLog::CapturedEntryList entries; |
| 1561 | log.GetEntries(&entries); |
| 1562 | EXPECT_TRUE(net::LogContainsBeginEvent( |
| 1563 | entries, 5, net::NetLog::TYPE_SSL_CONNECT)); |
| 1564 | if (rv == net::ERR_IO_PENDING) |
| 1565 | rv = callback.WaitForResult(); |
| 1566 | |
| 1567 | EXPECT_EQ(net::OK, rv); |
| 1568 | EXPECT_TRUE(sock->IsConnected()); |
| 1569 | log.GetEntries(&entries); |
| 1570 | EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); |
| 1571 | |
| 1572 | net::SSLInfo ssl_info; |
| 1573 | sock->GetSSLInfo(&ssl_info); |
| 1574 | |
| 1575 | // Verify that SSLInfo contains the corrected re-constructed chain A -> B |
| 1576 | // -> C2. |
| 1577 | const net::X509Certificate::OSCertHandles& intermediates = |
| 1578 | ssl_info.cert->GetIntermediateCertificates(); |
| 1579 | ASSERT_EQ(2U, intermediates.size()); |
| 1580 | EXPECT_TRUE(net::X509Certificate::IsSameOSCert( |
| 1581 | ssl_info.cert->os_cert_handle(), certs[0]->os_cert_handle())); |
| 1582 | EXPECT_TRUE(net::X509Certificate::IsSameOSCert( |
| 1583 | intermediates[0], certs[1]->os_cert_handle())); |
| 1584 | EXPECT_TRUE(net::X509Certificate::IsSameOSCert( |
| 1585 | intermediates[1], certs[2]->os_cert_handle())); |
| 1586 | |
| 1587 | sock->Disconnect(); |
| 1588 | EXPECT_FALSE(sock->IsConnected()); |
| 1589 | } |
| 1590 | |