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