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 "chrome/common/pref_names.h"
12 #include "components/proxy_config/proxy_config_dictionary.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(prefs::kProxy
);
35 ASSERT_TRUE(pref
!= NULL
);
36 EXPECT_TRUE(pref
->IsExtensionControlled());
38 ProxyConfigDictionary
dict(pref_service
->GetDictionary(prefs::kProxy
));
40 ProxyPrefs::ProxyMode mode
;
41 ASSERT_TRUE(dict
.GetMode(&mode
));
42 EXPECT_EQ(expected_mode
, mode
);
45 if (!bypass
.empty()) {
46 ASSERT_TRUE(dict
.GetBypassList(&value
));
47 EXPECT_EQ(bypass
, value
);
49 EXPECT_FALSE(dict
.GetBypassList(&value
));
52 if (!expected_pac_url
.empty()) {
53 ASSERT_TRUE(dict
.GetPacUrl(&value
));
54 EXPECT_EQ(expected_pac_url
, value
);
56 EXPECT_FALSE(dict
.GetPacUrl(&value
));
59 if (!expected_server
.empty()) {
60 ASSERT_TRUE(dict
.GetProxyServer(&value
));
61 EXPECT_EQ(expected_server
, value
);
63 EXPECT_FALSE(dict
.GetProxyServer(&value
));
67 void ExpectNoSettings(PrefService
* pref_service
) {
68 const PrefService::Preference
* pref
=
69 pref_service
->FindPreference(prefs::kProxy
);
70 ASSERT_TRUE(pref
!= NULL
);
71 EXPECT_FALSE(pref
->IsExtensionControlled());
74 bool SetIsIncognitoEnabled(bool enabled
) {
75 ResultCatcher catcher
;
76 extensions::util::SetIsIncognitoEnabled(
77 GetSingleLoadedExtension()->id(), browser()->profile(), enabled
);
78 if (!catcher
.GetNextResult()) {
79 message_
= catcher
.message();
86 // Tests direct connection settings.
87 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyDirectSettings
) {
88 ASSERT_TRUE(RunExtensionTestIncognito("proxy/direct")) << message_
;
89 const Extension
* extension
= GetSingleLoadedExtension();
90 ASSERT_TRUE(extension
);
92 PrefService
* pref_service
= browser()->profile()->GetPrefs();
93 ValidateSettings(ProxyPrefs::MODE_DIRECT
, kNoServer
, kNoBypass
, kNoPac
,
96 // As the extension is executed with incognito permission, the settings
97 // should propagate to incognito mode.
98 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
99 ValidateSettings(ProxyPrefs::MODE_DIRECT
, kNoServer
, kNoBypass
, kNoPac
,
103 // Tests auto-detect settings.
104 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyAutoSettings
) {
105 ASSERT_TRUE(RunExtensionTestIncognito("proxy/auto")) << message_
;
106 const Extension
* extension
= GetSingleLoadedExtension();
107 ASSERT_TRUE(extension
);
109 PrefService
* pref_service
= browser()->profile()->GetPrefs();
110 ValidateSettings(ProxyPrefs::MODE_AUTO_DETECT
, kNoServer
, kNoBypass
, kNoPac
,
114 // Tests PAC proxy settings.
115 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyPacScript
) {
116 ASSERT_TRUE(RunExtensionTest("proxy/pac")) << message_
;
117 const Extension
* extension
= GetSingleLoadedExtension();
118 ASSERT_TRUE(extension
);
120 PrefService
* pref_service
= browser()->profile()->GetPrefs();
121 ValidateSettings(ProxyPrefs::MODE_PAC_SCRIPT
, kNoServer
, kNoBypass
,
122 "http://wpad/windows.pac", pref_service
);
124 // As the extension is not executed with incognito permission, the settings
125 // should not propagate to incognito mode.
126 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
127 ExpectNoSettings(pref_service
);
129 // Now we enable the extension in incognito mode and verify that settings
131 ASSERT_TRUE(SetIsIncognitoEnabled(true));
132 ValidateSettings(ProxyPrefs::MODE_PAC_SCRIPT
, kNoServer
, kNoBypass
,
133 "http://wpad/windows.pac", pref_service
);
135 // Disabling incognito permission should revoke the settings for incognito
137 ASSERT_TRUE(SetIsIncognitoEnabled(false));
138 ExpectNoSettings(pref_service
);
141 // Tests PAC proxy settings.
142 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyPacDataUrl
) {
143 ASSERT_TRUE(RunExtensionTest("proxy/pacdataurl")) << message_
;
144 const Extension
* extension
= GetSingleLoadedExtension();
145 ASSERT_TRUE(extension
);
147 "data:;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
148 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
149 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
150 PrefService
* pref_service
= browser()->profile()->GetPrefs();
151 ValidateSettings(ProxyPrefs::MODE_PAC_SCRIPT
, kNoServer
, kNoBypass
,
155 // Tests PAC proxy settings.
156 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyPacData
) {
157 ASSERT_TRUE(RunExtensionTest("proxy/pacdata")) << message_
;
158 const Extension
* extension
= GetSingleLoadedExtension();
159 ASSERT_TRUE(extension
);
161 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
162 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
163 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
164 PrefService
* pref_service
= browser()->profile()->GetPrefs();
165 ValidateSettings(ProxyPrefs::MODE_PAC_SCRIPT
, kNoServer
, kNoBypass
,
169 // Tests setting a single proxy to cover all schemes.
170 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyFixedSingle
) {
171 ASSERT_TRUE(RunExtensionTest("proxy/single")) << message_
;
172 const Extension
* extension
= GetSingleLoadedExtension();
173 ASSERT_TRUE(extension
);
175 PrefService
* pref_service
= browser()->profile()->GetPrefs();
176 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
183 // Tests setting to use the system's proxy settings.
184 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxySystem
) {
185 ASSERT_TRUE(RunExtensionTest("proxy/system")) << message_
;
186 const Extension
* extension
= GetSingleLoadedExtension();
187 ASSERT_TRUE(extension
);
189 PrefService
* pref_service
= browser()->profile()->GetPrefs();
190 ValidateSettings(ProxyPrefs::MODE_SYSTEM
, kNoServer
, kNoBypass
, kNoPac
,
194 // Tests setting separate proxies for each scheme.
195 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyFixedIndividual
) {
196 ASSERT_TRUE(RunExtensionTestIncognito("proxy/individual")) << message_
;
197 const Extension
* extension
= GetSingleLoadedExtension();
198 ASSERT_TRUE(extension
);
200 PrefService
* pref_service
= browser()->profile()->GetPrefs();
201 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
202 "http=quic://1.1.1.1:443;"
203 "https=2.2.2.2:80;" // http:// is pruned.
204 "ftp=3.3.3.3:9000;" // http:// is pruned.
205 "socks=socks4://4.4.4.4:9090",
210 // Now check the incognito preferences.
211 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
212 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
213 "http=quic://1.1.1.1:443;"
216 "socks=socks4://4.4.4.4:9090",
222 // Tests setting values only for incognito mode
223 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
,
224 ProxyFixedIndividualIncognitoOnly
) {
225 ASSERT_TRUE(RunExtensionTestIncognito("proxy/individual_incognito_only")) <<
227 const Extension
* extension
= GetSingleLoadedExtension();
228 ASSERT_TRUE(extension
);
230 PrefService
* pref_service
= browser()->profile()->GetPrefs();
231 ExpectNoSettings(pref_service
);
233 // Now check the incognito preferences.
234 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
235 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
237 "https=socks5://2.2.2.2:1080;"
239 "socks=socks4://4.4.4.4:9090",
245 // Tests setting values also for incognito mode
246 // Test disabled due to http://crbug.com/88972.
247 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
,
248 DISABLED_ProxyFixedIndividualIncognitoAlso
) {
249 ASSERT_TRUE(RunExtensionTestIncognito("proxy/individual_incognito_also")) <<
251 const Extension
* extension
= GetSingleLoadedExtension();
252 ASSERT_TRUE(extension
);
254 PrefService
* pref_service
= browser()->profile()->GetPrefs();
255 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
257 "https=socks5://2.2.2.2:1080;"
259 "socks=socks4://4.4.4.4:9090",
264 // Now check the incognito preferences.
265 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
266 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
268 "https=socks5://6.6.6.6:1080;"
270 "socks=socks4://8.8.8.8:9090",
276 // Tests setting and unsetting values
277 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyFixedIndividualRemove
) {
278 ASSERT_TRUE(RunExtensionTest("proxy/individual_remove")) << message_
;
279 const Extension
* extension
= GetSingleLoadedExtension();
280 ASSERT_TRUE(extension
);
282 PrefService
* pref_service
= browser()->profile()->GetPrefs();
283 ExpectNoSettings(pref_service
);
286 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
,
288 ASSERT_TRUE(RunExtensionTestIncognito("proxy/bypass")) << message_
;
289 const Extension
* extension
= GetSingleLoadedExtension();
290 ASSERT_TRUE(extension
);
292 PrefService
* pref_service
= browser()->profile()->GetPrefs();
293 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
295 "localhost,::1,foo.bar,<local>",
299 // Now check the incognito preferences.
300 pref_service
= browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
301 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS
,
303 "localhost,::1,foo.bar,<local>",
308 // This test sets proxy to an inavalid host "does.not.exist" and then fetches
309 // a page from localhost, expecting an error since host is invalid.
310 // On ChromeOS, localhost is by default bypassed, so the page from localhost
311 // will be fetched successfully, resulting in no error. Hence this test
312 // shouldn't run on ChromeOS.
313 #if defined(OS_CHROMEOS)
314 #define MAYBE_ProxyEventsInvalidProxy DISABLED_ProxyEventsInvalidProxy
316 #define MAYBE_ProxyEventsInvalidProxy ProxyEventsInvalidProxy
317 #endif // defined(OS_CHROMEOS)
319 // Tests error events: invalid proxy
320 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, MAYBE_ProxyEventsInvalidProxy
) {
321 ASSERT_TRUE(StartEmbeddedTestServer());
323 RunExtensionSubtest("proxy/events", "invalid_proxy.html")) << message_
;
326 // Tests error events: PAC script parse error.
327 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest
, ProxyEventsParseError
) {
329 RunExtensionSubtest("proxy/events", "parse_error.html")) << message_
;
332 } // namespace extensions