Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / proxy / proxy_apitest.cc
blob740efa54e8567c8561441bf8901281dadac018b0
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 {
18 namespace {
20 const char kNoServer[] = "";
21 const char kNoBypass[] = "";
22 const char kNoPac[] = "";
24 } // namespace
26 class ProxySettingsApiTest : public ExtensionApiTest {
27 protected:
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);
45 std::string value;
46 if (!bypass.empty()) {
47 ASSERT_TRUE(dict.GetBypassList(&value));
48 EXPECT_EQ(bypass, value);
49 } else {
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);
56 } else {
57 EXPECT_FALSE(dict.GetPacUrl(&value));
60 if (!expected_server.empty()) {
61 ASSERT_TRUE(dict.GetProxyServer(&value));
62 EXPECT_EQ(expected_server, value);
63 } else {
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();
81 return false;
83 return true;
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,
95 pref_service);
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,
101 pref_service);
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,
112 pref_service);
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
131 // are applied.
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
137 // mode.
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);
147 const char url[] =
148 "data:;base64,ZnVuY3Rpb24gRmluZFByb3h5R"
149 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl"
150 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0=";
151 PrefService* pref_service = browser()->profile()->GetPrefs();
152 ValidateSettings(ProxyPrefs::MODE_PAC_SCRIPT, kNoServer, kNoBypass,
153 url, pref_service);
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);
161 const char url[] =
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,
167 url, pref_service);
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,
178 "127.0.0.1:100",
179 kNoBypass,
180 kNoPac,
181 pref_service);
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,
192 pref_service);
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",
207 kNoBypass,
208 kNoPac,
209 pref_service);
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;"
215 "https=2.2.2.2:80;"
216 "ftp=3.3.3.3:9000;"
217 "socks=socks4://4.4.4.4:9090",
218 kNoBypass,
219 kNoPac,
220 pref_service);
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")) <<
227 message_;
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,
237 "http=1.1.1.1:80;"
238 "https=socks5://2.2.2.2:1080;"
239 "ftp=3.3.3.3:9000;"
240 "socks=socks4://4.4.4.4:9090",
241 kNoBypass,
242 kNoPac,
243 pref_service);
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")) <<
251 message_;
252 const Extension* extension = GetSingleLoadedExtension();
253 ASSERT_TRUE(extension);
255 PrefService* pref_service = browser()->profile()->GetPrefs();
256 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS,
257 "http=1.1.1.1:80;"
258 "https=socks5://2.2.2.2:1080;"
259 "ftp=3.3.3.3:9000;"
260 "socks=socks4://4.4.4.4:9090",
261 kNoBypass,
262 kNoPac,
263 pref_service);
265 // Now check the incognito preferences.
266 pref_service = browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
267 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS,
268 "http=5.5.5.5:80;"
269 "https=socks5://6.6.6.6:1080;"
270 "ftp=7.7.7.7:9000;"
271 "socks=socks4://8.8.8.8:9090",
272 kNoBypass,
273 kNoPac,
274 pref_service);
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,
288 ProxyBypass) {
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,
295 "http=1.1.1.1:80",
296 "localhost,::1,foo.bar,<local>",
297 kNoPac,
298 pref_service);
300 // Now check the incognito preferences.
301 pref_service = browser()->profile()->GetOffTheRecordProfile()->GetPrefs();
302 ValidateSettings(ProxyPrefs::MODE_FIXED_SERVERS,
303 "http=1.1.1.1:80",
304 "localhost,::1,foo.bar,<local>",
305 kNoPac,
306 pref_service);
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
316 #else
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());
323 ASSERT_TRUE(
324 RunExtensionSubtest("proxy/events", "invalid_proxy.html")) << message_;
327 // Tests error events: PAC script parse error.
328 IN_PROC_BROWSER_TEST_F(ProxySettingsApiTest, ProxyEventsParseError) {
329 ASSERT_TRUE(
330 RunExtensionSubtest("proxy/events", "parse_error.html")) << message_;
333 } // namespace extensions