blob: 218e0e337aaedc7c8d2bb0ff577d1f07dbdb752f [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
Gabriel Charette078e3662017-08-28 22:59:047#include "base/run_loop.h"
Devlin Cronina3fe3d602017-11-22 04:47:438#include "base/test/scoped_feature_list.h"
[email protected]7eef3942013-08-14 02:53:499#include "chrome/browser/extensions/api/permissions/permissions_api.h"
[email protected]c80b8ee2011-12-03 04:26:5210#include "chrome/browser/extensions/extension_apitest.h"
jochen7923c2a2015-07-14 10:04:4511#include "chrome/browser/net/url_request_mock_util.h"
[email protected]fad73672012-06-15 23:26:0612#include "chrome/browser/ui/browser.h"
rdevlin.cronin83a4b3a2015-10-28 21:43:5813#include "chrome/browser/ui/tabs/tab_strip_model.h"
asargent79b64c32016-08-04 17:17:1414#include "chrome/common/chrome_switches.h"
rdevlin.cronin83a4b3a2015-10-28 21:43:5815#include "chrome/test/base/ui_test_utils.h"
jochen7923c2a2015-07-14 10:04:4516#include "content/public/browser/browser_thread.h"
[email protected]7d478cb2012-07-24 17:19:4217#include "content/public/test/browser_test_utils.h"
[email protected]22401dc2014-03-21 01:38:5718#include "extensions/browser/extension_host.h"
[email protected]98b6d942013-11-10 00:34:0719#include "extensions/browser/process_manager.h"
Devlin Cronina3fe3d602017-11-22 04:47:4320#include "extensions/common/extension_features.h"
lfg910f2f92014-09-19 05:31:0921#include "extensions/test/extension_test_message_listener.h"
yoze8dc2f12014-09-09 23:16:3222#include "extensions/test/result_catcher.h"
rdevlin.cronine6e20022017-06-13 18:23:4023#include "net/dns/mock_host_resolver.h"
rdevlin.cronin83a4b3a2015-10-28 21:43:5824#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]fad73672012-06-15 23:26:0625
[email protected]adafe5b2013-08-09 10:35:0426namespace extensions {
27namespace {
28
Devlin Croninc3a1e5072017-08-17 17:02:4929enum BindingsType { NATIVE_BINDINGS, JAVASCRIPT_BINDINGS };
30
31class ExtensionBindingsApiTest
32 : public ExtensionApiTest,
33 public ::testing::WithParamInterface<BindingsType> {
jochen7923c2a2015-07-14 10:04:4534 public:
Devlin Cronina3fe3d602017-11-22 04:47:4335 ExtensionBindingsApiTest() {}
36 ~ExtensionBindingsApiTest() override {}
37
38 void SetUp() override {
39 if (GetParam() == NATIVE_BINDINGS) {
40 scoped_feature_list_.InitAndEnableFeature(features::kNativeCrxBindings);
41 } else {
42 DCHECK_EQ(JAVASCRIPT_BINDINGS, GetParam());
43 scoped_feature_list_.InitAndDisableFeature(features::kNativeCrxBindings);
44 }
45 ExtensionApiTest::SetUp();
Devlin Croninc3a1e5072017-08-17 17:02:4946 }
47
jochen7923c2a2015-07-14 10:04:4548 void SetUpOnMainThread() override {
rdevlin.cronin17160cc62016-11-23 05:33:0849 ExtensionApiTest::SetUpOnMainThread();
rdevlin.cronine6e20022017-06-13 18:23:4050 host_resolver()->AddRule("*", "127.0.0.1");
51 ASSERT_TRUE(StartEmbeddedTestServer());
jochen7923c2a2015-07-14 10:04:4552 }
Devlin Cronina3fe3d602017-11-22 04:47:4353
54 private:
55 base::test::ScopedFeatureList scoped_feature_list_;
56
57 DISALLOW_COPY_AND_ASSIGN(ExtensionBindingsApiTest);
jochen7923c2a2015-07-14 10:04:4558};
[email protected]adafe5b2013-08-09 10:35:0459
Devlin Croninc3a1e5072017-08-17 17:02:4960IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest,
[email protected]7eef3942013-08-14 02:53:4961 UnavailableBindingsNeverRegistered) {
62 // Test will request the 'storage' permission.
63 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true);
64 ASSERT_TRUE(RunExtensionTest(
65 "bindings/unavailable_bindings_never_registered")) << message_;
66}
67
Devlin Croninc3a1e5072017-08-17 17:02:4968IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest,
[email protected]adafe5b2013-08-09 10:35:0469 ExceptionInHandlerShouldNotCrash) {
[email protected]c80b8ee2011-12-03 04:26:5270 ASSERT_TRUE(RunExtensionSubtest(
71 "bindings/exception_in_handler_should_not_crash",
72 "page.html")) << message_;
73}
[email protected]fad73672012-06-15 23:26:0674
75// Tests that an error raised during an async function still fires
[email protected]754ea8b72013-01-08 15:10:3176// the callback, but sets chrome.runtime.lastError.
Devlin Croninc3a1e5072017-08-17 17:02:4977IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest, LastError) {
[email protected]fad73672012-06-15 23:26:0678 ASSERT_TRUE(LoadExtension(
Devlin Croninc3a1e5072017-08-17 17:02:4979 test_data_dir_.AppendASCII("bindings").AppendASCII("last_error")));
[email protected]fad73672012-06-15 23:26:0680
81 // Get the ExtensionHost that is hosting our background page.
[email protected]98b6d942013-11-10 00:34:0782 extensions::ProcessManager* manager =
reillyg0ea3fa902014-10-28 15:30:2383 extensions::ProcessManager::Get(browser()->profile());
[email protected]3a1dc572012-07-31 22:25:1384 extensions::ExtensionHost* host = FindHostWithPath(manager, "/bg.html", 1);
[email protected]fad73672012-06-15 23:26:0685
86 bool result = false;
[email protected]b6987e02013-01-04 18:30:4387 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
88 host->render_view_host(), "testLastError()", &result));
[email protected]fad73672012-06-15 23:26:0689 EXPECT_TRUE(result);
90}
[email protected]52eafbd2013-04-03 04:43:1991
[email protected]adafe5b2013-08-09 10:35:0492// Regression test that we don't delete our own bindings with about:blank
93// iframes.
Devlin Croninc3a1e5072017-08-17 17:02:4994IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest, AboutBlankIframe) {
[email protected]adafe5b2013-08-09 10:35:0495 ResultCatcher catcher;
96 ExtensionTestMessageListener listener("load", true);
97
98 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("bindings")
99 .AppendASCII("about_blank_iframe")));
100
101 ASSERT_TRUE(listener.WaitUntilSatisfied());
102
103 const Extension* extension = LoadExtension(
104 test_data_dir_.AppendASCII("bindings")
105 .AppendASCII("internal_apis_not_on_chrome_object"));
106 ASSERT_TRUE(extension);
107 listener.Reply(extension->id());
108
109 ASSERT_TRUE(catcher.GetNextResult()) << message_;
110}
111
Devlin Croninc3a1e5072017-08-17 17:02:49112IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest,
[email protected]adafe5b2013-08-09 10:35:04113 InternalAPIsNotOnChromeObject) {
[email protected]52eafbd2013-04-03 04:43:19114 ASSERT_TRUE(RunExtensionSubtest(
115 "bindings/internal_apis_not_on_chrome_object",
116 "page.html")) << message_;
117}
[email protected]adafe5b2013-08-09 10:35:04118
[email protected]fc034482013-08-09 20:25:14119// Tests that we don't override events when bindings are re-injected.
120// Regression test for http://crbug.com/269149.
rpaquay96bf3b7d2014-11-26 00:19:08121// Regression test for http://crbug.com/436593.
Henrik Grunell6b7d9db2017-06-14 10:27:20122// Flaky on Mac. http://crbug.com/733064.
Giovanni Ortuño Urquidi7ce215452017-06-14 03:34:08123#if defined(OS_MACOSX)
124#define MAYBE_EventOverriding DISABLED_EventOverriding
125#else
126#define MAYBE_EventOverriding EventOverriding
127#endif
Devlin Croninc3a1e5072017-08-17 17:02:49128IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest, MAYBE_EventOverriding) {
[email protected]fc034482013-08-09 20:25:14129 ASSERT_TRUE(RunExtensionTest("bindings/event_overriding")) << message_;
rdevlin.cronind734f682017-06-13 21:23:11130 // The extension test removes a window and, during window removal, sends the
131 // success message. Make sure we flush all pending tasks.
132 base::RunLoop().RunUntilIdle();
[email protected]fc034482013-08-09 20:25:14133}
134
kalman1bd5b182015-01-13 19:01:18135// Tests the effectiveness of the 'nocompile' feature file property.
136// Regression test for http://crbug.com/356133.
Devlin Croninc3a1e5072017-08-17 17:02:49137IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest, Nocompile) {
kalman1bd5b182015-01-13 19:01:18138 ASSERT_TRUE(RunExtensionSubtest("bindings/nocompile", "page.html"))
139 << message_;
140}
141
Devlin Croninc3a1e5072017-08-17 17:02:49142IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest, ApiEnums) {
rdevlin.cronin2ba3c88d2015-03-03 01:18:22143 ASSERT_TRUE(RunExtensionTest("bindings/api_enums")) << message_;
144};
145
jochen7923c2a2015-07-14 10:04:45146// Regression test for http://crbug.com/504011 - proper access checks on
147// getModuleSystem().
Devlin Croninc3a1e5072017-08-17 17:02:49148IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest, ModuleSystem) {
jochen7923c2a2015-07-14 10:04:45149 ASSERT_TRUE(RunExtensionTest("bindings/module_system")) << message_;
150}
151
Devlin Croninc3a1e5072017-08-17 17:02:49152IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest, NoExportOverriding) {
rdevlin.cronin83a4b3a2015-10-28 21:43:58153 // We need to create runtime bindings in the web page. An extension that's
154 // externally connectable will do that for us.
155 ASSERT_TRUE(LoadExtension(
156 test_data_dir_.AppendASCII("bindings")
157 .AppendASCII("externally_connectable_everywhere")));
158
159 ui_test_utils::NavigateToURL(
160 browser(),
161 embedded_test_server()->GetURL(
162 "/extensions/api_test/bindings/override_exports.html"));
163
164 // See chrome/test/data/extensions/api_test/bindings/override_exports.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
Devlin Croninc3a1e5072017-08-17 17:02:49174IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest, NoGinDefineOverriding) {
rdevlin.cronin415b73b2015-11-13 01:14:47175 // We need to create runtime bindings in the web page. An extension that's
176 // externally connectable will do that for us.
177 ASSERT_TRUE(LoadExtension(
178 test_data_dir_.AppendASCII("bindings")
179 .AppendASCII("externally_connectable_everywhere")));
180
181 ui_test_utils::NavigateToURL(
182 browser(),
183 embedded_test_server()->GetURL(
184 "/extensions/api_test/bindings/override_gin_define.html"));
185 ASSERT_FALSE(
186 browser()->tab_strip_model()->GetActiveWebContents()->IsCrashed());
187
188 // See chrome/test/data/extensions/api_test/bindings/override_gin_define.html.
189 std::string result;
190 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
191 browser()->tab_strip_model()->GetActiveWebContents(),
192 "window.domAutomationController.send("
193 "document.getElementById('status').textContent.trim());",
194 &result));
195 EXPECT_EQ("success", result);
196}
197
Devlin Croninc3a1e5072017-08-17 17:02:49198IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest, HandlerFunctionTypeChecking) {
rdevlin.cronina5ecbc82015-10-29 23:41:29199 ui_test_utils::NavigateToURL(
200 browser(),
201 embedded_test_server()->GetURL(
202 "/extensions/api_test/bindings/handler_function_type_checking.html"));
203 content::WebContents* web_contents =
204 browser()->tab_strip_model()->GetActiveWebContents();
205 EXPECT_FALSE(web_contents->IsCrashed());
206 // See handler_function_type_checking.html.
207 std::string result;
208 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
209 web_contents,
210 "window.domAutomationController.send("
211 "document.getElementById('status').textContent.trim());",
212 &result));
213 EXPECT_EQ("success", result);
214}
215
Devlin Croninc3a1e5072017-08-17 17:02:49216IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest,
rdevlin.cronin75b803b2016-03-02 00:13:47217 MoreNativeFunctionInterceptionTests) {
rdevlin.cronin75b803b2016-03-02 00:13:47218 // We need to create runtime bindings in the web page. An extension that's
219 // externally connectable will do that for us.
220 ASSERT_TRUE(
221 LoadExtension(test_data_dir_.AppendASCII("bindings")
222 .AppendASCII("externally_connectable_everywhere")));
223
224 ui_test_utils::NavigateToURL(
225 browser(),
226 embedded_test_server()->GetURL(
227 "/extensions/api_test/bindings/function_interceptions.html"));
228 content::WebContents* web_contents =
229 browser()->tab_strip_model()->GetActiveWebContents();
230 EXPECT_FALSE(web_contents->IsCrashed());
231 // See function_interceptions.html.
232 std::string result;
233 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
234 web_contents, "window.domAutomationController.send(window.testStatus);",
235 &result));
236 EXPECT_EQ("success", result);
237}
238
asargent79b64c32016-08-04 17:17:14239class FramesExtensionBindingsApiTest : public ExtensionBindingsApiTest {
240 public:
241 void SetUpCommandLine(base::CommandLine* command_line) override {
242 ExtensionBindingsApiTest::SetUpCommandLine(command_line);
Devlin Croninc3a1e5072017-08-17 17:02:49243 command_line->AppendSwitch(::switches::kDisablePopupBlocking);
asargent79b64c32016-08-04 17:17:14244 }
245};
246
247// This tests that web pages with iframes or child windows pointing at
248// chrome-extenison:// urls, both web_accessible and nonexistent pages, don't
249// get improper extensions bindings injected while they briefly still point at
250// about:blank and are still scriptable by their parent.
251//
252// The general idea is to load up 2 extensions, one which listens for external
253// messages ("receiver") and one which we'll try first faking messages from in
254// the web page's iframe, as well as actually send a message from later
255// ("sender").
Dave Tapuska61ed7fbb2017-09-05 21:42:08256IN_PROC_BROWSER_TEST_P(FramesExtensionBindingsApiTest, FramesBeforeNavigation) {
asargent79b64c32016-08-04 17:17:14257 // Load the sender and receiver extensions, and make sure they are ready.
258 ExtensionTestMessageListener sender_ready("sender_ready", true);
259 const Extension* sender = LoadExtension(
260 test_data_dir_.AppendASCII("bindings").AppendASCII("message_sender"));
261 ASSERT_NE(nullptr, sender);
262 ASSERT_TRUE(sender_ready.WaitUntilSatisfied());
263
264 ExtensionTestMessageListener receiver_ready("receiver_ready", false);
265 const Extension* receiver =
266 LoadExtension(test_data_dir_.AppendASCII("bindings")
267 .AppendASCII("external_message_listener"));
268 ASSERT_NE(nullptr, receiver);
269 ASSERT_TRUE(receiver_ready.WaitUntilSatisfied());
270
271 // Load the web page which tries to impersonate the sender extension via
272 // scripting iframes/child windows before they finish navigating to pages
273 // within the sender extension.
asargent79b64c32016-08-04 17:17:14274 ui_test_utils::NavigateToURL(
275 browser(),
276 embedded_test_server()->GetURL(
277 "/extensions/api_test/bindings/frames_before_navigation.html"));
278
279 bool page_success = false;
280 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
281 browser()->tab_strip_model()->GetWebContentsAt(0), "getResult()",
282 &page_success));
283 EXPECT_TRUE(page_success);
284
285 // Reply to |sender|, causing it to send a message over to |receiver|, and
286 // then ask |receiver| for the total message count. It should be 1 since
287 // |receiver| should not have received any impersonated messages.
288 sender_ready.Reply(receiver->id());
289 int message_count = 0;
290 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
291 ProcessManager::Get(profile())
292 ->GetBackgroundHostForExtension(receiver->id())
293 ->host_contents(),
294 "getMessageCountAfterReceivingRealSenderMessage()", &message_count));
295 EXPECT_EQ(1, message_count);
296}
297
Devlin Croninc3a1e5072017-08-17 17:02:49298IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest, TestFreezingChrome) {
rdevlin.cronin741da002017-04-24 20:27:41299 ui_test_utils::NavigateToURL(
300 browser(), embedded_test_server()->GetURL(
301 "/extensions/api_test/bindings/freeze.html"));
302 content::WebContents* web_contents =
303 browser()->tab_strip_model()->GetActiveWebContents();
304 ASSERT_FALSE(web_contents->IsCrashed());
305}
306
rdevlin.cronine6e20022017-06-13 18:23:40307// Tests interaction with event filter parsing.
Devlin Croninc3a1e5072017-08-17 17:02:49308IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest, TestEventFilterParsing) {
rdevlin.cronine6e20022017-06-13 18:23:40309 ExtensionTestMessageListener listener("ready", false);
310 ASSERT_TRUE(
311 LoadExtension(test_data_dir_.AppendASCII("bindings/event_filter")));
312 ASSERT_TRUE(listener.WaitUntilSatisfied());
313
314 ResultCatcher catcher;
315 ui_test_utils::NavigateToURL(
316 browser(), embedded_test_server()->GetURL("example.com", "/title1.html"));
317 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
318}
319
rdevlin.cronin350824d42017-06-16 14:47:35320// crbug.com/733337
Devlin Croninc3a1e5072017-08-17 17:02:49321IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest, ValidationInterception) {
rdevlin.cronin350824d42017-06-16 14:47:35322 // We need to create runtime bindings in the web page. An extension that's
323 // externally connectable will do that for us.
324 ASSERT_TRUE(
325 LoadExtension(test_data_dir_.AppendASCII("bindings")
326 .AppendASCII("externally_connectable_everywhere")));
327
328 content::WebContents* web_contents =
329 browser()->tab_strip_model()->GetActiveWebContents();
330 ui_test_utils::NavigateToURL(
331 browser(),
332 embedded_test_server()->GetURL(
333 "/extensions/api_test/bindings/validation_interception.html"));
334 content::WaitForLoadStop(web_contents);
335 ASSERT_FALSE(web_contents->IsCrashed());
336 bool caught = false;
337 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
338 web_contents, "domAutomationController.send(caught)", &caught));
339 EXPECT_TRUE(caught);
340}
341
Devlin Cronin5cf20f02017-10-10 14:25:04342IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest, UncaughtExceptionLogging) {
343 ASSERT_TRUE(RunExtensionTest("bindings/uncaught_exception_logging"))
344 << message_;
345}
346
Alex Moshchuke63b9e92017-10-14 00:27:22347// Verify that when a web frame embeds an extension subframe, and that subframe
348// is the only active portion of the extension, the subframe gets proper JS
349// bindings. See https://crbug.com/760341.
350IN_PROC_BROWSER_TEST_P(ExtensionBindingsApiTest,
351 ExtensionSubframeGetsBindings) {
352 // Load an extension that does not have a background page or popup, so it
353 // won't be activated just yet.
354 const extensions::Extension* extension =
355 LoadExtension(test_data_dir_.AppendASCII("bindings")
356 .AppendASCII("extension_subframe_gets_bindings"));
357 ASSERT_TRUE(extension);
358
359 // Navigate current tab to a web URL with a subframe.
360 content::WebContents* web_contents =
361 browser()->tab_strip_model()->GetActiveWebContents();
362 ui_test_utils::NavigateToURL(browser(),
363 embedded_test_server()->GetURL("/iframe.html"));
364
365 // Navigate the subframe to the extension URL, which should activate the
366 // extension.
367 GURL extension_url(extension->GetResourceURL("page.html"));
368 ResultCatcher catcher;
369 content::NavigateIframeToURL(web_contents, "test", extension_url);
370 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
371}
372
Devlin Croninc3a1e5072017-08-17 17:02:49373// Run core bindings API tests with both native and JS-based bindings. This
374// ensures we have some minimum level of coverage while in the experimental
375// phase, when native bindings may be enabled on trunk but not at 100% stable.
376INSTANTIATE_TEST_CASE_P(Native,
377 ExtensionBindingsApiTest,
378 ::testing::Values(NATIVE_BINDINGS));
379INSTANTIATE_TEST_CASE_P(JavaScript,
380 ExtensionBindingsApiTest,
381 ::testing::Values(JAVASCRIPT_BINDINGS));
382
383INSTANTIATE_TEST_CASE_P(Native,
384 FramesExtensionBindingsApiTest,
385 ::testing::Values(NATIVE_BINDINGS));
386INSTANTIATE_TEST_CASE_P(JavaScript,
387 FramesExtensionBindingsApiTest,
388 ::testing::Values(JAVASCRIPT_BINDINGS));
389
[email protected]adafe5b2013-08-09 10:35:04390} // namespace
391} // namespace extensions