Switch all the page_set in perf/page_set to story_set.
[chromium-blink-merge.git] / jingle / glue / chrome_async_socket_unittest.cc
bloba7855533d2af355f645896f8d94786aca19e3ac7
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 "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 {
32 namespace {
34 // Data provider that handles reads/writes for ChromeAsyncSocket.
35 class AsyncSocketDataProvider : public net::SocketDataProvider {
36 public:
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 {
47 if (reads_.empty()) {
48 DCHECK(!has_pending_read_);
49 has_pending_read_ = true;
50 const net::MockRead pending_read(net::SYNCHRONOUS, net::ERR_IO_PENDING);
51 return pending_read;
53 net::MockRead mock_read = reads_.front();
54 reads_.pop_front();
55 return mock_read;
58 // Simply pops the next write and, if applicable, compares it to
59 // |data|.
60 net::MockWriteResult OnWrite(const std::string& data) override {
61 DCHECK(!writes_.empty());
62 net::MockWrite mock_write = writes_.front();
63 writes_.pop_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
76 // read/write events.
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;
86 return;
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();
104 private:
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 {
114 public:
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);
141 private:
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<> {
152 protected:
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__
159 // when called.
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(),
183 address_list));
184 chrome_async_socket_.reset(
185 new ChromeAsyncSocket(mock_xmpp_client_socket_factory.release(),
186 14, 20)),
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();
203 ExpectClosed();
204 ExpectNoSignal();
205 chrome_async_socket_.reset();
208 enum Signal {
209 SIGNAL_CONNECT,
210 SIGNAL_SSL_CONNECT,
211 SIGNAL_CLOSE,
212 SIGNAL_READ,
213 SIGNAL_ERROR,
216 // Helper struct that records the state at the time of a signal.
218 struct SignalSocketState {
219 SignalSocketState()
220 : signal(SIGNAL_ERROR),
221 state(ChromeAsyncSocket::STATE_CLOSED),
222 error(ChromeAsyncSocket::ERROR_NONE),
223 net_error(net::OK) {}
225 SignalSocketState(
226 Signal signal,
227 ChromeAsyncSocket::State state,
228 ChromeAsyncSocket::Error error,
229 net::Error net_error)
230 : signal(signal),
231 state(state),
232 error(error),
233 net_error(net_error) {}
235 bool IsEqual(const SignalSocketState& other) const {
236 return
237 (signal == other.signal) &&
238 (state == other.state) &&
239 (error == other.error) &&
240 (net_error == other.net_error);
243 static SignalSocketState FromAsyncSocket(
244 Signal signal,
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,
257 net::OK);
260 Signal signal;
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()));
272 void OnConnect() {
273 CaptureSocketState(SIGNAL_CONNECT);
276 void OnSSLConnect() {
277 CaptureSocketState(SIGNAL_SSL_CONNECT);
280 void OnClose() {
281 CaptureSocketState(SIGNAL_CLOSE);
284 void OnRead() {
285 CaptureSocketState(SIGNAL_READ);
288 void OnError() {
289 ADD_FAILURE();
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;
327 return;
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() {
356 ExpectClosed();
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
364 // queued up.
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() {
379 DoCloseOpened(
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));
387 DoOpenClosed();
388 ExpectReadSignal();
389 EXPECT_EQ(kDummyData, DrainRead(1));
391 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
392 message_loop_->RunUntilIdle();
393 ExpectSSLConnectSignal();
394 ExpectNoSignal();
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() {
408 DoSSLCloseOpened(
409 SignalSocketState::NoError(
410 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
413 // Read utility fucntions.
415 std::string DrainRead(size_t buf_size) {
416 std::string read;
417 scoped_ptr<char[]> buf(new char[buf_size]);
418 size_t len_read;
419 while (true) {
420 bool success =
421 chrome_async_socket_->Read(buf.get(), buf_size, &len_read);
422 if (!success) {
423 ADD_FAILURE();
424 break;
426 if (len_read == 0U) {
427 break;
429 read.append(buf.get(), len_read);
431 return 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_;
444 private:
445 DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocketTest);
448 TEST_F(ChromeAsyncSocketTest, InitialState) {
449 ExpectClosed();
450 ExpectNoSignal();
453 TEST_F(ChromeAsyncSocketTest, EmptyClose) {
454 ExpectClosed();
455 EXPECT_TRUE(chrome_async_socket_->Close());
456 ExpectClosed();
459 TEST_F(ChromeAsyncSocketTest, ImmediateConnectAndClose) {
460 DoOpenClosed();
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) {
471 DoOpenClosed();
473 EXPECT_TRUE(chrome_async_socket_->Close());
474 ExpectClosed();
475 ExpectSignalSocketState(
476 SignalSocketState::NoError(
477 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
479 EXPECT_TRUE(chrome_async_socket_->Close());
480 ExpectClosed();
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());
492 ExpectClosed();
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());
502 ExpectClosed();
505 TEST_F(ChromeAsyncSocketTest, DoubleConnect) {
506 EXPECT_DEBUG_DEATH({
507 DoOpenClosed();
509 EXPECT_FALSE(chrome_async_socket_->Connect(addr_));
510 ExpectErrorState(ChromeAsyncSocket::STATE_OPEN,
511 ChromeAsyncSocket::ERROR_WRONGSTATE);
513 DoCloseOpened(
514 SignalSocketState(SIGNAL_CLOSE,
515 ChromeAsyncSocket::STATE_CLOSED,
516 ChromeAsyncSocket::ERROR_WRONGSTATE,
517 net::OK));
518 }, "non-closed socket");
521 TEST_F(ChromeAsyncSocketTest, ImmediateConnectCloseBeforeRead) {
522 DoOpenClosed();
524 EXPECT_TRUE(chrome_async_socket_->Close());
525 ExpectClosed();
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);
536 ExpectNoSignal();
538 EXPECT_TRUE(chrome_async_socket_->Close());
539 ExpectClosed();
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);
550 ExpectNoSignal();
552 message_loop_->RunUntilIdle();
553 ExpectNonErrorState(ChromeAsyncSocket::STATE_OPEN);
554 ExpectSignalSocketState(
555 SignalSocketState::NoError(
556 SIGNAL_CONNECT, ChromeAsyncSocket::STATE_OPEN));
557 ExpectNoSignal();
559 message_loop_->RunUntilIdle();
561 DoCloseOpenedNoError();
564 // After this no need to test successful pending connect so
565 // thoroughly.
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(
590 SignalSocketState(
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) {
598 DoOpenClosed();
600 char buf[4096];
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) {
609 EXPECT_DEBUG_DEATH({
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);
614 ExpectNoSignal();
616 char buf[4096];
617 size_t len_read;
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(
623 SignalSocketState(
624 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
625 ChromeAsyncSocket::ERROR_WRONGSTATE, net::OK));
626 }, "non-open");
629 TEST_F(ChromeAsyncSocketTest, WrongReadClosed) {
630 char buf[4096];
631 size_t len_read;
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));
642 DoOpenClosed();
644 ExpectReadSignal();
645 ExpectNoSignal();
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));
656 DoOpenClosed();
658 ExpectReadSignal();
659 ExpectNoSignal();
661 EXPECT_EQ(kReadData, DrainRead(1));
663 message_loop_->RunUntilIdle();
665 const char kReadData2[] = "mydatatoread2";
666 async_socket_data_provider_.AddRead(net::MockRead(kReadData2));
668 ExpectReadSignal();
669 ExpectNoSignal();
671 EXPECT_EQ(kReadData2, DrainRead(1));
673 DoCloseOpenedNoError();
676 TEST_F(ChromeAsyncSocketTest, ReadError) {
677 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
678 DoOpenClosed();
680 ExpectReadSignal();
681 ExpectNoSignal();
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(
691 SignalSocketState(
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(""));
698 DoOpenClosed();
700 ExpectSignalSocketState(
701 SignalSocketState::NoError(
702 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
705 TEST_F(ChromeAsyncSocketTest, PendingRead) {
706 DoOpenClosed();
708 ExpectNoSignal();
710 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
712 ExpectSignalSocketState(
713 SignalSocketState::NoError(
714 SIGNAL_READ, ChromeAsyncSocket::STATE_OPEN));
715 ExpectNoSignal();
717 EXPECT_EQ(kReadData, DrainRead(1));
719 message_loop_->RunUntilIdle();
721 DoCloseOpenedNoError();
724 TEST_F(ChromeAsyncSocketTest, PendingEmptyRead) {
725 DoOpenClosed();
727 ExpectNoSignal();
729 async_socket_data_provider_.AddRead(net::MockRead(""));
731 ExpectSignalSocketState(
732 SignalSocketState::NoError(
733 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
736 TEST_F(ChromeAsyncSocketTest, PendingReadError) {
737 DoOpenClosed();
739 ExpectNoSignal();
741 async_socket_data_provider_.AddRead(
742 net::MockRead(net::ASYNC, net::ERR_TIMED_OUT));
744 ExpectSignalSocketState(
745 SignalSocketState(
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) {
753 EXPECT_DEBUG_DEATH({
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());
759 }, "non-open");
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));
772 DoOpenClosed();
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();
782 ExpectNoSignal();
784 DoCloseOpenedNoError();
787 TEST_F(ChromeAsyncSocketTest, AsyncWrite) {
788 DoOpenClosed();
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();
805 ExpectNoSignal();
807 DoCloseOpenedNoError();
810 TEST_F(ChromeAsyncSocketTest, AsyncWriteError) {
811 DoOpenClosed();
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(
829 SignalSocketState(
830 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
831 ChromeAsyncSocket::ERROR_WINSOCK, net::ERR_TIMED_OUT));
834 TEST_F(ChromeAsyncSocketTest, LargeWrite) {
835 EXPECT_DEBUG_DEATH({
836 DoOpenClosed();
838 std::string large_data(100, 'x');
839 EXPECT_FALSE(chrome_async_socket_->Write(large_data.data(),
840 large_data.size()));
841 ExpectState(ChromeAsyncSocket::STATE_OPEN,
842 ChromeAsyncSocket::ERROR_WINSOCK,
843 net::ERR_INSUFFICIENT_RESOURCES);
844 DoCloseOpened(
845 SignalSocketState(
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) {
853 EXPECT_DEBUG_DEATH({
854 DoOpenClosed();
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);
862 DoCloseOpened(
863 SignalSocketState(
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));
874 DoOpenClosed();
875 ExpectReadSignal();
877 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
878 ExpectNoSignal();
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));
890 DoOpenClosed();
891 ExpectReadSignal();
893 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
894 message_loop_->RunUntilIdle();
895 ExpectSSLConnectSignal();
896 ExpectNoSignal();
897 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN);
899 DoSSLCloseOpenedNoError();
902 TEST_F(ChromeAsyncSocketTest, DoubleSSLConnect) {
903 EXPECT_DEBUG_DEATH({
904 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
905 DoOpenClosed();
906 ExpectReadSignal();
908 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
909 message_loop_->RunUntilIdle();
910 ExpectSSLConnectSignal();
911 ExpectNoSignal();
912 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN);
914 EXPECT_FALSE(chrome_async_socket_->StartTls("fakedomain.com"));
916 DoSSLCloseOpened(
917 SignalSocketState(SIGNAL_CLOSE,
918 ChromeAsyncSocket::STATE_CLOSED,
919 ChromeAsyncSocket::ERROR_WRONGSTATE,
920 net::OK));
921 }, "wrong state");
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));
929 DoOpenClosed();
930 ExpectReadSignal();
932 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
933 message_loop_->RunUntilIdle();
934 ExpectSignalSocketState(
935 SignalSocketState(
936 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
937 ChromeAsyncSocket::ERROR_WINSOCK,
938 net::ERR_CERT_COMMON_NAME_INVALID));
940 EXPECT_TRUE(chrome_async_socket_->Close());
941 ExpectClosed();
944 TEST_F(ChromeAsyncSocketTest, ReadDuringSSLConnecting) {
945 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
946 DoOpenClosed();
947 ExpectReadSignal();
948 EXPECT_EQ(kReadData, DrainRead(1));
950 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
951 ExpectNoSignal();
953 // Shouldn't do anything.
954 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
956 char buf[4096];
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();
964 ExpectNoSignal();
965 ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN);
967 len_read = 10000U;
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));
976 DoOpenClosed();
977 ExpectReadSignal();
979 EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
980 ExpectNoSignal();
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();
994 ExpectNoSignal();
996 message_loop_->RunUntilIdle();
998 DoSSLCloseOpenedNoError();
1001 TEST_F(ChromeAsyncSocketTest, SSLConnectDuringPendingRead) {
1002 EXPECT_DEBUG_DEATH({
1003 DoOpenClosed();
1005 EXPECT_FALSE(chrome_async_socket_->StartTls("fakedomain.com"));
1007 DoCloseOpened(
1008 SignalSocketState(SIGNAL_CLOSE,
1009 ChromeAsyncSocket::STATE_CLOSED,
1010 ChromeAsyncSocket::ERROR_WRONGSTATE,
1011 net::OK));
1012 }, "wrong state");
1015 TEST_F(ChromeAsyncSocketTest, SSLConnectDuringPostedWrite) {
1016 EXPECT_DEBUG_DEATH({
1017 DoOpenClosed();
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();
1027 DoCloseOpened(
1028 SignalSocketState(SIGNAL_CLOSE,
1029 ChromeAsyncSocket::STATE_CLOSED,
1030 ChromeAsyncSocket::ERROR_WRONGSTATE,
1031 net::OK));
1032 }, "wrong state");
1035 // After this we can assume SSL connect works as expected.
1037 TEST_F(ChromeAsyncSocketTest, SSLRead) {
1038 DoSSLOpenClosed();
1039 async_socket_data_provider_.AddRead(net::MockRead(kReadData));
1040 message_loop_->RunUntilIdle();
1042 ExpectSSLReadSignal();
1043 ExpectNoSignal();
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));
1060 DoSSLOpenClosed();
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();
1070 ExpectNoSignal();
1072 DoSSLCloseOpenedNoError();
1075 TEST_F(ChromeAsyncSocketTest, SSLAsyncWrite) {
1076 DoSSLOpenClosed();
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();
1093 ExpectNoSignal();
1095 DoSSLCloseOpenedNoError();
1098 } // namespace
1100 } // namespace jingle_glue