Commit 48d426cb authored by Charlie Harrison's avatar Charlie Harrison Committed by Commit Bot

[subresource_filter] Refactor WebDocumentSubresourceFilterTests

This is in preparation to add ad-tagging tests

Bug: None
Change-Id: I47073f4f904c0a59239b31ba7e6cb3f1387b72bc
Reviewed-on: https://chromium-review.googlesource.com/988598Reviewed-by: default avatarNate Chapin <japhet@chromium.org>
Reviewed-by: default avatarJosh Karlin <jkarlin@chromium.org>
Commit-Queue: Charlie Harrison <csharrison@chromium.org>
Cr-Commit-Position: refs/heads/master@{#548623}
parent 7e044d65
...@@ -28,17 +28,23 @@ namespace { ...@@ -28,17 +28,23 @@ namespace {
class TestDocumentSubresourceFilter : public WebDocumentSubresourceFilter { class TestDocumentSubresourceFilter : public WebDocumentSubresourceFilter {
public: public:
explicit TestDocumentSubresourceFilter(bool allow_loads) explicit TestDocumentSubresourceFilter(LoadPolicy policy)
: allow_loads_(allow_loads) {} : load_policy_(policy) {}
LoadPolicy GetLoadPolicy(const WebURL& resource_url, LoadPolicy GetLoadPolicy(const WebURL& resource_url,
WebURLRequest::RequestContext) override { WebURLRequest::RequestContext) override {
std::string resource_path = WebString(KURL(resource_url).GetPath()).Utf8(); std::string resource_path = WebString(KURL(resource_url).GetPath()).Utf8();
if (std::find(queried_subresource_paths_.begin(), if (std::find(queried_subresource_paths_.begin(),
queried_subresource_paths_.end(), queried_subresource_paths_.end(),
resource_path) == queried_subresource_paths_.end()) resource_path) == queried_subresource_paths_.end()) {
queried_subresource_paths_.push_back(resource_path); queried_subresource_paths_.push_back(resource_path);
return allow_loads_ ? kAllow : kDisallow; }
String resource_string = resource_url.GetString();
for (const String& suffix : blacklisted_suffixes_) {
if (resource_string.EndsWith(suffix))
return load_policy_;
}
return LoadPolicy::kAllow;
} }
LoadPolicy GetLoadPolicyForWebSocketConnect(const WebURL& url) override { LoadPolicy GetLoadPolicyForWebSocketConnect(const WebURL& url) override {
...@@ -49,6 +55,10 @@ class TestDocumentSubresourceFilter : public WebDocumentSubresourceFilter { ...@@ -49,6 +55,10 @@ class TestDocumentSubresourceFilter : public WebDocumentSubresourceFilter {
bool ShouldLogToConsole() override { return false; } bool ShouldLogToConsole() override { return false; }
void AddToBlacklist(const String& suffix) {
blacklisted_suffixes_.push_back(suffix);
}
const std::vector<std::string>& QueriedSubresourcePaths() const { const std::vector<std::string>& QueriedSubresourcePaths() const {
return queried_subresource_paths_; return queried_subresource_paths_;
} }
...@@ -56,8 +66,10 @@ class TestDocumentSubresourceFilter : public WebDocumentSubresourceFilter { ...@@ -56,8 +66,10 @@ class TestDocumentSubresourceFilter : public WebDocumentSubresourceFilter {
bool GetIsAssociatedWithAdSubframe() const override { return false; } bool GetIsAssociatedWithAdSubframe() const override { return false; }
private: private:
// Using STL types for compatibility with gtest/gmock.
std::vector<std::string> queried_subresource_paths_; std::vector<std::string> queried_subresource_paths_;
bool allow_loads_; Vector<String> blacklisted_suffixes_;
LoadPolicy load_policy_;
}; };
class SubresourceFilteringWebFrameClient class SubresourceFilteringWebFrameClient
...@@ -69,12 +81,14 @@ class SubresourceFilteringWebFrameClient ...@@ -69,12 +81,14 @@ class SubresourceFilteringWebFrameClient
// the sake of this test, however, inject it earlier to verify that it // the sake of this test, however, inject it earlier to verify that it
// is not consulted for the main resource load. // is not consulted for the main resource load.
subresource_filter_ = subresource_filter_ =
new TestDocumentSubresourceFilter(allow_subresources_from_next_load_); new TestDocumentSubresourceFilter(load_policy_for_next_load_);
subresource_filter_->AddToBlacklist("1x1.png");
data_source->SetSubresourceFilter(subresource_filter_); data_source->SetSubresourceFilter(subresource_filter_);
} }
void SetAllowSubresourcesFromNextLoad(bool allow) { void SetLoadPolicyFromNextLoad(
allow_subresources_from_next_load_ = allow; TestDocumentSubresourceFilter::LoadPolicy policy) {
load_policy_for_next_load_ = policy;
} }
const TestDocumentSubresourceFilter* SubresourceFilter() const { const TestDocumentSubresourceFilter* SubresourceFilter() const {
return subresource_filter_; return subresource_filter_;
...@@ -83,7 +97,7 @@ class SubresourceFilteringWebFrameClient ...@@ -83,7 +97,7 @@ class SubresourceFilteringWebFrameClient
private: private:
// Weak, owned by WebDocumentLoader. // Weak, owned by WebDocumentLoader.
TestDocumentSubresourceFilter* subresource_filter_ = nullptr; TestDocumentSubresourceFilter* subresource_filter_ = nullptr;
bool allow_subresources_from_next_load_ = false; TestDocumentSubresourceFilter::LoadPolicy load_policy_for_next_load_;
}; };
} // namespace } // namespace
...@@ -96,8 +110,8 @@ class WebDocumentSubresourceFilterTest : public testing::Test { ...@@ -96,8 +110,8 @@ class WebDocumentSubresourceFilterTest : public testing::Test {
web_view_helper_.Initialize(&client_); web_view_helper_.Initialize(&client_);
} }
void LoadDocument(bool allow_subresources) { void LoadDocument(TestDocumentSubresourceFilter::LoadPolicy policy) {
client_.SetAllowSubresourcesFromNextLoad(allow_subresources); client_.SetLoadPolicyFromNextLoad(policy);
FrameTestHelpers::LoadFrame(MainFrame(), BaseURL() + "foo_with_image.html"); FrameTestHelpers::LoadFrame(MainFrame(), BaseURL() + "foo_with_image.html");
} }
...@@ -133,7 +147,7 @@ class WebDocumentSubresourceFilterTest : public testing::Test { ...@@ -133,7 +147,7 @@ class WebDocumentSubresourceFilterTest : public testing::Test {
}; };
TEST_F(WebDocumentSubresourceFilterTest, AllowedSubresource) { TEST_F(WebDocumentSubresourceFilterTest, AllowedSubresource) {
LoadDocument(true /* allowSubresources */); LoadDocument(TestDocumentSubresourceFilter::kAllow);
ExpectSubresourceWasLoaded(true); ExpectSubresourceWasLoaded(true);
// The filter should not be consulted for the main document resource. // The filter should not be consulted for the main document resource.
EXPECT_THAT(QueriedSubresourcePaths(), EXPECT_THAT(QueriedSubresourcePaths(),
...@@ -141,20 +155,20 @@ TEST_F(WebDocumentSubresourceFilterTest, AllowedSubresource) { ...@@ -141,20 +155,20 @@ TEST_F(WebDocumentSubresourceFilterTest, AllowedSubresource) {
} }
TEST_F(WebDocumentSubresourceFilterTest, DisallowedSubresource) { TEST_F(WebDocumentSubresourceFilterTest, DisallowedSubresource) {
LoadDocument(false /* allowSubresources */); LoadDocument(TestDocumentSubresourceFilter::kDisallow);
ExpectSubresourceWasLoaded(false); ExpectSubresourceWasLoaded(false);
} }
TEST_F(WebDocumentSubresourceFilterTest, FilteringDecisionIsMadeLoadByLoad) { TEST_F(WebDocumentSubresourceFilterTest, FilteringDecisionIsMadeLoadByLoad) {
for (const bool allow_subresources : {false, true}) { for (const TestDocumentSubresourceFilter::LoadPolicy policy :
SCOPED_TRACE(testing::Message() {TestDocumentSubresourceFilter::kDisallow,
<< "First load allows subresources = " << allow_subresources); TestDocumentSubresourceFilter::kAllow,
TestDocumentSubresourceFilter::kWouldDisallow}) {
LoadDocument(allow_subresources); SCOPED_TRACE(testing::Message() << "First load policy= " << policy);
ExpectSubresourceWasLoaded(allow_subresources);
LoadDocument(policy);
LoadDocument(!allow_subresources); ExpectSubresourceWasLoaded(policy !=
ExpectSubresourceWasLoaded(!allow_subresources); TestDocumentSubresourceFilter::kDisallow);
EXPECT_THAT(QueriedSubresourcePaths(), EXPECT_THAT(QueriedSubresourcePaths(),
testing::ElementsAre("/white-1x1.png")); testing::ElementsAre("/white-1x1.png"));
......
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