blob: d42bee153ddf9f050be28fb3aa011e710fce2575 [file] [log] [blame]
[email protected]c107d492012-01-30 17:59:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]912256b32009-09-18 09:47:352// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rob43ea0642016-03-19 01:05:015#include "base/strings/string_number_conversions.h"
avia2f4804a2015-12-24 23:11:136#include "build/build_config.h"
[email protected]912256b32009-09-18 09:47:357#include "chrome/browser/extensions/extension_apitest.h"
Karan Bhatia7a33a5422018-04-05 00:41:128#include "chrome/test/base/ui_test_utils.h"
9#include "net/base/filename_util.h"
[email protected]f2cb3cf2013-03-21 01:40:5310#include "net/dns/mock_host_resolver.h"
[email protected]912256b32009-09-18 09:47:3511
Devlin Croninef3e37e2018-05-14 23:47:2412class ExecuteScriptApiTest : public extensions::ExtensionApiTest {
[email protected]4712e852010-11-24 13:17:1913 protected:
jambb11ed742017-05-01 17:27:5914 void SetUpOnMainThread() override {
Devlin Croninef3e37e2018-05-14 23:47:2415 extensions::ExtensionApiTest::SetUpOnMainThread();
[email protected]4712e852010-11-24 13:17:1916 // 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);
jambb11ed742017-05-01 17:27:5918 host_resolver()->AddRule("*", "127.0.0.1");
19 ASSERT_TRUE(StartEmbeddedTestServer());
[email protected]4712e852010-11-24 13:17:1920 }
21};
[email protected]912256b32009-09-18 09:47:3522
[email protected]59d8048a2012-02-09 19:27:0023// If failing, mark disabled and update http://crbug.com/92105.
[email protected]de4381e2012-11-26 22:43:1424IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptBasic) {
[email protected]ad6530c2009-12-17 19:43:5625 ASSERT_TRUE(RunExtensionTest("executescript/basic")) << message_;
[email protected]4712e852010-11-24 13:17:1926}
27
robca0af322014-12-08 12:42:5928IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptBadEncoding) {
robca0af322014-12-08 12:42:5929 // data/extensions/api_test/../bad = data/extensions/bad
30 ASSERT_TRUE(RunExtensionTest("../bad")) << message_;
31}
32
[email protected]59d8048a2012-02-09 19:27:0033// If failing, mark disabled and update http://crbug.com/92105.
[email protected]de4381e2012-11-26 22:43:1434IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptInFrame) {
[email protected]ad6530c2009-12-17 19:43:5635 ASSERT_TRUE(RunExtensionTest("executescript/in_frame")) << message_;
[email protected]4712e852010-11-24 13:17:1936}
37
rob52277c82016-02-07 17:28:5738IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptByFrameId) {
rob52277c82016-02-07 17:28:5739 ASSERT_TRUE(RunExtensionTest("executescript/frame_id")) << message_;
40}
41
[email protected]110642b2013-02-06 21:25:5242// 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
50IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, MAYBE_ExecuteScriptPermissions) {
[email protected]61f5a7b2009-12-22 22:21:2051 ASSERT_TRUE(RunExtensionTest("executescript/permissions")) << message_;
[email protected]912256b32009-09-18 09:47:3552}
[email protected]a7664e12010-04-08 20:37:4353
[email protected]59d8048a2012-02-09 19:27:0054// If failing, mark disabled and update http://crbug.com/84760.
[email protected]f34706be2012-09-04 07:32:0955IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptFileAfterClose) {
[email protected]a7664e12010-04-08 20:37:4356 ASSERT_TRUE(RunExtensionTest("executescript/file_after_close")) << message_;
57}
[email protected]10e40f312011-02-01 03:54:2058
[email protected]59d8048a2012-02-09 19:27:0059// If crashing, mark disabled and update http://crbug.com/67774.
60IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptFragmentNavigation) {
thestig4b36dd32014-10-31 20:30:1961 const char extension_name[] = "executescript/fragment";
[email protected]10e40f312011-02-01 03:54:2062 ASSERT_TRUE(RunExtensionTest(extension_name)) << message_;
63}
[email protected]4b96a992011-04-07 19:47:0764
[email protected]e1b3f40a2013-05-24 10:40:0865// 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)
71IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest,
72 MAYBE_NavigationRaceExecuteScript) {
[email protected]4b96a992011-04-07 19:47:0773 ASSERT_TRUE(RunExtensionSubtest("executescript/navigation_race",
74 "execute_script.html")) << message_;
[email protected]c30cf4b2011-06-09 01:25:1075}
76
[email protected]59d8048a2012-02-09 19:27:0077// If failing, mark disabled and update http://crbug.com/92105.
78IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptFrameAfterLoad) {
[email protected]d45ce252011-04-11 18:20:5079 ASSERT_TRUE(RunExtensionTest("executescript/frame_after_load")) << message_;
80}
[email protected]e7d6ec82012-04-13 19:00:2481
rob7ad924c2016-01-30 18:22:3682IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, FrameWithHttp204) {
rob7ad924c2016-01-30 18:22:3683 ASSERT_TRUE(RunExtensionTest("executescript/http204")) << message_;
84}
85
[email protected]e7d6ec82012-04-13 19:00:2486IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptRunAt) {
[email protected]e7d6ec82012-04-13 19:00:2487 ASSERT_TRUE(RunExtensionTest("executescript/run_at")) << message_;
88}
[email protected]cab8cd982012-07-20 20:57:0389
Manish Jethani9494d722018-01-20 00:28:4790IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptCSSOrigin) {
91 ASSERT_TRUE(RunExtensionTest("executescript/css_origin")) << message_;
92}
93
[email protected]cab8cd982012-07-20 20:57:0394IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, ExecuteScriptCallback) {
[email protected]cab8cd982012-07-20 20:57:0395 ASSERT_TRUE(RunExtensionTest("executescript/callback")) << message_;
96}
[email protected]0df49432014-03-04 01:02:5097
98IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, UserGesture) {
[email protected]0df49432014-03-04 01:02:5099 ASSERT_TRUE(RunExtensionTest("executescript/user_gesture")) << message_;
100}
rdevlin.cronin77433552015-06-23 18:27:40101
102IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, InjectIntoSubframesOnLoad) {
rdevlin.cronin77433552015-06-23 18:27:40103 ASSERT_TRUE(RunExtensionTest("executescript/subframes_on_load")) << message_;
104}
rdevlin.cronin3ae4a32012015-06-30 17:43:19105
106IN_PROC_BROWSER_TEST_F(ExecuteScriptApiTest, RemovedFrames) {
rdevlin.cronin3ae4a32012015-06-30 17:43:19107 ASSERT_TRUE(RunExtensionTest("executescript/removed_frames")) << message_;
108}
rob43ea0642016-03-19 01:05:01109
Karan Bhatia7a33a5422018-04-05 00:41:12110// 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.
112IN_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.
125IN_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
rob43ea0642016-03-19 01:05:01137// 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.
140const int kDestructiveScriptTestBucketCount = 1;
141
142class 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) {
rob43ea0642016-03-19 01:05:01147 return RunExtensionSubtest(
148 "executescript/destructive",
Raul Tambrefff51b752019-02-04 13:09:47149 "test.html?" + test_host + "#bucketcount=" +
150 base::NumberToString(kDestructiveScriptTestBucketCount) +
151 "&bucketindex=" + base::NumberToString(GetParam()));
rob43ea0642016-03-19 01:05:01152 }
153};
154
155// Removes the frame as soon as the content script is executed.
156IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, SynchronousRemoval) {
rob43ea0642016-03-19 01:05:01157 ASSERT_TRUE(RunSubtest("synchronous")) << message_;
158}
159
160// Removes the frame at the frame's first scheduled microtask.
161IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, MicrotaskRemoval) {
rob43ea0642016-03-19 01:05:01162 ASSERT_TRUE(RunSubtest("microtask")) << message_;
163}
164
165// Removes the frame at the frame's first scheduled macrotask.
bmcquadee56b2dd02017-05-16 16:12:01166IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, MacrotaskRemoval) {
rob43ea0642016-03-19 01:05:01167 ASSERT_TRUE(RunSubtest("macrotask")) << message_;
168}
169
170// Removes the frame at the first DOMNodeInserted event.
171IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, DOMNodeInserted1) {
rob43ea0642016-03-19 01:05:01172 ASSERT_TRUE(RunSubtest("domnodeinserted1")) << message_;
173}
174
175// Removes the frame at the second DOMNodeInserted event.
176IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, DOMNodeInserted2) {
rob43ea0642016-03-19 01:05:01177 ASSERT_TRUE(RunSubtest("domnodeinserted2")) << message_;
178}
179
180// Removes the frame at the third DOMNodeInserted event.
181IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, DOMNodeInserted3) {
rob43ea0642016-03-19 01:05:01182 ASSERT_TRUE(RunSubtest("domnodeinserted3")) << message_;
183}
184
185// Removes the frame at the first DOMSubtreeModified event.
186IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, DOMSubtreeModified1) {
rob43ea0642016-03-19 01:05:01187 ASSERT_TRUE(RunSubtest("domsubtreemodified1")) << message_;
188}
189
190// Removes the frame at the second DOMSubtreeModified event.
191IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, DOMSubtreeModified2) {
rob43ea0642016-03-19 01:05:01192 ASSERT_TRUE(RunSubtest("domsubtreemodified2")) << message_;
193}
194
195// Removes the frame at the third DOMSubtreeModified event.
196IN_PROC_BROWSER_TEST_P(DestructiveScriptTest, DOMSubtreeModified3) {
rob43ea0642016-03-19 01:05:01197 ASSERT_TRUE(RunSubtest("domsubtreemodified3")) << message_;
198}
199
Victor Costan4a060e82019-01-28 18:25:34200INSTANTIATE_TEST_SUITE_P(ExecuteScriptApiTest,
201 DestructiveScriptTest,
202 ::testing::Range(0,
203 kDestructiveScriptTestBucketCount));