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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/common/chrome_features.h"
#include "components/version_info/version_info.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/prerender_test_util.h"
#include "extensions/browser/api/declarative_net_request/constants.h"
#include "extensions/browser/api/declarative_net_request/utils.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/features/feature_channel.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/blink/public/common/features.h"
namespace {
using ContextType = extensions::browser_test_util::ContextType;
using extensions::ScopedCurrentChannel;
class DeclarativeNetRequestApiTest : public extensions::ExtensionApiTest {
public:
DeclarativeNetRequestApiTest() {
feature_list_.InitWithFeatures(
/*enabled_features=*/{},
// TODO(crbug.com/40248833): Use HTTPS URLs in tests to avoid having to
// disable this feature.
/*disabled_features=*/{features::kHttpsUpgrades});
}
explicit DeclarativeNetRequestApiTest(ContextType context_type)
: extensions::ExtensionApiTest(context_type) {
feature_list_.InitWithFeatures(
/*enabled_features=*/{},
// TODO(crbug.com/40248833): Use HTTPS URLs in tests to avoid having to
// disable this feature.
/*disabled_features=*/{features::kHttpsUpgrades});
}
~DeclarativeNetRequestApiTest() override = default;
DeclarativeNetRequestApiTest(const DeclarativeNetRequestApiTest&) = delete;
DeclarativeNetRequestApiTest& operator=(const DeclarativeNetRequestApiTest&) =
delete;
protected:
// ExtensionApiTest override.
void SetUpOnMainThread() override {
extensions::ExtensionApiTest::SetUpOnMainThread();
ASSERT_TRUE(StartEmbeddedTestServer());
// Map all hosts to localhost.
host_resolver()->AddRule("*", "127.0.0.1");
base::FilePath test_data_dir =
test_data_dir_.AppendASCII("declarative_net_request");
// Copy the |test_data_dir| to a temporary location. We do this to ensure
// that the temporary kMetadata folder created as a result of loading the
// extension is not written to the src directory and is automatically
// removed.
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
base::CopyDirectory(test_data_dir, temp_dir_.GetPath(), true /*recursive*/);
// Override the path used for loading the extension.
test_data_dir_ = temp_dir_.GetPath().AppendASCII("declarative_net_request");
}
private:
base::ScopedTempDir temp_dir_;
base::test::ScopedFeatureList feature_list_;
};
class DeclarativeNetRequestLazyApiTest
: public DeclarativeNetRequestApiTest,
public testing::WithParamInterface<ContextType> {
public:
DeclarativeNetRequestLazyApiTest()
: DeclarativeNetRequestApiTest(GetParam()) {}
};
#if !BUILDFLAG(IS_ANDROID)
// Android only supports service worker.
INSTANTIATE_TEST_SUITE_P(EventPage,
DeclarativeNetRequestLazyApiTest,
::testing::Values(ContextType::kEventPage));
#endif
INSTANTIATE_TEST_SUITE_P(ServiceWorker,
DeclarativeNetRequestLazyApiTest,
::testing::Values(ContextType::kServiceWorker));
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestLazyApiTest, DynamicRules) {
ASSERT_TRUE(RunExtensionTest("dynamic_rules")) << message_;
}
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestLazyApiTest, RegexRuleMessage) {
// Ensure the error message for large RegEx rules is updated with the
// correct value for the memory limit.
std::string expected_amount = base::StringPrintf(
"%dKB", extensions::declarative_net_request::kRegexMaxMemKb);
EXPECT_THAT(extensions::declarative_net_request::kErrorRegexTooLarge,
testing::HasSubstr(expected_amount));
}
class DeclarativeNetRequestSafeRulesLazyApiTest
: public DeclarativeNetRequestLazyApiTest {
public:
DeclarativeNetRequestSafeRulesLazyApiTest() {
scoped_feature_list_.InitAndEnableFeature(
extensions_features::kDeclarativeNetRequestSafeRuleLimits);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
#if !BUILDFLAG(IS_ANDROID)
// Android only supports service worker.
INSTANTIATE_TEST_SUITE_P(PersistentBackground,
DeclarativeNetRequestSafeRulesLazyApiTest,
::testing::Values(ContextType::kPersistentBackground));
INSTANTIATE_TEST_SUITE_P(EventPage,
DeclarativeNetRequestSafeRulesLazyApiTest,
::testing::Values(ContextType::kEventPage));
#endif
INSTANTIATE_TEST_SUITE_P(ServiceWorker,
DeclarativeNetRequestSafeRulesLazyApiTest,
::testing::Values(ContextType::kServiceWorker));
// Flaky on ASAN/MSAN: https://crbug.com/1167168
#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER)
#define MAYBE_DynamicRulesLimits DISABLED_DynamicRulesLimits
#else
#define MAYBE_DynamicRulesLimits DynamicRulesLimits
#endif
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestSafeRulesLazyApiTest,
MAYBE_DynamicRulesLimits) {
ExtensionTestMessageListener listener("ready", ReplyBehavior::kWillReply);
// Set up overrides for rule limits and send them to the test extension. This
// is done because running the test with the actual rule limits will be very
// slow.
base::AutoReset<int> dynamic_rule_limit_override =
extensions::declarative_net_request::
CreateScopedDynamicRuleLimitOverrideForTesting(200);
base::AutoReset<int> unsafe_dynamic_rule_limit_override =
extensions::declarative_net_request::
CreateScopedUnsafeDynamicRuleLimitOverrideForTesting(50);
base::AutoReset<int> regex_rule_limit_override = extensions::
declarative_net_request::CreateScopedRegexRuleLimitOverrideForTesting(50);
std::string rule_limits = base::StringPrintf(
R"({"ruleLimit":%d,"unsafeRuleLimit":%d,"regexRuleLimit":%d})", 200, 50,
50);
const extensions::Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("dynamic_rules_limits"));
ASSERT_TRUE(extension);
ASSERT_TRUE(listener.WaitUntilSatisfied());
extensions::ResultCatcher result_catcher;
listener.Reply(rule_limits);
EXPECT_TRUE(result_catcher.GetNextResult()) << result_catcher.message();
}
#if !BUILDFLAG(IS_ANDROID)
// TODO(crbug.com/371432155): Port to desktop Android when chrome.tabs API is
// available.
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestLazyApiTest, OnRulesMatchedDebug) {
ASSERT_TRUE(RunExtensionTest("on_rules_matched_debug")) << message_;
}
// This test uses webRequest/webRequestBlocking, so it's not currently
// supported for service workers.
IN_PROC_BROWSER_TEST_F(DeclarativeNetRequestApiTest, ModifyHeaders) {
ASSERT_TRUE(RunExtensionTest("modify_headers")) << message_;
}
// TODO(crbug.com/371432155): Port to desktop Android when chrome.tabs API is
// available.
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestLazyApiTest, GetMatchedRules) {
ASSERT_TRUE(RunExtensionTest("get_matched_rules")) << message_;
}
#endif // !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestLazyApiTest, IsRegexSupported) {
ASSERT_TRUE(RunExtensionTest("is_regex_supported")) << message_;
}
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestLazyApiTest, TestMatchOutcome) {
ASSERT_TRUE(RunExtensionTest("test_match_outcome")) << message_;
}
IN_PROC_BROWSER_TEST_F(DeclarativeNetRequestApiTest, UpdateStaticRules) {
ASSERT_TRUE(RunExtensionTest("update_static_rules")) << message_;
}
class DeclarativeNetRequestApiFencedFrameTest
: public DeclarativeNetRequestApiTest {
protected:
DeclarativeNetRequestApiFencedFrameTest()
: DeclarativeNetRequestApiTest(ContextType::kPersistentBackground) {
feature_list_.InitWithFeaturesAndParameters(
{{blink::features::kFencedFrames, {{}}},
{features::kPrivacySandboxAdsAPIsOverride, {}}},
// TODO(crbug.com/40248833): Use HTTPS URLs in tests to avoid having to
// disable this feature.
/*disabled_features=*/{features::kHttpsUpgrades});
// Fenced frames are only allowed in secure contexts.
UseHttpsTestServer();
}
~DeclarativeNetRequestApiFencedFrameTest() override = default;
private:
base::test::ScopedFeatureList feature_list_;
};
// TODO(crbug.com/40877906): Re-enable this test
IN_PROC_BROWSER_TEST_F(DeclarativeNetRequestApiFencedFrameTest, DISABLED_Load) {
ASSERT_TRUE(RunExtensionTest("fenced_frames")) << message_;
}
#if !BUILDFLAG(IS_ANDROID)
class DeclarativeNetRequestApiPrerenderingTest
: public DeclarativeNetRequestLazyApiTest {
public:
DeclarativeNetRequestApiPrerenderingTest() = default;
~DeclarativeNetRequestApiPrerenderingTest() override = default;
private:
content::test::ScopedPrerenderFeatureList scoped_feature_list_;
};
INSTANTIATE_TEST_SUITE_P(PersistentBackground,
DeclarativeNetRequestApiPrerenderingTest,
::testing::Values(ContextType::kPersistentBackground));
INSTANTIATE_TEST_SUITE_P(EventPage,
DeclarativeNetRequestApiPrerenderingTest,
::testing::Values(ContextType::kEventPage));
INSTANTIATE_TEST_SUITE_P(ServiceWorker,
DeclarativeNetRequestApiPrerenderingTest,
::testing::Values(ContextType::kServiceWorker));
// TODO(crbug.com/371432155): Port to desktop Android when chrome.tabs API is
// available.
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestApiPrerenderingTest,
PrerenderedPageInterception) {
ASSERT_TRUE(RunExtensionTest("prerendering")) << message_;
}
#endif
class DeclarativeNetRequestLazyApiResponseHeadersTest
: public DeclarativeNetRequestLazyApiTest {
public:
DeclarativeNetRequestLazyApiResponseHeadersTest() {
scoped_feature_list_.InitAndEnableFeature(
extensions_features::kDeclarativeNetRequestResponseHeaderMatching);
}
private:
// TODO(crbug.com/40727004): Once feature is launched to stable and feature
// flag can be removed, replace usages of this test class with just
// DeclarativeNetRequestLazyApiTest.
base::test::ScopedFeatureList scoped_feature_list_;
ScopedCurrentChannel current_channel_override_{version_info::Channel::DEV};
};
#if !BUILDFLAG(IS_ANDROID)
// Android only supports service worker.
INSTANTIATE_TEST_SUITE_P(PersistentBackground,
DeclarativeNetRequestLazyApiResponseHeadersTest,
::testing::Values(ContextType::kPersistentBackground));
INSTANTIATE_TEST_SUITE_P(EventPage,
DeclarativeNetRequestLazyApiResponseHeadersTest,
::testing::Values(ContextType::kEventPage));
#endif
INSTANTIATE_TEST_SUITE_P(ServiceWorker,
DeclarativeNetRequestLazyApiResponseHeadersTest,
::testing::Values(ContextType::kServiceWorker));
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestLazyApiResponseHeadersTest,
TestMatchOutcomeWithResponseHeaders) {
ASSERT_TRUE(RunExtensionTest("test_match_outcome_response_headers"))
<< message_;
}
} // namespace
|