SupervisedUser SafeSites: Switch to the new SafeSearch API
[chromium-blink-merge.git] / sync / syncable / syncable_unittest.cc
blob850be5fe02b20d8d6aa10a446b520ec44a3d5450
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 TEST(OnDiskSyncableDirectory, FailInitialWrite) {
126 base::MessageLoop message_loop;
127 FakeEncryptor encryptor;
128 TestUnrecoverableErrorHandler handler;
129 base::ScopedTempDir temp_dir;
130 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
131 base::FilePath file_path = temp_dir.path().Append(
132 FILE_PATH_LITERAL("Test.sqlite3"));
133 std::string name = "user@x.com";
134 NullDirectoryChangeDelegate delegate;
136 scoped_ptr<TestDirectory> test_dir(TestDirectory::Create(
137 &encryptor, MakeWeakHandle(handler.GetWeakPtr()), name, file_path));
139 test_dir->StartFailingSaveChanges();
140 ASSERT_EQ(FAILED_INITIAL_WRITE, test_dir->Open(name, &delegate,
141 NullTransactionObserver()));
144 // A variant of SyncableDirectoryTest that uses a real sqlite database.
145 class OnDiskSyncableDirectoryTest : public SyncableDirectoryTest {
146 protected:
147 // SetUp() is called before each test case is run.
148 // The sqlite3 DB is deleted before each test is run.
149 void SetUp() override {
150 SyncableDirectoryTest::SetUp();
151 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
152 file_path_ = temp_dir_.path().Append(
153 FILE_PATH_LITERAL("Test.sqlite3"));
154 base::DeleteFile(file_path_, true);
155 CreateDirectory();
158 void TearDown() override {
159 // This also closes file handles.
160 dir()->SaveChanges();
161 dir().reset();
162 base::DeleteFile(file_path_, true);
163 SyncableDirectoryTest::TearDown();
166 // Creates a new directory. Deletes the old directory, if it exists.
167 void CreateDirectory() {
168 test_directory_ = TestDirectory::Create(
169 encryptor(),
170 MakeWeakHandle(unrecoverable_error_handler()->GetWeakPtr()),
171 kDirectoryName, file_path_);
172 dir().reset(test_directory_);
173 ASSERT_TRUE(dir().get());
174 ASSERT_EQ(OPENED,
175 dir()->Open(kDirectoryName,
176 directory_change_delegate(),
177 NullTransactionObserver()));
178 ASSERT_TRUE(dir()->good());
181 void SaveAndReloadDir() {
182 dir()->SaveChanges();
183 CreateDirectory();
186 void StartFailingSaveChanges() {
187 test_directory_->StartFailingSaveChanges();
190 TestDirectory *test_directory_; // mirrors scoped_ptr<Directory> dir_
191 base::ScopedTempDir temp_dir_;
192 base::FilePath file_path_;
195 sync_pb::DataTypeContext BuildContext(ModelType type) {
196 sync_pb::DataTypeContext context;
197 context.set_context("context");
198 context.set_data_type_id(GetSpecificsFieldNumberFromModelType(type));
199 return context;
202 TEST_F(OnDiskSyncableDirectoryTest, TestPurgeEntriesWithTypeIn) {
203 sync_pb::EntitySpecifics bookmark_specs;
204 sync_pb::EntitySpecifics autofill_specs;
205 sync_pb::EntitySpecifics preference_specs;
206 AddDefaultFieldValue(BOOKMARKS, &bookmark_specs);
207 AddDefaultFieldValue(PREFERENCES, &preference_specs);
208 AddDefaultFieldValue(AUTOFILL, &autofill_specs);
210 ModelTypeSet types_to_purge(PREFERENCES, AUTOFILL);
212 dir()->SetDownloadProgress(BOOKMARKS, BuildProgress(BOOKMARKS));
213 dir()->SetDownloadProgress(PREFERENCES, BuildProgress(PREFERENCES));
214 dir()->SetDownloadProgress(AUTOFILL, BuildProgress(AUTOFILL));
216 TestIdFactory id_factory;
217 // Create some items for each type.
219 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
221 dir()->SetDataTypeContext(&trans, BOOKMARKS, BuildContext(BOOKMARKS));
222 dir()->SetDataTypeContext(&trans, PREFERENCES, BuildContext(PREFERENCES));
223 dir()->SetDataTypeContext(&trans, AUTOFILL, BuildContext(AUTOFILL));
225 // Make it look like these types have completed initial sync.
226 CreateTypeRoot(&trans, dir().get(), BOOKMARKS);
227 CreateTypeRoot(&trans, dir().get(), PREFERENCES);
228 CreateTypeRoot(&trans, dir().get(), AUTOFILL);
230 // Add more nodes for this type. Technically, they should be placed under
231 // the proper type root nodes but the assertions in this test won't notice
232 // if their parent isn't quite right.
233 MutableEntry item1(&trans, CREATE, BOOKMARKS, trans.root_id(), "Item");
234 ASSERT_TRUE(item1.good());
235 item1.PutServerSpecifics(bookmark_specs);
236 item1.PutIsUnsynced(true);
238 MutableEntry item2(&trans, CREATE_NEW_UPDATE_ITEM,
239 id_factory.NewServerId());
240 ASSERT_TRUE(item2.good());
241 item2.PutServerSpecifics(bookmark_specs);
242 item2.PutIsUnappliedUpdate(true);
244 MutableEntry item3(&trans, CREATE, PREFERENCES,
245 trans.root_id(), "Item");
246 ASSERT_TRUE(item3.good());
247 item3.PutSpecifics(preference_specs);
248 item3.PutServerSpecifics(preference_specs);
249 item3.PutIsUnsynced(true);
251 MutableEntry item4(&trans, CREATE_NEW_UPDATE_ITEM,
252 id_factory.NewServerId());
253 ASSERT_TRUE(item4.good());
254 item4.PutServerSpecifics(preference_specs);
255 item4.PutIsUnappliedUpdate(true);
257 MutableEntry item5(&trans, CREATE, AUTOFILL,
258 trans.root_id(), "Item");
259 ASSERT_TRUE(item5.good());
260 item5.PutSpecifics(autofill_specs);
261 item5.PutServerSpecifics(autofill_specs);
262 item5.PutIsUnsynced(true);
264 MutableEntry item6(&trans, CREATE_NEW_UPDATE_ITEM,
265 id_factory.NewServerId());
266 ASSERT_TRUE(item6.good());
267 item6.PutServerSpecifics(autofill_specs);
268 item6.PutIsUnappliedUpdate(true);
271 dir()->SaveChanges();
273 ReadTransaction trans(FROM_HERE, dir().get());
274 MetahandleSet all_set;
275 GetAllMetaHandles(&trans, &all_set);
276 ASSERT_EQ(10U, all_set.size());
279 dir()->PurgeEntriesWithTypeIn(types_to_purge, ModelTypeSet(), ModelTypeSet());
281 // We first query the in-memory data, and then reload the directory (without
282 // saving) to verify that disk does not still have the data.
283 CheckPurgeEntriesWithTypeInSucceeded(types_to_purge, true);
284 SaveAndReloadDir();
285 CheckPurgeEntriesWithTypeInSucceeded(types_to_purge, false);
288 TEST_F(OnDiskSyncableDirectoryTest, TestShareInfo) {
289 dir()->set_store_birthday("Jan 31st");
290 const char* const bag_of_chips_array = "\0bag of chips";
291 const std::string bag_of_chips_string =
292 std::string(bag_of_chips_array, sizeof(bag_of_chips_array));
293 dir()->set_bag_of_chips(bag_of_chips_string);
295 ReadTransaction trans(FROM_HERE, dir().get());
296 EXPECT_EQ("Jan 31st", dir()->store_birthday());
297 EXPECT_EQ(bag_of_chips_string, dir()->bag_of_chips());
299 dir()->set_store_birthday("April 10th");
300 const char* const bag_of_chips2_array = "\0bag of chips2";
301 const std::string bag_of_chips2_string =
302 std::string(bag_of_chips2_array, sizeof(bag_of_chips2_array));
303 dir()->set_bag_of_chips(bag_of_chips2_string);
304 dir()->SaveChanges();
306 ReadTransaction trans(FROM_HERE, dir().get());
307 EXPECT_EQ("April 10th", dir()->store_birthday());
308 EXPECT_EQ(bag_of_chips2_string, dir()->bag_of_chips());
310 const char* const bag_of_chips3_array = "\0bag of chips3";
311 const std::string bag_of_chips3_string =
312 std::string(bag_of_chips3_array, sizeof(bag_of_chips3_array));
313 dir()->set_bag_of_chips(bag_of_chips3_string);
314 // Restore the directory from disk. Make sure that nothing's changed.
315 SaveAndReloadDir();
317 ReadTransaction trans(FROM_HERE, dir().get());
318 EXPECT_EQ("April 10th", dir()->store_birthday());
319 EXPECT_EQ(bag_of_chips3_string, dir()->bag_of_chips());
323 TEST_F(OnDiskSyncableDirectoryTest,
324 TestSimpleFieldsPreservedDuringSaveChanges) {
325 Id update_id = TestIdFactory::FromNumber(1);
326 Id create_id;
327 EntryKernel create_pre_save, update_pre_save;
328 EntryKernel create_post_save, update_post_save;
329 std::string create_name = "Create";
332 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
333 MutableEntry create(
334 &trans, CREATE, BOOKMARKS, trans.root_id(), create_name);
335 MutableEntry update(&trans, CREATE_NEW_UPDATE_ITEM, update_id);
336 create.PutIsUnsynced(true);
337 update.PutIsUnappliedUpdate(true);
338 sync_pb::EntitySpecifics specifics;
339 specifics.mutable_bookmark()->set_favicon("PNG");
340 specifics.mutable_bookmark()->set_url("http://nowhere");
341 create.PutSpecifics(specifics);
342 update.PutServerSpecifics(specifics);
343 create_pre_save = create.GetKernelCopy();
344 update_pre_save = update.GetKernelCopy();
345 create_id = create.GetId();
348 dir()->SaveChanges();
349 dir().reset(
350 new Directory(new OnDiskDirectoryBackingStore(kDirectoryName, file_path_),
351 MakeWeakHandle(unrecoverable_error_handler()->GetWeakPtr()),
352 base::Closure(), NULL, NULL));
354 ASSERT_TRUE(dir().get());
355 ASSERT_EQ(OPENED,
356 dir()->Open(kDirectoryName,
357 directory_change_delegate(),
358 NullTransactionObserver()));
359 ASSERT_TRUE(dir()->good());
362 ReadTransaction trans(FROM_HERE, dir().get());
363 Entry create(&trans, GET_BY_ID, create_id);
364 EXPECT_EQ(1, CountEntriesWithName(&trans, trans.root_id(), create_name));
365 Entry update(&trans, GET_BY_ID, update_id);
366 create_post_save = create.GetKernelCopy();
367 update_post_save = update.GetKernelCopy();
369 int i = BEGIN_FIELDS;
370 for ( ; i < INT64_FIELDS_END ; ++i) {
371 EXPECT_EQ(create_pre_save.ref((Int64Field)i) +
372 (i == TRANSACTION_VERSION ? 1 : 0),
373 create_post_save.ref((Int64Field)i))
374 << "int64 field #" << i << " changed during save/load";
375 EXPECT_EQ(update_pre_save.ref((Int64Field)i),
376 update_post_save.ref((Int64Field)i))
377 << "int64 field #" << i << " changed during save/load";
379 for ( ; i < TIME_FIELDS_END ; ++i) {
380 EXPECT_EQ(create_pre_save.ref((TimeField)i),
381 create_post_save.ref((TimeField)i))
382 << "time field #" << i << " changed during save/load";
383 EXPECT_EQ(update_pre_save.ref((TimeField)i),
384 update_post_save.ref((TimeField)i))
385 << "time field #" << i << " changed during save/load";
387 for ( ; i < ID_FIELDS_END ; ++i) {
388 EXPECT_EQ(create_pre_save.ref((IdField)i),
389 create_post_save.ref((IdField)i))
390 << "id field #" << i << " changed during save/load";
391 EXPECT_EQ(update_pre_save.ref((IdField)i),
392 update_pre_save.ref((IdField)i))
393 << "id field #" << i << " changed during save/load";
395 for ( ; i < BIT_FIELDS_END ; ++i) {
396 EXPECT_EQ(create_pre_save.ref((BitField)i),
397 create_post_save.ref((BitField)i))
398 << "Bit field #" << i << " changed during save/load";
399 EXPECT_EQ(update_pre_save.ref((BitField)i),
400 update_post_save.ref((BitField)i))
401 << "Bit field #" << i << " changed during save/load";
403 for ( ; i < STRING_FIELDS_END ; ++i) {
404 EXPECT_EQ(create_pre_save.ref((StringField)i),
405 create_post_save.ref((StringField)i))
406 << "String field #" << i << " changed during save/load";
407 EXPECT_EQ(update_pre_save.ref((StringField)i),
408 update_post_save.ref((StringField)i))
409 << "String field #" << i << " changed during save/load";
411 for ( ; i < PROTO_FIELDS_END; ++i) {
412 EXPECT_EQ(create_pre_save.ref((ProtoField)i).SerializeAsString(),
413 create_post_save.ref((ProtoField)i).SerializeAsString())
414 << "Blob field #" << i << " changed during save/load";
415 EXPECT_EQ(update_pre_save.ref((ProtoField)i).SerializeAsString(),
416 update_post_save.ref((ProtoField)i).SerializeAsString())
417 << "Blob field #" << i << " changed during save/load";
419 for ( ; i < UNIQUE_POSITION_FIELDS_END; ++i) {
420 EXPECT_TRUE(create_pre_save.ref((UniquePositionField)i).Equals(
421 create_post_save.ref((UniquePositionField)i)))
422 << "Position field #" << i << " changed during save/load";
423 EXPECT_TRUE(update_pre_save.ref((UniquePositionField)i).Equals(
424 update_post_save.ref((UniquePositionField)i)))
425 << "Position field #" << i << " changed during save/load";
429 TEST_F(OnDiskSyncableDirectoryTest, TestSaveChangesFailure) {
430 int64 handle1 = 0;
431 // Set up an item using a regular, saveable directory.
433 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
435 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), "aguilera");
436 ASSERT_TRUE(e1.good());
437 EXPECT_TRUE(e1.GetKernelCopy().is_dirty());
438 handle1 = e1.GetMetahandle();
439 e1.PutBaseVersion(1);
440 e1.PutIsDir(true);
441 e1.PutId(TestIdFactory::FromNumber(101));
442 EXPECT_TRUE(e1.GetKernelCopy().is_dirty());
443 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
445 ASSERT_TRUE(dir()->SaveChanges());
447 // Make sure the item is no longer dirty after saving,
448 // and make a modification.
450 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
452 MutableEntry aguilera(&trans, GET_BY_HANDLE, handle1);
453 ASSERT_TRUE(aguilera.good());
454 EXPECT_FALSE(aguilera.GetKernelCopy().is_dirty());
455 EXPECT_EQ(aguilera.GetNonUniqueName(), "aguilera");
456 aguilera.PutNonUniqueName("overwritten");
457 EXPECT_TRUE(aguilera.GetKernelCopy().is_dirty());
458 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
460 ASSERT_TRUE(dir()->SaveChanges());
462 // Now do some operations when SaveChanges() will fail.
463 StartFailingSaveChanges();
464 ASSERT_TRUE(dir()->good());
466 int64 handle2 = 0;
468 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
470 MutableEntry aguilera(&trans, GET_BY_HANDLE, handle1);
471 ASSERT_TRUE(aguilera.good());
472 EXPECT_FALSE(aguilera.GetKernelCopy().is_dirty());
473 EXPECT_EQ(aguilera.GetNonUniqueName(), "overwritten");
474 EXPECT_FALSE(aguilera.GetKernelCopy().is_dirty());
475 EXPECT_FALSE(IsInDirtyMetahandles(handle1));
476 aguilera.PutNonUniqueName("christina");
477 EXPECT_TRUE(aguilera.GetKernelCopy().is_dirty());
478 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
480 // New item.
481 MutableEntry kids_on_block(
482 &trans, CREATE, BOOKMARKS, trans.root_id(), "kids");
483 ASSERT_TRUE(kids_on_block.good());
484 handle2 = kids_on_block.GetMetahandle();
485 kids_on_block.PutBaseVersion(1);
486 kids_on_block.PutIsDir(true);
487 kids_on_block.PutId(TestIdFactory::FromNumber(102));
488 EXPECT_TRUE(kids_on_block.GetKernelCopy().is_dirty());
489 EXPECT_TRUE(IsInDirtyMetahandles(handle2));
492 // We are using an unsaveable directory, so this can't succeed. However,
493 // the HandleSaveChangesFailure code path should have been triggered.
494 ASSERT_FALSE(dir()->SaveChanges());
496 // Make sure things were rolled back and the world is as it was before call.
498 ReadTransaction trans(FROM_HERE, dir().get());
499 Entry e1(&trans, GET_BY_HANDLE, handle1);
500 ASSERT_TRUE(e1.good());
501 EntryKernel aguilera = e1.GetKernelCopy();
502 Entry kids(&trans, GET_BY_HANDLE, handle2);
503 ASSERT_TRUE(kids.good());
504 EXPECT_TRUE(kids.GetKernelCopy().is_dirty());
505 EXPECT_TRUE(IsInDirtyMetahandles(handle2));
506 EXPECT_TRUE(aguilera.is_dirty());
507 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
511 TEST_F(OnDiskSyncableDirectoryTest, TestSaveChangesFailureWithPurge) {
512 int64 handle1 = 0;
513 // Set up an item and progress marker using a regular, saveable directory.
514 dir()->SetDownloadProgress(BOOKMARKS, BuildProgress(BOOKMARKS));
516 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
518 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), "aguilera");
519 ASSERT_TRUE(e1.good());
520 EXPECT_TRUE(e1.GetKernelCopy().is_dirty());
521 handle1 = e1.GetMetahandle();
522 e1.PutBaseVersion(1);
523 e1.PutIsDir(true);
524 e1.PutId(TestIdFactory::FromNumber(101));
525 sync_pb::EntitySpecifics bookmark_specs;
526 AddDefaultFieldValue(BOOKMARKS, &bookmark_specs);
527 e1.PutSpecifics(bookmark_specs);
528 e1.PutServerSpecifics(bookmark_specs);
529 e1.PutId(TestIdFactory::FromNumber(101));
530 EXPECT_TRUE(e1.GetKernelCopy().is_dirty());
531 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
533 ASSERT_TRUE(dir()->SaveChanges());
535 // Now do some operations while SaveChanges() is set to fail.
536 StartFailingSaveChanges();
537 ASSERT_TRUE(dir()->good());
539 ModelTypeSet set(BOOKMARKS);
540 dir()->PurgeEntriesWithTypeIn(set, ModelTypeSet(), ModelTypeSet());
541 EXPECT_TRUE(IsInMetahandlesToPurge(handle1));
542 ASSERT_FALSE(dir()->SaveChanges());
543 EXPECT_TRUE(IsInMetahandlesToPurge(handle1));
546 class SyncableDirectoryManagement : public testing::Test {
547 public:
548 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
550 void TearDown() override {}
552 protected:
553 base::MessageLoop message_loop_;
554 base::ScopedTempDir temp_dir_;
555 FakeEncryptor encryptor_;
556 TestUnrecoverableErrorHandler handler_;
557 NullDirectoryChangeDelegate delegate_;
560 TEST_F(SyncableDirectoryManagement, TestFileRelease) {
561 base::FilePath path =
562 temp_dir_.path().Append(Directory::kSyncDatabaseFilename);
565 Directory dir(new OnDiskDirectoryBackingStore("ScopeTest", path),
566 MakeWeakHandle(handler_.GetWeakPtr()), base::Closure(), NULL,
567 NULL);
568 DirOpenResult result =
569 dir.Open("ScopeTest", &delegate_, NullTransactionObserver());
570 ASSERT_EQ(result, OPENED);
573 // Destroying the directory should have released the backing database file.
574 ASSERT_TRUE(base::DeleteFile(path, true));
577 class SyncableClientTagTest : public SyncableDirectoryTest {
578 public:
579 static const int kBaseVersion = 1;
580 const char* test_name_;
581 const char* test_tag_;
583 SyncableClientTagTest() : test_name_("test_name"), test_tag_("dietcoke") {}
585 bool CreateWithDefaultTag(Id id, bool deleted) {
586 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
587 MutableEntry me(&wtrans, CREATE, PREFERENCES,
588 wtrans.root_id(), test_name_);
589 CHECK(me.good());
590 me.PutId(id);
591 if (id.ServerKnows()) {
592 me.PutBaseVersion(kBaseVersion);
594 me.PutIsUnsynced(true);
595 me.PutIsDel(deleted);
596 me.PutIsDir(false);
597 return me.PutUniqueClientTag(test_tag_);
600 // Verify an entry exists with the default tag.
601 void VerifyTag(Id id, bool deleted) {
602 // Should still be present and valid in the client tag index.
603 ReadTransaction trans(FROM_HERE, dir().get());
604 Entry me(&trans, GET_BY_CLIENT_TAG, test_tag_);
605 CHECK(me.good());
606 EXPECT_EQ(me.GetId(), id);
607 EXPECT_EQ(me.GetUniqueClientTag(), test_tag_);
608 EXPECT_EQ(me.GetIsDel(), deleted);
610 // We only sync deleted items that the server knew about.
611 if (me.GetId().ServerKnows() || !me.GetIsDel()) {
612 EXPECT_EQ(me.GetIsUnsynced(), true);
616 protected:
617 TestIdFactory factory_;
620 TEST_F(SyncableClientTagTest, TestClientTagClear) {
621 Id server_id = factory_.NewServerId();
622 EXPECT_TRUE(CreateWithDefaultTag(server_id, false));
624 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
625 MutableEntry me(&trans, GET_BY_CLIENT_TAG, test_tag_);
626 EXPECT_TRUE(me.good());
627 me.PutUniqueClientTag(std::string());
630 ReadTransaction trans(FROM_HERE, dir().get());
631 Entry by_tag(&trans, GET_BY_CLIENT_TAG, test_tag_);
632 EXPECT_FALSE(by_tag.good());
634 Entry by_id(&trans, GET_BY_ID, server_id);
635 EXPECT_TRUE(by_id.good());
636 EXPECT_TRUE(by_id.GetUniqueClientTag().empty());
640 TEST_F(SyncableClientTagTest, TestClientTagIndexServerId) {
641 Id server_id = factory_.NewServerId();
642 EXPECT_TRUE(CreateWithDefaultTag(server_id, false));
643 VerifyTag(server_id, false);
646 TEST_F(SyncableClientTagTest, TestClientTagIndexClientId) {
647 Id client_id = factory_.NewLocalId();
648 EXPECT_TRUE(CreateWithDefaultTag(client_id, false));
649 VerifyTag(client_id, false);
652 TEST_F(SyncableClientTagTest, TestDeletedClientTagIndexClientId) {
653 Id client_id = factory_.NewLocalId();
654 EXPECT_TRUE(CreateWithDefaultTag(client_id, true));
655 VerifyTag(client_id, true);
658 TEST_F(SyncableClientTagTest, TestDeletedClientTagIndexServerId) {
659 Id server_id = factory_.NewServerId();
660 EXPECT_TRUE(CreateWithDefaultTag(server_id, true));
661 VerifyTag(server_id, true);
664 TEST_F(SyncableClientTagTest, TestClientTagIndexDuplicateServer) {
665 EXPECT_TRUE(CreateWithDefaultTag(factory_.NewServerId(), true));
666 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), true));
667 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), false));
668 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), false));
669 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), true));
672 } // namespace syncable
673 } // namespace syncer