chromium/components/safe_browsing/core/browser/db/v4_local_database_manager_unittest.cc

// Copyright 2016 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/db/v4_local_database_manager.h"

#include <utility>

#include "base/command_line.h"
#include "base/containers/fixed_flat_map.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/strings/string_tokenizer.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_command_line.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "base/test/test_simple_task_runner.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "components/safe_browsing/core/browser/db/v4_database.h"
#include "components/safe_browsing/core/browser/db/v4_protocol_manager_util.h"
#include "components/safe_browsing/core/browser/db/v4_test_util.h"
#include "components/safe_browsing/core/common/features.h"
#include "crypto/sha2.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_url_loader_factory.h"
#include "testing/platform_test.h"

namespace safe_browsing {

using enum ExtendedReportingLevel;

namespace {

FullHashInfos;

// Utility function for populating hashes.
FullHashStr HashForUrl(const GURL& url) {}

const int kDefaultStoreFileSizeInBytes =;

// Use this if you want GetFullHashes() to always return prescribed results.
class FakeGetHashProtocolManager : public V4GetHashProtocolManager {};

class FakeGetHashProtocolManagerFactory
    : public V4GetHashProtocolManagerFactory {};

// Use FakeGetHashProtocolManagerFactory in scope, then reset.
// You should make sure the DatabaseManager is created _after_ this.
class ScopedFakeGetHashProtocolManagerFactory {};

}  // namespace

// Use this if you want to use a real V4GetHashProtocolManager, but substitute
// the server response via the |test_url_loader_factory|.
// This must be defined outside the anonymous namespace so that it can be
// included as a friend class for V4GetHashProtocolManager.
class GetHashProtocolManagerFactoryWithTestUrlLoader
    : public V4GetHashProtocolManagerFactory {};

// Use ScopedGetHashProtocolManagerFactoryWithTestUrlLoader in scope, then
// reset. You should make sure the DatabaseManager is created _after_ this.
class ScopedGetHashProtocolManagerFactoryWithTestUrlLoader {};

class FakeV4Database : public V4Database {};

// TODO(nparker): This might be simpler with a mock and EXPECT calls.
// That would also catch unexpected calls.
class TestClient : public SafeBrowsingDatabaseManager::Client {};

class TestAllowlistClient : public SafeBrowsingDatabaseManager::Client {};

class TestExtensionClient : public SafeBrowsingDatabaseManager::Client {};

class FakeV4LocalDatabaseManager : public V4LocalDatabaseManager {};

class V4LocalDatabaseManagerTest : public PlatformTest {};

TEST_F(V4LocalDatabaseManagerTest, TestGetThreatSource) {}

TEST_F(V4LocalDatabaseManagerTest, TestCanCheckUrl) {}

TEST_F(V4LocalDatabaseManagerTest,
       TestCheckBrowseUrlWithEmptyStoresReturnsNoMatch) {}

TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) {}

TEST_F(V4LocalDatabaseManagerTest, TestCheckCsdAllowlistWithPrefixMatch) {}

// This is like CsdAllowlistWithPrefixMatch, but we also verify the
// full-hash-match results in an appropriate callback value.
TEST_F(V4LocalDatabaseManagerTest,
       TestCheckCsdAllowlistWithPrefixTheFullMatch) {}

TEST_F(V4LocalDatabaseManagerTest, TestCheckCsdAllowlistWithFullMatch) {}

TEST_F(V4LocalDatabaseManagerTest, TestCheckCsdAllowlistWithNoMatch) {}

// When allowlist is unavailable, all URLS should be allowed.
TEST_F(V4LocalDatabaseManagerTest, TestCheckCsdAllowlistUnavailable) {}

TEST_F(V4LocalDatabaseManagerTest,
       TestCheckBrowseUrlReturnsNoMatchWhenDisabled) {}

// Hash prefix matches on the high confidence allowlist, but not a full hash
// match, so it says there is no match and does not perform a full hash check.
// This can only happen with an invalid db setup.
TEST_F(V4LocalDatabaseManagerTest,
       TestCheckUrlForHCAllowlistWithPrefixMatchButNoLocalFullHashMatch) {}

