Chromium Code Reviews| 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 // Called from main thread. | |
|
Ryan Sleevi
2012/05/29 17:34:43
nit: still applicable? No threads involved now
Philippe
2012/06/01 13:06:37
Done.
| |
| 34 ProxyConfigService::ConfigAvailability availability() const { | |
| 35 return availability_; | |
| 36 } | |
| 37 | |
| 38 const ProxyConfig& config() const { | |
| 39 return config_; | |
| 40 } | |
| 41 | |
| 42 private: | |
| 43 ProxyConfig config_; | |
| 44 ProxyConfigService::ConfigAvailability availability_; | |
| 45 }; | |
| 46 | |
| 47 std::string ToString(const ProxyInfo& proxy_info) { | |
|
Ryan Sleevi
2012/05/29 17:34:43
The naming of this method is not ideal - but more
Philippe
2012/06/01 13:06:37
Not needed (anymore) indeed.
| |
| 48 return proxy_info.ToPacString(); | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 class ProxyConfigServiceAndroidTest : public testing::Test { | |
| 54 protected: | |
| 55 ProxyConfigServiceAndroidTest() : service_(NULL) {} | |
| 56 | |
| 57 // testing::Test: | |
| 58 virtual void SetUp() OVERRIDE { | |
|
Ryan Sleevi
2012/05/29 17:34:43
Note: Every test runs as a new instance, so you do
Philippe
2012/06/01 13:06:37
I didn't know that a new instance was created for
| |
| 59 observer_.reset(new TestObserver()); | |
| 60 message_loop_ = MessageLoop::current(); | |
|
Ryan Sleevi
2012/05/29 17:34:43
BUG: There is no guarantee that there will be a cu
Philippe
2012/06/01 13:06:37
I created a new message loop. Thanks for the detai
| |
| 61 service_.reset(new ProxyConfigServiceAndroid( | |
| 62 message_loop_->message_loop_proxy(), | |
| 63 message_loop_->message_loop_proxy(), | |
| 64 base::Bind(&ProxyConfigServiceAndroidTest::GetProperty, | |
| 65 base::Unretained(this)))); | |
| 66 message_loop_->RunAllPending(); | |
| 67 service_->AddObserver(observer_.get()); | |
| 68 } | |
| 69 | |
| 70 virtual void TearDown() OVERRIDE { | |
| 71 ClearConfiguration(); | |
| 72 service_.reset(); | |
|
Ryan Sleevi
2012/05/29 17:34:43
BUG: You don't RemoveObserver (line 67)
Philippe
2012/06/01 13:06:37
Done.
| |
| 73 message_loop_ = NULL; | |
| 74 } | |
| 75 | |
| 76 void ClearConfiguration() { | |
| 77 configuration_.clear(); | |
| 78 } | |
| 79 | |
| 80 void AddProperty(const std::string& key, const std::string& value) { | |
| 81 configuration_[key] = value; | |
| 82 } | |
| 83 | |
| 84 std::string GetProperty(const std::string& key) { | |
| 85 StringMap::const_iterator it = configuration_.find(key); | |
| 86 if (it == configuration_.end()) | |
| 87 return std::string(); | |
| 88 return it->second; | |
| 89 } | |
| 90 | |
| 91 void ProxySettingsChanged() { | |
| 92 service_->ProxySettingsChanged(NULL, NULL); | |
| 93 message_loop_->RunAllPending(); | |
| 94 } | |
| 95 | |
| 96 void TestMapping(const std::string& url, const std::string& expected) { | |
| 97 ProxyConfigService::ConfigAvailability availability; | |
| 98 ProxyConfig proxy_config; | |
| 99 availability = service_->GetLatestProxyConfig(&proxy_config); | |
| 100 TestMappingForProxyConfig(url, expected, availability, &proxy_config); | |
| 101 } | |
| 102 | |
| 103 static void TestMappingForProxyConfig( | |
| 104 const std::string& url, | |
| 105 const std::string& expected, | |
| 106 ProxyConfigService::ConfigAvailability availability, | |
| 107 ProxyConfig* proxy_config) { | |
|
Ryan Sleevi
2012/05/29 17:34:43
nit: You only seem to call this method on line 100
Philippe
2012/06/01 13:06:37
Indeed. I had a unit test using it before.
| |
| 108 EXPECT_EQ(ProxyConfigService::CONFIG_VALID, availability); | |
| 109 ProxyInfo proxy_info; | |
| 110 proxy_config->proxy_rules().Apply(GURL(url), &proxy_info); | |
| 111 EXPECT_EQ(expected, ToString(proxy_info)); | |
| 112 } | |
| 113 | |
| 114 scoped_ptr<TestObserver> observer_; | |
| 115 scoped_ptr<ProxyConfigServiceAndroid> service_; | |
| 116 | |
| 117 private: | |
| 118 typedef std::map<std::string, std::string> StringMap; | |
|
Ryan Sleevi
2012/05/29 17:34:43
Is this typedef still necessary? Seems to only be
Philippe
2012/06/01 13:06:37
It wasn't indeed. I had to keep it though since I'
| |
| 119 StringMap configuration_; | |
| 120 | |
| 121 MessageLoop* message_loop_; | |
| 122 }; | |
| 123 | |
| 124 class ProxyConfigServiceAndroidWithInitialConfigTest | |
| 125 : public ProxyConfigServiceAndroidTest { | |
| 126 protected: | |
| 127 // ProxyConfigServiceAndroidTest: | |
| 128 virtual void SetUp() OVERRIDE { | |
| 129 AddProperty("http.proxyHost", "httpproxy.com"); | |
| 130 AddProperty("http.proxyPort", "8080"); | |
| 131 ProxyConfigServiceAndroidTest::SetUp(); | |
|
Ryan Sleevi
2012/05/29 17:34:43
nit: You should generally always call the parent m
Philippe
2012/06/01 13:06:37
This is a little subtle. I set the configuration b
| |
| 132 } | |
| 133 }; | |
| 134 | |
| 135 TEST_F(ProxyConfigServiceAndroidTest, TestChangePropertiesNotification) { | |
| 136 // Set up a non-empty configuration | |
| 137 AddProperty("http.proxyHost", "localhost"); | |
| 138 ProxySettingsChanged(); | |
| 139 EXPECT_EQ(ProxyConfigService::CONFIG_VALID, observer_->availability()); | |
| 140 EXPECT_FALSE(observer_->config().proxy_rules().empty()); | |
| 141 | |
| 142 // Set up an empty configuration | |
| 143 ClearConfiguration(); | |
| 144 ProxySettingsChanged(); | |
| 145 EXPECT_EQ(ProxyConfigService::CONFIG_VALID, observer_->availability()); | |
| 146 EXPECT_TRUE(observer_->config().proxy_rules().empty()); | |
| 147 } | |
| 148 | |
| 149 TEST_F(ProxyConfigServiceAndroidWithInitialConfigTest, TestInitialConfig) { | |
| 150 // Make sure that the initial config is set. | |
| 151 TestMapping("ftp://example.com/", "DIRECT"); | |
| 152 TestMapping("http://example.com/", "PROXY httpproxy.com:8080"); | |
| 153 | |
| 154 // Override the initial configuration. | |
| 155 ClearConfiguration(); | |
| 156 AddProperty("http.proxyHost", "httpproxy.com"); | |
| 157 ProxySettingsChanged(); | |
| 158 TestMapping("http://example.com/", "PROXY httpproxy.com:80"); | |
| 159 } | |
| 160 | |
| 161 // !! The following test cases are automatically generated from | |
| 162 // !! net/android/tools/proxy_test_cases.py. | |
| 163 // !! Please edit that file instead of editing the test cases below and | |
| 164 // !! update also the corresponding Java unit tests in | |
| 165 // !! AndroidProxySelectorTest.java | |
| 166 | |
| 167 TEST_F(ProxyConfigServiceAndroidTest, NoProxy) { | |
| 168 // Test direct mapping when no proxy defined. | |
| 169 ProxySettingsChanged(); | |
| 170 TestMapping("ftp://example.com/", "DIRECT"); | |
| 171 TestMapping("http://example.com/", "DIRECT"); | |
| 172 TestMapping("https://example.com/", "DIRECT"); | |
| 173 } | |
| 174 | |
| 175 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostAndPort) { | |
| 176 // Test http.proxyHost and http.proxyPort works. | |
| 177 AddProperty("http.proxyHost", "httpproxy.com"); | |
| 178 AddProperty("http.proxyPort", "8080"); | |
| 179 ProxySettingsChanged(); | |
| 180 TestMapping("ftp://example.com/", "DIRECT"); | |
| 181 TestMapping("http://example.com/", "PROXY httpproxy.com:8080"); | |
| 182 TestMapping("https://example.com/", "DIRECT"); | |
| 183 } | |
| 184 | |
| 185 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostOnly) { | |
| 186 // We should get the default port (80) for proxied hosts. | |
| 187 AddProperty("http.proxyHost", "httpproxy.com"); | |
| 188 ProxySettingsChanged(); | |
| 189 TestMapping("ftp://example.com/", "DIRECT"); | |
| 190 TestMapping("http://example.com/", "PROXY httpproxy.com:80"); | |
| 191 TestMapping("https://example.com/", "DIRECT"); | |
| 192 } | |
| 193 | |
| 194 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyPortOnly) { | |
| 195 // http.proxyPort only should not result in any hosts being proxied. | |
| 196 AddProperty("http.proxyPort", "8080"); | |
| 197 ProxySettingsChanged(); | |
| 198 TestMapping("ftp://example.com/", "DIRECT"); | |
| 199 TestMapping("http://example.com/", "DIRECT"); | |
| 200 TestMapping("https://example.com/", "DIRECT"); | |
| 201 } | |
| 202 | |
| 203 TEST_F(ProxyConfigServiceAndroidTest, HttpNonProxyHosts1) { | |
| 204 // Test that HTTP non proxy hosts are mapped correctly | |
| 205 AddProperty("http.nonProxyHosts", "slashdot.org"); | |
| 206 AddProperty("http.proxyHost", "httpproxy.com"); | |
| 207 AddProperty("http.proxyPort", "8080"); | |
| 208 ProxySettingsChanged(); | |
| 209 TestMapping("http://example.com/", "PROXY httpproxy.com:8080"); | |
| 210 TestMapping("http://slashdot.org/", "DIRECT"); | |
| 211 } | |
| 212 | |
| 213 TEST_F(ProxyConfigServiceAndroidTest, HttpNonProxyHosts2) { | |
| 214 // Test that | pattern works. | |
| 215 AddProperty("http.nonProxyHosts", "slashdot.org|freecode.net"); | |
| 216 AddProperty("http.proxyHost", "httpproxy.com"); | |
| 217 AddProperty("http.proxyPort", "8080"); | |
| 218 ProxySettingsChanged(); | |
| 219 TestMapping("http://example.com/", "PROXY httpproxy.com:8080"); | |
| 220 TestMapping("http://freecode.net/", "DIRECT"); | |
| 221 TestMapping("http://slashdot.org/", "DIRECT"); | |
| 222 } | |
| 223 | |
| 224 TEST_F(ProxyConfigServiceAndroidTest, HttpNonProxyHosts3) { | |
| 225 // Test that * pattern works. | |
| 226 AddProperty("http.nonProxyHosts", "*example.com"); | |
| 227 AddProperty("http.proxyHost", "httpproxy.com"); | |
| 228 AddProperty("http.proxyPort", "8080"); | |
| 229 ProxySettingsChanged(); | |
| 230 TestMapping("http://example.com/", "DIRECT"); | |
| 231 TestMapping("http://slashdot.org/", "PROXY httpproxy.com:8080"); | |
| 232 TestMapping("http://www.example.com/", "DIRECT"); | |
| 233 } | |
| 234 | |
| 235 TEST_F(ProxyConfigServiceAndroidTest, FtpNonProxyHosts) { | |
| 236 // Test that FTP non proxy hosts are mapped correctly | |
| 237 AddProperty("ftp.nonProxyHosts", "slashdot.org"); | |
| 238 AddProperty("ftp.proxyHost", "httpproxy.com"); | |
| 239 AddProperty("ftp.proxyPort", "8080"); | |
| 240 ProxySettingsChanged(); | |
| 241 TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080"); | |
| 242 TestMapping("http://example.com/", "DIRECT"); | |
| 243 } | |
| 244 | |
| 245 TEST_F(ProxyConfigServiceAndroidTest, FtpProxyHostAndPort) { | |
| 246 // Test ftp.proxyHost and ftp.proxyPort works. | |
| 247 AddProperty("ftp.proxyHost", "httpproxy.com"); | |
| 248 AddProperty("ftp.proxyPort", "8080"); | |
| 249 ProxySettingsChanged(); | |
| 250 TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080"); | |
| 251 TestMapping("http://example.com/", "DIRECT"); | |
| 252 TestMapping("https://example.com/", "DIRECT"); | |
| 253 } | |
| 254 | |
| 255 TEST_F(ProxyConfigServiceAndroidTest, FtpProxyHostOnly) { | |
| 256 // Test ftp.proxyHost and default port. | |
| 257 AddProperty("ftp.proxyHost", "httpproxy.com"); | |
| 258 ProxySettingsChanged(); | |
| 259 TestMapping("ftp://example.com/", "PROXY httpproxy.com:80"); | |
| 260 TestMapping("http://example.com/", "DIRECT"); | |
| 261 TestMapping("https://example.com/", "DIRECT"); | |
| 262 } | |
| 263 | |
| 264 TEST_F(ProxyConfigServiceAndroidTest, HttpsProxyHostAndPort) { | |
| 265 // Test https.proxyHost and https.proxyPort works. | |
| 266 AddProperty("https.proxyHost", "httpproxy.com"); | |
| 267 AddProperty("https.proxyPort", "8080"); | |
| 268 ProxySettingsChanged(); | |
| 269 TestMapping("ftp://example.com/", "DIRECT"); | |
| 270 TestMapping("http://example.com/", "DIRECT"); | |
| 271 TestMapping("https://example.com/", "HTTPS httpproxy.com:8080"); | |
| 272 } | |
| 273 | |
| 274 TEST_F(ProxyConfigServiceAndroidTest, HttpsProxyHostOnly) { | |
| 275 // Test https.proxyHost and default port. | |
| 276 AddProperty("https.proxyHost", "httpproxy.com"); | |
| 277 ProxySettingsChanged(); | |
| 278 TestMapping("ftp://example.com/", "DIRECT"); | |
| 279 TestMapping("http://example.com/", "DIRECT"); | |
| 280 TestMapping("https://example.com/", "HTTPS httpproxy.com:443"); | |
| 281 } | |
| 282 | |
| 283 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostIPv6) { | |
| 284 // Test IPv6 https.proxyHost and default port. | |
| 285 AddProperty("http.proxyHost", "a:b:c::d:1"); | |
| 286 ProxySettingsChanged(); | |
| 287 TestMapping("ftp://example.com/", "DIRECT"); | |
| 288 TestMapping("http://example.com/", "PROXY [a:b:c::d:1]:80"); | |
| 289 } | |
| 290 | |
| 291 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostAndPortIPv6) { | |
| 292 // Test IPv6 http.proxyHost and http.proxyPort works. | |
| 293 AddProperty("http.proxyHost", "a:b:c::d:1"); | |
| 294 AddProperty("http.proxyPort", "8080"); | |
| 295 ProxySettingsChanged(); | |
| 296 TestMapping("ftp://example.com/", "DIRECT"); | |
| 297 TestMapping("http://example.com/", "PROXY [a:b:c::d:1]:8080"); | |
| 298 } | |
| 299 | |
| 300 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostAndInvalidPort) { | |
| 301 // Test invalid http.proxyPort does not crash. | |
| 302 AddProperty("http.proxyHost", "a:b:c::d:1"); | |
| 303 AddProperty("http.proxyPort", "65536"); | |
| 304 ProxySettingsChanged(); | |
| 305 TestMapping("ftp://example.com/", "DIRECT"); | |
| 306 TestMapping("http://example.com/", "DIRECT"); | |
| 307 } | |
| 308 | |
| 309 TEST_F(ProxyConfigServiceAndroidTest, DefaultProxyExplictPort) { | |
| 310 // Default http proxy is used if a scheme-specific one is not found. | |
| 311 AddProperty("ftp.proxyHost", "httpproxy.com"); | |
| 312 AddProperty("ftp.proxyPort", "8080"); | |
| 313 AddProperty("proxyHost", "defaultproxy.com"); | |
| 314 AddProperty("proxyPort", "8080"); | |
| 315 ProxySettingsChanged(); | |
| 316 TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080"); | |
| 317 TestMapping("http://example.com/", "PROXY defaultproxy.com:8080"); | |
| 318 TestMapping("https://example.com/", "HTTPS defaultproxy.com:8080"); | |
| 319 } | |
| 320 | |
| 321 TEST_F(ProxyConfigServiceAndroidTest, DefaultProxyDefaultPort) { | |
| 322 // Check that the default proxy port is as expected. | |
| 323 AddProperty("proxyHost", "defaultproxy.com"); | |
| 324 ProxySettingsChanged(); | |
| 325 TestMapping("http://example.com/", "PROXY defaultproxy.com:80"); | |
| 326 TestMapping("https://example.com/", "HTTPS defaultproxy.com:443"); | |
| 327 } | |
| 328 | |
| 329 TEST_F(ProxyConfigServiceAndroidTest, FallbackToSocks) { | |
| 330 // SOCKS proxy is used if scheme-specific one is not found. | |
| 331 AddProperty("http.proxyHost", "defaultproxy.com"); | |
| 332 AddProperty("socksProxyHost", "socksproxy.com"); | |
| 333 ProxySettingsChanged(); | |
| 334 TestMapping("ftp://example.com", "SOCKS5 socksproxy.com:1080"); | |
| 335 TestMapping("http://example.com/", "PROXY defaultproxy.com:80"); | |
| 336 TestMapping("https://example.com/", "SOCKS5 socksproxy.com:1080"); | |
| 337 } | |
| 338 | |
| 339 TEST_F(ProxyConfigServiceAndroidTest, SocksExplicitPort) { | |
| 340 // SOCKS proxy port is used if specified | |
| 341 AddProperty("socksProxyHost", "socksproxy.com"); | |
| 342 AddProperty("socksProxyPort", "9000"); | |
| 343 ProxySettingsChanged(); | |
| 344 TestMapping("http://example.com/", "SOCKS5 socksproxy.com:9000"); | |
| 345 } | |
| 346 | |
| 347 TEST_F(ProxyConfigServiceAndroidTest, HttpProxySupercedesSocks) { | |
| 348 // SOCKS proxy is ignored if default HTTP proxy defined. | |
| 349 AddProperty("proxyHost", "defaultproxy.com"); | |
| 350 AddProperty("socksProxyHost", "socksproxy.com"); | |
| 351 AddProperty("socksProxyPort", "9000"); | |
| 352 ProxySettingsChanged(); | |
| 353 TestMapping("http://example.com/", "PROXY defaultproxy.com:80"); | |
| 354 } | |
| 355 | |
| 356 } // namespace net | |
| OLD | NEW |