Commit 00d42fc6 authored by rajendrant's avatar rajendrant Committed by Chromium LUCI CQ

Rename RobotsRulesDecider to RobotsRulesParser

Change-Id: I59d9725499dfabe490411391350f5050ebe326c8
Bug: 1152527
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2574258
Commit-Queue: rajendrant <rajendrant@chromium.org>
Reviewed-by: default avatarRobert Ogden <robertogden@chromium.org>
Cr-Commit-Position: refs/heads/master@{#833814}
parent bc20c2e5
...@@ -98,8 +98,8 @@ static_library("renderer") { ...@@ -98,8 +98,8 @@ static_library("renderer") {
"previews/resource_loading_hints_agent.h", "previews/resource_loading_hints_agent.h",
"subresource_redirect/public_image_hints_url_loader_throttle.cc", "subresource_redirect/public_image_hints_url_loader_throttle.cc",
"subresource_redirect/public_image_hints_url_loader_throttle.h", "subresource_redirect/public_image_hints_url_loader_throttle.h",
"subresource_redirect/robots_rules_decider.cc", "subresource_redirect/robots_rules_parser.cc",
"subresource_redirect/robots_rules_decider.h", "subresource_redirect/robots_rules_parser.h",
"subresource_redirect/subresource_redirect_hints_agent.cc", "subresource_redirect/subresource_redirect_hints_agent.cc",
"subresource_redirect/subresource_redirect_hints_agent.h", "subresource_redirect/subresource_redirect_hints_agent.h",
"subresource_redirect/subresource_redirect_params.cc", "subresource_redirect/subresource_redirect_params.cc",
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/renderer/subresource_redirect/robots_rules_decider.h" #include "chrome/renderer/subresource_redirect/robots_rules_parser.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/logging.h" #include "base/logging.h"
...@@ -66,7 +66,7 @@ bool IsMatchingRobotsRule(const std::string& path, const std::string& pattern) { ...@@ -66,7 +66,7 @@ bool IsMatchingRobotsRule(const std::string& path, const std::string& pattern) {
} }
void RecordRobotsRulesReceiveResultHistogram( void RecordRobotsRulesReceiveResultHistogram(
RobotsRulesDecider::SubresourceRedirectRobotsRulesReceiveResult result) { RobotsRulesParser::SubresourceRedirectRobotsRulesReceiveResult result) {
UMA_HISTOGRAM_ENUMERATION( UMA_HISTOGRAM_ENUMERATION(
"SubresourceRedirect.RobotRulesDecider.ReceiveResult", result); "SubresourceRedirect.RobotRulesDecider.ReceiveResult", result);
} }
...@@ -78,27 +78,27 @@ void RecordRobotsRulesApplyDurationHistogram(base::TimeDelta duration) { ...@@ -78,27 +78,27 @@ void RecordRobotsRulesApplyDurationHistogram(base::TimeDelta duration) {
} // namespace } // namespace
bool RobotsRulesDecider::RobotsRule::Match(const std::string& path) const { bool RobotsRulesParser::RobotsRule::Match(const std::string& path) const {
return IsMatchingRobotsRule(path, pattern_); return IsMatchingRobotsRule(path, pattern_);
} }
RobotsRulesDecider::RobotsRulesDecider() { RobotsRulesParser::RobotsRulesParser() {
// Using base::Unretained(this) is safe here, since the timer // Using base::Unretained(this) is safe here, since the timer
// |rules_receive_timeout_timer_| is owned by |this| and destroyed before // |rules_receive_timeout_timer_| is owned by |this| and destroyed before
// |this|. // |this|.
rules_receive_timeout_timer_.Start( rules_receive_timeout_timer_.Start(
FROM_HERE, GetRobotsRulesReceiveTimeout(), FROM_HERE, GetRobotsRulesReceiveTimeout(),
base::BindOnce(&RobotsRulesDecider::OnRulesReceiveTimeout, base::BindOnce(&RobotsRulesParser::OnRulesReceiveTimeout,
base::Unretained(this))); base::Unretained(this)));
} }
RobotsRulesDecider::~RobotsRulesDecider() { RobotsRulesParser::~RobotsRulesParser() {
// Consider this as a timeout // Consider this as a timeout
if (rules_receive_timeout_timer_.IsRunning()) if (rules_receive_timeout_timer_.IsRunning())
rules_receive_timeout_timer_.FireNow(); rules_receive_timeout_timer_.FireNow();
} }
void RobotsRulesDecider::UpdateRobotsRules(const std::string& rules) { void RobotsRulesParser::UpdateRobotsRules(const std::string& rules) {
robots_rules_.reset(); robots_rules_.reset();
rules_receive_timeout_timer_.Stop(); rules_receive_timeout_timer_.Stop();
...@@ -133,7 +133,7 @@ void RobotsRulesDecider::UpdateRobotsRules(const std::string& rules) { ...@@ -133,7 +133,7 @@ void RobotsRulesDecider::UpdateRobotsRules(const std::string& rules) {
pending_check_requests_.clear(); pending_check_requests_.clear();
} }
void RobotsRulesDecider::CheckRobotsRules(const GURL& url, void RobotsRulesParser::CheckRobotsRules(const GURL& url,
CheckResultCallback callback) { CheckResultCallback callback) {
std::string path_with_query = url.path(); std::string path_with_query = url.path();
if (url.has_query()) if (url.has_query())
...@@ -149,7 +149,7 @@ void RobotsRulesDecider::CheckRobotsRules(const GURL& url, ...@@ -149,7 +149,7 @@ void RobotsRulesDecider::CheckRobotsRules(const GURL& url,
: CheckResult::kDisallowed); : CheckResult::kDisallowed);
} }
bool RobotsRulesDecider::IsAllowed(const std::string& url_path) const { bool RobotsRulesParser::IsAllowed(const std::string& url_path) const {
// Rules not received. Could be rule parse error or timeout. // Rules not received. Could be rule parse error or timeout.
if (!robots_rules_) if (!robots_rules_)
return false; return false;
...@@ -167,7 +167,7 @@ bool RobotsRulesDecider::IsAllowed(const std::string& url_path) const { ...@@ -167,7 +167,7 @@ bool RobotsRulesDecider::IsAllowed(const std::string& url_path) const {
return true; return true;
} }
void RobotsRulesDecider::OnRulesReceiveTimeout() { void RobotsRulesParser::OnRulesReceiveTimeout() {
DCHECK(!rules_receive_timeout_timer_.IsRunning()); DCHECK(!rules_receive_timeout_timer_.IsRunning());
for (auto& request : pending_check_requests_) for (auto& request : pending_check_requests_)
std::move(request.first).Run(CheckResult::kTimedout); std::move(request.first).Run(CheckResult::kTimedout);
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef CHROME_RENDERER_SUBRESOURCE_REDIRECT_ROBOTS_RULES_DECIDER_H_ #ifndef CHROME_RENDERER_SUBRESOURCE_REDIRECT_ROBOTS_RULES_PARSER_H_
#define CHROME_RENDERER_SUBRESOURCE_REDIRECT_ROBOTS_RULES_DECIDER_H_ #define CHROME_RENDERER_SUBRESOURCE_REDIRECT_ROBOTS_RULES_PARSER_H_
#include <map> #include <map>
#include <vector> #include <vector>
...@@ -20,7 +20,7 @@ namespace subresource_redirect { ...@@ -20,7 +20,7 @@ namespace subresource_redirect {
// url path is allowed or disallowed. Also supports a timeout to receive the // url path is allowed or disallowed. Also supports a timeout to receive the
// robots rules after which it will be treated as a full disallow. The check // robots rules after which it will be treated as a full disallow. The check
// result is delivered via callback asynchronously. // result is delivered via callback asynchronously.
class RobotsRulesDecider { class RobotsRulesParser {
public: public:
// The final result of robots rule retrieval. // The final result of robots rule retrieval.
// This should be kept in sync with // This should be kept in sync with
...@@ -41,11 +41,11 @@ class RobotsRulesDecider { ...@@ -41,11 +41,11 @@ class RobotsRulesDecider {
// Callback to notify the check robot rules result. // Callback to notify the check robot rules result.
using CheckResultCallback = base::OnceCallback<void(CheckResult)>; using CheckResultCallback = base::OnceCallback<void(CheckResult)>;
RobotsRulesDecider(); RobotsRulesParser();
~RobotsRulesDecider(); ~RobotsRulesParser();
RobotsRulesDecider(const RobotsRulesDecider&) = delete; RobotsRulesParser(const RobotsRulesParser&) = delete;
RobotsRulesDecider& operator=(const RobotsRulesDecider&) = delete; RobotsRulesParser& operator=(const RobotsRulesParser&) = delete;
// Update the robots rules. This causes any pending check requests to be // Update the robots rules. This causes any pending check requests to be
// processed immediately and called with th result. // processed immediately and called with th result.
...@@ -95,4 +95,4 @@ class RobotsRulesDecider { ...@@ -95,4 +95,4 @@ class RobotsRulesDecider {
} // namespace subresource_redirect } // namespace subresource_redirect
#endif // CHROME_RENDERER_SUBRESOURCE_REDIRECT_ROBOTS_RULES_DECIDER_H_ #endif // CHROME_RENDERER_SUBRESOURCE_REDIRECT_ROBOTS_RULES_PARSER_H_
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h" #include "base/test/task_environment.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/renderer/subresource_redirect/robots_rules_decider.h" #include "chrome/renderer/subresource_redirect/robots_rules_parser.h"
#include "components/data_reduction_proxy/proto/robots_rules.pb.h" #include "components/data_reduction_proxy/proto/robots_rules.pb.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h" #include "third_party/blink/public/common/features.h"
...@@ -33,20 +33,20 @@ struct Rule { ...@@ -33,20 +33,20 @@ struct Rule {
class CheckResultReceiver { class CheckResultReceiver {
public: public:
void OnCheckRobotsRulesResult(RobotsRulesDecider::CheckResult check_result) { void OnCheckRobotsRulesResult(RobotsRulesParser::CheckResult check_result) {
EXPECT_FALSE(did_receive_result_); EXPECT_FALSE(did_receive_result_);
did_receive_result_ = true; did_receive_result_ = true;
check_result_ = check_result; check_result_ = check_result;
} }
RobotsRulesDecider::CheckResultCallback GetCallback() { RobotsRulesParser::CheckResultCallback GetCallback() {
return base::BindOnce(&CheckResultReceiver::OnCheckRobotsRulesResult, return base::BindOnce(&CheckResultReceiver::OnCheckRobotsRulesResult,
weak_ptr_factory_.GetWeakPtr()); weak_ptr_factory_.GetWeakPtr());
} }
RobotsRulesDecider::CheckResult check_result() const { return check_result_; } RobotsRulesParser::CheckResult check_result() const { return check_result_; }
bool did_receive_result() const { return did_receive_result_; } bool did_receive_result() const { return did_receive_result_; }
private: private:
RobotsRulesDecider::CheckResult check_result_; RobotsRulesParser::CheckResult check_result_;
bool did_receive_result_ = false; bool did_receive_result_ = false;
base::WeakPtrFactory<CheckResultReceiver> weak_ptr_factory_{this}; base::WeakPtrFactory<CheckResultReceiver> weak_ptr_factory_{this};
}; };
...@@ -64,9 +64,9 @@ std::string GetRobotsRulesProtoString(const std::vector<Rule>& patterns) { ...@@ -64,9 +64,9 @@ std::string GetRobotsRulesProtoString(const std::vector<Rule>& patterns) {
return robots_rules.SerializeAsString(); return robots_rules.SerializeAsString();
} }
class SubresourceRedirectRobotsRulesDeciderTest : public testing::Test { class SubresourceRedirectRobotsRulesParserTest : public testing::Test {
public: public:
SubresourceRedirectRobotsRulesDeciderTest() SubresourceRedirectRobotsRulesParserTest()
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {} : task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
void SetUp() override { void SetUp() override {
scoped_feature_list_.InitWithFeaturesAndParameters( scoped_feature_list_.InitWithFeaturesAndParameters(
...@@ -74,14 +74,13 @@ class SubresourceRedirectRobotsRulesDeciderTest : public testing::Test { ...@@ -74,14 +74,13 @@ class SubresourceRedirectRobotsRulesDeciderTest : public testing::Test {
} }
void SetUpRobotsRules(const std::vector<Rule>& patterns) { void SetUpRobotsRules(const std::vector<Rule>& patterns) {
robots_rules_decider_.UpdateRobotsRules( robots_rules_parser_.UpdateRobotsRules(GetRobotsRulesProtoString(patterns));
GetRobotsRulesProtoString(patterns));
} }
void CheckRobotsRules(const std::string& url_path_with_query, void CheckRobotsRules(const std::string& url_path_with_query,
RobotsRulesDecider::CheckResult expected_result) { RobotsRulesParser::CheckResult expected_result) {
CheckResultReceiver result_receiver; CheckResultReceiver result_receiver;
robots_rules_decider_.CheckRobotsRules( robots_rules_parser_.CheckRobotsRules(
GURL(kTestOrigin + url_path_with_query), result_receiver.GetCallback()); GURL(kTestOrigin + url_path_with_query), result_receiver.GetCallback());
EXPECT_TRUE(result_receiver.did_receive_result()); EXPECT_TRUE(result_receiver.did_receive_result());
EXPECT_EQ(expected_result, result_receiver.check_result()); EXPECT_EQ(expected_result, result_receiver.check_result());
...@@ -90,14 +89,14 @@ class SubresourceRedirectRobotsRulesDeciderTest : public testing::Test { ...@@ -90,14 +89,14 @@ class SubresourceRedirectRobotsRulesDeciderTest : public testing::Test {
std::unique_ptr<CheckResultReceiver> CheckRobotsRulesAsync( std::unique_ptr<CheckResultReceiver> CheckRobotsRulesAsync(
const std::string& url_path_with_query) { const std::string& url_path_with_query) {
auto result_receiver = std::make_unique<CheckResultReceiver>(); auto result_receiver = std::make_unique<CheckResultReceiver>();
robots_rules_decider_.CheckRobotsRules( robots_rules_parser_.CheckRobotsRules(
GURL(kTestOrigin + url_path_with_query), GURL(kTestOrigin + url_path_with_query),
result_receiver->GetCallback()); result_receiver->GetCallback());
return result_receiver; return result_receiver;
} }
void VerifyRobotsRulesReceiveResultHistogram( void VerifyRobotsRulesReceiveResultHistogram(
RobotsRulesDecider::SubresourceRedirectRobotsRulesReceiveResult result) { RobotsRulesParser::SubresourceRedirectRobotsRulesReceiveResult result) {
histogram_tester().ExpectUniqueSample( histogram_tester().ExpectUniqueSample(
"SubresourceRedirect.RobotRulesDecider.ReceiveResult", result, 1); "SubresourceRedirect.RobotRulesDecider.ReceiveResult", result, 1);
} }
...@@ -118,60 +117,56 @@ class SubresourceRedirectRobotsRulesDeciderTest : public testing::Test { ...@@ -118,60 +117,56 @@ class SubresourceRedirectRobotsRulesDeciderTest : public testing::Test {
base::test::ScopedFeatureList scoped_feature_list_; base::test::ScopedFeatureList scoped_feature_list_;
base::test::TaskEnvironment task_environment_; base::test::TaskEnvironment task_environment_;
base::HistogramTester histogram_tester_; base::HistogramTester histogram_tester_;
RobotsRulesDecider robots_rules_decider_; RobotsRulesParser robots_rules_parser_;
}; };
TEST_F(SubresourceRedirectRobotsRulesDeciderTest, TEST_F(SubresourceRedirectRobotsRulesParserTest,
InvalidProtoParseErrorDisallowsAllPaths) { InvalidProtoParseErrorDisallowsAllPaths) {
robots_rules_decider_.UpdateRobotsRules("INVALID PROTO"); robots_rules_parser_.UpdateRobotsRules("INVALID PROTO");
VerifyRobotsRulesReceiveResultHistogram( VerifyRobotsRulesReceiveResultHistogram(
RobotsRulesDecider::SubresourceRedirectRobotsRulesReceiveResult:: RobotsRulesParser::SubresourceRedirectRobotsRulesReceiveResult::
kParseError); kParseError);
histogram_tester().ExpectTotalCount( histogram_tester().ExpectTotalCount(
"SubresourceRedirect.RobotRulesDecider.Count", 0); "SubresourceRedirect.RobotRulesDecider.Count", 0);
// All url paths should be disallowed. // All url paths should be disallowed.
CheckRobotsRules("", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/foo.jpg", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/foo.jpg", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/foo/bar.jpg", CheckRobotsRules("/foo/bar.jpg", RobotsRulesParser::CheckResult::kDisallowed);
RobotsRulesDecider::CheckResult::kDisallowed);
VerifyTotalRobotsRulesApplyHistograms(0); VerifyTotalRobotsRulesApplyHistograms(0);
} }
TEST_F(SubresourceRedirectRobotsRulesDeciderTest, EmptyRulesAllowsAllPaths) { TEST_F(SubresourceRedirectRobotsRulesParserTest, EmptyRulesAllowsAllPaths) {
SetUpRobotsRules({}); SetUpRobotsRules({});
VerifyRobotsRulesReceiveResultHistogram( VerifyRobotsRulesReceiveResultHistogram(
RobotsRulesDecider::SubresourceRedirectRobotsRulesReceiveResult:: RobotsRulesParser::SubresourceRedirectRobotsRulesReceiveResult::kSuccess);
kSuccess);
VerifyReceivedRobotsRulesCountHistogram(0); VerifyReceivedRobotsRulesCountHistogram(0);
// All url paths should be allowed. // All url paths should be allowed.
CheckRobotsRules("", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/foo.jpg", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/foo.jpg", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/foo/bar.jpg", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/foo/bar.jpg", RobotsRulesParser::CheckResult::kAllowed);
VerifyTotalRobotsRulesApplyHistograms(4); VerifyTotalRobotsRulesApplyHistograms(4);
} }
TEST_F(SubresourceRedirectRobotsRulesDeciderTest, TEST_F(SubresourceRedirectRobotsRulesParserTest,
RulesReceiveTimeoutDisallowsAllPaths) { RulesReceiveTimeoutDisallowsAllPaths) {
// Let the rule fetch timeout. // Let the rule fetch timeout.
task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(10)); task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(10));
VerifyRobotsRulesReceiveResultHistogram( VerifyRobotsRulesReceiveResultHistogram(
RobotsRulesDecider::SubresourceRedirectRobotsRulesReceiveResult:: RobotsRulesParser::SubresourceRedirectRobotsRulesReceiveResult::kTimeout);
kTimeout);
// All url paths should be disallowed. // All url paths should be disallowed.
CheckRobotsRules("", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/foo.jpg", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/foo.jpg", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/foo/bar.jpg", CheckRobotsRules("/foo/bar.jpg", RobotsRulesParser::CheckResult::kDisallowed);
RobotsRulesDecider::CheckResult::kDisallowed);
VerifyTotalRobotsRulesApplyHistograms(0); VerifyTotalRobotsRulesApplyHistograms(0);
} }
TEST_F(SubresourceRedirectRobotsRulesDeciderTest, TEST_F(SubresourceRedirectRobotsRulesParserTest,
CheckResultCallbackAfterRulesReceived) { CheckResultCallbackAfterRulesReceived) {
auto receiver1 = CheckRobotsRulesAsync("/foo.jpg"); auto receiver1 = CheckRobotsRulesAsync("/foo.jpg");
auto receiver2 = CheckRobotsRulesAsync("/bar"); auto receiver2 = CheckRobotsRulesAsync("/bar");
...@@ -182,55 +177,54 @@ TEST_F(SubresourceRedirectRobotsRulesDeciderTest, ...@@ -182,55 +177,54 @@ TEST_F(SubresourceRedirectRobotsRulesDeciderTest,
// Once the rules are received the callback should get called with the result. // Once the rules are received the callback should get called with the result.
SetUpRobotsRules({{kRuleTypeAllow, "/foo"}, {kRuleTypeDisallow, "/"}}); SetUpRobotsRules({{kRuleTypeAllow, "/foo"}, {kRuleTypeDisallow, "/"}});
VerifyRobotsRulesReceiveResultHistogram( VerifyRobotsRulesReceiveResultHistogram(
RobotsRulesDecider::SubresourceRedirectRobotsRulesReceiveResult:: RobotsRulesParser::SubresourceRedirectRobotsRulesReceiveResult::kSuccess);
kSuccess);
VerifyReceivedRobotsRulesCountHistogram(2); VerifyReceivedRobotsRulesCountHistogram(2);
EXPECT_TRUE(receiver1->did_receive_result()); EXPECT_TRUE(receiver1->did_receive_result());
EXPECT_TRUE(receiver2->did_receive_result()); EXPECT_TRUE(receiver2->did_receive_result());
EXPECT_EQ(RobotsRulesDecider::CheckResult::kAllowed, EXPECT_EQ(RobotsRulesParser::CheckResult::kAllowed,
receiver1->check_result()); receiver1->check_result());
EXPECT_EQ(RobotsRulesDecider::CheckResult::kDisallowed, EXPECT_EQ(RobotsRulesParser::CheckResult::kDisallowed,
receiver2->check_result()); receiver2->check_result());
VerifyTotalRobotsRulesApplyHistograms(2); VerifyTotalRobotsRulesApplyHistograms(2);
CheckRobotsRules("/foo.png", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/foo.png", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/bar", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/bar", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/baz", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/baz", RobotsRulesParser::CheckResult::kDisallowed);
VerifyTotalRobotsRulesApplyHistograms(5); VerifyTotalRobotsRulesApplyHistograms(5);
} }
// Verify if the callback is called before the decider gets destroyed. // Verify if the callback is called before the decider gets destroyed.
TEST_F(SubresourceRedirectRobotsRulesDeciderTest, TEST_F(SubresourceRedirectRobotsRulesParserTest,
VerifyCallbackCalledBeforeDeciderDestroy) { VerifyCallbackCalledBeforeDeciderDestroy) {
auto robots_rules_decider = std::make_unique<RobotsRulesDecider>(); auto robots_rules_parser = std::make_unique<RobotsRulesParser>();
auto receiver1 = std::make_unique<CheckResultReceiver>(); auto receiver1 = std::make_unique<CheckResultReceiver>();
auto receiver2 = std::make_unique<CheckResultReceiver>(); auto receiver2 = std::make_unique<CheckResultReceiver>();
robots_rules_decider->CheckRobotsRules(GURL("https://test.com/foo.jpg"), robots_rules_parser->CheckRobotsRules(GURL("https://test.com/foo.jpg"),
receiver1->GetCallback()); receiver1->GetCallback());
robots_rules_decider->CheckRobotsRules(GURL("https://test.com/bar"), robots_rules_parser->CheckRobotsRules(GURL("https://test.com/bar"),
receiver2->GetCallback()); receiver2->GetCallback());
EXPECT_FALSE(receiver1->did_receive_result()); EXPECT_FALSE(receiver1->did_receive_result());
EXPECT_FALSE(receiver2->did_receive_result()); EXPECT_FALSE(receiver2->did_receive_result());
VerifyTotalRobotsRulesApplyHistograms(0); VerifyTotalRobotsRulesApplyHistograms(0);
robots_rules_decider->UpdateRobotsRules(GetRobotsRulesProtoString( robots_rules_parser->UpdateRobotsRules(GetRobotsRulesProtoString(
{{kRuleTypeAllow, "/foo"}, {kRuleTypeDisallow, "/"}})); {{kRuleTypeAllow, "/foo"}, {kRuleTypeDisallow, "/"}}));
// Destroying the decider should trigger the callbacks. // Destroying the decider should trigger the callbacks.
robots_rules_decider.reset(); robots_rules_parser.reset();
EXPECT_TRUE(receiver1->did_receive_result()); EXPECT_TRUE(receiver1->did_receive_result());
EXPECT_TRUE(receiver2->did_receive_result()); EXPECT_TRUE(receiver2->did_receive_result());
EXPECT_EQ(RobotsRulesDecider::CheckResult::kAllowed, EXPECT_EQ(RobotsRulesParser::CheckResult::kAllowed,
receiver1->check_result()); receiver1->check_result());
EXPECT_EQ(RobotsRulesDecider::CheckResult::kDisallowed, EXPECT_EQ(RobotsRulesParser::CheckResult::kDisallowed,
receiver2->check_result()); receiver2->check_result());
VerifyTotalRobotsRulesApplyHistograms(2); VerifyTotalRobotsRulesApplyHistograms(2);
} }
TEST_F(SubresourceRedirectRobotsRulesDeciderTest, TEST_F(SubresourceRedirectRobotsRulesParserTest,
CheckResultCallbackAfterRulesReceiveTimeout) { CheckResultCallbackAfterRulesReceiveTimeout) {
auto receiver1 = CheckRobotsRulesAsync("/foo.jpg"); auto receiver1 = CheckRobotsRulesAsync("/foo.jpg");
auto receiver2 = CheckRobotsRulesAsync("/bar"); auto receiver2 = CheckRobotsRulesAsync("/bar");
...@@ -242,104 +236,98 @@ TEST_F(SubresourceRedirectRobotsRulesDeciderTest, ...@@ -242,104 +236,98 @@ TEST_F(SubresourceRedirectRobotsRulesDeciderTest,
// result. // result.
task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(10)); task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(10));
VerifyRobotsRulesReceiveResultHistogram( VerifyRobotsRulesReceiveResultHistogram(
RobotsRulesDecider::SubresourceRedirectRobotsRulesReceiveResult:: RobotsRulesParser::SubresourceRedirectRobotsRulesReceiveResult::kTimeout);
kTimeout);
EXPECT_TRUE(receiver1->did_receive_result()); EXPECT_TRUE(receiver1->did_receive_result());
EXPECT_TRUE(receiver2->did_receive_result()); EXPECT_TRUE(receiver2->did_receive_result());
EXPECT_EQ(RobotsRulesDecider::CheckResult::kTimedout, EXPECT_EQ(RobotsRulesParser::CheckResult::kTimedout,
receiver1->check_result()); receiver1->check_result());
EXPECT_EQ(RobotsRulesDecider::CheckResult::kTimedout, EXPECT_EQ(RobotsRulesParser::CheckResult::kTimedout,
receiver2->check_result()); receiver2->check_result());
VerifyTotalRobotsRulesApplyHistograms(0); VerifyTotalRobotsRulesApplyHistograms(0);
SetUpRobotsRules({{kRuleTypeAllow, "/foo"}, {kRuleTypeDisallow, "/"}}); SetUpRobotsRules({{kRuleTypeAllow, "/foo"}, {kRuleTypeDisallow, "/"}});
CheckRobotsRules("/foo.png", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/foo.png", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/bar", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/bar", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/baz", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/baz", RobotsRulesParser::CheckResult::kDisallowed);
VerifyTotalRobotsRulesApplyHistograms(3); VerifyTotalRobotsRulesApplyHistograms(3);
} }
TEST_F(SubresourceRedirectRobotsRulesDeciderTest, FullAllowRule) { TEST_F(SubresourceRedirectRobotsRulesParserTest, FullAllowRule) {
SetUpRobotsRules({{kRuleTypeAllow, "*"}}); SetUpRobotsRules({{kRuleTypeAllow, "*"}});
VerifyRobotsRulesReceiveResultHistogram( VerifyRobotsRulesReceiveResultHistogram(
RobotsRulesDecider::SubresourceRedirectRobotsRulesReceiveResult:: RobotsRulesParser::SubresourceRedirectRobotsRulesReceiveResult::kSuccess);
kSuccess);
VerifyReceivedRobotsRulesCountHistogram(1); VerifyReceivedRobotsRulesCountHistogram(1);
// All url paths should be allowed. // All url paths should be allowed.
CheckRobotsRules("", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/foo.jpg", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/foo.jpg", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/foo/bar.jpg", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/foo/bar.jpg", RobotsRulesParser::CheckResult::kAllowed);
VerifyTotalRobotsRulesApplyHistograms(4); VerifyTotalRobotsRulesApplyHistograms(4);
} }
TEST_F(SubresourceRedirectRobotsRulesDeciderTest, FullDisallowRule) { TEST_F(SubresourceRedirectRobotsRulesParserTest, FullDisallowRule) {
SetUpRobotsRules({{kRuleTypeDisallow, "*"}}); SetUpRobotsRules({{kRuleTypeDisallow, "*"}});
VerifyRobotsRulesReceiveResultHistogram( VerifyRobotsRulesReceiveResultHistogram(
RobotsRulesDecider::SubresourceRedirectRobotsRulesReceiveResult:: RobotsRulesParser::SubresourceRedirectRobotsRulesReceiveResult::kSuccess);
kSuccess);
VerifyReceivedRobotsRulesCountHistogram(1); VerifyReceivedRobotsRulesCountHistogram(1);
// All url paths should be disallowed. // All url paths should be disallowed.
CheckRobotsRules("", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/foo.jpg", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/foo.jpg", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/foo/bar.jpg", CheckRobotsRules("/foo/bar.jpg", RobotsRulesParser::CheckResult::kDisallowed);
RobotsRulesDecider::CheckResult::kDisallowed);
VerifyTotalRobotsRulesApplyHistograms(4); VerifyTotalRobotsRulesApplyHistograms(4);
} }
TEST_F(SubresourceRedirectRobotsRulesDeciderTest, TwoAllowRules) { TEST_F(SubresourceRedirectRobotsRulesParserTest, TwoAllowRules) {
SetUpRobotsRules({{kRuleTypeAllow, "/foo"}, SetUpRobotsRules({{kRuleTypeAllow, "/foo"},
{kRuleTypeAllow, "/bar$"}, {kRuleTypeAllow, "/bar$"},
{kRuleTypeDisallow, "*"}}); {kRuleTypeDisallow, "*"}});
VerifyRobotsRulesReceiveResultHistogram( VerifyRobotsRulesReceiveResultHistogram(
RobotsRulesDecider::SubresourceRedirectRobotsRulesReceiveResult:: RobotsRulesParser::SubresourceRedirectRobotsRulesReceiveResult::kSuccess);
kSuccess);
VerifyReceivedRobotsRulesCountHistogram(3); VerifyReceivedRobotsRulesCountHistogram(3);
CheckRobotsRules("", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/foo.jpg", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/foo.jpg", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/foo/baz.jpg", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/foo/baz.jpg", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/bar", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/bar", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/bar.jpg", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/bar.jpg", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/baz", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/baz", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("foo", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("foo", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("bar", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("bar", RobotsRulesParser::CheckResult::kDisallowed);
VerifyTotalRobotsRulesApplyHistograms(9); VerifyTotalRobotsRulesApplyHistograms(9);
} }
// When the URL path matches multiple allow and disallow rules, whichever rule // When the URL path matches multiple allow and disallow rules, whichever rule
// that comes first in the list should take precedence. // that comes first in the list should take precedence.
TEST_F(SubresourceRedirectRobotsRulesDeciderTest, TEST_F(SubresourceRedirectRobotsRulesParserTest,
FirstAllowRuleMatchOverridesLaterDisallowRule) { FirstAllowRuleMatchOverridesLaterDisallowRule) {
SetUpRobotsRules({{kRuleTypeAllow, "/foo"}, {kRuleTypeDisallow, "/foo"}}); SetUpRobotsRules({{kRuleTypeAllow, "/foo"}, {kRuleTypeDisallow, "/foo"}});
VerifyRobotsRulesReceiveResultHistogram( VerifyRobotsRulesReceiveResultHistogram(
RobotsRulesDecider::SubresourceRedirectRobotsRulesReceiveResult:: RobotsRulesParser::SubresourceRedirectRobotsRulesReceiveResult::kSuccess);
kSuccess);
VerifyReceivedRobotsRulesCountHistogram(2); VerifyReceivedRobotsRulesCountHistogram(2);
CheckRobotsRules("/foo", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/foo", RobotsRulesParser::CheckResult::kAllowed);
SetUpRobotsRules({{kRuleTypeDisallow, "/foo"}, {kRuleTypeAllow, "/foo"}}); SetUpRobotsRules({{kRuleTypeDisallow, "/foo"}, {kRuleTypeAllow, "/foo"}});
CheckRobotsRules("/foo", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/foo", RobotsRulesParser::CheckResult::kDisallowed);
SetUpRobotsRules({{kRuleTypeAllow, "/foo.jpg"}, {kRuleTypeDisallow, "/foo"}}); SetUpRobotsRules({{kRuleTypeAllow, "/foo.jpg"}, {kRuleTypeDisallow, "/foo"}});
CheckRobotsRules("/foo.jpg", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/foo.jpg", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/foo", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/foo", RobotsRulesParser::CheckResult::kDisallowed);
SetUpRobotsRules({{kRuleTypeDisallow, "/foo.jpg"}, {kRuleTypeAllow, "/foo"}}); SetUpRobotsRules({{kRuleTypeDisallow, "/foo.jpg"}, {kRuleTypeAllow, "/foo"}});
CheckRobotsRules("/foo.jpg", RobotsRulesDecider::CheckResult::kDisallowed); CheckRobotsRules("/foo.jpg", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/foo", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/foo", RobotsRulesParser::CheckResult::kAllowed);
VerifyTotalRobotsRulesApplyHistograms(6); VerifyTotalRobotsRulesApplyHistograms(6);
} }
TEST_F(SubresourceRedirectRobotsRulesDeciderTest, TestURLWithArguments) { TEST_F(SubresourceRedirectRobotsRulesParserTest, TestURLWithArguments) {
SetUpRobotsRules({{kRuleTypeAllow, "/*.jpg$"}, SetUpRobotsRules({{kRuleTypeAllow, "/*.jpg$"},
{kRuleTypeDisallow, "/*.png?*arg_disallowed"}, {kRuleTypeDisallow, "/*.png?*arg_disallowed"},
{kRuleTypeAllow, "/*.png"}, {kRuleTypeAllow, "/*.png"},
...@@ -347,46 +335,43 @@ TEST_F(SubresourceRedirectRobotsRulesDeciderTest, TestURLWithArguments) { ...@@ -347,46 +335,43 @@ TEST_F(SubresourceRedirectRobotsRulesDeciderTest, TestURLWithArguments) {
{kRuleTypeAllow, "/*.gif"}, {kRuleTypeAllow, "/*.gif"},
{kRuleTypeDisallow, "/"}}); {kRuleTypeDisallow, "/"}});
VerifyRobotsRulesReceiveResultHistogram( VerifyRobotsRulesReceiveResultHistogram(
RobotsRulesDecider::SubresourceRedirectRobotsRulesReceiveResult:: RobotsRulesParser::SubresourceRedirectRobotsRulesReceiveResult::kSuccess);
kSuccess);
VerifyReceivedRobotsRulesCountHistogram(6); VerifyReceivedRobotsRulesCountHistogram(6);
CheckRobotsRules("/allowed.jpg", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/allowed.jpg", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/allowed.png", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/allowed.png", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/allowed.gif", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/allowed.gif", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/disallowed.jpg?arg", CheckRobotsRules("/disallowed.jpg?arg",
RobotsRulesDecider::CheckResult::kDisallowed); RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/allowed.png?arg", CheckRobotsRules("/allowed.png?arg",
RobotsRulesDecider::CheckResult::kAllowed); RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/allowed.png?arg_allowed", CheckRobotsRules("/allowed.png?arg_allowed",
RobotsRulesDecider::CheckResult::kAllowed); RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/allowed.png?arg_allowed&arg_allowed2", CheckRobotsRules("/allowed.png?arg_allowed&arg_allowed2",
RobotsRulesDecider::CheckResult::kAllowed); RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/allowed.png?arg_disallowed", CheckRobotsRules("/allowed.png?arg_disallowed",
RobotsRulesDecider::CheckResult::kDisallowed); RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/allowed.png?arg_disallowed&arg_disallowed2", CheckRobotsRules("/allowed.png?arg_disallowed&arg_disallowed2",
RobotsRulesDecider::CheckResult::kDisallowed); RobotsRulesParser::CheckResult::kDisallowed);
VerifyTotalRobotsRulesApplyHistograms(9); VerifyTotalRobotsRulesApplyHistograms(9);
} }
TEST_F(SubresourceRedirectRobotsRulesDeciderTest, TestRulesAreCaseSensitive) { TEST_F(SubresourceRedirectRobotsRulesParserTest, TestRulesAreCaseSensitive) {
SetUpRobotsRules({{kRuleTypeAllow, "/allowed"}, SetUpRobotsRules({{kRuleTypeAllow, "/allowed"},
{kRuleTypeAllow, "/CamelCase"}, {kRuleTypeAllow, "/CamelCase"},
{kRuleTypeAllow, "/CAPITALIZE"}, {kRuleTypeAllow, "/CAPITALIZE"},
{kRuleTypeDisallow, "/"}}); {kRuleTypeDisallow, "/"}});
VerifyReceivedRobotsRulesCountHistogram(4); VerifyReceivedRobotsRulesCountHistogram(4);
CheckRobotsRules("/allowed.jpg", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/allowed.jpg", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/CamelCase.jpg", RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/CamelCase.jpg", RobotsRulesParser::CheckResult::kAllowed);
CheckRobotsRules("/CAPITALIZE.jpg", CheckRobotsRules("/CAPITALIZE.jpg", RobotsRulesParser::CheckResult::kAllowed);
RobotsRulesDecider::CheckResult::kAllowed); CheckRobotsRules("/Allowed.jpg", RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/Allowed.jpg",
RobotsRulesDecider::CheckResult::kDisallowed);
CheckRobotsRules("/camelcase.jpg", CheckRobotsRules("/camelcase.jpg",
RobotsRulesDecider::CheckResult::kDisallowed); RobotsRulesParser::CheckResult::kDisallowed);
CheckRobotsRules("/capitalize.jpg", CheckRobotsRules("/capitalize.jpg",
RobotsRulesDecider::CheckResult::kDisallowed); RobotsRulesParser::CheckResult::kDisallowed);
VerifyTotalRobotsRulesApplyHistograms(6); VerifyTotalRobotsRulesApplyHistograms(6);
} }
......
...@@ -3816,7 +3816,7 @@ test("unit_tests") { ...@@ -3816,7 +3816,7 @@ test("unit_tests") {
"../renderer/media/flash_embed_rewrite_unittest.cc", "../renderer/media/flash_embed_rewrite_unittest.cc",
"../renderer/net/net_error_helper_core_unittest.cc", "../renderer/net/net_error_helper_core_unittest.cc",
"../renderer/plugins/plugin_uma_unittest.cc", "../renderer/plugins/plugin_uma_unittest.cc",
"../renderer/subresource_redirect/robots_rules_decider_unittest.cc", "../renderer/subresource_redirect/robots_rules_parser_unittest.cc",
"../renderer/subresource_redirect/subresource_redirect_util_unittest.cc", "../renderer/subresource_redirect/subresource_redirect_util_unittest.cc",
"../renderer/v8_unwinder_unittest.cc", "../renderer/v8_unwinder_unittest.cc",
"../test/base/chrome_render_view_test.cc", "../test/base/chrome_render_view_test.cc",
......
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