ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / sync / syncable / directory_unittest.cc
blob64f3b57f3fbc42f2a51edef6e4ac09db07c3d891
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, TestBasicLookupNonExistantID) {
556 ReadTransaction rtrans(FROM_HERE, dir().get());
557 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99));
558 ASSERT_FALSE(e.good());
561 TEST_F(SyncableDirectoryTest, TestBasicLookupValidID) {
562 CreateEntry(BOOKMARKS, "rtc");
563 ReadTransaction rtrans(FROM_HERE, dir().get());
564 Entry e(&rtrans, GET_BY_ID, TestIdFactory::FromNumber(-99));
565 ASSERT_TRUE(e.good());
568 TEST_F(SyncableDirectoryTest, TestDelete) {
569 std::string name = "peanut butter jelly time";
570 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
571 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), name);
572 ASSERT_TRUE(e1.good());
573 e1.PutIsDel(true);
574 MutableEntry e2(&trans, CREATE, BOOKMARKS, trans.root_id(), name);
575 ASSERT_TRUE(e2.good());
576 e2.PutIsDel(true);
577 MutableEntry e3(&trans, CREATE, BOOKMARKS, trans.root_id(), name);
578 ASSERT_TRUE(e3.good());
579 e3.PutIsDel(true);
581 e1.PutIsDel(false);
582 e2.PutIsDel(false);
583 e3.PutIsDel(false);
585 e1.PutIsDel(true);
586 e2.PutIsDel(true);
587 e3.PutIsDel(true);
590 TEST_F(SyncableDirectoryTest, TestGetUnsynced) {
591 Directory::Metahandles handles;
592 int64 handle1, handle2;
594 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
596 dir()->GetUnsyncedMetaHandles(&trans, &handles);
597 ASSERT_TRUE(0 == handles.size());
599 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), "abba");
600 ASSERT_TRUE(e1.good());
601 handle1 = e1.GetMetahandle();
602 e1.PutBaseVersion(1);
603 e1.PutIsDir(true);
604 e1.PutId(TestIdFactory::FromNumber(101));
606 MutableEntry e2(&trans, CREATE, BOOKMARKS, e1.GetId(), "bread");
607 ASSERT_TRUE(e2.good());
608 handle2 = e2.GetMetahandle();
609 e2.PutBaseVersion(1);
610 e2.PutId(TestIdFactory::FromNumber(102));
612 dir()->SaveChanges();
614 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
616 dir()->GetUnsyncedMetaHandles(&trans, &handles);
617 ASSERT_TRUE(0 == handles.size());
619 MutableEntry e3(&trans, GET_BY_HANDLE, handle1);
620 ASSERT_TRUE(e3.good());
621 e3.PutIsUnsynced(true);
623 dir()->SaveChanges();
625 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
626 dir()->GetUnsyncedMetaHandles(&trans, &handles);
627 ASSERT_TRUE(1 == handles.size());
628 ASSERT_TRUE(handle1 == handles[0]);
630 MutableEntry e4(&trans, GET_BY_HANDLE, handle2);
631 ASSERT_TRUE(e4.good());
632 e4.PutIsUnsynced(true);
634 dir()->SaveChanges();
636 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
637 dir()->GetUnsyncedMetaHandles(&trans, &handles);
638 ASSERT_TRUE(2 == handles.size());
639 if (handle1 == handles[0]) {
640 ASSERT_TRUE(handle2 == handles[1]);
641 } else {
642 ASSERT_TRUE(handle2 == handles[0]);
643 ASSERT_TRUE(handle1 == handles[1]);
646 MutableEntry e5(&trans, GET_BY_HANDLE, handle1);
647 ASSERT_TRUE(e5.good());
648 ASSERT_TRUE(e5.GetIsUnsynced());
649 ASSERT_TRUE(e5.PutIsUnsynced(false));
650 ASSERT_FALSE(e5.GetIsUnsynced());
652 dir()->SaveChanges();
654 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
655 dir()->GetUnsyncedMetaHandles(&trans, &handles);
656 ASSERT_TRUE(1 == handles.size());
657 ASSERT_TRUE(handle2 == handles[0]);
661 TEST_F(SyncableDirectoryTest, TestGetUnappliedUpdates) {
662 std::vector<int64> handles;
663 int64 handle1, handle2;
664 const FullModelTypeSet all_types = FullModelTypeSet::All();
666 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
668 dir()->GetUnappliedUpdateMetaHandles(&trans, all_types, &handles);
669 ASSERT_TRUE(0 == handles.size());
671 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), "abba");
672 ASSERT_TRUE(e1.good());
673 handle1 = e1.GetMetahandle();
674 e1.PutIsUnappliedUpdate(false);
675 e1.PutBaseVersion(1);
676 e1.PutId(TestIdFactory::FromNumber(101));
677 e1.PutIsDir(true);
679 MutableEntry e2(&trans, CREATE, BOOKMARKS, e1.GetId(), "bread");
680 ASSERT_TRUE(e2.good());
681 handle2 = e2.GetMetahandle();
682 e2.PutIsUnappliedUpdate(false);
683 e2.PutBaseVersion(1);
684 e2.PutId(TestIdFactory::FromNumber(102));
686 dir()->SaveChanges();
688 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
690 dir()->GetUnappliedUpdateMetaHandles(&trans, all_types, &handles);
691 ASSERT_TRUE(0 == handles.size());
693 MutableEntry e3(&trans, GET_BY_HANDLE, handle1);
694 ASSERT_TRUE(e3.good());
695 e3.PutIsUnappliedUpdate(true);
697 dir()->SaveChanges();
699 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
700 dir()->GetUnappliedUpdateMetaHandles(&trans, all_types, &handles);
701 ASSERT_TRUE(1 == handles.size());
702 ASSERT_TRUE(handle1 == handles[0]);
704 MutableEntry e4(&trans, GET_BY_HANDLE, handle2);
705 ASSERT_TRUE(e4.good());
706 e4.PutIsUnappliedUpdate(true);
708 dir()->SaveChanges();
710 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
711 dir()->GetUnappliedUpdateMetaHandles(&trans, all_types, &handles);
712 ASSERT_TRUE(2 == handles.size());
713 if (handle1 == handles[0]) {
714 ASSERT_TRUE(handle2 == handles[1]);
715 } else {
716 ASSERT_TRUE(handle2 == handles[0]);
717 ASSERT_TRUE(handle1 == handles[1]);
720 MutableEntry e5(&trans, GET_BY_HANDLE, handle1);
721 ASSERT_TRUE(e5.good());
722 e5.PutIsUnappliedUpdate(false);
724 dir()->SaveChanges();
726 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
727 dir()->GetUnappliedUpdateMetaHandles(&trans, all_types, &handles);
728 ASSERT_TRUE(1 == handles.size());
729 ASSERT_TRUE(handle2 == handles[0]);
733 TEST_F(SyncableDirectoryTest, DeleteBug_531383) {
734 // Try to evoke a check failure...
735 TestIdFactory id_factory;
736 int64 grandchild_handle;
738 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
739 MutableEntry parent(&wtrans, CREATE, BOOKMARKS, id_factory.root(), "Bob");
740 ASSERT_TRUE(parent.good());
741 parent.PutIsDir(true);
742 parent.PutId(id_factory.NewServerId());
743 parent.PutBaseVersion(1);
744 MutableEntry child(&wtrans, CREATE, BOOKMARKS, parent.GetId(), "Bob");
745 ASSERT_TRUE(child.good());
746 child.PutIsDir(true);
747 child.PutId(id_factory.NewServerId());
748 child.PutBaseVersion(1);
749 MutableEntry grandchild(&wtrans, CREATE, BOOKMARKS, child.GetId(), "Bob");
750 ASSERT_TRUE(grandchild.good());
751 grandchild.PutId(id_factory.NewServerId());
752 grandchild.PutBaseVersion(1);
753 grandchild.PutIsDel(true);
754 MutableEntry twin(&wtrans, CREATE, BOOKMARKS, child.GetId(), "Bob");
755 ASSERT_TRUE(twin.good());
756 twin.PutIsDel(true);
757 grandchild.PutIsDel(false);
759 grandchild_handle = grandchild.GetMetahandle();
761 dir()->SaveChanges();
763 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
764 MutableEntry grandchild(&wtrans, GET_BY_HANDLE, grandchild_handle);
765 grandchild.PutIsDel(true); // Used to CHECK fail here.
769 TEST_F(SyncableDirectoryTest, TestIsLegalNewParent) {
770 TestIdFactory id_factory;
771 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
772 Entry root(&wtrans, GET_BY_ID, id_factory.root());
773 ASSERT_TRUE(root.good());
774 MutableEntry parent(&wtrans, CREATE, BOOKMARKS, root.GetId(), "Bob");
775 ASSERT_TRUE(parent.good());
776 parent.PutIsDir(true);
777 parent.PutId(id_factory.NewServerId());
778 parent.PutBaseVersion(1);
779 MutableEntry child(&wtrans, CREATE, BOOKMARKS, parent.GetId(), "Bob");
780 ASSERT_TRUE(child.good());
781 child.PutIsDir(true);
782 child.PutId(id_factory.NewServerId());
783 child.PutBaseVersion(1);
784 MutableEntry grandchild(&wtrans, CREATE, BOOKMARKS, child.GetId(), "Bob");
785 ASSERT_TRUE(grandchild.good());
786 grandchild.PutId(id_factory.NewServerId());
787 grandchild.PutBaseVersion(1);
789 MutableEntry parent2(&wtrans, CREATE, BOOKMARKS, root.GetId(), "Pete");
790 ASSERT_TRUE(parent2.good());
791 parent2.PutIsDir(true);
792 parent2.PutId(id_factory.NewServerId());
793 parent2.PutBaseVersion(1);
794 MutableEntry child2(&wtrans, CREATE, BOOKMARKS, parent2.GetId(), "Pete");
795 ASSERT_TRUE(child2.good());
796 child2.PutIsDir(true);
797 child2.PutId(id_factory.NewServerId());
798 child2.PutBaseVersion(1);
799 MutableEntry grandchild2(&wtrans, CREATE, BOOKMARKS, child2.GetId(), "Pete");
800 ASSERT_TRUE(grandchild2.good());
801 grandchild2.PutId(id_factory.NewServerId());
802 grandchild2.PutBaseVersion(1);
803 // resulting tree
804 // root
805 // / |
806 // parent parent2
807 // | |
808 // child child2
809 // | |
810 // grandchild grandchild2
811 ASSERT_TRUE(IsLegalNewParent(child, root));
812 ASSERT_TRUE(IsLegalNewParent(child, parent));
813 ASSERT_FALSE(IsLegalNewParent(child, child));
814 ASSERT_FALSE(IsLegalNewParent(child, grandchild));
815 ASSERT_TRUE(IsLegalNewParent(child, parent2));
816 ASSERT_TRUE(IsLegalNewParent(child, grandchild2));
817 ASSERT_FALSE(IsLegalNewParent(parent, grandchild));
818 ASSERT_FALSE(IsLegalNewParent(root, grandchild));
819 ASSERT_FALSE(IsLegalNewParent(parent, grandchild));
822 TEST_F(SyncableDirectoryTest, TestEntryIsInFolder) {
823 // Create a subdir and an entry.
824 int64 entry_handle;
825 syncable::Id folder_id;
826 syncable::Id entry_id;
827 std::string entry_name = "entry";
830 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
831 MutableEntry folder(&trans, CREATE, BOOKMARKS, trans.root_id(), "folder");
832 ASSERT_TRUE(folder.good());
833 folder.PutIsDir(true);
834 EXPECT_TRUE(folder.PutIsUnsynced(true));
835 folder_id = folder.GetId();
837 MutableEntry entry(&trans, CREATE, BOOKMARKS, folder.GetId(), entry_name);
838 ASSERT_TRUE(entry.good());
839 entry_handle = entry.GetMetahandle();
840 entry.PutIsUnsynced(true);
841 entry_id = entry.GetId();
844 // Make sure we can find the entry in the folder.
846 ReadTransaction trans(FROM_HERE, dir().get());
847 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), entry_name));
848 EXPECT_EQ(1, CountEntriesWithName(&trans, folder_id, entry_name));
850 Entry entry(&trans, GET_BY_ID, entry_id);
851 ASSERT_TRUE(entry.good());
852 EXPECT_EQ(entry_handle, entry.GetMetahandle());
853 EXPECT_TRUE(entry.GetNonUniqueName() == entry_name);
854 EXPECT_TRUE(entry.GetParentId() == folder_id);
858 TEST_F(SyncableDirectoryTest, TestParentIdIndexUpdate) {
859 std::string child_name = "child";
861 WriteTransaction wt(FROM_HERE, UNITTEST, dir().get());
862 MutableEntry parent_folder(&wt, CREATE, BOOKMARKS, wt.root_id(), "folder1");
863 parent_folder.PutIsUnsynced(true);
864 parent_folder.PutIsDir(true);
866 MutableEntry parent_folder2(&wt, CREATE, BOOKMARKS, wt.root_id(), "folder2");
867 parent_folder2.PutIsUnsynced(true);
868 parent_folder2.PutIsDir(true);
870 MutableEntry child(&wt, CREATE, BOOKMARKS, parent_folder.GetId(), child_name);
871 child.PutIsDir(true);
872 child.PutIsUnsynced(true);
874 ASSERT_TRUE(child.good());
876 EXPECT_EQ(0, CountEntriesWithName(&wt, wt.root_id(), child_name));
877 EXPECT_EQ(parent_folder.GetId(), child.GetParentId());
878 EXPECT_EQ(1, CountEntriesWithName(&wt, parent_folder.GetId(), child_name));
879 EXPECT_EQ(0, CountEntriesWithName(&wt, parent_folder2.GetId(), child_name));
880 child.PutParentId(parent_folder2.GetId());
881 EXPECT_EQ(parent_folder2.GetId(), child.GetParentId());
882 EXPECT_EQ(0, CountEntriesWithName(&wt, parent_folder.GetId(), child_name));
883 EXPECT_EQ(1, CountEntriesWithName(&wt, parent_folder2.GetId(), child_name));
886 TEST_F(SyncableDirectoryTest, TestNoReindexDeletedItems) {
887 std::string folder_name = "folder";
888 std::string new_name = "new_name";
890 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
891 MutableEntry folder(&trans, CREATE, BOOKMARKS, trans.root_id(), folder_name);
892 ASSERT_TRUE(folder.good());
893 folder.PutIsDir(true);
894 folder.PutIsDel(true);
896 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), folder_name));
898 MutableEntry deleted(&trans, GET_BY_ID, folder.GetId());
899 ASSERT_TRUE(deleted.good());
900 deleted.PutParentId(trans.root_id());
901 deleted.PutNonUniqueName(new_name);
903 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), folder_name));
904 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), new_name));
907 TEST_F(SyncableDirectoryTest, TestCaseChangeRename) {
908 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
909 MutableEntry folder(&trans, CREATE, BOOKMARKS, trans.root_id(), "CaseChange");
910 ASSERT_TRUE(folder.good());
911 folder.PutParentId(trans.root_id());
912 folder.PutNonUniqueName("CASECHANGE");
913 folder.PutIsDel(true);
916 // Create items of each model type, and check that GetModelType and
917 // GetServerModelType return the right value.
918 TEST_F(SyncableDirectoryTest, GetModelType) {
919 TestIdFactory id_factory;
920 ModelTypeSet protocol_types = ProtocolTypes();
921 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good();
922 iter.Inc()) {
923 ModelType datatype = iter.Get();
924 SCOPED_TRACE(testing::Message("Testing model type ") << datatype);
925 switch (datatype) {
926 case UNSPECIFIED:
927 case TOP_LEVEL_FOLDER:
928 continue; // Datatype isn't a function of Specifics.
929 default:
930 break;
932 sync_pb::EntitySpecifics specifics;
933 AddDefaultFieldValue(datatype, &specifics);
935 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
937 MutableEntry folder(&trans, CREATE, BOOKMARKS, trans.root_id(), "Folder");
938 ASSERT_TRUE(folder.good());
939 folder.PutId(id_factory.NewServerId());
940 folder.PutSpecifics(specifics);
941 folder.PutBaseVersion(1);
942 folder.PutIsDir(true);
943 folder.PutIsDel(false);
944 ASSERT_EQ(datatype, folder.GetModelType());
946 MutableEntry item(&trans, CREATE, BOOKMARKS, trans.root_id(), "Item");
947 ASSERT_TRUE(item.good());
948 item.PutId(id_factory.NewServerId());
949 item.PutSpecifics(specifics);
950 item.PutBaseVersion(1);
951 item.PutIsDir(false);
952 item.PutIsDel(false);
953 ASSERT_EQ(datatype, item.GetModelType());
955 // It's critical that deletion records retain their datatype, so that
956 // they can be dispatched to the appropriate change processor.
957 MutableEntry deleted_item(
958 &trans, CREATE, BOOKMARKS, trans.root_id(), "Deleted Item");
959 ASSERT_TRUE(item.good());
960 deleted_item.PutId(id_factory.NewServerId());
961 deleted_item.PutSpecifics(specifics);
962 deleted_item.PutBaseVersion(1);
963 deleted_item.PutIsDir(false);
964 deleted_item.PutIsDel(true);
965 ASSERT_EQ(datatype, deleted_item.GetModelType());
967 MutableEntry server_folder(
968 &trans, CREATE_NEW_UPDATE_ITEM, id_factory.NewServerId());
969 ASSERT_TRUE(server_folder.good());
970 server_folder.PutServerSpecifics(specifics);
971 server_folder.PutBaseVersion(1);
972 server_folder.PutServerIsDir(true);
973 server_folder.PutServerIsDel(false);
974 ASSERT_EQ(datatype, server_folder.GetServerModelType());
976 MutableEntry server_item(
977 &trans, CREATE_NEW_UPDATE_ITEM, id_factory.NewServerId());
978 ASSERT_TRUE(server_item.good());
979 server_item.PutServerSpecifics(specifics);
980 server_item.PutBaseVersion(1);
981 server_item.PutServerIsDir(false);
982 server_item.PutServerIsDel(false);
983 ASSERT_EQ(datatype, server_item.GetServerModelType());
985 sync_pb::SyncEntity folder_entity;
986 folder_entity.set_id_string(SyncableIdToProto(id_factory.NewServerId()));
987 folder_entity.set_deleted(false);
988 folder_entity.set_folder(true);
989 folder_entity.mutable_specifics()->CopyFrom(specifics);
990 ASSERT_EQ(datatype, GetModelType(folder_entity));
992 sync_pb::SyncEntity item_entity;
993 item_entity.set_id_string(SyncableIdToProto(id_factory.NewServerId()));
994 item_entity.set_deleted(false);
995 item_entity.set_folder(false);
996 item_entity.mutable_specifics()->CopyFrom(specifics);
997 ASSERT_EQ(datatype, GetModelType(item_entity));
1001 // A test that roughly mimics the directory interaction that occurs when a
1002 // bookmark folder and entry are created then synced for the first time. It is
1003 // a more common variant of the 'DeletedAndUnsyncedChild' scenario tested below.
1004 TEST_F(SyncableDirectoryTest, ChangeEntryIDAndUpdateChildren_ParentAndChild) {
1005 TestIdFactory id_factory;
1006 Id orig_parent_id;
1007 Id orig_child_id;
1010 // Create two client-side items, a parent and child.
1011 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1013 MutableEntry parent(&trans, CREATE, BOOKMARKS, id_factory.root(), "parent");
1014 parent.PutIsDir(true);
1015 parent.PutIsUnsynced(true);
1017 MutableEntry child(&trans, CREATE, BOOKMARKS, parent.GetId(), "child");
1018 child.PutIsUnsynced(true);
1020 orig_parent_id = parent.GetId();
1021 orig_child_id = child.GetId();
1025 // Simulate what happens after committing two items. Their IDs will be
1026 // replaced with server IDs. The child is renamed first, then the parent.
1027 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1029 MutableEntry parent(&trans, GET_BY_ID, orig_parent_id);
1030 MutableEntry child(&trans, GET_BY_ID, orig_child_id);
1032 ChangeEntryIDAndUpdateChildren(&trans, &child, id_factory.NewServerId());
1033 child.PutIsUnsynced(false);
1034 child.PutBaseVersion(1);
1035 child.PutServerVersion(1);
1037 ChangeEntryIDAndUpdateChildren(&trans, &parent, id_factory.NewServerId());
1038 parent.PutIsUnsynced(false);
1039 parent.PutBaseVersion(1);
1040 parent.PutServerVersion(1);
1043 // Final check for validity.
1044 EXPECT_EQ(OPENED, SimulateSaveAndReloadDir());
1047 // A test that roughly mimics the directory interaction that occurs when a
1048 // type root folder is created locally and then re-created (updated) from the
1049 // server.
1050 TEST_F(SyncableDirectoryTest, ChangeEntryIDAndUpdateChildren_ImplicitParent) {
1051 TestIdFactory id_factory;
1052 Id orig_parent_id;
1053 Id child_id;
1056 // Create two client-side items, a parent and child.
1057 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1059 MutableEntry parent(&trans, CREATE, PREFERENCES, id_factory.root(),
1060 "parent");
1061 parent.PutIsDir(true);
1062 parent.PutIsUnsynced(true);
1064 // The child has unset parent ID. The parent is inferred from the type.
1065 MutableEntry child(&trans, CREATE, PREFERENCES, "child");
1066 child.PutIsUnsynced(true);
1068 orig_parent_id = parent.GetId();
1069 child_id = child.GetId();
1073 // Simulate what happens after committing two items. Their IDs will be
1074 // replaced with server IDs. The child is renamed first, then the parent.
1075 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1077 MutableEntry parent(&trans, GET_BY_ID, orig_parent_id);
1079 ChangeEntryIDAndUpdateChildren(&trans, &parent, id_factory.NewServerId());
1080 parent.PutIsUnsynced(false);
1081 parent.PutBaseVersion(1);
1082 parent.PutServerVersion(1);
1085 // Final check for validity.
1086 EXPECT_EQ(OPENED, SimulateSaveAndReloadDir());
1088 // Verify that child's PARENT_ID hasn't been updated.
1090 ReadTransaction trans(FROM_HERE, dir().get());
1091 Entry child(&trans, GET_BY_ID, child_id);
1092 EXPECT_TRUE(child.good());
1093 EXPECT_TRUE(child.GetParentId().IsNull());
1097 // A test based on the scenario where we create a bookmark folder and entry
1098 // locally, but with a twist. In this case, the bookmark is deleted before we
1099 // are able to sync either it or its parent folder. This scenario used to cause
1100 // directory corruption, see crbug.com/125381.
1101 TEST_F(SyncableDirectoryTest,
1102 ChangeEntryIDAndUpdateChildren_DeletedAndUnsyncedChild) {
1103 TestIdFactory id_factory;
1104 Id orig_parent_id;
1105 Id orig_child_id;
1108 // Create two client-side items, a parent and child.
1109 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1111 MutableEntry parent(&trans, CREATE, BOOKMARKS, id_factory.root(), "parent");
1112 parent.PutIsDir(true);
1113 parent.PutIsUnsynced(true);
1115 MutableEntry child(&trans, CREATE, BOOKMARKS, parent.GetId(), "child");
1116 child.PutIsUnsynced(true);
1118 orig_parent_id = parent.GetId();
1119 orig_child_id = child.GetId();
1123 // Delete the child.
1124 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1126 MutableEntry child(&trans, GET_BY_ID, orig_child_id);
1127 child.PutIsDel(true);
1131 // Simulate what happens after committing the parent. Its ID will be
1132 // replaced with server a ID.
1133 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1135 MutableEntry parent(&trans, GET_BY_ID, orig_parent_id);
1137 ChangeEntryIDAndUpdateChildren(&trans, &parent, id_factory.NewServerId());
1138 parent.PutIsUnsynced(false);
1139 parent.PutBaseVersion(1);
1140 parent.PutServerVersion(1);
1143 // Final check for validity.
1144 EXPECT_EQ(OPENED, SimulateSaveAndReloadDir());
1147 // Ask the directory to generate a unique ID. Close and re-open the database
1148 // without saving, then ask for another unique ID. Verify IDs are not reused.
1149 // This scenario simulates a crash within the first few seconds of operation.
1150 TEST_F(SyncableDirectoryTest, LocalIdReuseTest) {
1151 Id pre_crash_id = dir()->NextId();
1152 SimulateCrashAndReloadDir();
1153 Id post_crash_id = dir()->NextId();
1154 EXPECT_NE(pre_crash_id, post_crash_id);
1157 // Ask the directory to generate a unique ID. Save the directory. Close and
1158 // re-open the database without saving, then ask for another unique ID. Verify
1159 // IDs are not reused. This scenario simulates a steady-state crash.
1160 TEST_F(SyncableDirectoryTest, LocalIdReuseTestWithSave) {
1161 Id pre_crash_id = dir()->NextId();
1162 dir()->SaveChanges();
1163 SimulateCrashAndReloadDir();
1164 Id post_crash_id = dir()->NextId();
1165 EXPECT_NE(pre_crash_id, post_crash_id);
1168 // Ensure that the unsynced, is_del and server unkown entries that may have been
1169 // left in the database by old clients will be deleted when we open the old
1170 // database.
1171 TEST_F(SyncableDirectoryTest, OldClientLeftUnsyncedDeletedLocalItem) {
1172 // We must create an entry with the offending properties. This is done with
1173 // some abuse of the MutableEntry's API; it doesn't expect us to modify an
1174 // item after it is deleted. If this hack becomes impractical we will need to
1175 // find a new way to simulate this scenario.
1177 TestIdFactory id_factory;
1179 // Happy-path: These valid entries should not get deleted.
1180 Id server_knows_id = id_factory.NewServerId();
1181 Id not_is_del_id = id_factory.NewLocalId();
1183 // The ID of the entry which will be unsynced, is_del and !ServerKnows().
1184 Id zombie_id = id_factory.NewLocalId();
1186 // We're about to do some bad things. Tell the directory verification
1187 // routines to look the other way.
1188 dir()->SetInvariantCheckLevel(OFF);
1191 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1193 // Create an uncommitted tombstone entry.
1194 MutableEntry server_knows(
1195 &trans, CREATE, BOOKMARKS, id_factory.root(), "server_knows");
1196 server_knows.PutId(server_knows_id);
1197 server_knows.PutIsUnsynced(true);
1198 server_knows.PutIsDel(true);
1199 server_knows.PutBaseVersion(5);
1200 server_knows.PutServerVersion(4);
1202 // Create a valid update entry.
1203 MutableEntry not_is_del(
1204 &trans, CREATE, BOOKMARKS, id_factory.root(), "not_is_del");
1205 not_is_del.PutId(not_is_del_id);
1206 not_is_del.PutIsDel(false);
1207 not_is_del.PutIsUnsynced(true);
1209 // Create a tombstone which should never be sent to the server because the
1210 // server never knew about the item's existence.
1212 // New clients should never put entries into this state. We work around
1213 // this by setting IS_DEL before setting IS_UNSYNCED, something which the
1214 // client should never do in practice.
1215 MutableEntry zombie(&trans, CREATE, BOOKMARKS, id_factory.root(), "zombie");
1216 zombie.PutId(zombie_id);
1217 zombie.PutIsDel(true);
1218 zombie.PutIsUnsynced(true);
1221 ASSERT_EQ(OPENED, SimulateSaveAndReloadDir());
1224 ReadTransaction trans(FROM_HERE, dir().get());
1226 // The directory loading routines should have cleaned things up, making it
1227 // safe to check invariants once again.
1228 dir()->FullyCheckTreeInvariants(&trans);
1230 Entry server_knows(&trans, GET_BY_ID, server_knows_id);
1231 EXPECT_TRUE(server_knows.good());
1233 Entry not_is_del(&trans, GET_BY_ID, not_is_del_id);
1234 EXPECT_TRUE(not_is_del.good());
1236 Entry zombie(&trans, GET_BY_ID, zombie_id);
1237 EXPECT_FALSE(zombie.good());
1241 TEST_F(SyncableDirectoryTest, PositionWithNullSurvivesSaveAndReload) {
1242 TestIdFactory id_factory;
1243 Id null_child_id;
1244 const char null_cstr[] = "\0null\0test";
1245 std::string null_str(null_cstr, arraysize(null_cstr) - 1);
1246 // Pad up to the minimum length with 0x7f characters, then add a string that
1247 // contains a few NULLs to the end. This is slightly wrong, since the suffix
1248 // part of a UniquePosition shouldn't contain NULLs, but it's good enough for
1249 // this test.
1250 std::string suffix =
1251 std::string(UniquePosition::kSuffixLength - null_str.length(), '\x7f') +
1252 null_str;
1253 UniquePosition null_pos = UniquePosition::FromInt64(10, suffix);
1256 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1258 MutableEntry parent(&trans, CREATE, BOOKMARKS, id_factory.root(), "parent");
1259 parent.PutIsDir(true);
1260 parent.PutIsUnsynced(true);
1262 MutableEntry child(&trans, CREATE, BOOKMARKS, parent.GetId(), "child");
1263 child.PutIsUnsynced(true);
1264 child.PutUniquePosition(null_pos);
1265 child.PutServerUniquePosition(null_pos);
1267 null_child_id = child.GetId();
1270 EXPECT_EQ(OPENED, SimulateSaveAndReloadDir());
1273 ReadTransaction trans(FROM_HERE, dir().get());
1275 Entry null_ordinal_child(&trans, GET_BY_ID, null_child_id);
1276 EXPECT_TRUE(null_pos.Equals(null_ordinal_child.GetUniquePosition()));
1277 EXPECT_TRUE(null_pos.Equals(null_ordinal_child.GetServerUniquePosition()));
1281 // Any item with BOOKMARKS in their local specifics should have a valid local
1282 // unique position. If there is an item in the loaded DB that does not match
1283 // this criteria, we consider the whole DB to be corrupt.
1284 TEST_F(SyncableDirectoryTest, BadPositionCountsAsCorruption) {
1285 TestIdFactory id_factory;
1288 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1290 MutableEntry parent(&trans, CREATE, BOOKMARKS, id_factory.root(), "parent");
1291 parent.PutIsDir(true);
1292 parent.PutIsUnsynced(true);
1294 // The code is littered with DCHECKs that try to stop us from doing what
1295 // we're about to do. Our work-around is to create a bookmark based on
1296 // a server update, then update its local specifics without updating its
1297 // local unique position.
1299 MutableEntry child(
1300 &trans, CREATE_NEW_UPDATE_ITEM, id_factory.MakeServer("child"));
1301 sync_pb::EntitySpecifics specifics;
1302 AddDefaultFieldValue(BOOKMARKS, &specifics);
1303 child.PutIsUnappliedUpdate(true);
1304 child.PutSpecifics(specifics);
1306 EXPECT_TRUE(child.ShouldMaintainPosition());
1307 EXPECT_TRUE(!child.GetUniquePosition().IsValid());
1310 EXPECT_EQ(FAILED_DATABASE_CORRUPT, SimulateSaveAndReloadDir());
1313 TEST_F(SyncableDirectoryTest, General) {
1314 int64 written_metahandle;
1315 const Id id = TestIdFactory::FromNumber(99);
1316 std::string name = "Jeff";
1317 // Test simple read operations on an empty DB.
1319 ReadTransaction rtrans(FROM_HERE, dir().get());
1320 Entry e(&rtrans, GET_BY_ID, id);
1321 ASSERT_FALSE(e.good()); // Hasn't been written yet.
1323 Directory::Metahandles child_handles;
1324 dir()->GetChildHandlesById(&rtrans, rtrans.root_id(), &child_handles);
1325 EXPECT_TRUE(child_handles.empty());
1328 // Test creating a new meta entry.
1330 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
1331 MutableEntry me(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), name);
1332 ASSERT_TRUE(me.good());
1333 me.PutId(id);
1334 me.PutBaseVersion(1);
1335 written_metahandle = me.GetMetahandle();
1338 // Test GetChildHandles* after something is now in the DB.
1339 // Also check that GET_BY_ID works.
1341 ReadTransaction rtrans(FROM_HERE, dir().get());
1342 Entry e(&rtrans, GET_BY_ID, id);
1343 ASSERT_TRUE(e.good());
1345 Directory::Metahandles child_handles;
1346 dir()->GetChildHandlesById(&rtrans, rtrans.root_id(), &child_handles);
1347 EXPECT_EQ(1u, child_handles.size());
1349 for (Directory::Metahandles::iterator i = child_handles.begin();
1350 i != child_handles.end(); ++i) {
1351 EXPECT_EQ(*i, written_metahandle);
1355 // Test writing data to an entity. Also check that GET_BY_HANDLE works.
1356 static const char s[] = "Hello World.";
1358 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1359 MutableEntry e(&trans, GET_BY_HANDLE, written_metahandle);
1360 ASSERT_TRUE(e.good());
1361 PutDataAsBookmarkFavicon(&trans, &e, s, sizeof(s));
1364 // Test reading back the contents that we just wrote.
1366 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1367 MutableEntry e(&trans, GET_BY_HANDLE, written_metahandle);
1368 ASSERT_TRUE(e.good());
1369 ExpectDataFromBookmarkFaviconEquals(&trans, &e, s, sizeof(s));
1372 // Verify it exists in the folder.
1374 ReadTransaction rtrans(FROM_HERE, dir().get());
1375 EXPECT_EQ(1, CountEntriesWithName(&rtrans, rtrans.root_id(), name));
1378 // Now delete it.
1380 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1381 MutableEntry e(&trans, GET_BY_HANDLE, written_metahandle);
1382 e.PutIsDel(true);
1384 EXPECT_EQ(0, CountEntriesWithName(&trans, trans.root_id(), name));
1387 dir()->SaveChanges();
1390 TEST_F(SyncableDirectoryTest, ChildrenOps) {
1391 int64 written_metahandle;
1392 const Id id = TestIdFactory::FromNumber(99);
1393 std::string name = "Jeff";
1395 ReadTransaction rtrans(FROM_HERE, dir().get());
1396 Entry e(&rtrans, GET_BY_ID, id);
1397 ASSERT_FALSE(e.good()); // Hasn't been written yet.
1399 Entry root(&rtrans, GET_BY_ID, rtrans.root_id());
1400 ASSERT_TRUE(root.good());
1401 EXPECT_FALSE(dir()->HasChildren(&rtrans, rtrans.root_id()));
1402 EXPECT_TRUE(root.GetFirstChildId().IsNull());
1406 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
1407 MutableEntry me(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), name);
1408 ASSERT_TRUE(me.good());
1409 me.PutId(id);
1410 me.PutBaseVersion(1);
1411 written_metahandle = me.GetMetahandle();
1414 // Test children ops after something is now in the DB.
1416 ReadTransaction rtrans(FROM_HERE, dir().get());
1417 Entry e(&rtrans, GET_BY_ID, id);
1418 ASSERT_TRUE(e.good());
1420 Entry child(&rtrans, GET_BY_HANDLE, written_metahandle);
1421 ASSERT_TRUE(child.good());
1423 Entry root(&rtrans, GET_BY_ID, rtrans.root_id());
1424 ASSERT_TRUE(root.good());
1425 EXPECT_TRUE(dir()->HasChildren(&rtrans, rtrans.root_id()));
1426 EXPECT_EQ(e.GetId(), root.GetFirstChildId());
1430 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
1431 MutableEntry me(&wtrans, GET_BY_HANDLE, written_metahandle);
1432 ASSERT_TRUE(me.good());
1433 me.PutIsDel(true);
1436 // Test children ops after the children have been deleted.
1438 ReadTransaction rtrans(FROM_HERE, dir().get());
1439 Entry e(&rtrans, GET_BY_ID, id);
1440 ASSERT_TRUE(e.good());
1442 Entry root(&rtrans, GET_BY_ID, rtrans.root_id());
1443 ASSERT_TRUE(root.good());
1444 EXPECT_FALSE(dir()->HasChildren(&rtrans, rtrans.root_id()));
1445 EXPECT_TRUE(root.GetFirstChildId().IsNull());
1448 dir()->SaveChanges();
1451 TEST_F(SyncableDirectoryTest, ClientIndexRebuildsProperly) {
1452 int64 written_metahandle;
1453 TestIdFactory factory;
1454 const Id id = factory.NewServerId();
1455 std::string name = "cheesepuffs";
1456 std::string tag = "dietcoke";
1458 // Test creating a new meta entry.
1460 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
1461 MutableEntry me(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), name);
1462 ASSERT_TRUE(me.good());
1463 me.PutId(id);
1464 me.PutBaseVersion(1);
1465 me.PutUniqueClientTag(tag);
1466 written_metahandle = me.GetMetahandle();
1468 dir()->SaveChanges();
1470 // Close and reopen, causing index regeneration.
1471 ReopenDirectory();
1473 ReadTransaction trans(FROM_HERE, dir().get());
1474 Entry me(&trans, GET_BY_CLIENT_TAG, tag);
1475 ASSERT_TRUE(me.good());
1476 EXPECT_EQ(me.GetId(), id);
1477 EXPECT_EQ(me.GetBaseVersion(), 1);
1478 EXPECT_EQ(me.GetUniqueClientTag(), tag);
1479 EXPECT_EQ(me.GetMetahandle(), written_metahandle);
1483 TEST_F(SyncableDirectoryTest, ClientIndexRebuildsDeletedProperly) {
1484 TestIdFactory factory;
1485 const Id id = factory.NewServerId();
1486 std::string tag = "dietcoke";
1488 // Test creating a deleted, unsynced, server meta entry.
1490 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
1491 MutableEntry me(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), "deleted");
1492 ASSERT_TRUE(me.good());
1493 me.PutId(id);
1494 me.PutBaseVersion(1);
1495 me.PutUniqueClientTag(tag);
1496 me.PutIsDel(true);
1497 me.PutIsUnsynced(true); // Or it might be purged.
1499 dir()->SaveChanges();
1501 // Close and reopen, causing index regeneration.
1502 ReopenDirectory();
1504 ReadTransaction trans(FROM_HERE, dir().get());
1505 Entry me(&trans, GET_BY_CLIENT_TAG, tag);
1506 // Should still be present and valid in the client tag index.
1507 ASSERT_TRUE(me.good());
1508 EXPECT_EQ(me.GetId(), id);
1509 EXPECT_EQ(me.GetUniqueClientTag(), tag);
1510 EXPECT_TRUE(me.GetIsDel());
1511 EXPECT_TRUE(me.GetIsUnsynced());
1515 TEST_F(SyncableDirectoryTest, ToValue) {
1516 const Id id = TestIdFactory::FromNumber(99);
1518 ReadTransaction rtrans(FROM_HERE, dir().get());
1519 Entry e(&rtrans, GET_BY_ID, id);
1520 EXPECT_FALSE(e.good()); // Hasn't been written yet.
1522 scoped_ptr<base::DictionaryValue> value(e.ToValue(NULL));
1523 ExpectDictBooleanValue(false, *value, "good");
1524 EXPECT_EQ(1u, value->size());
1527 // Test creating a new meta entry.
1529 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
1530 MutableEntry me(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), "new");
1531 ASSERT_TRUE(me.good());
1532 me.PutId(id);
1533 me.PutBaseVersion(1);
1535 scoped_ptr<base::DictionaryValue> value(me.ToValue(NULL));
1536 ExpectDictBooleanValue(true, *value, "good");
1537 EXPECT_TRUE(value->HasKey("kernel"));
1538 ExpectDictStringValue("Bookmarks", *value, "modelType");
1539 ExpectDictBooleanValue(true, *value, "existsOnClientBecauseNameIsNonEmpty");
1540 ExpectDictBooleanValue(false, *value, "isRoot");
1543 dir()->SaveChanges();
1546 // Test that the bookmark tag generation algorithm remains unchanged.
1547 TEST_F(SyncableDirectoryTest, BookmarkTagTest) {
1548 // This test needs its own InMemoryDirectoryBackingStore because it needs to
1549 // call request_consistent_cache_guid().
1550 InMemoryDirectoryBackingStore* store = new InMemoryDirectoryBackingStore("x");
1552 // The two inputs that form the bookmark tag are the directory's cache_guid
1553 // and its next_id value. We don't need to take any action to ensure
1554 // consistent next_id values, but we do need to explicitly request that our
1555 // InMemoryDirectoryBackingStore always return the same cache_guid.
1556 store->request_consistent_cache_guid();
1558 Directory dir(store, unrecoverable_error_handler(), NULL, NULL, NULL);
1559 ASSERT_EQ(
1560 OPENED,
1561 dir.Open("x", directory_change_delegate(), NullTransactionObserver()));
1564 WriteTransaction wtrans(FROM_HERE, UNITTEST, &dir);
1565 MutableEntry bm(&wtrans, CREATE, BOOKMARKS, wtrans.root_id(), "bm");
1566 bm.PutIsUnsynced(true);
1568 // If this assertion fails, that might indicate that the algorithm used to
1569 // generate bookmark tags has been modified. This could have implications
1570 // for bookmark ordering. Please make sure you know what you're doing if
1571 // you intend to make such a change.
1572 ASSERT_EQ("6wHRAb3kbnXV5GHrejp4/c1y5tw=", bm.GetUniqueBookmarkTag());
1576 // A thread that creates a bunch of directory entries.
1577 class StressTransactionsDelegate : public base::PlatformThread::Delegate {
1578 public:
1579 StressTransactionsDelegate(Directory* dir, int thread_number)
1580 : dir_(dir), thread_number_(thread_number) {}
1582 private:
1583 Directory* const dir_;
1584 const int thread_number_;
1586 // PlatformThread::Delegate methods:
1587 void ThreadMain() override {
1588 int entry_count = 0;
1589 std::string path_name;
1591 for (int i = 0; i < 20; ++i) {
1592 const int rand_action = rand() % 10;
1593 if (rand_action < 4 && !path_name.empty()) {
1594 ReadTransaction trans(FROM_HERE, dir_);
1595 CHECK(1 == CountEntriesWithName(&trans, trans.root_id(), path_name));
1596 base::PlatformThread::Sleep(
1597 base::TimeDelta::FromMilliseconds(rand() % 10));
1598 } else {
1599 std::string unique_name =
1600 base::StringPrintf("%d.%d", thread_number_, entry_count++);
1601 path_name.assign(unique_name.begin(), unique_name.end());
1602 WriteTransaction trans(FROM_HERE, UNITTEST, dir_);
1603 MutableEntry e(&trans, CREATE, BOOKMARKS, trans.root_id(), path_name);
1604 CHECK(e.good());
1605 base::PlatformThread::Sleep(
1606 base::TimeDelta::FromMilliseconds(rand() % 20));
1607 e.PutIsUnsynced(true);
1608 if (e.PutId(TestIdFactory::FromNumber(rand())) &&
1609 e.GetId().ServerKnows() && !e.GetId().IsRoot()) {
1610 e.PutBaseVersion(1);
1616 DISALLOW_COPY_AND_ASSIGN(StressTransactionsDelegate);
1619 // Stress test Directory by accessing it from several threads concurrently.
1620 TEST_F(SyncableDirectoryTest, StressTransactions) {
1621 const int kThreadCount = 7;
1622 base::PlatformThreadHandle threads[kThreadCount];
1623 scoped_ptr<StressTransactionsDelegate> thread_delegates[kThreadCount];
1625 for (int i = 0; i < kThreadCount; ++i) {
1626 thread_delegates[i].reset(new StressTransactionsDelegate(dir().get(), i));
1627 ASSERT_TRUE(base::PlatformThread::Create(
1628 0, thread_delegates[i].get(), &threads[i]));
1631 for (int i = 0; i < kThreadCount; ++i) {
1632 base::PlatformThread::Join(threads[i]);
1636 // Verify that Directory is notifed when a MutableEntry's AttachmentMetadata
1637 // changes.
1638 TEST_F(SyncableDirectoryTest, MutableEntry_PutAttachmentMetadata) {
1639 sync_pb::AttachmentMetadata attachment_metadata;
1640 sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record();
1641 sync_pb::AttachmentIdProto attachment_id_proto =
1642 syncer::CreateAttachmentIdProto();
1643 *record->mutable_id() = attachment_id_proto;
1644 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1646 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1648 // Create an entry with attachment metadata and see that the attachment id
1649 // is not linked.
1650 MutableEntry entry(
1651 &trans, CREATE, PREFERENCES, trans.root_id(), "some entry");
1652 entry.PutId(TestIdFactory::FromNumber(-1));
1653 entry.PutIsUnsynced(true);
1655 Directory::Metahandles metahandles;
1656 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1657 dir()->GetMetahandlesByAttachmentId(
1658 &trans, attachment_id_proto, &metahandles);
1659 ASSERT_TRUE(metahandles.empty());
1661 // Now add the attachment metadata and see that Directory believes it is
1662 // linked.
1663 entry.PutAttachmentMetadata(attachment_metadata);
1664 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto));
1665 dir()->GetMetahandlesByAttachmentId(
1666 &trans, attachment_id_proto, &metahandles);
1667 ASSERT_FALSE(metahandles.empty());
1668 ASSERT_EQ(metahandles[0], entry.GetMetahandle());
1670 // Clear out the attachment metadata and see that it's no longer linked.
1671 sync_pb::AttachmentMetadata empty_attachment_metadata;
1672 entry.PutAttachmentMetadata(empty_attachment_metadata);
1673 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1674 dir()->GetMetahandlesByAttachmentId(
1675 &trans, attachment_id_proto, &metahandles);
1676 ASSERT_TRUE(metahandles.empty());
1678 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1681 // Verify that UpdateAttachmentId updates attachment_id and is_on_server flag.
1682 TEST_F(SyncableDirectoryTest, MutableEntry_UpdateAttachmentId) {
1683 sync_pb::AttachmentMetadata attachment_metadata;
1684 sync_pb::AttachmentMetadataRecord* r1 = attachment_metadata.add_record();
1685 sync_pb::AttachmentMetadataRecord* r2 = attachment_metadata.add_record();
1686 *r1->mutable_id() = syncer::CreateAttachmentIdProto();
1687 *r2->mutable_id() = syncer::CreateAttachmentIdProto();
1688 sync_pb::AttachmentIdProto attachment_id_proto = r1->id();
1690 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1692 MutableEntry entry(
1693 &trans, CREATE, PREFERENCES, trans.root_id(), "some entry");
1694 entry.PutId(TestIdFactory::FromNumber(-1));
1695 entry.PutAttachmentMetadata(attachment_metadata);
1697 const sync_pb::AttachmentMetadata& entry_metadata =
1698 entry.GetAttachmentMetadata();
1699 ASSERT_EQ(2, entry_metadata.record_size());
1700 ASSERT_FALSE(entry_metadata.record(0).is_on_server());
1701 ASSERT_FALSE(entry_metadata.record(1).is_on_server());
1702 ASSERT_FALSE(entry.GetIsUnsynced());
1704 entry.MarkAttachmentAsOnServer(attachment_id_proto);
1706 ASSERT_TRUE(entry_metadata.record(0).is_on_server());
1707 ASSERT_FALSE(entry_metadata.record(1).is_on_server());
1708 ASSERT_TRUE(entry.GetIsUnsynced());
1711 // Verify that deleted entries with attachments will retain the attachments.
1712 TEST_F(SyncableDirectoryTest, Directory_DeleteDoesNotUnlinkAttachments) {
1713 sync_pb::AttachmentMetadata attachment_metadata;
1714 sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record();
1715 sync_pb::AttachmentIdProto attachment_id_proto =
1716 syncer::CreateAttachmentIdProto();
1717 *record->mutable_id() = attachment_id_proto;
1718 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1719 const Id id = TestIdFactory::FromNumber(-1);
1721 // Create an entry with attachment metadata and see that the attachment id
1722 // is linked.
1723 CreateEntryWithAttachmentMetadata(
1724 PREFERENCES, "some entry", id, attachment_metadata);
1725 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto));
1727 // Delete the entry and see that it's still linked because the entry hasn't
1728 // yet been purged.
1729 DeleteEntry(id);
1730 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto));
1732 // Reload the Directory, purging the deleted entry, and see that the
1733 // attachment is no longer linked.
1734 SimulateSaveAndReloadDir();
1735 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1738 // Verify that a given attachment can be referenced by multiple entries and that
1739 // any one of the references is sufficient to ensure it remains linked.
1740 TEST_F(SyncableDirectoryTest, Directory_LastReferenceUnlinksAttachments) {
1741 // Create one attachment.
1742 sync_pb::AttachmentMetadata attachment_metadata;
1743 sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record();
1744 sync_pb::AttachmentIdProto attachment_id_proto =
1745 syncer::CreateAttachmentIdProto();
1746 *record->mutable_id() = attachment_id_proto;
1748 // Create two entries, each referencing the attachment.
1749 const Id id1 = TestIdFactory::FromNumber(-1);
1750 const Id id2 = TestIdFactory::FromNumber(-2);
1751 CreateEntryWithAttachmentMetadata(
1752 PREFERENCES, "some entry", id1, attachment_metadata);
1753 CreateEntryWithAttachmentMetadata(
1754 PREFERENCES, "some other entry", id2, attachment_metadata);
1756 // See that the attachment is considered linked.
1757 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto));
1759 // Delete the first entry, reload the Directory, see that the attachment is
1760 // still linked.
1761 DeleteEntry(id1);
1762 SimulateSaveAndReloadDir();
1763 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id_proto));
1765 // Delete the second entry, reload the Directory, see that the attachment is
1766 // no loner linked.
1767 DeleteEntry(id2);
1768 SimulateSaveAndReloadDir();
1769 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id_proto));
1772 TEST_F(SyncableDirectoryTest, Directory_GetAttachmentIdsToUpload) {
1773 // Create one attachment, referenced by two entries.
1774 AttachmentId attachment_id = AttachmentId::Create();
1775 sync_pb::AttachmentIdProto attachment_id_proto = attachment_id.GetProto();
1776 sync_pb::AttachmentMetadata attachment_metadata;
1777 sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record();
1778 *record->mutable_id() = attachment_id_proto;
1779 const Id id1 = TestIdFactory::FromNumber(-1);
1780 const Id id2 = TestIdFactory::FromNumber(-2);
1781 CreateEntryWithAttachmentMetadata(
1782 PREFERENCES, "some entry", id1, attachment_metadata);
1783 CreateEntryWithAttachmentMetadata(
1784 PREFERENCES, "some other entry", id2, attachment_metadata);
1786 // See that Directory reports that this attachment is not on the server.
1787 AttachmentIdSet id_set;
1789 ReadTransaction trans(FROM_HERE, dir().get());
1790 dir()->GetAttachmentIdsToUpload(&trans, PREFERENCES, &id_set);
1792 ASSERT_EQ(1U, id_set.size());
1793 ASSERT_EQ(attachment_id, *id_set.begin());
1795 // Call again, but this time with a ModelType for which there are no entries.
1796 // See that Directory correctly reports that there are none.
1798 ReadTransaction trans(FROM_HERE, dir().get());
1799 dir()->GetAttachmentIdsToUpload(&trans, PASSWORDS, &id_set);
1801 ASSERT_TRUE(id_set.empty());
1803 // Now, mark the attachment as "on the server" via entry_1.
1805 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1806 MutableEntry entry_1(&trans, GET_BY_ID, id1);
1807 entry_1.MarkAttachmentAsOnServer(attachment_id_proto);
1810 // See that Directory no longer reports that this attachment is not on the
1811 // server.
1813 ReadTransaction trans(FROM_HERE, dir().get());
1814 dir()->GetAttachmentIdsToUpload(&trans, PREFERENCES, &id_set);
1816 ASSERT_TRUE(id_set.empty());
1819 // Verify that the directory accepts entries with unset parent ID.
1820 TEST_F(SyncableDirectoryTest, MutableEntry_ImplicitParentId) {
1821 TestIdFactory id_factory;
1822 const Id root_id = TestIdFactory::root();
1823 const Id p_root_id = id_factory.NewServerId();
1824 const Id a_root_id = id_factory.NewServerId();
1825 const Id item1_id = id_factory.NewServerId();
1826 const Id item2_id = id_factory.NewServerId();
1827 const Id item3_id = id_factory.NewServerId();
1828 // Create two type root folders that are necessary (for now)
1829 // for creating items without explicitly set Parent ID
1831 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1832 MutableEntry p_root(&trans, CREATE, PREFERENCES, root_id, "P");
1833 ASSERT_TRUE(p_root.good());
1834 p_root.PutIsDir(true);
1835 p_root.PutId(p_root_id);
1836 p_root.PutBaseVersion(1);
1838 MutableEntry a_root(&trans, CREATE, AUTOFILL, root_id, "A");
1839 ASSERT_TRUE(a_root.good());
1840 a_root.PutIsDir(true);
1841 a_root.PutId(a_root_id);
1842 a_root.PutBaseVersion(1);
1845 // Create two entries with implicit parent nodes and one entry with explicit
1846 // parent node.
1848 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
1849 MutableEntry item1(&trans, CREATE, PREFERENCES, "P1");
1850 item1.PutBaseVersion(1);
1851 item1.PutId(item1_id);
1852 MutableEntry item2(&trans, CREATE, AUTOFILL, "A1");
1853 item2.PutBaseVersion(1);
1854 item2.PutId(item2_id);
1855 // Placing an AUTOFILL item under the root isn't expected,
1856 // but let's test it to verify that explicit root overrides the implicit
1857 // one and this entry doesn't end up under the "A" root.
1858 MutableEntry item3(&trans, CREATE, AUTOFILL, root_id, "A2");
1859 item3.PutBaseVersion(1);
1860 item3.PutId(item3_id);
1864 ReadTransaction trans(FROM_HERE, dir().get());
1865 // Verify that item1 and item2 are good and have no ParentId.
1866 Entry item1(&trans, GET_BY_ID, item1_id);
1867 ASSERT_TRUE(item1.good());
1868 ASSERT_TRUE(item1.GetParentId().IsNull());
1869 Entry item2(&trans, GET_BY_ID, item2_id);
1870 ASSERT_TRUE(item2.good());
1871 ASSERT_TRUE(item2.GetParentId().IsNull());
1872 // Verify that p_root and a_root have exactly one child each
1873 // (subtract one to exclude roots themselves).
1874 Entry p_root(&trans, GET_BY_ID, p_root_id);
1875 ASSERT_EQ(item1_id, p_root.GetFirstChildId());
1876 ASSERT_EQ(1, p_root.GetTotalNodeCount() - 1);
1877 Entry a_root(&trans, GET_BY_ID, a_root_id);
1878 ASSERT_EQ(item2_id, a_root.GetFirstChildId());
1879 ASSERT_EQ(1, a_root.GetTotalNodeCount() - 1);
1883 } // namespace syncable
1885 } // namespace syncer