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 "base/prefs/pref_service.h"
6 #include "chrome/browser/extensions/extension_apitest.h"
7 #include "chrome/browser/extensions/extension_util.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "components/proxy_config/proxy_config_dictionary.h"
12 #include "components/proxy_config/proxy_config_pref_names.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/test/result_catcher.h"
16 namespace extensions
{
20 const char kNoServer
[] = "";
21 const char kNoBypass
[] = "";
22 const char kNoPac
[] = "";
26 class ProxySettingsApiTest
: public ExtensionApiTest
{
28 void ValidateSettings(int expected_mode
,
29 const std::string
& expected_server
,
30 const std::string
& bypass
,
31 const std::string
& expected_pac_url
,
32 PrefService
* pref_service
) {
33 const PrefService::Preference
* pref
=
34 pref_service
->FindPreference(proxy_config::prefs::kProxy
);
35 ASSERT_TRUE(pref
!= NULL
);
36 EXPECT_TRUE(pref
->IsExtensionControlled());
38 ProxyConfigDictionary
dict(
39 pref_service
->GetDictionary(proxy_config::prefs::kProxy
));
41 ProxyPrefs::ProxyMode mode
;
42 ASSERT_TRUE(dict
.GetMode(&mode
));
43 EXPECT_EQ(expected_mode
, mode
);
46 if (!bypass
.empty()) {
47 ASSERT_TRUE(dict
.GetBypassList(&value
));
48 EXPECT_EQ(bypass
, value
);
50 EXPECT_FALSE(dict
.GetBypassList(&value
));
53 if (!expected_pac_url
.empty()) {
54 ASSERT_TRUE(dict
.GetPacUrl(&value
));
55 EXPECT_EQ(expected_pac_url
, value
);
57 EXPECT_FALSE(dict
.GetPacUrl(&value
));
60 if (!expected_server
.empty()) {
61 ASSERT_TRUE(dict
.GetProxyServer(&value
));
62 EXPECT_EQ(expected_server
, value
);
64 EXPECT_FALSE(dict
.GetProxyServer(&value
));
68 void ExpectNoSettings(PrefService
* pref_service
) {
69 const PrefService::Preference
* pref
=
70 pref_service
->FindPreference(proxy_config::prefs::kProxy
);
71 ASSERT_TRUE(pref
!= NULL
);
72 EXPECT_FALSE(pref
->IsExtensionControlled());
75 bool SetIsIncognitoEnabled(bool enabled
) {
76 ResultCatcher catcher
;
77 extensions::util::SetIsIncognitoEnabled(
78 GetSingleLoadedExtension()->id(), browser()->profile(), enabled
);
79 if (!catcher
.GetNextResult()) {
80 message_
= catcher
.message();
87 // Tests direct connection settings.
88 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyDirectSettings
) {
89 ASSERT_TRUE(RunExtensionTestIncognito("proxy/direct")) << message_
;
90 const Extension
* extension
= GetSingleLoadedExtension();
91 ASSERT_TRUE(extension
);
93 PrefService
* pref_service
= browser()->profile()->GetPrefs();
94 ValidateSettings(ProxyPrefs::MODE_DIRECT
, kNoServer
, kNoBypass
, kNoPac
,
97 // As the extension is executed with incognito permission, the settings
98 // should propagate to incognito mode.
99 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
100 ValidateSettings(ProxyPrefs::MODE_DIRECT
, kNoServer
, kNoBypass
, kNoPac
,
104 // Tests auto-detect settings.
105 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyAutoSettings
) {
106 ASSERT_TRUE(RunExtensionTestIncognito("proxy/auto")) << message_
;
107 const Extension
* extension
= GetSingleLoadedExtension();
108 ASSERT_TRUE(extension
);
110 PrefService
* pref_service
= browser()->profile()->GetPrefs();
111 ValidateSettings(ProxyPrefs::MODE_AUTO_DETECT
, kNoServer
, kNoBypass
, kNoPac
,
115 // Tests PAC proxy settings.
116 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyPacScript
) {
117 ASSERT_TRUE(RunExtensionTest("proxy/pac")) << message_
;
118 const Extension
* extension
= GetSingleLoadedExtension();
119 ASSERT_TRUE(extension
);
121 PrefService
* pref_service
= browser()->profile()->GetPrefs();
122 ValidateSettings(ProxyPrefs::MODE_PAC_SCRIPT
, kNoServer
, kNoBypass
,
123 "http://wpad/windows.pac", pref_service
);
125 // As the extension is not executed with incognito permission, the settings
126 // should not propagate to incognito mode.
127 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
128 ExpectNoSettings(pref_service
);
130 // Now we enable the extension in incognito mode and verify that settings
132 ASSERT_TRUE(SetIsIncognitoEnabled(true));
133 ValidateSettings(ProxyPrefs::MODE_PAC_SCRIPT
, kNoServer
, kNoBypass
,
134 "http://wpad/windows.pac", pref_service
);
136 // Disabling incognito permission should revoke the settings for incognito
138 ASSERT_TRUE(SetIsIncognitoEnabled(false));
139 ExpectNoSettings(pref_service
);
142 // Tests PAC proxy settings.
143 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyPacDataUrl
) {
144 ASSERT_TRUE(RunExtensionTest("proxy/pacdataurl")) << message_
;
145 const Extension
* extension
= GetSingleLoadedExtension();
146 ASSERT_TRUE(extension
);
148 "data:;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
149 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
150 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
151 PrefService
* pref_service
= browser()->profile()->GetPrefs();
152 ValidateSettings(ProxyPrefs::MODE_PAC_SCRIPT
, kNoServer
, kNoBypass
,
156 // Tests PAC proxy settings.
157 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyPacData
) {
158 ASSERT_TRUE(RunExtensionTest("proxy/pacdata")) << message_
;
159 const Extension
* extension
= GetSingleLoadedExtension();
160 ASSERT_TRUE(extension
);
162 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
163 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
164 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
165 PrefService
* pref_service
= browser()->profile()->GetPrefs();
166 ValidateSettings(ProxyPrefs::MODE_PAC_SCRIPT
, kNoServer
, kNoBypass
,
170 // Tests setting a single proxy to cover all schemes.
171 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyFixedSingle
) {
172 ASSERT_TRUE(RunExtensionTest("proxy/single")) << message_
;
173 const Extension
* extension
= GetSingleLoadedExtension();
174 ASSERT_TRUE(extension
);
176 PrefService
* pref_service
= browser()->profile()->GetPrefs();
177 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
184 // Tests setting to use the system's proxy settings.
185 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxySystem
) {
186 ASSERT_TRUE(RunExtensionTest("proxy/system")) << message_
;
187 const Extension
* extension
= GetSingleLoadedExtension();
188 ASSERT_TRUE(extension
);
190 PrefService
* pref_service
= browser()->profile()->GetPrefs();
191 ValidateSettings(ProxyPrefs::MODE_SYSTEM
, kNoServer
, kNoBypass
, kNoPac
,
195 // Tests setting separate proxies for each scheme.
196 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyFixedIndividual
) {
197 ASSERT_TRUE(RunExtensionTestIncognito("proxy/individual")) << message_
;
198 const Extension
* extension
= GetSingleLoadedExtension();
199 ASSERT_TRUE(extension
);
201 PrefService
* pref_service
= browser()->profile()->GetPrefs();
202 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
203 "http=quic://1.1.1.1:443;"
204 "https=2.2.2.2:80;" // http:// is pruned.
205 "ftp=3.3.3.3:9000;" // http:// is pruned.
206 "socks=socks4://4.4.4.4:9090",
211 // Now check the incognito preferences.
212 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
213 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
214 "http=quic://1.1.1.1:443;"
217 "socks=socks4://4.4.4.4:9090",
223 // Tests setting values only for incognito mode
224 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
,
225 ProxyFixedIndividualIncognitoOnly
) {
226 ASSERT_TRUE(RunExtensionTestIncognito("proxy/individual_incognito_only")) <<
228 const Extension
* extension
= GetSingleLoadedExtension();
229 ASSERT_TRUE(extension
);
231 PrefService
* pref_service
= browser()->profile()->GetPrefs();
232 ExpectNoSettings(pref_service
);
234 // Now check the incognito preferences.
235 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
236 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
238 "https=socks5://2.2.2.2:1080;"
240 "socks=socks4://4.4.4.4:9090",
246 // Tests setting values also for incognito mode
247 // Test disabled due to http://crbug.com/88972.
248 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
,
249 DISABLED_ProxyFixedIndividualIncognitoAlso
) {
250 ASSERT_TRUE(RunExtensionTestIncognito("proxy/individual_incognito_also")) <<
252 const Extension
* extension
= GetSingleLoadedExtension();
253 ASSERT_TRUE(extension
);
255 PrefService
* pref_service
= browser()->profile()->GetPrefs();
256 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
258 "https=socks5://2.2.2.2:1080;"
260 "socks=socks4://4.4.4.4:9090",
265 // Now check the incognito preferences.
266 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
267 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
269 "https=socks5://6.6.6.6:1080;"
271 "socks=socks4://8.8.8.8:9090",
277 // Tests setting and unsetting values
278 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyFixedIndividualRemove
) {
279 ASSERT_TRUE(RunExtensionTest("proxy/individual_remove")) << message_
;
280 const Extension
* extension
= GetSingleLoadedExtension();
281 ASSERT_TRUE(extension
);
283 PrefService
* pref_service
= browser()->profile()->GetPrefs();
284 ExpectNoSettings(pref_service
);
287 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
,
289 ASSERT_TRUE(RunExtensionTestIncognito("proxy/bypass")) << message_
;
290 const Extension
* extension
= GetSingleLoadedExtension();
291 ASSERT_TRUE(extension
);
293 PrefService
* pref_service
= browser()->profile()->GetPrefs();
294 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
296 "localhost,::1,foo.bar,<local>",
300 // Now check the incognito preferences.
301 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
302 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
304 "localhost,::1,foo.bar,<local>",
309 // This test sets proxy to an inavalid host "does.not.exist" and then fetches
310 // a page from localhost, expecting an error since host is invalid.
311 // On ChromeOS, localhost is by default bypassed, so the page from localhost
312 // will be fetched successfully, resulting in no error. Hence this test
313 // shouldn't run on ChromeOS.
314 #if defined(OS_CHROMEOS)
315 #define MAYBE_ProxyEventsInvalidProxy DISABLED_ProxyEventsInvalidProxy
317 #define MAYBE_ProxyEventsInvalidProxy ProxyEventsInvalidProxy
318 #endif // defined(OS_CHROMEOS)
320 // Tests error events: invalid proxy
321 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, MAYBE_ProxyEventsInvalidProxy
) {
322 ASSERT_TRUE(StartEmbeddedTestServer());
324 RunExtensionSubtest("proxy/events", "invalid_proxy.html")) << message_
;
327 // Tests error events: PAC script parse error.
328 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyEventsParseError
) {
330 RunExtensionSubtest("proxy/events", "parse_error.html")) << message_
;
333 } // namespace extensions