blob: 4a2688265cc9f43dd235076d66fa3312f3e2bc62 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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"
henrike@webrtc.orgfded02c2014-09-19 13:10:10 +000015#include "webrtc/test/testsupport/gtest_disable.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
17namespace rtc {
18
19static const char kUserAgent[] = "";
20static const char kPath[] = "/";
21static const char kHost[] = "relay.google.com";
22static const uint16 kPort = 443;
23static 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.
33static const int kTimeoutMs = 10000;
34
35class 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
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000103TEST_F(AutoDetectProxyTest, TestDetectUnresolvedProxy) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104 TestCopesWithProxy(rtc::SocketAddress("localhost", 9999));
105}
106
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000107TEST_F(AutoDetectProxyTest, TestDetectUnresolvableProxy) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108 TestCopesWithProxy(rtc::SocketAddress("invalid", 9999));
109}
110
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000111TEST_F(AutoDetectProxyTest, TestDetectIPv6Proxy) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112 TestCopesWithProxy(rtc::SocketAddress("::1", 9999));
113}
114
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000115TEST_F(AutoDetectProxyTest, TestDetectIPv4Proxy) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 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.)
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +0000122TEST_F(AutoDetectProxyTest, TestProxyDetection) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 ASSERT_TRUE(Create(kUserAgent,
124 kPath,
125 kHost,
126 kPort,
127 kSecure,
128 true));
129 ASSERT_TRUE(Run(kTimeoutMs));
130}
131
132} // namespace rtc