| 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 "net/base/address_list.h" | 9 #include "net/base/address_list.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| (...skipping 1153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1164 EXPECT_EQ(ERR_CONNECTION_RESET, rv); | 1164 EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
| 1165 #else | 1165 #else |
| 1166 // OpenSSL treats any errors as a simple EOF. | 1166 // OpenSSL treats any errors as a simple EOF. |
| 1167 EXPECT_EQ(0, rv); | 1167 EXPECT_EQ(0, rv); |
| 1168 #endif | 1168 #endif |
| 1169 | 1169 |
| 1170 // The Write callback should not have been called. | 1170 // The Write callback should not have been called. |
| 1171 EXPECT_FALSE(callback.have_result()); | 1171 EXPECT_FALSE(callback.have_result()); |
| 1172 } | 1172 } |
| 1173 | 1173 |
| 1174 // Tests that the SSLClientSocket does not crash if data is received on the |
| 1175 // transport socket after a failing write. This can occur if we have a Write |
| 1176 // error in a SPDY socket. |
| 1177 // Regression test for http://crbug.com/335557 |
| 1178 TEST_F(SSLClientSocketTest, Read_WithWriteError) { |
| 1179 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
| 1180 SpawnedTestServer::kLocalhost, |
| 1181 base::FilePath()); |
| 1182 ASSERT_TRUE(test_server.Start()); |
| 1183 |
| 1184 AddressList addr; |
| 1185 ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 1186 |
| 1187 TestCompletionCallback callback; |
| 1188 scoped_ptr<StreamSocket> real_transport( |
| 1189 new TCPClientSocket(addr, NULL, NetLog::Source())); |
| 1190 // Note: |error_socket|'s ownership is handed to |transport|, but a pointer |
| 1191 // is retained in order to configure additional errors. |
| 1192 scoped_ptr<SynchronousErrorStreamSocket> error_socket( |
| 1193 new SynchronousErrorStreamSocket(real_transport.Pass())); |
| 1194 SynchronousErrorStreamSocket* raw_error_socket = error_socket.get(); |
| 1195 scoped_ptr<FakeBlockingStreamSocket> transport( |
| 1196 new FakeBlockingStreamSocket(error_socket.PassAs<StreamSocket>())); |
| 1197 FakeBlockingStreamSocket* raw_transport = transport.get(); |
| 1198 |
| 1199 int rv = callback.GetResult(transport->Connect(callback.callback())); |
| 1200 EXPECT_EQ(OK, rv); |
| 1201 |
| 1202 // Disable TLS False Start to avoid handshake non-determinism. |
| 1203 SSLConfig ssl_config; |
| 1204 ssl_config.false_start_enabled = false; |
| 1205 |
| 1206 scoped_ptr<SSLClientSocket> sock( |
| 1207 CreateSSLClientSocket(transport.PassAs<StreamSocket>(), |
| 1208 test_server.host_port_pair(), |
| 1209 ssl_config)); |
| 1210 |
| 1211 rv = callback.GetResult(sock->Connect(callback.callback())); |
| 1212 EXPECT_EQ(OK, rv); |
| 1213 EXPECT_TRUE(sock->IsConnected()); |
| 1214 |
| 1215 // Send a request so there is something to read from the socket. |
| 1216 const char request_text[] = "GET / HTTP/1.0\r\n\r\n"; |
| 1217 static const int kRequestTextSize = |
| 1218 static_cast<int>(arraysize(request_text) - 1); |
| 1219 scoped_refptr<IOBuffer> request_buffer(new IOBuffer(kRequestTextSize)); |
| 1220 memcpy(request_buffer->data(), request_text, kRequestTextSize); |
| 1221 |
| 1222 rv = callback.GetResult( |
| 1223 sock->Write(request_buffer.get(), kRequestTextSize, callback.callback())); |
| 1224 EXPECT_EQ(kRequestTextSize, rv); |
| 1225 |
| 1226 // Start a hanging read. |
| 1227 TestCompletionCallback read_callback; |
| 1228 raw_transport->SetNextReadShouldBlock(); |
| 1229 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
| 1230 rv = sock->Read(buf.get(), 4096, read_callback.callback()); |
| 1231 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1232 |
| 1233 // Perform another write, but have it fail. Write a request larger than the |
| 1234 // internal socket buffers so that the request hits the underlying transport |
| 1235 // socket and detects the error. |
| 1236 std::string long_request_text = |
| 1237 "GET / HTTP/1.1\r\nUser-Agent: long browser name "; |
| 1238 long_request_text.append(20 * 1024, '*'); |
| 1239 long_request_text.append("\r\n\r\n"); |
| 1240 scoped_refptr<DrainableIOBuffer> long_request_buffer(new DrainableIOBuffer( |
| 1241 new StringIOBuffer(long_request_text), long_request_text.size())); |
| 1242 |
| 1243 raw_error_socket->SetNextWriteError(ERR_CONNECTION_RESET); |
| 1244 |
| 1245 // Write as much data as possible until hitting an error. This is necessary |
| 1246 // for NSS. PR_Write will only consume as much data as it can encode into |
| 1247 // application data records before the internal memio buffer is full, which |
| 1248 // should only fill if writing a large amount of data and the underlying |
| 1249 // transport is blocked. Once this happens, NSS will return (total size of all |
| 1250 // application data records it wrote) - 1, with the caller expected to resume |
| 1251 // with the remaining unsent data. |
| 1252 do { |
| 1253 rv = callback.GetResult(sock->Write(long_request_buffer.get(), |
| 1254 long_request_buffer->BytesRemaining(), |
| 1255 callback.callback())); |
| 1256 if (rv > 0) { |
| 1257 long_request_buffer->DidConsume(rv); |
| 1258 // Abort if the entire buffer is ever consumed. |
| 1259 ASSERT_LT(0, long_request_buffer->BytesRemaining()); |
| 1260 } |
| 1261 } while (rv > 0); |
| 1262 |
| 1263 #if !defined(USE_OPENSSL) |
| 1264 // NSS records the error exactly. |
| 1265 EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
| 1266 #else |
| 1267 // OpenSSL treats the reset as a generic protocol error. |
| 1268 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, rv); |
| 1269 #endif |
| 1270 |
| 1271 // Release the read. Some bytes should go through. |
| 1272 raw_transport->UnblockRead(); |
| 1273 rv = read_callback.WaitForResult(); |
| 1274 |
| 1275 // Per the fix for http://crbug.com/249848, write failures currently break |
| 1276 // reads. Change this assertion if they're changed to not collide. |
| 1277 EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
| 1278 } |
| 1279 |
| 1174 TEST_F(SSLClientSocketTest, Read_SmallChunks) { | 1280 TEST_F(SSLClientSocketTest, Read_SmallChunks) { |
| 1175 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, | 1281 SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTPS, |
| 1176 SpawnedTestServer::kLocalhost, | 1282 SpawnedTestServer::kLocalhost, |
| 1177 base::FilePath()); | 1283 base::FilePath()); |
| 1178 ASSERT_TRUE(test_server.Start()); | 1284 ASSERT_TRUE(test_server.Start()); |
| 1179 | 1285 |
| 1180 AddressList addr; | 1286 AddressList addr; |
| 1181 ASSERT_TRUE(test_server.GetAddressList(&addr)); | 1287 ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 1182 | 1288 |
| 1183 TestCompletionCallback callback; | 1289 TestCompletionCallback callback; |
| (...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1947 log.GetEntries(&entries); | 2053 log.GetEntries(&entries); |
| 1948 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); | 2054 EXPECT_TRUE(LogContainsSSLConnectEndEvent(entries, -1)); |
| 1949 | 2055 |
| 1950 EXPECT_FALSE(sock->signed_cert_timestamps_received_); | 2056 EXPECT_FALSE(sock->signed_cert_timestamps_received_); |
| 1951 | 2057 |
| 1952 sock->Disconnect(); | 2058 sock->Disconnect(); |
| 1953 EXPECT_FALSE(sock->IsConnected()); | 2059 EXPECT_FALSE(sock->IsConnected()); |
| 1954 } | 2060 } |
| 1955 | 2061 |
| 1956 } // namespace net | 2062 } // namespace net |
| OLD | NEW |