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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
// Copyright 2015 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/incident_reporting/state_store.h"
#include <stdint.h>
#include "base/files/scoped_temp_dir.h"
#include "base/json/json_file_value_serializer.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_simple_task_runner.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/safe_browsing/incident_reporting/incident.h"
#include "chrome/browser/safe_browsing/incident_reporting/platform_state_store.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/in_memory_pref_store.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/sync_preferences/pref_service_syncable.h"
#include "components/sync_preferences/pref_service_syncable_factory.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/browser/quota_service.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_WIN)
#include "base/test/test_reg_util_win.h"
#endif
namespace safe_browsing {
#if BUILDFLAG(IS_WIN)
// A base test fixture that redirects HKCU for testing the platform state store
// backed by the Windows registry to prevent interference with existing Chrome
// installs or other tests.
class PlatformStateStoreTestBase : public ::testing::Test {
protected:
void SetUp() override {
::testing::Test::SetUp();
ASSERT_NO_FATAL_FAILURE(
registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER));
}
private:
registry_util::RegistryOverrideManager registry_override_manager_;
};
#else // BUILDFLAG(IS_WIN)
using PlatformStateStoreTestBase = ::testing::Test;
#endif // BUILDFLAG(IS_WIN)
// A test fixture with a testing profile that writes its user prefs to a json
// file.
class StateStoreTest : public PlatformStateStoreTestBase {
protected:
struct TestData {
IncidentType type;
const char* key;
uint32_t digest;
};
StateStoreTest()
: profile_(nullptr),
task_runner_(new base::TestSimpleTaskRunner()),
profile_manager_(TestingBrowserProcess::GetGlobal()) {}
StateStoreTest(const StateStoreTest&) = delete;
StateStoreTest& operator=(const StateStoreTest&) = delete;
void SetUp() override {
PlatformStateStoreTestBase::SetUp();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
ASSERT_TRUE(profile_manager_.SetUp());
CreateProfile();
}
void DeleteProfile() {
if (profile_) {
profile_ = nullptr;
profile_manager_.DeleteTestingProfile(kProfileName_);
}
}
// Commits a pending write (which posts a task to task_runner_) and waits for
// it to finish.
void CommitWrite() {
ASSERT_NE(nullptr, profile_);
profile_->GetPrefs()->CommitPendingWrite();
task_runner_->RunUntilIdle();
}
// Removes the safebrowsing.incidents_sent preference from the profile's pref
// store.
void TrimPref() {
ASSERT_EQ(nullptr, profile_.get());
std::unique_ptr<base::Value> prefs(JSONFileValueDeserializer(GetPrefsPath())
.Deserialize(nullptr, nullptr));
ASSERT_NE(nullptr, prefs.get());
base::Value::Dict* dict = prefs->GetIfDict();
ASSERT_TRUE(dict);
ASSERT_TRUE(dict->RemoveByDottedPath(prefs::kSafeBrowsingIncidentsSent));
ASSERT_TRUE(JSONFileValueSerializer(GetPrefsPath()).Serialize(*dict));
}
void CreateProfile() {
ASSERT_EQ(nullptr, profile_.get());
// Create the testing profile with a file-backed user pref store.
sync_preferences::PrefServiceSyncableFactory factory;
factory.SetUserPrefsFile(GetPrefsPath(), task_runner_.get());
factory.SetAccountPrefStore(base::MakeRefCounted<InMemoryPrefStore>());
user_prefs::PrefRegistrySyncable* pref_registry =
new user_prefs::PrefRegistrySyncable();
RegisterUserProfilePrefs(pref_registry);
profile_ = profile_manager_.CreateTestingProfile(
kProfileName_, factory.CreateSyncable(pref_registry),
base::UTF8ToUTF16(kProfileName_), 0,
TestingProfile::TestingFactories());
}
static const char kProfileName_[];
static const TestData kTestData_[];
content::BrowserTaskEnvironment task_environment_;
raw_ptr<TestingProfile, DanglingUntriaged> profile_;
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
private:
base::FilePath GetPrefsPath() {
return temp_dir_.GetPath().AppendASCII("prefs");
}
extensions::QuotaService::ScopedDisablePurgeForTesting
disable_purge_for_testing_;
base::ScopedTempDir temp_dir_;
TestingProfileManager profile_manager_;
};
// static
const char StateStoreTest::kProfileName_[] = "test_profile";
const StateStoreTest::TestData StateStoreTest::kTestData_[] = {
{IncidentType::TRACKED_PREFERENCE, "tp_one", 1},
{IncidentType::TRACKED_PREFERENCE, "tp_two", 2},
{IncidentType::TRACKED_PREFERENCE, "tp_three", 3},
{IncidentType::BINARY_INTEGRITY, "bi", 0},
};
TEST_F(StateStoreTest, MarkAsAndHasBeenReported) {
StateStore state_store(profile_);
for (const auto& data : kTestData_)
ASSERT_FALSE(state_store.HasBeenReported(data.type, data.key, data.digest));
{
StateStore::Transaction transaction(&state_store);
for (const auto& data : kTestData_) {
transaction.MarkAsReported(data.type, data.key, data.digest);
ASSERT_TRUE(
state_store.HasBeenReported(data.type, data.key, data.digest));
}
}
for (const auto& data : kTestData_)
ASSERT_TRUE(state_store.HasBeenReported(data.type, data.key, data.digest));
}
TEST_F(StateStoreTest, ClearForType) {
StateStore state_store(profile_);
{
StateStore::Transaction transaction(&state_store);
for (const auto& data : kTestData_)
transaction.MarkAsReported(data.type, data.key, data.digest);
}
for (const auto& data : kTestData_)
ASSERT_TRUE(state_store.HasBeenReported(data.type, data.key, data.digest));
const IncidentType removed_type = IncidentType::TRACKED_PREFERENCE;
StateStore::Transaction(&state_store).ClearForType(removed_type);
for (const auto& data : kTestData_) {
if (data.type == removed_type) {
ASSERT_FALSE(
state_store.HasBeenReported(data.type, data.key, data.digest));
} else {
ASSERT_TRUE(
state_store.HasBeenReported(data.type, data.key, data.digest));
}
}
}
TEST_F(StateStoreTest, ClearAll) {
StateStore state_store(profile_);
// Write some state to the store.
{
StateStore::Transaction transaction(&state_store);
for (const auto& data : kTestData_)
transaction.MarkAsReported(data.type, data.key, data.digest);
}
StateStore::Transaction(&state_store).ClearAll();
for (const auto& data : kTestData_) {
ASSERT_FALSE(state_store.HasBeenReported(data.type, data.key, data.digest));
}
// Write prefs out to the JsonPrefStore.
CommitWrite();
// Delete the profile.
DeleteProfile();
// Recreate the profile.
CreateProfile();
StateStore store_2(profile_);
for (const auto& data : kTestData_) {
// Verify that the state did not survive through the Platform State Store.
ASSERT_FALSE(store_2.HasBeenReported(data.type, data.key, data.digest));
}
}
TEST_F(StateStoreTest, Persistence) {
// Write some state to the store.
{
StateStore state_store(profile_);
StateStore::Transaction transaction(&state_store);
for (const auto& data : kTestData_)
transaction.MarkAsReported(data.type, data.key, data.digest);
}
// Run tasks to write prefs out to the JsonPrefStore.
CommitWrite();
// Delete the profile.
DeleteProfile();
// Recreate the profile.
CreateProfile();
// Verify that the state survived.
StateStore state_store(profile_);
for (const auto& data : kTestData_)
ASSERT_TRUE(state_store.HasBeenReported(data.type, data.key, data.digest));
}
TEST_F(StateStoreTest, PersistenceWithStoreDelete) {
// Write some state to the store.
{
StateStore state_store(profile_);
StateStore::Transaction transaction(&state_store);
for (const auto& data : kTestData_)
transaction.MarkAsReported(data.type, data.key, data.digest);
}
// Write prefs out to the JsonPrefStore.
CommitWrite();
// Delete the profile.
DeleteProfile();
// Delete the state pref.
TrimPref();
// Recreate the profile.
CreateProfile();
StateStore state_store(profile_);
for (const auto& data : kTestData_) {
#if defined(USE_PLATFORM_STATE_STORE)
// Verify that the state survived.
ASSERT_TRUE(state_store.HasBeenReported(data.type, data.key, data.digest));
#else
// Verify that the state did not survive.
ASSERT_FALSE(state_store.HasBeenReported(data.type, data.key, data.digest));
#endif
}
}
} // namespace safe_browsing
|