[email protected] | c107d49 | 2012-01-30 17:59:16 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 912256b3 | 2009-09-18 09:47:35 | [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 | |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 5 | #include "base/strings/string_number_conversions.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 6 | #include "build/build_config.h" |
[email protected] | 912256b3 | 2009-09-18 09:47:35 | [diff] [blame] | 7 | #include "chrome/browser/extensions/extension_apitest.h" |
Karan Bhatia | 7a33a542 | 2018-04-05 00:41:12 | [diff] [blame] | 8 | #include "chrome/test/base/ui_test_utils.h" |
| 9 | #include "net/base/filename_util.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 10 | #include "net/dns/mock_host_resolver.h" |
[email protected] | 912256b3 | 2009-09-18 09:47:35 | [diff] [blame] | 11 | |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 12 | class ExecuteScriptApiTest : public extensions::ExtensionApiTest { |
[email protected] | 4712e85 | 2010-11-24 13:17:19 | [diff] [blame] | 13 | protected: |
jam | bb11ed74 | 2017-05-01 17:27:59 | [diff] [blame] | 14 | void SetUpOnMainThread() override { |
Devlin Cronin | ef3e37e | 2018-05-14 23:47:24 | [diff] [blame] | 15 | extensions::ExtensionApiTest::SetUpOnMainThread(); |
[email protected] | 4712e85 | 2010-11-24 13:17:19 | [diff] [blame] | 16 | // We need a.com to be a little bit slow to trigger a race condition. |
| 17 | host_resolver()->AddRuleWithLatency("a.com", "127.0.0.1", 500); |
jam | bb11ed74 | 2017-05-01 17:27:59 | [diff] [blame] | 18 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 19 | ASSERT_TRUE(StartEmbeddedTestServer()); |
[email protected] | 4712e85 | 2010-11-24 13:17:19 | [diff] [blame] | 20 | } |
| 21 | }; |
[email protected] | 912256b3 | 2009-09-18 09:47:35 | [diff] [blame] | 22 | |
[email protected] | 59d8048a | 2012-02-09 19:27:00 | [diff] [blame] | 23 | // If failing, mark disabled and update http://crbug.com/92105. |
[email protected] | de4381e | 2012-11-26 22:43:14 | [diff] [blame] | 24 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptBasic) { |
[email protected] | ad6530c | 2009-12-17 19:43:56 | [diff] [blame] | 25 | ASSERT_TRUE(RunExtensionTest("executescript/basic")) << message_; |
[email protected] | 4712e85 | 2010-11-24 13:17:19 | [diff] [blame] | 26 | } |
| 27 | |
rob | ca0af32 | 2014-12-08 12:42:59 | [diff] [blame] | 28 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptBadEncoding) { |
rob | ca0af32 | 2014-12-08 12:42:59 | [diff] [blame] | 29 | // data/extensions/api_test/../bad = data/extensions/bad |
| 30 | ASSERT_TRUE(RunExtensionTest("../bad")) << message_; |
| 31 | } |
| 32 | |
[email protected] | 59d8048a | 2012-02-09 19:27:00 | [diff] [blame] | 33 | // If failing, mark disabled and update http://crbug.com/92105. |
[email protected] | de4381e | 2012-11-26 22:43:14 | [diff] [blame] | 34 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptInFrame) { |
[email protected] | ad6530c | 2009-12-17 19:43:56 | [diff] [blame] | 35 | ASSERT_TRUE(RunExtensionTest("executescript/in_frame")) << message_; |
[email protected] | 4712e85 | 2010-11-24 13:17:19 | [diff] [blame] | 36 | } |
| 37 | |
rob | 52277c8 | 2016-02-07 17:28:57 | [diff] [blame] | 38 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptByFrameId) { |
rob | 52277c8 | 2016-02-07 17:28:57 | [diff] [blame] | 39 | ASSERT_TRUE(RunExtensionTest("executescript/frame_id")) << message_; |
| 40 | } |
| 41 | |
[email protected] | 110642b | 2013-02-06 21:25:52 | [diff] [blame] | 42 | // Fails often on Windows. |
| 43 | // http://crbug.com/174715 |
| 44 | #if defined(OS_WIN) |
| 45 | #define MAYBE_ExecuteScriptPermissions DISABLED_ExecuteScriptPermissions |
| 46 | #else |
| 47 | #define MAYBE_ExecuteScriptPermissions ExecuteScriptPermissions |
| 48 | #endif // defined(OS_WIN) |
| 49 | |
| 50 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, MAYBE_ExecuteScriptPermissions) { |
[email protected] | 61f5a7b | 2009-12-22 22:21:20 | [diff] [blame] | 51 | ASSERT_TRUE(RunExtensionTest("executescript/permissions")) << message_; |
[email protected] | 912256b3 | 2009-09-18 09:47:35 | [diff] [blame] | 52 | } |
[email protected] | a7664e1 | 2010-04-08 20:37:43 | [diff] [blame] | 53 | |
[email protected] | 59d8048a | 2012-02-09 19:27:00 | [diff] [blame] | 54 | // If failing, mark disabled and update http://crbug.com/84760. |
[email protected] | f34706be | 2012-09-04 07:32:09 | [diff] [blame] | 55 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptFileAfterClose) { |
[email protected] | a7664e1 | 2010-04-08 20:37:43 | [diff] [blame] | 56 | ASSERT_TRUE(RunExtensionTest("executescript/file_after_close")) << message_; |
| 57 | } |
[email protected] | 10e40f31 | 2011-02-01 03:54:20 | [diff] [blame] | 58 | |
[email protected] | 59d8048a | 2012-02-09 19:27:00 | [diff] [blame] | 59 | // If crashing, mark disabled and update http://crbug.com/67774. |
| 60 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptFragmentNavigation) { |
thestig | 4b36dd3 | 2014-10-31 20:30:19 | [diff] [blame] | 61 | const char extension_name[] = "executescript/fragment"; |
[email protected] | 10e40f31 | 2011-02-01 03:54:20 | [diff] [blame] | 62 | ASSERT_TRUE(RunExtensionTest(extension_name)) << message_; |
| 63 | } |
[email protected] | 4b96a99 | 2011-04-07 19:47:07 | [diff] [blame] | 64 | |
[email protected] | e1b3f40a | 2013-05-24 10:40:08 | [diff] [blame] | 65 | // Fails often on Windows dbg bots. http://crbug.com/177163 |
| 66 | #if defined(OS_WIN) |
| 67 | #define MAYBE_NavigationRaceExecuteScript DISABLED_NavigationRaceExecuteScript |
| 68 | #else |
| 69 | #define MAYBE_NavigationRaceExecuteScript NavigationRaceExecuteScript |
| 70 | #endif // defined(OS_WIN) |
| 71 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, |
| 72 | MAYBE_NavigationRaceExecuteScript) { |
[email protected] | 4b96a99 | 2011-04-07 19:47:07 | [diff] [blame] | 73 | ASSERT_TRUE(RunExtensionSubtest("executescript/navigation_race", |
| 74 | "execute_script.html")) << message_; |
[email protected] | c30cf4b | 2011-06-09 01:25:10 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | 59d8048a | 2012-02-09 19:27:00 | [diff] [blame] | 77 | // If failing, mark disabled and update http://crbug.com/92105. |
| 78 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptFrameAfterLoad) { |
[email protected] | d45ce25 | 2011-04-11 18:20:50 | [diff] [blame] | 79 | ASSERT_TRUE(RunExtensionTest("executescript/frame_after_load")) << message_; |
| 80 | } |
[email protected] | e7d6ec8 | 2012-04-13 19:00:24 | [diff] [blame] | 81 | |
rob | 7ad924c | 2016-01-30 18:22:36 | [diff] [blame] | 82 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, FrameWithHttp204) { |
rob | 7ad924c | 2016-01-30 18:22:36 | [diff] [blame] | 83 | ASSERT_TRUE(RunExtensionTest("executescript/http204")) << message_; |
| 84 | } |
| 85 | |
[email protected] | e7d6ec8 | 2012-04-13 19:00:24 | [diff] [blame] | 86 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptRunAt) { |
[email protected] | e7d6ec8 | 2012-04-13 19:00:24 | [diff] [blame] | 87 | ASSERT_TRUE(RunExtensionTest("executescript/run_at")) << message_; |
| 88 | } |
[email protected] | cab8cd98 | 2012-07-20 20:57:03 | [diff] [blame] | 89 | |
Manish Jethani | 9494d72 | 2018-01-20 00:28:47 | [diff] [blame] | 90 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptCSSOrigin) { |
| 91 | ASSERT_TRUE(RunExtensionTest("executescript/css_origin")) << message_; |
| 92 | } |
| 93 | |
[email protected] | cab8cd98 | 2012-07-20 20:57:03 | [diff] [blame] | 94 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptCallback) { |
[email protected] | cab8cd98 | 2012-07-20 20:57:03 | [diff] [blame] | 95 | ASSERT_TRUE(RunExtensionTest("executescript/callback")) << message_; |
| 96 | } |
[email protected] | 0df4943 | 2014-03-04 01:02:50 | [diff] [blame] | 97 | |
| 98 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, UserGesture) { |
[email protected] | 0df4943 | 2014-03-04 01:02:50 | [diff] [blame] | 99 | ASSERT_TRUE(RunExtensionTest("executescript/user_gesture")) << message_; |
| 100 | } |
rdevlin.cronin | 7743355 | 2015-06-23 18:27:40 | [diff] [blame] | 101 | |
| 102 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, InjectIntoSubframesOnLoad) { |
rdevlin.cronin | 7743355 | 2015-06-23 18:27:40 | [diff] [blame] | 103 | ASSERT_TRUE(RunExtensionTest("executescript/subframes_on_load")) << message_; |
| 104 | } |
rdevlin.cronin | 3ae4a3201 | 2015-06-30 17:43:19 | [diff] [blame] | 105 | |
| 106 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, RemovedFrames) { |
rdevlin.cronin | 3ae4a3201 | 2015-06-30 17:43:19 | [diff] [blame] | 107 | ASSERT_TRUE(RunExtensionTest("executescript/removed_frames")) << message_; |
| 108 | } |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 109 | |
Karan Bhatia | 7a33a542 | 2018-04-05 00:41:12 | [diff] [blame] | 110 | // Ensure that an extension can inject a script in a file frame provided it has |
| 111 | // access to file urls enabled and the necessary host permissions. |
| 112 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, InjectScriptInFileFrameAllowed) { |
| 113 | // Navigate to a file url. The extension will subsequently try to inject a |
| 114 | // script into it. |
| 115 | base::FilePath test_file = |
| 116 | test_data_dir_.DirName().AppendASCII("test_file.txt"); |
| 117 | ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); |
| 118 | |
| 119 | SetCustomArg("ALLOWED"); |
| 120 | ASSERT_TRUE(RunExtensionTest("executescript/file_access")) << message_; |
| 121 | } |
| 122 | |
| 123 | // Ensure that an extension can't inject a script in a file frame if it doesn't |
| 124 | // have file access. |
| 125 | IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, InjectScriptInFileFrameDenied) { |
| 126 | // Navigate to a file url. The extension will subsequently try to inject a |
| 127 | // script into it. |
| 128 | base::FilePath test_file = |
| 129 | test_data_dir_.DirName().AppendASCII("test_file.txt"); |
| 130 | ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); |
| 131 | |
| 132 | SetCustomArg("DENIED"); |
| 133 | ASSERT_TRUE(RunExtensionTestNoFileAccess("executescript/file_access")) |
| 134 | << message_; |
| 135 | } |
| 136 | |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 137 | // If tests time out because it takes too long to run them, then this value can |
| 138 | // be increased to split the DestructiveScriptTest tests in approximately equal |
| 139 | // parts. Each part takes approximately the same time to run. |
| 140 | const int kDestructiveScriptTestBucketCount = 1; |
| 141 | |
| 142 | class DestructiveScriptTest : public ExecuteScriptApiTest, |
| 143 | public testing::WithParamInterface<int> { |
| 144 | protected: |
| 145 | // The test extension selects the sub test based on the host name. |
| 146 | bool RunSubtest(const std::string& test_host) { |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 147 | return RunExtensionSubtest( |
| 148 | "executescript/destructive", |
Raul Tambre | fff51b75 | 2019-02-04 13:09:47 | [diff] [blame] | 149 | "test.html?" + test_host + "#bucketcount=" + |
| 150 | base::NumberToString(kDestructiveScriptTestBucketCount) + |
| 151 | "&bucketindex=" + base::NumberToString(GetParam())); |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 152 | } |
| 153 | }; |
| 154 | |
| 155 | // Removes the frame as soon as the content script is executed. |
| 156 | IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, SynchronousRemoval) { |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 157 | ASSERT_TRUE(RunSubtest("synchronous")) << message_; |
| 158 | } |
| 159 | |
| 160 | // Removes the frame at the frame's first scheduled microtask. |
| 161 | IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, MicrotaskRemoval) { |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 162 | ASSERT_TRUE(RunSubtest("microtask")) << message_; |
| 163 | } |
| 164 | |
| 165 | // Removes the frame at the frame's first scheduled macrotask. |
bmcquade | e56b2dd0 | 2017-05-16 16:12:01 | [diff] [blame] | 166 | IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, MacrotaskRemoval) { |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 167 | ASSERT_TRUE(RunSubtest("macrotask")) << message_; |
| 168 | } |
| 169 | |
| 170 | // Removes the frame at the first DOMNodeInserted event. |
| 171 | IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, DOMNodeInserted1) { |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 172 | ASSERT_TRUE(RunSubtest("domnodeinserted1")) << message_; |
| 173 | } |
| 174 | |
| 175 | // Removes the frame at the second DOMNodeInserted event. |
| 176 | IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, DOMNodeInserted2) { |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 177 | ASSERT_TRUE(RunSubtest("domnodeinserted2")) << message_; |
| 178 | } |
| 179 | |
| 180 | // Removes the frame at the third DOMNodeInserted event. |
| 181 | IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, DOMNodeInserted3) { |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 182 | ASSERT_TRUE(RunSubtest("domnodeinserted3")) << message_; |
| 183 | } |
| 184 | |
| 185 | // Removes the frame at the first DOMSubtreeModified event. |
| 186 | IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, DOMSubtreeModified1) { |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 187 | ASSERT_TRUE(RunSubtest("domsubtreemodified1")) << message_; |
| 188 | } |
| 189 | |
| 190 | // Removes the frame at the second DOMSubtreeModified event. |
| 191 | IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, DOMSubtreeModified2) { |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 192 | ASSERT_TRUE(RunSubtest("domsubtreemodified2")) << message_; |
| 193 | } |
| 194 | |
| 195 | // Removes the frame at the third DOMSubtreeModified event. |
| 196 | IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, DOMSubtreeModified3) { |
rob | 43ea064 | 2016-03-19 01:05:01 | [diff] [blame] | 197 | ASSERT_TRUE(RunSubtest("domsubtreemodified3")) << message_; |
| 198 | } |
| 199 | |
Victor Costan | 4a060e8 | 2019-01-28 18:25:34 | [diff] [blame] | 200 | INSTANTIATE_TEST_SUITE_P(ExecuteScriptApiTest, |
| 201 | DestructiveScriptTest, |
| 202 | ::testing::Range(0, |
| 203 | kDestructiveScriptTestBucketCount)); |