chromium/components/policy/content/policy_blocklist_navigation_throttle_unittest.cc

// Copyright 2018 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/content/policy_blocklist_navigation_throttle.h"

#include <memory>
#include <string>
#include <utility>

#include "base/test/metrics/histogram_tester.h"
#include "base/values.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/policy/content/policy_blocklist_service.h"
#include "components/policy/content/safe_search_service.h"
#include "components/policy/content/safe_sites_navigation_throttle.h"
#include "components/policy/core/browser/url_blocklist_manager.h"
#include "components/policy/core/browser/url_blocklist_policy_handler.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/safe_search_api/stub_url_checker.h"
#include "components/safe_search_api/url_checker.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/navigation_throttle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_renderer_host.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace {

SafeSitesFilterBehavior;

constexpr size_t kCacheSize =;

}  // namespace

// TODO(crbug.com/40156526): Break out the tests into separate files. The
// SafeSites tests should be parameterized to run the same tests on both types.
class SafeSitesNavigationThrottleTest
    : public content::RenderViewHostTestHarness,
      public content::WebContentsObserver {};

class SafeSitesNavigationThrottleWithErrorContentTest
    : public SafeSitesNavigationThrottleTest {};

const char
    SafeSitesNavigationThrottleWithErrorContentTest::kErrorPageContent[] =;

class PolicyBlocklistNavigationThrottleTest
    : public SafeSitesNavigationThrottleTest {};

TEST_F(PolicyBlocklistNavigationThrottleTest, Blocklist) {}

TEST_F(PolicyBlocklistNavigationThrottleTest, Allowlist) {}

TEST_F(PolicyBlocklistNavigationThrottleTest, SafeSites_Safe) {}

TEST_F(PolicyBlocklistNavigationThrottleTest, SafeSites_Porn) {}

TEST_F(PolicyBlocklistNavigationThrottleTest, SafeSites_Allowlisted) {}

TEST_F(PolicyBlocklistNavigationThrottleTest, SafeSites_Schemes) {}

TEST_F(PolicyBlocklistNavigationThrottleTest, SafeSites_PolicyChange) {}

TEST_F(PolicyBlocklistNavigationThrottleTest, SafeSites_Failure) {}

void SafeSitesNavigationThrottleTest::TestSafeSitesCachedSites(
    const char* expected_error_page_content) {}

TEST_F(SafeSitesNavigationThrottleTest, SafeSites_CachedSites) {}

TEST_F(SafeSitesNavigationThrottleWithErrorContentTest, SafeSites_CachedSites) {}

TEST_F(PolicyBlocklistNavigationThrottleTest, SafeSites_CachedSites) {}

void SafeSitesNavigationThrottleTest::TestSafeSitesRedirectAndCachedSites(
    const char* expected_error_page_content) {}

TEST_F(SafeSitesNavigationThrottleTest, SafeSites_RedirectAndCachedSites) {}

TEST_F(SafeSitesNavigationThrottleWithErrorContentTest,
       SafeSites_RedirectAndCachedSites) {}

TEST_F(PolicyBlocklistNavigationThrottleTest,
       SafeSites_RedirectAndCachedSites) {}

#if BUILDFLAG(IS_CHROMEOS)
TEST_F(PolicyBlocklistNavigationThrottleTest, UseVpnPreConnectFiltering) {
  SetBlocklistUrlPattern("block-by-general-pref.com");
  base::Value::List list;
  list.Append("allowed-preconnect.com");
  pref_service_.SetManagedPref(
      policy::policy_prefs::kAlwaysOnVpnPreConnectUrlAllowlist,
      base::Value(std::move(list)));

  PolicyBlocklistService* service =
      PolicyBlocklistFactory::GetForBrowserContext(browser_context());
  service->SetAlwaysOnVpnPreConnectUrlAllowlistEnforced(
      /*enforced=*/true);

  task_environment()->RunUntilIdle();

  // Verify that the pref kAlwaysOnVpnPreConnectUrlAllowlist is enforced
  // while kEnforceAlwaysOnVpnPreConnectUrlAllowlist is true.
  auto navigation_simulator =
      StartNavigation(GURL("http://allowed-preconnect.com/"));
  ASSERT_FALSE(navigation_simulator->IsDeferred());
  EXPECT_EQ(content::NavigationThrottle::PROCEED,
            navigation_simulator->GetLastThrottleCheckResult());
  navigation_simulator =
      StartNavigation(GURL("http://neural-by-general-pref.com/"));
  EXPECT_EQ(content::NavigationThrottle::BLOCK_REQUEST,
            navigation_simulator->GetLastThrottleCheckResult());

  service->SetAlwaysOnVpnPreConnectUrlAllowlistEnforced(
      /*enforced=*/false);

  task_environment()->RunUntilIdle();

  navigation_simulator =
      StartNavigation(GURL("http://block-by-general-pref.com/"));
  ASSERT_FALSE(navigation_simulator->IsDeferred());
  EXPECT_EQ(content::NavigationThrottle::BLOCK_REQUEST,
            navigation_simulator->GetLastThrottleCheckResult());
  navigation_simulator =
      StartNavigation(GURL("http://neural-by-general-pref.com/"));
  EXPECT_EQ(content::NavigationThrottle::PROCEED,
            navigation_simulator->GetLastThrottleCheckResult());
}
#endif