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"
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 "base/message_loop/message_pump_default.h"
15 #include "jingle/glue/resolving_client_socket_factory.h"
16 #include "net/base/address_list.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/net_util.h"
19 #include "net/cert/mock_cert_verifier.h"
20 #include "net/http/transport_security_state.h"
21 #include "net/socket/socket_test_util.h"
22 #include "net/socket/ssl_client_socket.h"
23 #include "net/ssl/ssl_config_service.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "third_party/webrtc/base/ipaddress.h"
27 #include "third_party/webrtc/base/sigslot.h"
28 #include "third_party/webrtc/base/socketaddress.h"
30 namespace jingle_glue
{
34 // Data provider that handles reads/writes for ChromeAsyncSocket.
35 class AsyncSocketDataProvider
: public net::SocketDataProvider
{
37 AsyncSocketDataProvider() : has_pending_read_(false) {}
39 ~AsyncSocketDataProvider() override
{
40 EXPECT_TRUE(writes_
.empty());
41 EXPECT_TRUE(reads_
.empty());
44 // If there's no read, sets the "has pending read" flag. Otherwise,
45 // pops the next read.
46 net::MockRead
GetNextRead() override
{
48 DCHECK(!has_pending_read_
);
49 has_pending_read_
= true;
50 const net::MockRead
pending_read(net::SYNCHRONOUS
, net::ERR_IO_PENDING
);
53 net::MockRead mock_read
= reads_
.front();
58 // Simply pops the next write and, if applicable, compares it to
60 net::MockWriteResult
OnWrite(const std::string
& data
) override
{
61 DCHECK(!writes_
.empty());
62 net::MockWrite mock_write
= writes_
.front();
64 if (mock_write
.result
!= net::OK
) {
65 return net::MockWriteResult(mock_write
.mode
, mock_write
.result
);
67 std::string
expected_data(mock_write
.data
, mock_write
.data_len
);
68 EXPECT_EQ(expected_data
, data
);
69 if (expected_data
!= data
) {
70 return net::MockWriteResult(net::SYNCHRONOUS
, net::ERR_UNEXPECTED
);
72 return net::MockWriteResult(mock_write
.mode
, data
.size());
75 // We ignore resets so we can pre-load the socket data provider with
77 void Reset() override
{}
79 // If there is a pending read, completes it with the given read.
80 // Otherwise, queues up the given read.
81 void AddRead(const net::MockRead
& mock_read
) {
82 DCHECK_NE(mock_read
.result
, net::ERR_IO_PENDING
);
83 if (has_pending_read_
) {
84 socket()->OnReadComplete(mock_read
);
85 has_pending_read_
= false;
88 reads_
.push_back(mock_read
);
91 // Simply queues up the given write.
92 void AddWrite(const net::MockWrite
& mock_write
) {
93 writes_
.push_back(mock_write
);
97 std::deque
<net::MockRead
> reads_
;
98 bool has_pending_read_
;
100 std::deque
<net::MockWrite
> writes_
;
102 DISALLOW_COPY_AND_ASSIGN(AsyncSocketDataProvider
);
105 class MockXmppClientSocketFactory
: public ResolvingClientSocketFactory
{
107 MockXmppClientSocketFactory(
108 net::ClientSocketFactory
* mock_client_socket_factory
,
109 const net::AddressList
& address_list
)
110 : mock_client_socket_factory_(mock_client_socket_factory
),
111 address_list_(address_list
),
112 cert_verifier_(new net::MockCertVerifier
),
113 transport_security_state_(new net::TransportSecurityState
) {
116 // ResolvingClientSocketFactory implementation.
117 scoped_ptr
<net::StreamSocket
> CreateTransportClientSocket(
118 const net::HostPortPair
& host_and_port
) override
{
119 return mock_client_socket_factory_
->CreateTransportClientSocket(
120 address_list_
, NULL
, net::NetLog::Source());
123 scoped_ptr
<net::SSLClientSocket
> CreateSSLClientSocket(
124 scoped_ptr
<net::ClientSocketHandle
> transport_socket
,
125 const net::HostPortPair
& host_and_port
) override
{
126 net::SSLClientSocketContext context
;
127 context
.cert_verifier
= cert_verifier_
.get();
128 context
.transport_security_state
= transport_security_state_
.get();
129 return mock_client_socket_factory_
->CreateSSLClientSocket(
130 transport_socket
.Pass(), host_and_port
, ssl_config_
, context
);
134 scoped_ptr
<net::ClientSocketFactory
> mock_client_socket_factory_
;
135 net::AddressList address_list_
;
136 net::SSLConfig ssl_config_
;
137 scoped_ptr
<net::CertVerifier
> cert_verifier_
;
138 scoped_ptr
<net::TransportSecurityState
> transport_security_state_
;
141 class ChromeAsyncSocketTest
142 : public testing::Test
,
143 public sigslot::has_slots
<> {
145 ChromeAsyncSocketTest()
146 : ssl_socket_data_provider_(net::ASYNC
, net::OK
),
147 addr_("localhost", 35) {
148 // GTest death tests execute in a fork()ed but not exec()ed process.
149 // On OS X a CoreFoundation-backed message loop will exit with a
150 // __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__
152 // Explicitly create a MessagePumpDefault which can run in this enivronment.
153 scoped_ptr
<base::MessagePump
> pump(new base::MessagePumpDefault());
154 message_loop_
.reset(new base::MessageLoop(pump
.Pass()));
157 virtual ~ChromeAsyncSocketTest() {}
159 virtual void SetUp() {
160 scoped_ptr
<net::MockClientSocketFactory
> mock_client_socket_factory(
161 new net::MockClientSocketFactory());
162 mock_client_socket_factory
->AddSocketDataProvider(
163 &async_socket_data_provider_
);
164 mock_client_socket_factory
->AddSSLSocketDataProvider(
165 &ssl_socket_data_provider_
);
167 // Fake DNS resolution for |addr_| and pass it to the factory.
168 net::IPAddressNumber resolved_addr
;
169 EXPECT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &resolved_addr
));
170 const net::AddressList address_list
=
171 net::AddressList::CreateFromIPAddress(resolved_addr
, addr_
.port());
172 scoped_ptr
<MockXmppClientSocketFactory
> mock_xmpp_client_socket_factory(
173 new MockXmppClientSocketFactory(
174 mock_client_socket_factory
.release(),
176 chrome_async_socket_
.reset(
177 new ChromeAsyncSocket(mock_xmpp_client_socket_factory
.release(),
180 chrome_async_socket_
->SignalConnected
.connect(
181 this, &ChromeAsyncSocketTest::OnConnect
);
182 chrome_async_socket_
->SignalSSLConnected
.connect(
183 this, &ChromeAsyncSocketTest::OnSSLConnect
);
184 chrome_async_socket_
->SignalClosed
.connect(
185 this, &ChromeAsyncSocketTest::OnClose
);
186 chrome_async_socket_
->SignalRead
.connect(
187 this, &ChromeAsyncSocketTest::OnRead
);
188 chrome_async_socket_
->SignalError
.connect(
189 this, &ChromeAsyncSocketTest::OnError
);
192 virtual void TearDown() {
193 // Run any tasks that we forgot to pump.
194 message_loop_
->RunUntilIdle();
197 chrome_async_socket_
.reset();
208 // Helper struct that records the state at the time of a signal.
210 struct SignalSocketState
{
212 : signal(SIGNAL_ERROR
),
213 state(ChromeAsyncSocket::STATE_CLOSED
),
214 error(ChromeAsyncSocket::ERROR_NONE
),
215 net_error(net::OK
) {}
219 ChromeAsyncSocket::State state
,
220 ChromeAsyncSocket::Error error
,
221 net::Error net_error
)
225 net_error(net_error
) {}
227 bool IsEqual(const SignalSocketState
& other
) const {
229 (signal
== other
.signal
) &&
230 (state
== other
.state
) &&
231 (error
== other
.error
) &&
232 (net_error
== other
.net_error
);
235 static SignalSocketState
FromAsyncSocket(
237 buzz::AsyncSocket
* async_socket
) {
238 return SignalSocketState(signal
,
239 async_socket
->state(),
240 async_socket
->error(),
241 static_cast<net::Error
>(
242 async_socket
->GetError()));
245 static SignalSocketState
NoError(
246 Signal signal
, buzz::AsyncSocket::State state
) {
247 return SignalSocketState(signal
, state
,
248 buzz::AsyncSocket::ERROR_NONE
,
253 ChromeAsyncSocket::State state
;
254 ChromeAsyncSocket::Error error
;
255 net::Error net_error
;
258 void CaptureSocketState(Signal signal
) {
259 signal_socket_states_
.push_back(
260 SignalSocketState::FromAsyncSocket(
261 signal
, chrome_async_socket_
.get()));
265 CaptureSocketState(SIGNAL_CONNECT
);
268 void OnSSLConnect() {
269 CaptureSocketState(SIGNAL_SSL_CONNECT
);
273 CaptureSocketState(SIGNAL_CLOSE
);
277 CaptureSocketState(SIGNAL_READ
);
284 // State expect functions.
286 void ExpectState(ChromeAsyncSocket::State state
,
287 ChromeAsyncSocket::Error error
,
288 net::Error net_error
) {
289 EXPECT_EQ(state
, chrome_async_socket_
->state());
290 EXPECT_EQ(error
, chrome_async_socket_
->error());
291 EXPECT_EQ(net_error
, chrome_async_socket_
->GetError());
294 void ExpectNonErrorState(ChromeAsyncSocket::State state
) {
295 ExpectState(state
, ChromeAsyncSocket::ERROR_NONE
, net::OK
);
298 void ExpectErrorState(ChromeAsyncSocket::State state
,
299 ChromeAsyncSocket::Error error
) {
300 ExpectState(state
, error
, net::OK
);
303 void ExpectClosed() {
304 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED
);
307 // Signal expect functions.
309 void ExpectNoSignal() {
310 if (!signal_socket_states_
.empty()) {
311 ADD_FAILURE() << signal_socket_states_
.front().signal
;
315 void ExpectSignalSocketState(
316 SignalSocketState expected_signal_socket_state
) {
317 if (signal_socket_states_
.empty()) {
318 ADD_FAILURE() << expected_signal_socket_state
.signal
;
321 EXPECT_TRUE(expected_signal_socket_state
.IsEqual(
322 signal_socket_states_
.front()))
323 << signal_socket_states_
.front().signal
;
324 signal_socket_states_
.pop_front();
327 void ExpectReadSignal() {
328 ExpectSignalSocketState(
329 SignalSocketState::NoError(
330 SIGNAL_READ
, ChromeAsyncSocket::STATE_OPEN
));
333 void ExpectSSLConnectSignal() {
334 ExpectSignalSocketState(
335 SignalSocketState::NoError(SIGNAL_SSL_CONNECT
,
336 ChromeAsyncSocket::STATE_TLS_OPEN
));
339 void ExpectSSLReadSignal() {
340 ExpectSignalSocketState(
341 SignalSocketState::NoError(
342 SIGNAL_READ
, ChromeAsyncSocket::STATE_TLS_OPEN
));
345 // Open/close utility functions.
347 void DoOpenClosed() {
349 async_socket_data_provider_
.set_connect_data(
350 net::MockConnect(net::SYNCHRONOUS
, net::OK
));
351 EXPECT_TRUE(chrome_async_socket_
->Connect(addr_
));
352 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING
);
354 message_loop_
->RunUntilIdle();
355 // We may not necessarily be open; may have been other events
357 ExpectSignalSocketState(
358 SignalSocketState::NoError(
359 SIGNAL_CONNECT
, ChromeAsyncSocket::STATE_OPEN
));
362 void DoCloseOpened(SignalSocketState expected_signal_socket_state
) {
363 // We may be in an error state, so just compare state().
364 EXPECT_EQ(ChromeAsyncSocket::STATE_OPEN
, chrome_async_socket_
->state());
365 EXPECT_TRUE(chrome_async_socket_
->Close());
366 ExpectSignalSocketState(expected_signal_socket_state
);
367 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED
);
370 void DoCloseOpenedNoError() {
372 SignalSocketState::NoError(
373 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
376 void DoSSLOpenClosed() {
377 const char kDummyData
[] = "dummy_data";
378 async_socket_data_provider_
.AddRead(net::MockRead(kDummyData
));
381 EXPECT_EQ(kDummyData
, DrainRead(1));
383 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
384 message_loop_
->RunUntilIdle();
385 ExpectSSLConnectSignal();
387 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN
);
390 void DoSSLCloseOpened(SignalSocketState expected_signal_socket_state
) {
391 // We may be in an error state, so just compare state().
392 EXPECT_EQ(ChromeAsyncSocket::STATE_TLS_OPEN
,
393 chrome_async_socket_
->state());
394 EXPECT_TRUE(chrome_async_socket_
->Close());
395 ExpectSignalSocketState(expected_signal_socket_state
);
396 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED
);
399 void DoSSLCloseOpenedNoError() {
401 SignalSocketState::NoError(
402 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
405 // Read utility fucntions.
407 std::string
DrainRead(size_t buf_size
) {
409 scoped_ptr
<char[]> buf(new char[buf_size
]);
413 chrome_async_socket_
->Read(buf
.get(), buf_size
, &len_read
);
418 if (len_read
== 0U) {
421 read
.append(buf
.get(), len_read
);
426 // ChromeAsyncSocket expects a message loop.
427 scoped_ptr
<base::MessageLoop
> message_loop_
;
429 AsyncSocketDataProvider async_socket_data_provider_
;
430 net::SSLSocketDataProvider ssl_socket_data_provider_
;
432 scoped_ptr
<ChromeAsyncSocket
> chrome_async_socket_
;
433 std::deque
<SignalSocketState
> signal_socket_states_
;
434 const rtc::SocketAddress addr_
;
437 DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocketTest
);
440 TEST_F(ChromeAsyncSocketTest
, InitialState
) {
445 TEST_F(ChromeAsyncSocketTest
, EmptyClose
) {
447 EXPECT_TRUE(chrome_async_socket_
->Close());
451 TEST_F(ChromeAsyncSocketTest
, ImmediateConnectAndClose
) {
454 ExpectNonErrorState(ChromeAsyncSocket::STATE_OPEN
);
456 DoCloseOpenedNoError();
459 // After this, no need to test immediate successful connect and
460 // Close() so thoroughly.
462 TEST_F(ChromeAsyncSocketTest
, DoubleClose
) {
465 EXPECT_TRUE(chrome_async_socket_
->Close());
467 ExpectSignalSocketState(
468 SignalSocketState::NoError(
469 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
471 EXPECT_TRUE(chrome_async_socket_
->Close());
475 TEST_F(ChromeAsyncSocketTest
, NoHostnameConnect
) {
476 rtc::IPAddress ip_address
;
477 EXPECT_TRUE(rtc::IPFromString("127.0.0.1", &ip_address
));
478 const rtc::SocketAddress
no_hostname_addr(ip_address
, addr_
.port());
479 EXPECT_FALSE(chrome_async_socket_
->Connect(no_hostname_addr
));
480 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED
,
481 ChromeAsyncSocket::ERROR_DNS
);
483 EXPECT_TRUE(chrome_async_socket_
->Close());
487 TEST_F(ChromeAsyncSocketTest
, ZeroPortConnect
) {
488 const rtc::SocketAddress
zero_port_addr(addr_
.hostname(), 0);
489 EXPECT_FALSE(chrome_async_socket_
->Connect(zero_port_addr
));
490 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED
,
491 ChromeAsyncSocket::ERROR_DNS
);
493 EXPECT_TRUE(chrome_async_socket_
->Close());
497 TEST_F(ChromeAsyncSocketTest
, DoubleConnect
) {
501 EXPECT_FALSE(chrome_async_socket_
->Connect(addr_
));
502 ExpectErrorState(ChromeAsyncSocket::STATE_OPEN
,
503 ChromeAsyncSocket::ERROR_WRONGSTATE
);
506 SignalSocketState(SIGNAL_CLOSE
,
507 ChromeAsyncSocket::STATE_CLOSED
,
508 ChromeAsyncSocket::ERROR_WRONGSTATE
,
510 }, "non-closed socket");
513 TEST_F(ChromeAsyncSocketTest
, ImmediateConnectCloseBeforeRead
) {
516 EXPECT_TRUE(chrome_async_socket_
->Close());
518 ExpectSignalSocketState(
519 SignalSocketState::NoError(
520 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
522 message_loop_
->RunUntilIdle();
525 TEST_F(ChromeAsyncSocketTest
, HangingConnect
) {
526 EXPECT_TRUE(chrome_async_socket_
->Connect(addr_
));
527 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING
);
530 EXPECT_TRUE(chrome_async_socket_
->Close());
532 ExpectSignalSocketState(
533 SignalSocketState::NoError(
534 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
537 TEST_F(ChromeAsyncSocketTest
, PendingConnect
) {
538 async_socket_data_provider_
.set_connect_data(
539 net::MockConnect(net::ASYNC
, net::OK
));
540 EXPECT_TRUE(chrome_async_socket_
->Connect(addr_
));
541 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING
);
544 message_loop_
->RunUntilIdle();
545 ExpectNonErrorState(ChromeAsyncSocket::STATE_OPEN
);
546 ExpectSignalSocketState(
547 SignalSocketState::NoError(
548 SIGNAL_CONNECT
, ChromeAsyncSocket::STATE_OPEN
));
551 message_loop_
->RunUntilIdle();
553 DoCloseOpenedNoError();
556 // After this no need to test successful pending connect so
559 TEST_F(ChromeAsyncSocketTest
, PendingConnectCloseBeforeRead
) {
560 async_socket_data_provider_
.set_connect_data(
561 net::MockConnect(net::ASYNC
, net::OK
));
562 EXPECT_TRUE(chrome_async_socket_
->Connect(addr_
));
564 message_loop_
->RunUntilIdle();
565 ExpectSignalSocketState(
566 SignalSocketState::NoError(
567 SIGNAL_CONNECT
, ChromeAsyncSocket::STATE_OPEN
));
569 DoCloseOpenedNoError();
571 message_loop_
->RunUntilIdle();
574 TEST_F(ChromeAsyncSocketTest
, PendingConnectError
) {
575 async_socket_data_provider_
.set_connect_data(
576 net::MockConnect(net::ASYNC
, net::ERR_TIMED_OUT
));
577 EXPECT_TRUE(chrome_async_socket_
->Connect(addr_
));
579 message_loop_
->RunUntilIdle();
581 ExpectSignalSocketState(
583 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
584 ChromeAsyncSocket::ERROR_WINSOCK
, net::ERR_TIMED_OUT
));
587 // After this we can assume Connect() and Close() work as expected.
589 TEST_F(ChromeAsyncSocketTest
, EmptyRead
) {
593 size_t len_read
= 10000U;
594 EXPECT_TRUE(chrome_async_socket_
->Read(buf
, sizeof(buf
), &len_read
));
595 EXPECT_EQ(0U, len_read
);
597 DoCloseOpenedNoError();
600 TEST_F(ChromeAsyncSocketTest
, WrongRead
) {
602 async_socket_data_provider_
.set_connect_data(
603 net::MockConnect(net::ASYNC
, net::OK
));
604 EXPECT_TRUE(chrome_async_socket_
->Connect(addr_
));
605 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING
);
610 EXPECT_FALSE(chrome_async_socket_
->Read(buf
, sizeof(buf
), &len_read
));
611 ExpectErrorState(ChromeAsyncSocket::STATE_CONNECTING
,
612 ChromeAsyncSocket::ERROR_WRONGSTATE
);
613 EXPECT_TRUE(chrome_async_socket_
->Close());
614 ExpectSignalSocketState(
616 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
617 ChromeAsyncSocket::ERROR_WRONGSTATE
, net::OK
));
621 TEST_F(ChromeAsyncSocketTest
, WrongReadClosed
) {
624 EXPECT_FALSE(chrome_async_socket_
->Read(buf
, sizeof(buf
), &len_read
));
625 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED
,
626 ChromeAsyncSocket::ERROR_WRONGSTATE
);
627 EXPECT_TRUE(chrome_async_socket_
->Close());
630 const char kReadData
[] = "mydatatoread";
632 TEST_F(ChromeAsyncSocketTest
, Read
) {
633 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
639 EXPECT_EQ(kReadData
, DrainRead(1));
641 message_loop_
->RunUntilIdle();
643 DoCloseOpenedNoError();
646 TEST_F(ChromeAsyncSocketTest
, ReadTwice
) {
647 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
653 EXPECT_EQ(kReadData
, DrainRead(1));
655 message_loop_
->RunUntilIdle();
657 const char kReadData2
[] = "mydatatoread2";
658 async_socket_data_provider_
.AddRead(net::MockRead(kReadData2
));
663 EXPECT_EQ(kReadData2
, DrainRead(1));
665 DoCloseOpenedNoError();
668 TEST_F(ChromeAsyncSocketTest
, ReadError
) {
669 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
675 EXPECT_EQ(kReadData
, DrainRead(1));
677 message_loop_
->RunUntilIdle();
679 async_socket_data_provider_
.AddRead(
680 net::MockRead(net::SYNCHRONOUS
, net::ERR_TIMED_OUT
));
682 ExpectSignalSocketState(
684 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
685 ChromeAsyncSocket::ERROR_WINSOCK
, net::ERR_TIMED_OUT
));
688 TEST_F(ChromeAsyncSocketTest
, ReadEmpty
) {
689 async_socket_data_provider_
.AddRead(net::MockRead(""));
692 ExpectSignalSocketState(
693 SignalSocketState::NoError(
694 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
697 TEST_F(ChromeAsyncSocketTest
, PendingRead
) {
702 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
704 ExpectSignalSocketState(
705 SignalSocketState::NoError(
706 SIGNAL_READ
, ChromeAsyncSocket::STATE_OPEN
));
709 EXPECT_EQ(kReadData
, DrainRead(1));
711 message_loop_
->RunUntilIdle();
713 DoCloseOpenedNoError();
716 TEST_F(ChromeAsyncSocketTest
, PendingEmptyRead
) {
721 async_socket_data_provider_
.AddRead(net::MockRead(""));
723 ExpectSignalSocketState(
724 SignalSocketState::NoError(
725 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
728 TEST_F(ChromeAsyncSocketTest
, PendingReadError
) {
733 async_socket_data_provider_
.AddRead(
734 net::MockRead(net::ASYNC
, net::ERR_TIMED_OUT
));
736 ExpectSignalSocketState(
738 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
739 ChromeAsyncSocket::ERROR_WINSOCK
, net::ERR_TIMED_OUT
));
742 // After this we can assume non-SSL Read() works as expected.
744 TEST_F(ChromeAsyncSocketTest
, WrongWrite
) {
746 std::string
data("foo");
747 EXPECT_FALSE(chrome_async_socket_
->Write(data
.data(), data
.size()));
748 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED
,
749 ChromeAsyncSocket::ERROR_WRONGSTATE
);
750 EXPECT_TRUE(chrome_async_socket_
->Close());
754 const char kWriteData
[] = "mydatatowrite";
756 TEST_F(ChromeAsyncSocketTest
, SyncWrite
) {
757 async_socket_data_provider_
.AddWrite(
758 net::MockWrite(net::SYNCHRONOUS
, kWriteData
, 3));
759 async_socket_data_provider_
.AddWrite(
760 net::MockWrite(net::SYNCHRONOUS
, kWriteData
+ 3, 5));
761 async_socket_data_provider_
.AddWrite(
762 net::MockWrite(net::SYNCHRONOUS
,
763 kWriteData
+ 8, arraysize(kWriteData
) - 8));
766 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
767 message_loop_
->RunUntilIdle();
768 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 3, 5));
769 message_loop_
->RunUntilIdle();
770 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 8,
771 arraysize(kWriteData
) - 8));
772 message_loop_
->RunUntilIdle();
776 DoCloseOpenedNoError();
779 TEST_F(ChromeAsyncSocketTest
, AsyncWrite
) {
782 async_socket_data_provider_
.AddWrite(
783 net::MockWrite(net::ASYNC
, kWriteData
, 3));
784 async_socket_data_provider_
.AddWrite(
785 net::MockWrite(net::ASYNC
, kWriteData
+ 3, 5));
786 async_socket_data_provider_
.AddWrite(
787 net::MockWrite(net::ASYNC
, kWriteData
+ 8, arraysize(kWriteData
) - 8));
789 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
790 message_loop_
->RunUntilIdle();
791 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 3, 5));
792 message_loop_
->RunUntilIdle();
793 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 8,
794 arraysize(kWriteData
) - 8));
795 message_loop_
->RunUntilIdle();
799 DoCloseOpenedNoError();
802 TEST_F(ChromeAsyncSocketTest
, AsyncWriteError
) {
805 async_socket_data_provider_
.AddWrite(
806 net::MockWrite(net::ASYNC
, kWriteData
, 3));
807 async_socket_data_provider_
.AddWrite(
808 net::MockWrite(net::ASYNC
, kWriteData
+ 3, 5));
809 async_socket_data_provider_
.AddWrite(
810 net::MockWrite(net::ASYNC
, net::ERR_TIMED_OUT
));
812 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
813 message_loop_
->RunUntilIdle();
814 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 3, 5));
815 message_loop_
->RunUntilIdle();
816 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 8,
817 arraysize(kWriteData
) - 8));
818 message_loop_
->RunUntilIdle();
820 ExpectSignalSocketState(
822 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
823 ChromeAsyncSocket::ERROR_WINSOCK
, net::ERR_TIMED_OUT
));
826 TEST_F(ChromeAsyncSocketTest
, LargeWrite
) {
830 std::string
large_data(100, 'x');
831 EXPECT_FALSE(chrome_async_socket_
->Write(large_data
.data(),
833 ExpectState(ChromeAsyncSocket::STATE_OPEN
,
834 ChromeAsyncSocket::ERROR_WINSOCK
,
835 net::ERR_INSUFFICIENT_RESOURCES
);
838 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
839 ChromeAsyncSocket::ERROR_WINSOCK
,
840 net::ERR_INSUFFICIENT_RESOURCES
));
841 }, "exceed the max write buffer");
844 TEST_F(ChromeAsyncSocketTest
, LargeAccumulatedWrite
) {
848 std::string
data(15, 'x');
849 EXPECT_TRUE(chrome_async_socket_
->Write(data
.data(), data
.size()));
850 EXPECT_FALSE(chrome_async_socket_
->Write(data
.data(), data
.size()));
851 ExpectState(ChromeAsyncSocket::STATE_OPEN
,
852 ChromeAsyncSocket::ERROR_WINSOCK
,
853 net::ERR_INSUFFICIENT_RESOURCES
);
856 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
857 ChromeAsyncSocket::ERROR_WINSOCK
,
858 net::ERR_INSUFFICIENT_RESOURCES
));
859 }, "exceed the max write buffer");
862 // After this we can assume non-SSL I/O works as expected.
864 TEST_F(ChromeAsyncSocketTest
, HangingSSLConnect
) {
865 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
869 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
872 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_CONNECTING
);
873 EXPECT_TRUE(chrome_async_socket_
->Close());
874 ExpectSignalSocketState(
875 SignalSocketState::NoError(SIGNAL_CLOSE
,
876 ChromeAsyncSocket::STATE_CLOSED
));
877 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED
);
880 TEST_F(ChromeAsyncSocketTest
, ImmediateSSLConnect
) {
881 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
885 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
886 message_loop_
->RunUntilIdle();
887 ExpectSSLConnectSignal();
889 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN
);
891 DoSSLCloseOpenedNoError();
894 TEST_F(ChromeAsyncSocketTest
, DoubleSSLConnect
) {
896 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
900 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
901 message_loop_
->RunUntilIdle();
902 ExpectSSLConnectSignal();
904 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN
);
906 EXPECT_FALSE(chrome_async_socket_
->StartTls("fakedomain.com"));
909 SignalSocketState(SIGNAL_CLOSE
,
910 ChromeAsyncSocket::STATE_CLOSED
,
911 ChromeAsyncSocket::ERROR_WRONGSTATE
,
916 TEST_F(ChromeAsyncSocketTest
, FailedSSLConnect
) {
917 ssl_socket_data_provider_
.connect
=
918 net::MockConnect(net::ASYNC
, net::ERR_CERT_COMMON_NAME_INVALID
),
920 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
924 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
925 message_loop_
->RunUntilIdle();
926 ExpectSignalSocketState(
928 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
929 ChromeAsyncSocket::ERROR_WINSOCK
,
930 net::ERR_CERT_COMMON_NAME_INVALID
));
932 EXPECT_TRUE(chrome_async_socket_
->Close());
936 TEST_F(ChromeAsyncSocketTest
, ReadDuringSSLConnecting
) {
937 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
940 EXPECT_EQ(kReadData
, DrainRead(1));
942 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
945 // Shouldn't do anything.
946 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
949 size_t len_read
= 10000U;
950 EXPECT_TRUE(chrome_async_socket_
->Read(buf
, sizeof(buf
), &len_read
));
951 EXPECT_EQ(0U, len_read
);
953 message_loop_
->RunUntilIdle();
954 ExpectSSLConnectSignal();
955 ExpectSSLReadSignal();
957 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN
);
960 EXPECT_TRUE(chrome_async_socket_
->Read(buf
, sizeof(buf
), &len_read
));
961 EXPECT_EQ(kReadData
, std::string(buf
, len_read
));
963 DoSSLCloseOpenedNoError();
966 TEST_F(ChromeAsyncSocketTest
, WriteDuringSSLConnecting
) {
967 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
971 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
973 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_CONNECTING
);
975 async_socket_data_provider_
.AddWrite(
976 net::MockWrite(net::ASYNC
, kWriteData
, 3));
978 // Shouldn't do anything.
979 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
981 // TODO(akalin): Figure out how to test that the write happens
982 // *after* the SSL connect.
984 message_loop_
->RunUntilIdle();
985 ExpectSSLConnectSignal();
988 message_loop_
->RunUntilIdle();
990 DoSSLCloseOpenedNoError();
993 TEST_F(ChromeAsyncSocketTest
, SSLConnectDuringPendingRead
) {
997 EXPECT_FALSE(chrome_async_socket_
->StartTls("fakedomain.com"));
1000 SignalSocketState(SIGNAL_CLOSE
,
1001 ChromeAsyncSocket::STATE_CLOSED
,
1002 ChromeAsyncSocket::ERROR_WRONGSTATE
,
1007 TEST_F(ChromeAsyncSocketTest
, SSLConnectDuringPostedWrite
) {
1008 EXPECT_DEBUG_DEATH({
1011 async_socket_data_provider_
.AddWrite(
1012 net::MockWrite(net::ASYNC
, kWriteData
, 3));
1013 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
1015 EXPECT_FALSE(chrome_async_socket_
->StartTls("fakedomain.com"));
1017 message_loop_
->RunUntilIdle();
1020 SignalSocketState(SIGNAL_CLOSE
,
1021 ChromeAsyncSocket::STATE_CLOSED
,
1022 ChromeAsyncSocket::ERROR_WRONGSTATE
,
1027 // After this we can assume SSL connect works as expected.
1029 TEST_F(ChromeAsyncSocketTest
, SSLRead
) {
1031 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
1032 message_loop_
->RunUntilIdle();
1034 ExpectSSLReadSignal();
1037 EXPECT_EQ(kReadData
, DrainRead(1));
1039 message_loop_
->RunUntilIdle();
1041 DoSSLCloseOpenedNoError();
1044 TEST_F(ChromeAsyncSocketTest
, SSLSyncWrite
) {
1045 async_socket_data_provider_
.AddWrite(
1046 net::MockWrite(net::SYNCHRONOUS
, kWriteData
, 3));
1047 async_socket_data_provider_
.AddWrite(
1048 net::MockWrite(net::SYNCHRONOUS
, kWriteData
+ 3, 5));
1049 async_socket_data_provider_
.AddWrite(
1050 net::MockWrite(net::SYNCHRONOUS
,
1051 kWriteData
+ 8, arraysize(kWriteData
) - 8));
1054 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
1055 message_loop_
->RunUntilIdle();
1056 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 3, 5));
1057 message_loop_
->RunUntilIdle();
1058 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 8,
1059 arraysize(kWriteData
) - 8));
1060 message_loop_
->RunUntilIdle();
1064 DoSSLCloseOpenedNoError();
1067 TEST_F(ChromeAsyncSocketTest
, SSLAsyncWrite
) {
1070 async_socket_data_provider_
.AddWrite(
1071 net::MockWrite(net::ASYNC
, kWriteData
, 3));
1072 async_socket_data_provider_
.AddWrite(
1073 net::MockWrite(net::ASYNC
, kWriteData
+ 3, 5));
1074 async_socket_data_provider_
.AddWrite(
1075 net::MockWrite(net::ASYNC
, kWriteData
+ 8, arraysize(kWriteData
) - 8));
1077 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
1078 message_loop_
->RunUntilIdle();
1079 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 3, 5));
1080 message_loop_
->RunUntilIdle();
1081 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 8,
1082 arraysize(kWriteData
) - 8));
1083 message_loop_
->RunUntilIdle();
1087 DoSSLCloseOpenedNoError();
1092 } // namespace jingle_glue