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