Commit 5d30a543 authored by Karan Bhatia's avatar Karan Bhatia Committed by Commit Bot

DNR: Fix flaky DeclarativeNetRequestBrowserTest.BlockRequests_ResourceTypes test.

The DeclarativeNetRequestBrowserTest.BlockRequests_ResourceTypes can sometimes
timeout causing it to flake. Take care of this by splitting it into two tests.

BUG=787957, 696822

Change-Id: I6245e14ac2ad064d15c06792c5d492606a364d04
Reviewed-on: https://chromium-review.googlesource.com/807745
Commit-Queue: Karan Bhatia <karandeepb@chromium.org>
Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521963}
parent eb28a0bc
...@@ -896,149 +896,6 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest, ...@@ -896,149 +896,6 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest,
} }
} }
// Tests the "resourceTypes" and "excludedResourceTypes" fields of a declarative
// rule condition.
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest,
BlockRequests_ResourceTypes) {
// TODO(crbug.com/696822): Add tests for "object", "ping", "other", "font".
enum ResourceTypeMask {
kNone = 0,
kSubframe = 1 << 0,
kStylesheet = 1 << 1,
kScript = 1 << 2,
kImage = 1 << 3,
kXHR = 1 << 4,
kMedia = 1 << 5,
kWebSocket = 1 << 6,
kAll = (1 << 7) - 1
};
struct {
std::string domain;
size_t id;
std::vector<std::string> resource_types;
std::vector<std::string> excluded_resource_types;
} rules_data[] = {
{"block_subframe.com", 1, {"sub_frame"}, {}},
{"block_stylesheet.com", 2, {"stylesheet"}, {}},
{"block_script.com", 3, {"script"}, {}},
{"block_image.com", 4, {"image"}, {}},
{"block_xhr.com", 5, {"xmlhttprequest"}, {}},
{"block_media.com", 6, {"media"}, {}},
{"block_websocket.com", 7, {"websocket"}, {}},
{"block_image_and_stylesheet.com", 8, {"image", "stylesheet"}, {}},
{"block_subframe_and_xhr.com", 11, {"sub_frame", "xmlhttprequest"}, {}},
// With renderer side navigation, the main frame origin serves as the
// initiator for main frame page loads. Hence to ensure that the main
// frame page load is not blocked, also exclude the "other" resource type,
// which is used for main frame requests currently.
// TODO(crbug.com/696822): Change "other" to "main_frame" once it is
// implemented.
{"block_all.com", 9, {}, {"other"}},
{"block_all_but_xhr_and_script.com",
10,
{},
{"xmlhttprequest", "script", "other"}},
};
std::vector<TestRule> rules;
for (const auto& rule_data : rules_data) {
TestRule rule = CreateGenericRule();
// The "resourceTypes" property (i.e. |rule.condition->resource_types|)
// should not be an empty list. It should either be omitted or be a non-
// empty list.
if (rule_data.resource_types.empty())
rule.condition->resource_types = base::nullopt;
else
rule.condition->resource_types = rule_data.resource_types;
rule.condition->excluded_resource_types = rule_data.excluded_resource_types;
rule.id = rule_data.id;
rule.condition->domains = std::vector<std::string>({rule_data.domain});
// Don't specify the urlFilter, which should behaves the same as "*".
rule.condition->url_filter = base::nullopt;
rules.push_back(rule);
}
ASSERT_NO_FATAL_FAILURE(LoadExtensionWithRules(rules));
struct {
std::string hostname;
int blocked_mask;
} test_cases[] = {
{"block_subframe.com", kSubframe},
{"block_stylesheet.com", kStylesheet},
{"block_script.com", kScript},
{"block_image.com", kImage},
{"block_xhr.com", kXHR},
{"block_media.com", kMedia},
{"block_websocket.com", kWebSocket},
{"block_image_and_stylesheet.com", kImage | kStylesheet},
{"block_subframe_and_xhr.com", kSubframe | kXHR},
{"block_all.com", kAll},
{"block_all_but_xhr_and_script.com", kAll & ~kXHR & ~kScript},
{"block_none.com", kNone}};
// Start a web socket test server to test the websocket resource type.
net::SpawnedTestServer websocket_test_server(
net::SpawnedTestServer::TYPE_WS, net::GetWebSocketTestDataDirectory());
ASSERT_TRUE(websocket_test_server.Start());
// The |websocket_url| will echo the message we send to it.
GURL websocket_url = websocket_test_server.GetURL("echo-with-no-extension");
auto execute_script = [](content::RenderFrameHost* frame,
const std::string& script) {
bool subresource_loaded = false;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(frame, script,
&subresource_loaded));
return subresource_loaded;
};
for (const auto& test_case : test_cases) {
GURL url = embedded_test_server()->GetURL(test_case.hostname,
"/subresources.html");
SCOPED_TRACE(base::StringPrintf("Testing %s", url.spec().c_str()));
ui_test_utils::NavigateToURL(browser(), url);
ASSERT_EQ(content::PAGE_TYPE_NORMAL, GetPageType());
content::RenderFrameHost* frame = GetMainFrame();
// sub-frame.
EXPECT_EQ(
!(test_case.blocked_mask & kSubframe),
execute_script(frame,
"domAutomationController.send(!!window.frameLoaded);"));
// stylesheet
EXPECT_EQ(!(test_case.blocked_mask & kStylesheet),
execute_script(frame, "testStylesheet();"));
// script
EXPECT_EQ(!(test_case.blocked_mask & kScript),
execute_script(frame, "testScript();"));
// image
EXPECT_EQ(!(test_case.blocked_mask & kImage),
execute_script(frame, "testImage();"));
// xhr
EXPECT_EQ(!(test_case.blocked_mask & kXHR),
execute_script(frame, "testXHR();"));
// media
EXPECT_EQ(!(test_case.blocked_mask & kMedia),
execute_script(frame, "testMedia();"));
// websocket
EXPECT_EQ(!(test_case.blocked_mask & kWebSocket),
execute_script(frame,
base::StringPrintf("testWebSocket('%s');",
websocket_url.spec().c_str())));
}
}
// Ensure extensions can't intercept chrome:// urls. // Ensure extensions can't intercept chrome:// urls.
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest, ChromeURLS) { IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest, ChromeURLS) {
// Have the extension block all chrome:// urls. // Have the extension block all chrome:// urls.
...@@ -1152,11 +1009,181 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest, RendererCacheCleared) { ...@@ -1152,11 +1009,181 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest, RendererCacheCleared) {
content::RunAllTasksUntilIdle(); content::RunAllTasksUntilIdle();
} }
// Fixture to test the "resourceTypes" and "excludedResourceTypes" fields of a
// declarative rule condition.
class DeclarativeNetRequestResourceTypeBrowserTest
: public DeclarativeNetRequestBrowserTest {
public:
DeclarativeNetRequestResourceTypeBrowserTest() {}
protected:
// TODO(crbug.com/696822): Add tests for "object", "ping", "other", "font".
enum ResourceTypeMask {
kNone = 0,
kSubframe = 1 << 0,
kStylesheet = 1 << 1,
kScript = 1 << 2,
kImage = 1 << 3,
kXHR = 1 << 4,
kMedia = 1 << 5,
kWebSocket = 1 << 6,
kAll = (1 << 7) - 1
};
struct TestCase {
std::string hostname;
int blocked_mask;
};
void RunTests(const std::vector<TestCase>& test_cases) {
// Start a web socket test server to test the websocket resource type.
net::SpawnedTestServer websocket_test_server(
net::SpawnedTestServer::TYPE_WS, net::GetWebSocketTestDataDirectory());
ASSERT_TRUE(websocket_test_server.Start());
// The |websocket_url| will echo the message we send to it.
GURL websocket_url = websocket_test_server.GetURL("echo-with-no-extension");
auto execute_script = [](content::RenderFrameHost* frame,
const std::string& script) {
bool subresource_loaded = false;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(frame, script,
&subresource_loaded));
return subresource_loaded;
};
for (const auto& test_case : test_cases) {
GURL url = embedded_test_server()->GetURL(test_case.hostname,
"/subresources.html");
SCOPED_TRACE(base::StringPrintf("Testing %s", url.spec().c_str()));
ui_test_utils::NavigateToURL(browser(), url);
ASSERT_EQ(content::PAGE_TYPE_NORMAL, GetPageType());
content::RenderFrameHost* frame = GetMainFrame();
// sub-frame.
EXPECT_EQ(
!(test_case.blocked_mask & kSubframe),
execute_script(
frame, "domAutomationController.send(!!window.frameLoaded);"));
// stylesheet
EXPECT_EQ(!(test_case.blocked_mask & kStylesheet),
execute_script(frame, "testStylesheet();"));
// script
EXPECT_EQ(!(test_case.blocked_mask & kScript),
execute_script(frame, "testScript();"));
// image
EXPECT_EQ(!(test_case.blocked_mask & kImage),
execute_script(frame, "testImage();"));
// xhr
EXPECT_EQ(!(test_case.blocked_mask & kXHR),
execute_script(frame, "testXHR();"));
// media
EXPECT_EQ(!(test_case.blocked_mask & kMedia),
execute_script(frame, "testMedia();"));
// websocket
EXPECT_EQ(!(test_case.blocked_mask & kWebSocket),
execute_script(
frame, base::StringPrintf("testWebSocket('%s');",
websocket_url.spec().c_str())));
}
}
// Loads an extension to test blocking different resource types.
void LoadExtension() {
struct {
std::string domain;
size_t id;
std::vector<std::string> resource_types;
std::vector<std::string> excluded_resource_types;
} rules_data[] = {
{"block_subframe.com", 1, {"sub_frame"}, {}},
{"block_stylesheet.com", 2, {"stylesheet"}, {}},
{"block_script.com", 3, {"script"}, {}},
{"block_image.com", 4, {"image"}, {}},
{"block_xhr.com", 5, {"xmlhttprequest"}, {}},
{"block_media.com", 6, {"media"}, {}},
{"block_websocket.com", 7, {"websocket"}, {}},
{"block_image_and_stylesheet.com", 8, {"image", "stylesheet"}, {}},
{"block_subframe_and_xhr.com", 11, {"sub_frame", "xmlhttprequest"}, {}},
// With renderer side navigation, the main frame origin serves as the
// initiator for main frame page loads. Hence to ensure that the main
// frame page load is not blocked, also exclude the "other" resource
// type, which is used for main frame requests currently.
// TODO(crbug.com/696822): Change "other" to "main_frame" once it is
// implemented.
{"block_all.com", 9, {}, {"other"}},
{"block_all_but_xhr_and_script.com",
10,
{},
{"xmlhttprequest", "script", "other"}},
};
std::vector<TestRule> rules;
for (const auto& rule_data : rules_data) {
TestRule rule = CreateGenericRule();
// The "resourceTypes" property (i.e. |rule.condition->resource_types|)
// should not be an empty list. It should either be omitted or be a non-
// empty list.
if (rule_data.resource_types.empty())
rule.condition->resource_types = base::nullopt;
else
rule.condition->resource_types = rule_data.resource_types;
rule.condition->excluded_resource_types =
rule_data.excluded_resource_types;
rule.id = rule_data.id;
rule.condition->domains = std::vector<std::string>({rule_data.domain});
// Don't specify the urlFilter, which should behaves the same as "*".
rule.condition->url_filter = base::nullopt;
rules.push_back(rule);
}
LoadExtensionWithRules(rules);
}
private:
DISALLOW_COPY_AND_ASSIGN(DeclarativeNetRequestResourceTypeBrowserTest);
};
// These are split into two tests to prevent a timeout. See crbug.com/787957.
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestResourceTypeBrowserTest, Test1) {
ASSERT_NO_FATAL_FAILURE(LoadExtension());
RunTests({{"block_subframe.com", kSubframe},
{"block_stylesheet.com", kStylesheet},
{"block_script.com", kScript},
{"block_image.com", kImage},
{"block_xhr.com", kXHR},
{"block_media.com", kMedia}});
}
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestResourceTypeBrowserTest, Test2) {
ASSERT_NO_FATAL_FAILURE(LoadExtension());
RunTests({{"block_websocket.com", kWebSocket},
{"block_image_and_stylesheet.com", kImage | kStylesheet},
{"block_subframe_and_xhr.com", kSubframe | kXHR},
{"block_all.com", kAll},
{"block_all_but_xhr_and_script.com", kAll & ~kXHR & ~kScript},
{"block_none.com", kNone}});
}
INSTANTIATE_TEST_CASE_P(, INSTANTIATE_TEST_CASE_P(,
DeclarativeNetRequestBrowserTest, DeclarativeNetRequestBrowserTest,
::testing::Values(ExtensionLoadType::PACKED, ::testing::Values(ExtensionLoadType::PACKED,
ExtensionLoadType::UNPACKED)); ExtensionLoadType::UNPACKED));
INSTANTIATE_TEST_CASE_P(,
DeclarativeNetRequestResourceTypeBrowserTest,
::testing::Values(ExtensionLoadType::PACKED,
ExtensionLoadType::UNPACKED));
INSTANTIATE_TEST_CASE_P(, INSTANTIATE_TEST_CASE_P(,
DeclarativeNetRequestBrowserTest_Packed, DeclarativeNetRequestBrowserTest_Packed,
::testing::Values(ExtensionLoadType::PACKED)); ::testing::Values(ExtensionLoadType::PACKED));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment