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/prefs/proxy_config_dictionary.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/pref_names.h"
13 #include "extensions/common/extension.h"
15 namespace extensions
{
19 const char kNoServer
[] = "";
20 const char kNoBypass
[] = "";
21 const char kNoPac
[] = "";
25 class ProxySettingsApiTest
: public ExtensionApiTest
{
27 void ValidateSettings(int expected_mode
,
28 const std::string
& expected_server
,
29 const std::string
& bypass
,
30 const std::string
& expected_pac_url
,
31 PrefService
* pref_service
) {
32 const PrefService::Preference
* pref
=
33 pref_service
->FindPreference(prefs::kProxy
);
34 ASSERT_TRUE(pref
!= NULL
);
35 EXPECT_TRUE(pref
->IsExtensionControlled());
37 ProxyConfigDictionary
dict(pref_service
->GetDictionary(prefs::kProxy
));
39 ProxyPrefs::ProxyMode mode
;
40 ASSERT_TRUE(dict
.GetMode(&mode
));
41 EXPECT_EQ(expected_mode
, mode
);
44 if (!bypass
.empty()) {
45 ASSERT_TRUE(dict
.GetBypassList(&value
));
46 EXPECT_EQ(bypass
, value
);
48 EXPECT_FALSE(dict
.GetBypassList(&value
));
51 if (!expected_pac_url
.empty()) {
52 ASSERT_TRUE(dict
.GetPacUrl(&value
));
53 EXPECT_EQ(expected_pac_url
, value
);
55 EXPECT_FALSE(dict
.GetPacUrl(&value
));
58 if (!expected_server
.empty()) {
59 ASSERT_TRUE(dict
.GetProxyServer(&value
));
60 EXPECT_EQ(expected_server
, value
);
62 EXPECT_FALSE(dict
.GetProxyServer(&value
));
66 void ExpectNoSettings(PrefService
* pref_service
) {
67 const PrefService::Preference
* pref
=
68 pref_service
->FindPreference(prefs::kProxy
);
69 ASSERT_TRUE(pref
!= NULL
);
70 EXPECT_FALSE(pref
->IsExtensionControlled());
73 bool SetIsIncognitoEnabled(bool enabled
) {
74 ResultCatcher catcher
;
75 extensions::util::SetIsIncognitoEnabled(
76 GetSingleLoadedExtension()->id(), browser()->profile(), enabled
);
77 if (!catcher
.GetNextResult()) {
78 message_
= catcher
.message();
85 // Tests direct connection settings.
86 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyDirectSettings
) {
87 ASSERT_TRUE(RunExtensionTestIncognito("proxy/direct")) << message_
;
88 const Extension
* extension
= GetSingleLoadedExtension();
89 ASSERT_TRUE(extension
);
91 PrefService
* pref_service
= browser()->profile()->GetPrefs();
92 ValidateSettings(ProxyPrefs::MODE_DIRECT
, kNoServer
, kNoBypass
, kNoPac
,
95 // As the extension is executed with incognito permission, the settings
96 // should propagate to incognito mode.
97 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
98 ValidateSettings(ProxyPrefs::MODE_DIRECT
, kNoServer
, kNoBypass
, kNoPac
,
102 // Tests auto-detect settings.
103 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyAutoSettings
) {
104 ASSERT_TRUE(RunExtensionTestIncognito("proxy/auto")) << message_
;
105 const Extension
* extension
= GetSingleLoadedExtension();
106 ASSERT_TRUE(extension
);
108 PrefService
* pref_service
= browser()->profile()->GetPrefs();
109 ValidateSettings(ProxyPrefs::MODE_AUTO_DETECT
, kNoServer
, kNoBypass
, kNoPac
,
113 // Tests PAC proxy settings.
114 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyPacScript
) {
115 ASSERT_TRUE(RunExtensionTest("proxy/pac")) << message_
;
116 const Extension
* extension
= GetSingleLoadedExtension();
117 ASSERT_TRUE(extension
);
119 PrefService
* pref_service
= browser()->profile()->GetPrefs();
120 ValidateSettings(ProxyPrefs::MODE_PAC_SCRIPT
, kNoServer
, kNoBypass
,
121 "http://wpad/windows.pac", pref_service
);
123 // As the extension is not executed with incognito permission, the settings
124 // should not propagate to incognito mode.
125 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
126 ExpectNoSettings(pref_service
);
128 // Now we enable the extension in incognito mode and verify that settings
130 ASSERT_TRUE(SetIsIncognitoEnabled(true));
131 ValidateSettings(ProxyPrefs::MODE_PAC_SCRIPT
, kNoServer
, kNoBypass
,
132 "http://wpad/windows.pac", pref_service
);
134 // Disabling incognito permission should revoke the settings for incognito
136 ASSERT_TRUE(SetIsIncognitoEnabled(false));
137 ExpectNoSettings(pref_service
);
140 // Tests PAC proxy settings.
141 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyPacDataUrl
) {
142 ASSERT_TRUE(RunExtensionTest("proxy/pacdataurl")) << message_
;
143 const Extension
* extension
= GetSingleLoadedExtension();
144 ASSERT_TRUE(extension
);
146 "data:;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
147 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
148 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
149 PrefService
* pref_service
= browser()->profile()->GetPrefs();
150 ValidateSettings(ProxyPrefs::MODE_PAC_SCRIPT
, kNoServer
, kNoBypass
,
154 // Tests PAC proxy settings.
155 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyPacData
) {
156 ASSERT_TRUE(RunExtensionTest("proxy/pacdata")) << message_
;
157 const Extension
* extension
= GetSingleLoadedExtension();
158 ASSERT_TRUE(extension
);
160 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
161 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
162 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
163 PrefService
* pref_service
= browser()->profile()->GetPrefs();
164 ValidateSettings(ProxyPrefs::MODE_PAC_SCRIPT
, kNoServer
, kNoBypass
,
168 // Tests setting a single proxy to cover all schemes.
169 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyFixedSingle
) {
170 ASSERT_TRUE(RunExtensionTest("proxy/single")) << message_
;
171 const Extension
* extension
= GetSingleLoadedExtension();
172 ASSERT_TRUE(extension
);
174 PrefService
* pref_service
= browser()->profile()->GetPrefs();
175 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
182 // Tests setting to use the system's proxy settings.
183 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxySystem
) {
184 ASSERT_TRUE(RunExtensionTest("proxy/system")) << message_
;
185 const Extension
* extension
= GetSingleLoadedExtension();
186 ASSERT_TRUE(extension
);
188 PrefService
* pref_service
= browser()->profile()->GetPrefs();
189 ValidateSettings(ProxyPrefs::MODE_SYSTEM
, kNoServer
, kNoBypass
, kNoPac
,
193 // Tests setting separate proxies for each scheme.
194 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyFixedIndividual
) {
195 ASSERT_TRUE(RunExtensionTestIncognito("proxy/individual")) << message_
;
196 const Extension
* extension
= GetSingleLoadedExtension();
197 ASSERT_TRUE(extension
);
199 PrefService
* pref_service
= browser()->profile()->GetPrefs();
200 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
201 "http=quic://1.1.1.1:443;"
202 "https=2.2.2.2:80;" // http:// is pruned.
203 "ftp=3.3.3.3:9000;" // http:// is pruned.
204 "socks=socks4://4.4.4.4:9090",
209 // Now check the incognito preferences.
210 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
211 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
212 "http=quic://1.1.1.1:443;"
215 "socks=socks4://4.4.4.4:9090",
221 // Tests setting values only for incognito mode
222 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
,
223 ProxyFixedIndividualIncognitoOnly
) {
224 ASSERT_TRUE(RunExtensionTestIncognito("proxy/individual_incognito_only")) <<
226 const Extension
* extension
= GetSingleLoadedExtension();
227 ASSERT_TRUE(extension
);
229 PrefService
* pref_service
= browser()->profile()->GetPrefs();
230 ExpectNoSettings(pref_service
);
232 // Now check the incognito preferences.
233 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
234 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
236 "https=socks5://2.2.2.2:1080;"
238 "socks=socks4://4.4.4.4:9090",
244 // Tests setting values also for incognito mode
245 // Test disabled due to http://crbug.com/88972.
246 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
,
247 DISABLED_ProxyFixedIndividualIncognitoAlso
) {
248 ASSERT_TRUE(RunExtensionTestIncognito("proxy/individual_incognito_also")) <<
250 const Extension
* extension
= GetSingleLoadedExtension();
251 ASSERT_TRUE(extension
);
253 PrefService
* pref_service
= browser()->profile()->GetPrefs();
254 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
256 "https=socks5://2.2.2.2:1080;"
258 "socks=socks4://4.4.4.4:9090",
263 // Now check the incognito preferences.
264 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
265 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
267 "https=socks5://6.6.6.6:1080;"
269 "socks=socks4://8.8.8.8:9090",
275 // Tests setting and unsetting values
276 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyFixedIndividualRemove
) {
277 ASSERT_TRUE(RunExtensionTest("proxy/individual_remove")) << message_
;
278 const Extension
* extension
= GetSingleLoadedExtension();
279 ASSERT_TRUE(extension
);
281 PrefService
* pref_service
= browser()->profile()->GetPrefs();
282 ExpectNoSettings(pref_service
);
285 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
,
287 ASSERT_TRUE(RunExtensionTestIncognito("proxy/bypass")) << message_
;
288 const Extension
* extension
= GetSingleLoadedExtension();
289 ASSERT_TRUE(extension
);
291 PrefService
* pref_service
= browser()->profile()->GetPrefs();
292 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
294 "localhost,::1,foo.bar,<local>",
298 // Now check the incognito preferences.
299 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
300 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
302 "localhost,::1,foo.bar,<local>",
307 // This test sets proxy to an inavalid host "does.not.exist" and then fetches
308 // a page from localhost, expecting an error since host is invalid.
309 // On ChromeOS, localhost is by default bypassed, so the page from localhost
310 // will be fetched successfully, resulting in no error. Hence this test
311 // shouldn't run on ChromeOS.
312 #if defined(OS_CHROMEOS)
313 #define MAYBE_ProxyEventsInvalidProxy DISABLED_ProxyEventsInvalidProxy
315 #define MAYBE_ProxyEventsInvalidProxy ProxyEventsInvalidProxy
316 #endif // defined(OS_CHROMEOS)
318 // Tests error events: invalid proxy
319 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, MAYBE_ProxyEventsInvalidProxy
) {
320 ASSERT_TRUE(StartEmbeddedTestServer());
322 RunExtensionSubtest("proxy/events", "invalid_proxy.html")) << message_
;
325 // Tests error events: PAC script parse error.
326 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyEventsParseError
) {
328 RunExtensionSubtest("proxy/events", "parse_error.html")) << message_
;
331 } // namespace extensions