OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 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 |
| 18 #include <queue> |
| 19 |
| 20 #include "base/crypto/rsa_private_key.h" |
| 21 #include "net/base/address_list.h" |
| 22 #include "net/base/cert_verifier.h" |
| 23 #include "net/base/host_port_pair.h" |
| 24 #include "net/base/io_buffer.h" |
| 25 #include "net/base/net_errors.h" |
| 26 #include "net/base/net_log.h" |
| 27 #include "net/base/ssl_config_service.h" |
| 28 #include "net/base/x509_certificate.h" |
| 29 #include "net/socket/client_socket.h" |
| 30 #include "net/socket/client_socket_factory.h" |
| 31 #include "net/socket/socket_test_util.h" |
| 32 #include "net/socket/ssl_client_socket.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" |
| 34 #include "testing/platform_test.h" |
| 35 |
| 36 namespace net { |
| 37 |
| 38 namespace { |
| 39 |
| 40 class FakeDataChannel { |
| 41 public: |
| 42 FakeDataChannel() : read_callback_(NULL), read_buf_len_(0) { |
| 43 } |
| 44 |
| 45 virtual int Read(IOBuffer* buf, int buf_len, |
| 46 CompletionCallback* callback) { |
| 47 if (data_.empty()) { |
| 48 read_callback_ = callback; |
| 49 read_buf_ = buf; |
| 50 read_buf_len_ = buf_len; |
| 51 return net::ERR_IO_PENDING; |
| 52 } |
| 53 return PropogateData(buf, buf_len); |
| 54 } |
| 55 |
| 56 virtual int Write(IOBuffer* buf, int buf_len, |
| 57 CompletionCallback* callback) { |
| 58 data_.push(new net::DrainableIOBuffer(buf, buf_len)); |
| 59 DoReadCallback(); |
| 60 return buf_len; |
| 61 } |
| 62 |
| 63 private: |
| 64 void DoReadCallback() { |
| 65 if (!read_callback_) |
| 66 return; |
| 67 |
| 68 int copied = PropogateData(read_buf_, read_buf_len_); |
| 69 net::CompletionCallback* callback = read_callback_; |
| 70 read_callback_ = NULL; |
| 71 read_buf_ = NULL; |
| 72 read_buf_len_ = 0; |
| 73 callback->Run(copied); |
| 74 } |
| 75 |
| 76 int PropogateData(scoped_refptr<net::IOBuffer> read_buf, int read_buf_len) { |
| 77 scoped_refptr<net::DrainableIOBuffer> buf = data_.front(); |
| 78 int copied = std::min(buf->BytesRemaining(), read_buf_len); |
| 79 memcpy(read_buf->data(), buf->data(), copied); |
| 80 buf->DidConsume(copied); |
| 81 |
| 82 if (!buf->BytesRemaining()) |
| 83 data_.pop(); |
| 84 return copied; |
| 85 } |
| 86 |
| 87 net::CompletionCallback* read_callback_; |
| 88 scoped_refptr<net::IOBuffer> read_buf_; |
| 89 int read_buf_len_; |
| 90 |
| 91 std::queue<scoped_refptr<net::DrainableIOBuffer> > data_; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel); |
| 94 }; |
| 95 |
| 96 class FakeSocket : public ClientSocket { |
| 97 public: |
| 98 FakeSocket(FakeDataChannel* incoming_channel, |
| 99 FakeDataChannel* outgoing_channel) |
| 100 : incoming_(incoming_channel), |
| 101 outgoing_(outgoing_channel) { |
| 102 } |
| 103 |
| 104 virtual ~FakeSocket() { |
| 105 |
| 106 } |
| 107 |
| 108 virtual int Read(IOBuffer* buf, int buf_len, |
| 109 CompletionCallback* callback) { |
| 110 return incoming_->Read(buf, buf_len, callback); |
| 111 } |
| 112 |
| 113 virtual int Write(IOBuffer* buf, int buf_len, |
| 114 CompletionCallback* callback) { |
| 115 return outgoing_->Write(buf, buf_len, callback); |
| 116 } |
| 117 |
| 118 virtual bool SetReceiveBufferSize(int32 size) { |
| 119 return true; |
| 120 } |
| 121 |
| 122 virtual bool SetSendBufferSize(int32 size) { |
| 123 return true; |
| 124 } |
| 125 |
| 126 virtual int Connect(CompletionCallback* callback) { |
| 127 return net::OK; |
| 128 } |
| 129 |
| 130 virtual void Disconnect() {} |
| 131 |
| 132 virtual bool IsConnected() const { |
| 133 return true; |
| 134 } |
| 135 |
| 136 virtual bool IsConnectedAndIdle() const { |
| 137 return true; |
| 138 } |
| 139 |
| 140 virtual int GetPeerAddress(AddressList* address) const { |
| 141 net::IPAddressNumber ip_address(4); |
| 142 *address = net::AddressList(ip_address, 0, false); |
| 143 return net::OK; |
| 144 } |
| 145 |
| 146 virtual const BoundNetLog& NetLog() const { |
| 147 return net_log_; |
| 148 } |
| 149 |
| 150 virtual void SetSubresourceSpeculation() {} |
| 151 virtual void SetOmniboxSpeculation() {} |
| 152 |
| 153 virtual bool WasEverUsed() const { |
| 154 return true; |
| 155 } |
| 156 |
| 157 virtual bool UsingTCPFastOpen() const { |
| 158 return false; |
| 159 } |
| 160 |
| 161 private: |
| 162 net::BoundNetLog net_log_; |
| 163 FakeDataChannel* incoming_; |
| 164 FakeDataChannel* outgoing_; |
| 165 |
| 166 DISALLOW_COPY_AND_ASSIGN(FakeSocket); |
| 167 }; |
| 168 |
| 169 // Serial number for generating the certificate. |
| 170 int g_serial_number = 1; |
| 171 |
| 172 } // namespace |
| 173 |
| 174 // Verify the correctness of the test helper classes first. |
| 175 TEST(FakeSocketTest, DataTransfer) { |
| 176 // Establish channels between two sockets. |
| 177 FakeDataChannel channel_1; |
| 178 FakeDataChannel channel_2; |
| 179 FakeSocket client(&channel_1, &channel_2); |
| 180 FakeSocket server(&channel_2, &channel_1); |
| 181 |
| 182 const char kTestData[] = "testing123"; |
| 183 const int kTestDataSize = strlen(kTestData); |
| 184 const int kReadBufSize = 1024; |
| 185 scoped_refptr<net::IOBuffer> write_buf = new net::StringIOBuffer(kTestData); |
| 186 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize); |
| 187 |
| 188 // Write then read. |
| 189 EXPECT_EQ(kTestDataSize, server.Write(write_buf, kTestDataSize, NULL)); |
| 190 EXPECT_EQ(kTestDataSize, client.Read(read_buf, kReadBufSize, NULL)); |
| 191 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), kTestDataSize)); |
| 192 |
| 193 // Read then write. |
| 194 TestCompletionCallback callback; |
| 195 EXPECT_EQ(net::ERR_IO_PENDING, |
| 196 server.Read(read_buf, kReadBufSize, &callback)); |
| 197 EXPECT_EQ(kTestDataSize, client.Write(write_buf, kTestDataSize, NULL)); |
| 198 EXPECT_EQ(kTestDataSize, callback.WaitForResult()); |
| 199 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), kTestDataSize)); |
| 200 } |
| 201 |
| 202 class SSLServerSocketTest : public PlatformTest { |
| 203 public: |
| 204 SSLServerSocketTest() |
| 205 : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) { |
| 206 } |
| 207 |
| 208 protected: |
| 209 void Initialize() { |
| 210 FakeSocket* fake_client_socket = new FakeSocket(&channel_1_, &channel_2_); |
| 211 FakeSocket* fake_server_socket = new FakeSocket(&channel_2_, &channel_1_); |
| 212 |
| 213 // Generate private key and certificate. |
| 214 scoped_ptr<base::RSAPrivateKey> private_key( |
| 215 base::RSAPrivateKey::Create(1024)); |
| 216 |
| 217 std::string subject = "CN=net-unittest"; |
| 218 scoped_refptr<net::X509Certificate> cert = |
| 219 net::X509Certificate::CreateSelfSigned( |
| 220 private_key.get(), subject, g_serial_number++, |
| 221 base::TimeDelta::FromDays(1)); |
| 222 |
| 223 net::SSLConfig ssl_config; |
| 224 ssl_config.false_start_enabled = false; |
| 225 ssl_config.snap_start_enabled = false; |
| 226 ssl_config.ssl3_enabled = true; |
| 227 ssl_config.tls1_enabled = true; |
| 228 ssl_config.session_resume_disabled = true; |
| 229 |
| 230 // Certificate provided by the host doesn't need authority. |
| 231 net::SSLConfig::CertAndStatus cert_and_status; |
| 232 cert_and_status.cert_status = net::ERR_CERT_AUTHORITY_INVALID; |
| 233 cert_and_status.cert = cert; |
| 234 ssl_config.allowed_bad_certs.push_back(cert_and_status); |
| 235 |
| 236 net::HostPortPair host_and_pair("net-unittest", 0); |
| 237 client_socket_.reset( |
| 238 socket_factory_->CreateSSLClientSocket( |
| 239 fake_client_socket, host_and_pair, ssl_config, NULL, |
| 240 &cert_verifier_)); |
| 241 server_socket_.reset(net::CreateSSLServerSocket(fake_server_socket, |
| 242 cert, private_key.get(), |
| 243 net::SSLConfig())); |
| 244 } |
| 245 |
| 246 FakeDataChannel channel_1_; |
| 247 FakeDataChannel channel_2_; |
| 248 scoped_ptr<net::SSLClientSocket> client_socket_; |
| 249 scoped_ptr<net::SSLServerSocket> server_socket_; |
| 250 net::ClientSocketFactory* socket_factory_; |
| 251 net::CertVerifier cert_verifier_; |
| 252 }; |
| 253 |
| 254 // SSLServerSocket is only implemented using NSS. |
| 255 #if defined(USE_NSS) |
| 256 |
| 257 // This test only executes creation of client and server sockets. This is to |
| 258 // test that creation of sockets doesn't crash and have minimal code to run |
| 259 // under valgrind in order to help debugging memory problems. |
| 260 TEST_F(SSLServerSocketTest, Initialize) { |
| 261 Initialize(); |
| 262 } |
| 263 |
| 264 // This test executes Connect() of SSLClientSocket and Accept() of |
| 265 // SSLServerSocket to make sure handshaking between the two sockets are |
| 266 // completed successfully. |
| 267 TEST_F(SSLServerSocketTest, Handshake) { |
| 268 Initialize(); |
| 269 |
| 270 TestCompletionCallback connect_callback; |
| 271 TestCompletionCallback accept_callback; |
| 272 |
| 273 int client_ret = client_socket_->Connect(&connect_callback); |
| 274 EXPECT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING); |
| 275 |
| 276 int server_ret = server_socket_->Accept(&accept_callback); |
| 277 EXPECT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING); |
| 278 |
| 279 if (client_ret == net::ERR_IO_PENDING) { |
| 280 EXPECT_EQ(net::OK, connect_callback.WaitForResult()); |
| 281 } |
| 282 if (server_ret == net::ERR_IO_PENDING) { |
| 283 EXPECT_EQ(net::OK, accept_callback.WaitForResult()); |
| 284 } |
| 285 } |
| 286 |
| 287 TEST_F(SSLServerSocketTest, DataTransfer) { |
| 288 Initialize(); |
| 289 |
| 290 TestCompletionCallback connect_callback; |
| 291 TestCompletionCallback accept_callback; |
| 292 |
| 293 // Establish connection. |
| 294 int client_ret = client_socket_->Connect(&connect_callback); |
| 295 EXPECT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING); |
| 296 |
| 297 int server_ret = server_socket_->Accept(&accept_callback); |
| 298 EXPECT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING); |
| 299 |
| 300 if (client_ret == net::ERR_IO_PENDING) { |
| 301 EXPECT_EQ(net::OK, connect_callback.WaitForResult()); |
| 302 } |
| 303 if (server_ret == net::ERR_IO_PENDING) { |
| 304 EXPECT_EQ(net::OK, accept_callback.WaitForResult()); |
| 305 } |
| 306 |
| 307 const int kReadBufSize = 1024; |
| 308 scoped_refptr<net::StringIOBuffer> write_buf = |
| 309 new net::StringIOBuffer("testing123"); |
| 310 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize); |
| 311 |
| 312 // Write then read. |
| 313 TestCompletionCallback write_callback; |
| 314 TestCompletionCallback read_callback; |
| 315 server_ret = server_socket_->Write(write_buf, write_buf->size(), |
| 316 &write_callback); |
| 317 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING); |
| 318 client_ret = client_socket_->Read(read_buf, kReadBufSize, &read_callback); |
| 319 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING); |
| 320 |
| 321 if (server_ret == net::ERR_IO_PENDING) { |
| 322 EXPECT_GT(write_callback.WaitForResult(), 0); |
| 323 } |
| 324 if (client_ret == net::ERR_IO_PENDING) { |
| 325 EXPECT_GT(read_callback.WaitForResult(), 0); |
| 326 } |
| 327 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size())); |
| 328 |
| 329 // Read then write. |
| 330 write_buf = new net::StringIOBuffer("hello123"); |
| 331 server_ret = server_socket_->Read(read_buf, kReadBufSize, &read_callback); |
| 332 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING); |
| 333 client_ret = client_socket_->Write(write_buf, write_buf->size(), |
| 334 &write_callback); |
| 335 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING); |
| 336 |
| 337 if (server_ret == net::ERR_IO_PENDING) { |
| 338 EXPECT_GT(read_callback.WaitForResult(), 0); |
| 339 } |
| 340 if (client_ret == net::ERR_IO_PENDING) { |
| 341 EXPECT_GT(write_callback.WaitForResult(), 0); |
| 342 } |
| 343 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size())); |
| 344 } |
| 345 #endif |
| 346 |
| 347 } // namespace net |
OLD | NEW |