blob: 260e1ee3debc0578c7f44adc4496b8a46b0f3f53 [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.
[email protected]fc034482013-08-09 20:25:1498IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, EventOverriding) {
99 ASSERT_TRUE(RunExtensionTest("bindings/event_overriding")) << message_;
100}
101
kalman1bd5b182015-01-13 19:01:18102// Tests the effectiveness of the 'nocompile' feature file property.
103// Regression test for http://crbug.com/356133.
104IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, Nocompile) {
105 ASSERT_TRUE(RunExtensionSubtest("bindings/nocompile", "page.html"))
106 << message_;
107}
108
rdevlin.cronin2ba3c88d2015-03-03 01:18:22109IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ApiEnums) {
110 ASSERT_TRUE(RunExtensionTest("bindings/api_enums")) << message_;
111};
112
jochen7923c2a2015-07-14 10:04:45113// Regression test for http://crbug.com/504011 - proper access checks on
114// getModuleSystem().
115IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ModuleSystem) {
116 ASSERT_TRUE(RunExtensionTest("bindings/module_system")) << message_;
117}
118
rdevlin.cronin83a4b3a2015-10-28 21:43:58119IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoExportOverriding) {
rdevlin.cronin83a4b3a2015-10-28 21:43:58120 // We need to create runtime bindings in the web page. An extension that's
121 // externally connectable will do that for us.
122 ASSERT_TRUE(LoadExtension(
123 test_data_dir_.AppendASCII("bindings")
124 .AppendASCII("externally_connectable_everywhere")));
125
126 ui_test_utils::NavigateToURL(
127 browser(),
128 embedded_test_server()->GetURL(
129 "/extensions/api_test/bindings/override_exports.html"));
130
131 // See chrome/test/data/extensions/api_test/bindings/override_exports.html.
132 std::string result;
133 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
134 browser()->tab_strip_model()->GetActiveWebContents(),
135 "window.domAutomationController.send("
136 "document.getElementById('status').textContent.trim());",
137 &result));
138 EXPECT_EQ("success", result);
139}
140
rdevlin.cronin415b73b2015-11-13 01:14:47141IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoGinDefineOverriding) {
rdevlin.cronin415b73b2015-11-13 01:14:47142 // We need to create runtime bindings in the web page. An extension that's
143 // externally connectable will do that for us.
144 ASSERT_TRUE(LoadExtension(
145 test_data_dir_.AppendASCII("bindings")
146 .AppendASCII("externally_connectable_everywhere")));
147
148 ui_test_utils::NavigateToURL(
149 browser(),
150 embedded_test_server()->GetURL(
151 "/extensions/api_test/bindings/override_gin_define.html"));
152 ASSERT_FALSE(
153 browser()->tab_strip_model()->GetActiveWebContents()->IsCrashed());
154
155 // See chrome/test/data/extensions/api_test/bindings/override_gin_define.html.
156 std::string result;
157 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
158 browser()->tab_strip_model()->GetActiveWebContents(),
159 "window.domAutomationController.send("
160 "document.getElementById('status').textContent.trim());",
161 &result));
162 EXPECT_EQ("success", result);
163}
164
rdevlin.cronina5ecbc82015-10-29 23:41:29165IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, HandlerFunctionTypeChecking) {
rdevlin.cronina5ecbc82015-10-29 23:41:29166 ui_test_utils::NavigateToURL(
167 browser(),
168 embedded_test_server()->GetURL(
169 "/extensions/api_test/bindings/handler_function_type_checking.html"));
170 content::WebContents* web_contents =
171 browser()->tab_strip_model()->GetActiveWebContents();
172 EXPECT_FALSE(web_contents->IsCrashed());
173 // See handler_function_type_checking.html.
174 std::string result;
175 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
176 web_contents,
177 "window.domAutomationController.send("
178 "document.getElementById('status').textContent.trim());",
179 &result));
180 EXPECT_EQ("success", result);
181}
182
rdevlin.cronin75b803b2016-03-02 00:13:47183IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest,
184 MoreNativeFunctionInterceptionTests) {
rdevlin.cronin75b803b2016-03-02 00:13:47185 // We need to create runtime bindings in the web page. An extension that's
186 // externally connectable will do that for us.
187 ASSERT_TRUE(
188 LoadExtension(test_data_dir_.AppendASCII("bindings")
189 .AppendASCII("externally_connectable_everywhere")));
190
191 ui_test_utils::NavigateToURL(
192 browser(),
193 embedded_test_server()->GetURL(
194 "/extensions/api_test/bindings/function_interceptions.html"));
195 content::WebContents* web_contents =
196 browser()->tab_strip_model()->GetActiveWebContents();
197 EXPECT_FALSE(web_contents->IsCrashed());
198 // See function_interceptions.html.
199 std::string result;
200 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
201 web_contents, "window.domAutomationController.send(window.testStatus);",
202 &result));
203 EXPECT_EQ("success", result);
204}
205
asargent79b64c32016-08-04 17:17:14206class FramesExtensionBindingsApiTest : public ExtensionBindingsApiTest {
207 public:
208 void SetUpCommandLine(base::CommandLine* command_line) override {
209 ExtensionBindingsApiTest::SetUpCommandLine(command_line);
210 command_line->AppendSwitch(switches::kDisablePopupBlocking);
211 }
212};
213
214// This tests that web pages with iframes or child windows pointing at
215// chrome-extenison:// urls, both web_accessible and nonexistent pages, don't
216// get improper extensions bindings injected while they briefly still point at
217// about:blank and are still scriptable by their parent.
218//
219// The general idea is to load up 2 extensions, one which listens for external
220// messages ("receiver") and one which we'll try first faking messages from in
221// the web page's iframe, as well as actually send a message from later
222// ("sender").
223IN_PROC_BROWSER_TEST_F(FramesExtensionBindingsApiTest, FramesBeforeNavigation) {
224 // Load the sender and receiver extensions, and make sure they are ready.
225 ExtensionTestMessageListener sender_ready("sender_ready", true);
226 const Extension* sender = LoadExtension(
227 test_data_dir_.AppendASCII("bindings").AppendASCII("message_sender"));
228 ASSERT_NE(nullptr, sender);
229 ASSERT_TRUE(sender_ready.WaitUntilSatisfied());
230
231 ExtensionTestMessageListener receiver_ready("receiver_ready", false);
232 const Extension* receiver =
233 LoadExtension(test_data_dir_.AppendASCII("bindings")
234 .AppendASCII("external_message_listener"));
235 ASSERT_NE(nullptr, receiver);
236 ASSERT_TRUE(receiver_ready.WaitUntilSatisfied());
237
238 // Load the web page which tries to impersonate the sender extension via
239 // scripting iframes/child windows before they finish navigating to pages
240 // within the sender extension.
asargent79b64c32016-08-04 17:17:14241 ui_test_utils::NavigateToURL(
242 browser(),
243 embedded_test_server()->GetURL(
244 "/extensions/api_test/bindings/frames_before_navigation.html"));
245
246 bool page_success = false;
247 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
248 browser()->tab_strip_model()->GetWebContentsAt(0), "getResult()",
249 &page_success));
250 EXPECT_TRUE(page_success);
251
252 // Reply to |sender|, causing it to send a message over to |receiver|, and
253 // then ask |receiver| for the total message count. It should be 1 since
254 // |receiver| should not have received any impersonated messages.
255 sender_ready.Reply(receiver->id());
256 int message_count = 0;
257 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
258 ProcessManager::Get(profile())
259 ->GetBackgroundHostForExtension(receiver->id())
260 ->host_contents(),
261 "getMessageCountAfterReceivingRealSenderMessage()", &message_count));
262 EXPECT_EQ(1, message_count);
263}
264
rdevlin.cronin741da002017-04-24 20:27:41265IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, TestFreezingChrome) {
rdevlin.cronin741da002017-04-24 20:27:41266 ui_test_utils::NavigateToURL(
267 browser(), embedded_test_server()->GetURL(
268 "/extensions/api_test/bindings/freeze.html"));
269 content::WebContents* web_contents =
270 browser()->tab_strip_model()->GetActiveWebContents();
271 ASSERT_FALSE(web_contents->IsCrashed());
272}
273
rdevlin.cronine6e20022017-06-13 18:23:40274// Tests interaction with event filter parsing.
275IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, TestEventFilterParsing) {
276 ExtensionTestMessageListener listener("ready", false);
277 ASSERT_TRUE(
278 LoadExtension(test_data_dir_.AppendASCII("bindings/event_filter")));
279 ASSERT_TRUE(listener.WaitUntilSatisfied());
280
281 ResultCatcher catcher;
282 ui_test_utils::NavigateToURL(
283 browser(), embedded_test_server()->GetURL("example.com", "/title1.html"));
284 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
285}
286
[email protected]adafe5b2013-08-09 10:35:04287} // namespace
288} // namespace extensions