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