// Full hash match on the high confidence allowlist. Returns true
// synchronously and the full hash check is not performed.
TEST_F(V4LocalDatabaseManagerTest,
       TestCheckUrlForHCAllowlistWithLocalFullHashMatch) {}

// Hash prefix has no match on the high confidence allowlist. Returns false
// synchronously and the full hash check is not performed.
TEST_F(V4LocalDatabaseManagerTest, TestCheckUrlForHCAllowlistWithNoMatch) {}

// When allowlist is unavailable, all URLs should be considered as matches.
TEST_F(V4LocalDatabaseManagerTest, TestCheckUrlForHCAllowlistUnavailable) {}

TEST_F(V4LocalDatabaseManagerTest, TestCheckUrlForHCAllowlistAfterStopping) {}

// When allowlist is available but the size is too small, all URLs should be
// considered as matches.
TEST_F(V4LocalDatabaseManagerTest, TestCheckUrlForHCAllowlistSmallSize) {}

// Tests the command line flag that skips the high-confidence allowlist check.
TEST_F(V4LocalDatabaseManagerTest,
       TestCheckUrlForHCAllowlistSkippedViaCommandLineSwitch) {}

TEST_F(V4LocalDatabaseManagerTest, TestGetSeverestThreatTypeAndMetadata) {}

TEST_F(V4LocalDatabaseManagerTest, TestChecksAreQueued) {}

TEST_F(V4LocalDatabaseManagerTest, CancelWhileQueued) {}

// Verify that a window where checks cannot be cancelled is closed.
TEST_F(V4LocalDatabaseManagerTest, CancelPending) {}

// When the database load flushes the queued requests, make sure that
// CancelCheck() is not fatal in the client callback.
TEST_F(V4LocalDatabaseManagerTest, CancelQueued) {}

TEST_F(V4LocalDatabaseManagerTest, QueuedCheckWithFullHash) {}

// This test is somewhat similar to TestCheckBrowseUrlWithFakeDbReturnsMatch but
// it uses a fake V4LocalDatabaseManager to assert that PerformFullHashCheck is
// called async.
TEST_F(V4LocalDatabaseManagerTest, PerformFullHashCheckCalledAsync) {}

TEST_F(V4LocalDatabaseManagerTest, UsingWeakPtrDropsCallback) {}

TEST_F(V4LocalDatabaseManagerTest, TestMatchDownloadAllowlistUrl) {}

// This verifies the fix for race in http://crbug.com/660293
TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithSameClientAndCancel) {}

TEST_F(V4LocalDatabaseManagerTest, TestSubresourceFilterCallback) {}

TEST_F(V4LocalDatabaseManagerTest, TestCheckExtensionIDsNothingBlocklisted) {}

TEST_F(V4LocalDatabaseManagerTest, TestCheckExtensionIDsOneIsBlocklisted) {}

// This is similar to |TestCheckExtensionIDsOneIsBlocklisted|, but it uses a
// real |V4GetHashProtocolManager| instead of |FakeGetHashProtocolManager|. This
// tests that the values passed into the protocol manager are usable.
TEST_F(V4LocalDatabaseManagerTest,
       TestCheckExtensionIDsOneIsBlocklisted_RealProtocolManager) {}

TEST_F(V4LocalDatabaseManagerTest, TestCheckDownloadUrlNothingBlocklisted) {}

TEST_F(V4LocalDatabaseManagerTest, TestCheckDownloadUrlWithOneBlocklisted) {}

TEST_F(V4LocalDatabaseManagerTest, NotificationOnUpdate) {}

TEST_F(V4LocalDatabaseManagerTest, FlagOneUrlAsPhishing) {}

TEST_F(V4LocalDatabaseManagerTest, FlagOneUrlAsMalware) {}

TEST_F(V4LocalDatabaseManagerTest, FlagOneUrlAsUWS) {}

TEST_F(V4LocalDatabaseManagerTest, FlagMultipleUrls) {}

// Verify that the correct set of lists is synced on each platform: iOS,
// Chrome-branded desktop, and non-Chrome-branded desktop.
TEST_F(V4LocalDatabaseManagerTest, SyncedLists) {}

TEST_F(V4LocalDatabaseManagerTest, TestQueuedChecksMatchArtificialPrefixes) {}

}  // namespace safe_browsing