blob: 19afb5ce8d47eb93e5fc526cda7de12084b661f8 [file] [log] [blame]
[email protected]fad73672012-06-15 23:26:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c80b8ee2011-12-03 04:26:522// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Contains holistic tests of the bindings infrastructure
6
[email protected]7eef3942013-08-14 02:53:497#include "chrome/browser/extensions/api/permissions/permissions_api.h"
[email protected]c80b8ee2011-12-03 04:26:528#include "chrome/browser/extensions/extension_apitest.h"
jochen7923c2a2015-07-14 10:04:459#include "chrome/browser/net/url_request_mock_util.h"
[email protected]fad73672012-06-15 23:26:0610#include "chrome/browser/ui/browser.h"
rdevlin.cronin83a4b3a2015-10-28 21:43:5811#include "chrome/browser/ui/tabs/tab_strip_model.h"
asargent79b64c32016-08-04 17:17:1412#include "chrome/common/chrome_switches.h"
rdevlin.cronin83a4b3a2015-10-28 21:43:5813#include "chrome/test/base/ui_test_utils.h"
jochen7923c2a2015-07-14 10:04:4514#include "content/public/browser/browser_thread.h"
[email protected]7d478cb2012-07-24 17:19:4215#include "content/public/test/browser_test_utils.h"
[email protected]22401dc2014-03-21 01:38:5716#include "extensions/browser/extension_host.h"
[email protected]98b6d942013-11-10 00:34:0717#include "extensions/browser/process_manager.h"
lfg910f2f92014-09-19 05:31:0918#include "extensions/test/extension_test_message_listener.h"
yoze8dc2f12014-09-09 23:16:3219#include "extensions/test/result_catcher.h"
rdevlin.cronine6e20022017-06-13 18:23:4020#include "net/dns/mock_host_resolver.h"
rdevlin.cronin83a4b3a2015-10-28 21:43:5821#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]fad73672012-06-15 23:26:0622
[email protected]adafe5b2013-08-09 10:35:0423namespace extensions {
24namespace {
25
jochen7923c2a2015-07-14 10:04:4526class ExtensionBindingsApiTest : public ExtensionApiTest {
27 public:
28 void SetUpOnMainThread() override {
rdevlin.cronin17160cc62016-11-23 05:33:0829 ExtensionApiTest::SetUpOnMainThread();
rdevlin.cronine6e20022017-06-13 18:23:4030 host_resolver()->AddRule("*", "127.0.0.1");
31 ASSERT_TRUE(StartEmbeddedTestServer());
jochen7923c2a2015-07-14 10:04:4532 }
33};
[email protected]adafe5b2013-08-09 10:35:0434
35IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest,
[email protected]7eef3942013-08-14 02:53:4936 UnavailableBindingsNeverRegistered) {
37 // Test will request the 'storage' permission.
38 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true);
39 ASSERT_TRUE(RunExtensionTest(
40 "bindings/unavailable_bindings_never_registered")) << message_;
41}
42
43IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest,
[email protected]adafe5b2013-08-09 10:35:0444 ExceptionInHandlerShouldNotCrash) {
[email protected]c80b8ee2011-12-03 04:26:5245 ASSERT_TRUE(RunExtensionSubtest(
46 "bindings/exception_in_handler_should_not_crash",
47 "page.html")) << message_;
48}
[email protected]fad73672012-06-15 23:26:0649
50// Tests that an error raised during an async function still fires
[email protected]754ea8b72013-01-08 15:10:3151// the callback, but sets chrome.runtime.lastError.
[email protected]fc034482013-08-09 20:25:1452// FIXME should be in ExtensionBindingsApiTest.
[email protected]fad73672012-06-15 23:26:0653IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, LastError) {
54 ASSERT_TRUE(LoadExtension(
55 test_data_dir_.AppendASCII("browsertest").AppendASCII("last_error")));
56
57 // Get the ExtensionHost that is hosting our background page.
[email protected]98b6d942013-11-10 00:34:0758 extensions::ProcessManager* manager =
reillyg0ea3fa902014-10-28 15:30:2359 extensions::ProcessManager::Get(browser()->profile());
[email protected]3a1dc572012-07-31 22:25:1360 extensions::ExtensionHost* host = FindHostWithPath(manager, "/bg.html", 1);
[email protected]fad73672012-06-15 23:26:0661
62 bool result = false;
[email protected]b6987e02013-01-04 18:30:4363 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
64 host->render_view_host(), "testLastError()", &result));
[email protected]fad73672012-06-15 23:26:0665 EXPECT_TRUE(result);
66}
[email protected]52eafbd2013-04-03 04:43:1967
[email protected]adafe5b2013-08-09 10:35:0468// Regression test that we don't delete our own bindings with about:blank
69// iframes.
70IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, AboutBlankIframe) {
71 ResultCatcher catcher;
72 ExtensionTestMessageListener listener("load", true);
73
74 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("bindings")
75 .AppendASCII("about_blank_iframe")));
76
77 ASSERT_TRUE(listener.WaitUntilSatisfied());
78
79 const Extension* extension = LoadExtension(
80 test_data_dir_.AppendASCII("bindings")
81 .AppendASCII("internal_apis_not_on_chrome_object"));
82 ASSERT_TRUE(extension);
83 listener.Reply(extension->id());
84
85 ASSERT_TRUE(catcher.GetNextResult()) << message_;
86}
87
88IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest,
89 InternalAPIsNotOnChromeObject) {
[email protected]52eafbd2013-04-03 04:43:1990 ASSERT_TRUE(RunExtensionSubtest(
91 "bindings/internal_apis_not_on_chrome_object",
92 "page.html")) << message_;
93}
[email protected]adafe5b2013-08-09 10:35:0494
[email protected]fc034482013-08-09 20:25:1495// Tests that we don't override events when bindings are re-injected.
96// Regression test for http://crbug.com/269149.
rpaquay96bf3b7d2014-11-26 00:19:0897// Regression test for http://crbug.com/436593.
Henrik Grunell6b7d9db2017-06-14 10:27:2098// Flaky on Mac. http://crbug.com/733064.
Giovanni Ortuño Urquidi7ce215452017-06-14 03:34:0899#if defined(OS_MACOSX)
100#define MAYBE_EventOverriding DISABLED_EventOverriding
101#else
102#define MAYBE_EventOverriding EventOverriding
103#endif
Henrik Grunell6b7d9db2017-06-14 10:27:20104IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, MAYBE_EventOverriding) {
[email protected]fc034482013-08-09 20:25:14105 ASSERT_TRUE(RunExtensionTest("bindings/event_overriding")) << message_;
rdevlin.cronind734f682017-06-13 21:23:11106 // The extension test removes a window and, during window removal, sends the
107 // success message. Make sure we flush all pending tasks.
108 base::RunLoop().RunUntilIdle();
[email protected]fc034482013-08-09 20:25:14109}
110
kalman1bd5b182015-01-13 19:01:18111// Tests the effectiveness of the 'nocompile' feature file property.
112// Regression test for http://crbug.com/356133.
113IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, Nocompile) {
114 ASSERT_TRUE(RunExtensionSubtest("bindings/nocompile", "page.html"))
115 << message_;
116}
117
rdevlin.cronin2ba3c88d2015-03-03 01:18:22118IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ApiEnums) {
119 ASSERT_TRUE(RunExtensionTest("bindings/api_enums")) << message_;
120};
121
jochen7923c2a2015-07-14 10:04:45122// Regression test for http://crbug.com/504011 - proper access checks on
123// getModuleSystem().
124IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ModuleSystem) {
125 ASSERT_TRUE(RunExtensionTest("bindings/module_system")) << message_;
126}
127
rdevlin.cronin83a4b3a2015-10-28 21:43:58128IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoExportOverriding) {
rdevlin.cronin83a4b3a2015-10-28 21:43:58129 // We need to create runtime bindings in the web page. An extension that's
130 // externally connectable will do that for us.
131 ASSERT_TRUE(LoadExtension(
132 test_data_dir_.AppendASCII("bindings")
133 .AppendASCII("externally_connectable_everywhere")));
134
135 ui_test_utils::NavigateToURL(
136 browser(),
137 embedded_test_server()->GetURL(
138 "/extensions/api_test/bindings/override_exports.html"));
139
140 // See chrome/test/data/extensions/api_test/bindings/override_exports.html.
141 std::string result;
142 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
143 browser()->tab_strip_model()->GetActiveWebContents(),
144 "window.domAutomationController.send("
145 "document.getElementById('status').textContent.trim());",
146 &result));
147 EXPECT_EQ("success", result);
148}
149
rdevlin.cronin415b73b2015-11-13 01:14:47150IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoGinDefineOverriding) {
rdevlin.cronin415b73b2015-11-13 01:14:47151 // We need to create runtime bindings in the web page. An extension that's
152 // externally connectable will do that for us.
153 ASSERT_TRUE(LoadExtension(
154 test_data_dir_.AppendASCII("bindings")
155 .AppendASCII("externally_connectable_everywhere")));
156
157 ui_test_utils::NavigateToURL(
158 browser(),
159 embedded_test_server()->GetURL(
160 "/extensions/api_test/bindings/override_gin_define.html"));
161 ASSERT_FALSE(
162 browser()->tab_strip_model()->GetActiveWebContents()->IsCrashed());
163
164 // See chrome/test/data/extensions/api_test/bindings/override_gin_define.html.
165 std::string result;
166 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
167 browser()->tab_strip_model()->GetActiveWebContents(),
168 "window.domAutomationController.send("
169 "document.getElementById('status').textContent.trim());",
170 &result));
171 EXPECT_EQ("success", result);
172}
173
rdevlin.cronina5ecbc82015-10-29 23:41:29174IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, HandlerFunctionTypeChecking) {
rdevlin.cronina5ecbc82015-10-29 23:41:29175 ui_test_utils::NavigateToURL(
176 browser(),
177 embedded_test_server()->GetURL(
178 "/extensions/api_test/bindings/handler_function_type_checking.html"));
179 content::WebContents* web_contents =
180 browser()->tab_strip_model()->GetActiveWebContents();
181 EXPECT_FALSE(web_contents->IsCrashed());
182 // See handler_function_type_checking.html.
183 std::string result;
184 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
185 web_contents,
186 "window.domAutomationController.send("
187 "document.getElementById('status').textContent.trim());",
188 &result));
189 EXPECT_EQ("success", result);
190}
191
rdevlin.cronin75b803b2016-03-02 00:13:47192IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest,
193 MoreNativeFunctionInterceptionTests) {
rdevlin.cronin75b803b2016-03-02 00:13:47194 // We need to create runtime bindings in the web page. An extension that's
195 // externally connectable will do that for us.
196 ASSERT_TRUE(
197 LoadExtension(test_data_dir_.AppendASCII("bindings")
198 .AppendASCII("externally_connectable_everywhere")));
199
200 ui_test_utils::NavigateToURL(
201 browser(),
202 embedded_test_server()->GetURL(
203 "/extensions/api_test/bindings/function_interceptions.html"));
204 content::WebContents* web_contents =
205 browser()->tab_strip_model()->GetActiveWebContents();
206 EXPECT_FALSE(web_contents->IsCrashed());
207 // See function_interceptions.html.
208 std::string result;
209 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
210 web_contents, "window.domAutomationController.send(window.testStatus);",
211 &result));
212 EXPECT_EQ("success", result);
213}
214
asargent79b64c32016-08-04 17:17:14215class FramesExtensionBindingsApiTest : public ExtensionBindingsApiTest {
216 public:
217 void SetUpCommandLine(base::CommandLine* command_line) override {
218 ExtensionBindingsApiTest::SetUpCommandLine(command_line);
219 command_line->AppendSwitch(switches::kDisablePopupBlocking);
220 }
221};
222
223// This tests that web pages with iframes or child windows pointing at
224// chrome-extenison:// urls, both web_accessible and nonexistent pages, don't
225// get improper extensions bindings injected while they briefly still point at
226// about:blank and are still scriptable by their parent.
227//
228// The general idea is to load up 2 extensions, one which listens for external
229// messages ("receiver") and one which we'll try first faking messages from in
230// the web page's iframe, as well as actually send a message from later
231// ("sender").
232IN_PROC_BROWSER_TEST_F(FramesExtensionBindingsApiTest, FramesBeforeNavigation) {
233 // Load the sender and receiver extensions, and make sure they are ready.
234 ExtensionTestMessageListener sender_ready("sender_ready", true);
235 const Extension* sender = LoadExtension(
236 test_data_dir_.AppendASCII("bindings").AppendASCII("message_sender"));
237 ASSERT_NE(nullptr, sender);
238 ASSERT_TRUE(sender_ready.WaitUntilSatisfied());
239
240 ExtensionTestMessageListener receiver_ready("receiver_ready", false);
241 const Extension* receiver =
242 LoadExtension(test_data_dir_.AppendASCII("bindings")
243 .AppendASCII("external_message_listener"));
244 ASSERT_NE(nullptr, receiver);
245 ASSERT_TRUE(receiver_ready.WaitUntilSatisfied());
246
247 // Load the web page which tries to impersonate the sender extension via
248 // scripting iframes/child windows before they finish navigating to pages
249 // within the sender extension.
asargent79b64c32016-08-04 17:17:14250 ui_test_utils::NavigateToURL(
251 browser(),
252 embedded_test_server()->GetURL(
253 "/extensions/api_test/bindings/frames_before_navigation.html"));
254
255 bool page_success = false;
256 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
257 browser()->tab_strip_model()->GetWebContentsAt(0), "getResult()",
258 &page_success));
259 EXPECT_TRUE(page_success);
260
261 // Reply to |sender|, causing it to send a message over to |receiver|, and
262 // then ask |receiver| for the total message count. It should be 1 since
263 // |receiver| should not have received any impersonated messages.
264 sender_ready.Reply(receiver->id());
265 int message_count = 0;
266 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
267 ProcessManager::Get(profile())
268 ->GetBackgroundHostForExtension(receiver->id())
269 ->host_contents(),
270 "getMessageCountAfterReceivingRealSenderMessage()", &message_count));
271 EXPECT_EQ(1, message_count);
272}
273
rdevlin.cronin741da002017-04-24 20:27:41274IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, TestFreezingChrome) {
rdevlin.cronin741da002017-04-24 20:27:41275 ui_test_utils::NavigateToURL(
276 browser(), embedded_test_server()->GetURL(
277 "/extensions/api_test/bindings/freeze.html"));
278 content::WebContents* web_contents =
279 browser()->tab_strip_model()->GetActiveWebContents();
280 ASSERT_FALSE(web_contents->IsCrashed());
281}
282
rdevlin.cronine6e20022017-06-13 18:23:40283// Tests interaction with event filter parsing.
284IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, TestEventFilterParsing) {
285 ExtensionTestMessageListener listener("ready", false);
286 ASSERT_TRUE(
287 LoadExtension(test_data_dir_.AppendASCII("bindings/event_filter")));
288 ASSERT_TRUE(listener.WaitUntilSatisfied());
289
290 ResultCatcher catcher;
291 ui_test_utils::NavigateToURL(
292 browser(), embedded_test_server()->GetURL("example.com", "/title1.html"));
293 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
294}
295
rdevlin.cronin350824d42017-06-16 14:47:35296// crbug.com/733337
297IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ValidationInterception) {
298 // We need to create runtime bindings in the web page. An extension that's
299 // externally connectable will do that for us.
300 ASSERT_TRUE(
301 LoadExtension(test_data_dir_.AppendASCII("bindings")
302 .AppendASCII("externally_connectable_everywhere")));
303
304 content::WebContents* web_contents =
305 browser()->tab_strip_model()->GetActiveWebContents();
306 ui_test_utils::NavigateToURL(
307 browser(),
308 embedded_test_server()->GetURL(
309 "/extensions/api_test/bindings/validation_interception.html"));
310 content::WaitForLoadStop(web_contents);
311 ASSERT_FALSE(web_contents->IsCrashed());
312 bool caught = false;
313 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
314 web_contents, "domAutomationController.send(caught)", &caught));
315 EXPECT_TRUE(caught);
316}
317
[email protected]adafe5b2013-08-09 10:35:04318} // namespace
319} // namespace extensions