#include "components/policy/core/browser/url_blocklist_manager.h"
#include <stdint.h>
#include <memory>
#include <ostream>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/url_formatter/url_fixer.h"
#include "google_apis/gaia/gaia_urls.h"
#include "net/base/load_flags.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/url_features.h"
namespace policy {
namespace {
class TestingURLBlocklistManager : public URLBlocklistManager { … };
class URLBlocklistManagerTest : public testing::Test { … };
bool MatchesPattern(const std::string& pattern, const std::string& url) { … }
URLBlocklist::URLBlocklistState GetUrlBlocklistStateAfterAddingPattern(
const std::string& pattern,
const std::string& url,
const bool use_allowlist) { … }
URLBlocklist::URLBlocklistState GetUrlBlocklistStateAfterBlocking(
const std::string& pattern,
const std::string& url) { … }
URLBlocklist::URLBlocklistState GetUrlBlocklistStateAfterAllowing(
const std::string& pattern,
const std::string& url) { … }
}
TEST_F(URLBlocklistManagerTest, LoadBlocklistOnCreate) { … }
TEST_F(URLBlocklistManagerTest, LoadAllowlistOnCreate) { … }
TEST_F(URLBlocklistManagerTest, SingleUpdateForTwoPrefChanges) { … }
class URLBlocklistParamTest : public ::testing::Test,
public ::testing::WithParamInterface<bool> { … };
TEST_P(URLBlocklistParamTest, Filtering) { … }
TEST_F(URLBlocklistManagerTest, QueryParameters) { … }
TEST_F(URLBlocklistManagerTest, BlockAllWithExceptions) { … }
TEST_P(URLBlocklistParamTest, DefaultBlocklistExceptions) { … }
TEST_P(URLBlocklistParamTest, BlocklistBasicCoverage) { … }
INSTANTIATE_TEST_SUITE_P(…);
TEST_F(URLBlocklistManagerTest, UseBlocklistState) { … }
#if BUILDFLAG(IS_CHROMEOS)
class CustomBlocklistSource : public BlocklistSource {
public:
CustomBlocklistSource() {}
CustomBlocklistSource(const CustomBlocklistSource&) = delete;
CustomBlocklistSource& operator=(const CustomBlocklistSource&) = delete;
~CustomBlocklistSource() override = default;
const base::Value::List* GetBlocklistSpec() const override {
return &blocklist_;
}
const base::Value::List* GetAllowlistSpec() const override {
return &allowlist_;
}
void SetBlocklistObserver(base::RepeatingClosure observer) override {
blocklist_observer_ = std::move(observer);
}
void SetBlocklistSpec(base::Value::List blocklist) {
blocklist_ = std::move(blocklist);
TriggerObserver();
}
void SetAllowlistSpec(base::Value::List allowlist) {
allowlist_ = std::move(allowlist);
TriggerObserver();
}
private:
void TriggerObserver() {
if (!blocklist_observer_) {
return;
}
blocklist_observer_.Run();
}
base::Value::List blocklist_;
base::Value::List allowlist_;
base::RepeatingClosure blocklist_observer_;
};
TEST_F(URLBlocklistManagerTest, SetAndUnsetOverrideBlockListSource) {
SetUrlBlocklistPref(
base::Value::List().Append("blocked-by-general-pref.com"));
SetUrlAllowlistPref(
base::Value::List().Append("allowed-by-general-pref.com"));
task_environment()->RunUntilIdle();
std::unique_ptr<CustomBlocklistSource> custom_blocklist =
std::make_unique<CustomBlocklistSource>();
custom_blocklist->SetAllowlistSpec(
base::Value::List().Append("allowed-preconnect.com"));
custom_blocklist->SetBlocklistSpec(
base::Value::List().Append("blocked-preconnect.com"));
blocklist_manager()->SetOverrideBlockListSource(std::move(custom_blocklist));
task_environment()->RunUntilIdle();
EXPECT_EQ(URLBlocklist::URL_IN_ALLOWLIST,
blocklist_manager()->GetURLBlocklistState(
GURL("http://allowed-preconnect.com")));
EXPECT_EQ(URLBlocklist::URL_IN_BLOCKLIST,
blocklist_manager()->GetURLBlocklistState(
GURL("http://blocked-preconnect.com")));
EXPECT_EQ(URLBlocklist::URL_NEUTRAL_STATE,
blocklist_manager()->GetURLBlocklistState(
GURL("http://allowed-by-general-pref.com")));
EXPECT_EQ(URLBlocklist::URL_NEUTRAL_STATE,
blocklist_manager()->GetURLBlocklistState(
GURL("http://neutral-by-general-pref.com")));
blocklist_manager()->SetOverrideBlockListSource(nullptr);
task_environment()->RunUntilIdle();
EXPECT_EQ(URLBlocklist::URL_IN_BLOCKLIST,
blocklist_manager()->GetURLBlocklistState(
GURL("http://blocked-by-general-pref.com")));
EXPECT_EQ(URLBlocklist::URL_IN_ALLOWLIST,
blocklist_manager()->GetURLBlocklistState(
GURL("http://allowed-by-general-pref.com")));
EXPECT_EQ(URLBlocklist::URL_NEUTRAL_STATE,
blocklist_manager()->GetURLBlocklistState(
GURL("http://allowed-preconnect.com")));
EXPECT_EQ(URLBlocklist::URL_NEUTRAL_STATE,
blocklist_manager()->GetURLBlocklistState(
GURL("http://blocked-preconnect.com")));
}
TEST_F(URLBlocklistManagerTest, BlockListSourceUpdates) {
std::unique_ptr<CustomBlocklistSource> custom_blocklist =
std::make_unique<CustomBlocklistSource>();
custom_blocklist->SetBlocklistSpec(
base::Value::List().Append("preconnect.com"));
raw_ptr<CustomBlocklistSource> custom_blocklist_ptr = custom_blocklist.get();
blocklist_manager()->SetOverrideBlockListSource(std::move(custom_blocklist));
task_environment()->RunUntilIdle();
EXPECT_EQ(
URLBlocklist::URL_IN_BLOCKLIST,
blocklist_manager()->GetURLBlocklistState(GURL("http://preconnect.com")));
custom_blocklist_ptr->SetBlocklistSpec(base::Value::List());
task_environment()->RunUntilIdle();
custom_blocklist_ptr = nullptr;
EXPECT_EQ(
URLBlocklist::URL_NEUTRAL_STATE,
blocklist_manager()->GetURLBlocklistState(GURL("http://preconnect.com")));
}
#endif
}