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