chromium/components/safe_browsing/core/browser/hashprefix_realtime/hash_realtime_service_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/core/browser/hashprefix_realtime/hash_realtime_service.h"

#include <memory>

#include "base/base64url.h"
#include "base/strings/escape.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/safe_browsing/core/browser/db/v4_protocol_manager_util.h"
#include "components/safe_browsing/core/browser/hashprefix_realtime/ohttp_key_service.h"
#include "components/safe_browsing/core/browser/verdict_cache_manager.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/hashprefix_realtime/hash_realtime_utils.h"
#include "components/safe_browsing/core/common/proto/safebrowsingv5.pb.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "google_apis/google_api_keys.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_network_context.h"
#include "services/network/test/test_url_loader_factory.h"
#include "services/network/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

_;

namespace safe_browsing {

namespace {
// These two URLs have the same hash prefix. They were determined via a for loop
// up to 100,000 looking for the first two "https://example.a#" URLs whose hash
// prefixes matched.
constexpr char kUrlWithMatchingHashPrefix1[] =;
constexpr char kUrlWithMatchingHashPrefix2[] =;

constexpr char kTestRelayUrl[] =;
constexpr char kOhttpKey[] =;

// A class for testing requests sent via OHTTP. Call |AddResponse| and
// |SetInterceptor| to set up response before |GetViaObliviousHttp| is
// triggered.
class OhttpTestNetworkContext : public network::TestNetworkContext {};

class TestOhttpKeyService : public OhttpKeyService {};

class MockWebUIDelegate : public HashRealTimeService::WebUIDelegate {};

}  // namespace

class HashRealTimeServiceTest : public PlatformTest {};

class HashRealTimeServiceNoCacheManagerTest : public HashRealTimeServiceTest {};

TEST_F(HashRealTimeServiceTest, TestLookup_OneHash) {}

TEST_F(HashRealTimeServiceTest, TestLookup_OverlappingHashPrefixes) {}

TEST_F(HashRealTimeServiceTest, TestLookup_MaxHashes_Phishing) {}

TEST_F(HashRealTimeServiceTest,
       TestLookup_MaxHashes_SomeRelevantSomeIrrelevant) {}

TEST_F(HashRealTimeServiceTest, TestLookup_MaxHashes_OnlyIrrelevant) {}

TEST_F(HashRealTimeServiceTest, TestLookup_CompetingSeverities) {}

TEST_F(HashRealTimeServiceTest, TestLookup_Attributes) {}

TEST_F(HashRealTimeServiceTest, TestLookup_InvalidThreatTypes) {}

TEST_F(HashRealTimeServiceTest, TestLookup_InvalidAttributes) {}

TEST_F(HashRealTimeServiceTest, TestLookup_UnmatchedFullHashesInResponse) {}

TEST_F(HashRealTimeServiceTest, TestLookup_DuplicateFullHashesInResponse) {}

TEST_F(HashRealTimeServiceTest, TestLookup_DuplicateFullHashDetailsInResponse) {}

TEST_F(HashRealTimeServiceTest, TestLookupFailure_OhttpClientDestructedEarly) {}
TEST_F(HashRealTimeServiceTest, TestLookupFailure_NetError) {}
TEST_F(HashRealTimeServiceTest, TestLookupFailure_RetriableNetError) {}
TEST_F(HashRealTimeServiceTest, TestLookupFailure_NetErrorNameNotResolved) {}
TEST_F(HashRealTimeServiceTest, TestLookupFailure_NetErrorConnectionClosed) {}
TEST_F(HashRealTimeServiceTest, TestLookupFailure_NetErrorHttpCodeFailure) {}
TEST_F(HashRealTimeServiceTest, TestLookupFailure_OuterResponseCodeError) {}
TEST_F(HashRealTimeServiceTest, TestLookupFailure_InnerResponseCodeError) {}
TEST_F(HashRealTimeServiceTest, TestLookupFailure_ParseResponse) {}
TEST_F(HashRealTimeServiceTest, TestLookupFailure_IncorrectFullHashLength) {}
TEST_F(HashRealTimeServiceTest, TestLookupFailure_MissingCacheDuration) {}
TEST_F(HashRealTimeServiceTest, TestLookupFailure_MissingOhttpKey) {}

TEST_F(HashRealTimeServiceTest, TestFullyCached_OneHash_Safe) {}

TEST_F(HashRealTimeServiceTest, TestFullyCached_OneHash_Phishing) {}

TEST_F(HashRealTimeServiceTest, TestFullyCached_MaxHashes) {}

TEST_F(HashRealTimeServiceTest, TestFullyCached_OverlappingHashPrefixes) {}

// Since phishing is more severe than unwanted software, the results from the
// second request should be used rather than the cached results.
TEST_F(HashRealTimeServiceTest, TestPartiallyCached_RequestResultsUsed) {}

// Since phishing is more severe than unwanted software, the cached results
// should be used rather than the results from the second request.
TEST_F(HashRealTimeServiceTest, TestPartiallyCached_CachedResultsUsed) {}

TEST_F(HashRealTimeServiceTest, TestCacheDuration) {}

TEST_F(HashRealTimeServiceNoCacheManagerTest, TestNoCaching) {}

TEST_F(HashRealTimeServiceTest, TestShutdown) {}

TEST_F(HashRealTimeServiceTest, TestLookup_MultipleRequestsAtOnce) {}

TEST_F(HashRealTimeServiceTest, TestLookup_WebUiDelegateReturnsNullopt) {}

class HashRealTimeServiceNoWebUiDelegateTest : public HashRealTimeServiceTest {};
TEST_F(HashRealTimeServiceNoWebUiDelegateTest, TestLookup_NoWebUiDelegate) {}

TEST_F(HashRealTimeServiceTest, TestBackoffModeSet) {}

TEST_F(HashRealTimeServiceTest, TestBackoffModeSet_RetriableError) {}

TEST_F(HashRealTimeServiceTest, TestBackoffModeNotSet_MissingOhttpKey) {}

TEST_F(HashRealTimeServiceTest, TestBackoffModeRespected_FullyCached) {}

TEST_F(HashRealTimeServiceTest, TestBackoffModeRespected_NotCached) {}

TEST_F(HashRealTimeServiceTest, IsHashDetailMoreSevere) {}

}  // namespace safe_browsing