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