proxy_config_service_android_unittest.cc 25 KB

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