Home | History | Annotate | Download | only in glue
      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.
      4 
      5 #include "jingle/glue/chrome_async_socket.h"
      6 
      7 #include <deque>
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/logging.h"
     12 #include "base/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"
     28 
     29 namespace jingle_glue {
     30 
     31 namespace {
     32 
     33 // Data provider that handles reads/writes for ChromeAsyncSocket.
     34 class AsyncSocketDataProvider : public net::SocketDataProvider {
     35  public:
     36   AsyncSocketDataProvider() : has_pending_read_(false) {}
     37 
     38   virtual ~AsyncSocketDataProvider() {
     39     EXPECT_TRUE(writes_.empty());
     40     EXPECT_TRUE(reads_.empty());
     41   }
     42 
     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;
     51     }
     52     net::MockRead mock_read = reads_.front();
     53     reads_.pop_front();
     54     return mock_read;
     55   }
     56 
     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);
     65     }
     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);
     70     }
     71     return net::MockWriteResult(mock_write.mode, data.size());
     72   }
     73 
     74   // We ignore resets so we can pre-load the socket data provider with
     75   // read/write events.
     76   virtual void Reset() OVERRIDE {}
     77 
     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;
     86     }
     87     reads_.push_back(mock_read);
     88   }
     89 
     90   // Simply queues up the given write.
     91   void AddWrite(const net::MockWrite& mock_write) {
     92     writes_.push_back(mock_write);
     93   }
     94 
     95  private:
     96   std::deque<net::MockRead> reads_;
     97   bool has_pending_read_;
     98 
     99   std::deque<net::MockWrite> writes_;
    100 
    101   DISALLOW_COPY_AND_ASSIGN(AsyncSocketDataProvider);
    102 };
    103 
    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) {
    113   }
    114 
    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());
    120   }
    121 
    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);
    130   }
    131 
    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_;
    138 };
    139 
    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) {}
    147 
    148   virtual ~ChromeAsyncSocketTest() {}
    149 
    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_);
    157 
    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)),
    170 
    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);
    181   }
    182 
    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();
    189   }
    190 
    191   enum Signal {
    192     SIGNAL_CONNECT,
    193     SIGNAL_SSL_CONNECT,
    194     SIGNAL_CLOSE,
    195     SIGNAL_READ,
    196     SIGNAL_ERROR,
    197   };
    198 
    199   // Helper struct that records the state at the time of a signal.
    200 
    201   struct SignalSocketState {
    202     SignalSocketState()
    203         : signal(SIGNAL_ERROR),
    204           state(ChromeAsyncSocket::STATE_CLOSED),
    205           error(ChromeAsyncSocket::ERROR_NONE),
    206           net_error(net::OK) {}
    207 
    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) {}
    217 
    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);
    224     }
    225 
    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()));
    234     }
    235 
    236     static SignalSocketState NoError(
    237         Signal signal, buzz::AsyncSocket::State state) {
    238         return SignalSocketState(signal, state,
    239                                  buzz::AsyncSocket::ERROR_NONE,
    240                                  net::OK);
    241     }
    242 
    243     Signal signal;
    244     ChromeAsyncSocket::State state;
    245     ChromeAsyncSocket::Error error;
    246     net::Error net_error;
    247   };
    248 
    249   void CaptureSocketState(Signal signal) {
    250     signal_socket_states_.push_back(
    251         SignalSocketState::FromAsyncSocket(
    252             signal, chrome_async_socket_.get()));
    253   }
    254 
    255   void OnConnect() {
    256     CaptureSocketState(SIGNAL_CONNECT);
    257   }
    258 
    259   void OnSSLConnect() {
    260     CaptureSocketState(SIGNAL_SSL_CONNECT);
    261   }
    262 
    263   void OnClose() {
    264     CaptureSocketState(SIGNAL_CLOSE);
    265   }
    266 
    267   void OnRead() {
    268     CaptureSocketState(SIGNAL_READ);
    269   }
    270 
    271   void OnError() {
    272     ADD_FAILURE();
    273   }
    274 
    275   // State expect functions.
    276 
    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());
    283   }
    284 
    285   void ExpectNonErrorState(ChromeAsyncSocket::State state) {
    286     ExpectState(state, ChromeAsyncSocket::ERROR_NONE, net::OK);
    287   }
    288 
    289   void ExpectErrorState(ChromeAsyncSocket::State state,
    290                         ChromeAsyncSocket::Error error) {
    291     ExpectState(state, error, net::OK);
    292   }
    293 
    294   void ExpectClosed() {
    295     ExpectNonErrorState(ChromeAsyncSocket::STATE_CLOSED);
    296   }
    297 
    298   // Signal expect functions.
    299 
    300   void ExpectNoSignal() {
    301     if (!signal_socket_states_.empty()) {
    302       ADD_FAILURE() << signal_socket_states_.front().signal;
    303     }
    304   }
    305 
    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;
    311     }
    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();
    316   }
    317 
    318   void ExpectReadSignal() {
    319     ExpectSignalSocketState(
    320         SignalSocketState::NoError(
    321             SIGNAL_READ, ChromeAsyncSocket::STATE_OPEN));
    322   }
    323 
    324   void ExpectSSLConnectSignal() {
    325     ExpectSignalSocketState(
    326         SignalSocketState::NoError(SIGNAL_SSL_CONNECT,
    327                                    ChromeAsyncSocket::STATE_TLS_OPEN));
    328   }
    329 
    330   void ExpectSSLReadSignal() {
    331     ExpectSignalSocketState(
    332         SignalSocketState::NoError(
    333             SIGNAL_READ, ChromeAsyncSocket::STATE_TLS_OPEN));
    334   }
    335 
    336   // Open/close utility functions.
    337 
    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);
    344 
    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));
    351   }
    352 
    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);
    359   }
    360 
    361   void DoCloseOpenedNoError() {
    362     DoCloseOpened(
    363         SignalSocketState::NoError(
    364             SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
    365   }
    366 
    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));
    373 
    374     EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
    375     message_loop_.RunUntilIdle();
    376     ExpectSSLConnectSignal();
    377     ExpectNoSignal();
    378     ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN);
    379   }
    380 
    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);
    388   }
    389 
    390   void DoSSLCloseOpenedNoError() {
    391     DoSSLCloseOpened(
    392         SignalSocketState::NoError(
    393             SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
    394   }
    395 
    396   // Read utility fucntions.
    397 
    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;
    408       }
    409       if (len_read == 0U) {
    410         break;
    411       }
    412       read.append(buf.get(), len_read);
    413     }
    414     return read;
    415   }
    416 
    417   // ChromeAsyncSocket expects a message loop.
    418   base::MessageLoop message_loop_;
    419 
    420   AsyncSocketDataProvider async_socket_data_provider_;
    421   net::SSLSocketDataProvider ssl_socket_data_provider_;
    422 
    423   scoped_ptr<ChromeAsyncSocket> chrome_async_socket_;
    424   std::deque<SignalSocketState> signal_socket_states_;
    425   const talk_base::SocketAddress addr_;
    426 
    427  private:
    428   DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocketTest);
    429 };
    430 
    431 TEST_F(ChromeAsyncSocketTest, InitialState) {
    432   ExpectClosed();
    433   ExpectNoSignal();
    434 }
    435 
    436 TEST_F(ChromeAsyncSocketTest, EmptyClose) {
    437   ExpectClosed();
    438   EXPECT_TRUE(chrome_async_socket_->Close());
    439   ExpectClosed();
    440 }
    441 
    442 TEST_F(ChromeAsyncSocketTest, ImmediateConnectAndClose) {
    443   DoOpenClosed();
    444 
    445   ExpectNonErrorState(ChromeAsyncSocket::STATE_OPEN);
    446 
    447   DoCloseOpenedNoError();
    448 }
    449 
    450 // After this, no need to test immediate successful connect and
    451 // Close() so thoroughly.
    452 
    453 TEST_F(ChromeAsyncSocketTest, DoubleClose) {
    454   DoOpenClosed();
    455 
    456   EXPECT_TRUE(chrome_async_socket_->Close());
    457   ExpectClosed();
    458   ExpectSignalSocketState(
    459       SignalSocketState::NoError(
    460           SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
    461 
    462   EXPECT_TRUE(chrome_async_socket_->Close());
    463   ExpectClosed();
    464 }
    465 
    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);
    473 
    474   EXPECT_TRUE(chrome_async_socket_->Close());
    475   ExpectClosed();
    476 }
    477 
    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);
    483 
    484   EXPECT_TRUE(chrome_async_socket_->Close());
    485   ExpectClosed();
    486 }
    487 
    488 TEST_F(ChromeAsyncSocketTest, DoubleConnect) {
    489   EXPECT_DEBUG_DEATH({
    490     DoOpenClosed();
    491 
    492     EXPECT_FALSE(chrome_async_socket_->Connect(addr_));
    493     ExpectErrorState(ChromeAsyncSocket::STATE_OPEN,
    494                      ChromeAsyncSocket::ERROR_WRONGSTATE);
    495 
    496     DoCloseOpened(
    497         SignalSocketState(SIGNAL_CLOSE,
    498                           ChromeAsyncSocket::STATE_CLOSED,
    499                           ChromeAsyncSocket::ERROR_WRONGSTATE,
    500                           net::OK));
    501   }, "non-closed socket");
    502 }
    503 
    504 TEST_F(ChromeAsyncSocketTest, ImmediateConnectCloseBeforeRead) {
    505   DoOpenClosed();
    506 
    507   EXPECT_TRUE(chrome_async_socket_->Close());
    508   ExpectClosed();
    509   ExpectSignalSocketState(
    510       SignalSocketState::NoError(
    511           SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
    512 
    513   message_loop_.RunUntilIdle();
    514 }
    515 
    516 TEST_F(ChromeAsyncSocketTest, HangingConnect) {
    517   EXPECT_TRUE(chrome_async_socket_->Connect(addr_));
    518   ExpectNonErrorState(ChromeAsyncSocket::STATE_CONNECTING);
    519   ExpectNoSignal();
    520 
    521   EXPECT_TRUE(chrome_async_socket_->Close());
    522   ExpectClosed();
    523   ExpectSignalSocketState(
    524       SignalSocketState::NoError(
    525           SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
    526 }
    527 
    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();
    534 
    535   message_loop_.RunUntilIdle();
    536   ExpectNonErrorState(ChromeAsyncSocket::STATE_OPEN);
    537   ExpectSignalSocketState(
    538       SignalSocketState::NoError(
    539           SIGNAL_CONNECT, ChromeAsyncSocket::STATE_OPEN));
    540   ExpectNoSignal();
    541 
    542   message_loop_.RunUntilIdle();
    543 
    544   DoCloseOpenedNoError();
    545 }
    546 
    547 // After this no need to test successful pending connect so
    548 // thoroughly.
    549 
    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_));
    554 
    555   message_loop_.RunUntilIdle();
    556   ExpectSignalSocketState(
    557       SignalSocketState::NoError(
    558           SIGNAL_CONNECT, ChromeAsyncSocket::STATE_OPEN));
    559 
    560   DoCloseOpenedNoError();
    561 
    562   message_loop_.RunUntilIdle();
    563 }
    564 
    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_));
    569 
    570   message_loop_.RunUntilIdle();
    571 
    572   ExpectSignalSocketState(
    573       SignalSocketState(
    574           SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
    575           ChromeAsyncSocket::ERROR_WINSOCK, net::ERR_TIMED_OUT));
    576 }
    577 
    578 // After this we can assume Connect() and Close() work as expected.
    579 
    580 TEST_F(ChromeAsyncSocketTest, EmptyRead) {
    581   DoOpenClosed();
    582 
    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);
    587 
    588   DoCloseOpenedNoError();
    589 }
    590 
    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();
    598 
    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");
    610 }
    611 
    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());
    619 }
    620 
    621 const char kReadData[] = "mydatatoread";
    622 
    623 TEST_F(ChromeAsyncSocketTest, Read) {
    624   async_socket_data_provider_.AddRead(net::MockRead(kReadData));
    625   DoOpenClosed();
    626 
    627   ExpectReadSignal();
    628   ExpectNoSignal();
    629 
    630   EXPECT_EQ(kReadData, DrainRead(1));
    631 
    632   message_loop_.RunUntilIdle();
    633 
    634   DoCloseOpenedNoError();
    635 }
    636 
    637 TEST_F(ChromeAsyncSocketTest, ReadTwice) {
    638   async_socket_data_provider_.AddRead(net::MockRead(kReadData));
    639   DoOpenClosed();
    640 
    641   ExpectReadSignal();
    642   ExpectNoSignal();
    643 
    644   EXPECT_EQ(kReadData, DrainRead(1));
    645 
    646   message_loop_.RunUntilIdle();
    647 
    648   const char kReadData2[] = "mydatatoread2";
    649   async_socket_data_provider_.AddRead(net::MockRead(kReadData2));
    650 
    651   ExpectReadSignal();
    652   ExpectNoSignal();
    653 
    654   EXPECT_EQ(kReadData2, DrainRead(1));
    655 
    656   DoCloseOpenedNoError();
    657 }
    658 
    659 TEST_F(ChromeAsyncSocketTest, ReadError) {
    660   async_socket_data_provider_.AddRead(net::MockRead(kReadData));
    661   DoOpenClosed();
    662 
    663   ExpectReadSignal();
    664   ExpectNoSignal();
    665 
    666   EXPECT_EQ(kReadData, DrainRead(1));
    667 
    668   message_loop_.RunUntilIdle();
    669 
    670   async_socket_data_provider_.AddRead(
    671       net::MockRead(net::SYNCHRONOUS, net::ERR_TIMED_OUT));
    672 
    673   ExpectSignalSocketState(
    674       SignalSocketState(
    675           SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
    676           ChromeAsyncSocket::ERROR_WINSOCK, net::ERR_TIMED_OUT));
    677 }
    678 
    679 TEST_F(ChromeAsyncSocketTest, ReadEmpty) {
    680   async_socket_data_provider_.AddRead(net::MockRead(""));
    681   DoOpenClosed();
    682 
    683   ExpectSignalSocketState(
    684       SignalSocketState::NoError(
    685           SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
    686 }
    687 
    688 TEST_F(ChromeAsyncSocketTest, PendingRead) {
    689   DoOpenClosed();
    690 
    691   ExpectNoSignal();
    692 
    693   async_socket_data_provider_.AddRead(net::MockRead(kReadData));
    694 
    695   ExpectSignalSocketState(
    696       SignalSocketState::NoError(
    697           SIGNAL_READ, ChromeAsyncSocket::STATE_OPEN));
    698   ExpectNoSignal();
    699 
    700   EXPECT_EQ(kReadData, DrainRead(1));
    701 
    702   message_loop_.RunUntilIdle();
    703 
    704   DoCloseOpenedNoError();
    705 }
    706 
    707 TEST_F(ChromeAsyncSocketTest, PendingEmptyRead) {
    708   DoOpenClosed();
    709 
    710   ExpectNoSignal();
    711 
    712   async_socket_data_provider_.AddRead(net::MockRead(""));
    713 
    714   ExpectSignalSocketState(
    715       SignalSocketState::NoError(
    716           SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
    717 }
    718 
    719 TEST_F(ChromeAsyncSocketTest, PendingReadError) {
    720   DoOpenClosed();
    721 
    722   ExpectNoSignal();
    723 
    724   async_socket_data_provider_.AddRead(
    725       net::MockRead(net::ASYNC, net::ERR_TIMED_OUT));
    726 
    727   ExpectSignalSocketState(
    728       SignalSocketState(
    729           SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
    730           ChromeAsyncSocket::ERROR_WINSOCK, net::ERR_TIMED_OUT));
    731 }
    732 
    733 // After this we can assume non-SSL Read() works as expected.
    734 
    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");
    743 }
    744 
    745 const char kWriteData[] = "mydatatowrite";
    746 
    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();
    756 
    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();
    764 
    765   ExpectNoSignal();
    766 
    767   DoCloseOpenedNoError();
    768 }
    769 
    770 TEST_F(ChromeAsyncSocketTest, AsyncWrite) {
    771   DoOpenClosed();
    772 
    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));
    779 
    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();
    787 
    788   ExpectNoSignal();
    789 
    790   DoCloseOpenedNoError();
    791 }
    792 
    793 TEST_F(ChromeAsyncSocketTest, AsyncWriteError) {
    794   DoOpenClosed();
    795 
    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));
    802 
    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();
    810 
    811   ExpectSignalSocketState(
    812       SignalSocketState(
    813           SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED,
    814           ChromeAsyncSocket::ERROR_WINSOCK, net::ERR_TIMED_OUT));
    815 }
    816 
    817 TEST_F(ChromeAsyncSocketTest, LargeWrite) {
    818   EXPECT_DEBUG_DEATH({
    819     DoOpenClosed();
    820 
    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");
    833 }
    834 
    835 TEST_F(ChromeAsyncSocketTest, LargeAccumulatedWrite) {
    836   EXPECT_DEBUG_DEATH({
    837     DoOpenClosed();
    838 
    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");
    851 }
    852 
    853 // After this we can assume non-SSL I/O works as expected.
    854 
    855 TEST_F(ChromeAsyncSocketTest, HangingSSLConnect) {
    856   async_socket_data_provider_.AddRead(net::MockRead(kReadData));
    857   DoOpenClosed();
    858   ExpectReadSignal();
    859 
    860   EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
    861   ExpectNoSignal();
    862 
    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);
    869 }
    870 
    871 TEST_F(ChromeAsyncSocketTest, ImmediateSSLConnect) {
    872   async_socket_data_provider_.AddRead(net::MockRead(kReadData));
    873   DoOpenClosed();
    874   ExpectReadSignal();
    875 
    876   EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
    877   message_loop_.RunUntilIdle();
    878   ExpectSSLConnectSignal();
    879   ExpectNoSignal();
    880   ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN);
    881 
    882   DoSSLCloseOpenedNoError();
    883 }
    884 
    885 TEST_F(ChromeAsyncSocketTest, DoubleSSLConnect) {
    886   EXPECT_DEBUG_DEATH({
    887     async_socket_data_provider_.AddRead(net::MockRead(kReadData));
    888     DoOpenClosed();
    889     ExpectReadSignal();
    890 
    891     EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
    892     message_loop_.RunUntilIdle();
    893     ExpectSSLConnectSignal();
    894     ExpectNoSignal();
    895     ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN);
    896 
    897     EXPECT_FALSE(chrome_async_socket_->StartTls("fakedomain.com"));
    898 
    899     DoSSLCloseOpened(
    900         SignalSocketState(SIGNAL_CLOSE,
    901                           ChromeAsyncSocket::STATE_CLOSED,
    902                           ChromeAsyncSocket::ERROR_WRONGSTATE,
    903                           net::OK));
    904   }, "wrong state");
    905 }
    906 
    907 TEST_F(ChromeAsyncSocketTest, FailedSSLConnect) {
    908   ssl_socket_data_provider_.connect =
    909       net::MockConnect(net::ASYNC, net::ERR_CERT_COMMON_NAME_INVALID),
    910 
    911   async_socket_data_provider_.AddRead(net::MockRead(kReadData));
    912   DoOpenClosed();
    913   ExpectReadSignal();
    914 
    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));
    922 
    923   EXPECT_TRUE(chrome_async_socket_->Close());
    924   ExpectClosed();
    925 }
    926 
    927 TEST_F(ChromeAsyncSocketTest, ReadDuringSSLConnecting) {
    928   async_socket_data_provider_.AddRead(net::MockRead(kReadData));
    929   DoOpenClosed();
    930   ExpectReadSignal();
    931   EXPECT_EQ(kReadData, DrainRead(1));
    932 
    933   EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
    934   ExpectNoSignal();
    935 
    936   // Shouldn't do anything.
    937   async_socket_data_provider_.AddRead(net::MockRead(kReadData));
    938 
    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);
    943 
    944   message_loop_.RunUntilIdle();
    945   ExpectSSLConnectSignal();
    946   ExpectSSLReadSignal();
    947   ExpectNoSignal();
    948   ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_OPEN);
    949 
    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));
    953 
    954   DoSSLCloseOpenedNoError();
    955 }
    956 
    957 TEST_F(ChromeAsyncSocketTest, WriteDuringSSLConnecting) {
    958   async_socket_data_provider_.AddRead(net::MockRead(kReadData));
    959   DoOpenClosed();
    960   ExpectReadSignal();
    961 
    962   EXPECT_TRUE(chrome_async_socket_->StartTls("fakedomain.com"));
    963   ExpectNoSignal();
    964   ExpectNonErrorState(ChromeAsyncSocket::STATE_TLS_CONNECTING);
    965 
    966   async_socket_data_provider_.AddWrite(
    967       net::MockWrite(net::ASYNC, kWriteData, 3));
    968 
    969   // Shouldn't do anything.
    970   EXPECT_TRUE(chrome_async_socket_->Write(kWriteData, 3));
    971 
    972   // TODO(akalin): Figure out how to test that the write happens
    973   // *after* the SSL connect.
    974 
    975   message_loop_.RunUntilIdle();
    976   ExpectSSLConnectSignal();
    977   ExpectNoSignal();
    978 
    979   message_loop_.RunUntilIdle();
    980 
    981   DoSSLCloseOpenedNoError();
    982 }
    983 
    984 TEST_F(ChromeAsyncSocketTest, SSLConnectDuringPendingRead) {
    985   EXPECT_DEBUG_DEATH({
    986     DoOpenClosed();
    987 
    988     EXPECT_FALSE(chrome_async_socket_->StartTls("fakedomain.com"));
    989 
    990     DoCloseOpened(
    991         SignalSocketState(SIGNAL_CLOSE,
    992                           ChromeAsyncSocket::STATE_CLOSED,
    993                           ChromeAsyncSocket::ERROR_WRONGSTATE,
    994                           net::OK));
    995   }, "wrong state");
    996 }
    997 
    998 TEST_F(ChromeAsyncSocketTest, SSLConnectDuringPostedWrite) {
    999   EXPECT_DEBUG_DEATH({
   1000     DoOpenClosed();
   1001 
   1002     async_socket_data_provider_.AddWrite(
   1003         net::MockWrite(net::ASYNC, kWriteData, 3));
   1004     EXPECT_TRUE(chrome_async_socket_->Write(kWriteData, 3));
   1005 
   1006     EXPECT_FALSE(chrome_async_socket_->StartTls("fakedomain.com"));
   1007 
   1008     message_loop_.RunUntilIdle();
   1009 
   1010     DoCloseOpened(
   1011         SignalSocketState(SIGNAL_CLOSE,
   1012                           ChromeAsyncSocket::STATE_CLOSED,
   1013                           ChromeAsyncSocket::ERROR_WRONGSTATE,
   1014                           net::OK));
   1015   }, "wrong state");
   1016 }
   1017 
   1018 // After this we can assume SSL connect works as expected.
   1019 
   1020 TEST_F(ChromeAsyncSocketTest, SSLRead) {
   1021   DoSSLOpenClosed();
   1022   async_socket_data_provider_.AddRead(net::MockRead(kReadData));
   1023   message_loop_.RunUntilIdle();
   1024 
   1025   ExpectSSLReadSignal();
   1026   ExpectNoSignal();
   1027 
   1028   EXPECT_EQ(kReadData, DrainRead(1));
   1029 
   1030   message_loop_.RunUntilIdle();
   1031 
   1032   DoSSLCloseOpenedNoError();
   1033 }
   1034 
   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();
   1044 
   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();
   1052 
   1053   ExpectNoSignal();
   1054 
   1055   DoSSLCloseOpenedNoError();
   1056 }
   1057 
   1058 TEST_F(ChromeAsyncSocketTest, SSLAsyncWrite) {
   1059   DoSSLOpenClosed();
   1060 
   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));
   1067 
   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();
   1075 
   1076   ExpectNoSignal();
   1077 
   1078   DoSSLCloseOpenedNoError();
   1079 }
   1080 
   1081 }  // namespace
   1082 
   1083 }  // namespace jingle_glue
   1084