Revert 271602 "Implementation of leveldb-backed PrefStore."
[chromium-blink-merge.git] / jingle / glue / chrome_async_socket_unittest.cc
blobdb3d2b09c9ffd6a60cf631264b35c08436c4e558
1 // Copyright (c) 2012 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.
5 #include "jingle/glue/chrome_async_socket.h"
7 #include <deque>
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "jingle/glue/resolving_client_socket_factory.h"
15 #include "net/base/address_list.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/net_util.h"
18 #include "net/cert/mock_cert_verifier.h"
19 #include "net/http/transport_security_state.h"
20 #include "net/socket/socket_test_util.h"
21 #include "net/socket/ssl_client_socket.h"
22 #include "net/ssl/ssl_config_service.h"
23 #include "net/url_request/url_request_context_getter.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/libjingle/source/talk/base/ipaddress.h"
26 #include "third_party/libjingle/source/talk/base/sigslot.h"
27 #include "third_party/libjingle/source/talk/base/socketaddress.h"
29 namespace jingle_glue {
31 namespace {
33 // Data provider that handles reads/writes for ChromeAsyncSocket.
34 class AsyncSocketDataProvider : public net::SocketDataProvider {
35 public:
36 AsyncSocketDataProvider() : has_pending_read_(false) {}
38 virtual ~AsyncSocketDataProvider() {
39 EXPECT_TRUE(writes_.empty());
40 EXPECT_TRUE(reads_.empty());
43 // If there's no read, sets the "has pending read" flag. Otherwise,
44 // pops the next read.
45 virtual net::MockRead GetNextRead() OVERRIDE {
46 if (reads_.empty()) {
47 DCHECK(!has_pending_read_);
48 has_pending_read_ = true;
49 const net::MockRead pending_read(net::SYNCHRONOUS, net::ERR_IO_PENDING);
50 return pending_read;
52 net::MockRead mock_read = reads_.front();
53 reads_.pop_front();
54 return mock_read;
57 // Simply pops the next write and, if applicable, compares it to
58 // |data|.
59 virtual net::MockWriteResult OnWrite(const std::string& data) OVERRIDE {
60 DCHECK(!writes_.empty());
61 net::MockWrite mock_write = writes_.front();
62 writes_.pop_front();
63 if (mock_write.result != net::OK) {
64 return net::MockWriteResult(mock_write.mode, mock_write.result);
66 std::string expected_data(mock_write.data, mock_write.data_len);
67 EXPECT_EQ(expected_data, data);
68 if (expected_data != data) {
69 return net::MockWriteResult(net::SYNCHRONOUS, net::ERR_UNEXPECTED);
71 return net::MockWriteResult(mock_write.mode, data.size());
74 // We ignore resets so we can pre-load the socket data provider with
75 // read/write events.
76 virtual void Reset() OVERRIDE {}
78 // If there is a pending read, completes it with the given read.
79 // Otherwise, queues up the given read.
80 void AddRead(const net::MockRead& mock_read) {
81 DCHECK_NE(mock_read.result, net::ERR_IO_PENDING);
82 if (has_pending_read_) {
83 socket()->OnReadComplete(mock_read);
84 has_pending_read_ = false;
85 return;
87 reads_.push_back(mock_read);
90 // Simply queues up the given write.
91 void AddWrite(const net::MockWrite& mock_write) {
92 writes_.push_back(mock_write);
95 private:
96 std::deque<net::MockRead> reads_;
97 bool has_pending_read_;
99 std::deque<net::MockWrite> writes_;
101 DISALLOW_COPY_AND_ASSIGN(AsyncSocketDataProvider);
104 class MockXmppClientSocketFactory : public ResolvingClientSocketFactory {
105 public:
106 MockXmppClientSocketFactory(
107 net::ClientSocketFactory* mock_client_socket_factory,
108 const net::AddressList& address_list)
109 : mock_client_socket_factory_(mock_client_socket_factory),
110 address_list_(address_list),
111 cert_verifier_(new net::MockCertVerifier),
112 transport_security_state_(new net::TransportSecurityState) {
115 // ResolvingClientSocketFactory implementation.
116 virtual scoped_ptr<net::StreamSocket> CreateTransportClientSocket(
117 const net::HostPortPair& host_and_port) OVERRIDE {
118 return mock_client_socket_factory_->CreateTransportClientSocket(
119 address_list_, NULL, net::NetLog::Source());
122 virtual scoped_ptr<net::SSLClientSocket> CreateSSLClientSocket(
123 scoped_ptr<net::ClientSocketHandle> transport_socket,
124 const net::HostPortPair& host_and_port) OVERRIDE {
125 net::SSLClientSocketContext context;
126 context.cert_verifier = cert_verifier_.get();
127 context.transport_security_state = transport_security_state_.get();
128 return mock_client_socket_factory_->CreateSSLClientSocket(
129 transport_socket.Pass(), host_and_port, ssl_config_, context);
132 private:
133 scoped_ptr<net::ClientSocketFactory> mock_client_socket_factory_;
134 net::AddressList address_list_;
135 net::SSLConfig ssl_config_;
136 scoped_ptr<net::CertVerifier> cert_verifier_;
137 scoped_ptr<net::TransportSecurityState> transport_security_state_;
140 class ChromeAsyncSocketTest
141 : public testing::Test,
142 public sigslot::has_slots<> {
143 protected:
144 ChromeAsyncSocketTest()
145 : ssl_socket_data_provider_(net::ASYNC, net::OK),
146 addr_("localhost", 35) {}
148 virtual ~ChromeAsyncSocketTest() {}
150 virtual void SetUp() {
151 scoped_ptr<net::MockClientSocketFactory> mock_client_socket_factory(
152 new net::MockClientSocketFactory());
153 mock_client_socket_factory->AddSocketDataProvider(
154 &async_socket_data_provider_);
155 mock_client_socket_factory->AddSSLSocketDataProvider(
156 &ssl_socket_data_provider_);
158 // Fake DNS resolution for |addr_| and pass it to the factory.
159 net::IPAddressNumber resolved_addr;
160 EXPECT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &resolved_addr));
161 const net::AddressList address_list =
162 net::AddressList::CreateFromIPAddress(resolved_addr, addr_.port());
163 scoped_ptr<MockXmppClientSocketFactory> mock_xmpp_client_socket_factory(
164 new MockXmppClientSocketFactory(
165 mock_client_socket_factory.release(),
166 address_list));
167 chrome_async_socket_.reset(
168 new ChromeAsyncSocket(mock_xmpp_client_socket_factory.release(),
169 14, 20)),
171 chrome_async_socket_->SignalConnected.connect(
172 this, &ChromeAsyncSocketTest::OnConnect);
173 chrome_async_socket_->SignalSSLConnected.connect(
174 this, &ChromeAsyncSocketTest::OnSSLConnect);
175 chrome_async_socket_->SignalClosed.connect(
176 this, &ChromeAsyncSocketTest::OnClose);
177 chrome_async_socket_->SignalRead.connect(
178 this, &ChromeAsyncSocketTest::OnRead);
179 chrome_async_socket_->SignalError.connect(
180 this, &ChromeAsyncSocketTest::OnError);
183 virtual void TearDown() {
184 // Run any tasks that we forgot to pump.
185 message_loop_.RunUntilIdle();
186 ExpectClosed();
187 ExpectNoSignal();
188 chrome_async_socket_.reset();
191 enum Signal {
192 SIGNAL_CONNECT,
193 SIGNAL_SSL_CONNECT,
194 SIGNAL_CLOSE,
195 SIGNAL_READ,
196 SIGNAL_ERROR,
199 // Helper struct that records the state at the time of a signal.
201 struct SignalSocketState {
202 SignalSocketState()
203 : signal(SIGNAL_ERROR),
204 state(ChromeAsyncSocket::STATE_CLOSED),
205 error(ChromeAsyncSocket::ERROR_NONE),
206 net_error(net::OK) {}
208 SignalSocketState(
209 Signal signal,
210 ChromeAsyncSocket::State state,
211 ChromeAsyncSocket::Error error,
212 net::Error net_error)
213 : signal(signal),
214 state(state),
215 error(error),
216 net_error(net_error) {}
218 bool IsEqual(const SignalSocketState& other) const {
219 return
220 (signal == other.signal) &&
221 (state == other.state) &&
222 (error == other.error) &&
223 (net_error == other.net_error);
226 static SignalSocketState FromAsyncSocket(
227 Signal signal,
228 buzz::AsyncSocket* async_socket) {
229 return SignalSocketState(signal,
230 async_socket->state(),
231 async_socket->error(),
232 static_cast<net::Error>(
233 async_socket->GetError()));
236 static SignalSocketState NoError(
237 Signal signal, buzz::AsyncSocket::State state) {
238 return SignalSocketState(signal, state,
239 buzz::AsyncSocket::ERROR_NONE,
240 net::OK);
243 Signal signal;
244 ChromeAsyncSocket::State state;
245 ChromeAsyncSocket::Error error;
246 net::Error net_error;
249 void CaptureSocketState(Signal signal) {
250 signal_socket_states_.push_back(
251 SignalSocketState::FromAsyncSocket(
252 signal, chrome_async_socket_.get()));
255 void OnConnect() {
256 CaptureSocketState(SIGNAL_CONNECT);
259 void OnSSLConnect() {
260 CaptureSocketState(SIGNAL_SSL_CONNECT);
263 void OnClose() {
264 CaptureSocketState(SIGNAL_CLOSE);
267 void OnRead() {
268 CaptureSocketState(SIGNAL_READ);
271 void OnError() {
272 ADD_FAILURE();
275 // State expect functions.
277 void ExpectState(ChromeAsyncSocket::State state,
278 ChromeAsyncSocket::Error error,
279 net::Error net_error) {
280 EXPECT_EQ(state, chrome_async_socket_->state());
281 EXPECT_EQ(error, chrome_async_socket_->error());
282 EXPECT_EQ(net_error, chrome_async_socket_->GetError());
285 void ExpectNonErrorState(ChromeAsyncSocket::State state) {
286 ExpectState(state, ChromeAsyncSocket::ERROR_NONE, net::OK);
289 void ExpectErrorState(ChromeAsyncSocket::State state,
290 ChromeAsyncSocket::Error error) {
291 ExpectState(state, error, net::OK);
294 void ExpectClosed() {
295 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED);
298 // Signal expect functions.
300 void ExpectNoSignal() {
301 if (!signal_socket_states_.empty()) {
302 ADD_FAILURE() << signal_socket_states_.front().signal;
306 void ExpectSignalSocketState(
307 SignalSocketState expected_signal_socket_state) {
308 if (signal_socket_states_.empty()) {
309 ADD_FAILURE() << expected_signal_socket_state.signal;
310 return;
312 EXPECT_TRUE(expected_signal_socket_state.IsEqual(
313 signal_socket_states_.front()))
314 << signal_socket_states_.front().signal;
315 signal_socket_states_.pop_front();
318 void ExpectReadSignal() {
319 ExpectSignalSocketState(
320 SignalSocketState::NoError(
321 SIGNAL_READ, ChromeAsyncSocket::STATE_OPEN));
324 void ExpectSSLConnectSignal() {
325 ExpectSignalSocketState(
326 SignalSocketState::NoError(SIGNAL_SSL_CONNECT,
327 ChromeAsyncSocket::STATE_TLS_OPEN));
330 void ExpectSSLReadSignal() {
331 ExpectSignalSocketState(
332 SignalSocketState::NoError(
333 SIGNAL_READ, ChromeAsyncSocket::STATE_TLS_OPEN));
336 // Open/close utility functions.
338 void DoOpenClosed() {
339 ExpectClosed();
340 async_socket_data_provider_.set_connect_data(
341 net::MockConnect(net::SYNCHRONOUS, net::OK));
342 EXPECT_TRUE(chrome_async_socket_->Connect(addr_));
343 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING);
345 message_loop_.RunUntilIdle();
346 // We may not necessarily be open; may have been other events
347 // queued up.
348 ExpectSignalSocketState(
349 SignalSocketState::NoError(
350 SIGNAL_CONNECT, ChromeAsyncSocket::STATE_OPEN));
353 void DoCloseOpened(SignalSocketState expected_signal_socket_state) {
354 // We may be in an error state, so just compare state().
355 EXPECT_EQ(ChromeAsyncSocket::STATE_OPEN, chrome_async_socket_->state());
356 EXPECT_TRUE(chrome_async_socket_->Close());
357 ExpectSignalSocketState(expected_signal_socket_state);
358 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED);
361 void DoCloseOpenedNoError() {
362 DoCloseOpened(
363 SignalSocketState::NoError(
364 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
367 void DoSSLOpenClosed() {
368 const char kDummyData[] = "dummy_data";
369 async_socket_data_provider_.AddRead(net::MockRead(kDummyData));
370 DoOpenClosed();
371 ExpectReadSignal();
372 EXPECT_EQ(kDummyData, DrainRead(1));
374 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
375 message_loop_.RunUntilIdle();
376 ExpectSSLConnectSignal();
377 ExpectNoSignal();
378 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN);
381 void DoSSLCloseOpened(SignalSocketState expected_signal_socket_state) {
382 // We may be in an error state, so just compare state().
383 EXPECT_EQ(ChromeAsyncSocket::STATE_TLS_OPEN,
384 chrome_async_socket_->state());
385 EXPECT_TRUE(chrome_async_socket_->Close());
386 ExpectSignalSocketState(expected_signal_socket_state);
387 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED);
390 void DoSSLCloseOpenedNoError() {
391 DoSSLCloseOpened(
392 SignalSocketState::NoError(
393 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
396 // Read utility fucntions.
398 std::string DrainRead(size_t buf_size) {
399 std::string read;
400 scoped_ptr<char[]> buf(new char[buf_size]);
401 size_t len_read;
402 while (true) {
403 bool success =
404 chrome_async_socket_->Read(buf.get(), buf_size, &len_read);
405 if (!success) {
406 ADD_FAILURE();
407 break;
409 if (len_read == 0U) {
410 break;
412 read.append(buf.get(), len_read);
414 return read;
417 // ChromeAsyncSocket expects a message loop.
418 base::MessageLoop message_loop_;
420 AsyncSocketDataProvider async_socket_data_provider_;
421 net::SSLSocketDataProvider ssl_socket_data_provider_;
423 scoped_ptr<ChromeAsyncSocket> chrome_async_socket_;
424 std::deque<SignalSocketState> signal_socket_states_;
425 const talk_base::SocketAddress addr_;
427 private:
428 DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocketTest);
431 TEST_F(ChromeAsyncSocketTest, InitialState) {
432 ExpectClosed();
433 ExpectNoSignal();
436 TEST_F(ChromeAsyncSocketTest, EmptyClose) {
437 ExpectClosed();
438 EXPECT_TRUE(chrome_async_socket_->Close());
439 ExpectClosed();
442 TEST_F(ChromeAsyncSocketTest, ImmediateConnectAndClose) {
443 DoOpenClosed();
445 ExpectNonErrorState(ChromeAsyncSocket::STATE_OPEN);
447 DoCloseOpenedNoError();
450 // After this, no need to test immediate successful connect and
451 // Close() so thoroughly.
453 TEST_F(ChromeAsyncSocketTest, DoubleClose) {
454 DoOpenClosed();
456 EXPECT_TRUE(chrome_async_socket_->Close());
457 ExpectClosed();
458 ExpectSignalSocketState(
459 SignalSocketState::NoError(
460 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
462 EXPECT_TRUE(chrome_async_socket_->Close());
463 ExpectClosed();
466 TEST_F(ChromeAsyncSocketTest, NoHostnameConnect) {
467 talk_base::IPAddress ip_address;
468 EXPECT_TRUE(talk_base::IPFromString("127.0.0.1", &ip_address));
469 const talk_base::SocketAddress no_hostname_addr(ip_address, addr_.port());
470 EXPECT_FALSE(chrome_async_socket_->Connect(no_hostname_addr));
471 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED,
472 ChromeAsyncSocket::ERROR_DNS);
474 EXPECT_TRUE(chrome_async_socket_->Close());
475 ExpectClosed();
478 TEST_F(ChromeAsyncSocketTest, ZeroPortConnect) {
479 const talk_base::SocketAddress zero_port_addr(addr_.hostname(), 0);
480 EXPECT_FALSE(chrome_async_socket_->Connect(zero_port_addr));
481 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED,
482 ChromeAsyncSocket::ERROR_DNS);
484 EXPECT_TRUE(chrome_async_socket_->Close());
485 ExpectClosed();
488 TEST_F(ChromeAsyncSocketTest, DoubleConnect) {
489 EXPECT_DEBUG_DEATH({
490 DoOpenClosed();
492 EXPECT_FALSE(chrome_async_socket_->Connect(addr_));
493 ExpectErrorState(ChromeAsyncSocket::STATE_OPEN,
494 ChromeAsyncSocket::ERROR_WRONGSTATE);
496 DoCloseOpened(
497 SignalSocketState(SIGNAL_CLOSE,
498 ChromeAsyncSocket::STATE_CLOSED,
499 ChromeAsyncSocket::ERROR_WRONGSTATE,
500 net::OK));
501 }, "non-closed socket");
504 TEST_F(ChromeAsyncSocketTest, ImmediateConnectCloseBeforeRead) {
505 DoOpenClosed();
507 EXPECT_TRUE(chrome_async_socket_->Close());
508 ExpectClosed();
509 ExpectSignalSocketState(
510 SignalSocketState::NoError(
511 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
513 message_loop_.RunUntilIdle();
516 TEST_F(ChromeAsyncSocketTest, HangingConnect) {
517 EXPECT_TRUE(chrome_async_socket_->Connect(addr_));
518 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING);
519 ExpectNoSignal();
521 EXPECT_TRUE(chrome_async_socket_->Close());
522 ExpectClosed();
523 ExpectSignalSocketState(
524 SignalSocketState::NoError(
525 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
528 TEST_F(ChromeAsyncSocketTest, PendingConnect) {
529 async_socket_data_provider_.set_connect_data(
530 net::MockConnect(net::ASYNC, net::OK));
531 EXPECT_TRUE(chrome_async_socket_->Connect(addr_));
532 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING);
533 ExpectNoSignal();
535 message_loop_.RunUntilIdle();
536 ExpectNonErrorState(ChromeAsyncSocket::STATE_OPEN);
537 ExpectSignalSocketState(
538 SignalSocketState::NoError(
539 SIGNAL_CONNECT, ChromeAsyncSocket::STATE_OPEN));
540 ExpectNoSignal();
542 message_loop_.RunUntilIdle();
544 DoCloseOpenedNoError();
547 // After this no need to test successful pending connect so
548 // thoroughly.
550 TEST_F(ChromeAsyncSocketTest, PendingConnectCloseBeforeRead) {
551 async_socket_data_provider_.set_connect_data(
552 net::MockConnect(net::ASYNC, net::OK));
553 EXPECT_TRUE(chrome_async_socket_->Connect(addr_));
555 message_loop_.RunUntilIdle();
556 ExpectSignalSocketState(
557 SignalSocketState::NoError(
558 SIGNAL_CONNECT, ChromeAsyncSocket::STATE_OPEN));
560 DoCloseOpenedNoError();
562 message_loop_.RunUntilIdle();
565 TEST_F(ChromeAsyncSocketTest, PendingConnectError) {
566 async_socket_data_provider_.set_connect_data(
567 net::MockConnect(net::ASYNC, net::ERR_TIMED_OUT));
568 EXPECT_TRUE(chrome_async_socket_->Connect(addr_));
570 message_loop_.RunUntilIdle();
572 ExpectSignalSocketState(
573 SignalSocketState(
574 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
575 ChromeAsyncSocket::ERROR_WINSOCK, net::ERR_TIMED_OUT));
578 // After this we can assume Connect() and Close() work as expected.
580 TEST_F(ChromeAsyncSocketTest, EmptyRead) {
581 DoOpenClosed();
583 char buf[4096];
584 size_t len_read = 10000U;
585 EXPECT_TRUE(chrome_async_socket_->Read(buf, sizeof(buf), &len_read));
586 EXPECT_EQ(0U, len_read);
588 DoCloseOpenedNoError();
591 TEST_F(ChromeAsyncSocketTest, WrongRead) {
592 EXPECT_DEBUG_DEATH({
593 async_socket_data_provider_.set_connect_data(
594 net::MockConnect(net::ASYNC, net::OK));
595 EXPECT_TRUE(chrome_async_socket_->Connect(addr_));
596 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING);
597 ExpectNoSignal();
599 char buf[4096];
600 size_t len_read;
601 EXPECT_FALSE(chrome_async_socket_->Read(buf, sizeof(buf), &len_read));
602 ExpectErrorState(ChromeAsyncSocket::STATE_CONNECTING,
603 ChromeAsyncSocket::ERROR_WRONGSTATE);
604 EXPECT_TRUE(chrome_async_socket_->Close());
605 ExpectSignalSocketState(
606 SignalSocketState(
607 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
608 ChromeAsyncSocket::ERROR_WRONGSTATE, net::OK));
609 }, "non-open");
612 TEST_F(ChromeAsyncSocketTest, WrongReadClosed) {
613 char buf[4096];
614 size_t len_read;
615 EXPECT_FALSE(chrome_async_socket_->Read(buf, sizeof(buf), &len_read));
616 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED,
617 ChromeAsyncSocket::ERROR_WRONGSTATE);
618 EXPECT_TRUE(chrome_async_socket_->Close());
621 const char kReadData[] = "mydatatoread";
623 TEST_F(ChromeAsyncSocketTest, Read) {
624 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
625 DoOpenClosed();
627 ExpectReadSignal();
628 ExpectNoSignal();
630 EXPECT_EQ(kReadData, DrainRead(1));
632 message_loop_.RunUntilIdle();
634 DoCloseOpenedNoError();
637 TEST_F(ChromeAsyncSocketTest, ReadTwice) {
638 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
639 DoOpenClosed();
641 ExpectReadSignal();
642 ExpectNoSignal();
644 EXPECT_EQ(kReadData, DrainRead(1));
646 message_loop_.RunUntilIdle();
648 const char kReadData2[] = "mydatatoread2";
649 async_socket_data_provider_.AddRead(net::MockRead(kReadData2));
651 ExpectReadSignal();
652 ExpectNoSignal();
654 EXPECT_EQ(kReadData2, DrainRead(1));
656 DoCloseOpenedNoError();
659 TEST_F(ChromeAsyncSocketTest, ReadError) {
660 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
661 DoOpenClosed();
663 ExpectReadSignal();
664 ExpectNoSignal();
666 EXPECT_EQ(kReadData, DrainRead(1));
668 message_loop_.RunUntilIdle();
670 async_socket_data_provider_.AddRead(
671 net::MockRead(net::SYNCHRONOUS, net::ERR_TIMED_OUT));
673 ExpectSignalSocketState(
674 SignalSocketState(
675 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
676 ChromeAsyncSocket::ERROR_WINSOCK, net::ERR_TIMED_OUT));
679 TEST_F(ChromeAsyncSocketTest, ReadEmpty) {
680 async_socket_data_provider_.AddRead(net::MockRead(""));
681 DoOpenClosed();
683 ExpectSignalSocketState(
684 SignalSocketState::NoError(
685 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
688 TEST_F(ChromeAsyncSocketTest, PendingRead) {
689 DoOpenClosed();
691 ExpectNoSignal();
693 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
695 ExpectSignalSocketState(
696 SignalSocketState::NoError(
697 SIGNAL_READ, ChromeAsyncSocket::STATE_OPEN));
698 ExpectNoSignal();
700 EXPECT_EQ(kReadData, DrainRead(1));
702 message_loop_.RunUntilIdle();
704 DoCloseOpenedNoError();
707 TEST_F(ChromeAsyncSocketTest, PendingEmptyRead) {
708 DoOpenClosed();
710 ExpectNoSignal();
712 async_socket_data_provider_.AddRead(net::MockRead(""));
714 ExpectSignalSocketState(
715 SignalSocketState::NoError(
716 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
719 TEST_F(ChromeAsyncSocketTest, PendingReadError) {
720 DoOpenClosed();
722 ExpectNoSignal();
724 async_socket_data_provider_.AddRead(
725 net::MockRead(net::ASYNC, net::ERR_TIMED_OUT));
727 ExpectSignalSocketState(
728 SignalSocketState(
729 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
730 ChromeAsyncSocket::ERROR_WINSOCK, net::ERR_TIMED_OUT));
733 // After this we can assume non-SSL Read() works as expected.
735 TEST_F(ChromeAsyncSocketTest, WrongWrite) {
736 EXPECT_DEBUG_DEATH({
737 std::string data("foo");
738 EXPECT_FALSE(chrome_async_socket_->Write(data.data(), data.size()));
739 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED,
740 ChromeAsyncSocket::ERROR_WRONGSTATE);
741 EXPECT_TRUE(chrome_async_socket_->Close());
742 }, "non-open");
745 const char kWriteData[] = "mydatatowrite";
747 TEST_F(ChromeAsyncSocketTest, SyncWrite) {
748 async_socket_data_provider_.AddWrite(
749 net::MockWrite(net::SYNCHRONOUS, kWriteData, 3));
750 async_socket_data_provider_.AddWrite(
751 net::MockWrite(net::SYNCHRONOUS, kWriteData + 3, 5));
752 async_socket_data_provider_.AddWrite(
753 net::MockWrite(net::SYNCHRONOUS,
754 kWriteData + 8, arraysize(kWriteData) - 8));
755 DoOpenClosed();
757 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData, 3));
758 message_loop_.RunUntilIdle();
759 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData + 3, 5));
760 message_loop_.RunUntilIdle();
761 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData + 8,
762 arraysize(kWriteData) - 8));
763 message_loop_.RunUntilIdle();
765 ExpectNoSignal();
767 DoCloseOpenedNoError();
770 TEST_F(ChromeAsyncSocketTest, AsyncWrite) {
771 DoOpenClosed();
773 async_socket_data_provider_.AddWrite(
774 net::MockWrite(net::ASYNC, kWriteData, 3));
775 async_socket_data_provider_.AddWrite(
776 net::MockWrite(net::ASYNC, kWriteData + 3, 5));
777 async_socket_data_provider_.AddWrite(
778 net::MockWrite(net::ASYNC, kWriteData + 8, arraysize(kWriteData) - 8));
780 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData, 3));
781 message_loop_.RunUntilIdle();
782 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData + 3, 5));
783 message_loop_.RunUntilIdle();
784 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData + 8,
785 arraysize(kWriteData) - 8));
786 message_loop_.RunUntilIdle();
788 ExpectNoSignal();
790 DoCloseOpenedNoError();
793 TEST_F(ChromeAsyncSocketTest, AsyncWriteError) {
794 DoOpenClosed();
796 async_socket_data_provider_.AddWrite(
797 net::MockWrite(net::ASYNC, kWriteData, 3));
798 async_socket_data_provider_.AddWrite(
799 net::MockWrite(net::ASYNC, kWriteData + 3, 5));
800 async_socket_data_provider_.AddWrite(
801 net::MockWrite(net::ASYNC, net::ERR_TIMED_OUT));
803 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData, 3));
804 message_loop_.RunUntilIdle();
805 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData + 3, 5));
806 message_loop_.RunUntilIdle();
807 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData + 8,
808 arraysize(kWriteData) - 8));
809 message_loop_.RunUntilIdle();
811 ExpectSignalSocketState(
812 SignalSocketState(
813 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
814 ChromeAsyncSocket::ERROR_WINSOCK, net::ERR_TIMED_OUT));
817 TEST_F(ChromeAsyncSocketTest, LargeWrite) {
818 EXPECT_DEBUG_DEATH({
819 DoOpenClosed();
821 std::string large_data(100, 'x');
822 EXPECT_FALSE(chrome_async_socket_->Write(large_data.data(),
823 large_data.size()));
824 ExpectState(ChromeAsyncSocket::STATE_OPEN,
825 ChromeAsyncSocket::ERROR_WINSOCK,
826 net::ERR_INSUFFICIENT_RESOURCES);
827 DoCloseOpened(
828 SignalSocketState(
829 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
830 ChromeAsyncSocket::ERROR_WINSOCK,
831 net::ERR_INSUFFICIENT_RESOURCES));
832 }, "exceed the max write buffer");
835 TEST_F(ChromeAsyncSocketTest, LargeAccumulatedWrite) {
836 EXPECT_DEBUG_DEATH({
837 DoOpenClosed();
839 std::string data(15, 'x');
840 EXPECT_TRUE(chrome_async_socket_->Write(data.data(), data.size()));
841 EXPECT_FALSE(chrome_async_socket_->Write(data.data(), data.size()));
842 ExpectState(ChromeAsyncSocket::STATE_OPEN,
843 ChromeAsyncSocket::ERROR_WINSOCK,
844 net::ERR_INSUFFICIENT_RESOURCES);
845 DoCloseOpened(
846 SignalSocketState(
847 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
848 ChromeAsyncSocket::ERROR_WINSOCK,
849 net::ERR_INSUFFICIENT_RESOURCES));
850 }, "exceed the max write buffer");
853 // After this we can assume non-SSL I/O works as expected.
855 TEST_F(ChromeAsyncSocketTest, HangingSSLConnect) {
856 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
857 DoOpenClosed();
858 ExpectReadSignal();
860 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
861 ExpectNoSignal();
863 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_CONNECTING);
864 EXPECT_TRUE(chrome_async_socket_->Close());
865 ExpectSignalSocketState(
866 SignalSocketState::NoError(SIGNAL_CLOSE,
867 ChromeAsyncSocket::STATE_CLOSED));
868 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED);
871 TEST_F(ChromeAsyncSocketTest, ImmediateSSLConnect) {
872 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
873 DoOpenClosed();
874 ExpectReadSignal();
876 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
877 message_loop_.RunUntilIdle();
878 ExpectSSLConnectSignal();
879 ExpectNoSignal();
880 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN);
882 DoSSLCloseOpenedNoError();
885 TEST_F(ChromeAsyncSocketTest, DoubleSSLConnect) {
886 EXPECT_DEBUG_DEATH({
887 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
888 DoOpenClosed();
889 ExpectReadSignal();
891 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
892 message_loop_.RunUntilIdle();
893 ExpectSSLConnectSignal();
894 ExpectNoSignal();
895 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN);
897 EXPECT_FALSE(chrome_async_socket_->StartTls("fakedomain.com"));
899 DoSSLCloseOpened(
900 SignalSocketState(SIGNAL_CLOSE,
901 ChromeAsyncSocket::STATE_CLOSED,
902 ChromeAsyncSocket::ERROR_WRONGSTATE,
903 net::OK));
904 }, "wrong state");
907 TEST_F(ChromeAsyncSocketTest, FailedSSLConnect) {
908 ssl_socket_data_provider_.connect =
909 net::MockConnect(net::ASYNC, net::ERR_CERT_COMMON_NAME_INVALID),
911 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
912 DoOpenClosed();
913 ExpectReadSignal();
915 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
916 message_loop_.RunUntilIdle();
917 ExpectSignalSocketState(
918 SignalSocketState(
919 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
920 ChromeAsyncSocket::ERROR_WINSOCK,
921 net::ERR_CERT_COMMON_NAME_INVALID));
923 EXPECT_TRUE(chrome_async_socket_->Close());
924 ExpectClosed();
927 TEST_F(ChromeAsyncSocketTest, ReadDuringSSLConnecting) {
928 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
929 DoOpenClosed();
930 ExpectReadSignal();
931 EXPECT_EQ(kReadData, DrainRead(1));
933 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
934 ExpectNoSignal();
936 // Shouldn't do anything.
937 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
939 char buf[4096];
940 size_t len_read = 10000U;
941 EXPECT_TRUE(chrome_async_socket_->Read(buf, sizeof(buf), &len_read));
942 EXPECT_EQ(0U, len_read);
944 message_loop_.RunUntilIdle();
945 ExpectSSLConnectSignal();
946 ExpectSSLReadSignal();
947 ExpectNoSignal();
948 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN);
950 len_read = 10000U;
951 EXPECT_TRUE(chrome_async_socket_->Read(buf, sizeof(buf), &len_read));
952 EXPECT_EQ(kReadData, std::string(buf, len_read));
954 DoSSLCloseOpenedNoError();
957 TEST_F(ChromeAsyncSocketTest, WriteDuringSSLConnecting) {
958 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
959 DoOpenClosed();
960 ExpectReadSignal();
962 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
963 ExpectNoSignal();
964 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_CONNECTING);
966 async_socket_data_provider_.AddWrite(
967 net::MockWrite(net::ASYNC, kWriteData, 3));
969 // Shouldn't do anything.
970 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData, 3));
972 // TODO(akalin): Figure out how to test that the write happens
973 // *after* the SSL connect.
975 message_loop_.RunUntilIdle();
976 ExpectSSLConnectSignal();
977 ExpectNoSignal();
979 message_loop_.RunUntilIdle();
981 DoSSLCloseOpenedNoError();
984 TEST_F(ChromeAsyncSocketTest, SSLConnectDuringPendingRead) {
985 EXPECT_DEBUG_DEATH({
986 DoOpenClosed();
988 EXPECT_FALSE(chrome_async_socket_->StartTls("fakedomain.com"));
990 DoCloseOpened(
991 SignalSocketState(SIGNAL_CLOSE,
992 ChromeAsyncSocket::STATE_CLOSED,
993 ChromeAsyncSocket::ERROR_WRONGSTATE,
994 net::OK));
995 }, "wrong state");
998 TEST_F(ChromeAsyncSocketTest, SSLConnectDuringPostedWrite) {
999 EXPECT_DEBUG_DEATH({
1000 DoOpenClosed();
1002 async_socket_data_provider_.AddWrite(
1003 net::MockWrite(net::ASYNC, kWriteData, 3));
1004 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData, 3));
1006 EXPECT_FALSE(chrome_async_socket_->StartTls("fakedomain.com"));
1008 message_loop_.RunUntilIdle();
1010 DoCloseOpened(
1011 SignalSocketState(SIGNAL_CLOSE,
1012 ChromeAsyncSocket::STATE_CLOSED,
1013 ChromeAsyncSocket::ERROR_WRONGSTATE,
1014 net::OK));
1015 }, "wrong state");
1018 // After this we can assume SSL connect works as expected.
1020 TEST_F(ChromeAsyncSocketTest, SSLRead) {
1021 DoSSLOpenClosed();
1022 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
1023 message_loop_.RunUntilIdle();
1025 ExpectSSLReadSignal();
1026 ExpectNoSignal();
1028 EXPECT_EQ(kReadData, DrainRead(1));
1030 message_loop_.RunUntilIdle();
1032 DoSSLCloseOpenedNoError();
1035 TEST_F(ChromeAsyncSocketTest, SSLSyncWrite) {
1036 async_socket_data_provider_.AddWrite(
1037 net::MockWrite(net::SYNCHRONOUS, kWriteData, 3));
1038 async_socket_data_provider_.AddWrite(
1039 net::MockWrite(net::SYNCHRONOUS, kWriteData + 3, 5));
1040 async_socket_data_provider_.AddWrite(
1041 net::MockWrite(net::SYNCHRONOUS,
1042 kWriteData + 8, arraysize(kWriteData) - 8));
1043 DoSSLOpenClosed();
1045 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData, 3));
1046 message_loop_.RunUntilIdle();
1047 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData + 3, 5));
1048 message_loop_.RunUntilIdle();
1049 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData + 8,
1050 arraysize(kWriteData) - 8));
1051 message_loop_.RunUntilIdle();
1053 ExpectNoSignal();
1055 DoSSLCloseOpenedNoError();
1058 TEST_F(ChromeAsyncSocketTest, SSLAsyncWrite) {
1059 DoSSLOpenClosed();
1061 async_socket_data_provider_.AddWrite(
1062 net::MockWrite(net::ASYNC, kWriteData, 3));
1063 async_socket_data_provider_.AddWrite(
1064 net::MockWrite(net::ASYNC, kWriteData + 3, 5));
1065 async_socket_data_provider_.AddWrite(
1066 net::MockWrite(net::ASYNC, kWriteData + 8, arraysize(kWriteData) - 8));
1068 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData, 3));
1069 message_loop_.RunUntilIdle();
1070 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData + 3, 5));
1071 message_loop_.RunUntilIdle();
1072 EXPECT_TRUE(chrome_async_socket_->Write(kWriteData + 8,
1073 arraysize(kWriteData) - 8));
1074 message_loop_.RunUntilIdle();
1076 ExpectNoSignal();
1078 DoSSLCloseOpenedNoError();
1081 } // namespace
1083 } // namespace jingle_glue