Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(826)

Side by Side Diff: jingle/notifier/base/chrome_async_socket_unittest.cc

Issue 10389098: [Sync] Make ChromeAsyncSocket use only the hostname on connect (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More tweaks Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 net::IPAddressNumber resolved_addr;
155 EXPECT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &resolved_addr));
156 const net::AddressList address_list =
157 net::AddressList::CreateFromIPAddress(resolved_addr, addr_.port());
rlarocque 2012/05/11 20:04:18 It's probably nothing, but this has me a bit conce
akalin 2012/05/11 20:32:20 sockaddrs don't store the hostname, but just the i
rlarocque 2012/05/11 20:46:16 You're right, I was being a bit loose with the ter
168 scoped_ptr<MockXmppClientSocketFactory> mock_xmpp_client_socket_factory( 158 scoped_ptr<MockXmppClientSocketFactory> mock_xmpp_client_socket_factory(
169 new MockXmppClientSocketFactory( 159 new MockXmppClientSocketFactory(
170 mock_client_socket_factory.release(), 160 mock_client_socket_factory.release(),
171 SocketAddressToAddressList(addr_))); 161 address_list));
172 chrome_async_socket_.reset( 162 chrome_async_socket_.reset(
173 new ChromeAsyncSocket(mock_xmpp_client_socket_factory.release(), 163 new ChromeAsyncSocket(mock_xmpp_client_socket_factory.release(),
174 14, 20)), 164 14, 20)),
175 165
176 chrome_async_socket_->SignalConnected.connect( 166 chrome_async_socket_->SignalConnected.connect(
177 this, &ChromeAsyncSocketTest::OnConnect); 167 this, &ChromeAsyncSocketTest::OnConnect);
178 chrome_async_socket_->SignalSSLConnected.connect( 168 chrome_async_socket_->SignalSSLConnected.connect(
179 this, &ChromeAsyncSocketTest::OnSSLConnect); 169 this, &ChromeAsyncSocketTest::OnSSLConnect);
180 chrome_async_socket_->SignalClosed.connect( 170 chrome_async_socket_->SignalClosed.connect(
181 this, &ChromeAsyncSocketTest::OnClose); 171 this, &ChromeAsyncSocketTest::OnClose);
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 EXPECT_TRUE(chrome_async_socket_->Close()); 451 EXPECT_TRUE(chrome_async_socket_->Close());
462 ExpectClosed(); 452 ExpectClosed();
463 ExpectSignalSocketState( 453 ExpectSignalSocketState(
464 SignalSocketState::NoError( 454 SignalSocketState::NoError(
465 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED)); 455 SIGNAL_CLOSE, ChromeAsyncSocket::STATE_CLOSED));
466 456
467 EXPECT_TRUE(chrome_async_socket_->Close()); 457 EXPECT_TRUE(chrome_async_socket_->Close());
468 ExpectClosed(); 458 ExpectClosed();
469 } 459 }
470 460
471 TEST_F(ChromeAsyncSocketTest, UnresolvedConnect) { 461 TEST_F(ChromeAsyncSocketTest, NoHostnameConnect) {
472 const talk_base::SocketAddress unresolved_addr(0, 0); 462 talk_base::IPAddress ip_address;
473 EXPECT_FALSE(chrome_async_socket_->Connect(unresolved_addr)); 463 EXPECT_TRUE(talk_base::IPFromString("127.0.0.1", &ip_address));
464 const talk_base::SocketAddress no_hostname_addr(ip_address, addr_.port());
465 EXPECT_FALSE(chrome_async_socket_->Connect(no_hostname_addr));
474 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED, 466 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED,
475 ChromeAsyncSocket::ERROR_DNS); 467 ChromeAsyncSocket::ERROR_DNS);
476 468
469 EXPECT_TRUE(chrome_async_socket_->Close());
470 ExpectClosed();
471 }
472
473 TEST_F(ChromeAsyncSocketTest, ZeroPortConnect) {
474 const talk_base::SocketAddress zero_port_addr(addr_.hostname(), 0);
475 EXPECT_FALSE(chrome_async_socket_->Connect(zero_port_addr));
476 ExpectErrorState(ChromeAsyncSocket::STATE_CLOSED,
477 ChromeAsyncSocket::ERROR_DNS);
478
477 EXPECT_TRUE(chrome_async_socket_->Close()); 479 EXPECT_TRUE(chrome_async_socket_->Close());
478 ExpectClosed(); 480 ExpectClosed();
479 } 481 }
480 482
481 TEST_F(ChromeAsyncSocketTest, DoubleConnect) { 483 TEST_F(ChromeAsyncSocketTest, DoubleConnect) {
482 EXPECT_DEBUG_DEATH({ 484 EXPECT_DEBUG_DEATH({
483 DoOpenClosed(); 485 DoOpenClosed();
484 486
485 EXPECT_FALSE(chrome_async_socket_->Connect(addr_)); 487 EXPECT_FALSE(chrome_async_socket_->Connect(addr_));
486 ExpectErrorState(ChromeAsyncSocket::STATE_OPEN, 488 ExpectErrorState(ChromeAsyncSocket::STATE_OPEN,
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after
1067 message_loop_.RunAllPending(); 1069 message_loop_.RunAllPending();
1068 1070
1069 ExpectNoSignal(); 1071 ExpectNoSignal();
1070 1072
1071 DoSSLCloseOpenedNoError(); 1073 DoSSLCloseOpenedNoError();
1072 } 1074 }
1073 1075
1074 } // namespace 1076 } // namespace
1075 1077
1076 } // namespace notifier 1078 } // namespace notifier
OLDNEW
« no previous file with comments | « jingle/notifier/base/chrome_async_socket.cc ('k') | jingle/notifier/base/proxy_resolving_client_socket.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698