[email protected] | 1bc6f5e | 2012-03-15 00:20:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This test suite uses SSLClientSocket to test the implementation of |
| 6 | // SSLServerSocket. In order to establish connections between the sockets |
| 7 | // we need two additional classes: |
| 8 | // 1. FakeSocket |
| 9 | // Connects SSL socket to FakeDataChannel. This class is just a stub. |
| 10 | // |
| 11 | // 2. FakeDataChannel |
| 12 | // Implements the actual exchange of data between two FakeSockets. |
| 13 | // |
| 14 | // Implementations of these two classes are included in this file. |
| 15 | |
| 16 | #include "net/socket/ssl_server_socket.h" |
| 17 | |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 20 | #include <queue> |
| 21 | |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 22 | #include "base/compiler_specific.h" |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 23 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 24 | #include "base/files/file_path.h" |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 25 | #include "base/message_loop.h" |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 26 | #include "base/path_service.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 27 | #include "crypto/nss_util.h" |
| 28 | #include "crypto/rsa_private_key.h" |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 29 | #include "net/base/address_list.h" |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 30 | #include "net/base/completion_callback.h" |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 31 | #include "net/base/host_port_pair.h" |
| 32 | #include "net/base/io_buffer.h" |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 33 | #include "net/base/ip_endpoint.h" |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 34 | #include "net/base/net_errors.h" |
| 35 | #include "net/base/net_log.h" |
[email protected] | 42fdb45 | 2012-11-01 12:44:40 | [diff] [blame] | 36 | #include "net/base/test_data_directory.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 37 | #include "net/cert/cert_status_flags.h" |
| 38 | #include "net/cert/mock_cert_verifier.h" |
| 39 | #include "net/cert/x509_certificate.h" |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame^] | 40 | #include "net/http/transport_security_state.h" |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 41 | #include "net/socket/client_socket_factory.h" |
| 42 | #include "net/socket/socket_test_util.h" |
| 43 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 44 | #include "net/socket/stream_socket.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 45 | #include "net/ssl/ssl_config_service.h" |
| 46 | #include "net/ssl/ssl_info.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 47 | #include "net/test/cert_test_util.h" |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 48 | #include "testing/gtest/include/gtest/gtest.h" |
| 49 | #include "testing/platform_test.h" |
| 50 | |
| 51 | namespace net { |
| 52 | |
| 53 | namespace { |
| 54 | |
| 55 | class FakeDataChannel { |
| 56 | public: |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 57 | FakeDataChannel() |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 58 | : read_buf_len_(0), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 59 | weak_factory_(this), |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 60 | closed_(false), |
| 61 | write_called_after_close_(false) { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 47a1286 | 2012-04-10 01:00:49 | [diff] [blame] | 64 | int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 65 | if (closed_) |
| 66 | return 0; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 67 | if (data_.empty()) { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 68 | read_callback_ = callback; |
| 69 | read_buf_ = buf; |
| 70 | read_buf_len_ = buf_len; |
| 71 | return net::ERR_IO_PENDING; |
| 72 | } |
| 73 | return PropogateData(buf, buf_len); |
| 74 | } |
| 75 | |
[email protected] | 47a1286 | 2012-04-10 01:00:49 | [diff] [blame] | 76 | int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 77 | if (closed_) { |
| 78 | if (write_called_after_close_) |
| 79 | return net::ERR_CONNECTION_RESET; |
| 80 | write_called_after_close_ = true; |
| 81 | write_callback_ = callback; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 82 | base::MessageLoop::current()->PostTask( |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 83 | FROM_HERE, base::Bind(&FakeDataChannel::DoWriteCallback, |
| 84 | weak_factory_.GetWeakPtr())); |
| 85 | return net::ERR_IO_PENDING; |
| 86 | } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 87 | data_.push(new net::DrainableIOBuffer(buf, buf_len)); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 88 | base::MessageLoop::current()->PostTask( |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 89 | FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback, |
| 90 | weak_factory_.GetWeakPtr())); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 91 | return buf_len; |
| 92 | } |
| 93 | |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 94 | // Closes the FakeDataChannel. After Close() is called, Read() returns 0, |
| 95 | // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that |
| 96 | // after the FakeDataChannel is closed, the first Write() call completes |
| 97 | // asynchronously, which is necessary to reproduce bug 127822. |
| 98 | void Close() { |
| 99 | closed_ = true; |
| 100 | } |
| 101 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 102 | private: |
| 103 | void DoReadCallback() { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 104 | if (read_callback_.is_null() || data_.empty()) |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 105 | return; |
| 106 | |
| 107 | int copied = PropogateData(read_buf_, read_buf_len_); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 108 | CompletionCallback callback = read_callback_; |
| 109 | read_callback_.Reset(); |
| 110 | read_buf_ = NULL; |
| 111 | read_buf_len_ = 0; |
| 112 | callback.Run(copied); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 115 | void DoWriteCallback() { |
| 116 | if (write_callback_.is_null()) |
| 117 | return; |
| 118 | |
| 119 | CompletionCallback callback = write_callback_; |
| 120 | write_callback_.Reset(); |
| 121 | callback.Run(net::ERR_CONNECTION_RESET); |
| 122 | } |
| 123 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 124 | int PropogateData(scoped_refptr<net::IOBuffer> read_buf, int read_buf_len) { |
| 125 | scoped_refptr<net::DrainableIOBuffer> buf = data_.front(); |
| 126 | int copied = std::min(buf->BytesRemaining(), read_buf_len); |
| 127 | memcpy(read_buf->data(), buf->data(), copied); |
| 128 | buf->DidConsume(copied); |
| 129 | |
| 130 | if (!buf->BytesRemaining()) |
| 131 | data_.pop(); |
| 132 | return copied; |
| 133 | } |
| 134 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 135 | CompletionCallback read_callback_; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 136 | scoped_refptr<net::IOBuffer> read_buf_; |
| 137 | int read_buf_len_; |
| 138 | |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 139 | CompletionCallback write_callback_; |
| 140 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 141 | std::queue<scoped_refptr<net::DrainableIOBuffer> > data_; |
| 142 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 143 | base::WeakPtrFactory<FakeDataChannel> weak_factory_; |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 144 | |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 145 | // True if Close() has been called. |
| 146 | bool closed_; |
| 147 | |
| 148 | // Controls the completion of Write() after the FakeDataChannel is closed. |
| 149 | // After the FakeDataChannel is closed, the first Write() call completes |
| 150 | // asynchronously. |
| 151 | bool write_called_after_close_; |
| 152 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 153 | DISALLOW_COPY_AND_ASSIGN(FakeDataChannel); |
| 154 | }; |
| 155 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 156 | class FakeSocket : public StreamSocket { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 157 | public: |
| 158 | FakeSocket(FakeDataChannel* incoming_channel, |
| 159 | FakeDataChannel* outgoing_channel) |
| 160 | : incoming_(incoming_channel), |
| 161 | outgoing_(outgoing_channel) { |
| 162 | } |
| 163 | |
| 164 | virtual ~FakeSocket() { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | virtual int Read(IOBuffer* buf, int buf_len, |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 168 | const CompletionCallback& callback) OVERRIDE { |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 169 | // Read random number of bytes. |
| 170 | buf_len = rand() % buf_len + 1; |
| 171 | return incoming_->Read(buf, buf_len, callback); |
| 172 | } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 173 | |
| 174 | virtual int Write(IOBuffer* buf, int buf_len, |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 175 | const CompletionCallback& callback) OVERRIDE { |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 176 | // Write random number of bytes. |
| 177 | buf_len = rand() % buf_len + 1; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 178 | return outgoing_->Write(buf, buf_len, callback); |
| 179 | } |
| 180 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 181 | virtual bool SetReceiveBufferSize(int32 size) OVERRIDE { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 182 | return true; |
| 183 | } |
| 184 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 185 | virtual bool SetSendBufferSize(int32 size) OVERRIDE { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 186 | return true; |
| 187 | } |
| 188 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 189 | virtual int Connect(const CompletionCallback& callback) OVERRIDE { |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 190 | return net::OK; |
| 191 | } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 192 | |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 193 | virtual void Disconnect() OVERRIDE { |
| 194 | incoming_->Close(); |
| 195 | outgoing_->Close(); |
| 196 | } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 197 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 198 | virtual bool IsConnected() const OVERRIDE { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 199 | return true; |
| 200 | } |
| 201 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 202 | virtual bool IsConnectedAndIdle() const OVERRIDE { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 203 | return true; |
| 204 | } |
| 205 | |
[email protected] | a352869 | 2012-06-08 00:11:42 | [diff] [blame] | 206 | virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE { |
| 207 | net::IPAddressNumber ip_address(net::kIPv4AddressSize); |
| 208 | *address = net::IPEndPoint(ip_address, 0 /*port*/); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 209 | return net::OK; |
| 210 | } |
| 211 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 212 | virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE { |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 213 | net::IPAddressNumber ip_address(4); |
| 214 | *address = net::IPEndPoint(ip_address, 0); |
| 215 | return net::OK; |
| 216 | } |
| 217 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 218 | virtual const BoundNetLog& NetLog() const OVERRIDE { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 219 | return net_log_; |
| 220 | } |
| 221 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 222 | virtual void SetSubresourceSpeculation() OVERRIDE {} |
| 223 | virtual void SetOmniboxSpeculation() OVERRIDE {} |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 224 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 225 | virtual bool WasEverUsed() const OVERRIDE { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 229 | virtual bool UsingTCPFastOpen() const OVERRIDE { |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 230 | return false; |
| 231 | } |
| 232 | |
[email protected] | 5e6efa5 | 2011-06-27 17:26:41 | [diff] [blame] | 233 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 234 | virtual bool WasNpnNegotiated() const OVERRIDE { |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 238 | virtual NextProto GetNegotiatedProtocol() const OVERRIDE { |
[email protected] | 33661e48 | 2012-04-03 16:16:26 | [diff] [blame] | 239 | return kProtoUnknown; |
| 240 | } |
| 241 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 242 | virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 246 | private: |
| 247 | net::BoundNetLog net_log_; |
| 248 | FakeDataChannel* incoming_; |
| 249 | FakeDataChannel* outgoing_; |
| 250 | |
| 251 | DISALLOW_COPY_AND_ASSIGN(FakeSocket); |
| 252 | }; |
| 253 | |
| 254 | } // namespace |
| 255 | |
| 256 | // Verify the correctness of the test helper classes first. |
| 257 | TEST(FakeSocketTest, DataTransfer) { |
| 258 | // Establish channels between two sockets. |
| 259 | FakeDataChannel channel_1; |
| 260 | FakeDataChannel channel_2; |
| 261 | FakeSocket client(&channel_1, &channel_2); |
| 262 | FakeSocket server(&channel_2, &channel_1); |
| 263 | |
| 264 | const char kTestData[] = "testing123"; |
| 265 | const int kTestDataSize = strlen(kTestData); |
| 266 | const int kReadBufSize = 1024; |
| 267 | scoped_refptr<net::IOBuffer> write_buf = new net::StringIOBuffer(kTestData); |
| 268 | scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize); |
| 269 | |
| 270 | // Write then read. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 271 | int written = |
| 272 | server.Write(write_buf.get(), kTestDataSize, CompletionCallback()); |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 273 | EXPECT_GT(written, 0); |
| 274 | EXPECT_LE(written, kTestDataSize); |
| 275 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 276 | int read = client.Read(read_buf.get(), kReadBufSize, CompletionCallback()); |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 277 | EXPECT_GT(read, 0); |
| 278 | EXPECT_LE(read, written); |
| 279 | EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 280 | |
| 281 | // Read then write. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 282 | TestCompletionCallback callback; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 283 | EXPECT_EQ(net::ERR_IO_PENDING, |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 284 | server.Read(read_buf.get(), kReadBufSize, callback.callback())); |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 285 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 286 | written = client.Write(write_buf.get(), kTestDataSize, CompletionCallback()); |
[email protected] | 55ee0e5 | 2011-07-21 18:29:44 | [diff] [blame] | 287 | EXPECT_GT(written, 0); |
| 288 | EXPECT_LE(written, kTestDataSize); |
| 289 | |
| 290 | read = callback.WaitForResult(); |
| 291 | EXPECT_GT(read, 0); |
| 292 | EXPECT_LE(read, written); |
| 293 | EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | class SSLServerSocketTest : public PlatformTest { |
| 297 | public: |
| 298 | SSLServerSocketTest() |
[email protected] | 9f59fac | 2012-03-21 23:18:11 | [diff] [blame] | 299 | : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()), |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame^] | 300 | cert_verifier_(new MockCertVerifier()), |
| 301 | transport_security_state_(new TransportSecurityState) { |
[email protected] | 47a1286 | 2012-04-10 01:00:49 | [diff] [blame] | 302 | cert_verifier_->set_default_result(net::CERT_STATUS_AUTHORITY_INVALID); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | protected: |
| 306 | void Initialize() { |
| 307 | FakeSocket* fake_client_socket = new FakeSocket(&channel_1_, &channel_2_); |
| 308 | FakeSocket* fake_server_socket = new FakeSocket(&channel_2_, &channel_1_); |
| 309 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 310 | base::FilePath certs_dir(GetTestCertsDirectory()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 311 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 312 | base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 313 | std::string cert_der; |
| 314 | ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_der)); |
| 315 | |
| 316 | scoped_refptr<net::X509Certificate> cert = |
| 317 | X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size()); |
| 318 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 319 | base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 320 | std::string key_string; |
| 321 | ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string)); |
| 322 | std::vector<uint8> key_vector( |
| 323 | reinterpret_cast<const uint8*>(key_string.data()), |
| 324 | reinterpret_cast<const uint8*>(key_string.data() + |
| 325 | key_string.length())); |
| 326 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 327 | scoped_ptr<crypto::RSAPrivateKey> private_key( |
| 328 | crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 329 | |
| 330 | net::SSLConfig ssl_config; |
[email protected] | 2fb7e3ba | 2011-06-22 19:24:38 | [diff] [blame] | 331 | ssl_config.cached_info_enabled = false; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 332 | ssl_config.false_start_enabled = false; |
[email protected] | 6b4903f | 2012-06-26 02:13:49 | [diff] [blame] | 333 | ssl_config.channel_id_enabled = false; |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 334 | ssl_config.version_min = SSL_PROTOCOL_VERSION_SSL3; |
[email protected] | 21a1a18 | 2012-08-20 22:18:23 | [diff] [blame] | 335 | ssl_config.version_max = SSL_PROTOCOL_VERSION_TLS1_1; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 336 | |
| 337 | // Certificate provided by the host doesn't need authority. |
| 338 | net::SSLConfig::CertAndStatus cert_and_status; |
[email protected] | 4dc832e | 2011-04-28 22:04:24 | [diff] [blame] | 339 | cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; |
[email protected] | 3d5c1bd | 2011-07-20 02:14:01 | [diff] [blame] | 340 | cert_and_status.der_cert = cert_der; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 341 | ssl_config.allowed_bad_certs.push_back(cert_and_status); |
| 342 | |
| 343 | net::HostPortPair host_and_pair("unittest", 0); |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 344 | net::SSLClientSocketContext context; |
[email protected] | 9f59fac | 2012-03-21 23:18:11 | [diff] [blame] | 345 | context.cert_verifier = cert_verifier_.get(); |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame^] | 346 | context.transport_security_state = transport_security_state_.get(); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 347 | client_socket_.reset( |
| 348 | socket_factory_->CreateSSLClientSocket( |
[email protected] | efe22215 | 2012-06-27 16:48:46 | [diff] [blame] | 349 | fake_client_socket, host_and_pair, ssl_config, context)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 350 | server_socket_.reset(net::CreateSSLServerSocket( |
| 351 | fake_server_socket, cert.get(), private_key.get(), net::SSLConfig())); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | FakeDataChannel channel_1_; |
| 355 | FakeDataChannel channel_2_; |
| 356 | scoped_ptr<net::SSLClientSocket> client_socket_; |
| 357 | scoped_ptr<net::SSLServerSocket> server_socket_; |
| 358 | net::ClientSocketFactory* socket_factory_; |
[email protected] | 47a1286 | 2012-04-10 01:00:49 | [diff] [blame] | 359 | scoped_ptr<net::MockCertVerifier> cert_verifier_; |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame^] | 360 | scoped_ptr<net::TransportSecurityState> transport_security_state_; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | // SSLServerSocket is only implemented using NSS. |
| 364 | #if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) |
| 365 | |
| 366 | // This test only executes creation of client and server sockets. This is to |
| 367 | // test that creation of sockets doesn't crash and have minimal code to run |
| 368 | // under valgrind in order to help debugging memory problems. |
| 369 | TEST_F(SSLServerSocketTest, Initialize) { |
| 370 | Initialize(); |
| 371 | } |
| 372 | |
[email protected] | a7ac3c3 | 2011-06-17 19:10:15 | [diff] [blame] | 373 | // This test executes Connect() on SSLClientSocket and Handshake() on |
| 374 | // SSLServerSocket to make sure handshaking between the two sockets is |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 375 | // completed successfully. |
| 376 | TEST_F(SSLServerSocketTest, Handshake) { |
| 377 | Initialize(); |
| 378 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 379 | TestCompletionCallback connect_callback; |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 380 | TestCompletionCallback handshake_callback; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 381 | |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 382 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 383 | EXPECT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING); |
| 384 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 385 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 386 | EXPECT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING); |
| 387 | |
| 388 | if (client_ret == net::ERR_IO_PENDING) { |
| 389 | EXPECT_EQ(net::OK, connect_callback.WaitForResult()); |
| 390 | } |
| 391 | if (server_ret == net::ERR_IO_PENDING) { |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 392 | EXPECT_EQ(net::OK, handshake_callback.WaitForResult()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 393 | } |
[email protected] | 4dc832e | 2011-04-28 22:04:24 | [diff] [blame] | 394 | |
| 395 | // Make sure the cert status is expected. |
| 396 | SSLInfo ssl_info; |
| 397 | client_socket_->GetSSLInfo(&ssl_info); |
| 398 | EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | TEST_F(SSLServerSocketTest, DataTransfer) { |
| 402 | Initialize(); |
| 403 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 404 | TestCompletionCallback connect_callback; |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 405 | TestCompletionCallback handshake_callback; |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 406 | |
| 407 | // Establish connection. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 408 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
[email protected] | 302b627 | 2011-01-19 01:27:22 | [diff] [blame] | 409 | ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 410 | |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 411 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
[email protected] | 302b627 | 2011-01-19 01:27:22 | [diff] [blame] | 412 | ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 413 | |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 414 | client_ret = connect_callback.GetResult(client_ret); |
| 415 | ASSERT_EQ(net::OK, client_ret); |
| 416 | server_ret = handshake_callback.GetResult(server_ret); |
| 417 | ASSERT_EQ(net::OK, server_ret); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 418 | |
| 419 | const int kReadBufSize = 1024; |
| 420 | scoped_refptr<net::StringIOBuffer> write_buf = |
| 421 | new net::StringIOBuffer("testing123"); |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 422 | scoped_refptr<net::DrainableIOBuffer> read_buf = |
| 423 | new net::DrainableIOBuffer(new net::IOBuffer(kReadBufSize), |
| 424 | kReadBufSize); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 425 | |
| 426 | // Write then read. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 427 | TestCompletionCallback write_callback; |
| 428 | TestCompletionCallback read_callback; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 429 | server_ret = server_socket_->Write( |
| 430 | write_buf.get(), write_buf->size(), write_callback.callback()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 431 | EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 432 | client_ret = client_socket_->Read( |
| 433 | read_buf.get(), read_buf->BytesRemaining(), read_callback.callback()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 434 | EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING); |
| 435 | |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 436 | server_ret = write_callback.GetResult(server_ret); |
| 437 | EXPECT_GT(server_ret, 0); |
| 438 | client_ret = read_callback.GetResult(client_ret); |
| 439 | ASSERT_GT(client_ret, 0); |
| 440 | |
| 441 | read_buf->DidConsume(client_ret); |
| 442 | while (read_buf->BytesConsumed() < write_buf->size()) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 443 | client_ret = client_socket_->Read( |
| 444 | read_buf.get(), read_buf->BytesRemaining(), read_callback.callback()); |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 445 | EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING); |
| 446 | client_ret = read_callback.GetResult(client_ret); |
| 447 | ASSERT_GT(client_ret, 0); |
| 448 | read_buf->DidConsume(client_ret); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 449 | } |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 450 | EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed()); |
| 451 | read_buf->SetOffset(0); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 452 | EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size())); |
| 453 | |
| 454 | // Read then write. |
| 455 | write_buf = new net::StringIOBuffer("hello123"); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 456 | server_ret = server_socket_->Read( |
| 457 | read_buf.get(), read_buf->BytesRemaining(), read_callback.callback()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 458 | EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 459 | client_ret = client_socket_->Write( |
| 460 | write_buf.get(), write_buf->size(), write_callback.callback()); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 461 | EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING); |
| 462 | |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 463 | server_ret = read_callback.GetResult(server_ret); |
| 464 | ASSERT_GT(server_ret, 0); |
| 465 | client_ret = write_callback.GetResult(client_ret); |
| 466 | EXPECT_GT(client_ret, 0); |
| 467 | |
| 468 | read_buf->DidConsume(server_ret); |
| 469 | while (read_buf->BytesConsumed() < write_buf->size()) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 470 | server_ret = server_socket_->Read( |
| 471 | read_buf.get(), read_buf->BytesRemaining(), read_callback.callback()); |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 472 | EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING); |
| 473 | server_ret = read_callback.GetResult(server_ret); |
| 474 | ASSERT_GT(server_ret, 0); |
| 475 | read_buf->DidConsume(server_ret); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 476 | } |
[email protected] | febbbb5 | 2011-08-17 04:59:23 | [diff] [blame] | 477 | EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed()); |
| 478 | read_buf->SetOffset(0); |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 479 | EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size())); |
| 480 | } |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 481 | |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 482 | // A regression test for bug 127822 (http://crbug.com/127822). |
| 483 | // If the server closes the connection after the handshake is finished, |
| 484 | // the client's Write() call should not cause an infinite loop. |
| 485 | // NOTE: this is a test for SSLClientSocket rather than SSLServerSocket. |
| 486 | TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) { |
| 487 | Initialize(); |
| 488 | |
| 489 | TestCompletionCallback connect_callback; |
| 490 | TestCompletionCallback handshake_callback; |
| 491 | |
| 492 | // Establish connection. |
| 493 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 494 | ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING); |
| 495 | |
| 496 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 497 | ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING); |
| 498 | |
| 499 | client_ret = connect_callback.GetResult(client_ret); |
| 500 | ASSERT_EQ(net::OK, client_ret); |
| 501 | server_ret = handshake_callback.GetResult(server_ret); |
| 502 | ASSERT_EQ(net::OK, server_ret); |
| 503 | |
| 504 | scoped_refptr<net::StringIOBuffer> write_buf = |
| 505 | new net::StringIOBuffer("testing123"); |
| 506 | |
| 507 | // The server closes the connection. The server needs to write some |
| 508 | // data first so that the client's Read() calls from the transport |
| 509 | // socket won't return ERR_IO_PENDING. This ensures that the client |
| 510 | // will call Read() on the transport socket again. |
| 511 | TestCompletionCallback write_callback; |
| 512 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 513 | server_ret = server_socket_->Write( |
| 514 | write_buf.get(), write_buf->size(), write_callback.callback()); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 515 | EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING); |
| 516 | |
| 517 | server_ret = write_callback.GetResult(server_ret); |
| 518 | EXPECT_GT(server_ret, 0); |
| 519 | |
| 520 | server_socket_->Disconnect(); |
| 521 | |
| 522 | // The client writes some data. This should not cause an infinite loop. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 523 | client_ret = client_socket_->Write( |
| 524 | write_buf.get(), write_buf->size(), write_callback.callback()); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 525 | EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING); |
| 526 | |
| 527 | client_ret = write_callback.GetResult(client_ret); |
| 528 | EXPECT_GT(client_ret, 0); |
| 529 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 530 | base::MessageLoop::current()->PostDelayedTask( |
| 531 | FROM_HERE, base::MessageLoop::QuitClosure(), |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 532 | base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 533 | base::MessageLoop::current()->Run(); |
[email protected] | c0e4dd1 | 2012-05-16 19:36:31 | [diff] [blame] | 534 | } |
| 535 | |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 536 | // This test executes ExportKeyingMaterial() on the client and server sockets, |
| 537 | // after connecting them, and verifies that the results match. |
| 538 | // This test will fail if False Start is enabled (see crbug.com/90208). |
| 539 | TEST_F(SSLServerSocketTest, ExportKeyingMaterial) { |
| 540 | Initialize(); |
| 541 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 542 | TestCompletionCallback connect_callback; |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 543 | TestCompletionCallback handshake_callback; |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 544 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 545 | int client_ret = client_socket_->Connect(connect_callback.callback()); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 546 | ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING); |
| 547 | |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 548 | int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 549 | ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING); |
| 550 | |
| 551 | if (client_ret == net::ERR_IO_PENDING) { |
| 552 | ASSERT_EQ(net::OK, connect_callback.WaitForResult()); |
| 553 | } |
| 554 | if (server_ret == net::ERR_IO_PENDING) { |
| 555 | ASSERT_EQ(net::OK, handshake_callback.WaitForResult()); |
| 556 | } |
| 557 | |
| 558 | const int kKeyingMaterialSize = 32; |
| 559 | const char* kKeyingLabel = "EXPERIMENTAL-server-socket-test"; |
| 560 | const char* kKeyingContext = ""; |
| 561 | unsigned char server_out[kKeyingMaterialSize]; |
[email protected] | 1bc6f5e | 2012-03-15 00:20:58 | [diff] [blame] | 562 | int rv = server_socket_->ExportKeyingMaterial(kKeyingLabel, |
| 563 | false, kKeyingContext, |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 564 | server_out, sizeof(server_out)); |
[email protected] | 47a1286 | 2012-04-10 01:00:49 | [diff] [blame] | 565 | ASSERT_EQ(net::OK, rv); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 566 | |
| 567 | unsigned char client_out[kKeyingMaterialSize]; |
[email protected] | 1bc6f5e | 2012-03-15 00:20:58 | [diff] [blame] | 568 | rv = client_socket_->ExportKeyingMaterial(kKeyingLabel, |
| 569 | false, kKeyingContext, |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 570 | client_out, sizeof(client_out)); |
[email protected] | 47a1286 | 2012-04-10 01:00:49 | [diff] [blame] | 571 | ASSERT_EQ(net::OK, rv); |
| 572 | EXPECT_EQ(0, memcmp(server_out, client_out, sizeof(server_out))); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 573 | |
| 574 | const char* kKeyingLabelBad = "EXPERIMENTAL-server-socket-test-bad"; |
| 575 | unsigned char client_bad[kKeyingMaterialSize]; |
[email protected] | 1bc6f5e | 2012-03-15 00:20:58 | [diff] [blame] | 576 | rv = client_socket_->ExportKeyingMaterial(kKeyingLabelBad, |
| 577 | false, kKeyingContext, |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 578 | client_bad, sizeof(client_bad)); |
| 579 | ASSERT_EQ(rv, net::OK); |
[email protected] | 47a1286 | 2012-04-10 01:00:49 | [diff] [blame] | 580 | EXPECT_NE(0, memcmp(server_out, client_bad, sizeof(server_out))); |
[email protected] | b0ff3f8 | 2011-07-23 05:12:39 | [diff] [blame] | 581 | } |
[email protected] | f61c397 | 2010-12-23 09:54:15 | [diff] [blame] | 582 | #endif |
| 583 | |
| 584 | } // namespace net |