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.
5 #include "jingle/glue/proxy_resolving_client_socket.h"
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/message_loop/message_loop.h"
10 #include "net/base/test_completion_callback.h"
11 #include "net/dns/mock_host_resolver.h"
12 #include "net/proxy/proxy_service.h"
13 #include "net/socket/socket_test_util.h"
14 #include "net/url_request/url_request_context_getter.h"
15 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 class MyTestURLRequestContext
: public net::TestURLRequestContext
{
22 MyTestURLRequestContext() : TestURLRequestContext(true) {
23 context_storage_
.set_proxy_service(
24 net::ProxyService::CreateFixedFromPacResult(
25 "PROXY bad:99; PROXY maybe:80; DIRECT"));
28 ~MyTestURLRequestContext() override
{}
33 namespace jingle_glue
{
35 class ProxyResolvingClientSocketTest
: public testing::Test
{
37 ProxyResolvingClientSocketTest()
38 : url_request_context_getter_(new net::TestURLRequestContextGetter(
39 base::MessageLoopProxy::current(),
40 scoped_ptr
<net::TestURLRequestContext
>(
41 new MyTestURLRequestContext
))) {}
43 virtual ~ProxyResolvingClientSocketTest() {}
45 virtual void TearDown() {
46 // Clear out any messages posted by ProxyResolvingClientSocket's
48 message_loop_
.RunUntilIdle();
51 base::MessageLoop message_loop_
;
52 scoped_refptr
<net::TestURLRequestContextGetter
> url_request_context_getter_
;
55 // TODO(sanjeevr): Fix this test on Linux.
56 TEST_F(ProxyResolvingClientSocketTest
, DISABLED_ConnectError
) {
57 net::HostPortPair
dest("0.0.0.0", 0);
58 ProxyResolvingClientSocket
proxy_resolving_socket(
60 url_request_context_getter_
,
63 net::TestCompletionCallback callback
;
64 int status
= proxy_resolving_socket
.Connect(callback
.callback());
65 // Connect always returns ERR_IO_PENDING because it is always asynchronous.
66 EXPECT_EQ(net::ERR_IO_PENDING
, status
);
67 status
= callback
.WaitForResult();
68 // ProxyResolvingClientSocket::Connect() will always return an error of
69 // ERR_ADDRESS_INVALID for a 0 IP address.
70 EXPECT_EQ(net::ERR_ADDRESS_INVALID
, status
);
73 TEST_F(ProxyResolvingClientSocketTest
, ReportsBadProxies
) {
74 net::HostPortPair
dest("example.com", 443);
75 net::MockClientSocketFactory socket_factory
;
77 net::StaticSocketDataProvider socket_data1
;
78 socket_data1
.set_connect_data(
79 net::MockConnect(net::ASYNC
, net::ERR_ADDRESS_UNREACHABLE
));
80 socket_factory
.AddSocketDataProvider(&socket_data1
);
82 net::MockRead reads
[] = {
83 net::MockRead("HTTP/1.1 200 Success\r\n\r\n")
85 net::MockWrite writes
[] = {
86 net::MockWrite("CONNECT example.com:443 HTTP/1.1\r\n"
87 "Host: example.com:443\r\n"
88 "Proxy-Connection: keep-alive\r\n\r\n")
90 net::StaticSocketDataProvider
socket_data2(reads
, arraysize(reads
),
91 writes
, arraysize(writes
));
92 socket_data2
.set_connect_data(net::MockConnect(net::ASYNC
, net::OK
));
93 socket_factory
.AddSocketDataProvider(&socket_data2
);
95 ProxyResolvingClientSocket
proxy_resolving_socket(
97 url_request_context_getter_
,
101 net::TestCompletionCallback callback
;
102 int status
= proxy_resolving_socket
.Connect(callback
.callback());
103 EXPECT_EQ(net::ERR_IO_PENDING
, status
);
104 status
= callback
.WaitForResult();
105 EXPECT_EQ(net::OK
, status
);
107 net::URLRequestContext
* context
=
108 url_request_context_getter_
->GetURLRequestContext();
109 const net::ProxyRetryInfoMap
& retry_info
=
110 context
->proxy_service()->proxy_retry_info();
112 EXPECT_EQ(1u, retry_info
.size());
113 net::ProxyRetryInfoMap::const_iterator iter
= retry_info
.find("bad:99");
114 EXPECT_TRUE(iter
!= retry_info
.end());
117 // TODO(sanjeevr): Add more unit-tests.
118 } // namespace jingle_glue