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.
5 #include "net/proxy/proxy_config_service_linux.h"
11 #include "base/bind.h"
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/format_macros.h"
16 #include "base/logging.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/synchronization/waitable_event.h"
20 #include "base/threading/thread.h"
21 #include "net/proxy/proxy_config.h"
22 #include "net/proxy/proxy_config_service_common_unittest.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "testing/platform_test.h"
29 // Set of values for all environment variables that we might
30 // query. NULL represents an unset variable.
32 // The strange capitalization is so that the field matches the
33 // environment variable name exactly.
34 const char *DESKTOP_SESSION, *HOME,
35 *KDEHOME, *KDE_SESSION_VERSION,
36 *auto_proxy, *all_proxy,
37 *http_proxy, *https_proxy, *ftp_proxy,
38 *SOCKS_SERVER, *SOCKS_VERSION,
42 // Undo macro pollution from GDK includes (from message_loop.h).
46 // So as to distinguish between an unset gconf boolean variable and
48 enum BoolSettingValue {
49 UNSET = 0, TRUE, FALSE
52 // Set of values for all gconf settings that we might query.
55 const char *mode, *autoconfig_url,
56 *http_host, *secure_host, *ftp_host, *socks_host;
58 int http_port, secure_port, ftp_port, socks_port;
60 BoolSettingValue use_proxy, same_proxy, use_auth;
62 std::vector<std::string> ignore_hosts;
65 // Mapping from a setting name to the location of the corresponding
66 // value (inside a EnvVarValues or GConfValues struct).
67 template<typename key_type, typename value_type>
68 struct SettingsTable {
69 typedef std::map<key_type, value_type*> map_type;
71 // Gets the value from its location
72 value_type Get(key_type key) {
73 typename map_type::const_iterator it = settings.find(key);
74 // In case there's a typo or the unittest becomes out of sync.
75 CHECK(it != settings.end()) << "key " << key << " not found";
76 value_type* value_ptr = it->second;
83 class MockEnvironment : public base::Environment {
86 #define ENTRY(x) table[#x] = &values.x
87 ENTRY(DESKTOP_SESSION);
90 ENTRY(KDE_SESSION_VERSION);
103 // Zeroes all environment values.
105 EnvVarValues zero_values = { 0 };
106 values = zero_values;
109 // Begin base::Environment implementation.
110 virtual bool GetVar(const char* variable_name, std::string* result) OVERRIDE {
111 std::map<std::string, const char**>::iterator it =
112 table.find(variable_name);
113 if (it != table.end() && *(it->second) != NULL) {
114 // Note that the variable may be defined but empty.
115 *result = *(it->second);
121 virtual bool SetVar(const char* variable_name, const std::string& new_value)
127 virtual bool UnSetVar(const char* variable_name) OVERRIDE {
131 // End base::Environment implementation.
133 // Intentionally public, for convenience when setting up a test.
137 std::map<std::string, const char**> table;
140 class MockSettingGetter
141 : public ProxyConfigServiceLinux::SettingGetter {
143 typedef ProxyConfigServiceLinux::SettingGetter SettingGetter;
144 MockSettingGetter() {
145 #define ENTRY(key, field) \
146 strings_table.settings[SettingGetter::key] = &values.field
147 ENTRY(PROXY_MODE, mode);
148 ENTRY(PROXY_AUTOCONF_URL, autoconfig_url);
149 ENTRY(PROXY_HTTP_HOST, http_host);
150 ENTRY(PROXY_HTTPS_HOST, secure_host);
151 ENTRY(PROXY_FTP_HOST, ftp_host);
152 ENTRY(PROXY_SOCKS_HOST, socks_host);
154 #define ENTRY(key, field) \
155 ints_table.settings[SettingGetter::key] = &values.field
156 ENTRY(PROXY_HTTP_PORT, http_port);
157 ENTRY(PROXY_HTTPS_PORT, secure_port);
158 ENTRY(PROXY_FTP_PORT, ftp_port);
159 ENTRY(PROXY_SOCKS_PORT, socks_port);
161 #define ENTRY(key, field) \
162 bools_table.settings[SettingGetter::key] = &values.field
163 ENTRY(PROXY_USE_HTTP_PROXY, use_proxy);
164 ENTRY(PROXY_USE_SAME_PROXY, same_proxy);
165 ENTRY(PROXY_USE_AUTHENTICATION, use_auth);
167 string_lists_table.settings[SettingGetter::PROXY_IGNORE_HOSTS] =
168 &values.ignore_hosts;
172 // Zeros all environment values.
174 GConfValues zero_values = { 0 };
175 values = zero_values;
179 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
180 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner)
182 task_runner_ = glib_task_runner;
186 virtual void ShutDown() OVERRIDE {}
188 virtual bool SetUpNotifications(ProxyConfigServiceLinux::Delegate* delegate)
193 virtual const scoped_refptr<base::SingleThreadTaskRunner>&
194 GetNotificationTaskRunner() OVERRIDE {
198 virtual ProxyConfigSource GetConfigSource() OVERRIDE {
199 return PROXY_CONFIG_SOURCE_TEST;
202 virtual bool GetString(StringSetting key, std::string* result) OVERRIDE {
203 const char* value = strings_table.Get(key);
211 virtual bool GetBool(BoolSetting key, bool* result) OVERRIDE {
212 BoolSettingValue value = bools_table.Get(key);
225 virtual bool GetInt(IntSetting key, int* result) OVERRIDE {
226 // We don't bother to distinguish unset keys from 0 values.
227 *result = ints_table.Get(key);
231 virtual bool GetStringList(StringListSetting key,
232 std::vector<std::string>* result) OVERRIDE {
233 *result = string_lists_table.Get(key);
234 // We don't bother to distinguish unset keys from empty lists.
235 return !result->empty();
238 virtual bool BypassListIsReversed() OVERRIDE {
242 virtual bool MatchHostsUsingSuffixMatching() OVERRIDE {
246 // Intentionally public, for convenience when setting up a test.
250 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
251 SettingsTable<StringSetting, const char*> strings_table;
252 SettingsTable<BoolSetting, BoolSettingValue> bools_table;
253 SettingsTable<IntSetting, int> ints_table;
254 SettingsTable<StringListSetting,
255 std::vector<std::string> > string_lists_table;
261 // This helper class runs ProxyConfigServiceLinux::GetLatestProxyConfig() on
262 // the IO thread and synchronously waits for the result.
263 // Some code duplicated from proxy_script_fetcher_unittest.cc.
264 class SynchConfigGetter {
266 // Takes ownership of |config_service|.
267 explicit SynchConfigGetter(net::ProxyConfigServiceLinux* config_service)
268 : event_(false, false),
269 io_thread_("IO_Thread"),
270 config_service_(config_service) {
271 // Start an IO thread.
272 base::Thread::Options options;
273 options.message_loop_type = base::MessageLoop::TYPE_IO;
274 io_thread_.StartWithOptions(options);
276 // Make sure the thread started.
277 io_thread_.message_loop()->PostTask(FROM_HERE,
278 base::Bind(&SynchConfigGetter::Init, base::Unretained(this)));
282 ~SynchConfigGetter() {
283 // Let the config service post a destroy message to the IO thread
284 // before cleaning up that thread.
285 delete config_service_;
286 // Clean up the IO thread.
287 io_thread_.message_loop()->PostTask(FROM_HERE,
288 base::Bind(&SynchConfigGetter::CleanUp, base::Unretained(this)));
292 // Does gconf setup and initial fetch of the proxy config,
293 // all on the calling thread (meant to be the thread with the
294 // default glib main loop, which is the UI thread).
295 void SetupAndInitialFetch() {
296 // We pass the mock IO thread as both the IO and file threads.
297 config_service_->SetupAndFetchInitialConfig(
298 base::MessageLoopProxy::current(),
299 io_thread_.message_loop_proxy(),
300 io_thread_.message_loop_proxy());
302 // Synchronously gets the proxy config.
303 net::ProxyConfigService::ConfigAvailability SyncGetLatestProxyConfig(
304 net::ProxyConfig* config) {
305 io_thread_.message_loop()->PostTask(FROM_HERE,
306 base::Bind(&SynchConfigGetter::GetLatestConfigOnIOThread,
307 base::Unretained(this)));
309 *config = proxy_config_;
310 return get_latest_config_result_;
314 // [Runs on |io_thread_|]
319 // Calls GetLatestProxyConfig, running on |io_thread_| Signals |event_|
321 void GetLatestConfigOnIOThread() {
322 get_latest_config_result_ =
323 config_service_->GetLatestProxyConfig(&proxy_config_);
327 // [Runs on |io_thread_|] Signals |event_| on cleanup completion.
329 base::MessageLoop::current()->RunUntilIdle();
338 base::WaitableEvent event_;
339 base::Thread io_thread_;
341 net::ProxyConfigServiceLinux* config_service_;
343 // The config obtained by |io_thread_| and read back by the main
345 net::ProxyConfig proxy_config_;
347 // Return value from GetLatestProxyConfig().
348 net::ProxyConfigService::ConfigAvailability get_latest_config_result_;
353 // This test fixture is only really needed for the KDEConfigParser test case,
354 // but all the test cases with the same prefix ("ProxyConfigServiceLinuxTest")
355 // must use the same test fixture class (also "ProxyConfigServiceLinuxTest").
356 class ProxyConfigServiceLinuxTest : public PlatformTest {
358 virtual void SetUp() OVERRIDE {
359 PlatformTest::SetUp();
360 // Set up a temporary KDE home directory.
361 std::string prefix("ProxyConfigServiceLinuxTest_user_home");
362 base::CreateNewTempDirectory(prefix, &user_home_);
363 kde_home_ = user_home_.Append(FILE_PATH_LITERAL(".kde"));
364 base::FilePath path = kde_home_.Append(FILE_PATH_LITERAL("share"));
365 path = path.Append(FILE_PATH_LITERAL("config"));
366 base::CreateDirectory(path);
367 kioslaverc_ = path.Append(FILE_PATH_LITERAL("kioslaverc"));
368 // Set up paths but do not create the directory for .kde4.
369 kde4_home_ = user_home_.Append(FILE_PATH_LITERAL(".kde4"));
370 path = kde4_home_.Append(FILE_PATH_LITERAL("share"));
371 kde4_config_ = path.Append(FILE_PATH_LITERAL("config"));
372 kioslaverc4_ = kde4_config_.Append(FILE_PATH_LITERAL("kioslaverc"));
375 virtual void TearDown() OVERRIDE {
376 // Delete the temporary KDE home directory.
377 base::DeleteFile(user_home_, true);
378 PlatformTest::TearDown();
381 base::FilePath user_home_;
383 base::FilePath kde_home_;
384 base::FilePath kioslaverc_;
386 base::FilePath kde4_home_;
387 base::FilePath kde4_config_;
388 base::FilePath kioslaverc4_;
391 // Builds an identifier for each test in an array.
392 #define TEST_DESC(desc) base::StringPrintf("at line %d <%s>", __LINE__, desc)
394 TEST_F(ProxyConfigServiceLinuxTest, BasicGConfTest) {
395 std::vector<std::string> empty_ignores;
397 std::vector<std::string> google_ignores;
398 google_ignores.push_back("*.google.com");
400 // Inspired from proxy_config_service_win_unittest.cc.
401 // Very neat, but harder to track down failures though.
403 // Short description to identify the test
404 std::string description;
409 // Expected outputs (availability and fields of ProxyConfig).
410 ProxyConfigService::ConfigAvailability availability;
413 ProxyRulesExpectation proxy_rules;
416 TEST_DESC("No proxying"),
419 "", // autoconfig_url
420 "", "", "", "", // hosts
422 FALSE, FALSE, FALSE, // use, same, auth
423 empty_ignores, // ignore_hosts
427 ProxyConfigService::CONFIG_VALID,
428 false, // auto_detect
430 ProxyRulesExpectation::Empty(),
434 TEST_DESC("Auto detect"),
437 "", // autoconfig_url
438 "", "", "", "", // hosts
440 FALSE, FALSE, FALSE, // use, same, auth
441 empty_ignores, // ignore_hosts
445 ProxyConfigService::CONFIG_VALID,
448 ProxyRulesExpectation::Empty(),
452 TEST_DESC("Valid PAC URL"),
455 "http://wpad/wpad.dat", // autoconfig_url
456 "", "", "", "", // hosts
458 FALSE, FALSE, FALSE, // use, same, auth
459 empty_ignores, // ignore_hosts
463 ProxyConfigService::CONFIG_VALID,
464 false, // auto_detect
465 GURL("http://wpad/wpad.dat"), // pac_url
466 ProxyRulesExpectation::Empty(),
470 TEST_DESC("Invalid PAC URL"),
473 "wpad.dat", // autoconfig_url
474 "", "", "", "", // hosts
476 FALSE, FALSE, FALSE, // use, same, auth
477 empty_ignores, // ignore_hosts
481 ProxyConfigService::CONFIG_VALID,
482 false, // auto_detect
484 ProxyRulesExpectation::Empty(),
488 TEST_DESC("Single-host in proxy list"),
491 "", // autoconfig_url
492 "www.google.com", "", "", "", // hosts
493 80, 0, 0, 0, // ports
494 TRUE, TRUE, FALSE, // use, same, auth
495 empty_ignores, // ignore_hosts
499 ProxyConfigService::CONFIG_VALID,
500 false, // auto_detect
502 ProxyRulesExpectation::Single(
503 "www.google.com:80", // single proxy
508 TEST_DESC("use_http_proxy is honored"),
511 "", // autoconfig_url
512 "www.google.com", "", "", "", // hosts
513 80, 0, 0, 0, // ports
514 FALSE, TRUE, FALSE, // use, same, auth
515 empty_ignores, // ignore_hosts
519 ProxyConfigService::CONFIG_VALID,
520 false, // auto_detect
522 ProxyRulesExpectation::Empty(),
526 TEST_DESC("use_http_proxy and use_same_proxy are optional"),
529 "", // autoconfig_url
530 "www.google.com", "", "", "", // hosts
531 80, 0, 0, 0, // ports
532 UNSET, UNSET, FALSE, // use, same, auth
533 empty_ignores, // ignore_hosts
537 ProxyConfigService::CONFIG_VALID,
538 false, // auto_detect
540 ProxyRulesExpectation::PerScheme(
541 "www.google.com:80", // http
548 TEST_DESC("Single-host, different port"),
551 "", // autoconfig_url
552 "www.google.com", "", "", "", // hosts
553 88, 0, 0, 0, // ports
554 TRUE, TRUE, FALSE, // use, same, auth
555 empty_ignores, // ignore_hosts
559 ProxyConfigService::CONFIG_VALID,
560 false, // auto_detect
562 ProxyRulesExpectation::Single(
563 "www.google.com:88", // single proxy
568 TEST_DESC("Per-scheme proxy rules"),
571 "", // autoconfig_url
572 "www.google.com", // http_host
573 "www.foo.com", // secure_host
574 "ftp.foo.com", // ftp
576 88, 110, 121, 0, // ports
577 TRUE, FALSE, FALSE, // use, same, auth
578 empty_ignores, // ignore_hosts
582 ProxyConfigService::CONFIG_VALID,
583 false, // auto_detect
585 ProxyRulesExpectation::PerScheme(
586 "www.google.com:88", // http
587 "www.foo.com:110", // https
588 "ftp.foo.com:121", // ftp
596 "", // autoconfig_url
597 "", "", "", "socks.com", // hosts
598 0, 0, 0, 99, // ports
599 TRUE, FALSE, FALSE, // use, same, auth
600 empty_ignores, // ignore_hosts
604 ProxyConfigService::CONFIG_VALID,
605 false, // auto_detect
607 ProxyRulesExpectation::Single(
608 "socks5://socks.com:99", // single proxy
613 TEST_DESC("Per-scheme proxy rules with fallback to SOCKS"),
616 "", // autoconfig_url
617 "www.google.com", // http_host
618 "www.foo.com", // secure_host
619 "ftp.foo.com", // ftp
620 "foobar.net", // socks
621 88, 110, 121, 99, // ports
622 TRUE, FALSE, FALSE, // use, same, auth
623 empty_ignores, // ignore_hosts
627 ProxyConfigService::CONFIG_VALID,
628 false, // auto_detect
630 ProxyRulesExpectation::PerSchemeWithSocks(
631 "www.google.com:88", // http
632 "www.foo.com:110", // https
633 "ftp.foo.com:121", // ftp
634 "socks5://foobar.net:99", // socks
639 TEST_DESC("Per-scheme proxy rules (just HTTP) with fallback to SOCKS"),
642 "", // autoconfig_url
643 "www.google.com", // http_host
646 "foobar.net", // socks
647 88, 0, 0, 99, // ports
648 TRUE, FALSE, FALSE, // use, same, auth
649 empty_ignores, // ignore_hosts
653 ProxyConfigService::CONFIG_VALID,
654 false, // auto_detect
656 ProxyRulesExpectation::PerSchemeWithSocks(
657 "www.google.com:88", // http
660 "socks5://foobar.net:99", // socks
665 TEST_DESC("Bypass *.google.com"),
668 "", // autoconfig_url
669 "www.google.com", "", "", "", // hosts
670 80, 0, 0, 0, // ports
671 TRUE, TRUE, FALSE, // use, same, auth
672 google_ignores, // ignore_hosts
675 ProxyConfigService::CONFIG_VALID,
676 false, // auto_detect
678 ProxyRulesExpectation::Single(
679 "www.google.com:80", // single proxy
680 "*.google.com"), // bypass rules
684 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
685 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "] %s", i,
686 tests[i].description.c_str()));
687 MockEnvironment* env = new MockEnvironment;
688 MockSettingGetter* setting_getter = new MockSettingGetter;
689 SynchConfigGetter sync_config_getter(
690 new ProxyConfigServiceLinux(env, setting_getter));
692 setting_getter->values = tests[i].values;
693 sync_config_getter.SetupAndInitialFetch();
694 ProxyConfigService::ConfigAvailability availability =
695 sync_config_getter.SyncGetLatestProxyConfig(&config);
696 EXPECT_EQ(tests[i].availability, availability);
698 if (availability == ProxyConfigService::CONFIG_VALID) {
699 EXPECT_EQ(tests[i].auto_detect, config.auto_detect());
700 EXPECT_EQ(tests[i].pac_url, config.pac_url());
701 EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules()));
706 TEST_F(ProxyConfigServiceLinuxTest, BasicEnvTest) {
707 // Inspired from proxy_config_service_win_unittest.cc.
709 // Short description to identify the test
710 std::string description;
715 // Expected outputs (availability and fields of ProxyConfig).
716 ProxyConfigService::ConfigAvailability availability;
719 ProxyRulesExpectation proxy_rules;
722 TEST_DESC("No proxying"),
724 NULL, // DESKTOP_SESSION
727 NULL, // KDE_SESSION_VERSION
730 NULL, NULL, NULL, // per-proto proxies
736 ProxyConfigService::CONFIG_VALID,
737 false, // auto_detect
739 ProxyRulesExpectation::Empty(),
743 TEST_DESC("Auto detect"),
745 NULL, // DESKTOP_SESSION
748 NULL, // KDE_SESSION_VERSION
751 NULL, NULL, NULL, // per-proto proxies
757 ProxyConfigService::CONFIG_VALID,
760 ProxyRulesExpectation::Empty(),
764 TEST_DESC("Valid PAC URL"),
766 NULL, // DESKTOP_SESSION
769 NULL, // KDE_SESSION_VERSION
770 "http://wpad/wpad.dat", // auto_proxy
772 NULL, NULL, NULL, // per-proto proxies
778 ProxyConfigService::CONFIG_VALID,
779 false, // auto_detect
780 GURL("http://wpad/wpad.dat"), // pac_url
781 ProxyRulesExpectation::Empty(),
785 TEST_DESC("Invalid PAC URL"),
787 NULL, // DESKTOP_SESSION
790 NULL, // KDE_SESSION_VERSION
791 "wpad.dat", // auto_proxy
793 NULL, NULL, NULL, // per-proto proxies
799 ProxyConfigService::CONFIG_VALID,
800 false, // auto_detect
802 ProxyRulesExpectation::Empty(),
806 TEST_DESC("Single-host in proxy list"),
808 NULL, // DESKTOP_SESSION
811 NULL, // KDE_SESSION_VERSION
813 "www.google.com", // all_proxy
814 NULL, NULL, NULL, // per-proto proxies
820 ProxyConfigService::CONFIG_VALID,
821 false, // auto_detect
823 ProxyRulesExpectation::Single(
824 "www.google.com:80", // single proxy
829 TEST_DESC("Single-host, different port"),
831 NULL, // DESKTOP_SESSION
834 NULL, // KDE_SESSION_VERSION
836 "www.google.com:99", // all_proxy
837 NULL, NULL, NULL, // per-proto proxies
843 ProxyConfigService::CONFIG_VALID,
844 false, // auto_detect
846 ProxyRulesExpectation::Single(
847 "www.google.com:99", // single
852 TEST_DESC("Tolerate a scheme"),
854 NULL, // DESKTOP_SESSION
857 NULL, // KDE_SESSION_VERSION
859 "http://www.google.com:99", // all_proxy
860 NULL, NULL, NULL, // per-proto proxies
866 ProxyConfigService::CONFIG_VALID,
867 false, // auto_detect
869 ProxyRulesExpectation::Single(
870 "www.google.com:99", // single proxy
875 TEST_DESC("Per-scheme proxy rules"),
877 NULL, // DESKTOP_SESSION
880 NULL, // KDE_SESSION_VERSION
883 "www.google.com:80", "www.foo.com:110", "ftp.foo.com:121", // per-proto
889 ProxyConfigService::CONFIG_VALID,
890 false, // auto_detect
892 ProxyRulesExpectation::PerScheme(
893 "www.google.com:80", // http
894 "www.foo.com:110", // https
895 "ftp.foo.com:121", // ftp
902 NULL, // DESKTOP_SESSION
905 NULL, // KDE_SESSION_VERSION
908 NULL, NULL, NULL, // per-proto proxies
909 "socks.com:888", NULL, // SOCKS
914 ProxyConfigService::CONFIG_VALID,
915 false, // auto_detect
917 ProxyRulesExpectation::Single(
918 "socks5://socks.com:888", // single proxy
925 NULL, // DESKTOP_SESSION
928 NULL, // KDE_SESSION_VERSION
931 NULL, NULL, NULL, // per-proto proxies
932 "socks.com:888", "4", // SOCKS
937 ProxyConfigService::CONFIG_VALID,
938 false, // auto_detect
940 ProxyRulesExpectation::Single(
941 "socks4://socks.com:888", // single proxy
946 TEST_DESC("socks default port"),
948 NULL, // DESKTOP_SESSION
951 NULL, // KDE_SESSION_VERSION
954 NULL, NULL, NULL, // per-proto proxies
955 "socks.com", NULL, // SOCKS
960 ProxyConfigService::CONFIG_VALID,
961 false, // auto_detect
963 ProxyRulesExpectation::Single(
964 "socks5://socks.com:1080", // single proxy
971 NULL, // DESKTOP_SESSION
974 NULL, // KDE_SESSION_VERSION
976 "www.google.com", // all_proxy
977 NULL, NULL, NULL, // per-proto
979 ".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8", // no_proxy
983 ProxyConfigService::CONFIG_VALID,
984 false, // auto_detect
986 ProxyRulesExpectation::Single(
988 "*.google.com,*foo.com:99,1.2.3.4:22,127.0.0.1/8"),
992 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
993 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "] %s", i,
994 tests[i].description.c_str()));
995 MockEnvironment* env = new MockEnvironment;
996 MockSettingGetter* setting_getter = new MockSettingGetter;
997 SynchConfigGetter sync_config_getter(
998 new ProxyConfigServiceLinux(env, setting_getter));
1000 env->values = tests[i].values;
1001 sync_config_getter.SetupAndInitialFetch();
1002 ProxyConfigService::ConfigAvailability availability =
1003 sync_config_getter.SyncGetLatestProxyConfig(&config);
1004 EXPECT_EQ(tests[i].availability, availability);
1006 if (availability == ProxyConfigService::CONFIG_VALID) {
1007 EXPECT_EQ(tests[i].auto_detect, config.auto_detect());
1008 EXPECT_EQ(tests[i].pac_url, config.pac_url());
1009 EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules()));
1014 TEST_F(ProxyConfigServiceLinuxTest, GconfNotification) {
1015 MockEnvironment* env = new MockEnvironment;
1016 MockSettingGetter* setting_getter = new MockSettingGetter;
1017 ProxyConfigServiceLinux* service =
1018 new ProxyConfigServiceLinux(env, setting_getter);
1019 SynchConfigGetter sync_config_getter(service);
1022 // Start with no proxy.
1023 setting_getter->values.mode = "none";
1024 sync_config_getter.SetupAndInitialFetch();
1025 EXPECT_EQ(ProxyConfigService::CONFIG_VALID,
1026 sync_config_getter.SyncGetLatestProxyConfig(&config));
1027 EXPECT_FALSE(config.auto_detect());
1029 // Now set to auto-detect.
1030 setting_getter->values.mode = "auto";
1031 // Simulate setting change notification callback.
1032 service->OnCheckProxyConfigSettings();
1033 EXPECT_EQ(ProxyConfigService::CONFIG_VALID,
1034 sync_config_getter.SyncGetLatestProxyConfig(&config));
1035 EXPECT_TRUE(config.auto_detect());
1038 TEST_F(ProxyConfigServiceLinuxTest, KDEConfigParser) {
1039 // One of the tests below needs a worst-case long line prefix. We build it
1040 // programmatically so that it will always be the right size.
1041 std::string long_line;
1042 size_t limit = ProxyConfigServiceLinux::SettingGetter::BUFFER_SIZE - 1;
1043 for (size_t i = 0; i < limit; ++i)
1046 // Inspired from proxy_config_service_win_unittest.cc.
1048 // Short description to identify the test
1049 std::string description;
1052 std::string kioslaverc;
1053 EnvVarValues env_values;
1055 // Expected outputs (availability and fields of ProxyConfig).
1056 ProxyConfigService::ConfigAvailability availability;
1059 ProxyRulesExpectation proxy_rules;
1062 TEST_DESC("No proxying"),
1065 "[Proxy Settings]\nProxyType=0\n",
1069 ProxyConfigService::CONFIG_VALID,
1070 false, // auto_detect
1072 ProxyRulesExpectation::Empty(),
1076 TEST_DESC("Auto detect"),
1079 "[Proxy Settings]\nProxyType=3\n",
1083 ProxyConfigService::CONFIG_VALID,
1084 true, // auto_detect
1086 ProxyRulesExpectation::Empty(),
1090 TEST_DESC("Valid PAC URL"),
1093 "[Proxy Settings]\nProxyType=2\n"
1094 "Proxy Config Script=http://wpad/wpad.dat\n",
1098 ProxyConfigService::CONFIG_VALID,
1099 false, // auto_detect
1100 GURL("http://wpad/wpad.dat"), // pac_url
1101 ProxyRulesExpectation::Empty(),
1105 TEST_DESC("Valid PAC file without file://"),
1108 "[Proxy Settings]\nProxyType=2\n"
1109 "Proxy Config Script=/wpad/wpad.dat\n",
1113 ProxyConfigService::CONFIG_VALID,
1114 false, // auto_detect
1115 GURL("file:///wpad/wpad.dat"), // pac_url
1116 ProxyRulesExpectation::Empty(),
1120 TEST_DESC("Per-scheme proxy rules"),
1123 "[Proxy Settings]\nProxyType=1\nhttpProxy=www.google.com\n"
1124 "httpsProxy=www.foo.com\nftpProxy=ftp.foo.com\n",
1128 ProxyConfigService::CONFIG_VALID,
1129 false, // auto_detect
1131 ProxyRulesExpectation::PerScheme(
1132 "www.google.com:80", // http
1133 "www.foo.com:80", // https
1134 "ftp.foo.com:80", // http
1135 ""), // bypass rules
1139 TEST_DESC("Only HTTP proxy specified"),
1142 "[Proxy Settings]\nProxyType=1\n"
1143 "httpProxy=www.google.com\n",
1147 ProxyConfigService::CONFIG_VALID,
1148 false, // auto_detect
1150 ProxyRulesExpectation::PerScheme(
1151 "www.google.com:80", // http
1154 ""), // bypass rules
1158 TEST_DESC("Only HTTP proxy specified, different port"),
1161 "[Proxy Settings]\nProxyType=1\n"
1162 "httpProxy=www.google.com:88\n",
1166 ProxyConfigService::CONFIG_VALID,
1167 false, // auto_detect
1169 ProxyRulesExpectation::PerScheme(
1170 "www.google.com:88", // http
1173 ""), // bypass rules
1177 TEST_DESC("Only HTTP proxy specified, different port, space-delimited"),
1180 "[Proxy Settings]\nProxyType=1\n"
1181 "httpProxy=www.google.com 88\n",
1185 ProxyConfigService::CONFIG_VALID,
1186 false, // auto_detect
1188 ProxyRulesExpectation::PerScheme(
1189 "www.google.com:88", // http
1192 ""), // bypass rules
1196 TEST_DESC("Bypass *.google.com"),
1199 "[Proxy Settings]\nProxyType=1\nhttpProxy=www.google.com\n"
1200 "NoProxyFor=.google.com\n",
1204 ProxyConfigService::CONFIG_VALID,
1205 false, // auto_detect
1207 ProxyRulesExpectation::PerScheme(
1208 "www.google.com:80", // http
1211 "*.google.com"), // bypass rules
1215 TEST_DESC("Bypass *.google.com and *.kde.org"),
1218 "[Proxy Settings]\nProxyType=1\nhttpProxy=www.google.com\n"
1219 "NoProxyFor=.google.com,.kde.org\n",
1223 ProxyConfigService::CONFIG_VALID,
1224 false, // auto_detect
1226 ProxyRulesExpectation::PerScheme(
1227 "www.google.com:80", // http
1230 "*.google.com,*.kde.org"), // bypass rules
1234 TEST_DESC("Correctly parse bypass list with ReversedException"),
1237 "[Proxy Settings]\nProxyType=1\nhttpProxy=www.google.com\n"
1238 "NoProxyFor=.google.com\nReversedException=true\n",
1242 ProxyConfigService::CONFIG_VALID,
1243 false, // auto_detect
1245 ProxyRulesExpectation::PerSchemeWithBypassReversed(
1246 "www.google.com:80", // http
1249 "*.google.com"), // bypass rules
1256 "[Proxy Settings]\nProxyType=1\nsocksProxy=socks.com 888\n",
1260 ProxyConfigService::CONFIG_VALID,
1261 false, // auto_detect
1263 ProxyRulesExpectation::Single(
1264 "socks5://socks.com:888", // single proxy
1265 ""), // bypass rules
1269 TEST_DESC("socks4"),
1272 "[Proxy Settings]\nProxyType=1\nsocksProxy=socks4://socks.com 888\n",
1276 ProxyConfigService::CONFIG_VALID,
1277 false, // auto_detect
1279 ProxyRulesExpectation::Single(
1280 "socks4://socks.com:888", // single proxy
1281 ""), // bypass rules
1285 TEST_DESC("Treat all hostname patterns as wildcard patterns"),
1288 "[Proxy Settings]\nProxyType=1\nhttpProxy=www.google.com\n"
1289 "NoProxyFor=google.com,kde.org,<local>\n",
1293 ProxyConfigService::CONFIG_VALID,
1294 false, // auto_detect
1296 ProxyRulesExpectation::PerScheme(
1297 "www.google.com:80", // http
1300 "*google.com,*kde.org,<local>"), // bypass rules
1304 TEST_DESC("Allow trailing whitespace after boolean value"),
1307 "[Proxy Settings]\nProxyType=1\nhttpProxy=www.google.com\n"
1308 "NoProxyFor=.google.com\nReversedException=true \n",
1312 ProxyConfigService::CONFIG_VALID,
1313 false, // auto_detect
1315 ProxyRulesExpectation::PerSchemeWithBypassReversed(
1316 "www.google.com:80", // http
1319 "*.google.com"), // bypass rules
1323 TEST_DESC("Ignore settings outside [Proxy Settings]"),
1326 "httpsProxy=www.foo.com\n[Proxy Settings]\nProxyType=1\n"
1327 "httpProxy=www.google.com\n[Other Section]\nftpProxy=ftp.foo.com\n",
1331 ProxyConfigService::CONFIG_VALID,
1332 false, // auto_detect
1334 ProxyRulesExpectation::PerScheme(
1335 "www.google.com:80", // http
1338 ""), // bypass rules
1342 TEST_DESC("Handle CRLF line endings"),
1345 "[Proxy Settings]\r\nProxyType=1\r\nhttpProxy=www.google.com\r\n",
1349 ProxyConfigService::CONFIG_VALID,
1350 false, // auto_detect
1352 ProxyRulesExpectation::PerScheme(
1353 "www.google.com:80", // http
1356 ""), // bypass rules
1360 TEST_DESC("Handle blank lines and mixed line endings"),
1363 "[Proxy Settings]\r\n\nProxyType=1\n\r\nhttpProxy=www.google.com\n\n",
1367 ProxyConfigService::CONFIG_VALID,
1368 false, // auto_detect
1370 ProxyRulesExpectation::PerScheme(
1371 "www.google.com:80", // http
1374 ""), // bypass rules
1378 TEST_DESC("Handle localized settings"),
1381 "[Proxy Settings]\nProxyType[$e]=1\nhttpProxy[$e]=www.google.com\n",
1385 ProxyConfigService::CONFIG_VALID,
1386 false, // auto_detect
1388 ProxyRulesExpectation::PerScheme(
1389 "www.google.com:80", // http
1392 ""), // bypass rules
1396 TEST_DESC("Ignore malformed localized settings"),
1399 "[Proxy Settings]\nProxyType=1\nhttpProxy=www.google.com\n"
1400 "httpsProxy$e]=www.foo.com\nftpProxy=ftp.foo.com\n",
1404 ProxyConfigService::CONFIG_VALID,
1405 false, // auto_detect
1407 ProxyRulesExpectation::PerScheme(
1408 "www.google.com:80", // http
1410 "ftp.foo.com:80", // ftp
1411 ""), // bypass rules
1415 TEST_DESC("Handle strange whitespace"),
1418 "[Proxy Settings]\nProxyType [$e] =2\n"
1419 " Proxy Config Script = http:// foo\n",
1423 ProxyConfigService::CONFIG_VALID,
1424 false, // auto_detect
1425 GURL("http:// foo"), // pac_url
1426 ProxyRulesExpectation::Empty(),
1430 TEST_DESC("Ignore all of a line which is too long"),
1433 std::string("[Proxy Settings]\nProxyType=1\nftpProxy=ftp.foo.com\n") +
1434 long_line + "httpsProxy=www.foo.com\nhttpProxy=www.google.com\n",
1438 ProxyConfigService::CONFIG_VALID,
1439 false, // auto_detect
1441 ProxyRulesExpectation::PerScheme(
1442 "www.google.com:80", // http
1444 "ftp.foo.com:80", // ftp
1445 ""), // bypass rules
1449 TEST_DESC("Indirect Proxy - no env vars set"),
1452 "[Proxy Settings]\nProxyType=4\nhttpProxy=http_proxy\n"
1453 "httpsProxy=https_proxy\nftpProxy=ftp_proxy\nNoProxyFor=no_proxy\n",
1457 ProxyConfigService::CONFIG_VALID,
1458 false, // auto_detect
1460 ProxyRulesExpectation::Empty(),
1464 TEST_DESC("Indirect Proxy - with env vars set"),
1467 "[Proxy Settings]\nProxyType=4\nhttpProxy=http_proxy\n"
1468 "httpsProxy=https_proxy\nftpProxy=ftp_proxy\nNoProxyFor=no_proxy\n",
1470 NULL, // DESKTOP_SESSION
1473 NULL, // KDE_SESSION_VERSION
1476 "www.normal.com", // http_proxy
1477 "www.secure.com", // https_proxy
1478 "ftp.foo.com", // ftp_proxy
1479 NULL, NULL, // SOCKS
1480 ".google.com, .kde.org", // no_proxy
1484 ProxyConfigService::CONFIG_VALID,
1485 false, // auto_detect
1487 ProxyRulesExpectation::PerScheme(
1488 "www.normal.com:80", // http
1489 "www.secure.com:80", // https
1490 "ftp.foo.com:80", // ftp
1491 "*.google.com,*.kde.org"), // bypass rules
1496 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
1497 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "] %s", i,
1498 tests[i].description.c_str()));
1499 MockEnvironment* env = new MockEnvironment;
1500 env->values = tests[i].env_values;
1501 // Force the KDE getter to be used and tell it where the test is.
1502 env->values.DESKTOP_SESSION = "kde4";
1503 env->values.KDEHOME = kde_home_.value().c_str();
1504 SynchConfigGetter sync_config_getter(
1505 new ProxyConfigServiceLinux(env));
1507 // Overwrite the kioslaverc file.
1508 base::WriteFile(kioslaverc_, tests[i].kioslaverc.c_str(),
1509 tests[i].kioslaverc.length());
1510 sync_config_getter.SetupAndInitialFetch();
1511 ProxyConfigService::ConfigAvailability availability =
1512 sync_config_getter.SyncGetLatestProxyConfig(&config);
1513 EXPECT_EQ(tests[i].availability, availability);
1515 if (availability == ProxyConfigService::CONFIG_VALID) {
1516 EXPECT_EQ(tests[i].auto_detect, config.auto_detect());
1517 EXPECT_EQ(tests[i].pac_url, config.pac_url());
1518 EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules()));
1523 TEST_F(ProxyConfigServiceLinuxTest, KDEHomePicker) {
1524 // Auto detect proxy settings.
1525 std::string slaverc3 = "[Proxy Settings]\nProxyType=3\n";
1527 std::string slaverc4 = "[Proxy Settings]\nProxyType=2\n"
1528 "Proxy Config Script=http://wpad/wpad.dat\n";
1529 GURL slaverc4_pac_url("http://wpad/wpad.dat");
1531 // Overwrite the .kde kioslaverc file.
1532 base::WriteFile(kioslaverc_, slaverc3.c_str(), slaverc3.length());
1534 // If .kde4 exists it will mess up the first test. It should not, as
1535 // we created the directory for $HOME in the test setup.
1536 CHECK(!base::DirectoryExists(kde4_home_));
1538 { SCOPED_TRACE("KDE4, no .kde4 directory, verify fallback");
1539 MockEnvironment* env = new MockEnvironment;
1540 env->values.DESKTOP_SESSION = "kde4";
1541 env->values.HOME = user_home_.value().c_str();
1542 SynchConfigGetter sync_config_getter(
1543 new ProxyConfigServiceLinux(env));
1545 sync_config_getter.SetupAndInitialFetch();
1546 EXPECT_EQ(ProxyConfigService::CONFIG_VALID,
1547 sync_config_getter.SyncGetLatestProxyConfig(&config));
1548 EXPECT_TRUE(config.auto_detect());
1549 EXPECT_EQ(GURL(), config.pac_url());
1552 // Now create .kde4 and put a kioslaverc in the config directory.
1553 // Note that its timestamp will be at least as new as the .kde one.
1554 base::CreateDirectory(kde4_config_);
1555 base::WriteFile(kioslaverc4_, slaverc4.c_str(), slaverc4.length());
1556 CHECK(base::PathExists(kioslaverc4_));
1558 { SCOPED_TRACE("KDE4, .kde4 directory present, use it");
1559 MockEnvironment* env = new MockEnvironment;
1560 env->values.DESKTOP_SESSION = "kde4";
1561 env->values.HOME = user_home_.value().c_str();
1562 SynchConfigGetter sync_config_getter(
1563 new ProxyConfigServiceLinux(env));
1565 sync_config_getter.SetupAndInitialFetch();
1566 EXPECT_EQ(ProxyConfigService::CONFIG_VALID,
1567 sync_config_getter.SyncGetLatestProxyConfig(&config));
1568 EXPECT_FALSE(config.auto_detect());
1569 EXPECT_EQ(slaverc4_pac_url, config.pac_url());
1572 { SCOPED_TRACE("KDE3, .kde4 directory present, ignore it");
1573 MockEnvironment* env = new MockEnvironment;
1574 env->values.DESKTOP_SESSION = "kde";
1575 env->values.HOME = user_home_.value().c_str();
1576 SynchConfigGetter sync_config_getter(
1577 new ProxyConfigServiceLinux(env));
1579 sync_config_getter.SetupAndInitialFetch();
1580 EXPECT_EQ(ProxyConfigService::CONFIG_VALID,
1581 sync_config_getter.SyncGetLatestProxyConfig(&config));
1582 EXPECT_TRUE(config.auto_detect());
1583 EXPECT_EQ(GURL(), config.pac_url());
1586 { SCOPED_TRACE("KDE4, .kde4 directory present, KDEHOME set to .kde");
1587 MockEnvironment* env = new MockEnvironment;
1588 env->values.DESKTOP_SESSION = "kde4";
1589 env->values.HOME = user_home_.value().c_str();
1590 env->values.KDEHOME = kde_home_.value().c_str();
1591 SynchConfigGetter sync_config_getter(
1592 new ProxyConfigServiceLinux(env));
1594 sync_config_getter.SetupAndInitialFetch();
1595 EXPECT_EQ(ProxyConfigService::CONFIG_VALID,
1596 sync_config_getter.SyncGetLatestProxyConfig(&config));
1597 EXPECT_TRUE(config.auto_detect());
1598 EXPECT_EQ(GURL(), config.pac_url());
1601 // Finally, make the .kde4 config directory older than the .kde directory
1602 // and make sure we then use .kde instead of .kde4 since it's newer.
1603 base::TouchFile(kde4_config_, base::Time(), base::Time());
1605 { SCOPED_TRACE("KDE4, very old .kde4 directory present, use .kde");
1606 MockEnvironment* env = new MockEnvironment;
1607 env->values.DESKTOP_SESSION = "kde4";
1608 env->values.HOME = user_home_.value().c_str();
1609 SynchConfigGetter sync_config_getter(
1610 new ProxyConfigServiceLinux(env));
1612 sync_config_getter.SetupAndInitialFetch();
1613 EXPECT_EQ(ProxyConfigService::CONFIG_VALID,
1614 sync_config_getter.SyncGetLatestProxyConfig(&config));
1615 EXPECT_TRUE(config.auto_detect());
1616 EXPECT_EQ(GURL(), config.pac_url());