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