| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/socket_bio_adapter.h" | 5 #include "net/socket/socket_bio_adapter.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/test/scoped_feature_list.h" |
| 16 #include "crypto/openssl_util.h" | 17 #include "crypto/openssl_util.h" |
| 17 #include "net/base/address_list.h" | 18 #include "net/base/address_list.h" |
| 18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 19 #include "net/log/net_log_source.h" | 20 #include "net/log/net_log_source.h" |
| 20 #include "net/socket/socket_test_util.h" | 21 #include "net/socket/socket_test_util.h" |
| 21 #include "net/socket/stream_socket.h" | 22 #include "net/socket/stream_socket.h" |
| 22 #include "net/ssl/openssl_ssl_util.h" | 23 #include "net/ssl/openssl_ssl_util.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "third_party/boringssl/src/include/openssl/bio.h" | 25 #include "third_party/boringssl/src/include/openssl/bio.h" |
| 25 #include "third_party/boringssl/src/include/openssl/err.h" | 26 #include "third_party/boringssl/src/include/openssl/err.h" |
| 26 #include "third_party/boringssl/src/include/openssl/ssl.h" | 27 #include "third_party/boringssl/src/include/openssl/ssl.h" |
| 27 | 28 |
| 28 namespace net { | 29 namespace net { |
| 29 | 30 |
| 30 class SocketBIOAdapterTest : public testing::Test, | 31 enum ReadIfReadySupport { |
| 32 // ReadIfReady() field trial is enabled, and ReadyIfReady() is implemented. |
| 33 READ_IF_READY_ENABLED_SUPPORTED, |
| 34 // ReadIfReady() field trial is enabled, but ReadyIfReady() is unimplemented. |
| 35 READ_IF_READY_ENABLED_NOT_SUPPORTED, |
| 36 // ReadIfReady() field trial is disabled. |
| 37 READ_IF_READY_DISABLED, |
| 38 }; |
| 39 |
| 40 class SocketBIOAdapterTest : public testing::TestWithParam<ReadIfReadySupport>, |
| 31 public SocketBIOAdapter::Delegate { | 41 public SocketBIOAdapter::Delegate { |
| 32 protected: | 42 protected: |
| 43 void SetUp() override { |
| 44 if (GetParam() != READ_IF_READY_DISABLED) |
| 45 scoped_feature_list_.InitAndEnableFeature(Socket::kReadIfReadyExperiment); |
| 46 if (GetParam() == READ_IF_READY_ENABLED_SUPPORTED) |
| 47 factory_.set_enable_read_if_ready(true); |
| 48 } |
| 49 |
| 33 std::unique_ptr<StreamSocket> MakeTestSocket(SocketDataProvider* data) { | 50 std::unique_ptr<StreamSocket> MakeTestSocket(SocketDataProvider* data) { |
| 34 data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); | 51 data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
| 35 factory_.AddSocketDataProvider(data); | 52 factory_.AddSocketDataProvider(data); |
| 36 std::unique_ptr<StreamSocket> socket = factory_.CreateTransportClientSocket( | 53 std::unique_ptr<StreamSocket> socket = factory_.CreateTransportClientSocket( |
| 37 AddressList(), nullptr, nullptr, NetLogSource()); | 54 AddressList(), nullptr, nullptr, NetLogSource()); |
| 38 CHECK_EQ(OK, socket->Connect(net::CompletionCallback())); | 55 CHECK_EQ(OK, socket->Connect(net::CompletionCallback())); |
| 39 return socket; | 56 return socket; |
| 40 } | 57 } |
| 41 | 58 |
| 42 void set_reset_on_write_ready( | 59 void set_reset_on_write_ready( |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 expect_write_ready_ = false; | 149 expect_write_ready_ = false; |
| 133 if (reset_on_write_ready_) | 150 if (reset_on_write_ready_) |
| 134 reset_on_write_ready_->reset(); | 151 reset_on_write_ready_->reset(); |
| 135 } | 152 } |
| 136 | 153 |
| 137 private: | 154 private: |
| 138 bool expect_read_ready_ = false; | 155 bool expect_read_ready_ = false; |
| 139 bool expect_write_ready_ = false; | 156 bool expect_write_ready_ = false; |
| 140 MockClientSocketFactory factory_; | 157 MockClientSocketFactory factory_; |
| 141 std::unique_ptr<SocketBIOAdapter>* reset_on_write_ready_ = nullptr; | 158 std::unique_ptr<SocketBIOAdapter>* reset_on_write_ready_ = nullptr; |
| 159 base::test::ScopedFeatureList scoped_feature_list_; |
| 142 }; | 160 }; |
| 143 | 161 |
| 162 INSTANTIATE_TEST_CASE_P(/* no prefix */, |
| 163 SocketBIOAdapterTest, |
| 164 testing::Values(READ_IF_READY_ENABLED_SUPPORTED, |
| 165 READ_IF_READY_ENABLED_NOT_SUPPORTED, |
| 166 READ_IF_READY_DISABLED)); |
| 167 |
| 144 // Test that data can be read synchronously. | 168 // Test that data can be read synchronously. |
| 145 TEST_F(SocketBIOAdapterTest, ReadSync) { | 169 TEST_P(SocketBIOAdapterTest, ReadSync) { |
| 146 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); | 170 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 147 | 171 |
| 148 MockRead reads[] = { | 172 MockRead reads[] = { |
| 149 MockRead(SYNCHRONOUS, 0, "hello"), MockRead(SYNCHRONOUS, 1, "world"), | 173 MockRead(SYNCHRONOUS, 0, "hello"), MockRead(SYNCHRONOUS, 1, "world"), |
| 150 MockRead(SYNCHRONOUS, ERR_CONNECTION_RESET, 2), | 174 MockRead(SYNCHRONOUS, ERR_CONNECTION_RESET, 2), |
| 151 }; | 175 }; |
| 152 | 176 |
| 153 SequencedSocketData data(reads, arraysize(reads), nullptr, 0); | 177 SequencedSocketData data(reads, arraysize(reads), nullptr, 0); |
| 154 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); | 178 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 155 std::unique_ptr<SocketBIOAdapter> adapter = | 179 std::unique_ptr<SocketBIOAdapter> adapter = |
| (...skipping 20 matching lines...) Expand all Loading... |
| 176 // The remainder may be consumed in a single BIO_read. | 200 // The remainder may be consumed in a single BIO_read. |
| 177 EXPECT_EQ(3, BIO_read(bio, buf, sizeof(buf))); | 201 EXPECT_EQ(3, BIO_read(bio, buf, sizeof(buf))); |
| 178 EXPECT_EQ(0, memcmp("rld", buf, 3)); | 202 EXPECT_EQ(0, memcmp("rld", buf, 3)); |
| 179 EXPECT_FALSE(adapter->HasPendingReadData()); | 203 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 180 | 204 |
| 181 // The error is available synchoronously. | 205 // The error is available synchoronously. |
| 182 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); | 206 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); |
| 183 } | 207 } |
| 184 | 208 |
| 185 // Test that data can be read asynchronously. | 209 // Test that data can be read asynchronously. |
| 186 TEST_F(SocketBIOAdapterTest, ReadAsync) { | 210 TEST_P(SocketBIOAdapterTest, ReadAsync) { |
| 187 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); | 211 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 188 | 212 |
| 189 MockRead reads[] = { | 213 MockRead reads[] = { |
| 190 MockRead(ASYNC, 0, "hello"), MockRead(ASYNC, 1, "world"), | 214 MockRead(ASYNC, 0, "hello"), MockRead(ASYNC, 1, "world"), |
| 191 MockRead(ASYNC, ERR_CONNECTION_RESET, 2), | 215 MockRead(ASYNC, ERR_CONNECTION_RESET, 2), |
| 192 }; | 216 }; |
| 193 | 217 |
| 194 SequencedSocketData data(reads, arraysize(reads), nullptr, 0); | 218 SequencedSocketData data(reads, arraysize(reads), nullptr, 0); |
| 195 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); | 219 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 196 std::unique_ptr<SocketBIOAdapter> adapter = | 220 std::unique_ptr<SocketBIOAdapter> adapter = |
| 197 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); | 221 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); |
| 198 BIO* bio = adapter->bio(); | 222 BIO* bio = adapter->bio(); |
| 199 EXPECT_FALSE(adapter->HasPendingReadData()); | 223 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 200 | 224 |
| 201 // Attempt to read data. It will fail but schedule a Read. | 225 // Attempt to read data. It will fail but schedule a Read. |
| 202 char buf[10]; | 226 char buf[10]; |
| 203 ExpectBlockingRead(bio, buf, sizeof(buf)); | 227 ExpectBlockingRead(bio, buf, sizeof(buf)); |
| 204 EXPECT_FALSE(adapter->HasPendingReadData()); | 228 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 205 | 229 |
| 206 // After waiting, the data is available. | 230 // After waiting, the data is available if Read() is used. |
| 207 WaitForReadReady(); | 231 WaitForReadReady(); |
| 208 EXPECT_TRUE(adapter->HasPendingReadData()); | 232 if (GetParam() == READ_IF_READY_ENABLED_SUPPORTED) { |
| 233 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 234 } else { |
| 235 EXPECT_TRUE(adapter->HasPendingReadData()); |
| 236 } |
| 209 | 237 |
| 210 // The first read is now available synchronously. | 238 // The first read is now available synchronously. |
| 211 EXPECT_EQ(5, BIO_read(bio, buf, sizeof(buf))); | 239 EXPECT_EQ(5, BIO_read(bio, buf, sizeof(buf))); |
| 212 EXPECT_EQ(0, memcmp("hello", buf, 5)); | 240 EXPECT_EQ(0, memcmp("hello", buf, 5)); |
| 213 EXPECT_FALSE(adapter->HasPendingReadData()); | 241 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 214 | 242 |
| 215 // The adapter does not schedule another Read until BIO_read is next called. | 243 // The adapter does not schedule another Read until BIO_read is next called. |
| 216 base::RunLoop().RunUntilIdle(); | 244 base::RunLoop().RunUntilIdle(); |
| 217 EXPECT_FALSE(adapter->HasPendingReadData()); | 245 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 218 | 246 |
| 219 // This time, under-request the data. The adapter should still read the full | 247 // This time, under-request the data. The adapter should still read the full |
| 220 // amount. | 248 // amount. |
| 221 ExpectBlockingRead(bio, buf, 1); | 249 ExpectBlockingRead(bio, buf, 1); |
| 222 EXPECT_FALSE(adapter->HasPendingReadData()); | 250 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 251 |
| 252 // After waiting, the data is available if Read() is used. |
| 223 WaitForReadReady(); | 253 WaitForReadReady(); |
| 224 EXPECT_TRUE(adapter->HasPendingReadData()); | 254 if (GetParam() == READ_IF_READY_ENABLED_SUPPORTED) { |
| 255 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 256 } else { |
| 257 EXPECT_TRUE(adapter->HasPendingReadData()); |
| 258 } |
| 225 | 259 |
| 226 // The next read is now available synchronously. | 260 // The next read is now available synchronously. |
| 227 EXPECT_EQ(5, BIO_read(bio, buf, sizeof(buf))); | 261 EXPECT_EQ(5, BIO_read(bio, buf, sizeof(buf))); |
| 228 EXPECT_EQ(0, memcmp("world", buf, 5)); | 262 EXPECT_EQ(0, memcmp("world", buf, 5)); |
| 229 EXPECT_FALSE(adapter->HasPendingReadData()); | 263 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 230 | 264 |
| 231 // The error is not yet available. | 265 // The error is not yet available. |
| 232 ExpectBlockingRead(bio, buf, sizeof(buf)); | 266 ExpectBlockingRead(bio, buf, sizeof(buf)); |
| 233 WaitForReadReady(); | 267 WaitForReadReady(); |
| 234 | 268 |
| 235 // The error is now available synchoronously. | 269 // The error is now available synchoronously. |
| 236 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); | 270 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); |
| 237 } | 271 } |
| 238 | 272 |
| 239 // Test that synchronous EOF is mapped to ERR_CONNECTION_CLOSED. | 273 // Test that synchronous EOF is mapped to ERR_CONNECTION_CLOSED. |
| 240 TEST_F(SocketBIOAdapterTest, ReadEOFSync) { | 274 TEST_P(SocketBIOAdapterTest, ReadEOFSync) { |
| 241 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); | 275 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 242 | 276 |
| 243 MockRead reads[] = { | 277 MockRead reads[] = { |
| 244 MockRead(SYNCHRONOUS, 0, 0), | 278 MockRead(SYNCHRONOUS, 0, 0), |
| 245 }; | 279 }; |
| 246 | 280 |
| 247 SequencedSocketData data(reads, arraysize(reads), nullptr, 0); | 281 SequencedSocketData data(reads, arraysize(reads), nullptr, 0); |
| 248 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); | 282 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 249 std::unique_ptr<SocketBIOAdapter> adapter = | 283 std::unique_ptr<SocketBIOAdapter> adapter = |
| 250 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); | 284 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); |
| 251 | 285 |
| 252 ExpectReadError(adapter->bio(), ERR_CONNECTION_CLOSED, tracer); | 286 ExpectReadError(adapter->bio(), ERR_CONNECTION_CLOSED, tracer); |
| 253 } | 287 } |
| 254 | 288 |
| 255 // Test that asynchronous EOF is mapped to ERR_CONNECTION_CLOSED. | 289 // Test that asynchronous EOF is mapped to ERR_CONNECTION_CLOSED. |
| 256 TEST_F(SocketBIOAdapterTest, ReadEOFAsync) { | 290 TEST_P(SocketBIOAdapterTest, ReadEOFAsync) { |
| 257 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); | 291 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 258 | 292 |
| 259 MockRead reads[] = { | 293 MockRead reads[] = { |
| 260 MockRead(ASYNC, 0, 0), | 294 MockRead(ASYNC, 0, 0), |
| 261 }; | 295 }; |
| 262 | 296 |
| 263 SequencedSocketData data(reads, arraysize(reads), nullptr, 0); | 297 SequencedSocketData data(reads, arraysize(reads), nullptr, 0); |
| 264 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); | 298 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 265 std::unique_ptr<SocketBIOAdapter> adapter = | 299 std::unique_ptr<SocketBIOAdapter> adapter = |
| 266 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); | 300 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); |
| 267 | 301 |
| 268 char buf; | 302 char buf; |
| 269 ExpectBlockingRead(adapter->bio(), &buf, 1); | 303 ExpectBlockingRead(adapter->bio(), &buf, 1); |
| 270 WaitForReadReady(); | 304 WaitForReadReady(); |
| 271 ExpectReadError(adapter->bio(), ERR_CONNECTION_CLOSED, tracer); | 305 ExpectReadError(adapter->bio(), ERR_CONNECTION_CLOSED, tracer); |
| 272 } | 306 } |
| 273 | 307 |
| 274 // Test that data can be written synchronously. | 308 // Test that data can be written synchronously. |
| 275 TEST_F(SocketBIOAdapterTest, WriteSync) { | 309 TEST_P(SocketBIOAdapterTest, WriteSync) { |
| 276 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); | 310 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 277 | 311 |
| 278 MockWrite writes[] = { | 312 MockWrite writes[] = { |
| 279 MockWrite(SYNCHRONOUS, 0, "hello"), | 313 MockWrite(SYNCHRONOUS, 0, "hello"), |
| 280 MockWrite(SYNCHRONOUS, 1, "wor"), | 314 MockWrite(SYNCHRONOUS, 1, "wor"), |
| 281 MockWrite(SYNCHRONOUS, 2, "ld"), | 315 MockWrite(SYNCHRONOUS, 2, "ld"), |
| 282 MockWrite(SYNCHRONOUS, 3, "helloworld"), | 316 MockWrite(SYNCHRONOUS, 3, "helloworld"), |
| 283 MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET, 4), | 317 MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET, 4), |
| 284 }; | 318 }; |
| 285 | 319 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 300 | 334 |
| 301 // Writing "aaaaa" fails (event 4), but there is a write buffer, so errors | 335 // Writing "aaaaa" fails (event 4), but there is a write buffer, so errors |
| 302 // are delayed. | 336 // are delayed. |
| 303 EXPECT_EQ(5, BIO_write(bio, "aaaaa", 5)); | 337 EXPECT_EQ(5, BIO_write(bio, "aaaaa", 5)); |
| 304 | 338 |
| 305 // However once the error is registered, subsequent writes fail. | 339 // However once the error is registered, subsequent writes fail. |
| 306 ExpectWriteError(bio, ERR_CONNECTION_RESET, tracer); | 340 ExpectWriteError(bio, ERR_CONNECTION_RESET, tracer); |
| 307 } | 341 } |
| 308 | 342 |
| 309 // Test that data can be written asynchronously. | 343 // Test that data can be written asynchronously. |
| 310 TEST_F(SocketBIOAdapterTest, WriteAsync) { | 344 TEST_P(SocketBIOAdapterTest, WriteAsync) { |
| 311 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); | 345 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 312 | 346 |
| 313 MockWrite writes[] = { | 347 MockWrite writes[] = { |
| 314 MockWrite(ASYNC, 0, "aaa"), | 348 MockWrite(ASYNC, 0, "aaa"), |
| 315 MockWrite(ASYNC, ERR_IO_PENDING, 1), // pause | 349 MockWrite(ASYNC, ERR_IO_PENDING, 1), // pause |
| 316 MockWrite(ASYNC, 2, "aabbbbb"), | 350 MockWrite(ASYNC, 2, "aabbbbb"), |
| 317 MockWrite(ASYNC, 3, "ccc"), | 351 MockWrite(ASYNC, 3, "ccc"), |
| 318 MockWrite(ASYNC, 4, "ddd"), | 352 MockWrite(ASYNC, 4, "ddd"), |
| 319 MockWrite(ASYNC, ERR_IO_PENDING, 5), // pause | 353 MockWrite(ASYNC, ERR_IO_PENDING, 5), // pause |
| 320 MockWrite(ASYNC, 6, "dd"), | 354 MockWrite(ASYNC, 6, "dd"), |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 EXPECT_EQ(5, BIO_write(bio, "hhhhh", 5)); | 463 EXPECT_EQ(5, BIO_write(bio, "hhhhh", 5)); |
| 430 | 464 |
| 431 // Release the write error (event 14). At this point future BIO_write calls | 465 // Release the write error (event 14). At this point future BIO_write calls |
| 432 // fail. The buffer was not full, so OnWriteReady is not signalled. | 466 // fail. The buffer was not full, so OnWriteReady is not signalled. |
| 433 base::RunLoop().RunUntilIdle(); | 467 base::RunLoop().RunUntilIdle(); |
| 434 ExpectWriteError(bio, ERR_CONNECTION_RESET, tracer); | 468 ExpectWriteError(bio, ERR_CONNECTION_RESET, tracer); |
| 435 } | 469 } |
| 436 | 470 |
| 437 // Test that a failed socket write is reported through BIO_read and prevents it | 471 // Test that a failed socket write is reported through BIO_read and prevents it |
| 438 // from scheduling a socket read. See https://crbug.com/249848. | 472 // from scheduling a socket read. See https://crbug.com/249848. |
| 439 TEST_F(SocketBIOAdapterTest, WriteStopsRead) { | 473 TEST_P(SocketBIOAdapterTest, WriteStopsRead) { |
| 440 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); | 474 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 441 | 475 |
| 442 MockWrite writes[] = { | 476 MockWrite writes[] = { |
| 443 MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET, 0), | 477 MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET, 0), |
| 444 }; | 478 }; |
| 445 | 479 |
| 446 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); | 480 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); |
| 447 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); | 481 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 448 std::unique_ptr<SocketBIOAdapter> adapter = | 482 std::unique_ptr<SocketBIOAdapter> adapter = |
| 449 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); | 483 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); |
| 450 BIO* bio = adapter->bio(); | 484 BIO* bio = adapter->bio(); |
| 451 | 485 |
| 452 // The write fails, but there is a write buffer, so errors are delayed. | 486 // The write fails, but there is a write buffer, so errors are delayed. |
| 453 EXPECT_EQ(5, BIO_write(bio, "aaaaa", 5)); | 487 EXPECT_EQ(5, BIO_write(bio, "aaaaa", 5)); |
| 454 | 488 |
| 455 // The write error is surfaced out of BIO_read. There are no MockReads, so | 489 // The write error is surfaced out of BIO_read. There are no MockReads, so |
| 456 // this also tests that no socket reads are attempted. | 490 // this also tests that no socket reads are attempted. |
| 457 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); | 491 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); |
| 458 } | 492 } |
| 459 | 493 |
| 460 // Test that a synchronous failed socket write interrupts a blocked | 494 // Test that a synchronous failed socket write interrupts a blocked |
| 461 // BIO_read. See https://crbug.com/249848. | 495 // BIO_read. See https://crbug.com/249848. |
| 462 TEST_F(SocketBIOAdapterTest, SyncWriteInterruptsRead) { | 496 TEST_P(SocketBIOAdapterTest, SyncWriteInterruptsRead) { |
| 463 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); | 497 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 464 | 498 |
| 465 MockRead reads[] = { | 499 MockRead reads[] = { |
| 466 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0), | 500 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0), |
| 467 }; | 501 }; |
| 468 | 502 |
| 469 MockWrite writes[] = { | 503 MockWrite writes[] = { |
| 470 MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET, 1), | 504 MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET, 1), |
| 471 }; | 505 }; |
| 472 | 506 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 485 | 519 |
| 486 // The write error triggers OnReadReady. | 520 // The write error triggers OnReadReady. |
| 487 WaitForReadReady(); | 521 WaitForReadReady(); |
| 488 | 522 |
| 489 // The write error is surfaced out of BIO_read. | 523 // The write error is surfaced out of BIO_read. |
| 490 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); | 524 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); |
| 491 } | 525 } |
| 492 | 526 |
| 493 // Test that an asynchronous failed socket write interrupts a blocked | 527 // Test that an asynchronous failed socket write interrupts a blocked |
| 494 // BIO_read. See https://crbug.com/249848. | 528 // BIO_read. See https://crbug.com/249848. |
| 495 TEST_F(SocketBIOAdapterTest, AsyncWriteInterruptsRead) { | 529 TEST_P(SocketBIOAdapterTest, AsyncWriteInterruptsRead) { |
| 496 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); | 530 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 497 | 531 |
| 498 MockRead reads[] = { | 532 MockRead reads[] = { |
| 499 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0), | 533 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0), |
| 500 }; | 534 }; |
| 501 | 535 |
| 502 MockWrite writes[] = { | 536 MockWrite writes[] = { |
| 503 MockWrite(ASYNC, ERR_CONNECTION_RESET, 1), | 537 MockWrite(ASYNC, ERR_CONNECTION_RESET, 1), |
| 504 }; | 538 }; |
| 505 | 539 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 520 // OnReadReady is signaled. The write buffer was not full, so OnWriteReady is | 554 // OnReadReady is signaled. The write buffer was not full, so OnWriteReady is |
| 521 // not signaled. | 555 // not signaled. |
| 522 WaitForReadReady(); | 556 WaitForReadReady(); |
| 523 | 557 |
| 524 // The write error is surfaced out of BIO_read. | 558 // The write error is surfaced out of BIO_read. |
| 525 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); | 559 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); |
| 526 } | 560 } |
| 527 | 561 |
| 528 // Test that an asynchronous failed socket write interrupts a blocked BIO_read, | 562 // Test that an asynchronous failed socket write interrupts a blocked BIO_read, |
| 529 // signaling both if the buffer was full. See https://crbug.com/249848. | 563 // signaling both if the buffer was full. See https://crbug.com/249848. |
| 530 TEST_F(SocketBIOAdapterTest, AsyncWriteInterruptsBoth) { | 564 TEST_P(SocketBIOAdapterTest, AsyncWriteInterruptsBoth) { |
| 531 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); | 565 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 532 | 566 |
| 533 MockRead reads[] = { | 567 MockRead reads[] = { |
| 534 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0), | 568 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0), |
| 535 }; | 569 }; |
| 536 | 570 |
| 537 MockWrite writes[] = { | 571 MockWrite writes[] = { |
| 538 MockWrite(ASYNC, ERR_CONNECTION_RESET, 1), | 572 MockWrite(ASYNC, ERR_CONNECTION_RESET, 1), |
| 539 }; | 573 }; |
| 540 | 574 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 555 // OnReadReady is signaled. The write buffer was full, so both OnWriteReady is | 589 // OnReadReady is signaled. The write buffer was full, so both OnWriteReady is |
| 556 // also signaled. | 590 // also signaled. |
| 557 WaitForBothReady(); | 591 WaitForBothReady(); |
| 558 | 592 |
| 559 // The write error is surfaced out of BIO_read. | 593 // The write error is surfaced out of BIO_read. |
| 560 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); | 594 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); |
| 561 } | 595 } |
| 562 | 596 |
| 563 // Test that SocketBIOAdapter handles OnWriteReady deleting itself when both | 597 // Test that SocketBIOAdapter handles OnWriteReady deleting itself when both |
| 564 // need to be signaled. | 598 // need to be signaled. |
| 565 TEST_F(SocketBIOAdapterTest, DeleteOnWriteReady) { | 599 TEST_P(SocketBIOAdapterTest, DeleteOnWriteReady) { |
| 566 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); | 600 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 567 | 601 |
| 568 MockRead reads[] = { | 602 MockRead reads[] = { |
| 569 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0), | 603 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0), |
| 570 }; | 604 }; |
| 571 | 605 |
| 572 MockWrite writes[] = { | 606 MockWrite writes[] = { |
| 573 MockWrite(ASYNC, ERR_CONNECTION_RESET, 1), | 607 MockWrite(ASYNC, ERR_CONNECTION_RESET, 1), |
| 574 }; | 608 }; |
| 575 | 609 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 588 // Both OnWriteReady and OnReadReady would be signaled, but OnWriteReady | 622 // Both OnWriteReady and OnReadReady would be signaled, but OnWriteReady |
| 589 // deletes the adapter first. | 623 // deletes the adapter first. |
| 590 set_reset_on_write_ready(&adapter); | 624 set_reset_on_write_ready(&adapter); |
| 591 WaitForWriteReady(nullptr); | 625 WaitForWriteReady(nullptr); |
| 592 | 626 |
| 593 EXPECT_FALSE(adapter); | 627 EXPECT_FALSE(adapter); |
| 594 } | 628 } |
| 595 | 629 |
| 596 // Test that using a BIO after the underlying adapter is destroyed fails | 630 // Test that using a BIO after the underlying adapter is destroyed fails |
| 597 // gracefully. | 631 // gracefully. |
| 598 TEST_F(SocketBIOAdapterTest, Detached) { | 632 TEST_P(SocketBIOAdapterTest, Detached) { |
| 599 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); | 633 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 600 | 634 |
| 601 SequencedSocketData data(nullptr, 0, nullptr, 0); | 635 SequencedSocketData data(nullptr, 0, nullptr, 0); |
| 602 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); | 636 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 603 std::unique_ptr<SocketBIOAdapter> adapter = | 637 std::unique_ptr<SocketBIOAdapter> adapter = |
| 604 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); | 638 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); |
| 605 | 639 |
| 606 // Retain an additional reference to the BIO. | 640 // Retain an additional reference to the BIO. |
| 607 bssl::UniquePtr<BIO> bio(adapter->bio()); | 641 bssl::UniquePtr<BIO> bio(adapter->bio()); |
| 608 BIO_up_ref(bio.get()); | 642 BIO_up_ref(bio.get()); |
| 609 | 643 |
| 610 // Release the adapter. | 644 // Release the adapter. |
| 611 adapter.reset(); | 645 adapter.reset(); |
| 612 | 646 |
| 613 ExpectReadError(bio.get(), ERR_UNEXPECTED, tracer); | 647 ExpectReadError(bio.get(), ERR_UNEXPECTED, tracer); |
| 614 ExpectWriteError(bio.get(), ERR_UNEXPECTED, tracer); | 648 ExpectWriteError(bio.get(), ERR_UNEXPECTED, tracer); |
| 615 } | 649 } |
| 616 | 650 |
| 617 } // namespace net | 651 } // namespace net |
| OLD | NEW |