1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/safe_browsing/test_safe_browsing_database_helper.h"
#include <utility>
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/strings/stringprintf.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "chrome/browser/safe_browsing/test_safe_browsing_service.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/security_interstitials/core/unsafe_resource.h"
#include "content/public/test/test_utils.h"
namespace {
// UI manager that never actually shows any interstitials, but emulates as if
// the user chose to proceed through them.
class FakeSafeBrowsingUIManager
: public safe_browsing::TestSafeBrowsingUIManager {
public:
FakeSafeBrowsingUIManager() = default;
FakeSafeBrowsingUIManager(const FakeSafeBrowsingUIManager&) = delete;
FakeSafeBrowsingUIManager& operator=(const FakeSafeBrowsingUIManager&) =
delete;
protected:
~FakeSafeBrowsingUIManager() override = default;
void DisplayBlockingPage(const UnsafeResource& resource) override {
resource.DispatchCallback(FROM_HERE, true /* proceed */,
true /* showed_interstitial */,
false /* has_post_commit_interstitial_skipped */);
}
};
} // namespace
// This class automatically inserts lists into the store map when initializing
// the test database.
class InsertingDatabaseFactory : public safe_browsing::TestV4DatabaseFactory {
public:
explicit InsertingDatabaseFactory(
safe_browsing::TestV4StoreFactory* store_factory,
const std::vector<safe_browsing::ListIdentifier>& lists_to_insert)
: lists_to_insert_(lists_to_insert), store_factory_(store_factory) {}
std::unique_ptr<safe_browsing::V4Database, base::OnTaskRunnerDeleter> Create(
const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
std::unique_ptr<safe_browsing::StoreMap> store_map) override {
const base::FilePath base_store_path(FILE_PATH_LITERAL("UrlDb.store"));
for (const auto& id : lists_to_insert_) {
if (!base::Contains(*store_map, id)) {
const base::FilePath store_path = base::GetUniquePath(base_store_path);
store_map->insert(
{id, store_factory_->CreateV4Store(
db_task_runner,
store_path.empty() ? base_store_path : store_path)});
}
}
for (const auto& it : *store_map)
lists_.push_back(it.first);
return safe_browsing::TestV4DatabaseFactory::Create(db_task_runner,
std::move(store_map));
}
const std::vector<safe_browsing::ListIdentifier> lists() { return lists_; }
private:
std::vector<safe_browsing::ListIdentifier> lists_to_insert_;
std::vector<safe_browsing::ListIdentifier> lists_;
raw_ptr<safe_browsing::TestV4StoreFactory> store_factory_;
};
TestSafeBrowsingDatabaseHelper::TestSafeBrowsingDatabaseHelper()
: TestSafeBrowsingDatabaseHelper(
std::make_unique<
safe_browsing::TestV4GetHashProtocolManagerFactory>(),
std::vector<safe_browsing::ListIdentifier>()) {}
TestSafeBrowsingDatabaseHelper::TestSafeBrowsingDatabaseHelper(
std::unique_ptr<safe_browsing::TestV4GetHashProtocolManagerFactory>
v4_get_hash_factory,
std::vector<safe_browsing::ListIdentifier> lists_to_insert)
: v4_get_hash_factory_(v4_get_hash_factory.get()) {
sb_factory_ =
std::make_unique<safe_browsing::TestSafeBrowsingServiceFactory>();
sb_factory_->SetTestUIManager(new FakeSafeBrowsingUIManager());
sb_factory_->UseV4LocalDatabaseManager();
safe_browsing::SafeBrowsingService::RegisterFactory(sb_factory_.get());
auto store_factory = std::make_unique<safe_browsing::TestV4StoreFactory>();
auto v4_db_factory = std::make_unique<InsertingDatabaseFactory>(
store_factory.get(), lists_to_insert);
v4_db_factory_ = v4_db_factory.get();
safe_browsing::V4Database::RegisterStoreFactoryForTest(
std::move(store_factory));
safe_browsing::V4Database::RegisterDatabaseFactoryForTest(
std::move(v4_db_factory));
if (v4_get_hash_factory) {
safe_browsing::V4GetHashProtocolManager::RegisterFactory(
std::move(v4_get_hash_factory));
}
}
TestSafeBrowsingDatabaseHelper::~TestSafeBrowsingDatabaseHelper() {
safe_browsing::V4GetHashProtocolManager::RegisterFactory(nullptr);
safe_browsing::V4Database::RegisterDatabaseFactoryForTest(nullptr);
safe_browsing::V4Database::RegisterStoreFactoryForTest(nullptr);
safe_browsing::SafeBrowsingService::RegisterFactory(nullptr);
}
void TestSafeBrowsingDatabaseHelper::AddFullHashToDbAndFullHashCache(
const GURL& bad_url,
const safe_browsing::ListIdentifier& list_id,
const safe_browsing::ThreatMetadata& threat_metadata) {
// Should only be called if we are mocking the v4 hash factory.
DCHECK(v4_get_hash_factory_);
LocallyMarkPrefixAsBad(bad_url, list_id);
safe_browsing::FullHashInfo full_hash_info =
GetFullHashInfoWithMetadata(bad_url, list_id, threat_metadata);
v4_get_hash_factory_->AddToFullHashCache(full_hash_info);
}
void TestSafeBrowsingDatabaseHelper::LocallyMarkPrefixAsBad(
const GURL& url,
const safe_browsing::ListIdentifier& list_id) {
safe_browsing::FullHashStr full_hash =
safe_browsing::V4ProtocolManagerUtil::GetFullHash(url);
while (!v4_db_factory_->IsReady()) {
content::RunAllTasksUntilIdle();
}
v4_db_factory_->MarkPrefixAsBad(list_id, full_hash);
}
bool TestSafeBrowsingDatabaseHelper::HasListSynced(
const safe_browsing::ListIdentifier& list_id) {
return base::Contains(v4_db_factory_->lists(), list_id);
}
|