| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket/ssl_client_socket.h" | 5 #include "net/socket/ssl_client_socket.h" |
| 6 | 6 |
| 7 #include "net/base/address_list.h" | 7 #include "net/base/address_list.h" |
| 8 #include "net/base/host_resolver.h" | 8 #include "net/base/host_resolver.h" |
| 9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 10 #include "net/base/load_log.h" |
| 11 #include "net/base/load_log_unittest.h" |
| 10 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 11 #include "net/base/ssl_config_service.h" | 13 #include "net/base/ssl_config_service.h" |
| 12 #include "net/base/test_completion_callback.h" | 14 #include "net/base/test_completion_callback.h" |
| 13 #include "net/socket/client_socket_factory.h" | 15 #include "net/socket/client_socket_factory.h" |
| 14 #include "net/socket/ssl_test_util.h" | 16 #include "net/socket/ssl_test_util.h" |
| 15 #include "net/socket/tcp_client_socket.h" | 17 #include "net/socket/tcp_client_socket.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "testing/platform_test.h" | 19 #include "testing/platform_test.h" |
| 18 | 20 |
| 19 //----------------------------------------------------------------------------- | 21 //----------------------------------------------------------------------------- |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 StartOKServer(); | 62 StartOKServer(); |
| 61 | 63 |
| 62 net::AddressList addr; | 64 net::AddressList addr; |
| 63 TestCompletionCallback callback; | 65 TestCompletionCallback callback; |
| 64 | 66 |
| 65 net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); | 67 net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); |
| 66 int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); | 68 int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); |
| 67 EXPECT_EQ(net::OK, rv); | 69 EXPECT_EQ(net::OK, rv); |
| 68 | 70 |
| 69 net::ClientSocket *transport = new net::TCPClientSocket(addr); | 71 net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 70 rv = transport->Connect(&callback); | 72 rv = transport->Connect(&callback, NULL); |
| 71 if (rv == net::ERR_IO_PENDING) | 73 if (rv == net::ERR_IO_PENDING) |
| 72 rv = callback.WaitForResult(); | 74 rv = callback.WaitForResult(); |
| 73 EXPECT_EQ(net::OK, rv); | 75 EXPECT_EQ(net::OK, rv); |
| 74 | 76 |
| 75 scoped_ptr<net::SSLClientSocket> sock( | 77 scoped_ptr<net::SSLClientSocket> sock( |
| 76 socket_factory_->CreateSSLClientSocket(transport, | 78 socket_factory_->CreateSSLClientSocket(transport, |
| 77 server_.kHostName, kDefaultSSLConfig)); | 79 server_.kHostName, kDefaultSSLConfig)); |
| 78 | 80 |
| 79 EXPECT_FALSE(sock->IsConnected()); | 81 EXPECT_FALSE(sock->IsConnected()); |
| 80 | 82 |
| 81 rv = sock->Connect(&callback); | 83 scoped_refptr<net::LoadLog> log(new net::LoadLog); |
| 84 rv = sock->Connect(&callback, log); |
| 85 EXPECT_TRUE(net::LogContains( |
| 86 *log, 0, net::LoadLog::TYPE_SSL_CONNECT, net::LoadLog::PHASE_BEGIN)); |
| 82 if (rv != net::OK) { | 87 if (rv != net::OK) { |
| 83 ASSERT_EQ(net::ERR_IO_PENDING, rv); | 88 ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 84 EXPECT_FALSE(sock->IsConnected()); | 89 EXPECT_FALSE(sock->IsConnected()); |
| 90 EXPECT_FALSE(net::LogContains( |
| 91 *log, -1, net::LoadLog::TYPE_SSL_CONNECT, net::LoadLog::PHASE_END)); |
| 85 | 92 |
| 86 rv = callback.WaitForResult(); | 93 rv = callback.WaitForResult(); |
| 87 EXPECT_EQ(net::OK, rv); | 94 EXPECT_EQ(net::OK, rv); |
| 88 } | 95 } |
| 89 | 96 |
| 90 EXPECT_TRUE(sock->IsConnected()); | 97 EXPECT_TRUE(sock->IsConnected()); |
| 98 EXPECT_TRUE(net::LogContains( |
| 99 *log, -1, net::LoadLog::TYPE_SSL_CONNECT, net::LoadLog::PHASE_END)); |
| 91 | 100 |
| 92 sock->Disconnect(); | 101 sock->Disconnect(); |
| 93 EXPECT_FALSE(sock->IsConnected()); | 102 EXPECT_FALSE(sock->IsConnected()); |
| 94 } | 103 } |
| 95 | 104 |
| 96 TEST_F(SSLClientSocketTest, ConnectExpired) { | 105 TEST_F(SSLClientSocketTest, ConnectExpired) { |
| 97 StartExpiredServer(); | 106 StartExpiredServer(); |
| 98 | 107 |
| 99 net::AddressList addr; | 108 net::AddressList addr; |
| 100 TestCompletionCallback callback; | 109 TestCompletionCallback callback; |
| 101 | 110 |
| 102 net::HostResolver::RequestInfo info(server_.kHostName, server_.kBadHTTPSPort); | 111 net::HostResolver::RequestInfo info(server_.kHostName, server_.kBadHTTPSPort); |
| 103 int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); | 112 int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); |
| 104 EXPECT_EQ(net::OK, rv); | 113 EXPECT_EQ(net::OK, rv); |
| 105 | 114 |
| 106 net::ClientSocket *transport = new net::TCPClientSocket(addr); | 115 net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 107 rv = transport->Connect(&callback); | 116 rv = transport->Connect(&callback, NULL); |
| 108 if (rv == net::ERR_IO_PENDING) | 117 if (rv == net::ERR_IO_PENDING) |
| 109 rv = callback.WaitForResult(); | 118 rv = callback.WaitForResult(); |
| 110 EXPECT_EQ(net::OK, rv); | 119 EXPECT_EQ(net::OK, rv); |
| 111 | 120 |
| 112 scoped_ptr<net::SSLClientSocket> sock( | 121 scoped_ptr<net::SSLClientSocket> sock( |
| 113 socket_factory_->CreateSSLClientSocket(transport, | 122 socket_factory_->CreateSSLClientSocket(transport, |
| 114 server_.kHostName, kDefaultSSLConfig)); | 123 server_.kHostName, kDefaultSSLConfig)); |
| 115 | 124 |
| 116 EXPECT_FALSE(sock->IsConnected()); | 125 EXPECT_FALSE(sock->IsConnected()); |
| 117 | 126 |
| 118 rv = sock->Connect(&callback); | 127 scoped_refptr<net::LoadLog> log(new net::LoadLog); |
| 128 rv = sock->Connect(&callback, log); |
| 129 EXPECT_TRUE(net::LogContains( |
| 130 *log, 0, net::LoadLog::TYPE_SSL_CONNECT, net::LoadLog::PHASE_BEGIN)); |
| 119 if (rv != net::OK) { | 131 if (rv != net::OK) { |
| 120 ASSERT_EQ(net::ERR_IO_PENDING, rv); | 132 ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 121 EXPECT_FALSE(sock->IsConnected()); | 133 EXPECT_FALSE(sock->IsConnected()); |
| 134 EXPECT_FALSE(net::LogContains( |
| 135 *log, -1, net::LoadLog::TYPE_SSL_CONNECT, net::LoadLog::PHASE_END)); |
| 122 | 136 |
| 123 rv = callback.WaitForResult(); | 137 rv = callback.WaitForResult(); |
| 124 EXPECT_EQ(net::ERR_CERT_DATE_INVALID, rv); | 138 EXPECT_EQ(net::ERR_CERT_DATE_INVALID, rv); |
| 125 } | 139 } |
| 126 | 140 |
| 127 // We cannot test sock->IsConnected(), as the NSS implementation disconnects | 141 // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 128 // the socket when it encounters an error, whereas other implementations | 142 // the socket when it encounters an error, whereas other implementations |
| 129 // leave it connected. | 143 // leave it connected. |
| 144 |
| 145 EXPECT_TRUE(net::LogContains( |
| 146 *log, -1, net::LoadLog::TYPE_SSL_CONNECT, net::LoadLog::PHASE_END)); |
| 130 } | 147 } |
| 131 | 148 |
| 132 TEST_F(SSLClientSocketTest, ConnectMismatched) { | 149 TEST_F(SSLClientSocketTest, ConnectMismatched) { |
| 133 StartMismatchedServer(); | 150 StartMismatchedServer(); |
| 134 | 151 |
| 135 net::AddressList addr; | 152 net::AddressList addr; |
| 136 TestCompletionCallback callback; | 153 TestCompletionCallback callback; |
| 137 | 154 |
| 138 net::HostResolver::RequestInfo info(server_.kMismatchedHostName, | 155 net::HostResolver::RequestInfo info(server_.kMismatchedHostName, |
| 139 server_.kOKHTTPSPort); | 156 server_.kOKHTTPSPort); |
| 140 int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); | 157 int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); |
| 141 EXPECT_EQ(net::OK, rv); | 158 EXPECT_EQ(net::OK, rv); |
| 142 | 159 |
| 143 net::ClientSocket *transport = new net::TCPClientSocket(addr); | 160 net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 144 rv = transport->Connect(&callback); | 161 rv = transport->Connect(&callback, NULL); |
| 145 if (rv == net::ERR_IO_PENDING) | 162 if (rv == net::ERR_IO_PENDING) |
| 146 rv = callback.WaitForResult(); | 163 rv = callback.WaitForResult(); |
| 147 EXPECT_EQ(net::OK, rv); | 164 EXPECT_EQ(net::OK, rv); |
| 148 | 165 |
| 149 scoped_ptr<net::SSLClientSocket> sock( | 166 scoped_ptr<net::SSLClientSocket> sock( |
| 150 socket_factory_->CreateSSLClientSocket(transport, | 167 socket_factory_->CreateSSLClientSocket(transport, |
| 151 server_.kMismatchedHostName, kDefaultSSLConfig)); | 168 server_.kMismatchedHostName, kDefaultSSLConfig)); |
| 152 | 169 |
| 153 EXPECT_FALSE(sock->IsConnected()); | 170 EXPECT_FALSE(sock->IsConnected()); |
| 154 | 171 |
| 155 rv = sock->Connect(&callback); | 172 scoped_refptr<net::LoadLog> log(new net::LoadLog); |
| 173 rv = sock->Connect(&callback, log); |
| 174 EXPECT_TRUE(net::LogContains( |
| 175 *log, 0, net::LoadLog::TYPE_SSL_CONNECT, net::LoadLog::PHASE_BEGIN)); |
| 156 if (rv != net::ERR_CERT_COMMON_NAME_INVALID) { | 176 if (rv != net::ERR_CERT_COMMON_NAME_INVALID) { |
| 157 ASSERT_EQ(net::ERR_IO_PENDING, rv); | 177 ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 158 EXPECT_FALSE(sock->IsConnected()); | 178 EXPECT_FALSE(sock->IsConnected()); |
| 179 EXPECT_FALSE(net::LogContains( |
| 180 *log, -1, net::LoadLog::TYPE_SSL_CONNECT, net::LoadLog::PHASE_END)); |
| 159 | 181 |
| 160 rv = callback.WaitForResult(); | 182 rv = callback.WaitForResult(); |
| 161 EXPECT_EQ(net::ERR_CERT_COMMON_NAME_INVALID, rv); | 183 EXPECT_EQ(net::ERR_CERT_COMMON_NAME_INVALID, rv); |
| 162 } | 184 } |
| 163 | 185 |
| 164 // We cannot test sock->IsConnected(), as the NSS implementation disconnects | 186 // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 165 // the socket when it encounters an error, whereas other implementations | 187 // the socket when it encounters an error, whereas other implementations |
| 166 // leave it connected. | 188 // leave it connected. |
| 189 |
| 190 EXPECT_TRUE(net::LogContains( |
| 191 *log, -1, net::LoadLog::TYPE_SSL_CONNECT, net::LoadLog::PHASE_END)); |
| 167 } | 192 } |
| 168 | 193 |
| 169 // TODO(wtc): Add unit tests for IsConnectedAndIdle: | 194 // TODO(wtc): Add unit tests for IsConnectedAndIdle: |
| 170 // - Server closes an SSL connection (with a close_notify alert message). | 195 // - Server closes an SSL connection (with a close_notify alert message). |
| 171 // - Server closes the underlying TCP connection directly. | 196 // - Server closes the underlying TCP connection directly. |
| 172 // - Server sends data unexpectedly. | 197 // - Server sends data unexpectedly. |
| 173 | 198 |
| 174 TEST_F(SSLClientSocketTest, Read) { | 199 TEST_F(SSLClientSocketTest, Read) { |
| 175 StartOKServer(); | 200 StartOKServer(); |
| 176 | 201 |
| 177 net::AddressList addr; | 202 net::AddressList addr; |
| 178 TestCompletionCallback callback; | 203 TestCompletionCallback callback; |
| 179 | 204 |
| 180 net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); | 205 net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); |
| 181 int rv = resolver_->Resolve(info, &addr, &callback, NULL, NULL); | 206 int rv = resolver_->Resolve(info, &addr, &callback, NULL, NULL); |
| 182 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 207 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 183 | 208 |
| 184 rv = callback.WaitForResult(); | 209 rv = callback.WaitForResult(); |
| 185 EXPECT_EQ(net::OK, rv); | 210 EXPECT_EQ(net::OK, rv); |
| 186 | 211 |
| 187 net::ClientSocket *transport = new net::TCPClientSocket(addr); | 212 net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 188 rv = transport->Connect(&callback); | 213 rv = transport->Connect(&callback, NULL); |
| 189 if (rv == net::ERR_IO_PENDING) | 214 if (rv == net::ERR_IO_PENDING) |
| 190 rv = callback.WaitForResult(); | 215 rv = callback.WaitForResult(); |
| 191 EXPECT_EQ(net::OK, rv); | 216 EXPECT_EQ(net::OK, rv); |
| 192 | 217 |
| 193 scoped_ptr<net::SSLClientSocket> sock( | 218 scoped_ptr<net::SSLClientSocket> sock( |
| 194 socket_factory_->CreateSSLClientSocket(transport, | 219 socket_factory_->CreateSSLClientSocket(transport, |
| 195 server_.kHostName, | 220 server_.kHostName, |
| 196 kDefaultSSLConfig)); | 221 kDefaultSSLConfig)); |
| 197 | 222 |
| 198 rv = sock->Connect(&callback); | 223 rv = sock->Connect(&callback, NULL); |
| 199 if (rv != net::OK) { | 224 if (rv != net::OK) { |
| 200 ASSERT_EQ(net::ERR_IO_PENDING, rv); | 225 ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 201 | 226 |
| 202 rv = callback.WaitForResult(); | 227 rv = callback.WaitForResult(); |
| 203 EXPECT_EQ(net::OK, rv); | 228 EXPECT_EQ(net::OK, rv); |
| 204 } | 229 } |
| 205 EXPECT_TRUE(sock->IsConnected()); | 230 EXPECT_TRUE(sock->IsConnected()); |
| 206 | 231 |
| 207 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 232 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 208 scoped_refptr<net::IOBuffer> request_buffer = | 233 scoped_refptr<net::IOBuffer> request_buffer = |
| (...skipping 26 matching lines...) Expand all Loading... |
| 235 StartOKServer(); | 260 StartOKServer(); |
| 236 | 261 |
| 237 net::AddressList addr; | 262 net::AddressList addr; |
| 238 TestCompletionCallback callback; | 263 TestCompletionCallback callback; |
| 239 | 264 |
| 240 net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); | 265 net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); |
| 241 int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); | 266 int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); |
| 242 EXPECT_EQ(net::OK, rv); | 267 EXPECT_EQ(net::OK, rv); |
| 243 | 268 |
| 244 net::ClientSocket *transport = new net::TCPClientSocket(addr); | 269 net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 245 rv = transport->Connect(&callback); | 270 rv = transport->Connect(&callback, NULL); |
| 246 if (rv == net::ERR_IO_PENDING) | 271 if (rv == net::ERR_IO_PENDING) |
| 247 rv = callback.WaitForResult(); | 272 rv = callback.WaitForResult(); |
| 248 EXPECT_EQ(net::OK, rv); | 273 EXPECT_EQ(net::OK, rv); |
| 249 | 274 |
| 250 scoped_ptr<net::SSLClientSocket> sock( | 275 scoped_ptr<net::SSLClientSocket> sock( |
| 251 socket_factory_->CreateSSLClientSocket(transport, | 276 socket_factory_->CreateSSLClientSocket(transport, |
| 252 server_.kHostName, kDefaultSSLConfig)); | 277 server_.kHostName, kDefaultSSLConfig)); |
| 253 | 278 |
| 254 rv = sock->Connect(&callback); | 279 rv = sock->Connect(&callback, NULL); |
| 255 if (rv != net::OK) { | 280 if (rv != net::OK) { |
| 256 ASSERT_EQ(net::ERR_IO_PENDING, rv); | 281 ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 257 | 282 |
| 258 rv = callback.WaitForResult(); | 283 rv = callback.WaitForResult(); |
| 259 EXPECT_EQ(net::OK, rv); | 284 EXPECT_EQ(net::OK, rv); |
| 260 } | 285 } |
| 261 | 286 |
| 262 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 287 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 263 scoped_refptr<net::IOBuffer> request_buffer = | 288 scoped_refptr<net::IOBuffer> request_buffer = |
| 264 new net::IOBuffer(arraysize(request_text) - 1); | 289 new net::IOBuffer(arraysize(request_text) - 1); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 290 StartOKServer(); | 315 StartOKServer(); |
| 291 | 316 |
| 292 net::AddressList addr; | 317 net::AddressList addr; |
| 293 TestCompletionCallback callback; | 318 TestCompletionCallback callback; |
| 294 | 319 |
| 295 net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); | 320 net::HostResolver::RequestInfo info(server_.kHostName, server_.kOKHTTPSPort); |
| 296 int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); | 321 int rv = resolver_->Resolve(info, &addr, NULL, NULL, NULL); |
| 297 EXPECT_EQ(net::OK, rv); | 322 EXPECT_EQ(net::OK, rv); |
| 298 | 323 |
| 299 net::ClientSocket *transport = new net::TCPClientSocket(addr); | 324 net::ClientSocket *transport = new net::TCPClientSocket(addr); |
| 300 rv = transport->Connect(&callback); | 325 rv = transport->Connect(&callback, NULL); |
| 301 if (rv == net::ERR_IO_PENDING) | 326 if (rv == net::ERR_IO_PENDING) |
| 302 rv = callback.WaitForResult(); | 327 rv = callback.WaitForResult(); |
| 303 EXPECT_EQ(net::OK, rv); | 328 EXPECT_EQ(net::OK, rv); |
| 304 | 329 |
| 305 scoped_ptr<net::SSLClientSocket> sock( | 330 scoped_ptr<net::SSLClientSocket> sock( |
| 306 socket_factory_->CreateSSLClientSocket(transport, | 331 socket_factory_->CreateSSLClientSocket(transport, |
| 307 server_.kHostName, kDefaultSSLConfig)); | 332 server_.kHostName, kDefaultSSLConfig)); |
| 308 | 333 |
| 309 rv = sock->Connect(&callback); | 334 rv = sock->Connect(&callback, NULL); |
| 310 if (rv != net::OK) { | 335 if (rv != net::OK) { |
| 311 ASSERT_EQ(net::ERR_IO_PENDING, rv); | 336 ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 312 | 337 |
| 313 rv = callback.WaitForResult(); | 338 rv = callback.WaitForResult(); |
| 314 EXPECT_EQ(net::OK, rv); | 339 EXPECT_EQ(net::OK, rv); |
| 315 } | 340 } |
| 316 | 341 |
| 317 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; | 342 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 318 scoped_refptr<net::IOBuffer> request_buffer = | 343 scoped_refptr<net::IOBuffer> request_buffer = |
| 319 new net::IOBuffer(arraysize(request_text) - 1); | 344 new net::IOBuffer(arraysize(request_text) - 1); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 330 // Do a partial read and then exit. This test should not crash! | 355 // Do a partial read and then exit. This test should not crash! |
| 331 scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(512); | 356 scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(512); |
| 332 rv = sock->Read(buf, 512, &callback); | 357 rv = sock->Read(buf, 512, &callback); |
| 333 EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); | 358 EXPECT_TRUE(rv >= 0 || rv == net::ERR_IO_PENDING); |
| 334 | 359 |
| 335 if (rv == net::ERR_IO_PENDING) | 360 if (rv == net::ERR_IO_PENDING) |
| 336 rv = callback.WaitForResult(); | 361 rv = callback.WaitForResult(); |
| 337 | 362 |
| 338 EXPECT_NE(rv, 0); | 363 EXPECT_NE(rv, 0); |
| 339 } | 364 } |
| OLD | NEW |