| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Contains holistic tests of the bindings infrastructure | 5 // Contains holistic tests of the bindings infrastructure |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/permissions/permissions_api.h" | 7 #include "chrome/browser/extensions/api/permissions/permissions_api.h" |
| 8 #include "chrome/browser/extensions/extension_apitest.h" | 8 #include "chrome/browser/extensions/extension_apitest.h" |
| 9 #include "chrome/browser/net/url_request_mock_util.h" | 9 #include "chrome/browser/net/url_request_mock_util.h" |
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/test/base/ui_test_utils.h" | 13 #include "chrome/test/base/ui_test_utils.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/test/browser_test_utils.h" | 15 #include "content/public/test/browser_test_utils.h" |
| 16 #include "extensions/browser/extension_host.h" | 16 #include "extensions/browser/extension_host.h" |
| 17 #include "extensions/browser/process_manager.h" | 17 #include "extensions/browser/process_manager.h" |
| 18 #include "extensions/test/extension_test_message_listener.h" | 18 #include "extensions/test/extension_test_message_listener.h" |
| 19 #include "extensions/test/result_catcher.h" | 19 #include "extensions/test/result_catcher.h" |
| 20 #include "net/dns/mock_host_resolver.h" | |
| 21 #include "net/test/embedded_test_server/embedded_test_server.h" | 20 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 22 | 21 |
| 23 namespace extensions { | 22 namespace extensions { |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 class ExtensionBindingsApiTest : public ExtensionApiTest { | 25 class ExtensionBindingsApiTest : public ExtensionApiTest { |
| 27 public: | 26 public: |
| 28 void SetUpOnMainThread() override { | 27 void SetUpOnMainThread() override { |
| 29 ExtensionApiTest::SetUpOnMainThread(); | 28 ExtensionApiTest::SetUpOnMainThread(); |
| 30 host_resolver()->AddRule("*", "127.0.0.1"); | |
| 31 ASSERT_TRUE(StartEmbeddedTestServer()); | |
| 32 | |
| 33 content::BrowserThread::PostTask( | 29 content::BrowserThread::PostTask( |
| 34 content::BrowserThread::IO, FROM_HERE, | 30 content::BrowserThread::IO, FROM_HERE, |
| 35 base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); | 31 base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| 36 } | 32 } |
| 37 }; | 33 }; |
| 38 | 34 |
| 39 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, | 35 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
| 40 UnavailableBindingsNeverRegistered) { | 36 UnavailableBindingsNeverRegistered) { |
| 41 // Test will request the 'storage' permission. | 37 // Test will request the 'storage' permission. |
| 42 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true); | 38 PermissionsRequestFunction::SetIgnoreUserGestureForTests(true); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 ASSERT_TRUE(RunExtensionTest("bindings/api_enums")) << message_; | 110 ASSERT_TRUE(RunExtensionTest("bindings/api_enums")) << message_; |
| 115 }; | 111 }; |
| 116 | 112 |
| 117 // Regression test for http://crbug.com/504011 - proper access checks on | 113 // Regression test for http://crbug.com/504011 - proper access checks on |
| 118 // getModuleSystem(). | 114 // getModuleSystem(). |
| 119 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ModuleSystem) { | 115 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, ModuleSystem) { |
| 120 ASSERT_TRUE(RunExtensionTest("bindings/module_system")) << message_; | 116 ASSERT_TRUE(RunExtensionTest("bindings/module_system")) << message_; |
| 121 } | 117 } |
| 122 | 118 |
| 123 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoExportOverriding) { | 119 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoExportOverriding) { |
| 120 ASSERT_TRUE(embedded_test_server()->Start()); |
| 121 |
| 124 // We need to create runtime bindings in the web page. An extension that's | 122 // We need to create runtime bindings in the web page. An extension that's |
| 125 // externally connectable will do that for us. | 123 // externally connectable will do that for us. |
| 126 ASSERT_TRUE(LoadExtension( | 124 ASSERT_TRUE(LoadExtension( |
| 127 test_data_dir_.AppendASCII("bindings") | 125 test_data_dir_.AppendASCII("bindings") |
| 128 .AppendASCII("externally_connectable_everywhere"))); | 126 .AppendASCII("externally_connectable_everywhere"))); |
| 129 | 127 |
| 130 ui_test_utils::NavigateToURL( | 128 ui_test_utils::NavigateToURL( |
| 131 browser(), | 129 browser(), |
| 132 embedded_test_server()->GetURL( | 130 embedded_test_server()->GetURL( |
| 133 "/extensions/api_test/bindings/override_exports.html")); | 131 "/extensions/api_test/bindings/override_exports.html")); |
| 134 | 132 |
| 135 // See chrome/test/data/extensions/api_test/bindings/override_exports.html. | 133 // See chrome/test/data/extensions/api_test/bindings/override_exports.html. |
| 136 std::string result; | 134 std::string result; |
| 137 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 135 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 138 browser()->tab_strip_model()->GetActiveWebContents(), | 136 browser()->tab_strip_model()->GetActiveWebContents(), |
| 139 "window.domAutomationController.send(" | 137 "window.domAutomationController.send(" |
| 140 "document.getElementById('status').textContent.trim());", | 138 "document.getElementById('status').textContent.trim());", |
| 141 &result)); | 139 &result)); |
| 142 EXPECT_EQ("success", result); | 140 EXPECT_EQ("success", result); |
| 143 } | 141 } |
| 144 | 142 |
| 145 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoGinDefineOverriding) { | 143 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, NoGinDefineOverriding) { |
| 144 ASSERT_TRUE(embedded_test_server()->Start()); |
| 145 |
| 146 // We need to create runtime bindings in the web page. An extension that's | 146 // We need to create runtime bindings in the web page. An extension that's |
| 147 // externally connectable will do that for us. | 147 // externally connectable will do that for us. |
| 148 ASSERT_TRUE(LoadExtension( | 148 ASSERT_TRUE(LoadExtension( |
| 149 test_data_dir_.AppendASCII("bindings") | 149 test_data_dir_.AppendASCII("bindings") |
| 150 .AppendASCII("externally_connectable_everywhere"))); | 150 .AppendASCII("externally_connectable_everywhere"))); |
| 151 | 151 |
| 152 ui_test_utils::NavigateToURL( | 152 ui_test_utils::NavigateToURL( |
| 153 browser(), | 153 browser(), |
| 154 embedded_test_server()->GetURL( | 154 embedded_test_server()->GetURL( |
| 155 "/extensions/api_test/bindings/override_gin_define.html")); | 155 "/extensions/api_test/bindings/override_gin_define.html")); |
| 156 ASSERT_FALSE( | 156 ASSERT_FALSE( |
| 157 browser()->tab_strip_model()->GetActiveWebContents()->IsCrashed()); | 157 browser()->tab_strip_model()->GetActiveWebContents()->IsCrashed()); |
| 158 | 158 |
| 159 // See chrome/test/data/extensions/api_test/bindings/override_gin_define.html. | 159 // See chrome/test/data/extensions/api_test/bindings/override_gin_define.html. |
| 160 std::string result; | 160 std::string result; |
| 161 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 161 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 162 browser()->tab_strip_model()->GetActiveWebContents(), | 162 browser()->tab_strip_model()->GetActiveWebContents(), |
| 163 "window.domAutomationController.send(" | 163 "window.domAutomationController.send(" |
| 164 "document.getElementById('status').textContent.trim());", | 164 "document.getElementById('status').textContent.trim());", |
| 165 &result)); | 165 &result)); |
| 166 EXPECT_EQ("success", result); | 166 EXPECT_EQ("success", result); |
| 167 } | 167 } |
| 168 | 168 |
| 169 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, HandlerFunctionTypeChecking) { | 169 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, HandlerFunctionTypeChecking) { |
| 170 ASSERT_TRUE(embedded_test_server()->Start()); |
| 170 ui_test_utils::NavigateToURL( | 171 ui_test_utils::NavigateToURL( |
| 171 browser(), | 172 browser(), |
| 172 embedded_test_server()->GetURL( | 173 embedded_test_server()->GetURL( |
| 173 "/extensions/api_test/bindings/handler_function_type_checking.html")); | 174 "/extensions/api_test/bindings/handler_function_type_checking.html")); |
| 174 content::WebContents* web_contents = | 175 content::WebContents* web_contents = |
| 175 browser()->tab_strip_model()->GetActiveWebContents(); | 176 browser()->tab_strip_model()->GetActiveWebContents(); |
| 176 EXPECT_FALSE(web_contents->IsCrashed()); | 177 EXPECT_FALSE(web_contents->IsCrashed()); |
| 177 // See handler_function_type_checking.html. | 178 // See handler_function_type_checking.html. |
| 178 std::string result; | 179 std::string result; |
| 179 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 180 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 180 web_contents, | 181 web_contents, |
| 181 "window.domAutomationController.send(" | 182 "window.domAutomationController.send(" |
| 182 "document.getElementById('status').textContent.trim());", | 183 "document.getElementById('status').textContent.trim());", |
| 183 &result)); | 184 &result)); |
| 184 EXPECT_EQ("success", result); | 185 EXPECT_EQ("success", result); |
| 185 } | 186 } |
| 186 | 187 |
| 187 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, | 188 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, |
| 188 MoreNativeFunctionInterceptionTests) { | 189 MoreNativeFunctionInterceptionTests) { |
| 190 ASSERT_TRUE(embedded_test_server()->Start()); |
| 191 |
| 189 // We need to create runtime bindings in the web page. An extension that's | 192 // We need to create runtime bindings in the web page. An extension that's |
| 190 // externally connectable will do that for us. | 193 // externally connectable will do that for us. |
| 191 ASSERT_TRUE( | 194 ASSERT_TRUE( |
| 192 LoadExtension(test_data_dir_.AppendASCII("bindings") | 195 LoadExtension(test_data_dir_.AppendASCII("bindings") |
| 193 .AppendASCII("externally_connectable_everywhere"))); | 196 .AppendASCII("externally_connectable_everywhere"))); |
| 194 | 197 |
| 195 ui_test_utils::NavigateToURL( | 198 ui_test_utils::NavigateToURL( |
| 196 browser(), | 199 browser(), |
| 197 embedded_test_server()->GetURL( | 200 embedded_test_server()->GetURL( |
| 198 "/extensions/api_test/bindings/function_interceptions.html")); | 201 "/extensions/api_test/bindings/function_interceptions.html")); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 ExtensionTestMessageListener receiver_ready("receiver_ready", false); | 238 ExtensionTestMessageListener receiver_ready("receiver_ready", false); |
| 236 const Extension* receiver = | 239 const Extension* receiver = |
| 237 LoadExtension(test_data_dir_.AppendASCII("bindings") | 240 LoadExtension(test_data_dir_.AppendASCII("bindings") |
| 238 .AppendASCII("external_message_listener")); | 241 .AppendASCII("external_message_listener")); |
| 239 ASSERT_NE(nullptr, receiver); | 242 ASSERT_NE(nullptr, receiver); |
| 240 ASSERT_TRUE(receiver_ready.WaitUntilSatisfied()); | 243 ASSERT_TRUE(receiver_ready.WaitUntilSatisfied()); |
| 241 | 244 |
| 242 // Load the web page which tries to impersonate the sender extension via | 245 // Load the web page which tries to impersonate the sender extension via |
| 243 // scripting iframes/child windows before they finish navigating to pages | 246 // scripting iframes/child windows before they finish navigating to pages |
| 244 // within the sender extension. | 247 // within the sender extension. |
| 248 ASSERT_TRUE(embedded_test_server()->Start()); |
| 245 ui_test_utils::NavigateToURL( | 249 ui_test_utils::NavigateToURL( |
| 246 browser(), | 250 browser(), |
| 247 embedded_test_server()->GetURL( | 251 embedded_test_server()->GetURL( |
| 248 "/extensions/api_test/bindings/frames_before_navigation.html")); | 252 "/extensions/api_test/bindings/frames_before_navigation.html")); |
| 249 | 253 |
| 250 bool page_success = false; | 254 bool page_success = false; |
| 251 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | 255 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 252 browser()->tab_strip_model()->GetWebContentsAt(0), "getResult()", | 256 browser()->tab_strip_model()->GetWebContentsAt(0), "getResult()", |
| 253 &page_success)); | 257 &page_success)); |
| 254 EXPECT_TRUE(page_success); | 258 EXPECT_TRUE(page_success); |
| 255 | 259 |
| 256 // Reply to |sender|, causing it to send a message over to |receiver|, and | 260 // Reply to |sender|, causing it to send a message over to |receiver|, and |
| 257 // then ask |receiver| for the total message count. It should be 1 since | 261 // then ask |receiver| for the total message count. It should be 1 since |
| 258 // |receiver| should not have received any impersonated messages. | 262 // |receiver| should not have received any impersonated messages. |
| 259 sender_ready.Reply(receiver->id()); | 263 sender_ready.Reply(receiver->id()); |
| 260 int message_count = 0; | 264 int message_count = 0; |
| 261 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( | 265 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( |
| 262 ProcessManager::Get(profile()) | 266 ProcessManager::Get(profile()) |
| 263 ->GetBackgroundHostForExtension(receiver->id()) | 267 ->GetBackgroundHostForExtension(receiver->id()) |
| 264 ->host_contents(), | 268 ->host_contents(), |
| 265 "getMessageCountAfterReceivingRealSenderMessage()", &message_count)); | 269 "getMessageCountAfterReceivingRealSenderMessage()", &message_count)); |
| 266 EXPECT_EQ(1, message_count); | 270 EXPECT_EQ(1, message_count); |
| 267 } | 271 } |
| 268 | 272 |
| 269 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, TestFreezingChrome) { | 273 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, TestFreezingChrome) { |
| 274 ASSERT_TRUE(embedded_test_server()->Start()); |
| 270 ui_test_utils::NavigateToURL( | 275 ui_test_utils::NavigateToURL( |
| 271 browser(), embedded_test_server()->GetURL( | 276 browser(), embedded_test_server()->GetURL( |
| 272 "/extensions/api_test/bindings/freeze.html")); | 277 "/extensions/api_test/bindings/freeze.html")); |
| 273 content::WebContents* web_contents = | 278 content::WebContents* web_contents = |
| 274 browser()->tab_strip_model()->GetActiveWebContents(); | 279 browser()->tab_strip_model()->GetActiveWebContents(); |
| 275 ASSERT_FALSE(web_contents->IsCrashed()); | 280 ASSERT_FALSE(web_contents->IsCrashed()); |
| 276 } | 281 } |
| 277 | 282 |
| 278 // Tests interaction with event filter parsing. | |
| 279 IN_PROC_BROWSER_TEST_F(ExtensionBindingsApiTest, TestEventFilterParsing) { | |
| 280 ExtensionTestMessageListener listener("ready", false); | |
| 281 ASSERT_TRUE( | |
| 282 LoadExtension(test_data_dir_.AppendASCII("bindings/event_filter"))); | |
| 283 ASSERT_TRUE(listener.WaitUntilSatisfied()); | |
| 284 | |
| 285 ResultCatcher catcher; | |
| 286 ui_test_utils::NavigateToURL( | |
| 287 browser(), embedded_test_server()->GetURL("example.com", "/title1.html")); | |
| 288 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | |
| 289 } | |
| 290 | |
| 291 } // namespace | 283 } // namespace |
| 292 } // namespace extensions | 284 } // namespace extensions |
| OLD | NEW |