| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <map> |
| 6 #include <string> |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop.h" |
| 12 #include "net/proxy/proxy_config.h" |
| 13 #include "net/proxy/proxy_config_service_android.h" |
| 14 #include "net/proxy/proxy_info.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace net { |
| 18 |
| 19 namespace { |
| 20 |
| 21 class TestObserver : public ProxyConfigService::Observer { |
| 22 public: |
| 23 TestObserver() : availability_(ProxyConfigService::CONFIG_UNSET) {} |
| 24 |
| 25 // ProxyConfigService::Observer: |
| 26 virtual void OnProxyConfigChanged( |
| 27 const ProxyConfig& config, |
| 28 ProxyConfigService::ConfigAvailability availability) OVERRIDE { |
| 29 config_ = config; |
| 30 availability_ = availability; |
| 31 } |
| 32 |
| 33 ProxyConfigService::ConfigAvailability availability() const { |
| 34 return availability_; |
| 35 } |
| 36 |
| 37 const ProxyConfig& config() const { |
| 38 return config_; |
| 39 } |
| 40 |
| 41 private: |
| 42 ProxyConfig config_; |
| 43 ProxyConfigService::ConfigAvailability availability_; |
| 44 }; |
| 45 |
| 46 } // namespace |
| 47 |
| 48 typedef std::map<std::string, std::string> StringMap; |
| 49 |
| 50 class ProxyConfigServiceAndroidTestBase : public testing::Test { |
| 51 protected: |
| 52 // Note that the current thread's message loop is initialized by the test |
| 53 // suite (see net/base/net_test_suite.cc). |
| 54 ProxyConfigServiceAndroidTestBase(const StringMap& initial_configuration) |
| 55 : configuration_(initial_configuration), |
| 56 message_loop_(MessageLoop::current()), |
| 57 service_( |
| 58 message_loop_->message_loop_proxy(), |
| 59 message_loop_->message_loop_proxy(), |
| 60 base::Bind(&ProxyConfigServiceAndroidTestBase::GetProperty, |
| 61 base::Unretained(this))) {} |
| 62 |
| 63 virtual ~ProxyConfigServiceAndroidTestBase() {} |
| 64 |
| 65 // testing::Test: |
| 66 virtual void SetUp() OVERRIDE { |
| 67 message_loop_->RunAllPending(); |
| 68 service_.AddObserver(&observer_); |
| 69 } |
| 70 |
| 71 virtual void TearDown() OVERRIDE { |
| 72 service_.RemoveObserver(&observer_); |
| 73 } |
| 74 |
| 75 void ClearConfiguration() { |
| 76 configuration_.clear(); |
| 77 } |
| 78 |
| 79 void AddProperty(const std::string& key, const std::string& value) { |
| 80 configuration_[key] = value; |
| 81 } |
| 82 |
| 83 std::string GetProperty(const std::string& key) { |
| 84 StringMap::const_iterator it = configuration_.find(key); |
| 85 if (it == configuration_.end()) |
| 86 return std::string(); |
| 87 return it->second; |
| 88 } |
| 89 |
| 90 void ProxySettingsChanged() { |
| 91 service_.ProxySettingsChanged(); |
| 92 message_loop_->RunAllPending(); |
| 93 } |
| 94 |
| 95 void TestMapping(const std::string& url, const std::string& expected) { |
| 96 ProxyConfigService::ConfigAvailability availability; |
| 97 ProxyConfig proxy_config; |
| 98 availability = service_.GetLatestProxyConfig(&proxy_config); |
| 99 EXPECT_EQ(ProxyConfigService::CONFIG_VALID, availability); |
| 100 ProxyInfo proxy_info; |
| 101 proxy_config.proxy_rules().Apply(GURL(url), &proxy_info); |
| 102 EXPECT_EQ(expected, proxy_info.ToPacString()); |
| 103 } |
| 104 |
| 105 StringMap configuration_; |
| 106 TestObserver observer_; |
| 107 MessageLoop* const message_loop_; |
| 108 ProxyConfigServiceAndroid service_; |
| 109 }; |
| 110 |
| 111 class ProxyConfigServiceAndroidTest : public ProxyConfigServiceAndroidTestBase { |
| 112 public: |
| 113 ProxyConfigServiceAndroidTest() |
| 114 : ProxyConfigServiceAndroidTestBase(StringMap()) {} |
| 115 }; |
| 116 |
| 117 class ProxyConfigServiceAndroidWithInitialConfigTest |
| 118 : public ProxyConfigServiceAndroidTestBase { |
| 119 public: |
| 120 ProxyConfigServiceAndroidWithInitialConfigTest() |
| 121 : ProxyConfigServiceAndroidTestBase(MakeInitialConfiguration()) {} |
| 122 |
| 123 private: |
| 124 StringMap MakeInitialConfiguration() { |
| 125 StringMap initial_configuration; |
| 126 initial_configuration["http.proxyHost"] = "httpproxy.com"; |
| 127 initial_configuration["http.proxyPort"] = "8080"; |
| 128 return initial_configuration; |
| 129 } |
| 130 }; |
| 131 |
| 132 TEST_F(ProxyConfigServiceAndroidTest, TestChangePropertiesNotification) { |
| 133 // Set up a non-empty configuration |
| 134 AddProperty("http.proxyHost", "localhost"); |
| 135 ProxySettingsChanged(); |
| 136 EXPECT_EQ(ProxyConfigService::CONFIG_VALID, observer_.availability()); |
| 137 EXPECT_FALSE(observer_.config().proxy_rules().empty()); |
| 138 |
| 139 // Set up an empty configuration |
| 140 ClearConfiguration(); |
| 141 ProxySettingsChanged(); |
| 142 EXPECT_EQ(ProxyConfigService::CONFIG_VALID, observer_.availability()); |
| 143 EXPECT_TRUE(observer_.config().proxy_rules().empty()); |
| 144 } |
| 145 |
| 146 TEST_F(ProxyConfigServiceAndroidWithInitialConfigTest, TestInitialConfig) { |
| 147 // Make sure that the initial config is set. |
| 148 TestMapping("ftp://example.com/", "DIRECT"); |
| 149 TestMapping("http://example.com/", "PROXY httpproxy.com:8080"); |
| 150 |
| 151 // Override the initial configuration. |
| 152 ClearConfiguration(); |
| 153 AddProperty("http.proxyHost", "httpproxy.com"); |
| 154 ProxySettingsChanged(); |
| 155 TestMapping("http://example.com/", "PROXY httpproxy.com:80"); |
| 156 } |
| 157 |
| 158 // !! The following test cases are automatically generated from |
| 159 // !! net/android/tools/proxy_test_cases.py. |
| 160 // !! Please edit that file instead of editing the test cases below and |
| 161 // !! update also the corresponding Java unit tests in |
| 162 // !! AndroidProxySelectorTest.java |
| 163 |
| 164 TEST_F(ProxyConfigServiceAndroidTest, NoProxy) { |
| 165 // Test direct mapping when no proxy defined. |
| 166 ProxySettingsChanged(); |
| 167 TestMapping("ftp://example.com/", "DIRECT"); |
| 168 TestMapping("http://example.com/", "DIRECT"); |
| 169 TestMapping("https://example.com/", "DIRECT"); |
| 170 } |
| 171 |
| 172 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostAndPort) { |
| 173 // Test http.proxyHost and http.proxyPort works. |
| 174 AddProperty("http.proxyHost", "httpproxy.com"); |
| 175 AddProperty("http.proxyPort", "8080"); |
| 176 ProxySettingsChanged(); |
| 177 TestMapping("ftp://example.com/", "DIRECT"); |
| 178 TestMapping("http://example.com/", "PROXY httpproxy.com:8080"); |
| 179 TestMapping("https://example.com/", "DIRECT"); |
| 180 } |
| 181 |
| 182 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostOnly) { |
| 183 // We should get the default port (80) for proxied hosts. |
| 184 AddProperty("http.proxyHost", "httpproxy.com"); |
| 185 ProxySettingsChanged(); |
| 186 TestMapping("ftp://example.com/", "DIRECT"); |
| 187 TestMapping("http://example.com/", "PROXY httpproxy.com:80"); |
| 188 TestMapping("https://example.com/", "DIRECT"); |
| 189 } |
| 190 |
| 191 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyPortOnly) { |
| 192 // http.proxyPort only should not result in any hosts being proxied. |
| 193 AddProperty("http.proxyPort", "8080"); |
| 194 ProxySettingsChanged(); |
| 195 TestMapping("ftp://example.com/", "DIRECT"); |
| 196 TestMapping("http://example.com/", "DIRECT"); |
| 197 TestMapping("https://example.com/", "DIRECT"); |
| 198 } |
| 199 |
| 200 TEST_F(ProxyConfigServiceAndroidTest, HttpNonProxyHosts1) { |
| 201 // Test that HTTP non proxy hosts are mapped correctly |
| 202 AddProperty("http.nonProxyHosts", "slashdot.org"); |
| 203 AddProperty("http.proxyHost", "httpproxy.com"); |
| 204 AddProperty("http.proxyPort", "8080"); |
| 205 ProxySettingsChanged(); |
| 206 TestMapping("http://example.com/", "PROXY httpproxy.com:8080"); |
| 207 TestMapping("http://slashdot.org/", "DIRECT"); |
| 208 } |
| 209 |
| 210 TEST_F(ProxyConfigServiceAndroidTest, HttpNonProxyHosts2) { |
| 211 // Test that | pattern works. |
| 212 AddProperty("http.nonProxyHosts", "slashdot.org|freecode.net"); |
| 213 AddProperty("http.proxyHost", "httpproxy.com"); |
| 214 AddProperty("http.proxyPort", "8080"); |
| 215 ProxySettingsChanged(); |
| 216 TestMapping("http://example.com/", "PROXY httpproxy.com:8080"); |
| 217 TestMapping("http://freecode.net/", "DIRECT"); |
| 218 TestMapping("http://slashdot.org/", "DIRECT"); |
| 219 } |
| 220 |
| 221 TEST_F(ProxyConfigServiceAndroidTest, HttpNonProxyHosts3) { |
| 222 // Test that * pattern works. |
| 223 AddProperty("http.nonProxyHosts", "*example.com"); |
| 224 AddProperty("http.proxyHost", "httpproxy.com"); |
| 225 AddProperty("http.proxyPort", "8080"); |
| 226 ProxySettingsChanged(); |
| 227 TestMapping("http://example.com/", "DIRECT"); |
| 228 TestMapping("http://slashdot.org/", "PROXY httpproxy.com:8080"); |
| 229 TestMapping("http://www.example.com/", "DIRECT"); |
| 230 } |
| 231 |
| 232 TEST_F(ProxyConfigServiceAndroidTest, FtpNonProxyHosts) { |
| 233 // Test that FTP non proxy hosts are mapped correctly |
| 234 AddProperty("ftp.nonProxyHosts", "slashdot.org"); |
| 235 AddProperty("ftp.proxyHost", "httpproxy.com"); |
| 236 AddProperty("ftp.proxyPort", "8080"); |
| 237 ProxySettingsChanged(); |
| 238 TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080"); |
| 239 TestMapping("http://example.com/", "DIRECT"); |
| 240 } |
| 241 |
| 242 TEST_F(ProxyConfigServiceAndroidTest, FtpProxyHostAndPort) { |
| 243 // Test ftp.proxyHost and ftp.proxyPort works. |
| 244 AddProperty("ftp.proxyHost", "httpproxy.com"); |
| 245 AddProperty("ftp.proxyPort", "8080"); |
| 246 ProxySettingsChanged(); |
| 247 TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080"); |
| 248 TestMapping("http://example.com/", "DIRECT"); |
| 249 TestMapping("https://example.com/", "DIRECT"); |
| 250 } |
| 251 |
| 252 TEST_F(ProxyConfigServiceAndroidTest, FtpProxyHostOnly) { |
| 253 // Test ftp.proxyHost and default port. |
| 254 AddProperty("ftp.proxyHost", "httpproxy.com"); |
| 255 ProxySettingsChanged(); |
| 256 TestMapping("ftp://example.com/", "PROXY httpproxy.com:80"); |
| 257 TestMapping("http://example.com/", "DIRECT"); |
| 258 TestMapping("https://example.com/", "DIRECT"); |
| 259 } |
| 260 |
| 261 TEST_F(ProxyConfigServiceAndroidTest, HttpsProxyHostAndPort) { |
| 262 // Test https.proxyHost and https.proxyPort works. |
| 263 AddProperty("https.proxyHost", "httpproxy.com"); |
| 264 AddProperty("https.proxyPort", "8080"); |
| 265 ProxySettingsChanged(); |
| 266 TestMapping("ftp://example.com/", "DIRECT"); |
| 267 TestMapping("http://example.com/", "DIRECT"); |
| 268 TestMapping("https://example.com/", "HTTPS httpproxy.com:8080"); |
| 269 } |
| 270 |
| 271 TEST_F(ProxyConfigServiceAndroidTest, HttpsProxyHostOnly) { |
| 272 // Test https.proxyHost and default port. |
| 273 AddProperty("https.proxyHost", "httpproxy.com"); |
| 274 ProxySettingsChanged(); |
| 275 TestMapping("ftp://example.com/", "DIRECT"); |
| 276 TestMapping("http://example.com/", "DIRECT"); |
| 277 TestMapping("https://example.com/", "HTTPS httpproxy.com:443"); |
| 278 } |
| 279 |
| 280 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostIPv6) { |
| 281 // Test IPv6 https.proxyHost and default port. |
| 282 AddProperty("http.proxyHost", "a:b:c::d:1"); |
| 283 ProxySettingsChanged(); |
| 284 TestMapping("ftp://example.com/", "DIRECT"); |
| 285 TestMapping("http://example.com/", "PROXY [a:b:c::d:1]:80"); |
| 286 } |
| 287 |
| 288 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostAndPortIPv6) { |
| 289 // Test IPv6 http.proxyHost and http.proxyPort works. |
| 290 AddProperty("http.proxyHost", "a:b:c::d:1"); |
| 291 AddProperty("http.proxyPort", "8080"); |
| 292 ProxySettingsChanged(); |
| 293 TestMapping("ftp://example.com/", "DIRECT"); |
| 294 TestMapping("http://example.com/", "PROXY [a:b:c::d:1]:8080"); |
| 295 } |
| 296 |
| 297 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostAndInvalidPort) { |
| 298 // Test invalid http.proxyPort does not crash. |
| 299 AddProperty("http.proxyHost", "a:b:c::d:1"); |
| 300 AddProperty("http.proxyPort", "65536"); |
| 301 ProxySettingsChanged(); |
| 302 TestMapping("ftp://example.com/", "DIRECT"); |
| 303 TestMapping("http://example.com/", "DIRECT"); |
| 304 } |
| 305 |
| 306 TEST_F(ProxyConfigServiceAndroidTest, DefaultProxyExplictPort) { |
| 307 // Default http proxy is used if a scheme-specific one is not found. |
| 308 AddProperty("ftp.proxyHost", "httpproxy.com"); |
| 309 AddProperty("ftp.proxyPort", "8080"); |
| 310 AddProperty("proxyHost", "defaultproxy.com"); |
| 311 AddProperty("proxyPort", "8080"); |
| 312 ProxySettingsChanged(); |
| 313 TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080"); |
| 314 TestMapping("http://example.com/", "PROXY defaultproxy.com:8080"); |
| 315 TestMapping("https://example.com/", "HTTPS defaultproxy.com:8080"); |
| 316 } |
| 317 |
| 318 TEST_F(ProxyConfigServiceAndroidTest, DefaultProxyDefaultPort) { |
| 319 // Check that the default proxy port is as expected. |
| 320 AddProperty("proxyHost", "defaultproxy.com"); |
| 321 ProxySettingsChanged(); |
| 322 TestMapping("http://example.com/", "PROXY defaultproxy.com:80"); |
| 323 TestMapping("https://example.com/", "HTTPS defaultproxy.com:443"); |
| 324 } |
| 325 |
| 326 TEST_F(ProxyConfigServiceAndroidTest, FallbackToSocks) { |
| 327 // SOCKS proxy is used if scheme-specific one is not found. |
| 328 AddProperty("http.proxyHost", "defaultproxy.com"); |
| 329 AddProperty("socksProxyHost", "socksproxy.com"); |
| 330 ProxySettingsChanged(); |
| 331 TestMapping("ftp://example.com", "SOCKS5 socksproxy.com:1080"); |
| 332 TestMapping("http://example.com/", "PROXY defaultproxy.com:80"); |
| 333 TestMapping("https://example.com/", "SOCKS5 socksproxy.com:1080"); |
| 334 } |
| 335 |
| 336 TEST_F(ProxyConfigServiceAndroidTest, SocksExplicitPort) { |
| 337 // SOCKS proxy port is used if specified |
| 338 AddProperty("socksProxyHost", "socksproxy.com"); |
| 339 AddProperty("socksProxyPort", "9000"); |
| 340 ProxySettingsChanged(); |
| 341 TestMapping("http://example.com/", "SOCKS5 socksproxy.com:9000"); |
| 342 } |
| 343 |
| 344 TEST_F(ProxyConfigServiceAndroidTest, HttpProxySupercedesSocks) { |
| 345 // SOCKS proxy is ignored if default HTTP proxy defined. |
| 346 AddProperty("proxyHost", "defaultproxy.com"); |
| 347 AddProperty("socksProxyHost", "socksproxy.com"); |
| 348 AddProperty("socksProxyPort", "9000"); |
| 349 ProxySettingsChanged(); |
| 350 TestMapping("http://example.com/", "PROXY defaultproxy.com:80"); |
| 351 } |
| 352 |
| 353 } // namespace net |
| OLD | NEW |