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] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 26 | : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) { |
| 27 | } |
| 28 | |
| 29 | void StartOKServer() { |
| 30 | bool success = server_.Start(net::TestServerLauncher::ProtoHTTP, |
| 31 | server_.kHostName, server_.kOKHTTPSPort, |
[email protected] | deb27ae | 2009-04-10 02:37:22 | [diff] [blame] | 32 | FilePath(), server_.GetOKCertPath(), std::wstring()); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 33 | ASSERT_TRUE(success); |
| 34 | } |
| 35 | |
| 36 | void StartMismatchedServer() { |
| 37 | bool success = server_.Start(net::TestServerLauncher::ProtoHTTP, |
| 38 | server_.kMismatchedHostName, server_.kOKHTTPSPort, |
[email protected] | deb27ae | 2009-04-10 02:37:22 | [diff] [blame] | 39 | FilePath(), server_.GetOKCertPath(), std::wstring()); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 40 | ASSERT_TRUE(success); |
| 41 | } |
| 42 | |
| 43 | void StartExpiredServer() { |
| 44 | bool success = server_.Start(net::TestServerLauncher::ProtoHTTP, |
| 45 | server_.kHostName, server_.kBadHTTPSPort, |
[email protected] | deb27ae | 2009-04-10 02:37:22 | [diff] [blame] | 46 | FilePath(), server_.GetExpiredCertPath(), std::wstring()); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 47 | ASSERT_TRUE(success); |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 48 | } |
[email protected] | 471822ca | 2009-01-29 11:32:26 | [diff] [blame] | 49 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 50 | protected: |
| 51 | net::ClientSocketFactory* socket_factory_; |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 52 | net::TestServerLauncher server_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 53 | }; |
| 54 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 55 | //----------------------------------------------------------------------------- |
| 56 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 57 | #if defined(OS_MACOSX) |
[email protected] | 3016ded | 2009-06-19 18:27:59 | [diff] [blame] | 58 | // Status 6/19/09: |
| 59 | // |
| 60 | // If these tests are enabled on OSX, we choke at the point |
| 61 | // SSLHandshake() (Security framework call) is called from |
| 62 | // SSLClientSocketMac::DoHandshake(). Return value is -9812 (cert |
| 63 | // valid but root not trusted), but if you don't have the cert in your |
| 64 | // keychain as documented on |
| 65 | // http://dev.chromium.org/developers/testing, the -9812 becomes a |
| 66 | // -9813 (no root cert). |
| 67 | // |
| 68 | // See related handshake failures exhibited by disabled tests in |
| 69 | // net/url_request/url_request_unittest.cc. |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 70 | #define MAYBE_Connect DISABLED_Connect |
| 71 | #define MAYBE_ConnectExpired DISABLED_ConnectExpired |
| 72 | #define MAYBE_ConnectMismatched DISABLED_ConnectMismatched |
| 73 | #define MAYBE_Read DISABLED_Read |
| 74 | #define MAYBE_Read_SmallChunks DISABLED_Read_SmallChunks |
| 75 | #define MAYBE_Read_Interrupted DISABLED_Read_Interrupted |
| 76 | #else |
| 77 | #define MAYBE_Connect Connect |
| 78 | #define MAYBE_ConnectExpired ConnectExpired |
| 79 | #define MAYBE_ConnectMismatched ConnectMismatched |
| 80 | #define MAYBE_Read Read |
| 81 | #define MAYBE_Read_SmallChunks Read_SmallChunks |
| 82 | #define MAYBE_Read_Interrupted Read_Interrupted |
| 83 | #endif |
| 84 | |
| 85 | TEST_F(SSLClientSocketTest, MAYBE_Connect) { |
| 86 | StartOKServer(); |
| 87 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 88 | net::AddressList addr; |
| 89 | net::HostResolver resolver; |
| 90 | TestCompletionCallback callback; |
| 91 | |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 92 | net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); |
| 93 | int rv = resolver.Resolve(info, &addr, NULL, NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 94 | EXPECT_EQ(net::OK, rv); |
| 95 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 96 | net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 97 | rv = transport->Connect(&callback); |
| 98 | if (rv == net::ERR_IO_PENDING) |
| 99 | rv = callback.WaitForResult(); |
| 100 | EXPECT_EQ(net::OK, rv); |
| 101 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 102 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 103 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 104 | server_.kHostName, kDefaultSSLConfig)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 105 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 106 | EXPECT_FALSE(sock->IsConnected()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 107 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 108 | rv = sock->Connect(&callback); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 109 | if (rv != net::OK) { |
| 110 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 111 | EXPECT_FALSE(sock->IsConnected()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 112 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 113 | rv = callback.WaitForResult(); |
| 114 | EXPECT_EQ(net::OK, rv); |
| 115 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 116 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 117 | EXPECT_TRUE(sock->IsConnected()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 118 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 119 | sock->Disconnect(); |
| 120 | EXPECT_FALSE(sock->IsConnected()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 123 | TEST_F(SSLClientSocketTest, MAYBE_ConnectExpired) { |
| 124 | StartExpiredServer(); |
| 125 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 126 | net::AddressList addr; |
| 127 | net::HostResolver resolver; |
| 128 | TestCompletionCallback callback; |
| 129 | |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 130 | net::HostResolver::RequestInfo info(server_.kHostName, server_.kBadHTTPSPort); |
| 131 | int rv = resolver.Resolve(info, &addr, NULL, NULL); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 132 | EXPECT_EQ(net::OK, rv); |
| 133 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 134 | net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 135 | rv = transport->Connect(&callback); |
| 136 | if (rv == net::ERR_IO_PENDING) |
| 137 | rv = callback.WaitForResult(); |
| 138 | EXPECT_EQ(net::OK, rv); |
| 139 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 140 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 141 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 142 | server_.kHostName, kDefaultSSLConfig)); |
| 143 | |
| 144 | EXPECT_FALSE(sock->IsConnected()); |
| 145 | |
| 146 | rv = sock->Connect(&callback); |
| 147 | if (rv != net::OK) { |
| 148 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 149 | EXPECT_FALSE(sock->IsConnected()); |
| 150 | |
| 151 | rv = callback.WaitForResult(); |
| 152 | EXPECT_EQ(net::ERR_CERT_DATE_INVALID, rv); |
| 153 | } |
| 154 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 155 | // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 156 | // the socket when it encounters an error, whereas other implementations |
| 157 | // leave it connected. |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | TEST_F(SSLClientSocketTest, MAYBE_ConnectMismatched) { |
| 161 | StartMismatchedServer(); |
| 162 | |
| 163 | net::AddressList addr; |
| 164 | net::HostResolver resolver; |
| 165 | TestCompletionCallback callback; |
| 166 | |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 167 | net::HostResolver::RequestInfo info(server_.kMismatchedHostName, |
| 168 | server_.kOKHTTPSPort); |
| 169 | int rv = resolver.Resolve(info, &addr, NULL, NULL); |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 170 | EXPECT_EQ(net::OK, rv); |
| 171 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 172 | net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 173 | rv = transport->Connect(&callback); |
| 174 | if (rv == net::ERR_IO_PENDING) |
| 175 | rv = callback.WaitForResult(); |
| 176 | EXPECT_EQ(net::OK, rv); |
| 177 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 178 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 179 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 180 | server_.kMismatchedHostName, kDefaultSSLConfig)); |
| 181 | |
| 182 | EXPECT_FALSE(sock->IsConnected()); |
| 183 | |
| 184 | rv = sock->Connect(&callback); |
| 185 | if (rv != net::ERR_CERT_COMMON_NAME_INVALID) { |
| 186 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 187 | EXPECT_FALSE(sock->IsConnected()); |
| 188 | |
| 189 | rv = callback.WaitForResult(); |
| 190 | EXPECT_EQ(net::ERR_CERT_COMMON_NAME_INVALID, rv); |
| 191 | } |
| 192 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 193 | // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 194 | // the socket when it encounters an error, whereas other implementations |
| 195 | // leave it connected. |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 196 | } |
| 197 | |
[email protected] | b219785 | 2009-02-19 23:27:33 | [diff] [blame] | 198 | // TODO(wtc): Add unit tests for IsConnectedAndIdle: |
| 199 | // - Server closes an SSL connection (with a close_notify alert message). |
| 200 | // - Server closes the underlying TCP connection directly. |
| 201 | // - Server sends data unexpectedly. |
| 202 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 203 | TEST_F(SSLClientSocketTest, MAYBE_Read) { |
| 204 | StartOKServer(); |
| 205 | |
| 206 | net::AddressList addr; |
| 207 | net::HostResolver resolver; |
| 208 | TestCompletionCallback callback; |
| 209 | |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 210 | net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); |
| 211 | int rv = resolver.Resolve(info, &addr, &callback, NULL); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 212 | EXPECT_EQ(net::ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 213 | |
| 214 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 215 | EXPECT_EQ(net::OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 216 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 217 | net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 218 | rv = transport->Connect(&callback); |
| 219 | if (rv == net::ERR_IO_PENDING) |
| 220 | rv = callback.WaitForResult(); |
| 221 | EXPECT_EQ(net::OK, rv); |
| 222 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 223 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 224 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 225 | server_.kHostName, |
| 226 | kDefaultSSLConfig)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 227 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 228 | rv = sock->Connect(&callback); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 229 | if (rv != net::OK) { |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 230 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 231 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 232 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 233 | EXPECT_EQ(net::OK, rv); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 234 | } |
[email protected] | b43c97c | 2008-10-22 19:50:58 | [diff] [blame] | 235 | EXPECT_TRUE(sock->IsConnected()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 236 | |
| 237 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 238 | scoped_refptr<net::IOBuffer> request_buffer = |
| 239 | new net::IOBuffer(arraysize(request_text) - 1); |
| 240 | memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 241 | |
| 242 | rv = sock->Write(request_buffer, arraysize(request_text) - 1, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 243 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 244 | |
| 245 | if (rv == net::ERR_IO_PENDING) { |
| 246 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 247 | EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 250 | scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(4096); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 251 | for (;;) { |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 252 | rv = sock->Read(buf, 4096, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 253 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 254 | |
| 255 | if (rv == net::ERR_IO_PENDING) |
| 256 | rv = callback.WaitForResult(); |
| 257 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 258 | EXPECT_GE(rv, 0); |
| 259 | if (rv <= 0) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 264 | TEST_F(SSLClientSocketTest, MAYBE_Read_SmallChunks) { |
| 265 | StartOKServer(); |
| 266 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 267 | net::AddressList addr; |
| 268 | net::HostResolver resolver; |
| 269 | TestCompletionCallback callback; |
| 270 | |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 271 | net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); |
| 272 | int rv = resolver.Resolve(info, &addr, NULL, NULL); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 273 | EXPECT_EQ(net::OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 274 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 275 | net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 276 | rv = transport->Connect(&callback); |
| 277 | if (rv == net::ERR_IO_PENDING) |
| 278 | rv = callback.WaitForResult(); |
| 279 | EXPECT_EQ(net::OK, rv); |
| 280 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 281 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 282 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 283 | server_.kHostName, kDefaultSSLConfig)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 284 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 285 | rv = sock->Connect(&callback); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 286 | if (rv != net::OK) { |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 287 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 288 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 289 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 290 | EXPECT_EQ(net::OK, rv); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 291 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 292 | |
| 293 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 294 | scoped_refptr<net::IOBuffer> request_buffer = |
| 295 | new net::IOBuffer(arraysize(request_text) - 1); |
| 296 | memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 297 | |
| 298 | rv = sock->Write(request_buffer, arraysize(request_text) - 1, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 299 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 300 | |
| 301 | if (rv == net::ERR_IO_PENDING) { |
| 302 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 303 | EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 304 | } |
| 305 | |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 306 | scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 307 | for (;;) { |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 308 | rv = sock->Read(buf, 1, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 309 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 310 | |
| 311 | if (rv == net::ERR_IO_PENDING) |
| 312 | rv = callback.WaitForResult(); |
| 313 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 314 | EXPECT_GE(rv, 0); |
| 315 | if (rv <= 0) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 316 | break; |
| 317 | } |
| 318 | } |
| 319 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 320 | TEST_F(SSLClientSocketTest, MAYBE_Read_Interrupted) { |
| 321 | StartOKServer(); |
| 322 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 323 | net::AddressList addr; |
| 324 | net::HostResolver resolver; |
| 325 | TestCompletionCallback callback; |
| 326 | |
[email protected] | 2884a46 | 2009-06-15 05:08:42 | [diff] [blame] | 327 | net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); |
| 328 | int rv = resolver.Resolve(info, &addr, NULL, NULL); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 329 | EXPECT_EQ(net::OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 330 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 331 | net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 332 | rv = transport->Connect(&callback); |
| 333 | if (rv == net::ERR_IO_PENDING) |
| 334 | rv = callback.WaitForResult(); |
| 335 | EXPECT_EQ(net::OK, rv); |
| 336 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 337 | scoped_ptr<net::SSLClientSocket> sock( |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 338 | socket_factory_->CreateSSLClientSocket(transport, |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 339 | server_.kHostName, kDefaultSSLConfig)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 340 | |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 341 | rv = sock->Connect(&callback); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 342 | if (rv != net::OK) { |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 343 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 344 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 345 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 346 | EXPECT_EQ(net::OK, rv); |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 347 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 348 | |
| 349 | const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 350 | scoped_refptr<net::IOBuffer> request_buffer = |
| 351 | new net::IOBuffer(arraysize(request_text) - 1); |
| 352 | memcpy(request_buffer->data(), request_text, arraysize(request_text) - 1); |
| 353 | |
| 354 | rv = sock->Write(request_buffer, arraysize(request_text) - 1, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 355 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 356 | |
| 357 | if (rv == net::ERR_IO_PENDING) { |
| 358 | rv = callback.WaitForResult(); |
[email protected] | b75523f | 2008-10-17 14:49:07 | [diff] [blame] | 359 | EXPECT_EQ(static_cast<int>(arraysize(request_text) - 1), rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | // Do a partial read and then exit. This test should not crash! |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 363 | scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(512); |
| 364 | rv = sock->Read(buf, 512, &callback); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 365 | EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 366 | |
| 367 | if (rv == net::ERR_IO_PENDING) |
| 368 | rv = callback.WaitForResult(); |
| 369 | |
[email protected] | 7b822b2b | 2008-08-05 00:15:45 | [diff] [blame] | 370 | EXPECT_NE(rv, 0); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 371 | } |