Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: net/proxy/proxy_config_service_android_unittest.cc

Issue 10206014: Upstream Android proxy config service. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix two threading issues + Sync Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 ProxyConfigServiceAndroidTestBase(const StringMap& initial_configuration)
53 : configuration_(initial_configuration),
54 message_loop_(MessageLoop::current()),
55 service_(
56 message_loop_->message_loop_proxy(),
57 message_loop_->message_loop_proxy(),
58 base::Bind(&ProxyConfigServiceAndroidTestBase::GetProperty,
59 base::Unretained(this))) {}
60
61 virtual ~ProxyConfigServiceAndroidTestBase() {}
62
63 // testing::Test:
64 virtual void SetUp() OVERRIDE {
65 message_loop_->RunAllPending();
66 service_.AddObserver(&observer_);
67 }
68
69 virtual void TearDown() OVERRIDE {
70 service_.RemoveObserver(&observer_);
71 }
72
73 void ClearConfiguration() {
74 configuration_.clear();
75 }
76
77 void AddProperty(const std::string& key, const std::string& value) {
78 configuration_[key] = value;
79 }
80
81 std::string GetProperty(const std::string& key) {
82 StringMap::const_iterator it = configuration_.find(key);
83 if (it == configuration_.end())
84 return std::string();
85 return it->second;
86 }
87
88 void ProxySettingsChanged() {
89 service_.ProxySettingsChanged();
90 message_loop_->RunAllPending();
91 }
92
93 void TestMapping(const std::string& url, const std::string& expected) {
94 ProxyConfigService::ConfigAvailability availability;
95 ProxyConfig proxy_config;
96 availability = service_.GetLatestProxyConfig(&proxy_config);
97 EXPECT_EQ(ProxyConfigService::CONFIG_VALID, availability);
98 ProxyInfo proxy_info;
99 proxy_config.proxy_rules().Apply(GURL(url), &proxy_info);
100 EXPECT_EQ(expected, proxy_info.ToPacString());
101 }
102
103 StringMap configuration_;
104 TestObserver observer_;
105 MessageLoop* const message_loop_;
106 ProxyConfigServiceAndroid service_;
107 };
108
109 class ProxyConfigServiceAndroidTest : public ProxyConfigServiceAndroidTestBase {
110 public:
111 ProxyConfigServiceAndroidTest()
112 : ProxyConfigServiceAndroidTestBase(StringMap()) {}
113 };
114
115 class ProxyConfigServiceAndroidWithInitialConfigTest
116 : public ProxyConfigServiceAndroidTestBase {
117 public:
118 ProxyConfigServiceAndroidWithInitialConfigTest()
119 : ProxyConfigServiceAndroidTestBase(MakeInitialConfiguration()) {}
120
121 private:
122 StringMap MakeInitialConfiguration() {
123 StringMap initial_configuration;
124 initial_configuration["http.proxyHost"] = "httpproxy.com";
125 initial_configuration["http.proxyPort"] = "8080";
126 return initial_configuration;
127 }
128 };
129
130 TEST_F(ProxyConfigServiceAndroidTest, TestChangePropertiesNotification) {
131 // Set up a non-empty configuration
132 AddProperty("http.proxyHost", "localhost");
133 ProxySettingsChanged();
134 EXPECT_EQ(ProxyConfigService::CONFIG_VALID, observer_.availability());
135 EXPECT_FALSE(observer_.config().proxy_rules().empty());
136
137 // Set up an empty configuration
138 ClearConfiguration();
139 ProxySettingsChanged();
140 EXPECT_EQ(ProxyConfigService::CONFIG_VALID, observer_.availability());
141 EXPECT_TRUE(observer_.config().proxy_rules().empty());
142 }
143
144 TEST_F(ProxyConfigServiceAndroidWithInitialConfigTest, TestInitialConfig) {
145 // Make sure that the initial config is set.
146 TestMapping("ftp://example.com/", "DIRECT");
147 TestMapping("http://example.com/", "PROXY httpproxy.com:8080");
148
149 // Override the initial configuration.
150 ClearConfiguration();
151 AddProperty("http.proxyHost", "httpproxy.com");
152 ProxySettingsChanged();
153 TestMapping("http://example.com/", "PROXY httpproxy.com:80");
154 }
155
156 // !! The following test cases are automatically generated from
157 // !! net/android/tools/proxy_test_cases.py.
158 // !! Please edit that file instead of editing the test cases below and
159 // !! update also the corresponding Java unit tests in
160 // !! AndroidProxySelectorTest.java
161
162 TEST_F(ProxyConfigServiceAndroidTest, NoProxy) {
163 // Test direct mapping when no proxy defined.
164 ProxySettingsChanged();
165 TestMapping("ftp://example.com/", "DIRECT");
166 TestMapping("http://example.com/", "DIRECT");
167 TestMapping("https://example.com/", "DIRECT");
168 }
169
170 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostAndPort) {
171 // Test http.proxyHost and http.proxyPort works.
172 AddProperty("http.proxyHost", "httpproxy.com");
173 AddProperty("http.proxyPort", "8080");
174 ProxySettingsChanged();
175 TestMapping("ftp://example.com/", "DIRECT");
176 TestMapping("http://example.com/", "PROXY httpproxy.com:8080");
177 TestMapping("https://example.com/", "DIRECT");
178 }
179
180 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostOnly) {
181 // We should get the default port (80) for proxied hosts.
182 AddProperty("http.proxyHost", "httpproxy.com");
183 ProxySettingsChanged();
184 TestMapping("ftp://example.com/", "DIRECT");
185 TestMapping("http://example.com/", "PROXY httpproxy.com:80");
186 TestMapping("https://example.com/", "DIRECT");
187 }
188
189 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyPortOnly) {
190 // http.proxyPort only should not result in any hosts being proxied.
191 AddProperty("http.proxyPort", "8080");
192 ProxySettingsChanged();
193 TestMapping("ftp://example.com/", "DIRECT");
194 TestMapping("http://example.com/", "DIRECT");
195 TestMapping("https://example.com/", "DIRECT");
196 }
197
198 TEST_F(ProxyConfigServiceAndroidTest, HttpNonProxyHosts1) {
199 // Test that HTTP non proxy hosts are mapped correctly
200 AddProperty("http.nonProxyHosts", "slashdot.org");
201 AddProperty("http.proxyHost", "httpproxy.com");
202 AddProperty("http.proxyPort", "8080");
203 ProxySettingsChanged();
204 TestMapping("http://example.com/", "PROXY httpproxy.com:8080");
205 TestMapping("http://slashdot.org/", "DIRECT");
206 }
207
208 TEST_F(ProxyConfigServiceAndroidTest, HttpNonProxyHosts2) {
209 // Test that | pattern works.
210 AddProperty("http.nonProxyHosts", "slashdot.org|freecode.net");
211 AddProperty("http.proxyHost", "httpproxy.com");
212 AddProperty("http.proxyPort", "8080");
213 ProxySettingsChanged();
214 TestMapping("http://example.com/", "PROXY httpproxy.com:8080");
215 TestMapping("http://freecode.net/", "DIRECT");
216 TestMapping("http://slashdot.org/", "DIRECT");
217 }
218
219 TEST_F(ProxyConfigServiceAndroidTest, HttpNonProxyHosts3) {
220 // Test that * pattern works.
221 AddProperty("http.nonProxyHosts", "*example.com");
222 AddProperty("http.proxyHost", "httpproxy.com");
223 AddProperty("http.proxyPort", "8080");
224 ProxySettingsChanged();
225 TestMapping("http://example.com/", "DIRECT");
226 TestMapping("http://slashdot.org/", "PROXY httpproxy.com:8080");
227 TestMapping("http://www.example.com/", "DIRECT");
228 }
229
230 TEST_F(ProxyConfigServiceAndroidTest, FtpNonProxyHosts) {
231 // Test that FTP non proxy hosts are mapped correctly
232 AddProperty("ftp.nonProxyHosts", "slashdot.org");
233 AddProperty("ftp.proxyHost", "httpproxy.com");
234 AddProperty("ftp.proxyPort", "8080");
235 ProxySettingsChanged();
236 TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080");
237 TestMapping("http://example.com/", "DIRECT");
238 }
239
240 TEST_F(ProxyConfigServiceAndroidTest, FtpProxyHostAndPort) {
241 // Test ftp.proxyHost and ftp.proxyPort works.
242 AddProperty("ftp.proxyHost", "httpproxy.com");
243 AddProperty("ftp.proxyPort", "8080");
244 ProxySettingsChanged();
245 TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080");
246 TestMapping("http://example.com/", "DIRECT");
247 TestMapping("https://example.com/", "DIRECT");
248 }
249
250 TEST_F(ProxyConfigServiceAndroidTest, FtpProxyHostOnly) {
251 // Test ftp.proxyHost and default port.
252 AddProperty("ftp.proxyHost", "httpproxy.com");
253 ProxySettingsChanged();
254 TestMapping("ftp://example.com/", "PROXY httpproxy.com:80");
255 TestMapping("http://example.com/", "DIRECT");
256 TestMapping("https://example.com/", "DIRECT");
257 }
258
259 TEST_F(ProxyConfigServiceAndroidTest, HttpsProxyHostAndPort) {
260 // Test https.proxyHost and https.proxyPort works.
261 AddProperty("https.proxyHost", "httpproxy.com");
262 AddProperty("https.proxyPort", "8080");
263 ProxySettingsChanged();
264 TestMapping("ftp://example.com/", "DIRECT");
265 TestMapping("http://example.com/", "DIRECT");
266 TestMapping("https://example.com/", "HTTPS httpproxy.com:8080");
267 }
268
269 TEST_F(ProxyConfigServiceAndroidTest, HttpsProxyHostOnly) {
270 // Test https.proxyHost and default port.
271 AddProperty("https.proxyHost", "httpproxy.com");
272 ProxySettingsChanged();
273 TestMapping("ftp://example.com/", "DIRECT");
274 TestMapping("http://example.com/", "DIRECT");
275 TestMapping("https://example.com/", "HTTPS httpproxy.com:443");
276 }
277
278 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostIPv6) {
279 // Test IPv6 https.proxyHost and default port.
280 AddProperty("http.proxyHost", "a:b:c::d:1");
281 ProxySettingsChanged();
282 TestMapping("ftp://example.com/", "DIRECT");
283 TestMapping("http://example.com/", "PROXY [a:b:c::d:1]:80");
284 }
285
286 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostAndPortIPv6) {
287 // Test IPv6 http.proxyHost and http.proxyPort works.
288 AddProperty("http.proxyHost", "a:b:c::d:1");
289 AddProperty("http.proxyPort", "8080");
290 ProxySettingsChanged();
291 TestMapping("ftp://example.com/", "DIRECT");
292 TestMapping("http://example.com/", "PROXY [a:b:c::d:1]:8080");
293 }
294
295 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostAndInvalidPort) {
296 // Test invalid http.proxyPort does not crash.
297 AddProperty("http.proxyHost", "a:b:c::d:1");
298 AddProperty("http.proxyPort", "65536");
299 ProxySettingsChanged();
300 TestMapping("ftp://example.com/", "DIRECT");
301 TestMapping("http://example.com/", "DIRECT");
302 }
303
304 TEST_F(ProxyConfigServiceAndroidTest, DefaultProxyExplictPort) {
305 // Default http proxy is used if a scheme-specific one is not found.
306 AddProperty("ftp.proxyHost", "httpproxy.com");
307 AddProperty("ftp.proxyPort", "8080");
308 AddProperty("proxyHost", "defaultproxy.com");
309 AddProperty("proxyPort", "8080");
310 ProxySettingsChanged();
311 TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080");
312 TestMapping("http://example.com/", "PROXY defaultproxy.com:8080");
313 TestMapping("https://example.com/", "HTTPS defaultproxy.com:8080");
314 }
315
316 TEST_F(ProxyConfigServiceAndroidTest, DefaultProxyDefaultPort) {
317 // Check that the default proxy port is as expected.
318 AddProperty("proxyHost", "defaultproxy.com");
319 ProxySettingsChanged();
320 TestMapping("http://example.com/", "PROXY defaultproxy.com:80");
321 TestMapping("https://example.com/", "HTTPS defaultproxy.com:443");
322 }
323
324 TEST_F(ProxyConfigServiceAndroidTest, FallbackToSocks) {
325 // SOCKS proxy is used if scheme-specific one is not found.
326 AddProperty("http.proxyHost", "defaultproxy.com");
327 AddProperty("socksProxyHost", "socksproxy.com");
328 ProxySettingsChanged();
329 TestMapping("ftp://example.com", "SOCKS5 socksproxy.com:1080");
330 TestMapping("http://example.com/", "PROXY defaultproxy.com:80");
331 TestMapping("https://example.com/", "SOCKS5 socksproxy.com:1080");
332 }
333
334 TEST_F(ProxyConfigServiceAndroidTest, SocksExplicitPort) {
335 // SOCKS proxy port is used if specified
336 AddProperty("socksProxyHost", "socksproxy.com");
337 AddProperty("socksProxyPort", "9000");
338 ProxySettingsChanged();
339 TestMapping("http://example.com/", "SOCKS5 socksproxy.com:9000");
340 }
341
342 TEST_F(ProxyConfigServiceAndroidTest, HttpProxySupercedesSocks) {
343 // SOCKS proxy is ignored if default HTTP proxy defined.
344 AddProperty("proxyHost", "defaultproxy.com");
345 AddProperty("socksProxyHost", "socksproxy.com");
346 AddProperty("socksProxyPort", "9000");
347 ProxySettingsChanged();
348 TestMapping("http://example.com/", "PROXY defaultproxy.com:80");
349 }
350
351 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698