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