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] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 10 | #include "net/base/net_errors.h" |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 11 | #include "net/base/ssl_config_service.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 12 | #include "net/base/test_completion_callback.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 13 | #include "net/socket/client_socket_factory.h" |
| 14 | #include "net/socket/ssl_test_util.h" |
| 15 | #include "net/socket/tcp_client_socket.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 17 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 18 | |
| 19 | //----------------------------------------------------------------------------- |
| 20 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 21 | const net::SSLConfig kDefaultSSLConfig; |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 22 | |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 23 | class SSLClientSocketTest : public PlatformTest { |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 24 | public: |
| 25 | SSLClientSocketTest() |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 26 | : resolver_(net::CreateSystemHostResolver()), |
| 27 | socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) { |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void StartOKServer() { |
| 31 | bool success = server_.Start(net::TestServerLauncher::ProtoHTTP, |
| 32 | server_.kHostName, server_.kOKHTTPSPort, |
[email protected] | deb27ae | 2009-04-10 02:37:22 | [diff] [blame] | 33 | FilePath(), server_.GetOKCertPath(), std::wstring()); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 34 | ASSERT_TRUE(success); |
| 35 | } |
| 36 | |
| 37 | void StartMismatchedServer() { |
| 38 | bool success = server_.Start(net::TestServerLauncher::ProtoHTTP, |
| 39 | server_.kMismatchedHostName, server_.kOKHTTPSPort, |
[email protected] | deb27ae | 2009-04-10 02:37:22 | [diff] [blame] | 40 | FilePath(), server_.GetOKCertPath(), std::wstring()); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 41 | ASSERT_TRUE(success); |
| 42 | } |
| 43 | |
| 44 | void StartExpiredServer() { |
| 45 | bool success = server_.Start(net::TestServerLauncher::ProtoHTTP, |
| 46 | server_.kHostName, server_.kBadHTTPSPort, |
[email protected] | deb27ae | 2009-04-10 02:37:22 | [diff] [blame] | 47 | FilePath(), server_.GetExpiredCertPath(), std::wstring()); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 48 | ASSERT_TRUE(success); |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 49 | } |
[email protected] | 471822ca | 2009-01-29 11:32:26 | [diff] [blame] | 50 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 51 | protected: |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 52 | scoped_refptr<net::HostResolver> resolver_; |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 53 | net::ClientSocketFactory* socket_factory_; |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 54 | net::TestServerLauncher server_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 55 | }; |
| 56 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 57 | //----------------------------------------------------------------------------- |
| 58 | |
[email protected] | 010e27e | 2009-08-27 17:49:41 | [diff] [blame^] | 59 | TEST_F(SSLClientSocketTest, Connect) { |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 60 | StartOKServer(); |
| 61 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 62 | net::AddressList addr; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 63 | TestCompletionCallback callback; |
| 64 | |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 65 | net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 66 | int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 67 | EXPECT_EQ(net::OK, rv); |
| 68 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 69 | net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 70 | rv = transport->Connect(&callback); |
| 71 | if (rv == net::ERR_IO_PENDING) |
| 72 | rv = callback.WaitForResult(); |
| 73 | EXPECT_EQ(net::OK, rv); |
| 74 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 75 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 76 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 77 | server_.kHostName, kDefaultSSLConfig)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 78 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 79 | EXPECT_FALSE(sock->IsConnected()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 80 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 81 | rv = sock->Connect(&callback); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 82 | if (rv != net::OK) { |
| 83 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 84 | EXPECT_FALSE(sock->IsConnected()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 85 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 86 | rv = callback.WaitForResult(); |
| 87 | EXPECT_EQ(net::OK, rv); |
| 88 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 89 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 90 | EXPECT_TRUE(sock->IsConnected()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 91 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 92 | sock->Disconnect(); |
| 93 | EXPECT_FALSE(sock->IsConnected()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 010e27e | 2009-08-27 17:49:41 | [diff] [blame^] | 96 | TEST_F(SSLClientSocketTest, ConnectExpired) { |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 97 | StartExpiredServer(); |
| 98 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 99 | net::AddressList addr; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 100 | TestCompletionCallback callback; |
| 101 | |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 102 | net::HostResolver::RequestInfo info(server_.kHostName, server_.kBadHTTPSPort); |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 103 | int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 104 | EXPECT_EQ(net::OK, rv); |
| 105 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 106 | net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 107 | rv = transport->Connect(&callback); |
| 108 | if (rv == net::ERR_IO_PENDING) |
| 109 | rv = callback.WaitForResult(); |
| 110 | EXPECT_EQ(net::OK, rv); |
| 111 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 112 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 113 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 114 | server_.kHostName, kDefaultSSLConfig)); |
| 115 | |
| 116 | EXPECT_FALSE(sock->IsConnected()); |
| 117 | |
| 118 | rv = sock->Connect(&callback); |
| 119 | if (rv != net::OK) { |
| 120 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 121 | EXPECT_FALSE(sock->IsConnected()); |
| 122 | |
| 123 | rv = callback.WaitForResult(); |
| 124 | EXPECT_EQ(net::ERR_CERT_DATE_INVALID, rv); |
| 125 | } |
| 126 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 127 | // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 128 | // the socket when it encounters an error, whereas other implementations |
| 129 | // leave it connected. |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 010e27e | 2009-08-27 17:49:41 | [diff] [blame^] | 132 | TEST_F(SSLClientSocketTest, ConnectMismatched) { |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 133 | StartMismatchedServer(); |
| 134 | |
| 135 | net::AddressList addr; |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 136 | TestCompletionCallback callback; |
| 137 | |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 138 | net::HostResolver::RequestInfo info(server_.kMismatchedHostName, |
| 139 | server_.kOKHTTPSPort); |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 140 | int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 141 | EXPECT_EQ(net::OK, rv); |
| 142 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 143 | net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 144 | rv = transport->Connect(&callback); |
| 145 | if (rv == net::ERR_IO_PENDING) |
| 146 | rv = callback.WaitForResult(); |
| 147 | EXPECT_EQ(net::OK, rv); |
| 148 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 149 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 150 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 151 | server_.kMismatchedHostName, kDefaultSSLConfig)); |
| 152 | |
| 153 | EXPECT_FALSE(sock->IsConnected()); |
| 154 | |
| 155 | rv = sock->Connect(&callback); |
| 156 | if (rv != net::ERR_CERT_COMMON_NAME_INVALID) { |
| 157 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 158 | EXPECT_FALSE(sock->IsConnected()); |
| 159 | |
| 160 | rv = callback.WaitForResult(); |
| 161 | EXPECT_EQ(net::ERR_CERT_COMMON_NAME_INVALID, rv); |
| 162 | } |
| 163 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 164 | // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 165 | // the socket when it encounters an error, whereas other implementations |
| 166 | // leave it connected. |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | b219785 | 2009-02-19 23:27:33 | [diff] [blame] | 169 | // TODO(wtc): Add unit tests for IsConnectedAndIdle: |
| 170 | // - Server closes an SSL connection (with a close_notify alert message). |
| 171 | // - Server closes the underlying TCP connection directly. |
| 172 | // - Server sends data unexpectedly. |
| 173 | |
[email protected] | 010e27e | 2009-08-27 17:49:41 | [diff] [blame^] | 174 | TEST_F(SSLClientSocketTest, Read) { |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 175 | StartOKServer(); |
| 176 | |
| 177 | net::AddressList addr; |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 178 | TestCompletionCallback callback; |
| 179 | |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 180 | net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 181 | int rv = resolver_->Resolve(info, &addr, &callback, NULL, NULL); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 182 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 183 | |
| 184 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 185 | EXPECT_EQ(net::OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 186 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 187 | net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 188 | rv = transport->Connect(&callback); |
| 189 | if (rv == net::ERR_IO_PENDING) |
| 190 | rv = callback.WaitForResult(); |
| 191 | EXPECT_EQ(net::OK, rv); |
| 192 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 193 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 194 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 195 | server_.kHostName, |
| 196 | kDefaultSSLConfig)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 197 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 198 | rv = sock->Connect(&callback); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 199 | if (rv != net::OK) { |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 200 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 201 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 202 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 203 | EXPECT_EQ(net::OK, rv); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 204 | } |
[email protected] | b43c97c | 2008-10-22 19:50:58 | [diff] [blame] | 205 | EXPECT_TRUE(sock->IsConnected()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 206 | |
| 207 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 208 | scoped_refptr<net::IOBuffer> request_buffer = |
| 209 | new net::IOBuffer(arraysize(request_text) - 1); |
| 210 | memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 211 | |
| 212 | rv = sock->Write(request_buffer, arraysize(request_text) - 1, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 213 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 214 | |
| 215 | if (rv == net::ERR_IO_PENDING) { |
| 216 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 217 | EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 218 | } |
| 219 | |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 220 | scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(4096); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 221 | for (;;) { |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 222 | rv = sock->Read(buf, 4096, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 223 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 224 | |
| 225 | if (rv == net::ERR_IO_PENDING) |
| 226 | rv = callback.WaitForResult(); |
| 227 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 228 | EXPECT_GE(rv, 0); |
| 229 | if (rv <= 0) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | } |
| 233 | |
[email protected] | 010e27e | 2009-08-27 17:49:41 | [diff] [blame^] | 234 | TEST_F(SSLClientSocketTest, Read_SmallChunks) { |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 235 | StartOKServer(); |
| 236 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 237 | net::AddressList addr; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 238 | TestCompletionCallback callback; |
| 239 | |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 240 | net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 241 | int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 242 | EXPECT_EQ(net::OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 243 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 244 | net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 245 | rv = transport->Connect(&callback); |
| 246 | if (rv == net::ERR_IO_PENDING) |
| 247 | rv = callback.WaitForResult(); |
| 248 | EXPECT_EQ(net::OK, rv); |
| 249 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 250 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 251 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 252 | server_.kHostName, kDefaultSSLConfig)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 253 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 254 | rv = sock->Connect(&callback); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 255 | if (rv != net::OK) { |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 256 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 257 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 258 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 259 | EXPECT_EQ(net::OK, rv); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 260 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 261 | |
| 262 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 263 | scoped_refptr<net::IOBuffer> request_buffer = |
| 264 | new net::IOBuffer(arraysize(request_text) - 1); |
| 265 | memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 266 | |
| 267 | rv = sock->Write(request_buffer, arraysize(request_text) - 1, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 268 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 269 | |
| 270 | if (rv == net::ERR_IO_PENDING) { |
| 271 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 272 | EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 275 | scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 276 | for (;;) { |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 277 | rv = sock->Read(buf, 1, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 278 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 279 | |
| 280 | if (rv == net::ERR_IO_PENDING) |
| 281 | rv = callback.WaitForResult(); |
| 282 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 283 | EXPECT_GE(rv, 0); |
| 284 | if (rv <= 0) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 285 | break; |
| 286 | } |
| 287 | } |
| 288 | |
[email protected] | 010e27e | 2009-08-27 17:49:41 | [diff] [blame^] | 289 | TEST_F(SSLClientSocketTest, Read_Interrupted) { |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 290 | StartOKServer(); |
| 291 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 292 | net::AddressList addr; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 293 | TestCompletionCallback callback; |
| 294 | |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 295 | net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 296 | int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 297 | EXPECT_EQ(net::OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 298 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 299 | net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 300 | rv = transport->Connect(&callback); |
| 301 | if (rv == net::ERR_IO_PENDING) |
| 302 | rv = callback.WaitForResult(); |
| 303 | EXPECT_EQ(net::OK, rv); |
| 304 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 305 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 306 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 307 | server_.kHostName, kDefaultSSLConfig)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 308 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 309 | rv = sock->Connect(&callback); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 310 | if (rv != net::OK) { |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 311 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 312 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 313 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 314 | EXPECT_EQ(net::OK, rv); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 315 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 316 | |
| 317 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 318 | scoped_refptr<net::IOBuffer> request_buffer = |
| 319 | new net::IOBuffer(arraysize(request_text) - 1); |
| 320 | memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 321 | |
| 322 | rv = sock->Write(request_buffer, arraysize(request_text) - 1, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 323 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 324 | |
| 325 | if (rv == net::ERR_IO_PENDING) { |
| 326 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 327 | EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // Do a partial read and then exit. This test should not crash! |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 331 | scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(512); |
| 332 | rv = sock->Read(buf, 512, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 333 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 334 | |
| 335 | if (rv == net::ERR_IO_PENDING) |
| 336 | rv = callback.WaitForResult(); |
| 337 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 338 | EXPECT_NE(rv, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 339 | } |