[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 | e6e2002 | 2017-06-13 18:23:40 | [diff] [blame^] | 20 | #include "net/dns/mock_host_resolver.h" |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 21 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 22 | |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 23 | namespace extensions { |
| 24 | namespace { |
| 25 | |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 26 | class ExtensionBindingsApiTest : public ExtensionApiTest { |
| 27 | public: |
| 28 | void SetUpOnMainThread() override { |
rdevlin.cronin | 17160cc6 | 2016-11-23 05:33:08 | [diff] [blame] | 29 | ExtensionApiTest::SetUpOnMainThread(); |
rdevlin.cronin | e6e2002 | 2017-06-13 18:23:40 | [diff] [blame^] | 30 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 31 | ASSERT_TRUE(StartEmbeddedTestServer()); |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 32 | } |
| 33 | }; |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 34 | |
| 35 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
[email protected] | 7eef394 | 2013-08-14 02:53:49 | [diff] [blame] | 36 | 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 | |
| 43 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 44 | ExceptionInHandlerShouldNotCrash) { |
[email protected] | c80b8ee | 2011-12-03 04:26:52 | [diff] [blame] | 45 | ASSERT_TRUE(RunExtensionSubtest( |
| 46 | "bindings/exception_in_handler_should_not_crash", |
| 47 | "page.html")) << message_; |
| 48 | } |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 49 | |
| 50 | // Tests that an error raised during an async function still fires |
[email protected] | 754ea8b7 | 2013-01-08 15:10:31 | [diff] [blame] | 51 | // the callback, but sets chrome.runtime.lastError. |
[email protected] | fc03448 | 2013-08-09 20:25:14 | [diff] [blame] | 52 | // FIXME should be in ExtensionBindingsApiTest. |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 53 | IN_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] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 58 | extensions::ProcessManager* manager = |
reillyg | 0ea3fa90 | 2014-10-28 15:30:23 | [diff] [blame] | 59 | extensions::ProcessManager::Get(browser()->profile()); |
[email protected] | 3a1dc57 | 2012-07-31 22:25:13 | [diff] [blame] | 60 | extensions::ExtensionHost* host = FindHostWithPath(manager, "/bg.html", 1); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 61 | |
| 62 | bool result = false; |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 63 | ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 64 | host->render_view_host(), "testLastError()", &result)); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 65 | EXPECT_TRUE(result); |
| 66 | } |
[email protected] | 52eafbd | 2013-04-03 04:43:19 | [diff] [blame] | 67 | |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 68 | // Regression test that we don't delete our own bindings with about:blank |
| 69 | // iframes. |
| 70 | IN_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 | |
| 88 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
| 89 | InternalAPIsNotOnChromeObject) { |
[email protected] | 52eafbd | 2013-04-03 04:43:19 | [diff] [blame] | 90 | ASSERT_TRUE(RunExtensionSubtest( |
| 91 | "bindings/internal_apis_not_on_chrome_object", |
| 92 | "page.html")) << message_; |
| 93 | } |
[email protected] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 94 | |
[email protected] | fc03448 | 2013-08-09 20:25:14 | [diff] [blame] | 95 | // Tests that we don't override events when bindings are re-injected. |
| 96 | // Regression test for http://crbug.com/269149. |
rpaquay | 96bf3b7d | 2014-11-26 00:19:08 | [diff] [blame] | 97 | // Regression test for http://crbug.com/436593. |
[email protected] | fc03448 | 2013-08-09 20:25:14 | [diff] [blame] | 98 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, EventOverriding) { |
| 99 | ASSERT_TRUE(RunExtensionTest("bindings/event_overriding")) << message_; |
| 100 | } |
| 101 | |
kalman | 1bd5b18 | 2015-01-13 19:01:18 | [diff] [blame] | 102 | // Tests the effectiveness of the 'nocompile' feature file property. |
| 103 | // Regression test for http://crbug.com/356133. |
| 104 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, Nocompile) { |
| 105 | ASSERT_TRUE(RunExtensionSubtest("bindings/nocompile", "page.html")) |
| 106 | << message_; |
| 107 | } |
| 108 | |
rdevlin.cronin | 2ba3c88d | 2015-03-03 01:18:22 | [diff] [blame] | 109 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ApiEnums) { |
| 110 | ASSERT_TRUE(RunExtensionTest("bindings/api_enums")) << message_; |
| 111 | }; |
| 112 | |
jochen | 7923c2a | 2015-07-14 10:04:45 | [diff] [blame] | 113 | // Regression test for http://crbug.com/504011 - proper access checks on |
| 114 | // getModuleSystem(). |
| 115 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ModuleSystem) { |
| 116 | ASSERT_TRUE(RunExtensionTest("bindings/module_system")) << message_; |
| 117 | } |
| 118 | |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 119 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoExportOverriding) { |
rdevlin.cronin | 83a4b3a | 2015-10-28 21:43:58 | [diff] [blame] | 120 | // 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.cronin | 415b73b | 2015-11-13 01:14:47 | [diff] [blame] | 141 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoGinDefineOverriding) { |
rdevlin.cronin | 415b73b | 2015-11-13 01:14:47 | [diff] [blame] | 142 | // 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.cronin | a5ecbc8 | 2015-10-29 23:41:29 | [diff] [blame] | 165 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, HandlerFunctionTypeChecking) { |
rdevlin.cronin | a5ecbc8 | 2015-10-29 23:41:29 | [diff] [blame] | 166 | 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.cronin | 75b803b | 2016-03-02 00:13:47 | [diff] [blame] | 183 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
| 184 | MoreNativeFunctionInterceptionTests) { |
rdevlin.cronin | 75b803b | 2016-03-02 00:13:47 | [diff] [blame] | 185 | // 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 | |
asargent | 79b64c3 | 2016-08-04 17:17:14 | [diff] [blame] | 206 | class 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"). |
| 223 | IN_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. |
asargent | 79b64c3 | 2016-08-04 17:17:14 | [diff] [blame] | 241 | 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.cronin | 741da00 | 2017-04-24 20:27:41 | [diff] [blame] | 265 | IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, TestFreezingChrome) { |
rdevlin.cronin | 741da00 | 2017-04-24 20:27:41 | [diff] [blame] | 266 | 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.cronin | e6e2002 | 2017-06-13 18:23:40 | [diff] [blame^] | 274 | // Tests interaction with event filter parsing. |
| 275 | IN_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] | adafe5b | 2013-08-09 10:35:04 | [diff] [blame] | 287 | } // namespace |
| 288 | } // namespace extensions |