chromium/components/safe_browsing/content/browser/browser_url_loader_throttle_unittest.cc

// Copyright 2023 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/safe_browsing/content/browser/browser_url_loader_throttle.h"

#include <string_view>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "components/safe_browsing/content/browser/async_check_tracker.h"
#include "components/safe_browsing/content/browser/base_ui_manager.h"
#include "components/safe_browsing/content/browser/url_checker_holder.h"
#include "components/safe_browsing/core/browser/realtime/fake_url_lookup_service.h"
#include "components/safe_browsing/core/browser/safe_browsing_url_checker_impl.h"
#include "components/safe_browsing/core/browser/url_checker_delegate.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/hashprefix_realtime/hash_realtime_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_web_contents_factory.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace safe_browsing {

namespace {

UnsafeResource;

class MockUrlCheckerDelegate : public UrlCheckerDelegate {};

class MockThrottleDelegate : public blink::URLLoaderThrottle::Delegate {};

class FakeRealTimeUrlLookupService
    : public testing::FakeRealTimeUrlLookupService {};

class MockSafeBrowsingUrlChecker : public SafeBrowsingUrlCheckerImpl {};

struct WillStartRequestOptionalArgs {};

}  // namespace

class SBBrowserUrlLoaderThrottleTestBase : public ::testing::Test {};

class SBBrowserUrlLoaderThrottleTest
    : public SBBrowserUrlLoaderThrottleTestBase,
      public ::testing::WithParamInterface<bool> {};

INSTANTIATE_TEST_SUITE_P();

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyDefer_DoesNotDeferOnSafeDocumentUrl) {}

TEST_P(SBBrowserUrlLoaderThrottleTest, VerifyDefer_DeferOnUnsafeDocumentUrl) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyDefer_DoesNotDeferOnUnsafeIframeUrl) {}

TEST_P(SBBrowserUrlLoaderThrottleTest, VerifyDefer_DeferIfRedirectUrlIsUnsafe) {}

TEST_P(SBBrowserUrlLoaderThrottleTest, VerifyDefer_DoesNotDeferOnSkippedUrl) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyDefer_DoesNotDeferOnSkippedDocumentUrl) {}

TEST_P(SBBrowserUrlLoaderThrottleTest, VerifyDefer_DoesNotDeferOnKnownSafeUrl) {}

TEST_P(SBBrowserUrlLoaderThrottleTest, VerifyDefer_DeferOnSlowCheck) {}

TEST_P(SBBrowserUrlLoaderThrottleTest, VerifyDefer_DeferOnSlowRedirectCheck) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyDefer_DoesNotResumeOnSlowCheckNotProceed) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyDefer_DoesNotDeferRedirectOnSlowCheck) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyDefer_DeferRedirectWhenFirstUrlAlreadyBlocked) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyErrorCodeWhenInterstitialNotShown) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_FastCheckFromNetwork) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_FastCheckFromCache) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_SlowCheckFromNetwork) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_SlowCheckFromCache) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_HashPrefixDatabaseCheck) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_HashPrefixRealTimeCheck) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_SkippedCheck) {}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_UrlRealTimeCheck) {}

class SBBrowserUrlLoaderThrottleAsyncCheckTest
    : public SBBrowserUrlLoaderThrottleTestBase {};

TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest, VerifyCheckerParams) {}

// Sync check completed -> WillProcessResponse called -> async check completed.
TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest, VerifyDefer_AsyncNotDeferred) {}

// WillProcessResponse called -> Sync check completed.
TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest, VerifyDefer_SyncResumed) {}

// Async check completed -> WillProcessResponse called -> Sync check completed.
TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyDefer_AsyncCompletedBeforeSync) {}

// URL redirected -> Sync check completed -> WillProcessResponse called.
TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyDefer_AsyncRedirectCheckNotCompleted) {}

// Async check completed -> Sync check completed -> WillProcessResponse called.
TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyDefer_AsyncCheckCompletedBeforeSyncCheck) {}

// WillProcessResponse called -> Async check completed -> Sync check completed.
TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyDefer_AsyncCheckCompletedAfterProcessResponseBeforeSyncCheck) {}

TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyErrorCode_SyncCheckBlocked) {}

TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyErrorCode_SyncCheckBlockedOnRedirect) {}

TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyErrorCode_AsyncCheckBlocked) {}

TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyAsyncChecksNotEligible_LoadPrefetch) {}

}  // namespace safe_browsing