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 "base/thread_task_runner_handle.h"
11 #include "net/base/test_completion_callback.h"
12 #include "net/dns/mock_host_resolver.h"
13 #include "net/proxy/proxy_service.h"
14 #include "net/socket/socket_test_util.h"
15 #include "net/url_request/url_request_context_getter.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 class MyTestURLRequestContext
: public net::TestURLRequestContext
{
23 MyTestURLRequestContext() : TestURLRequestContext(true) {
24 context_storage_
.set_proxy_service(
25 net::ProxyService::CreateFixedFromPacResult(
26 "PROXY bad:99; PROXY maybe:80; DIRECT"));
29 ~MyTestURLRequestContext() override
{}
34 namespace jingle_glue
{
36 class ProxyResolvingClientSocketTest
: public testing::Test
{
38 ProxyResolvingClientSocketTest()
39 : url_request_context_getter_(new net::TestURLRequestContextGetter(
40 base::ThreadTaskRunnerHandle::Get(),
41 scoped_ptr
<net::TestURLRequestContext
>(
42 new MyTestURLRequestContext
))) {}
44 ~ProxyResolvingClientSocketTest() override
{}
46 void TearDown() override
{
47 // Clear out any messages posted by ProxyResolvingClientSocket's
49 message_loop_
.RunUntilIdle();
52 base::MessageLoop message_loop_
;
53 scoped_refptr
<net::TestURLRequestContextGetter
> url_request_context_getter_
;
56 // TODO(sanjeevr): Fix this test on Linux.
57 TEST_F(ProxyResolvingClientSocketTest
, DISABLED_ConnectError
) {
58 net::HostPortPair
dest("0.0.0.0", 0);
59 ProxyResolvingClientSocket
proxy_resolving_socket(
61 url_request_context_getter_
,
64 net::TestCompletionCallback callback
;
65 int status
= proxy_resolving_socket
.Connect(callback
.callback());
66 // Connect always returns ERR_IO_PENDING because it is always asynchronous.
67 EXPECT_EQ(net::ERR_IO_PENDING
, status
);
68 status
= callback
.WaitForResult();
69 // ProxyResolvingClientSocket::Connect() will always return an error of
70 // ERR_ADDRESS_INVALID for a 0 IP address.
71 EXPECT_EQ(net::ERR_ADDRESS_INVALID
, status
);
74 TEST_F(ProxyResolvingClientSocketTest
, ReportsBadProxies
) {
75 net::HostPortPair
dest("example.com", 443);
76 net::MockClientSocketFactory socket_factory
;
78 net::StaticSocketDataProvider socket_data1
;
79 socket_data1
.set_connect_data(
80 net::MockConnect(net::ASYNC
, net::ERR_ADDRESS_UNREACHABLE
));
81 socket_factory
.AddSocketDataProvider(&socket_data1
);
83 net::MockRead reads
[] = {
84 net::MockRead("HTTP/1.1 200 Success\r\n\r\n")
86 net::MockWrite writes
[] = {
87 net::MockWrite("CONNECT example.com:443 HTTP/1.1\r\n"
88 "Host: example.com\r\n"
89 "Proxy-Connection: keep-alive\r\n\r\n")
91 net::StaticSocketDataProvider
socket_data2(reads
, arraysize(reads
),
92 writes
, arraysize(writes
));
93 socket_data2
.set_connect_data(net::MockConnect(net::ASYNC
, net::OK
));
94 socket_factory
.AddSocketDataProvider(&socket_data2
);
96 ProxyResolvingClientSocket
proxy_resolving_socket(
98 url_request_context_getter_
,
102 net::TestCompletionCallback callback
;
103 int status
= proxy_resolving_socket
.Connect(callback
.callback());
104 EXPECT_EQ(net::ERR_IO_PENDING
, status
);
105 status
= callback
.WaitForResult();
106 EXPECT_EQ(net::OK
, status
);
108 net::URLRequestContext
* context
=
109 url_request_context_getter_
->GetURLRequestContext();
110 const net::ProxyRetryInfoMap
& retry_info
=
111 context
->proxy_service()->proxy_retry_info();
113 EXPECT_EQ(1u, retry_info
.size());
114 net::ProxyRetryInfoMap::const_iterator iter
= retry_info
.find("bad:99");
115 EXPECT_TRUE(iter
!= retry_info
.end());
118 // TODO(sanjeevr): Add more unit-tests.
119 } // namespace jingle_glue