blob: 959d6b3fc0da30d2389ebd020c4e927960af9a56 [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]6ea7b152011-12-21 21:21:1330#include "net/base/completion_callback.h"
[email protected]f61c3972010-12-23 09:54:1531#include "net/base/host_port_pair.h"
32#include "net/base/io_buffer.h"
[email protected]e7f74da2011-04-19 23:49:3533#include "net/base/ip_endpoint.h"
[email protected]f61c3972010-12-23 09:54:1534#include "net/base/net_errors.h"
35#include "net/base/net_log.h"
[email protected]42fdb452012-11-01 12:44:4036#include "net/base/test_data_directory.h"
[email protected]6e7845ae2013-03-29 21:48:1137#include "net/cert/cert_status_flags.h"
38#include "net/cert/mock_cert_verifier.h"
39#include "net/cert/x509_certificate.h"
[email protected]b1c988b2013-06-13 06:48:1140#include "net/http/transport_security_state.h"
[email protected]f61c3972010-12-23 09:54:1541#include "net/socket/client_socket_factory.h"
42#include "net/socket/socket_test_util.h"
43#include "net/socket/ssl_client_socket.h"
[email protected]3268023f2011-05-05 00:08:1044#include "net/socket/stream_socket.h"
[email protected]536fd0b2013-03-14 17:41:5745#include "net/ssl/ssl_config_service.h"
46#include "net/ssl/ssl_info.h"
[email protected]6e7845ae2013-03-29 21:48:1147#include "net/test/cert_test_util.h"
[email protected]f61c3972010-12-23 09:54:1548#include "testing/gtest/include/gtest/gtest.h"
49#include "testing/platform_test.h"
50
51namespace net {
52
53namespace {
54
55class FakeDataChannel {
56 public:
[email protected]55ee0e52011-07-21 18:29:4457 FakeDataChannel()
[email protected]83039bb2011-12-09 18:43:5558 : read_buf_len_(0),
[email protected]aa249b52013-04-30 01:04:3259 weak_factory_(this),
[email protected]c0e4dd12012-05-16 19:36:3160 closed_(false),
61 write_called_after_close_(false) {
[email protected]f61c3972010-12-23 09:54:1562 }
63
[email protected]47a12862012-04-10 01:00:4964 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
[email protected]c0e4dd12012-05-16 19:36:3165 if (closed_)
66 return 0;
[email protected]3f55aa12011-12-07 02:03:3367 if (data_.empty()) {
[email protected]f61c3972010-12-23 09:54:1568 read_callback_ = callback;
69 read_buf_ = buf;
70 read_buf_len_ = buf_len;
71 return net::ERR_IO_PENDING;
72 }
73 return PropogateData(buf, buf_len);
74 }
75
[email protected]47a12862012-04-10 01:00:4976 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
[email protected]c0e4dd12012-05-16 19:36:3177 if (closed_) {
78 if (write_called_after_close_)
79 return net::ERR_CONNECTION_RESET;
80 write_called_after_close_ = true;
81 write_callback_ = callback;
[email protected]2da659e2013-05-23 20:51:3482 base::MessageLoop::current()->PostTask(
[email protected]c0e4dd12012-05-16 19:36:3183 FROM_HERE, base::Bind(&FakeDataChannel::DoWriteCallback,
84 weak_factory_.GetWeakPtr()));
85 return net::ERR_IO_PENDING;
86 }
[email protected]f61c3972010-12-23 09:54:1587 data_.push(new net::DrainableIOBuffer(buf, buf_len));
[email protected]2da659e2013-05-23 20:51:3488 base::MessageLoop::current()->PostTask(
[email protected]83039bb2011-12-09 18:43:5589 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback,
90 weak_factory_.GetWeakPtr()));
[email protected]f61c3972010-12-23 09:54:1591 return buf_len;
92 }
93
[email protected]c0e4dd12012-05-16 19:36:3194 // Closes the FakeDataChannel. After Close() is called, Read() returns 0,
95 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that
96 // after the FakeDataChannel is closed, the first Write() call completes
97 // asynchronously, which is necessary to reproduce bug 127822.
98 void Close() {
99 closed_ = true;
100 }
101
[email protected]f61c3972010-12-23 09:54:15102 private:
103 void DoReadCallback() {
[email protected]83039bb2011-12-09 18:43:55104 if (read_callback_.is_null() || data_.empty())
[email protected]f61c3972010-12-23 09:54:15105 return;
106
107 int copied = PropogateData(read_buf_, read_buf_len_);
[email protected]83039bb2011-12-09 18:43:55108 CompletionCallback callback = read_callback_;
109 read_callback_.Reset();
110 read_buf_ = NULL;
111 read_buf_len_ = 0;
112 callback.Run(copied);
[email protected]f61c3972010-12-23 09:54:15113 }
114
[email protected]c0e4dd12012-05-16 19:36:31115 void DoWriteCallback() {
116 if (write_callback_.is_null())
117 return;
118
119 CompletionCallback callback = write_callback_;
120 write_callback_.Reset();
121 callback.Run(net::ERR_CONNECTION_RESET);
122 }
123
[email protected]f61c3972010-12-23 09:54:15124 int PropogateData(scoped_refptr<net::IOBuffer> read_buf, int read_buf_len) {
125 scoped_refptr<net::DrainableIOBuffer> buf = data_.front();
126 int copied = std::min(buf->BytesRemaining(), read_buf_len);
127 memcpy(read_buf->data(), buf->data(), copied);
128 buf->DidConsume(copied);
129
130 if (!buf->BytesRemaining())
131 data_.pop();
132 return copied;
133 }
134
[email protected]83039bb2011-12-09 18:43:55135 CompletionCallback read_callback_;
[email protected]f61c3972010-12-23 09:54:15136 scoped_refptr<net::IOBuffer> read_buf_;
137 int read_buf_len_;
138
[email protected]c0e4dd12012-05-16 19:36:31139 CompletionCallback write_callback_;
140
[email protected]f61c3972010-12-23 09:54:15141 std::queue<scoped_refptr<net::DrainableIOBuffer> > data_;
142
[email protected]83039bb2011-12-09 18:43:55143 base::WeakPtrFactory<FakeDataChannel> weak_factory_;
[email protected]55ee0e52011-07-21 18:29:44144
[email protected]c0e4dd12012-05-16 19:36:31145 // True if Close() has been called.
146 bool closed_;
147
148 // Controls the completion of Write() after the FakeDataChannel is closed.
149 // After the FakeDataChannel is closed, the first Write() call completes
150 // asynchronously.
151 bool write_called_after_close_;
152
[email protected]f61c3972010-12-23 09:54:15153 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel);
154};
155
[email protected]3268023f2011-05-05 00:08:10156class FakeSocket : public StreamSocket {
[email protected]f61c3972010-12-23 09:54:15157 public:
158 FakeSocket(FakeDataChannel* incoming_channel,
159 FakeDataChannel* outgoing_channel)
160 : incoming_(incoming_channel),
161 outgoing_(outgoing_channel) {
162 }
163
164 virtual ~FakeSocket() {
[email protected]f61c3972010-12-23 09:54:15165 }
166
167 virtual int Read(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55168 const CompletionCallback& callback) OVERRIDE {
[email protected]3f55aa12011-12-07 02:03:33169 // Read random number of bytes.
170 buf_len = rand() % buf_len + 1;
171 return incoming_->Read(buf, buf_len, callback);
172 }
[email protected]f61c3972010-12-23 09:54:15173
174 virtual int Write(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55175 const CompletionCallback& callback) OVERRIDE {
[email protected]55ee0e52011-07-21 18:29:44176 // Write random number of bytes.
177 buf_len = rand() % buf_len + 1;
[email protected]f61c3972010-12-23 09:54:15178 return outgoing_->Write(buf, buf_len, callback);
179 }
180
[email protected]83039bb2011-12-09 18:43:55181 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15182 return true;
183 }
184
[email protected]83039bb2011-12-09 18:43:55185 virtual bool SetSendBufferSize(int32 size) OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15186 return true;
187 }
188
[email protected]83039bb2011-12-09 18:43:55189 virtual int Connect(const CompletionCallback& callback) OVERRIDE {
[email protected]dbf036f2011-12-06 23:33:24190 return net::OK;
191 }
[email protected]f61c3972010-12-23 09:54:15192
[email protected]c0e4dd12012-05-16 19:36:31193 virtual void Disconnect() OVERRIDE {
194 incoming_->Close();
195 outgoing_->Close();
196 }
[email protected]f61c3972010-12-23 09:54:15197
[email protected]83039bb2011-12-09 18:43:55198 virtual bool IsConnected() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15199 return true;
200 }
201
[email protected]83039bb2011-12-09 18:43:55202 virtual bool IsConnectedAndIdle() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15203 return true;
204 }
205
[email protected]a3528692012-06-08 00:11:42206 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE {
207 net::IPAddressNumber ip_address(net::kIPv4AddressSize);
208 *address = net::IPEndPoint(ip_address, 0 /*port*/);
[email protected]f61c3972010-12-23 09:54:15209 return net::OK;
210 }
211
[email protected]83039bb2011-12-09 18:43:55212 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE {
[email protected]e7f74da2011-04-19 23:49:35213 net::IPAddressNumber ip_address(4);
214 *address = net::IPEndPoint(ip_address, 0);
215 return net::OK;
216 }
217
[email protected]83039bb2011-12-09 18:43:55218 virtual const BoundNetLog& NetLog() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15219 return net_log_;
220 }
221
[email protected]83039bb2011-12-09 18:43:55222 virtual void SetSubresourceSpeculation() OVERRIDE {}
223 virtual void SetOmniboxSpeculation() OVERRIDE {}
[email protected]f61c3972010-12-23 09:54:15224
[email protected]83039bb2011-12-09 18:43:55225 virtual bool WasEverUsed() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15226 return true;
227 }
228
[email protected]83039bb2011-12-09 18:43:55229 virtual bool UsingTCPFastOpen() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15230 return false;
231 }
232
[email protected]5e6efa52011-06-27 17:26:41233
[email protected]46fadfd2013-02-06 09:40:16234 virtual bool WasNpnNegotiated() const OVERRIDE {
[email protected]2d88e7d2012-07-19 17:55:17235 return false;
236 }
237
[email protected]46fadfd2013-02-06 09:40:16238 virtual NextProto GetNegotiatedProtocol() const OVERRIDE {
[email protected]33661e482012-04-03 16:16:26239 return kProtoUnknown;
240 }
241
[email protected]46fadfd2013-02-06 09:40:16242 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE {
[email protected]2d88e7d2012-07-19 17:55:17243 return false;
244 }
245
[email protected]f61c3972010-12-23 09:54:15246 private:
247 net::BoundNetLog net_log_;
248 FakeDataChannel* incoming_;
249 FakeDataChannel* outgoing_;
250
251 DISALLOW_COPY_AND_ASSIGN(FakeSocket);
252};
253
254} // namespace
255
256// Verify the correctness of the test helper classes first.
257TEST(FakeSocketTest, DataTransfer) {
258 // Establish channels between two sockets.
259 FakeDataChannel channel_1;
260 FakeDataChannel channel_2;
261 FakeSocket client(&channel_1, &channel_2);
262 FakeSocket server(&channel_2, &channel_1);
263
264 const char kTestData[] = "testing123";
265 const int kTestDataSize = strlen(kTestData);
266 const int kReadBufSize = 1024;
267 scoped_refptr<net::IOBuffer> write_buf = new net::StringIOBuffer(kTestData);
268 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize);
269
270 // Write then read.
[email protected]90499482013-06-01 00:39:50271 int written =
272 server.Write(write_buf.get(), kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44273 EXPECT_GT(written, 0);
274 EXPECT_LE(written, kTestDataSize);
275
[email protected]90499482013-06-01 00:39:50276 int read = client.Read(read_buf.get(), kReadBufSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44277 EXPECT_GT(read, 0);
278 EXPECT_LE(read, written);
279 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15280
281 // Read then write.
[email protected]83039bb2011-12-09 18:43:55282 TestCompletionCallback callback;
[email protected]f61c3972010-12-23 09:54:15283 EXPECT_EQ(net::ERR_IO_PENDING,
[email protected]90499482013-06-01 00:39:50284 server.Read(read_buf.get(), kReadBufSize, callback.callback()));
[email protected]55ee0e52011-07-21 18:29:44285
[email protected]90499482013-06-01 00:39:50286 written = client.Write(write_buf.get(), kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44287 EXPECT_GT(written, 0);
288 EXPECT_LE(written, kTestDataSize);
289
290 read = callback.WaitForResult();
291 EXPECT_GT(read, 0);
292 EXPECT_LE(read, written);
293 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15294}
295
296class SSLServerSocketTest : public PlatformTest {
297 public:
298 SSLServerSocketTest()
[email protected]9f59fac2012-03-21 23:18:11299 : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()),
[email protected]b1c988b2013-06-13 06:48:11300 cert_verifier_(new MockCertVerifier()),
301 transport_security_state_(new TransportSecurityState) {
[email protected]47a12862012-04-10 01:00:49302 cert_verifier_->set_default_result(net::CERT_STATUS_AUTHORITY_INVALID);
[email protected]f61c3972010-12-23 09:54:15303 }
304
305 protected:
306 void Initialize() {
307 FakeSocket* fake_client_socket = new FakeSocket(&channel_1_, &channel_2_);
308 FakeSocket* fake_server_socket = new FakeSocket(&channel_2_, &channel_1_);
309
[email protected]6cdfd7f2013-02-08 20:40:15310 base::FilePath certs_dir(GetTestCertsDirectory());
[email protected]f61c3972010-12-23 09:54:15311
[email protected]6cdfd7f2013-02-08 20:40:15312 base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der");
[email protected]f61c3972010-12-23 09:54:15313 std::string cert_der;
314 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_der));
315
316 scoped_refptr<net::X509Certificate> cert =
317 X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size());
318
[email protected]6cdfd7f2013-02-08 20:40:15319 base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
[email protected]f61c3972010-12-23 09:54:15320 std::string key_string;
321 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string));
322 std::vector<uint8> key_vector(
323 reinterpret_cast<const uint8*>(key_string.data()),
324 reinterpret_cast<const uint8*>(key_string.data() +
325 key_string.length()));
326
[email protected]4b559b4d2011-04-14 17:37:14327 scoped_ptr<crypto::RSAPrivateKey> private_key(
328 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector));
[email protected]f61c3972010-12-23 09:54:15329
330 net::SSLConfig ssl_config;
[email protected]2fb7e3ba2011-06-22 19:24:38331 ssl_config.cached_info_enabled = false;
[email protected]f61c3972010-12-23 09:54:15332 ssl_config.false_start_enabled = false;
[email protected]6b4903f2012-06-26 02:13:49333 ssl_config.channel_id_enabled = false;
[email protected]80c75f682012-05-26 16:22:17334 ssl_config.version_min = SSL_PROTOCOL_VERSION_SSL3;
[email protected]21a1a182012-08-20 22:18:23335 ssl_config.version_max = SSL_PROTOCOL_VERSION_TLS1_1;
[email protected]f61c3972010-12-23 09:54:15336
337 // Certificate provided by the host doesn't need authority.
338 net::SSLConfig::CertAndStatus cert_and_status;
[email protected]4dc832e2011-04-28 22:04:24339 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID;
[email protected]3d5c1bd2011-07-20 02:14:01340 cert_and_status.der_cert = cert_der;
[email protected]f61c3972010-12-23 09:54:15341 ssl_config.allowed_bad_certs.push_back(cert_and_status);
342
343 net::HostPortPair host_and_pair("unittest", 0);
[email protected]feb79bcd2011-07-21 16:55:17344 net::SSLClientSocketContext context;
[email protected]9f59fac2012-03-21 23:18:11345 context.cert_verifier = cert_verifier_.get();
[email protected]b1c988b2013-06-13 06:48:11346 context.transport_security_state = transport_security_state_.get();
[email protected]f61c3972010-12-23 09:54:15347 client_socket_.reset(
348 socket_factory_->CreateSSLClientSocket(
[email protected]efe222152012-06-27 16:48:46349 fake_client_socket, host_and_pair, ssl_config, context));
[email protected]90499482013-06-01 00:39:50350 server_socket_.reset(net::CreateSSLServerSocket(
351 fake_server_socket, cert.get(), private_key.get(), net::SSLConfig()));
[email protected]f61c3972010-12-23 09:54:15352 }
353
354 FakeDataChannel channel_1_;
355 FakeDataChannel channel_2_;
356 scoped_ptr<net::SSLClientSocket> client_socket_;
357 scoped_ptr<net::SSLServerSocket> server_socket_;
358 net::ClientSocketFactory* socket_factory_;
[email protected]47a12862012-04-10 01:00:49359 scoped_ptr<net::MockCertVerifier> cert_verifier_;
[email protected]b1c988b2013-06-13 06:48:11360 scoped_ptr<net::TransportSecurityState> transport_security_state_;
[email protected]f61c3972010-12-23 09:54:15361};
362
363// SSLServerSocket is only implemented using NSS.
364#if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX)
365
366// This test only executes creation of client and server sockets. This is to
367// test that creation of sockets doesn't crash and have minimal code to run
368// under valgrind in order to help debugging memory problems.
369TEST_F(SSLServerSocketTest, Initialize) {
370 Initialize();
371}
372
[email protected]a7ac3c32011-06-17 19:10:15373// This test executes Connect() on SSLClientSocket and Handshake() on
374// SSLServerSocket to make sure handshaking between the two sockets is
[email protected]f61c3972010-12-23 09:54:15375// completed successfully.
376TEST_F(SSLServerSocketTest, Handshake) {
377 Initialize();
378
[email protected]83039bb2011-12-09 18:43:55379 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13380 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15381
[email protected]6ea7b152011-12-21 21:21:13382 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]f61c3972010-12-23 09:54:15383 EXPECT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
384
[email protected]83039bb2011-12-09 18:43:55385 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]f61c3972010-12-23 09:54:15386 EXPECT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
387
388 if (client_ret == net::ERR_IO_PENDING) {
389 EXPECT_EQ(net::OK, connect_callback.WaitForResult());
390 }
391 if (server_ret == net::ERR_IO_PENDING) {
[email protected]b0ff3f82011-07-23 05:12:39392 EXPECT_EQ(net::OK, handshake_callback.WaitForResult());
[email protected]f61c3972010-12-23 09:54:15393 }
[email protected]4dc832e2011-04-28 22:04:24394
395 // Make sure the cert status is expected.
396 SSLInfo ssl_info;
397 client_socket_->GetSSLInfo(&ssl_info);
398 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status);
[email protected]f61c3972010-12-23 09:54:15399}
400
401TEST_F(SSLServerSocketTest, DataTransfer) {
402 Initialize();
403
[email protected]83039bb2011-12-09 18:43:55404 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13405 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15406
407 // Establish connection.
[email protected]83039bb2011-12-09 18:43:55408 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]302b6272011-01-19 01:27:22409 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15410
[email protected]6ea7b152011-12-21 21:21:13411 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]302b6272011-01-19 01:27:22412 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15413
[email protected]febbbb52011-08-17 04:59:23414 client_ret = connect_callback.GetResult(client_ret);
415 ASSERT_EQ(net::OK, client_ret);
416 server_ret = handshake_callback.GetResult(server_ret);
417 ASSERT_EQ(net::OK, server_ret);
[email protected]f61c3972010-12-23 09:54:15418
419 const int kReadBufSize = 1024;
420 scoped_refptr<net::StringIOBuffer> write_buf =
421 new net::StringIOBuffer("testing123");
[email protected]febbbb52011-08-17 04:59:23422 scoped_refptr<net::DrainableIOBuffer> read_buf =
423 new net::DrainableIOBuffer(new net::IOBuffer(kReadBufSize),
424 kReadBufSize);
[email protected]f61c3972010-12-23 09:54:15425
426 // Write then read.
[email protected]83039bb2011-12-09 18:43:55427 TestCompletionCallback write_callback;
428 TestCompletionCallback read_callback;
[email protected]90499482013-06-01 00:39:50429 server_ret = server_socket_->Write(
430 write_buf.get(), write_buf->size(), write_callback.callback());
[email protected]f61c3972010-12-23 09:54:15431 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
[email protected]90499482013-06-01 00:39:50432 client_ret = client_socket_->Read(
433 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback());
[email protected]f61c3972010-12-23 09:54:15434 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
435
[email protected]febbbb52011-08-17 04:59:23436 server_ret = write_callback.GetResult(server_ret);
437 EXPECT_GT(server_ret, 0);
438 client_ret = read_callback.GetResult(client_ret);
439 ASSERT_GT(client_ret, 0);
440
441 read_buf->DidConsume(client_ret);
442 while (read_buf->BytesConsumed() < write_buf->size()) {
[email protected]90499482013-06-01 00:39:50443 client_ret = client_socket_->Read(
444 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback());
[email protected]febbbb52011-08-17 04:59:23445 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
446 client_ret = read_callback.GetResult(client_ret);
447 ASSERT_GT(client_ret, 0);
448 read_buf->DidConsume(client_ret);
[email protected]f61c3972010-12-23 09:54:15449 }
[email protected]febbbb52011-08-17 04:59:23450 EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed());
451 read_buf->SetOffset(0);
[email protected]f61c3972010-12-23 09:54:15452 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
453
454 // Read then write.
455 write_buf = new net::StringIOBuffer("hello123");
[email protected]90499482013-06-01 00:39:50456 server_ret = server_socket_->Read(
457 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback());
[email protected]f61c3972010-12-23 09:54:15458 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
[email protected]90499482013-06-01 00:39:50459 client_ret = client_socket_->Write(
460 write_buf.get(), write_buf->size(), write_callback.callback());
[email protected]f61c3972010-12-23 09:54:15461 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
462
[email protected]febbbb52011-08-17 04:59:23463 server_ret = read_callback.GetResult(server_ret);
464 ASSERT_GT(server_ret, 0);
465 client_ret = write_callback.GetResult(client_ret);
466 EXPECT_GT(client_ret, 0);
467
468 read_buf->DidConsume(server_ret);
469 while (read_buf->BytesConsumed() < write_buf->size()) {
[email protected]90499482013-06-01 00:39:50470 server_ret = server_socket_->Read(
471 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback());
[email protected]febbbb52011-08-17 04:59:23472 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
473 server_ret = read_callback.GetResult(server_ret);
474 ASSERT_GT(server_ret, 0);
475 read_buf->DidConsume(server_ret);
[email protected]f61c3972010-12-23 09:54:15476 }
[email protected]febbbb52011-08-17 04:59:23477 EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed());
478 read_buf->SetOffset(0);
[email protected]f61c3972010-12-23 09:54:15479 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
480}
[email protected]b0ff3f82011-07-23 05:12:39481
[email protected]c0e4dd12012-05-16 19:36:31482// A regression test for bug 127822 (http://crbug.com/127822).
483// If the server closes the connection after the handshake is finished,
484// the client's Write() call should not cause an infinite loop.
485// NOTE: this is a test for SSLClientSocket rather than SSLServerSocket.
486TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) {
487 Initialize();
488
489 TestCompletionCallback connect_callback;
490 TestCompletionCallback handshake_callback;
491
492 // Establish connection.
493 int client_ret = client_socket_->Connect(connect_callback.callback());
494 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
495
496 int server_ret = server_socket_->Handshake(handshake_callback.callback());
497 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
498
499 client_ret = connect_callback.GetResult(client_ret);
500 ASSERT_EQ(net::OK, client_ret);
501 server_ret = handshake_callback.GetResult(server_ret);
502 ASSERT_EQ(net::OK, server_ret);
503
504 scoped_refptr<net::StringIOBuffer> write_buf =
505 new net::StringIOBuffer("testing123");
506
507 // The server closes the connection. The server needs to write some
508 // data first so that the client's Read() calls from the transport
509 // socket won't return ERR_IO_PENDING. This ensures that the client
510 // will call Read() on the transport socket again.
511 TestCompletionCallback write_callback;
512
[email protected]90499482013-06-01 00:39:50513 server_ret = server_socket_->Write(
514 write_buf.get(), write_buf->size(), write_callback.callback());
[email protected]c0e4dd12012-05-16 19:36:31515 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
516
517 server_ret = write_callback.GetResult(server_ret);
518 EXPECT_GT(server_ret, 0);
519
520 server_socket_->Disconnect();
521
522 // The client writes some data. This should not cause an infinite loop.
[email protected]90499482013-06-01 00:39:50523 client_ret = client_socket_->Write(
524 write_buf.get(), write_buf->size(), write_callback.callback());
[email protected]c0e4dd12012-05-16 19:36:31525 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
526
527 client_ret = write_callback.GetResult(client_ret);
528 EXPECT_GT(client_ret, 0);
529
[email protected]2da659e2013-05-23 20:51:34530 base::MessageLoop::current()->PostDelayedTask(
531 FROM_HERE, base::MessageLoop::QuitClosure(),
[email protected]c0e4dd12012-05-16 19:36:31532 base::TimeDelta::FromMilliseconds(10));
[email protected]2da659e2013-05-23 20:51:34533 base::MessageLoop::current()->Run();
[email protected]c0e4dd12012-05-16 19:36:31534}
535
[email protected]b0ff3f82011-07-23 05:12:39536// This test executes ExportKeyingMaterial() on the client and server sockets,
537// after connecting them, and verifies that the results match.
538// This test will fail if False Start is enabled (see crbug.com/90208).
539TEST_F(SSLServerSocketTest, ExportKeyingMaterial) {
540 Initialize();
541
[email protected]83039bb2011-12-09 18:43:55542 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13543 TestCompletionCallback handshake_callback;
[email protected]b0ff3f82011-07-23 05:12:39544
[email protected]83039bb2011-12-09 18:43:55545 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]b0ff3f82011-07-23 05:12:39546 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
547
[email protected]6ea7b152011-12-21 21:21:13548 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]b0ff3f82011-07-23 05:12:39549 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
550
551 if (client_ret == net::ERR_IO_PENDING) {
552 ASSERT_EQ(net::OK, connect_callback.WaitForResult());
553 }
554 if (server_ret == net::ERR_IO_PENDING) {
555 ASSERT_EQ(net::OK, handshake_callback.WaitForResult());
556 }
557
558 const int kKeyingMaterialSize = 32;
559 const char* kKeyingLabel = "EXPERIMENTAL-server-socket-test";
560 const char* kKeyingContext = "";
561 unsigned char server_out[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58562 int rv = server_socket_->ExportKeyingMaterial(kKeyingLabel,
563 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39564 server_out, sizeof(server_out));
[email protected]47a12862012-04-10 01:00:49565 ASSERT_EQ(net::OK, rv);
[email protected]b0ff3f82011-07-23 05:12:39566
567 unsigned char client_out[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58568 rv = client_socket_->ExportKeyingMaterial(kKeyingLabel,
569 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39570 client_out, sizeof(client_out));
[email protected]47a12862012-04-10 01:00:49571 ASSERT_EQ(net::OK, rv);
572 EXPECT_EQ(0, memcmp(server_out, client_out, sizeof(server_out)));
[email protected]b0ff3f82011-07-23 05:12:39573
574 const char* kKeyingLabelBad = "EXPERIMENTAL-server-socket-test-bad";
575 unsigned char client_bad[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58576 rv = client_socket_->ExportKeyingMaterial(kKeyingLabelBad,
577 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39578 client_bad, sizeof(client_bad));
579 ASSERT_EQ(rv, net::OK);
[email protected]47a12862012-04-10 01:00:49580 EXPECT_NE(0, memcmp(server_out, client_bad, sizeof(server_out)));
[email protected]b0ff3f82011-07-23 05:12:39581}
[email protected]f61c3972010-12-23 09:54:15582#endif
583
584} // namespace net