blob: 79d5a7fd1eb0fdae53c000896d5adfa5f12e3610 [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]18b577412013-07-18 04:19:1525#include "base/message_loop/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]c0e4dd12012-05-16 19:36:3159 closed_(false),
[email protected]d5492c52013-11-10 20:44:3960 write_called_after_close_(false),
61 weak_factory_(this) {
[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]4da82282014-07-16 18:40:4365 DCHECK(read_callback_.is_null());
66 DCHECK(!read_buf_);
[email protected]c0e4dd12012-05-16 19:36:3167 if (closed_)
68 return 0;
[email protected]3f55aa12011-12-07 02:03:3369 if (data_.empty()) {
[email protected]f61c3972010-12-23 09:54:1570 read_callback_ = callback;
71 read_buf_ = buf;
72 read_buf_len_ = buf_len;
[email protected]fa6ce922014-07-17 04:27:0473 return ERR_IO_PENDING;
[email protected]f61c3972010-12-23 09:54:1574 }
75 return PropogateData(buf, buf_len);
76 }
77
[email protected]47a12862012-04-10 01:00:4978 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
[email protected]4da82282014-07-16 18:40:4379 DCHECK(write_callback_.is_null());
[email protected]c0e4dd12012-05-16 19:36:3180 if (closed_) {
81 if (write_called_after_close_)
[email protected]fa6ce922014-07-17 04:27:0482 return ERR_CONNECTION_RESET;
[email protected]c0e4dd12012-05-16 19:36:3183 write_called_after_close_ = true;
84 write_callback_ = callback;
[email protected]2da659e2013-05-23 20:51:3485 base::MessageLoop::current()->PostTask(
[email protected]c0e4dd12012-05-16 19:36:3186 FROM_HERE, base::Bind(&FakeDataChannel::DoWriteCallback,
87 weak_factory_.GetWeakPtr()));
[email protected]fa6ce922014-07-17 04:27:0488 return ERR_IO_PENDING;
[email protected]c0e4dd12012-05-16 19:36:3189 }
[email protected]4da82282014-07-16 18:40:4390 // This function returns synchronously, so make a copy of the buffer.
[email protected]fa6ce922014-07-17 04:27:0491 data_.push(new DrainableIOBuffer(
92 new StringIOBuffer(std::string(buf->data(), buf_len)),
[email protected]4da82282014-07-16 18:40:4393 buf_len));
[email protected]2da659e2013-05-23 20:51:3494 base::MessageLoop::current()->PostTask(
[email protected]83039bb2011-12-09 18:43:5595 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback,
96 weak_factory_.GetWeakPtr()));
[email protected]f61c3972010-12-23 09:54:1597 return buf_len;
98 }
99
[email protected]c0e4dd12012-05-16 19:36:31100 // Closes the FakeDataChannel. After Close() is called, Read() returns 0,
101 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that
102 // after the FakeDataChannel is closed, the first Write() call completes
103 // asynchronously, which is necessary to reproduce bug 127822.
104 void Close() {
105 closed_ = true;
106 }
107
[email protected]f61c3972010-12-23 09:54:15108 private:
109 void DoReadCallback() {
[email protected]83039bb2011-12-09 18:43:55110 if (read_callback_.is_null() || data_.empty())
[email protected]f61c3972010-12-23 09:54:15111 return;
112
113 int copied = PropogateData(read_buf_, read_buf_len_);
[email protected]83039bb2011-12-09 18:43:55114 CompletionCallback callback = read_callback_;
115 read_callback_.Reset();
116 read_buf_ = NULL;
117 read_buf_len_ = 0;
118 callback.Run(copied);
[email protected]f61c3972010-12-23 09:54:15119 }
120
[email protected]c0e4dd12012-05-16 19:36:31121 void DoWriteCallback() {
122 if (write_callback_.is_null())
123 return;
124
125 CompletionCallback callback = write_callback_;
126 write_callback_.Reset();
[email protected]fa6ce922014-07-17 04:27:04127 callback.Run(ERR_CONNECTION_RESET);
[email protected]c0e4dd12012-05-16 19:36:31128 }
129
[email protected]fa6ce922014-07-17 04:27:04130 int PropogateData(scoped_refptr<IOBuffer> read_buf, int read_buf_len) {
131 scoped_refptr<DrainableIOBuffer> buf = data_.front();
[email protected]f61c3972010-12-23 09:54:15132 int copied = std::min(buf->BytesRemaining(), read_buf_len);
133 memcpy(read_buf->data(), buf->data(), copied);
134 buf->DidConsume(copied);
135
136 if (!buf->BytesRemaining())
137 data_.pop();
138 return copied;
139 }
140
[email protected]83039bb2011-12-09 18:43:55141 CompletionCallback read_callback_;
[email protected]fa6ce922014-07-17 04:27:04142 scoped_refptr<IOBuffer> read_buf_;
[email protected]f61c3972010-12-23 09:54:15143 int read_buf_len_;
144
[email protected]c0e4dd12012-05-16 19:36:31145 CompletionCallback write_callback_;
146
[email protected]fa6ce922014-07-17 04:27:04147 std::queue<scoped_refptr<DrainableIOBuffer> > data_;
[email protected]f61c3972010-12-23 09:54:15148
[email protected]c0e4dd12012-05-16 19:36:31149 // True if Close() has been called.
150 bool closed_;
151
152 // Controls the completion of Write() after the FakeDataChannel is closed.
153 // After the FakeDataChannel is closed, the first Write() call completes
154 // asynchronously.
155 bool write_called_after_close_;
156
[email protected]d5492c52013-11-10 20:44:39157 base::WeakPtrFactory<FakeDataChannel> weak_factory_;
158
[email protected]f61c3972010-12-23 09:54:15159 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel);
160};
161
[email protected]3268023f2011-05-05 00:08:10162class FakeSocket : public StreamSocket {
[email protected]f61c3972010-12-23 09:54:15163 public:
164 FakeSocket(FakeDataChannel* incoming_channel,
165 FakeDataChannel* outgoing_channel)
166 : incoming_(incoming_channel),
167 outgoing_(outgoing_channel) {
168 }
169
170 virtual ~FakeSocket() {
[email protected]f61c3972010-12-23 09:54:15171 }
172
173 virtual int Read(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55174 const CompletionCallback& callback) OVERRIDE {
[email protected]3f55aa12011-12-07 02:03:33175 // Read random number of bytes.
176 buf_len = rand() % buf_len + 1;
177 return incoming_->Read(buf, buf_len, callback);
178 }
[email protected]f61c3972010-12-23 09:54:15179
180 virtual int Write(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55181 const CompletionCallback& callback) OVERRIDE {
[email protected]55ee0e52011-07-21 18:29:44182 // Write random number of bytes.
183 buf_len = rand() % buf_len + 1;
[email protected]f61c3972010-12-23 09:54:15184 return outgoing_->Write(buf, buf_len, callback);
185 }
186
[email protected]28b96d1c2014-04-09 12:21:15187 virtual int SetReceiveBufferSize(int32 size) OVERRIDE {
[email protected]fa6ce922014-07-17 04:27:04188 return OK;
[email protected]f61c3972010-12-23 09:54:15189 }
190
[email protected]28b96d1c2014-04-09 12:21:15191 virtual int SetSendBufferSize(int32 size) OVERRIDE {
[email protected]fa6ce922014-07-17 04:27:04192 return OK;
[email protected]f61c3972010-12-23 09:54:15193 }
194
[email protected]83039bb2011-12-09 18:43:55195 virtual int Connect(const CompletionCallback& callback) OVERRIDE {
[email protected]fa6ce922014-07-17 04:27:04196 return OK;
[email protected]dbf036f2011-12-06 23:33:24197 }
[email protected]f61c3972010-12-23 09:54:15198
[email protected]c0e4dd12012-05-16 19:36:31199 virtual void Disconnect() OVERRIDE {
200 incoming_->Close();
201 outgoing_->Close();
202 }
[email protected]f61c3972010-12-23 09:54:15203
[email protected]83039bb2011-12-09 18:43:55204 virtual bool IsConnected() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15205 return true;
206 }
207
[email protected]83039bb2011-12-09 18:43:55208 virtual bool IsConnectedAndIdle() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15209 return true;
210 }
211
[email protected]a3528692012-06-08 00:11:42212 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE {
[email protected]fa6ce922014-07-17 04:27:04213 IPAddressNumber ip_address(kIPv4AddressSize);
214 *address = IPEndPoint(ip_address, 0 /*port*/);
215 return OK;
[email protected]f61c3972010-12-23 09:54:15216 }
217
[email protected]83039bb2011-12-09 18:43:55218 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE {
[email protected]fa6ce922014-07-17 04:27:04219 IPAddressNumber ip_address(4);
220 *address = IPEndPoint(ip_address, 0);
221 return OK;
[email protected]e7f74da2011-04-19 23:49:35222 }
223
[email protected]83039bb2011-12-09 18:43:55224 virtual const BoundNetLog& NetLog() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15225 return net_log_;
226 }
227
[email protected]83039bb2011-12-09 18:43:55228 virtual void SetSubresourceSpeculation() OVERRIDE {}
229 virtual void SetOmniboxSpeculation() OVERRIDE {}
[email protected]f61c3972010-12-23 09:54:15230
[email protected]83039bb2011-12-09 18:43:55231 virtual bool WasEverUsed() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15232 return true;
233 }
234
[email protected]83039bb2011-12-09 18:43:55235 virtual bool UsingTCPFastOpen() const OVERRIDE {
[email protected]f61c3972010-12-23 09:54:15236 return false;
237 }
238
[email protected]5e6efa52011-06-27 17:26:41239
[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:
[email protected]fa6ce922014-07-17 04:27:04253 BoundNetLog net_log_;
[email protected]f61c3972010-12-23 09:54:15254 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;
[email protected]fa6ce922014-07-17 04:27:04273 scoped_refptr<IOBuffer> write_buf = new StringIOBuffer(kTestData);
274 scoped_refptr<IOBuffer> read_buf = new IOBuffer(kReadBufSize);
[email protected]f61c3972010-12-23 09:54:15275
276 // Write then read.
[email protected]90499482013-06-01 00:39:50277 int written =
278 server.Write(write_buf.get(), kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44279 EXPECT_GT(written, 0);
280 EXPECT_LE(written, kTestDataSize);
281
[email protected]90499482013-06-01 00:39:50282 int read = client.Read(read_buf.get(), kReadBufSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44283 EXPECT_GT(read, 0);
284 EXPECT_LE(read, written);
285 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15286
287 // Read then write.
[email protected]83039bb2011-12-09 18:43:55288 TestCompletionCallback callback;
[email protected]fa6ce922014-07-17 04:27:04289 EXPECT_EQ(ERR_IO_PENDING,
[email protected]90499482013-06-01 00:39:50290 server.Read(read_buf.get(), kReadBufSize, callback.callback()));
[email protected]55ee0e52011-07-21 18:29:44291
[email protected]90499482013-06-01 00:39:50292 written = client.Write(write_buf.get(), kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44293 EXPECT_GT(written, 0);
294 EXPECT_LE(written, kTestDataSize);
295
296 read = callback.WaitForResult();
297 EXPECT_GT(read, 0);
298 EXPECT_LE(read, written);
299 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15300}
301
302class SSLServerSocketTest : public PlatformTest {
303 public:
304 SSLServerSocketTest()
[email protected]fa6ce922014-07-17 04:27:04305 : socket_factory_(ClientSocketFactory::GetDefaultFactory()),
[email protected]b1c988b2013-06-13 06:48:11306 cert_verifier_(new MockCertVerifier()),
307 transport_security_state_(new TransportSecurityState) {
[email protected]fa6ce922014-07-17 04:27:04308 cert_verifier_->set_default_result(CERT_STATUS_AUTHORITY_INVALID);
[email protected]f61c3972010-12-23 09:54:15309 }
310
311 protected:
312 void Initialize() {
[email protected]18ccfdb2013-08-15 00:13:44313 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle);
314 client_connection->SetSocket(
315 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_)));
316 scoped_ptr<StreamSocket> server_socket(
317 new FakeSocket(&channel_2_, &channel_1_));
[email protected]f61c3972010-12-23 09:54:15318
[email protected]6cdfd7f2013-02-08 20:40:15319 base::FilePath certs_dir(GetTestCertsDirectory());
[email protected]f61c3972010-12-23 09:54:15320
[email protected]6cdfd7f2013-02-08 20:40:15321 base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der");
[email protected]f61c3972010-12-23 09:54:15322 std::string cert_der;
[email protected]82f84b92013-08-30 18:23:50323 ASSERT_TRUE(base::ReadFileToString(cert_path, &cert_der));
[email protected]f61c3972010-12-23 09:54:15324
[email protected]fa6ce922014-07-17 04:27:04325 scoped_refptr<X509Certificate> cert =
[email protected]f61c3972010-12-23 09:54:15326 X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size());
327
[email protected]6cdfd7f2013-02-08 20:40:15328 base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
[email protected]f61c3972010-12-23 09:54:15329 std::string key_string;
[email protected]82f84b92013-08-30 18:23:50330 ASSERT_TRUE(base::ReadFileToString(key_path, &key_string));
[email protected]f61c3972010-12-23 09:54:15331 std::vector<uint8> key_vector(
332 reinterpret_cast<const uint8*>(key_string.data()),
333 reinterpret_cast<const uint8*>(key_string.data() +
334 key_string.length()));
335
[email protected]4b559b4d2011-04-14 17:37:14336 scoped_ptr<crypto::RSAPrivateKey> private_key(
337 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector));
[email protected]f61c3972010-12-23 09:54:15338
[email protected]fa6ce922014-07-17 04:27:04339 SSLConfig ssl_config;
[email protected]f61c3972010-12-23 09:54:15340 ssl_config.false_start_enabled = false;
[email protected]6b4903f2012-06-26 02:13:49341 ssl_config.channel_id_enabled = false;
[email protected]f61c3972010-12-23 09:54:15342
343 // Certificate provided by the host doesn't need authority.
[email protected]fa6ce922014-07-17 04:27:04344 SSLConfig::CertAndStatus cert_and_status;
[email protected]4dc832e2011-04-28 22:04:24345 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID;
[email protected]3d5c1bd2011-07-20 02:14:01346 cert_and_status.der_cert = cert_der;
[email protected]f61c3972010-12-23 09:54:15347 ssl_config.allowed_bad_certs.push_back(cert_and_status);
348
[email protected]fa6ce922014-07-17 04:27:04349 HostPortPair host_and_pair("unittest", 0);
350 SSLClientSocketContext context;
[email protected]9f59fac2012-03-21 23:18:11351 context.cert_verifier = cert_verifier_.get();
[email protected]b1c988b2013-06-13 06:48:11352 context.transport_security_state = transport_security_state_.get();
[email protected]18ccfdb2013-08-15 00:13:44353 client_socket_ =
[email protected]f61c3972010-12-23 09:54:15354 socket_factory_->CreateSSLClientSocket(
[email protected]18ccfdb2013-08-15 00:13:44355 client_connection.Pass(), host_and_pair, ssl_config, context);
[email protected]fa6ce922014-07-17 04:27:04356 server_socket_ = CreateSSLServerSocket(
[email protected]18ccfdb2013-08-15 00:13:44357 server_socket.Pass(),
[email protected]fa6ce922014-07-17 04:27:04358 cert.get(), private_key.get(), SSLConfig());
[email protected]f61c3972010-12-23 09:54:15359 }
360
361 FakeDataChannel channel_1_;
362 FakeDataChannel channel_2_;
[email protected]fa6ce922014-07-17 04:27:04363 scoped_ptr<SSLClientSocket> client_socket_;
364 scoped_ptr<SSLServerSocket> server_socket_;
365 ClientSocketFactory* socket_factory_;
366 scoped_ptr<MockCertVerifier> cert_verifier_;
367 scoped_ptr<TransportSecurityState> transport_security_state_;
[email protected]f61c3972010-12-23 09:54:15368};
369
[email protected]f61c3972010-12-23 09:54:15370// This test only executes creation of client and server sockets. This is to
371// test that creation of sockets doesn't crash and have minimal code to run
372// under valgrind in order to help debugging memory problems.
373TEST_F(SSLServerSocketTest, Initialize) {
374 Initialize();
375}
376
[email protected]a7ac3c32011-06-17 19:10:15377// This test executes Connect() on SSLClientSocket and Handshake() on
378// SSLServerSocket to make sure handshaking between the two sockets is
[email protected]f61c3972010-12-23 09:54:15379// completed successfully.
380TEST_F(SSLServerSocketTest, Handshake) {
381 Initialize();
382
[email protected]83039bb2011-12-09 18:43:55383 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13384 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15385
[email protected]6ea7b152011-12-21 21:21:13386 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04387 EXPECT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15388
[email protected]83039bb2011-12-09 18:43:55389 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04390 EXPECT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15391
[email protected]fa6ce922014-07-17 04:27:04392 if (client_ret == ERR_IO_PENDING) {
393 EXPECT_EQ(OK, connect_callback.WaitForResult());
[email protected]f61c3972010-12-23 09:54:15394 }
[email protected]fa6ce922014-07-17 04:27:04395 if (server_ret == ERR_IO_PENDING) {
396 EXPECT_EQ(OK, handshake_callback.WaitForResult());
[email protected]f61c3972010-12-23 09:54:15397 }
[email protected]4dc832e2011-04-28 22:04:24398
399 // Make sure the cert status is expected.
400 SSLInfo ssl_info;
401 client_socket_->GetSSLInfo(&ssl_info);
402 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status);
[email protected]f61c3972010-12-23 09:54:15403}
404
405TEST_F(SSLServerSocketTest, DataTransfer) {
406 Initialize();
407
[email protected]83039bb2011-12-09 18:43:55408 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13409 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15410
411 // Establish connection.
[email protected]83039bb2011-12-09 18:43:55412 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04413 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15414
[email protected]6ea7b152011-12-21 21:21:13415 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04416 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15417
[email protected]febbbb52011-08-17 04:59:23418 client_ret = connect_callback.GetResult(client_ret);
[email protected]fa6ce922014-07-17 04:27:04419 ASSERT_EQ(OK, client_ret);
[email protected]febbbb52011-08-17 04:59:23420 server_ret = handshake_callback.GetResult(server_ret);
[email protected]fa6ce922014-07-17 04:27:04421 ASSERT_EQ(OK, server_ret);
[email protected]f61c3972010-12-23 09:54:15422
423 const int kReadBufSize = 1024;
[email protected]fa6ce922014-07-17 04:27:04424 scoped_refptr<StringIOBuffer> write_buf =
425 new StringIOBuffer("testing123");
426 scoped_refptr<DrainableIOBuffer> read_buf =
427 new DrainableIOBuffer(new IOBuffer(kReadBufSize), 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]90499482013-06-01 00:39:50432 server_ret = server_socket_->Write(
433 write_buf.get(), write_buf->size(), write_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04434 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING);
[email protected]90499482013-06-01 00:39:50435 client_ret = client_socket_->Read(
436 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04437 EXPECT_TRUE(client_ret > 0 || client_ret == ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15438
[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()) {
[email protected]90499482013-06-01 00:39:50446 client_ret = client_socket_->Read(
447 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04448 EXPECT_TRUE(client_ret > 0 || client_ret == ERR_IO_PENDING);
[email protected]febbbb52011-08-17 04:59:23449 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.
[email protected]fa6ce922014-07-17 04:27:04458 write_buf = new StringIOBuffer("hello123");
[email protected]90499482013-06-01 00:39:50459 server_ret = server_socket_->Read(
460 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04461 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING);
[email protected]90499482013-06-01 00:39:50462 client_ret = client_socket_->Write(
463 write_buf.get(), write_buf->size(), write_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04464 EXPECT_TRUE(client_ret > 0 || client_ret == ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15465
[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()) {
[email protected]90499482013-06-01 00:39:50473 server_ret = server_socket_->Read(
474 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04475 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING);
[email protected]febbbb52011-08-17 04:59:23476 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.
[email protected]4da82282014-07-16 18:40:43489TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) {
[email protected]c0e4dd12012-05-16 19:36:31490 Initialize();
491
492 TestCompletionCallback connect_callback;
493 TestCompletionCallback handshake_callback;
494
495 // Establish connection.
496 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04497 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
[email protected]c0e4dd12012-05-16 19:36:31498
499 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04500 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
[email protected]c0e4dd12012-05-16 19:36:31501
502 client_ret = connect_callback.GetResult(client_ret);
[email protected]fa6ce922014-07-17 04:27:04503 ASSERT_EQ(OK, client_ret);
[email protected]c0e4dd12012-05-16 19:36:31504 server_ret = handshake_callback.GetResult(server_ret);
[email protected]fa6ce922014-07-17 04:27:04505 ASSERT_EQ(OK, server_ret);
[email protected]c0e4dd12012-05-16 19:36:31506
[email protected]fa6ce922014-07-17 04:27:04507 scoped_refptr<StringIOBuffer> write_buf = new StringIOBuffer("testing123");
[email protected]c0e4dd12012-05-16 19:36:31508
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
[email protected]90499482013-06-01 00:39:50515 server_ret = server_socket_->Write(
516 write_buf.get(), write_buf->size(), write_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04517 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING);
[email protected]c0e4dd12012-05-16 19:36:31518
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.
[email protected]90499482013-06-01 00:39:50525 client_ret = client_socket_->Write(
526 write_buf.get(), write_buf->size(), write_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04527 EXPECT_TRUE(client_ret > 0 || client_ret == ERR_IO_PENDING);
[email protected]c0e4dd12012-05-16 19:36:31528
529 client_ret = write_callback.GetResult(client_ret);
530 EXPECT_GT(client_ret, 0);
531
[email protected]2da659e2013-05-23 20:51:34532 base::MessageLoop::current()->PostDelayedTask(
533 FROM_HERE, base::MessageLoop::QuitClosure(),
[email protected]c0e4dd12012-05-16 19:36:31534 base::TimeDelta::FromMilliseconds(10));
[email protected]2da659e2013-05-23 20:51:34535 base::MessageLoop::current()->Run();
[email protected]c0e4dd12012-05-16 19:36:31536}
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]fa6ce922014-07-17 04:27:04548 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
[email protected]b0ff3f82011-07-23 05:12:39549
[email protected]6ea7b152011-12-21 21:21:13550 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04551 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
[email protected]b0ff3f82011-07-23 05:12:39552
[email protected]fa6ce922014-07-17 04:27:04553 if (client_ret == ERR_IO_PENDING) {
554 ASSERT_EQ(OK, connect_callback.WaitForResult());
[email protected]b0ff3f82011-07-23 05:12:39555 }
[email protected]fa6ce922014-07-17 04:27:04556 if (server_ret == ERR_IO_PENDING) {
557 ASSERT_EQ(OK, handshake_callback.WaitForResult());
[email protected]b0ff3f82011-07-23 05:12:39558 }
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]fa6ce922014-07-17 04:27:04567 ASSERT_EQ(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]fa6ce922014-07-17 04:27:04573 ASSERT_EQ(OK, rv);
[email protected]47a12862012-04-10 01:00:49574 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));
[email protected]fa6ce922014-07-17 04:27:04581 ASSERT_EQ(rv, 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
585} // namespace net