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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
// Copyright 2018 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/strike_database/strike_database.h"
#include <utility>
#include <vector>
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/proto/strike_data.pb.h"
#include "components/leveldb_proto/public/proto_database.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace autofill {
namespace {
// Note: This class is NOT the same as test_strike_database.h. This is an
// actual implementation of StrikeDatabase, but with helper functions
// added for easier test setup. If you want a TestStrikeDatabase, please use the
// one in test_strike_database.h. This one is purely for this unit test class.
class TestStrikeDatabase : public StrikeDatabase {
public:
TestStrikeDatabase(leveldb_proto::ProtoDatabaseProvider* db_provider,
base::FilePath profile_path)
: StrikeDatabase(db_provider, profile_path) {
database_initialized_ = true;
}
TestStrikeDatabase(const TestStrikeDatabase&) = delete;
TestStrikeDatabase& operator=(const TestStrikeDatabase&) = delete;
void AddProtoEntries(
std::vector<std::pair<std::string, StrikeData>> entries_to_add,
const SetValueCallback& callback) {
std::unique_ptr<leveldb_proto::ProtoDatabase<StrikeData>::KeyEntryVector>
entries(new leveldb_proto::ProtoDatabase<StrikeData>::KeyEntryVector());
for (std::pair<std::string, StrikeData> entry : entries_to_add) {
entries->push_back(entry);
}
db_->UpdateEntries(
/*entries_to_save=*/std::move(entries),
/*keys_to_remove=*/std::make_unique<std::vector<std::string>>(),
callback);
}
};
} // namespace
// The anonymous namespace needs to end here because of `friend`ships between
// the tests and the production code.
// Runs tests against the actual StrikeDatabase class, complete with
// ProtoDatabase.
class StrikeDatabaseTest : public ::testing::Test {
public:
StrikeDatabaseTest() = default;
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
db_provider_ = std::make_unique<leveldb_proto::ProtoDatabaseProvider>(
temp_dir_.GetPath());
strike_database_ = std::make_unique<TestStrikeDatabase>(
db_provider_.get(), temp_dir_.GetPath());
}
void AddProtoEntries(
std::vector<std::pair<std::string, StrikeData>> entries_to_add) {
base::RunLoop run_loop;
strike_database_->AddProtoEntries(
entries_to_add,
base::BindRepeating(&StrikeDatabaseTest::OnAddProtoEntries,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
}
void OnAddProtoEntries(base::RepeatingClosure run_loop_closure,
bool success) {
run_loop_closure.Run();
}
void OnGetProtoStrikes(base::RepeatingClosure run_loop_closure,
int num_strikes) {
num_strikes_ = num_strikes;
run_loop_closure.Run();
}
int GetProtoStrikes(std::string key) {
base::RunLoop run_loop;
strike_database_->GetProtoStrikes(
key,
base::BindRepeating(&StrikeDatabaseTest::OnGetProtoStrikes,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
return num_strikes_;
}
void OnClearAllProtoStrikesForKey(base::RepeatingClosure run_loop_closure,
bool success) {
run_loop_closure.Run();
}
void ClearAllProtoStrikesForKey(const std::string key) {
base::RunLoop run_loop;
strike_database_->ClearAllProtoStrikesForKey(
key,
base::BindRepeating(&StrikeDatabaseTest::OnClearAllProtoStrikesForKey,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
}
void OnClearAllProtoStrikesForKeys(base::RepeatingClosure run_loop_closure,
bool success) {
run_loop_closure.Run();
}
void ClearAllProtoStrikesForKeys(const std::vector<std::string>& keys) {
base::RunLoop run_loop;
strike_database_->ClearAllProtoStrikesForKeys(
keys,
base::BindRepeating(&StrikeDatabaseTest::OnClearAllProtoStrikesForKeys,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
}
void OnClearAllProtoStrikes(base::RepeatingClosure run_loop_closure,
bool success) {
run_loop_closure.Run();
}
void ClearAllProtoStrikes() {
base::RunLoop run_loop;
strike_database_->ClearAllProtoStrikes(
base::BindRepeating(&StrikeDatabaseTest::OnClearAllProtoStrikesForKey,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
}
protected:
base::HistogramTester* GetHistogramTester() { return &histogram_tester_; }
base::ScopedTempDir temp_dir_;
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
std::unique_ptr<leveldb_proto::ProtoDatabaseProvider> db_provider_;
std::unique_ptr<TestStrikeDatabase> strike_database_;
private:
base::HistogramTester histogram_tester_;
int num_strikes_;
std::unique_ptr<StrikeData> strike_data_;
};
TEST_F(StrikeDatabaseTest, GetStrikesForMissingKeyTest) {
const std::string key = "12345";
int strikes = GetProtoStrikes(key);
EXPECT_EQ(0, strikes);
}
TEST_F(StrikeDatabaseTest, GetStrikeForNonZeroStrikesTest) {
// Set up database with 3 pre-existing strikes at `key`.
const std::string key = "12345";
std::vector<std::pair<std::string, StrikeData>> entries;
StrikeData data;
data.set_num_strikes(3);
entries.emplace_back(key, data);
AddProtoEntries(entries);
int strikes = GetProtoStrikes(key);
EXPECT_EQ(3, strikes);
}
TEST_F(StrikeDatabaseTest, ClearStrikesForMissingKeyTest) {
const std::string key = "12345";
ClearAllProtoStrikesForKey(key);
int strikes = GetProtoStrikes(key);
EXPECT_EQ(0, strikes);
}
TEST_F(StrikeDatabaseTest, ClearStrikesForNonZeroStrikesTest) {
// Set up database with 3 pre-existing strikes at `key`.
const std::string key = "12345";
std::vector<std::pair<std::string, StrikeData>> entries;
StrikeData data;
data.set_num_strikes(3);
entries.emplace_back(key, data);
AddProtoEntries(entries);
int strikes = GetProtoStrikes(key);
EXPECT_EQ(3, strikes);
ClearAllProtoStrikesForKey(key);
strikes = GetProtoStrikes(key);
EXPECT_EQ(0, strikes);
}
TEST_F(StrikeDatabaseTest, ClearStrikesForMultipleNonZeroStrikesTest) {
// Set up database with 3 pre-existing strikes for three different keys.
const std::string key1 = "12345";
const std::string key2 = "67890";
const std::string key3 = "99000";
std::vector<std::pair<std::string, StrikeData>> entries;
StrikeData data;
data.set_num_strikes(3);
entries.emplace_back(key1, data);
entries.emplace_back(key2, data);
entries.emplace_back(key3, data);
AddProtoEntries(entries);
EXPECT_EQ(3, GetProtoStrikes(key1));
EXPECT_EQ(3, GetProtoStrikes(key2));
EXPECT_EQ(3, GetProtoStrikes(key3));
std::vector<std::string> keys_to_clear({key1, key2});
ClearAllProtoStrikesForKeys(keys_to_clear);
EXPECT_EQ(0, GetProtoStrikes(key1));
EXPECT_EQ(0, GetProtoStrikes(key2));
// The strikes for the third key should not have been reset.
EXPECT_EQ(3, GetProtoStrikes(key3));
}
TEST_F(StrikeDatabaseTest, ClearStrikesForMultipleNonZeroStrikesEntriesTest) {
// Set up database with 3 pre-existing strikes at `key1`, and 5 pre-existing
// strikes at `key2`.
const std::string key1 = "12345";
const std::string key2 = "13579";
std::vector<std::pair<std::string, StrikeData>> entries;
StrikeData data1;
data1.set_num_strikes(3);
entries.emplace_back(key1, data1);
StrikeData data2;
data2.set_num_strikes(5);
entries.emplace_back(key2, data2);
AddProtoEntries(entries);
int strikes = GetProtoStrikes(key1);
EXPECT_EQ(3, strikes);
strikes = GetProtoStrikes(key2);
EXPECT_EQ(5, strikes);
ClearAllProtoStrikesForKey(key1);
strikes = GetProtoStrikes(key1);
EXPECT_EQ(0, strikes);
strikes = GetProtoStrikes(key2);
EXPECT_EQ(5, strikes);
}
TEST_F(StrikeDatabaseTest, ClearAllProtoStrikesTest) {
// Set up database with 3 pre-existing strikes at `key1`, and 5 pre-existing
// strikes at `key2`.
const std::string key1 = "12345";
const std::string key2 = "13579";
std::vector<std::pair<std::string, StrikeData>> entries;
StrikeData data1;
data1.set_num_strikes(3);
entries.push_back(std::make_pair(key1, data1));
StrikeData data2;
data2.set_num_strikes(5);
entries.push_back(std::make_pair(key2, data2));
AddProtoEntries(entries);
EXPECT_EQ(3, GetProtoStrikes(key1));
EXPECT_EQ(5, GetProtoStrikes(key2));
ClearAllProtoStrikes();
EXPECT_EQ(0, GetProtoStrikes(key1));
EXPECT_EQ(0, GetProtoStrikes(key2));
}
TEST_F(StrikeDatabaseTest, GetAllStrikeKeysForProject) {
const std::string key1 = "project_12345";
const std::string key2 = "project_13579";
const std::string key3 = "otherproject_13579";
strike_database_->AddStrikes(1, key1);
strike_database_->AddStrikes(2, key2);
strike_database_->AddStrikes(2, key3);
std::vector<std::string> expected_keys({key1, key2});
EXPECT_EQ(strike_database_->GetAllStrikeKeysForProject("project"),
expected_keys);
expected_keys = {key3};
EXPECT_EQ(strike_database_->GetAllStrikeKeysForProject("otherproject"),
expected_keys);
ClearAllProtoStrikes();
}
TEST_F(StrikeDatabaseTest, ClearStrikesForKeys) {
const std::string key1 = "project_12345";
const std::string key2 = "project_13579";
const std::string key3 = "otherproject_13579";
strike_database_->AddStrikes(1, key1);
strike_database_->AddStrikes(2, key2);
strike_database_->AddStrikes(2, key3);
strike_database_->ClearStrikesForKeys(std::vector<std::string>({key1, key2}));
std::vector<std::string> expected_keys({});
EXPECT_EQ(strike_database_->GetAllStrikeKeysForProject("project"),
expected_keys);
expected_keys.emplace_back(key3);
EXPECT_EQ(strike_database_->GetAllStrikeKeysForProject("otherproject"),
expected_keys);
ClearAllProtoStrikes();
}
// Test to ensure that the timestamp of strike being added is logged and
// retrieved correctly.
TEST_F(StrikeDatabaseTest, LastUpdateTimestamp) {
strike_database_->AddStrikes(1, "fake key");
base::Time strike_added_timestamp =
strike_database_->GetLastUpdatedTimestamp("fake key");
EXPECT_FALSE(strike_added_timestamp.is_null());
task_environment_.AdvanceClock(base::Microseconds(5));
strike_database_->AddStrikes(1, "fake key");
EXPECT_LT(strike_added_timestamp,
strike_database_->GetLastUpdatedTimestamp("fake key"));
ClearAllProtoStrikes();
}
} // namespace autofill
|