Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / sync / syncable / syncable_unittest.cc
blob52baee46a1bc2776c295ecbb19685ebbf0e0f507
1 // Copyright 2012 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 <string>
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/location.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/stl_util.h"
17 #include "base/synchronization/condition_variable.h"
18 #include "base/test/values_test_util.h"
19 #include "base/threading/platform_thread.h"
20 #include "base/values.h"
21 #include "sync/protocol/bookmark_specifics.pb.h"
22 #include "sync/syncable/directory_backing_store.h"
23 #include "sync/syncable/directory_change_delegate.h"
24 #include "sync/syncable/directory_unittest.h"
25 #include "sync/syncable/in_memory_directory_backing_store.h"
26 #include "sync/syncable/metahandle_set.h"
27 #include "sync/syncable/mutable_entry.h"
28 #include "sync/syncable/on_disk_directory_backing_store.h"
29 #include "sync/syncable/syncable_proto_util.h"
30 #include "sync/syncable/syncable_read_transaction.h"
31 #include "sync/syncable/syncable_util.h"
32 #include "sync/syncable/syncable_write_transaction.h"
33 #include "sync/test/engine/test_id_factory.h"
34 #include "sync/test/engine/test_syncable_utils.h"
35 #include "sync/test/fake_encryptor.h"
36 #include "sync/test/null_directory_change_delegate.h"
37 #include "sync/test/null_transaction_observer.h"
38 #include "sync/util/test_unrecoverable_error_handler.h"
39 #include "testing/gtest/include/gtest/gtest.h"
41 namespace syncer {
42 namespace syncable {
44 using base::ExpectDictBooleanValue;
45 using base::ExpectDictStringValue;
47 // An OnDiskDirectoryBackingStore that can be set to always fail SaveChanges.
48 class TestBackingStore : public OnDiskDirectoryBackingStore {
49 public:
50 TestBackingStore(const std::string& dir_name,
51 const base::FilePath& backing_filepath);
53 ~TestBackingStore() override;
55 bool SaveChanges(const Directory::SaveChangesSnapshot& snapshot) override;
57 void StartFailingSaveChanges() {
58 fail_save_changes_ = true;
61 private:
62 bool fail_save_changes_;
65 TestBackingStore::TestBackingStore(const std::string& dir_name,
66 const base::FilePath& backing_filepath)
67 : OnDiskDirectoryBackingStore(dir_name, backing_filepath),
68 fail_save_changes_(false) {
71 TestBackingStore::~TestBackingStore() { }
73 bool TestBackingStore::SaveChanges(
74 const Directory::SaveChangesSnapshot& snapshot){
75 if (fail_save_changes_) {
76 return false;
77 } else {
78 return OnDiskDirectoryBackingStore::SaveChanges(snapshot);
82 // A directory whose Save() function can be set to always fail.
83 class TestDirectory : public Directory {
84 public:
85 // A factory function used to work around some initialization order issues.
86 static TestDirectory* Create(
87 Encryptor *encryptor,
88 const WeakHandle<UnrecoverableErrorHandler>& handler,
89 const std::string& dir_name,
90 const base::FilePath& backing_filepath);
92 ~TestDirectory() override;
94 void StartFailingSaveChanges() {
95 backing_store_->StartFailingSaveChanges();
98 private:
99 TestDirectory(Encryptor* encryptor,
100 const WeakHandle<UnrecoverableErrorHandler>& handler,
101 TestBackingStore* backing_store);
103 TestBackingStore* backing_store_;
106 TestDirectory* TestDirectory::Create(
107 Encryptor *encryptor,
108 const WeakHandle<UnrecoverableErrorHandler>& handler,
109 const std::string& dir_name,
110 const base::FilePath& backing_filepath) {
111 TestBackingStore* backing_store =
112 new TestBackingStore(dir_name, backing_filepath);
113 return new TestDirectory(encryptor, handler, backing_store);
116 TestDirectory::TestDirectory(
117 Encryptor* encryptor,
118 const WeakHandle<UnrecoverableErrorHandler>& handler,
119 TestBackingStore* backing_store)
120 : Directory(backing_store, handler, base::Closure(), NULL, NULL),
121 backing_store_(backing_store) {}
123 TestDirectory::~TestDirectory() { }
125 // crbug.com/144422
126 #if defined(OS_ANDROID)
127 #define MAYBE_FailInitialWrite DISABLED_FailInitialWrite
128 #else
129 #define MAYBE_FailInitialWrite FailInitialWrite
130 #endif
131 TEST(OnDiskSyncableDirectory, MAYBE_FailInitialWrite) {
132 base::MessageLoop message_loop;
133 FakeEncryptor encryptor;
134 TestUnrecoverableErrorHandler handler;
135 base::ScopedTempDir temp_dir;
136 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
137 base::FilePath file_path = temp_dir.path().Append(
138 FILE_PATH_LITERAL("Test.sqlite3"));
139 std::string name = "user@x.com";
140 NullDirectoryChangeDelegate delegate;
142 scoped_ptr<TestDirectory> test_dir(TestDirectory::Create(
143 &encryptor, MakeWeakHandle(handler.GetWeakPtr()), name, file_path));
145 test_dir->StartFailingSaveChanges();
146 ASSERT_EQ(FAILED_INITIAL_WRITE, test_dir->Open(name, &delegate,
147 NullTransactionObserver()));
150 // A variant of SyncableDirectoryTest that uses a real sqlite database.
151 class OnDiskSyncableDirectoryTest : public SyncableDirectoryTest {
152 protected:
153 // SetUp() is called before each test case is run.
154 // The sqlite3 DB is deleted before each test is run.
155 void SetUp() override {
156 SyncableDirectoryTest::SetUp();
157 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
158 file_path_ = temp_dir_.path().Append(
159 FILE_PATH_LITERAL("Test.sqlite3"));
160 base::DeleteFile(file_path_, true);
161 CreateDirectory();
164 void TearDown() override {
165 // This also closes file handles.
166 dir()->SaveChanges();
167 dir().reset();
168 base::DeleteFile(file_path_, true);
169 SyncableDirectoryTest::TearDown();
172 // Creates a new directory. Deletes the old directory, if it exists.
173 void CreateDirectory() {
174 test_directory_ = TestDirectory::Create(
175 encryptor(),
176 MakeWeakHandle(unrecoverable_error_handler()->GetWeakPtr()),
177 kDirectoryName, file_path_);
178 dir().reset(test_directory_);
179 ASSERT_TRUE(dir().get());
180 ASSERT_EQ(OPENED,
181 dir()->Open(kDirectoryName,
182 directory_change_delegate(),
183 NullTransactionObserver()));
184 ASSERT_TRUE(dir()->good());
187 void SaveAndReloadDir() {
188 dir()->SaveChanges();
189 CreateDirectory();
192 void StartFailingSaveChanges() {
193 test_directory_->StartFailingSaveChanges();
196 TestDirectory *test_directory_; // mirrors scoped_ptr<Directory> dir_
197 base::ScopedTempDir temp_dir_;
198 base::FilePath file_path_;
201 sync_pb::DataTypeContext BuildContext(ModelType type) {
202 sync_pb::DataTypeContext context;
203 context.set_context("context");
204 context.set_data_type_id(GetSpecificsFieldNumberFromModelType(type));
205 return context;
208 TEST_F(OnDiskSyncableDirectoryTest, TestPurgeEntriesWithTypeIn) {
209 sync_pb::EntitySpecifics bookmark_specs;
210 sync_pb::EntitySpecifics autofill_specs;
211 sync_pb::EntitySpecifics preference_specs;
212 AddDefaultFieldValue(BOOKMARKS, &bookmark_specs);
213 AddDefaultFieldValue(PREFERENCES, &preference_specs);
214 AddDefaultFieldValue(AUTOFILL, &autofill_specs);
216 ModelTypeSet types_to_purge(PREFERENCES, AUTOFILL);
218 dir()->SetDownloadProgress(BOOKMARKS, BuildProgress(BOOKMARKS));
219 dir()->SetDownloadProgress(PREFERENCES, BuildProgress(PREFERENCES));
220 dir()->SetDownloadProgress(AUTOFILL, BuildProgress(AUTOFILL));
222 TestIdFactory id_factory;
223 // Create some items for each type.
225 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
227 dir()->SetDataTypeContext(&trans, BOOKMARKS, BuildContext(BOOKMARKS));
228 dir()->SetDataTypeContext(&trans, PREFERENCES, BuildContext(PREFERENCES));
229 dir()->SetDataTypeContext(&trans, AUTOFILL, BuildContext(AUTOFILL));
231 // Make it look like these types have completed initial sync.
232 CreateTypeRoot(&trans, dir().get(), BOOKMARKS);
233 CreateTypeRoot(&trans, dir().get(), PREFERENCES);
234 CreateTypeRoot(&trans, dir().get(), AUTOFILL);
236 // Add more nodes for this type. Technically, they should be placed under
237 // the proper type root nodes but the assertions in this test won't notice
238 // if their parent isn't quite right.
239 MutableEntry item1(&trans, CREATE, BOOKMARKS, trans.root_id(), "Item");
240 ASSERT_TRUE(item1.good());
241 item1.PutServerSpecifics(bookmark_specs);
242 item1.PutIsUnsynced(true);
244 MutableEntry item2(&trans, CREATE_NEW_UPDATE_ITEM,
245 id_factory.NewServerId());
246 ASSERT_TRUE(item2.good());
247 item2.PutServerSpecifics(bookmark_specs);
248 item2.PutIsUnappliedUpdate(true);
250 MutableEntry item3(&trans, CREATE, PREFERENCES,
251 trans.root_id(), "Item");
252 ASSERT_TRUE(item3.good());
253 item3.PutSpecifics(preference_specs);
254 item3.PutServerSpecifics(preference_specs);
255 item3.PutIsUnsynced(true);
257 MutableEntry item4(&trans, CREATE_NEW_UPDATE_ITEM,
258 id_factory.NewServerId());
259 ASSERT_TRUE(item4.good());
260 item4.PutServerSpecifics(preference_specs);
261 item4.PutIsUnappliedUpdate(true);
263 MutableEntry item5(&trans, CREATE, AUTOFILL,
264 trans.root_id(), "Item");
265 ASSERT_TRUE(item5.good());
266 item5.PutSpecifics(autofill_specs);
267 item5.PutServerSpecifics(autofill_specs);
268 item5.PutIsUnsynced(true);
270 MutableEntry item6(&trans, CREATE_NEW_UPDATE_ITEM,
271 id_factory.NewServerId());
272 ASSERT_TRUE(item6.good());
273 item6.PutServerSpecifics(autofill_specs);
274 item6.PutIsUnappliedUpdate(true);
277 dir()->SaveChanges();
279 ReadTransaction trans(FROM_HERE, dir().get());
280 MetahandleSet all_set;
281 GetAllMetaHandles(&trans, &all_set);
282 ASSERT_EQ(10U, all_set.size());
285 dir()->PurgeEntriesWithTypeIn(types_to_purge, ModelTypeSet(), ModelTypeSet());
287 // We first query the in-memory data, and then reload the directory (without
288 // saving) to verify that disk does not still have the data.
289 CheckPurgeEntriesWithTypeInSucceeded(types_to_purge, true);
290 SaveAndReloadDir();
291 CheckPurgeEntriesWithTypeInSucceeded(types_to_purge, false);
294 TEST_F(OnDiskSyncableDirectoryTest, TestShareInfo) {
295 dir()->set_store_birthday("Jan 31st");
296 const char* const bag_of_chips_array = "\0bag of chips";
297 const std::string bag_of_chips_string =
298 std::string(bag_of_chips_array, sizeof(bag_of_chips_array));
299 dir()->set_bag_of_chips(bag_of_chips_string);
301 ReadTransaction trans(FROM_HERE, dir().get());
302 EXPECT_EQ("Jan 31st", dir()->store_birthday());
303 EXPECT_EQ(bag_of_chips_string, dir()->bag_of_chips());
305 dir()->set_store_birthday("April 10th");
306 const char* const bag_of_chips2_array = "\0bag of chips2";
307 const std::string bag_of_chips2_string =
308 std::string(bag_of_chips2_array, sizeof(bag_of_chips2_array));
309 dir()->set_bag_of_chips(bag_of_chips2_string);
310 dir()->SaveChanges();
312 ReadTransaction trans(FROM_HERE, dir().get());
313 EXPECT_EQ("April 10th", dir()->store_birthday());
314 EXPECT_EQ(bag_of_chips2_string, dir()->bag_of_chips());
316 const char* const bag_of_chips3_array = "\0bag of chips3";
317 const std::string bag_of_chips3_string =
318 std::string(bag_of_chips3_array, sizeof(bag_of_chips3_array));
319 dir()->set_bag_of_chips(bag_of_chips3_string);
320 // Restore the directory from disk. Make sure that nothing's changed.
321 SaveAndReloadDir();
323 ReadTransaction trans(FROM_HERE, dir().get());
324 EXPECT_EQ("April 10th", dir()->store_birthday());
325 EXPECT_EQ(bag_of_chips3_string, dir()->bag_of_chips());
329 TEST_F(OnDiskSyncableDirectoryTest,
330 TestSimpleFieldsPreservedDuringSaveChanges) {
331 Id update_id = TestIdFactory::FromNumber(1);
332 Id create_id;
333 EntryKernel create_pre_save, update_pre_save;
334 EntryKernel create_post_save, update_post_save;
335 std::string create_name = "Create";
338 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
339 MutableEntry create(
340 &trans, CREATE, BOOKMARKS, trans.root_id(), create_name);
341 MutableEntry update(&trans, CREATE_NEW_UPDATE_ITEM, update_id);
342 create.PutIsUnsynced(true);
343 update.PutIsUnappliedUpdate(true);
344 sync_pb::EntitySpecifics specifics;
345 specifics.mutable_bookmark()->set_favicon("PNG");
346 specifics.mutable_bookmark()->set_url("http://nowhere");
347 create.PutSpecifics(specifics);
348 update.PutServerSpecifics(specifics);
349 create_pre_save = create.GetKernelCopy();
350 update_pre_save = update.GetKernelCopy();
351 create_id = create.GetId();
354 dir()->SaveChanges();
355 dir().reset(
356 new Directory(new OnDiskDirectoryBackingStore(kDirectoryName, file_path_),
357 MakeWeakHandle(unrecoverable_error_handler()->GetWeakPtr()),
358 base::Closure(), NULL, NULL));
360 ASSERT_TRUE(dir().get());
361 ASSERT_EQ(OPENED,
362 dir()->Open(kDirectoryName,
363 directory_change_delegate(),
364 NullTransactionObserver()));
365 ASSERT_TRUE(dir()->good());
368 ReadTransaction trans(FROM_HERE, dir().get());
369 Entry create(&trans, GET_BY_ID, create_id);
370 EXPECT_EQ(1, CountEntriesWithName(&trans, trans.root_id(), create_name));
371 Entry update(&trans, GET_BY_ID, update_id);
372 create_post_save = create.GetKernelCopy();
373 update_post_save = update.GetKernelCopy();
375 int i = BEGIN_FIELDS;
376 for ( ; i < INT64_FIELDS_END ; ++i) {
377 EXPECT_EQ(create_pre_save.ref((Int64Field)i) +
378 (i == TRANSACTION_VERSION ? 1 : 0),
379 create_post_save.ref((Int64Field)i))
380 << "int64 field #" << i << " changed during save/load";
381 EXPECT_EQ(update_pre_save.ref((Int64Field)i),
382 update_post_save.ref((Int64Field)i))
383 << "int64 field #" << i << " changed during save/load";
385 for ( ; i < TIME_FIELDS_END ; ++i) {
386 EXPECT_EQ(create_pre_save.ref((TimeField)i),
387 create_post_save.ref((TimeField)i))
388 << "time field #" << i << " changed during save/load";
389 EXPECT_EQ(update_pre_save.ref((TimeField)i),
390 update_post_save.ref((TimeField)i))
391 << "time field #" << i << " changed during save/load";
393 for ( ; i < ID_FIELDS_END ; ++i) {
394 EXPECT_EQ(create_pre_save.ref((IdField)i),
395 create_post_save.ref((IdField)i))
396 << "id field #" << i << " changed during save/load";
397 EXPECT_EQ(update_pre_save.ref((IdField)i),
398 update_pre_save.ref((IdField)i))
399 << "id field #" << i << " changed during save/load";
401 for ( ; i < BIT_FIELDS_END ; ++i) {
402 EXPECT_EQ(create_pre_save.ref((BitField)i),
403 create_post_save.ref((BitField)i))
404 << "Bit field #" << i << " changed during save/load";
405 EXPECT_EQ(update_pre_save.ref((BitField)i),
406 update_post_save.ref((BitField)i))
407 << "Bit field #" << i << " changed during save/load";
409 for ( ; i < STRING_FIELDS_END ; ++i) {
410 EXPECT_EQ(create_pre_save.ref((StringField)i),
411 create_post_save.ref((StringField)i))
412 << "String field #" << i << " changed during save/load";
413 EXPECT_EQ(update_pre_save.ref((StringField)i),
414 update_post_save.ref((StringField)i))
415 << "String field #" << i << " changed during save/load";
417 for ( ; i < PROTO_FIELDS_END; ++i) {
418 EXPECT_EQ(create_pre_save.ref((ProtoField)i).SerializeAsString(),
419 create_post_save.ref((ProtoField)i).SerializeAsString())
420 << "Blob field #" << i << " changed during save/load";
421 EXPECT_EQ(update_pre_save.ref((ProtoField)i).SerializeAsString(),
422 update_post_save.ref((ProtoField)i).SerializeAsString())
423 << "Blob field #" << i << " changed during save/load";
425 for ( ; i < UNIQUE_POSITION_FIELDS_END; ++i) {
426 EXPECT_TRUE(create_pre_save.ref((UniquePositionField)i).Equals(
427 create_post_save.ref((UniquePositionField)i)))
428 << "Position field #" << i << " changed during save/load";
429 EXPECT_TRUE(update_pre_save.ref((UniquePositionField)i).Equals(
430 update_post_save.ref((UniquePositionField)i)))
431 << "Position field #" << i << " changed during save/load";
435 TEST_F(OnDiskSyncableDirectoryTest, TestSaveChangesFailure) {
436 int64 handle1 = 0;
437 // Set up an item using a regular, saveable directory.
439 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
441 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), "aguilera");
442 ASSERT_TRUE(e1.good());
443 EXPECT_TRUE(e1.GetKernelCopy().is_dirty());
444 handle1 = e1.GetMetahandle();
445 e1.PutBaseVersion(1);
446 e1.PutIsDir(true);
447 e1.PutId(TestIdFactory::FromNumber(101));
448 EXPECT_TRUE(e1.GetKernelCopy().is_dirty());
449 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
451 ASSERT_TRUE(dir()->SaveChanges());
453 // Make sure the item is no longer dirty after saving,
454 // and make a modification.
456 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
458 MutableEntry aguilera(&trans, GET_BY_HANDLE, handle1);
459 ASSERT_TRUE(aguilera.good());
460 EXPECT_FALSE(aguilera.GetKernelCopy().is_dirty());
461 EXPECT_EQ(aguilera.GetNonUniqueName(), "aguilera");
462 aguilera.PutNonUniqueName("overwritten");
463 EXPECT_TRUE(aguilera.GetKernelCopy().is_dirty());
464 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
466 ASSERT_TRUE(dir()->SaveChanges());
468 // Now do some operations when SaveChanges() will fail.
469 StartFailingSaveChanges();
470 ASSERT_TRUE(dir()->good());
472 int64 handle2 = 0;
474 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
476 MutableEntry aguilera(&trans, GET_BY_HANDLE, handle1);
477 ASSERT_TRUE(aguilera.good());
478 EXPECT_FALSE(aguilera.GetKernelCopy().is_dirty());
479 EXPECT_EQ(aguilera.GetNonUniqueName(), "overwritten");
480 EXPECT_FALSE(aguilera.GetKernelCopy().is_dirty());
481 EXPECT_FALSE(IsInDirtyMetahandles(handle1));
482 aguilera.PutNonUniqueName("christina");
483 EXPECT_TRUE(aguilera.GetKernelCopy().is_dirty());
484 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
486 // New item.
487 MutableEntry kids_on_block(
488 &trans, CREATE, BOOKMARKS, trans.root_id(), "kids");
489 ASSERT_TRUE(kids_on_block.good());
490 handle2 = kids_on_block.GetMetahandle();
491 kids_on_block.PutBaseVersion(1);
492 kids_on_block.PutIsDir(true);
493 kids_on_block.PutId(TestIdFactory::FromNumber(102));
494 EXPECT_TRUE(kids_on_block.GetKernelCopy().is_dirty());
495 EXPECT_TRUE(IsInDirtyMetahandles(handle2));
498 // We are using an unsaveable directory, so this can't succeed. However,
499 // the HandleSaveChangesFailure code path should have been triggered.
500 ASSERT_FALSE(dir()->SaveChanges());
502 // Make sure things were rolled back and the world is as it was before call.
504 ReadTransaction trans(FROM_HERE, dir().get());
505 Entry e1(&trans, GET_BY_HANDLE, handle1);
506 ASSERT_TRUE(e1.good());
507 EntryKernel aguilera = e1.GetKernelCopy();
508 Entry kids(&trans, GET_BY_HANDLE, handle2);
509 ASSERT_TRUE(kids.good());
510 EXPECT_TRUE(kids.GetKernelCopy().is_dirty());
511 EXPECT_TRUE(IsInDirtyMetahandles(handle2));
512 EXPECT_TRUE(aguilera.is_dirty());
513 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
517 TEST_F(OnDiskSyncableDirectoryTest, TestSaveChangesFailureWithPurge) {
518 int64 handle1 = 0;
519 // Set up an item and progress marker using a regular, saveable directory.
520 dir()->SetDownloadProgress(BOOKMARKS, BuildProgress(BOOKMARKS));
522 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
524 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), "aguilera");
525 ASSERT_TRUE(e1.good());
526 EXPECT_TRUE(e1.GetKernelCopy().is_dirty());
527 handle1 = e1.GetMetahandle();
528 e1.PutBaseVersion(1);
529 e1.PutIsDir(true);
530 e1.PutId(TestIdFactory::FromNumber(101));
531 sync_pb::EntitySpecifics bookmark_specs;
532 AddDefaultFieldValue(BOOKMARKS, &bookmark_specs);
533 e1.PutSpecifics(bookmark_specs);
534 e1.PutServerSpecifics(bookmark_specs);
535 e1.PutId(TestIdFactory::FromNumber(101));
536 EXPECT_TRUE(e1.GetKernelCopy().is_dirty());
537 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
539 ASSERT_TRUE(dir()->SaveChanges());
541 // Now do some operations while SaveChanges() is set to fail.
542 StartFailingSaveChanges();
543 ASSERT_TRUE(dir()->good());
545 ModelTypeSet set(BOOKMARKS);
546 dir()->PurgeEntriesWithTypeIn(set, ModelTypeSet(), ModelTypeSet());
547 EXPECT_TRUE(IsInMetahandlesToPurge(handle1));
548 ASSERT_FALSE(dir()->SaveChanges());
549 EXPECT_TRUE(IsInMetahandlesToPurge(handle1));
552 class SyncableDirectoryManagement : public testing::Test {
553 public:
554 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
556 void TearDown() override {}
558 protected:
559 base::MessageLoop message_loop_;
560 base::ScopedTempDir temp_dir_;
561 FakeEncryptor encryptor_;
562 TestUnrecoverableErrorHandler handler_;
563 NullDirectoryChangeDelegate delegate_;
566 TEST_F(SyncableDirectoryManagement, TestFileRelease) {
567 base::FilePath path =
568 temp_dir_.path().Append(Directory::kSyncDatabaseFilename);
571 Directory dir(new OnDiskDirectoryBackingStore("ScopeTest", path),
572 MakeWeakHandle(handler_.GetWeakPtr()), base::Closure(), NULL,
573 NULL);
574 DirOpenResult result =
575 dir.Open("ScopeTest", &delegate_, NullTransactionObserver());
576 ASSERT_EQ(result, OPENED);
579 // Destroying the directory should have released the backing database file.
580 ASSERT_TRUE(base::DeleteFile(path, true));
583 class SyncableClientTagTest : public SyncableDirectoryTest {
584 public:
585 static const int kBaseVersion = 1;
586 const char* test_name_;
587 const char* test_tag_;
589 SyncableClientTagTest() : test_name_("test_name"), test_tag_("dietcoke") {}
591 bool CreateWithDefaultTag(Id id, bool deleted) {
592 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
593 MutableEntry me(&wtrans, CREATE, PREFERENCES,
594 wtrans.root_id(), test_name_);
595 CHECK(me.good());
596 me.PutId(id);
597 if (id.ServerKnows()) {
598 me.PutBaseVersion(kBaseVersion);
600 me.PutIsUnsynced(true);
601 me.PutIsDel(deleted);
602 me.PutIsDir(false);
603 return me.PutUniqueClientTag(test_tag_);
606 // Verify an entry exists with the default tag.
607 void VerifyTag(Id id, bool deleted) {
608 // Should still be present and valid in the client tag index.
609 ReadTransaction trans(FROM_HERE, dir().get());
610 Entry me(&trans, GET_BY_CLIENT_TAG, test_tag_);
611 CHECK(me.good());
612 EXPECT_EQ(me.GetId(), id);
613 EXPECT_EQ(me.GetUniqueClientTag(), test_tag_);
614 EXPECT_EQ(me.GetIsDel(), deleted);
616 // We only sync deleted items that the server knew about.
617 if (me.GetId().ServerKnows() || !me.GetIsDel()) {
618 EXPECT_EQ(me.GetIsUnsynced(), true);
622 protected:
623 TestIdFactory factory_;
626 TEST_F(SyncableClientTagTest, TestClientTagClear) {
627 Id server_id = factory_.NewServerId();
628 EXPECT_TRUE(CreateWithDefaultTag(server_id, false));
630 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
631 MutableEntry me(&trans, GET_BY_CLIENT_TAG, test_tag_);
632 EXPECT_TRUE(me.good());
633 me.PutUniqueClientTag(std::string());
636 ReadTransaction trans(FROM_HERE, dir().get());
637 Entry by_tag(&trans, GET_BY_CLIENT_TAG, test_tag_);
638 EXPECT_FALSE(by_tag.good());
640 Entry by_id(&trans, GET_BY_ID, server_id);
641 EXPECT_TRUE(by_id.good());
642 EXPECT_TRUE(by_id.GetUniqueClientTag().empty());
646 TEST_F(SyncableClientTagTest, TestClientTagIndexServerId) {
647 Id server_id = factory_.NewServerId();
648 EXPECT_TRUE(CreateWithDefaultTag(server_id, false));
649 VerifyTag(server_id, false);
652 TEST_F(SyncableClientTagTest, TestClientTagIndexClientId) {
653 Id client_id = factory_.NewLocalId();
654 EXPECT_TRUE(CreateWithDefaultTag(client_id, false));
655 VerifyTag(client_id, false);
658 TEST_F(SyncableClientTagTest, TestDeletedClientTagIndexClientId) {
659 Id client_id = factory_.NewLocalId();
660 EXPECT_TRUE(CreateWithDefaultTag(client_id, true));
661 VerifyTag(client_id, true);
664 TEST_F(SyncableClientTagTest, TestDeletedClientTagIndexServerId) {
665 Id server_id = factory_.NewServerId();
666 EXPECT_TRUE(CreateWithDefaultTag(server_id, true));
667 VerifyTag(server_id, true);
670 TEST_F(SyncableClientTagTest, TestClientTagIndexDuplicateServer) {
671 EXPECT_TRUE(CreateWithDefaultTag(factory_.NewServerId(), true));
672 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), true));
673 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), false));
674 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), false));
675 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), true));
678 } // namespace syncable
679 } // namespace syncer