| 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 // This test suite uses SSLClientSocket to test the implementation of | 5 // This test suite uses SSLClientSocket to test the implementation of |
| 6 // SSLServerSocket. In order to establish connections between the sockets | 6 // SSLServerSocket. In order to establish connections between the sockets |
| 7 // we need two additional classes: | 7 // we need two additional classes: |
| 8 // 1. FakeSocket | 8 // 1. FakeSocket |
| 9 // Connects SSL socket to FakeDataChannel. This class is just a stub. | 9 // Connects SSL socket to FakeDataChannel. This class is just a stub. |
| 10 // | 10 // |
| 11 // 2. FakeDataChannel | 11 // 2. FakeDataChannel |
| 12 // Implements the actual exchange of data between two FakeSockets. | 12 // Implements the actual exchange of data between two FakeSockets. |
| 13 // | 13 // |
| 14 // Implementations of these two classes are included in this file. | 14 // Implementations of these two classes are in net/socket/socket_test_util.h. |
| 15 | 15 |
| 16 #include "net/socket/ssl_server_socket.h" | 16 #include "net/socket/ssl_server_socket.h" |
| 17 | 17 |
| 18 #include <stdint.h> | 18 #include <stdint.h> |
| 19 #include <stdlib.h> | 19 #include <stdlib.h> |
| 20 #include <queue> | 20 #include <queue> |
| 21 #include <utility> | 21 #include <utility> |
| 22 | 22 |
| 23 #include <openssl/evp.h> | 23 #include <openssl/evp.h> |
| 24 #include <openssl/ssl.h> | 24 #include <openssl/ssl.h> |
| 25 #include <openssl/x509.h> | 25 #include <openssl/x509.h> |
| 26 | 26 |
| 27 #include "base/callback_helpers.h" | |
| 28 #include "base/compiler_specific.h" | 27 #include "base/compiler_specific.h" |
| 29 #include "base/files/file_path.h" | 28 #include "base/files/file_path.h" |
| 30 #include "base/files/file_util.h" | 29 #include "base/files/file_util.h" |
| 31 #include "base/location.h" | 30 #include "base/location.h" |
| 32 #include "base/logging.h" | 31 #include "base/logging.h" |
| 33 #include "base/macros.h" | 32 #include "base/macros.h" |
| 34 #include "base/message_loop/message_loop.h" | 33 #include "base/message_loop/message_loop.h" |
| 35 #include "base/run_loop.h" | 34 #include "base/run_loop.h" |
| 36 #include "base/single_thread_task_runner.h" | |
| 37 #include "base/threading/thread_task_runner_handle.h" | 35 #include "base/threading/thread_task_runner_handle.h" |
| 38 #include "build/build_config.h" | 36 #include "build/build_config.h" |
| 39 #include "crypto/nss_util.h" | 37 #include "crypto/nss_util.h" |
| 40 #include "crypto/rsa_private_key.h" | 38 #include "crypto/rsa_private_key.h" |
| 41 #include "crypto/scoped_openssl_types.h" | 39 #include "crypto/scoped_openssl_types.h" |
| 42 #include "crypto/signature_creator.h" | 40 #include "crypto/signature_creator.h" |
| 43 #include "net/base/address_list.h" | |
| 44 #include "net/base/completion_callback.h" | 41 #include "net/base/completion_callback.h" |
| 45 #include "net/base/host_port_pair.h" | 42 #include "net/base/host_port_pair.h" |
| 46 #include "net/base/io_buffer.h" | 43 #include "net/base/io_buffer.h" |
| 47 #include "net/base/ip_address.h" | |
| 48 #include "net/base/ip_endpoint.h" | |
| 49 #include "net/base/net_errors.h" | 44 #include "net/base/net_errors.h" |
| 50 #include "net/cert/cert_status_flags.h" | 45 #include "net/cert/cert_status_flags.h" |
| 51 #include "net/cert/ct_policy_enforcer.h" | 46 #include "net/cert/ct_policy_enforcer.h" |
| 52 #include "net/cert/ct_policy_status.h" | 47 #include "net/cert/ct_policy_status.h" |
| 53 #include "net/cert/ct_verifier.h" | 48 #include "net/cert/ct_verifier.h" |
| 54 #include "net/cert/mock_cert_verifier.h" | 49 #include "net/cert/mock_cert_verifier.h" |
| 55 #include "net/cert/mock_client_cert_verifier.h" | 50 #include "net/cert/mock_client_cert_verifier.h" |
| 56 #include "net/cert/x509_certificate.h" | 51 #include "net/cert/x509_certificate.h" |
| 57 #include "net/http/transport_security_state.h" | 52 #include "net/http/transport_security_state.h" |
| 58 #include "net/log/net_log.h" | 53 #include "net/log/net_log.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 107 |
| 113 ct::EVPolicyCompliance DoesConformToCTEVPolicy( | 108 ct::EVPolicyCompliance DoesConformToCTEVPolicy( |
| 114 X509Certificate* cert, | 109 X509Certificate* cert, |
| 115 const ct::EVCertsWhitelist* ev_whitelist, | 110 const ct::EVCertsWhitelist* ev_whitelist, |
| 116 const SCTList& verified_scts, | 111 const SCTList& verified_scts, |
| 117 const BoundNetLog& net_log) override { | 112 const BoundNetLog& net_log) override { |
| 118 return ct::EVPolicyCompliance::EV_POLICY_COMPLIES_VIA_SCTS; | 113 return ct::EVPolicyCompliance::EV_POLICY_COMPLIES_VIA_SCTS; |
| 119 } | 114 } |
| 120 }; | 115 }; |
| 121 | 116 |
| 122 class FakeDataChannel { | |
| 123 public: | |
| 124 FakeDataChannel() | |
| 125 : read_buf_len_(0), | |
| 126 closed_(false), | |
| 127 write_called_after_close_(false), | |
| 128 weak_factory_(this) { | |
| 129 } | |
| 130 | |
| 131 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | |
| 132 DCHECK(read_callback_.is_null()); | |
| 133 DCHECK(!read_buf_.get()); | |
| 134 if (closed_) | |
| 135 return 0; | |
| 136 if (data_.empty()) { | |
| 137 read_callback_ = callback; | |
| 138 read_buf_ = buf; | |
| 139 read_buf_len_ = buf_len; | |
| 140 return ERR_IO_PENDING; | |
| 141 } | |
| 142 return PropagateData(buf, buf_len); | |
| 143 } | |
| 144 | |
| 145 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | |
| 146 DCHECK(write_callback_.is_null()); | |
| 147 if (closed_) { | |
| 148 if (write_called_after_close_) | |
| 149 return ERR_CONNECTION_RESET; | |
| 150 write_called_after_close_ = true; | |
| 151 write_callback_ = callback; | |
| 152 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 153 FROM_HERE, base::Bind(&FakeDataChannel::DoWriteCallback, | |
| 154 weak_factory_.GetWeakPtr())); | |
| 155 return ERR_IO_PENDING; | |
| 156 } | |
| 157 // This function returns synchronously, so make a copy of the buffer. | |
| 158 data_.push(new DrainableIOBuffer( | |
| 159 new StringIOBuffer(std::string(buf->data(), buf_len)), | |
| 160 buf_len)); | |
| 161 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 162 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback, | |
| 163 weak_factory_.GetWeakPtr())); | |
| 164 return buf_len; | |
| 165 } | |
| 166 | |
| 167 // Closes the FakeDataChannel. After Close() is called, Read() returns 0, | |
| 168 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that | |
| 169 // after the FakeDataChannel is closed, the first Write() call completes | |
| 170 // asynchronously, which is necessary to reproduce bug 127822. | |
| 171 void Close() { | |
| 172 closed_ = true; | |
| 173 if (!read_callback_.is_null()) { | |
| 174 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 175 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback, | |
| 176 weak_factory_.GetWeakPtr())); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 private: | |
| 181 void DoReadCallback() { | |
| 182 if (read_callback_.is_null()) | |
| 183 return; | |
| 184 | |
| 185 if (closed_) { | |
| 186 base::ResetAndReturn(&read_callback_).Run(ERR_CONNECTION_CLOSED); | |
| 187 return; | |
| 188 } | |
| 189 | |
| 190 if (data_.empty()) | |
| 191 return; | |
| 192 | |
| 193 int copied = PropagateData(read_buf_, read_buf_len_); | |
| 194 CompletionCallback callback = read_callback_; | |
| 195 read_callback_.Reset(); | |
| 196 read_buf_ = NULL; | |
| 197 read_buf_len_ = 0; | |
| 198 callback.Run(copied); | |
| 199 } | |
| 200 | |
| 201 void DoWriteCallback() { | |
| 202 if (write_callback_.is_null()) | |
| 203 return; | |
| 204 | |
| 205 CompletionCallback callback = write_callback_; | |
| 206 write_callback_.Reset(); | |
| 207 callback.Run(ERR_CONNECTION_RESET); | |
| 208 } | |
| 209 | |
| 210 int PropagateData(scoped_refptr<IOBuffer> read_buf, int read_buf_len) { | |
| 211 scoped_refptr<DrainableIOBuffer> buf = data_.front(); | |
| 212 int copied = std::min(buf->BytesRemaining(), read_buf_len); | |
| 213 memcpy(read_buf->data(), buf->data(), copied); | |
| 214 buf->DidConsume(copied); | |
| 215 | |
| 216 if (!buf->BytesRemaining()) | |
| 217 data_.pop(); | |
| 218 return copied; | |
| 219 } | |
| 220 | |
| 221 CompletionCallback read_callback_; | |
| 222 scoped_refptr<IOBuffer> read_buf_; | |
| 223 int read_buf_len_; | |
| 224 | |
| 225 CompletionCallback write_callback_; | |
| 226 | |
| 227 std::queue<scoped_refptr<DrainableIOBuffer> > data_; | |
| 228 | |
| 229 // True if Close() has been called. | |
| 230 bool closed_; | |
| 231 | |
| 232 // Controls the completion of Write() after the FakeDataChannel is closed. | |
| 233 // After the FakeDataChannel is closed, the first Write() call completes | |
| 234 // asynchronously. | |
| 235 bool write_called_after_close_; | |
| 236 | |
| 237 base::WeakPtrFactory<FakeDataChannel> weak_factory_; | |
| 238 | |
| 239 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel); | |
| 240 }; | |
| 241 | |
| 242 class FakeSocket : public StreamSocket { | |
| 243 public: | |
| 244 FakeSocket(FakeDataChannel* incoming_channel, | |
| 245 FakeDataChannel* outgoing_channel) | |
| 246 : incoming_(incoming_channel), outgoing_(outgoing_channel) {} | |
| 247 | |
| 248 ~FakeSocket() override {} | |
| 249 | |
| 250 int Read(IOBuffer* buf, | |
| 251 int buf_len, | |
| 252 const CompletionCallback& callback) override { | |
| 253 // Read random number of bytes. | |
| 254 buf_len = rand() % buf_len + 1; | |
| 255 return incoming_->Read(buf, buf_len, callback); | |
| 256 } | |
| 257 | |
| 258 int Write(IOBuffer* buf, | |
| 259 int buf_len, | |
| 260 const CompletionCallback& callback) override { | |
| 261 // Write random number of bytes. | |
| 262 buf_len = rand() % buf_len + 1; | |
| 263 return outgoing_->Write(buf, buf_len, callback); | |
| 264 } | |
| 265 | |
| 266 int SetReceiveBufferSize(int32_t size) override { return OK; } | |
| 267 | |
| 268 int SetSendBufferSize(int32_t size) override { return OK; } | |
| 269 | |
| 270 int Connect(const CompletionCallback& callback) override { return OK; } | |
| 271 | |
| 272 void Disconnect() override { | |
| 273 incoming_->Close(); | |
| 274 outgoing_->Close(); | |
| 275 } | |
| 276 | |
| 277 bool IsConnected() const override { return true; } | |
| 278 | |
| 279 bool IsConnectedAndIdle() const override { return true; } | |
| 280 | |
| 281 int GetPeerAddress(IPEndPoint* address) const override { | |
| 282 *address = IPEndPoint(IPAddress::IPv4AllZeros(), 0 /*port*/); | |
| 283 return OK; | |
| 284 } | |
| 285 | |
| 286 int GetLocalAddress(IPEndPoint* address) const override { | |
| 287 *address = IPEndPoint(IPAddress::IPv4AllZeros(), 0 /*port*/); | |
| 288 return OK; | |
| 289 } | |
| 290 | |
| 291 const BoundNetLog& NetLog() const override { return net_log_; } | |
| 292 | |
| 293 void SetSubresourceSpeculation() override {} | |
| 294 void SetOmniboxSpeculation() override {} | |
| 295 | |
| 296 bool WasEverUsed() const override { return true; } | |
| 297 | |
| 298 bool WasNpnNegotiated() const override { return false; } | |
| 299 | |
| 300 NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } | |
| 301 | |
| 302 bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } | |
| 303 | |
| 304 void GetConnectionAttempts(ConnectionAttempts* out) const override { | |
| 305 out->clear(); | |
| 306 } | |
| 307 | |
| 308 void ClearConnectionAttempts() override {} | |
| 309 | |
| 310 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} | |
| 311 | |
| 312 int64_t GetTotalReceivedBytes() const override { | |
| 313 NOTIMPLEMENTED(); | |
| 314 return 0; | |
| 315 } | |
| 316 | |
| 317 private: | |
| 318 BoundNetLog net_log_; | |
| 319 FakeDataChannel* incoming_; | |
| 320 FakeDataChannel* outgoing_; | |
| 321 | |
| 322 DISALLOW_COPY_AND_ASSIGN(FakeSocket); | |
| 323 }; | |
| 324 | |
| 325 } // namespace | 117 } // namespace |
| 326 | 118 |
| 327 // Verify the correctness of the test helper classes first. | 119 // Verify the correctness of the test helper classes first. |
| 328 TEST(FakeSocketTest, DataTransfer) { | 120 TEST(FakeSocketTest, DataTransfer) { |
| 329 // Establish channels between two sockets. | 121 // Establish channels between two sockets. |
| 330 FakeDataChannel channel_1; | 122 FakeDataChannel channel_1; |
| 331 FakeDataChannel channel_2; | 123 FakeDataChannel channel_2; |
| 332 FakeSocket client(&channel_1, &channel_2); | 124 FakeSocket client(&channel_1, &channel_2); |
| 333 FakeSocket server(&channel_2, &channel_1); | 125 FakeSocket server(&channel_2, &channel_1); |
| 334 | 126 |
| (...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1112 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 904 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 1113 | 905 |
| 1114 client_ret = connect_callback.GetResult(client_ret); | 906 client_ret = connect_callback.GetResult(client_ret); |
| 1115 server_ret = handshake_callback.GetResult(server_ret); | 907 server_ret = handshake_callback.GetResult(server_ret); |
| 1116 | 908 |
| 1117 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); | 909 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); |
| 1118 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); | 910 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); |
| 1119 } | 911 } |
| 1120 | 912 |
| 1121 } // namespace net | 913 } // namespace net |
| OLD | NEW |