| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "jingle/notifier/base/chrome_async_socket.h" | 5 #include "jingle/notifier/base/chrome_async_socket.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) |
| 8 #include <winsock2.h> |
| 9 #elif defined(OS_POSIX) |
| 10 #include <arpa/inet.h> |
| 11 #endif |
| 12 |
| 7 #include <deque> | 13 #include <deque> |
| 8 #include <string> | 14 #include <string> |
| 9 | 15 |
| 10 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 11 #include "base/logging.h" | 17 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop.h" | 19 #include "base/message_loop.h" |
| 20 #include "jingle/notifier/base/resolving_client_socket_factory.h" |
| 14 #include "net/base/capturing_net_log.h" | 21 #include "net/base/capturing_net_log.h" |
| 15 #include "net/base/cert_verifier.h" | 22 #include "net/base/cert_verifier.h" |
| 16 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
| 24 #include "net/base/net_log.h" |
| 17 #include "net/base/ssl_config_service.h" | 25 #include "net/base/ssl_config_service.h" |
| 18 #include "net/socket/socket_test_util.h" | 26 #include "net/socket/socket_test_util.h" |
| 27 #include "net/url_request/url_request_context_getter.h" |
| 19 #include "talk/base/sigslot.h" | 28 #include "talk/base/sigslot.h" |
| 20 #include "talk/base/socketaddress.h" | 29 #include "talk/base/socketaddress.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 31 |
| 23 namespace notifier { | 32 namespace notifier { |
| 24 | 33 |
| 25 namespace { | 34 namespace { |
| 26 | 35 |
| 27 // Data provider that handles reads/writes for ChromeAsyncSocket. | 36 // Data provider that handles reads/writes for ChromeAsyncSocket. |
| 28 class AsyncSocketDataProvider : public net::SocketDataProvider { | 37 class AsyncSocketDataProvider : public net::SocketDataProvider { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 | 97 |
| 89 private: | 98 private: |
| 90 std::deque<net::MockRead> reads_; | 99 std::deque<net::MockRead> reads_; |
| 91 bool has_pending_read_; | 100 bool has_pending_read_; |
| 92 | 101 |
| 93 std::deque<net::MockWrite> writes_; | 102 std::deque<net::MockWrite> writes_; |
| 94 | 103 |
| 95 DISALLOW_COPY_AND_ASSIGN(AsyncSocketDataProvider); | 104 DISALLOW_COPY_AND_ASSIGN(AsyncSocketDataProvider); |
| 96 }; | 105 }; |
| 97 | 106 |
| 107 // Takes a 32-bit integer in host byte order and converts it to a |
| 108 // net::IPAddressNumber. |
| 109 net::IPAddressNumber Uint32ToIPAddressNumber(uint32 ip) { |
| 110 uint32 ip_nbo = htonl(ip); |
| 111 const unsigned char* const ip_start = |
| 112 reinterpret_cast<const unsigned char*>(&ip_nbo); |
| 113 return net::IPAddressNumber(ip_start, ip_start + (sizeof ip_nbo)); |
| 114 } |
| 115 |
| 116 net::AddressList SocketAddressToAddressList( |
| 117 const talk_base::SocketAddress& address) { |
| 118 DCHECK_NE(address.ip(), 0U); |
| 119 return net::AddressList(Uint32ToIPAddressNumber(address.ip()), |
| 120 address.port(), false); |
| 121 } |
| 122 |
| 123 class MockXmppClientSocketFactory : public ResolvingClientSocketFactory { |
| 124 public: |
| 125 MockXmppClientSocketFactory( |
| 126 net::ClientSocketFactory* mock_client_socket_factory, |
| 127 const net::AddressList& address_list) |
| 128 : mock_client_socket_factory_(mock_client_socket_factory), |
| 129 address_list_(address_list) { |
| 130 } |
| 131 |
| 132 // ResolvingClientSocketFactory implementation. |
| 133 virtual net::ClientSocket* CreateTransportClientSocket( |
| 134 const net::HostPortPair& host_and_port, net::NetLog* net_log) { |
| 135 return mock_client_socket_factory_->CreateTransportClientSocket( |
| 136 address_list_, net_log, net::NetLog::Source()); |
| 137 } |
| 138 |
| 139 virtual net::SSLClientSocket* CreateSSLClientSocket( |
| 140 net::ClientSocketHandle* transport_socket, |
| 141 const net::HostPortPair& host_and_port) { |
| 142 return mock_client_socket_factory_->CreateSSLClientSocket( |
| 143 transport_socket, host_and_port, ssl_config_, NULL, &cert_verifier_, |
| 144 NULL); |
| 145 } |
| 146 |
| 147 private: |
| 148 scoped_ptr<net::ClientSocketFactory> mock_client_socket_factory_; |
| 149 net::AddressList address_list_; |
| 150 net::SSLConfig ssl_config_; |
| 151 net::CertVerifier cert_verifier_; |
| 152 }; |
| 153 |
| 98 class ChromeAsyncSocketTest | 154 class ChromeAsyncSocketTest |
| 99 : public testing::Test, | 155 : public testing::Test, |
| 100 public sigslot::has_slots<> { | 156 public sigslot::has_slots<> { |
| 101 protected: | 157 protected: |
| 102 ChromeAsyncSocketTest() | 158 ChromeAsyncSocketTest() |
| 103 : ssl_socket_data_provider_(true, net::OK), | 159 : ssl_socket_data_provider_(true, net::OK), |
| 104 capturing_net_log_(net::CapturingNetLog::kUnbounded), | 160 capturing_net_log_(net::CapturingNetLog::kUnbounded), |
| 105 addr_(0xaabbccdd, 35) {} | 161 addr_(0xaabbccdd, 35) {} |
| 106 | 162 |
| 107 virtual ~ChromeAsyncSocketTest() {} | 163 virtual ~ChromeAsyncSocketTest() {} |
| 108 | 164 |
| 109 virtual void SetUp() { | 165 virtual void SetUp() { |
| 110 scoped_ptr<net::MockClientSocketFactory> mock_client_socket_factory( | 166 scoped_ptr<net::MockClientSocketFactory> mock_client_socket_factory( |
| 111 new net::MockClientSocketFactory()); | 167 new net::MockClientSocketFactory()); |
| 112 mock_client_socket_factory->AddSocketDataProvider( | 168 mock_client_socket_factory->AddSocketDataProvider( |
| 113 &async_socket_data_provider_); | 169 &async_socket_data_provider_); |
| 114 mock_client_socket_factory->AddSSLSocketDataProvider( | 170 mock_client_socket_factory->AddSSLSocketDataProvider( |
| 115 &ssl_socket_data_provider_); | 171 &ssl_socket_data_provider_); |
| 116 | 172 |
| 173 scoped_ptr<MockXmppClientSocketFactory> mock_xmpp_client_socket_factory( |
| 174 new MockXmppClientSocketFactory( |
| 175 mock_client_socket_factory.release(), |
| 176 SocketAddressToAddressList(addr_))); |
| 117 chrome_async_socket_.reset( | 177 chrome_async_socket_.reset( |
| 118 new ChromeAsyncSocket(mock_client_socket_factory.release(), | 178 new ChromeAsyncSocket(mock_xmpp_client_socket_factory.release(), |
| 119 ssl_config_, &cert_verifier_, 14, 20, | 179 14, 20, |
| 120 &capturing_net_log_)), | 180 &capturing_net_log_)), |
| 121 | 181 |
| 122 chrome_async_socket_->SignalConnected.connect( | 182 chrome_async_socket_->SignalConnected.connect( |
| 123 this, &ChromeAsyncSocketTest::OnConnect); | 183 this, &ChromeAsyncSocketTest::OnConnect); |
| 124 chrome_async_socket_->SignalSSLConnected.connect( | 184 chrome_async_socket_->SignalSSLConnected.connect( |
| 125 this, &ChromeAsyncSocketTest::OnSSLConnect); | 185 this, &ChromeAsyncSocketTest::OnSSLConnect); |
| 126 chrome_async_socket_->SignalClosed.connect( | 186 chrome_async_socket_->SignalClosed.connect( |
| 127 this, &ChromeAsyncSocketTest::OnClose); | 187 this, &ChromeAsyncSocketTest::OnClose); |
| 128 chrome_async_socket_->SignalRead.connect( | 188 chrome_async_socket_->SignalRead.connect( |
| 129 this, &ChromeAsyncSocketTest::OnRead); | 189 this, &ChromeAsyncSocketTest::OnRead); |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 return read; | 425 return read; |
| 366 } | 426 } |
| 367 | 427 |
| 368 // ChromeAsyncSocket expects a message loop. | 428 // ChromeAsyncSocket expects a message loop. |
| 369 MessageLoop message_loop_; | 429 MessageLoop message_loop_; |
| 370 | 430 |
| 371 AsyncSocketDataProvider async_socket_data_provider_; | 431 AsyncSocketDataProvider async_socket_data_provider_; |
| 372 net::SSLSocketDataProvider ssl_socket_data_provider_; | 432 net::SSLSocketDataProvider ssl_socket_data_provider_; |
| 373 | 433 |
| 374 net::CapturingNetLog capturing_net_log_; | 434 net::CapturingNetLog capturing_net_log_; |
| 375 net::SSLConfig ssl_config_; | |
| 376 net::CertVerifier cert_verifier_; | |
| 377 scoped_ptr<ChromeAsyncSocket> chrome_async_socket_; | 435 scoped_ptr<ChromeAsyncSocket> chrome_async_socket_; |
| 378 std::deque<SignalSocketState> signal_socket_states_; | 436 std::deque<SignalSocketState> signal_socket_states_; |
| 379 const talk_base::SocketAddress addr_; | 437 const talk_base::SocketAddress addr_; |
| 380 | 438 |
| 381 private: | 439 private: |
| 382 DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocketTest); | 440 DISALLOW_COPY_AND_ASSIGN(ChromeAsyncSocketTest); |
| 383 }; | 441 }; |
| 384 | 442 |
| 385 TEST_F(ChromeAsyncSocketTest, InitialState) { | 443 TEST_F(ChromeAsyncSocketTest, InitialState) { |
| 386 ExpectClosed(); | 444 ExpectClosed(); |
| (...skipping 627 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1014 message_loop_.RunAllPending(); | 1072 message_loop_.RunAllPending(); |
| 1015 | 1073 |
| 1016 ExpectNoSignal(); | 1074 ExpectNoSignal(); |
| 1017 | 1075 |
| 1018 DoSSLCloseOpenedNoError(); | 1076 DoSSLCloseOpenedNoError(); |
| 1019 } | 1077 } |
| 1020 | 1078 |
| 1021 } // namespace | 1079 } // namespace |
| 1022 | 1080 |
| 1023 } // namespace notifier | 1081 } // namespace notifier |
| OLD | NEW |