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 "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
(...skipping 1118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1129 | 1129 |
1130 if (rv == ERR_IO_PENDING) | 1130 if (rv == ERR_IO_PENDING) |
1131 rv = callback.WaitForResult(); | 1131 rv = callback.WaitForResult(); |
1132 | 1132 |
1133 EXPECT_GE(rv, 0); | 1133 EXPECT_GE(rv, 0); |
1134 if (rv <= 0) | 1134 if (rv <= 0) |
1135 break; | 1135 break; |
1136 } | 1136 } |
1137 } | 1137 } |
1138 | 1138 |
1139 TEST_F(SSLClientSocketTest, Connect_WithSynchronousError) { | |
1140 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | |
1141 SpawnedTestServer::kLocalhost, | |
1142 base::FilePath()); | |
1143 ASSERT_TRUE(test_server.Start()); | |
1144 | |
1145 AddressList addr; | |
1146 ASSERT_TRUE(test_server.GetAddressList(&addr)); | |
1147 | |
1148 TestCompletionCallback callback; | |
1149 scoped_ptr<StreamSocket> real_transport( | |
1150 new TCPClientSocket(addr, NULL, NetLog::Source())); | |
1151 scoped_ptr<SynchronousErrorStreamSocket> transport( | |
1152 new SynchronousErrorStreamSocket(real_transport.Pass())); | |
1153 int rv = callback.GetResult(transport->Connect(callback.callback())); | |
1154 EXPECT_EQ(OK, rv); | |
1155 | |
1156 // Disable TLS False Start to avoid handshake non-determinism. | |
1157 SSLConfig ssl_config; | |
1158 ssl_config.false_start_enabled = false; | |
1159 | |
1160 SynchronousErrorStreamSocket* raw_transport = transport.get(); | |
1161 scoped_ptr<SSLClientSocket> sock( | |
1162 CreateSSLClientSocket(transport.PassAs<StreamSocket>(), | |
1163 test_server.host_port_pair(), | |
1164 ssl_config)); | |
1165 | |
1166 raw_transport->SetNextWriteError(ERR_CONNECTION_RESET); | |
1167 | |
1168 rv = callback.GetResult(sock->Connect(callback.callback())); | |
1169 EXPECT_EQ(ERR_CONNECTION_RESET, rv); | |
1170 EXPECT_FALSE(sock->IsConnected()); | |
1171 } | |
1172 | |
1139 // Tests that the SSLClientSocket properly handles when the underlying transport | 1173 // Tests that the SSLClientSocket properly handles when the underlying transport |
1140 // synchronously returns an error code - such as if an intermediary terminates | 1174 // synchronously returns an error code - such as if an intermediary terminates |
1141 // the socket connection uncleanly. | 1175 // the socket connection uncleanly. |
1142 // This is a regression test for http://crbug.com/238536 | 1176 // This is a regression test for http://crbug.com/238536 |
1143 TEST_F(SSLClientSocketTest, Read_WithSynchronousError) { | 1177 TEST_F(SSLClientSocketTest, Read_WithSynchronousError) { |
1144 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 1178 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
1145 SpawnedTestServer::kLocalhost, | 1179 SpawnedTestServer::kLocalhost, |
1146 base::FilePath()); | 1180 base::FilePath()); |
1147 ASSERT_TRUE(test_server.Start()); | 1181 ASSERT_TRUE(test_server.Start()); |
1148 | 1182 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1184 // Simulate an unclean/forcible shutdown. | 1218 // Simulate an unclean/forcible shutdown. |
1185 raw_transport->SetNextReadError(ERR_CONNECTION_RESET); | 1219 raw_transport->SetNextReadError(ERR_CONNECTION_RESET); |
1186 | 1220 |
1187 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 1221 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
1188 | 1222 |
1189 // Note: This test will hang if this bug has regressed. Simply checking that | 1223 // Note: This test will hang if this bug has regressed. Simply checking that |
1190 // rv != ERR_IO_PENDING is insufficient, as ERR_IO_PENDING is a legitimate | 1224 // rv != ERR_IO_PENDING is insufficient, as ERR_IO_PENDING is a legitimate |
1191 // result when using a dedicated task runner for NSS. | 1225 // result when using a dedicated task runner for NSS. |
1192 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); | 1226 rv = callback.GetResult(sock->Read(buf.get(), 4096, callback.callback())); |
1193 | 1227 |
1194 #if !defined(USE_OPENSSL) | |
1195 // SSLClientSocketNSS records the error exactly | |
1196 EXPECT_EQ(ERR_CONNECTION_RESET, rv); | 1228 EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
1197 #else | |
1198 // SSLClientSocketOpenSSL treats any errors as a simple EOF. | |
1199 EXPECT_EQ(0, rv); | |
1200 #endif | |
1201 } | 1229 } |
1202 | 1230 |
1203 // Tests that the SSLClientSocket properly handles when the underlying transport | 1231 // Tests that the SSLClientSocket properly handles when the underlying transport |
1204 // asynchronously returns an error code while writing data - such as if an | 1232 // asynchronously returns an error code while writing data - such as if an |
1205 // intermediary terminates the socket connection uncleanly. | 1233 // intermediary terminates the socket connection uncleanly. |
1206 // This is a regression test for http://crbug.com/249848 | 1234 // This is a regression test for http://crbug.com/249848 |
1207 TEST_F(SSLClientSocketTest, Write_WithSynchronousError) { | 1235 TEST_F(SSLClientSocketTest, Write_WithSynchronousError) { |
1208 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 1236 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
1209 SpawnedTestServer::kLocalhost, | 1237 SpawnedTestServer::kLocalhost, |
1210 base::FilePath()); | 1238 base::FilePath()); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1265 | 1293 |
1266 // Now unblock the outgoing request, having it fail with the connection | 1294 // Now unblock the outgoing request, having it fail with the connection |
1267 // being reset. | 1295 // being reset. |
1268 raw_transport->UnblockWrite(); | 1296 raw_transport->UnblockWrite(); |
1269 | 1297 |
1270 // Note: This will cause an inifite loop if this bug has regressed. Simply | 1298 // Note: This will cause an inifite loop if this bug has regressed. Simply |
1271 // checking that rv != ERR_IO_PENDING is insufficient, as ERR_IO_PENDING | 1299 // checking that rv != ERR_IO_PENDING is insufficient, as ERR_IO_PENDING |
1272 // is a legitimate result when using a dedicated task runner for NSS. | 1300 // is a legitimate result when using a dedicated task runner for NSS. |
1273 rv = callback.GetResult(rv); | 1301 rv = callback.GetResult(rv); |
1274 | 1302 |
1275 #if !defined(USE_OPENSSL) | |
1276 // SSLClientSocketNSS records the error exactly | |
1277 EXPECT_EQ(ERR_CONNECTION_RESET, rv); | 1303 EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
1278 #else | |
1279 // SSLClientSocketOpenSSL treats any errors as a simple EOF. | |
1280 EXPECT_EQ(0, rv); | |
1281 #endif | |
1282 } | 1304 } |
1283 | 1305 |
1284 // Test the full duplex mode, with Read and Write pending at the same time. | 1306 // Test the full duplex mode, with Read and Write pending at the same time. |
1285 // This test also serves as a regression test for http://crbug.com/29815. | 1307 // This test also serves as a regression test for http://crbug.com/29815. |
1286 TEST_F(SSLClientSocketTest, Read_FullDuplex) { | 1308 TEST_F(SSLClientSocketTest, Read_FullDuplex) { |
1287 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 1309 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
1288 SpawnedTestServer::kLocalhost, | 1310 SpawnedTestServer::kLocalhost, |
1289 base::FilePath()); | 1311 base::FilePath()); |
1290 ASSERT_TRUE(test_server.Start()); | 1312 ASSERT_TRUE(test_server.Start()); |
1291 | 1313 |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1446 ASSERT_EQ(ERR_IO_PENDING, rv); | 1468 ASSERT_EQ(ERR_IO_PENDING, rv); |
1447 ASSERT_FALSE(callback.have_result()); | 1469 ASSERT_FALSE(callback.have_result()); |
1448 | 1470 |
1449 // Now unblock Write(), which will invoke OnSendComplete and (eventually) | 1471 // Now unblock Write(), which will invoke OnSendComplete and (eventually) |
1450 // call the Read() callback, deleting the socket and thus aborting calling | 1472 // call the Read() callback, deleting the socket and thus aborting calling |
1451 // the Write() callback. | 1473 // the Write() callback. |
1452 raw_transport->UnblockWrite(); | 1474 raw_transport->UnblockWrite(); |
1453 | 1475 |
1454 rv = read_callback.WaitForResult(); | 1476 rv = read_callback.WaitForResult(); |
1455 | 1477 |
1456 #if !defined(USE_OPENSSL) | |
1457 // NSS records the error exactly. | |
1458 EXPECT_EQ(ERR_CONNECTION_RESET, rv); | 1478 EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
1459 #else | |
1460 // OpenSSL treats any errors as a simple EOF. | |
1461 EXPECT_EQ(0, rv); | |
1462 #endif | |
1463 | 1479 |
1464 // The Write callback should not have been called. | 1480 // The Write callback should not have been called. |
1465 EXPECT_FALSE(callback.have_result()); | 1481 EXPECT_FALSE(callback.have_result()); |
1466 } | 1482 } |
1467 | 1483 |
1468 // Tests that the SSLClientSocket does not crash if data is received on the | 1484 // Tests that the SSLClientSocket does not crash if data is received on the |
1469 // transport socket after a failing write. This can occur if we have a Write | 1485 // transport socket after a failing write. This can occur if we have a Write |
1470 // error in a SPDY socket. | 1486 // error in a SPDY socket. |
1471 // Regression test for http://crbug.com/335557 | 1487 // Regression test for http://crbug.com/335557 |
1472 TEST_F(SSLClientSocketTest, Read_WithWriteError) { | 1488 TEST_F(SSLClientSocketTest, Read_WithWriteError) { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1547 rv = callback.GetResult(sock->Write(long_request_buffer.get(), | 1563 rv = callback.GetResult(sock->Write(long_request_buffer.get(), |
1548 long_request_buffer->BytesRemaining(), | 1564 long_request_buffer->BytesRemaining(), |
1549 callback.callback())); | 1565 callback.callback())); |
1550 if (rv > 0) { | 1566 if (rv > 0) { |
1551 long_request_buffer->DidConsume(rv); | 1567 long_request_buffer->DidConsume(rv); |
1552 // Abort if the entire buffer is ever consumed. | 1568 // Abort if the entire buffer is ever consumed. |
1553 ASSERT_LT(0, long_request_buffer->BytesRemaining()); | 1569 ASSERT_LT(0, long_request_buffer->BytesRemaining()); |
1554 } | 1570 } |
1555 } while (rv > 0); | 1571 } while (rv > 0); |
1556 | 1572 |
1557 #if !defined(USE_OPENSSL) | |
1558 // NSS records the error exactly. | |
1559 EXPECT_EQ(ERR_CONNECTION_RESET, rv); | 1573 EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
1560 #else | |
1561 // OpenSSL treats the reset as a generic protocol error. | |
1562 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, rv); | |
1563 #endif | |
1564 | 1574 |
1565 // Release the read. Some bytes should go through. | 1575 // Release the read. |
1566 raw_transport->UnblockReadResult(); | 1576 raw_transport->UnblockReadResult(); |
1567 rv = read_callback.WaitForResult(); | 1577 rv = read_callback.WaitForResult(); |
1568 | 1578 |
1569 // Per the fix for http://crbug.com/249848, write failures currently break | 1579 #if defined(USE_OPENSSL) |
1570 // reads. Change this assertion if they're changed to not collide. | 1580 // Should still read bytes despite the write error. |
1581 EXPECT_LT(0, rv); | |
1582 #else | |
1583 // NSS attempts to flush the write buffer in PR_Read on an SSL socket before | |
1584 // pumping the read state machine, unless configured with SSL_ENABLE_FDX, so | |
1585 // the write error bleeds into the read. | |
davidben
2014/06/06 23:31:51
This comment is referring to this block:
https://
| |
1571 EXPECT_EQ(ERR_CONNECTION_RESET, rv); | 1586 EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
1587 #endif | |
1572 } | 1588 } |
1573 | 1589 |
1574 TEST_F(SSLClientSocketTest, Read_SmallChunks) { | 1590 TEST_F(SSLClientSocketTest, Read_SmallChunks) { |
1575 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 1591 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
1576 SpawnedTestServer::kLocalhost, | 1592 SpawnedTestServer::kLocalhost, |
1577 base::FilePath()); | 1593 base::FilePath()); |
1578 ASSERT_TRUE(test_server.Start()); | 1594 ASSERT_TRUE(test_server.Start()); |
1579 | 1595 |
1580 AddressList addr; | 1596 AddressList addr; |
1581 ASSERT_TRUE(test_server.GetAddressList(&addr)); | 1597 ASSERT_TRUE(test_server.GetAddressList(&addr)); |
(...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2519 | 2535 |
2520 // TODO(haavardm@opera.com): Due to differences in threading, Linux returns | 2536 // TODO(haavardm@opera.com): Due to differences in threading, Linux returns |
2521 // ERR_UNEXPECTED while Mac and Windows return ERR_PROTOCOL_ERROR. Accept all | 2537 // ERR_UNEXPECTED while Mac and Windows return ERR_PROTOCOL_ERROR. Accept all |
2522 // error codes for now. | 2538 // error codes for now. |
2523 // http://crbug.com/373670 | 2539 // http://crbug.com/373670 |
2524 EXPECT_NE(OK, rv); | 2540 EXPECT_NE(OK, rv); |
2525 EXPECT_FALSE(sock_->IsConnected()); | 2541 EXPECT_FALSE(sock_->IsConnected()); |
2526 } | 2542 } |
2527 | 2543 |
2528 } // namespace net | 2544 } // namespace net |
OLD | NEW |