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