Add test_runner support for new accessibility event
[chromium-blink-merge.git] / sync / syncable / directory_unittest.cc
blob1d04c3a0a2819dc70715af2626cb1804645eec3d
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/strings/stringprintf.h"
8 #include "base/test/values_test_util.h"
9 #include "sync/internal_api/public/base/attachment_id_proto.h"
10 #include "sync/syncable/syncable_proto_util.h"
11 #include "sync/syncable/syncable_util.h"
12 #include "sync/syncable/syncable_write_transaction.h"
13 #include "sync/test/engine/test_syncable_utils.h"
14 #include "sync/test/test_directory_backing_store.h"
16 using base::ExpectDictBooleanValue;
17 using base::ExpectDictStringValue;
19 namespace syncer {
21 namespace syncable {
23 namespace {
25 bool IsLegalNewParent(const Entry& a, const Entry& b) {
26 return IsLegalNewParent(a.trans(), a.GetId(), b.GetId());
29 void PutDataAsBookmarkFavicon(WriteTransaction* wtrans,
30 MutableEntry* e,
31 const char* bytes,
32 size_t bytes_length) {
33 sync_pb::EntitySpecifics specifics;
34 specifics.mutable_bookmark()->set_url("http://demo/");
35 specifics.mutable_bookmark()->set_favicon(bytes, bytes_length);
36 e->PutSpecifics(specifics);
39 void ExpectDataFromBookmarkFaviconEquals(BaseTransaction* trans,
40 Entry* e,
41 const char* bytes,
42 size_t bytes_length) {
43 ASSERT_TRUE(e->good());
44 ASSERT_TRUE(e->GetSpecifics().has_bookmark());
45 ASSERT_EQ("http://demo/", e->GetSpecifics().bookmark().url());
46 ASSERT_EQ(std::string(bytes, bytes_length),
47 e->GetSpecifics().bookmark().favicon());
50 } // namespace
52 const char SyncableDirectoryTest::kDirectoryName[] = "Foo";
54 SyncableDirectoryTest::SyncableDirectoryTest() {
57 SyncableDirectoryTest::~SyncableDirectoryTest() {
60 void SyncableDirectoryTest::SetUp() {
61 ASSERT_TRUE(connection_.OpenInMemory());
62 ASSERT_EQ(OPENED, ReopenDirectory());
65 void SyncableDirectoryTest::TearDown() {
66 if (dir_)
67 dir_->SaveChanges();
68 dir_.reset();
71 DirOpenResult SyncableDirectoryTest::ReopenDirectory() {
72 // Use a TestDirectoryBackingStore and sql::Connection so we can have test
73 // data persist across Directory object lifetimes while getting the
74 // performance benefits of not writing to disk.
75 dir_.reset(
76 new Directory(new TestDirectoryBackingStore(kDirectoryName, &connection_),
77 &handler_,
78 NULL,
79 NULL,
80 NULL));
82 DirOpenResult open_result =
83 dir_->Open(kDirectoryName, &delegate_, NullTransactionObserver());
85 if (open_result != OPENED) {
86 dir_.reset();
89 return open_result;
92 // Creates an empty entry and sets the ID field to a default one.
93 void SyncableDirectoryTest::CreateEntry(const ModelType& model_type,
94 const std::string& entryname) {
95 CreateEntry(model_type, entryname, TestIdFactory::FromNumber(-99));
98 // Creates an empty entry and sets the ID field to id.
99 void SyncableDirectoryTest::CreateEntry(const ModelType& model_type,
100 const std::string& entryname,
101 const int id) {
102 CreateEntry(model_type, entryname, TestIdFactory::FromNumber(id));
105 void SyncableDirectoryTest::CreateEntry(const ModelType& model_type,
106 const std::string& entryname,
107 const Id& id) {
108 CreateEntryWithAttachmentMetadata(
109 model_type, entryname, id, sync_pb::AttachmentMetadata());
112 void SyncableDirectoryTest::CreateEntryWithAttachmentMetadata(
113 const ModelType& model_type,
114 const std::string& entryname,
115 const Id& id,
116 const sync_pb::AttachmentMetadata& attachment_metadata) {
117 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir_.get());
118 MutableEntry me(&wtrans, CREATE, model_type, wtrans.root_id(), entryname);
119 ASSERT_TRUE(me.good());
120 me.PutId(id);
121 me.PutAttachmentMetadata(attachment_metadata);
122 me.PutIsUnsynced(true);
125 void SyncableDirectoryTest::DeleteEntry(const Id& id) {
126 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
127 MutableEntry entry(&trans, GET_BY_ID, id);
128 ASSERT_TRUE(entry.good());
129 entry.PutIsDel(true);
132 DirOpenResult SyncableDirectoryTest::SimulateSaveAndReloadDir() {
133 if (!dir_->SaveChanges())
134 return FAILED_IN_UNITTEST;
136 return ReopenDirectory();
139 DirOpenResult SyncableDirectoryTest::SimulateCrashAndReloadDir() {
140 return ReopenDirectory();
143 void SyncableDirectoryTest::GetAllMetaHandles(BaseTransaction* trans,
144 MetahandleSet* result) {
145 dir_->GetAllMetaHandles(trans, result);
148 void SyncableDirectoryTest::CheckPurgeEntriesWithTypeInSucceeded(
149 ModelTypeSet types_to_purge,
150 bool before_reload) {
151 SCOPED_TRACE(testing::Message("Before reload: ") << before_reload);
153 ReadTransaction trans(FROM_HERE, dir_.get());
154 MetahandleSet all_set;
155 dir_->GetAllMetaHandles(&trans, &all_set);
156 EXPECT_EQ(4U, all_set.size());
157 if (before_reload)
158 EXPECT_EQ(6U, dir_->kernel_->metahandles_to_purge.size());
159 for (MetahandleSet::iterator iter = all_set.begin(); iter != all_set.end();
160 ++iter) {
161 Entry e(&trans, GET_BY_HANDLE, *iter);
162 const ModelType local_type = e.GetModelType();
163 const ModelType server_type = e.GetServerModelType();
165 // Note the dance around incrementing |it|, since we sometimes erase().
166 if ((IsRealDataType(local_type) && types_to_purge.Has(local_type)) ||
167 (IsRealDataType(server_type) && types_to_purge.Has(server_type))) {
168 FAIL() << "Illegal type should have been deleted.";
173 for (ModelTypeSet::Iterator it = types_to_purge.First(); it.Good();
174 it.Inc()) {
175 EXPECT_FALSE(dir_->InitialSyncEndedForType(it.Get()));
176 sync_pb::DataTypeProgressMarker progress;
177 dir_->GetDownloadProgress(it.Get(), &progress);
178 EXPECT_EQ("", progress.token());
180 ReadTransaction trans(FROM_HERE, dir_.get());
181 sync_pb::DataTypeContext context;
182 dir_->GetDataTypeContext(&trans, it.Get(), &context);
183 EXPECT_TRUE(context.SerializeAsString().empty());
185 EXPECT_FALSE(types_to_purge.Has(BOOKMARKS));
186 EXPECT_TRUE(dir_->InitialSyncEndedForType(BOOKMARKS));
189 bool SyncableDirectoryTest::IsInDirtyMetahandles(int64 metahandle) {
190 return 1 == dir_->kernel_->dirty_metahandles.count(metahandle);
193 bool SyncableDirectoryTest::IsInMetahandlesToPurge(int64 metahandle) {
194 return 1 == dir_->kernel_->metahandles_to_purge.count(metahandle);
197 scoped_ptr<Directory>& SyncableDirectoryTest::dir() {
198 return dir_;
201 DirectoryChangeDelegate* SyncableDirectoryTest::directory_change_delegate() {
202 return &delegate_;
205 Encryptor* SyncableDirectoryTest::encryptor() {
206 return &encryptor_;
209 UnrecoverableErrorHandler*
210 SyncableDirectoryTest::unrecoverable_error_handler() {
211 return &handler_;
214 void SyncableDirectoryTest::ValidateEntry(BaseTransaction* trans,
215 int64 id,
216 bool check_name,
217 const std::string& name,
218 int64 base_version,
219 int64 server_version,
220 bool is_del) {
221 Entry e(trans, GET_BY_ID, TestIdFactory::FromNumber(id));
222 ASSERT_TRUE(e.good());
223 if (check_name)
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;
242 if (i % 2 == 0) {
243 AddDefaultFieldValue(BOOKMARKS, &specs);
244 expected_purges.insert(e.GetMetahandle());
245 all_handles.insert(e.GetMetahandle());
246 } else {
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);
263 to_purge.Clear();
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();
295 ++i) {
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();
307 ++i) {
308 // Change existing entries to directories to dirty them.
309 MutableEntry e1(&trans, GET_BY_HANDLE, *i);
310 e1.PutIsDir(true);
311 e1.PutIsUnsynced(true);
312 // Add new entries
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();
331 ++i) {
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();
360 ++i) {
361 // Change existing entries to directories to dirty them.
362 MutableEntry e1(&trans, GET_BY_HANDLE, *i);
363 ASSERT_TRUE(e1.good());
364 e1.PutIsDir(true);
365 e1.PutIsUnsynced(true);
366 // Add new entries
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();
382 ++i) {
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();
403 ++i) {
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;
408 if (should_change) {
409 bool not_dir = !e.GetIsDir();
410 e.PutIsDir(not_dir);
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();
425 ++i) {
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 Id id1 = TestIdFactory::FromNumber(-1);
439 Id id2 = TestIdFactory::FromNumber(-2);
440 int64 handle1 = 0;
441 int64 handle2 = 0;
443 // Create two bookmark entries and save in database.
444 CreateEntry(BOOKMARKS, "item1", id1);
445 CreateEntry(BOOKMARKS, "item2", id2);
447 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
448 MutableEntry item1(&trans, GET_BY_ID, id1);
449 ASSERT_TRUE(item1.good());
450 handle1 = item1.GetMetahandle();
451 item1.PutSpecifics(bookmark_specifics);
452 item1.PutServerSpecifics(bookmark_specifics);
453 MutableEntry item2(&trans, GET_BY_ID, id2);
454 ASSERT_TRUE(item2.good());
455 handle2 = item2.GetMetahandle();
456 item2.PutSpecifics(bookmark_specifics);
457 item2.PutServerSpecifics(bookmark_specifics);
459 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir());
462 { // Test adding and saving delete journals.
463 DeleteJournal* delete_journal = dir()->delete_journal();
465 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
466 EntryKernelSet journal_entries;
467 delete_journal->GetDeleteJournals(&trans, BOOKMARKS, &journal_entries);
468 ASSERT_EQ(0u, journal_entries.size());
470 // Set SERVER_IS_DEL of the entries to true and they should be added to
471 // delete journals.
472 MutableEntry item1(&trans, GET_BY_ID, id1);
473 ASSERT_TRUE(item1.good());
474 item1.PutServerIsDel(true);
475 MutableEntry item2(&trans, GET_BY_ID, id2);
476 ASSERT_TRUE(item2.good());
477 item2.PutServerIsDel(true);
478 EntryKernel tmp;
479 tmp.put(ID, id1);
480 EXPECT_TRUE(delete_journal->delete_journals_.count(&tmp));
481 tmp.put(ID, id2);
482 EXPECT_TRUE(delete_journal->delete_journals_.count(&tmp));
485 // Save delete journals in database and verify memory clearing.
486 ASSERT_TRUE(dir()->SaveChanges());
488 ReadTransaction trans(FROM_HERE, dir().get());
489 EXPECT_EQ(0u, delete_journal->GetDeleteJournalSize(&trans));
491 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir());
496 // Test reading delete journals from database.
497 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
498 DeleteJournal* delete_journal = dir()->delete_journal();
499 EntryKernelSet journal_entries;
500 delete_journal->GetDeleteJournals(&trans, BOOKMARKS, &journal_entries);
501 ASSERT_EQ(2u, journal_entries.size());
502 EntryKernel tmp;
503 tmp.put(META_HANDLE, handle1);
504 EXPECT_TRUE(journal_entries.count(&tmp));
505 tmp.put(META_HANDLE, handle2);
506 EXPECT_TRUE(journal_entries.count(&tmp));
508 // Purge item2.
509 MetahandleSet to_purge;
510 to_purge.insert(handle2);
511 delete_journal->PurgeDeleteJournals(&trans, to_purge);
513 // Verify that item2 is purged from journals in memory and will be
514 // purged from database.
515 tmp.put(ID, id2);
516 EXPECT_FALSE(delete_journal->delete_journals_.count(&tmp));
517 EXPECT_EQ(1u, delete_journal->delete_journals_to_purge_.size());
518 EXPECT_TRUE(delete_journal->delete_journals_to_purge_.count(handle2));
520 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir());
525 // Verify purged entry is gone in database.
526 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
527 DeleteJournal* delete_journal = dir()->delete_journal();
528 EntryKernelSet journal_entries;
529 delete_journal->GetDeleteJournals(&trans, BOOKMARKS, &journal_entries);
530 ASSERT_EQ(1u, journal_entries.size());
531 EntryKernel tmp;
532 tmp.put(ID, id1);
533 tmp.put(META_HANDLE, handle1);
534 EXPECT_TRUE(journal_entries.count(&tmp));
536 // Undelete item1.
537 MutableEntry item1(&trans, GET_BY_ID, id1);
538 ASSERT_TRUE(item1.good());
539 item1.PutServerIsDel(false);
540 EXPECT_TRUE(delete_journal->delete_journals_.empty());
541 EXPECT_EQ(1u, delete_journal->delete_journals_to_purge_.size());
542 EXPECT_TRUE(delete_journal->delete_journals_to_purge_.count(handle1));
544 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir());
548 // Verify undeleted entry is gone from database.
549 ReadTransaction trans(FROM_HERE, dir().get());
550 DeleteJournal* delete_journal = dir()->delete_journal();
551 ASSERT_EQ(0u, delete_journal->GetDeleteJournalSize(&trans));
555 TEST_F(SyncableDirectoryTest, TestPurgeDeletedEntriesOnReload) {
556 sync_pb::EntitySpecifics specifics;
557 AddDefaultFieldValue(PREFERENCES, &specifics);
559 const int kClientCount = 2;
560 const int kServerCount = 5;
561 const int kTestCount = kClientCount + kServerCount;
562 int64 handles[kTestCount];
564 // The idea is to recreate various combinations of IDs, IS_DEL,
565 // IS_UNSYNCED, and IS_UNAPPLIED_UPDATE flags to test all combinations
566 // for DirectoryBackingStore::SafeToPurgeOnLoading.
567 // 0: client ID, IS_DEL, IS_UNSYNCED
568 // 1: client ID, IS_UNSYNCED
569 // 2: server ID, IS_DEL, IS_UNSYNCED, IS_UNAPPLIED_UPDATE
570 // 3: server ID, IS_DEL, IS_UNSYNCED
571 // 4: server ID, IS_DEL, IS_UNAPPLIED_UPDATE
572 // 5: server ID, IS_DEL
573 // 6: server ID
575 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
577 for (int i = 0; i < kTestCount; i++) {
578 std::string name = base::StringPrintf("item%d", i);
579 MutableEntry item(&trans, CREATE, PREFERENCES, trans.root_id(), name);
580 ASSERT_TRUE(item.good());
582 handles[i] = item.GetMetahandle();
584 if (i < kClientCount) {
585 item.PutId(TestIdFactory::FromNumber(i - kClientCount));
586 } else {
587 item.PutId(TestIdFactory::FromNumber(i));
590 item.PutUniqueClientTag(name);
591 item.PutIsUnsynced(true);
592 item.PutSpecifics(specifics);
593 item.PutServerSpecifics(specifics);
595 if (i >= kClientCount) {
596 item.PutBaseVersion(10);
597 item.PutServerVersion(10);
600 // Set flags
601 if (i != 1 && i != 6)
602 item.PutIsDel(true);
604 if (i >= 4)
605 item.PutIsUnsynced(false);
607 if (i == 2 || i == 4)
608 item.PutIsUnappliedUpdate(true);
611 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir());
613 // Expect items 0 and 5 to be purged according to
614 // DirectoryBackingStore::SafeToPurgeOnLoading:
615 // - Item 0 is an item with IS_DEL flag and client ID.
616 // - Item 5 is an item with IS_DEL flag which has both
617 // IS_UNSYNCED and IS_UNAPPLIED_UPDATE unset.
618 std::vector<int64> expected_purged;
619 expected_purged.push_back(0);
620 expected_purged.push_back(5);
622 std::vector<int64> actually_purged;
624 ReadTransaction trans(FROM_HERE, dir().get());
625 for (int i = 0; i < kTestCount; i++) {
626 Entry item(&trans, GET_BY_HANDLE, handles[i]);
627 if (!item.good()) {
628 actually_purged.push_back(i);
633 EXPECT_EQ(expected_purged, actually_purged);
636 TEST_F(SyncableDirectoryTest, TestBasicLookupNonExistantID) {
637 ReadTransaction rtrans(FROM_HERE, dir().get());
638 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99));
639 ASSERT_FALSE(e.good());
642 TEST_F(SyncableDirectoryTest, TestBasicLookupValidID) {
643 CreateEntry(BOOKMARKS, "rtc");
644 ReadTransaction rtrans(FROM_HERE, dir().get());
645 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99));
646 ASSERT_TRUE(e.good());
649 TEST_F(SyncableDirectoryTest, TestDelete) {
650 std::string name = "peanut butter jelly time";
651 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
652 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), name);
653 ASSERT_TRUE(e1.good());
654 e1.PutIsDel(true);
655 MutableEntry e2(&trans, CREATE, BOOKMARKS, trans.root_id(), name);
656 ASSERT_TRUE(e2.good());
657 e2.PutIsDel(true);
658 MutableEntry e3(&trans, CREATE, BOOKMARKS, trans.root_id(), name);
659 ASSERT_TRUE(e3.good());
660 e3.PutIsDel(true);
662 e1.PutIsDel(false);
663 e2.PutIsDel(false);
664 e3.PutIsDel(false);
666 e1.PutIsDel(true);
667 e2.PutIsDel(true);
668 e3.PutIsDel(true);
671 TEST_F(SyncableDirectoryTest, TestGetUnsynced) {
672 Directory::Metahandles handles;
673 int64 handle1, handle2;
675 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
677 dir()->GetUnsyncedMetaHandles(&trans, &handles);
678 ASSERT_TRUE(0 == handles.size());
680 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), "abba");
681 ASSERT_TRUE(e1.good());
682 handle1 = e1.GetMetahandle();
683 e1.PutBaseVersion(1);
684 e1.PutIsDir(true);
685 e1.PutId(TestIdFactory::FromNumber(101));
687 MutableEntry e2(&trans, CREATE, BOOKMARKS, e1.GetId(), "bread");
688 ASSERT_TRUE(e2.good());
689 handle2 = e2.GetMetahandle();
690 e2.PutBaseVersion(1);
691 e2.PutId(TestIdFactory::FromNumber(102));
693 dir()->SaveChanges();
695 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
697 dir()->GetUnsyncedMetaHandles(&trans, &handles);
698 ASSERT_TRUE(0 == handles.size());
700 MutableEntry e3(&trans, GET_BY_HANDLE, handle1);
701 ASSERT_TRUE(e3.good());
702 e3.PutIsUnsynced(true);
704 dir()->SaveChanges();
706 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
707 dir()->GetUnsyncedMetaHandles(&trans, &handles);
708 ASSERT_TRUE(1 == handles.size());
709 ASSERT_TRUE(handle1 == handles[0]);
711 MutableEntry e4(&trans, GET_BY_HANDLE, handle2);
712 ASSERT_TRUE(e4.good());
713 e4.PutIsUnsynced(true);
715 dir()->SaveChanges();
717 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
718 dir()->GetUnsyncedMetaHandles(&trans, &handles);
719 ASSERT_TRUE(2 == handles.size());
720 if (handle1 == handles[0]) {
721 ASSERT_TRUE(handle2 == handles[1]);
722 } else {
723 ASSERT_TRUE(handle2 == handles[0]);
724 ASSERT_TRUE(handle1 == handles[1]);
727 MutableEntry e5(&trans, GET_BY_HANDLE, handle1);
728 ASSERT_TRUE(e5.good());
729 ASSERT_TRUE(e5.GetIsUnsynced());
730 ASSERT_TRUE(e5.PutIsUnsynced(false));
731 ASSERT_FALSE(e5.GetIsUnsynced());
733 dir()->SaveChanges();
735 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
736 dir()->GetUnsyncedMetaHandles(&trans, &handles);
737 ASSERT_TRUE(1 == handles.size());
738 ASSERT_TRUE(handle2 == handles[0]);
742 TEST_F(SyncableDirectoryTest, TestGetUnappliedUpdates) {
743 std::vector<int64> handles;
744 int64 handle1, handle2;
745 const FullModelTypeSet all_types = FullModelTypeSet::All();
747 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
749 dir()->GetUnappliedUpdateMetaHandles(&trans, all_types, &handles);
750 ASSERT_TRUE(0 == handles.size());
752 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), "abba");
753 ASSERT_TRUE(e1.good());
754 handle1 = e1.GetMetahandle();
755 e1.PutIsUnappliedUpdate(false);
756 e1.PutBaseVersion(1);
757 e1.PutId(TestIdFactory::FromNumber(101));
758 e1.PutIsDir(true);
760 MutableEntry e2(&trans, CREATE, BOOKMARKS, e1.GetId(), "bread");
761 ASSERT_TRUE(e2.good());
762 handle2 = e2.GetMetahandle();
763 e2.PutIsUnappliedUpdate(false);
764 e2.PutBaseVersion(1);
765 e2.PutId(TestIdFactory::FromNumber(102));
767 dir()->SaveChanges();
769 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
771 dir()->GetUnappliedUpdateMetaHandles(&trans, all_types, &handles);
772 ASSERT_TRUE(0 == handles.size());
774 MutableEntry e3(&trans, GET_BY_HANDLE, handle1);
775 ASSERT_TRUE(e3.good());
776 e3.PutIsUnappliedUpdate(true);
778 dir()->SaveChanges();
780 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
781 dir()->GetUnappliedUpdateMetaHandles(&trans, all_types, &handles);
782 ASSERT_TRUE(1 == handles.size());
783 ASSERT_TRUE(handle1 == handles[0]);
785 MutableEntry e4(&trans, GET_BY_HANDLE, handle2);
786 ASSERT_TRUE(e4.good());
787 e4.PutIsUnappliedUpdate(true);
789 dir()->SaveChanges();
791 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
792 dir()->GetUnappliedUpdateMetaHandles(&trans, all_types, &handles);
793 ASSERT_TRUE(2 == handles.size());
794 if (handle1 == handles[0]) {
795 ASSERT_TRUE(handle2 == handles[1]);
796 } else {
797 ASSERT_TRUE(handle2 == handles[0]);
798 ASSERT_TRUE(handle1 == handles[1]);
801 MutableEntry e5(&trans, GET_BY_HANDLE, handle1);
802 ASSERT_TRUE(e5.good());
803 e5.PutIsUnappliedUpdate(false);
805 dir()->SaveChanges();
807 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
808 dir()->GetUnappliedUpdateMetaHandles(&trans, all_types, &handles);
809 ASSERT_TRUE(1 == handles.size());
810 ASSERT_TRUE(handle2 == handles[0]);
814 TEST_F(SyncableDirectoryTest, DeleteBug_531383) {
815 // Try to evoke a check failure...
816 TestIdFactory id_factory;
817 int64 grandchild_handle;
819 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
820 MutableEntry parent(&wtrans, CREATE, BOOKMARKS, id_factory.root(), "Bob");
821 ASSERT_TRUE(parent.good());
822 parent.PutIsDir(true);
823 parent.PutId(id_factory.NewServerId());
824 parent.PutBaseVersion(1);
825 MutableEntry child(&wtrans, CREATE, BOOKMARKS, parent.GetId(), "Bob");
826 ASSERT_TRUE(child.good());
827 child.PutIsDir(true);
828 child.PutId(id_factory.NewServerId());
829 child.PutBaseVersion(1);
830 MutableEntry grandchild(&wtrans, CREATE, BOOKMARKS, child.GetId(), "Bob");
831 ASSERT_TRUE(grandchild.good());
832 grandchild.PutId(id_factory.NewServerId());
833 grandchild.PutBaseVersion(1);
834 grandchild.PutIsDel(true);
835 MutableEntry twin(&wtrans, CREATE, BOOKMARKS, child.GetId(), "Bob");
836 ASSERT_TRUE(twin.good());
837 twin.PutIsDel(true);
838 grandchild.PutIsDel(false);
840 grandchild_handle = grandchild.GetMetahandle();
842 dir()->SaveChanges();
844 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
845 MutableEntry grandchild(&wtrans, GET_BY_HANDLE, grandchild_handle);
846 grandchild.PutIsDel(true); // Used to CHECK fail here.
850 TEST_F(SyncableDirectoryTest, TestIsLegalNewParent) {
851 TestIdFactory id_factory;
852 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
853 Entry root(&wtrans, GET_BY_ID, id_factory.root());
854 ASSERT_TRUE(root.good());
855 MutableEntry parent(&wtrans, CREATE, BOOKMARKS, root.GetId(), "Bob");
856 ASSERT_TRUE(parent.good());
857 parent.PutIsDir(true);
858 parent.PutId(id_factory.NewServerId());
859 parent.PutBaseVersion(1);
860 MutableEntry child(&wtrans, CREATE, BOOKMARKS, parent.GetId(), "Bob");
861 ASSERT_TRUE(child.good());
862 child.PutIsDir(true);
863 child.PutId(id_factory.NewServerId());
864 child.PutBaseVersion(1);
865 MutableEntry grandchild(&wtrans, CREATE, BOOKMARKS, child.GetId(), "Bob");
866 ASSERT_TRUE(grandchild.good());
867 grandchild.PutId(id_factory.NewServerId());
868 grandchild.PutBaseVersion(1);
870 MutableEntry parent2(&wtrans, CREATE, BOOKMARKS, root.GetId(), "Pete");
871 ASSERT_TRUE(parent2.good());
872 parent2.PutIsDir(true);
873 parent2.PutId(id_factory.NewServerId());
874 parent2.PutBaseVersion(1);
875 MutableEntry child2(&wtrans, CREATE, BOOKMARKS, parent2.GetId(), "Pete");
876 ASSERT_TRUE(child2.good());
877 child2.PutIsDir(true);
878 child2.PutId(id_factory.NewServerId());
879 child2.PutBaseVersion(1);
880 MutableEntry grandchild2(&wtrans, CREATE, BOOKMARKS, child2.GetId(), "Pete");
881 ASSERT_TRUE(grandchild2.good());
882 grandchild2.PutId(id_factory.NewServerId());
883 grandchild2.PutBaseVersion(1);
884 // resulting tree
885 // root
886 // / |
887 // parent parent2
888 // | |
889 // child child2
890 // | |
891 // grandchild grandchild2
892 ASSERT_TRUE(IsLegalNewParent(child, root));
893 ASSERT_TRUE(IsLegalNewParent(child, parent));
894 ASSERT_FALSE(IsLegalNewParent(child, child));
895 ASSERT_FALSE(IsLegalNewParent(child, grandchild));
896 ASSERT_TRUE(IsLegalNewParent(child, parent2));
897 ASSERT_TRUE(IsLegalNewParent(child, grandchild2));
898 ASSERT_FALSE(IsLegalNewParent(parent, grandchild));
899 ASSERT_FALSE(IsLegalNewParent(root, grandchild));
900 ASSERT_FALSE(IsLegalNewParent(parent, grandchild));
903 TEST_F(SyncableDirectoryTest, TestEntryIsInFolder) {
904 // Create a subdir and an entry.
905 int64 entry_handle;
906 syncable::Id folder_id;
907 syncable::Id entry_id;
908 std::string entry_name = "entry";
911 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
912 MutableEntry folder(&trans, CREATE, BOOKMARKS, trans.root_id(), "folder");
913 ASSERT_TRUE(folder.good());
914 folder.PutIsDir(true);
915 EXPECT_TRUE(folder.PutIsUnsynced(true));
916 folder_id = folder.GetId();
918 MutableEntry entry(&trans, CREATE, BOOKMARKS, folder.GetId(), entry_name);
919 ASSERT_TRUE(entry.good());
920 entry_handle = entry.GetMetahandle();
921 entry.PutIsUnsynced(true);
922 entry_id = entry.GetId();
925 // Make sure we can find the entry in the folder.
927 ReadTransaction trans(FROM_HERE, dir().get());
928 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), entry_name));
929 EXPECT_EQ(1, CountEntriesWithName(&trans, folder_id, entry_name));
931 Entry entry(&trans, GET_BY_ID, entry_id);
932 ASSERT_TRUE(entry.good());
933 EXPECT_EQ(entry_handle, entry.GetMetahandle());
934 EXPECT_TRUE(entry.GetNonUniqueName() == entry_name);
935 EXPECT_TRUE(entry.GetParentId() == folder_id);
939 TEST_F(SyncableDirectoryTest, TestParentIdIndexUpdate) {
940 std::string child_name = "child";
942 WriteTransaction wt(FROM_HERE, UNITTEST, dir().get());
943 MutableEntry parent_folder(&wt, CREATE, BOOKMARKS, wt.root_id(), "folder1");
944 parent_folder.PutIsUnsynced(true);
945 parent_folder.PutIsDir(true);
947 MutableEntry parent_folder2(&wt, CREATE, BOOKMARKS, wt.root_id(), "folder2");
948 parent_folder2.PutIsUnsynced(true);
949 parent_folder2.PutIsDir(true);
951 MutableEntry child(&wt, CREATE, BOOKMARKS, parent_folder.GetId(), child_name);
952 child.PutIsDir(true);
953 child.PutIsUnsynced(true);
955 ASSERT_TRUE(child.good());
957 EXPECT_EQ(0, CountEntriesWithName(&wt, wt.root_id(), child_name));
958 EXPECT_EQ(parent_folder.GetId(), child.GetParentId());
959 EXPECT_EQ(1, CountEntriesWithName(&wt, parent_folder.GetId(), child_name));
960 EXPECT_EQ(0, CountEntriesWithName(&wt, parent_folder2.GetId(), child_name));
961 child.PutParentId(parent_folder2.GetId());
962 EXPECT_EQ(parent_folder2.GetId(), child.GetParentId());
963 EXPECT_EQ(0, CountEntriesWithName(&wt, parent_folder.GetId(), child_name));
964 EXPECT_EQ(1, CountEntriesWithName(&wt, parent_folder2.GetId(), child_name));
967 TEST_F(SyncableDirectoryTest, TestNoReindexDeletedItems) {
968 std::string folder_name = "folder";
969 std::string new_name = "new_name";
971 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
972 MutableEntry folder(&trans, CREATE, BOOKMARKS, trans.root_id(), folder_name);
973 ASSERT_TRUE(folder.good());
974 folder.PutIsDir(true);
975 folder.PutIsDel(true);
977 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), folder_name));
979 MutableEntry deleted(&trans, GET_BY_ID, folder.GetId());
980 ASSERT_TRUE(deleted.good());
981 deleted.PutParentId(trans.root_id());
982 deleted.PutNonUniqueName(new_name);
984 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), folder_name));
985 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), new_name));
988 TEST_F(SyncableDirectoryTest, TestCaseChangeRename) {
989 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
990 MutableEntry folder(&trans, CREATE, BOOKMARKS, trans.root_id(), "CaseChange");
991 ASSERT_TRUE(folder.good());
992 folder.PutParentId(trans.root_id());
993 folder.PutNonUniqueName("CASECHANGE");
994 folder.PutIsDel(true);
997 // Create items of each model type, and check that GetModelType and
998 // GetServerModelType return the right value.
999 TEST_F(SyncableDirectoryTest, GetModelType) {
1000 TestIdFactory id_factory;
1001 ModelTypeSet protocol_types = ProtocolTypes();
1002 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good();
1003 iter.Inc()) {
1004 ModelType datatype = iter.Get();
1005 SCOPED_TRACE(testing::Message("Testing model type ") << datatype);
1006 switch (datatype) {
1007 case UNSPECIFIED:
1008 case TOP_LEVEL_FOLDER:
1009 continue; // Datatype isn't a function of Specifics.
1010 default:
1011 break;
1013 sync_pb::EntitySpecifics specifics;
1014 AddDefaultFieldValue(datatype, &specifics);
1016 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1018 MutableEntry folder(&trans, CREATE, BOOKMARKS, trans.root_id(), "Folder");
1019 ASSERT_TRUE(folder.good());
1020 folder.PutId(id_factory.NewServerId());
1021 folder.PutSpecifics(specifics);
1022 folder.PutBaseVersion(1);
1023 folder.PutIsDir(true);
1024 folder.PutIsDel(false);
1025 ASSERT_EQ(datatype, folder.GetModelType());
1027 MutableEntry item(&trans, CREATE, BOOKMARKS, trans.root_id(), "Item");
1028 ASSERT_TRUE(item.good());
1029 item.PutId(id_factory.NewServerId());
1030 item.PutSpecifics(specifics);
1031 item.PutBaseVersion(1);
1032 item.PutIsDir(false);
1033 item.PutIsDel(false);
1034 ASSERT_EQ(datatype, item.GetModelType());
1036 // It's critical that deletion records retain their datatype, so that
1037 // they can be dispatched to the appropriate change processor.
1038 MutableEntry deleted_item(
1039 &trans, CREATE, BOOKMARKS, trans.root_id(), "Deleted Item");
1040 ASSERT_TRUE(item.good());
1041 deleted_item.PutId(id_factory.NewServerId());
1042 deleted_item.PutSpecifics(specifics);
1043 deleted_item.PutBaseVersion(1);
1044 deleted_item.PutIsDir(false);
1045 deleted_item.PutIsDel(true);
1046 ASSERT_EQ(datatype, deleted_item.GetModelType());
1048 MutableEntry server_folder(
1049 &trans, CREATE_NEW_UPDATE_ITEM, id_factory.NewServerId());
1050 ASSERT_TRUE(server_folder.good());
1051 server_folder.PutServerSpecifics(specifics);
1052 server_folder.PutBaseVersion(1);
1053 server_folder.PutServerIsDir(true);
1054 server_folder.PutServerIsDel(false);
1055 ASSERT_EQ(datatype, server_folder.GetServerModelType());
1057 MutableEntry server_item(
1058 &trans, CREATE_NEW_UPDATE_ITEM, id_factory.NewServerId());
1059 ASSERT_TRUE(server_item.good());
1060 server_item.PutServerSpecifics(specifics);
1061 server_item.PutBaseVersion(1);
1062 server_item.PutServerIsDir(false);
1063 server_item.PutServerIsDel(false);
1064 ASSERT_EQ(datatype, server_item.GetServerModelType());
1066 sync_pb::SyncEntity folder_entity;
1067 folder_entity.set_id_string(SyncableIdToProto(id_factory.NewServerId()));
1068 folder_entity.set_deleted(false);
1069 folder_entity.set_folder(true);
1070 folder_entity.mutable_specifics()->CopyFrom(specifics);
1071 ASSERT_EQ(datatype, GetModelType(folder_entity));
1073 sync_pb::SyncEntity item_entity;
1074 item_entity.set_id_string(SyncableIdToProto(id_factory.NewServerId()));
1075 item_entity.set_deleted(false);
1076 item_entity.set_folder(false);
1077 item_entity.mutable_specifics()->CopyFrom(specifics);
1078 ASSERT_EQ(datatype, GetModelType(item_entity));
1082 // A test that roughly mimics the directory interaction that occurs when a
1083 // bookmark folder and entry are created then synced for the first time. It is
1084 // a more common variant of the 'DeletedAndUnsyncedChild' scenario tested below.
1085 TEST_F(SyncableDirectoryTest, ChangeEntryIDAndUpdateChildren_ParentAndChild) {
1086 TestIdFactory id_factory;
1087 Id orig_parent_id;
1088 Id orig_child_id;
1091 // Create two client-side items, a parent and child.
1092 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1094 MutableEntry parent(&trans, CREATE, BOOKMARKS, id_factory.root(), "parent");
1095 parent.PutIsDir(true);
1096 parent.PutIsUnsynced(true);
1098 MutableEntry child(&trans, CREATE, BOOKMARKS, parent.GetId(), "child");
1099 child.PutIsUnsynced(true);
1101 orig_parent_id = parent.GetId();
1102 orig_child_id = child.GetId();
1106 // Simulate what happens after committing two items. Their IDs will be
1107 // replaced with server IDs. The child is renamed first, then the parent.
1108 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1110 MutableEntry parent(&trans, GET_BY_ID, orig_parent_id);
1111 MutableEntry child(&trans, GET_BY_ID, orig_child_id);
1113 ChangeEntryIDAndUpdateChildren(&trans, &child, id_factory.NewServerId());
1114 child.PutIsUnsynced(false);
1115 child.PutBaseVersion(1);
1116 child.PutServerVersion(1);
1118 ChangeEntryIDAndUpdateChildren(&trans, &parent, id_factory.NewServerId());
1119 parent.PutIsUnsynced(false);
1120 parent.PutBaseVersion(1);
1121 parent.PutServerVersion(1);
1124 // Final check for validity.
1125 EXPECT_EQ(OPENED, SimulateSaveAndReloadDir());
1128 // A test that roughly mimics the directory interaction that occurs when a
1129 // type root folder is created locally and then re-created (updated) from the
1130 // server.
1131 TEST_F(SyncableDirectoryTest, ChangeEntryIDAndUpdateChildren_ImplicitParent) {
1132 TestIdFactory id_factory;
1133 Id orig_parent_id;
1134 Id child_id;
1137 // Create two client-side items, a parent and child.
1138 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1140 MutableEntry parent(&trans, CREATE, PREFERENCES, id_factory.root(),
1141 "parent");
1142 parent.PutIsDir(true);
1143 parent.PutIsUnsynced(true);
1145 // The child has unset parent ID. The parent is inferred from the type.
1146 MutableEntry child(&trans, CREATE, PREFERENCES, "child");
1147 child.PutIsUnsynced(true);
1149 orig_parent_id = parent.GetId();
1150 child_id = child.GetId();
1154 // Simulate what happens after committing two items. Their IDs will be
1155 // replaced with server IDs. The child is renamed first, then the parent.
1156 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1158 MutableEntry parent(&trans, GET_BY_ID, orig_parent_id);
1160 ChangeEntryIDAndUpdateChildren(&trans, &parent, id_factory.NewServerId());
1161 parent.PutIsUnsynced(false);
1162 parent.PutBaseVersion(1);
1163 parent.PutServerVersion(1);
1166 // Final check for validity.
1167 EXPECT_EQ(OPENED, SimulateSaveAndReloadDir());
1169 // Verify that child's PARENT_ID hasn't been updated.
1171 ReadTransaction trans(FROM_HERE, dir().get());
1172 Entry child(&trans, GET_BY_ID, child_id);
1173 EXPECT_TRUE(child.good());
1174 EXPECT_TRUE(child.GetParentId().IsNull());
1178 // A test based on the scenario where we create a bookmark folder and entry
1179 // locally, but with a twist. In this case, the bookmark is deleted before we
1180 // are able to sync either it or its parent folder. This scenario used to cause
1181 // directory corruption, see crbug.com/125381.
1182 TEST_F(SyncableDirectoryTest,
1183 ChangeEntryIDAndUpdateChildren_DeletedAndUnsyncedChild) {
1184 TestIdFactory id_factory;
1185 Id orig_parent_id;
1186 Id orig_child_id;
1189 // Create two client-side items, a parent and child.
1190 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1192 MutableEntry parent(&trans, CREATE, BOOKMARKS, id_factory.root(), "parent");
1193 parent.PutIsDir(true);
1194 parent.PutIsUnsynced(true);
1196 MutableEntry child(&trans, CREATE, BOOKMARKS, parent.GetId(), "child");
1197 child.PutIsUnsynced(true);
1199 orig_parent_id = parent.GetId();
1200 orig_child_id = child.GetId();
1204 // Delete the child.
1205 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1207 MutableEntry child(&trans, GET_BY_ID, orig_child_id);
1208 child.PutIsDel(true);
1212 // Simulate what happens after committing the parent. Its ID will be
1213 // replaced with server a ID.
1214 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1216 MutableEntry parent(&trans, GET_BY_ID, orig_parent_id);
1218 ChangeEntryIDAndUpdateChildren(&trans, &parent, id_factory.NewServerId());
1219 parent.PutIsUnsynced(false);
1220 parent.PutBaseVersion(1);
1221 parent.PutServerVersion(1);
1224 // Final check for validity.
1225 EXPECT_EQ(OPENED, SimulateSaveAndReloadDir());
1228 // Ask the directory to generate a unique ID. Close and re-open the database
1229 // without saving, then ask for another unique ID. Verify IDs are not reused.
1230 // This scenario simulates a crash within the first few seconds of operation.
1231 TEST_F(SyncableDirectoryTest, LocalIdReuseTest) {
1232 Id pre_crash_id = dir()->NextId();
1233 SimulateCrashAndReloadDir();
1234 Id post_crash_id = dir()->NextId();
1235 EXPECT_NE(pre_crash_id, post_crash_id);
1238 // Ask the directory to generate a unique ID. Save the directory. Close and
1239 // re-open the database without saving, then ask for another unique ID. Verify
1240 // IDs are not reused. This scenario simulates a steady-state crash.
1241 TEST_F(SyncableDirectoryTest, LocalIdReuseTestWithSave) {
1242 Id pre_crash_id = dir()->NextId();
1243 dir()->SaveChanges();
1244 SimulateCrashAndReloadDir();
1245 Id post_crash_id = dir()->NextId();
1246 EXPECT_NE(pre_crash_id, post_crash_id);
1249 // Ensure that the unsynced, is_del and server unkown entries that may have been
1250 // left in the database by old clients will be deleted when we open the old
1251 // database.
1252 TEST_F(SyncableDirectoryTest, OldClientLeftUnsyncedDeletedLocalItem) {
1253 // We must create an entry with the offending properties. This is done with
1254 // some abuse of the MutableEntry's API; it doesn't expect us to modify an
1255 // item after it is deleted. If this hack becomes impractical we will need to
1256 // find a new way to simulate this scenario.
1258 TestIdFactory id_factory;
1260 // Happy-path: These valid entries should not get deleted.
1261 Id server_knows_id = id_factory.NewServerId();
1262 Id not_is_del_id = id_factory.NewLocalId();
1264 // The ID of the entry which will be unsynced, is_del and !ServerKnows().
1265 Id zombie_id = id_factory.NewLocalId();
1267 // We're about to do some bad things. Tell the directory verification
1268 // routines to look the other way.
1269 dir()->SetInvariantCheckLevel(OFF);
1272 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1274 // Create an uncommitted tombstone entry.
1275 MutableEntry server_knows(
1276 &trans, CREATE, BOOKMARKS, id_factory.root(), "server_knows");
1277 server_knows.PutId(server_knows_id);
1278 server_knows.PutIsUnsynced(true);
1279 server_knows.PutIsDel(true);
1280 server_knows.PutBaseVersion(5);
1281 server_knows.PutServerVersion(4);
1283 // Create a valid update entry.
1284 MutableEntry not_is_del(
1285 &trans, CREATE, BOOKMARKS, id_factory.root(), "not_is_del");
1286 not_is_del.PutId(not_is_del_id);
1287 not_is_del.PutIsDel(false);
1288 not_is_del.PutIsUnsynced(true);
1290 // Create a tombstone which should never be sent to the server because the
1291 // server never knew about the item's existence.
1293 // New clients should never put entries into this state. We work around
1294 // this by setting IS_DEL before setting IS_UNSYNCED, something which the
1295 // client should never do in practice.
1296 MutableEntry zombie(&trans, CREATE, BOOKMARKS, id_factory.root(), "zombie");
1297 zombie.PutId(zombie_id);
1298 zombie.PutIsDel(true);
1299 zombie.PutIsUnsynced(true);
1302 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir());
1305 ReadTransaction trans(FROM_HERE, dir().get());
1307 // The directory loading routines should have cleaned things up, making it
1308 // safe to check invariants once again.
1309 dir()->FullyCheckTreeInvariants(&trans);
1311 Entry server_knows(&trans, GET_BY_ID, server_knows_id);
1312 EXPECT_TRUE(server_knows.good());
1314 Entry not_is_del(&trans, GET_BY_ID, not_is_del_id);
1315 EXPECT_TRUE(not_is_del.good());
1317 Entry zombie(&trans, GET_BY_ID, zombie_id);
1318 EXPECT_FALSE(zombie.good());
1322 TEST_F(SyncableDirectoryTest, PositionWithNullSurvivesSaveAndReload) {
1323 TestIdFactory id_factory;
1324 Id null_child_id;
1325 const char null_cstr[] = "\0null\0test";
1326 std::string null_str(null_cstr, arraysize(null_cstr) - 1);
1327 // Pad up to the minimum length with 0x7f characters, then add a string that
1328 // contains a few NULLs to the end. This is slightly wrong, since the suffix
1329 // part of a UniquePosition shouldn't contain NULLs, but it's good enough for
1330 // this test.
1331 std::string suffix =
1332 std::string(UniquePosition::kSuffixLength - null_str.length(), '\x7f') +
1333 null_str;
1334 UniquePosition null_pos = UniquePosition::FromInt64(10, suffix);
1337 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1339 MutableEntry parent(&trans, CREATE, BOOKMARKS, id_factory.root(), "parent");
1340 parent.PutIsDir(true);
1341 parent.PutIsUnsynced(true);
1343 MutableEntry child(&trans, CREATE, BOOKMARKS, parent.GetId(), "child");
1344 child.PutIsUnsynced(true);
1345 child.PutUniquePosition(null_pos);
1346 child.PutServerUniquePosition(null_pos);
1348 null_child_id = child.GetId();
1351 EXPECT_EQ(OPENED, SimulateSaveAndReloadDir());
1354 ReadTransaction trans(FROM_HERE, dir().get());
1356 Entry null_ordinal_child(&trans, GET_BY_ID, null_child_id);
1357 EXPECT_TRUE(null_pos.Equals(null_ordinal_child.GetUniquePosition()));
1358 EXPECT_TRUE(null_pos.Equals(null_ordinal_child.GetServerUniquePosition()));
1362 // Any item with BOOKMARKS in their local specifics should have a valid local
1363 // unique position. If there is an item in the loaded DB that does not match
1364 // this criteria, we consider the whole DB to be corrupt.
1365 TEST_F(SyncableDirectoryTest, BadPositionCountsAsCorruption) {
1366 TestIdFactory id_factory;
1369 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1371 MutableEntry parent(&trans, CREATE, BOOKMARKS, id_factory.root(), "parent");
1372 parent.PutIsDir(true);
1373 parent.PutIsUnsynced(true);
1375 // The code is littered with DCHECKs that try to stop us from doing what
1376 // we're about to do. Our work-around is to create a bookmark based on
1377 // a server update, then update its local specifics without updating its
1378 // local unique position.
1380 MutableEntry child(
1381 &trans, CREATE_NEW_UPDATE_ITEM, id_factory.MakeServer("child"));
1382 sync_pb::EntitySpecifics specifics;
1383 AddDefaultFieldValue(BOOKMARKS, &specifics);
1384 child.PutIsUnappliedUpdate(true);
1385 child.PutSpecifics(specifics);
1387 EXPECT_TRUE(child.ShouldMaintainPosition());
1388 EXPECT_TRUE(!child.GetUniquePosition().IsValid());
1391 EXPECT_EQ(FAILED_DATABASE_CORRUPT, SimulateSaveAndReloadDir());
1394 TEST_F(SyncableDirectoryTest, General) {
1395 int64 written_metahandle;
1396 const Id id = TestIdFactory::FromNumber(99);
1397 std::string name = "Jeff";
1398 // Test simple read operations on an empty DB.
1400 ReadTransaction rtrans(FROM_HERE, dir().get());
1401 Entry e(&rtrans, GET_BY_ID, id);
1402 ASSERT_FALSE(e.good()); // Hasn't been written yet.
1404 Directory::Metahandles child_handles;
1405 dir()->GetChildHandlesById(&rtrans, rtrans.root_id(), &child_handles);
1406 EXPECT_TRUE(child_handles.empty());
1409 // Test creating a new meta entry.
1411 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
1412 MutableEntry me(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), name);
1413 ASSERT_TRUE(me.good());
1414 me.PutId(id);
1415 me.PutBaseVersion(1);
1416 written_metahandle = me.GetMetahandle();
1419 // Test GetChildHandles* after something is now in the DB.
1420 // Also check that GET_BY_ID works.
1422 ReadTransaction rtrans(FROM_HERE, dir().get());
1423 Entry e(&rtrans, GET_BY_ID, id);
1424 ASSERT_TRUE(e.good());
1426 Directory::Metahandles child_handles;
1427 dir()->GetChildHandlesById(&rtrans, rtrans.root_id(), &child_handles);
1428 EXPECT_EQ(1u, child_handles.size());
1430 for (Directory::Metahandles::iterator i = child_handles.begin();
1431 i != child_handles.end(); ++i) {
1432 EXPECT_EQ(*i, written_metahandle);
1436 // Test writing data to an entity. Also check that GET_BY_HANDLE works.
1437 static const char s[] = "Hello World.";
1439 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1440 MutableEntry e(&trans, GET_BY_HANDLE, written_metahandle);
1441 ASSERT_TRUE(e.good());
1442 PutDataAsBookmarkFavicon(&trans, &e, s, sizeof(s));
1445 // Test reading back the contents that we just wrote.
1447 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1448 MutableEntry e(&trans, GET_BY_HANDLE, written_metahandle);
1449 ASSERT_TRUE(e.good());
1450 ExpectDataFromBookmarkFaviconEquals(&trans, &e, s, sizeof(s));
1453 // Verify it exists in the folder.
1455 ReadTransaction rtrans(FROM_HERE, dir().get());
1456 EXPECT_EQ(1, CountEntriesWithName(&rtrans, rtrans.root_id(), name));
1459 // Now delete it.
1461 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1462 MutableEntry e(&trans, GET_BY_HANDLE, written_metahandle);
1463 e.PutIsDel(true);
1465 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), name));
1468 dir()->SaveChanges();
1471 TEST_F(SyncableDirectoryTest, ChildrenOps) {
1472 int64 written_metahandle;
1473 const Id id = TestIdFactory::FromNumber(99);
1474 std::string name = "Jeff";
1476 ReadTransaction rtrans(FROM_HERE, dir().get());
1477 Entry e(&rtrans, GET_BY_ID, id);
1478 ASSERT_FALSE(e.good()); // Hasn't been written yet.
1480 Entry root(&rtrans, GET_BY_ID, rtrans.root_id());
1481 ASSERT_TRUE(root.good());
1482 EXPECT_FALSE(dir()->HasChildren(&rtrans, rtrans.root_id()));
1483 EXPECT_TRUE(root.GetFirstChildId().IsNull());
1487 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
1488 MutableEntry me(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), name);
1489 ASSERT_TRUE(me.good());
1490 me.PutId(id);
1491 me.PutBaseVersion(1);
1492 written_metahandle = me.GetMetahandle();
1495 // Test children ops after something is now in the DB.
1497 ReadTransaction rtrans(FROM_HERE, dir().get());
1498 Entry e(&rtrans, GET_BY_ID, id);
1499 ASSERT_TRUE(e.good());
1501 Entry child(&rtrans, GET_BY_HANDLE, written_metahandle);
1502 ASSERT_TRUE(child.good());
1504 Entry root(&rtrans, GET_BY_ID, rtrans.root_id());
1505 ASSERT_TRUE(root.good());
1506 EXPECT_TRUE(dir()->HasChildren(&rtrans, rtrans.root_id()));
1507 EXPECT_EQ(e.GetId(), root.GetFirstChildId());
1511 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
1512 MutableEntry me(&wtrans, GET_BY_HANDLE, written_metahandle);
1513 ASSERT_TRUE(me.good());
1514 me.PutIsDel(true);
1517 // Test children ops after the children have been deleted.
1519 ReadTransaction rtrans(FROM_HERE, dir().get());
1520 Entry e(&rtrans, GET_BY_ID, id);
1521 ASSERT_TRUE(e.good());
1523 Entry root(&rtrans, GET_BY_ID, rtrans.root_id());
1524 ASSERT_TRUE(root.good());
1525 EXPECT_FALSE(dir()->HasChildren(&rtrans, rtrans.root_id()));
1526 EXPECT_TRUE(root.GetFirstChildId().IsNull());
1529 dir()->SaveChanges();
1532 TEST_F(SyncableDirectoryTest, ClientIndexRebuildsProperly) {
1533 int64 written_metahandle;
1534 TestIdFactory factory;
1535 const Id id = factory.NewServerId();
1536 std::string name = "cheesepuffs";
1537 std::string tag = "dietcoke";
1539 // Test creating a new meta entry.
1541 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
1542 MutableEntry me(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), name);
1543 ASSERT_TRUE(me.good());
1544 me.PutId(id);
1545 me.PutBaseVersion(1);
1546 me.PutUniqueClientTag(tag);
1547 written_metahandle = me.GetMetahandle();
1549 dir()->SaveChanges();
1551 // Close and reopen, causing index regeneration.
1552 ReopenDirectory();
1554 ReadTransaction trans(FROM_HERE, dir().get());
1555 Entry me(&trans, GET_BY_CLIENT_TAG, tag);
1556 ASSERT_TRUE(me.good());
1557 EXPECT_EQ(me.GetId(), id);
1558 EXPECT_EQ(me.GetBaseVersion(), 1);
1559 EXPECT_EQ(me.GetUniqueClientTag(), tag);
1560 EXPECT_EQ(me.GetMetahandle(), written_metahandle);
1564 TEST_F(SyncableDirectoryTest, ClientIndexRebuildsDeletedProperly) {
1565 TestIdFactory factory;
1566 const Id id = factory.NewServerId();
1567 std::string tag = "dietcoke";
1569 // Test creating a deleted, unsynced, server meta entry.
1571 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
1572 MutableEntry me(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), "deleted");
1573 ASSERT_TRUE(me.good());
1574 me.PutId(id);
1575 me.PutBaseVersion(1);
1576 me.PutUniqueClientTag(tag);
1577 me.PutIsDel(true);
1578 me.PutIsUnsynced(true); // Or it might be purged.
1580 dir()->SaveChanges();
1582 // Close and reopen, causing index regeneration.
1583 ReopenDirectory();
1585 ReadTransaction trans(FROM_HERE, dir().get());
1586 Entry me(&trans, GET_BY_CLIENT_TAG, tag);
1587 // Should still be present and valid in the client tag index.
1588 ASSERT_TRUE(me.good());
1589 EXPECT_EQ(me.GetId(), id);
1590 EXPECT_EQ(me.GetUniqueClientTag(), tag);
1591 EXPECT_TRUE(me.GetIsDel());
1592 EXPECT_TRUE(me.GetIsUnsynced());
1596 TEST_F(SyncableDirectoryTest, ToValue) {
1597 const Id id = TestIdFactory::FromNumber(99);
1599 ReadTransaction rtrans(FROM_HERE, dir().get());
1600 Entry e(&rtrans, GET_BY_ID, id);
1601 EXPECT_FALSE(e.good()); // Hasn't been written yet.
1603 scoped_ptr<base::DictionaryValue> value(e.ToValue(NULL));
1604 ExpectDictBooleanValue(false, *value, "good");
1605 EXPECT_EQ(1u, value->size());
1608 // Test creating a new meta entry.
1610 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
1611 MutableEntry me(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), "new");
1612 ASSERT_TRUE(me.good());
1613 me.PutId(id);
1614 me.PutBaseVersion(1);
1616 scoped_ptr<base::DictionaryValue> value(me.ToValue(NULL));
1617 ExpectDictBooleanValue(true, *value, "good");
1618 EXPECT_TRUE(value->HasKey("kernel"));
1619 ExpectDictStringValue("Bookmarks", *value, "modelType");
1620 ExpectDictBooleanValue(true, *value, "existsOnClientBecauseNameIsNonEmpty");
1621 ExpectDictBooleanValue(false, *value, "isRoot");
1624 dir()->SaveChanges();
1627 // Test that the bookmark tag generation algorithm remains unchanged.
1628 TEST_F(SyncableDirectoryTest, BookmarkTagTest) {
1629 // This test needs its own InMemoryDirectoryBackingStore because it needs to
1630 // call request_consistent_cache_guid().
1631 InMemoryDirectoryBackingStore* store = new InMemoryDirectoryBackingStore("x");
1633 // The two inputs that form the bookmark tag are the directory's cache_guid
1634 // and its next_id value. We don't need to take any action to ensure
1635 // consistent next_id values, but we do need to explicitly request that our
1636 // InMemoryDirectoryBackingStore always return the same cache_guid.
1637 store->request_consistent_cache_guid();
1639 Directory dir(store, unrecoverable_error_handler(), NULL, NULL, NULL);
1640 ASSERT_EQ(
1641 OPENED,
1642 dir.Open("x", directory_change_delegate(), NullTransactionObserver()));
1645 WriteTransaction wtrans(FROM_HERE, UNITTEST, &dir);
1646 MutableEntry bm(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), "bm");
1647 bm.PutIsUnsynced(true);
1649 // If this assertion fails, that might indicate that the algorithm used to
1650 // generate bookmark tags has been modified. This could have implications
1651 // for bookmark ordering. Please make sure you know what you're doing if
1652 // you intend to make such a change.
1653 ASSERT_EQ("6wHRAb3kbnXV5GHrejp4/c1y5tw=", bm.GetUniqueBookmarkTag());
1657 // A thread that creates a bunch of directory entries.
1658 class StressTransactionsDelegate : public base::PlatformThread::Delegate {
1659 public:
1660 StressTransactionsDelegate(Directory* dir, int thread_number)
1661 : dir_(dir), thread_number_(thread_number) {}
1663 private:
1664 Directory* const dir_;
1665 const int thread_number_;
1667 // PlatformThread::Delegate methods:
1668 void ThreadMain() override {
1669 int entry_count = 0;
1670 std::string path_name;
1672 for (int i = 0; i < 20; ++i) {
1673 const int rand_action = rand() % 10;
1674 if (rand_action < 4 && !path_name.empty()) {
1675 ReadTransaction trans(FROM_HERE, dir_);
1676 CHECK(1 == CountEntriesWithName(&trans, trans.root_id(), path_name));
1677 base::PlatformThread::Sleep(
1678 base::TimeDelta::FromMilliseconds(rand() % 10));
1679 } else {
1680 std::string unique_name =
1681 base::StringPrintf("%d.%d", thread_number_, entry_count++);
1682 path_name.assign(unique_name.begin(), unique_name.end());
1683 WriteTransaction trans(FROM_HERE, UNITTEST, dir_);
1684 MutableEntry e(&trans, CREATE, BOOKMARKS, trans.root_id(), path_name);
1685 CHECK(e.good());
1686 base::PlatformThread::Sleep(
1687 base::TimeDelta::FromMilliseconds(rand() % 20));
1688 e.PutIsUnsynced(true);
1689 if (e.PutId(TestIdFactory::FromNumber(rand())) &&
1690 e.GetId().ServerKnows() && !e.GetId().IsRoot()) {
1691 e.PutBaseVersion(1);
1697 DISALLOW_COPY_AND_ASSIGN(StressTransactionsDelegate);
1700 // Stress test Directory by accessing it from several threads concurrently.
1701 TEST_F(SyncableDirectoryTest, StressTransactions) {
1702 const int kThreadCount = 7;
1703 base::PlatformThreadHandle threads[kThreadCount];
1704 scoped_ptr<StressTransactionsDelegate> thread_delegates[kThreadCount];
1706 for (int i = 0; i < kThreadCount; ++i) {
1707 thread_delegates[i].reset(new StressTransactionsDelegate(dir().get(), i));
1708 ASSERT_TRUE(base::PlatformThread::Create(
1709 0, thread_delegates[i].get(), &threads[i]));
1712 for (int i = 0; i < kThreadCount; ++i) {
1713 base::PlatformThread::Join(threads[i]);
1717 // Verify that Directory is notifed when a MutableEntry's AttachmentMetadata
1718 // changes.
1719 TEST_F(SyncableDirectoryTest, MutableEntry_PutAttachmentMetadata) {
1720 sync_pb::AttachmentMetadata attachment_metadata;
1721 sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record();
1722 sync_pb::AttachmentIdProto attachment_id_proto =
1723 syncer::CreateAttachmentIdProto(0, 0);
1724 *record->mutable_id() = attachment_id_proto;
1725 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1727 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1729 // Create an entry with attachment metadata and see that the attachment id
1730 // is not linked.
1731 MutableEntry entry(
1732 &trans, CREATE, PREFERENCES, trans.root_id(), "some entry");
1733 entry.PutId(TestIdFactory::FromNumber(-1));
1734 entry.PutIsUnsynced(true);
1736 Directory::Metahandles metahandles;
1737 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1738 dir()->GetMetahandlesByAttachmentId(
1739 &trans, attachment_id_proto, &metahandles);
1740 ASSERT_TRUE(metahandles.empty());
1742 // Now add the attachment metadata and see that Directory believes it is
1743 // linked.
1744 entry.PutAttachmentMetadata(attachment_metadata);
1745 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto));
1746 dir()->GetMetahandlesByAttachmentId(
1747 &trans, attachment_id_proto, &metahandles);
1748 ASSERT_FALSE(metahandles.empty());
1749 ASSERT_EQ(metahandles[0], entry.GetMetahandle());
1751 // Clear out the attachment metadata and see that it's no longer linked.
1752 sync_pb::AttachmentMetadata empty_attachment_metadata;
1753 entry.PutAttachmentMetadata(empty_attachment_metadata);
1754 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1755 dir()->GetMetahandlesByAttachmentId(
1756 &trans, attachment_id_proto, &metahandles);
1757 ASSERT_TRUE(metahandles.empty());
1759 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1762 // Verify that UpdateAttachmentId updates attachment_id and is_on_server flag.
1763 TEST_F(SyncableDirectoryTest, MutableEntry_UpdateAttachmentId) {
1764 sync_pb::AttachmentMetadata attachment_metadata;
1765 sync_pb::AttachmentMetadataRecord* r1 = attachment_metadata.add_record();
1766 sync_pb::AttachmentMetadataRecord* r2 = attachment_metadata.add_record();
1767 *r1->mutable_id() = syncer::CreateAttachmentIdProto(0, 0);
1768 *r2->mutable_id() = syncer::CreateAttachmentIdProto(0, 0);
1769 sync_pb::AttachmentIdProto attachment_id_proto = r1->id();
1771 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1773 MutableEntry entry(
1774 &trans, CREATE, PREFERENCES, trans.root_id(), "some entry");
1775 entry.PutId(TestIdFactory::FromNumber(-1));
1776 entry.PutAttachmentMetadata(attachment_metadata);
1778 const sync_pb::AttachmentMetadata& entry_metadata =
1779 entry.GetAttachmentMetadata();
1780 ASSERT_EQ(2, entry_metadata.record_size());
1781 ASSERT_FALSE(entry_metadata.record(0).is_on_server());
1782 ASSERT_FALSE(entry_metadata.record(1).is_on_server());
1783 ASSERT_FALSE(entry.GetIsUnsynced());
1785 entry.MarkAttachmentAsOnServer(attachment_id_proto);
1787 ASSERT_TRUE(entry_metadata.record(0).is_on_server());
1788 ASSERT_FALSE(entry_metadata.record(1).is_on_server());
1789 ASSERT_TRUE(entry.GetIsUnsynced());
1792 // Verify that deleted entries with attachments will retain the attachments.
1793 TEST_F(SyncableDirectoryTest, Directory_DeleteDoesNotUnlinkAttachments) {
1794 sync_pb::AttachmentMetadata attachment_metadata;
1795 sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record();
1796 sync_pb::AttachmentIdProto attachment_id_proto =
1797 syncer::CreateAttachmentIdProto(0, 0);
1798 *record->mutable_id() = attachment_id_proto;
1799 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1800 const Id id = TestIdFactory::FromNumber(-1);
1802 // Create an entry with attachment metadata and see that the attachment id
1803 // is linked.
1804 CreateEntryWithAttachmentMetadata(
1805 PREFERENCES, "some entry", id, attachment_metadata);
1806 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto));
1808 // Delete the entry and see that it's still linked because the entry hasn't
1809 // yet been purged.
1810 DeleteEntry(id);
1811 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto));
1813 // Reload the Directory, purging the deleted entry, and see that the
1814 // attachment is no longer linked.
1815 SimulateSaveAndReloadDir();
1816 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1819 // Verify that a given attachment can be referenced by multiple entries and that
1820 // any one of the references is sufficient to ensure it remains linked.
1821 TEST_F(SyncableDirectoryTest, Directory_LastReferenceUnlinksAttachments) {
1822 // Create one attachment.
1823 sync_pb::AttachmentMetadata attachment_metadata;
1824 sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record();
1825 sync_pb::AttachmentIdProto attachment_id_proto =
1826 syncer::CreateAttachmentIdProto(0, 0);
1827 *record->mutable_id() = attachment_id_proto;
1829 // Create two entries, each referencing the attachment.
1830 const Id id1 = TestIdFactory::FromNumber(-1);
1831 const Id id2 = TestIdFactory::FromNumber(-2);
1832 CreateEntryWithAttachmentMetadata(
1833 PREFERENCES, "some entry", id1, attachment_metadata);
1834 CreateEntryWithAttachmentMetadata(
1835 PREFERENCES, "some other entry", id2, attachment_metadata);
1837 // See that the attachment is considered linked.
1838 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto));
1840 // Delete the first entry, reload the Directory, see that the attachment is
1841 // still linked.
1842 DeleteEntry(id1);
1843 SimulateSaveAndReloadDir();
1844 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto));
1846 // Delete the second entry, reload the Directory, see that the attachment is
1847 // no loner linked.
1848 DeleteEntry(id2);
1849 SimulateSaveAndReloadDir();
1850 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1853 TEST_F(SyncableDirectoryTest, Directory_GetAttachmentIdsToUpload) {
1854 // Create one attachment, referenced by two entries.
1855 AttachmentId attachment_id = AttachmentId::Create(0, 0);
1856 sync_pb::AttachmentIdProto attachment_id_proto = attachment_id.GetProto();
1857 sync_pb::AttachmentMetadata attachment_metadata;
1858 sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record();
1859 *record->mutable_id() = attachment_id_proto;
1860 const Id id1 = TestIdFactory::FromNumber(-1);
1861 const Id id2 = TestIdFactory::FromNumber(-2);
1862 CreateEntryWithAttachmentMetadata(
1863 PREFERENCES, "some entry", id1, attachment_metadata);
1864 CreateEntryWithAttachmentMetadata(
1865 PREFERENCES, "some other entry", id2, attachment_metadata);
1867 // See that Directory reports that this attachment is not on the server.
1868 AttachmentIdList ids;
1870 ReadTransaction trans(FROM_HERE, dir().get());
1871 dir()->GetAttachmentIdsToUpload(&trans, PREFERENCES, &ids);
1873 ASSERT_EQ(1U, ids.size());
1874 ASSERT_EQ(attachment_id, *ids.begin());
1876 // Call again, but this time with a ModelType for which there are no entries.
1877 // See that Directory correctly reports that there are none.
1879 ReadTransaction trans(FROM_HERE, dir().get());
1880 dir()->GetAttachmentIdsToUpload(&trans, PASSWORDS, &ids);
1882 ASSERT_TRUE(ids.empty());
1884 // Now, mark the attachment as "on the server" via entry_1.
1886 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1887 MutableEntry entry_1(&trans, GET_BY_ID, id1);
1888 entry_1.MarkAttachmentAsOnServer(attachment_id_proto);
1891 // See that Directory no longer reports that this attachment is not on the
1892 // server.
1894 ReadTransaction trans(FROM_HERE, dir().get());
1895 dir()->GetAttachmentIdsToUpload(&trans, PREFERENCES, &ids);
1897 ASSERT_TRUE(ids.empty());
1900 // Verify that the directory accepts entries with unset parent ID.
1901 TEST_F(SyncableDirectoryTest, MutableEntry_ImplicitParentId) {
1902 TestIdFactory id_factory;
1903 const Id root_id = TestIdFactory::root();
1904 const Id p_root_id = id_factory.NewServerId();
1905 const Id a_root_id = id_factory.NewServerId();
1906 const Id item1_id = id_factory.NewServerId();
1907 const Id item2_id = id_factory.NewServerId();
1908 const Id item3_id = id_factory.NewServerId();
1909 // Create two type root folders that are necessary (for now)
1910 // for creating items without explicitly set Parent ID
1912 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1913 MutableEntry p_root(&trans, CREATE, PREFERENCES, root_id, "P");
1914 ASSERT_TRUE(p_root.good());
1915 p_root.PutIsDir(true);
1916 p_root.PutId(p_root_id);
1917 p_root.PutBaseVersion(1);
1919 MutableEntry a_root(&trans, CREATE, AUTOFILL, root_id, "A");
1920 ASSERT_TRUE(a_root.good());
1921 a_root.PutIsDir(true);
1922 a_root.PutId(a_root_id);
1923 a_root.PutBaseVersion(1);
1926 // Create two entries with implicit parent nodes and one entry with explicit
1927 // parent node.
1929 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1930 MutableEntry item1(&trans, CREATE, PREFERENCES, "P1");
1931 item1.PutBaseVersion(1);
1932 item1.PutId(item1_id);
1933 MutableEntry item2(&trans, CREATE, AUTOFILL, "A1");
1934 item2.PutBaseVersion(1);
1935 item2.PutId(item2_id);
1936 // Placing an AUTOFILL item under the root isn't expected,
1937 // but let's test it to verify that explicit root overrides the implicit
1938 // one and this entry doesn't end up under the "A" root.
1939 MutableEntry item3(&trans, CREATE, AUTOFILL, root_id, "A2");
1940 item3.PutBaseVersion(1);
1941 item3.PutId(item3_id);
1945 ReadTransaction trans(FROM_HERE, dir().get());
1946 // Verify that item1 and item2 are good and have no ParentId.
1947 Entry item1(&trans, GET_BY_ID, item1_id);
1948 ASSERT_TRUE(item1.good());
1949 ASSERT_TRUE(item1.GetParentId().IsNull());
1950 Entry item2(&trans, GET_BY_ID, item2_id);
1951 ASSERT_TRUE(item2.good());
1952 ASSERT_TRUE(item2.GetParentId().IsNull());
1953 // Verify that p_root and a_root have exactly one child each
1954 // (subtract one to exclude roots themselves).
1955 Entry p_root(&trans, GET_BY_ID, p_root_id);
1956 ASSERT_EQ(item1_id, p_root.GetFirstChildId());
1957 ASSERT_EQ(1, p_root.GetTotalNodeCount() - 1);
1958 Entry a_root(&trans, GET_BY_ID, a_root_id);
1959 ASSERT_EQ(item2_id, a_root.GetFirstChildId());
1960 ASSERT_EQ(1, a_root.GetTotalNodeCount() - 1);
1964 // Verify that the successor / predecessor navigation still works for
1965 // directory entries with unset Parent IDs.
1966 TEST_F(SyncableDirectoryTest, MutableEntry_ImplicitParentId_Siblings) {
1967 TestIdFactory id_factory;
1968 const Id root_id = TestIdFactory::root();
1969 const Id p_root_id = id_factory.NewServerId();
1970 const Id item1_id = id_factory.FromNumber(1);
1971 const Id item2_id = id_factory.FromNumber(2);
1973 // Create type root folder for PREFERENCES.
1975 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1976 MutableEntry p_root(&trans, CREATE, PREFERENCES, root_id, "P");
1977 ASSERT_TRUE(p_root.good());
1978 p_root.PutIsDir(true);
1979 p_root.PutId(p_root_id);
1980 p_root.PutBaseVersion(1);
1983 // Create two PREFERENCES entries with implicit parent nodes.
1985 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1986 MutableEntry item1(&trans, CREATE, PREFERENCES, "P1");
1987 item1.PutBaseVersion(1);
1988 item1.PutId(item1_id);
1989 MutableEntry item2(&trans, CREATE, PREFERENCES, "P2");
1990 item2.PutBaseVersion(1);
1991 item2.PutId(item2_id);
1994 // Verify GetSuccessorId and GetPredecessorId calls for these items.
1995 // Please note that items are sorted according to their ID, e.g.
1996 // item1 first, then item2.
1998 ReadTransaction trans(FROM_HERE, dir().get());
1999 Entry item1(&trans, GET_BY_ID, item1_id);
2000 EXPECT_EQ(Id(), item1.GetPredecessorId());
2001 EXPECT_EQ(item2_id, item1.GetSuccessorId());
2003 Entry item2(&trans, GET_BY_ID, item2_id);
2004 EXPECT_EQ(item1_id, item2.GetPredecessorId());
2005 EXPECT_EQ(Id(), item2.GetSuccessorId());
2009 } // namespace syncable
2011 } // namespace syncer