blob: 5006270d002ce5fe4773e37690021637c02fe7fa [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.cronin83a4b3a2015-10-28 21:43:5820#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]fad73672012-06-15 23:26:0621
[email protected]adafe5b2013-08-09 10:35:0422namespace extensions {
23namespace {
24
jochen7923c2a2015-07-14 10:04:4525class ExtensionBindingsApiTest : public ExtensionApiTest {
26 public:
27 void SetUpOnMainThread() override {
28 content::BrowserThread::PostTask(
29 content::BrowserThread::IO, FROM_HERE,
30 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
31 }
32};
[email protected]adafe5b2013-08-09 10:35:0433
34IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest,
[email protected]7eef3942013-08-14 02:53:4935 UnavailableBindingsNeverRegistered) {
36 // Test will request the 'storage' permission.
37 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true);
38 ASSERT_TRUE(RunExtensionTest(
39 "bindings/unavailable_bindings_never_registered")) << message_;
40}
41
42IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest,
[email protected]adafe5b2013-08-09 10:35:0443 ExceptionInHandlerShouldNotCrash) {
[email protected]c80b8ee2011-12-03 04:26:5244 ASSERT_TRUE(RunExtensionSubtest(
45 "bindings/exception_in_handler_should_not_crash",
46 "page.html")) << message_;
47}
[email protected]fad73672012-06-15 23:26:0648
49// Tests that an error raised during an async function still fires
[email protected]754ea8b72013-01-08 15:10:3150// the callback, but sets chrome.runtime.lastError.
[email protected]fc034482013-08-09 20:25:1451// FIXME should be in ExtensionBindingsApiTest.
[email protected]fad73672012-06-15 23:26:0652IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, LastError) {
53 ASSERT_TRUE(LoadExtension(
54 test_data_dir_.AppendASCII("browsertest").AppendASCII("last_error")));
55
56 // Get the ExtensionHost that is hosting our background page.
[email protected]98b6d942013-11-10 00:34:0757 extensions::ProcessManager* manager =
reillyg0ea3fa902014-10-28 15:30:2358 extensions::ProcessManager::Get(browser()->profile());
[email protected]3a1dc572012-07-31 22:25:1359 extensions::ExtensionHost* host = FindHostWithPath(manager, "/bg.html", 1);
[email protected]fad73672012-06-15 23:26:0660
61 bool result = false;
[email protected]b6987e02013-01-04 18:30:4362 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
63 host->render_view_host(), "testLastError()", &result));
[email protected]fad73672012-06-15 23:26:0664 EXPECT_TRUE(result);
65}
[email protected]52eafbd2013-04-03 04:43:1966
[email protected]adafe5b2013-08-09 10:35:0467// Regression test that we don't delete our own bindings with about:blank
68// iframes.
69IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, AboutBlankIframe) {
70 ResultCatcher catcher;
71 ExtensionTestMessageListener listener("load", true);
72
73 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("bindings")
74 .AppendASCII("about_blank_iframe")));
75
76 ASSERT_TRUE(listener.WaitUntilSatisfied());
77
78 const Extension* extension = LoadExtension(
79 test_data_dir_.AppendASCII("bindings")
80 .AppendASCII("internal_apis_not_on_chrome_object"));
81 ASSERT_TRUE(extension);
82 listener.Reply(extension->id());
83
84 ASSERT_TRUE(catcher.GetNextResult()) << message_;
85}
86
87IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest,
88 InternalAPIsNotOnChromeObject) {
[email protected]52eafbd2013-04-03 04:43:1989 ASSERT_TRUE(RunExtensionSubtest(
90 "bindings/internal_apis_not_on_chrome_object",
91 "page.html")) << message_;
92}
[email protected]adafe5b2013-08-09 10:35:0493
[email protected]fc034482013-08-09 20:25:1494// Tests that we don't override events when bindings are re-injected.
95// Regression test for http://crbug.com/269149.
rpaquay96bf3b7d2014-11-26 00:19:0896// Regression test for http://crbug.com/436593.
[email protected]fc034482013-08-09 20:25:1497IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, EventOverriding) {
98 ASSERT_TRUE(RunExtensionTest("bindings/event_overriding")) << message_;
99}
100
kalman1bd5b182015-01-13 19:01:18101// Tests the effectiveness of the 'nocompile' feature file property.
102// Regression test for http://crbug.com/356133.
103IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, Nocompile) {
104 ASSERT_TRUE(RunExtensionSubtest("bindings/nocompile", "page.html"))
105 << message_;
106}
107
rdevlin.cronin2ba3c88d2015-03-03 01:18:22108IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ApiEnums) {
109 ASSERT_TRUE(RunExtensionTest("bindings/api_enums")) << message_;
110};
111
jochen7923c2a2015-07-14 10:04:45112// Regression test for http://crbug.com/504011 - proper access checks on
113// getModuleSystem().
114IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ModuleSystem) {
115 ASSERT_TRUE(RunExtensionTest("bindings/module_system")) << message_;
116}
117
rdevlin.cronin83a4b3a2015-10-28 21:43:58118IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoExportOverriding) {
tfarina8dfd1142015-11-30 22:00:26119 ASSERT_TRUE(embedded_test_server()->Start());
rdevlin.cronin83a4b3a2015-10-28 21:43:58120
121 // We need to create runtime bindings in the web page. An extension that's
122 // externally connectable will do that for us.
123 ASSERT_TRUE(LoadExtension(
124 test_data_dir_.AppendASCII("bindings")
125 .AppendASCII("externally_connectable_everywhere")));
126
127 ui_test_utils::NavigateToURL(
128 browser(),
129 embedded_test_server()->GetURL(
130 "/extensions/api_test/bindings/override_exports.html"));
131
132 // See chrome/test/data/extensions/api_test/bindings/override_exports.html.
133 std::string result;
134 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
135 browser()->tab_strip_model()->GetActiveWebContents(),
136 "window.domAutomationController.send("
137 "document.getElementById('status').textContent.trim());",
138 &result));
139 EXPECT_EQ("success", result);
140}
141
rdevlin.cronin415b73b2015-11-13 01:14:47142IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoGinDefineOverriding) {
tfarina8dfd1142015-11-30 22:00:26143 ASSERT_TRUE(embedded_test_server()->Start());
rdevlin.cronin415b73b2015-11-13 01:14:47144
145 // We need to create runtime bindings in the web page. An extension that's
146 // externally connectable will do that for us.
147 ASSERT_TRUE(LoadExtension(
148 test_data_dir_.AppendASCII("bindings")
149 .AppendASCII("externally_connectable_everywhere")));
150
151 ui_test_utils::NavigateToURL(
152 browser(),
153 embedded_test_server()->GetURL(
154 "/extensions/api_test/bindings/override_gin_define.html"));
155 ASSERT_FALSE(
156 browser()->tab_strip_model()->GetActiveWebContents()->IsCrashed());
157
158 // See chrome/test/data/extensions/api_test/bindings/override_gin_define.html.
159 std::string result;
160 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
161 browser()->tab_strip_model()->GetActiveWebContents(),
162 "window.domAutomationController.send("
163 "document.getElementById('status').textContent.trim());",
164 &result));
165 EXPECT_EQ("success", result);
166}
167
rdevlin.cronina5ecbc82015-10-29 23:41:29168IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, HandlerFunctionTypeChecking) {
tfarina8dfd1142015-11-30 22:00:26169 ASSERT_TRUE(embedded_test_server()->Start());
rdevlin.cronina5ecbc82015-10-29 23:41:29170 ui_test_utils::NavigateToURL(
171 browser(),
172 embedded_test_server()->GetURL(
173 "/extensions/api_test/bindings/handler_function_type_checking.html"));
174 content::WebContents* web_contents =
175 browser()->tab_strip_model()->GetActiveWebContents();
176 EXPECT_FALSE(web_contents->IsCrashed());
177 // See handler_function_type_checking.html.
178 std::string result;
179 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
180 web_contents,
181 "window.domAutomationController.send("
182 "document.getElementById('status').textContent.trim());",
183 &result));
184 EXPECT_EQ("success", result);
185}
186
rdevlin.cronin75b803b2016-03-02 00:13:47187IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest,
188 MoreNativeFunctionInterceptionTests) {
189 ASSERT_TRUE(embedded_test_server()->Start());
190
191 // We need to create runtime bindings in the web page. An extension that's
192 // externally connectable will do that for us.
193 ASSERT_TRUE(
194 LoadExtension(test_data_dir_.AppendASCII("bindings")
195 .AppendASCII("externally_connectable_everywhere")));
196
197 ui_test_utils::NavigateToURL(
198 browser(),
199 embedded_test_server()->GetURL(
200 "/extensions/api_test/bindings/function_interceptions.html"));
201 content::WebContents* web_contents =
202 browser()->tab_strip_model()->GetActiveWebContents();
203 EXPECT_FALSE(web_contents->IsCrashed());
204 // See function_interceptions.html.
205 std::string result;
206 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
207 web_contents, "window.domAutomationController.send(window.testStatus);",
208 &result));
209 EXPECT_EQ("success", result);
210}
211
asargent79b64c32016-08-04 17:17:14212class FramesExtensionBindingsApiTest : public ExtensionBindingsApiTest {
213 public:
214 void SetUpCommandLine(base::CommandLine* command_line) override {
215 ExtensionBindingsApiTest::SetUpCommandLine(command_line);
216 command_line->AppendSwitch(switches::kDisablePopupBlocking);
217 }
218};
219
220// This tests that web pages with iframes or child windows pointing at
221// chrome-extenison:// urls, both web_accessible and nonexistent pages, don't
222// get improper extensions bindings injected while they briefly still point at
223// about:blank and are still scriptable by their parent.
224//
225// The general idea is to load up 2 extensions, one which listens for external
226// messages ("receiver") and one which we'll try first faking messages from in
227// the web page's iframe, as well as actually send a message from later
228// ("sender").
229IN_PROC_BROWSER_TEST_F(FramesExtensionBindingsApiTest, FramesBeforeNavigation) {
230 // Load the sender and receiver extensions, and make sure they are ready.
231 ExtensionTestMessageListener sender_ready("sender_ready", true);
232 const Extension* sender = LoadExtension(
233 test_data_dir_.AppendASCII("bindings").AppendASCII("message_sender"));
234 ASSERT_NE(nullptr, sender);
235 ASSERT_TRUE(sender_ready.WaitUntilSatisfied());
236
237 ExtensionTestMessageListener receiver_ready("receiver_ready", false);
238 const Extension* receiver =
239 LoadExtension(test_data_dir_.AppendASCII("bindings")
240 .AppendASCII("external_message_listener"));
241 ASSERT_NE(nullptr, receiver);
242 ASSERT_TRUE(receiver_ready.WaitUntilSatisfied());
243
244 // Load the web page which tries to impersonate the sender extension via
245 // scripting iframes/child windows before they finish navigating to pages
246 // within the sender extension.
247 ASSERT_TRUE(embedded_test_server()->Start());
248 ui_test_utils::NavigateToURL(
249 browser(),
250 embedded_test_server()->GetURL(
251 "/extensions/api_test/bindings/frames_before_navigation.html"));
252
253 bool page_success = false;
254 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
255 browser()->tab_strip_model()->GetWebContentsAt(0), "getResult()",
256 &page_success));
257 EXPECT_TRUE(page_success);
258
259 // Reply to |sender|, causing it to send a message over to |receiver|, and
260 // then ask |receiver| for the total message count. It should be 1 since
261 // |receiver| should not have received any impersonated messages.
262 sender_ready.Reply(receiver->id());
263 int message_count = 0;
264 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
265 ProcessManager::Get(profile())
266 ->GetBackgroundHostForExtension(receiver->id())
267 ->host_contents(),
268 "getMessageCountAfterReceivingRealSenderMessage()", &message_count));
269 EXPECT_EQ(1, message_count);
270}
271
[email protected]adafe5b2013-08-09 10:35:04272} // namespace
273} // namespace extensions