You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "talk/base/autodetectproxy.h"
  28. #include "talk/base/gunit.h"
  29. #include "talk/base/httpcommon.h"
  30. #include "talk/base/httpcommon-inl.h"
  31. namespace talk_base {
  32. static const char kUserAgent[] = "";
  33. static const char kPath[] = "/";
  34. static const char kHost[] = "relay.google.com";
  35. static const uint16 kPort = 443;
  36. static const bool kSecure = true;
  37. // Each of the two stages in AutoDetectProxy has a 2-second time-out, so 5
  38. // seconds total should be enough.
  39. static const int kTimeoutMs = 5000;
  40. class AutoDetectProxyTest : public testing::Test, public sigslot::has_slots<> {
  41. public:
  42. AutoDetectProxyTest() : auto_detect_proxy_(NULL), done_(false) {}
  43. protected:
  44. bool Create(const std::string &user_agent,
  45. const std::string &path,
  46. const std::string &host,
  47. uint16 port,
  48. bool secure,
  49. bool startnow) {
  50. auto_detect_proxy_ = new AutoDetectProxy(user_agent);
  51. EXPECT_TRUE(auto_detect_proxy_ != NULL);
  52. if (!auto_detect_proxy_) {
  53. return false;
  54. }
  55. Url<char> host_url(path, host, port);
  56. host_url.set_secure(secure);
  57. auto_detect_proxy_->set_server_url(host_url.url());
  58. auto_detect_proxy_->SignalWorkDone.connect(
  59. this,
  60. &AutoDetectProxyTest::OnWorkDone);
  61. if (startnow) {
  62. auto_detect_proxy_->Start();
  63. }
  64. return true;
  65. }
  66. bool Run(int timeout_ms) {
  67. EXPECT_TRUE_WAIT(done_, timeout_ms);
  68. return done_;
  69. }
  70. void SetProxy(const SocketAddress& proxy) {
  71. auto_detect_proxy_->set_proxy(proxy);
  72. }
  73. void Start() {
  74. auto_detect_proxy_->Start();
  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. private:
  89. void OnWorkDone(talk_base::SignalThread *thread) {
  90. AutoDetectProxy *auto_detect_proxy =
  91. static_cast<talk_base::AutoDetectProxy *>(thread);
  92. EXPECT_TRUE(auto_detect_proxy == auto_detect_proxy_);
  93. auto_detect_proxy_ = NULL;
  94. auto_detect_proxy->Release();
  95. done_ = true;
  96. }
  97. AutoDetectProxy *auto_detect_proxy_;
  98. bool done_;
  99. };
  100. TEST_F(AutoDetectProxyTest, TestDetectUnresolvedProxy) {
  101. TestCopesWithProxy(talk_base::SocketAddress("localhost", 9999));
  102. }
  103. TEST_F(AutoDetectProxyTest, TestDetectUnresolvableProxy) {
  104. TestCopesWithProxy(talk_base::SocketAddress("invalid", 9999));
  105. }
  106. TEST_F(AutoDetectProxyTest, TestDetectIPv6Proxy) {
  107. TestCopesWithProxy(talk_base::SocketAddress("::1", 9999));
  108. }
  109. TEST_F(AutoDetectProxyTest, TestDetectIPv4Proxy) {
  110. TestCopesWithProxy(talk_base::SocketAddress("127.0.0.1", 9999));
  111. }
  112. // Test that proxy detection completes successfully. (Does not actually verify
  113. // the correct detection result since we don't know what proxy to expect on an
  114. // arbitrary machine.)
  115. TEST_F(AutoDetectProxyTest, TestProxyDetection) {
  116. ASSERT_TRUE(Create(kUserAgent,
  117. kPath,
  118. kHost,
  119. kPort,
  120. kSecure,
  121. true));
  122. ASSERT_TRUE(Run(kTimeoutMs));
  123. }
  124. } // namespace talk_base