Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/net_log.h" | 10 #include "net/base/net_log.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 SSLClientSocketTest() | 28 SSLClientSocketTest() |
| 29 : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) { | 29 : socket_factory_(net::ClientSocketFactory::GetDefaultFactory()) { |
| 30 } | 30 } |
| 31 | 31 |
| 32 protected: | 32 protected: |
| 33 net::ClientSocketFactory* socket_factory_; | 33 net::ClientSocketFactory* socket_factory_; |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 //----------------------------------------------------------------------------- | 36 //----------------------------------------------------------------------------- |
| 37 | 37 |
| 38 // LogContainsSSLConnectEndEvent returns true if the given index in the given | |
| 39 // log is an SSL connect end event. The NSS sockets will cork in an attempt to | |
| 40 // merge the first application data record with the Finished message when false | |
| 41 // starting. However, in order to avoid the server timing out the handshake, | |
| 42 // they'll give up waiting for application data and send the Finished after a | |
| 43 // timeout. This means that an SSL connect end event may appear as a socket | |
|
wtc
2010/10/07 21:29:54
This sentence is confusing. Can you explain exact
| |
| 44 // write. | |
| 45 static bool LogContainsSSLConnectEndEvent( | |
| 46 const net::CapturingNetLog::EntryList& log, int i) { | |
|
wtc
2010/10/07 21:25:26
BUG: the parameter 'i' is not used. It seems that
| |
| 47 return net::LogContainsEndEvent(log, -1, net::NetLog::TYPE_SSL_CONNECT) || | |
| 48 net::LogContainsEndEvent(log, -1, net::NetLog::TYPE_SOCKET_BYTES_SENT); | |
| 49 }; | |
| 50 | |
| 38 TEST_F(SSLClientSocketTest, Connect) { | 51 TEST_F(SSLClientSocketTest, Connect) { |
| 39 net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath()); | 52 net::TestServer test_server(net::TestServer::TYPE_HTTPS, FilePath()); |
| 40 ASSERT_TRUE(test_server.Start()); | 53 ASSERT_TRUE(test_server.Start()); |
| 41 | 54 |
| 42 net::AddressList addr; | 55 net::AddressList addr; |
| 43 ASSERT_TRUE(test_server.GetAddressList(&addr)); | 56 ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 44 | 57 |
| 45 TestCompletionCallback callback; | 58 TestCompletionCallback callback; |
| 46 net::CapturingNetLog log(net::CapturingNetLog::kUnbounded); | 59 net::CapturingNetLog log(net::CapturingNetLog::kUnbounded); |
| 47 net::ClientSocket* transport = new net::TCPClientSocket( | 60 net::ClientSocket* transport = new net::TCPClientSocket( |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 64 ASSERT_EQ(net::ERR_IO_PENDING, rv); | 77 ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 65 EXPECT_FALSE(sock->IsConnected()); | 78 EXPECT_FALSE(sock->IsConnected()); |
| 66 EXPECT_FALSE(net::LogContainsEndEvent( | 79 EXPECT_FALSE(net::LogContainsEndEvent( |
| 67 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); | 80 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); |
| 68 | 81 |
| 69 rv = callback.WaitForResult(); | 82 rv = callback.WaitForResult(); |
| 70 EXPECT_EQ(net::OK, rv); | 83 EXPECT_EQ(net::OK, rv); |
| 71 } | 84 } |
| 72 | 85 |
| 73 EXPECT_TRUE(sock->IsConnected()); | 86 EXPECT_TRUE(sock->IsConnected()); |
| 74 EXPECT_TRUE(net::LogContainsEndEvent( | 87 EXPECT_TRUE(LogContainsSSLConnectEndEvent(log.entries(), -1)); |
| 75 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); | |
| 76 | 88 |
| 77 sock->Disconnect(); | 89 sock->Disconnect(); |
| 78 EXPECT_FALSE(sock->IsConnected()); | 90 EXPECT_FALSE(sock->IsConnected()); |
| 79 } | 91 } |
| 80 | 92 |
| 81 TEST_F(SSLClientSocketTest, ConnectExpired) { | 93 TEST_F(SSLClientSocketTest, ConnectExpired) { |
| 82 net::TestServer test_server(net::TestServer::TYPE_HTTPS_EXPIRED_CERTIFICATE, | 94 net::TestServer test_server(net::TestServer::TYPE_HTTPS_EXPIRED_CERTIFICATE, |
| 83 FilePath()); | 95 FilePath()); |
| 84 ASSERT_TRUE(test_server.Start()); | 96 ASSERT_TRUE(test_server.Start()); |
| 85 | 97 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 110 EXPECT_FALSE(net::LogContainsEndEvent( | 122 EXPECT_FALSE(net::LogContainsEndEvent( |
| 111 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); | 123 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); |
| 112 | 124 |
| 113 rv = callback.WaitForResult(); | 125 rv = callback.WaitForResult(); |
| 114 EXPECT_EQ(net::ERR_CERT_DATE_INVALID, rv); | 126 EXPECT_EQ(net::ERR_CERT_DATE_INVALID, rv); |
| 115 } | 127 } |
| 116 | 128 |
| 117 // We cannot test sock->IsConnected(), as the NSS implementation disconnects | 129 // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 118 // the socket when it encounters an error, whereas other implementations | 130 // the socket when it encounters an error, whereas other implementations |
| 119 // leave it connected. | 131 // leave it connected. |
| 120 | 132 EXPECT_TRUE(LogContainsSSLConnectEndEvent(log.entries(), -1)); |
| 121 EXPECT_TRUE(net::LogContainsEndEvent( | |
| 122 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); | |
| 123 } | 133 } |
| 124 | 134 |
| 125 TEST_F(SSLClientSocketTest, ConnectMismatched) { | 135 TEST_F(SSLClientSocketTest, ConnectMismatched) { |
| 126 net::TestServer test_server(net::TestServer::TYPE_HTTPS_MISMATCHED_HOSTNAME, | 136 net::TestServer test_server(net::TestServer::TYPE_HTTPS_MISMATCHED_HOSTNAME, |
| 127 FilePath()); | 137 FilePath()); |
| 128 ASSERT_TRUE(test_server.Start()); | 138 ASSERT_TRUE(test_server.Start()); |
| 129 | 139 |
| 130 net::AddressList addr; | 140 net::AddressList addr; |
| 131 ASSERT_TRUE(test_server.GetAddressList(&addr)); | 141 ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 132 | 142 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 155 EXPECT_FALSE(net::LogContainsEndEvent( | 165 EXPECT_FALSE(net::LogContainsEndEvent( |
| 156 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); | 166 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); |
| 157 | 167 |
| 158 rv = callback.WaitForResult(); | 168 rv = callback.WaitForResult(); |
| 159 EXPECT_EQ(net::ERR_CERT_COMMON_NAME_INVALID, rv); | 169 EXPECT_EQ(net::ERR_CERT_COMMON_NAME_INVALID, rv); |
| 160 } | 170 } |
| 161 | 171 |
| 162 // We cannot test sock->IsConnected(), as the NSS implementation disconnects | 172 // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 163 // the socket when it encounters an error, whereas other implementations | 173 // the socket when it encounters an error, whereas other implementations |
| 164 // leave it connected. | 174 // leave it connected. |
| 165 | 175 EXPECT_TRUE(LogContainsSSLConnectEndEvent(log.entries(), -1)); |
| 166 EXPECT_TRUE(net::LogContainsEndEvent( | |
| 167 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); | |
| 168 } | 176 } |
| 169 | 177 |
| 170 // Attempt to connect to a page which requests a client certificate. It should | 178 // Attempt to connect to a page which requests a client certificate. It should |
| 171 // return an error code on connect. | 179 // return an error code on connect. |
| 172 TEST_F(SSLClientSocketTest, FLAKY_ConnectClientAuthCertRequested) { | 180 TEST_F(SSLClientSocketTest, FLAKY_ConnectClientAuthCertRequested) { |
| 173 net::TestServer test_server(net::TestServer::TYPE_HTTPS_CLIENT_AUTH, | 181 net::TestServer test_server(net::TestServer::TYPE_HTTPS_CLIENT_AUTH, |
| 174 FilePath()); | 182 FilePath()); |
| 175 ASSERT_TRUE(test_server.Start()); | 183 ASSERT_TRUE(test_server.Start()); |
| 176 | 184 |
| 177 net::AddressList addr; | 185 net::AddressList addr; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 201 EXPECT_FALSE(net::LogContainsEndEvent( | 209 EXPECT_FALSE(net::LogContainsEndEvent( |
| 202 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); | 210 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); |
| 203 | 211 |
| 204 rv = callback.WaitForResult(); | 212 rv = callback.WaitForResult(); |
| 205 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); | 213 EXPECT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); |
| 206 } | 214 } |
| 207 | 215 |
| 208 // We cannot test sock->IsConnected(), as the NSS implementation disconnects | 216 // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 209 // the socket when it encounters an error, whereas other implementations | 217 // the socket when it encounters an error, whereas other implementations |
| 210 // leave it connected. | 218 // leave it connected. |
| 211 | 219 EXPECT_TRUE(LogContainsSSLConnectEndEvent(log.entries(), -1)); |
| 212 EXPECT_TRUE(net::LogContainsEndEvent( | |
| 213 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); | |
| 214 } | 220 } |
| 215 | 221 |
| 216 // Connect to a server requesting optional client authentication. Send it a | 222 // Connect to a server requesting optional client authentication. Send it a |
| 217 // null certificate. It should allow the connection. | 223 // null certificate. It should allow the connection. |
| 218 // | 224 // |
| 219 // TODO(davidben): Also test providing an actual certificate. | 225 // TODO(davidben): Also test providing an actual certificate. |
| 220 TEST_F(SSLClientSocketTest, ConnectClientAuthSendNullCert) { | 226 TEST_F(SSLClientSocketTest, ConnectClientAuthSendNullCert) { |
| 221 net::TestServer test_server(net::TestServer::TYPE_HTTPS_CLIENT_AUTH, | 227 net::TestServer test_server(net::TestServer::TYPE_HTTPS_CLIENT_AUTH, |
| 222 FilePath()); | 228 FilePath()); |
| 223 ASSERT_TRUE(test_server.Start()); | 229 ASSERT_TRUE(test_server.Start()); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 253 ASSERT_EQ(net::ERR_IO_PENDING, rv); | 259 ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 254 EXPECT_FALSE(sock->IsConnected()); | 260 EXPECT_FALSE(sock->IsConnected()); |
| 255 EXPECT_FALSE(net::LogContainsEndEvent( | 261 EXPECT_FALSE(net::LogContainsEndEvent( |
| 256 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); | 262 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); |
| 257 | 263 |
| 258 rv = callback.WaitForResult(); | 264 rv = callback.WaitForResult(); |
| 259 EXPECT_EQ(net::OK, rv); | 265 EXPECT_EQ(net::OK, rv); |
| 260 } | 266 } |
| 261 | 267 |
| 262 EXPECT_TRUE(sock->IsConnected()); | 268 EXPECT_TRUE(sock->IsConnected()); |
| 263 EXPECT_TRUE(net::LogContainsEndEvent( | 269 EXPECT_TRUE(LogContainsSSLConnectEndEvent(log.entries(), -1)); |
| 264 log.entries(), -1, net::NetLog::TYPE_SSL_CONNECT)); | |
| 265 | 270 |
| 266 sock->Disconnect(); | 271 sock->Disconnect(); |
| 267 EXPECT_FALSE(sock->IsConnected()); | 272 EXPECT_FALSE(sock->IsConnected()); |
| 268 } | 273 } |
| 269 | 274 |
| 270 // TODO(wtc): Add unit tests for IsConnectedAndIdle: | 275 // TODO(wtc): Add unit tests for IsConnectedAndIdle: |
| 271 // - Server closes an SSL connection (with a close_notify alert message). | 276 // - Server closes an SSL connection (with a close_notify alert message). |
| 272 // - Server closes the underlying TCP connection directly. | 277 // - Server closes the underlying TCP connection directly. |
| 273 // - Server sends data unexpectedly. | 278 // - Server sends data unexpectedly. |
| 274 | 279 |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 532 rv = callback.WaitForResult(); | 537 rv = callback.WaitForResult(); |
| 533 EXPECT_EQ(net::OK, rv); | 538 EXPECT_EQ(net::OK, rv); |
| 534 | 539 |
| 535 scoped_ptr<net::SSLClientSocket> sock( | 540 scoped_ptr<net::SSLClientSocket> sock( |
| 536 socket_factory_->CreateSSLClientSocket( | 541 socket_factory_->CreateSSLClientSocket( |
| 537 transport, test_server.host_port_pair().host(), kDefaultSSLConfig)); | 542 transport, test_server.host_port_pair().host(), kDefaultSSLConfig)); |
| 538 | 543 |
| 539 rv = sock->Connect(&callback); | 544 rv = sock->Connect(&callback); |
| 540 EXPECT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv); | 545 EXPECT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv); |
| 541 } | 546 } |
| OLD | NEW |