| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/socket/socket_bio_adapter.h" |
| 6 |
| 7 #include <openssl/bio.h> |
| 8 #include <openssl/err.h> |
| 9 #include <openssl/ssl.h> |
| 10 #include <string.h> |
| 11 |
| 12 #include <memory> |
| 13 |
| 14 #include "base/location.h" |
| 15 #include "base/logging.h" |
| 16 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/run_loop.h" |
| 19 #include "crypto/openssl_util.h" |
| 20 #include "net/base/address_list.h" |
| 21 #include "net/base/net_errors.h" |
| 22 #include "net/log/net_log_source.h" |
| 23 #include "net/socket/socket_test_util.h" |
| 24 #include "net/socket/stream_socket.h" |
| 25 #include "net/ssl/openssl_ssl_util.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 |
| 28 namespace net { |
| 29 |
| 30 class SocketBIOAdapterTest : public testing::Test, |
| 31 public SocketBIOAdapter::Delegate { |
| 32 protected: |
| 33 std::unique_ptr<StreamSocket> MakeTestSocket(SocketDataProvider* data) { |
| 34 data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
| 35 factory_.AddSocketDataProvider(data); |
| 36 std::unique_ptr<StreamSocket> socket = factory_.CreateTransportClientSocket( |
| 37 AddressList(), nullptr, nullptr, NetLogSource()); |
| 38 CHECK_EQ(OK, socket->Connect(net::CompletionCallback())); |
| 39 return socket; |
| 40 } |
| 41 |
| 42 void set_reset_on_write_ready( |
| 43 std::unique_ptr<SocketBIOAdapter>* reset_on_write_ready) { |
| 44 reset_on_write_ready_ = reset_on_write_ready; |
| 45 } |
| 46 |
| 47 void ExpectReadError(BIO* bio, |
| 48 int error, |
| 49 const crypto::OpenSSLErrStackTracer& tracer) { |
| 50 // BIO_read should fail. |
| 51 char buf; |
| 52 EXPECT_EQ(-1, BIO_read(bio, &buf, 1)); |
| 53 EXPECT_EQ(error, MapOpenSSLError(SSL_ERROR_SSL, tracer)); |
| 54 EXPECT_FALSE(BIO_should_read(bio)); |
| 55 |
| 56 // Repeating the operation should replay the error. |
| 57 EXPECT_EQ(-1, BIO_read(bio, &buf, 1)); |
| 58 EXPECT_EQ(error, MapOpenSSLError(SSL_ERROR_SSL, tracer)); |
| 59 EXPECT_FALSE(BIO_should_read(bio)); |
| 60 } |
| 61 |
| 62 void ExpectBlockingRead(BIO* bio, void* buf, int len) { |
| 63 // BIO_read should return a retryable error. |
| 64 EXPECT_EQ(-1, BIO_read(bio, buf, len)); |
| 65 EXPECT_TRUE(BIO_should_read(bio)); |
| 66 EXPECT_EQ(0u, ERR_peek_error()); |
| 67 |
| 68 // Repeating the operation has the same result. |
| 69 EXPECT_EQ(-1, BIO_read(bio, buf, len)); |
| 70 EXPECT_TRUE(BIO_should_read(bio)); |
| 71 EXPECT_EQ(0u, ERR_peek_error()); |
| 72 } |
| 73 |
| 74 void ExpectWriteError(BIO* bio, |
| 75 int error, |
| 76 const crypto::OpenSSLErrStackTracer& tracer) { |
| 77 // BIO_write should fail. |
| 78 char buf = '?'; |
| 79 EXPECT_EQ(-1, BIO_write(bio, &buf, 1)); |
| 80 EXPECT_EQ(error, MapOpenSSLError(SSL_ERROR_SSL, tracer)); |
| 81 EXPECT_FALSE(BIO_should_write(bio)); |
| 82 |
| 83 // Repeating the operation should replay the error. |
| 84 EXPECT_EQ(-1, BIO_write(bio, &buf, 1)); |
| 85 EXPECT_EQ(error, MapOpenSSLError(SSL_ERROR_SSL, tracer)); |
| 86 EXPECT_FALSE(BIO_should_write(bio)); |
| 87 } |
| 88 |
| 89 void ExpectBlockingWrite(BIO* bio, const void* buf, int len) { |
| 90 // BIO_write should return a retryable error. |
| 91 EXPECT_EQ(-1, BIO_write(bio, buf, len)); |
| 92 EXPECT_TRUE(BIO_should_write(bio)); |
| 93 EXPECT_EQ(0u, ERR_peek_error()); |
| 94 |
| 95 // Repeating the operation has the same result. |
| 96 EXPECT_EQ(-1, BIO_write(bio, buf, len)); |
| 97 EXPECT_TRUE(BIO_should_write(bio)); |
| 98 EXPECT_EQ(0u, ERR_peek_error()); |
| 99 } |
| 100 |
| 101 void WaitForReadReady() { |
| 102 expect_read_ready_ = true; |
| 103 base::RunLoop().RunUntilIdle(); |
| 104 EXPECT_FALSE(expect_read_ready_); |
| 105 } |
| 106 |
| 107 void WaitForWriteReady(SequencedSocketData* to_resume) { |
| 108 expect_write_ready_ = true; |
| 109 if (to_resume) { |
| 110 to_resume->Resume(); |
| 111 } |
| 112 base::RunLoop().RunUntilIdle(); |
| 113 EXPECT_FALSE(expect_write_ready_); |
| 114 } |
| 115 |
| 116 void WaitForBothReady() { |
| 117 expect_read_ready_ = true; |
| 118 expect_write_ready_ = true; |
| 119 base::RunLoop().RunUntilIdle(); |
| 120 EXPECT_FALSE(expect_read_ready_); |
| 121 EXPECT_FALSE(expect_write_ready_); |
| 122 } |
| 123 |
| 124 // SocketBIOAdapter::Delegate implementation: |
| 125 void OnReadReady() override { |
| 126 EXPECT_TRUE(expect_read_ready_); |
| 127 expect_read_ready_ = false; |
| 128 } |
| 129 |
| 130 void OnWriteReady() override { |
| 131 EXPECT_TRUE(expect_write_ready_); |
| 132 expect_write_ready_ = false; |
| 133 if (reset_on_write_ready_) |
| 134 reset_on_write_ready_->reset(); |
| 135 } |
| 136 |
| 137 private: |
| 138 bool expect_read_ready_ = false; |
| 139 bool expect_write_ready_ = false; |
| 140 MockClientSocketFactory factory_; |
| 141 std::unique_ptr<SocketBIOAdapter>* reset_on_write_ready_ = nullptr; |
| 142 }; |
| 143 |
| 144 // Test that data can be read synchronously. |
| 145 TEST_F(SocketBIOAdapterTest, ReadSync) { |
| 146 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 147 |
| 148 MockRead reads[] = { |
| 149 MockRead(SYNCHRONOUS, 0, "hello"), MockRead(SYNCHRONOUS, 1, "world"), |
| 150 MockRead(SYNCHRONOUS, ERR_CONNECTION_RESET, 2), |
| 151 }; |
| 152 |
| 153 SequencedSocketData data(reads, arraysize(reads), nullptr, 0); |
| 154 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 155 std::unique_ptr<SocketBIOAdapter> adapter = |
| 156 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); |
| 157 BIO* bio = adapter->bio(); |
| 158 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 159 |
| 160 // Read the data synchronously. Although the buffer has room for both, |
| 161 // BIO_read only reports one socket-level Read. |
| 162 char buf[10]; |
| 163 EXPECT_EQ(5, BIO_read(bio, buf, sizeof(buf))); |
| 164 EXPECT_EQ(0, memcmp("hello", buf, 5)); |
| 165 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 166 |
| 167 // Consume the next portion one byte at a time. |
| 168 EXPECT_EQ(1, BIO_read(bio, buf, 1)); |
| 169 EXPECT_EQ('w', buf[0]); |
| 170 EXPECT_TRUE(adapter->HasPendingReadData()); |
| 171 |
| 172 EXPECT_EQ(1, BIO_read(bio, buf, 1)); |
| 173 EXPECT_EQ('o', buf[0]); |
| 174 EXPECT_TRUE(adapter->HasPendingReadData()); |
| 175 |
| 176 // The remainder may be consumed in a single BIO_read. |
| 177 EXPECT_EQ(3, BIO_read(bio, buf, sizeof(buf))); |
| 178 EXPECT_EQ(0, memcmp("rld", buf, 3)); |
| 179 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 180 |
| 181 // The error is available synchoronously. |
| 182 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); |
| 183 } |
| 184 |
| 185 // Test that data can be read asynchronously. |
| 186 TEST_F(SocketBIOAdapterTest, ReadAsync) { |
| 187 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 188 |
| 189 MockRead reads[] = { |
| 190 MockRead(ASYNC, 0, "hello"), MockRead(ASYNC, 1, "world"), |
| 191 MockRead(ASYNC, ERR_CONNECTION_RESET, 2), |
| 192 }; |
| 193 |
| 194 SequencedSocketData data(reads, arraysize(reads), nullptr, 0); |
| 195 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 196 std::unique_ptr<SocketBIOAdapter> adapter = |
| 197 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); |
| 198 BIO* bio = adapter->bio(); |
| 199 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 200 |
| 201 // Attempt to read data. It will fail but schedule a Read. |
| 202 char buf[10]; |
| 203 ExpectBlockingRead(bio, buf, sizeof(buf)); |
| 204 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 205 |
| 206 // After waiting, the data is available. |
| 207 WaitForReadReady(); |
| 208 EXPECT_TRUE(adapter->HasPendingReadData()); |
| 209 |
| 210 // The first read is now available synchronously. |
| 211 EXPECT_EQ(5, BIO_read(bio, buf, sizeof(buf))); |
| 212 EXPECT_EQ(0, memcmp("hello", buf, 5)); |
| 213 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 214 |
| 215 // The adapter does not schedule another Read until BIO_read is next called. |
| 216 base::RunLoop().RunUntilIdle(); |
| 217 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 218 |
| 219 // This time, under-request the data. The adapter should still read the full |
| 220 // amount. |
| 221 ExpectBlockingRead(bio, buf, 1); |
| 222 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 223 WaitForReadReady(); |
| 224 EXPECT_TRUE(adapter->HasPendingReadData()); |
| 225 |
| 226 // The next read is now available synchronously. |
| 227 EXPECT_EQ(5, BIO_read(bio, buf, sizeof(buf))); |
| 228 EXPECT_EQ(0, memcmp("world", buf, 5)); |
| 229 EXPECT_FALSE(adapter->HasPendingReadData()); |
| 230 |
| 231 // The error is not yet available. |
| 232 ExpectBlockingRead(bio, buf, sizeof(buf)); |
| 233 WaitForReadReady(); |
| 234 |
| 235 // The error is now available synchoronously. |
| 236 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); |
| 237 } |
| 238 |
| 239 // Test that synchronous EOF is mapped to ERR_CONNECTION_CLOSED. |
| 240 TEST_F(SocketBIOAdapterTest, ReadEOFSync) { |
| 241 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 242 |
| 243 MockRead reads[] = { |
| 244 MockRead(SYNCHRONOUS, 0, 0), |
| 245 }; |
| 246 |
| 247 SequencedSocketData data(reads, arraysize(reads), nullptr, 0); |
| 248 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 249 std::unique_ptr<SocketBIOAdapter> adapter = |
| 250 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); |
| 251 |
| 252 ExpectReadError(adapter->bio(), ERR_CONNECTION_CLOSED, tracer); |
| 253 } |
| 254 |
| 255 // Test that asynchronous EOF is mapped to ERR_CONNECTION_CLOSED. |
| 256 TEST_F(SocketBIOAdapterTest, ReadEOFAsync) { |
| 257 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 258 |
| 259 MockRead reads[] = { |
| 260 MockRead(ASYNC, 0, 0), |
| 261 }; |
| 262 |
| 263 SequencedSocketData data(reads, arraysize(reads), nullptr, 0); |
| 264 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 265 std::unique_ptr<SocketBIOAdapter> adapter = |
| 266 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); |
| 267 |
| 268 char buf; |
| 269 ExpectBlockingRead(adapter->bio(), &buf, 1); |
| 270 WaitForReadReady(); |
| 271 ExpectReadError(adapter->bio(), ERR_CONNECTION_CLOSED, tracer); |
| 272 } |
| 273 |
| 274 // Test that data can be written synchronously. |
| 275 TEST_F(SocketBIOAdapterTest, WriteSync) { |
| 276 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 277 |
| 278 MockWrite writes[] = { |
| 279 MockWrite(SYNCHRONOUS, 0, "hello"), |
| 280 MockWrite(SYNCHRONOUS, 1, "wor"), |
| 281 MockWrite(SYNCHRONOUS, 2, "ld"), |
| 282 MockWrite(SYNCHRONOUS, 3, "helloworld"), |
| 283 MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET, 4), |
| 284 }; |
| 285 |
| 286 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); |
| 287 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 288 std::unique_ptr<SocketBIOAdapter> adapter = |
| 289 base::MakeUnique<SocketBIOAdapter>(socket.get(), 10, 10, this); |
| 290 BIO* bio = adapter->bio(); |
| 291 |
| 292 // Test data entering and leaving the buffer synchronously. The second write |
| 293 // takes multiple iterations (events 0 to 2). |
| 294 EXPECT_EQ(5, BIO_write(bio, "hello", 5)); |
| 295 EXPECT_EQ(5, BIO_write(bio, "world", 5)); |
| 296 |
| 297 // If writing larger than the buffer size, only part of the data is written |
| 298 // (event 3). |
| 299 EXPECT_EQ(10, BIO_write(bio, "helloworldhelloworld", 20)); |
| 300 |
| 301 // Writing "aaaaa" fails (event 4), but there is a write buffer, so errors |
| 302 // are delayed. |
| 303 EXPECT_EQ(5, BIO_write(bio, "aaaaa", 5)); |
| 304 |
| 305 // However once the error is registered, subsequent writes fail. |
| 306 ExpectWriteError(bio, ERR_CONNECTION_RESET, tracer); |
| 307 } |
| 308 |
| 309 // Test that data can be written asynchronously. |
| 310 TEST_F(SocketBIOAdapterTest, WriteAsync) { |
| 311 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 312 |
| 313 MockWrite writes[] = { |
| 314 MockWrite(ASYNC, 0, "aaa"), |
| 315 MockWrite(ASYNC, ERR_IO_PENDING, 1), // pause |
| 316 MockWrite(ASYNC, 2, "aabbbbb"), |
| 317 MockWrite(ASYNC, 3, "ccc"), |
| 318 MockWrite(ASYNC, 4, "ddd"), |
| 319 MockWrite(ASYNC, ERR_IO_PENDING, 5), // pause |
| 320 MockWrite(ASYNC, 6, "dd"), |
| 321 MockWrite(SYNCHRONOUS, 7, "e"), |
| 322 MockWrite(SYNCHRONOUS, 8, "e"), |
| 323 MockWrite(ASYNC, 9, "e"), |
| 324 MockWrite(ASYNC, 10, "ee"), |
| 325 MockWrite(ASYNC, ERR_IO_PENDING, 11), // pause |
| 326 MockWrite(ASYNC, 12, "eff"), |
| 327 MockWrite(ASYNC, 13, "ggggggg"), |
| 328 MockWrite(ASYNC, ERR_CONNECTION_RESET, 14), |
| 329 }; |
| 330 |
| 331 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); |
| 332 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 333 std::unique_ptr<SocketBIOAdapter> adapter = |
| 334 base::MakeUnique<SocketBIOAdapter>(socket.get(), 10, 10, this); |
| 335 BIO* bio = adapter->bio(); |
| 336 |
| 337 // Data which fits in the buffer is returned synchronously, even if not |
| 338 // flushed synchronously. |
| 339 EXPECT_EQ(5, BIO_write(bio, "aaaaa", 5)); |
| 340 EXPECT_EQ(5, BIO_write(bio, "bbbbb", 5)); |
| 341 |
| 342 // The buffer contains: |
| 343 // |
| 344 // [aaaaabbbbb] |
| 345 // ^ |
| 346 |
| 347 // The buffer is full now, so the next write will block. |
| 348 ExpectBlockingWrite(bio, "zzzzz", 5); |
| 349 |
| 350 // Let the first socket write complete (event 0) and pause (event 1). |
| 351 WaitForWriteReady(nullptr); |
| 352 EXPECT_TRUE(data.IsPaused()); |
| 353 |
| 354 // The buffer contains: |
| 355 // |
| 356 // [...aabbbbb] |
| 357 // ^ |
| 358 |
| 359 // The ring buffer now has 3 bytes of space with "aabbbbb" still to be |
| 360 // written. Attempting to write 3 bytes means 3 succeed. |
| 361 EXPECT_EQ(3, BIO_write(bio, "cccccccccc", 10)); |
| 362 |
| 363 // The buffer contains: |
| 364 // |
| 365 // [cccaabbbbb] |
| 366 // ^ |
| 367 |
| 368 // Drain the buffer (events 2 and 3). |
| 369 WaitForWriteReady(&data); |
| 370 |
| 371 // The buffer is now empty. |
| 372 |
| 373 // Now test something similar but arrange for a BIO_write (the 'e's below) to |
| 374 // wrap around the buffer. Write five bytes into the buffer, flush the first |
| 375 // three (event 4), and pause (event 5). OnWriteReady is not signaled because |
| 376 // the buffer was not full. |
| 377 EXPECT_EQ(5, BIO_write(bio, "ddddd", 5)); |
| 378 base::RunLoop().RunUntilIdle(); |
| 379 EXPECT_TRUE(data.IsPaused()); |
| 380 |
| 381 // The buffer contains: |
| 382 // |
| 383 // [...dd.....] |
| 384 // ^ |
| 385 |
| 386 // The adapter maintains a ring buffer, so 6 bytes fit. |
| 387 EXPECT_EQ(6, BIO_write(bio, "eeeeee", 6)); |
| 388 |
| 389 // The buffer contains: |
| 390 // |
| 391 // [e..ddeeeee] |
| 392 // ^ |
| 393 |
| 394 // The remaining space may be filled in. |
| 395 EXPECT_EQ(2, BIO_write(bio, "ffffffffff", 10)); |
| 396 |
| 397 // The buffer contains: |
| 398 // |
| 399 // [effddeeeee] |
| 400 // ^ |
| 401 |
| 402 // Drain to the end of the ring buffer, so it wraps around (events 6 to 10) |
| 403 // and pause (event 11). Test that synchronous and asynchronous writes both |
| 404 // drain. The start of the buffer has now wrapped around. |
| 405 WaitForWriteReady(&data); |
| 406 EXPECT_TRUE(data.IsPaused()); |
| 407 |
| 408 // The buffer contains: |
| 409 // |
| 410 // [eff.......] |
| 411 // ^ |
| 412 |
| 413 // Test wrapping around works correctly and the buffer may be appended to. |
| 414 EXPECT_EQ(7, BIO_write(bio, "gggggggggg", 10)); |
| 415 |
| 416 // The buffer contains: |
| 417 // |
| 418 // [effggggggg] |
| 419 // ^ |
| 420 |
| 421 // The buffer is full now, so the next write will block. |
| 422 ExpectBlockingWrite(bio, "zzzzz", 5); |
| 423 |
| 424 // Drain the buffer to confirm the ring buffer's contents are as expected |
| 425 // (events 12 and 13). |
| 426 WaitForWriteReady(&data); |
| 427 |
| 428 // Write again so the write error may be discovered. |
| 429 EXPECT_EQ(5, BIO_write(bio, "hhhhh", 5)); |
| 430 |
| 431 // 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. |
| 433 base::RunLoop().RunUntilIdle(); |
| 434 ExpectWriteError(bio, ERR_CONNECTION_RESET, tracer); |
| 435 } |
| 436 |
| 437 // 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. |
| 439 TEST_F(SocketBIOAdapterTest, WriteStopsRead) { |
| 440 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 441 |
| 442 MockWrite writes[] = { |
| 443 MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET, 0), |
| 444 }; |
| 445 |
| 446 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); |
| 447 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 448 std::unique_ptr<SocketBIOAdapter> adapter = |
| 449 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); |
| 450 BIO* bio = adapter->bio(); |
| 451 |
| 452 // The write fails, but there is a write buffer, so errors are delayed. |
| 453 EXPECT_EQ(5, BIO_write(bio, "aaaaa", 5)); |
| 454 |
| 455 // The write error is surfaced out of BIO_read. There are no MockReads, so |
| 456 // this also tests that no socket reads are attempted. |
| 457 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); |
| 458 } |
| 459 |
| 460 // Test that a synchronous failed socket write interrupts a blocked |
| 461 // BIO_read. See https://crbug.com/249848. |
| 462 TEST_F(SocketBIOAdapterTest, SyncWriteInterruptsRead) { |
| 463 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 464 |
| 465 MockRead reads[] = { |
| 466 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0), |
| 467 }; |
| 468 |
| 469 MockWrite writes[] = { |
| 470 MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET, 1), |
| 471 }; |
| 472 |
| 473 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
| 474 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 475 std::unique_ptr<SocketBIOAdapter> adapter = |
| 476 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); |
| 477 BIO* bio = adapter->bio(); |
| 478 |
| 479 // Attempt to read from the transport. It will block indefinitely. |
| 480 char buf; |
| 481 ExpectBlockingRead(adapter->bio(), &buf, 1); |
| 482 |
| 483 // Schedule a socket write. |
| 484 EXPECT_EQ(5, BIO_write(bio, "aaaaa", 5)); |
| 485 |
| 486 // The write error triggers OnReadReady. |
| 487 WaitForReadReady(); |
| 488 |
| 489 // The write error is surfaced out of BIO_read. |
| 490 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); |
| 491 } |
| 492 |
| 493 // Test that an asynchronous failed socket write interrupts a blocked |
| 494 // BIO_read. See https://crbug.com/249848. |
| 495 TEST_F(SocketBIOAdapterTest, AsyncWriteInterruptsRead) { |
| 496 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 497 |
| 498 MockRead reads[] = { |
| 499 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0), |
| 500 }; |
| 501 |
| 502 MockWrite writes[] = { |
| 503 MockWrite(ASYNC, ERR_CONNECTION_RESET, 1), |
| 504 }; |
| 505 |
| 506 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
| 507 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 508 std::unique_ptr<SocketBIOAdapter> adapter = |
| 509 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); |
| 510 BIO* bio = adapter->bio(); |
| 511 |
| 512 // Attempt to read from the transport. It will block indefinitely. |
| 513 char buf; |
| 514 ExpectBlockingRead(adapter->bio(), &buf, 1); |
| 515 |
| 516 // Schedule a socket write. |
| 517 EXPECT_EQ(5, BIO_write(bio, "aaaaa", 5)); |
| 518 |
| 519 // The write error is signaled asynchronously and interrupts BIO_read, so |
| 520 // OnReadReady is signaled. The write buffer was not full, so OnWriteReady is |
| 521 // not signaled. |
| 522 WaitForReadReady(); |
| 523 |
| 524 // The write error is surfaced out of BIO_read. |
| 525 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); |
| 526 } |
| 527 |
| 528 // 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. |
| 530 TEST_F(SocketBIOAdapterTest, AsyncWriteInterruptsBoth) { |
| 531 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 532 |
| 533 MockRead reads[] = { |
| 534 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0), |
| 535 }; |
| 536 |
| 537 MockWrite writes[] = { |
| 538 MockWrite(ASYNC, ERR_CONNECTION_RESET, 1), |
| 539 }; |
| 540 |
| 541 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
| 542 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 543 std::unique_ptr<SocketBIOAdapter> adapter = |
| 544 base::MakeUnique<SocketBIOAdapter>(socket.get(), 5, 5, this); |
| 545 BIO* bio = adapter->bio(); |
| 546 |
| 547 // Attempt to read from the transport. It will block indefinitely. |
| 548 char buf; |
| 549 ExpectBlockingRead(adapter->bio(), &buf, 1); |
| 550 |
| 551 // Schedule a socket write. |
| 552 EXPECT_EQ(5, BIO_write(bio, "aaaaa", 5)); |
| 553 |
| 554 // The write error is signaled asynchronously and interrupts BIO_read, so |
| 555 // OnReadReady is signaled. The write buffer was full, so both OnWriteReady is |
| 556 // also signaled. |
| 557 WaitForBothReady(); |
| 558 |
| 559 // The write error is surfaced out of BIO_read. |
| 560 ExpectReadError(bio, ERR_CONNECTION_RESET, tracer); |
| 561 } |
| 562 |
| 563 // Test that SocketBIOAdapter handles OnWriteReady deleting itself when both |
| 564 // need to be signaled. |
| 565 TEST_F(SocketBIOAdapterTest, DeleteOnWriteReady) { |
| 566 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 567 |
| 568 MockRead reads[] = { |
| 569 MockRead(SYNCHRONOUS, ERR_IO_PENDING, 0), |
| 570 }; |
| 571 |
| 572 MockWrite writes[] = { |
| 573 MockWrite(ASYNC, ERR_CONNECTION_RESET, 1), |
| 574 }; |
| 575 |
| 576 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
| 577 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 578 std::unique_ptr<SocketBIOAdapter> adapter = |
| 579 base::MakeUnique<SocketBIOAdapter>(socket.get(), 5, 5, this); |
| 580 BIO* bio = adapter->bio(); |
| 581 |
| 582 // Arrange for OnReadReady and OnWriteReady to both be signaled due to write |
| 583 // error propagation (see the AsyncWriteInterruptsBoth test). |
| 584 char buf; |
| 585 ExpectBlockingRead(adapter->bio(), &buf, 1); |
| 586 EXPECT_EQ(5, BIO_write(bio, "aaaaa", 5)); |
| 587 |
| 588 // Both OnWriteReady and OnReadReady would be signaled, but OnWriteReady |
| 589 // deletes the adapter first. |
| 590 set_reset_on_write_ready(&adapter); |
| 591 WaitForWriteReady(nullptr); |
| 592 |
| 593 EXPECT_FALSE(adapter); |
| 594 } |
| 595 |
| 596 // Test that using a BIO after the underlying adapter is destroyed fails |
| 597 // gracefully. |
| 598 TEST_F(SocketBIOAdapterTest, Detached) { |
| 599 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 600 |
| 601 SequencedSocketData data(nullptr, 0, nullptr, 0); |
| 602 std::unique_ptr<StreamSocket> socket = MakeTestSocket(&data); |
| 603 std::unique_ptr<SocketBIOAdapter> adapter = |
| 604 base::MakeUnique<SocketBIOAdapter>(socket.get(), 100, 100, this); |
| 605 |
| 606 // Retain an additional reference to the BIO. |
| 607 bssl::UniquePtr<BIO> bio(adapter->bio()); |
| 608 BIO_up_ref(bio.get()); |
| 609 |
| 610 // Release the adapter. |
| 611 adapter.reset(); |
| 612 |
| 613 ExpectReadError(bio.get(), ERR_UNEXPECTED, tracer); |
| 614 ExpectWriteError(bio.get(), ERR_UNEXPECTED, tracer); |
| 615 } |
| 616 |
| 617 } // namespace net |
| OLD | NEW |