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 "net/proxy_resolution/proxy_config_service_android.h"
6 
7 #include <map>
8 #include <memory>
9 #include <string>
10 
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/compiler_specific.h"
14 #include "base/run_loop.h"
15 #include "base/threading/thread_task_runner_handle.h"
16 #include "net/net_test_jni_headers/AndroidProxyConfigServiceTestUtil_jni.h"
17 #include "net/proxy_resolution/proxy_config_with_annotation.h"
18 #include "net/proxy_resolution/proxy_info.h"
19 #include "net/test/test_with_task_environment.h"
20 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 
23 namespace net {
24 
25 namespace {
26 
27 class TestObserver : public ProxyConfigService::Observer {
28  public:
TestObserver()29   TestObserver() : availability_(ProxyConfigService::CONFIG_UNSET) {}
30 
31   // ProxyConfigService::Observer:
OnProxyConfigChanged(const ProxyConfigWithAnnotation & config,ProxyConfigService::ConfigAvailability availability)32   void OnProxyConfigChanged(
33       const ProxyConfigWithAnnotation& config,
34       ProxyConfigService::ConfigAvailability availability) override {
35     config_ = config;
36     availability_ = availability;
37   }
38 
availability() const39   ProxyConfigService::ConfigAvailability availability() const {
40     return availability_;
41   }
42 
config() const43   const ProxyConfigWithAnnotation& config() const { return config_; }
44 
45  private:
46   ProxyConfigWithAnnotation config_;
47   ProxyConfigService::ConfigAvailability availability_;
48 };
49 
50 // Helper class that simply prepares Java's Looper on construction.
51 class JavaLooperPreparer {
52  public:
JavaLooperPreparer()53   JavaLooperPreparer() {
54     Java_AndroidProxyConfigServiceTestUtil_prepareLooper(
55         base::android::AttachCurrentThread());
56   }
57 };
58 
59 }  // namespace
60 
61 typedef std::map<std::string, std::string> StringMap;
62 
63 class ProxyConfigServiceAndroidTestBase : public TestWithTaskEnvironment {
64  protected:
65   // Note that the current thread's message loop is initialized by the test
66   // suite (see net/test/net_test_suite.cc).
ProxyConfigServiceAndroidTestBase(const StringMap & initial_configuration)67   ProxyConfigServiceAndroidTestBase(const StringMap& initial_configuration)
68       : configuration_(initial_configuration),
69         service_(
70             base::ThreadTaskRunnerHandle::Get(),
71             base::ThreadTaskRunnerHandle::Get(),
72             base::BindRepeating(&ProxyConfigServiceAndroidTestBase::GetProperty,
73                                 base::Unretained(this))) {}
74 
~ProxyConfigServiceAndroidTestBase()75   ~ProxyConfigServiceAndroidTestBase() override {}
76 
77   // testing::Test:
SetUp()78   void SetUp() override {
79     base::RunLoop().RunUntilIdle();
80     service_.AddObserver(&observer_);
81   }
82 
TearDown()83   void TearDown() override { service_.RemoveObserver(&observer_); }
84 
ClearConfiguration()85   void ClearConfiguration() {
86     configuration_.clear();
87   }
88 
AddProperty(const std::string & key,const std::string & value)89   void AddProperty(const std::string& key, const std::string& value) {
90     configuration_[key] = value;
91   }
92 
GetProperty(const std::string & key)93   std::string GetProperty(const std::string& key) {
94     StringMap::const_iterator it = configuration_.find(key);
95     if (it == configuration_.end())
96       return std::string();
97     return it->second;
98   }
99 
ProxySettingsChangedTo(const std::string & host,int port,const std::string & pac_url,const std::vector<std::string> & exclusion_list)100   void ProxySettingsChangedTo(const std::string& host,
101                               int port,
102                               const std::string& pac_url,
103                               const std::vector<std::string>& exclusion_list) {
104     service_.ProxySettingsChangedTo(host, port, pac_url, exclusion_list);
105     base::RunLoop().RunUntilIdle();
106   }
107 
ProxySettingsChanged()108   void ProxySettingsChanged() {
109     service_.ProxySettingsChanged();
110     base::RunLoop().RunUntilIdle();
111   }
112 
TestMapping(const std::string & url,const std::string & expected)113   void TestMapping(const std::string& url, const std::string& expected) {
114     ProxyConfigService::ConfigAvailability availability;
115     ProxyConfigWithAnnotation proxy_config;
116     availability = service_.GetLatestProxyConfig(&proxy_config);
117     EXPECT_EQ(ProxyConfigService::CONFIG_VALID, availability);
118     ProxyInfo proxy_info;
119     proxy_config.value().proxy_rules().Apply(GURL(url), &proxy_info);
120     EXPECT_EQ(expected, proxy_info.ToPacString());
121   }
122 
SetProxyOverride(const ProxyConfigServiceAndroid::ProxyOverrideRule & rule,const std::vector<std::string> & bypass_rules,base::OnceClosure callback)123   void SetProxyOverride(
124       const ProxyConfigServiceAndroid::ProxyOverrideRule& rule,
125       const std::vector<std::string>& bypass_rules,
126       base::OnceClosure callback) {
127     std::vector<ProxyConfigServiceAndroid::ProxyOverrideRule> rules;
128     rules.push_back(rule);
129     SetProxyOverride(rules, bypass_rules, std::move(callback));
130   }
131 
SetProxyOverride(const std::vector<ProxyConfigServiceAndroid::ProxyOverrideRule> & rules,const std::vector<std::string> & bypass_rules,base::OnceClosure callback)132   void SetProxyOverride(
133       const std::vector<ProxyConfigServiceAndroid::ProxyOverrideRule>& rules,
134       const std::vector<std::string>& bypass_rules,
135       base::OnceClosure callback) {
136     service_.SetProxyOverride(rules, bypass_rules, std::move(callback));
137     base::RunLoop().RunUntilIdle();
138   }
139 
ClearProxyOverride(base::OnceClosure callback)140   void ClearProxyOverride(base::OnceClosure callback) {
141     service_.ClearProxyOverride(std::move(callback));
142     base::RunLoop().RunUntilIdle();
143   }
144 
145   StringMap configuration_;
146   TestObserver observer_;
147   // |java_looper_preparer_| appears before |service_| so that Java's Looper is
148   // prepared before constructing |service_| as it creates a ProxyChangeListener
149   // which requires a Looper.
150   JavaLooperPreparer java_looper_preparer_;
151   ProxyConfigServiceAndroid service_;
152 };
153 
154 class ProxyConfigServiceAndroidTest : public ProxyConfigServiceAndroidTestBase {
155  public:
ProxyConfigServiceAndroidTest()156   ProxyConfigServiceAndroidTest()
157       : ProxyConfigServiceAndroidTestBase(StringMap()) {}
158 };
159 
160 class ProxyConfigServiceAndroidWithInitialConfigTest
161     : public ProxyConfigServiceAndroidTestBase {
162  public:
ProxyConfigServiceAndroidWithInitialConfigTest()163   ProxyConfigServiceAndroidWithInitialConfigTest()
164       : ProxyConfigServiceAndroidTestBase(MakeInitialConfiguration()) {}
165 
166  private:
MakeInitialConfiguration()167   StringMap MakeInitialConfiguration() {
168     StringMap initial_configuration;
169     initial_configuration["http.proxyHost"] = "httpproxy.com";
170     initial_configuration["http.proxyPort"] = "8080";
171     return initial_configuration;
172   }
173 };
174 
TEST_F(ProxyConfigServiceAndroidTest,TestChangePropertiesNotification)175 TEST_F(ProxyConfigServiceAndroidTest, TestChangePropertiesNotification) {
176   // Set up a non-empty configuration
177   AddProperty("http.proxyHost", "localhost");
178   ProxySettingsChanged();
179   EXPECT_EQ(ProxyConfigService::CONFIG_VALID, observer_.availability());
180   EXPECT_FALSE(observer_.config().value().proxy_rules().empty());
181 
182   // Set up an empty configuration
183   ClearConfiguration();
184   ProxySettingsChanged();
185   EXPECT_EQ(ProxyConfigService::CONFIG_VALID, observer_.availability());
186   EXPECT_TRUE(observer_.config().value().proxy_rules().empty());
187 }
188 
TEST_F(ProxyConfigServiceAndroidWithInitialConfigTest,TestInitialConfig)189 TEST_F(ProxyConfigServiceAndroidWithInitialConfigTest, TestInitialConfig) {
190   // Make sure that the initial config is set.
191   TestMapping("ftp://example.com/", "DIRECT");
192   TestMapping("http://example.com/", "PROXY httpproxy.com:8080");
193 
194   // Override the initial configuration.
195   ClearConfiguration();
196   AddProperty("http.proxyHost", "httpproxy.com");
197   ProxySettingsChanged();
198   TestMapping("http://example.com/", "PROXY httpproxy.com:80");
199 }
200 
TEST_F(ProxyConfigServiceAndroidTest,TestClearProxy)201 TEST_F(ProxyConfigServiceAndroidTest, TestClearProxy) {
202   AddProperty("http.proxyHost", "httpproxy.com");
203   ProxySettingsChanged();
204   TestMapping("http://example.com/", "PROXY httpproxy.com:80");
205 
206   // These values are used in ProxyChangeListener.java to indicate a direct
207   // proxy connection.
208   ProxySettingsChangedTo("", 0, "", {});
209   TestMapping("http://example.com/", "DIRECT");
210 }
211 
212 struct ProxyCallback {
ProxyCallbacknet::ProxyCallback213   ProxyCallback()
214       : called(false),
215         callback(base::BindOnce(&ProxyCallback::Call, base::Unretained(this))) {
216   }
217 
Callnet::ProxyCallback218   void Call() { called = true; }
219 
220   bool called;
221   base::OnceClosure callback;
222 };
223 
TEST_F(ProxyConfigServiceAndroidTest,TestProxyOverrideCallback)224 TEST_F(ProxyConfigServiceAndroidTest, TestProxyOverrideCallback) {
225   ProxyCallback proxyCallback;
226   ASSERT_FALSE(proxyCallback.called);
227   ClearProxyOverride(std::move(proxyCallback.callback));
228   base::RunLoop().RunUntilIdle();
229   EXPECT_TRUE(proxyCallback.called);
230 }
231 
TEST_F(ProxyConfigServiceAndroidTest,TestProxyOverrideSchemes)232 TEST_F(ProxyConfigServiceAndroidTest, TestProxyOverrideSchemes) {
233   std::vector<std::string> bypass_rules;
234 
235   // Check that webview uses the default proxy
236   TestMapping("http://example.com/", "DIRECT");
237   TestMapping("https://example.com/", "DIRECT");
238   TestMapping("ftp://example.com/", "DIRECT");
239 
240   SetProxyOverride({"*", "httpoverrideproxy.com:200"}, bypass_rules,
241                    base::DoNothing());
242   TestMapping("http://example.com/", "PROXY httpoverrideproxy.com:200");
243   TestMapping("https://example.com/", "PROXY httpoverrideproxy.com:200");
244   TestMapping("ftp://example.com/", "PROXY httpoverrideproxy.com:200");
245 
246   // Check that webview uses the custom proxy only for https
247   SetProxyOverride({"https", "httpoverrideproxy.com:200"}, bypass_rules,
248                    base::DoNothing());
249   TestMapping("http://example.com/", "DIRECT");
250   TestMapping("https://example.com/", "PROXY httpoverrideproxy.com:200");
251   TestMapping("ftp://example.com/", "DIRECT");
252 
253   // Check that webview uses the default proxy
254   ClearProxyOverride(base::DoNothing());
255   TestMapping("http://example.com/", "DIRECT");
256   TestMapping("https://example.com/", "DIRECT");
257   TestMapping("ftp://example.com/", "DIRECT");
258 }
259 
TEST_F(ProxyConfigServiceAndroidTest,TestProxyOverridePorts)260 TEST_F(ProxyConfigServiceAndroidTest, TestProxyOverridePorts) {
261   std::vector<std::string> bypass_rules;
262 
263   // Check that webview uses the default proxy
264   TestMapping("http://example.com/", "DIRECT");
265   TestMapping("https://example.com/", "DIRECT");
266   TestMapping("ftp://example.com/", "DIRECT");
267 
268   // Check that webview uses port 80 for http proxies
269   SetProxyOverride({"*", "httpoverrideproxy.com"}, bypass_rules,
270                    base::DoNothing());
271   TestMapping("http://example.com:444", "PROXY httpoverrideproxy.com:80");
272   TestMapping("https://example.com:2222", "PROXY httpoverrideproxy.com:80");
273   TestMapping("ftp://example.com:15", "PROXY httpoverrideproxy.com:80");
274 
275   // Check that webview uses port 443 for https proxies
276   SetProxyOverride({"*", "https://httpoverrideproxy.com"}, bypass_rules,
277                    base::DoNothing());
278   TestMapping("http://example.com:8080", "HTTPS httpoverrideproxy.com:443");
279   TestMapping("https://example.com:1111", "HTTPS httpoverrideproxy.com:443");
280   TestMapping("ftp://example.com:752", "HTTPS httpoverrideproxy.com:443");
281 
282   // Check that webview uses custom port
283   SetProxyOverride({"*", "https://httpoverrideproxy.com:777"}, bypass_rules,
284                    base::DoNothing());
285   TestMapping("http://example.com:8080", "HTTPS httpoverrideproxy.com:777");
286   TestMapping("https://example.com:1111", "HTTPS httpoverrideproxy.com:777");
287   TestMapping("ftp://example.com:752", "HTTPS httpoverrideproxy.com:777");
288 
289   ClearProxyOverride(base::DoNothing());
290 }
291 
TEST_F(ProxyConfigServiceAndroidTest,TestProxyOverrideMultipleRules)292 TEST_F(ProxyConfigServiceAndroidTest, TestProxyOverrideMultipleRules) {
293   std::vector<std::string> bypass_rules;
294 
295   // Multiple rules with schemes are valid
296   std::vector<ProxyConfigServiceAndroid::ProxyOverrideRule> rules;
297   rules.push_back({"http", "httpoverrideproxy.com"});
298   rules.push_back({"https", "https://httpoverrideproxy.com"});
299   SetProxyOverride(rules, bypass_rules, base::DoNothing());
300   TestMapping("https://example.com/", "HTTPS httpoverrideproxy.com:443");
301   TestMapping("http://example.com/", "PROXY httpoverrideproxy.com:80");
302 
303   // Rules with and without scheme can be combined
304   rules.clear();
305   rules.push_back({"http", "overrideproxy1.com"});
306   rules.push_back({"*", "overrideproxy2.com"});
307   SetProxyOverride(rules, bypass_rules, base::DoNothing());
308   TestMapping("https://example.com/", "PROXY overrideproxy2.com:80");
309   TestMapping("http://example.com/", "PROXY overrideproxy1.com:80");
310 
311   ClearProxyOverride(base::DoNothing());
312 }
313 
TEST_F(ProxyConfigServiceAndroidTest,TestProxyOverrideListOfRules)314 TEST_F(ProxyConfigServiceAndroidTest, TestProxyOverrideListOfRules) {
315   std::vector<std::string> bypass_rules;
316 
317   std::vector<ProxyConfigServiceAndroid::ProxyOverrideRule> rules;
318   rules.push_back({"http", "httpproxy1"});
319   rules.push_back({"*", "socks5://fallback1"});
320   rules.push_back({"http", "httpproxy2"});
321   rules.push_back({"*", "fallback2"});
322   rules.push_back({"*", "direct://"});
323   SetProxyOverride(rules, bypass_rules, base::DoNothing());
324 
325   TestMapping("http://example.com", "PROXY httpproxy1:80;PROXY httpproxy2:80");
326   TestMapping("https://example.com",
327               "SOCKS5 fallback1:1080;PROXY fallback2:80;DIRECT");
328 }
329 
TEST_F(ProxyConfigServiceAndroidTest,TestOverrideAndProxy)330 TEST_F(ProxyConfigServiceAndroidTest, TestOverrideAndProxy) {
331   std::vector<std::string> bypass_rules;
332   bypass_rules.push_back("www.excluded.com");
333 
334   // Check that webview uses the default proxy
335   TestMapping("http://example.com/", "DIRECT");
336 
337   // Check that webview uses the custom proxy
338   SetProxyOverride({"*", "httpoverrideproxy.com:200"}, bypass_rules,
339                    base::DoNothing());
340   TestMapping("http://example.com/", "PROXY httpoverrideproxy.com:200");
341 
342   // Check that webview continues to use the custom proxy
343   AddProperty("http.proxyHost", "httpsomeproxy.com");
344   ProxySettingsChanged();
345   TestMapping("http://example.com/", "PROXY httpoverrideproxy.com:200");
346   TestMapping("http://www.excluded.com/", "DIRECT");
347 
348   // Check that webview uses the non default proxy
349   ClearProxyOverride(base::DoNothing());
350   TestMapping("http://example.com/", "PROXY httpsomeproxy.com:80");
351 }
352 
TEST_F(ProxyConfigServiceAndroidTest,TestProxyAndOverride)353 TEST_F(ProxyConfigServiceAndroidTest, TestProxyAndOverride) {
354   std::vector<std::string> bypass_rules;
355 
356   // Check that webview uses the default proxy
357   TestMapping("http://example.com/", "DIRECT");
358 
359   // Check that webview uses the non default proxy
360   AddProperty("http.proxyHost", "httpsomeproxy.com");
361   ProxySettingsChanged();
362   TestMapping("http://example.com/", "PROXY httpsomeproxy.com:80");
363 
364   // Check that webview uses the custom proxy
365   SetProxyOverride({"*", "httpoverrideproxy.com:200"}, bypass_rules,
366                    base::DoNothing());
367   TestMapping("http://example.com/", "PROXY httpoverrideproxy.com:200");
368 
369   // Check that webview uses the non default proxy
370   ClearProxyOverride(base::DoNothing());
371   TestMapping("http://example.com/", "PROXY httpsomeproxy.com:80");
372 }
373 
TEST_F(ProxyConfigServiceAndroidTest,TestOverrideThenProxy)374 TEST_F(ProxyConfigServiceAndroidTest, TestOverrideThenProxy) {
375   std::vector<std::string> bypass_rules;
376 
377   // Check that webview uses the default proxy
378   TestMapping("http://example.com/", "DIRECT");
379 
380   // Check that webview uses the custom proxy
381   SetProxyOverride({"*", "httpoverrideproxy.com:200"}, bypass_rules,
382                    base::DoNothing());
383   TestMapping("http://example.com/", "PROXY httpoverrideproxy.com:200");
384 
385   // Check that webview uses the default proxy
386   ClearProxyOverride(base::DoNothing());
387   TestMapping("http://example.com/", "DIRECT");
388 
389   // Check that webview uses the non default proxy
390   AddProperty("http.proxyHost", "httpsomeproxy.com");
391   ProxySettingsChanged();
392   TestMapping("http://example.com/", "PROXY httpsomeproxy.com:80");
393 }
394 
TEST_F(ProxyConfigServiceAndroidTest,TestClearOverride)395 TEST_F(ProxyConfigServiceAndroidTest, TestClearOverride) {
396   std::vector<std::string> bypass_rules;
397 
398   // Check that webview uses the default proxy
399   TestMapping("http://example.com/", "DIRECT");
400 
401   // Check that webview uses the default proxy
402   ClearProxyOverride(base::DoNothing());
403   TestMapping("http://example.com/", "DIRECT");
404 }
405 
TEST_F(ProxyConfigServiceAndroidTest,TestProxyAndClearOverride)406 TEST_F(ProxyConfigServiceAndroidTest, TestProxyAndClearOverride) {
407   std::vector<std::string> bypass_rules;
408 
409   // Check that webview uses the non default proxy
410   AddProperty("http.proxyHost", "httpsomeproxy.com");
411   ProxySettingsChanged();
412   TestMapping("http://example.com/", "PROXY httpsomeproxy.com:80");
413 
414   // Check that webview uses the non default proxy
415   ClearProxyOverride(base::DoNothing());
416   TestMapping("http://example.com/", "PROXY httpsomeproxy.com:80");
417 }
418 
TEST_F(ProxyConfigServiceAndroidTest,TestOverrideBypassRules)419 TEST_F(ProxyConfigServiceAndroidTest, TestOverrideBypassRules) {
420   std::vector<std::string> bypass_rules;
421   bypass_rules.push_back("excluded.com");
422 
423   // Check that webview uses the default proxy
424   TestMapping("http://excluded.com/", "DIRECT");
425   TestMapping("http://example.com/", "DIRECT");
426 
427   // Check that webview handles the bypass rules correctly
428   SetProxyOverride({"*", "httpoverrideproxy.com:200"}, bypass_rules,
429                    base::DoNothing());
430   TestMapping("http://excluded.com/", "DIRECT");
431   TestMapping("http://example.com/", "PROXY httpoverrideproxy.com:200");
432 
433   // Check that webview uses the default proxy
434   ClearProxyOverride(base::DoNothing());
435   TestMapping("http://excluded.com/", "DIRECT");
436   TestMapping("http://example.com/", "DIRECT");
437 }
438 
TEST_F(ProxyConfigServiceAndroidTest,TestOverrideToDirect)439 TEST_F(ProxyConfigServiceAndroidTest, TestOverrideToDirect) {
440   std::vector<std::string> bypass_rules;
441 
442   // Check that webview uses the non default proxy
443   AddProperty("http.proxyHost", "httpsomeproxy.com");
444   ProxySettingsChanged();
445   TestMapping("http://example.com/", "PROXY httpsomeproxy.com:80");
446 
447   // Check that webview uses no proxy
448   TestMapping("http://example.com/", "PROXY httpsomeproxy.com:80");
449   SetProxyOverride({"*", "direct://"}, bypass_rules, base::DoNothing());
450   TestMapping("http://example.com/", "DIRECT");
451 
452   ClearProxyOverride(base::DoNothing());
453 }
454 
455 // !! The following test cases are automatically generated from
456 // !! net/android/tools/proxy_test_cases.py.
457 // !! Please edit that file instead of editing the test cases below and
458 // !! update also the corresponding Java unit tests in
459 // !! AndroidProxySelectorTest.java
460 
TEST_F(ProxyConfigServiceAndroidTest,NoProxy)461 TEST_F(ProxyConfigServiceAndroidTest, NoProxy) {
462   // Test direct mapping when no proxy defined.
463   ProxySettingsChanged();
464   TestMapping("ftp://example.com/", "DIRECT");
465   TestMapping("http://example.com/", "DIRECT");
466   TestMapping("https://example.com/", "DIRECT");
467 }
468 
TEST_F(ProxyConfigServiceAndroidTest,HttpProxyHostAndPort)469 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostAndPort) {
470   // Test http.proxyHost and http.proxyPort works.
471   AddProperty("http.proxyHost", "httpproxy.com");
472   AddProperty("http.proxyPort", "8080");
473   ProxySettingsChanged();
474   TestMapping("ftp://example.com/", "DIRECT");
475   TestMapping("http://example.com/", "PROXY httpproxy.com:8080");
476   TestMapping("https://example.com/", "DIRECT");
477 }
478 
TEST_F(ProxyConfigServiceAndroidTest,HttpProxyHostOnly)479 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostOnly) {
480   // We should get the default port (80) for proxied hosts.
481   AddProperty("http.proxyHost", "httpproxy.com");
482   ProxySettingsChanged();
483   TestMapping("ftp://example.com/", "DIRECT");
484   TestMapping("http://example.com/", "PROXY httpproxy.com:80");
485   TestMapping("https://example.com/", "DIRECT");
486 }
487 
TEST_F(ProxyConfigServiceAndroidTest,HttpProxyPortOnly)488 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyPortOnly) {
489   // http.proxyPort only should not result in any hosts being proxied.
490   AddProperty("http.proxyPort", "8080");
491   ProxySettingsChanged();
492   TestMapping("ftp://example.com/", "DIRECT");
493   TestMapping("http://example.com/", "DIRECT");
494   TestMapping("https://example.com/", "DIRECT");
495 }
496 
TEST_F(ProxyConfigServiceAndroidTest,HttpNonProxyHosts1)497 TEST_F(ProxyConfigServiceAndroidTest, HttpNonProxyHosts1) {
498   // Test that HTTP non proxy hosts are mapped correctly
499   AddProperty("http.nonProxyHosts", "slashdot.org");
500   AddProperty("http.proxyHost", "httpproxy.com");
501   AddProperty("http.proxyPort", "8080");
502   ProxySettingsChanged();
503   TestMapping("http://example.com/", "PROXY httpproxy.com:8080");
504   TestMapping("http://slashdot.org/", "DIRECT");
505 }
506 
TEST_F(ProxyConfigServiceAndroidTest,HttpNonProxyHosts2)507 TEST_F(ProxyConfigServiceAndroidTest, HttpNonProxyHosts2) {
508   // Test that | pattern works.
509   AddProperty("http.nonProxyHosts", "slashdot.org|freecode.net");
510   AddProperty("http.proxyHost", "httpproxy.com");
511   AddProperty("http.proxyPort", "8080");
512   ProxySettingsChanged();
513   TestMapping("http://example.com/", "PROXY httpproxy.com:8080");
514   TestMapping("http://freecode.net/", "DIRECT");
515   TestMapping("http://slashdot.org/", "DIRECT");
516 }
517 
TEST_F(ProxyConfigServiceAndroidTest,HttpNonProxyHosts3)518 TEST_F(ProxyConfigServiceAndroidTest, HttpNonProxyHosts3) {
519   // Test that * pattern works.
520   AddProperty("http.nonProxyHosts", "*example.com");
521   AddProperty("http.proxyHost", "httpproxy.com");
522   AddProperty("http.proxyPort", "8080");
523   ProxySettingsChanged();
524   TestMapping("http://example.com/", "DIRECT");
525   TestMapping("http://slashdot.org/", "PROXY httpproxy.com:8080");
526   TestMapping("http://www.example.com/", "DIRECT");
527 }
528 
TEST_F(ProxyConfigServiceAndroidTest,FtpNonProxyHosts)529 TEST_F(ProxyConfigServiceAndroidTest, FtpNonProxyHosts) {
530   // Test that FTP non proxy hosts are mapped correctly
531   AddProperty("ftp.nonProxyHosts", "slashdot.org");
532   AddProperty("ftp.proxyHost", "httpproxy.com");
533   AddProperty("ftp.proxyPort", "8080");
534   ProxySettingsChanged();
535   TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080");
536   TestMapping("http://example.com/", "DIRECT");
537 }
538 
TEST_F(ProxyConfigServiceAndroidTest,FtpProxyHostAndPort)539 TEST_F(ProxyConfigServiceAndroidTest, FtpProxyHostAndPort) {
540   // Test ftp.proxyHost and ftp.proxyPort works.
541   AddProperty("ftp.proxyHost", "httpproxy.com");
542   AddProperty("ftp.proxyPort", "8080");
543   ProxySettingsChanged();
544   TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080");
545   TestMapping("http://example.com/", "DIRECT");
546   TestMapping("https://example.com/", "DIRECT");
547 }
548 
TEST_F(ProxyConfigServiceAndroidTest,FtpProxyHostOnly)549 TEST_F(ProxyConfigServiceAndroidTest, FtpProxyHostOnly) {
550   // Test ftp.proxyHost and default port.
551   AddProperty("ftp.proxyHost", "httpproxy.com");
552   ProxySettingsChanged();
553   TestMapping("ftp://example.com/", "PROXY httpproxy.com:80");
554   TestMapping("http://example.com/", "DIRECT");
555   TestMapping("https://example.com/", "DIRECT");
556 }
557 
TEST_F(ProxyConfigServiceAndroidTest,HttpsProxyHostAndPort)558 TEST_F(ProxyConfigServiceAndroidTest, HttpsProxyHostAndPort) {
559   // Test https.proxyHost and https.proxyPort works.
560   AddProperty("https.proxyHost", "httpproxy.com");
561   AddProperty("https.proxyPort", "8080");
562   ProxySettingsChanged();
563   TestMapping("ftp://example.com/", "DIRECT");
564   TestMapping("http://example.com/", "DIRECT");
565   TestMapping("https://example.com/", "PROXY httpproxy.com:8080");
566 }
567 
TEST_F(ProxyConfigServiceAndroidTest,HttpsProxyHostOnly)568 TEST_F(ProxyConfigServiceAndroidTest, HttpsProxyHostOnly) {
569   // Test https.proxyHost and default port.
570   AddProperty("https.proxyHost", "httpproxy.com");
571   ProxySettingsChanged();
572   TestMapping("ftp://example.com/", "DIRECT");
573   TestMapping("http://example.com/", "DIRECT");
574   TestMapping("https://example.com/", "PROXY httpproxy.com:80");
575 }
576 
TEST_F(ProxyConfigServiceAndroidTest,HttpProxyHostIPv6)577 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostIPv6) {
578   // Test IPv6 https.proxyHost and default port.
579   AddProperty("http.proxyHost", "a:b:c::d:1");
580   ProxySettingsChanged();
581   TestMapping("ftp://example.com/", "DIRECT");
582   TestMapping("http://example.com/", "PROXY [a:b:c::d:1]:80");
583 }
584 
TEST_F(ProxyConfigServiceAndroidTest,HttpProxyHostAndPortIPv6)585 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostAndPortIPv6) {
586   // Test IPv6 http.proxyHost and http.proxyPort works.
587   AddProperty("http.proxyHost", "a:b:c::d:1");
588   AddProperty("http.proxyPort", "8080");
589   ProxySettingsChanged();
590   TestMapping("ftp://example.com/", "DIRECT");
591   TestMapping("http://example.com/", "PROXY [a:b:c::d:1]:8080");
592 }
593 
TEST_F(ProxyConfigServiceAndroidTest,HttpProxyHostAndInvalidPort)594 TEST_F(ProxyConfigServiceAndroidTest, HttpProxyHostAndInvalidPort) {
595   // Test invalid http.proxyPort does not crash.
596   AddProperty("http.proxyHost", "a:b:c::d:1");
597   AddProperty("http.proxyPort", "65536");
598   ProxySettingsChanged();
599   TestMapping("ftp://example.com/", "DIRECT");
600   TestMapping("http://example.com/", "DIRECT");
601 }
602 
TEST_F(ProxyConfigServiceAndroidTest,DefaultProxyExplictPort)603 TEST_F(ProxyConfigServiceAndroidTest, DefaultProxyExplictPort) {
604   // Default http proxy is used if a scheme-specific one is not found.
605   AddProperty("ftp.proxyHost", "httpproxy.com");
606   AddProperty("ftp.proxyPort", "8080");
607   AddProperty("proxyHost", "defaultproxy.com");
608   AddProperty("proxyPort", "8080");
609   ProxySettingsChanged();
610   TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080");
611   TestMapping("http://example.com/", "PROXY defaultproxy.com:8080");
612   TestMapping("https://example.com/", "PROXY defaultproxy.com:8080");
613 }
614 
TEST_F(ProxyConfigServiceAndroidTest,DefaultProxyDefaultPort)615 TEST_F(ProxyConfigServiceAndroidTest, DefaultProxyDefaultPort) {
616   // Check that the default proxy port is as expected.
617   AddProperty("proxyHost", "defaultproxy.com");
618   ProxySettingsChanged();
619   TestMapping("http://example.com/", "PROXY defaultproxy.com:80");
620   TestMapping("https://example.com/", "PROXY defaultproxy.com:80");
621 }
622 
TEST_F(ProxyConfigServiceAndroidTest,FallbackToSocks)623 TEST_F(ProxyConfigServiceAndroidTest, FallbackToSocks) {
624   // SOCKS proxy is used if scheme-specific one is not found.
625   AddProperty("http.proxyHost", "defaultproxy.com");
626   AddProperty("socksProxyHost", "socksproxy.com");
627   ProxySettingsChanged();
628   TestMapping("ftp://example.com", "SOCKS5 socksproxy.com:1080");
629   TestMapping("http://example.com/", "PROXY defaultproxy.com:80");
630   TestMapping("https://example.com/", "SOCKS5 socksproxy.com:1080");
631 }
632 
TEST_F(ProxyConfigServiceAndroidTest,SocksExplicitPort)633 TEST_F(ProxyConfigServiceAndroidTest, SocksExplicitPort) {
634   // SOCKS proxy port is used if specified
635   AddProperty("socksProxyHost", "socksproxy.com");
636   AddProperty("socksProxyPort", "9000");
637   ProxySettingsChanged();
638   TestMapping("http://example.com/", "SOCKS5 socksproxy.com:9000");
639 }
640 
TEST_F(ProxyConfigServiceAndroidTest,HttpProxySupercedesSocks)641 TEST_F(ProxyConfigServiceAndroidTest, HttpProxySupercedesSocks) {
642   // SOCKS proxy is ignored if default HTTP proxy defined.
643   AddProperty("proxyHost", "defaultproxy.com");
644   AddProperty("socksProxyHost", "socksproxy.com");
645   AddProperty("socksProxyPort", "9000");
646   ProxySettingsChanged();
647   TestMapping("http://example.com/", "PROXY defaultproxy.com:80");
648 }
649 
650 }  // namespace net
651