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