[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c80b8ee | 2011-12-03 04:26:52 | [diff] [blame] | 2 | // 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] | 7eef394 | 2013-08-14 02:53:49 | [diff] [blame] | 7 | #include "chrome/browser/extensions/api/permissions/permissions_api.h" |
[email protected] | c80b8ee | 2011-12-03 04:26:52 | [diff] [blame] | 8 | #include "chrome/browser/extensions/extension_apitest.h" |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 9 | #include "chrome/browser/net/url_request_mock_util.h" |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 10 | #include "chrome/browser/ui/browser.h" |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 11 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
asargent | 79b64c3 | 2016-08-04 17:17:14 | [diff] [blame^] | 12 | #include "chrome/common/chrome_switches.h" |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 13 | #include "chrome/test/base/ui_test_utils.h" |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 14 | #include "content/public/browser/browser_thread.h" |
[email protected] | 7d478cb | 2012-07-24 17:19:42 | [diff] [blame] | 15 | #include "content/public/test/browser_test_utils.h" |
[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 16 | #include "extensions/browser/extension_host.h" |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 17 | #include "extensions/browser/process_manager.h" |
lfg | 910f2f9 | 2014-09-19 05:31:09 | [diff] [blame] | 18 | #include "extensions/test/extension_test_message_listener.h" |
yoz | e8dc2f1 | 2014-09-09 23:16:32 | [diff] [blame] | 19 | #include "extensions/test/result_catcher.h" |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 20 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 21 | |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 22 | namespace extensions { |
| 23 | namespace { |
| 24 | |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 25 | class 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] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 33 | |
| 34 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
[email protected] | 7eef394 | 2013-08-14 02:53:49 | [diff] [blame] | 35 | 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 | |
| 42 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 43 | ExceptionInHandlerShouldNotCrash) { |
[email protected] | c80b8ee | 2011-12-03 04:26:52 | [diff] [blame] | 44 | ASSERT_TRUE(RunExtensionSubtest( |
| 45 | "bindings/exception_in_handler_should_not_crash", |
| 46 | "page.html")) << message_; |
| 47 | } |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 48 | |
| 49 | // Tests that an error raised during an async function still fires |
[email protected] | 754ea8b7 | 2013-01-08 15:10:31 | [diff] [blame] | 50 | // the callback, but sets chrome.runtime.lastError. |
[email protected] | fc03448 | 2013-08-09 20:25:14 | [diff] [blame] | 51 | // FIXME should be in ExtensionBindingsApiTest. |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 52 | IN_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] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 57 | extensions::ProcessManager* manager = |
reillyg | 0ea3fa90 | 2014-10-28 15:30:23 | [diff] [blame] | 58 | extensions::ProcessManager::Get(browser()->profile()); |
[email protected] | 3a1dc57 | 2012-07-31 22:25:13 | [diff] [blame] | 59 | extensions::ExtensionHost* host = FindHostWithPath(manager, "/bg.html", 1); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 60 | |
| 61 | bool result = false; |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 62 | ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 63 | host->render_view_host(), "testLastError()", &result)); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 64 | EXPECT_TRUE(result); |
| 65 | } |
[email protected] | 52eafbd | 2013-04-03 04:43:19 | [diff] [blame] | 66 | |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 67 | // Regression test that we don't delete our own bindings with about:blank |
| 68 | // iframes. |
| 69 | IN_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 | |
| 87 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
| 88 | InternalAPIsNotOnChromeObject) { |
[email protected] | 52eafbd | 2013-04-03 04:43:19 | [diff] [blame] | 89 | ASSERT_TRUE(RunExtensionSubtest( |
| 90 | "bindings/internal_apis_not_on_chrome_object", |
| 91 | "page.html")) << message_; |
| 92 | } |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 93 | |
[email protected] | fc03448 | 2013-08-09 20:25:14 | [diff] [blame] | 94 | // Tests that we don't override events when bindings are re-injected. |
| 95 | // Regression test for http://crbug.com/269149. |
rpaquay | 96bf3b7d | 2014-11-26 00:19:08 | [diff] [blame] | 96 | // Regression test for http://crbug.com/436593. |
[email protected] | fc03448 | 2013-08-09 20:25:14 | [diff] [blame] | 97 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, EventOverriding) { |
| 98 | ASSERT_TRUE(RunExtensionTest("bindings/event_overriding")) << message_; |
| 99 | } |
| 100 | |
kalman | 1bd5b18 | 2015-01-13 19:01:18 | [diff] [blame] | 101 | // Tests the effectiveness of the 'nocompile' feature file property. |
| 102 | // Regression test for http://crbug.com/356133. |
| 103 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, Nocompile) { |
| 104 | ASSERT_TRUE(RunExtensionSubtest("bindings/nocompile", "page.html")) |
| 105 | << message_; |
| 106 | } |
| 107 | |
rdevlin.cronin | 2ba3c88d | 2015-03-03 01:18:22 | [diff] [blame] | 108 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ApiEnums) { |
| 109 | ASSERT_TRUE(RunExtensionTest("bindings/api_enums")) << message_; |
| 110 | }; |
| 111 | |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 112 | // Regression test for http://crbug.com/504011 - proper access checks on |
| 113 | // getModuleSystem(). |
| 114 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ModuleSystem) { |
| 115 | ASSERT_TRUE(RunExtensionTest("bindings/module_system")) << message_; |
| 116 | } |
| 117 | |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 118 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoExportOverriding) { |
tfarina | 8dfd114 | 2015-11-30 22:00:26 | [diff] [blame] | 119 | ASSERT_TRUE(embedded_test_server()->Start()); |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 120 | |
| 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.cronin | 415b73b | 2015-11-13 01:14:47 | [diff] [blame] | 142 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoGinDefineOverriding) { |
tfarina | 8dfd114 | 2015-11-30 22:00:26 | [diff] [blame] | 143 | ASSERT_TRUE(embedded_test_server()->Start()); |
rdevlin.cronin | 415b73b | 2015-11-13 01:14:47 | [diff] [blame] | 144 | |
| 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.cronin | a5ecbc8 | 2015-10-29 23:41:29 | [diff] [blame] | 168 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, HandlerFunctionTypeChecking) { |
tfarina | 8dfd114 | 2015-11-30 22:00:26 | [diff] [blame] | 169 | ASSERT_TRUE(embedded_test_server()->Start()); |
rdevlin.cronin | a5ecbc8 | 2015-10-29 23:41:29 | [diff] [blame] | 170 | 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.cronin | 75b803b | 2016-03-02 00:13:47 | [diff] [blame] | 187 | IN_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 | |
asargent | 79b64c3 | 2016-08-04 17:17:14 | [diff] [blame^] | 212 | class 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"). |
| 229 | IN_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] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 272 | } // namespace |
| 273 | } // namespace extensions |