Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / web_request / web_request_apitest.cc
blob410c54ef6f6d32558436fa451a4fb6de83e33f58
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/command_line.h"
6 #include "chrome/browser/chrome_notification_types.h"
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/login/login_prompt.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "extensions/browser/api/web_request/web_request_api.h"
20 #include "extensions/browser/extension_system.h"
21 #include "extensions/common/extension_builder.h"
22 #include "extensions/common/features/feature.h"
23 #include "extensions/test/extension_test_message_listener.h"
24 #include "extensions/test/result_catcher.h"
25 #include "net/dns/mock_host_resolver.h"
26 #include "net/test/embedded_test_server/embedded_test_server.h"
27 #include "third_party/WebKit/public/web/WebInputEvent.h"
29 using content::WebContents;
30 using extensions::Feature;
31 using extensions::ResultCatcher;
33 namespace {
35 class CancelLoginDialog : public content::NotificationObserver {
36 public:
37 CancelLoginDialog() {
38 registrar_.Add(this,
39 chrome::NOTIFICATION_AUTH_NEEDED,
40 content::NotificationService::AllSources());
43 ~CancelLoginDialog() override {}
45 void Observe(int type,
46 const content::NotificationSource& source,
47 const content::NotificationDetails& details) override {
48 LoginHandler* handler =
49 content::Details<LoginNotificationDetails>(details).ptr()->handler();
50 handler->CancelAuth();
53 private:
54 content::NotificationRegistrar registrar_;
56 DISALLOW_COPY_AND_ASSIGN(CancelLoginDialog);
59 } // namespace
61 class ExtensionWebRequestApiTest : public ExtensionApiTest {
62 public:
63 void SetUpInProcessBrowserTestFixture() override {
64 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
65 host_resolver()->AddRule("*", "127.0.0.1");
68 void RunPermissionTest(
69 const char* extension_directory,
70 bool load_extension_with_incognito_permission,
71 bool wait_for_extension_loaded_in_incognito,
72 const char* expected_content_regular_window,
73 const char* exptected_content_incognito_window);
76 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebRequestApi) {
77 ASSERT_TRUE(StartEmbeddedTestServer());
78 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_api.html")) << message_;
81 // Fails often on Windows dbg bots. http://crbug.com/177163
82 #if defined(OS_WIN)
83 #define MAYBE_WebRequestSimple DISABLED_WebRequestSimple
84 #else
85 #define MAYBE_WebRequestSimple WebRequestSimple
86 #endif // defined(OS_WIN)
87 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, MAYBE_WebRequestSimple) {
88 ASSERT_TRUE(StartEmbeddedTestServer());
89 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_simple.html")) <<
90 message_;
93 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebRequestComplex) {
94 ASSERT_TRUE(StartEmbeddedTestServer());
95 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_complex.html")) <<
96 message_;
99 // Flaky (sometimes crash): http://crbug.com/140976
100 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
101 DISABLED_WebRequestAuthRequired) {
102 CancelLoginDialog login_dialog_helper;
104 ASSERT_TRUE(StartEmbeddedTestServer());
105 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_auth_required.html")) <<
106 message_;
109 // This test times out regularly on win_rel trybots. See http://crbug.com/122178
110 #if defined(OS_WIN)
111 #define MAYBE_WebRequestBlocking DISABLED_WebRequestBlocking
112 #else
113 #define MAYBE_WebRequestBlocking WebRequestBlocking
114 #endif
115 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, MAYBE_WebRequestBlocking) {
116 ASSERT_TRUE(StartEmbeddedTestServer());
117 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_blocking.html")) <<
118 message_;
121 // Fails often on Windows dbg bots. http://crbug.com/177163
122 #if defined(OS_WIN)
123 #define MAYBE_WebRequestNewTab DISABLED_WebRequestNewTab
124 #else
125 #define MAYBE_WebRequestNewTab WebRequestNewTab
126 #endif // defined(OS_WIN)
127 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, MAYBE_WebRequestNewTab) {
128 ASSERT_TRUE(StartEmbeddedTestServer());
129 // Wait for the extension to set itself up and return control to us.
130 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_newTab.html"))
131 << message_;
133 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
134 content::WaitForLoadStop(tab);
136 ResultCatcher catcher;
138 ExtensionService* service = extensions::ExtensionSystem::Get(
139 browser()->profile())->extension_service();
140 const extensions::Extension* extension =
141 service->GetExtensionById(last_loaded_extension_id(), false);
142 GURL url = extension->GetResourceURL("newTab/a.html");
144 ui_test_utils::NavigateToURL(browser(), url);
146 // There's a link on a.html with target=_blank. Click on it to open it in a
147 // new tab.
148 blink::WebMouseEvent mouse_event;
149 mouse_event.type = blink::WebInputEvent::MouseDown;
150 mouse_event.button = blink::WebMouseEvent::ButtonLeft;
151 mouse_event.x = 7;
152 mouse_event.y = 7;
153 mouse_event.clickCount = 1;
154 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
155 mouse_event.type = blink::WebInputEvent::MouseUp;
156 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
158 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
161 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebRequestDeclarative1) {
162 ASSERT_TRUE(StartEmbeddedTestServer());
163 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_declarative1.html"))
164 << message_;
167 // This test times out on XP. See http://crbug.com/178296
168 #if defined(OS_WIN)
169 #define MAYBE_WebRequestDeclarative2 DISABLED_WebRequestDeclarative2
170 #else
171 #define MAYBE_WebRequestDeclarative2 WebRequestDeclarative2
172 #endif
173 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
174 MAYBE_WebRequestDeclarative2) {
175 ASSERT_TRUE(StartEmbeddedTestServer());
176 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_declarative2.html"))
177 << message_;
180 void ExtensionWebRequestApiTest::RunPermissionTest(
181 const char* extension_directory,
182 bool load_extension_with_incognito_permission,
183 bool wait_for_extension_loaded_in_incognito,
184 const char* expected_content_regular_window,
185 const char* exptected_content_incognito_window) {
186 ResultCatcher catcher;
187 catcher.RestrictToBrowserContext(browser()->profile());
188 ResultCatcher catcher_incognito;
189 catcher_incognito.RestrictToBrowserContext(
190 browser()->profile()->GetOffTheRecordProfile());
192 ExtensionTestMessageListener listener("done", true);
193 ExtensionTestMessageListener listener_incognito("done_incognito", true);
195 int load_extension_flags = kFlagNone;
196 if (load_extension_with_incognito_permission)
197 load_extension_flags |= kFlagEnableIncognito;
198 ASSERT_TRUE(LoadExtensionWithFlags(
199 test_data_dir_.AppendASCII("webrequest_permissions")
200 .AppendASCII(extension_directory),
201 load_extension_flags));
203 // Test that navigation in regular window is properly redirected.
204 EXPECT_TRUE(listener.WaitUntilSatisfied());
206 // This navigation should be redirected.
207 ui_test_utils::NavigateToURL(
208 browser(),
209 embedded_test_server()->GetURL("/extensions/test_file.html"));
211 std::string body;
212 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
213 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
214 tab,
215 "window.domAutomationController.send(document.body.textContent)",
216 &body));
217 EXPECT_EQ(expected_content_regular_window, body);
219 // Test that navigation in OTR window is properly redirected.
220 Browser* otr_browser =
221 OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
223 if (wait_for_extension_loaded_in_incognito)
224 EXPECT_TRUE(listener_incognito.WaitUntilSatisfied());
226 // This navigation should be redirected if
227 // load_extension_with_incognito_permission is true.
228 ui_test_utils::NavigateToURL(
229 otr_browser,
230 embedded_test_server()->GetURL("/extensions/test_file.html"));
232 body.clear();
233 WebContents* otr_tab = otr_browser->tab_strip_model()->GetActiveWebContents();
234 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
235 otr_tab,
236 "window.domAutomationController.send(document.body.textContent)",
237 &body));
238 EXPECT_EQ(exptected_content_incognito_window, body);
241 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
242 WebRequestDeclarativePermissionSpanning1) {
243 // Test spanning with incognito permission.
244 ASSERT_TRUE(StartEmbeddedTestServer());
245 RunPermissionTest("spanning", true, false, "redirected1", "redirected1");
248 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
249 WebRequestDeclarativePermissionSpanning2) {
250 // Test spanning without incognito permission.
251 ASSERT_TRUE(StartEmbeddedTestServer());
252 RunPermissionTest("spanning", false, false, "redirected1", "");
256 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
257 WebRequestDeclarativePermissionSplit1) {
258 // Test split with incognito permission.
259 ASSERT_TRUE(StartEmbeddedTestServer());
260 RunPermissionTest("split", true, true, "redirected1", "redirected2");
263 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
264 WebRequestDeclarativePermissionSplit2) {
265 // Test split without incognito permission.
266 ASSERT_TRUE(StartEmbeddedTestServer());
267 RunPermissionTest("split", false, false, "redirected1", "");
270 // TODO(vabr): Cure these flaky tests, http://crbug.com/238179.
271 #if !defined(NDEBUG)
272 #define MAYBE_PostData1 DISABLED_PostData1
273 #define MAYBE_PostData2 DISABLED_PostData2
274 #else
275 #define MAYBE_PostData1 PostData1
276 #define MAYBE_PostData2 PostData2
277 #endif
278 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, MAYBE_PostData1) {
279 // Test HTML form POST data access with the default and "url" encoding.
280 ASSERT_TRUE(StartEmbeddedTestServer());
281 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_post1.html")) <<
282 message_;
285 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, MAYBE_PostData2) {
286 // Test HTML form POST data access with the multipart and plaintext encoding.
287 ASSERT_TRUE(StartEmbeddedTestServer());
288 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_post2.html")) <<
289 message_;
292 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
293 DeclarativeSendMessage) {
294 ASSERT_TRUE(StartEmbeddedTestServer());
295 ASSERT_TRUE(RunExtensionTest("webrequest_sendmessage")) << message_;
298 // Check that reloading an extension that runs in incognito split mode and
299 // has two active background pages with registered events does not crash the
300 // browser. Regression test for http://crbug.com/224094
301 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, IncognitoSplitModeReload) {
302 ASSERT_TRUE(StartEmbeddedTestServer());
303 // Wait for rules to be set up.
304 ExtensionTestMessageListener listener("done", true);
305 ExtensionTestMessageListener listener_incognito("done_incognito", true);
307 const extensions::Extension* extension = LoadExtensionWithFlags(
308 test_data_dir_.AppendASCII("webrequest_reload"),
309 kFlagEnableIncognito);
310 ASSERT_TRUE(extension);
311 OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
313 EXPECT_TRUE(listener.WaitUntilSatisfied());
314 EXPECT_TRUE(listener_incognito.WaitUntilSatisfied());
316 // Reload extension and wait for rules to be set up again. This should not
317 // crash the browser.
318 ExtensionTestMessageListener listener2("done", true);
319 ExtensionTestMessageListener listener_incognito2("done_incognito", true);
321 ReloadExtension(extension->id());
323 EXPECT_TRUE(listener2.WaitUntilSatisfied());
324 EXPECT_TRUE(listener_incognito2.WaitUntilSatisfied());
327 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, ExtensionRequests) {
328 ASSERT_TRUE(StartEmbeddedTestServer());
329 ExtensionTestMessageListener listener_main1("web_request_status1", true);
330 ExtensionTestMessageListener listener_main2("web_request_status2", true);
332 ExtensionTestMessageListener listener_app("app_done", false);
333 ExtensionTestMessageListener listener_extension("extension_done", true);
335 // Set up webRequest listener
336 ASSERT_TRUE(LoadExtension(
337 test_data_dir_.AppendASCII("webrequest_extensions/main")));
338 EXPECT_TRUE(listener_main1.WaitUntilSatisfied());
339 EXPECT_TRUE(listener_main2.WaitUntilSatisfied());
341 // Perform some network activity in an app and another extension.
342 ASSERT_TRUE(LoadExtension(
343 test_data_dir_.AppendASCII("webrequest_extensions/app")));
344 ASSERT_TRUE(LoadExtension(
345 test_data_dir_.AppendASCII("webrequest_extensions/extension")));
347 EXPECT_TRUE(listener_app.WaitUntilSatisfied());
348 EXPECT_TRUE(listener_extension.WaitUntilSatisfied());
350 // Load a page, a content script will ping us when it is ready.
351 ExtensionTestMessageListener listener_pageready("contentscript_ready", true);
352 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL(
353 "/extensions/test_file.html?match_webrequest_test"));
354 EXPECT_TRUE(listener_pageready.WaitUntilSatisfied());
356 // The extension and app-generated requests should not have triggered any
357 // webRequest event filtered by type 'xmlhttprequest'.
358 // (check this here instead of before the navigation, in case the webRequest
359 // event routing is slow for some reason).
360 ExtensionTestMessageListener listener_result(false);
361 listener_main1.Reply("");
362 EXPECT_TRUE(listener_result.WaitUntilSatisfied());
363 EXPECT_EQ("Did not intercept any requests.", listener_result.message());
365 ExtensionTestMessageListener listener_contentscript("contentscript_done",
366 false);
367 ExtensionTestMessageListener listener_framescript("framescript_done", false);
369 // Proceed with the final tests: Let the content script fire a request and
370 // then load an iframe which also fires a XHR request.
371 listener_pageready.Reply("");
372 EXPECT_TRUE(listener_contentscript.WaitUntilSatisfied());
373 EXPECT_TRUE(listener_framescript.WaitUntilSatisfied());
375 // Collect the visited URLs. The content script and subframe does not run in
376 // the extension's process, so the requests should be visible to the main
377 // extension.
378 listener_result.Reset();
379 listener_main2.Reply("");
380 EXPECT_TRUE(listener_result.WaitUntilSatisfied());
381 if (content::AreAllSitesIsolatedForTesting()) {
382 // With --site-per-process, the extension frame does run in the extension's
383 // process.
384 EXPECT_EQ("Intercepted requests: ?contentscript",
385 listener_result.message());
386 } else {
387 EXPECT_EQ("Intercepted requests: ?contentscript, ?framescript",
388 listener_result.message());
392 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, HostedAppRequest) {
393 ASSERT_TRUE(StartEmbeddedTestServer());
394 GURL hosted_app_url(
395 embedded_test_server()->GetURL(
396 "/extensions/api_test/webrequest_hosted_app/index.html"));
397 scoped_refptr<extensions::Extension> hosted_app =
398 extensions::ExtensionBuilder()
399 .SetManifest(extensions::DictionaryBuilder()
400 .Set("name", "Some hosted app")
401 .Set("version", "1")
402 .Set("manifest_version", 2)
403 .Set("app", extensions::DictionaryBuilder()
404 .Set("launch", extensions::DictionaryBuilder()
405 .Set("web_url", hosted_app_url.spec()))))
406 .Build();
407 extensions::ExtensionSystem::Get(browser()->profile())->extension_service()
408 ->AddExtension(hosted_app.get());
410 ExtensionTestMessageListener listener1("main_frame", false);
411 ExtensionTestMessageListener listener2("xmlhttprequest", false);
413 ASSERT_TRUE(LoadExtension(
414 test_data_dir_.AppendASCII("webrequest_hosted_app")));
416 ui_test_utils::NavigateToURL(browser(), hosted_app_url);
418 EXPECT_TRUE(listener1.WaitUntilSatisfied());
419 EXPECT_TRUE(listener2.WaitUntilSatisfied());