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