chromium/components/policy/core/browser/url_blocklist_manager_unittest.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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 {};

// Returns whether |url| matches the |pattern|.
bool MatchesPattern(const std::string& pattern, const std::string& url) {}

// Returns the state from blocklist after adding |pattern| to be blocked or
// allowed depending on |use_allowlist| and checking |url|.
URLBlocklist::URLBlocklistState GetUrlBlocklistStateAfterAddingPattern(
    const std::string& pattern,
    const std::string& url,
    const bool use_allowlist) {}

// Returns the URL blocklist state after adding the pattern to the blocklist.
URLBlocklist::URLBlocklistState GetUrlBlocklistStateAfterBlocking(
    const std::string& pattern,
    const std::string& url) {}

// Returns the URL blocklist state after adding the pattern to the allowlist.
URLBlocklist::URLBlocklistState GetUrlBlocklistStateAfterAllowing(
    const std::string& pattern,
    const std::string& url) {}

}  // namespace

TEST_F(URLBlocklistManagerTest, LoadBlocklistOnCreate) {}

TEST_F(URLBlocklistManagerTest, LoadAllowlistOnCreate) {}

TEST_F(URLBlocklistManagerTest, SingleUpdateForTwoPrefChanges) {}

// Non-special URLs behavior is affected by the
// StandardCompliantNonSpecialSchemeURLParsing feature.
// See https://crbug.com/40063064 for details.
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 for GetURLBlocklistState method.
TEST_F(URLBlocklistManagerTest, UseBlocklistState) {}

#if BUILDFLAG(IS_CHROMEOS)
// Custom BlocklistSource implementation.
// Custom BlocklistSource implementation.
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();
  // Verify that custom BlocklistSource is active.
  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")));
  // URLs not covered by the custom BlocklistSource should be in neutrat state.
  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();
  // Verify that default BlocklistSource is active.
  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")));
  // URLs not covered by the default BlocklistSource should be in neutrat state.
  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")));

  // Update the BlocklistSource.
  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
}  // namespace policy