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.
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/file_util.h"
10 #include "base/files/file_path.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"
44 using base::ExpectDictBooleanValue
;
45 using base::ExpectDictStringValue
;
47 // An OnDiskDirectoryBackingStore that can be set to always fail SaveChanges.
48 class TestBackingStore
: public OnDiskDirectoryBackingStore
{
50 TestBackingStore(const std::string
& dir_name
,
51 const base::FilePath
& backing_filepath
);
53 virtual ~TestBackingStore();
55 virtual bool SaveChanges(const Directory::SaveChangesSnapshot
& snapshot
)
58 void StartFailingSaveChanges() {
59 fail_save_changes_
= true;
63 bool fail_save_changes_
;
66 TestBackingStore::TestBackingStore(const std::string
& dir_name
,
67 const base::FilePath
& backing_filepath
)
68 : OnDiskDirectoryBackingStore(dir_name
, backing_filepath
),
69 fail_save_changes_(false) {
72 TestBackingStore::~TestBackingStore() { }
74 bool TestBackingStore::SaveChanges(
75 const Directory::SaveChangesSnapshot
& snapshot
){
76 if (fail_save_changes_
) {
79 return OnDiskDirectoryBackingStore::SaveChanges(snapshot
);
83 // A directory whose Save() function can be set to always fail.
84 class TestDirectory
: public Directory
{
86 // A factory function used to work around some initialization order issues.
87 static TestDirectory
* Create(
89 UnrecoverableErrorHandler
*handler
,
90 const std::string
& dir_name
,
91 const base::FilePath
& backing_filepath
);
93 virtual ~TestDirectory();
95 void StartFailingSaveChanges() {
96 backing_store_
->StartFailingSaveChanges();
100 TestDirectory(Encryptor
* encryptor
,
101 UnrecoverableErrorHandler
* handler
,
102 TestBackingStore
* backing_store
);
104 TestBackingStore
* backing_store_
;
107 TestDirectory
* TestDirectory::Create(
108 Encryptor
*encryptor
,
109 UnrecoverableErrorHandler
*handler
,
110 const std::string
& dir_name
,
111 const base::FilePath
& backing_filepath
) {
112 TestBackingStore
* backing_store
=
113 new TestBackingStore(dir_name
, backing_filepath
);
114 return new TestDirectory(encryptor
, handler
, backing_store
);
117 TestDirectory::TestDirectory(Encryptor
* encryptor
,
118 UnrecoverableErrorHandler
* handler
,
119 TestBackingStore
* backing_store
)
120 : Directory(backing_store
, handler
, NULL
, NULL
, NULL
),
121 backing_store_(backing_store
) {
124 TestDirectory::~TestDirectory() { }
126 TEST(OnDiskSyncableDirectory
, FailInitialWrite
) {
127 base::MessageLoop message_loop
;
128 FakeEncryptor encryptor
;
129 TestUnrecoverableErrorHandler handler
;
130 base::ScopedTempDir temp_dir
;
131 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
132 base::FilePath file_path
= temp_dir
.path().Append(
133 FILE_PATH_LITERAL("Test.sqlite3"));
134 std::string name
= "user@x.com";
135 NullDirectoryChangeDelegate delegate
;
137 scoped_ptr
<TestDirectory
> test_dir(
138 TestDirectory::Create(&encryptor
, &handler
, name
, file_path
));
140 test_dir
->StartFailingSaveChanges();
141 ASSERT_EQ(FAILED_INITIAL_WRITE
, test_dir
->Open(name
, &delegate
,
142 NullTransactionObserver()));
145 // A variant of SyncableDirectoryTest that uses a real sqlite database.
146 class OnDiskSyncableDirectoryTest
: public SyncableDirectoryTest
{
148 // SetUp() is called before each test case is run.
149 // The sqlite3 DB is deleted before each test is run.
150 virtual void SetUp() {
151 SyncableDirectoryTest::SetUp();
152 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
153 file_path_
= temp_dir_
.path().Append(
154 FILE_PATH_LITERAL("Test.sqlite3"));
155 base::DeleteFile(file_path_
, true);
159 virtual void TearDown() {
160 // This also closes file handles.
161 dir()->SaveChanges();
163 base::DeleteFile(file_path_
, true);
164 SyncableDirectoryTest::TearDown();
167 // Creates a new directory. Deletes the old directory, if it exists.
168 void CreateDirectory() {
169 test_directory_
= TestDirectory::Create(
170 encryptor(), unrecoverable_error_handler(), kDirectoryName
, file_path_
);
171 dir().reset(test_directory_
);
172 ASSERT_TRUE(dir().get());
174 dir()->Open(kDirectoryName
,
175 directory_change_delegate(),
176 NullTransactionObserver()));
177 ASSERT_TRUE(dir()->good());
180 void SaveAndReloadDir() {
181 dir()->SaveChanges();
185 void StartFailingSaveChanges() {
186 test_directory_
->StartFailingSaveChanges();
189 TestDirectory
*test_directory_
; // mirrors scoped_ptr<Directory> dir_
190 base::ScopedTempDir temp_dir_
;
191 base::FilePath file_path_
;
194 sync_pb::DataTypeProgressMarker
BuildProgress(ModelType type
) {
195 sync_pb::DataTypeProgressMarker progress
;
196 progress
.set_token("token");
197 progress
.set_data_type_id(GetSpecificsFieldNumberFromModelType(type
));
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
));
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);
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.
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);
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());
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();
356 new Directory(new OnDiskDirectoryBackingStore(kDirectoryName
, file_path_
),
357 unrecoverable_error_handler(),
362 ASSERT_TRUE(dir().get());
364 dir()->Open(kDirectoryName
,
365 directory_change_delegate(),
366 NullTransactionObserver()));
367 ASSERT_TRUE(dir()->good());
370 ReadTransaction
trans(FROM_HERE
, dir().get());
371 Entry
create(&trans
, GET_BY_ID
, create_id
);
372 EXPECT_EQ(1, CountEntriesWithName(&trans
, trans
.root_id(), create_name
));
373 Entry
update(&trans
, GET_BY_ID
, update_id
);
374 create_post_save
= create
.GetKernelCopy();
375 update_post_save
= update
.GetKernelCopy();
377 int i
= BEGIN_FIELDS
;
378 for ( ; i
< INT64_FIELDS_END
; ++i
) {
379 EXPECT_EQ(create_pre_save
.ref((Int64Field
)i
) +
380 (i
== TRANSACTION_VERSION
? 1 : 0),
381 create_post_save
.ref((Int64Field
)i
))
382 << "int64 field #" << i
<< " changed during save/load";
383 EXPECT_EQ(update_pre_save
.ref((Int64Field
)i
),
384 update_post_save
.ref((Int64Field
)i
))
385 << "int64 field #" << i
<< " changed during save/load";
387 for ( ; i
< TIME_FIELDS_END
; ++i
) {
388 EXPECT_EQ(create_pre_save
.ref((TimeField
)i
),
389 create_post_save
.ref((TimeField
)i
))
390 << "time field #" << i
<< " changed during save/load";
391 EXPECT_EQ(update_pre_save
.ref((TimeField
)i
),
392 update_post_save
.ref((TimeField
)i
))
393 << "time field #" << i
<< " changed during save/load";
395 for ( ; i
< ID_FIELDS_END
; ++i
) {
396 EXPECT_EQ(create_pre_save
.ref((IdField
)i
),
397 create_post_save
.ref((IdField
)i
))
398 << "id field #" << i
<< " changed during save/load";
399 EXPECT_EQ(update_pre_save
.ref((IdField
)i
),
400 update_pre_save
.ref((IdField
)i
))
401 << "id field #" << i
<< " changed during save/load";
403 for ( ; i
< BIT_FIELDS_END
; ++i
) {
404 EXPECT_EQ(create_pre_save
.ref((BitField
)i
),
405 create_post_save
.ref((BitField
)i
))
406 << "Bit field #" << i
<< " changed during save/load";
407 EXPECT_EQ(update_pre_save
.ref((BitField
)i
),
408 update_post_save
.ref((BitField
)i
))
409 << "Bit field #" << i
<< " changed during save/load";
411 for ( ; i
< STRING_FIELDS_END
; ++i
) {
412 EXPECT_EQ(create_pre_save
.ref((StringField
)i
),
413 create_post_save
.ref((StringField
)i
))
414 << "String field #" << i
<< " changed during save/load";
415 EXPECT_EQ(update_pre_save
.ref((StringField
)i
),
416 update_post_save
.ref((StringField
)i
))
417 << "String field #" << i
<< " changed during save/load";
419 for ( ; i
< PROTO_FIELDS_END
; ++i
) {
420 EXPECT_EQ(create_pre_save
.ref((ProtoField
)i
).SerializeAsString(),
421 create_post_save
.ref((ProtoField
)i
).SerializeAsString())
422 << "Blob field #" << i
<< " changed during save/load";
423 EXPECT_EQ(update_pre_save
.ref((ProtoField
)i
).SerializeAsString(),
424 update_post_save
.ref((ProtoField
)i
).SerializeAsString())
425 << "Blob field #" << i
<< " changed during save/load";
427 for ( ; i
< UNIQUE_POSITION_FIELDS_END
; ++i
) {
428 EXPECT_TRUE(create_pre_save
.ref((UniquePositionField
)i
).Equals(
429 create_post_save
.ref((UniquePositionField
)i
)))
430 << "Position field #" << i
<< " changed during save/load";
431 EXPECT_TRUE(update_pre_save
.ref((UniquePositionField
)i
).Equals(
432 update_post_save
.ref((UniquePositionField
)i
)))
433 << "Position field #" << i
<< " changed during save/load";
437 TEST_F(OnDiskSyncableDirectoryTest
, TestSaveChangesFailure
) {
439 // Set up an item using a regular, saveable directory.
441 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
443 MutableEntry
e1(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "aguilera");
444 ASSERT_TRUE(e1
.good());
445 EXPECT_TRUE(e1
.GetKernelCopy().is_dirty());
446 handle1
= e1
.GetMetahandle();
447 e1
.PutBaseVersion(1);
449 e1
.PutId(TestIdFactory::FromNumber(101));
450 EXPECT_TRUE(e1
.GetKernelCopy().is_dirty());
451 EXPECT_TRUE(IsInDirtyMetahandles(handle1
));
453 ASSERT_TRUE(dir()->SaveChanges());
455 // Make sure the item is no longer dirty after saving,
456 // and make a modification.
458 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
460 MutableEntry
aguilera(&trans
, GET_BY_HANDLE
, handle1
);
461 ASSERT_TRUE(aguilera
.good());
462 EXPECT_FALSE(aguilera
.GetKernelCopy().is_dirty());
463 EXPECT_EQ(aguilera
.GetNonUniqueName(), "aguilera");
464 aguilera
.PutNonUniqueName("overwritten");
465 EXPECT_TRUE(aguilera
.GetKernelCopy().is_dirty());
466 EXPECT_TRUE(IsInDirtyMetahandles(handle1
));
468 ASSERT_TRUE(dir()->SaveChanges());
470 // Now do some operations when SaveChanges() will fail.
471 StartFailingSaveChanges();
472 ASSERT_TRUE(dir()->good());
476 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
478 MutableEntry
aguilera(&trans
, GET_BY_HANDLE
, handle1
);
479 ASSERT_TRUE(aguilera
.good());
480 EXPECT_FALSE(aguilera
.GetKernelCopy().is_dirty());
481 EXPECT_EQ(aguilera
.GetNonUniqueName(), "overwritten");
482 EXPECT_FALSE(aguilera
.GetKernelCopy().is_dirty());
483 EXPECT_FALSE(IsInDirtyMetahandles(handle1
));
484 aguilera
.PutNonUniqueName("christina");
485 EXPECT_TRUE(aguilera
.GetKernelCopy().is_dirty());
486 EXPECT_TRUE(IsInDirtyMetahandles(handle1
));
489 MutableEntry
kids_on_block(
490 &trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "kids");
491 ASSERT_TRUE(kids_on_block
.good());
492 handle2
= kids_on_block
.GetMetahandle();
493 kids_on_block
.PutBaseVersion(1);
494 kids_on_block
.PutIsDir(true);
495 kids_on_block
.PutId(TestIdFactory::FromNumber(102));
496 EXPECT_TRUE(kids_on_block
.GetKernelCopy().is_dirty());
497 EXPECT_TRUE(IsInDirtyMetahandles(handle2
));
500 // We are using an unsaveable directory, so this can't succeed. However,
501 // the HandleSaveChangesFailure code path should have been triggered.
502 ASSERT_FALSE(dir()->SaveChanges());
504 // Make sure things were rolled back and the world is as it was before call.
506 ReadTransaction
trans(FROM_HERE
, dir().get());
507 Entry
e1(&trans
, GET_BY_HANDLE
, handle1
);
508 ASSERT_TRUE(e1
.good());
509 EntryKernel aguilera
= e1
.GetKernelCopy();
510 Entry
kids(&trans
, GET_BY_HANDLE
, handle2
);
511 ASSERT_TRUE(kids
.good());
512 EXPECT_TRUE(kids
.GetKernelCopy().is_dirty());
513 EXPECT_TRUE(IsInDirtyMetahandles(handle2
));
514 EXPECT_TRUE(aguilera
.is_dirty());
515 EXPECT_TRUE(IsInDirtyMetahandles(handle1
));
519 TEST_F(OnDiskSyncableDirectoryTest
, TestSaveChangesFailureWithPurge
) {
521 // Set up an item using a regular, saveable directory.
523 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
525 MutableEntry
e1(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "aguilera");
526 ASSERT_TRUE(e1
.good());
527 EXPECT_TRUE(e1
.GetKernelCopy().is_dirty());
528 handle1
= e1
.GetMetahandle();
529 e1
.PutBaseVersion(1);
531 e1
.PutId(TestIdFactory::FromNumber(101));
532 sync_pb::EntitySpecifics bookmark_specs
;
533 AddDefaultFieldValue(BOOKMARKS
, &bookmark_specs
);
534 e1
.PutSpecifics(bookmark_specs
);
535 e1
.PutServerSpecifics(bookmark_specs
);
536 e1
.PutId(TestIdFactory::FromNumber(101));
537 EXPECT_TRUE(e1
.GetKernelCopy().is_dirty());
538 EXPECT_TRUE(IsInDirtyMetahandles(handle1
));
540 ASSERT_TRUE(dir()->SaveChanges());
542 // Now do some operations while SaveChanges() is set to fail.
543 StartFailingSaveChanges();
544 ASSERT_TRUE(dir()->good());
546 ModelTypeSet
set(BOOKMARKS
);
547 dir()->PurgeEntriesWithTypeIn(set
, ModelTypeSet(), ModelTypeSet());
548 EXPECT_TRUE(IsInMetahandlesToPurge(handle1
));
549 ASSERT_FALSE(dir()->SaveChanges());
550 EXPECT_TRUE(IsInMetahandlesToPurge(handle1
));
553 class SyncableDirectoryManagement
: public testing::Test
{
555 virtual void SetUp() {
556 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
559 virtual void TearDown() {
562 base::MessageLoop message_loop_
;
563 base::ScopedTempDir temp_dir_
;
564 FakeEncryptor encryptor_
;
565 TestUnrecoverableErrorHandler handler_
;
566 NullDirectoryChangeDelegate delegate_
;
569 TEST_F(SyncableDirectoryManagement
, TestFileRelease
) {
570 base::FilePath path
=
571 temp_dir_
.path().Append(Directory::kSyncDatabaseFilename
);
573 Directory
dir(new OnDiskDirectoryBackingStore("ScopeTest", path
),
578 DirOpenResult result
=
579 dir
.Open("ScopeTest", &delegate_
, NullTransactionObserver());
580 ASSERT_EQ(result
, OPENED
);
583 // Closing the directory should have released the backing database file.
584 ASSERT_TRUE(base::DeleteFile(path
, true));
587 class SyncableClientTagTest
: public SyncableDirectoryTest
{
589 static const int kBaseVersion
= 1;
590 const char* test_name_
;
591 const char* test_tag_
;
593 SyncableClientTagTest() : test_name_("test_name"), test_tag_("dietcoke") {}
595 bool CreateWithDefaultTag(Id id
, bool deleted
) {
596 WriteTransaction
wtrans(FROM_HERE
, UNITTEST
, dir().get());
597 MutableEntry
me(&wtrans
, CREATE
, PREFERENCES
,
598 wtrans
.root_id(), test_name_
);
601 if (id
.ServerKnows()) {
602 me
.PutBaseVersion(kBaseVersion
);
604 me
.PutIsUnsynced(true);
605 me
.PutIsDel(deleted
);
607 return me
.PutUniqueClientTag(test_tag_
);
610 // Verify an entry exists with the default tag.
611 void VerifyTag(Id id
, bool deleted
) {
612 // Should still be present and valid in the client tag index.
613 ReadTransaction
trans(FROM_HERE
, dir().get());
614 Entry
me(&trans
, GET_BY_CLIENT_TAG
, test_tag_
);
616 EXPECT_EQ(me
.GetId(), id
);
617 EXPECT_EQ(me
.GetUniqueClientTag(), test_tag_
);
618 EXPECT_EQ(me
.GetIsDel(), deleted
);
620 // We only sync deleted items that the server knew about.
621 if (me
.GetId().ServerKnows() || !me
.GetIsDel()) {
622 EXPECT_EQ(me
.GetIsUnsynced(), true);
627 TestIdFactory factory_
;
630 TEST_F(SyncableClientTagTest
, TestClientTagClear
) {
631 Id server_id
= factory_
.NewServerId();
632 EXPECT_TRUE(CreateWithDefaultTag(server_id
, false));
634 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
635 MutableEntry
me(&trans
, GET_BY_CLIENT_TAG
, test_tag_
);
636 EXPECT_TRUE(me
.good());
637 me
.PutUniqueClientTag(std::string());
640 ReadTransaction
trans(FROM_HERE
, dir().get());
641 Entry
by_tag(&trans
, GET_BY_CLIENT_TAG
, test_tag_
);
642 EXPECT_FALSE(by_tag
.good());
644 Entry
by_id(&trans
, GET_BY_ID
, server_id
);
645 EXPECT_TRUE(by_id
.good());
646 EXPECT_TRUE(by_id
.GetUniqueClientTag().empty());
650 TEST_F(SyncableClientTagTest
, TestClientTagIndexServerId
) {
651 Id server_id
= factory_
.NewServerId();
652 EXPECT_TRUE(CreateWithDefaultTag(server_id
, false));
653 VerifyTag(server_id
, false);
656 TEST_F(SyncableClientTagTest
, TestClientTagIndexClientId
) {
657 Id client_id
= factory_
.NewLocalId();
658 EXPECT_TRUE(CreateWithDefaultTag(client_id
, false));
659 VerifyTag(client_id
, false);
662 TEST_F(SyncableClientTagTest
, TestDeletedClientTagIndexClientId
) {
663 Id client_id
= factory_
.NewLocalId();
664 EXPECT_TRUE(CreateWithDefaultTag(client_id
, true));
665 VerifyTag(client_id
, true);
668 TEST_F(SyncableClientTagTest
, TestDeletedClientTagIndexServerId
) {
669 Id server_id
= factory_
.NewServerId();
670 EXPECT_TRUE(CreateWithDefaultTag(server_id
, true));
671 VerifyTag(server_id
, true);
674 TEST_F(SyncableClientTagTest
, TestClientTagIndexDuplicateServer
) {
675 EXPECT_TRUE(CreateWithDefaultTag(factory_
.NewServerId(), true));
676 EXPECT_FALSE(CreateWithDefaultTag(factory_
.NewServerId(), true));
677 EXPECT_FALSE(CreateWithDefaultTag(factory_
.NewServerId(), false));
678 EXPECT_FALSE(CreateWithDefaultTag(factory_
.NewLocalId(), false));
679 EXPECT_FALSE(CreateWithDefaultTag(factory_
.NewLocalId(), true));
682 } // namespace syncable
683 } // namespace syncer