Home | History | Annotate | Download | only in glue
      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.
      4 
      5 #include "jingle/glue/proxy_resolving_client_socket.h"
      6 
      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"
     17 
     18 namespace {
     19 
     20 class MyTestURLRequestContext : public net::TestURLRequestContext {
     21  public:
     22   MyTestURLRequestContext() : TestURLRequestContext(true) {
     23     context_storage_.set_proxy_service(
     24         net::ProxyService::CreateFixedFromPacResult(
     25             "PROXY bad:99; PROXY maybe:80; DIRECT"));
     26     Init();
     27   }
     28   virtual ~MyTestURLRequestContext() {}
     29 };
     30 
     31 }  // namespace
     32 
     33 namespace jingle_glue {
     34 
     35 class ProxyResolvingClientSocketTest : public testing::Test {
     36  protected:
     37   ProxyResolvingClientSocketTest()
     38       : url_request_context_getter_(new net::TestURLRequestContextGetter(
     39             base::MessageLoopProxy::current(),
     40             scoped_ptr<net::TestURLRequestContext>(
     41                 new MyTestURLRequestContext))) {}
     42 
     43   virtual ~ProxyResolvingClientSocketTest() {}
     44 
     45   virtual void TearDown() {
     46     // Clear out any messages posted by ProxyResolvingClientSocket's
     47     // destructor.
     48     message_loop_.RunUntilIdle();
     49   }
     50 
     51   base::MessageLoop message_loop_;
     52   scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
     53 };
     54 
     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(
     59       NULL,
     60       url_request_context_getter_,
     61       net::SSLConfig(),
     62       dest);
     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);
     71 }
     72 
     73 TEST_F(ProxyResolvingClientSocketTest, ReportsBadProxies) {
     74   net::HostPortPair dest("example.com", 443);
     75   net::MockClientSocketFactory socket_factory;
     76 
     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);
     81 
     82   net::MockRead reads[] = {
     83     net::MockRead("HTTP/1.1 200 Success\r\n\r\n")
     84   };
     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")
     89   };
     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);
     94 
     95   ProxyResolvingClientSocket proxy_resolving_socket(
     96       &socket_factory,
     97       url_request_context_getter_,
     98       net::SSLConfig(),
     99       dest);
    100 
    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);
    106 
    107   net::URLRequestContext* context =
    108       url_request_context_getter_->GetURLRequestContext();
    109   const net::ProxyRetryInfoMap& retry_info =
    110       context->proxy_service()->proxy_retry_info();
    111 
    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());
    115 }
    116 
    117 // TODO(sanjeevr): Add more unit-tests.
    118 }  // namespace jingle_glue
    119