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
OnRead() 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
);
96 bool AllReadDataConsumed() const override
{
97 return reads_
.empty();
100 bool AllWriteDataConsumed() const override
{
101 return writes_
.empty();
105 std::deque
<net::MockRead
> reads_
;
106 bool has_pending_read_
;
108 std::deque
<net::MockWrite
> writes_
;
110 DISALLOW_COPY_AND_ASSIGN(AsyncSocketDataProvider
);
113 class MockXmppClientSocketFactory
: public ResolvingClientSocketFactory
{
115 MockXmppClientSocketFactory(
116 net::ClientSocketFactory
* mock_client_socket_factory
,
117 const net::AddressList
& address_list
)
118 : mock_client_socket_factory_(mock_client_socket_factory
),
119 address_list_(address_list
),
120 cert_verifier_(new net::MockCertVerifier
),
121 transport_security_state_(new net::TransportSecurityState
) {
124 // ResolvingClientSocketFactory implementation.
125 scoped_ptr
<net::StreamSocket
> CreateTransportClientSocket(
126 const net::HostPortPair
& host_and_port
) override
{
127 return mock_client_socket_factory_
->CreateTransportClientSocket(
128 address_list_
, NULL
, net::NetLog::Source());
131 scoped_ptr
<net::SSLClientSocket
> CreateSSLClientSocket(
132 scoped_ptr
<net::ClientSocketHandle
> transport_socket
,
133 const net::HostPortPair
& host_and_port
) override
{
134 net::SSLClientSocketContext context
;
135 context
.cert_verifier
= cert_verifier_
.get();
136 context
.transport_security_state
= transport_security_state_
.get();
137 return mock_client_socket_factory_
->CreateSSLClientSocket(
138 transport_socket
.Pass(), host_and_port
, ssl_config_
, context
);
142 scoped_ptr
<net::ClientSocketFactory
> mock_client_socket_factory_
;
143 net::AddressList address_list_
;
144 net::SSLConfig ssl_config_
;
145 scoped_ptr
<net::CertVerifier
> cert_verifier_
;
146 scoped_ptr
<net::TransportSecurityState
> transport_security_state_
;
149 class ChromeAsyncSocketTest
150 : public testing::Test
,
151 public sigslot::has_slots
<> {
153 ChromeAsyncSocketTest()
154 : ssl_socket_data_provider_(net::ASYNC
, net::OK
),
155 addr_("localhost", 35) {
156 // GTest death tests execute in a fork()ed but not exec()ed process.
157 // On OS X a CoreFoundation-backed message loop will exit with a
158 // __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__
160 // Explicitly create a MessagePumpDefault which can run in this enivronment.
161 scoped_ptr
<base::MessagePump
> pump(new base::MessagePumpDefault());
162 message_loop_
.reset(new base::MessageLoop(pump
.Pass()));
165 ~ChromeAsyncSocketTest() override
{}
167 void SetUp() override
{
168 scoped_ptr
<net::MockClientSocketFactory
> mock_client_socket_factory(
169 new net::MockClientSocketFactory());
170 mock_client_socket_factory
->AddSocketDataProvider(
171 &async_socket_data_provider_
);
172 mock_client_socket_factory
->AddSSLSocketDataProvider(
173 &ssl_socket_data_provider_
);
175 // Fake DNS resolution for |addr_| and pass it to the factory.
176 net::IPAddressNumber resolved_addr
;
177 EXPECT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &resolved_addr
));
178 const net::AddressList address_list
=
179 net::AddressList::CreateFromIPAddress(resolved_addr
, addr_
.port());
180 scoped_ptr
<MockXmppClientSocketFactory
> mock_xmpp_client_socket_factory(
181 new MockXmppClientSocketFactory(
182 mock_client_socket_factory
.release(),
184 chrome_async_socket_
.reset(
185 new ChromeAsyncSocket(mock_xmpp_client_socket_factory
.release(),
188 chrome_async_socket_
->SignalConnected
.connect(
189 this, &ChromeAsyncSocketTest::OnConnect
);
190 chrome_async_socket_
->SignalSSLConnected
.connect(
191 this, &ChromeAsyncSocketTest::OnSSLConnect
);
192 chrome_async_socket_
->SignalClosed
.connect(
193 this, &ChromeAsyncSocketTest::OnClose
);
194 chrome_async_socket_
->SignalRead
.connect(
195 this, &ChromeAsyncSocketTest::OnRead
);
196 chrome_async_socket_
->SignalError
.connect(
197 this, &ChromeAsyncSocketTest::OnError
);
200 void TearDown() override
{
201 // Run any tasks that we forgot to pump.
202 message_loop_
->RunUntilIdle();
205 chrome_async_socket_
.reset();
216 // Helper struct that records the state at the time of a signal.
218 struct SignalSocketState
{
220 : signal(SIGNAL_ERROR
),
221 state(ChromeAsyncSocket::STATE_CLOSED
),
222 error(ChromeAsyncSocket::ERROR_NONE
),
223 net_error(net::OK
) {}
227 ChromeAsyncSocket::State state
,
228 ChromeAsyncSocket::Error error
,
229 net::Error net_error
)
233 net_error(net_error
) {}
235 bool IsEqual(const SignalSocketState
& other
) const {
237 (signal
== other
.signal
) &&
238 (state
== other
.state
) &&
239 (error
== other
.error
) &&
240 (net_error
== other
.net_error
);
243 static SignalSocketState
FromAsyncSocket(
245 buzz::AsyncSocket
* async_socket
) {
246 return SignalSocketState(signal
,
247 async_socket
->state(),
248 async_socket
->error(),
249 static_cast<net::Error
>(
250 async_socket
->GetError()));
253 static SignalSocketState
NoError(
254 Signal signal
, buzz::AsyncSocket::State state
) {
255 return SignalSocketState(signal
, state
,
256 buzz::AsyncSocket::ERROR_NONE
,
261 ChromeAsyncSocket::State state
;
262 ChromeAsyncSocket::Error error
;
263 net::Error net_error
;
266 void CaptureSocketState(Signal signal
) {
267 signal_socket_states_
.push_back(
268 SignalSocketState::FromAsyncSocket(
269 signal
, chrome_async_socket_
.get()));
273 CaptureSocketState(SIGNAL_CONNECT
);
276 void OnSSLConnect() {
277 CaptureSocketState(SIGNAL_SSL_CONNECT
);
281 CaptureSocketState(SIGNAL_CLOSE
);
285 CaptureSocketState(SIGNAL_READ
);
292 // State expect functions.
294 void ExpectState(ChromeAsyncSocket::State state
,
295 ChromeAsyncSocket::Error error
,
296 net::Error net_error
) {
297 EXPECT_EQ(state
, chrome_async_socket_
->state());
298 EXPECT_EQ(error
, chrome_async_socket_
->error());
299 EXPECT_EQ(net_error
, chrome_async_socket_
->GetError());
302 void ExpectNonErrorState(ChromeAsyncSocket::State state
) {
303 ExpectState(state
, ChromeAsyncSocket::ERROR_NONE
, net::OK
);
306 void ExpectErrorState(ChromeAsyncSocket::State state
,
307 ChromeAsyncSocket::Error error
) {
308 ExpectState(state
, error
, net::OK
);
311 void ExpectClosed() {
312 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED
);
315 // Signal expect functions.
317 void ExpectNoSignal() {
318 if (!signal_socket_states_
.empty()) {
319 ADD_FAILURE() << signal_socket_states_
.front().signal
;
323 void ExpectSignalSocketState(
324 SignalSocketState expected_signal_socket_state
) {
325 if (signal_socket_states_
.empty()) {
326 ADD_FAILURE() << expected_signal_socket_state
.signal
;
329 EXPECT_TRUE(expected_signal_socket_state
.IsEqual(
330 signal_socket_states_
.front()))
331 << signal_socket_states_
.front().signal
;
332 signal_socket_states_
.pop_front();
335 void ExpectReadSignal() {
336 ExpectSignalSocketState(
337 SignalSocketState::NoError(
338 SIGNAL_READ
, ChromeAsyncSocket::STATE_OPEN
));
341 void ExpectSSLConnectSignal() {
342 ExpectSignalSocketState(
343 SignalSocketState::NoError(SIGNAL_SSL_CONNECT
,
344 ChromeAsyncSocket::STATE_TLS_OPEN
));
347 void ExpectSSLReadSignal() {
348 ExpectSignalSocketState(
349 SignalSocketState::NoError(
350 SIGNAL_READ
, ChromeAsyncSocket::STATE_TLS_OPEN
));
353 // Open/close utility functions.
355 void DoOpenClosed() {
357 async_socket_data_provider_
.set_connect_data(
358 net::MockConnect(net::SYNCHRONOUS
, net::OK
));
359 EXPECT_TRUE(chrome_async_socket_
->Connect(addr_
));
360 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING
);
362 message_loop_
->RunUntilIdle();
363 // We may not necessarily be open; may have been other events
365 ExpectSignalSocketState(
366 SignalSocketState::NoError(
367 SIGNAL_CONNECT
, ChromeAsyncSocket::STATE_OPEN
));
370 void DoCloseOpened(SignalSocketState expected_signal_socket_state
) {
371 // We may be in an error state, so just compare state().
372 EXPECT_EQ(ChromeAsyncSocket::STATE_OPEN
, chrome_async_socket_
->state());
373 EXPECT_TRUE(chrome_async_socket_
->Close());
374 ExpectSignalSocketState(expected_signal_socket_state
);
375 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED
);
378 void DoCloseOpenedNoError() {
380 SignalSocketState::NoError(
381 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
384 void DoSSLOpenClosed() {
385 const char kDummyData
[] = "dummy_data";
386 async_socket_data_provider_
.AddRead(net::MockRead(kDummyData
));
389 EXPECT_EQ(kDummyData
, DrainRead(1));
391 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
392 message_loop_
->RunUntilIdle();
393 ExpectSSLConnectSignal();
395 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN
);
398 void DoSSLCloseOpened(SignalSocketState expected_signal_socket_state
) {
399 // We may be in an error state, so just compare state().
400 EXPECT_EQ(ChromeAsyncSocket::STATE_TLS_OPEN
,
401 chrome_async_socket_
->state());
402 EXPECT_TRUE(chrome_async_socket_
->Close());
403 ExpectSignalSocketState(expected_signal_socket_state
);
404 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED
);
407 void DoSSLCloseOpenedNoError() {
409 SignalSocketState::NoError(
410 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
413 // Read utility fucntions.
415 std::string
DrainRead(size_t buf_size
) {
417 scoped_ptr
<char[]> buf(new char[buf_size
]);
421 chrome_async_socket_
->Read(buf
.get(), buf_size
, &len_read
);
426 if (len_read
== 0U) {
429 read
.append(buf
.get(), len_read
);
434 // ChromeAsyncSocket expects a message loop.
435 scoped_ptr
<base::MessageLoop
> message_loop_
;
437 AsyncSocketDataProvider async_socket_data_provider_
;
438 net::SSLSocketDataProvider ssl_socket_data_provider_
;
440 scoped_ptr
<ChromeAsyncSocket
> chrome_async_socket_
;
441 std::deque
<SignalSocketState
> signal_socket_states_
;
442 const rtc::SocketAddress addr_
;
445 DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocketTest
);
448 TEST_F(ChromeAsyncSocketTest
, InitialState
) {
453 TEST_F(ChromeAsyncSocketTest
, EmptyClose
) {
455 EXPECT_TRUE(chrome_async_socket_
->Close());
459 TEST_F(ChromeAsyncSocketTest
, ImmediateConnectAndClose
) {
462 ExpectNonErrorState(ChromeAsyncSocket::STATE_OPEN
);
464 DoCloseOpenedNoError();
467 // After this, no need to test immediate successful connect and
468 // Close() so thoroughly.
470 TEST_F(ChromeAsyncSocketTest
, DoubleClose
) {
473 EXPECT_TRUE(chrome_async_socket_
->Close());
475 ExpectSignalSocketState(
476 SignalSocketState::NoError(
477 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
479 EXPECT_TRUE(chrome_async_socket_
->Close());
483 TEST_F(ChromeAsyncSocketTest
, NoHostnameConnect
) {
484 rtc::IPAddress ip_address
;
485 EXPECT_TRUE(rtc::IPFromString("127.0.0.1", &ip_address
));
486 const rtc::SocketAddress
no_hostname_addr(ip_address
, addr_
.port());
487 EXPECT_FALSE(chrome_async_socket_
->Connect(no_hostname_addr
));
488 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED
,
489 ChromeAsyncSocket::ERROR_DNS
);
491 EXPECT_TRUE(chrome_async_socket_
->Close());
495 TEST_F(ChromeAsyncSocketTest
, ZeroPortConnect
) {
496 const rtc::SocketAddress
zero_port_addr(addr_
.hostname(), 0);
497 EXPECT_FALSE(chrome_async_socket_
->Connect(zero_port_addr
));
498 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED
,
499 ChromeAsyncSocket::ERROR_DNS
);
501 EXPECT_TRUE(chrome_async_socket_
->Close());
505 TEST_F(ChromeAsyncSocketTest
, DoubleConnect
) {
509 EXPECT_FALSE(chrome_async_socket_
->Connect(addr_
));
510 ExpectErrorState(ChromeAsyncSocket::STATE_OPEN
,
511 ChromeAsyncSocket::ERROR_WRONGSTATE
);
514 SignalSocketState(SIGNAL_CLOSE
,
515 ChromeAsyncSocket::STATE_CLOSED
,
516 ChromeAsyncSocket::ERROR_WRONGSTATE
,
518 }, "non-closed socket");
521 TEST_F(ChromeAsyncSocketTest
, ImmediateConnectCloseBeforeRead
) {
524 EXPECT_TRUE(chrome_async_socket_
->Close());
526 ExpectSignalSocketState(
527 SignalSocketState::NoError(
528 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
530 message_loop_
->RunUntilIdle();
533 TEST_F(ChromeAsyncSocketTest
, HangingConnect
) {
534 EXPECT_TRUE(chrome_async_socket_
->Connect(addr_
));
535 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING
);
538 EXPECT_TRUE(chrome_async_socket_
->Close());
540 ExpectSignalSocketState(
541 SignalSocketState::NoError(
542 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
545 TEST_F(ChromeAsyncSocketTest
, PendingConnect
) {
546 async_socket_data_provider_
.set_connect_data(
547 net::MockConnect(net::ASYNC
, net::OK
));
548 EXPECT_TRUE(chrome_async_socket_
->Connect(addr_
));
549 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING
);
552 message_loop_
->RunUntilIdle();
553 ExpectNonErrorState(ChromeAsyncSocket::STATE_OPEN
);
554 ExpectSignalSocketState(
555 SignalSocketState::NoError(
556 SIGNAL_CONNECT
, ChromeAsyncSocket::STATE_OPEN
));
559 message_loop_
->RunUntilIdle();
561 DoCloseOpenedNoError();
564 // After this no need to test successful pending connect so
567 TEST_F(ChromeAsyncSocketTest
, PendingConnectCloseBeforeRead
) {
568 async_socket_data_provider_
.set_connect_data(
569 net::MockConnect(net::ASYNC
, net::OK
));
570 EXPECT_TRUE(chrome_async_socket_
->Connect(addr_
));
572 message_loop_
->RunUntilIdle();
573 ExpectSignalSocketState(
574 SignalSocketState::NoError(
575 SIGNAL_CONNECT
, ChromeAsyncSocket::STATE_OPEN
));
577 DoCloseOpenedNoError();
579 message_loop_
->RunUntilIdle();
582 TEST_F(ChromeAsyncSocketTest
, PendingConnectError
) {
583 async_socket_data_provider_
.set_connect_data(
584 net::MockConnect(net::ASYNC
, net::ERR_TIMED_OUT
));
585 EXPECT_TRUE(chrome_async_socket_
->Connect(addr_
));
587 message_loop_
->RunUntilIdle();
589 ExpectSignalSocketState(
591 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
592 ChromeAsyncSocket::ERROR_WINSOCK
, net::ERR_TIMED_OUT
));
595 // After this we can assume Connect() and Close() work as expected.
597 TEST_F(ChromeAsyncSocketTest
, EmptyRead
) {
601 size_t len_read
= 10000U;
602 EXPECT_TRUE(chrome_async_socket_
->Read(buf
, sizeof(buf
), &len_read
));
603 EXPECT_EQ(0U, len_read
);
605 DoCloseOpenedNoError();
608 TEST_F(ChromeAsyncSocketTest
, WrongRead
) {
610 async_socket_data_provider_
.set_connect_data(
611 net::MockConnect(net::ASYNC
, net::OK
));
612 EXPECT_TRUE(chrome_async_socket_
->Connect(addr_
));
613 ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING
);
618 EXPECT_FALSE(chrome_async_socket_
->Read(buf
, sizeof(buf
), &len_read
));
619 ExpectErrorState(ChromeAsyncSocket::STATE_CONNECTING
,
620 ChromeAsyncSocket::ERROR_WRONGSTATE
);
621 EXPECT_TRUE(chrome_async_socket_
->Close());
622 ExpectSignalSocketState(
624 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
625 ChromeAsyncSocket::ERROR_WRONGSTATE
, net::OK
));
629 TEST_F(ChromeAsyncSocketTest
, WrongReadClosed
) {
632 EXPECT_FALSE(chrome_async_socket_
->Read(buf
, sizeof(buf
), &len_read
));
633 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED
,
634 ChromeAsyncSocket::ERROR_WRONGSTATE
);
635 EXPECT_TRUE(chrome_async_socket_
->Close());
638 const char kReadData
[] = "mydatatoread";
640 TEST_F(ChromeAsyncSocketTest
, Read
) {
641 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
647 EXPECT_EQ(kReadData
, DrainRead(1));
649 message_loop_
->RunUntilIdle();
651 DoCloseOpenedNoError();
654 TEST_F(ChromeAsyncSocketTest
, ReadTwice
) {
655 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
661 EXPECT_EQ(kReadData
, DrainRead(1));
663 message_loop_
->RunUntilIdle();
665 const char kReadData2
[] = "mydatatoread2";
666 async_socket_data_provider_
.AddRead(net::MockRead(kReadData2
));
671 EXPECT_EQ(kReadData2
, DrainRead(1));
673 DoCloseOpenedNoError();
676 TEST_F(ChromeAsyncSocketTest
, ReadError
) {
677 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
683 EXPECT_EQ(kReadData
, DrainRead(1));
685 message_loop_
->RunUntilIdle();
687 async_socket_data_provider_
.AddRead(
688 net::MockRead(net::SYNCHRONOUS
, net::ERR_TIMED_OUT
));
690 ExpectSignalSocketState(
692 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
693 ChromeAsyncSocket::ERROR_WINSOCK
, net::ERR_TIMED_OUT
));
696 TEST_F(ChromeAsyncSocketTest
, ReadEmpty
) {
697 async_socket_data_provider_
.AddRead(net::MockRead(""));
700 ExpectSignalSocketState(
701 SignalSocketState::NoError(
702 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
705 TEST_F(ChromeAsyncSocketTest
, PendingRead
) {
710 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
712 ExpectSignalSocketState(
713 SignalSocketState::NoError(
714 SIGNAL_READ
, ChromeAsyncSocket::STATE_OPEN
));
717 EXPECT_EQ(kReadData
, DrainRead(1));
719 message_loop_
->RunUntilIdle();
721 DoCloseOpenedNoError();
724 TEST_F(ChromeAsyncSocketTest
, PendingEmptyRead
) {
729 async_socket_data_provider_
.AddRead(net::MockRead(""));
731 ExpectSignalSocketState(
732 SignalSocketState::NoError(
733 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
));
736 TEST_F(ChromeAsyncSocketTest
, PendingReadError
) {
741 async_socket_data_provider_
.AddRead(
742 net::MockRead(net::ASYNC
, net::ERR_TIMED_OUT
));
744 ExpectSignalSocketState(
746 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
747 ChromeAsyncSocket::ERROR_WINSOCK
, net::ERR_TIMED_OUT
));
750 // After this we can assume non-SSL Read() works as expected.
752 TEST_F(ChromeAsyncSocketTest
, WrongWrite
) {
754 std::string
data("foo");
755 EXPECT_FALSE(chrome_async_socket_
->Write(data
.data(), data
.size()));
756 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED
,
757 ChromeAsyncSocket::ERROR_WRONGSTATE
);
758 EXPECT_TRUE(chrome_async_socket_
->Close());
762 const char kWriteData
[] = "mydatatowrite";
764 TEST_F(ChromeAsyncSocketTest
, SyncWrite
) {
765 async_socket_data_provider_
.AddWrite(
766 net::MockWrite(net::SYNCHRONOUS
, kWriteData
, 3));
767 async_socket_data_provider_
.AddWrite(
768 net::MockWrite(net::SYNCHRONOUS
, kWriteData
+ 3, 5));
769 async_socket_data_provider_
.AddWrite(
770 net::MockWrite(net::SYNCHRONOUS
,
771 kWriteData
+ 8, arraysize(kWriteData
) - 8));
774 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
775 message_loop_
->RunUntilIdle();
776 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 3, 5));
777 message_loop_
->RunUntilIdle();
778 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 8,
779 arraysize(kWriteData
) - 8));
780 message_loop_
->RunUntilIdle();
784 DoCloseOpenedNoError();
787 TEST_F(ChromeAsyncSocketTest
, AsyncWrite
) {
790 async_socket_data_provider_
.AddWrite(
791 net::MockWrite(net::ASYNC
, kWriteData
, 3));
792 async_socket_data_provider_
.AddWrite(
793 net::MockWrite(net::ASYNC
, kWriteData
+ 3, 5));
794 async_socket_data_provider_
.AddWrite(
795 net::MockWrite(net::ASYNC
, kWriteData
+ 8, arraysize(kWriteData
) - 8));
797 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
798 message_loop_
->RunUntilIdle();
799 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 3, 5));
800 message_loop_
->RunUntilIdle();
801 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 8,
802 arraysize(kWriteData
) - 8));
803 message_loop_
->RunUntilIdle();
807 DoCloseOpenedNoError();
810 TEST_F(ChromeAsyncSocketTest
, AsyncWriteError
) {
813 async_socket_data_provider_
.AddWrite(
814 net::MockWrite(net::ASYNC
, kWriteData
, 3));
815 async_socket_data_provider_
.AddWrite(
816 net::MockWrite(net::ASYNC
, kWriteData
+ 3, 5));
817 async_socket_data_provider_
.AddWrite(
818 net::MockWrite(net::ASYNC
, net::ERR_TIMED_OUT
));
820 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
821 message_loop_
->RunUntilIdle();
822 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 3, 5));
823 message_loop_
->RunUntilIdle();
824 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 8,
825 arraysize(kWriteData
) - 8));
826 message_loop_
->RunUntilIdle();
828 ExpectSignalSocketState(
830 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
831 ChromeAsyncSocket::ERROR_WINSOCK
, net::ERR_TIMED_OUT
));
834 TEST_F(ChromeAsyncSocketTest
, LargeWrite
) {
838 std::string
large_data(100, 'x');
839 EXPECT_FALSE(chrome_async_socket_
->Write(large_data
.data(),
841 ExpectState(ChromeAsyncSocket::STATE_OPEN
,
842 ChromeAsyncSocket::ERROR_WINSOCK
,
843 net::ERR_INSUFFICIENT_RESOURCES
);
846 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
847 ChromeAsyncSocket::ERROR_WINSOCK
,
848 net::ERR_INSUFFICIENT_RESOURCES
));
849 }, "exceed the max write buffer");
852 TEST_F(ChromeAsyncSocketTest
, LargeAccumulatedWrite
) {
856 std::string
data(15, 'x');
857 EXPECT_TRUE(chrome_async_socket_
->Write(data
.data(), data
.size()));
858 EXPECT_FALSE(chrome_async_socket_
->Write(data
.data(), data
.size()));
859 ExpectState(ChromeAsyncSocket::STATE_OPEN
,
860 ChromeAsyncSocket::ERROR_WINSOCK
,
861 net::ERR_INSUFFICIENT_RESOURCES
);
864 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
865 ChromeAsyncSocket::ERROR_WINSOCK
,
866 net::ERR_INSUFFICIENT_RESOURCES
));
867 }, "exceed the max write buffer");
870 // After this we can assume non-SSL I/O works as expected.
872 TEST_F(ChromeAsyncSocketTest
, HangingSSLConnect
) {
873 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
877 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
880 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_CONNECTING
);
881 EXPECT_TRUE(chrome_async_socket_
->Close());
882 ExpectSignalSocketState(
883 SignalSocketState::NoError(SIGNAL_CLOSE
,
884 ChromeAsyncSocket::STATE_CLOSED
));
885 ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED
);
888 TEST_F(ChromeAsyncSocketTest
, ImmediateSSLConnect
) {
889 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
893 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
894 message_loop_
->RunUntilIdle();
895 ExpectSSLConnectSignal();
897 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN
);
899 DoSSLCloseOpenedNoError();
902 TEST_F(ChromeAsyncSocketTest
, DoubleSSLConnect
) {
904 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
908 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
909 message_loop_
->RunUntilIdle();
910 ExpectSSLConnectSignal();
912 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN
);
914 EXPECT_FALSE(chrome_async_socket_
->StartTls("fakedomain.com"));
917 SignalSocketState(SIGNAL_CLOSE
,
918 ChromeAsyncSocket::STATE_CLOSED
,
919 ChromeAsyncSocket::ERROR_WRONGSTATE
,
924 TEST_F(ChromeAsyncSocketTest
, FailedSSLConnect
) {
925 ssl_socket_data_provider_
.connect
=
926 net::MockConnect(net::ASYNC
, net::ERR_CERT_COMMON_NAME_INVALID
),
928 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
932 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
933 message_loop_
->RunUntilIdle();
934 ExpectSignalSocketState(
936 SIGNAL_CLOSE
, ChromeAsyncSocket::STATE_CLOSED
,
937 ChromeAsyncSocket::ERROR_WINSOCK
,
938 net::ERR_CERT_COMMON_NAME_INVALID
));
940 EXPECT_TRUE(chrome_async_socket_
->Close());
944 TEST_F(ChromeAsyncSocketTest
, ReadDuringSSLConnecting
) {
945 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
948 EXPECT_EQ(kReadData
, DrainRead(1));
950 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
953 // Shouldn't do anything.
954 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
957 size_t len_read
= 10000U;
958 EXPECT_TRUE(chrome_async_socket_
->Read(buf
, sizeof(buf
), &len_read
));
959 EXPECT_EQ(0U, len_read
);
961 message_loop_
->RunUntilIdle();
962 ExpectSSLConnectSignal();
963 ExpectSSLReadSignal();
965 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN
);
968 EXPECT_TRUE(chrome_async_socket_
->Read(buf
, sizeof(buf
), &len_read
));
969 EXPECT_EQ(kReadData
, std::string(buf
, len_read
));
971 DoSSLCloseOpenedNoError();
974 TEST_F(ChromeAsyncSocketTest
, WriteDuringSSLConnecting
) {
975 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
979 EXPECT_TRUE(chrome_async_socket_
->StartTls("fakedomain.com"));
981 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_CONNECTING
);
983 async_socket_data_provider_
.AddWrite(
984 net::MockWrite(net::ASYNC
, kWriteData
, 3));
986 // Shouldn't do anything.
987 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
989 // TODO(akalin): Figure out how to test that the write happens
990 // *after* the SSL connect.
992 message_loop_
->RunUntilIdle();
993 ExpectSSLConnectSignal();
996 message_loop_
->RunUntilIdle();
998 DoSSLCloseOpenedNoError();
1001 TEST_F(ChromeAsyncSocketTest
, SSLConnectDuringPendingRead
) {
1002 EXPECT_DEBUG_DEATH({
1005 EXPECT_FALSE(chrome_async_socket_
->StartTls("fakedomain.com"));
1008 SignalSocketState(SIGNAL_CLOSE
,
1009 ChromeAsyncSocket::STATE_CLOSED
,
1010 ChromeAsyncSocket::ERROR_WRONGSTATE
,
1015 TEST_F(ChromeAsyncSocketTest
, SSLConnectDuringPostedWrite
) {
1016 EXPECT_DEBUG_DEATH({
1019 async_socket_data_provider_
.AddWrite(
1020 net::MockWrite(net::ASYNC
, kWriteData
, 3));
1021 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
1023 EXPECT_FALSE(chrome_async_socket_
->StartTls("fakedomain.com"));
1025 message_loop_
->RunUntilIdle();
1028 SignalSocketState(SIGNAL_CLOSE
,
1029 ChromeAsyncSocket::STATE_CLOSED
,
1030 ChromeAsyncSocket::ERROR_WRONGSTATE
,
1035 // After this we can assume SSL connect works as expected.
1037 TEST_F(ChromeAsyncSocketTest
, SSLRead
) {
1039 async_socket_data_provider_
.AddRead(net::MockRead(kReadData
));
1040 message_loop_
->RunUntilIdle();
1042 ExpectSSLReadSignal();
1045 EXPECT_EQ(kReadData
, DrainRead(1));
1047 message_loop_
->RunUntilIdle();
1049 DoSSLCloseOpenedNoError();
1052 TEST_F(ChromeAsyncSocketTest
, SSLSyncWrite
) {
1053 async_socket_data_provider_
.AddWrite(
1054 net::MockWrite(net::SYNCHRONOUS
, kWriteData
, 3));
1055 async_socket_data_provider_
.AddWrite(
1056 net::MockWrite(net::SYNCHRONOUS
, kWriteData
+ 3, 5));
1057 async_socket_data_provider_
.AddWrite(
1058 net::MockWrite(net::SYNCHRONOUS
,
1059 kWriteData
+ 8, arraysize(kWriteData
) - 8));
1062 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
1063 message_loop_
->RunUntilIdle();
1064 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 3, 5));
1065 message_loop_
->RunUntilIdle();
1066 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 8,
1067 arraysize(kWriteData
) - 8));
1068 message_loop_
->RunUntilIdle();
1072 DoSSLCloseOpenedNoError();
1075 TEST_F(ChromeAsyncSocketTest
, SSLAsyncWrite
) {
1078 async_socket_data_provider_
.AddWrite(
1079 net::MockWrite(net::ASYNC
, kWriteData
, 3));
1080 async_socket_data_provider_
.AddWrite(
1081 net::MockWrite(net::ASYNC
, kWriteData
+ 3, 5));
1082 async_socket_data_provider_
.AddWrite(
1083 net::MockWrite(net::ASYNC
, kWriteData
+ 8, arraysize(kWriteData
) - 8));
1085 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
, 3));
1086 message_loop_
->RunUntilIdle();
1087 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 3, 5));
1088 message_loop_
->RunUntilIdle();
1089 EXPECT_TRUE(chrome_async_socket_
->Write(kWriteData
+ 8,
1090 arraysize(kWriteData
) - 8));
1091 message_loop_
->RunUntilIdle();
1095 DoSSLCloseOpenedNoError();
1100 } // namespace jingle_glue