Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include "webrtc/base/autodetectproxy.h"
     12 #include "webrtc/base/gunit.h"
     13 #include "webrtc/base/httpcommon.h"
     14 #include "webrtc/base/httpcommon-inl.h"
     15 #include "webrtc/test/testsupport/gtest_disable.h"
     16 
     17 namespace rtc {
     18 
     19 static const char kUserAgent[] = "";
     20 static const char kPath[] = "/";
     21 static const char kHost[] = "relay.google.com";
     22 static const uint16 kPort = 443;
     23 static const bool kSecure = true;
     24 // At most, AutoDetectProxy should take ~6 seconds. Each connect step is
     25 // allotted 2 seconds, with the initial resolution + connect given an
     26 // extra 2 seconds. The slowest case is:
     27 // 1) Resolution + HTTPS takes full 4 seconds and fails (but resolution
     28 // succeeds).
     29 // 2) SOCKS5 takes the full 2 seconds.
     30 // Socket creation time seems unbounded, and has been observed to take >1 second
     31 // on a linux machine under load. As such, we allow for 10 seconds for timeout,
     32 // though could still end up with some flakiness.
     33 static const int kTimeoutMs = 10000;
     34 
     35 class AutoDetectProxyTest : public testing::Test, public sigslot::has_slots<> {
     36  public:
     37   AutoDetectProxyTest() : auto_detect_proxy_(NULL), done_(false) {}
     38 
     39  protected:
     40   bool Create(const std::string &user_agent,
     41               const std::string &path,
     42               const std::string &host,
     43               uint16 port,
     44               bool secure,
     45               bool startnow) {
     46     auto_detect_proxy_ = new AutoDetectProxy(user_agent);
     47     EXPECT_TRUE(auto_detect_proxy_ != NULL);
     48     if (!auto_detect_proxy_) {
     49       return false;
     50     }
     51     Url<char> host_url(path, host, port);
     52     host_url.set_secure(secure);
     53     auto_detect_proxy_->set_server_url(host_url.url());
     54     auto_detect_proxy_->SignalWorkDone.connect(
     55         this,
     56         &AutoDetectProxyTest::OnWorkDone);
     57     if (startnow) {
     58       auto_detect_proxy_->Start();
     59     }
     60     return true;
     61   }
     62 
     63   bool Run(int timeout_ms) {
     64     EXPECT_TRUE_WAIT(done_, timeout_ms);
     65     return done_;
     66   }
     67 
     68   void SetProxy(const SocketAddress& proxy) {
     69     auto_detect_proxy_->set_proxy(proxy);
     70   }
     71 
     72   void Start() {
     73     auto_detect_proxy_->Start();
     74   }
     75 
     76   void TestCopesWithProxy(const SocketAddress& proxy) {
     77     // Tests that at least autodetect doesn't crash for a given proxy address.
     78     ASSERT_TRUE(Create(kUserAgent,
     79                        kPath,
     80                        kHost,
     81                        kPort,
     82                        kSecure,
     83                        false));
     84     SetProxy(proxy);
     85     Start();
     86     ASSERT_TRUE(Run(kTimeoutMs));
     87   }
     88 
     89  private:
     90   void OnWorkDone(rtc::SignalThread *thread) {
     91     AutoDetectProxy *auto_detect_proxy =
     92         static_cast<rtc::AutoDetectProxy *>(thread);
     93     EXPECT_TRUE(auto_detect_proxy == auto_detect_proxy_);
     94     auto_detect_proxy_ = NULL;
     95     auto_detect_proxy->Release();
     96     done_ = true;
     97   }
     98 
     99   AutoDetectProxy *auto_detect_proxy_;
    100   bool done_;
    101 };
    102 
    103 TEST_F(AutoDetectProxyTest, DISABLED_ON_MAC(TestDetectUnresolvedProxy)) {
    104   TestCopesWithProxy(rtc::SocketAddress("localhost", 9999));
    105 }
    106 
    107 TEST_F(AutoDetectProxyTest, DISABLED_ON_MAC(TestDetectUnresolvableProxy)) {
    108   TestCopesWithProxy(rtc::SocketAddress("invalid", 9999));
    109 }
    110 
    111 TEST_F(AutoDetectProxyTest, DISABLED_ON_MAC(TestDetectIPv6Proxy)) {
    112   TestCopesWithProxy(rtc::SocketAddress("::1", 9999));
    113 }
    114 
    115 TEST_F(AutoDetectProxyTest, DISABLED_ON_MAC(TestDetectIPv4Proxy)) {
    116   TestCopesWithProxy(rtc::SocketAddress("127.0.0.1", 9999));
    117 }
    118 
    119 // Test that proxy detection completes successfully. (Does not actually verify
    120 // the correct detection result since we don't know what proxy to expect on an
    121 // arbitrary machine.)
    122 TEST_F(AutoDetectProxyTest, DISABLED_ON_MAC(TestProxyDetection)) {
    123   ASSERT_TRUE(Create(kUserAgent,
    124                      kPath,
    125                      kHost,
    126                      kPort,
    127                      kSecure,
    128                      true));
    129   ASSERT_TRUE(Run(kTimeoutMs));
    130 }
    131 
    132 }  // namespace rtc
    133