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 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(
137 TestDirectory::Create(&encryptor
, &handler
, 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
{
147 // SetUp() is called before each test case is run.
148 // The sqlite3 DB is deleted before each test is run.
149 virtual void SetUp() {
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);
158 virtual void TearDown() {
159 // This also closes file handles.
160 dir()->SaveChanges();
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(), unrecoverable_error_handler(), kDirectoryName
, file_path_
);
170 dir().reset(test_directory_
);
171 ASSERT_TRUE(dir().get());
173 dir()->Open(kDirectoryName
,
174 directory_change_delegate(),
175 NullTransactionObserver()));
176 ASSERT_TRUE(dir()->good());
179 void SaveAndReloadDir() {
180 dir()->SaveChanges();
184 void StartFailingSaveChanges() {
185 test_directory_
->StartFailingSaveChanges();
188 TestDirectory
*test_directory_
; // mirrors scoped_ptr<Directory> dir_
189 base::ScopedTempDir temp_dir_
;
190 base::FilePath file_path_
;
193 sync_pb::DataTypeProgressMarker
BuildProgress(ModelType type
) {
194 sync_pb::DataTypeProgressMarker progress
;
195 progress
.set_token("token");
196 progress
.set_data_type_id(GetSpecificsFieldNumberFromModelType(type
));
200 sync_pb::DataTypeContext
BuildContext(ModelType type
) {
201 sync_pb::DataTypeContext context
;
202 context
.set_context("context");
203 context
.set_data_type_id(GetSpecificsFieldNumberFromModelType(type
));
207 TEST_F(OnDiskSyncableDirectoryTest
, TestPurgeEntriesWithTypeIn
) {
208 sync_pb::EntitySpecifics bookmark_specs
;
209 sync_pb::EntitySpecifics autofill_specs
;
210 sync_pb::EntitySpecifics preference_specs
;
211 AddDefaultFieldValue(BOOKMARKS
, &bookmark_specs
);
212 AddDefaultFieldValue(PREFERENCES
, &preference_specs
);
213 AddDefaultFieldValue(AUTOFILL
, &autofill_specs
);
215 ModelTypeSet
types_to_purge(PREFERENCES
, AUTOFILL
);
217 dir()->SetDownloadProgress(BOOKMARKS
, BuildProgress(BOOKMARKS
));
218 dir()->SetDownloadProgress(PREFERENCES
, BuildProgress(PREFERENCES
));
219 dir()->SetDownloadProgress(AUTOFILL
, BuildProgress(AUTOFILL
));
221 TestIdFactory id_factory
;
222 // Create some items for each type.
224 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
226 dir()->SetDataTypeContext(&trans
, BOOKMARKS
, BuildContext(BOOKMARKS
));
227 dir()->SetDataTypeContext(&trans
, PREFERENCES
, BuildContext(PREFERENCES
));
228 dir()->SetDataTypeContext(&trans
, AUTOFILL
, BuildContext(AUTOFILL
));
230 // Make it look like these types have completed initial sync.
231 CreateTypeRoot(&trans
, dir().get(), BOOKMARKS
);
232 CreateTypeRoot(&trans
, dir().get(), PREFERENCES
);
233 CreateTypeRoot(&trans
, dir().get(), AUTOFILL
);
235 // Add more nodes for this type. Technically, they should be placed under
236 // the proper type root nodes but the assertions in this test won't notice
237 // if their parent isn't quite right.
238 MutableEntry
item1(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "Item");
239 ASSERT_TRUE(item1
.good());
240 item1
.PutServerSpecifics(bookmark_specs
);
241 item1
.PutIsUnsynced(true);
243 MutableEntry
item2(&trans
, CREATE_NEW_UPDATE_ITEM
,
244 id_factory
.NewServerId());
245 ASSERT_TRUE(item2
.good());
246 item2
.PutServerSpecifics(bookmark_specs
);
247 item2
.PutIsUnappliedUpdate(true);
249 MutableEntry
item3(&trans
, CREATE
, PREFERENCES
,
250 trans
.root_id(), "Item");
251 ASSERT_TRUE(item3
.good());
252 item3
.PutSpecifics(preference_specs
);
253 item3
.PutServerSpecifics(preference_specs
);
254 item3
.PutIsUnsynced(true);
256 MutableEntry
item4(&trans
, CREATE_NEW_UPDATE_ITEM
,
257 id_factory
.NewServerId());
258 ASSERT_TRUE(item4
.good());
259 item4
.PutServerSpecifics(preference_specs
);
260 item4
.PutIsUnappliedUpdate(true);
262 MutableEntry
item5(&trans
, CREATE
, AUTOFILL
,
263 trans
.root_id(), "Item");
264 ASSERT_TRUE(item5
.good());
265 item5
.PutSpecifics(autofill_specs
);
266 item5
.PutServerSpecifics(autofill_specs
);
267 item5
.PutIsUnsynced(true);
269 MutableEntry
item6(&trans
, CREATE_NEW_UPDATE_ITEM
,
270 id_factory
.NewServerId());
271 ASSERT_TRUE(item6
.good());
272 item6
.PutServerSpecifics(autofill_specs
);
273 item6
.PutIsUnappliedUpdate(true);
276 dir()->SaveChanges();
278 ReadTransaction
trans(FROM_HERE
, dir().get());
279 MetahandleSet all_set
;
280 GetAllMetaHandles(&trans
, &all_set
);
281 ASSERT_EQ(10U, all_set
.size());
284 dir()->PurgeEntriesWithTypeIn(types_to_purge
, ModelTypeSet(), ModelTypeSet());
286 // We first query the in-memory data, and then reload the directory (without
287 // saving) to verify that disk does not still have the data.
288 CheckPurgeEntriesWithTypeInSucceeded(types_to_purge
, true);
290 CheckPurgeEntriesWithTypeInSucceeded(types_to_purge
, false);
293 TEST_F(OnDiskSyncableDirectoryTest
, TestShareInfo
) {
294 dir()->set_store_birthday("Jan 31st");
295 const char* const bag_of_chips_array
= "\0bag of chips";
296 const std::string bag_of_chips_string
=
297 std::string(bag_of_chips_array
, sizeof(bag_of_chips_array
));
298 dir()->set_bag_of_chips(bag_of_chips_string
);
300 ReadTransaction
trans(FROM_HERE
, dir().get());
301 EXPECT_EQ("Jan 31st", dir()->store_birthday());
302 EXPECT_EQ(bag_of_chips_string
, dir()->bag_of_chips());
304 dir()->set_store_birthday("April 10th");
305 const char* const bag_of_chips2_array
= "\0bag of chips2";
306 const std::string bag_of_chips2_string
=
307 std::string(bag_of_chips2_array
, sizeof(bag_of_chips2_array
));
308 dir()->set_bag_of_chips(bag_of_chips2_string
);
309 dir()->SaveChanges();
311 ReadTransaction
trans(FROM_HERE
, dir().get());
312 EXPECT_EQ("April 10th", dir()->store_birthday());
313 EXPECT_EQ(bag_of_chips2_string
, dir()->bag_of_chips());
315 const char* const bag_of_chips3_array
= "\0bag of chips3";
316 const std::string bag_of_chips3_string
=
317 std::string(bag_of_chips3_array
, sizeof(bag_of_chips3_array
));
318 dir()->set_bag_of_chips(bag_of_chips3_string
);
319 // Restore the directory from disk. Make sure that nothing's changed.
322 ReadTransaction
trans(FROM_HERE
, dir().get());
323 EXPECT_EQ("April 10th", dir()->store_birthday());
324 EXPECT_EQ(bag_of_chips3_string
, dir()->bag_of_chips());
328 TEST_F(OnDiskSyncableDirectoryTest
,
329 TestSimpleFieldsPreservedDuringSaveChanges
) {
330 Id update_id
= TestIdFactory::FromNumber(1);
332 EntryKernel create_pre_save
, update_pre_save
;
333 EntryKernel create_post_save
, update_post_save
;
334 std::string create_name
= "Create";
337 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
339 &trans
, CREATE
, BOOKMARKS
, trans
.root_id(), create_name
);
340 MutableEntry
update(&trans
, CREATE_NEW_UPDATE_ITEM
, update_id
);
341 create
.PutIsUnsynced(true);
342 update
.PutIsUnappliedUpdate(true);
343 sync_pb::EntitySpecifics specifics
;
344 specifics
.mutable_bookmark()->set_favicon("PNG");
345 specifics
.mutable_bookmark()->set_url("http://nowhere");
346 create
.PutSpecifics(specifics
);
347 update
.PutSpecifics(specifics
);
348 create_pre_save
= create
.GetKernelCopy();
349 update_pre_save
= update
.GetKernelCopy();
350 create_id
= create
.GetId();
353 dir()->SaveChanges();
355 new Directory(new OnDiskDirectoryBackingStore(kDirectoryName
, file_path_
),
356 unrecoverable_error_handler(),
361 ASSERT_TRUE(dir().get());
363 dir()->Open(kDirectoryName
,
364 directory_change_delegate(),
365 NullTransactionObserver()));
366 ASSERT_TRUE(dir()->good());
369 ReadTransaction
trans(FROM_HERE
, dir().get());
370 Entry
create(&trans
, GET_BY_ID
, create_id
);
371 EXPECT_EQ(1, CountEntriesWithName(&trans
, trans
.root_id(), create_name
));
372 Entry
update(&trans
, GET_BY_ID
, update_id
);
373 create_post_save
= create
.GetKernelCopy();
374 update_post_save
= update
.GetKernelCopy();
376 int i
= BEGIN_FIELDS
;
377 for ( ; i
< INT64_FIELDS_END
; ++i
) {
378 EXPECT_EQ(create_pre_save
.ref((Int64Field
)i
) +
379 (i
== TRANSACTION_VERSION
? 1 : 0),
380 create_post_save
.ref((Int64Field
)i
))
381 << "int64 field #" << i
<< " changed during save/load";
382 EXPECT_EQ(update_pre_save
.ref((Int64Field
)i
) +
383 (i
== TRANSACTION_VERSION
? 1 : 0),
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