| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 1245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1256 | 1256 |
| 1257 sock->Disconnect(); | 1257 sock->Disconnect(); |
| 1258 EXPECT_FALSE(sock->IsConnected()); | 1258 EXPECT_FALSE(sock->IsConnected()); |
| 1259 } | 1259 } |
| 1260 | 1260 |
| 1261 // TODO(wtc): Add unit tests for IsConnectedAndIdle: | 1261 // TODO(wtc): Add unit tests for IsConnectedAndIdle: |
| 1262 // - Server closes an SSL connection (with a close_notify alert message). | 1262 // - Server closes an SSL connection (with a close_notify alert message). |
| 1263 // - Server closes the underlying TCP connection directly. | 1263 // - Server closes the underlying TCP connection directly. |
| 1264 // - Server sends data unexpectedly. | 1264 // - Server sends data unexpectedly. |
| 1265 | 1265 |
| 1266 // Tests that the socket can be read from successfully. Also test that a peer's |
| 1267 // close_notify alert is successfully processed without error. |
| 1266 TEST_F(SSLClientSocketTest, Read) { | 1268 TEST_F(SSLClientSocketTest, Read) { |
| 1267 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 1269 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
| 1268 SpawnedTestServer::kLocalhost, | 1270 SpawnedTestServer::kLocalhost, |
| 1269 base::FilePath()); | 1271 base::FilePath()); |
| 1270 ASSERT_TRUE(test_server.Start()); | 1272 ASSERT_TRUE(test_server.Start()); |
| 1271 | 1273 |
| 1272 AddressList addr; | 1274 AddressList addr; |
| 1273 ASSERT_TRUE(test_server.GetAddressList(&addr)); | 1275 ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 1274 | 1276 |
| 1275 TestCompletionCallback callback; | 1277 TestCompletionCallback callback; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1307 rv = sock->Read(buf.get(), 4096, callback.callback()); | 1309 rv = sock->Read(buf.get(), 4096, callback.callback()); |
| 1308 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 1310 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
| 1309 | 1311 |
| 1310 if (rv == ERR_IO_PENDING) | 1312 if (rv == ERR_IO_PENDING) |
| 1311 rv = callback.WaitForResult(); | 1313 rv = callback.WaitForResult(); |
| 1312 | 1314 |
| 1313 EXPECT_GE(rv, 0); | 1315 EXPECT_GE(rv, 0); |
| 1314 if (rv <= 0) | 1316 if (rv <= 0) |
| 1315 break; | 1317 break; |
| 1316 } | 1318 } |
| 1319 |
| 1320 // The peer should have cleanly closed the connection with a close_notify. |
| 1321 EXPECT_EQ(0, rv); |
| 1317 } | 1322 } |
| 1318 | 1323 |
| 1319 // Tests that SSLClientSocket properly handles when the underlying transport | 1324 // Tests that SSLClientSocket properly handles when the underlying transport |
| 1320 // synchronously fails a transport read in during the handshake. The error code | 1325 // synchronously fails a transport read in during the handshake. The error code |
| 1321 // should be preserved so SSLv3 fallback logic can condition on it. | 1326 // should be preserved so SSLv3 fallback logic can condition on it. |
| 1322 TEST_F(SSLClientSocketTest, Connect_WithSynchronousError) { | 1327 TEST_F(SSLClientSocketTest, Connect_WithSynchronousError) { |
| 1323 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 1328 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
| 1324 SpawnedTestServer::kLocalhost, | 1329 SpawnedTestServer::kLocalhost, |
| 1325 base::FilePath()); | 1330 base::FilePath()); |
| 1326 ASSERT_TRUE(test_server.Start()); | 1331 ASSERT_TRUE(test_server.Start()); |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1847 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( | 1852 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
| 1848 transport.Pass(), test_server.host_port_pair(), SSLConfig())); | 1853 transport.Pass(), test_server.host_port_pair(), SSLConfig())); |
| 1849 | 1854 |
| 1850 raw_transport->SetNextReadError(0); | 1855 raw_transport->SetNextReadError(0); |
| 1851 | 1856 |
| 1852 rv = callback.GetResult(sock->Connect(callback.callback())); | 1857 rv = callback.GetResult(sock->Connect(callback.callback())); |
| 1853 EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); | 1858 EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); |
| 1854 EXPECT_FALSE(sock->IsConnected()); | 1859 EXPECT_FALSE(sock->IsConnected()); |
| 1855 } | 1860 } |
| 1856 | 1861 |
| 1857 // Tests that SSLClientSocket cleanly returns a Read of size 0 if the | 1862 // Tests that SSLClientSocket returns a Read of size 0 if the underlying socket |
| 1858 // underlying socket is cleanly closed. | 1863 // is cleanly closed, but the peer does not send close_notify. |
| 1859 // This is a regression test for https://crbug.com/422246 | 1864 // This is a regression test for https://crbug.com/422246 |
| 1860 TEST_F(SSLClientSocketTest, Read_WithZeroReturn) { | 1865 TEST_F(SSLClientSocketTest, Read_WithZeroReturn) { |
| 1861 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 1866 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
| 1862 SpawnedTestServer::kLocalhost, | 1867 SpawnedTestServer::kLocalhost, |
| 1863 base::FilePath()); | 1868 base::FilePath()); |
| 1864 ASSERT_TRUE(test_server.Start()); | 1869 ASSERT_TRUE(test_server.Start()); |
| 1865 | 1870 |
| 1866 AddressList addr; | 1871 AddressList addr; |
| 1867 ASSERT_TRUE(test_server.GetAddressList(&addr)); | 1872 ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 1868 | 1873 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1935 raw_transport->BlockReadResult(); | 1940 raw_transport->BlockReadResult(); |
| 1936 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1941 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
| 1937 rv = sock->Read(buf.get(), 4096, callback.callback()); | 1942 rv = sock->Read(buf.get(), 4096, callback.callback()); |
| 1938 EXPECT_EQ(ERR_IO_PENDING, rv); | 1943 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1939 | 1944 |
| 1940 raw_transport->UnblockReadResult(); | 1945 raw_transport->UnblockReadResult(); |
| 1941 rv = callback.GetResult(rv); | 1946 rv = callback.GetResult(rv); |
| 1942 EXPECT_EQ(0, rv); | 1947 EXPECT_EQ(0, rv); |
| 1943 } | 1948 } |
| 1944 | 1949 |
| 1950 // Tests that fatal alerts from the peer are processed. This is a regression |
| 1951 // test for https://crbug.com/466303. |
| 1952 TEST_F(SSLClientSocketTest, Read_WithFatalAlert) { |
| 1953 SpawnedTestServer::SSLOptions ssl_options; |
| 1954 ssl_options.alert_after_handshake = true; |
| 1955 ASSERT_TRUE(StartTestServer(ssl_options)); |
| 1956 |
| 1957 SSLConfig ssl_config; |
| 1958 TestCompletionCallback callback; |
| 1959 scoped_ptr<StreamSocket> transport( |
| 1960 new TCPClientSocket(addr(), &log_, NetLog::Source())); |
| 1961 EXPECT_EQ(OK, callback.GetResult(transport->Connect(callback.callback()))); |
| 1962 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
| 1963 transport.Pass(), test_server()->host_port_pair(), ssl_config)); |
| 1964 EXPECT_EQ(OK, callback.GetResult(sock->Connect(callback.callback()))); |
| 1965 |
| 1966 // Receive the fatal alert. |
| 1967 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
| 1968 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, callback.GetResult(sock->Read( |
| 1969 buf.get(), 4096, callback.callback()))); |
| 1970 } |
| 1971 |
| 1945 TEST_F(SSLClientSocketTest, Read_SmallChunks) { | 1972 TEST_F(SSLClientSocketTest, Read_SmallChunks) { |
| 1946 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 1973 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
| 1947 SpawnedTestServer::kLocalhost, | 1974 SpawnedTestServer::kLocalhost, |
| 1948 base::FilePath()); | 1975 base::FilePath()); |
| 1949 ASSERT_TRUE(test_server.Start()); | 1976 ASSERT_TRUE(test_server.Start()); |
| 1950 | 1977 |
| 1951 AddressList addr; | 1978 AddressList addr; |
| 1952 ASSERT_TRUE(test_server.GetAddressList(&addr)); | 1979 ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 1953 | 1980 |
| 1954 TestCompletionCallback callback; | 1981 TestCompletionCallback callback; |
| (...skipping 1311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3266 ssl_config.channel_id_enabled = true; | 3293 ssl_config.channel_id_enabled = true; |
| 3267 | 3294 |
| 3268 int rv; | 3295 int rv; |
| 3269 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | 3296 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); |
| 3270 | 3297 |
| 3271 EXPECT_EQ(ERR_UNEXPECTED, rv); | 3298 EXPECT_EQ(ERR_UNEXPECTED, rv); |
| 3272 EXPECT_FALSE(sock_->IsConnected()); | 3299 EXPECT_FALSE(sock_->IsConnected()); |
| 3273 } | 3300 } |
| 3274 | 3301 |
| 3275 } // namespace net | 3302 } // namespace net |
| OLD | NEW |