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