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