3 * Copyright 2004--2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
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"
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;
44 class AutoDetectProxyTest : public testing::Test, public sigslot::has_slots<> {
46 AutoDetectProxyTest() : auto_detect_proxy_(NULL), done_(false) {}
49 bool Create(const std::string &user_agent,
50 const std::string &path,
51 const std::string &host,
55 auto_detect_proxy_ = new AutoDetectProxy(user_agent);
56 EXPECT_TRUE(auto_detect_proxy_ != NULL);
57 if (!auto_detect_proxy_) {
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(
65 &AutoDetectProxyTest::OnWorkDone);
67 auto_detect_proxy_->Start();
72 bool Run(int timeout_ms) {
73 EXPECT_TRUE_WAIT(done_, timeout_ms);
77 void SetProxy(const SocketAddress& proxy) {
78 auto_detect_proxy_->set_proxy(proxy);
82 auto_detect_proxy_->Start();
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,
95 ASSERT_TRUE(Run(kTimeoutMs));
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();
108 AutoDetectProxy *auto_detect_proxy_;
112 TEST_F(AutoDetectProxyTest, TestDetectUnresolvedProxy) {
113 TestCopesWithProxy(talk_base::SocketAddress("localhost", 9999));
116 TEST_F(AutoDetectProxyTest, TestDetectUnresolvableProxy) {
117 TestCopesWithProxy(talk_base::SocketAddress("invalid", 9999));
120 TEST_F(AutoDetectProxyTest, TestDetectIPv6Proxy) {
121 TestCopesWithProxy(talk_base::SocketAddress("::1", 9999));
124 TEST_F(AutoDetectProxyTest, TestDetectIPv4Proxy) {
125 TestCopesWithProxy(talk_base::SocketAddress("127.0.0.1", 9999));
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,
138 ASSERT_TRUE(Run(kTimeoutMs));
141 } // namespace talk_base