blob: 7b9436d29e9eb86329d3f606e5202a25b92d1093 [file] [log] [blame]
[email protected]1d872d32011-05-19 02:45:331// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]f7984fc62009-06-22 23:26:445#include "net/socket/ssl_client_socket.h"
6
initial.commit586acc5fe2008-07-26 22:42:527#include "net/base/address_list.h"
[email protected]822581d2010-12-16 17:27:158#include "net/base/cert_verifier.h"
initial.commit586acc5fe2008-07-26 22:42:529#include "net/base/host_resolver.h"
[email protected]597cf6e2009-05-29 09:43:2610#include "net/base/io_buffer.h"
[email protected]9e743cd2010-03-16 07:03:5311#include "net/base/net_log.h"
12#include "net/base/net_log_unittest.h"
[email protected]7b822b2b2008-08-05 00:15:4513#include "net/base/net_errors.h"
[email protected]aaead502008-10-15 00:20:1114#include "net/base/ssl_config_service.h"
initial.commit586acc5fe2008-07-26 22:42:5215#include "net/base/test_completion_callback.h"
[email protected]f7984fc62009-06-22 23:26:4416#include "net/socket/client_socket_factory.h"
[email protected]b442da32011-08-16 19:32:2817#include "net/socket/client_socket_handle.h"
[email protected]39afe642010-04-29 14:55:1818#include "net/socket/socket_test_util.h"
[email protected]f7984fc62009-06-22 23:26:4419#include "net/socket/tcp_client_socket.h"
[email protected]1b9565c2010-07-21 01:19:3120#include "net/test/test_server.h"
initial.commit586acc5fe2008-07-26 22:42:5221#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1522#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5223
24//-----------------------------------------------------------------------------
25
[email protected]aaead502008-10-15 00:20:1126const net::SSLConfig kDefaultSSLConfig;
[email protected]c5949a32008-10-08 17:28:2327
[email protected]b75523f2008-10-17 14:49:0728class SSLClientSocketTest : public PlatformTest {
[email protected]aaead502008-10-15 00:20:1129 public:
30 SSLClientSocketTest()
[email protected]822581d2010-12-16 17:27:1531 : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()),
32 cert_verifier_(new net::CertVerifier) {
[email protected]73e0bba2009-02-19 22:57:0933 }
34
[email protected]aaead502008-10-15 00:20:1135 protected:
[email protected]822581d2010-12-16 17:27:1536 net::SSLClientSocket* CreateSSLClientSocket(
[email protected]3268023f2011-05-05 00:08:1037 net::StreamSocket* transport_socket,
[email protected]822581d2010-12-16 17:27:1538 const net::HostPortPair& host_and_port,
39 const net::SSLConfig& ssl_config) {
[email protected]feb79bcd2011-07-21 16:55:1740 net::SSLClientSocketContext context;
41 context.cert_verifier = cert_verifier_.get();
[email protected]822581d2010-12-16 17:27:1542 return socket_factory_->CreateSSLClientSocket(transport_socket,
43 host_and_port,
44 ssl_config,
45 NULL,
[email protected]feb79bcd2011-07-21 16:55:1746 context);
[email protected]822581d2010-12-16 17:27:1547 }
48
[email protected]aaead502008-10-15 00:20:1149 net::ClientSocketFactory* socket_factory_;
[email protected]822581d2010-12-16 17:27:1550 scoped_ptr<net::CertVerifier> cert_verifier_;
initial.commit586acc5fe2008-07-26 22:42:5251};
52
initial.commit586acc5fe2008-07-26 22:42:5253//-----------------------------------------------------------------------------
54
[email protected]6a47ac82010-10-08 14:39:3055// LogContainsSSLConnectEndEvent returns true if the given index in the given
56// log is an SSL connect end event. The NSS sockets will cork in an attempt to
57// merge the first application data record with the Finished message when false
58// starting. However, in order to avoid the server timing out the handshake,
59// they'll give up waiting for application data and send the Finished after a
60// timeout. This means that an SSL connect end event may appear as a socket
61// write.
62static bool LogContainsSSLConnectEndEvent(
63 const net::CapturingNetLog::EntryList& log, int i) {
[email protected]7deea3d2011-01-09 06:03:4164 return net::LogContainsEndEvent(log, i, net::NetLog::TYPE_SSL_CONNECT) ||
65 net::LogContainsEvent(log, i, net::NetLog::TYPE_SOCKET_BYTES_SENT,
[email protected]7f2fff12010-10-09 18:42:1166 net::NetLog::PHASE_NONE);
[email protected]6a47ac82010-10-08 14:39:3067};
68
[email protected]010e27e2009-08-27 17:49:4169TEST_F(SSLClientSocketTest, Connect) {
[email protected]95409e12010-08-17 20:07:1170 net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath());
71 ASSERT_TRUE(test_server.Start());
[email protected]73e0bba2009-02-19 22:57:0972
initial.commit586acc5fe2008-07-26 22:42:5273 net::AddressList addr;
[email protected]95409e12010-08-17 20:07:1174 ASSERT_TRUE(test_server.GetAddressList(&addr));
75
[email protected]83039bb2011-12-09 18:43:5576 net::TestCompletionCallback callback;
[email protected]a2006ece2010-04-23 16:44:0277 net::CapturingNetLog log(net::CapturingNetLog::kUnbounded);
[email protected]3268023f2011-05-05 00:08:1078 net::StreamSocket* transport = new net::TCPClientSocket(
[email protected]0a0b7682010-08-25 17:08:0779 addr, &log, net::NetLog::Source());
[email protected]83039bb2011-12-09 18:43:5580 int rv = transport->Connect(callback.callback());
[email protected]bacff652009-03-31 17:50:3381 if (rv == net::ERR_IO_PENDING)
82 rv = callback.WaitForResult();
83 EXPECT_EQ(net::OK, rv);
84
[email protected]feb79bcd2011-07-21 16:55:1785 net::SSLClientSocketContext context;
86 context.cert_verifier = cert_verifier_.get();
[email protected]aaead502008-10-15 00:20:1187 scoped_ptr<net::SSLClientSocket> sock(
[email protected]6526380c2010-11-10 04:40:3388 socket_factory_->CreateSSLClientSocket(
[email protected]822581d2010-12-16 17:27:1589 transport, test_server.host_port_pair(), kDefaultSSLConfig,
[email protected]feb79bcd2011-07-21 16:55:1790 NULL, context));
initial.commit586acc5fe2008-07-26 22:42:5291
[email protected]aaead502008-10-15 00:20:1192 EXPECT_FALSE(sock->IsConnected());
initial.commit586acc5fe2008-07-26 22:42:5293
[email protected]83039bb2011-12-09 18:43:5594 rv = sock->Connect(callback.callback());
[email protected]b2fcd0e2010-12-01 15:19:4095
96 net::CapturingNetLog::EntryList entries;
97 log.GetEntries(&entries);
[email protected]eb8605c2010-05-21 22:17:4798 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]b2fcd0e2010-12-01 15:19:4099 entries, 5, net::NetLog::TYPE_SSL_CONNECT));
[email protected]6526380c2010-11-10 04:40:33100 if (rv == net::ERR_IO_PENDING)
[email protected]7b822b2b2008-08-05 00:15:45101 rv = callback.WaitForResult();
[email protected]6526380c2010-11-10 04:40:33102 EXPECT_EQ(net::OK, rv);
[email protected]aaead502008-10-15 00:20:11103 EXPECT_TRUE(sock->IsConnected());
[email protected]b2fcd0e2010-12-01 15:19:40104 log.GetEntries(&entries);
105 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1));
initial.commit586acc5fe2008-07-26 22:42:52106
[email protected]aaead502008-10-15 00:20:11107 sock->Disconnect();
108 EXPECT_FALSE(sock->IsConnected());
initial.commit586acc5fe2008-07-26 22:42:52109}
110
[email protected]7f2fff12010-10-09 18:42:11111TEST_F(SSLClientSocketTest, ConnectExpired) {
[email protected]347599952010-10-28 11:57:36112 net::TestServer::HTTPSOptions https_options(
113 net::TestServer::HTTPSOptions::CERT_EXPIRED);
114 net::TestServer test_server(https_options, FilePath());
[email protected]95409e12010-08-17 20:07:11115 ASSERT_TRUE(test_server.Start());
[email protected]73e0bba2009-02-19 22:57:09116
initial.commit586acc5fe2008-07-26 22:42:52117 net::AddressList addr;
[email protected]95409e12010-08-17 20:07:11118 ASSERT_TRUE(test_server.GetAddressList(&addr));
119
[email protected]83039bb2011-12-09 18:43:55120 net::TestCompletionCallback callback;
[email protected]a2006ece2010-04-23 16:44:02121 net::CapturingNetLog log(net::CapturingNetLog::kUnbounded);
[email protected]3268023f2011-05-05 00:08:10122 net::StreamSocket* transport = new net::TCPClientSocket(
[email protected]0a0b7682010-08-25 17:08:07123 addr, &log, net::NetLog::Source());
[email protected]83039bb2011-12-09 18:43:55124 int rv = transport->Connect(callback.callback());
[email protected]bacff652009-03-31 17:50:33125 if (rv == net::ERR_IO_PENDING)
126 rv = callback.WaitForResult();
127 EXPECT_EQ(net::OK, rv);
128
[email protected]73e0bba2009-02-19 22:57:09129 scoped_ptr<net::SSLClientSocket> sock(
[email protected]822581d2010-12-16 17:27:15130 CreateSSLClientSocket(transport, test_server.host_port_pair(),
131 kDefaultSSLConfig));
[email protected]73e0bba2009-02-19 22:57:09132
133 EXPECT_FALSE(sock->IsConnected());
134
[email protected]83039bb2011-12-09 18:43:55135 rv = sock->Connect(callback.callback());
[email protected]b2fcd0e2010-12-01 15:19:40136
137 net::CapturingNetLog::EntryList entries;
138 log.GetEntries(&entries);
[email protected]eb8605c2010-05-21 22:17:47139 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]b2fcd0e2010-12-01 15:19:40140 entries, 5, net::NetLog::TYPE_SSL_CONNECT));
[email protected]6526380c2010-11-10 04:40:33141 if (rv == net::ERR_IO_PENDING)
[email protected]73e0bba2009-02-19 22:57:09142 rv = callback.WaitForResult();
[email protected]73e0bba2009-02-19 22:57:09143
[email protected]6526380c2010-11-10 04:40:33144 EXPECT_EQ(net::ERR_CERT_DATE_INVALID, rv);
145
146 // Rather than testing whether or not the underlying socket is connected,
147 // test that the handshake has finished. This is because it may be
148 // desirable to disconnect the socket before showing a user prompt, since
149 // the user may take indefinitely long to respond.
[email protected]b2fcd0e2010-12-01 15:19:40150 log.GetEntries(&entries);
151 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1));
[email protected]73e0bba2009-02-19 22:57:09152}
153
[email protected]7f2fff12010-10-09 18:42:11154TEST_F(SSLClientSocketTest, ConnectMismatched) {
[email protected]347599952010-10-28 11:57:36155 net::TestServer::HTTPSOptions https_options(
156 net::TestServer::HTTPSOptions::CERT_MISMATCHED_NAME);
157 net::TestServer test_server(https_options, FilePath());
[email protected]95409e12010-08-17 20:07:11158 ASSERT_TRUE(test_server.Start());
[email protected]73e0bba2009-02-19 22:57:09159
160 net::AddressList addr;
[email protected]95409e12010-08-17 20:07:11161 ASSERT_TRUE(test_server.GetAddressList(&addr));
162
[email protected]83039bb2011-12-09 18:43:55163 net::TestCompletionCallback callback;
[email protected]a2006ece2010-04-23 16:44:02164 net::CapturingNetLog log(net::CapturingNetLog::kUnbounded);
[email protected]3268023f2011-05-05 00:08:10165 net::StreamSocket* transport = new net::TCPClientSocket(
[email protected]0a0b7682010-08-25 17:08:07166 addr, &log, net::NetLog::Source());
[email protected]83039bb2011-12-09 18:43:55167 int rv = transport->Connect(callback.callback());
[email protected]bacff652009-03-31 17:50:33168 if (rv == net::ERR_IO_PENDING)
169 rv = callback.WaitForResult();
170 EXPECT_EQ(net::OK, rv);
171
[email protected]73e0bba2009-02-19 22:57:09172 scoped_ptr<net::SSLClientSocket> sock(
[email protected]822581d2010-12-16 17:27:15173 CreateSSLClientSocket(transport, test_server.host_port_pair(),
174 kDefaultSSLConfig));
[email protected]73e0bba2009-02-19 22:57:09175
176 EXPECT_FALSE(sock->IsConnected());
177
[email protected]83039bb2011-12-09 18:43:55178 rv = sock->Connect(callback.callback());
[email protected]4b32be92010-05-20 03:21:42179
[email protected]b2fcd0e2010-12-01 15:19:40180 net::CapturingNetLog::EntryList entries;
181 log.GetEntries(&entries);
[email protected]eb8605c2010-05-21 22:17:47182 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]b2fcd0e2010-12-01 15:19:40183 entries, 5, net::NetLog::TYPE_SSL_CONNECT));
[email protected]6526380c2010-11-10 04:40:33184 if (rv == net::ERR_IO_PENDING)
[email protected]73e0bba2009-02-19 22:57:09185 rv = callback.WaitForResult();
[email protected]73e0bba2009-02-19 22:57:09186
[email protected]6526380c2010-11-10 04:40:33187 EXPECT_EQ(net::ERR_CERT_COMMON_NAME_INVALID, rv);
188
189 // Rather than testing whether or not the underlying socket is connected,
190 // test that the handshake has finished. This is because it may be
191 // desirable to disconnect the socket before showing a user prompt, since
192 // the user may take indefinitely long to respond.
[email protected]b2fcd0e2010-12-01 15:19:40193 log.GetEntries(&entries);
194 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1));
[email protected]73e0bba2009-02-19 22:57:09195}
196
[email protected]65a3b912010-08-21 05:46:58197// Attempt to connect to a page which requests a client certificate. It should
198// return an error code on connect.
[email protected]091fd3b72011-03-14 03:23:52199TEST_F(SSLClientSocketTest, ConnectClientAuthCertRequested) {
[email protected]347599952010-10-28 11:57:36200 net::TestServer::HTTPSOptions https_options;
201 https_options.request_client_certificate = true;
202 net::TestServer test_server(https_options, FilePath());
[email protected]95409e12010-08-17 20:07:11203 ASSERT_TRUE(test_server.Start());
[email protected]8df162a2010-08-07 01:10:02204
205 net::AddressList addr;
[email protected]95409e12010-08-17 20:07:11206 ASSERT_TRUE(test_server.GetAddressList(&addr));
207
[email protected]83039bb2011-12-09 18:43:55208 net::TestCompletionCallback callback;
[email protected]8df162a2010-08-07 01:10:02209 net::CapturingNetLog log(net::CapturingNetLog::kUnbounded);
[email protected]3268023f2011-05-05 00:08:10210 net::StreamSocket* transport = new net::TCPClientSocket(
[email protected]0a0b7682010-08-25 17:08:07211 addr, &log, net::NetLog::Source());
[email protected]83039bb2011-12-09 18:43:55212 int rv = transport->Connect(callback.callback());
[email protected]8df162a2010-08-07 01:10:02213 if (rv == net::ERR_IO_PENDING)
214 rv = callback.WaitForResult();
215 EXPECT_EQ(net::OK, rv);
216
217 scoped_ptr<net::SSLClientSocket> sock(
[email protected]822581d2010-12-16 17:27:15218 CreateSSLClientSocket(transport, test_server.host_port_pair(),
219 kDefaultSSLConfig));
[email protected]8df162a2010-08-07 01:10:02220
221 EXPECT_FALSE(sock->IsConnected());
222
[email protected]83039bb2011-12-09 18:43:55223 rv = sock->Connect(callback.callback());
[email protected]b2fcd0e2010-12-01 15:19:40224
225 net::CapturingNetLog::EntryList entries;
226 log.GetEntries(&entries);
[email protected]8df162a2010-08-07 01:10:02227 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]b2fcd0e2010-12-01 15:19:40228 entries, 5, net::NetLog::TYPE_SSL_CONNECT));
[email protected]6526380c2010-11-10 04:40:33229 if (rv == net::ERR_IO_PENDING)
[email protected]8df162a2010-08-07 01:10:02230 rv = callback.WaitForResult();
[email protected]8df162a2010-08-07 01:10:02231
[email protected]b2fcd0e2010-12-01 15:19:40232 log.GetEntries(&entries);
[email protected]091fd3b72011-03-14 03:23:52233 // Because we prematurely kill the handshake at CertificateRequest,
234 // the server may still send data (notably the ServerHelloDone)
235 // after the error is returned. As a result, the SSL_CONNECT may not
236 // be the last entry. See http://crbug.com/54445. We use
237 // ExpectLogContainsSomewhere instead of
238 // LogContainsSSLConnectEndEvent to avoid assuming, e.g., only one
239 // extra read instead of two. This occurs before the handshake ends,
240 // so the corking logic of LogContainsSSLConnectEndEvent isn't
241 // necessary.
242 //
243 // TODO(davidben): When SSL_RestartHandshakeAfterCertReq in NSS is
244 // fixed and we can respond to the first CertificateRequest
245 // without closing the socket, add a unit test for sending the
246 // certificate. This test may still be useful as we'll want to close
247 // the socket on a timeout if the user takes a long time to pick a
248 // cert. Related bug: https://bugzilla.mozilla.org/show_bug.cgi?id=542832
249 net::ExpectLogContainsSomewhere(
250 entries, 0, net::NetLog::TYPE_SSL_CONNECT, net::NetLog::PHASE_END);
[email protected]6526380c2010-11-10 04:40:33251 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv);
252 EXPECT_FALSE(sock->IsConnected());
[email protected]8df162a2010-08-07 01:10:02253}
254
[email protected]65a3b912010-08-21 05:46:58255// Connect to a server requesting optional client authentication. Send it a
256// null certificate. It should allow the connection.
257//
258// TODO(davidben): Also test providing an actual certificate.
259TEST_F(SSLClientSocketTest, ConnectClientAuthSendNullCert) {
[email protected]347599952010-10-28 11:57:36260 net::TestServer::HTTPSOptions https_options;
261 https_options.request_client_certificate = true;
262 net::TestServer test_server(https_options, FilePath());
[email protected]65a3b912010-08-21 05:46:58263 ASSERT_TRUE(test_server.Start());
264
265 net::AddressList addr;
266 ASSERT_TRUE(test_server.GetAddressList(&addr));
267
[email protected]83039bb2011-12-09 18:43:55268 net::TestCompletionCallback callback;
[email protected]65a3b912010-08-21 05:46:58269 net::CapturingNetLog log(net::CapturingNetLog::kUnbounded);
[email protected]3268023f2011-05-05 00:08:10270 net::StreamSocket* transport = new net::TCPClientSocket(
[email protected]0a0b7682010-08-25 17:08:07271 addr, &log, net::NetLog::Source());
[email protected]83039bb2011-12-09 18:43:55272 int rv = transport->Connect(callback.callback());
[email protected]65a3b912010-08-21 05:46:58273 if (rv == net::ERR_IO_PENDING)
274 rv = callback.WaitForResult();
275 EXPECT_EQ(net::OK, rv);
276
277 net::SSLConfig ssl_config = kDefaultSSLConfig;
278 ssl_config.send_client_cert = true;
279 ssl_config.client_cert = NULL;
280
281 scoped_ptr<net::SSLClientSocket> sock(
[email protected]822581d2010-12-16 17:27:15282 CreateSSLClientSocket(transport, test_server.host_port_pair(),
283 ssl_config));
[email protected]65a3b912010-08-21 05:46:58284
285 EXPECT_FALSE(sock->IsConnected());
286
287 // Our test server accepts certificate-less connections.
288 // TODO(davidben): Add a test which requires them and verify the error.
[email protected]83039bb2011-12-09 18:43:55289 rv = sock->Connect(callback.callback());
[email protected]b2fcd0e2010-12-01 15:19:40290
291 net::CapturingNetLog::EntryList entries;
292 log.GetEntries(&entries);
[email protected]65a3b912010-08-21 05:46:58293 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]b2fcd0e2010-12-01 15:19:40294 entries, 5, net::NetLog::TYPE_SSL_CONNECT));
[email protected]6526380c2010-11-10 04:40:33295 if (rv == net::ERR_IO_PENDING)
[email protected]65a3b912010-08-21 05:46:58296 rv = callback.WaitForResult();
[email protected]65a3b912010-08-21 05:46:58297
[email protected]6526380c2010-11-10 04:40:33298 EXPECT_EQ(net::OK, rv);
[email protected]65a3b912010-08-21 05:46:58299 EXPECT_TRUE(sock->IsConnected());
[email protected]b2fcd0e2010-12-01 15:19:40300 log.GetEntries(&entries);
301 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1));
[email protected]65a3b912010-08-21 05:46:58302
[email protected]17a60a52011-10-28 01:18:10303 // We responded to the server's certificate request with a Certificate
304 // message with no client certificate in it. ssl_info.client_cert_sent
305 // should be false in this case.
306 net::SSLInfo ssl_info;
307 sock->GetSSLInfo(&ssl_info);
308 EXPECT_FALSE(ssl_info.client_cert_sent);
309
[email protected]65a3b912010-08-21 05:46:58310 sock->Disconnect();
311 EXPECT_FALSE(sock->IsConnected());
312}
313
[email protected]b2197852009-02-19 23:27:33314// TODO(wtc): Add unit tests for IsConnectedAndIdle:
315// - Server closes an SSL connection (with a close_notify alert message).
316// - Server closes the underlying TCP connection directly.
317// - Server sends data unexpectedly.
318
[email protected]e3eb8f82010-09-21 15:25:15319TEST_F(SSLClientSocketTest, Read) {
[email protected]95409e12010-08-17 20:07:11320 net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath());
321 ASSERT_TRUE(test_server.Start());
[email protected]73e0bba2009-02-19 22:57:09322
323 net::AddressList addr;
[email protected]95409e12010-08-17 20:07:11324 ASSERT_TRUE(test_server.GetAddressList(&addr));
325
[email protected]83039bb2011-12-09 18:43:55326 net::TestCompletionCallback callback;
[email protected]3268023f2011-05-05 00:08:10327 net::StreamSocket* transport = new net::TCPClientSocket(
[email protected]0a0b7682010-08-25 17:08:07328 addr, NULL, net::NetLog::Source());
[email protected]83039bb2011-12-09 18:43:55329 int rv = transport->Connect(callback.callback());
[email protected]bacff652009-03-31 17:50:33330 if (rv == net::ERR_IO_PENDING)
331 rv = callback.WaitForResult();
332 EXPECT_EQ(net::OK, rv);
333
[email protected]aaead502008-10-15 00:20:11334 scoped_ptr<net::SSLClientSocket> sock(
[email protected]822581d2010-12-16 17:27:15335 CreateSSLClientSocket(transport, test_server.host_port_pair(),
336 kDefaultSSLConfig));
initial.commit586acc5fe2008-07-26 22:42:52337
[email protected]83039bb2011-12-09 18:43:55338 rv = sock->Connect(callback.callback());
[email protected]6526380c2010-11-10 04:40:33339 if (rv == net::ERR_IO_PENDING)
[email protected]7b822b2b2008-08-05 00:15:45340 rv = callback.WaitForResult();
[email protected]6526380c2010-11-10 04:40:33341 EXPECT_EQ(net::OK, rv);
[email protected]b43c97c2008-10-22 19:50:58342 EXPECT_TRUE(sock->IsConnected());
initial.commit586acc5fe2008-07-26 22:42:52343
[email protected]e3eb8f82010-09-21 15:25:15344 const char request_text[] = "GET / HTTP/1.0\r\n\r\n";
[email protected]ad8e04a2010-11-01 04:16:27345 scoped_refptr<net::IOBuffer> request_buffer(
346 new net::IOBuffer(arraysize(request_text) - 1));
[email protected]e3eb8f82010-09-21 15:25:15347 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1);
[email protected]ffeb0882009-04-30 21:51:25348
[email protected]83039bb2011-12-09 18:43:55349 rv = sock->Write(request_buffer, arraysize(request_text) - 1,
350 callback.callback());
initial.commit586acc5fe2008-07-26 22:42:52351 EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING);
352
[email protected]914286d62009-12-10 23:06:44353 if (rv == net::ERR_IO_PENDING)
initial.commit586acc5fe2008-07-26 22:42:52354 rv = callback.WaitForResult();
[email protected]e3eb8f82010-09-21 15:25:15355 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv);
initial.commit586acc5fe2008-07-26 22:42:52356
[email protected]ad8e04a2010-11-01 04:16:27357 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(4096));
initial.commit586acc5fe2008-07-26 22:42:52358 for (;;) {
[email protected]83039bb2011-12-09 18:43:55359 rv = sock->Read(buf, 4096, callback.callback());
initial.commit586acc5fe2008-07-26 22:42:52360 EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING);
361
362 if (rv == net::ERR_IO_PENDING)
363 rv = callback.WaitForResult();
364
[email protected]7b822b2b2008-08-05 00:15:45365 EXPECT_GE(rv, 0);
366 if (rv <= 0)
initial.commit586acc5fe2008-07-26 22:42:52367 break;
368 }
369}
370
[email protected]914286d62009-12-10 23:06:44371// Test the full duplex mode, with Read and Write pending at the same time.
372// This test also serves as a regression test for http://crbug.com/29815.
373TEST_F(SSLClientSocketTest, Read_FullDuplex) {
[email protected]95409e12010-08-17 20:07:11374 net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath());
375 ASSERT_TRUE(test_server.Start());
[email protected]914286d62009-12-10 23:06:44376
377 net::AddressList addr;
[email protected]95409e12010-08-17 20:07:11378 ASSERT_TRUE(test_server.GetAddressList(&addr));
379
[email protected]83039bb2011-12-09 18:43:55380 net::TestCompletionCallback callback; // Used for everything except Write.
[email protected]914286d62009-12-10 23:06:44381
[email protected]3268023f2011-05-05 00:08:10382 net::StreamSocket* transport = new net::TCPClientSocket(
[email protected]0a0b7682010-08-25 17:08:07383 addr, NULL, net::NetLog::Source());
[email protected]83039bb2011-12-09 18:43:55384 int rv = transport->Connect(callback.callback());
[email protected]914286d62009-12-10 23:06:44385 if (rv == net::ERR_IO_PENDING)
386 rv = callback.WaitForResult();
387 EXPECT_EQ(net::OK, rv);
388
[email protected]feb79bcd2011-07-21 16:55:17389 net::SSLClientSocketContext context;
390 context.cert_verifier = cert_verifier_.get();
[email protected]914286d62009-12-10 23:06:44391 scoped_ptr<net::SSLClientSocket> sock(
[email protected]73c45322010-10-01 23:57:54392 socket_factory_->CreateSSLClientSocket(
[email protected]822581d2010-12-16 17:27:15393 transport, test_server.host_port_pair(), kDefaultSSLConfig,
[email protected]feb79bcd2011-07-21 16:55:17394 NULL, context));
[email protected]914286d62009-12-10 23:06:44395
[email protected]83039bb2011-12-09 18:43:55396 rv = sock->Connect(callback.callback());
[email protected]6526380c2010-11-10 04:40:33397 if (rv == net::ERR_IO_PENDING)
[email protected]914286d62009-12-10 23:06:44398 rv = callback.WaitForResult();
[email protected]6526380c2010-11-10 04:40:33399 EXPECT_EQ(net::OK, rv);
[email protected]914286d62009-12-10 23:06:44400 EXPECT_TRUE(sock->IsConnected());
401
402 // Issue a "hanging" Read first.
[email protected]ad8e04a2010-11-01 04:16:27403 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(4096));
[email protected]83039bb2011-12-09 18:43:55404 rv = sock->Read(buf, 4096, callback.callback());
[email protected]914286d62009-12-10 23:06:44405 // We haven't written the request, so there should be no response yet.
406 ASSERT_EQ(net::ERR_IO_PENDING, rv);
407
408 // Write the request.
409 // The request is padded with a User-Agent header to a size that causes the
410 // memio circular buffer (4k bytes) in SSLClientSocketNSS to wrap around.
411 // This tests the fix for http://crbug.com/29815.
412 std::string request_text = "GET / HTTP/1.1\r\nUser-Agent: long browser name ";
[email protected]73343a32011-08-18 02:32:16413 for (int i = 0; i < 3770; ++i)
[email protected]914286d62009-12-10 23:06:44414 request_text.push_back('*');
415 request_text.append("\r\n\r\n");
[email protected]ad8e04a2010-11-01 04:16:27416 scoped_refptr<net::IOBuffer> request_buffer(
417 new net::StringIOBuffer(request_text));
[email protected]914286d62009-12-10 23:06:44418
[email protected]83039bb2011-12-09 18:43:55419 net::TestCompletionCallback callback2; // Used for Write only.
420 rv = sock->Write(request_buffer, request_text.size(), callback2.callback());
[email protected]914286d62009-12-10 23:06:44421 EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING);
422
423 if (rv == net::ERR_IO_PENDING)
424 rv = callback2.WaitForResult();
425 EXPECT_EQ(static_cast<int>(request_text.size()), rv);
426
427 // Now get the Read result.
428 rv = callback.WaitForResult();
429 EXPECT_GT(rv, 0);
430}
431
[email protected]010e27e2009-08-27 17:49:41432TEST_F(SSLClientSocketTest, Read_SmallChunks) {
[email protected]95409e12010-08-17 20:07:11433 net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath());
434 ASSERT_TRUE(test_server.Start());
[email protected]73e0bba2009-02-19 22:57:09435
initial.commit586acc5fe2008-07-26 22:42:52436 net::AddressList addr;
[email protected]95409e12010-08-17 20:07:11437 ASSERT_TRUE(test_server.GetAddressList(&addr));
438
[email protected]83039bb2011-12-09 18:43:55439 net::TestCompletionCallback callback;
[email protected]3268023f2011-05-05 00:08:10440 net::StreamSocket* transport = new net::TCPClientSocket(
[email protected]0a0b7682010-08-25 17:08:07441 addr, NULL, net::NetLog::Source());
[email protected]83039bb2011-12-09 18:43:55442 int rv = transport->Connect(callback.callback());
[email protected]bacff652009-03-31 17:50:33443 if (rv == net::ERR_IO_PENDING)
444 rv = callback.WaitForResult();
445 EXPECT_EQ(net::OK, rv);
446
[email protected]aaead502008-10-15 00:20:11447 scoped_ptr<net::SSLClientSocket> sock(
[email protected]822581d2010-12-16 17:27:15448 CreateSSLClientSocket(transport, test_server.host_port_pair(),
449 kDefaultSSLConfig));
initial.commit586acc5fe2008-07-26 22:42:52450
[email protected]83039bb2011-12-09 18:43:55451 rv = sock->Connect(callback.callback());
[email protected]6526380c2010-11-10 04:40:33452 if (rv == net::ERR_IO_PENDING)
[email protected]7b822b2b2008-08-05 00:15:45453 rv = callback.WaitForResult();
[email protected]6526380c2010-11-10 04:40:33454 EXPECT_EQ(net::OK, rv);
initial.commit586acc5fe2008-07-26 22:42:52455
456 const char request_text[] = "GET / HTTP/1.0\r\n\r\n";
[email protected]ad8e04a2010-11-01 04:16:27457 scoped_refptr<net::IOBuffer> request_buffer(
458 new net::IOBuffer(arraysize(request_text) - 1));
[email protected]ffeb0882009-04-30 21:51:25459 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1);
460
[email protected]83039bb2011-12-09 18:43:55461 rv = sock->Write(request_buffer, arraysize(request_text) - 1,
462 callback.callback());
initial.commit586acc5fe2008-07-26 22:42:52463 EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING);
464
[email protected]914286d62009-12-10 23:06:44465 if (rv == net::ERR_IO_PENDING)
initial.commit586acc5fe2008-07-26 22:42:52466 rv = callback.WaitForResult();
[email protected]914286d62009-12-10 23:06:44467 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv);
initial.commit586acc5fe2008-07-26 22:42:52468
[email protected]ad8e04a2010-11-01 04:16:27469 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(1));
initial.commit586acc5fe2008-07-26 22:42:52470 for (;;) {
[email protected]83039bb2011-12-09 18:43:55471 rv = sock->Read(buf, 1, callback.callback());
initial.commit586acc5fe2008-07-26 22:42:52472 EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING);
473
474 if (rv == net::ERR_IO_PENDING)
475 rv = callback.WaitForResult();
476
[email protected]7b822b2b2008-08-05 00:15:45477 EXPECT_GE(rv, 0);
478 if (rv <= 0)
initial.commit586acc5fe2008-07-26 22:42:52479 break;
480 }
481}
482
[email protected]010e27e2009-08-27 17:49:41483TEST_F(SSLClientSocketTest, Read_Interrupted) {
[email protected]95409e12010-08-17 20:07:11484 net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath());
485 ASSERT_TRUE(test_server.Start());
[email protected]73e0bba2009-02-19 22:57:09486
initial.commit586acc5fe2008-07-26 22:42:52487 net::AddressList addr;
[email protected]95409e12010-08-17 20:07:11488 ASSERT_TRUE(test_server.GetAddressList(&addr));
489
[email protected]83039bb2011-12-09 18:43:55490 net::TestCompletionCallback callback;
[email protected]3268023f2011-05-05 00:08:10491 net::StreamSocket* transport = new net::TCPClientSocket(
[email protected]0a0b7682010-08-25 17:08:07492 addr, NULL, net::NetLog::Source());
[email protected]83039bb2011-12-09 18:43:55493 int rv = transport->Connect(callback.callback());
[email protected]bacff652009-03-31 17:50:33494 if (rv == net::ERR_IO_PENDING)
495 rv = callback.WaitForResult();
496 EXPECT_EQ(net::OK, rv);
497
[email protected]aaead502008-10-15 00:20:11498 scoped_ptr<net::SSLClientSocket> sock(
[email protected]822581d2010-12-16 17:27:15499 CreateSSLClientSocket(transport, test_server.host_port_pair(),
500 kDefaultSSLConfig));
initial.commit586acc5fe2008-07-26 22:42:52501
[email protected]83039bb2011-12-09 18:43:55502 rv = sock->Connect(callback.callback());
[email protected]6526380c2010-11-10 04:40:33503 if (rv == net::ERR_IO_PENDING)
[email protected]7b822b2b2008-08-05 00:15:45504 rv = callback.WaitForResult();
[email protected]6526380c2010-11-10 04:40:33505 EXPECT_EQ(net::OK, rv);
initial.commit586acc5fe2008-07-26 22:42:52506
507 const char request_text[] = "GET / HTTP/1.0\r\n\r\n";
[email protected]ad8e04a2010-11-01 04:16:27508 scoped_refptr<net::IOBuffer> request_buffer(
509 new net::IOBuffer(arraysize(request_text) - 1));
[email protected]ffeb0882009-04-30 21:51:25510 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1);
511
[email protected]83039bb2011-12-09 18:43:55512 rv = sock->Write(request_buffer, arraysize(request_text) - 1,
513 callback.callback());
initial.commit586acc5fe2008-07-26 22:42:52514 EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING);
515
[email protected]914286d62009-12-10 23:06:44516 if (rv == net::ERR_IO_PENDING)
initial.commit586acc5fe2008-07-26 22:42:52517 rv = callback.WaitForResult();
[email protected]914286d62009-12-10 23:06:44518 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv);
initial.commit586acc5fe2008-07-26 22:42:52519
520 // Do a partial read and then exit. This test should not crash!
[email protected]ad8e04a2010-11-01 04:16:27521 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(512));
[email protected]83039bb2011-12-09 18:43:55522 rv = sock->Read(buf, 512, callback.callback());
[email protected]914286d62009-12-10 23:06:44523 EXPECT_TRUE(rv > 0 || rv == net::ERR_IO_PENDING);
initial.commit586acc5fe2008-07-26 22:42:52524
525 if (rv == net::ERR_IO_PENDING)
526 rv = callback.WaitForResult();
527
[email protected]914286d62009-12-10 23:06:44528 EXPECT_GT(rv, 0);
initial.commit586acc5fe2008-07-26 22:42:52529}
[email protected]39afe642010-04-29 14:55:18530
[email protected]1d872d32011-05-19 02:45:33531TEST_F(SSLClientSocketTest, Read_FullLogging) {
532 net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath());
533 ASSERT_TRUE(test_server.Start());
534
535 net::AddressList addr;
536 ASSERT_TRUE(test_server.GetAddressList(&addr));
537
[email protected]83039bb2011-12-09 18:43:55538 net::TestCompletionCallback callback;
[email protected]1d872d32011-05-19 02:45:33539 net::CapturingNetLog log(net::CapturingNetLog::kUnbounded);
540 log.SetLogLevel(net::NetLog::LOG_ALL);
541 net::StreamSocket* transport = new net::TCPClientSocket(
542 addr, &log, net::NetLog::Source());
[email protected]83039bb2011-12-09 18:43:55543 int rv = transport->Connect(callback.callback());
[email protected]1d872d32011-05-19 02:45:33544 if (rv == net::ERR_IO_PENDING)
545 rv = callback.WaitForResult();
546 EXPECT_EQ(net::OK, rv);
547
548 scoped_ptr<net::SSLClientSocket> sock(
549 CreateSSLClientSocket(transport, test_server.host_port_pair(),
550 kDefaultSSLConfig));
551
[email protected]83039bb2011-12-09 18:43:55552 rv = sock->Connect(callback.callback());
[email protected]1d872d32011-05-19 02:45:33553 if (rv == net::ERR_IO_PENDING)
554 rv = callback.WaitForResult();
555 EXPECT_EQ(net::OK, rv);
556 EXPECT_TRUE(sock->IsConnected());
557
558 const char request_text[] = "GET / HTTP/1.0\r\n\r\n";
559 scoped_refptr<net::IOBuffer> request_buffer(
560 new net::IOBuffer(arraysize(request_text) - 1));
561 memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1);
562
[email protected]83039bb2011-12-09 18:43:55563 rv = sock->Write(request_buffer, arraysize(request_text) - 1,
564 callback.callback());
[email protected]1d872d32011-05-19 02:45:33565 EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING);
566
567 if (rv == net::ERR_IO_PENDING)
568 rv = callback.WaitForResult();
569 EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv);
570
571 net::CapturingNetLog::EntryList entries;
572 log.GetEntries(&entries);
573 size_t last_index = net::ExpectLogContainsSomewhereAfter(
574 entries, 5, net::NetLog::TYPE_SSL_SOCKET_BYTES_SENT,
575 net::NetLog::PHASE_NONE);
576
577 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(4096));
578 for (;;) {
[email protected]83039bb2011-12-09 18:43:55579 rv = sock->Read(buf, 4096, callback.callback());
[email protected]1d872d32011-05-19 02:45:33580 EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING);
581
582 if (rv == net::ERR_IO_PENDING)
583 rv = callback.WaitForResult();
584
585 EXPECT_GE(rv, 0);
586 if (rv <= 0)
587 break;
588
589 log.GetEntries(&entries);
590 last_index = net::ExpectLogContainsSomewhereAfter(
591 entries, last_index + 1, net::NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED,
592 net::NetLog::PHASE_NONE);
593 }
594}
595
[email protected]39afe642010-04-29 14:55:18596// Regression test for http://crbug.com/42538
597TEST_F(SSLClientSocketTest, PrematureApplicationData) {
[email protected]95409e12010-08-17 20:07:11598 net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath());
599 ASSERT_TRUE(test_server.Start());
600
[email protected]39afe642010-04-29 14:55:18601 net::AddressList addr;
[email protected]83039bb2011-12-09 18:43:55602 net::TestCompletionCallback callback;
[email protected]39afe642010-04-29 14:55:18603
604 static const unsigned char application_data[] = {
605 0x17, 0x03, 0x01, 0x00, 0x4a, 0x02, 0x00, 0x00, 0x46, 0x03, 0x01, 0x4b,
606 0xc2, 0xf8, 0xb2, 0xc1, 0x56, 0x42, 0xb9, 0x57, 0x7f, 0xde, 0x87, 0x46,
607 0xf7, 0xa3, 0x52, 0x42, 0x21, 0xf0, 0x13, 0x1c, 0x9c, 0x83, 0x88, 0xd6,
608 0x93, 0x0c, 0xf6, 0x36, 0x30, 0x05, 0x7e, 0x20, 0xb5, 0xb5, 0x73, 0x36,
609 0x53, 0x83, 0x0a, 0xfc, 0x17, 0x63, 0xbf, 0xa0, 0xe4, 0x42, 0x90, 0x0d,
610 0x2f, 0x18, 0x6d, 0x20, 0xd8, 0x36, 0x3f, 0xfc, 0xe6, 0x01, 0xfa, 0x0f,
611 0xa5, 0x75, 0x7f, 0x09, 0x00, 0x04, 0x00, 0x16, 0x03, 0x01, 0x11, 0x57,
612 0x0b, 0x00, 0x11, 0x53, 0x00, 0x11, 0x50, 0x00, 0x06, 0x22, 0x30, 0x82,
613 0x06, 0x1e, 0x30, 0x82, 0x05, 0x06, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
614 0x0a
615 };
616
617 // All reads and writes complete synchronously (async=false).
618 net::MockRead data_reads[] = {
619 net::MockRead(false, reinterpret_cast<const char*>(application_data),
620 arraysize(application_data)),
621 net::MockRead(false, net::OK),
622 };
623
624 net::StaticSocketDataProvider data(data_reads, arraysize(data_reads),
625 NULL, 0);
626
[email protected]3268023f2011-05-05 00:08:10627 net::StreamSocket* transport =
[email protected]39afe642010-04-29 14:55:18628 new net::MockTCPClientSocket(addr, NULL, &data);
[email protected]83039bb2011-12-09 18:43:55629 int rv = transport->Connect(callback.callback());
[email protected]39afe642010-04-29 14:55:18630 if (rv == net::ERR_IO_PENDING)
631 rv = callback.WaitForResult();
632 EXPECT_EQ(net::OK, rv);
633
634 scoped_ptr<net::SSLClientSocket> sock(
[email protected]822581d2010-12-16 17:27:15635 CreateSSLClientSocket(transport, test_server.host_port_pair(),
636 kDefaultSSLConfig));
[email protected]39afe642010-04-29 14:55:18637
[email protected]83039bb2011-12-09 18:43:55638 rv = sock->Connect(callback.callback());
[email protected]39afe642010-04-29 14:55:18639 EXPECT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv);
640}
[email protected]47f7d742010-11-11 04:12:53641
[email protected]109805a2010-12-07 18:17:06642// TODO(rsleevi): Not implemented for Schannel. As Schannel is only used when
[email protected]47f7d742010-11-11 04:12:53643// performing client authentication, it will not be tested here.
[email protected]7deea3d2011-01-09 06:03:41644TEST_F(SSLClientSocketTest, CipherSuiteDisables) {
[email protected]47f7d742010-11-11 04:12:53645 // Rather than exhaustively disabling every RC4 ciphersuite defined at
646 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml,
647 // only disabling those cipher suites that the test server actually
648 // implements.
649 const uint16 kCiphersToDisable[] = {
650 0x0005, // TLS_RSA_WITH_RC4_128_SHA
651 };
652
653 net::TestServer::HTTPSOptions https_options;
654 // Enable only RC4 on the test server.
655 https_options.bulk_ciphers =
656 net::TestServer::HTTPSOptions::BULK_CIPHER_RC4;
657 net::TestServer test_server(https_options, FilePath());
658 ASSERT_TRUE(test_server.Start());
659
660 net::AddressList addr;
661 ASSERT_TRUE(test_server.GetAddressList(&addr));
662
[email protected]83039bb2011-12-09 18:43:55663 net::TestCompletionCallback callback;
[email protected]47f7d742010-11-11 04:12:53664 net::CapturingNetLog log(net::CapturingNetLog::kUnbounded);
[email protected]3268023f2011-05-05 00:08:10665 net::StreamSocket* transport = new net::TCPClientSocket(
[email protected]47f7d742010-11-11 04:12:53666 addr, &log, net::NetLog::Source());
[email protected]83039bb2011-12-09 18:43:55667 int rv = transport->Connect(callback.callback());
[email protected]47f7d742010-11-11 04:12:53668 if (rv == net::ERR_IO_PENDING)
669 rv = callback.WaitForResult();
670 EXPECT_EQ(net::OK, rv);
671
672 net::SSLConfig ssl_config;
673 for (size_t i = 0; i < arraysize(kCiphersToDisable); ++i)
674 ssl_config.disabled_cipher_suites.push_back(kCiphersToDisable[i]);
675
676 scoped_ptr<net::SSLClientSocket> sock(
[email protected]822581d2010-12-16 17:27:15677 CreateSSLClientSocket(transport, test_server.host_port_pair(),
678 ssl_config));
[email protected]47f7d742010-11-11 04:12:53679
680 EXPECT_FALSE(sock->IsConnected());
681
[email protected]83039bb2011-12-09 18:43:55682 rv = sock->Connect(callback.callback());
[email protected]b2fcd0e2010-12-01 15:19:40683 net::CapturingNetLog::EntryList entries;
684 log.GetEntries(&entries);
[email protected]47f7d742010-11-11 04:12:53685 EXPECT_TRUE(net::LogContainsBeginEvent(
[email protected]b2fcd0e2010-12-01 15:19:40686 entries, 5, net::NetLog::TYPE_SSL_CONNECT));
[email protected]47f7d742010-11-11 04:12:53687
688 // NSS has special handling that maps a handshake_failure alert received
689 // immediately after a client_hello to be a mismatched cipher suite error,
690 // leading to ERR_SSL_VERSION_OR_CIPHER_MISMATCH. When using OpenSSL or
691 // Secure Transport (OS X), the handshake_failure is bubbled up without any
692 // interpretation, leading to ERR_SSL_PROTOCOL_ERROR. Either way, a failure
693 // indicates that no cipher suite was negotiated with the test server.
694 if (rv == net::ERR_IO_PENDING)
695 rv = callback.WaitForResult();
696 EXPECT_TRUE(rv == net::ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
697 rv == net::ERR_SSL_PROTOCOL_ERROR);
698 // The exact ordering differs between SSLClientSocketNSS (which issues an
699 // extra read) and SSLClientSocketMac (which does not). Just make sure the
700 // error appears somewhere in the log.
[email protected]b2fcd0e2010-12-01 15:19:40701 log.GetEntries(&entries);
702 net::ExpectLogContainsSomewhere(entries, 0,
[email protected]47f7d742010-11-11 04:12:53703 net::NetLog::TYPE_SSL_HANDSHAKE_ERROR,
704 net::NetLog::PHASE_NONE);
705
706 // We cannot test sock->IsConnected(), as the NSS implementation disconnects
707 // the socket when it encounters an error, whereas other implementations
708 // leave it connected.
[email protected]7deea3d2011-01-09 06:03:41709 // Because this an error that the test server is mutually aware of, as opposed
710 // to being an error such as a certificate name mismatch, which is
711 // client-only, the exact index of the SSL connect end depends on how
712 // quickly the test server closes the underlying socket. If the test server
713 // closes before the IO message loop pumps messages, there may be a 0-byte
714 // Read event in the NetLog due to TCPClientSocket picking up the EOF. As a
715 // result, the SSL connect end event will be the second-to-last entry,
716 // rather than the last entry.
717 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1) ||
718 LogContainsSSLConnectEndEvent(entries, -2));
[email protected]47f7d742010-11-11 04:12:53719}
[email protected]b442da32011-08-16 19:32:28720
721// When creating an SSLClientSocket, it is allowed to pass in a
722// ClientSocketHandle that is not obtained from a client socket pool.
723// Here we verify that such a simple ClientSocketHandle, not associated with any
724// client socket pool, can be destroyed safely.
725TEST_F(SSLClientSocketTest, ClientSocketHandleNotFromPool) {
726 net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath());
727 ASSERT_TRUE(test_server.Start());
728
729 net::AddressList addr;
730 ASSERT_TRUE(test_server.GetAddressList(&addr));
731
[email protected]83039bb2011-12-09 18:43:55732 net::TestCompletionCallback callback;
[email protected]b442da32011-08-16 19:32:28733 net::StreamSocket* transport = new net::TCPClientSocket(
734 addr, NULL, net::NetLog::Source());
[email protected]83039bb2011-12-09 18:43:55735 int rv = transport->Connect(callback.callback());
[email protected]b442da32011-08-16 19:32:28736 if (rv == net::ERR_IO_PENDING)
737 rv = callback.WaitForResult();
738 EXPECT_EQ(net::OK, rv);
739
740 net::ClientSocketHandle* socket_handle = new net::ClientSocketHandle();
741 socket_handle->set_socket(transport);
742
743 net::SSLClientSocketContext context;
744 context.cert_verifier = cert_verifier_.get();
745 scoped_ptr<net::SSLClientSocket> ssl_socket(
746 socket_factory_->CreateSSLClientSocket(
747 socket_handle, test_server.host_port_pair(), kDefaultSSLConfig,
748 NULL, context));
749
750 EXPECT_FALSE(ssl_socket->IsConnected());
[email protected]83039bb2011-12-09 18:43:55751 rv = ssl_socket->Connect(callback.callback());
[email protected]b442da32011-08-16 19:32:28752 if (rv == net::ERR_IO_PENDING)
753 rv = callback.WaitForResult();
754 EXPECT_EQ(net::OK, rv);
755}
[email protected]c3456bb2011-12-12 22:22:19756
757// Verifies that SSLClientSocket::ClearSessionCache can be called without
758// explicit NSS initialization.
759TEST(SSLClientSocket, ClearSessionCache) {
760 net::SSLClientSocket::ClearSessionCache();
761}