blob: 1608b9ffe8a10a227917f0667055406e0ab38ba8 [file] [log] [blame]
[email protected]1bc6f5e2012-03-15 00:20:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f61c3972010-12-23 09:54:152// 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]55ee0e52011-07-21 18:29:4418#include <stdlib.h>
19
[email protected]f61c3972010-12-23 09:54:1520#include <queue>
21
[email protected]55ee0e52011-07-21 18:29:4422#include "base/compiler_specific.h"
[email protected]f61c3972010-12-23 09:54:1523#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5224#include "base/files/file_path.h"
[email protected]55ee0e52011-07-21 18:29:4425#include "base/message_loop.h"
[email protected]f61c3972010-12-23 09:54:1526#include "base/path_service.h"
[email protected]4b559b4d2011-04-14 17:37:1427#include "crypto/nss_util.h"
28#include "crypto/rsa_private_key.h"
[email protected]f61c3972010-12-23 09:54:1529#include "net/base/address_list.h"
[email protected]bd707a62011-03-14 23:29:3430#include "net/base/cert_status_flags.h"
[email protected]a8e0ab9d2012-03-31 01:09:4431#include "net/base/cert_test_util.h"
[email protected]6ea7b152011-12-21 21:21:1332#include "net/base/completion_callback.h"
[email protected]f61c3972010-12-23 09:54:1533#include "net/base/host_port_pair.h"
34#include "net/base/io_buffer.h"
[email protected]e7f74da2011-04-19 23:49:3535#include "net/base/ip_endpoint.h"
[email protected]47a12862012-04-10 01:00:4936#include "net/base/mock_cert_verifier.h"
[email protected]f61c3972010-12-23 09:54:1537#include "net/base/net_errors.h"
38#include "net/base/net_log.h"
39#include "net/base/ssl_config_service.h"
[email protected]4dc832e2011-04-28 22:04:2440#include "net/base/ssl_info.h"
[email protected]42fdb452012-11-01 12:44:4041#include "net/base/test_data_directory.h"
[email protected]f61c3972010-12-23 09:54:1542#include "net/base/x509_certificate.h"
[email protected]f61c3972010-12-23 09:54:1543#include "net/socket/client_socket_factory.h"
44#include "net/socket/socket_test_util.h"
45#include "net/socket/ssl_client_socket.h"
[email protected]3268023f2011-05-05 00:08:1046#include "net/socket/stream_socket.h"
[email protected]f61c3972010-12-23 09:54:1547#include "testing/gtest/include/gtest/gtest.h"
48#include "testing/platform_test.h"
49
50namespace net {
51
52namespace {
53
54class FakeDataChannel {
55 public:
[email protected]55ee0e52011-07-21 18:29:4456 FakeDataChannel()
[email protected]83039bb2011-12-09 18:43:5557 : read_buf_len_(0),
[email protected]c0e4dd12012-05-16 19:36:3158 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
59 closed_(false),
60 write_called_after_close_(false) {
[email protected]f61c3972010-12-23 09:54:1561 }
62
[email protected]47a12862012-04-10 01:00:4963 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
[email protected]c0e4dd12012-05-16 19:36:3164 if (closed_)
65 return 0;
[email protected]3f55aa12011-12-07 02:03:3366 if (data_.empty()) {
[email protected]f61c3972010-12-23 09:54:1567 read_callback_ = callback;
68 read_buf_ = buf;
69 read_buf_len_ = buf_len;
70 return net::ERR_IO_PENDING;
71 }
72 return PropogateData(buf, buf_len);
73 }
74
[email protected]47a12862012-04-10 01:00:4975 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
[email protected]c0e4dd12012-05-16 19:36:3176 if (closed_) {
77 if (write_called_after_close_)
78 return net::ERR_CONNECTION_RESET;
79 write_called_after_close_ = true;
80 write_callback_ = callback;
81 MessageLoop::current()->PostTask(
82 FROM_HERE, base::Bind(&FakeDataChannel::DoWriteCallback,
83 weak_factory_.GetWeakPtr()));
84 return net::ERR_IO_PENDING;
85 }
[email protected]f61c3972010-12-23 09:54:1586 data_.push(new net::DrainableIOBuffer(buf, buf_len));
[email protected]55ee0e52011-07-21 18:29:4487 MessageLoop::current()->PostTask(
[email protected]83039bb2011-12-09 18:43:5588 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback,
89 weak_factory_.GetWeakPtr()));
[email protected]f61c3972010-12-23 09:54:1590 return buf_len;
91 }
92
[email protected]c0e4dd12012-05-16 19:36:3193 // Closes the FakeDataChannel. After Close() is called, Read() returns 0,
94 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that
95 // after the FakeDataChannel is closed, the first Write() call completes
96 // asynchronously, which is necessary to reproduce bug 127822.
97 void Close() {
98 closed_ = true;
99 }
100
[email protected]f61c3972010-12-23 09:54:15101 private:
102 void DoReadCallback() {
[email protected]83039bb2011-12-09 18:43:55103 if (read_callback_.is_null() || data_.empty())
[email protected]f61c3972010-12-23 09:54:15104 return;
105
106 int copied = PropogateData(read_buf_, read_buf_len_);
[email protected]83039bb2011-12-09 18:43:55107 CompletionCallback callback = read_callback_;
108 read_callback_.Reset();
109 read_buf_ = NULL;
110 read_buf_len_ = 0;
111 callback.Run(copied);
[email protected]f61c3972010-12-23 09:54:15112 }
113
[email protected]c0e4dd12012-05-16 19:36:31114 void DoWriteCallback() {
115 if (write_callback_.is_null())
116 return;
117
118 CompletionCallback callback = write_callback_;
119 write_callback_.Reset();
120 callback.Run(net::ERR_CONNECTION_RESET);
121 }
122
[email protected]f61c3972010-12-23 09:54:15123 int PropogateData(scoped_refptr<net::IOBuffer> read_buf, int read_buf_len) {
124 scoped_refptr<net::DrainableIOBuffer> buf = data_.front();
125 int copied = std::min(buf->BytesRemaining(), read_buf_len);
126 memcpy(read_buf->data(), buf->data(), copied);
127 buf->DidConsume(copied);
128
129 if (!buf->BytesRemaining())
130 data_.pop();
131 return copied;
132 }
133
[email protected]83039bb2011-12-09 18:43:55134 CompletionCallback read_callback_;
[email protected]f61c3972010-12-23 09:54:15135 scoped_refptr<net::IOBuffer> read_buf_;
136 int read_buf_len_;
137
[email protected]c0e4dd12012-05-16 19:36:31138 CompletionCallback write_callback_;
139
[email protected]f61c3972010-12-23 09:54:15140 std::queue<scoped_refptr<net::DrainableIOBuffer> > data_;
141
[email protected]83039bb2011-12-09 18:43:55142 base::WeakPtrFactory<FakeDataChannel> weak_factory_;
[email protected]55ee0e52011-07-21 18:29:44143
[email protected]c0e4dd12012-05-16 19:36:31144 // True if Close() has been called.
145 bool closed_;
146
147 // Controls the completion of Write() after the FakeDataChannel is closed.
148 // After the FakeDataChannel is closed, the first Write() call completes
149 // asynchronously.
150 bool write_called_after_close_;
151
[email protected]f61c3972010-12-23 09:54:15152 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel);
153};
154
[email protected]3268023f2011-05-05 00:08:10155class FakeSocket : public StreamSocket {
[email protected]f61c3972010-12-23 09:54:15156 public:
157 FakeSocket(FakeDataChannel* incoming_channel,
158 FakeDataChannel* outgoing_channel)
159 : incoming_(incoming_channel),
160 outgoing_(outgoing_channel) {
161 }
162
163 virtual ~FakeSocket() {
[email protected]f61c3972010-12-23 09:54:15164 }
165
166 virtual int Read(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55167 const CompletionCallback& callback) OVERRIDE {
[email protected]3f55aa12011-12-07 02:03:33168 // Read random number of bytes.
169 buf_len = rand() % buf_len + 1;
170 return incoming_->Read(buf, buf_len, callback);
171 }
[email protected]f61c3972010-12-23 09:54:15172
173 virtual int Write(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55174 const CompletionCallback& callback) OVERRIDE {
[email protected]55ee0e52011-07-21 18:29:44175 // Write random number of bytes.
176 buf_len = rand() % buf_len + 1;
[email protected]f61c3972010-12-23 09:54:15177 return outgoing_->Write(buf, buf_len, callback);
178 }
179
[email protected]83039bb2011-12-09 18:43:55180 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15181 return true;
182 }
183
[email protected]83039bb2011-12-09 18:43:55184 virtual bool SetSendBufferSize(int32 size) OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15185 return true;
186 }
187
[email protected]83039bb2011-12-09 18:43:55188 virtual int Connect(const CompletionCallback& callback) OVERRIDE {
[email protected]dbf036f2011-12-06 23:33:24189 return net::OK;
190 }
[email protected]f61c3972010-12-23 09:54:15191
[email protected]c0e4dd12012-05-16 19:36:31192 virtual void Disconnect() OVERRIDE {
193 incoming_->Close();
194 outgoing_->Close();
195 }
[email protected]f61c3972010-12-23 09:54:15196
[email protected]83039bb2011-12-09 18:43:55197 virtual bool IsConnected() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15198 return true;
199 }
200
[email protected]83039bb2011-12-09 18:43:55201 virtual bool IsConnectedAndIdle() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15202 return true;
203 }
204
[email protected]a3528692012-06-08 00:11:42205 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE {
206 net::IPAddressNumber ip_address(net::kIPv4AddressSize);
207 *address = net::IPEndPoint(ip_address, 0 /*port*/);
[email protected]f61c3972010-12-23 09:54:15208 return net::OK;
209 }
210
[email protected]83039bb2011-12-09 18:43:55211 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE {
[email protected]e7f74da2011-04-19 23:49:35212 net::IPAddressNumber ip_address(4);
213 *address = net::IPEndPoint(ip_address, 0);
214 return net::OK;
215 }
216
[email protected]83039bb2011-12-09 18:43:55217 virtual const BoundNetLog& NetLog() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15218 return net_log_;
219 }
220
[email protected]83039bb2011-12-09 18:43:55221 virtual void SetSubresourceSpeculation() OVERRIDE {}
222 virtual void SetOmniboxSpeculation() OVERRIDE {}
[email protected]f61c3972010-12-23 09:54:15223
[email protected]83039bb2011-12-09 18:43:55224 virtual bool WasEverUsed() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15225 return true;
226 }
227
[email protected]83039bb2011-12-09 18:43:55228 virtual bool UsingTCPFastOpen() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15229 return false;
230 }
231
[email protected]83039bb2011-12-09 18:43:55232 virtual int64 NumBytesRead() const OVERRIDE {
[email protected]5e6efa52011-06-27 17:26:41233 return -1;
234 }
235
[email protected]83039bb2011-12-09 18:43:55236 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE {
[email protected]5e6efa52011-06-27 17:26:41237 return base::TimeDelta::FromMicroseconds(-1);
238 }
239
[email protected]46fadfd2013-02-06 09:40:16240 virtual bool WasNpnNegotiated() const OVERRIDE {
[email protected]2d88e7d2012-07-19 17:55:17241 return false;
242 }
243
[email protected]46fadfd2013-02-06 09:40:16244 virtual NextProto GetNegotiatedProtocol() const OVERRIDE {
[email protected]33661e482012-04-03 16:16:26245 return kProtoUnknown;
246 }
247
[email protected]46fadfd2013-02-06 09:40:16248 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE {
[email protected]2d88e7d2012-07-19 17:55:17249 return false;
250 }
251
[email protected]f61c3972010-12-23 09:54:15252 private:
253 net::BoundNetLog net_log_;
254 FakeDataChannel* incoming_;
255 FakeDataChannel* outgoing_;
256
257 DISALLOW_COPY_AND_ASSIGN(FakeSocket);
258};
259
260} // namespace
261
262// Verify the correctness of the test helper classes first.
263TEST(FakeSocketTest, DataTransfer) {
264 // Establish channels between two sockets.
265 FakeDataChannel channel_1;
266 FakeDataChannel channel_2;
267 FakeSocket client(&channel_1, &channel_2);
268 FakeSocket server(&channel_2, &channel_1);
269
270 const char kTestData[] = "testing123";
271 const int kTestDataSize = strlen(kTestData);
272 const int kReadBufSize = 1024;
273 scoped_refptr<net::IOBuffer> write_buf = new net::StringIOBuffer(kTestData);
274 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize);
275
276 // Write then read.
[email protected]83039bb2011-12-09 18:43:55277 int written = server.Write(write_buf, kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44278 EXPECT_GT(written, 0);
279 EXPECT_LE(written, kTestDataSize);
280
[email protected]83039bb2011-12-09 18:43:55281 int read = client.Read(read_buf, kReadBufSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44282 EXPECT_GT(read, 0);
283 EXPECT_LE(read, written);
284 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15285
286 // Read then write.
[email protected]83039bb2011-12-09 18:43:55287 TestCompletionCallback callback;
[email protected]f61c3972010-12-23 09:54:15288 EXPECT_EQ(net::ERR_IO_PENDING,
[email protected]83039bb2011-12-09 18:43:55289 server.Read(read_buf, kReadBufSize, callback.callback()));
[email protected]55ee0e52011-07-21 18:29:44290
[email protected]83039bb2011-12-09 18:43:55291 written = client.Write(write_buf, kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44292 EXPECT_GT(written, 0);
293 EXPECT_LE(written, kTestDataSize);
294
295 read = callback.WaitForResult();
296 EXPECT_GT(read, 0);
297 EXPECT_LE(read, written);
298 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15299}
300
301class SSLServerSocketTest : public PlatformTest {
302 public:
303 SSLServerSocketTest()
[email protected]9f59fac2012-03-21 23:18:11304 : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()),
[email protected]47a12862012-04-10 01:00:49305 cert_verifier_(new MockCertVerifier()) {
306 cert_verifier_->set_default_result(net::CERT_STATUS_AUTHORITY_INVALID);
[email protected]f61c3972010-12-23 09:54:15307 }
308
309 protected:
310 void Initialize() {
311 FakeSocket* fake_client_socket = new FakeSocket(&channel_1_, &channel_2_);
312 FakeSocket* fake_server_socket = new FakeSocket(&channel_2_, &channel_1_);
313
[email protected]6cdfd7f2013-02-08 20:40:15314 base::FilePath certs_dir(GetTestCertsDirectory());
[email protected]f61c3972010-12-23 09:54:15315
[email protected]6cdfd7f2013-02-08 20:40:15316 base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der");
[email protected]f61c3972010-12-23 09:54:15317 std::string cert_der;
318 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_der));
319
320 scoped_refptr<net::X509Certificate> cert =
321 X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size());
322
[email protected]6cdfd7f2013-02-08 20:40:15323 base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
[email protected]f61c3972010-12-23 09:54:15324 std::string key_string;
325 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string));
326 std::vector<uint8> key_vector(
327 reinterpret_cast<const uint8*>(key_string.data()),
328 reinterpret_cast<const uint8*>(key_string.data() +
329 key_string.length()));
330
[email protected]4b559b4d2011-04-14 17:37:14331 scoped_ptr<crypto::RSAPrivateKey> private_key(
332 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector));
[email protected]f61c3972010-12-23 09:54:15333
334 net::SSLConfig ssl_config;
[email protected]2fb7e3ba2011-06-22 19:24:38335 ssl_config.cached_info_enabled = false;
[email protected]f61c3972010-12-23 09:54:15336 ssl_config.false_start_enabled = false;
[email protected]6b4903f2012-06-26 02:13:49337 ssl_config.channel_id_enabled = false;
[email protected]80c75f682012-05-26 16:22:17338 ssl_config.version_min = SSL_PROTOCOL_VERSION_SSL3;
[email protected]21a1a182012-08-20 22:18:23339 ssl_config.version_max = SSL_PROTOCOL_VERSION_TLS1_1;
[email protected]f61c3972010-12-23 09:54:15340
341 // Certificate provided by the host doesn't need authority.
342 net::SSLConfig::CertAndStatus cert_and_status;
[email protected]4dc832e2011-04-28 22:04:24343 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID;
[email protected]3d5c1bd2011-07-20 02:14:01344 cert_and_status.der_cert = cert_der;
[email protected]f61c3972010-12-23 09:54:15345 ssl_config.allowed_bad_certs.push_back(cert_and_status);
346
347 net::HostPortPair host_and_pair("unittest", 0);
[email protected]feb79bcd2011-07-21 16:55:17348 net::SSLClientSocketContext context;
[email protected]9f59fac2012-03-21 23:18:11349 context.cert_verifier = cert_verifier_.get();
[email protected]f61c3972010-12-23 09:54:15350 client_socket_.reset(
351 socket_factory_->CreateSSLClientSocket(
[email protected]efe222152012-06-27 16:48:46352 fake_client_socket, host_and_pair, ssl_config, context));
[email protected]f61c3972010-12-23 09:54:15353 server_socket_.reset(net::CreateSSLServerSocket(fake_server_socket,
354 cert, private_key.get(),
355 net::SSLConfig()));
356 }
357
358 FakeDataChannel channel_1_;
359 FakeDataChannel channel_2_;
360 scoped_ptr<net::SSLClientSocket> client_socket_;
361 scoped_ptr<net::SSLServerSocket> server_socket_;
362 net::ClientSocketFactory* socket_factory_;
[email protected]47a12862012-04-10 01:00:49363 scoped_ptr<net::MockCertVerifier> cert_verifier_;
[email protected]f61c3972010-12-23 09:54:15364};
365
366// SSLServerSocket is only implemented using NSS.
367#if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX)
368
369// This test only executes creation of client and server sockets. This is to
370// test that creation of sockets doesn't crash and have minimal code to run
371// under valgrind in order to help debugging memory problems.
372TEST_F(SSLServerSocketTest, Initialize) {
373 Initialize();
374}
375
[email protected]a7ac3c32011-06-17 19:10:15376// This test executes Connect() on SSLClientSocket and Handshake() on
377// SSLServerSocket to make sure handshaking between the two sockets is
[email protected]f61c3972010-12-23 09:54:15378// completed successfully.
379TEST_F(SSLServerSocketTest, Handshake) {
380 Initialize();
381
[email protected]83039bb2011-12-09 18:43:55382 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13383 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15384
[email protected]6ea7b152011-12-21 21:21:13385 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]f61c3972010-12-23 09:54:15386 EXPECT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
387
[email protected]83039bb2011-12-09 18:43:55388 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]f61c3972010-12-23 09:54:15389 EXPECT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
390
391 if (client_ret == net::ERR_IO_PENDING) {
392 EXPECT_EQ(net::OK, connect_callback.WaitForResult());
393 }
394 if (server_ret == net::ERR_IO_PENDING) {
[email protected]b0ff3f82011-07-23 05:12:39395 EXPECT_EQ(net::OK, handshake_callback.WaitForResult());
[email protected]f61c3972010-12-23 09:54:15396 }
[email protected]4dc832e2011-04-28 22:04:24397
398 // Make sure the cert status is expected.
399 SSLInfo ssl_info;
400 client_socket_->GetSSLInfo(&ssl_info);
401 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status);
[email protected]f61c3972010-12-23 09:54:15402}
403
404TEST_F(SSLServerSocketTest, DataTransfer) {
405 Initialize();
406
[email protected]83039bb2011-12-09 18:43:55407 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13408 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15409
410 // Establish connection.
[email protected]83039bb2011-12-09 18:43:55411 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]302b6272011-01-19 01:27:22412 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15413
[email protected]6ea7b152011-12-21 21:21:13414 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]302b6272011-01-19 01:27:22415 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15416
[email protected]febbbb52011-08-17 04:59:23417 client_ret = connect_callback.GetResult(client_ret);
418 ASSERT_EQ(net::OK, client_ret);
419 server_ret = handshake_callback.GetResult(server_ret);
420 ASSERT_EQ(net::OK, server_ret);
[email protected]f61c3972010-12-23 09:54:15421
422 const int kReadBufSize = 1024;
423 scoped_refptr<net::StringIOBuffer> write_buf =
424 new net::StringIOBuffer("testing123");
[email protected]febbbb52011-08-17 04:59:23425 scoped_refptr<net::DrainableIOBuffer> read_buf =
426 new net::DrainableIOBuffer(new net::IOBuffer(kReadBufSize),
427 kReadBufSize);
[email protected]f61c3972010-12-23 09:54:15428
429 // Write then read.
[email protected]83039bb2011-12-09 18:43:55430 TestCompletionCallback write_callback;
431 TestCompletionCallback read_callback;
[email protected]f61c3972010-12-23 09:54:15432 server_ret = server_socket_->Write(write_buf, write_buf->size(),
[email protected]83039bb2011-12-09 18:43:55433 write_callback.callback());
[email protected]f61c3972010-12-23 09:54:15434 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
[email protected]febbbb52011-08-17 04:59:23435 client_ret = client_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55436 read_callback.callback());
[email protected]f61c3972010-12-23 09:54:15437 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
438
[email protected]febbbb52011-08-17 04:59:23439 server_ret = write_callback.GetResult(server_ret);
440 EXPECT_GT(server_ret, 0);
441 client_ret = read_callback.GetResult(client_ret);
442 ASSERT_GT(client_ret, 0);
443
444 read_buf->DidConsume(client_ret);
445 while (read_buf->BytesConsumed() < write_buf->size()) {
446 client_ret = client_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55447 read_callback.callback());
[email protected]febbbb52011-08-17 04:59:23448 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
449 client_ret = read_callback.GetResult(client_ret);
450 ASSERT_GT(client_ret, 0);
451 read_buf->DidConsume(client_ret);
[email protected]f61c3972010-12-23 09:54:15452 }
[email protected]febbbb52011-08-17 04:59:23453 EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed());
454 read_buf->SetOffset(0);
[email protected]f61c3972010-12-23 09:54:15455 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
456
457 // Read then write.
458 write_buf = new net::StringIOBuffer("hello123");
[email protected]febbbb52011-08-17 04:59:23459 server_ret = server_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55460 read_callback.callback());
[email protected]f61c3972010-12-23 09:54:15461 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
462 client_ret = client_socket_->Write(write_buf, write_buf->size(),
[email protected]83039bb2011-12-09 18:43:55463 write_callback.callback());
[email protected]f61c3972010-12-23 09:54:15464 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
465
[email protected]febbbb52011-08-17 04:59:23466 server_ret = read_callback.GetResult(server_ret);
467 ASSERT_GT(server_ret, 0);
468 client_ret = write_callback.GetResult(client_ret);
469 EXPECT_GT(client_ret, 0);
470
471 read_buf->DidConsume(server_ret);
472 while (read_buf->BytesConsumed() < write_buf->size()) {
473 server_ret = server_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55474 read_callback.callback());
[email protected]febbbb52011-08-17 04:59:23475 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
476 server_ret = read_callback.GetResult(server_ret);
477 ASSERT_GT(server_ret, 0);
478 read_buf->DidConsume(server_ret);
[email protected]f61c3972010-12-23 09:54:15479 }
[email protected]febbbb52011-08-17 04:59:23480 EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed());
481 read_buf->SetOffset(0);
[email protected]f61c3972010-12-23 09:54:15482 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
483}
[email protected]b0ff3f82011-07-23 05:12:39484
[email protected]c0e4dd12012-05-16 19:36:31485// 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.
489TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) {
490 Initialize();
491
492 TestCompletionCallback connect_callback;
493 TestCompletionCallback handshake_callback;
494
495 // Establish connection.
496 int client_ret = client_socket_->Connect(connect_callback.callback());
497 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
498
499 int server_ret = server_socket_->Handshake(handshake_callback.callback());
500 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
501
502 client_ret = connect_callback.GetResult(client_ret);
503 ASSERT_EQ(net::OK, client_ret);
504 server_ret = handshake_callback.GetResult(server_ret);
505 ASSERT_EQ(net::OK, server_ret);
506
507 scoped_refptr<net::StringIOBuffer> write_buf =
508 new net::StringIOBuffer("testing123");
509
510 // The server closes the connection. The server needs to write some
511 // data first so that the client's Read() calls from the transport
512 // socket won't return ERR_IO_PENDING. This ensures that the client
513 // will call Read() on the transport socket again.
514 TestCompletionCallback write_callback;
515
516 server_ret = server_socket_->Write(write_buf, write_buf->size(),
517 write_callback.callback());
518 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
519
520 server_ret = write_callback.GetResult(server_ret);
521 EXPECT_GT(server_ret, 0);
522
523 server_socket_->Disconnect();
524
525 // The client writes some data. This should not cause an infinite loop.
526 client_ret = client_socket_->Write(write_buf, write_buf->size(),
527 write_callback.callback());
528 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
529
530 client_ret = write_callback.GetResult(client_ret);
531 EXPECT_GT(client_ret, 0);
532
533 MessageLoop::current()->PostDelayedTask(
534 FROM_HERE, MessageLoop::QuitClosure(),
535 base::TimeDelta::FromMilliseconds(10));
536 MessageLoop::current()->Run();
537}
538
[email protected]b0ff3f82011-07-23 05:12:39539// This test executes ExportKeyingMaterial() on the client and server sockets,
540// after connecting them, and verifies that the results match.
541// This test will fail if False Start is enabled (see crbug.com/90208).
542TEST_F(SSLServerSocketTest, ExportKeyingMaterial) {
543 Initialize();
544
[email protected]83039bb2011-12-09 18:43:55545 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13546 TestCompletionCallback handshake_callback;
[email protected]b0ff3f82011-07-23 05:12:39547
[email protected]83039bb2011-12-09 18:43:55548 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]b0ff3f82011-07-23 05:12:39549 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
550
[email protected]6ea7b152011-12-21 21:21:13551 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]b0ff3f82011-07-23 05:12:39552 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
553
554 if (client_ret == net::ERR_IO_PENDING) {
555 ASSERT_EQ(net::OK, connect_callback.WaitForResult());
556 }
557 if (server_ret == net::ERR_IO_PENDING) {
558 ASSERT_EQ(net::OK, handshake_callback.WaitForResult());
559 }
560
561 const int kKeyingMaterialSize = 32;
562 const char* kKeyingLabel = "EXPERIMENTAL-server-socket-test";
563 const char* kKeyingContext = "";
564 unsigned char server_out[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58565 int rv = server_socket_->ExportKeyingMaterial(kKeyingLabel,
566 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39567 server_out, sizeof(server_out));
[email protected]47a12862012-04-10 01:00:49568 ASSERT_EQ(net::OK, rv);
[email protected]b0ff3f82011-07-23 05:12:39569
570 unsigned char client_out[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58571 rv = client_socket_->ExportKeyingMaterial(kKeyingLabel,
572 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39573 client_out, sizeof(client_out));
[email protected]47a12862012-04-10 01:00:49574 ASSERT_EQ(net::OK, rv);
575 EXPECT_EQ(0, memcmp(server_out, client_out, sizeof(server_out)));
[email protected]b0ff3f82011-07-23 05:12:39576
577 const char* kKeyingLabelBad = "EXPERIMENTAL-server-socket-test-bad";
578 unsigned char client_bad[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58579 rv = client_socket_->ExportKeyingMaterial(kKeyingLabelBad,
580 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39581 client_bad, sizeof(client_bad));
582 ASSERT_EQ(rv, net::OK);
[email protected]47a12862012-04-10 01:00:49583 EXPECT_NE(0, memcmp(server_out, client_bad, sizeof(server_out)));
[email protected]b0ff3f82011-07-23 05:12:39584}
[email protected]f61c3972010-12-23 09:54:15585#endif
586
587} // namespace net