| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Syncer unit tests. Unfortunately a lot of these tests | 5 // Syncer unit tests. Unfortunately a lot of these tests |
| 6 // are outdated and need to be reworked and updated. | 6 // are outdated and need to be reworked and updated. |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <list> | 10 #include <list> |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 // If this test starts failing, be aware other sort orders could be valid. | 359 // If this test starts failing, be aware other sort orders could be valid. |
| 360 for (size_t i = 0; i < expected_positions.size(); ++i) { | 360 for (size_t i = 0; i < expected_positions.size(); ++i) { |
| 361 EXPECT_EQ(1u, expected_positions.count(i)); | 361 EXPECT_EQ(1u, expected_positions.count(i)); |
| 362 EXPECT_TRUE(expected_positions[i] == mock_server_->committed_ids()[i]); | 362 EXPECT_TRUE(expected_positions[i] == mock_server_->committed_ids()[i]); |
| 363 } | 363 } |
| 364 } | 364 } |
| 365 | 365 |
| 366 void DoTruncationTest(const ScopedDirLookup& dir, | 366 void DoTruncationTest(const ScopedDirLookup& dir, |
| 367 const vector<int64>& unsynced_handle_view, | 367 const vector<int64>& unsynced_handle_view, |
| 368 const vector<syncable::Id>& expected_id_order) { | 368 const vector<syncable::Id>& expected_id_order) { |
| 369 // The expected order is "x", "b", "c", "e", truncated appropriately. | |
| 370 for (size_t limit = expected_id_order.size() + 2; limit > 0; --limit) { | 369 for (size_t limit = expected_id_order.size() + 2; limit > 0; --limit) { |
| 371 StatusController* status = session_->mutable_status_controller(); | 370 StatusController* status = session_->mutable_status_controller(); |
| 372 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir); | 371 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir); |
| 373 ScopedSetSessionWriteTransaction set_trans(session_.get(), &wtrans); | 372 ScopedSetSessionWriteTransaction set_trans(session_.get(), &wtrans); |
| 374 status->set_unsynced_handles(unsynced_handle_view); | |
| 375 | 373 |
| 376 ModelSafeRoutingInfo routes; | 374 ModelSafeRoutingInfo routes; |
| 377 GetModelSafeRoutingInfo(&routes); | 375 GetModelSafeRoutingInfo(&routes); |
| 378 GetCommitIdsCommand command(limit); | 376 GetCommitIdsCommand command(limit); |
| 379 command.BuildCommitIds( | 377 std::set<int64> ready_unsynced_set; |
| 380 session_->status_controller().unsynced_handles(), | 378 command.FilterUnreadyEntries(&wtrans, syncable::ModelTypeSet(), |
| 381 session_->write_transaction(), routes, syncable::ModelTypeSet()); | 379 syncable::ModelTypeSet(), false, |
| 380 unsynced_handle_view, &ready_unsynced_set); |
| 381 command.BuildCommitIds(session_->write_transaction(), routes, |
| 382 ready_unsynced_set); |
| 383 syncable::Directory::UnsyncedMetaHandles ready_unsynced_vector( |
| 384 ready_unsynced_set.begin(), ready_unsynced_set.end()); |
| 385 status->set_unsynced_handles(ready_unsynced_vector); |
| 382 vector<syncable::Id> output = | 386 vector<syncable::Id> output = |
| 383 command.ordered_commit_set_->GetAllCommitIds(); | 387 command.ordered_commit_set_->GetAllCommitIds(); |
| 384 size_t truncated_size = std::min(limit, expected_id_order.size()); | 388 size_t truncated_size = std::min(limit, expected_id_order.size()); |
| 385 ASSERT_TRUE(truncated_size == output.size()); | 389 ASSERT_EQ(truncated_size, output.size()); |
| 386 for (size_t i = 0; i < truncated_size; ++i) { | 390 for (size_t i = 0; i < truncated_size; ++i) { |
| 387 ASSERT_TRUE(expected_id_order[i] == output[i]) | 391 ASSERT_EQ(expected_id_order[i], output[i]) |
| 388 << "At index " << i << " with batch size limited to " << limit; | 392 << "At index " << i << " with batch size limited to " << limit; |
| 389 } | 393 } |
| 390 sessions::OrderedCommitSet::Projection proj; | 394 sessions::OrderedCommitSet::Projection proj; |
| 391 proj = command.ordered_commit_set_->GetCommitIdProjection(GROUP_PASSIVE); | 395 proj = command.ordered_commit_set_->GetCommitIdProjection(GROUP_PASSIVE); |
| 392 ASSERT_EQ(truncated_size, proj.size()); | 396 ASSERT_EQ(truncated_size, proj.size()); |
| 393 for (size_t i = 0; i < truncated_size; ++i) { | 397 for (size_t i = 0; i < truncated_size; ++i) { |
| 394 SCOPED_TRACE(::testing::Message("Projection mismatch with i = ") << i); | 398 SCOPED_TRACE(::testing::Message("Projection mismatch with i = ") << i); |
| 395 syncable::Id projected = | 399 syncable::Id projected = |
| 396 command.ordered_commit_set_->GetCommitIdAt(proj[i]); | 400 command.ordered_commit_set_->GetCommitIdAt(proj[i]); |
| 397 ASSERT_TRUE(expected_id_order[proj[i]] == projected); | 401 ASSERT_EQ(expected_id_order[proj[i]], projected); |
| 398 // Since this projection is the identity, the following holds. | 402 // Since this projection is the identity, the following holds. |
| 399 ASSERT_TRUE(expected_id_order[i] == projected); | 403 ASSERT_EQ(expected_id_order[i], projected); |
| 400 } | 404 } |
| 401 } | 405 } |
| 402 } | 406 } |
| 403 | 407 |
| 404 int64 CreateUnsyncedDirectory(const string& entry_name, | 408 int64 CreateUnsyncedDirectory(const string& entry_name, |
| 405 const string& idstring) { | 409 const string& idstring) { |
| 406 return CreateUnsyncedDirectory(entry_name, | 410 return CreateUnsyncedDirectory(entry_name, |
| 407 syncable::Id::CreateFromServerId(idstring)); | 411 syncable::Id::CreateFromServerId(idstring)); |
| 408 } | 412 } |
| 409 | 413 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 ASSERT_EQ(0u, handles.size()); | 524 ASSERT_EQ(0u, handles.size()); |
| 521 } | 525 } |
| 522 // TODO(sync): When we can dynamically connect and disconnect the mock | 526 // TODO(sync): When we can dynamically connect and disconnect the mock |
| 523 // ServerConnectionManager test disconnected GetUnsyncedEntries here. It's a | 527 // ServerConnectionManager test disconnected GetUnsyncedEntries here. It's a |
| 524 // regression for a very old bug. | 528 // regression for a very old bug. |
| 525 } | 529 } |
| 526 | 530 |
| 527 TEST_F(SyncerTest, GetCommitIdsCommandTruncates) { | 531 TEST_F(SyncerTest, GetCommitIdsCommandTruncates) { |
| 528 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); | 532 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); |
| 529 ASSERT_TRUE(dir.good()); | 533 ASSERT_TRUE(dir.good()); |
| 530 int64 handle_c = CreateUnsyncedDirectory("C", ids_.MakeLocal("c")); | 534 |
| 531 int64 handle_x = CreateUnsyncedDirectory("X", ids_.MakeLocal("x")); | 535 syncable::Id root = ids_.root(); |
| 532 int64 handle_b = CreateUnsyncedDirectory("B", ids_.MakeLocal("b")); | 536 // Create two server entries. |
| 533 int64 handle_d = CreateUnsyncedDirectory("D", ids_.MakeLocal("d")); | 537 mock_server_->AddUpdateDirectory(ids_.MakeServer("x"), root, "X", 10, 10); |
| 534 int64 handle_e = CreateUnsyncedDirectory("E", ids_.MakeLocal("e")); | 538 mock_server_->AddUpdateDirectory(ids_.MakeServer("w"), root, "W", 10, 10); |
| 539 SyncShareAsDelegate(); |
| 540 |
| 541 // Create some new client entries. |
| 542 CreateUnsyncedDirectory("C", ids_.MakeLocal("c")); |
| 543 CreateUnsyncedDirectory("B", ids_.MakeLocal("b")); |
| 544 CreateUnsyncedDirectory("D", ids_.MakeLocal("d")); |
| 545 CreateUnsyncedDirectory("E", ids_.MakeLocal("e")); |
| 546 CreateUnsyncedDirectory("J", ids_.MakeLocal("j")); |
| 547 |
| 535 { | 548 { |
| 536 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir); | 549 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir); |
| 537 MutableEntry entry_x(&wtrans, GET_BY_HANDLE, handle_x); | 550 MutableEntry entry_x(&wtrans, GET_BY_ID, ids_.MakeServer("x")); |
| 538 MutableEntry entry_b(&wtrans, GET_BY_HANDLE, handle_b); | 551 MutableEntry entry_b(&wtrans, GET_BY_ID, ids_.MakeLocal("b")); |
| 539 MutableEntry entry_c(&wtrans, GET_BY_HANDLE, handle_c); | 552 MutableEntry entry_c(&wtrans, GET_BY_ID, ids_.MakeLocal("c")); |
| 540 MutableEntry entry_d(&wtrans, GET_BY_HANDLE, handle_d); | 553 MutableEntry entry_d(&wtrans, GET_BY_ID, ids_.MakeLocal("d")); |
| 541 MutableEntry entry_e(&wtrans, GET_BY_HANDLE, handle_e); | 554 MutableEntry entry_e(&wtrans, GET_BY_ID, ids_.MakeLocal("e")); |
| 542 entry_x.Put(SPECIFICS, DefaultBookmarkSpecifics()); | 555 MutableEntry entry_w(&wtrans, GET_BY_ID, ids_.MakeServer("w")); |
| 543 entry_b.Put(SPECIFICS, DefaultBookmarkSpecifics()); | 556 MutableEntry entry_j(&wtrans, GET_BY_ID, ids_.MakeLocal("j")); |
| 544 entry_c.Put(SPECIFICS, DefaultBookmarkSpecifics()); | 557 entry_x.Put(IS_UNSYNCED, true); |
| 545 entry_d.Put(SPECIFICS, DefaultBookmarkSpecifics()); | |
| 546 entry_e.Put(SPECIFICS, DefaultBookmarkSpecifics()); | |
| 547 entry_b.Put(PARENT_ID, entry_x.Get(ID)); | 558 entry_b.Put(PARENT_ID, entry_x.Get(ID)); |
| 559 entry_d.Put(PARENT_ID, entry_b.Get(ID)); |
| 548 entry_c.Put(PARENT_ID, entry_x.Get(ID)); | 560 entry_c.Put(PARENT_ID, entry_x.Get(ID)); |
| 549 entry_c.PutPredecessor(entry_b.Get(ID)); | 561 entry_c.PutPredecessor(entry_b.Get(ID)); |
| 550 entry_d.Put(PARENT_ID, entry_b.Get(ID)); | |
| 551 entry_e.Put(PARENT_ID, entry_c.Get(ID)); | 562 entry_e.Put(PARENT_ID, entry_c.Get(ID)); |
| 563 entry_w.PutPredecessor(entry_x.Get(ID)); |
| 564 entry_w.Put(IS_UNSYNCED, true); |
| 565 entry_w.Put(SERVER_VERSION, 20); |
| 566 entry_w.Put(IS_UNAPPLIED_UPDATE, true); // Fake a conflict. |
| 567 entry_j.PutPredecessor(entry_w.Get(ID)); |
| 552 } | 568 } |
| 553 | 569 |
| 554 // The arrangement is now: x (b (d) c (e)). | 570 // The arrangement is now: x (b (d) c (e)) w j |
| 571 // Entry "w" is in conflict, making its sucessors unready to commit. |
| 555 vector<int64> unsynced_handle_view; | 572 vector<int64> unsynced_handle_view; |
| 556 vector<syncable::Id> expected_order; | 573 vector<syncable::Id> expected_order; |
| 557 // The expected order is "x", "b", "c", "e", truncated appropriately. | 574 { |
| 558 unsynced_handle_view.push_back(handle_e); | 575 ReadTransaction rtrans(FROM_HERE, dir); |
| 559 expected_order.push_back(ids_.MakeLocal("x")); | 576 SyncerUtil::GetUnsyncedEntries(&rtrans, &unsynced_handle_view); |
| 577 } |
| 578 // The expected order is "x", "b", "c", "d", "e", truncated appropriately. |
| 579 expected_order.push_back(ids_.MakeServer("x")); |
| 560 expected_order.push_back(ids_.MakeLocal("b")); | 580 expected_order.push_back(ids_.MakeLocal("b")); |
| 561 expected_order.push_back(ids_.MakeLocal("c")); | 581 expected_order.push_back(ids_.MakeLocal("c")); |
| 582 expected_order.push_back(ids_.MakeLocal("d")); |
| 562 expected_order.push_back(ids_.MakeLocal("e")); | 583 expected_order.push_back(ids_.MakeLocal("e")); |
| 563 DoTruncationTest(dir, unsynced_handle_view, expected_order); | 584 DoTruncationTest(dir, unsynced_handle_view, expected_order); |
| 564 } | 585 } |
| 565 | 586 |
| 566 TEST_F(SyncerTest, GetCommitIdsFiltersThrottledEntries) { | 587 TEST_F(SyncerTest, GetCommitIdsFiltersThrottledEntries) { |
| 567 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); | 588 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); |
| 568 ASSERT_TRUE(dir.good()); | 589 ASSERT_TRUE(dir.good()); |
| 569 const syncable::ModelTypeSet throttled_types(syncable::BOOKMARKS); | 590 const syncable::ModelTypeSet throttled_types(syncable::BOOKMARKS); |
| 570 sync_pb::EntitySpecifics bookmark_data; | 591 sync_pb::EntitySpecifics bookmark_data; |
| 571 AddDefaultExtensionValue(syncable::BOOKMARKS, &bookmark_data); | 592 AddDefaultExtensionValue(syncable::BOOKMARKS, &bookmark_data); |
| (...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1055 CanDecryptUsingDefaultKey(our_encrypted_specifics.encrypted())); | 1076 CanDecryptUsingDefaultKey(our_encrypted_specifics.encrypted())); |
| 1056 EXPECT_TRUE(syncdb_.manager()->GetCryptographer(&wtrans)->CanDecrypt( | 1077 EXPECT_TRUE(syncdb_.manager()->GetCryptographer(&wtrans)->CanDecrypt( |
| 1057 other_encrypted_specifics.encrypted())); | 1078 other_encrypted_specifics.encrypted())); |
| 1058 EXPECT_TRUE(syncdb_.manager()->GetCryptographer(&wtrans)-> | 1079 EXPECT_TRUE(syncdb_.manager()->GetCryptographer(&wtrans)-> |
| 1059 CanDecryptUsingDefaultKey(other_encrypted_specifics.encrypted())); | 1080 CanDecryptUsingDefaultKey(other_encrypted_specifics.encrypted())); |
| 1060 EXPECT_TRUE(nigori_entry.Get(SPECIFICS).GetExtension(sync_pb::nigori) | 1081 EXPECT_TRUE(nigori_entry.Get(SPECIFICS).GetExtension(sync_pb::nigori) |
| 1061 .sync_tabs()); | 1082 .sync_tabs()); |
| 1062 } | 1083 } |
| 1063 } | 1084 } |
| 1064 | 1085 |
| 1065 // TODO(chron): More corner case unit tests around validation. | |
| 1066 TEST_F(SyncerTest, TestCommitMetahandleIterator) { | |
| 1067 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); | |
| 1068 ASSERT_TRUE(dir.good()); | |
| 1069 StatusController* status = session_->mutable_status_controller(); | |
| 1070 const vector<int64>& unsynced(status->unsynced_handles()); | |
| 1071 | |
| 1072 { | |
| 1073 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir); | |
| 1074 ScopedSetSessionWriteTransaction set_trans(session_.get(), &wtrans); | |
| 1075 | |
| 1076 sessions::OrderedCommitSet commit_set(session_->routing_info()); | |
| 1077 GetCommitIdsCommand::CommitMetahandleIterator iterator(unsynced, &wtrans, | |
| 1078 &commit_set); | |
| 1079 EXPECT_FALSE(iterator.Valid()); | |
| 1080 EXPECT_FALSE(iterator.Increment()); | |
| 1081 } | |
| 1082 | |
| 1083 { | |
| 1084 vector<int64> session_metahandles; | |
| 1085 session_metahandles.push_back(CreateUnsyncedDirectory("test1", "testid1")); | |
| 1086 session_metahandles.push_back(CreateUnsyncedDirectory("test2", "testid2")); | |
| 1087 session_metahandles.push_back(CreateUnsyncedDirectory("test3", "testid3")); | |
| 1088 status->set_unsynced_handles(session_metahandles); | |
| 1089 | |
| 1090 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir); | |
| 1091 ScopedSetSessionWriteTransaction set_trans(session_.get(), &wtrans); | |
| 1092 sessions::OrderedCommitSet commit_set(session_->routing_info()); | |
| 1093 GetCommitIdsCommand::CommitMetahandleIterator iterator(unsynced, &wtrans, | |
| 1094 &commit_set); | |
| 1095 | |
| 1096 EXPECT_TRUE(iterator.Valid()); | |
| 1097 EXPECT_TRUE(iterator.Current() == session_metahandles[0]); | |
| 1098 EXPECT_TRUE(iterator.Increment()); | |
| 1099 | |
| 1100 EXPECT_TRUE(iterator.Valid()); | |
| 1101 EXPECT_TRUE(iterator.Current() == session_metahandles[1]); | |
| 1102 EXPECT_TRUE(iterator.Increment()); | |
| 1103 | |
| 1104 EXPECT_TRUE(iterator.Valid()); | |
| 1105 EXPECT_TRUE(iterator.Current() == session_metahandles[2]); | |
| 1106 EXPECT_FALSE(iterator.Increment()); | |
| 1107 | |
| 1108 EXPECT_FALSE(iterator.Valid()); | |
| 1109 } | |
| 1110 } | |
| 1111 | |
| 1112 TEST_F(SyncerTest, TestGetUnsyncedAndSimpleCommit) { | 1086 TEST_F(SyncerTest, TestGetUnsyncedAndSimpleCommit) { |
| 1113 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); | 1087 ScopedDirLookup dir(syncdb_.manager(), syncdb_.name()); |
| 1114 ASSERT_TRUE(dir.good()); | 1088 ASSERT_TRUE(dir.good()); |
| 1115 { | 1089 { |
| 1116 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir); | 1090 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir); |
| 1117 MutableEntry parent(&wtrans, syncable::CREATE, wtrans.root_id(), | 1091 MutableEntry parent(&wtrans, syncable::CREATE, wtrans.root_id(), |
| 1118 "Pete"); | 1092 "Pete"); |
| 1119 ASSERT_TRUE(parent.good()); | 1093 ASSERT_TRUE(parent.good()); |
| 1120 parent.Put(syncable::IS_UNSYNCED, true); | 1094 parent.Put(syncable::IS_UNSYNCED, true); |
| 1121 parent.Put(syncable::IS_DIR, true); | 1095 parent.Put(syncable::IS_DIR, true); |
| (...skipping 3697 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4819 Add(low_id_); | 4793 Add(low_id_); |
| 4820 Add(high_id_); | 4794 Add(high_id_); |
| 4821 SyncShareAsDelegate(); | 4795 SyncShareAsDelegate(); |
| 4822 ExpectLocalOrderIsByServerId(); | 4796 ExpectLocalOrderIsByServerId(); |
| 4823 } | 4797 } |
| 4824 | 4798 |
| 4825 const SyncerTest::CommitOrderingTest | 4799 const SyncerTest::CommitOrderingTest |
| 4826 SyncerTest::CommitOrderingTest::LAST_COMMIT_ITEM = {-1, TestIdFactory::root()}; | 4800 SyncerTest::CommitOrderingTest::LAST_COMMIT_ITEM = {-1, TestIdFactory::root()}; |
| 4827 | 4801 |
| 4828 } // namespace browser_sync | 4802 } // namespace browser_sync |
| OLD | NEW |