| 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 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 16 #include "base/stl_util.h" |
| 16 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
| 17 #include "base/synchronization/condition_variable.h" | 18 #include "base/synchronization/condition_variable.h" |
| 18 #include "base/test/values_test_util.h" | 19 #include "base/test/values_test_util.h" |
| 19 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
| 20 #include "base/values.h" | 21 #include "base/values.h" |
| 21 #include "sync/internal_api/public/base/node_ordinal.h" | 22 #include "sync/internal_api/public/base/node_ordinal.h" |
| 22 #include "sync/protocol/bookmark_specifics.pb.h" | 23 #include "sync/protocol/bookmark_specifics.pb.h" |
| 23 #include "sync/syncable/directory_backing_store.h" | 24 #include "sync/syncable/directory_backing_store.h" |
| 24 #include "sync/syncable/directory_change_delegate.h" | 25 #include "sync/syncable/directory_change_delegate.h" |
| 25 #include "sync/syncable/in_memory_directory_backing_store.h" | 26 #include "sync/syncable/in_memory_directory_backing_store.h" |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 } | 486 } |
| 486 EXPECT_FALSE(types_to_purge.Has(BOOKMARKS)); | 487 EXPECT_FALSE(types_to_purge.Has(BOOKMARKS)); |
| 487 EXPECT_TRUE(dir_->initial_sync_ended_for_type(BOOKMARKS)); | 488 EXPECT_TRUE(dir_->initial_sync_ended_for_type(BOOKMARKS)); |
| 488 } | 489 } |
| 489 | 490 |
| 490 FakeEncryptor encryptor_; | 491 FakeEncryptor encryptor_; |
| 491 TestUnrecoverableErrorHandler handler_; | 492 TestUnrecoverableErrorHandler handler_; |
| 492 scoped_ptr<Directory> dir_; | 493 scoped_ptr<Directory> dir_; |
| 493 NullDirectoryChangeDelegate delegate_; | 494 NullDirectoryChangeDelegate delegate_; |
| 494 | 495 |
| 495 // Creates an empty entry and sets the ID field to a default one. | 496 // Creates an empty entry and sets the ID field to a default one. Return meta |
| 497 // handel of created entry. |
| 496 void CreateEntry(const std::string& entryname) { | 498 void CreateEntry(const std::string& entryname) { |
| 497 CreateEntry(entryname, TestIdFactory::FromNumber(-99)); | 499 CreateEntry(entryname, TestIdFactory::FromNumber(-99)); |
| 498 } | 500 } |
| 499 | 501 |
| 500 // Creates an empty entry and sets the ID field to id. | 502 // Creates an empty entry and sets the ID field to id. Return meta handel of |
| 503 // created entry. |
| 501 void CreateEntry(const std::string& entryname, const int id) { | 504 void CreateEntry(const std::string& entryname, const int id) { |
| 502 CreateEntry(entryname, TestIdFactory::FromNumber(id)); | 505 CreateEntry(entryname, TestIdFactory::FromNumber(id)); |
| 503 } | 506 } |
| 504 void CreateEntry(const std::string& entryname, Id id) { | 507 void CreateEntry(const std::string& entryname, Id id) { |
| 505 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir_.get()); | 508 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir_.get()); |
| 506 MutableEntry me(&wtrans, CREATE, wtrans.root_id(), entryname); | 509 MutableEntry me(&wtrans, CREATE, wtrans.root_id(), entryname); |
| 507 ASSERT_TRUE(me.good()); | 510 ASSERT_TRUE(me.good()); |
| 508 me.Put(ID, id); | 511 me.Put(ID, id); |
| 509 me.Put(IS_UNSYNCED, true); | 512 me.Put(IS_UNSYNCED, true); |
| 510 } | 513 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 // Fake SaveChanges() and make sure we got what we expected. | 595 // Fake SaveChanges() and make sure we got what we expected. |
| 593 { | 596 { |
| 594 Directory::SaveChangesSnapshot snapshot; | 597 Directory::SaveChangesSnapshot snapshot; |
| 595 base::AutoLock scoped_lock(dir_->kernel_->save_changes_mutex); | 598 base::AutoLock scoped_lock(dir_->kernel_->save_changes_mutex); |
| 596 dir_->TakeSnapshotForSaveChanges(&snapshot); | 599 dir_->TakeSnapshotForSaveChanges(&snapshot); |
| 597 // Make sure there's an entry for each new metahandle. Make sure all | 600 // Make sure there's an entry for each new metahandle. Make sure all |
| 598 // entries are marked dirty. | 601 // entries are marked dirty. |
| 599 ASSERT_EQ(expected_dirty_metahandles.size(), snapshot.dirty_metas.size()); | 602 ASSERT_EQ(expected_dirty_metahandles.size(), snapshot.dirty_metas.size()); |
| 600 for (EntryKernelSet::const_iterator i = snapshot.dirty_metas.begin(); | 603 for (EntryKernelSet::const_iterator i = snapshot.dirty_metas.begin(); |
| 601 i != snapshot.dirty_metas.end(); ++i) { | 604 i != snapshot.dirty_metas.end(); ++i) { |
| 602 ASSERT_TRUE(i->is_dirty()); | 605 ASSERT_TRUE((*i)->is_dirty()); |
| 603 } | 606 } |
| 604 dir_->VacuumAfterSaveChanges(snapshot); | 607 dir_->VacuumAfterSaveChanges(snapshot); |
| 605 } | 608 } |
| 606 // Put a new value with existing transactions as well as adding new ones. | 609 // Put a new value with existing transactions as well as adding new ones. |
| 607 { | 610 { |
| 608 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get()); | 611 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get()); |
| 609 std::vector<int64> new_dirty_metahandles; | 612 std::vector<int64> new_dirty_metahandles; |
| 610 for (std::vector<int64>::const_iterator i = | 613 for (std::vector<int64>::const_iterator i = |
| 611 expected_dirty_metahandles.begin(); | 614 expected_dirty_metahandles.begin(); |
| 612 i != expected_dirty_metahandles.end(); ++i) { | 615 i != expected_dirty_metahandles.end(); ++i) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 625 // Fake SaveChanges() and make sure we got what we expected. | 628 // Fake SaveChanges() and make sure we got what we expected. |
| 626 { | 629 { |
| 627 Directory::SaveChangesSnapshot snapshot; | 630 Directory::SaveChangesSnapshot snapshot; |
| 628 base::AutoLock scoped_lock(dir_->kernel_->save_changes_mutex); | 631 base::AutoLock scoped_lock(dir_->kernel_->save_changes_mutex); |
| 629 dir_->TakeSnapshotForSaveChanges(&snapshot); | 632 dir_->TakeSnapshotForSaveChanges(&snapshot); |
| 630 // Make sure there's an entry for each new metahandle. Make sure all | 633 // Make sure there's an entry for each new metahandle. Make sure all |
| 631 // entries are marked dirty. | 634 // entries are marked dirty. |
| 632 EXPECT_EQ(expected_dirty_metahandles.size(), snapshot.dirty_metas.size()); | 635 EXPECT_EQ(expected_dirty_metahandles.size(), snapshot.dirty_metas.size()); |
| 633 for (EntryKernelSet::const_iterator i = snapshot.dirty_metas.begin(); | 636 for (EntryKernelSet::const_iterator i = snapshot.dirty_metas.begin(); |
| 634 i != snapshot.dirty_metas.end(); ++i) { | 637 i != snapshot.dirty_metas.end(); ++i) { |
| 635 EXPECT_TRUE(i->is_dirty()); | 638 EXPECT_TRUE((*i)->is_dirty()); |
| 636 } | 639 } |
| 637 dir_->VacuumAfterSaveChanges(snapshot); | 640 dir_->VacuumAfterSaveChanges(snapshot); |
| 638 } | 641 } |
| 639 } | 642 } |
| 640 | 643 |
| 641 TEST_F(SyncableDirectoryTest, TakeSnapshotGetsOnlyDirtyHandlesTest) { | 644 TEST_F(SyncableDirectoryTest, TakeSnapshotGetsOnlyDirtyHandlesTest) { |
| 642 const int metahandles_to_create = 100; | 645 const int metahandles_to_create = 100; |
| 643 | 646 |
| 644 // half of 2 * metahandles_to_create | 647 // half of 2 * metahandles_to_create |
| 645 const unsigned int number_changed = 100u; | 648 const unsigned int number_changed = 100u; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 714 // Fake SaveChanges() and make sure we got what we expected. | 717 // Fake SaveChanges() and make sure we got what we expected. |
| 715 { | 718 { |
| 716 Directory::SaveChangesSnapshot snapshot; | 719 Directory::SaveChangesSnapshot snapshot; |
| 717 base::AutoLock scoped_lock(dir_->kernel_->save_changes_mutex); | 720 base::AutoLock scoped_lock(dir_->kernel_->save_changes_mutex); |
| 718 dir_->TakeSnapshotForSaveChanges(&snapshot); | 721 dir_->TakeSnapshotForSaveChanges(&snapshot); |
| 719 // Make sure there's an entry for each changed metahandle. Make sure all | 722 // Make sure there's an entry for each changed metahandle. Make sure all |
| 720 // entries are marked dirty. | 723 // entries are marked dirty. |
| 721 EXPECT_EQ(number_changed, snapshot.dirty_metas.size()); | 724 EXPECT_EQ(number_changed, snapshot.dirty_metas.size()); |
| 722 for (EntryKernelSet::const_iterator i = snapshot.dirty_metas.begin(); | 725 for (EntryKernelSet::const_iterator i = snapshot.dirty_metas.begin(); |
| 723 i != snapshot.dirty_metas.end(); ++i) { | 726 i != snapshot.dirty_metas.end(); ++i) { |
| 724 EXPECT_TRUE(i->is_dirty()); | 727 EXPECT_TRUE((*i)->is_dirty()); |
| 725 } | 728 } |
| 726 dir_->VacuumAfterSaveChanges(snapshot); | 729 dir_->VacuumAfterSaveChanges(snapshot); |
| 727 } | 730 } |
| 728 } | 731 } |
| 729 | 732 |
| 733 // Test delete journals management. |
| 734 TEST_F(SyncableDirectoryTest, ManageDeleteJournals) { |
| 735 sync_pb::EntitySpecifics bookmark_specifics; |
| 736 AddDefaultFieldValue(BOOKMARKS, &bookmark_specifics); |
| 737 bookmark_specifics.mutable_bookmark()->set_url("url"); |
| 738 |
| 739 Id id1 = TestIdFactory::FromNumber(-1); |
| 740 Id id2 = TestIdFactory::FromNumber(-2); |
| 741 int64 handle1 = 0; |
| 742 int64 handle2 = 0; |
| 743 { |
| 744 // Create two bookmark entries and save in database. |
| 745 CreateEntry("item1", id1); |
| 746 CreateEntry("item2", id2); |
| 747 { |
| 748 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get()); |
| 749 MutableEntry item1(&trans, GET_BY_ID, id1); |
| 750 ASSERT_TRUE(item1.good()); |
| 751 handle1 = item1.Get(META_HANDLE); |
| 752 item1.Put(SPECIFICS, bookmark_specifics); |
| 753 item1.Put(SERVER_SPECIFICS, bookmark_specifics); |
| 754 MutableEntry item2(&trans, GET_BY_ID, id2); |
| 755 ASSERT_TRUE(item2.good()); |
| 756 handle2 = item2.Get(META_HANDLE); |
| 757 item2.Put(SPECIFICS, bookmark_specifics); |
| 758 item2.Put(SERVER_SPECIFICS, bookmark_specifics); |
| 759 } |
| 760 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir()); |
| 761 } |
| 762 |
| 763 { // Test adding and saving delete journals. |
| 764 { |
| 765 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get()); |
| 766 EntryKernelSet delete_journals; |
| 767 dir_->GetDeleteJournals(&trans, BOOKMARKS, &delete_journals); |
| 768 ASSERT_EQ(0u, delete_journals.size()); |
| 769 |
| 770 // Set SERVER_IS_DEL of the entries to true and they should be added to |
| 771 // delete journals. |
| 772 MutableEntry item1(&trans, GET_BY_ID, id1); |
| 773 ASSERT_TRUE(item1.good()); |
| 774 item1.Put(SERVER_IS_DEL, true); |
| 775 MutableEntry item2(&trans, GET_BY_ID, id2); |
| 776 ASSERT_TRUE(item2.good()); |
| 777 item2.Put(SERVER_IS_DEL, true); |
| 778 EntryKernel tmp; |
| 779 tmp.put(ID, id1); |
| 780 EXPECT_TRUE(dir_->kernel_->delete_journals_->count(&tmp)); |
| 781 tmp.put(ID, id2); |
| 782 EXPECT_TRUE(dir_->kernel_->delete_journals_->count(&tmp)); |
| 783 } |
| 784 |
| 785 // Save delete journals in database and verify memory clearing. |
| 786 ASSERT_TRUE(dir_->SaveChanges()); |
| 787 EXPECT_EQ(0u, dir_->kernel_->delete_journals_->size()); |
| 788 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir()); |
| 789 } |
| 790 |
| 791 { |
| 792 { |
| 793 // Test reading delete journals from database. |
| 794 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get()); |
| 795 EntryKernelSet delete_journals; |
| 796 dir_->GetDeleteJournals(&trans, BOOKMARKS, &delete_journals); |
| 797 ASSERT_EQ(2u, delete_journals.size()); |
| 798 EntryKernel tmp; |
| 799 tmp.put(META_HANDLE, handle1); |
| 800 EXPECT_TRUE(delete_journals.count(&tmp)); |
| 801 tmp.put(META_HANDLE, handle2); |
| 802 EXPECT_TRUE(delete_journals.count(&tmp)); |
| 803 |
| 804 // Purge item2. |
| 805 MetahandleSet to_purge; |
| 806 to_purge.insert(handle2); |
| 807 dir_->PurgeDeleteJournals(&trans, to_purge); |
| 808 |
| 809 // Verify that item2 is purged from journals in memory and will be |
| 810 // purged from database. |
| 811 tmp.put(ID, id2); |
| 812 EXPECT_FALSE(dir_->kernel_->delete_journals_->count(&tmp)); |
| 813 EXPECT_EQ(1u, dir_->kernel_->delete_journals_to_purge_->size()); |
| 814 EXPECT_TRUE(dir_->kernel_->delete_journals_to_purge_->count(handle2)); |
| 815 } |
| 816 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir()); |
| 817 } |
| 818 |
| 819 { |
| 820 { |
| 821 // Verify purged entry is gone in database. |
| 822 WriteTransaction trans(FROM_HERE, UNITTEST, dir_.get()); |
| 823 EntryKernelSet delete_journals; |
| 824 dir_->GetDeleteJournals(&trans, BOOKMARKS, &delete_journals); |
| 825 ASSERT_EQ(1u, delete_journals.size()); |
| 826 EntryKernel tmp; |
| 827 tmp.put(ID, id1); |
| 828 tmp.put(META_HANDLE, handle1); |
| 829 EXPECT_TRUE(delete_journals.count(&tmp)); |
| 830 |
| 831 // Undelete item1. |
| 832 MutableEntry item1(&trans, GET_BY_ID, id1); |
| 833 ASSERT_TRUE(item1.good()); |
| 834 item1.Put(SERVER_IS_DEL, false); |
| 835 EXPECT_TRUE(dir_->kernel_->delete_journals_->empty()); |
| 836 EXPECT_EQ(1u, dir_->kernel_->delete_journals_to_purge_->size()); |
| 837 EXPECT_TRUE(dir_->kernel_->delete_journals_to_purge_->count(handle1)); |
| 838 } |
| 839 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir()); |
| 840 } |
| 841 |
| 842 { |
| 843 // Verify undeleted entry is gone from database. |
| 844 ReadTransaction trans(FROM_HERE, dir_.get()); |
| 845 EntryKernelSet delete_journals; |
| 846 dir_->GetDeleteJournals(&trans, BOOKMARKS, &delete_journals); |
| 847 ASSERT_EQ(0u, delete_journals.size()); |
| 848 } |
| 849 } |
| 850 |
| 730 const char SyncableDirectoryTest::kName[] = "Foo"; | 851 const char SyncableDirectoryTest::kName[] = "Foo"; |
| 731 | 852 |
| 732 namespace { | 853 namespace { |
| 733 | 854 |
| 734 TEST_F(SyncableDirectoryTest, TestBasicLookupNonExistantID) { | 855 TEST_F(SyncableDirectoryTest, TestBasicLookupNonExistantID) { |
| 735 ReadTransaction rtrans(FROM_HERE, dir_.get()); | 856 ReadTransaction rtrans(FROM_HERE, dir_.get()); |
| 736 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99)); | 857 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99)); |
| 737 ASSERT_FALSE(e.good()); | 858 ASSERT_FALSE(e.good()); |
| 738 } | 859 } |
| 739 | 860 |
| (...skipping 1396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2136 EXPECT_TRUE(CreateWithDefaultTag(factory_.NewServerId(), true)); | 2257 EXPECT_TRUE(CreateWithDefaultTag(factory_.NewServerId(), true)); |
| 2137 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), true)); | 2258 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), true)); |
| 2138 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), false)); | 2259 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), false)); |
| 2139 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), false)); | 2260 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), false)); |
| 2140 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), true)); | 2261 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), true)); |
| 2141 } | 2262 } |
| 2142 | 2263 |
| 2143 } // namespace | 2264 } // namespace |
| 2144 } // namespace syncable | 2265 } // namespace syncable |
| 2145 } // namespace syncer | 2266 } // namespace syncer |
| OLD | NEW |