OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include <deque> | 7 #include <deque> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/sys_byteorder.h" | |
15 #include "jingle/notifier/base/resolving_client_socket_factory.h" | 14 #include "jingle/notifier/base/resolving_client_socket_factory.h" |
15 #include "net/base/address_list.h" | |
16 #include "net/base/mock_cert_verifier.h" | 16 #include "net/base/mock_cert_verifier.h" |
17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
18 #include "net/base/net_util.h" | |
18 #include "net/base/ssl_config_service.h" | 19 #include "net/base/ssl_config_service.h" |
19 #include "net/socket/socket_test_util.h" | 20 #include "net/socket/socket_test_util.h" |
20 #include "net/socket/ssl_client_socket.h" | 21 #include "net/socket/ssl_client_socket.h" |
21 #include "net/url_request/url_request_context_getter.h" | 22 #include "net/url_request/url_request_context_getter.h" |
22 #include "talk/base/sigslot.h" | 23 #include "talk/base/sigslot.h" |
24 #include "talk/base/ipaddress.h" | |
23 #include "talk/base/socketaddress.h" | 25 #include "talk/base/socketaddress.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
25 | 27 |
26 namespace notifier { | 28 namespace notifier { |
27 | 29 |
28 namespace { | 30 namespace { |
29 | 31 |
30 // Data provider that handles reads/writes for ChromeAsyncSocket. | 32 // Data provider that handles reads/writes for ChromeAsyncSocket. |
31 class AsyncSocketDataProvider : public net::SocketDataProvider { | 33 class AsyncSocketDataProvider : public net::SocketDataProvider { |
32 public: | 34 public: |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 | 93 |
92 private: | 94 private: |
93 std::deque<net::MockRead> reads_; | 95 std::deque<net::MockRead> reads_; |
94 bool has_pending_read_; | 96 bool has_pending_read_; |
95 | 97 |
96 std::deque<net::MockWrite> writes_; | 98 std::deque<net::MockWrite> writes_; |
97 | 99 |
98 DISALLOW_COPY_AND_ASSIGN(AsyncSocketDataProvider); | 100 DISALLOW_COPY_AND_ASSIGN(AsyncSocketDataProvider); |
99 }; | 101 }; |
100 | 102 |
101 // Takes a 32-bit integer in host byte order and converts it to a | |
102 // net::IPAddressNumber. | |
103 net::IPAddressNumber Uint32ToIPAddressNumber(uint32 ip) { | |
104 uint32 ip_nbo = base::HostToNet32(ip); | |
105 const unsigned char* const ip_start = | |
106 reinterpret_cast<const unsigned char*>(&ip_nbo); | |
107 return net::IPAddressNumber(ip_start, ip_start + (sizeof ip_nbo)); | |
108 } | |
109 | |
110 net::AddressList SocketAddressToAddressList( | |
111 const talk_base::SocketAddress& address) { | |
112 DCHECK_NE(address.ip(), 0U); | |
113 return net::AddressList::CreateFromIPAddress( | |
114 Uint32ToIPAddressNumber(address.ip()), address.port()); | |
115 } | |
116 | |
117 class MockXmppClientSocketFactory : public ResolvingClientSocketFactory { | 103 class MockXmppClientSocketFactory : public ResolvingClientSocketFactory { |
118 public: | 104 public: |
119 MockXmppClientSocketFactory( | 105 MockXmppClientSocketFactory( |
120 net::ClientSocketFactory* mock_client_socket_factory, | 106 net::ClientSocketFactory* mock_client_socket_factory, |
121 const net::AddressList& address_list) | 107 const net::AddressList& address_list) |
122 : mock_client_socket_factory_(mock_client_socket_factory), | 108 : mock_client_socket_factory_(mock_client_socket_factory), |
123 address_list_(address_list), | 109 address_list_(address_list), |
124 cert_verifier_(new net::MockCertVerifier) { | 110 cert_verifier_(new net::MockCertVerifier) { |
125 } | 111 } |
126 | 112 |
(...skipping 19 matching lines...) Expand all Loading... | |
146 net::SSLConfig ssl_config_; | 132 net::SSLConfig ssl_config_; |
147 scoped_ptr<net::CertVerifier> cert_verifier_; | 133 scoped_ptr<net::CertVerifier> cert_verifier_; |
148 }; | 134 }; |
149 | 135 |
150 class ChromeAsyncSocketTest | 136 class ChromeAsyncSocketTest |
151 : public testing::Test, | 137 : public testing::Test, |
152 public sigslot::has_slots<> { | 138 public sigslot::has_slots<> { |
153 protected: | 139 protected: |
154 ChromeAsyncSocketTest() | 140 ChromeAsyncSocketTest() |
155 : ssl_socket_data_provider_(net::ASYNC, net::OK), | 141 : ssl_socket_data_provider_(net::ASYNC, net::OK), |
156 addr_(0xaabbccdd, 35) {} | 142 addr_("example.com", 35) {} |
157 | 143 |
158 virtual ~ChromeAsyncSocketTest() {} | 144 virtual ~ChromeAsyncSocketTest() {} |
159 | 145 |
160 virtual void SetUp() { | 146 virtual void SetUp() { |
161 scoped_ptr<net::MockClientSocketFactory> mock_client_socket_factory( | 147 scoped_ptr<net::MockClientSocketFactory> mock_client_socket_factory( |
162 new net::MockClientSocketFactory()); | 148 new net::MockClientSocketFactory()); |
163 mock_client_socket_factory->AddSocketDataProvider( | 149 mock_client_socket_factory->AddSocketDataProvider( |
164 &async_socket_data_provider_); | 150 &async_socket_data_provider_); |
165 mock_client_socket_factory->AddSSLSocketDataProvider( | 151 mock_client_socket_factory->AddSSLSocketDataProvider( |
166 &ssl_socket_data_provider_); | 152 &ssl_socket_data_provider_); |
167 | 153 |
154 // Fake DNS resolution for |addr_| and pass it to the factory. | |
akalin
2012/05/11 20:32:20
Added comment.
| |
155 net::IPAddressNumber resolved_addr; | |
156 EXPECT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &resolved_addr)); | |
157 const net::AddressList address_list = | |
158 net::AddressList::CreateFromIPAddress(resolved_addr, addr_.port()); | |
168 scoped_ptr<MockXmppClientSocketFactory> mock_xmpp_client_socket_factory( | 159 scoped_ptr<MockXmppClientSocketFactory> mock_xmpp_client_socket_factory( |
169 new MockXmppClientSocketFactory( | 160 new MockXmppClientSocketFactory( |
170 mock_client_socket_factory.release(), | 161 mock_client_socket_factory.release(), |
171 SocketAddressToAddressList(addr_))); | 162 address_list)); |
172 chrome_async_socket_.reset( | 163 chrome_async_socket_.reset( |
173 new ChromeAsyncSocket(mock_xmpp_client_socket_factory.release(), | 164 new ChromeAsyncSocket(mock_xmpp_client_socket_factory.release(), |
174 14, 20)), | 165 14, 20)), |
175 | 166 |
176 chrome_async_socket_->SignalConnected.connect( | 167 chrome_async_socket_->SignalConnected.connect( |
177 this, &ChromeAsyncSocketTest::OnConnect); | 168 this, &ChromeAsyncSocketTest::OnConnect); |
178 chrome_async_socket_->SignalSSLConnected.connect( | 169 chrome_async_socket_->SignalSSLConnected.connect( |
179 this, &ChromeAsyncSocketTest::OnSSLConnect); | 170 this, &ChromeAsyncSocketTest::OnSSLConnect); |
180 chrome_async_socket_->SignalClosed.connect( | 171 chrome_async_socket_->SignalClosed.connect( |
181 this, &ChromeAsyncSocketTest::OnClose); | 172 this, &ChromeAsyncSocketTest::OnClose); |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
461 EXPECT_TRUE(chrome_async_socket_->Close()); | 452 EXPECT_TRUE(chrome_async_socket_->Close()); |
462 ExpectClosed(); | 453 ExpectClosed(); |
463 ExpectSignalSocketState( | 454 ExpectSignalSocketState( |
464 SignalSocketState::NoError( | 455 SignalSocketState::NoError( |
465 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED)); | 456 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED)); |
466 | 457 |
467 EXPECT_TRUE(chrome_async_socket_->Close()); | 458 EXPECT_TRUE(chrome_async_socket_->Close()); |
468 ExpectClosed(); | 459 ExpectClosed(); |
469 } | 460 } |
470 | 461 |
471 TEST_F(ChromeAsyncSocketTest, UnresolvedConnect) { | 462 TEST_F(ChromeAsyncSocketTest, NoHostnameConnect) { |
472 const talk_base::SocketAddress unresolved_addr(0, 0); | 463 talk_base::IPAddress ip_address; |
473 EXPECT_FALSE(chrome_async_socket_->Connect(unresolved_addr)); | 464 EXPECT_TRUE(talk_base::IPFromString("127.0.0.1", &ip_address)); |
465 const talk_base::SocketAddress no_hostname_addr(ip_address, addr_.port()); | |
466 EXPECT_FALSE(chrome_async_socket_->Connect(no_hostname_addr)); | |
474 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED, | 467 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED, |
475 ChromeAsyncSocket::ERROR_DNS); | 468 ChromeAsyncSocket::ERROR_DNS); |
476 | 469 |
470 EXPECT_TRUE(chrome_async_socket_->Close()); | |
471 ExpectClosed(); | |
472 } | |
473 | |
474 TEST_F(ChromeAsyncSocketTest, ZeroPortConnect) { | |
475 const talk_base::SocketAddress zero_port_addr(addr_.hostname(), 0); | |
476 EXPECT_FALSE(chrome_async_socket_->Connect(zero_port_addr)); | |
477 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED, | |
478 ChromeAsyncSocket::ERROR_DNS); | |
479 | |
477 EXPECT_TRUE(chrome_async_socket_->Close()); | 480 EXPECT_TRUE(chrome_async_socket_->Close()); |
478 ExpectClosed(); | 481 ExpectClosed(); |
479 } | 482 } |
480 | 483 |
481 TEST_F(ChromeAsyncSocketTest, DoubleConnect) { | 484 TEST_F(ChromeAsyncSocketTest, DoubleConnect) { |
482 EXPECT_DEBUG_DEATH({ | 485 EXPECT_DEBUG_DEATH({ |
483 DoOpenClosed(); | 486 DoOpenClosed(); |
484 | 487 |
485 EXPECT_FALSE(chrome_async_socket_->Connect(addr_)); | 488 EXPECT_FALSE(chrome_async_socket_->Connect(addr_)); |
486 ExpectErrorState(ChromeAsyncSocket::STATE_OPEN, | 489 ExpectErrorState(ChromeAsyncSocket::STATE_OPEN, |
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1067 message_loop_.RunAllPending(); | 1070 message_loop_.RunAllPending(); |
1068 | 1071 |
1069 ExpectNoSignal(); | 1072 ExpectNoSignal(); |
1070 | 1073 |
1071 DoSSLCloseOpenedNoError(); | 1074 DoSSLCloseOpenedNoError(); |
1072 } | 1075 } |
1073 | 1076 |
1074 } // namespace | 1077 } // namespace |
1075 | 1078 |
1076 } // namespace notifier | 1079 } // namespace notifier |
OLD | NEW |