2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
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.
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"
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
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;
35 class AutoDetectProxyTest : public testing::Test, public sigslot::has_slots<> {
37 AutoDetectProxyTest() : auto_detect_proxy_(NULL), done_(false) {}
40 bool Create(const std::string &user_agent,
41 const std::string &path,
42 const std::string &host,
46 auto_detect_proxy_ = new AutoDetectProxy(user_agent);
47 EXPECT_TRUE(auto_detect_proxy_ != NULL);
48 if (!auto_detect_proxy_) {
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(
56 &AutoDetectProxyTest::OnWorkDone);
58 auto_detect_proxy_->Start();
63 bool Run(int timeout_ms) {
64 EXPECT_TRUE_WAIT(done_, timeout_ms);
68 void SetProxy(const SocketAddress& proxy) {
69 auto_detect_proxy_->set_proxy(proxy);
73 auto_detect_proxy_->Start();
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,
86 ASSERT_TRUE(Run(kTimeoutMs));
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();
99 AutoDetectProxy *auto_detect_proxy_;
103 TEST_F(AutoDetectProxyTest, DISABLED_ON_MAC(TestDetectUnresolvedProxy)) {
104 TestCopesWithProxy(rtc::SocketAddress("localhost", 9999));
107 TEST_F(AutoDetectProxyTest, DISABLED_ON_MAC(TestDetectUnresolvableProxy)) {
108 TestCopesWithProxy(rtc::SocketAddress("invalid", 9999));
111 TEST_F(AutoDetectProxyTest, DISABLED_ON_MAC(TestDetectIPv6Proxy)) {
112 TestCopesWithProxy(rtc::SocketAddress("::1", 9999));
115 TEST_F(AutoDetectProxyTest, DISABLED_ON_MAC(TestDetectIPv4Proxy)) {
116 TestCopesWithProxy(rtc::SocketAddress("127.0.0.1", 9999));
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,
129 ASSERT_TRUE(Run(kTimeoutMs));