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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
|
// Copyright 2020 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/session_proto_db/session_proto_db.h"
#include <array>
#include <map>
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/thread_pool.h"
#include "components/leveldb_proto/testing/fake_db.h"
#include "components/session_proto_db/session_proto_db_test_proto.pb.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using KeyValuePair =
std::pair<std::string, persisted_state_db::PersistedStateContentProto>;
namespace {
persisted_state_db::PersistedStateContentProto BuildProto(
const char* protoKey,
const std::vector<uint8_t> byteArray) {
persisted_state_db::PersistedStateContentProto proto;
proto.set_key(protoKey);
proto.set_content_data(byteArray.data(), byteArray.size());
return proto;
}
session_proto_db::SessionProtoDBTestProto BuildTestProto(const char* key,
const int32_t value) {
session_proto_db::SessionProtoDBTestProto proto;
proto.set_key(key);
proto.set_b(value);
return proto;
}
const char kMockKeyA[] = "A_key";
const char kMockKeyPrefixA[] = "A";
const std::vector<uint8_t> kMockValueArrayA = {0xfa, 0x5b, 0x4c, 0x12};
const persisted_state_db::PersistedStateContentProto kMockValueA =
BuildProto(kMockKeyA, kMockValueArrayA);
const std::vector<
SessionProtoDB<persisted_state_db::PersistedStateContentProto>::KeyAndValue>
kExpectedA = {{kMockKeyA, kMockValueA}};
const char kMockKeyB[] = "B_key";
const std::vector<uint8_t> kMockValueArrayB = {0x3c, 0x9f, 0x5e, 0x69};
const persisted_state_db::PersistedStateContentProto kMockValueB =
BuildProto(kMockKeyB, kMockValueArrayB);
const std::vector<
SessionProtoDB<persisted_state_db::PersistedStateContentProto>::KeyAndValue>
kExpectedB = {{kMockKeyB, kMockValueB}};
const std::vector<
SessionProtoDB<persisted_state_db::PersistedStateContentProto>::KeyAndValue>
kExpectedAB = {{kMockKeyA, kMockValueA}, {kMockKeyB, kMockValueB}};
const std::vector<
SessionProtoDB<persisted_state_db::PersistedStateContentProto>::KeyAndValue>
kEmptyExpected = {};
const session_proto_db::SessionProtoDBTestProto kTestProto =
BuildTestProto(kMockKeyA, 42);
const std::vector<
SessionProtoDB<session_proto_db::SessionProtoDBTestProto>::KeyAndValue>
kTestProtoExpected = {{kMockKeyA, kTestProto}};
const char kSPTDTab1Key[] = "1_SPTD";
const persisted_state_db::PersistedStateContentProto kSPTDTab1Value =
BuildProto(kSPTDTab1Key, {0x5b, 0x2c});
const char kSPTDTab2Key[] = "2_SPTD";
const persisted_state_db::PersistedStateContentProto kSPTDTab2Value =
BuildProto(kSPTDTab2Key, {0xfe, 0xab});
const char kSPTDTab3Key[] = "3_SPTD";
const persisted_state_db::PersistedStateContentProto kSPTDTab3Value =
BuildProto(kSPTDTab3Key, {0x0a, 0x1b});
const char kMPTDTab1Key[] = "1_MPTD";
const persisted_state_db::PersistedStateContentProto kMPTDTab1Value =
BuildProto(kMPTDTab1Key, {0xaa, 0x3b});
const char kMatchingDataIdForMaintenance[] = "SPTD";
const char kNonMatchingDataIdForMaintenance[] = "asdf";
} // namespace
class SessionProtoDBTest : public testing::Test {
public:
SessionProtoDBTest() = default;
SessionProtoDBTest(const SessionProtoDBTest&) = delete;
SessionProtoDBTest& operator=(const SessionProtoDBTest&) = delete;
// The following methods are specific to the database containing
// persisted_state_db::PersistedStateContentProto. There is one test which
// contains session_proto_db::SessionProtoDBTestProto to test the use case
// of multiple SessionProtoDB databases running at the same time.
// Initialize the test database
void InitPersistedStateDB() {
InitPersistedStateDBWithoutCallback();
MockInitCallbackPersistedStateDB(content_db_,
leveldb_proto::Enums::InitStatus::kOK);
}
void InitPersistedStateDBWithoutCallback() {
auto storage_db = std::make_unique<leveldb_proto::test::FakeDB<
persisted_state_db::PersistedStateContentProto>>(&content_db_storage_);
content_db_ = storage_db.get();
persisted_state_db_ = base::WrapUnique(
new SessionProtoDB<persisted_state_db::PersistedStateContentProto>(
std::move(storage_db),
base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE}),
content::GetUIThreadTaskRunner({})));
}
void MockInitCallbackPersistedStateDB(
leveldb_proto::test::FakeDB<
persisted_state_db::PersistedStateContentProto>* storage_db,
leveldb_proto::Enums::InitStatus status) {
storage_db->InitStatusCallback(status);
RunUntilIdle();
}
void MockInsertCallbackPersistedStateDB(
leveldb_proto::test::FakeDB<
persisted_state_db::PersistedStateContentProto>* storage_db,
bool result) {
storage_db->UpdateCallback(result);
RunUntilIdle();
}
void MockLoadCallbackPersistedStateDB(
leveldb_proto::test::FakeDB<
persisted_state_db::PersistedStateContentProto>* storage_db,
bool res) {
storage_db->LoadCallback(res);
RunUntilIdle();
}
void MockDeleteCallbackPersistedStateDB(
leveldb_proto::test::FakeDB<
persisted_state_db::PersistedStateContentProto>* storage_db,
bool res) {
storage_db->UpdateCallback(res);
RunUntilIdle();
}
void GetEvaluationPersistedStateDB(
base::OnceClosure closure,
std::vector<SessionProtoDB<
persisted_state_db::PersistedStateContentProto>::KeyAndValue>
expected,
bool result,
std::vector<SessionProtoDB<
persisted_state_db::PersistedStateContentProto>::KeyAndValue> found) {
for (size_t i = 0; i < expected.size(); i++) {
EXPECT_EQ(found[i].first, expected[i].first);
EXPECT_EQ(found[i].second.content_data(),
expected[i].second.content_data());
}
std::move(closure).Run();
}
void GetEmptyEvaluationPersistedStateDB(
base::OnceClosure closure,
bool result,
std::vector<SessionProtoDB<
persisted_state_db::PersistedStateContentProto>::KeyAndValue> found) {
EXPECT_TRUE(result);
EXPECT_EQ(found.size(), 0U);
std::move(closure).Run();
}
// Common to both databases
void OperationEvaluation(base::OnceClosure closure,
bool expected_success,
bool actual_success) {
EXPECT_EQ(expected_success, actual_success);
std::move(closure).Run();
}
// Wait for all tasks to be cleared off the queue
void RunUntilIdle() { task_environment_.RunUntilIdle(); }
// Specific to session_proto_db::SessionProtoDBTestProto database
void InitTestProtoDB() {
auto storage_db = std::make_unique<
leveldb_proto::test::FakeDB<session_proto_db::SessionProtoDBTestProto>>(
&test_content_db_storage_);
test_content_db_ = storage_db.get();
test_proto_db_ = base::WrapUnique(
new SessionProtoDB<session_proto_db::SessionProtoDBTestProto>(
std::move(storage_db),
base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE}),
content::GetUIThreadTaskRunner({})));
}
void GetTestEvaluationTestProtoDB(
base::OnceClosure closure,
std::vector<SessionProtoDB<
session_proto_db::SessionProtoDBTestProto>::KeyAndValue> expected,
bool result,
std::vector<SessionProtoDB<
session_proto_db::SessionProtoDBTestProto>::KeyAndValue> found) {
for (size_t i = 0; i < expected.size(); i++) {
EXPECT_EQ(found[i].first, expected[i].first);
EXPECT_EQ(found[i].second.b(), expected[i].second.b());
}
std::move(closure).Run();
}
void InsertContentAndVerify(
const std::string& key,
const persisted_state_db::PersistedStateContentProto& value) {
InsertContent(key, value);
VerifyContent(key, value);
}
void InsertContent(
const std::string& key,
const persisted_state_db::PersistedStateContentProto& value) {
base::RunLoop wait_for_insert;
persisted_state_db()->InsertContent(
key, value,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), wait_for_insert.QuitClosure(),
true));
MockInsertCallbackPersistedStateDB(content_db(), true);
wait_for_insert.Run();
}
void VerifyContent(
const std::string& key,
const persisted_state_db::PersistedStateContentProto& value) {
base::RunLoop wait_for_load;
persisted_state_db()->LoadContentWithPrefix(
key,
base::BindOnce(
&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), wait_for_load.QuitClosure(),
std::vector<SessionProtoDB<
persisted_state_db::PersistedStateContentProto>::KeyAndValue>(
{{key, value}})));
MockLoadCallbackPersistedStateDB(content_db(), true);
wait_for_load.Run();
}
void VerifyContentEmpty(const std::string& key) {
base::RunLoop wait_for_load;
persisted_state_db()->LoadContentWithPrefix(
key,
base::BindOnce(&SessionProtoDBTest::GetEmptyEvaluationPersistedStateDB,
base::Unretained(this), wait_for_load.QuitClosure()));
MockLoadCallbackPersistedStateDB(content_db(), true);
wait_for_load.Run();
}
void PerformMaintenance(const std::vector<std::string>& keys_to_keep,
const std::string& data_id) {
base::RunLoop wait_for_maintenance;
persisted_state_db()->PerformMaintenance(
keys_to_keep, data_id,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this),
wait_for_maintenance.QuitClosure(), true));
MockLoadCallbackPersistedStateDB(content_db(), true);
MockInsertCallbackPersistedStateDB(content_db(), true);
wait_for_maintenance.Run();
}
// For persisted_state_db::PersistedStateContentProto database
SessionProtoDB<persisted_state_db::PersistedStateContentProto>*
persisted_state_db() {
return persisted_state_db_.get();
}
leveldb_proto::test::FakeDB<persisted_state_db::PersistedStateContentProto>*
content_db() {
return content_db_;
}
std::vector<base::OnceClosure>& deferred_operations() {
return persisted_state_db()->deferred_operations_;
}
bool InitStatusUnknown() { return persisted_state_db()->InitStatusUnknown(); }
bool FailedToInit() { return persisted_state_db()->FailedToInit(); }
std::map<std::string, persisted_state_db::PersistedStateContentProto>
content_db_storage_;
// For session_proto_db::SessionProtoDBTestProto database
SessionProtoDB<session_proto_db::SessionProtoDBTestProto>* test_proto_db() {
return test_proto_db_.get();
}
leveldb_proto::test::FakeDB<session_proto_db::SessionProtoDBTestProto>*
test_content_db() {
return test_content_db_;
}
std::map<std::string, session_proto_db::SessionProtoDBTestProto>
test_content_db_storage_;
protected:
raw_ptr<
leveldb_proto::test::FakeDB<session_proto_db::SessionProtoDBTestProto>,
DanglingUntriaged>
test_content_db_;
private:
content::BrowserTaskEnvironment task_environment_;
// For persisted_state_db::PersistedStateContentProto database
raw_ptr<leveldb_proto::test::FakeDB<
persisted_state_db::PersistedStateContentProto>,
DanglingUntriaged>
content_db_;
std::unique_ptr<
SessionProtoDB<persisted_state_db::PersistedStateContentProto>>
persisted_state_db_;
// For session_proto_db::SessionProtoDBTestProto database
std::unique_ptr<SessionProtoDB<session_proto_db::SessionProtoDBTestProto>>
test_proto_db_;
};
// Test an arbitrary proto - this ensures we can have two ProfilProtoDB
// databases running simultaneously.
TEST_F(SessionProtoDBTest, TestArbitraryProto) {
InitTestProtoDB();
test_content_db_->InitStatusCallback(leveldb_proto::Enums::InitStatus::kOK);
RunUntilIdle();
std::array<base::RunLoop, 2> run_loop;
test_proto_db()->InsertContent(
kMockKeyA, kTestProto,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), true));
test_content_db()->UpdateCallback(true);
RunUntilIdle();
run_loop[0].Run();
test_proto_db()->LoadContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::GetTestEvaluationTestProtoDB,
base::Unretained(this), run_loop[1].QuitClosure(),
kTestProtoExpected));
test_content_db()->LoadCallback(true);
RunUntilIdle();
run_loop[1].Run();
}
TEST_F(SessionProtoDBTest, TestInit) {
InitPersistedStateDB();
EXPECT_EQ(false, FailedToInit());
}
TEST_F(SessionProtoDBTest, TestKeyInsertionSucceeded) {
InitPersistedStateDB();
std::array<base::RunLoop, 2> run_loop;
persisted_state_db()->InsertContent(
kMockKeyA, kMockValueA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), true));
MockInsertCallbackPersistedStateDB(content_db(), true);
run_loop[0].Run();
persisted_state_db()->LoadContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[1].QuitClosure(),
kExpectedA));
MockLoadCallbackPersistedStateDB(content_db(), true);
run_loop[1].Run();
}
TEST_F(SessionProtoDBTest, TestKeyInsertionFailed) {
InitPersistedStateDB();
std::array<base::RunLoop, 2> run_loop;
persisted_state_db()->InsertContent(
kMockKeyA, kMockValueA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), false));
MockInsertCallbackPersistedStateDB(content_db(), false);
run_loop[0].Run();
persisted_state_db()->LoadContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[1].QuitClosure(),
kEmptyExpected));
MockLoadCallbackPersistedStateDB(content_db(), true);
run_loop[1].Run();
}
TEST_F(SessionProtoDBTest, TestKeyInsertionPrefix) {
InitPersistedStateDB();
std::array<base::RunLoop, 2> run_loop;
persisted_state_db()->InsertContent(
kMockKeyA, kMockValueA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), true));
MockInsertCallbackPersistedStateDB(content_db(), true);
run_loop[0].Run();
persisted_state_db()->LoadContentWithPrefix(
kMockKeyPrefixA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[1].QuitClosure(),
kExpectedA));
MockLoadCallbackPersistedStateDB(content_db(), true);
run_loop[1].Run();
}
TEST_F(SessionProtoDBTest, TestLoadOneEntry) {
InitPersistedStateDB();
std::array<base::RunLoop, 4> run_loop;
persisted_state_db()->InsertContent(
kMockKeyA, kMockValueA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), true));
MockInsertCallbackPersistedStateDB(content_db(), true);
run_loop[0].Run();
persisted_state_db()->InsertContent(
kMockKeyB, kMockValueB,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[1].QuitClosure(), true));
MockInsertCallbackPersistedStateDB(content_db(), true);
run_loop[1].Run();
persisted_state_db()->LoadOneEntry(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[2].QuitClosure(),
kExpectedA));
content_db()->GetCallback(true);
run_loop[2].Run();
persisted_state_db()->LoadOneEntry(
kMockKeyB,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[3].QuitClosure(),
kExpectedB));
content_db()->GetCallback(true);
run_loop[3].Run();
}
TEST_F(SessionProtoDBTest, TestLoadAllEntries) {
InitPersistedStateDB();
std::array<base::RunLoop, 3> run_loop;
persisted_state_db()->InsertContent(
kMockKeyA, kMockValueA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), true));
MockInsertCallbackPersistedStateDB(content_db(), true);
run_loop[0].Run();
persisted_state_db()->InsertContent(
kMockKeyB, kMockValueB,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[1].QuitClosure(), true));
MockInsertCallbackPersistedStateDB(content_db(), true);
run_loop[1].Run();
persisted_state_db()->LoadAllEntries(base::BindOnce(
&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[2].QuitClosure(), kExpectedAB));
MockLoadCallbackPersistedStateDB(content_db(), true);
run_loop[2].Run();
}
TEST_F(SessionProtoDBTest, TestDeleteWithPrefix) {
InitPersistedStateDB();
std::array<base::RunLoop, 4> run_loop;
persisted_state_db()->InsertContent(
kMockKeyA, kMockValueA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), true));
MockInsertCallbackPersistedStateDB(content_db(), true);
run_loop[0].Run();
persisted_state_db()->LoadContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[1].QuitClosure(),
kExpectedA));
MockLoadCallbackPersistedStateDB(content_db(), true);
run_loop[1].Run();
persisted_state_db()->DeleteContentWithPrefix(
kMockKeyPrefixA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[2].QuitClosure(), true));
MockDeleteCallbackPersistedStateDB(content_db(), true);
run_loop[2].Run();
persisted_state_db()->LoadContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[3].QuitClosure(),
kEmptyExpected));
MockLoadCallbackPersistedStateDB(content_db(), true);
run_loop[3].Run();
}
TEST_F(SessionProtoDBTest, TestDeleteOneEntry) {
InitPersistedStateDB();
std::array<base::RunLoop, 6> run_loop;
persisted_state_db()->InsertContent(
kMockKeyA, kMockValueA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), true));
MockInsertCallbackPersistedStateDB(content_db(), true);
run_loop[0].Run();
persisted_state_db()->LoadContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[1].QuitClosure(),
kExpectedA));
MockLoadCallbackPersistedStateDB(content_db(), true);
run_loop[1].Run();
persisted_state_db()->DeleteOneEntry(
kMockKeyPrefixA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[2].QuitClosure(), false));
MockDeleteCallbackPersistedStateDB(content_db(), false);
run_loop[2].Run();
persisted_state_db()->LoadContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[3].QuitClosure(),
kExpectedA));
MockLoadCallbackPersistedStateDB(content_db(), true);
run_loop[3].Run();
persisted_state_db()->DeleteOneEntry(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[4].QuitClosure(), true));
MockDeleteCallbackPersistedStateDB(content_db(), true);
run_loop[4].Run();
persisted_state_db()->LoadContentWithPrefix(
kMockKeyPrefixA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[5].QuitClosure(),
kEmptyExpected));
MockLoadCallbackPersistedStateDB(content_db(), true);
run_loop[5].Run();
}
TEST_F(SessionProtoDBTest, TestDeferredOperations) {
InitPersistedStateDBWithoutCallback();
RunUntilIdle();
EXPECT_EQ(true, InitStatusUnknown());
std::array<base::RunLoop, 4> run_loop;
persisted_state_db()->InsertContent(
kMockKeyA, kMockValueA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), true));
persisted_state_db()->LoadContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[1].QuitClosure(),
kExpectedA));
EXPECT_EQ(2u, deferred_operations().size());
content_db()->InitStatusCallback(leveldb_proto::Enums::InitStatus::kOK);
EXPECT_EQ(false, FailedToInit());
MockInsertCallbackPersistedStateDB(content_db(), true);
MockLoadCallbackPersistedStateDB(content_db(), true);
run_loop[0].Run();
run_loop[1].Run();
EXPECT_EQ(0u, deferred_operations().size());
persisted_state_db()->DeleteContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[2].QuitClosure(), true));
EXPECT_EQ(0u, deferred_operations().size());
MockDeleteCallbackPersistedStateDB(content_db(), true);
persisted_state_db()->LoadContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[3].QuitClosure(),
kEmptyExpected));
EXPECT_EQ(0u, deferred_operations().size());
MockLoadCallbackPersistedStateDB(content_db(), true);
run_loop[3].Run();
}
TEST_F(SessionProtoDBTest, TestInitializationFailure) {
InitPersistedStateDBWithoutCallback();
RunUntilIdle();
EXPECT_EQ(true, InitStatusUnknown());
std::array<base::RunLoop, 6> run_loop;
// Do some operations before database status is known
persisted_state_db()->InsertContent(
kMockKeyA, kMockValueA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), false));
persisted_state_db()->LoadContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[1].QuitClosure(),
kEmptyExpected));
persisted_state_db()->DeleteContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[2].QuitClosure(), false));
EXPECT_EQ(3u, deferred_operations().size());
// Error initializing database
content_db()->InitStatusCallback(leveldb_proto::Enums::InitStatus::kError);
EXPECT_EQ(true, FailedToInit());
for (int i = 0; i < 3; i++) {
run_loop[i].Run();
}
// Check deferred_operations is flushed
EXPECT_EQ(0u, deferred_operations().size());
// More operations should just return false/null as the database
// failed to initialize
persisted_state_db()->InsertContent(
kMockKeyA, kMockValueA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[3].QuitClosure(), false));
persisted_state_db()->LoadContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[4].QuitClosure(),
kEmptyExpected));
persisted_state_db()->DeleteContentWithPrefix(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[5].QuitClosure(), false));
// Operations should have returned immediately as database was initialization
// resulted in an error
EXPECT_EQ(0u, deferred_operations().size());
for (int i = 3; i < 6; i++) {
run_loop[i].Run();
}
}
TEST_F(SessionProtoDBTest, TestUpdateEntries) {
InitPersistedStateDB();
std::array<base::RunLoop, 6> run_loop;
persisted_state_db()->InsertContent(
kMockKeyA, kMockValueA,
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), true));
MockInsertCallbackPersistedStateDB(content_db(), true);
run_loop[0].Run();
// Do one update and one insert for the UpdateEntries call.
auto entries_to_update = std::make_unique<std::vector<KeyValuePair>>();
entries_to_update->emplace_back(kMockKeyA, kMockValueB);
entries_to_update->emplace_back(kMockKeyB, kMockValueA);
persisted_state_db()->UpdateEntries(
std::move(entries_to_update),
std::make_unique<std::vector<std::string>>(),
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[1].QuitClosure(), true));
MockInsertCallbackPersistedStateDB(content_db(), true);
run_loop[1].Run();
persisted_state_db()->LoadOneEntry(
kMockKeyA,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[2].QuitClosure(),
kExpectedB));
content_db()->GetCallback(true);
run_loop[2].Run();
persisted_state_db()->LoadOneEntry(
kMockKeyB,
base::BindOnce(&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[3].QuitClosure(),
kExpectedA));
content_db()->GetCallback(true);
run_loop[3].Run();
// Reverts the update and insertion earlier.
entries_to_update = std::make_unique<std::vector<KeyValuePair>>();
auto keys_to_remove = std::make_unique<std::vector<std::string>>();
entries_to_update->emplace_back(kMockKeyA, kMockValueA);
keys_to_remove->emplace_back(kMockKeyB);
persisted_state_db()->UpdateEntries(
std::move(entries_to_update), std::move(keys_to_remove),
base::BindOnce(&SessionProtoDBTest::OperationEvaluation,
base::Unretained(this), run_loop[4].QuitClosure(), true));
MockInsertCallbackPersistedStateDB(content_db(), true);
run_loop[4].Run();
persisted_state_db()->LoadAllEntries(base::BindOnce(
&SessionProtoDBTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[5].QuitClosure(), kExpectedA));
MockLoadCallbackPersistedStateDB(content_db(), true);
}
TEST_F(SessionProtoDBTest, TestMaintenanceKeepSomeKeys) {
InitPersistedStateDB();
InsertContentAndVerify(kSPTDTab1Key, kSPTDTab1Value);
InsertContentAndVerify(kSPTDTab2Key, kSPTDTab2Value);
InsertContentAndVerify(kSPTDTab3Key, kSPTDTab3Value);
InsertContentAndVerify(kMPTDTab1Key, kMPTDTab1Value);
PerformMaintenance(std::vector<std::string>{kSPTDTab1Key, kSPTDTab3Key},
kMatchingDataIdForMaintenance);
VerifyContent(kSPTDTab1Key, kSPTDTab1Value);
VerifyContent(kSPTDTab3Key, kSPTDTab3Value);
VerifyContent(kMPTDTab1Key, kMPTDTab1Value);
VerifyContentEmpty(kSPTDTab2Key);
}
TEST_F(SessionProtoDBTest, TestMaintenanceKeepNoKeys) {
InitPersistedStateDB();
InsertContentAndVerify(kSPTDTab1Key, kSPTDTab1Value);
InsertContentAndVerify(kSPTDTab2Key, kSPTDTab2Value);
InsertContentAndVerify(kSPTDTab3Key, kSPTDTab3Value);
InsertContentAndVerify(kMPTDTab1Key, kMPTDTab1Value);
PerformMaintenance(std::vector<std::string>{}, kMatchingDataIdForMaintenance);
VerifyContent(kMPTDTab1Key, kMPTDTab1Value);
VerifyContentEmpty(kSPTDTab1Key);
VerifyContentEmpty(kSPTDTab2Key);
VerifyContentEmpty(kSPTDTab3Key);
}
TEST_F(SessionProtoDBTest, TestMaintenanceNonMatchingDataId) {
InitPersistedStateDB();
InsertContentAndVerify(kSPTDTab1Key, kSPTDTab1Value);
InsertContentAndVerify(kSPTDTab2Key, kSPTDTab2Value);
InsertContentAndVerify(kSPTDTab3Key, kSPTDTab3Value);
InsertContentAndVerify(kMPTDTab1Key, kMPTDTab1Value);
PerformMaintenance(std::vector<std::string>{kSPTDTab1Key, kSPTDTab3Key},
kNonMatchingDataIdForMaintenance);
VerifyContent(kSPTDTab1Key, kSPTDTab1Value);
VerifyContent(kSPTDTab2Key, kSPTDTab2Value);
VerifyContent(kSPTDTab3Key, kSPTDTab3Value);
VerifyContent(kMPTDTab1Key, kMPTDTab1Value);
}
|