Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2004--2011, Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include "talk/base/autodetectproxy.h"
     29 #include "talk/base/gunit.h"
     30 #include "talk/base/httpcommon.h"
     31 #include "talk/base/httpcommon-inl.h"
     32 
     33 namespace talk_base {
     34 
     35 static const char kUserAgent[] = "";
     36 static const char kPath[] = "/";
     37 static const char kHost[] = "relay.google.com";
     38 static const uint16 kPort = 443;
     39 static const bool kSecure = true;
     40 // Each of the two stages in AutoDetectProxy has a 2-second time-out, so 5
     41 // seconds total should be enough.
     42 static const int kTimeoutMs = 5000;
     43 
     44 class AutoDetectProxyTest : public testing::Test, public sigslot::has_slots<> {
     45  public:
     46   AutoDetectProxyTest() : auto_detect_proxy_(NULL), done_(false) {}
     47 
     48  protected:
     49   bool Create(const std::string &user_agent,
     50               const std::string &path,
     51               const std::string &host,
     52               uint16 port,
     53               bool secure,
     54               bool startnow) {
     55     auto_detect_proxy_ = new AutoDetectProxy(user_agent);
     56     EXPECT_TRUE(auto_detect_proxy_ != NULL);
     57     if (!auto_detect_proxy_) {
     58       return false;
     59     }
     60     Url<char> host_url(path, host, port);
     61     host_url.set_secure(secure);
     62     auto_detect_proxy_->set_server_url(host_url.url());
     63     auto_detect_proxy_->SignalWorkDone.connect(
     64         this,
     65         &AutoDetectProxyTest::OnWorkDone);
     66     if (startnow) {
     67       auto_detect_proxy_->Start();
     68     }
     69     return true;
     70   }
     71 
     72   bool Run(int timeout_ms) {
     73     EXPECT_TRUE_WAIT(done_, timeout_ms);
     74     return done_;
     75   }
     76 
     77   void SetProxy(const SocketAddress& proxy) {
     78     auto_detect_proxy_->set_proxy(proxy);
     79   }
     80 
     81   void Start() {
     82     auto_detect_proxy_->Start();
     83   }
     84 
     85   void TestCopesWithProxy(const SocketAddress& proxy) {
     86     // Tests that at least autodetect doesn't crash for a given proxy address.
     87     ASSERT_TRUE(Create(kUserAgent,
     88                        kPath,
     89                        kHost,
     90                        kPort,
     91                        kSecure,
     92                        false));
     93     SetProxy(proxy);
     94     Start();
     95     ASSERT_TRUE(Run(kTimeoutMs));
     96   }
     97 
     98  private:
     99   void OnWorkDone(talk_base::SignalThread *thread) {
    100     AutoDetectProxy *auto_detect_proxy =
    101         static_cast<talk_base::AutoDetectProxy *>(thread);
    102     EXPECT_TRUE(auto_detect_proxy == auto_detect_proxy_);
    103     auto_detect_proxy_ = NULL;
    104     auto_detect_proxy->Release();
    105     done_ = true;
    106   }
    107 
    108   AutoDetectProxy *auto_detect_proxy_;
    109   bool done_;
    110 };
    111 
    112 TEST_F(AutoDetectProxyTest, TestDetectUnresolvedProxy) {
    113   TestCopesWithProxy(talk_base::SocketAddress("localhost", 9999));
    114 }
    115 
    116 TEST_F(AutoDetectProxyTest, TestDetectUnresolvableProxy) {
    117   TestCopesWithProxy(talk_base::SocketAddress("invalid", 9999));
    118 }
    119 
    120 TEST_F(AutoDetectProxyTest, TestDetectIPv6Proxy) {
    121   TestCopesWithProxy(talk_base::SocketAddress("::1", 9999));
    122 }
    123 
    124 TEST_F(AutoDetectProxyTest, TestDetectIPv4Proxy) {
    125   TestCopesWithProxy(talk_base::SocketAddress("127.0.0.1", 9999));
    126 }
    127 
    128 // Test that proxy detection completes successfully. (Does not actually verify
    129 // the correct detection result since we don't know what proxy to expect on an
    130 // arbitrary machine.)
    131 TEST_F(AutoDetectProxyTest, TestProxyDetection) {
    132   ASSERT_TRUE(Create(kUserAgent,
    133                      kPath,
    134                      kHost,
    135                      kPort,
    136                      kSecure,
    137                      true));
    138   ASSERT_TRUE(Run(kTimeoutMs));
    139 }
    140 
    141 }  // namespace talk_base
    142