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 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync/engine_impl/directory_update_handler.h"
#include <stdint.h>
#include <set>
#include <string>
#include <utility>
#include "base/compiler_specific.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "components/sync/base/attachment_id_proto.h"
#include "components/sync/engine_impl/cycle/directory_type_debug_info_emitter.h"
#include "components/sync/engine_impl/cycle/status_controller.h"
#include "components/sync/engine_impl/syncer_proto_util.h"
#include "components/sync/engine_impl/test_entry_factory.h"
#include "components/sync/protocol/sync.pb.h"
#include "components/sync/syncable/directory.h"
#include "components/sync/syncable/entry.h"
#include "components/sync/syncable/mutable_entry.h"
#include "components/sync/syncable/syncable_model_neutral_write_transaction.h"
#include "components/sync/syncable/syncable_proto_util.h"
#include "components/sync/syncable/syncable_read_transaction.h"
#include "components/sync/syncable/syncable_write_transaction.h"
#include "components/sync/test/engine/fake_model_worker.h"
#include "components/sync/test/engine/test_directory_setter_upper.h"
#include "components/sync/test/engine/test_id_factory.h"
#include "components/sync/test/engine/test_syncable_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
using syncable::Id;
using syncable::UNITTEST;
static const int64_t kDefaultVersion = 1000;
// A test harness for tests that focus on processing updates.
//
// Update processing is what occurs when we first download updates. It converts
// the received protobuf message into information in the syncable::Directory.
// Any invalid or redundant updates will be dropped at this point.
class DirectoryUpdateHandlerProcessUpdateTest : public ::testing::Test {
public:
DirectoryUpdateHandlerProcessUpdateTest()
: ui_worker_(new FakeModelWorker(GROUP_UI)) {}
~DirectoryUpdateHandlerProcessUpdateTest() override {}
void SetUp() override { dir_maker_.SetUp(); }
void TearDown() override { dir_maker_.TearDown(); }
syncable::Directory* dir() { return dir_maker_.directory(); }
protected:
std::unique_ptr<sync_pb::SyncEntity> CreateUpdate(const std::string& id,
const std::string& parent,
const ModelType& type);
// This exists mostly to give tests access to the protected member function.
// Warning: This takes the syncable directory lock.
void UpdateSyncEntities(DirectoryUpdateHandler* handler,
const SyncEntityList& applicable_updates,
StatusController* status);
// Another function to access private member functions.
void UpdateProgressMarkers(DirectoryUpdateHandler* handler,
const sync_pb::DataTypeProgressMarker& progress);
scoped_refptr<FakeModelWorker> ui_worker() { return ui_worker_; }
bool EntryExists(const std::string& id) {
syncable::ReadTransaction trans(FROM_HERE, dir());
syncable::Entry e(&trans, syncable::GET_BY_ID, Id::CreateFromServerId(id));
return e.good() && !e.GetIsDel();
}
bool TypeRootExists(ModelType model_type) {
return dir()->InitialSyncEndedForType(model_type);
}
protected:
// Used in the construction of DirectoryTypeDebugInfoEmitters.
base::ObserverList<TypeDebugInfoObserver> type_observers_;
private:
base::MessageLoop loop_; // Needed to initialize the directory.
TestDirectorySetterUpper dir_maker_;
scoped_refptr<FakeModelWorker> ui_worker_;
};
std::unique_ptr<sync_pb::SyncEntity>
DirectoryUpdateHandlerProcessUpdateTest::CreateUpdate(const std::string& id,
const std::string& parent,
const ModelType& type) {
std::unique_ptr<sync_pb::SyncEntity> e(new sync_pb::SyncEntity());
e->set_id_string(id);
e->set_parent_id_string(parent);
e->set_non_unique_name(id);
e->set_name(id);
e->set_version(kDefaultVersion);
AddDefaultFieldValue(type, e->mutable_specifics());
return e;
}
void DirectoryUpdateHandlerProcessUpdateTest::UpdateSyncEntities(
DirectoryUpdateHandler* handler,
const SyncEntityList& applicable_updates,
StatusController* status) {
syncable::ModelNeutralWriteTransaction trans(FROM_HERE, UNITTEST, dir());
handler->UpdateSyncEntities(&trans, applicable_updates, status);
}
void DirectoryUpdateHandlerProcessUpdateTest::UpdateProgressMarkers(
DirectoryUpdateHandler* handler,
const sync_pb::DataTypeProgressMarker& progress) {
handler->UpdateProgressMarker(progress);
}
static const char kCacheGuid[] = "IrcjZ2jyzHDV9Io4+zKcXQ==";
// Test that the bookmark tag is set on newly downloaded items.
TEST_F(DirectoryUpdateHandlerProcessUpdateTest, NewBookmarkTag) {
DirectoryTypeDebugInfoEmitter emitter(BOOKMARKS, &type_observers_);
DirectoryUpdateHandler handler(dir(), BOOKMARKS, ui_worker(), &emitter);
sync_pb::GetUpdatesResponse gu_response;
StatusController status;
// Add a bookmark item to the update message.
std::string root = Id::GetRoot().GetServerId();
Id server_id = Id::CreateFromServerId("b1");
std::unique_ptr<sync_pb::SyncEntity> e =
CreateUpdate(SyncableIdToProto(server_id), root, BOOKMARKS);
e->set_originator_cache_guid(
std::string(kCacheGuid, arraysize(kCacheGuid) - 1));
Id client_id = Id::CreateFromClientString("-2");
e->set_originator_client_item_id(client_id.GetServerId());
e->set_position_in_parent(0);
// Add it to the applicable updates list.
SyncEntityList bookmark_updates;
bookmark_updates.push_back(e.get());
// Process the update.
UpdateSyncEntities(&handler, bookmark_updates, &status);
syncable::ReadTransaction trans(FROM_HERE, dir());
syncable::Entry entry(&trans, syncable::GET_BY_ID, server_id);
ASSERT_TRUE(entry.good());
EXPECT_TRUE(UniquePosition::IsValidSuffix(entry.GetUniqueBookmarkTag()));
EXPECT_TRUE(entry.GetServerUniquePosition().IsValid());
// If this assertion fails, that might indicate that the algorithm used to
// generate bookmark tags has been modified. This could have implications for
// bookmark ordering. Please make sure you know what you're doing if you
// intend to make such a change.
EXPECT_EQ("6wHRAb3kbnXV5GHrejp4/c1y5tw=", entry.GetUniqueBookmarkTag());
}
// Test the receipt of a type root node.
TEST_F(DirectoryUpdateHandlerProcessUpdateTest,
ReceiveServerCreatedBookmarkFolders) {
DirectoryTypeDebugInfoEmitter emitter(BOOKMARKS, &type_observers_);
DirectoryUpdateHandler handler(dir(), BOOKMARKS, ui_worker(), &emitter);
sync_pb::GetUpdatesResponse gu_response;
StatusController status;
// Create an update that mimics the bookmark root.
Id server_id = Id::CreateFromServerId("xyz");
std::string root = Id::GetRoot().GetServerId();
std::unique_ptr<sync_pb::SyncEntity> e =
CreateUpdate(SyncableIdToProto(server_id), root, BOOKMARKS);
e->set_server_defined_unique_tag("google_chrome_bookmarks");
e->set_folder(true);
// Add it to the applicable updates list.
SyncEntityList bookmark_updates;
bookmark_updates.push_back(e.get());
EXPECT_FALSE(SyncerProtoUtil::ShouldMaintainPosition(*e));
// Process it.
UpdateSyncEntities(&handler, bookmark_updates, &status);
// Verify the results.
syncable::ReadTransaction trans(FROM_HERE, dir());
syncable::Entry entry(&trans, syncable::GET_BY_ID, server_id);
ASSERT_TRUE(entry.good());
EXPECT_FALSE(entry.ShouldMaintainPosition());
EXPECT_FALSE(entry.GetUniquePosition().IsValid());
EXPECT_FALSE(entry.GetServerUniquePosition().IsValid());
EXPECT_TRUE(entry.GetUniqueBookmarkTag().empty());
}
// Test the receipt of a non-bookmark item.
TEST_F(DirectoryUpdateHandlerProcessUpdateTest, ReceiveNonBookmarkItem) {
DirectoryTypeDebugInfoEmitter emitter(AUTOFILL, &type_observers_);
DirectoryUpdateHandler handler(dir(), AUTOFILL, ui_worker(), &emitter);
sync_pb::GetUpdatesResponse gu_response;
StatusController status;
std::string root = Id::GetRoot().GetServerId();
Id server_id = Id::CreateFromServerId("xyz");
std::unique_ptr<sync_pb::SyncEntity> e =
CreateUpdate(SyncableIdToProto(server_id), root, AUTOFILL);
e->set_server_defined_unique_tag("9PGRuKdX5sHyGMB17CvYTXuC43I=");
// Add it to the applicable updates list.
SyncEntityList autofill_updates;
autofill_updates.push_back(e.get());
EXPECT_FALSE(SyncerProtoUtil::ShouldMaintainPosition(*e));
// Process it.
UpdateSyncEntities(&handler, autofill_updates, &status);
syncable::ReadTransaction trans(FROM_HERE, dir());
syncable::Entry entry(&trans, syncable::GET_BY_ID, server_id);
ASSERT_TRUE(entry.good());
EXPECT_FALSE(entry.ShouldMaintainPosition());
EXPECT_FALSE(entry.GetUniquePosition().IsValid());
EXPECT_FALSE(entry.GetServerUniquePosition().IsValid());
EXPECT_TRUE(entry.GetUniqueBookmarkTag().empty());
}
// Tests the setting of progress markers.
TEST_F(DirectoryUpdateHandlerProcessUpdateTest, ProcessNewProgressMarkers) {
DirectoryTypeDebugInfoEmitter emitter(BOOKMARKS, &type_observers_);
DirectoryUpdateHandler handler(dir(), BOOKMARKS, ui_worker(), &emitter);
sync_pb::DataTypeProgressMarker progress;
progress.set_data_type_id(GetSpecificsFieldNumberFromModelType(BOOKMARKS));
progress.set_token("token");
UpdateProgressMarkers(&handler, progress);
sync_pb::DataTypeProgressMarker saved;
dir()->GetDownloadProgress(BOOKMARKS, &saved);
EXPECT_EQ(progress.token(), saved.token());
EXPECT_EQ(progress.data_type_id(), saved.data_type_id());
}
TEST_F(DirectoryUpdateHandlerProcessUpdateTest, GarbageCollectionByVersion) {
DirectoryTypeDebugInfoEmitter emitter(SYNCED_NOTIFICATIONS, &type_observers_);
DirectoryUpdateHandler handler(dir(), SYNCED_NOTIFICATIONS, ui_worker(),
&emitter);
StatusController status;
sync_pb::DataTypeProgressMarker progress;
progress.set_data_type_id(
GetSpecificsFieldNumberFromModelType(SYNCED_NOTIFICATIONS));
progress.set_token("token");
progress.mutable_gc_directive()->set_version_watermark(kDefaultVersion + 10);
sync_pb::DataTypeContext context;
context.set_data_type_id(
GetSpecificsFieldNumberFromModelType(SYNCED_NOTIFICATIONS));
context.set_context("context");
context.set_version(1);
std::unique_ptr<sync_pb::SyncEntity> e1 =
CreateUpdate(SyncableIdToProto(Id::CreateFromServerId("e1")), "",
SYNCED_NOTIFICATIONS);
std::unique_ptr<sync_pb::SyncEntity> e2 =
CreateUpdate(SyncableIdToProto(Id::CreateFromServerId("e2")), "",
SYNCED_NOTIFICATIONS);
e2->set_version(kDefaultVersion + 100);
// Add to the applicable updates list.
SyncEntityList updates;
updates.push_back(e1.get());
updates.push_back(e2.get());
// Process and apply updates.
EXPECT_EQ(SYNCER_OK, handler.ProcessGetUpdatesResponse(progress, context,
updates, &status));
handler.ApplyUpdates(&status);
// Verify none is deleted because they are unapplied during GC.
EXPECT_TRUE(TypeRootExists(SYNCED_NOTIFICATIONS));
EXPECT_TRUE(EntryExists(e1->id_string()));
EXPECT_TRUE(EntryExists(e2->id_string()));
// Process and apply again. Old entry is deleted but not root.
progress.mutable_gc_directive()->set_version_watermark(kDefaultVersion + 20);
EXPECT_EQ(SYNCER_OK, handler.ProcessGetUpdatesResponse(
progress, context, SyncEntityList(), &status));
handler.ApplyUpdates(&status);
EXPECT_FALSE(EntryExists(e1->id_string()));
EXPECT_TRUE(EntryExists(e2->id_string()));
}
TEST_F(DirectoryUpdateHandlerProcessUpdateTest, ContextVersion) {
DirectoryTypeDebugInfoEmitter emitter(SYNCED_NOTIFICATIONS, &type_observers_);
DirectoryUpdateHandler handler(dir(), SYNCED_NOTIFICATIONS, ui_worker(),
&emitter);
StatusController status;
int field_number = GetSpecificsFieldNumberFromModelType(SYNCED_NOTIFICATIONS);
sync_pb::DataTypeProgressMarker progress;
progress.set_data_type_id(
GetSpecificsFieldNumberFromModelType(SYNCED_NOTIFICATIONS));
progress.set_token("token");
sync_pb::DataTypeContext old_context;
old_context.set_version(1);
old_context.set_context("data");
old_context.set_data_type_id(field_number);
std::unique_ptr<sync_pb::SyncEntity> e1 =
CreateUpdate(SyncableIdToProto(Id::CreateFromServerId("e1")), "",
SYNCED_NOTIFICATIONS);
SyncEntityList updates;
updates.push_back(e1.get());
// The first response should be processed fine.
EXPECT_EQ(SYNCER_OK, handler.ProcessGetUpdatesResponse(progress, old_context,
updates, &status));
handler.ApplyUpdates(&status);
// The PREFERENCES root should be auto-created.
EXPECT_TRUE(TypeRootExists(SYNCED_NOTIFICATIONS));
EXPECT_TRUE(EntryExists(e1->id_string()));
{
sync_pb::DataTypeContext dir_context;
syncable::ReadTransaction trans(FROM_HERE, dir());
trans.directory()->GetDataTypeContext(&trans, SYNCED_NOTIFICATIONS,
&dir_context);
EXPECT_EQ(old_context.SerializeAsString(), dir_context.SerializeAsString());
}
sync_pb::DataTypeContext new_context;
new_context.set_version(0);
new_context.set_context("old");
new_context.set_data_type_id(field_number);
std::unique_ptr<sync_pb::SyncEntity> e2 =
CreateUpdate(SyncableIdToProto(Id::CreateFromServerId("e2")), "",
SYNCED_NOTIFICATIONS);
updates.clear();
updates.push_back(e2.get());
// The second response, with an old context version, should result in an
// error and the updates should be dropped.
EXPECT_EQ(DATATYPE_TRIGGERED_RETRY,
handler.ProcessGetUpdatesResponse(progress, new_context, updates,
&status));
handler.ApplyUpdates(&status);
EXPECT_FALSE(EntryExists(e2->id_string()));
{
sync_pb::DataTypeContext dir_context;
syncable::ReadTransaction trans(FROM_HERE, dir());
trans.directory()->GetDataTypeContext(&trans, SYNCED_NOTIFICATIONS,
&dir_context);
EXPECT_EQ(old_context.SerializeAsString(), dir_context.SerializeAsString());
}
}
// See that updates containing attachment metadata are applied
// (i.e. server_attachment_metadata is copied to attachment_metadata).
TEST_F(DirectoryUpdateHandlerProcessUpdateTest,
ProcessAndApplyUpdatesWithAttachments) {
DirectoryTypeDebugInfoEmitter emitter(ARTICLES, &type_observers_);
DirectoryUpdateHandler handler(dir(), ARTICLES, ui_worker(), &emitter);
StatusController status;
sync_pb::DataTypeProgressMarker progress;
progress.set_data_type_id(GetSpecificsFieldNumberFromModelType(ARTICLES));
progress.set_token("token");
progress.mutable_gc_directive()->set_version_watermark(kDefaultVersion + 10);
sync_pb::DataTypeContext context;
context.set_data_type_id(GetSpecificsFieldNumberFromModelType(ARTICLES));
context.set_context("context");
context.set_version(1);
std::unique_ptr<sync_pb::SyncEntity> e1 = CreateUpdate(
SyncableIdToProto(Id::CreateFromServerId("e1")), "", ARTICLES);
sync_pb::AttachmentIdProto* attachment_id = e1->add_attachment_id();
*attachment_id = CreateAttachmentIdProto(0, 0);
SyncEntityList updates;
updates.push_back(e1.get());
// Process and apply updates.
EXPECT_EQ(SYNCER_OK, handler.ProcessGetUpdatesResponse(progress, context,
updates, &status));
handler.ApplyUpdates(&status);
ASSERT_TRUE(TypeRootExists(ARTICLES));
ASSERT_TRUE(EntryExists(e1->id_string()));
{
syncable::ReadTransaction trans(FROM_HERE, dir());
syncable::Entry e(&trans, syncable::GET_BY_ID,
Id::CreateFromServerId(e1->id_string()));
// See that the attachment_metadata is correct.
sync_pb::AttachmentMetadata attachment_metadata = e.GetAttachmentMetadata();
ASSERT_EQ(1, attachment_metadata.record_size());
ASSERT_EQ(attachment_id->SerializeAsString(),
attachment_metadata.record(0).id().SerializeAsString());
ASSERT_TRUE(attachment_metadata.record(0).is_on_server());
// See that attachment_metadata and server_attachment_metadata are equal.
ASSERT_EQ(attachment_metadata.SerializeAsString(),
e.GetServerAttachmentMetadata().SerializeAsString());
}
}
// Tests that IsInitialSyncEnded value is updated by ApplyUpdates, but not by
// ProcessGetUpdatesResponse.
TEST_F(DirectoryUpdateHandlerProcessUpdateTest, IsInitialSyncEnded) {
DirectoryTypeDebugInfoEmitter emitter(AUTOFILL, &type_observers_);
DirectoryUpdateHandler handler(dir(), AUTOFILL, ui_worker(), &emitter);
StatusController status;
EXPECT_FALSE(handler.IsInitialSyncEnded());
sync_pb::DataTypeProgressMarker progress;
progress.set_data_type_id(GetSpecificsFieldNumberFromModelType(AUTOFILL));
progress.set_token("token");
progress.mutable_gc_directive()->set_version_watermark(kDefaultVersion + 10);
std::unique_ptr<sync_pb::SyncEntity> e = CreateUpdate(
SyncableIdToProto(Id::CreateFromServerId("e1")), "", AUTOFILL);
SyncEntityList updates;
updates.push_back(e.get());
handler.ProcessGetUpdatesResponse(progress, sync_pb::DataTypeContext(),
updates, &status);
EXPECT_FALSE(handler.IsInitialSyncEnded());
handler.ApplyUpdates(&status);
EXPECT_TRUE(handler.IsInitialSyncEnded());
}
// A test harness for tests that focus on applying updates.
//
// Update application is performed when we want to take updates that were
// previously downloaded, processed, and stored in our syncable::Directory
// and use them to update our local state (both the Directory's local state
// and the model's local state, though these tests focus only on the Directory's
// local state).
//
// This is kept separate from the update processing test in part for historical
// reasons, and in part because these tests may require a bit more infrastrcture
// in the future. Update application should happen on a different thread a lot
// of the time so these tests may end up requiring more infrastructure than the
// update processing tests. Currently, we're bypassing most of those issues by
// using FakeModelWorkers, so there's not much difference between the two test
// harnesses.
class DirectoryUpdateHandlerApplyUpdateTest : public ::testing::Test {
public:
DirectoryUpdateHandlerApplyUpdateTest()
: ui_worker_(new FakeModelWorker(GROUP_UI)),
password_worker_(new FakeModelWorker(GROUP_PASSWORD)),
passive_worker_(new FakeModelWorker(GROUP_PASSIVE)),
bookmarks_emitter_(BOOKMARKS, &type_observers_),
passwords_emitter_(PASSWORDS, &type_observers_),
articles_emitter_(ARTICLES, &type_observers_) {}
void SetUp() override {
dir_maker_.SetUp();
entry_factory_ = base::MakeUnique<TestEntryFactory>(directory());
update_handler_map_.insert(std::make_pair(
BOOKMARKS,
base::MakeUnique<DirectoryUpdateHandler>(
directory(), BOOKMARKS, ui_worker_, &bookmarks_emitter_)));
update_handler_map_.insert(std::make_pair(
PASSWORDS,
base::MakeUnique<DirectoryUpdateHandler>(
directory(), PASSWORDS, password_worker_, &passwords_emitter_)));
update_handler_map_.insert(std::make_pair(
ARTICLES, base::MakeUnique<DirectoryUpdateHandler>(
directory(), ARTICLES, ui_worker_, &articles_emitter_)));
}
void TearDown() override { dir_maker_.TearDown(); }
const UpdateCounters& GetBookmarksUpdateCounters() {
return bookmarks_emitter_.GetUpdateCounters();
}
const UpdateCounters& GetPasswordsUpdateCounters() {
return passwords_emitter_.GetUpdateCounters();
}
const UpdateCounters& GetArticlesUpdateCounters() {
return articles_emitter_.GetUpdateCounters();
}
protected:
void ApplyBookmarkUpdates(StatusController* status) {
update_handler_map_.find(BOOKMARKS)->second->ApplyUpdates(status);
}
void ApplyPasswordUpdates(StatusController* status) {
update_handler_map_.find(PASSWORDS)->second->ApplyUpdates(status);
}
void ApplyArticlesUpdates(StatusController* status) {
update_handler_map_.find(ARTICLES)->second->ApplyUpdates(status);
}
TestEntryFactory* entry_factory() { return entry_factory_.get(); }
syncable::Directory* directory() { return dir_maker_.directory(); }
private:
base::MessageLoop loop_; // Needed to initialize the directory.
TestDirectorySetterUpper dir_maker_;
std::unique_ptr<TestEntryFactory> entry_factory_;
scoped_refptr<FakeModelWorker> ui_worker_;
scoped_refptr<FakeModelWorker> password_worker_;
scoped_refptr<FakeModelWorker> passive_worker_;
base::ObserverList<TypeDebugInfoObserver> type_observers_;
DirectoryTypeDebugInfoEmitter bookmarks_emitter_;
DirectoryTypeDebugInfoEmitter passwords_emitter_;
DirectoryTypeDebugInfoEmitter articles_emitter_;
std::map<ModelType, std::unique_ptr<UpdateHandler>> update_handler_map_;
};
namespace {
sync_pb::EntitySpecifics DefaultBookmarkSpecifics() {
sync_pb::EntitySpecifics result;
AddDefaultFieldValue(BOOKMARKS, &result);
return result;
}
} // namespace
// Test update application for a few bookmark items.
TEST_F(DirectoryUpdateHandlerApplyUpdateTest, SimpleBookmark) {
StatusController status;
std::string root_server_id = Id::GetRoot().GetServerId();
int64_t parent_handle =
entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
"parent", DefaultBookmarkSpecifics(), root_server_id);
int64_t child_handle =
entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
"child", DefaultBookmarkSpecifics(), "parent");
ApplyBookmarkUpdates(&status);
const UpdateCounters& counter = GetBookmarksUpdateCounters();
EXPECT_EQ(0, counter.num_encryption_conflict_application_failures)
<< "Simple update shouldn't result in conflicts";
EXPECT_EQ(0, counter.num_hierarchy_conflict_application_failures)
<< "Simple update shouldn't result in conflicts";
EXPECT_EQ(2, counter.num_updates_applied)
<< "All items should have been successfully applied";
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry parent(&trans, syncable::GET_BY_HANDLE, parent_handle);
syncable::Entry child(&trans, syncable::GET_BY_HANDLE, child_handle);
ASSERT_TRUE(parent.good());
ASSERT_TRUE(child.good());
EXPECT_FALSE(parent.GetIsUnsynced());
EXPECT_FALSE(parent.GetIsUnappliedUpdate());
EXPECT_FALSE(child.GetIsUnsynced());
EXPECT_FALSE(child.GetIsUnappliedUpdate());
}
}
// Test that the applicator can handle updates delivered out of order.
TEST_F(DirectoryUpdateHandlerApplyUpdateTest, BookmarkChildrenBeforeParent) {
// Start with some bookmarks whose parents are unknown.
std::string root_server_id = Id::GetRoot().GetServerId();
int64_t a_handle = entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
"a_child_created_first", DefaultBookmarkSpecifics(), "parent");
int64_t x_handle = entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
"x_child_created_first", DefaultBookmarkSpecifics(), "parent");
// Update application will fail.
StatusController status1;
ApplyBookmarkUpdates(&status1);
EXPECT_EQ(0, status1.num_updates_applied());
EXPECT_EQ(2, status1.num_hierarchy_conflicts());
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry a(&trans, syncable::GET_BY_HANDLE, a_handle);
syncable::Entry x(&trans, syncable::GET_BY_HANDLE, x_handle);
ASSERT_TRUE(a.good());
ASSERT_TRUE(x.good());
EXPECT_TRUE(a.GetIsUnappliedUpdate());
EXPECT_TRUE(x.GetIsUnappliedUpdate());
}
// Now add their parent and a few siblings.
entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
"parent", DefaultBookmarkSpecifics(), root_server_id);
entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
"a_child_created_second", DefaultBookmarkSpecifics(), "parent");
entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
"x_child_created_second", DefaultBookmarkSpecifics(), "parent");
// Update application will succeed.
StatusController status2;
ApplyBookmarkUpdates(&status2);
EXPECT_EQ(5, status2.num_updates_applied())
<< "All updates should have been successfully applied";
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry a(&trans, syncable::GET_BY_HANDLE, a_handle);
syncable::Entry x(&trans, syncable::GET_BY_HANDLE, x_handle);
ASSERT_TRUE(a.good());
ASSERT_TRUE(x.good());
EXPECT_FALSE(a.GetIsUnappliedUpdate());
EXPECT_FALSE(x.GetIsUnappliedUpdate());
}
}
// Try to apply changes on an item that is both IS_UNSYNCED and
// IS_UNAPPLIED_UPDATE. Conflict resolution should be performed.
TEST_F(DirectoryUpdateHandlerApplyUpdateTest, SimpleBookmarkConflict) {
int64_t handle = entry_factory()->CreateUnappliedAndUnsyncedBookmarkItem("x");
int original_server_version = -10;
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry e(&trans, syncable::GET_BY_HANDLE, handle);
original_server_version = e.GetServerVersion();
ASSERT_NE(original_server_version, e.GetBaseVersion());
EXPECT_TRUE(e.GetIsUnsynced());
}
StatusController status;
ApplyBookmarkUpdates(&status);
const UpdateCounters& counters = GetBookmarksUpdateCounters();
EXPECT_EQ(1, counters.num_server_overwrites)
<< "Unsynced and unapplied item conflict should be resolved";
EXPECT_EQ(0, counters.num_updates_applied)
<< "Update should not be applied; we should override the server.";
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry e(&trans, syncable::GET_BY_HANDLE, handle);
ASSERT_TRUE(e.good());
EXPECT_EQ(original_server_version, e.GetServerVersion());
EXPECT_EQ(original_server_version, e.GetBaseVersion());
EXPECT_FALSE(e.GetIsUnappliedUpdate());
// The unsynced flag will remain set until we successfully commit the item.
EXPECT_TRUE(e.GetIsUnsynced());
}
}
// Create a simple conflict that is also a hierarchy conflict. If we were to
// follow the normal "server wins" logic, we'd end up violating hierarchy
// constraints. The hierarchy conflict must take precedence. We can not allow
// the update to be applied. The item must remain in the conflict state.
TEST_F(DirectoryUpdateHandlerApplyUpdateTest, HierarchyAndSimpleConflict) {
// Create a simply-conflicting item. It will start with valid parent ids.
int64_t handle = entry_factory()->CreateUnappliedAndUnsyncedBookmarkItem(
"orphaned_by_server");
{
// Manually set the SERVER_PARENT_ID to bad value.
// A bad parent indicates a hierarchy conflict.
syncable::WriteTransaction trans(FROM_HERE, UNITTEST, directory());
syncable::MutableEntry entry(&trans, syncable::GET_BY_HANDLE, handle);
ASSERT_TRUE(entry.good());
entry.PutServerParentId(TestIdFactory::MakeServer("bogus_parent"));
}
StatusController status;
ApplyBookmarkUpdates(&status);
const UpdateCounters& counters = GetBookmarksUpdateCounters();
EXPECT_EQ(0, counters.num_updates_applied);
EXPECT_EQ(0, counters.num_server_overwrites);
EXPECT_EQ(1, counters.num_hierarchy_conflict_application_failures);
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry e(&trans, syncable::GET_BY_HANDLE, handle);
ASSERT_TRUE(e.good());
EXPECT_TRUE(e.GetIsUnappliedUpdate());
EXPECT_TRUE(e.GetIsUnsynced());
}
}
// Attempt to apply an udpate that would create a bookmark folder loop. This
// application should fail.
TEST_F(DirectoryUpdateHandlerApplyUpdateTest, BookmarkFolderLoop) {
// Item 'X' locally has parent of 'root'. Server is updating it to have
// parent of 'Y'.
// Create it as a child of root node.
int64_t handle = entry_factory()->CreateSyncedItem("X", BOOKMARKS, true);
{
syncable::WriteTransaction trans(FROM_HERE, UNITTEST, directory());
syncable::MutableEntry entry(&trans, syncable::GET_BY_HANDLE, handle);
ASSERT_TRUE(entry.good());
// Re-parent from root to "Y"
entry.PutServerVersion(entry_factory()->GetNextRevision());
entry.PutIsUnappliedUpdate(true);
entry.PutServerParentId(TestIdFactory::MakeServer("Y"));
}
// Item 'Y' is child of 'X'.
entry_factory()->CreateUnsyncedItem(TestIdFactory::MakeServer("Y"),
TestIdFactory::MakeServer("X"), "Y", true,
BOOKMARKS, nullptr);
// If the server's update were applied, we would have X be a child of Y, and Y
// as a child of X. That's a directory loop. The UpdateApplicator should
// prevent the update from being applied and note that this is a hierarchy
// conflict.
StatusController status;
ApplyBookmarkUpdates(&status);
// This should count as a hierarchy conflict.
const UpdateCounters& counters = GetBookmarksUpdateCounters();
EXPECT_EQ(1, counters.num_hierarchy_conflict_application_failures);
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry e(&trans, syncable::GET_BY_HANDLE, handle);
ASSERT_TRUE(e.good());
EXPECT_TRUE(e.GetIsUnappliedUpdate());
EXPECT_FALSE(e.GetIsUnsynced());
}
}
// Test update application where the update has been orphaned by a local folder
// deletion. The update application attempt should fail.
TEST_F(DirectoryUpdateHandlerApplyUpdateTest, HierarchyConflictDeletedParent) {
// Create a locally deleted parent item.
int64_t parent_handle;
entry_factory()->CreateUnsyncedItem(Id::CreateFromServerId("parent"),
TestIdFactory::root(), "parent", true,
BOOKMARKS, &parent_handle);
{
syncable::WriteTransaction trans(FROM_HERE, UNITTEST, directory());
syncable::MutableEntry entry(&trans, syncable::GET_BY_HANDLE,
parent_handle);
entry.PutIsDel(true);
}
// Create an incoming child from the server.
int64_t child_handle = entry_factory()->CreateUnappliedNewItemWithParent(
"child", DefaultBookmarkSpecifics(), "parent");
// The server's update may seem valid to some other client, but on this client
// that new item's parent no longer exists. The update should not be applied
// and the update applicator should indicate this is a hierarchy conflict.
StatusController status;
ApplyBookmarkUpdates(&status);
const UpdateCounters& counters = GetBookmarksUpdateCounters();
EXPECT_EQ(1, counters.num_hierarchy_conflict_application_failures);
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry child(&trans, syncable::GET_BY_HANDLE, child_handle);
ASSERT_TRUE(child.good());
EXPECT_TRUE(child.GetIsUnappliedUpdate());
EXPECT_FALSE(child.GetIsUnsynced());
}
}
// Attempt to apply an update that deletes a folder where the folder has
// locally-created children. The update application should fail.
TEST_F(DirectoryUpdateHandlerApplyUpdateTest,
HierarchyConflictDeleteNonEmptyDirectory) {
// Create a server-deleted folder as a child of root node.
int64_t parent_handle =
entry_factory()->CreateSyncedItem("parent", BOOKMARKS, true);
{
syncable::WriteTransaction trans(FROM_HERE, UNITTEST, directory());
syncable::MutableEntry entry(&trans, syncable::GET_BY_HANDLE,
parent_handle);
ASSERT_TRUE(entry.good());
// Delete it on the server.
entry.PutServerVersion(entry_factory()->GetNextRevision());
entry.PutIsUnappliedUpdate(true);
entry.PutServerParentId(TestIdFactory::root());
entry.PutServerIsDel(true);
}
// Create a local child of the server-deleted directory.
entry_factory()->CreateUnsyncedItem(TestIdFactory::MakeServer("child"),
TestIdFactory::MakeServer("parent"),
"child", false, BOOKMARKS, nullptr);
// The server's request to delete the directory must be ignored, otherwise our
// unsynced new child would be orphaned. This is a hierarchy conflict.
StatusController status;
ApplyBookmarkUpdates(&status);
// This should count as a hierarchy conflict.
const UpdateCounters& counters = GetBookmarksUpdateCounters();
EXPECT_EQ(1, counters.num_hierarchy_conflict_application_failures);
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry parent(&trans, syncable::GET_BY_HANDLE, parent_handle);
ASSERT_TRUE(parent.good());
EXPECT_TRUE(parent.GetIsUnappliedUpdate());
EXPECT_FALSE(parent.GetIsUnsynced());
}
}
// Attempt to apply updates where the updated item's parent is not known to this
// client. The update application attempt should fail.
TEST_F(DirectoryUpdateHandlerApplyUpdateTest, HierarchyConflictUnknownParent) {
// We shouldn't be able to do anything with either of these items.
int64_t x_handle = entry_factory()->CreateUnappliedNewItemWithParent(
"some_item", DefaultBookmarkSpecifics(), "unknown_parent");
int64_t y_handle = entry_factory()->CreateUnappliedNewItemWithParent(
"some_other_item", DefaultBookmarkSpecifics(), "some_item");
StatusController status;
ApplyBookmarkUpdates(&status);
const UpdateCounters& counters = GetBookmarksUpdateCounters();
EXPECT_EQ(2, counters.num_hierarchy_conflict_application_failures)
<< "All updates with an unknown ancestors should be in conflict";
EXPECT_EQ(0, counters.num_updates_applied)
<< "No item with an unknown ancestor should be applied";
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry x(&trans, syncable::GET_BY_HANDLE, x_handle);
syncable::Entry y(&trans, syncable::GET_BY_HANDLE, y_handle);
ASSERT_TRUE(x.good());
ASSERT_TRUE(y.good());
EXPECT_TRUE(x.GetIsUnappliedUpdate());
EXPECT_TRUE(y.GetIsUnappliedUpdate());
EXPECT_FALSE(x.GetIsUnsynced());
EXPECT_FALSE(y.GetIsUnsynced());
}
}
// Attempt application of a mix of items. Some update application attempts will
// fail due to hierarchy conflicts. Others should succeed.
TEST_F(DirectoryUpdateHandlerApplyUpdateTest, ItemsBothKnownAndUnknown) {
// See what happens when there's a mixture of good and bad updates.
std::string root_server_id = Id::GetRoot().GetServerId();
int64_t u1_handle = entry_factory()->CreateUnappliedNewItemWithParent(
"first_unknown_item", DefaultBookmarkSpecifics(), "unknown_parent");
int64_t k1_handle = entry_factory()->CreateUnappliedNewItemWithParent(
"first_known_item", DefaultBookmarkSpecifics(), root_server_id);
int64_t u2_handle = entry_factory()->CreateUnappliedNewItemWithParent(
"second_unknown_item", DefaultBookmarkSpecifics(), "unknown_parent");
int64_t k2_handle = entry_factory()->CreateUnappliedNewItemWithParent(
"second_known_item", DefaultBookmarkSpecifics(), "first_known_item");
int64_t k3_handle = entry_factory()->CreateUnappliedNewItemWithParent(
"third_known_item", DefaultBookmarkSpecifics(), "fourth_known_item");
int64_t k4_handle = entry_factory()->CreateUnappliedNewItemWithParent(
"fourth_known_item", DefaultBookmarkSpecifics(), root_server_id);
StatusController status;
ApplyBookmarkUpdates(&status);
const UpdateCounters& counters = GetBookmarksUpdateCounters();
EXPECT_EQ(2, counters.num_hierarchy_conflict_application_failures)
<< "The updates with unknown ancestors should be in conflict";
EXPECT_EQ(4, counters.num_updates_applied)
<< "The updates with known ancestors should be successfully applied";
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry u1(&trans, syncable::GET_BY_HANDLE, u1_handle);
syncable::Entry u2(&trans, syncable::GET_BY_HANDLE, u2_handle);
syncable::Entry k1(&trans, syncable::GET_BY_HANDLE, k1_handle);
syncable::Entry k2(&trans, syncable::GET_BY_HANDLE, k2_handle);
syncable::Entry k3(&trans, syncable::GET_BY_HANDLE, k3_handle);
syncable::Entry k4(&trans, syncable::GET_BY_HANDLE, k4_handle);
ASSERT_TRUE(u1.good());
ASSERT_TRUE(u2.good());
ASSERT_TRUE(k1.good());
ASSERT_TRUE(k2.good());
ASSERT_TRUE(k3.good());
ASSERT_TRUE(k4.good());
EXPECT_TRUE(u1.GetIsUnappliedUpdate());
EXPECT_TRUE(u2.GetIsUnappliedUpdate());
EXPECT_FALSE(k1.GetIsUnappliedUpdate());
EXPECT_FALSE(k2.GetIsUnappliedUpdate());
EXPECT_FALSE(k3.GetIsUnappliedUpdate());
EXPECT_FALSE(k4.GetIsUnappliedUpdate());
}
}
// Attempt application of password upates where the passphrase is known.
TEST_F(DirectoryUpdateHandlerApplyUpdateTest, DecryptablePassword) {
// Decryptable password updates should be applied.
Cryptographer* cryptographer;
{
// Storing the cryptographer separately is bad, but for this test we
// know it's safe.
syncable::ReadTransaction trans(FROM_HERE, directory());
cryptographer = directory()->GetCryptographer(&trans);
}
KeyParams params = {"localhost", "dummy", "foobar"};
cryptographer->AddKey(params);
sync_pb::EntitySpecifics specifics;
sync_pb::PasswordSpecificsData data;
data.set_origin("http://example.com");
cryptographer->Encrypt(data,
specifics.mutable_password()->mutable_encrypted());
int64_t handle =
entry_factory()->CreateUnappliedNewItem("item", specifics, false);
StatusController status;
ApplyPasswordUpdates(&status);
const UpdateCounters& counters = GetPasswordsUpdateCounters();
EXPECT_EQ(1, counters.num_updates_applied)
<< "The updates that can be decrypted should be applied";
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry e(&trans, syncable::GET_BY_HANDLE, handle);
ASSERT_TRUE(e.good());
EXPECT_FALSE(e.GetIsUnappliedUpdate());
EXPECT_FALSE(e.GetIsUnsynced());
}
}
// Attempt application of encrypted items when the passphrase is not known.
TEST_F(DirectoryUpdateHandlerApplyUpdateTest, UndecryptableData) {
// Undecryptable updates should not be applied.
sync_pb::EntitySpecifics encrypted_bookmark;
encrypted_bookmark.mutable_encrypted();
AddDefaultFieldValue(BOOKMARKS, &encrypted_bookmark);
std::string root_server_id = Id::GetRoot().GetServerId();
int64_t folder_handle = entry_factory()->CreateUnappliedNewItemWithParent(
"folder", encrypted_bookmark, root_server_id);
int64_t bookmark_handle = entry_factory()->CreateUnappliedNewItem(
"item2", encrypted_bookmark, false);
sync_pb::EntitySpecifics encrypted_password;
encrypted_password.mutable_password();
int64_t password_handle = entry_factory()->CreateUnappliedNewItem(
"item3", encrypted_password, false);
StatusController status;
ApplyBookmarkUpdates(&status);
ApplyPasswordUpdates(&status);
const UpdateCounters& bm_counters = GetBookmarksUpdateCounters();
EXPECT_EQ(2, bm_counters.num_encryption_conflict_application_failures)
<< "Updates that can't be decrypted should be in encryption conflict";
EXPECT_EQ(0, bm_counters.num_updates_applied)
<< "No update that can't be decrypted should be applied";
const UpdateCounters& pw_counters = GetPasswordsUpdateCounters();
EXPECT_EQ(1, pw_counters.num_encryption_conflict_application_failures)
<< "Updates that can't be decrypted should be in encryption conflict";
EXPECT_EQ(0, pw_counters.num_updates_applied)
<< "No update that can't be decrypted should be applied";
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry folder(&trans, syncable::GET_BY_HANDLE, folder_handle);
syncable::Entry bm(&trans, syncable::GET_BY_HANDLE, bookmark_handle);
syncable::Entry pw(&trans, syncable::GET_BY_HANDLE, password_handle);
ASSERT_TRUE(folder.good());
ASSERT_TRUE(bm.good());
ASSERT_TRUE(pw.good());
EXPECT_TRUE(folder.GetIsUnappliedUpdate());
EXPECT_TRUE(bm.GetIsUnappliedUpdate());
EXPECT_TRUE(pw.GetIsUnappliedUpdate());
}
}
// Test a mix of decryptable and undecryptable updates.
TEST_F(DirectoryUpdateHandlerApplyUpdateTest, SomeUndecryptablePassword) {
Cryptographer* cryptographer;
int64_t decryptable_handle = -1;
int64_t undecryptable_handle = -1;
// Only decryptable password updates should be applied.
{
sync_pb::EntitySpecifics specifics;
sync_pb::PasswordSpecificsData data;
data.set_origin("http://example.com/1");
{
syncable::ReadTransaction trans(FROM_HERE, directory());
cryptographer = directory()->GetCryptographer(&trans);
KeyParams params = {"localhost", "dummy", "foobar"};
cryptographer->AddKey(params);
cryptographer->Encrypt(data,
specifics.mutable_password()->mutable_encrypted());
}
decryptable_handle =
entry_factory()->CreateUnappliedNewItem("item1", specifics, false);
}
{
// Create a new cryptographer, independent of the one in the cycle.
Cryptographer other_cryptographer(cryptographer->encryptor());
KeyParams params = {"localhost", "dummy", "bazqux"};
other_cryptographer.AddKey(params);
sync_pb::EntitySpecifics specifics;
sync_pb::PasswordSpecificsData data;
data.set_origin("http://example.com/2");
other_cryptographer.Encrypt(
data, specifics.mutable_password()->mutable_encrypted());
undecryptable_handle =
entry_factory()->CreateUnappliedNewItem("item2", specifics, false);
}
StatusController status;
ApplyPasswordUpdates(&status);
const UpdateCounters& counters = GetPasswordsUpdateCounters();
EXPECT_EQ(1, counters.num_encryption_conflict_application_failures)
<< "The updates that can't be decrypted should be in encryption "
<< "conflict";
EXPECT_EQ(1, counters.num_updates_applied)
<< "The undecryptable password update shouldn't be applied";
{
syncable::ReadTransaction trans(FROM_HERE, directory());
syncable::Entry e1(&trans, syncable::GET_BY_HANDLE, decryptable_handle);
syncable::Entry e2(&trans, syncable::GET_BY_HANDLE, undecryptable_handle);
ASSERT_TRUE(e1.good());
ASSERT_TRUE(e2.good());
EXPECT_FALSE(e1.GetIsUnappliedUpdate());
EXPECT_TRUE(e2.GetIsUnappliedUpdate());
}
}
TEST_F(DirectoryUpdateHandlerApplyUpdateTest,
SimpleConflictDifferentAttachmentMetadata) {
const bool is_folder = false;
sync_pb::EntitySpecifics specifics;
*specifics.mutable_article() = sync_pb::ArticleSpecifics();
int64_t handle =
entry_factory()->CreateSyncedItem("art1", ARTICLES, is_folder);
sync_pb::AttachmentIdProto local_attachment_id =
CreateAttachmentIdProto(0, 0);
sync_pb::AttachmentIdProto server_attachment_id =
CreateAttachmentIdProto(0, 0);
// Add an attachment to the local attachment metadata.
sync_pb::AttachmentMetadata local_metadata;
sync_pb::AttachmentMetadataRecord* local_record = local_metadata.add_record();
*local_record->mutable_id() = local_attachment_id;
local_record->set_is_on_server(true);
entry_factory()->SetLocalAttachmentMetadataForItem(handle, local_metadata);
// Add a different attachment to the server attachment metadata.
sync_pb::AttachmentMetadata server_metadata;
sync_pb::AttachmentMetadataRecord* server_record =
server_metadata.add_record();
*server_record->mutable_id() = server_attachment_id;
server_record->set_is_on_server(true);
entry_factory()->SetServerAttachmentMetadataForItem(handle, server_metadata);
// At this point we have a simple conflict. The server says art1 should have
// server_attachment_id, but the local sync engine says it should have
// local_attachment_id.
StatusController status;
ApplyArticlesUpdates(&status);
// See that the server won.
const UpdateCounters& counters = GetArticlesUpdateCounters();
EXPECT_EQ(1, counters.num_updates_applied);
EXPECT_EQ(1, counters.num_local_overwrites);
EXPECT_EQ(0, counters.num_server_overwrites);
local_metadata = entry_factory()->GetLocalAttachmentMetadataForItem(handle);
EXPECT_EQ(server_metadata.SerializeAsString(),
local_metadata.SerializeAsString());
}
TEST_F(DirectoryUpdateHandlerApplyUpdateTest,
SimpleConflictSameAttachmentMetadataDifferentOrder) {
const bool is_folder = false;
sync_pb::EntitySpecifics specifics;
*specifics.mutable_article() = sync_pb::ArticleSpecifics();
int64_t handle =
entry_factory()->CreateSyncedItem("art1", ARTICLES, is_folder);
sync_pb::AttachmentIdProto id1 = CreateAttachmentIdProto(0, 0);
sync_pb::AttachmentIdProto id2 = CreateAttachmentIdProto(0, 0);
// Add id1, then id2 to the local attachment metadata.
sync_pb::AttachmentMetadata local_metadata;
sync_pb::AttachmentMetadataRecord* record = local_metadata.add_record();
*record->mutable_id() = id1;
record->set_is_on_server(true);
record = local_metadata.add_record();
*record->mutable_id() = id2;
record->set_is_on_server(true);
entry_factory()->SetLocalAttachmentMetadataForItem(handle, local_metadata);
// Add id1 and id2 to the server attachment metadata, but in reverse order.
sync_pb::AttachmentMetadata server_metadata;
record = server_metadata.add_record();
*record->mutable_id() = id2;
record->set_is_on_server(true);
record = local_metadata.add_record();
*record->mutable_id() = id1;
record->set_is_on_server(true);
entry_factory()->SetServerAttachmentMetadataForItem(handle, server_metadata);
// At this point we have a (false) conflict.
StatusController status;
ApplyArticlesUpdates(&status);
// See that the server won.
const UpdateCounters& counters = GetArticlesUpdateCounters();
EXPECT_EQ(1, counters.num_updates_applied);
EXPECT_EQ(1, counters.num_local_overwrites);
EXPECT_EQ(0, counters.num_server_overwrites);
local_metadata = entry_factory()->GetLocalAttachmentMetadataForItem(handle);
EXPECT_EQ(server_metadata.SerializeAsString(),
local_metadata.SerializeAsString());
}
} // namespace syncer
|