OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "chrome/browser/sync/tools/chrome_async_socket.h" |
| 6 |
| 7 #include <deque> |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/message_loop.h" |
| 13 #include "net/base/ssl_config_service.h" |
| 14 #include "net/base/capturing_net_log.h" |
| 15 #include "net/base/net_errors.h" |
| 16 #include "net/socket/socket_test_util.h" |
| 17 #include "talk/base/sigslot.h" |
| 18 #include "talk/base/socketaddress.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace sync_tools { |
| 22 |
| 23 namespace { |
| 24 |
| 25 // Data provider that handles reads/writes for ChromeAsyncSocket. |
| 26 class AsyncSocketDataProvider : public net::SocketDataProvider { |
| 27 public: |
| 28 AsyncSocketDataProvider() : has_pending_read_(false) {} |
| 29 |
| 30 virtual ~AsyncSocketDataProvider() { |
| 31 EXPECT_TRUE(writes_.empty()); |
| 32 EXPECT_TRUE(reads_.empty()); |
| 33 } |
| 34 |
| 35 // If there's no read, sets the "has pending read" flag. Otherwise, |
| 36 // pops the next read. |
| 37 virtual net::MockRead GetNextRead() { |
| 38 if (reads_.empty()) { |
| 39 DCHECK(!has_pending_read_); |
| 40 has_pending_read_ = true; |
| 41 const net::MockRead pending_read(false, net::ERR_IO_PENDING); |
| 42 return pending_read; |
| 43 } |
| 44 net::MockRead mock_read = reads_.front(); |
| 45 reads_.pop_front(); |
| 46 return mock_read; |
| 47 } |
| 48 |
| 49 // Simply pops the next write and, if applicable, compares it to |
| 50 // |data|. |
| 51 virtual net::MockWriteResult OnWrite(const std::string& data) { |
| 52 DCHECK(!writes_.empty()); |
| 53 net::MockWrite mock_write = writes_.front(); |
| 54 writes_.pop_front(); |
| 55 if (mock_write.result != net::OK) { |
| 56 return net::MockWriteResult(mock_write.async, mock_write.result); |
| 57 } |
| 58 std::string expected_data(mock_write.data, mock_write.data_len); |
| 59 EXPECT_EQ(expected_data, data); |
| 60 if (expected_data != data) { |
| 61 return net::MockWriteResult(false, net::ERR_UNEXPECTED); |
| 62 } |
| 63 return net::MockWriteResult(mock_write.async, data.size()); |
| 64 } |
| 65 |
| 66 // We ignore resets so we can pre-load the socket data provider with |
| 67 // read/write events. |
| 68 virtual void Reset() {} |
| 69 |
| 70 // If there is a pending read, completes it with the given read. |
| 71 // Otherwise, queues up the given read. |
| 72 void AddRead(const net::MockRead& mock_read) { |
| 73 DCHECK_NE(mock_read.result, net::ERR_IO_PENDING); |
| 74 if (has_pending_read_) { |
| 75 socket()->OnReadComplete(mock_read); |
| 76 has_pending_read_ = false; |
| 77 return; |
| 78 } |
| 79 reads_.push_back(mock_read); |
| 80 } |
| 81 |
| 82 // Simply queues up the given write. |
| 83 void AddWrite(const net::MockWrite& mock_write) { |
| 84 writes_.push_back(mock_write); |
| 85 } |
| 86 |
| 87 private: |
| 88 std::deque<net::MockRead> reads_; |
| 89 bool has_pending_read_; |
| 90 |
| 91 std::deque<net::MockWrite> writes_; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(AsyncSocketDataProvider); |
| 94 }; |
| 95 |
| 96 class ChromeAsyncSocketTest |
| 97 : public testing::Test, |
| 98 public sigslot::has_slots<> { |
| 99 protected: |
| 100 // TODO(akalin): test SSL states other than connection success. |
| 101 ChromeAsyncSocketTest() |
| 102 : ssl_socket_data_provider_(true, net::OK), |
| 103 capturing_net_log_(net::CapturingNetLog::kUnbounded), |
| 104 chrome_async_socket_(&mock_client_socket_factory_, |
| 105 ssl_config_, 14, 20, &capturing_net_log_), |
| 106 addr_(0xaabbccdd, 35) {} |
| 107 |
| 108 virtual ~ChromeAsyncSocketTest() {} |
| 109 |
| 110 virtual void SetUp() { |
| 111 mock_client_socket_factory_.AddSocketDataProvider( |
| 112 &async_socket_data_provider_); |
| 113 mock_client_socket_factory_.AddSSLSocketDataProvider( |
| 114 &ssl_socket_data_provider_); |
| 115 |
| 116 chrome_async_socket_.SignalConnected.connect( |
| 117 this, &ChromeAsyncSocketTest::OnConnect); |
| 118 chrome_async_socket_.SignalSSLConnected.connect( |
| 119 this, &ChromeAsyncSocketTest::OnSSLConnect); |
| 120 chrome_async_socket_.SignalClosed.connect( |
| 121 this, &ChromeAsyncSocketTest::OnClose); |
| 122 chrome_async_socket_.SignalRead.connect( |
| 123 this, &ChromeAsyncSocketTest::OnRead); |
| 124 chrome_async_socket_.SignalError.connect( |
| 125 this, &ChromeAsyncSocketTest::OnError); |
| 126 } |
| 127 |
| 128 virtual void TearDown() { |
| 129 // Run any tasks that we forgot to pump. |
| 130 message_loop_.RunAllPending(); |
| 131 ExpectClosed(); |
| 132 ExpectNoSignal(); |
| 133 chrome_async_socket_.SignalConnected.disconnect(this); |
| 134 chrome_async_socket_.SignalSSLConnected.disconnect(this); |
| 135 chrome_async_socket_.SignalClosed.disconnect(this); |
| 136 chrome_async_socket_.SignalRead.disconnect(this); |
| 137 chrome_async_socket_.SignalError.disconnect(this); |
| 138 } |
| 139 |
| 140 enum Signal { |
| 141 SIGNAL_CONNECT, |
| 142 SIGNAL_SSL_CONNECT, |
| 143 SIGNAL_CLOSE, |
| 144 SIGNAL_READ, |
| 145 SIGNAL_ERROR, |
| 146 }; |
| 147 |
| 148 // Helper struct that records the state at the time of a signal. |
| 149 |
| 150 struct SignalSocketState { |
| 151 SignalSocketState() |
| 152 : signal(SIGNAL_ERROR), |
| 153 state(ChromeAsyncSocket::STATE_CLOSED), |
| 154 error(ChromeAsyncSocket::ERROR_NONE), |
| 155 net_error(net::OK) {} |
| 156 |
| 157 SignalSocketState( |
| 158 Signal signal, |
| 159 ChromeAsyncSocket::State state, |
| 160 ChromeAsyncSocket::Error error, |
| 161 net::Error net_error) |
| 162 : signal(signal), |
| 163 state(state), |
| 164 error(error), |
| 165 net_error(net_error) {} |
| 166 |
| 167 bool IsEqual(const SignalSocketState& other) const { |
| 168 return |
| 169 (signal == other.signal) && |
| 170 (state == other.state) && |
| 171 (error == other.error) && |
| 172 (net_error == other.net_error); |
| 173 } |
| 174 |
| 175 static SignalSocketState FromAsyncSocket( |
| 176 Signal signal, |
| 177 buzz::AsyncSocket* async_socket) { |
| 178 return SignalSocketState(signal, |
| 179 async_socket->state(), |
| 180 async_socket->error(), |
| 181 static_cast<net::Error>( |
| 182 async_socket->GetError())); |
| 183 } |
| 184 |
| 185 static SignalSocketState NoError( |
| 186 Signal signal, buzz::AsyncSocket::State state) { |
| 187 return SignalSocketState(signal, state, |
| 188 buzz::AsyncSocket::ERROR_NONE, |
| 189 net::OK); |
| 190 } |
| 191 |
| 192 Signal signal; |
| 193 ChromeAsyncSocket::State state; |
| 194 ChromeAsyncSocket::Error error; |
| 195 net::Error net_error; |
| 196 }; |
| 197 |
| 198 void CaptureSocketState(Signal signal) { |
| 199 signal_socket_states_.push_back( |
| 200 SignalSocketState::FromAsyncSocket(signal, &chrome_async_socket_)); |
| 201 } |
| 202 |
| 203 void OnConnect() { |
| 204 CaptureSocketState(SIGNAL_CONNECT); |
| 205 } |
| 206 |
| 207 void OnSSLConnect() { |
| 208 CaptureSocketState(SIGNAL_SSL_CONNECT); |
| 209 } |
| 210 |
| 211 void OnClose() { |
| 212 CaptureSocketState(SIGNAL_CLOSE); |
| 213 } |
| 214 |
| 215 void OnRead() { |
| 216 CaptureSocketState(SIGNAL_READ); |
| 217 } |
| 218 |
| 219 void OnError() { |
| 220 ADD_FAILURE(); |
| 221 } |
| 222 |
| 223 // State expect functions. |
| 224 |
| 225 void ExpectState(ChromeAsyncSocket::State state, |
| 226 ChromeAsyncSocket::Error error, |
| 227 net::Error net_error) { |
| 228 EXPECT_EQ(state, chrome_async_socket_.state()); |
| 229 EXPECT_EQ(error, chrome_async_socket_.error()); |
| 230 EXPECT_EQ(net_error, chrome_async_socket_.GetError()); |
| 231 } |
| 232 |
| 233 void ExpectNonErrorState(ChromeAsyncSocket::State state) { |
| 234 ExpectState(state, ChromeAsyncSocket::ERROR_NONE, net::OK); |
| 235 } |
| 236 |
| 237 void ExpectErrorState(ChromeAsyncSocket::State state, |
| 238 ChromeAsyncSocket::Error error) { |
| 239 ExpectState(state, error, net::OK); |
| 240 } |
| 241 |
| 242 void ExpectClosed() { |
| 243 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED); |
| 244 } |
| 245 |
| 246 // Signal expect functions. |
| 247 |
| 248 void ExpectNoSignal() { |
| 249 if (!signal_socket_states_.empty()) { |
| 250 ADD_FAILURE() << signal_socket_states_.front().signal; |
| 251 } |
| 252 } |
| 253 |
| 254 void ExpectSignalSocketState( |
| 255 SignalSocketState expected_signal_socket_state) { |
| 256 if (signal_socket_states_.empty()) { |
| 257 ADD_FAILURE() << expected_signal_socket_state.signal; |
| 258 return; |
| 259 } |
| 260 EXPECT_TRUE(expected_signal_socket_state.IsEqual( |
| 261 signal_socket_states_.front())) |
| 262 << signal_socket_states_.front().signal; |
| 263 signal_socket_states_.pop_front(); |
| 264 } |
| 265 |
| 266 void ExpectReadSignal() { |
| 267 ExpectSignalSocketState( |
| 268 SignalSocketState::NoError( |
| 269 SIGNAL_READ, ChromeAsyncSocket::STATE_OPEN)); |
| 270 } |
| 271 |
| 272 void ExpectSSLConnectSignal() { |
| 273 ExpectSignalSocketState( |
| 274 SignalSocketState::NoError(SIGNAL_SSL_CONNECT, |
| 275 ChromeAsyncSocket::STATE_TLS_OPEN)); |
| 276 } |
| 277 |
| 278 void ExpectSSLReadSignal() { |
| 279 ExpectSignalSocketState( |
| 280 SignalSocketState::NoError( |
| 281 SIGNAL_READ, ChromeAsyncSocket::STATE_TLS_OPEN)); |
| 282 } |
| 283 |
| 284 // Open/close utility functions. |
| 285 |
| 286 void DoOpenClosed() { |
| 287 ExpectClosed(); |
| 288 async_socket_data_provider_.set_connect_data( |
| 289 net::MockConnect(false, net::OK)); |
| 290 EXPECT_TRUE(chrome_async_socket_.Connect(addr_)); |
| 291 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING); |
| 292 |
| 293 message_loop_.RunAllPending(); |
| 294 // We may not necessarily be open; may have been other events |
| 295 // queued up. |
| 296 ExpectSignalSocketState( |
| 297 SignalSocketState::NoError( |
| 298 SIGNAL_CONNECT, ChromeAsyncSocket::STATE_OPEN)); |
| 299 } |
| 300 |
| 301 void DoCloseOpened(SignalSocketState expected_signal_socket_state) { |
| 302 // We may be in an error state, so just compare state(). |
| 303 EXPECT_EQ(ChromeAsyncSocket::STATE_OPEN, chrome_async_socket_.state()); |
| 304 EXPECT_TRUE(chrome_async_socket_.Close()); |
| 305 ExpectSignalSocketState(expected_signal_socket_state); |
| 306 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED); |
| 307 } |
| 308 |
| 309 void DoCloseOpenedNoError() { |
| 310 DoCloseOpened( |
| 311 SignalSocketState::NoError( |
| 312 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED)); |
| 313 } |
| 314 |
| 315 void DoSSLOpenClosed() { |
| 316 const char kDummyData[] = "dummy_data"; |
| 317 async_socket_data_provider_.AddRead(net::MockRead(kDummyData)); |
| 318 DoOpenClosed(); |
| 319 ExpectReadSignal(); |
| 320 EXPECT_EQ(kDummyData, DrainRead(1)); |
| 321 |
| 322 EXPECT_TRUE(chrome_async_socket_.StartTls("fakedomain.com")); |
| 323 message_loop_.RunAllPending(); |
| 324 ExpectSSLConnectSignal(); |
| 325 ExpectNoSignal(); |
| 326 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN); |
| 327 } |
| 328 |
| 329 void DoSSLCloseOpened(SignalSocketState expected_signal_socket_state) { |
| 330 // We may be in an error state, so just compare state(). |
| 331 EXPECT_EQ(ChromeAsyncSocket::STATE_TLS_OPEN, |
| 332 chrome_async_socket_.state()); |
| 333 EXPECT_TRUE(chrome_async_socket_.Close()); |
| 334 ExpectSignalSocketState(expected_signal_socket_state); |
| 335 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED); |
| 336 } |
| 337 |
| 338 void DoSSLCloseOpenedNoError() { |
| 339 DoSSLCloseOpened( |
| 340 SignalSocketState::NoError( |
| 341 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED)); |
| 342 } |
| 343 |
| 344 // Read utility fucntions. |
| 345 |
| 346 std::string DrainRead(size_t buf_size) { |
| 347 std::string read; |
| 348 scoped_array<char> buf(new char[buf_size]); |
| 349 size_t len_read; |
| 350 while (true) { |
| 351 bool success = |
| 352 chrome_async_socket_.Read(buf.get(), buf_size, &len_read); |
| 353 if (!success) { |
| 354 ADD_FAILURE(); |
| 355 break; |
| 356 } |
| 357 if (len_read == 0) { |
| 358 break; |
| 359 } |
| 360 read.append(buf.get(), len_read); |
| 361 } |
| 362 return read; |
| 363 } |
| 364 |
| 365 // ChromeAsyncSocket expects a message loop. |
| 366 MessageLoop message_loop_; |
| 367 |
| 368 net::MockClientSocketFactory mock_client_socket_factory_; |
| 369 AsyncSocketDataProvider async_socket_data_provider_; |
| 370 net::SSLSocketDataProvider ssl_socket_data_provider_; |
| 371 |
| 372 net::CapturingNetLog capturing_net_log_; |
| 373 net::SSLConfig ssl_config_; |
| 374 ChromeAsyncSocket chrome_async_socket_; |
| 375 std::deque<SignalSocketState> signal_socket_states_; |
| 376 const talk_base::SocketAddress addr_; |
| 377 |
| 378 private: |
| 379 DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocketTest); |
| 380 }; |
| 381 |
| 382 TEST_F(ChromeAsyncSocketTest, InitialState) { |
| 383 ExpectClosed(); |
| 384 ExpectNoSignal(); |
| 385 } |
| 386 |
| 387 TEST_F(ChromeAsyncSocketTest, EmptyClose) { |
| 388 ExpectClosed(); |
| 389 EXPECT_TRUE(chrome_async_socket_.Close()); |
| 390 ExpectClosed(); |
| 391 } |
| 392 |
| 393 TEST_F(ChromeAsyncSocketTest, ImmediateConnectAndClose) { |
| 394 DoOpenClosed(); |
| 395 |
| 396 ExpectNonErrorState(ChromeAsyncSocket::STATE_OPEN); |
| 397 |
| 398 DoCloseOpenedNoError(); |
| 399 } |
| 400 |
| 401 // After this, no need to test immediate successful connect and |
| 402 // Close() so thoroughly. |
| 403 |
| 404 TEST_F(ChromeAsyncSocketTest, DoubleClose) { |
| 405 DoOpenClosed(); |
| 406 |
| 407 EXPECT_TRUE(chrome_async_socket_.Close()); |
| 408 ExpectClosed(); |
| 409 ExpectSignalSocketState( |
| 410 SignalSocketState::NoError( |
| 411 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED)); |
| 412 |
| 413 EXPECT_TRUE(chrome_async_socket_.Close()); |
| 414 ExpectClosed(); |
| 415 } |
| 416 |
| 417 TEST_F(ChromeAsyncSocketTest, UnresolvedConnect) { |
| 418 const talk_base::SocketAddress unresolved_addr(0, 0); |
| 419 EXPECT_FALSE(chrome_async_socket_.Connect(unresolved_addr)); |
| 420 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED, |
| 421 ChromeAsyncSocket::ERROR_DNS); |
| 422 |
| 423 EXPECT_TRUE(chrome_async_socket_.Close()); |
| 424 ExpectClosed(); |
| 425 } |
| 426 |
| 427 TEST_F(ChromeAsyncSocketTest, DoubleConnect) { |
| 428 EXPECT_DEBUG_DEATH({ |
| 429 DoOpenClosed(); |
| 430 |
| 431 EXPECT_FALSE(chrome_async_socket_.Connect(addr_)); |
| 432 ExpectErrorState(ChromeAsyncSocket::STATE_OPEN, |
| 433 ChromeAsyncSocket::ERROR_WRONGSTATE); |
| 434 |
| 435 DoCloseOpened( |
| 436 SignalSocketState(SIGNAL_CLOSE, |
| 437 ChromeAsyncSocket::STATE_CLOSED, |
| 438 ChromeAsyncSocket::ERROR_WRONGSTATE, |
| 439 net::OK)); |
| 440 }, "non-closed socket"); |
| 441 } |
| 442 |
| 443 TEST_F(ChromeAsyncSocketTest, ImmediateConnectCloseBeforeRead) { |
| 444 DoOpenClosed(); |
| 445 |
| 446 EXPECT_TRUE(chrome_async_socket_.Close()); |
| 447 ExpectClosed(); |
| 448 ExpectSignalSocketState( |
| 449 SignalSocketState::NoError( |
| 450 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED)); |
| 451 |
| 452 message_loop_.RunAllPending(); |
| 453 } |
| 454 |
| 455 TEST_F(ChromeAsyncSocketTest, HangingConnect) { |
| 456 EXPECT_TRUE(chrome_async_socket_.Connect(addr_)); |
| 457 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING); |
| 458 ExpectNoSignal(); |
| 459 |
| 460 EXPECT_TRUE(chrome_async_socket_.Close()); |
| 461 ExpectClosed(); |
| 462 ExpectSignalSocketState( |
| 463 SignalSocketState::NoError( |
| 464 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED)); |
| 465 } |
| 466 |
| 467 TEST_F(ChromeAsyncSocketTest, PendingConnect) { |
| 468 async_socket_data_provider_.set_connect_data( |
| 469 net::MockConnect(true, net::OK)); |
| 470 EXPECT_TRUE(chrome_async_socket_.Connect(addr_)); |
| 471 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING); |
| 472 ExpectNoSignal(); |
| 473 |
| 474 message_loop_.RunAllPending(); |
| 475 ExpectNonErrorState(ChromeAsyncSocket::STATE_OPEN); |
| 476 ExpectSignalSocketState( |
| 477 SignalSocketState::NoError( |
| 478 SIGNAL_CONNECT, ChromeAsyncSocket::STATE_OPEN)); |
| 479 ExpectNoSignal(); |
| 480 |
| 481 message_loop_.RunAllPending(); |
| 482 |
| 483 DoCloseOpenedNoError(); |
| 484 } |
| 485 |
| 486 // After this no need to test successful pending connect so |
| 487 // thoroughly. |
| 488 |
| 489 TEST_F(ChromeAsyncSocketTest, PendingConnectCloseBeforeRead) { |
| 490 async_socket_data_provider_.set_connect_data( |
| 491 net::MockConnect(true, net::OK)); |
| 492 EXPECT_TRUE(chrome_async_socket_.Connect(addr_)); |
| 493 |
| 494 message_loop_.RunAllPending(); |
| 495 ExpectSignalSocketState( |
| 496 SignalSocketState::NoError( |
| 497 SIGNAL_CONNECT, ChromeAsyncSocket::STATE_OPEN)); |
| 498 |
| 499 DoCloseOpenedNoError(); |
| 500 |
| 501 message_loop_.RunAllPending(); |
| 502 } |
| 503 |
| 504 TEST_F(ChromeAsyncSocketTest, PendingConnectError) { |
| 505 async_socket_data_provider_.set_connect_data( |
| 506 net::MockConnect(true, net::ERR_TIMED_OUT)); |
| 507 EXPECT_TRUE(chrome_async_socket_.Connect(addr_)); |
| 508 |
| 509 message_loop_.RunAllPending(); |
| 510 |
| 511 ExpectSignalSocketState( |
| 512 SignalSocketState( |
| 513 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED, |
| 514 ChromeAsyncSocket::ERROR_WINSOCK, net::ERR_TIMED_OUT)); |
| 515 } |
| 516 |
| 517 // After this we can assume Connect() and Close() work as expected. |
| 518 |
| 519 TEST_F(ChromeAsyncSocketTest, EmptyRead) { |
| 520 DoOpenClosed(); |
| 521 |
| 522 char buf[4096]; |
| 523 size_t len_read = 10000; |
| 524 EXPECT_TRUE(chrome_async_socket_.Read(buf, sizeof(buf), &len_read)); |
| 525 EXPECT_EQ(0, len_read); |
| 526 |
| 527 DoCloseOpenedNoError(); |
| 528 } |
| 529 |
| 530 TEST_F(ChromeAsyncSocketTest, WrongRead) { |
| 531 EXPECT_DEBUG_DEATH({ |
| 532 char buf[4096]; |
| 533 size_t len_read; |
| 534 EXPECT_FALSE(chrome_async_socket_.Read(buf, sizeof(buf), &len_read)); |
| 535 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED, |
| 536 ChromeAsyncSocket::ERROR_WRONGSTATE); |
| 537 EXPECT_TRUE(chrome_async_socket_.Close()); |
| 538 }, "non-open"); |
| 539 } |
| 540 |
| 541 const char kReadData[] = "mydatatoread"; |
| 542 |
| 543 TEST_F(ChromeAsyncSocketTest, Read) { |
| 544 async_socket_data_provider_.AddRead(net::MockRead(kReadData)); |
| 545 DoOpenClosed(); |
| 546 |
| 547 ExpectReadSignal(); |
| 548 ExpectNoSignal(); |
| 549 |
| 550 EXPECT_EQ(kReadData, DrainRead(1)); |
| 551 |
| 552 message_loop_.RunAllPending(); |
| 553 |
| 554 DoCloseOpenedNoError(); |
| 555 } |
| 556 |
| 557 TEST_F(ChromeAsyncSocketTest, ReadTwice) { |
| 558 async_socket_data_provider_.AddRead(net::MockRead(kReadData)); |
| 559 DoOpenClosed(); |
| 560 |
| 561 ExpectReadSignal(); |
| 562 ExpectNoSignal(); |
| 563 |
| 564 EXPECT_EQ(kReadData, DrainRead(1)); |
| 565 |
| 566 message_loop_.RunAllPending(); |
| 567 |
| 568 const char kReadData2[] = "mydatatoread2"; |
| 569 async_socket_data_provider_.AddRead(net::MockRead(kReadData2)); |
| 570 |
| 571 ExpectReadSignal(); |
| 572 ExpectNoSignal(); |
| 573 |
| 574 EXPECT_EQ(kReadData2, DrainRead(1)); |
| 575 |
| 576 DoCloseOpenedNoError(); |
| 577 } |
| 578 |
| 579 TEST_F(ChromeAsyncSocketTest, ReadError) { |
| 580 async_socket_data_provider_.AddRead(net::MockRead(kReadData)); |
| 581 DoOpenClosed(); |
| 582 |
| 583 ExpectReadSignal(); |
| 584 ExpectNoSignal(); |
| 585 |
| 586 EXPECT_EQ(kReadData, DrainRead(1)); |
| 587 |
| 588 message_loop_.RunAllPending(); |
| 589 |
| 590 async_socket_data_provider_.AddRead( |
| 591 net::MockRead(false, net::ERR_TIMED_OUT)); |
| 592 |
| 593 ExpectSignalSocketState( |
| 594 SignalSocketState( |
| 595 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED, |
| 596 ChromeAsyncSocket::ERROR_WINSOCK, net::ERR_TIMED_OUT)); |
| 597 } |
| 598 |
| 599 TEST_F(ChromeAsyncSocketTest, ReadEmpty) { |
| 600 async_socket_data_provider_.AddRead(net::MockRead("")); |
| 601 DoOpenClosed(); |
| 602 |
| 603 ExpectSignalSocketState( |
| 604 SignalSocketState::NoError( |
| 605 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED)); |
| 606 } |
| 607 |
| 608 TEST_F(ChromeAsyncSocketTest, PendingRead) { |
| 609 DoOpenClosed(); |
| 610 |
| 611 ExpectNoSignal(); |
| 612 |
| 613 async_socket_data_provider_.AddRead(net::MockRead(kReadData)); |
| 614 |
| 615 ExpectSignalSocketState( |
| 616 SignalSocketState::NoError( |
| 617 SIGNAL_READ, ChromeAsyncSocket::STATE_OPEN)); |
| 618 ExpectNoSignal(); |
| 619 |
| 620 EXPECT_EQ(kReadData, DrainRead(1)); |
| 621 |
| 622 message_loop_.RunAllPending(); |
| 623 |
| 624 DoCloseOpenedNoError(); |
| 625 } |
| 626 |
| 627 TEST_F(ChromeAsyncSocketTest, PendingEmptyRead) { |
| 628 DoOpenClosed(); |
| 629 |
| 630 ExpectNoSignal(); |
| 631 |
| 632 async_socket_data_provider_.AddRead(net::MockRead("")); |
| 633 |
| 634 ExpectSignalSocketState( |
| 635 SignalSocketState::NoError( |
| 636 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED)); |
| 637 } |
| 638 |
| 639 TEST_F(ChromeAsyncSocketTest, PendingReadError) { |
| 640 DoOpenClosed(); |
| 641 |
| 642 ExpectNoSignal(); |
| 643 |
| 644 async_socket_data_provider_.AddRead( |
| 645 net::MockRead(true, net::ERR_TIMED_OUT)); |
| 646 |
| 647 ExpectSignalSocketState( |
| 648 SignalSocketState( |
| 649 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED, |
| 650 ChromeAsyncSocket::ERROR_WINSOCK, net::ERR_TIMED_OUT)); |
| 651 } |
| 652 |
| 653 // After this we can assume non-SSL Read() works as expected. |
| 654 |
| 655 TEST_F(ChromeAsyncSocketTest, WrongWrite) { |
| 656 EXPECT_DEBUG_DEATH({ |
| 657 std::string data("foo"); |
| 658 EXPECT_FALSE(chrome_async_socket_.Write(data.data(), data.size())); |
| 659 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED, |
| 660 ChromeAsyncSocket::ERROR_WRONGSTATE); |
| 661 EXPECT_TRUE(chrome_async_socket_.Close()); |
| 662 }, "non-open"); |
| 663 } |
| 664 |
| 665 const char kWriteData[] = "mydatatowrite"; |
| 666 |
| 667 TEST_F(ChromeAsyncSocketTest, SyncWrite) { |
| 668 async_socket_data_provider_.AddWrite( |
| 669 net::MockWrite(false, kWriteData, 3)); |
| 670 async_socket_data_provider_.AddWrite( |
| 671 net::MockWrite(false, kWriteData + 3, 5)); |
| 672 async_socket_data_provider_.AddWrite( |
| 673 net::MockWrite(false, kWriteData + 8, arraysize(kWriteData) - 8)); |
| 674 DoOpenClosed(); |
| 675 |
| 676 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData, 3)); |
| 677 message_loop_.RunAllPending(); |
| 678 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData + 3, 5)); |
| 679 message_loop_.RunAllPending(); |
| 680 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData + 8, |
| 681 arraysize(kWriteData) - 8)); |
| 682 message_loop_.RunAllPending(); |
| 683 |
| 684 ExpectNoSignal(); |
| 685 |
| 686 DoCloseOpenedNoError(); |
| 687 } |
| 688 |
| 689 TEST_F(ChromeAsyncSocketTest, AsyncWrite) { |
| 690 DoOpenClosed(); |
| 691 |
| 692 async_socket_data_provider_.AddWrite( |
| 693 net::MockWrite(true, kWriteData, 3)); |
| 694 async_socket_data_provider_.AddWrite( |
| 695 net::MockWrite(true, kWriteData + 3, 5)); |
| 696 async_socket_data_provider_.AddWrite( |
| 697 net::MockWrite(true, kWriteData + 8, arraysize(kWriteData) - 8)); |
| 698 |
| 699 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData, 3)); |
| 700 message_loop_.RunAllPending(); |
| 701 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData + 3, 5)); |
| 702 message_loop_.RunAllPending(); |
| 703 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData + 8, |
| 704 arraysize(kWriteData) - 8)); |
| 705 message_loop_.RunAllPending(); |
| 706 |
| 707 ExpectNoSignal(); |
| 708 |
| 709 DoCloseOpenedNoError(); |
| 710 } |
| 711 |
| 712 TEST_F(ChromeAsyncSocketTest, AsyncWriteError) { |
| 713 DoOpenClosed(); |
| 714 |
| 715 async_socket_data_provider_.AddWrite( |
| 716 net::MockWrite(true, kWriteData, 3)); |
| 717 async_socket_data_provider_.AddWrite( |
| 718 net::MockWrite(true, kWriteData + 3, 5)); |
| 719 async_socket_data_provider_.AddWrite( |
| 720 net::MockWrite(true, net::ERR_TIMED_OUT)); |
| 721 |
| 722 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData, 3)); |
| 723 message_loop_.RunAllPending(); |
| 724 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData + 3, 5)); |
| 725 message_loop_.RunAllPending(); |
| 726 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData + 8, |
| 727 arraysize(kWriteData) - 8)); |
| 728 message_loop_.RunAllPending(); |
| 729 |
| 730 ExpectSignalSocketState( |
| 731 SignalSocketState( |
| 732 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED, |
| 733 ChromeAsyncSocket::ERROR_WINSOCK, net::ERR_TIMED_OUT)); |
| 734 } |
| 735 |
| 736 TEST_F(ChromeAsyncSocketTest, LargeWrite) { |
| 737 EXPECT_DEBUG_DEATH({ |
| 738 DoOpenClosed(); |
| 739 |
| 740 std::string large_data(100, 'x'); |
| 741 EXPECT_FALSE(chrome_async_socket_.Write(large_data.data(), |
| 742 large_data.size())); |
| 743 ExpectState(ChromeAsyncSocket::STATE_OPEN, |
| 744 ChromeAsyncSocket::ERROR_WINSOCK, |
| 745 net::ERR_INSUFFICIENT_RESOURCES); |
| 746 DoCloseOpened( |
| 747 SignalSocketState( |
| 748 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED, |
| 749 ChromeAsyncSocket::ERROR_WINSOCK, |
| 750 net::ERR_INSUFFICIENT_RESOURCES)); |
| 751 }, "exceed the max write buffer"); |
| 752 } |
| 753 |
| 754 TEST_F(ChromeAsyncSocketTest, LargeAccumulatedWrite) { |
| 755 EXPECT_DEBUG_DEATH({ |
| 756 DoOpenClosed(); |
| 757 |
| 758 std::string data(15, 'x'); |
| 759 EXPECT_TRUE(chrome_async_socket_.Write(data.data(), data.size())); |
| 760 EXPECT_FALSE(chrome_async_socket_.Write(data.data(), data.size())); |
| 761 ExpectState(ChromeAsyncSocket::STATE_OPEN, |
| 762 ChromeAsyncSocket::ERROR_WINSOCK, |
| 763 net::ERR_INSUFFICIENT_RESOURCES); |
| 764 DoCloseOpened( |
| 765 SignalSocketState( |
| 766 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED, |
| 767 ChromeAsyncSocket::ERROR_WINSOCK, |
| 768 net::ERR_INSUFFICIENT_RESOURCES)); |
| 769 }, "exceed the max write buffer"); |
| 770 } |
| 771 |
| 772 // After this we can assume non-SSL I/O works as expected. |
| 773 |
| 774 TEST_F(ChromeAsyncSocketTest, HangingSSLConnect) { |
| 775 async_socket_data_provider_.AddRead(net::MockRead(kReadData)); |
| 776 DoOpenClosed(); |
| 777 ExpectReadSignal(); |
| 778 |
| 779 EXPECT_TRUE(chrome_async_socket_.StartTls("fakedomain.com")); |
| 780 ExpectNoSignal(); |
| 781 |
| 782 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_CONNECTING); |
| 783 EXPECT_TRUE(chrome_async_socket_.Close()); |
| 784 ExpectSignalSocketState( |
| 785 SignalSocketState::NoError(SIGNAL_CLOSE, |
| 786 ChromeAsyncSocket::STATE_CLOSED)); |
| 787 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED); |
| 788 } |
| 789 |
| 790 TEST_F(ChromeAsyncSocketTest, ImmediateSSLConnect) { |
| 791 async_socket_data_provider_.AddRead(net::MockRead(kReadData)); |
| 792 DoOpenClosed(); |
| 793 ExpectReadSignal(); |
| 794 |
| 795 EXPECT_TRUE(chrome_async_socket_.StartTls("fakedomain.com")); |
| 796 message_loop_.RunAllPending(); |
| 797 ExpectSSLConnectSignal(); |
| 798 ExpectNoSignal(); |
| 799 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN); |
| 800 |
| 801 DoSSLCloseOpenedNoError(); |
| 802 } |
| 803 |
| 804 TEST_F(ChromeAsyncSocketTest, DoubleSSLConnect) { |
| 805 EXPECT_DEBUG_DEATH({ |
| 806 async_socket_data_provider_.AddRead(net::MockRead(kReadData)); |
| 807 DoOpenClosed(); |
| 808 ExpectReadSignal(); |
| 809 |
| 810 EXPECT_TRUE(chrome_async_socket_.StartTls("fakedomain.com")); |
| 811 message_loop_.RunAllPending(); |
| 812 ExpectSSLConnectSignal(); |
| 813 ExpectNoSignal(); |
| 814 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN); |
| 815 |
| 816 EXPECT_FALSE(chrome_async_socket_.StartTls("fakedomain.com")); |
| 817 |
| 818 DoSSLCloseOpened( |
| 819 SignalSocketState(SIGNAL_CLOSE, |
| 820 ChromeAsyncSocket::STATE_CLOSED, |
| 821 ChromeAsyncSocket::ERROR_WRONGSTATE, |
| 822 net::OK)); |
| 823 }, "wrong state"); |
| 824 } |
| 825 |
| 826 TEST_F(ChromeAsyncSocketTest, ReadDuringSSLConnecting) { |
| 827 async_socket_data_provider_.AddRead(net::MockRead(kReadData)); |
| 828 DoOpenClosed(); |
| 829 ExpectReadSignal(); |
| 830 EXPECT_EQ(kReadData, DrainRead(1)); |
| 831 |
| 832 EXPECT_TRUE(chrome_async_socket_.StartTls("fakedomain.com")); |
| 833 ExpectNoSignal(); |
| 834 |
| 835 // Shouldn't do anything. |
| 836 async_socket_data_provider_.AddRead(net::MockRead(kReadData)); |
| 837 |
| 838 char buf[4096]; |
| 839 size_t len_read = 10000; |
| 840 EXPECT_TRUE(chrome_async_socket_.Read(buf, sizeof(buf), &len_read)); |
| 841 EXPECT_EQ(0, len_read); |
| 842 |
| 843 message_loop_.RunAllPending(); |
| 844 ExpectSSLConnectSignal(); |
| 845 ExpectSSLReadSignal(); |
| 846 ExpectNoSignal(); |
| 847 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN); |
| 848 |
| 849 len_read = 10000; |
| 850 EXPECT_TRUE(chrome_async_socket_.Read(buf, sizeof(buf), &len_read)); |
| 851 EXPECT_EQ(kReadData, std::string(buf, len_read)); |
| 852 |
| 853 DoSSLCloseOpenedNoError(); |
| 854 } |
| 855 |
| 856 TEST_F(ChromeAsyncSocketTest, WriteDuringSSLConnecting) { |
| 857 async_socket_data_provider_.AddRead(net::MockRead(kReadData)); |
| 858 DoOpenClosed(); |
| 859 ExpectReadSignal(); |
| 860 |
| 861 EXPECT_TRUE(chrome_async_socket_.StartTls("fakedomain.com")); |
| 862 ExpectNoSignal(); |
| 863 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_CONNECTING); |
| 864 |
| 865 async_socket_data_provider_.AddWrite( |
| 866 net::MockWrite(true, kWriteData, 3)); |
| 867 |
| 868 // Shouldn't do anything. |
| 869 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData, 3)); |
| 870 |
| 871 // TODO(akalin): Figure out how to test that the write happens |
| 872 // *after* the SSL connect. |
| 873 |
| 874 message_loop_.RunAllPending(); |
| 875 ExpectSSLConnectSignal(); |
| 876 ExpectNoSignal(); |
| 877 |
| 878 message_loop_.RunAllPending(); |
| 879 |
| 880 DoSSLCloseOpenedNoError(); |
| 881 } |
| 882 |
| 883 TEST_F(ChromeAsyncSocketTest, SSLConnectDuringPendingRead) { |
| 884 EXPECT_DEBUG_DEATH({ |
| 885 DoOpenClosed(); |
| 886 |
| 887 EXPECT_FALSE(chrome_async_socket_.StartTls("fakedomain.com")); |
| 888 |
| 889 DoCloseOpened( |
| 890 SignalSocketState(SIGNAL_CLOSE, |
| 891 ChromeAsyncSocket::STATE_CLOSED, |
| 892 ChromeAsyncSocket::ERROR_WRONGSTATE, |
| 893 net::OK)); |
| 894 }, "wrong state"); |
| 895 } |
| 896 |
| 897 TEST_F(ChromeAsyncSocketTest, SSLConnectDuringPostedWrite) { |
| 898 EXPECT_DEBUG_DEATH({ |
| 899 DoOpenClosed(); |
| 900 |
| 901 async_socket_data_provider_.AddWrite( |
| 902 net::MockWrite(true, kWriteData, 3)); |
| 903 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData, 3)); |
| 904 |
| 905 EXPECT_FALSE(chrome_async_socket_.StartTls("fakedomain.com")); |
| 906 |
| 907 message_loop_.RunAllPending(); |
| 908 |
| 909 DoCloseOpened( |
| 910 SignalSocketState(SIGNAL_CLOSE, |
| 911 ChromeAsyncSocket::STATE_CLOSED, |
| 912 ChromeAsyncSocket::ERROR_WRONGSTATE, |
| 913 net::OK)); |
| 914 }, "wrong state"); |
| 915 } |
| 916 |
| 917 // After this we can assume SSL connect works as expected. |
| 918 |
| 919 TEST_F(ChromeAsyncSocketTest, SSLRead) { |
| 920 DoSSLOpenClosed(); |
| 921 async_socket_data_provider_.AddRead(net::MockRead(kReadData)); |
| 922 message_loop_.RunAllPending(); |
| 923 |
| 924 ExpectSSLReadSignal(); |
| 925 ExpectNoSignal(); |
| 926 |
| 927 EXPECT_EQ(kReadData, DrainRead(1)); |
| 928 |
| 929 message_loop_.RunAllPending(); |
| 930 |
| 931 DoSSLCloseOpenedNoError(); |
| 932 } |
| 933 |
| 934 TEST_F(ChromeAsyncSocketTest, SSLSyncWrite) { |
| 935 async_socket_data_provider_.AddWrite( |
| 936 net::MockWrite(false, kWriteData, 3)); |
| 937 async_socket_data_provider_.AddWrite( |
| 938 net::MockWrite(false, kWriteData + 3, 5)); |
| 939 async_socket_data_provider_.AddWrite( |
| 940 net::MockWrite(false, kWriteData + 8, arraysize(kWriteData) - 8)); |
| 941 DoSSLOpenClosed(); |
| 942 |
| 943 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData, 3)); |
| 944 message_loop_.RunAllPending(); |
| 945 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData + 3, 5)); |
| 946 message_loop_.RunAllPending(); |
| 947 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData + 8, |
| 948 arraysize(kWriteData) - 8)); |
| 949 message_loop_.RunAllPending(); |
| 950 |
| 951 ExpectNoSignal(); |
| 952 |
| 953 DoSSLCloseOpenedNoError(); |
| 954 } |
| 955 |
| 956 TEST_F(ChromeAsyncSocketTest, SSLAsyncWrite) { |
| 957 DoSSLOpenClosed(); |
| 958 |
| 959 async_socket_data_provider_.AddWrite( |
| 960 net::MockWrite(true, kWriteData, 3)); |
| 961 async_socket_data_provider_.AddWrite( |
| 962 net::MockWrite(true, kWriteData + 3, 5)); |
| 963 async_socket_data_provider_.AddWrite( |
| 964 net::MockWrite(true, kWriteData + 8, arraysize(kWriteData) - 8)); |
| 965 |
| 966 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData, 3)); |
| 967 message_loop_.RunAllPending(); |
| 968 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData + 3, 5)); |
| 969 message_loop_.RunAllPending(); |
| 970 EXPECT_TRUE(chrome_async_socket_.Write(kWriteData + 8, |
| 971 arraysize(kWriteData) - 8)); |
| 972 message_loop_.RunAllPending(); |
| 973 |
| 974 ExpectNoSignal(); |
| 975 |
| 976 DoSSLCloseOpenedNoError(); |
| 977 } |
| 978 |
| 979 } // namespace |
| 980 |
| 981 } // namespace sync_tools |
OLD | NEW |