1 /*
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
33 namespace talk_base {
34
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;
43
44 class AutoDetectProxyTest : public testing::Test, public sigslot::has_slots<> {
45 public:
AutoDetectProxyTest()46 AutoDetectProxyTest() : auto_detect_proxy_(NULL), done_(false) {}
47
48 protected:
Create(const std::string & user_agent,const std::string & path,const std::string & host,uint16 port,bool secure)49 bool Create(const std::string &user_agent,
50 const std::string &path,
51 const std::string &host,
52 uint16 port,
53 bool secure) {
54 auto_detect_proxy_ = new AutoDetectProxy(user_agent);
55 EXPECT_TRUE(auto_detect_proxy_ != NULL);
56 if (!auto_detect_proxy_) {
57 return false;
58 }
59 Url<char> host_url(path, host, port);
60 host_url.set_secure(secure);
61 auto_detect_proxy_->set_server_url(host_url.url());
62 auto_detect_proxy_->SignalWorkDone.connect(
63 this,
64 &AutoDetectProxyTest::OnWorkDone);
65 auto_detect_proxy_->Start();
66 return true;
67 }
68
Run(int timeout_ms)69 bool Run(int timeout_ms) {
70 EXPECT_TRUE_WAIT(done_, timeout_ms);
71 return done_;
72 }
73
74 private:
OnWorkDone(talk_base::SignalThread * thread)75 void OnWorkDone(talk_base::SignalThread *thread) {
76 AutoDetectProxy *auto_detect_proxy =
77 static_cast<talk_base::AutoDetectProxy *>(thread);
78 EXPECT_TRUE(auto_detect_proxy == auto_detect_proxy_);
79 auto_detect_proxy_ = NULL;
80 auto_detect_proxy->Release();
81 done_ = true;
82 }
83
84 AutoDetectProxy *auto_detect_proxy_;
85 bool done_;
86 };
87
88 // Test that proxy detection completes successfully. (Does not actually verify
89 // the correct detection result since we don't know what proxy to expect on an
90 // arbitrary machine.)
TEST_F(AutoDetectProxyTest,TestProxyDetection)91 TEST_F(AutoDetectProxyTest, TestProxyDetection) {
92 ASSERT_TRUE(Create(kUserAgent,
93 kPath,
94 kHost,
95 kPort,
96 kSecure));
97 ASSERT_TRUE(Run(kTimeoutMs));
98 }
99
100 } // namespace talk_base
101