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 1207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1218 | 1218 |
1219 sock->Disconnect(); | 1219 sock->Disconnect(); |
1220 EXPECT_FALSE(sock->IsConnected()); | 1220 EXPECT_FALSE(sock->IsConnected()); |
1221 } | 1221 } |
1222 | 1222 |
1223 // TODO(wtc): Add unit tests for IsConnectedAndIdle: | 1223 // TODO(wtc): Add unit tests for IsConnectedAndIdle: |
1224 // - Server closes an SSL connection (with a close_notify alert message). | 1224 // - Server closes an SSL connection (with a close_notify alert message). |
1225 // - Server closes the underlying TCP connection directly. | 1225 // - Server closes the underlying TCP connection directly. |
1226 // - Server sends data unexpectedly. | 1226 // - Server sends data unexpectedly. |
1227 | 1227 |
| 1228 // Tests that the socket can be read from successfully. Also test that a peer's |
| 1229 // close_notify alert is successfully processed without error. |
1228 TEST_F(SSLClientSocketTest, Read) { | 1230 TEST_F(SSLClientSocketTest, Read) { |
1229 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 1231 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
1230 SpawnedTestServer::kLocalhost, | 1232 SpawnedTestServer::kLocalhost, |
1231 base::FilePath()); | 1233 base::FilePath()); |
1232 ASSERT_TRUE(test_server.Start()); | 1234 ASSERT_TRUE(test_server.Start()); |
1233 | 1235 |
1234 AddressList addr; | 1236 AddressList addr; |
1235 ASSERT_TRUE(test_server.GetAddressList(&addr)); | 1237 ASSERT_TRUE(test_server.GetAddressList(&addr)); |
1236 | 1238 |
1237 TestCompletionCallback callback; | 1239 TestCompletionCallback callback; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1269 rv = sock->Read(buf.get(), 4096, callback.callback()); | 1271 rv = sock->Read(buf.get(), 4096, callback.callback()); |
1270 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); | 1272 EXPECT_TRUE(rv >= 0 || rv == ERR_IO_PENDING); |
1271 | 1273 |
1272 if (rv == ERR_IO_PENDING) | 1274 if (rv == ERR_IO_PENDING) |
1273 rv = callback.WaitForResult(); | 1275 rv = callback.WaitForResult(); |
1274 | 1276 |
1275 EXPECT_GE(rv, 0); | 1277 EXPECT_GE(rv, 0); |
1276 if (rv <= 0) | 1278 if (rv <= 0) |
1277 break; | 1279 break; |
1278 } | 1280 } |
| 1281 |
| 1282 // The peer should have cleanly closed the connection with a close_notify. |
| 1283 EXPECT_EQ(0, rv); |
1279 } | 1284 } |
1280 | 1285 |
1281 // Tests that SSLClientSocket properly handles when the underlying transport | 1286 // Tests that SSLClientSocket properly handles when the underlying transport |
1282 // synchronously fails a transport read in during the handshake. The error code | 1287 // synchronously fails a transport read in during the handshake. The error code |
1283 // should be preserved so SSLv3 fallback logic can condition on it. | 1288 // should be preserved so SSLv3 fallback logic can condition on it. |
1284 TEST_F(SSLClientSocketTest, Connect_WithSynchronousError) { | 1289 TEST_F(SSLClientSocketTest, Connect_WithSynchronousError) { |
1285 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 1290 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
1286 SpawnedTestServer::kLocalhost, | 1291 SpawnedTestServer::kLocalhost, |
1287 base::FilePath()); | 1292 base::FilePath()); |
1288 ASSERT_TRUE(test_server.Start()); | 1293 ASSERT_TRUE(test_server.Start()); |
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1809 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( | 1814 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
1810 transport.Pass(), test_server.host_port_pair(), SSLConfig())); | 1815 transport.Pass(), test_server.host_port_pair(), SSLConfig())); |
1811 | 1816 |
1812 raw_transport->SetNextReadError(0); | 1817 raw_transport->SetNextReadError(0); |
1813 | 1818 |
1814 rv = callback.GetResult(sock->Connect(callback.callback())); | 1819 rv = callback.GetResult(sock->Connect(callback.callback())); |
1815 EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); | 1820 EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); |
1816 EXPECT_FALSE(sock->IsConnected()); | 1821 EXPECT_FALSE(sock->IsConnected()); |
1817 } | 1822 } |
1818 | 1823 |
1819 // Tests that SSLClientSocket cleanly returns a Read of size 0 if the | 1824 // Tests that SSLClientSocket returns a Read of size 0 if the underlying socket |
1820 // underlying socket is cleanly closed. | 1825 // is cleanly closed, but the peer does not send close_notify. |
1821 // This is a regression test for https://crbug.com/422246 | 1826 // This is a regression test for https://crbug.com/422246 |
1822 TEST_F(SSLClientSocketTest, Read_WithZeroReturn) { | 1827 TEST_F(SSLClientSocketTest, Read_WithZeroReturn) { |
1823 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 1828 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
1824 SpawnedTestServer::kLocalhost, | 1829 SpawnedTestServer::kLocalhost, |
1825 base::FilePath()); | 1830 base::FilePath()); |
1826 ASSERT_TRUE(test_server.Start()); | 1831 ASSERT_TRUE(test_server.Start()); |
1827 | 1832 |
1828 AddressList addr; | 1833 AddressList addr; |
1829 ASSERT_TRUE(test_server.GetAddressList(&addr)); | 1834 ASSERT_TRUE(test_server.GetAddressList(&addr)); |
1830 | 1835 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1897 raw_transport->BlockReadResult(); | 1902 raw_transport->BlockReadResult(); |
1898 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1903 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1899 rv = sock->Read(buf.get(), 4096, callback.callback()); | 1904 rv = sock->Read(buf.get(), 4096, callback.callback()); |
1900 EXPECT_EQ(ERR_IO_PENDING, rv); | 1905 EXPECT_EQ(ERR_IO_PENDING, rv); |
1901 | 1906 |
1902 raw_transport->UnblockReadResult(); | 1907 raw_transport->UnblockReadResult(); |
1903 rv = callback.GetResult(rv); | 1908 rv = callback.GetResult(rv); |
1904 EXPECT_EQ(0, rv); | 1909 EXPECT_EQ(0, rv); |
1905 } | 1910 } |
1906 | 1911 |
| 1912 // Tests that fatal alerts from the peer are processed. This is a regression |
| 1913 // test for https://crbug.com/466303. |
| 1914 TEST_F(SSLClientSocketTest, Read_WithFatalAlert) { |
| 1915 SpawnedTestServer::SSLOptions ssl_options; |
| 1916 ssl_options.alert_after_handshake = true; |
| 1917 ASSERT_TRUE(StartTestServer(ssl_options)); |
| 1918 |
| 1919 SSLConfig ssl_config; |
| 1920 TestCompletionCallback callback; |
| 1921 scoped_ptr<StreamSocket> transport( |
| 1922 new TCPClientSocket(addr(), &log_, NetLog::Source())); |
| 1923 EXPECT_EQ(OK, callback.GetResult(transport->Connect(callback.callback()))); |
| 1924 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
| 1925 transport.Pass(), test_server()->host_port_pair(), ssl_config)); |
| 1926 EXPECT_EQ(OK, callback.GetResult(sock->Connect(callback.callback()))); |
| 1927 |
| 1928 // Receive the fatal alert. |
| 1929 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
| 1930 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, callback.GetResult(sock->Read( |
| 1931 buf.get(), 4096, callback.callback()))); |
| 1932 } |
| 1933 |
1907 TEST_F(SSLClientSocketTest, Read_SmallChunks) { | 1934 TEST_F(SSLClientSocketTest, Read_SmallChunks) { |
1908 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 1935 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
1909 SpawnedTestServer::kLocalhost, | 1936 SpawnedTestServer::kLocalhost, |
1910 base::FilePath()); | 1937 base::FilePath()); |
1911 ASSERT_TRUE(test_server.Start()); | 1938 ASSERT_TRUE(test_server.Start()); |
1912 | 1939 |
1913 AddressList addr; | 1940 AddressList addr; |
1914 ASSERT_TRUE(test_server.GetAddressList(&addr)); | 1941 ASSERT_TRUE(test_server.GetAddressList(&addr)); |
1915 | 1942 |
1916 TestCompletionCallback callback; | 1943 TestCompletionCallback callback; |
(...skipping 1155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3072 ssl_config.channel_id_enabled = true; | 3099 ssl_config.channel_id_enabled = true; |
3073 | 3100 |
3074 int rv; | 3101 int rv; |
3075 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | 3102 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); |
3076 | 3103 |
3077 EXPECT_EQ(ERR_UNEXPECTED, rv); | 3104 EXPECT_EQ(ERR_UNEXPECTED, rv); |
3078 EXPECT_FALSE(sock_->IsConnected()); | 3105 EXPECT_FALSE(sock_->IsConnected()); |
3079 } | 3106 } |
3080 | 3107 |
3081 } // namespace net | 3108 } // namespace net |
OLD | NEW |