| 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 <errno.h> | 7 #include <errno.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 1237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1248 // Number of bytes received on the network after the handshake should be | 1248 // Number of bytes received on the network after the handshake should be |
| 1249 // higher than the number of encrypted bytes read. | 1249 // higher than the number of encrypted bytes read. |
| 1250 EXPECT_GE(sock->GetTotalReceivedBytes() - network_bytes_read_during_handshake, | 1250 EXPECT_GE(sock->GetTotalReceivedBytes() - network_bytes_read_during_handshake, |
| 1251 unencrypted_bytes_read); | 1251 unencrypted_bytes_read); |
| 1252 | 1252 |
| 1253 // The peer should have cleanly closed the connection with a close_notify. | 1253 // The peer should have cleanly closed the connection with a close_notify. |
| 1254 EXPECT_EQ(0, rv); | 1254 EXPECT_EQ(0, rv); |
| 1255 } | 1255 } |
| 1256 | 1256 |
| 1257 // Tests that SSLClientSocket properly handles when the underlying transport | 1257 // Tests that SSLClientSocket properly handles when the underlying transport |
| 1258 // synchronously fails a transport read in during the handshake. | 1258 // synchronously fails a transport write in during the handshake. |
| 1259 TEST_F(SSLClientSocketTest, Connect_WithSynchronousError) { | 1259 TEST_F(SSLClientSocketTest, Connect_WithSynchronousError) { |
| 1260 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1260 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
| 1261 | 1261 |
| 1262 TestCompletionCallback callback; | 1262 TestCompletionCallback callback; |
| 1263 std::unique_ptr<StreamSocket> real_transport( | 1263 std::unique_ptr<StreamSocket> real_transport( |
| 1264 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1264 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
| 1265 std::unique_ptr<SynchronousErrorStreamSocket> transport( | 1265 std::unique_ptr<SynchronousErrorStreamSocket> transport( |
| 1266 new SynchronousErrorStreamSocket(std::move(real_transport))); | 1266 new SynchronousErrorStreamSocket(std::move(real_transport))); |
| 1267 int rv = callback.GetResult(transport->Connect(callback.callback())); | 1267 int rv = callback.GetResult(transport->Connect(callback.callback())); |
| 1268 EXPECT_THAT(rv, IsOk()); | 1268 EXPECT_THAT(rv, IsOk()); |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1636 // socket and detects the error. | 1636 // socket and detects the error. |
| 1637 std::string long_request_text = | 1637 std::string long_request_text = |
| 1638 "GET / HTTP/1.1\r\nUser-Agent: long browser name "; | 1638 "GET / HTTP/1.1\r\nUser-Agent: long browser name "; |
| 1639 long_request_text.append(20 * 1024, '*'); | 1639 long_request_text.append(20 * 1024, '*'); |
| 1640 long_request_text.append("\r\n\r\n"); | 1640 long_request_text.append("\r\n\r\n"); |
| 1641 scoped_refptr<DrainableIOBuffer> long_request_buffer(new DrainableIOBuffer( | 1641 scoped_refptr<DrainableIOBuffer> long_request_buffer(new DrainableIOBuffer( |
| 1642 new StringIOBuffer(long_request_text), long_request_text.size())); | 1642 new StringIOBuffer(long_request_text), long_request_text.size())); |
| 1643 | 1643 |
| 1644 raw_error_socket->SetNextWriteError(ERR_CONNECTION_RESET); | 1644 raw_error_socket->SetNextWriteError(ERR_CONNECTION_RESET); |
| 1645 | 1645 |
| 1646 // Write as much data as possible until hitting an error. This is necessary | 1646 // Write as much data as possible until hitting an error. |
| 1647 // for NSS. PR_Write will only consume as much data as it can encode into | |
| 1648 // application data records before the internal memio buffer is full, which | |
| 1649 // should only fill if writing a large amount of data and the underlying | |
| 1650 // transport is blocked. Once this happens, NSS will return (total size of all | |
| 1651 // application data records it wrote) - 1, with the caller expected to resume | |
| 1652 // with the remaining unsent data. | |
| 1653 do { | 1647 do { |
| 1654 rv = callback.GetResult(sock->Write(long_request_buffer.get(), | 1648 rv = callback.GetResult(sock->Write(long_request_buffer.get(), |
| 1655 long_request_buffer->BytesRemaining(), | 1649 long_request_buffer->BytesRemaining(), |
| 1656 callback.callback())); | 1650 callback.callback())); |
| 1657 if (rv > 0) { | 1651 if (rv > 0) { |
| 1658 long_request_buffer->DidConsume(rv); | 1652 long_request_buffer->DidConsume(rv); |
| 1659 // Abort if the entire buffer is ever consumed. | 1653 // Abort if the entire input is ever consumed. The input is larger than |
| 1654 // the SSLClientSocket's write buffers. |
| 1660 ASSERT_LT(0, long_request_buffer->BytesRemaining()); | 1655 ASSERT_LT(0, long_request_buffer->BytesRemaining()); |
| 1661 } | 1656 } |
| 1662 } while (rv > 0); | 1657 } while (rv > 0); |
| 1663 | 1658 |
| 1664 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); | 1659 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
| 1665 | 1660 |
| 1666 // Release the read. | 1661 // At this point the Read result is available. Transport write errors are |
| 1662 // surfaced through Writes. See https://crbug.com/249848. |
| 1663 rv = read_callback.WaitForResult(); |
| 1664 EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
| 1665 |
| 1666 // Release the read. This does not cause a crash. |
| 1667 raw_transport->UnblockReadResult(); | 1667 raw_transport->UnblockReadResult(); |
| 1668 rv = read_callback.WaitForResult(); | 1668 base::RunLoop().RunUntilIdle(); |
| 1669 | |
| 1670 // Should still read bytes despite the write error. | |
| 1671 EXPECT_LT(0, rv); | |
| 1672 } | 1669 } |
| 1673 | 1670 |
| 1674 // Tests that SSLClientSocket fails the handshake if the underlying | 1671 // Tests that SSLClientSocket fails the handshake if the underlying |
| 1675 // transport is cleanly closed. | 1672 // transport is cleanly closed. |
| 1676 TEST_F(SSLClientSocketTest, Connect_WithZeroReturn) { | 1673 TEST_F(SSLClientSocketTest, Connect_WithZeroReturn) { |
| 1677 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); | 1674 ASSERT_TRUE(StartTestServer(SpawnedTestServer::SSLOptions())); |
| 1678 | 1675 |
| 1679 TestCompletionCallback callback; | 1676 TestCompletionCallback callback; |
| 1680 std::unique_ptr<StreamSocket> real_transport( | 1677 std::unique_ptr<StreamSocket> real_transport( |
| 1681 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); | 1678 new TCPClientSocket(addr(), NULL, NULL, NetLogSource())); |
| (...skipping 1988 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3670 // Replace it with an alert. | 3667 // Replace it with an alert. |
| 3671 raw_transport->ReplaceReadResult( | 3668 raw_transport->ReplaceReadResult( |
| 3672 FormatTLS12Alert(49 /* AlertDescription.access_denied */)); | 3669 FormatTLS12Alert(49 /* AlertDescription.access_denied */)); |
| 3673 raw_transport->UnblockReadResult(); | 3670 raw_transport->UnblockReadResult(); |
| 3674 | 3671 |
| 3675 rv = callback.GetResult(rv); | 3672 rv = callback.GetResult(rv); |
| 3676 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT)); | 3673 EXPECT_THAT(rv, IsError(ERR_BAD_SSL_CLIENT_AUTH_CERT)); |
| 3677 } | 3674 } |
| 3678 | 3675 |
| 3679 } // namespace net | 3676 } // namespace net |
| OLD | NEW |