1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sync/syncable/directory_unittest.h"
7 #include "base/run_loop.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/test/values_test_util.h"
10 #include "sync/internal_api/public/base/attachment_id_proto.h"
11 #include "sync/syncable/syncable_proto_util.h"
12 #include "sync/syncable/syncable_util.h"
13 #include "sync/syncable/syncable_write_transaction.h"
14 #include "sync/test/engine/test_syncable_utils.h"
15 #include "sync/test/test_directory_backing_store.h"
16 #include "sync/util/mock_unrecoverable_error_handler.h"
18 using base::ExpectDictBooleanValue
;
19 using base::ExpectDictStringValue
;
27 bool IsLegalNewParent(const Entry
& a
, const Entry
& b
) {
28 return IsLegalNewParent(a
.trans(), a
.GetId(), b
.GetId());
31 void PutDataAsBookmarkFavicon(WriteTransaction
* wtrans
,
34 size_t bytes_length
) {
35 sync_pb::EntitySpecifics specifics
;
36 specifics
.mutable_bookmark()->set_url("http://demo/");
37 specifics
.mutable_bookmark()->set_favicon(bytes
, bytes_length
);
38 e
->PutSpecifics(specifics
);
41 void ExpectDataFromBookmarkFaviconEquals(BaseTransaction
* trans
,
44 size_t bytes_length
) {
45 ASSERT_TRUE(e
->good());
46 ASSERT_TRUE(e
->GetSpecifics().has_bookmark());
47 ASSERT_EQ("http://demo/", e
->GetSpecifics().bookmark().url());
48 ASSERT_EQ(std::string(bytes
, bytes_length
),
49 e
->GetSpecifics().bookmark().favicon());
54 const char SyncableDirectoryTest::kDirectoryName
[] = "Foo";
56 SyncableDirectoryTest::SyncableDirectoryTest() {
59 SyncableDirectoryTest::~SyncableDirectoryTest() {
62 void SyncableDirectoryTest::SetUp() {
63 ASSERT_TRUE(connection_
.OpenInMemory());
64 ASSERT_EQ(OPENED
, ReopenDirectory());
67 void SyncableDirectoryTest::TearDown() {
73 DirOpenResult
SyncableDirectoryTest::ReopenDirectory() {
74 // Use a TestDirectoryBackingStore and sql::Connection so we can have test
75 // data persist across Directory object lifetimes while getting the
76 // performance benefits of not writing to disk.
77 dir_
.reset(new Directory(
78 new TestDirectoryBackingStore(kDirectoryName
, &connection_
),
79 MakeWeakHandle(handler_
.GetWeakPtr()), base::Closure(), NULL
, NULL
));
81 DirOpenResult open_result
=
82 dir_
->Open(kDirectoryName
, &delegate_
, NullTransactionObserver());
84 if (open_result
!= OPENED
) {
91 // Creates an empty entry and sets the ID field to a default one.
92 void SyncableDirectoryTest::CreateEntry(const ModelType
& model_type
,
93 const std::string
& entryname
) {
94 CreateEntry(model_type
, entryname
, TestIdFactory::FromNumber(-99));
97 // Creates an empty entry and sets the ID field to id.
98 void SyncableDirectoryTest::CreateEntry(const ModelType
& model_type
,
99 const std::string
& entryname
,
101 CreateEntry(model_type
, entryname
, TestIdFactory::FromNumber(id
));
104 void SyncableDirectoryTest::CreateEntry(const ModelType
& model_type
,
105 const std::string
& entryname
,
107 CreateEntryWithAttachmentMetadata(
108 model_type
, entryname
, id
, sync_pb::AttachmentMetadata());
111 void SyncableDirectoryTest::CreateEntryWithAttachmentMetadata(
112 const ModelType
& model_type
,
113 const std::string
& entryname
,
115 const sync_pb::AttachmentMetadata
& attachment_metadata
) {
116 WriteTransaction
wtrans(FROM_HERE
, UNITTEST
, dir_
.get());
117 MutableEntry
me(&wtrans
, CREATE
, model_type
, wtrans
.root_id(), entryname
);
118 ASSERT_TRUE(me
.good());
120 me
.PutAttachmentMetadata(attachment_metadata
);
121 me
.PutIsUnsynced(true);
124 void SyncableDirectoryTest::DeleteEntry(const Id
& id
) {
125 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
126 MutableEntry
entry(&trans
, GET_BY_ID
, id
);
127 ASSERT_TRUE(entry
.good());
128 entry
.PutIsDel(true);
131 DirOpenResult
SyncableDirectoryTest::SimulateSaveAndReloadDir() {
132 if (!dir_
->SaveChanges())
133 return FAILED_IN_UNITTEST
;
135 return ReopenDirectory();
138 DirOpenResult
SyncableDirectoryTest::SimulateCrashAndReloadDir() {
139 return ReopenDirectory();
142 void SyncableDirectoryTest::GetAllMetaHandles(BaseTransaction
* trans
,
143 MetahandleSet
* result
) {
144 dir_
->GetAllMetaHandles(trans
, result
);
147 void SyncableDirectoryTest::CheckPurgeEntriesWithTypeInSucceeded(
148 ModelTypeSet types_to_purge
,
149 bool before_reload
) {
150 SCOPED_TRACE(testing::Message("Before reload: ") << before_reload
);
152 ReadTransaction
trans(FROM_HERE
, dir_
.get());
153 MetahandleSet all_set
;
154 dir_
->GetAllMetaHandles(&trans
, &all_set
);
155 EXPECT_EQ(4U, all_set
.size());
157 EXPECT_EQ(6U, dir_
->kernel()->metahandles_to_purge
.size());
158 for (MetahandleSet::iterator iter
= all_set
.begin(); iter
!= all_set
.end();
160 Entry
e(&trans
, GET_BY_HANDLE
, *iter
);
161 const ModelType local_type
= e
.GetModelType();
162 const ModelType server_type
= e
.GetServerModelType();
164 // Note the dance around incrementing |it|, since we sometimes erase().
165 if ((IsRealDataType(local_type
) && types_to_purge
.Has(local_type
)) ||
166 (IsRealDataType(server_type
) && types_to_purge
.Has(server_type
))) {
167 FAIL() << "Illegal type should have been deleted.";
172 for (ModelTypeSet::Iterator it
= types_to_purge
.First(); it
.Good();
174 EXPECT_FALSE(dir_
->InitialSyncEndedForType(it
.Get()));
175 sync_pb::DataTypeProgressMarker progress
;
176 dir_
->GetDownloadProgress(it
.Get(), &progress
);
177 EXPECT_EQ("", progress
.token());
179 ReadTransaction
trans(FROM_HERE
, dir_
.get());
180 sync_pb::DataTypeContext context
;
181 dir_
->GetDataTypeContext(&trans
, it
.Get(), &context
);
182 EXPECT_TRUE(context
.SerializeAsString().empty());
184 EXPECT_FALSE(types_to_purge
.Has(BOOKMARKS
));
185 EXPECT_TRUE(dir_
->InitialSyncEndedForType(BOOKMARKS
));
188 bool SyncableDirectoryTest::IsInDirtyMetahandles(int64 metahandle
) {
189 return 1 == dir_
->kernel()->dirty_metahandles
.count(metahandle
);
192 bool SyncableDirectoryTest::IsInMetahandlesToPurge(int64 metahandle
) {
193 return 1 == dir_
->kernel()->metahandles_to_purge
.count(metahandle
);
196 scoped_ptr
<Directory
>& SyncableDirectoryTest::dir() {
200 DirectoryChangeDelegate
* SyncableDirectoryTest::directory_change_delegate() {
204 Encryptor
* SyncableDirectoryTest::encryptor() {
209 TestUnrecoverableErrorHandler
*
210 SyncableDirectoryTest::unrecoverable_error_handler() {
214 void SyncableDirectoryTest::ValidateEntry(BaseTransaction
* trans
,
217 const std::string
& name
,
219 int64 server_version
,
221 Entry
e(trans
, GET_BY_ID
, TestIdFactory::FromNumber(id
));
222 ASSERT_TRUE(e
.good());
224 ASSERT_TRUE(name
== e
.GetNonUniqueName());
225 ASSERT_TRUE(base_version
== e
.GetBaseVersion());
226 ASSERT_TRUE(server_version
== e
.GetServerVersion());
227 ASSERT_TRUE(is_del
== e
.GetIsDel());
230 TEST_F(SyncableDirectoryTest
, TakeSnapshotGetsMetahandlesToPurge
) {
231 const int metas_to_create
= 50;
232 MetahandleSet expected_purges
;
233 MetahandleSet all_handles
;
235 dir()->SetDownloadProgress(BOOKMARKS
, BuildProgress(BOOKMARKS
));
236 dir()->SetDownloadProgress(PREFERENCES
, BuildProgress(PREFERENCES
));
237 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
238 for (int i
= 0; i
< metas_to_create
; i
++) {
239 MutableEntry
e(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "foo");
240 e
.PutIsUnsynced(true);
241 sync_pb::EntitySpecifics specs
;
243 AddDefaultFieldValue(BOOKMARKS
, &specs
);
244 expected_purges
.insert(e
.GetMetahandle());
245 all_handles
.insert(e
.GetMetahandle());
247 AddDefaultFieldValue(PREFERENCES
, &specs
);
248 all_handles
.insert(e
.GetMetahandle());
250 e
.PutSpecifics(specs
);
251 e
.PutServerSpecifics(specs
);
255 ModelTypeSet
to_purge(BOOKMARKS
);
256 dir()->PurgeEntriesWithTypeIn(to_purge
, ModelTypeSet(), ModelTypeSet());
258 Directory::SaveChangesSnapshot snapshot1
;
259 base::AutoLock
scoped_lock(dir()->kernel()->save_changes_mutex
);
260 dir()->TakeSnapshotForSaveChanges(&snapshot1
);
261 EXPECT_TRUE(expected_purges
== snapshot1
.metahandles_to_purge
);
264 to_purge
.Put(PREFERENCES
);
265 dir()->PurgeEntriesWithTypeIn(to_purge
, ModelTypeSet(), ModelTypeSet());
267 dir()->HandleSaveChangesFailure(snapshot1
);
269 Directory::SaveChangesSnapshot snapshot2
;
270 dir()->TakeSnapshotForSaveChanges(&snapshot2
);
271 EXPECT_TRUE(all_handles
== snapshot2
.metahandles_to_purge
);
274 TEST_F(SyncableDirectoryTest
, TakeSnapshotGetsAllDirtyHandlesTest
) {
275 const int metahandles_to_create
= 100;
276 std::vector
<int64
> expected_dirty_metahandles
;
278 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
279 for (int i
= 0; i
< metahandles_to_create
; i
++) {
280 MutableEntry
e(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "foo");
281 expected_dirty_metahandles
.push_back(e
.GetMetahandle());
282 e
.PutIsUnsynced(true);
285 // Fake SaveChanges() and make sure we got what we expected.
287 Directory::SaveChangesSnapshot snapshot
;
288 base::AutoLock
scoped_lock(dir()->kernel()->save_changes_mutex
);
289 dir()->TakeSnapshotForSaveChanges(&snapshot
);
290 // Make sure there's an entry for each new metahandle. Make sure all
291 // entries are marked dirty.
292 ASSERT_EQ(expected_dirty_metahandles
.size(), snapshot
.dirty_metas
.size());
293 for (EntryKernelSet::const_iterator i
= snapshot
.dirty_metas
.begin();
294 i
!= snapshot
.dirty_metas
.end();
296 ASSERT_TRUE((*i
)->is_dirty());
298 dir()->VacuumAfterSaveChanges(snapshot
);
300 // Put a new value with existing transactions as well as adding new ones.
302 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
303 std::vector
<int64
> new_dirty_metahandles
;
304 for (std::vector
<int64
>::const_iterator i
=
305 expected_dirty_metahandles
.begin();
306 i
!= expected_dirty_metahandles
.end();
308 // Change existing entries to directories to dirty them.
309 MutableEntry
e1(&trans
, GET_BY_HANDLE
, *i
);
311 e1
.PutIsUnsynced(true);
313 MutableEntry
e2(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "bar");
314 e2
.PutIsUnsynced(true);
315 new_dirty_metahandles
.push_back(e2
.GetMetahandle());
317 expected_dirty_metahandles
.insert(expected_dirty_metahandles
.end(),
318 new_dirty_metahandles
.begin(),
319 new_dirty_metahandles
.end());
321 // Fake SaveChanges() and make sure we got what we expected.
323 Directory::SaveChangesSnapshot snapshot
;
324 base::AutoLock
scoped_lock(dir()->kernel()->save_changes_mutex
);
325 dir()->TakeSnapshotForSaveChanges(&snapshot
);
326 // Make sure there's an entry for each new metahandle. Make sure all
327 // entries are marked dirty.
328 EXPECT_EQ(expected_dirty_metahandles
.size(), snapshot
.dirty_metas
.size());
329 for (EntryKernelSet::const_iterator i
= snapshot
.dirty_metas
.begin();
330 i
!= snapshot
.dirty_metas
.end();
332 EXPECT_TRUE((*i
)->is_dirty());
334 dir()->VacuumAfterSaveChanges(snapshot
);
338 TEST_F(SyncableDirectoryTest
, TakeSnapshotGetsOnlyDirtyHandlesTest
) {
339 const int metahandles_to_create
= 100;
341 // half of 2 * metahandles_to_create
342 const unsigned int number_changed
= 100u;
343 std::vector
<int64
> expected_dirty_metahandles
;
345 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
346 for (int i
= 0; i
< metahandles_to_create
; i
++) {
347 MutableEntry
e(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "foo");
348 expected_dirty_metahandles
.push_back(e
.GetMetahandle());
349 e
.PutIsUnsynced(true);
352 dir()->SaveChanges();
353 // Put a new value with existing transactions as well as adding new ones.
355 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
356 std::vector
<int64
> new_dirty_metahandles
;
357 for (std::vector
<int64
>::const_iterator i
=
358 expected_dirty_metahandles
.begin();
359 i
!= expected_dirty_metahandles
.end();
361 // Change existing entries to directories to dirty them.
362 MutableEntry
e1(&trans
, GET_BY_HANDLE
, *i
);
363 ASSERT_TRUE(e1
.good());
365 e1
.PutIsUnsynced(true);
367 MutableEntry
e2(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "bar");
368 e2
.PutIsUnsynced(true);
369 new_dirty_metahandles
.push_back(e2
.GetMetahandle());
371 expected_dirty_metahandles
.insert(expected_dirty_metahandles
.end(),
372 new_dirty_metahandles
.begin(),
373 new_dirty_metahandles
.end());
375 dir()->SaveChanges();
376 // Don't make any changes whatsoever and ensure nothing comes back.
378 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
379 for (std::vector
<int64
>::const_iterator i
=
380 expected_dirty_metahandles
.begin();
381 i
!= expected_dirty_metahandles
.end();
383 MutableEntry
e(&trans
, GET_BY_HANDLE
, *i
);
384 ASSERT_TRUE(e
.good());
385 // We aren't doing anything to dirty these entries.
388 // Fake SaveChanges() and make sure we got what we expected.
390 Directory::SaveChangesSnapshot snapshot
;
391 base::AutoLock
scoped_lock(dir()->kernel()->save_changes_mutex
);
392 dir()->TakeSnapshotForSaveChanges(&snapshot
);
393 // Make sure there are no dirty_metahandles.
394 EXPECT_EQ(0u, snapshot
.dirty_metas
.size());
395 dir()->VacuumAfterSaveChanges(snapshot
);
398 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
399 bool should_change
= false;
400 for (std::vector
<int64
>::const_iterator i
=
401 expected_dirty_metahandles
.begin();
402 i
!= expected_dirty_metahandles
.end();
404 // Maybe change entries by flipping IS_DIR.
405 MutableEntry
e(&trans
, GET_BY_HANDLE
, *i
);
406 ASSERT_TRUE(e
.good());
407 should_change
= !should_change
;
409 bool not_dir
= !e
.GetIsDir();
411 e
.PutIsUnsynced(true);
415 // Fake SaveChanges() and make sure we got what we expected.
417 Directory::SaveChangesSnapshot snapshot
;
418 base::AutoLock
scoped_lock(dir()->kernel()->save_changes_mutex
);
419 dir()->TakeSnapshotForSaveChanges(&snapshot
);
420 // Make sure there's an entry for each changed metahandle. Make sure all
421 // entries are marked dirty.
422 EXPECT_EQ(number_changed
, snapshot
.dirty_metas
.size());
423 for (EntryKernelSet::const_iterator i
= snapshot
.dirty_metas
.begin();
424 i
!= snapshot
.dirty_metas
.end();
426 EXPECT_TRUE((*i
)->is_dirty());
428 dir()->VacuumAfterSaveChanges(snapshot
);
432 // Test delete journals management.
433 TEST_F(SyncableDirectoryTest
, ManageDeleteJournals
) {
434 sync_pb::EntitySpecifics bookmark_specifics
;
435 AddDefaultFieldValue(BOOKMARKS
, &bookmark_specifics
);
436 bookmark_specifics
.mutable_bookmark()->set_url("url");
438 // The first two IDs are server IDs.
439 Id id1
= TestIdFactory::FromNumber(1);
440 Id id2
= TestIdFactory::FromNumber(2);
441 // The third one is a client ID.
442 Id id3
= TestIdFactory::FromNumber(-3);
447 // Create 3 bookmark entries and save in database.
449 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
451 MutableEntry
item1(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "item1");
453 item1
.PutSpecifics(bookmark_specifics
);
454 item1
.PutServerSpecifics(bookmark_specifics
);
455 item1
.PutIsUnappliedUpdate(true);
456 item1
.PutBaseVersion(10);
457 handle1
= item1
.GetMetahandle();
459 MutableEntry
item2(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "item2");
461 item2
.PutSpecifics(bookmark_specifics
);
462 item2
.PutServerSpecifics(bookmark_specifics
);
463 item2
.PutIsUnappliedUpdate(true);
464 item2
.PutBaseVersion(10);
465 handle2
= item2
.GetMetahandle();
467 MutableEntry
item3(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "item3");
469 item3
.PutSpecifics(bookmark_specifics
);
470 item3
.PutServerSpecifics(bookmark_specifics
);
471 item3
.PutIsUnsynced(true);
472 handle3
= item3
.GetMetahandle();
474 ASSERT_EQ(OPENED
, SimulateSaveAndReloadDir());
477 { // Test adding and saving delete journals.
478 DeleteJournal
* delete_journal
= dir()->delete_journal();
480 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
481 EntryKernelSet journal_entries
;
482 delete_journal
->GetDeleteJournals(&trans
, BOOKMARKS
, &journal_entries
);
483 ASSERT_EQ(0u, journal_entries
.size());
485 // Set SERVER_IS_DEL of the entries to true and they should be added to
486 // delete journals, but only if the deletion is initiated in update e.g.
487 // IS_UNAPPLIED_UPDATE is also true.
488 MutableEntry
item1(&trans
, GET_BY_ID
, id1
);
489 ASSERT_TRUE(item1
.good());
490 item1
.PutServerIsDel(true);
491 MutableEntry
item2(&trans
, GET_BY_ID
, id2
);
492 ASSERT_TRUE(item2
.good());
493 item2
.PutServerIsDel(true);
494 MutableEntry
item3(&trans
, GET_BY_ID
, id3
);
495 ASSERT_TRUE(item3
.good());
496 item3
.PutServerIsDel(true);
497 // Expect only the first two items to be in the delete journal.
500 EXPECT_TRUE(delete_journal
->delete_journals_
.count(&tmp
));
502 EXPECT_TRUE(delete_journal
->delete_journals_
.count(&tmp
));
504 EXPECT_FALSE(delete_journal
->delete_journals_
.count(&tmp
));
507 // Save delete journals in database and verify memory clearing.
508 ASSERT_TRUE(dir()->SaveChanges());
510 ReadTransaction
trans(FROM_HERE
, dir().get());
511 EXPECT_EQ(0u, delete_journal
->GetDeleteJournalSize(&trans
));
513 ASSERT_EQ(OPENED
, SimulateSaveAndReloadDir());
518 // Test reading delete journals from database.
519 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
520 DeleteJournal
* delete_journal
= dir()->delete_journal();
521 EntryKernelSet journal_entries
;
522 delete_journal
->GetDeleteJournals(&trans
, BOOKMARKS
, &journal_entries
);
523 ASSERT_EQ(2u, journal_entries
.size());
525 tmp
.put(META_HANDLE
, handle1
);
526 EXPECT_TRUE(journal_entries
.count(&tmp
));
527 tmp
.put(META_HANDLE
, handle2
);
528 EXPECT_TRUE(journal_entries
.count(&tmp
));
529 tmp
.put(META_HANDLE
, handle3
);
530 EXPECT_FALSE(journal_entries
.count(&tmp
));
533 MetahandleSet to_purge
;
534 to_purge
.insert(handle2
);
535 delete_journal
->PurgeDeleteJournals(&trans
, to_purge
);
537 // Verify that item2 is purged from journals in memory and will be
538 // purged from database.
540 EXPECT_FALSE(delete_journal
->delete_journals_
.count(&tmp
));
541 EXPECT_EQ(1u, delete_journal
->delete_journals_to_purge_
.size());
542 EXPECT_TRUE(delete_journal
->delete_journals_to_purge_
.count(handle2
));
544 ASSERT_EQ(OPENED
, SimulateSaveAndReloadDir());
549 // Verify purged entry is gone in database.
550 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
551 DeleteJournal
* delete_journal
= dir()->delete_journal();
552 EntryKernelSet journal_entries
;
553 delete_journal
->GetDeleteJournals(&trans
, BOOKMARKS
, &journal_entries
);
554 ASSERT_EQ(1u, journal_entries
.size());
557 tmp
.put(META_HANDLE
, handle1
);
558 EXPECT_TRUE(journal_entries
.count(&tmp
));
560 // Undelete item1 (IS_UNAPPLIED_UPDATE shouldn't matter in this case).
561 MutableEntry
item1(&trans
, GET_BY_ID
, id1
);
562 ASSERT_TRUE(item1
.good());
563 item1
.PutIsUnappliedUpdate(false);
564 item1
.PutServerIsDel(false);
565 EXPECT_TRUE(delete_journal
->delete_journals_
.empty());
566 EXPECT_EQ(1u, delete_journal
->delete_journals_to_purge_
.size());
567 EXPECT_TRUE(delete_journal
->delete_journals_to_purge_
.count(handle1
));
569 ASSERT_EQ(OPENED
, SimulateSaveAndReloadDir());
573 // Verify undeleted entry is gone from database.
574 ReadTransaction
trans(FROM_HERE
, dir().get());
575 DeleteJournal
* delete_journal
= dir()->delete_journal();
576 ASSERT_EQ(0u, delete_journal
->GetDeleteJournalSize(&trans
));
580 TEST_F(SyncableDirectoryTest
, TestPurgeDeletedEntriesOnReload
) {
581 sync_pb::EntitySpecifics specifics
;
582 AddDefaultFieldValue(PREFERENCES
, &specifics
);
584 const int kClientCount
= 2;
585 const int kServerCount
= 5;
586 const int kTestCount
= kClientCount
+ kServerCount
;
587 int64 handles
[kTestCount
];
589 // The idea is to recreate various combinations of IDs, IS_DEL,
590 // IS_UNSYNCED, and IS_UNAPPLIED_UPDATE flags to test all combinations
591 // for DirectoryBackingStore::SafeToPurgeOnLoading.
592 // 0: client ID, IS_DEL, IS_UNSYNCED
593 // 1: client ID, IS_UNSYNCED
594 // 2: server ID, IS_DEL, IS_UNSYNCED, IS_UNAPPLIED_UPDATE
595 // 3: server ID, IS_DEL, IS_UNSYNCED
596 // 4: server ID, IS_DEL, IS_UNAPPLIED_UPDATE
597 // 5: server ID, IS_DEL
600 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
602 for (int i
= 0; i
< kTestCount
; i
++) {
603 std::string name
= base::StringPrintf("item%d", i
);
604 MutableEntry
item(&trans
, CREATE
, PREFERENCES
, trans
.root_id(), name
);
605 ASSERT_TRUE(item
.good());
607 handles
[i
] = item
.GetMetahandle();
609 if (i
< kClientCount
) {
610 item
.PutId(TestIdFactory::FromNumber(i
- kClientCount
));
612 item
.PutId(TestIdFactory::FromNumber(i
));
615 item
.PutUniqueClientTag(name
);
616 item
.PutIsUnsynced(true);
617 item
.PutSpecifics(specifics
);
618 item
.PutServerSpecifics(specifics
);
620 if (i
>= kClientCount
) {
621 item
.PutBaseVersion(10);
622 item
.PutServerVersion(10);
626 if (i
!= 1 && i
!= 6)
630 item
.PutIsUnsynced(false);
632 if (i
== 2 || i
== 4)
633 item
.PutIsUnappliedUpdate(true);
636 ASSERT_EQ(OPENED
, SimulateSaveAndReloadDir());
638 // Expect items 0 and 5 to be purged according to
639 // DirectoryBackingStore::SafeToPurgeOnLoading:
640 // - Item 0 is an item with IS_DEL flag and client ID.
641 // - Item 5 is an item with IS_DEL flag which has both
642 // IS_UNSYNCED and IS_UNAPPLIED_UPDATE unset.
643 std::vector
<int64
> expected_purged
;
644 expected_purged
.push_back(0);
645 expected_purged
.push_back(5);
647 std::vector
<int64
> actually_purged
;
649 ReadTransaction
trans(FROM_HERE
, dir().get());
650 for (int i
= 0; i
< kTestCount
; i
++) {
651 Entry
item(&trans
, GET_BY_HANDLE
, handles
[i
]);
653 actually_purged
.push_back(i
);
658 EXPECT_EQ(expected_purged
, actually_purged
);
661 TEST_F(SyncableDirectoryTest
, TestBasicLookupNonExistantID
) {
662 ReadTransaction
rtrans(FROM_HERE
, dir().get());
663 Entry
e(&rtrans
, GET_BY_ID
, TestIdFactory::FromNumber(-99));
664 ASSERT_FALSE(e
.good());
667 TEST_F(SyncableDirectoryTest
, TestBasicLookupValidID
) {
668 CreateEntry(BOOKMARKS
, "rtc");
669 ReadTransaction
rtrans(FROM_HERE
, dir().get());
670 Entry
e(&rtrans
, GET_BY_ID
, TestIdFactory::FromNumber(-99));
671 ASSERT_TRUE(e
.good());
674 TEST_F(SyncableDirectoryTest
, TestDelete
) {
675 std::string name
= "peanut butter jelly time";
676 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
677 MutableEntry
e1(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), name
);
678 ASSERT_TRUE(e1
.good());
680 MutableEntry
e2(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), name
);
681 ASSERT_TRUE(e2
.good());
683 MutableEntry
e3(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), name
);
684 ASSERT_TRUE(e3
.good());
696 TEST_F(SyncableDirectoryTest
, TestGetUnsynced
) {
697 Directory::Metahandles handles
;
698 int64 handle1
, handle2
;
700 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
702 dir()->GetUnsyncedMetaHandles(&trans
, &handles
);
703 ASSERT_TRUE(0 == handles
.size());
705 MutableEntry
e1(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "abba");
706 ASSERT_TRUE(e1
.good());
707 handle1
= e1
.GetMetahandle();
708 e1
.PutBaseVersion(1);
710 e1
.PutId(TestIdFactory::FromNumber(101));
712 MutableEntry
e2(&trans
, CREATE
, BOOKMARKS
, e1
.GetId(), "bread");
713 ASSERT_TRUE(e2
.good());
714 handle2
= e2
.GetMetahandle();
715 e2
.PutBaseVersion(1);
716 e2
.PutId(TestIdFactory::FromNumber(102));
718 dir()->SaveChanges();
720 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
722 dir()->GetUnsyncedMetaHandles(&trans
, &handles
);
723 ASSERT_TRUE(0 == handles
.size());
725 MutableEntry
e3(&trans
, GET_BY_HANDLE
, handle1
);
726 ASSERT_TRUE(e3
.good());
727 e3
.PutIsUnsynced(true);
729 dir()->SaveChanges();
731 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
732 dir()->GetUnsyncedMetaHandles(&trans
, &handles
);
733 ASSERT_TRUE(1 == handles
.size());
734 ASSERT_TRUE(handle1
== handles
[0]);
736 MutableEntry
e4(&trans
, GET_BY_HANDLE
, handle2
);
737 ASSERT_TRUE(e4
.good());
738 e4
.PutIsUnsynced(true);
740 dir()->SaveChanges();
742 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
743 dir()->GetUnsyncedMetaHandles(&trans
, &handles
);
744 ASSERT_TRUE(2 == handles
.size());
745 if (handle1
== handles
[0]) {
746 ASSERT_TRUE(handle2
== handles
[1]);
748 ASSERT_TRUE(handle2
== handles
[0]);
749 ASSERT_TRUE(handle1
== handles
[1]);
752 MutableEntry
e5(&trans
, GET_BY_HANDLE
, handle1
);
753 ASSERT_TRUE(e5
.good());
754 ASSERT_TRUE(e5
.GetIsUnsynced());
755 ASSERT_TRUE(e5
.PutIsUnsynced(false));
756 ASSERT_FALSE(e5
.GetIsUnsynced());
758 dir()->SaveChanges();
760 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
761 dir()->GetUnsyncedMetaHandles(&trans
, &handles
);
762 ASSERT_TRUE(1 == handles
.size());
763 ASSERT_TRUE(handle2
== handles
[0]);
767 TEST_F(SyncableDirectoryTest
, TestGetUnappliedUpdates
) {
768 std::vector
<int64
> handles
;
769 int64 handle1
, handle2
;
770 const FullModelTypeSet all_types
= FullModelTypeSet::All();
772 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
774 dir()->GetUnappliedUpdateMetaHandles(&trans
, all_types
, &handles
);
775 ASSERT_TRUE(0 == handles
.size());
777 MutableEntry
e1(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "abba");
778 ASSERT_TRUE(e1
.good());
779 handle1
= e1
.GetMetahandle();
780 e1
.PutIsUnappliedUpdate(false);
781 e1
.PutBaseVersion(1);
782 e1
.PutId(TestIdFactory::FromNumber(101));
785 MutableEntry
e2(&trans
, CREATE
, BOOKMARKS
, e1
.GetId(), "bread");
786 ASSERT_TRUE(e2
.good());
787 handle2
= e2
.GetMetahandle();
788 e2
.PutIsUnappliedUpdate(false);
789 e2
.PutBaseVersion(1);
790 e2
.PutId(TestIdFactory::FromNumber(102));
792 dir()->SaveChanges();
794 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
796 dir()->GetUnappliedUpdateMetaHandles(&trans
, all_types
, &handles
);
797 ASSERT_TRUE(0 == handles
.size());
799 MutableEntry
e3(&trans
, GET_BY_HANDLE
, handle1
);
800 ASSERT_TRUE(e3
.good());
801 e3
.PutIsUnappliedUpdate(true);
803 dir()->SaveChanges();
805 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
806 dir()->GetUnappliedUpdateMetaHandles(&trans
, all_types
, &handles
);
807 ASSERT_TRUE(1 == handles
.size());
808 ASSERT_TRUE(handle1
== handles
[0]);
810 MutableEntry
e4(&trans
, GET_BY_HANDLE
, handle2
);
811 ASSERT_TRUE(e4
.good());
812 e4
.PutIsUnappliedUpdate(true);
814 dir()->SaveChanges();
816 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
817 dir()->GetUnappliedUpdateMetaHandles(&trans
, all_types
, &handles
);
818 ASSERT_TRUE(2 == handles
.size());
819 if (handle1
== handles
[0]) {
820 ASSERT_TRUE(handle2
== handles
[1]);
822 ASSERT_TRUE(handle2
== handles
[0]);
823 ASSERT_TRUE(handle1
== handles
[1]);
826 MutableEntry
e5(&trans
, GET_BY_HANDLE
, handle1
);
827 ASSERT_TRUE(e5
.good());
828 e5
.PutIsUnappliedUpdate(false);
830 dir()->SaveChanges();
832 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
833 dir()->GetUnappliedUpdateMetaHandles(&trans
, all_types
, &handles
);
834 ASSERT_TRUE(1 == handles
.size());
835 ASSERT_TRUE(handle2
== handles
[0]);
839 TEST_F(SyncableDirectoryTest
, DeleteBug_531383
) {
840 // Try to evoke a check failure...
841 TestIdFactory id_factory
;
842 int64 grandchild_handle
;
844 WriteTransaction
wtrans(FROM_HERE
, UNITTEST
, dir().get());
845 MutableEntry
parent(&wtrans
, CREATE
, BOOKMARKS
, id_factory
.root(), "Bob");
846 ASSERT_TRUE(parent
.good());
847 parent
.PutIsDir(true);
848 parent
.PutId(id_factory
.NewServerId());
849 parent
.PutBaseVersion(1);
850 MutableEntry
child(&wtrans
, CREATE
, BOOKMARKS
, parent
.GetId(), "Bob");
851 ASSERT_TRUE(child
.good());
852 child
.PutIsDir(true);
853 child
.PutId(id_factory
.NewServerId());
854 child
.PutBaseVersion(1);
855 MutableEntry
grandchild(&wtrans
, CREATE
, BOOKMARKS
, child
.GetId(), "Bob");
856 ASSERT_TRUE(grandchild
.good());
857 grandchild
.PutId(id_factory
.NewServerId());
858 grandchild
.PutBaseVersion(1);
859 grandchild
.PutIsDel(true);
860 MutableEntry
twin(&wtrans
, CREATE
, BOOKMARKS
, child
.GetId(), "Bob");
861 ASSERT_TRUE(twin
.good());
863 grandchild
.PutIsDel(false);
865 grandchild_handle
= grandchild
.GetMetahandle();
867 dir()->SaveChanges();
869 WriteTransaction
wtrans(FROM_HERE
, UNITTEST
, dir().get());
870 MutableEntry
grandchild(&wtrans
, GET_BY_HANDLE
, grandchild_handle
);
871 grandchild
.PutIsDel(true); // Used to CHECK fail here.
875 TEST_F(SyncableDirectoryTest
, TestIsLegalNewParent
) {
876 TestIdFactory id_factory
;
877 WriteTransaction
wtrans(FROM_HERE
, UNITTEST
, dir().get());
878 Entry
root(&wtrans
, GET_BY_ID
, id_factory
.root());
879 ASSERT_TRUE(root
.good());
880 MutableEntry
parent(&wtrans
, CREATE
, BOOKMARKS
, root
.GetId(), "Bob");
881 ASSERT_TRUE(parent
.good());
882 parent
.PutIsDir(true);
883 parent
.PutId(id_factory
.NewServerId());
884 parent
.PutBaseVersion(1);
885 MutableEntry
child(&wtrans
, CREATE
, BOOKMARKS
, parent
.GetId(), "Bob");
886 ASSERT_TRUE(child
.good());
887 child
.PutIsDir(true);
888 child
.PutId(id_factory
.NewServerId());
889 child
.PutBaseVersion(1);
890 MutableEntry
grandchild(&wtrans
, CREATE
, BOOKMARKS
, child
.GetId(), "Bob");
891 ASSERT_TRUE(grandchild
.good());
892 grandchild
.PutId(id_factory
.NewServerId());
893 grandchild
.PutBaseVersion(1);
895 MutableEntry
parent2(&wtrans
, CREATE
, BOOKMARKS
, root
.GetId(), "Pete");
896 ASSERT_TRUE(parent2
.good());
897 parent2
.PutIsDir(true);
898 parent2
.PutId(id_factory
.NewServerId());
899 parent2
.PutBaseVersion(1);
900 MutableEntry
child2(&wtrans
, CREATE
, BOOKMARKS
, parent2
.GetId(), "Pete");
901 ASSERT_TRUE(child2
.good());
902 child2
.PutIsDir(true);
903 child2
.PutId(id_factory
.NewServerId());
904 child2
.PutBaseVersion(1);
905 MutableEntry
grandchild2(&wtrans
, CREATE
, BOOKMARKS
, child2
.GetId(), "Pete");
906 ASSERT_TRUE(grandchild2
.good());
907 grandchild2
.PutId(id_factory
.NewServerId());
908 grandchild2
.PutBaseVersion(1);
916 // grandchild grandchild2
917 ASSERT_TRUE(IsLegalNewParent(child
, root
));
918 ASSERT_TRUE(IsLegalNewParent(child
, parent
));
919 ASSERT_FALSE(IsLegalNewParent(child
, child
));
920 ASSERT_FALSE(IsLegalNewParent(child
, grandchild
));
921 ASSERT_TRUE(IsLegalNewParent(child
, parent2
));
922 ASSERT_TRUE(IsLegalNewParent(child
, grandchild2
));
923 ASSERT_FALSE(IsLegalNewParent(parent
, grandchild
));
924 ASSERT_FALSE(IsLegalNewParent(root
, grandchild
));
925 ASSERT_FALSE(IsLegalNewParent(parent
, grandchild
));
928 TEST_F(SyncableDirectoryTest
, TestEntryIsInFolder
) {
929 // Create a subdir and an entry.
931 syncable::Id folder_id
;
932 syncable::Id entry_id
;
933 std::string entry_name
= "entry";
936 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
937 MutableEntry
folder(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "folder");
938 ASSERT_TRUE(folder
.good());
939 folder
.PutIsDir(true);
940 EXPECT_TRUE(folder
.PutIsUnsynced(true));
941 folder_id
= folder
.GetId();
943 MutableEntry
entry(&trans
, CREATE
, BOOKMARKS
, folder
.GetId(), entry_name
);
944 ASSERT_TRUE(entry
.good());
945 entry_handle
= entry
.GetMetahandle();
946 entry
.PutIsUnsynced(true);
947 entry_id
= entry
.GetId();
950 // Make sure we can find the entry in the folder.
952 ReadTransaction
trans(FROM_HERE
, dir().get());
953 EXPECT_EQ(0, CountEntriesWithName(&trans
, trans
.root_id(), entry_name
));
954 EXPECT_EQ(1, CountEntriesWithName(&trans
, folder_id
, entry_name
));
956 Entry
entry(&trans
, GET_BY_ID
, entry_id
);
957 ASSERT_TRUE(entry
.good());
958 EXPECT_EQ(entry_handle
, entry
.GetMetahandle());
959 EXPECT_TRUE(entry
.GetNonUniqueName() == entry_name
);
960 EXPECT_TRUE(entry
.GetParentId() == folder_id
);
964 TEST_F(SyncableDirectoryTest
, TestParentIdIndexUpdate
) {
965 std::string child_name
= "child";
967 WriteTransaction
wt(FROM_HERE
, UNITTEST
, dir().get());
968 MutableEntry
parent_folder(&wt
, CREATE
, BOOKMARKS
, wt
.root_id(), "folder1");
969 parent_folder
.PutIsUnsynced(true);
970 parent_folder
.PutIsDir(true);
972 MutableEntry
parent_folder2(&wt
, CREATE
, BOOKMARKS
, wt
.root_id(), "folder2");
973 parent_folder2
.PutIsUnsynced(true);
974 parent_folder2
.PutIsDir(true);
976 MutableEntry
child(&wt
, CREATE
, BOOKMARKS
, parent_folder
.GetId(), child_name
);
977 child
.PutIsDir(true);
978 child
.PutIsUnsynced(true);
980 ASSERT_TRUE(child
.good());
982 EXPECT_EQ(0, CountEntriesWithName(&wt
, wt
.root_id(), child_name
));
983 EXPECT_EQ(parent_folder
.GetId(), child
.GetParentId());
984 EXPECT_EQ(1, CountEntriesWithName(&wt
, parent_folder
.GetId(), child_name
));
985 EXPECT_EQ(0, CountEntriesWithName(&wt
, parent_folder2
.GetId(), child_name
));
986 child
.PutParentId(parent_folder2
.GetId());
987 EXPECT_EQ(parent_folder2
.GetId(), child
.GetParentId());
988 EXPECT_EQ(0, CountEntriesWithName(&wt
, parent_folder
.GetId(), child_name
));
989 EXPECT_EQ(1, CountEntriesWithName(&wt
, parent_folder2
.GetId(), child_name
));
992 TEST_F(SyncableDirectoryTest
, TestNoReindexDeletedItems
) {
993 std::string folder_name
= "folder";
994 std::string new_name
= "new_name";
996 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
997 MutableEntry
folder(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), folder_name
);
998 ASSERT_TRUE(folder
.good());
999 folder
.PutIsDir(true);
1000 folder
.PutIsDel(true);
1002 EXPECT_EQ(0, CountEntriesWithName(&trans
, trans
.root_id(), folder_name
));
1004 MutableEntry
deleted(&trans
, GET_BY_ID
, folder
.GetId());
1005 ASSERT_TRUE(deleted
.good());
1006 deleted
.PutParentId(trans
.root_id());
1007 deleted
.PutNonUniqueName(new_name
);
1009 EXPECT_EQ(0, CountEntriesWithName(&trans
, trans
.root_id(), folder_name
));
1010 EXPECT_EQ(0, CountEntriesWithName(&trans
, trans
.root_id(), new_name
));
1013 TEST_F(SyncableDirectoryTest
, TestCaseChangeRename
) {
1014 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1015 MutableEntry
folder(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "CaseChange");
1016 ASSERT_TRUE(folder
.good());
1017 folder
.PutParentId(trans
.root_id());
1018 folder
.PutNonUniqueName("CASECHANGE");
1019 folder
.PutIsDel(true);
1022 // Create items of each model type, and check that GetModelType and
1023 // GetServerModelType return the right value.
1024 TEST_F(SyncableDirectoryTest
, GetModelType
) {
1025 TestIdFactory id_factory
;
1026 ModelTypeSet protocol_types
= ProtocolTypes();
1027 for (ModelTypeSet::Iterator iter
= protocol_types
.First(); iter
.Good();
1029 ModelType datatype
= iter
.Get();
1030 SCOPED_TRACE(testing::Message("Testing model type ") << datatype
);
1033 case TOP_LEVEL_FOLDER
:
1034 continue; // Datatype isn't a function of Specifics.
1038 sync_pb::EntitySpecifics specifics
;
1039 AddDefaultFieldValue(datatype
, &specifics
);
1041 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1043 MutableEntry
folder(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "Folder");
1044 ASSERT_TRUE(folder
.good());
1045 folder
.PutId(id_factory
.NewServerId());
1046 folder
.PutSpecifics(specifics
);
1047 folder
.PutBaseVersion(1);
1048 folder
.PutIsDir(true);
1049 folder
.PutIsDel(false);
1050 ASSERT_EQ(datatype
, folder
.GetModelType());
1052 MutableEntry
item(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "Item");
1053 ASSERT_TRUE(item
.good());
1054 item
.PutId(id_factory
.NewServerId());
1055 item
.PutSpecifics(specifics
);
1056 item
.PutBaseVersion(1);
1057 item
.PutIsDir(false);
1058 item
.PutIsDel(false);
1059 ASSERT_EQ(datatype
, item
.GetModelType());
1061 // It's critical that deletion records retain their datatype, so that
1062 // they can be dispatched to the appropriate change processor.
1063 MutableEntry
deleted_item(
1064 &trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "Deleted Item");
1065 ASSERT_TRUE(item
.good());
1066 deleted_item
.PutId(id_factory
.NewServerId());
1067 deleted_item
.PutSpecifics(specifics
);
1068 deleted_item
.PutBaseVersion(1);
1069 deleted_item
.PutIsDir(false);
1070 deleted_item
.PutIsDel(true);
1071 ASSERT_EQ(datatype
, deleted_item
.GetModelType());
1073 MutableEntry
server_folder(
1074 &trans
, CREATE_NEW_UPDATE_ITEM
, id_factory
.NewServerId());
1075 ASSERT_TRUE(server_folder
.good());
1076 server_folder
.PutServerSpecifics(specifics
);
1077 server_folder
.PutBaseVersion(1);
1078 server_folder
.PutServerIsDir(true);
1079 server_folder
.PutServerIsDel(false);
1080 ASSERT_EQ(datatype
, server_folder
.GetServerModelType());
1082 MutableEntry
server_item(
1083 &trans
, CREATE_NEW_UPDATE_ITEM
, id_factory
.NewServerId());
1084 ASSERT_TRUE(server_item
.good());
1085 server_item
.PutServerSpecifics(specifics
);
1086 server_item
.PutBaseVersion(1);
1087 server_item
.PutServerIsDir(false);
1088 server_item
.PutServerIsDel(false);
1089 ASSERT_EQ(datatype
, server_item
.GetServerModelType());
1091 sync_pb::SyncEntity folder_entity
;
1092 folder_entity
.set_id_string(SyncableIdToProto(id_factory
.NewServerId()));
1093 folder_entity
.set_deleted(false);
1094 folder_entity
.set_folder(true);
1095 folder_entity
.mutable_specifics()->CopyFrom(specifics
);
1096 ASSERT_EQ(datatype
, GetModelType(folder_entity
));
1098 sync_pb::SyncEntity item_entity
;
1099 item_entity
.set_id_string(SyncableIdToProto(id_factory
.NewServerId()));
1100 item_entity
.set_deleted(false);
1101 item_entity
.set_folder(false);
1102 item_entity
.mutable_specifics()->CopyFrom(specifics
);
1103 ASSERT_EQ(datatype
, GetModelType(item_entity
));
1107 // A test that roughly mimics the directory interaction that occurs when a
1108 // bookmark folder and entry are created then synced for the first time. It is
1109 // a more common variant of the 'DeletedAndUnsyncedChild' scenario tested below.
1110 TEST_F(SyncableDirectoryTest
, ChangeEntryIDAndUpdateChildren_ParentAndChild
) {
1111 TestIdFactory id_factory
;
1116 // Create two client-side items, a parent and child.
1117 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1119 MutableEntry
parent(&trans
, CREATE
, BOOKMARKS
, id_factory
.root(), "parent");
1120 parent
.PutIsDir(true);
1121 parent
.PutIsUnsynced(true);
1123 MutableEntry
child(&trans
, CREATE
, BOOKMARKS
, parent
.GetId(), "child");
1124 child
.PutIsUnsynced(true);
1126 orig_parent_id
= parent
.GetId();
1127 orig_child_id
= child
.GetId();
1131 // Simulate what happens after committing two items. Their IDs will be
1132 // replaced with server IDs. The child is renamed first, then the parent.
1133 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1135 MutableEntry
parent(&trans
, GET_BY_ID
, orig_parent_id
);
1136 MutableEntry
child(&trans
, GET_BY_ID
, orig_child_id
);
1138 ChangeEntryIDAndUpdateChildren(&trans
, &child
, id_factory
.NewServerId());
1139 child
.PutIsUnsynced(false);
1140 child
.PutBaseVersion(1);
1141 child
.PutServerVersion(1);
1143 ChangeEntryIDAndUpdateChildren(&trans
, &parent
, id_factory
.NewServerId());
1144 parent
.PutIsUnsynced(false);
1145 parent
.PutBaseVersion(1);
1146 parent
.PutServerVersion(1);
1149 // Final check for validity.
1150 EXPECT_EQ(OPENED
, SimulateSaveAndReloadDir());
1153 // A test that roughly mimics the directory interaction that occurs when a
1154 // type root folder is created locally and then re-created (updated) from the
1156 TEST_F(SyncableDirectoryTest
, ChangeEntryIDAndUpdateChildren_ImplicitParent
) {
1157 TestIdFactory id_factory
;
1162 // Create two client-side items, a parent and child.
1163 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1165 MutableEntry
parent(&trans
, CREATE
, PREFERENCES
, id_factory
.root(),
1167 parent
.PutIsDir(true);
1168 parent
.PutIsUnsynced(true);
1170 // The child has unset parent ID. The parent is inferred from the type.
1171 MutableEntry
child(&trans
, CREATE
, PREFERENCES
, "child");
1172 child
.PutIsUnsynced(true);
1174 orig_parent_id
= parent
.GetId();
1175 child_id
= child
.GetId();
1179 // Simulate what happens after committing two items. Their IDs will be
1180 // replaced with server IDs. The child is renamed first, then the parent.
1181 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1183 MutableEntry
parent(&trans
, GET_BY_ID
, orig_parent_id
);
1185 ChangeEntryIDAndUpdateChildren(&trans
, &parent
, id_factory
.NewServerId());
1186 parent
.PutIsUnsynced(false);
1187 parent
.PutBaseVersion(1);
1188 parent
.PutServerVersion(1);
1191 // Final check for validity.
1192 EXPECT_EQ(OPENED
, SimulateSaveAndReloadDir());
1194 // Verify that child's PARENT_ID hasn't been updated.
1196 ReadTransaction
trans(FROM_HERE
, dir().get());
1197 Entry
child(&trans
, GET_BY_ID
, child_id
);
1198 EXPECT_TRUE(child
.good());
1199 EXPECT_TRUE(child
.GetParentId().IsNull());
1203 // A test based on the scenario where we create a bookmark folder and entry
1204 // locally, but with a twist. In this case, the bookmark is deleted before we
1205 // are able to sync either it or its parent folder. This scenario used to cause
1206 // directory corruption, see crbug.com/125381.
1207 TEST_F(SyncableDirectoryTest
,
1208 ChangeEntryIDAndUpdateChildren_DeletedAndUnsyncedChild
) {
1209 TestIdFactory id_factory
;
1214 // Create two client-side items, a parent and child.
1215 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1217 MutableEntry
parent(&trans
, CREATE
, BOOKMARKS
, id_factory
.root(), "parent");
1218 parent
.PutIsDir(true);
1219 parent
.PutIsUnsynced(true);
1221 MutableEntry
child(&trans
, CREATE
, BOOKMARKS
, parent
.GetId(), "child");
1222 child
.PutIsUnsynced(true);
1224 orig_parent_id
= parent
.GetId();
1225 orig_child_id
= child
.GetId();
1229 // Delete the child.
1230 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1232 MutableEntry
child(&trans
, GET_BY_ID
, orig_child_id
);
1233 child
.PutIsDel(true);
1237 // Simulate what happens after committing the parent. Its ID will be
1238 // replaced with server a ID.
1239 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1241 MutableEntry
parent(&trans
, GET_BY_ID
, orig_parent_id
);
1243 ChangeEntryIDAndUpdateChildren(&trans
, &parent
, id_factory
.NewServerId());
1244 parent
.PutIsUnsynced(false);
1245 parent
.PutBaseVersion(1);
1246 parent
.PutServerVersion(1);
1249 // Final check for validity.
1250 EXPECT_EQ(OPENED
, SimulateSaveAndReloadDir());
1253 // Ask the directory to generate a unique ID. Close and re-open the database
1254 // without saving, then ask for another unique ID. Verify IDs are not reused.
1255 // This scenario simulates a crash within the first few seconds of operation.
1256 TEST_F(SyncableDirectoryTest
, LocalIdReuseTest
) {
1257 Id pre_crash_id
= dir()->NextId();
1258 SimulateCrashAndReloadDir();
1259 Id post_crash_id
= dir()->NextId();
1260 EXPECT_NE(pre_crash_id
, post_crash_id
);
1263 // Ask the directory to generate a unique ID. Save the directory. Close and
1264 // re-open the database without saving, then ask for another unique ID. Verify
1265 // IDs are not reused. This scenario simulates a steady-state crash.
1266 TEST_F(SyncableDirectoryTest
, LocalIdReuseTestWithSave
) {
1267 Id pre_crash_id
= dir()->NextId();
1268 dir()->SaveChanges();
1269 SimulateCrashAndReloadDir();
1270 Id post_crash_id
= dir()->NextId();
1271 EXPECT_NE(pre_crash_id
, post_crash_id
);
1274 // Ensure that the unsynced, is_del and server unkown entries that may have been
1275 // left in the database by old clients will be deleted when we open the old
1277 TEST_F(SyncableDirectoryTest
, OldClientLeftUnsyncedDeletedLocalItem
) {
1278 // We must create an entry with the offending properties. This is done with
1279 // some abuse of the MutableEntry's API; it doesn't expect us to modify an
1280 // item after it is deleted. If this hack becomes impractical we will need to
1281 // find a new way to simulate this scenario.
1283 TestIdFactory id_factory
;
1285 // Happy-path: These valid entries should not get deleted.
1286 Id server_knows_id
= id_factory
.NewServerId();
1287 Id not_is_del_id
= id_factory
.NewLocalId();
1289 // The ID of the entry which will be unsynced, is_del and !ServerKnows().
1290 Id zombie_id
= id_factory
.NewLocalId();
1292 // We're about to do some bad things. Tell the directory verification
1293 // routines to look the other way.
1294 dir()->SetInvariantCheckLevel(OFF
);
1297 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1299 // Create an uncommitted tombstone entry.
1300 MutableEntry
server_knows(
1301 &trans
, CREATE
, BOOKMARKS
, id_factory
.root(), "server_knows");
1302 server_knows
.PutId(server_knows_id
);
1303 server_knows
.PutIsUnsynced(true);
1304 server_knows
.PutIsDel(true);
1305 server_knows
.PutBaseVersion(5);
1306 server_knows
.PutServerVersion(4);
1308 // Create a valid update entry.
1309 MutableEntry
not_is_del(
1310 &trans
, CREATE
, BOOKMARKS
, id_factory
.root(), "not_is_del");
1311 not_is_del
.PutId(not_is_del_id
);
1312 not_is_del
.PutIsDel(false);
1313 not_is_del
.PutIsUnsynced(true);
1315 // Create a tombstone which should never be sent to the server because the
1316 // server never knew about the item's existence.
1318 // New clients should never put entries into this state. We work around
1319 // this by setting IS_DEL before setting IS_UNSYNCED, something which the
1320 // client should never do in practice.
1321 MutableEntry
zombie(&trans
, CREATE
, BOOKMARKS
, id_factory
.root(), "zombie");
1322 zombie
.PutId(zombie_id
);
1323 zombie
.PutIsDel(true);
1324 zombie
.PutIsUnsynced(true);
1327 ASSERT_EQ(OPENED
, SimulateSaveAndReloadDir());
1330 ReadTransaction
trans(FROM_HERE
, dir().get());
1332 // The directory loading routines should have cleaned things up, making it
1333 // safe to check invariants once again.
1334 dir()->FullyCheckTreeInvariants(&trans
);
1336 Entry
server_knows(&trans
, GET_BY_ID
, server_knows_id
);
1337 EXPECT_TRUE(server_knows
.good());
1339 Entry
not_is_del(&trans
, GET_BY_ID
, not_is_del_id
);
1340 EXPECT_TRUE(not_is_del
.good());
1342 Entry
zombie(&trans
, GET_BY_ID
, zombie_id
);
1343 EXPECT_FALSE(zombie
.good());
1347 TEST_F(SyncableDirectoryTest
, PositionWithNullSurvivesSaveAndReload
) {
1348 TestIdFactory id_factory
;
1350 const char null_cstr
[] = "\0null\0test";
1351 std::string
null_str(null_cstr
, arraysize(null_cstr
) - 1);
1352 // Pad up to the minimum length with 0x7f characters, then add a string that
1353 // contains a few NULLs to the end. This is slightly wrong, since the suffix
1354 // part of a UniquePosition shouldn't contain NULLs, but it's good enough for
1356 std::string suffix
=
1357 std::string(UniquePosition::kSuffixLength
- null_str
.length(), '\x7f') +
1359 UniquePosition null_pos
= UniquePosition::FromInt64(10, suffix
);
1362 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1364 MutableEntry
parent(&trans
, CREATE
, BOOKMARKS
, id_factory
.root(), "parent");
1365 parent
.PutIsDir(true);
1366 parent
.PutIsUnsynced(true);
1368 MutableEntry
child(&trans
, CREATE
, BOOKMARKS
, parent
.GetId(), "child");
1369 child
.PutIsUnsynced(true);
1370 child
.PutUniquePosition(null_pos
);
1371 child
.PutServerUniquePosition(null_pos
);
1373 null_child_id
= child
.GetId();
1376 EXPECT_EQ(OPENED
, SimulateSaveAndReloadDir());
1379 ReadTransaction
trans(FROM_HERE
, dir().get());
1381 Entry
null_ordinal_child(&trans
, GET_BY_ID
, null_child_id
);
1382 EXPECT_TRUE(null_pos
.Equals(null_ordinal_child
.GetUniquePosition()));
1383 EXPECT_TRUE(null_pos
.Equals(null_ordinal_child
.GetServerUniquePosition()));
1387 // Any item with BOOKMARKS in their local specifics should have a valid local
1388 // unique position. If there is an item in the loaded DB that does not match
1389 // this criteria, we consider the whole DB to be corrupt.
1390 TEST_F(SyncableDirectoryTest
, BadPositionCountsAsCorruption
) {
1391 TestIdFactory id_factory
;
1394 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1396 MutableEntry
parent(&trans
, CREATE
, BOOKMARKS
, id_factory
.root(), "parent");
1397 parent
.PutIsDir(true);
1398 parent
.PutIsUnsynced(true);
1400 // The code is littered with DCHECKs that try to stop us from doing what
1401 // we're about to do. Our work-around is to create a bookmark based on
1402 // a server update, then update its local specifics without updating its
1403 // local unique position.
1406 &trans
, CREATE_NEW_UPDATE_ITEM
, id_factory
.MakeServer("child"));
1407 sync_pb::EntitySpecifics specifics
;
1408 AddDefaultFieldValue(BOOKMARKS
, &specifics
);
1409 child
.PutIsUnappliedUpdate(true);
1410 child
.PutSpecifics(specifics
);
1412 EXPECT_TRUE(child
.ShouldMaintainPosition());
1413 EXPECT_TRUE(!child
.GetUniquePosition().IsValid());
1416 EXPECT_EQ(FAILED_DATABASE_CORRUPT
, SimulateSaveAndReloadDir());
1419 TEST_F(SyncableDirectoryTest
, General
) {
1420 int64 written_metahandle
;
1421 const Id id
= TestIdFactory::FromNumber(99);
1422 std::string name
= "Jeff";
1423 // Test simple read operations on an empty DB.
1425 ReadTransaction
rtrans(FROM_HERE
, dir().get());
1426 Entry
e(&rtrans
, GET_BY_ID
, id
);
1427 ASSERT_FALSE(e
.good()); // Hasn't been written yet.
1429 Directory::Metahandles child_handles
;
1430 dir()->GetChildHandlesById(&rtrans
, rtrans
.root_id(), &child_handles
);
1431 EXPECT_TRUE(child_handles
.empty());
1434 // Test creating a new meta entry.
1436 WriteTransaction
wtrans(FROM_HERE
, UNITTEST
, dir().get());
1437 MutableEntry
me(&wtrans
, CREATE
, BOOKMARKS
, wtrans
.root_id(), name
);
1438 ASSERT_TRUE(me
.good());
1440 me
.PutBaseVersion(1);
1441 written_metahandle
= me
.GetMetahandle();
1444 // Test GetChildHandles* after something is now in the DB.
1445 // Also check that GET_BY_ID works.
1447 ReadTransaction
rtrans(FROM_HERE
, dir().get());
1448 Entry
e(&rtrans
, GET_BY_ID
, id
);
1449 ASSERT_TRUE(e
.good());
1451 Directory::Metahandles child_handles
;
1452 dir()->GetChildHandlesById(&rtrans
, rtrans
.root_id(), &child_handles
);
1453 EXPECT_EQ(1u, child_handles
.size());
1455 for (Directory::Metahandles::iterator i
= child_handles
.begin();
1456 i
!= child_handles
.end(); ++i
) {
1457 EXPECT_EQ(*i
, written_metahandle
);
1461 // Test writing data to an entity. Also check that GET_BY_HANDLE works.
1462 static const char s
[] = "Hello World.";
1464 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1465 MutableEntry
e(&trans
, GET_BY_HANDLE
, written_metahandle
);
1466 ASSERT_TRUE(e
.good());
1467 PutDataAsBookmarkFavicon(&trans
, &e
, s
, sizeof(s
));
1470 // Test reading back the contents that we just wrote.
1472 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1473 MutableEntry
e(&trans
, GET_BY_HANDLE
, written_metahandle
);
1474 ASSERT_TRUE(e
.good());
1475 ExpectDataFromBookmarkFaviconEquals(&trans
, &e
, s
, sizeof(s
));
1478 // Verify it exists in the folder.
1480 ReadTransaction
rtrans(FROM_HERE
, dir().get());
1481 EXPECT_EQ(1, CountEntriesWithName(&rtrans
, rtrans
.root_id(), name
));
1486 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1487 MutableEntry
e(&trans
, GET_BY_HANDLE
, written_metahandle
);
1490 EXPECT_EQ(0, CountEntriesWithName(&trans
, trans
.root_id(), name
));
1493 dir()->SaveChanges();
1496 TEST_F(SyncableDirectoryTest
, ChildrenOps
) {
1497 int64 written_metahandle
;
1498 const Id id
= TestIdFactory::FromNumber(99);
1499 std::string name
= "Jeff";
1501 ReadTransaction
rtrans(FROM_HERE
, dir().get());
1502 Entry
e(&rtrans
, GET_BY_ID
, id
);
1503 ASSERT_FALSE(e
.good()); // Hasn't been written yet.
1505 Entry
root(&rtrans
, GET_BY_ID
, rtrans
.root_id());
1506 ASSERT_TRUE(root
.good());
1507 EXPECT_FALSE(dir()->HasChildren(&rtrans
, rtrans
.root_id()));
1508 EXPECT_TRUE(root
.GetFirstChildId().IsNull());
1512 WriteTransaction
wtrans(FROM_HERE
, UNITTEST
, dir().get());
1513 MutableEntry
me(&wtrans
, CREATE
, BOOKMARKS
, wtrans
.root_id(), name
);
1514 ASSERT_TRUE(me
.good());
1516 me
.PutBaseVersion(1);
1517 written_metahandle
= me
.GetMetahandle();
1520 // Test children ops after something is now in the DB.
1522 ReadTransaction
rtrans(FROM_HERE
, dir().get());
1523 Entry
e(&rtrans
, GET_BY_ID
, id
);
1524 ASSERT_TRUE(e
.good());
1526 Entry
child(&rtrans
, GET_BY_HANDLE
, written_metahandle
);
1527 ASSERT_TRUE(child
.good());
1529 Entry
root(&rtrans
, GET_BY_ID
, rtrans
.root_id());
1530 ASSERT_TRUE(root
.good());
1531 EXPECT_TRUE(dir()->HasChildren(&rtrans
, rtrans
.root_id()));
1532 EXPECT_EQ(e
.GetId(), root
.GetFirstChildId());
1536 WriteTransaction
wtrans(FROM_HERE
, UNITTEST
, dir().get());
1537 MutableEntry
me(&wtrans
, GET_BY_HANDLE
, written_metahandle
);
1538 ASSERT_TRUE(me
.good());
1542 // Test children ops after the children have been deleted.
1544 ReadTransaction
rtrans(FROM_HERE
, dir().get());
1545 Entry
e(&rtrans
, GET_BY_ID
, id
);
1546 ASSERT_TRUE(e
.good());
1548 Entry
root(&rtrans
, GET_BY_ID
, rtrans
.root_id());
1549 ASSERT_TRUE(root
.good());
1550 EXPECT_FALSE(dir()->HasChildren(&rtrans
, rtrans
.root_id()));
1551 EXPECT_TRUE(root
.GetFirstChildId().IsNull());
1554 dir()->SaveChanges();
1557 TEST_F(SyncableDirectoryTest
, ClientIndexRebuildsProperly
) {
1558 int64 written_metahandle
;
1559 TestIdFactory factory
;
1560 const Id id
= factory
.NewServerId();
1561 std::string name
= "cheesepuffs";
1562 std::string tag
= "dietcoke";
1564 // Test creating a new meta entry.
1566 WriteTransaction
wtrans(FROM_HERE
, UNITTEST
, dir().get());
1567 MutableEntry
me(&wtrans
, CREATE
, BOOKMARKS
, wtrans
.root_id(), name
);
1568 ASSERT_TRUE(me
.good());
1570 me
.PutBaseVersion(1);
1571 me
.PutUniqueClientTag(tag
);
1572 written_metahandle
= me
.GetMetahandle();
1574 dir()->SaveChanges();
1576 // Close and reopen, causing index regeneration.
1579 ReadTransaction
trans(FROM_HERE
, dir().get());
1580 Entry
me(&trans
, GET_BY_CLIENT_TAG
, tag
);
1581 ASSERT_TRUE(me
.good());
1582 EXPECT_EQ(me
.GetId(), id
);
1583 EXPECT_EQ(me
.GetBaseVersion(), 1);
1584 EXPECT_EQ(me
.GetUniqueClientTag(), tag
);
1585 EXPECT_EQ(me
.GetMetahandle(), written_metahandle
);
1589 TEST_F(SyncableDirectoryTest
, ClientIndexRebuildsDeletedProperly
) {
1590 TestIdFactory factory
;
1591 const Id id
= factory
.NewServerId();
1592 std::string tag
= "dietcoke";
1594 // Test creating a deleted, unsynced, server meta entry.
1596 WriteTransaction
wtrans(FROM_HERE
, UNITTEST
, dir().get());
1597 MutableEntry
me(&wtrans
, CREATE
, BOOKMARKS
, wtrans
.root_id(), "deleted");
1598 ASSERT_TRUE(me
.good());
1600 me
.PutBaseVersion(1);
1601 me
.PutUniqueClientTag(tag
);
1603 me
.PutIsUnsynced(true); // Or it might be purged.
1605 dir()->SaveChanges();
1607 // Close and reopen, causing index regeneration.
1610 ReadTransaction
trans(FROM_HERE
, dir().get());
1611 Entry
me(&trans
, GET_BY_CLIENT_TAG
, tag
);
1612 // Should still be present and valid in the client tag index.
1613 ASSERT_TRUE(me
.good());
1614 EXPECT_EQ(me
.GetId(), id
);
1615 EXPECT_EQ(me
.GetUniqueClientTag(), tag
);
1616 EXPECT_TRUE(me
.GetIsDel());
1617 EXPECT_TRUE(me
.GetIsUnsynced());
1621 TEST_F(SyncableDirectoryTest
, ToValue
) {
1622 const Id id
= TestIdFactory::FromNumber(99);
1624 ReadTransaction
rtrans(FROM_HERE
, dir().get());
1625 Entry
e(&rtrans
, GET_BY_ID
, id
);
1626 EXPECT_FALSE(e
.good()); // Hasn't been written yet.
1628 scoped_ptr
<base::DictionaryValue
> value(e
.ToValue(NULL
));
1629 ExpectDictBooleanValue(false, *value
, "good");
1630 EXPECT_EQ(1u, value
->size());
1633 // Test creating a new meta entry.
1635 WriteTransaction
wtrans(FROM_HERE
, UNITTEST
, dir().get());
1636 MutableEntry
me(&wtrans
, CREATE
, BOOKMARKS
, wtrans
.root_id(), "new");
1637 ASSERT_TRUE(me
.good());
1639 me
.PutBaseVersion(1);
1641 scoped_ptr
<base::DictionaryValue
> value(me
.ToValue(NULL
));
1642 ExpectDictBooleanValue(true, *value
, "good");
1643 EXPECT_TRUE(value
->HasKey("kernel"));
1644 ExpectDictStringValue("Bookmarks", *value
, "modelType");
1645 ExpectDictBooleanValue(true, *value
, "existsOnClientBecauseNameIsNonEmpty");
1646 ExpectDictBooleanValue(false, *value
, "isRoot");
1649 dir()->SaveChanges();
1652 // A thread that creates a bunch of directory entries.
1653 class StressTransactionsDelegate
: public base::PlatformThread::Delegate
{
1655 StressTransactionsDelegate(Directory
* dir
, int thread_number
)
1656 : dir_(dir
), thread_number_(thread_number
) {}
1659 Directory
* const dir_
;
1660 const int thread_number_
;
1662 // PlatformThread::Delegate methods:
1663 void ThreadMain() override
{
1664 int entry_count
= 0;
1665 std::string path_name
;
1667 for (int i
= 0; i
< 20; ++i
) {
1668 const int rand_action
= rand() % 10;
1669 if (rand_action
< 4 && !path_name
.empty()) {
1670 ReadTransaction
trans(FROM_HERE
, dir_
);
1671 CHECK(1 == CountEntriesWithName(&trans
, trans
.root_id(), path_name
));
1672 base::PlatformThread::Sleep(
1673 base::TimeDelta::FromMilliseconds(rand() % 10));
1675 std::string unique_name
=
1676 base::StringPrintf("%d.%d", thread_number_
, entry_count
++);
1677 path_name
.assign(unique_name
.begin(), unique_name
.end());
1678 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir_
);
1679 MutableEntry
e(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), path_name
);
1681 base::PlatformThread::Sleep(
1682 base::TimeDelta::FromMilliseconds(rand() % 20));
1683 e
.PutIsUnsynced(true);
1684 if (e
.PutId(TestIdFactory::FromNumber(rand())) &&
1685 e
.GetId().ServerKnows() && !e
.GetId().IsRoot()) {
1686 e
.PutBaseVersion(1);
1692 DISALLOW_COPY_AND_ASSIGN(StressTransactionsDelegate
);
1695 // Stress test Directory by accessing it from several threads concurrently.
1696 TEST_F(SyncableDirectoryTest
, StressTransactions
) {
1697 const int kThreadCount
= 7;
1698 base::PlatformThreadHandle threads
[kThreadCount
];
1699 scoped_ptr
<StressTransactionsDelegate
> thread_delegates
[kThreadCount
];
1701 for (int i
= 0; i
< kThreadCount
; ++i
) {
1702 thread_delegates
[i
].reset(new StressTransactionsDelegate(dir().get(), i
));
1703 ASSERT_TRUE(base::PlatformThread::Create(
1704 0, thread_delegates
[i
].get(), &threads
[i
]));
1707 for (int i
= 0; i
< kThreadCount
; ++i
) {
1708 base::PlatformThread::Join(threads
[i
]);
1712 // Verify that Directory is notifed when a MutableEntry's AttachmentMetadata
1714 TEST_F(SyncableDirectoryTest
, MutableEntry_PutAttachmentMetadata
) {
1715 sync_pb::AttachmentMetadata attachment_metadata
;
1716 sync_pb::AttachmentMetadataRecord
* record
= attachment_metadata
.add_record();
1717 sync_pb::AttachmentIdProto attachment_id_proto
=
1718 syncer::CreateAttachmentIdProto(0, 0);
1719 *record
->mutable_id() = attachment_id_proto
;
1720 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto
));
1722 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1724 // Create an entry with attachment metadata and see that the attachment id
1727 &trans
, CREATE
, PREFERENCES
, trans
.root_id(), "some entry");
1728 entry
.PutId(TestIdFactory::FromNumber(-1));
1729 entry
.PutIsUnsynced(true);
1731 Directory::Metahandles metahandles
;
1732 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto
));
1733 dir()->GetMetahandlesByAttachmentId(
1734 &trans
, attachment_id_proto
, &metahandles
);
1735 ASSERT_TRUE(metahandles
.empty());
1737 // Now add the attachment metadata and see that Directory believes it is
1739 entry
.PutAttachmentMetadata(attachment_metadata
);
1740 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto
));
1741 dir()->GetMetahandlesByAttachmentId(
1742 &trans
, attachment_id_proto
, &metahandles
);
1743 ASSERT_FALSE(metahandles
.empty());
1744 ASSERT_EQ(metahandles
[0], entry
.GetMetahandle());
1746 // Clear out the attachment metadata and see that it's no longer linked.
1747 sync_pb::AttachmentMetadata empty_attachment_metadata
;
1748 entry
.PutAttachmentMetadata(empty_attachment_metadata
);
1749 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto
));
1750 dir()->GetMetahandlesByAttachmentId(
1751 &trans
, attachment_id_proto
, &metahandles
);
1752 ASSERT_TRUE(metahandles
.empty());
1754 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto
));
1757 // Verify that UpdateAttachmentId updates attachment_id and is_on_server flag.
1758 TEST_F(SyncableDirectoryTest
, MutableEntry_UpdateAttachmentId
) {
1759 sync_pb::AttachmentMetadata attachment_metadata
;
1760 sync_pb::AttachmentMetadataRecord
* r1
= attachment_metadata
.add_record();
1761 sync_pb::AttachmentMetadataRecord
* r2
= attachment_metadata
.add_record();
1762 *r1
->mutable_id() = syncer::CreateAttachmentIdProto(0, 0);
1763 *r2
->mutable_id() = syncer::CreateAttachmentIdProto(0, 0);
1764 sync_pb::AttachmentIdProto attachment_id_proto
= r1
->id();
1766 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1769 &trans
, CREATE
, PREFERENCES
, trans
.root_id(), "some entry");
1770 entry
.PutId(TestIdFactory::FromNumber(-1));
1771 entry
.PutAttachmentMetadata(attachment_metadata
);
1774 const sync_pb::AttachmentMetadata
& entry_metadata
=
1775 entry
.GetAttachmentMetadata();
1776 ASSERT_EQ(2, entry_metadata
.record_size());
1777 ASSERT_FALSE(entry_metadata
.record(0).is_on_server());
1778 ASSERT_FALSE(entry_metadata
.record(1).is_on_server());
1779 ASSERT_FALSE(entry
.GetIsUnsynced());
1782 entry
.MarkAttachmentAsOnServer(attachment_id_proto
);
1785 // Re-get entry_metadata because it is immutable in the directory and
1786 // entry_metadata reference has been made invalid by
1787 // MarkAttachmentAsOnServer call above.
1788 const sync_pb::AttachmentMetadata
& entry_metadata
=
1789 entry
.GetAttachmentMetadata();
1790 ASSERT_TRUE(entry_metadata
.record(0).is_on_server());
1791 ASSERT_FALSE(entry_metadata
.record(1).is_on_server());
1792 ASSERT_TRUE(entry
.GetIsUnsynced());
1796 // Verify that deleted entries with attachments will retain the attachments.
1797 TEST_F(SyncableDirectoryTest
, Directory_DeleteDoesNotUnlinkAttachments
) {
1798 sync_pb::AttachmentMetadata attachment_metadata
;
1799 sync_pb::AttachmentMetadataRecord
* record
= attachment_metadata
.add_record();
1800 sync_pb::AttachmentIdProto attachment_id_proto
=
1801 syncer::CreateAttachmentIdProto(0, 0);
1802 *record
->mutable_id() = attachment_id_proto
;
1803 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto
));
1804 const Id id
= TestIdFactory::FromNumber(-1);
1806 // Create an entry with attachment metadata and see that the attachment id
1808 CreateEntryWithAttachmentMetadata(
1809 PREFERENCES
, "some entry", id
, attachment_metadata
);
1810 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto
));
1812 // Delete the entry and see that it's still linked because the entry hasn't
1815 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto
));
1817 // Reload the Directory, purging the deleted entry, and see that the
1818 // attachment is no longer linked.
1819 SimulateSaveAndReloadDir();
1820 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto
));
1823 // Verify that a given attachment can be referenced by multiple entries and that
1824 // any one of the references is sufficient to ensure it remains linked.
1825 TEST_F(SyncableDirectoryTest
, Directory_LastReferenceUnlinksAttachments
) {
1826 // Create one attachment.
1827 sync_pb::AttachmentMetadata attachment_metadata
;
1828 sync_pb::AttachmentMetadataRecord
* record
= attachment_metadata
.add_record();
1829 sync_pb::AttachmentIdProto attachment_id_proto
=
1830 syncer::CreateAttachmentIdProto(0, 0);
1831 *record
->mutable_id() = attachment_id_proto
;
1833 // Create two entries, each referencing the attachment.
1834 const Id id1
= TestIdFactory::FromNumber(-1);
1835 const Id id2
= TestIdFactory::FromNumber(-2);
1836 CreateEntryWithAttachmentMetadata(
1837 PREFERENCES
, "some entry", id1
, attachment_metadata
);
1838 CreateEntryWithAttachmentMetadata(
1839 PREFERENCES
, "some other entry", id2
, attachment_metadata
);
1841 // See that the attachment is considered linked.
1842 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto
));
1844 // Delete the first entry, reload the Directory, see that the attachment is
1847 SimulateSaveAndReloadDir();
1848 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto
));
1850 // Delete the second entry, reload the Directory, see that the attachment is
1853 SimulateSaveAndReloadDir();
1854 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto
));
1857 TEST_F(SyncableDirectoryTest
, Directory_GetAttachmentIdsToUpload
) {
1858 // Create one attachment, referenced by two entries.
1859 AttachmentId attachment_id
= AttachmentId::Create(0, 0);
1860 sync_pb::AttachmentIdProto attachment_id_proto
= attachment_id
.GetProto();
1861 sync_pb::AttachmentMetadata attachment_metadata
;
1862 sync_pb::AttachmentMetadataRecord
* record
= attachment_metadata
.add_record();
1863 *record
->mutable_id() = attachment_id_proto
;
1864 const Id id1
= TestIdFactory::FromNumber(-1);
1865 const Id id2
= TestIdFactory::FromNumber(-2);
1866 CreateEntryWithAttachmentMetadata(
1867 PREFERENCES
, "some entry", id1
, attachment_metadata
);
1868 CreateEntryWithAttachmentMetadata(
1869 PREFERENCES
, "some other entry", id2
, attachment_metadata
);
1871 // See that Directory reports that this attachment is not on the server.
1872 AttachmentIdList ids
;
1874 ReadTransaction
trans(FROM_HERE
, dir().get());
1875 dir()->GetAttachmentIdsToUpload(&trans
, PREFERENCES
, &ids
);
1877 ASSERT_EQ(1U, ids
.size());
1878 ASSERT_EQ(attachment_id
, *ids
.begin());
1880 // Call again, but this time with a ModelType for which there are no entries.
1881 // See that Directory correctly reports that there are none.
1883 ReadTransaction
trans(FROM_HERE
, dir().get());
1884 dir()->GetAttachmentIdsToUpload(&trans
, PASSWORDS
, &ids
);
1886 ASSERT_TRUE(ids
.empty());
1888 // Now, mark the attachment as "on the server" via entry_1.
1890 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1891 MutableEntry
entry_1(&trans
, GET_BY_ID
, id1
);
1892 entry_1
.MarkAttachmentAsOnServer(attachment_id_proto
);
1895 // See that Directory no longer reports that this attachment is not on the
1898 ReadTransaction
trans(FROM_HERE
, dir().get());
1899 dir()->GetAttachmentIdsToUpload(&trans
, PREFERENCES
, &ids
);
1901 ASSERT_TRUE(ids
.empty());
1904 // Verify that the directory accepts entries with unset parent ID.
1905 TEST_F(SyncableDirectoryTest
, MutableEntry_ImplicitParentId
) {
1906 TestIdFactory id_factory
;
1907 const Id root_id
= TestIdFactory::root();
1908 const Id p_root_id
= id_factory
.NewServerId();
1909 const Id a_root_id
= id_factory
.NewServerId();
1910 const Id item1_id
= id_factory
.NewServerId();
1911 const Id item2_id
= id_factory
.NewServerId();
1912 const Id item3_id
= id_factory
.NewServerId();
1913 // Create two type root folders that are necessary (for now)
1914 // for creating items without explicitly set Parent ID
1916 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1917 MutableEntry
p_root(&trans
, CREATE
, PREFERENCES
, root_id
, "P");
1918 ASSERT_TRUE(p_root
.good());
1919 p_root
.PutIsDir(true);
1920 p_root
.PutId(p_root_id
);
1921 p_root
.PutBaseVersion(1);
1923 MutableEntry
a_root(&trans
, CREATE
, AUTOFILL
, root_id
, "A");
1924 ASSERT_TRUE(a_root
.good());
1925 a_root
.PutIsDir(true);
1926 a_root
.PutId(a_root_id
);
1927 a_root
.PutBaseVersion(1);
1930 // Create two entries with implicit parent nodes and one entry with explicit
1933 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1934 MutableEntry
item1(&trans
, CREATE
, PREFERENCES
, "P1");
1935 item1
.PutBaseVersion(1);
1936 item1
.PutId(item1_id
);
1937 MutableEntry
item2(&trans
, CREATE
, AUTOFILL
, "A1");
1938 item2
.PutBaseVersion(1);
1939 item2
.PutId(item2_id
);
1940 // Placing an AUTOFILL item under the root isn't expected,
1941 // but let's test it to verify that explicit root overrides the implicit
1942 // one and this entry doesn't end up under the "A" root.
1943 MutableEntry
item3(&trans
, CREATE
, AUTOFILL
, root_id
, "A2");
1944 item3
.PutBaseVersion(1);
1945 item3
.PutId(item3_id
);
1949 ReadTransaction
trans(FROM_HERE
, dir().get());
1950 // Verify that item1 and item2 are good and have no ParentId.
1951 Entry
item1(&trans
, GET_BY_ID
, item1_id
);
1952 ASSERT_TRUE(item1
.good());
1953 ASSERT_TRUE(item1
.GetParentId().IsNull());
1954 Entry
item2(&trans
, GET_BY_ID
, item2_id
);
1955 ASSERT_TRUE(item2
.good());
1956 ASSERT_TRUE(item2
.GetParentId().IsNull());
1957 // Verify that p_root and a_root have exactly one child each
1958 // (subtract one to exclude roots themselves).
1959 Entry
p_root(&trans
, GET_BY_ID
, p_root_id
);
1960 ASSERT_EQ(item1_id
, p_root
.GetFirstChildId());
1961 ASSERT_EQ(1, p_root
.GetTotalNodeCount() - 1);
1962 Entry
a_root(&trans
, GET_BY_ID
, a_root_id
);
1963 ASSERT_EQ(item2_id
, a_root
.GetFirstChildId());
1964 ASSERT_EQ(1, a_root
.GetTotalNodeCount() - 1);
1968 // Verify that the successor / predecessor navigation still works for
1969 // directory entries with unset Parent IDs.
1970 TEST_F(SyncableDirectoryTest
, MutableEntry_ImplicitParentId_Siblings
) {
1971 TestIdFactory id_factory
;
1972 const Id root_id
= TestIdFactory::root();
1973 const Id p_root_id
= id_factory
.NewServerId();
1974 const Id item1_id
= id_factory
.FromNumber(1);
1975 const Id item2_id
= id_factory
.FromNumber(2);
1977 // Create type root folder for PREFERENCES.
1979 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1980 MutableEntry
p_root(&trans
, CREATE
, PREFERENCES
, root_id
, "P");
1981 ASSERT_TRUE(p_root
.good());
1982 p_root
.PutIsDir(true);
1983 p_root
.PutId(p_root_id
);
1984 p_root
.PutBaseVersion(1);
1987 // Create two PREFERENCES entries with implicit parent nodes.
1989 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
1990 MutableEntry
item1(&trans
, CREATE
, PREFERENCES
, "P1");
1991 item1
.PutBaseVersion(1);
1992 item1
.PutId(item1_id
);
1993 MutableEntry
item2(&trans
, CREATE
, PREFERENCES
, "P2");
1994 item2
.PutBaseVersion(1);
1995 item2
.PutId(item2_id
);
1998 // Verify GetSuccessorId and GetPredecessorId calls for these items.
1999 // Please note that items are sorted according to their ID, e.g.
2000 // item1 first, then item2.
2002 ReadTransaction
trans(FROM_HERE
, dir().get());
2003 Entry
item1(&trans
, GET_BY_ID
, item1_id
);
2004 EXPECT_EQ(Id(), item1
.GetPredecessorId());
2005 EXPECT_EQ(item2_id
, item1
.GetSuccessorId());
2007 Entry
item2(&trans
, GET_BY_ID
, item2_id
);
2008 EXPECT_EQ(item1_id
, item2
.GetPredecessorId());
2009 EXPECT_EQ(Id(), item2
.GetSuccessorId());
2013 TEST_F(SyncableDirectoryTest
, SaveChangesSnapshot_HasUnsavedMetahandleChanges
) {
2015 Directory::SaveChangesSnapshot snapshot
;
2016 EXPECT_FALSE(snapshot
.HasUnsavedMetahandleChanges());
2017 snapshot
.dirty_metas
.insert(&kernel
);
2018 EXPECT_TRUE(snapshot
.HasUnsavedMetahandleChanges());
2019 snapshot
.dirty_metas
.clear();
2021 EXPECT_FALSE(snapshot
.HasUnsavedMetahandleChanges());
2022 snapshot
.metahandles_to_purge
.insert(1);
2023 EXPECT_TRUE(snapshot
.HasUnsavedMetahandleChanges());
2024 snapshot
.metahandles_to_purge
.clear();
2026 EXPECT_FALSE(snapshot
.HasUnsavedMetahandleChanges());
2027 snapshot
.delete_journals
.insert(&kernel
);
2028 EXPECT_TRUE(snapshot
.HasUnsavedMetahandleChanges());
2029 snapshot
.delete_journals
.clear();
2031 EXPECT_FALSE(snapshot
.HasUnsavedMetahandleChanges());
2032 snapshot
.delete_journals_to_purge
.insert(1);
2033 EXPECT_TRUE(snapshot
.HasUnsavedMetahandleChanges());
2034 snapshot
.delete_journals_to_purge
.clear();
2037 // Verify that Directory triggers an unrecoverable error when a catastrophic
2038 // DirectoryBackingStore error is detected.
2039 TEST_F(SyncableDirectoryTest
, CatastrophicError
) {
2040 MockUnrecoverableErrorHandler unrecoverable_error_handler
;
2041 Directory
dir(new InMemoryDirectoryBackingStore("catastrophic_error"),
2042 MakeWeakHandle(unrecoverable_error_handler
.GetWeakPtr()),
2043 base::Closure(), nullptr, nullptr);
2044 ASSERT_EQ(OPENED
, dir
.Open(kDirectoryName
, directory_change_delegate(),
2045 NullTransactionObserver()));
2046 ASSERT_EQ(0, unrecoverable_error_handler
.invocation_count());
2048 // Fire off two catastrophic errors. Call it twice to ensure Directory is
2049 // tolerant of multiple invocations since that may happen in the real world.
2050 dir
.OnCatastrophicError();
2051 dir
.OnCatastrophicError();
2053 base::RunLoop().RunUntilIdle();
2055 // See that the unrecoverable error handler has been invoked twice.
2056 ASSERT_EQ(2, unrecoverable_error_handler
.invocation_count());
2059 bool EntitySpecificsValuesAreSame(const sync_pb::EntitySpecifics
& v1
,
2060 const sync_pb::EntitySpecifics
& v2
) {
2064 // Verifies that server and client specifics are shared when their values
2066 TEST_F(SyncableDirectoryTest
, SharingOfClientAndServerSpecifics
) {
2067 sync_pb::EntitySpecifics specifics1
;
2068 sync_pb::EntitySpecifics specifics2
;
2069 sync_pb::EntitySpecifics specifics3
;
2070 AddDefaultFieldValue(BOOKMARKS
, &specifics1
);
2071 AddDefaultFieldValue(BOOKMARKS
, &specifics2
);
2072 AddDefaultFieldValue(BOOKMARKS
, &specifics3
);
2073 specifics1
.mutable_bookmark()->set_url("foo");
2074 specifics2
.mutable_bookmark()->set_url("bar");
2075 // specifics3 has the same URL as specifics1
2076 specifics3
.mutable_bookmark()->set_url("foo");
2078 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
2079 MutableEntry
item(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "item");
2080 item
.PutId(TestIdFactory::FromNumber(1));
2081 item
.PutBaseVersion(10);
2084 item
.PutSpecifics(specifics1
);
2085 item
.PutServerSpecifics(specifics1
);
2086 EXPECT_TRUE(EntitySpecificsValuesAreSame(item
.GetSpecifics(),
2087 item
.GetServerSpecifics()));
2089 // Verify that specifics are no longer shared.
2090 item
.PutServerSpecifics(specifics2
);
2091 EXPECT_FALSE(EntitySpecificsValuesAreSame(item
.GetSpecifics(),
2092 item
.GetServerSpecifics()));
2094 // Verify that specifics are shared again because specifics3 matches
2096 item
.PutServerSpecifics(specifics3
);
2097 EXPECT_TRUE(EntitySpecificsValuesAreSame(item
.GetSpecifics(),
2098 item
.GetServerSpecifics()));
2100 // Verify that copying the same value back to SPECIFICS is still OK.
2101 item
.PutSpecifics(specifics3
);
2102 EXPECT_TRUE(EntitySpecificsValuesAreSame(item
.GetSpecifics(),
2103 item
.GetServerSpecifics()));
2105 // Verify sharing with BASE_SERVER_SPECIFICS.
2106 EXPECT_FALSE(EntitySpecificsValuesAreSame(item
.GetServerSpecifics(),
2107 item
.GetBaseServerSpecifics()));
2108 item
.PutBaseServerSpecifics(specifics3
);
2109 EXPECT_TRUE(EntitySpecificsValuesAreSame(item
.GetServerSpecifics(),
2110 item
.GetBaseServerSpecifics()));
2113 } // namespace syncable
2115 } // namespace syncer