blob: f93d7c69ffb2c0b447ad18a5d0f249d208fdd120 [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_path.h"
24#include "base/file_util.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]f61c3972010-12-23 09:54:1541#include "net/base/x509_certificate.h"
[email protected]f61c3972010-12-23 09:54:1542#include "net/socket/client_socket_factory.h"
43#include "net/socket/socket_test_util.h"
44#include "net/socket/ssl_client_socket.h"
[email protected]3268023f2011-05-05 00:08:1045#include "net/socket/stream_socket.h"
[email protected]f61c3972010-12-23 09:54:1546#include "testing/gtest/include/gtest/gtest.h"
47#include "testing/platform_test.h"
48
49namespace net {
50
51namespace {
52
53class FakeDataChannel {
54 public:
[email protected]55ee0e52011-07-21 18:29:4455 FakeDataChannel()
[email protected]83039bb2011-12-09 18:43:5556 : read_buf_len_(0),
[email protected]c0e4dd12012-05-16 19:36:3157 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
58 closed_(false),
59 write_called_after_close_(false) {
[email protected]f61c3972010-12-23 09:54:1560 }
61
[email protected]47a12862012-04-10 01:00:4962 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
[email protected]c0e4dd12012-05-16 19:36:3163 if (closed_)
64 return 0;
[email protected]3f55aa12011-12-07 02:03:3365 if (data_.empty()) {
[email protected]f61c3972010-12-23 09:54:1566 read_callback_ = callback;
67 read_buf_ = buf;
68 read_buf_len_ = buf_len;
69 return net::ERR_IO_PENDING;
70 }
71 return PropogateData(buf, buf_len);
72 }
73
[email protected]47a12862012-04-10 01:00:4974 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
[email protected]c0e4dd12012-05-16 19:36:3175 if (closed_) {
76 if (write_called_after_close_)
77 return net::ERR_CONNECTION_RESET;
78 write_called_after_close_ = true;
79 write_callback_ = callback;
80 MessageLoop::current()->PostTask(
81 FROM_HERE, base::Bind(&FakeDataChannel::DoWriteCallback,
82 weak_factory_.GetWeakPtr()));
83 return net::ERR_IO_PENDING;
84 }
[email protected]f61c3972010-12-23 09:54:1585 data_.push(new net::DrainableIOBuffer(buf, buf_len));
[email protected]55ee0e52011-07-21 18:29:4486 MessageLoop::current()->PostTask(
[email protected]83039bb2011-12-09 18:43:5587 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback,
88 weak_factory_.GetWeakPtr()));
[email protected]f61c3972010-12-23 09:54:1589 return buf_len;
90 }
91
[email protected]c0e4dd12012-05-16 19:36:3192 // Closes the FakeDataChannel. After Close() is called, Read() returns 0,
93 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that
94 // after the FakeDataChannel is closed, the first Write() call completes
95 // asynchronously, which is necessary to reproduce bug 127822.
96 void Close() {
97 closed_ = true;
98 }
99
[email protected]f61c3972010-12-23 09:54:15100 private:
101 void DoReadCallback() {
[email protected]83039bb2011-12-09 18:43:55102 if (read_callback_.is_null() || data_.empty())
[email protected]f61c3972010-12-23 09:54:15103 return;
104
105 int copied = PropogateData(read_buf_, read_buf_len_);
[email protected]83039bb2011-12-09 18:43:55106 CompletionCallback callback = read_callback_;
107 read_callback_.Reset();
108 read_buf_ = NULL;
109 read_buf_len_ = 0;
110 callback.Run(copied);
[email protected]f61c3972010-12-23 09:54:15111 }
112
[email protected]c0e4dd12012-05-16 19:36:31113 void DoWriteCallback() {
114 if (write_callback_.is_null())
115 return;
116
117 CompletionCallback callback = write_callback_;
118 write_callback_.Reset();
119 callback.Run(net::ERR_CONNECTION_RESET);
120 }
121
[email protected]f61c3972010-12-23 09:54:15122 int PropogateData(scoped_refptr<net::IOBuffer> read_buf, int read_buf_len) {
123 scoped_refptr<net::DrainableIOBuffer> buf = data_.front();
124 int copied = std::min(buf->BytesRemaining(), read_buf_len);
125 memcpy(read_buf->data(), buf->data(), copied);
126 buf->DidConsume(copied);
127
128 if (!buf->BytesRemaining())
129 data_.pop();
130 return copied;
131 }
132
[email protected]83039bb2011-12-09 18:43:55133 CompletionCallback read_callback_;
[email protected]f61c3972010-12-23 09:54:15134 scoped_refptr<net::IOBuffer> read_buf_;
135 int read_buf_len_;
136
[email protected]c0e4dd12012-05-16 19:36:31137 CompletionCallback write_callback_;
138
[email protected]f61c3972010-12-23 09:54:15139 std::queue<scoped_refptr<net::DrainableIOBuffer> > data_;
140
[email protected]83039bb2011-12-09 18:43:55141 base::WeakPtrFactory<FakeDataChannel> weak_factory_;
[email protected]55ee0e52011-07-21 18:29:44142
[email protected]c0e4dd12012-05-16 19:36:31143 // True if Close() has been called.
144 bool closed_;
145
146 // Controls the completion of Write() after the FakeDataChannel is closed.
147 // After the FakeDataChannel is closed, the first Write() call completes
148 // asynchronously.
149 bool write_called_after_close_;
150
[email protected]f61c3972010-12-23 09:54:15151 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel);
152};
153
[email protected]3268023f2011-05-05 00:08:10154class FakeSocket : public StreamSocket {
[email protected]f61c3972010-12-23 09:54:15155 public:
156 FakeSocket(FakeDataChannel* incoming_channel,
157 FakeDataChannel* outgoing_channel)
158 : incoming_(incoming_channel),
159 outgoing_(outgoing_channel) {
160 }
161
162 virtual ~FakeSocket() {
[email protected]f61c3972010-12-23 09:54:15163 }
164
165 virtual int Read(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55166 const CompletionCallback& callback) OVERRIDE {
[email protected]3f55aa12011-12-07 02:03:33167 // Read random number of bytes.
168 buf_len = rand() % buf_len + 1;
169 return incoming_->Read(buf, buf_len, callback);
170 }
[email protected]f61c3972010-12-23 09:54:15171
172 virtual int Write(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55173 const CompletionCallback& callback) OVERRIDE {
[email protected]55ee0e52011-07-21 18:29:44174 // Write random number of bytes.
175 buf_len = rand() % buf_len + 1;
[email protected]f61c3972010-12-23 09:54:15176 return outgoing_->Write(buf, buf_len, callback);
177 }
178
[email protected]83039bb2011-12-09 18:43:55179 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15180 return true;
181 }
182
[email protected]83039bb2011-12-09 18:43:55183 virtual bool SetSendBufferSize(int32 size) OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15184 return true;
185 }
186
[email protected]83039bb2011-12-09 18:43:55187 virtual int Connect(const CompletionCallback& callback) OVERRIDE {
[email protected]dbf036f2011-12-06 23:33:24188 return net::OK;
189 }
[email protected]f61c3972010-12-23 09:54:15190
[email protected]c0e4dd12012-05-16 19:36:31191 virtual void Disconnect() OVERRIDE {
192 incoming_->Close();
193 outgoing_->Close();
194 }
[email protected]f61c3972010-12-23 09:54:15195
[email protected]83039bb2011-12-09 18:43:55196 virtual bool IsConnected() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15197 return true;
198 }
199
[email protected]83039bb2011-12-09 18:43:55200 virtual bool IsConnectedAndIdle() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15201 return true;
202 }
203
[email protected]a3528692012-06-08 00:11:42204 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE {
205 net::IPAddressNumber ip_address(net::kIPv4AddressSize);
206 *address = net::IPEndPoint(ip_address, 0 /*port*/);
[email protected]f61c3972010-12-23 09:54:15207 return net::OK;
208 }
209
[email protected]83039bb2011-12-09 18:43:55210 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE {
[email protected]e7f74da2011-04-19 23:49:35211 net::IPAddressNumber ip_address(4);
212 *address = net::IPEndPoint(ip_address, 0);
213 return net::OK;
214 }
215
[email protected]83039bb2011-12-09 18:43:55216 virtual const BoundNetLog& NetLog() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15217 return net_log_;
218 }
219
[email protected]83039bb2011-12-09 18:43:55220 virtual void SetSubresourceSpeculation() OVERRIDE {}
221 virtual void SetOmniboxSpeculation() OVERRIDE {}
[email protected]f61c3972010-12-23 09:54:15222
[email protected]83039bb2011-12-09 18:43:55223 virtual bool WasEverUsed() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15224 return true;
225 }
226
[email protected]83039bb2011-12-09 18:43:55227 virtual bool UsingTCPFastOpen() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15228 return false;
229 }
230
[email protected]83039bb2011-12-09 18:43:55231 virtual int64 NumBytesRead() const OVERRIDE {
[email protected]5e6efa52011-06-27 17:26:41232 return -1;
233 }
234
[email protected]83039bb2011-12-09 18:43:55235 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE {
[email protected]5e6efa52011-06-27 17:26:41236 return base::TimeDelta::FromMicroseconds(-1);
237 }
238
[email protected]2d88e7d2012-07-19 17:55:17239 virtual bool WasNpnNegotiated() const {
240 return false;
241 }
242
[email protected]33661e482012-04-03 16:16:26243 virtual NextProto GetNegotiatedProtocol() const {
244 return kProtoUnknown;
245 }
246
[email protected]2d88e7d2012-07-19 17:55:17247 virtual bool GetSSLInfo(SSLInfo* ssl_info) {
248 return false;
249 }
250
[email protected]f61c3972010-12-23 09:54:15251 private:
252 net::BoundNetLog net_log_;
253 FakeDataChannel* incoming_;
254 FakeDataChannel* outgoing_;
255
256 DISALLOW_COPY_AND_ASSIGN(FakeSocket);
257};
258
259} // namespace
260
261// Verify the correctness of the test helper classes first.
262TEST(FakeSocketTest, DataTransfer) {
263 // Establish channels between two sockets.
264 FakeDataChannel channel_1;
265 FakeDataChannel channel_2;
266 FakeSocket client(&channel_1, &channel_2);
267 FakeSocket server(&channel_2, &channel_1);
268
269 const char kTestData[] = "testing123";
270 const int kTestDataSize = strlen(kTestData);
271 const int kReadBufSize = 1024;
272 scoped_refptr<net::IOBuffer> write_buf = new net::StringIOBuffer(kTestData);
273 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kReadBufSize);
274
275 // Write then read.
[email protected]83039bb2011-12-09 18:43:55276 int written = server.Write(write_buf, kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44277 EXPECT_GT(written, 0);
278 EXPECT_LE(written, kTestDataSize);
279
[email protected]83039bb2011-12-09 18:43:55280 int read = client.Read(read_buf, kReadBufSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44281 EXPECT_GT(read, 0);
282 EXPECT_LE(read, written);
283 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15284
285 // Read then write.
[email protected]83039bb2011-12-09 18:43:55286 TestCompletionCallback callback;
[email protected]f61c3972010-12-23 09:54:15287 EXPECT_EQ(net::ERR_IO_PENDING,
[email protected]83039bb2011-12-09 18:43:55288 server.Read(read_buf, kReadBufSize, callback.callback()));
[email protected]55ee0e52011-07-21 18:29:44289
[email protected]83039bb2011-12-09 18:43:55290 written = client.Write(write_buf, kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44291 EXPECT_GT(written, 0);
292 EXPECT_LE(written, kTestDataSize);
293
294 read = callback.WaitForResult();
295 EXPECT_GT(read, 0);
296 EXPECT_LE(read, written);
297 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15298}
299
300class SSLServerSocketTest : public PlatformTest {
301 public:
302 SSLServerSocketTest()
[email protected]9f59fac2012-03-21 23:18:11303 : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()),
[email protected]47a12862012-04-10 01:00:49304 cert_verifier_(new MockCertVerifier()) {
305 cert_verifier_->set_default_result(net::CERT_STATUS_AUTHORITY_INVALID);
[email protected]f61c3972010-12-23 09:54:15306 }
307
308 protected:
309 void Initialize() {
310 FakeSocket* fake_client_socket = new FakeSocket(&channel_1_, &channel_2_);
311 FakeSocket* fake_server_socket = new FakeSocket(&channel_2_, &channel_1_);
312
[email protected]a8e0ab9d2012-03-31 01:09:44313 FilePath certs_dir(GetTestCertsDirectory());
[email protected]f61c3972010-12-23 09:54:15314
315 FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der");
316 std::string cert_der;
317 ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_der));
318
319 scoped_refptr<net::X509Certificate> cert =
320 X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size());
321
322 FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
323 std::string key_string;
324 ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string));
325 std::vector<uint8> key_vector(
326 reinterpret_cast<const uint8*>(key_string.data()),
327 reinterpret_cast<const uint8*>(key_string.data() +
328 key_string.length()));
329
[email protected]4b559b4d2011-04-14 17:37:14330 scoped_ptr<crypto::RSAPrivateKey> private_key(
331 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector));
[email protected]f61c3972010-12-23 09:54:15332
333 net::SSLConfig ssl_config;
[email protected]2fb7e3ba2011-06-22 19:24:38334 ssl_config.cached_info_enabled = false;
[email protected]f61c3972010-12-23 09:54:15335 ssl_config.false_start_enabled = false;
[email protected]6b4903f2012-06-26 02:13:49336 ssl_config.channel_id_enabled = false;
[email protected]80c75f682012-05-26 16:22:17337 ssl_config.version_min = SSL_PROTOCOL_VERSION_SSL3;
[email protected]21a1a182012-08-20 22:18:23338 ssl_config.version_max = SSL_PROTOCOL_VERSION_TLS1_1;
[email protected]f61c3972010-12-23 09:54:15339
340 // Certificate provided by the host doesn't need authority.
341 net::SSLConfig::CertAndStatus cert_and_status;
[email protected]4dc832e2011-04-28 22:04:24342 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID;
[email protected]3d5c1bd2011-07-20 02:14:01343 cert_and_status.der_cert = cert_der;
[email protected]f61c3972010-12-23 09:54:15344 ssl_config.allowed_bad_certs.push_back(cert_and_status);
345
346 net::HostPortPair host_and_pair("unittest", 0);
[email protected]feb79bcd2011-07-21 16:55:17347 net::SSLClientSocketContext context;
[email protected]9f59fac2012-03-21 23:18:11348 context.cert_verifier = cert_verifier_.get();
[email protected]f61c3972010-12-23 09:54:15349 client_socket_.reset(
350 socket_factory_->CreateSSLClientSocket(
[email protected]efe222152012-06-27 16:48:46351 fake_client_socket, host_and_pair, ssl_config, context));
[email protected]f61c3972010-12-23 09:54:15352 server_socket_.reset(net::CreateSSLServerSocket(fake_server_socket,
353 cert, private_key.get(),
354 net::SSLConfig()));
355 }
356
357 FakeDataChannel channel_1_;
358 FakeDataChannel channel_2_;
359 scoped_ptr<net::SSLClientSocket> client_socket_;
360 scoped_ptr<net::SSLServerSocket> server_socket_;
361 net::ClientSocketFactory* socket_factory_;
[email protected]47a12862012-04-10 01:00:49362 scoped_ptr<net::MockCertVerifier> cert_verifier_;
[email protected]f61c3972010-12-23 09:54:15363};
364
365// SSLServerSocket is only implemented using NSS.
366#if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX)
367
368// This test only executes creation of client and server sockets. This is to
369// test that creation of sockets doesn't crash and have minimal code to run
370// under valgrind in order to help debugging memory problems.
371TEST_F(SSLServerSocketTest, Initialize) {
372 Initialize();
373}
374
[email protected]a7ac3c32011-06-17 19:10:15375// This test executes Connect() on SSLClientSocket and Handshake() on
376// SSLServerSocket to make sure handshaking between the two sockets is
[email protected]f61c3972010-12-23 09:54:15377// completed successfully.
378TEST_F(SSLServerSocketTest, Handshake) {
379 Initialize();
380
[email protected]83039bb2011-12-09 18:43:55381 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13382 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15383
[email protected]6ea7b152011-12-21 21:21:13384 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]f61c3972010-12-23 09:54:15385 EXPECT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
386
[email protected]83039bb2011-12-09 18:43:55387 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]f61c3972010-12-23 09:54:15388 EXPECT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
389
390 if (client_ret == net::ERR_IO_PENDING) {
391 EXPECT_EQ(net::OK, connect_callback.WaitForResult());
392 }
393 if (server_ret == net::ERR_IO_PENDING) {
[email protected]b0ff3f82011-07-23 05:12:39394 EXPECT_EQ(net::OK, handshake_callback.WaitForResult());
[email protected]f61c3972010-12-23 09:54:15395 }
[email protected]4dc832e2011-04-28 22:04:24396
397 // Make sure the cert status is expected.
398 SSLInfo ssl_info;
399 client_socket_->GetSSLInfo(&ssl_info);
400 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status);
[email protected]f61c3972010-12-23 09:54:15401}
402
403TEST_F(SSLServerSocketTest, DataTransfer) {
404 Initialize();
405
[email protected]83039bb2011-12-09 18:43:55406 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13407 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15408
409 // Establish connection.
[email protected]83039bb2011-12-09 18:43:55410 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]302b6272011-01-19 01:27:22411 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15412
[email protected]6ea7b152011-12-21 21:21:13413 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]302b6272011-01-19 01:27:22414 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15415
[email protected]febbbb52011-08-17 04:59:23416 client_ret = connect_callback.GetResult(client_ret);
417 ASSERT_EQ(net::OK, client_ret);
418 server_ret = handshake_callback.GetResult(server_ret);
419 ASSERT_EQ(net::OK, server_ret);
[email protected]f61c3972010-12-23 09:54:15420
421 const int kReadBufSize = 1024;
422 scoped_refptr<net::StringIOBuffer> write_buf =
423 new net::StringIOBuffer("testing123");
[email protected]febbbb52011-08-17 04:59:23424 scoped_refptr<net::DrainableIOBuffer> read_buf =
425 new net::DrainableIOBuffer(new net::IOBuffer(kReadBufSize),
426 kReadBufSize);
[email protected]f61c3972010-12-23 09:54:15427
428 // Write then read.
[email protected]83039bb2011-12-09 18:43:55429 TestCompletionCallback write_callback;
430 TestCompletionCallback read_callback;
[email protected]f61c3972010-12-23 09:54:15431 server_ret = server_socket_->Write(write_buf, write_buf->size(),
[email protected]83039bb2011-12-09 18:43:55432 write_callback.callback());
[email protected]f61c3972010-12-23 09:54:15433 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
[email protected]febbbb52011-08-17 04:59:23434 client_ret = client_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55435 read_callback.callback());
[email protected]f61c3972010-12-23 09:54:15436 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
437
[email protected]febbbb52011-08-17 04:59:23438 server_ret = write_callback.GetResult(server_ret);
439 EXPECT_GT(server_ret, 0);
440 client_ret = read_callback.GetResult(client_ret);
441 ASSERT_GT(client_ret, 0);
442
443 read_buf->DidConsume(client_ret);
444 while (read_buf->BytesConsumed() < write_buf->size()) {
445 client_ret = client_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55446 read_callback.callback());
[email protected]febbbb52011-08-17 04:59:23447 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
448 client_ret = read_callback.GetResult(client_ret);
449 ASSERT_GT(client_ret, 0);
450 read_buf->DidConsume(client_ret);
[email protected]f61c3972010-12-23 09:54:15451 }
[email protected]febbbb52011-08-17 04:59:23452 EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed());
453 read_buf->SetOffset(0);
[email protected]f61c3972010-12-23 09:54:15454 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
455
456 // Read then write.
457 write_buf = new net::StringIOBuffer("hello123");
[email protected]febbbb52011-08-17 04:59:23458 server_ret = server_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55459 read_callback.callback());
[email protected]f61c3972010-12-23 09:54:15460 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
461 client_ret = client_socket_->Write(write_buf, write_buf->size(),
[email protected]83039bb2011-12-09 18:43:55462 write_callback.callback());
[email protected]f61c3972010-12-23 09:54:15463 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
464
[email protected]febbbb52011-08-17 04:59:23465 server_ret = read_callback.GetResult(server_ret);
466 ASSERT_GT(server_ret, 0);
467 client_ret = write_callback.GetResult(client_ret);
468 EXPECT_GT(client_ret, 0);
469
470 read_buf->DidConsume(server_ret);
471 while (read_buf->BytesConsumed() < write_buf->size()) {
472 server_ret = server_socket_->Read(read_buf, read_buf->BytesRemaining(),
[email protected]83039bb2011-12-09 18:43:55473 read_callback.callback());
[email protected]febbbb52011-08-17 04:59:23474 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
475 server_ret = read_callback.GetResult(server_ret);
476 ASSERT_GT(server_ret, 0);
477 read_buf->DidConsume(server_ret);
[email protected]f61c3972010-12-23 09:54:15478 }
[email protected]febbbb52011-08-17 04:59:23479 EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed());
480 read_buf->SetOffset(0);
[email protected]f61c3972010-12-23 09:54:15481 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
482}
[email protected]b0ff3f82011-07-23 05:12:39483
[email protected]c0e4dd12012-05-16 19:36:31484// A regression test for bug 127822 (http://crbug.com/127822).
485// If the server closes the connection after the handshake is finished,
486// the client's Write() call should not cause an infinite loop.
487// NOTE: this is a test for SSLClientSocket rather than SSLServerSocket.
488TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) {
489 Initialize();
490
491 TestCompletionCallback connect_callback;
492 TestCompletionCallback handshake_callback;
493
494 // Establish connection.
495 int client_ret = client_socket_->Connect(connect_callback.callback());
496 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
497
498 int server_ret = server_socket_->Handshake(handshake_callback.callback());
499 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
500
501 client_ret = connect_callback.GetResult(client_ret);
502 ASSERT_EQ(net::OK, client_ret);
503 server_ret = handshake_callback.GetResult(server_ret);
504 ASSERT_EQ(net::OK, server_ret);
505
506 scoped_refptr<net::StringIOBuffer> write_buf =
507 new net::StringIOBuffer("testing123");
508
509 // The server closes the connection. The server needs to write some
510 // data first so that the client's Read() calls from the transport
511 // socket won't return ERR_IO_PENDING. This ensures that the client
512 // will call Read() on the transport socket again.
513 TestCompletionCallback write_callback;
514
515 server_ret = server_socket_->Write(write_buf, write_buf->size(),
516 write_callback.callback());
517 EXPECT_TRUE(server_ret > 0 || server_ret == net::ERR_IO_PENDING);
518
519 server_ret = write_callback.GetResult(server_ret);
520 EXPECT_GT(server_ret, 0);
521
522 server_socket_->Disconnect();
523
524 // The client writes some data. This should not cause an infinite loop.
525 client_ret = client_socket_->Write(write_buf, write_buf->size(),
526 write_callback.callback());
527 EXPECT_TRUE(client_ret > 0 || client_ret == net::ERR_IO_PENDING);
528
529 client_ret = write_callback.GetResult(client_ret);
530 EXPECT_GT(client_ret, 0);
531
532 MessageLoop::current()->PostDelayedTask(
533 FROM_HERE, MessageLoop::QuitClosure(),
534 base::TimeDelta::FromMilliseconds(10));
535 MessageLoop::current()->Run();
536}
537
[email protected]b0ff3f82011-07-23 05:12:39538// This test executes ExportKeyingMaterial() on the client and server sockets,
539// after connecting them, and verifies that the results match.
540// This test will fail if False Start is enabled (see crbug.com/90208).
541TEST_F(SSLServerSocketTest, ExportKeyingMaterial) {
542 Initialize();
543
[email protected]83039bb2011-12-09 18:43:55544 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13545 TestCompletionCallback handshake_callback;
[email protected]b0ff3f82011-07-23 05:12:39546
[email protected]83039bb2011-12-09 18:43:55547 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]b0ff3f82011-07-23 05:12:39548 ASSERT_TRUE(client_ret == net::OK || client_ret == net::ERR_IO_PENDING);
549
[email protected]6ea7b152011-12-21 21:21:13550 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]b0ff3f82011-07-23 05:12:39551 ASSERT_TRUE(server_ret == net::OK || server_ret == net::ERR_IO_PENDING);
552
553 if (client_ret == net::ERR_IO_PENDING) {
554 ASSERT_EQ(net::OK, connect_callback.WaitForResult());
555 }
556 if (server_ret == net::ERR_IO_PENDING) {
557 ASSERT_EQ(net::OK, handshake_callback.WaitForResult());
558 }
559
560 const int kKeyingMaterialSize = 32;
561 const char* kKeyingLabel = "EXPERIMENTAL-server-socket-test";
562 const char* kKeyingContext = "";
563 unsigned char server_out[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58564 int rv = server_socket_->ExportKeyingMaterial(kKeyingLabel,
565 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39566 server_out, sizeof(server_out));
[email protected]47a12862012-04-10 01:00:49567 ASSERT_EQ(net::OK, rv);
[email protected]b0ff3f82011-07-23 05:12:39568
569 unsigned char client_out[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58570 rv = client_socket_->ExportKeyingMaterial(kKeyingLabel,
571 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39572 client_out, sizeof(client_out));
[email protected]47a12862012-04-10 01:00:49573 ASSERT_EQ(net::OK, rv);
574 EXPECT_EQ(0, memcmp(server_out, client_out, sizeof(server_out)));
[email protected]b0ff3f82011-07-23 05:12:39575
576 const char* kKeyingLabelBad = "EXPERIMENTAL-server-socket-test-bad";
577 unsigned char client_bad[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58578 rv = client_socket_->ExportKeyingMaterial(kKeyingLabelBad,
579 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39580 client_bad, sizeof(client_bad));
581 ASSERT_EQ(rv, net::OK);
[email protected]47a12862012-04-10 01:00:49582 EXPECT_NE(0, memcmp(server_out, client_bad, sizeof(server_out)));
[email protected]b0ff3f82011-07-23 05:12:39583}
[email protected]f61c3972010-12-23 09:54:15584#endif
585
586} // namespace net