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/proxy_resolving_client_socket.h" | 5 #include "jingle/notifier/base/proxy_resolving_client_socket.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "net/base/mock_host_resolver.h" |
| 10 #include "net/base/test_completion_callback.h" |
| 11 #include "net/socket/socket_test_util.h" |
9 #include "net/url_request/url_request_context_getter.h" | 12 #include "net/url_request/url_request_context_getter.h" |
10 #include "net/url_request/url_request_test_util.h" | 13 #include "net/url_request/url_request_test_util.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 | 15 |
14 namespace { | 16 namespace { |
15 // TODO(sanjeevr): Move this to net_test_support. | 17 // TODO(sanjeevr): Move this to net_test_support. |
16 // Used to return a dummy context. | 18 // Used to return a dummy context. |
17 class TestURLRequestContextGetter : public net::URLRequestContextGetter { | 19 class TestURLRequestContextGetter : public net::URLRequestContextGetter { |
18 public: | 20 public: |
19 TestURLRequestContextGetter() | 21 TestURLRequestContextGetter() |
20 : message_loop_proxy_(base::MessageLoopProxy::current()) { | 22 : message_loop_proxy_(base::MessageLoopProxy::current()) { |
21 } | 23 } |
22 virtual ~TestURLRequestContextGetter() { } | 24 virtual ~TestURLRequestContextGetter() { } |
23 | 25 |
24 // net::URLRequestContextGetter: | 26 // net::URLRequestContextGetter: |
25 virtual net::URLRequestContext* GetURLRequestContext() { | 27 virtual net::URLRequestContext* GetURLRequestContext() { |
26 if (!context_) | 28 if (!context_) |
27 context_ = new TestURLRequestContext(); | 29 CreateURLRequestContext(); |
28 return context_.get(); | 30 return context_.get(); |
29 } | 31 } |
30 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { | 32 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { |
31 return message_loop_proxy_; | 33 return message_loop_proxy_; |
32 } | 34 } |
33 | 35 |
34 private: | 36 private: |
| 37 void CreateURLRequestContext() { |
| 38 context_ = new TestURLRequestContext(); |
| 39 context_->set_host_resolver(new net::MockHostResolver()); |
| 40 context_->set_proxy_service(net::ProxyService::CreateFixedFromPacResult( |
| 41 "PROXY bad:99; PROXY maybe:80; DIRECT")); |
| 42 } |
| 43 |
35 scoped_refptr<net::URLRequestContext> context_; | 44 scoped_refptr<net::URLRequestContext> context_; |
36 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 45 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
37 }; | 46 }; |
38 } // namespace | 47 } // namespace |
39 | 48 |
40 namespace notifier { | 49 namespace notifier { |
41 | 50 |
42 class ProxyResolvingClientSocketTest : public testing::Test { | 51 class ProxyResolvingClientSocketTest : public testing::Test { |
43 protected: | 52 protected: |
44 ProxyResolvingClientSocketTest() | 53 ProxyResolvingClientSocketTest() |
45 : url_request_context_getter_(new TestURLRequestContextGetter()), | 54 : url_request_context_getter_(new TestURLRequestContextGetter()) {} |
46 connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | |
47 &ProxyResolvingClientSocketTest::NetCallback) { } | |
48 | 55 |
49 virtual ~ProxyResolvingClientSocketTest() {} | 56 virtual ~ProxyResolvingClientSocketTest() {} |
50 | 57 |
51 virtual void TearDown() { | 58 virtual void TearDown() { |
52 // Clear out any messages posted by ProxyResolvingClientSocket's | 59 // Clear out any messages posted by ProxyResolvingClientSocket's |
53 // destructor. | 60 // destructor. |
54 message_loop_.RunAllPending(); | 61 message_loop_.RunAllPending(); |
55 } | 62 } |
56 | 63 |
57 MOCK_METHOD1(NetCallback, void(int status)); | |
58 | |
59 // Needed by XmppConnection. | 64 // Needed by XmppConnection. |
60 MessageLoopForIO message_loop_; | 65 MessageLoopForIO message_loop_; |
61 scoped_refptr<TestURLRequestContextGetter> url_request_context_getter_; | 66 scoped_refptr<TestURLRequestContextGetter> url_request_context_getter_; |
62 net::CompletionCallbackImpl<ProxyResolvingClientSocketTest> connect_callback_; | |
63 }; | 67 }; |
64 | 68 |
65 // TODO(sanjeevr): Fix this test on Linux. | 69 // TODO(sanjeevr): Fix this test on Linux. |
66 TEST_F(ProxyResolvingClientSocketTest, DISABLED_ConnectError) { | 70 TEST_F(ProxyResolvingClientSocketTest, DISABLED_ConnectError) { |
67 net::HostPortPair dest("0.0.0.0", 0); | 71 net::HostPortPair dest("0.0.0.0", 0); |
68 ProxyResolvingClientSocket proxy_resolving_socket( | 72 ProxyResolvingClientSocket proxy_resolving_socket( |
| 73 NULL, |
69 url_request_context_getter_, | 74 url_request_context_getter_, |
70 net::SSLConfig(), | 75 net::SSLConfig(), |
71 dest); | 76 dest); |
| 77 TestCompletionCallback callback; |
| 78 int status = proxy_resolving_socket.Connect(&callback); |
| 79 // Connect always returns ERR_IO_PENDING because it is always asynchronous. |
| 80 EXPECT_EQ(net::ERR_IO_PENDING, status); |
| 81 status = callback.WaitForResult(); |
72 // ProxyResolvingClientSocket::Connect() will always return an error of | 82 // ProxyResolvingClientSocket::Connect() will always return an error of |
73 // ERR_ADDRESS_INVALID for a 0 IP address. | 83 // ERR_ADDRESS_INVALID for a 0 IP address. |
74 EXPECT_CALL(*this, NetCallback(net::ERR_ADDRESS_INVALID)).Times(1); | 84 EXPECT_EQ(net::ERR_ADDRESS_INVALID, status); |
75 int status = proxy_resolving_socket.Connect(&connect_callback_); | 85 } |
76 // Connect always returns ERR_IO_PENDING because it is always asynchronous. | 86 |
77 EXPECT_EQ(status, net::ERR_IO_PENDING); | 87 TEST_F(ProxyResolvingClientSocketTest, ReportsBadProxies) { |
78 message_loop_.RunAllPending(); | 88 net::HostPortPair dest("example.com", 443); |
| 89 net::MockClientSocketFactory socket_factory; |
| 90 |
| 91 net::StaticSocketDataProvider socket_data1; |
| 92 socket_data1.set_connect_data( |
| 93 net::MockConnect(true, net::ERR_ADDRESS_UNREACHABLE)); |
| 94 socket_factory.AddSocketDataProvider(&socket_data1); |
| 95 |
| 96 net::MockRead reads[] = { |
| 97 net::MockRead("HTTP/1.1 200 Success\r\n\r\n") |
| 98 }; |
| 99 net::MockWrite writes[] = { |
| 100 net::MockWrite("CONNECT example.com:443 HTTP/1.1\r\n" |
| 101 "Host: example.com:443\r\n" |
| 102 "Proxy-Connection: keep-alive\r\n\r\n") |
| 103 }; |
| 104 net::StaticSocketDataProvider socket_data2(reads, arraysize(reads), |
| 105 writes, arraysize(writes)); |
| 106 socket_data2.set_connect_data(net::MockConnect(true, net::OK)); |
| 107 socket_factory.AddSocketDataProvider(&socket_data2); |
| 108 |
| 109 ProxyResolvingClientSocket proxy_resolving_socket( |
| 110 &socket_factory, |
| 111 url_request_context_getter_, |
| 112 net::SSLConfig(), |
| 113 dest); |
| 114 |
| 115 TestCompletionCallback callback; |
| 116 int status = proxy_resolving_socket.Connect(&callback); |
| 117 EXPECT_EQ(net::ERR_IO_PENDING, status); |
| 118 status = callback.WaitForResult(); |
| 119 EXPECT_EQ(net::OK, status); |
| 120 |
| 121 net::URLRequestContext* context = |
| 122 url_request_context_getter_->GetURLRequestContext(); |
| 123 const net::ProxyRetryInfoMap& retry_info = |
| 124 context->proxy_service()->proxy_retry_info(); |
| 125 |
| 126 EXPECT_EQ(1u, retry_info.size()); |
| 127 net::ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99"); |
| 128 EXPECT_TRUE(iter != retry_info.end()); |
79 } | 129 } |
80 | 130 |
81 // TODO(sanjeevr): Add more unit-tests. | 131 // TODO(sanjeevr): Add more unit-tests. |
82 } // namespace notifier | 132 } // namespace notifier |
OLD | NEW |