[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 5 | #include "net/socket/ssl_client_socket.h" |
| 6 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 7 | #include "net/base/address_list.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 8 | #include "net/base/host_resolver.h" |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 9 | #include "net/base/io_buffer.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 10 | #include "net/base/net_log.h" |
| 11 | #include "net/base/net_log_unittest.h" |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 12 | #include "net/base/net_errors.h" |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 13 | #include "net/base/ssl_config_service.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 14 | #include "net/base/test_completion_callback.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 15 | #include "net/socket/client_socket_factory.h" |
[email protected] | 39afe64 | 2010-04-29 14:55:18 | [diff] [blame] | 16 | #include "net/socket/socket_test_util.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 17 | #include "net/socket/tcp_client_socket.h" |
[email protected] | 1b9565c | 2010-07-21 01:19:31 | [diff] [blame] | 18 | #include "net/test/test_server.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 20 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | |
| 22 | //----------------------------------------------------------------------------- |
| 23 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 24 | const net::SSLConfig kDefaultSSLConfig; |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 25 | |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 26 | class SSLClientSocketTest : public PlatformTest { |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 27 | public: |
| 28 | SSLClientSocketTest() |
[email protected] | 962b9821 | 2010-07-17 03:37:51 | [diff] [blame] | 29 | : resolver_(net::CreateSystemHostResolver( |
| 30 | net::HostResolver::kDefaultParallelism)), |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 31 | socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) { |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 34 | protected: |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 35 | scoped_refptr<net::HostResolver> resolver_; |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 36 | net::ClientSocketFactory* socket_factory_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 37 | }; |
| 38 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 39 | //----------------------------------------------------------------------------- |
| 40 | |
[email protected] | 010e27e | 2009-08-27 17:49:41 | [diff] [blame] | 41 | TEST_F(SSLClientSocketTest, Connect) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 42 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath()); |
| 43 | ASSERT_TRUE(test_server.Start()); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 44 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 45 | net::AddressList addr; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 46 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 47 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 48 | TestCompletionCallback callback; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 49 | net::CapturingNetLog log(net::CapturingNetLog::kUnbounded); |
| 50 | net::ClientSocket* transport = new net::TCPClientSocket(addr, &log); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 51 | int rv = transport->Connect(&callback); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 52 | if (rv == net::ERR_IO_PENDING) |
| 53 | rv = callback.WaitForResult(); |
| 54 | EXPECT_EQ(net::OK, rv); |
| 55 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 56 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 57 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 58 | test_server.host_port_pair().host(), kDefaultSSLConfig)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 59 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 60 | EXPECT_FALSE(sock->IsConnected()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 61 | |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 62 | rv = sock->Connect(&callback); |
[email protected] | eb8605c | 2010-05-21 22:17:47 | [diff] [blame] | 63 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 64 | log.entries(), 5, net::NetLog::TYPE_SSL_CONNECT)); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 65 | if (rv != net::OK) { |
| 66 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 67 | EXPECT_FALSE(sock->IsConnected()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 68 | EXPECT_FALSE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 69 | log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 70 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 71 | rv = callback.WaitForResult(); |
| 72 | EXPECT_EQ(net::OK, rv); |
| 73 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 74 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 75 | EXPECT_TRUE(sock->IsConnected()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 76 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 77 | log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 78 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 79 | sock->Disconnect(); |
| 80 | EXPECT_FALSE(sock->IsConnected()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 010e27e | 2009-08-27 17:49:41 | [diff] [blame] | 83 | TEST_F(SSLClientSocketTest, ConnectExpired) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 84 | net::TestServer test_server(net::TestServer::TYPE_HTTPS_EXPIRED_CERTIFICATE, |
| 85 | FilePath()); |
| 86 | ASSERT_TRUE(test_server.Start()); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 87 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 88 | net::AddressList addr; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 89 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 90 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 91 | TestCompletionCallback callback; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 92 | net::CapturingNetLog log(net::CapturingNetLog::kUnbounded); |
| 93 | net::ClientSocket* transport = new net::TCPClientSocket(addr, &log); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 94 | int rv = transport->Connect(&callback); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 95 | if (rv == net::ERR_IO_PENDING) |
| 96 | rv = callback.WaitForResult(); |
| 97 | EXPECT_EQ(net::OK, rv); |
| 98 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 99 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 100 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 101 | test_server.host_port_pair().host(), kDefaultSSLConfig)); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 102 | |
| 103 | EXPECT_FALSE(sock->IsConnected()); |
| 104 | |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 105 | rv = sock->Connect(&callback); |
[email protected] | eb8605c | 2010-05-21 22:17:47 | [diff] [blame] | 106 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 107 | log.entries(), 5, net::NetLog::TYPE_SSL_CONNECT)); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 108 | if (rv != net::OK) { |
| 109 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 110 | EXPECT_FALSE(sock->IsConnected()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 111 | EXPECT_FALSE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 112 | log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 113 | |
| 114 | rv = callback.WaitForResult(); |
| 115 | EXPECT_EQ(net::ERR_CERT_DATE_INVALID, rv); |
| 116 | } |
| 117 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 118 | // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 119 | // the socket when it encounters an error, whereas other implementations |
| 120 | // leave it connected. |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 121 | |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 122 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 123 | log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | 010e27e | 2009-08-27 17:49:41 | [diff] [blame] | 126 | TEST_F(SSLClientSocketTest, ConnectMismatched) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 127 | net::TestServer test_server(net::TestServer::TYPE_HTTPS_MISMATCHED_HOSTNAME, |
| 128 | FilePath()); |
| 129 | ASSERT_TRUE(test_server.Start()); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 130 | |
| 131 | net::AddressList addr; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 132 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 133 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 134 | TestCompletionCallback callback; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 135 | net::CapturingNetLog log(net::CapturingNetLog::kUnbounded); |
| 136 | net::ClientSocket* transport = new net::TCPClientSocket(addr, &log); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 137 | int rv = transport->Connect(&callback); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 138 | if (rv == net::ERR_IO_PENDING) |
| 139 | rv = callback.WaitForResult(); |
| 140 | EXPECT_EQ(net::OK, rv); |
| 141 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 142 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 143 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 144 | test_server.host_port_pair().host(), kDefaultSSLConfig)); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 145 | |
| 146 | EXPECT_FALSE(sock->IsConnected()); |
| 147 | |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 148 | rv = sock->Connect(&callback); |
[email protected] | 4b32be9 | 2010-05-20 03:21:42 | [diff] [blame] | 149 | |
[email protected] | eb8605c | 2010-05-21 22:17:47 | [diff] [blame] | 150 | EXPECT_TRUE(net::LogContainsBeginEvent( |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 151 | log.entries(), 5, net::NetLog::TYPE_SSL_CONNECT)); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 152 | if (rv != net::ERR_CERT_COMMON_NAME_INVALID) { |
| 153 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 154 | EXPECT_FALSE(sock->IsConnected()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 155 | EXPECT_FALSE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 156 | log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 157 | |
| 158 | rv = callback.WaitForResult(); |
| 159 | EXPECT_EQ(net::ERR_CERT_COMMON_NAME_INVALID, rv); |
| 160 | } |
| 161 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 162 | // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 163 | // the socket when it encounters an error, whereas other implementations |
| 164 | // leave it connected. |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 165 | |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 166 | EXPECT_TRUE(net::LogContainsEndEvent( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 167 | log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 170 | // TODO(davidben): Also test providing a certificate. |
| 171 | TEST_F(SSLClientSocketTest, ConnectClientAuthNoCert) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 172 | net::TestServer test_server(net::TestServer::TYPE_HTTPS_CLIENT_AUTH, |
| 173 | FilePath()); |
| 174 | ASSERT_TRUE(test_server.Start()); |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 175 | |
| 176 | net::AddressList addr; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 177 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 178 | |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 179 | TestCompletionCallback callback; |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 180 | net::CapturingNetLog log(net::CapturingNetLog::kUnbounded); |
| 181 | net::ClientSocket* transport = new net::TCPClientSocket(addr, &log); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 182 | int rv = transport->Connect(&callback); |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 183 | if (rv == net::ERR_IO_PENDING) |
| 184 | rv = callback.WaitForResult(); |
| 185 | EXPECT_EQ(net::OK, rv); |
| 186 | |
| 187 | scoped_ptr<net::SSLClientSocket> sock( |
| 188 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 189 | test_server.host_port_pair().host(), kDefaultSSLConfig)); |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 190 | |
| 191 | EXPECT_FALSE(sock->IsConnected()); |
| 192 | |
| 193 | rv = sock->Connect(&callback); |
| 194 | EXPECT_TRUE(net::LogContainsBeginEvent( |
| 195 | log.entries(), 5, net::NetLog::TYPE_SSL_CONNECT)); |
| 196 | if (rv != net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 197 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 198 | EXPECT_FALSE(sock->IsConnected()); |
| 199 | EXPECT_FALSE(net::LogContainsEndEvent( |
| 200 | log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); |
| 201 | |
| 202 | rv = callback.WaitForResult(); |
| 203 | EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); |
| 204 | } |
| 205 | |
| 206 | // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 207 | // the socket when it encounters an error, whereas other implementations |
| 208 | // leave it connected. |
| 209 | |
| 210 | EXPECT_TRUE(net::LogContainsEndEvent( |
| 211 | log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); |
| 212 | } |
| 213 | |
[email protected] | b219785 | 2009-02-19 23:27:33 | [diff] [blame] | 214 | // TODO(wtc): Add unit tests for IsConnectedAndIdle: |
| 215 | // - Server closes an SSL connection (with a close_notify alert message). |
| 216 | // - Server closes the underlying TCP connection directly. |
| 217 | // - Server sends data unexpectedly. |
| 218 | |
[email protected] | 010e27e | 2009-08-27 17:49:41 | [diff] [blame] | 219 | TEST_F(SSLClientSocketTest, Read) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 220 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath()); |
| 221 | ASSERT_TRUE(test_server.Start()); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 222 | |
| 223 | net::AddressList addr; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 224 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 225 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 226 | TestCompletionCallback callback; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 227 | net::ClientSocket* transport = new net::TCPClientSocket(addr, NULL); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 228 | int rv = transport->Connect(&callback); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 229 | if (rv == net::ERR_IO_PENDING) |
| 230 | rv = callback.WaitForResult(); |
| 231 | EXPECT_EQ(net::OK, rv); |
| 232 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 233 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 234 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 235 | test_server.host_port_pair().host(), |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 236 | kDefaultSSLConfig)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 237 | |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 238 | rv = sock->Connect(&callback); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 239 | if (rv != net::OK) { |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 240 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 241 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 242 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 243 | EXPECT_EQ(net::OK, rv); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 244 | } |
[email protected] | b43c97c | 2008-10-22 19:50:58 | [diff] [blame] | 245 | EXPECT_TRUE(sock->IsConnected()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 246 | |
| 247 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 248 | scoped_refptr<net::IOBuffer> request_buffer = |
| 249 | new net::IOBuffer(arraysize(request_text) - 1); |
| 250 | memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 251 | |
| 252 | rv = sock->Write(request_buffer, arraysize(request_text) - 1, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 253 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 254 | |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 255 | if (rv == net::ERR_IO_PENDING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 256 | rv = callback.WaitForResult(); |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 257 | EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 258 | |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 259 | scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(4096); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 260 | for (;;) { |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 261 | rv = sock->Read(buf, 4096, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 262 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 263 | |
| 264 | if (rv == net::ERR_IO_PENDING) |
| 265 | rv = callback.WaitForResult(); |
| 266 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 267 | EXPECT_GE(rv, 0); |
| 268 | if (rv <= 0) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 269 | break; |
| 270 | } |
| 271 | } |
| 272 | |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 273 | // Test the full duplex mode, with Read and Write pending at the same time. |
| 274 | // This test also serves as a regression test for http://crbug.com/29815. |
| 275 | TEST_F(SSLClientSocketTest, Read_FullDuplex) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 276 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath()); |
| 277 | ASSERT_TRUE(test_server.Start()); |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 278 | |
| 279 | net::AddressList addr; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 280 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 281 | |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 282 | TestCompletionCallback callback; // Used for everything except Write. |
| 283 | TestCompletionCallback callback2; // Used for Write only. |
| 284 | |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 285 | net::ClientSocket* transport = new net::TCPClientSocket(addr, NULL); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 286 | int rv = transport->Connect(&callback); |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 287 | if (rv == net::ERR_IO_PENDING) |
| 288 | rv = callback.WaitForResult(); |
| 289 | EXPECT_EQ(net::OK, rv); |
| 290 | |
| 291 | scoped_ptr<net::SSLClientSocket> sock( |
| 292 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 293 | test_server.host_port_pair().host(), |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 294 | kDefaultSSLConfig)); |
| 295 | |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 296 | rv = sock->Connect(&callback); |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 297 | if (rv != net::OK) { |
| 298 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 299 | |
| 300 | rv = callback.WaitForResult(); |
| 301 | EXPECT_EQ(net::OK, rv); |
| 302 | } |
| 303 | EXPECT_TRUE(sock->IsConnected()); |
| 304 | |
| 305 | // Issue a "hanging" Read first. |
| 306 | scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(4096); |
| 307 | rv = sock->Read(buf, 4096, &callback); |
| 308 | // We haven't written the request, so there should be no response yet. |
| 309 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 310 | |
| 311 | // Write the request. |
| 312 | // The request is padded with a User-Agent header to a size that causes the |
| 313 | // memio circular buffer (4k bytes) in SSLClientSocketNSS to wrap around. |
| 314 | // This tests the fix for http://crbug.com/29815. |
| 315 | std::string request_text = "GET / HTTP/1.1\r\nUser-Agent: long browser name "; |
| 316 | for (int i = 0; i < 3800; ++i) |
| 317 | request_text.push_back('*'); |
| 318 | request_text.append("\r\n\r\n"); |
| 319 | scoped_refptr<net::IOBuffer> request_buffer = |
| 320 | new net::StringIOBuffer(request_text); |
| 321 | |
| 322 | rv = sock->Write(request_buffer, request_text.size(), &callback2); |
| 323 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 324 | |
| 325 | if (rv == net::ERR_IO_PENDING) |
| 326 | rv = callback2.WaitForResult(); |
| 327 | EXPECT_EQ(static_cast<int>(request_text.size()), rv); |
| 328 | |
| 329 | // Now get the Read result. |
| 330 | rv = callback.WaitForResult(); |
| 331 | EXPECT_GT(rv, 0); |
| 332 | } |
| 333 | |
[email protected] | 010e27e | 2009-08-27 17:49:41 | [diff] [blame] | 334 | TEST_F(SSLClientSocketTest, Read_SmallChunks) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 335 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath()); |
| 336 | ASSERT_TRUE(test_server.Start()); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 337 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 338 | net::AddressList addr; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 339 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 340 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 341 | TestCompletionCallback callback; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 342 | net::ClientSocket* transport = new net::TCPClientSocket(addr, NULL); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 343 | int rv = transport->Connect(&callback); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 344 | if (rv == net::ERR_IO_PENDING) |
| 345 | rv = callback.WaitForResult(); |
| 346 | EXPECT_EQ(net::OK, rv); |
| 347 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 348 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 349 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 350 | test_server.host_port_pair().host(), kDefaultSSLConfig)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 351 | |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 352 | rv = sock->Connect(&callback); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 353 | if (rv != net::OK) { |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 354 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 355 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 356 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 357 | EXPECT_EQ(net::OK, rv); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 358 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 359 | |
| 360 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 361 | scoped_refptr<net::IOBuffer> request_buffer = |
| 362 | new net::IOBuffer(arraysize(request_text) - 1); |
| 363 | memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 364 | |
| 365 | rv = sock->Write(request_buffer, arraysize(request_text) - 1, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 366 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 367 | |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 368 | if (rv == net::ERR_IO_PENDING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 369 | rv = callback.WaitForResult(); |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 370 | EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 371 | |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 372 | scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 373 | for (;;) { |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 374 | rv = sock->Read(buf, 1, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 375 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 376 | |
| 377 | if (rv == net::ERR_IO_PENDING) |
| 378 | rv = callback.WaitForResult(); |
| 379 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 380 | EXPECT_GE(rv, 0); |
| 381 | if (rv <= 0) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 382 | break; |
| 383 | } |
| 384 | } |
| 385 | |
[email protected] | 010e27e | 2009-08-27 17:49:41 | [diff] [blame] | 386 | TEST_F(SSLClientSocketTest, Read_Interrupted) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 387 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath()); |
| 388 | ASSERT_TRUE(test_server.Start()); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 389 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 390 | net::AddressList addr; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 391 | ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 392 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 393 | TestCompletionCallback callback; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 394 | net::ClientSocket* transport = new net::TCPClientSocket(addr, NULL); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 395 | int rv = transport->Connect(&callback); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 396 | if (rv == net::ERR_IO_PENDING) |
| 397 | rv = callback.WaitForResult(); |
| 398 | EXPECT_EQ(net::OK, rv); |
| 399 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 400 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 401 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 402 | test_server.host_port_pair().host(), kDefaultSSLConfig)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 403 | |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 404 | rv = sock->Connect(&callback); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 405 | if (rv != net::OK) { |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 406 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 407 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 408 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 409 | EXPECT_EQ(net::OK, rv); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 410 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 411 | |
| 412 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 413 | scoped_refptr<net::IOBuffer> request_buffer = |
| 414 | new net::IOBuffer(arraysize(request_text) - 1); |
| 415 | memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 416 | |
| 417 | rv = sock->Write(request_buffer, arraysize(request_text) - 1, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 418 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 419 | |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 420 | if (rv == net::ERR_IO_PENDING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 421 | rv = callback.WaitForResult(); |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 422 | EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 423 | |
| 424 | // Do a partial read and then exit. This test should not crash! |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 425 | scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(512); |
| 426 | rv = sock->Read(buf, 512, &callback); |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 427 | EXPECT_TRUE(rv > 0 || rv == net::ERR_IO_PENDING); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 428 | |
| 429 | if (rv == net::ERR_IO_PENDING) |
| 430 | rv = callback.WaitForResult(); |
| 431 | |
[email protected] | 914286d6 | 2009-12-10 23:06:44 | [diff] [blame] | 432 | EXPECT_GT(rv, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 433 | } |
[email protected] | 39afe64 | 2010-04-29 14:55:18 | [diff] [blame] | 434 | |
[email protected] | 39afe64 | 2010-04-29 14:55:18 | [diff] [blame] | 435 | // Regression test for http://crbug.com/42538 |
| 436 | TEST_F(SSLClientSocketTest, PrematureApplicationData) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 437 | net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath()); |
| 438 | ASSERT_TRUE(test_server.Start()); |
| 439 | |
[email protected] | 39afe64 | 2010-04-29 14:55:18 | [diff] [blame] | 440 | net::AddressList addr; |
| 441 | TestCompletionCallback callback; |
| 442 | |
| 443 | static const unsigned char application_data[] = { |
| 444 | 0x17, 0x03, 0x01, 0x00, 0x4a, 0x02, 0x00, 0x00, 0x46, 0x03, 0x01, 0x4b, |
| 445 | 0xc2, 0xf8, 0xb2, 0xc1, 0x56, 0x42, 0xb9, 0x57, 0x7f, 0xde, 0x87, 0x46, |
| 446 | 0xf7, 0xa3, 0x52, 0x42, 0x21, 0xf0, 0x13, 0x1c, 0x9c, 0x83, 0x88, 0xd6, |
| 447 | 0x93, 0x0c, 0xf6, 0x36, 0x30, 0x05, 0x7e, 0x20, 0xb5, 0xb5, 0x73, 0x36, |
| 448 | 0x53, 0x83, 0x0a, 0xfc, 0x17, 0x63, 0xbf, 0xa0, 0xe4, 0x42, 0x90, 0x0d, |
| 449 | 0x2f, 0x18, 0x6d, 0x20, 0xd8, 0x36, 0x3f, 0xfc, 0xe6, 0x01, 0xfa, 0x0f, |
| 450 | 0xa5, 0x75, 0x7f, 0x09, 0x00, 0x04, 0x00, 0x16, 0x03, 0x01, 0x11, 0x57, |
| 451 | 0x0b, 0x00, 0x11, 0x53, 0x00, 0x11, 0x50, 0x00, 0x06, 0x22, 0x30, 0x82, |
| 452 | 0x06, 0x1e, 0x30, 0x82, 0x05, 0x06, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, |
| 453 | 0x0a |
| 454 | }; |
| 455 | |
| 456 | // All reads and writes complete synchronously (async=false). |
| 457 | net::MockRead data_reads[] = { |
| 458 | net::MockRead(false, reinterpret_cast<const char*>(application_data), |
| 459 | arraysize(application_data)), |
| 460 | net::MockRead(false, net::OK), |
| 461 | }; |
| 462 | |
| 463 | net::StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 464 | NULL, 0); |
| 465 | |
| 466 | net::ClientSocket* transport = |
| 467 | new net::MockTCPClientSocket(addr, NULL, &data); |
| 468 | int rv = transport->Connect(&callback); |
| 469 | if (rv == net::ERR_IO_PENDING) |
| 470 | rv = callback.WaitForResult(); |
| 471 | EXPECT_EQ(net::OK, rv); |
| 472 | |
| 473 | scoped_ptr<net::SSLClientSocket> sock( |
| 474 | socket_factory_->CreateSSLClientSocket( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame^] | 475 | transport, test_server.host_port_pair().host(), kDefaultSSLConfig)); |
[email protected] | 39afe64 | 2010-04-29 14:55:18 | [diff] [blame] | 476 | |
| 477 | rv = sock->Connect(&callback); |
| 478 | EXPECT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv); |
| 479 | } |