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/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"
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 ~TestBackingStore() override
;
55 bool SaveChanges(const Directory::SaveChangesSnapshot
& snapshot
) override
;
57 void StartFailingSaveChanges() {
58 fail_save_changes_
= true;
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_
) {
78 return OnDiskDirectoryBackingStore::SaveChanges(snapshot
);
82 // A directory whose Save() function can be set to always fail.
83 class TestDirectory
: public Directory
{
85 // A factory function used to work around some initialization order issues.
86 static TestDirectory
* Create(
88 UnrecoverableErrorHandler
*handler
,
89 const std::string
& dir_name
,
90 const base::FilePath
& backing_filepath
);
92 ~TestDirectory() override
;
94 void StartFailingSaveChanges() {
95 backing_store_
->StartFailingSaveChanges();
99 TestDirectory(Encryptor
* encryptor
,
100 UnrecoverableErrorHandler
* handler
,
101 TestBackingStore
* backing_store
);
103 TestBackingStore
* backing_store_
;
106 TestDirectory
* TestDirectory::Create(
107 Encryptor
*encryptor
,
108 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(Encryptor
* encryptor
,
117 UnrecoverableErrorHandler
* handler
,
118 TestBackingStore
* backing_store
)
119 : Directory(backing_store
, handler
, NULL
, NULL
, NULL
),
120 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(
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 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);
158 void TearDown() override
{
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::DataTypeContext
BuildContext(ModelType type
) {
194 sync_pb::DataTypeContext context
;
195 context
.set_context("context");
196 context
.set_data_type_id(GetSpecificsFieldNumberFromModelType(type
));
200 TEST_F(OnDiskSyncableDirectoryTest
, TestPurgeEntriesWithTypeIn
) {
201 sync_pb::EntitySpecifics bookmark_specs
;
202 sync_pb::EntitySpecifics autofill_specs
;
203 sync_pb::EntitySpecifics preference_specs
;
204 AddDefaultFieldValue(BOOKMARKS
, &bookmark_specs
);
205 AddDefaultFieldValue(PREFERENCES
, &preference_specs
);
206 AddDefaultFieldValue(AUTOFILL
, &autofill_specs
);
208 ModelTypeSet
types_to_purge(PREFERENCES
, AUTOFILL
);
210 dir()->SetDownloadProgress(BOOKMARKS
, BuildProgress(BOOKMARKS
));
211 dir()->SetDownloadProgress(PREFERENCES
, BuildProgress(PREFERENCES
));
212 dir()->SetDownloadProgress(AUTOFILL
, BuildProgress(AUTOFILL
));
214 TestIdFactory id_factory
;
215 // Create some items for each type.
217 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
219 dir()->SetDataTypeContext(&trans
, BOOKMARKS
, BuildContext(BOOKMARKS
));
220 dir()->SetDataTypeContext(&trans
, PREFERENCES
, BuildContext(PREFERENCES
));
221 dir()->SetDataTypeContext(&trans
, AUTOFILL
, BuildContext(AUTOFILL
));
223 // Make it look like these types have completed initial sync.
224 CreateTypeRoot(&trans
, dir().get(), BOOKMARKS
);
225 CreateTypeRoot(&trans
, dir().get(), PREFERENCES
);
226 CreateTypeRoot(&trans
, dir().get(), AUTOFILL
);
228 // Add more nodes for this type. Technically, they should be placed under
229 // the proper type root nodes but the assertions in this test won't notice
230 // if their parent isn't quite right.
231 MutableEntry
item1(&trans
, CREATE
, BOOKMARKS
, trans
.root_id(), "Item");
232 ASSERT_TRUE(item1
.good());
233 item1
.PutServerSpecifics(bookmark_specs
);
234 item1
.PutIsUnsynced(true);
236 MutableEntry
item2(&trans
, CREATE_NEW_UPDATE_ITEM
,
237 id_factory
.NewServerId());
238 ASSERT_TRUE(item2
.good());
239 item2
.PutServerSpecifics(bookmark_specs
);
240 item2
.PutIsUnappliedUpdate(true);
242 MutableEntry
item3(&trans
, CREATE
, PREFERENCES
,
243 trans
.root_id(), "Item");
244 ASSERT_TRUE(item3
.good());
245 item3
.PutSpecifics(preference_specs
);
246 item3
.PutServerSpecifics(preference_specs
);
247 item3
.PutIsUnsynced(true);
249 MutableEntry
item4(&trans
, CREATE_NEW_UPDATE_ITEM
,
250 id_factory
.NewServerId());
251 ASSERT_TRUE(item4
.good());
252 item4
.PutServerSpecifics(preference_specs
);
253 item4
.PutIsUnappliedUpdate(true);
255 MutableEntry
item5(&trans
, CREATE
, AUTOFILL
,
256 trans
.root_id(), "Item");
257 ASSERT_TRUE(item5
.good());
258 item5
.PutSpecifics(autofill_specs
);
259 item5
.PutServerSpecifics(autofill_specs
);
260 item5
.PutIsUnsynced(true);
262 MutableEntry
item6(&trans
, CREATE_NEW_UPDATE_ITEM
,
263 id_factory
.NewServerId());
264 ASSERT_TRUE(item6
.good());
265 item6
.PutServerSpecifics(autofill_specs
);
266 item6
.PutIsUnappliedUpdate(true);
269 dir()->SaveChanges();
271 ReadTransaction
trans(FROM_HERE
, dir().get());
272 MetahandleSet all_set
;
273 GetAllMetaHandles(&trans
, &all_set
);
274 ASSERT_EQ(10U, all_set
.size());
277 dir()->PurgeEntriesWithTypeIn(types_to_purge
, ModelTypeSet(), ModelTypeSet());
279 // We first query the in-memory data, and then reload the directory (without
280 // saving) to verify that disk does not still have the data.
281 CheckPurgeEntriesWithTypeInSucceeded(types_to_purge
, true);
283 CheckPurgeEntriesWithTypeInSucceeded(types_to_purge
, false);
286 TEST_F(OnDiskSyncableDirectoryTest
, TestShareInfo
) {
287 dir()->set_store_birthday("Jan 31st");
288 const char* const bag_of_chips_array
= "\0bag of chips";
289 const std::string bag_of_chips_string
=
290 std::string(bag_of_chips_array
, sizeof(bag_of_chips_array
));
291 dir()->set_bag_of_chips(bag_of_chips_string
);
293 ReadTransaction
trans(FROM_HERE
, dir().get());
294 EXPECT_EQ("Jan 31st", dir()->store_birthday());
295 EXPECT_EQ(bag_of_chips_string
, dir()->bag_of_chips());
297 dir()->set_store_birthday("April 10th");
298 const char* const bag_of_chips2_array
= "\0bag of chips2";
299 const std::string bag_of_chips2_string
=
300 std::string(bag_of_chips2_array
, sizeof(bag_of_chips2_array
));
301 dir()->set_bag_of_chips(bag_of_chips2_string
);
302 dir()->SaveChanges();
304 ReadTransaction
trans(FROM_HERE
, dir().get());
305 EXPECT_EQ("April 10th", dir()->store_birthday());
306 EXPECT_EQ(bag_of_chips2_string
, dir()->bag_of_chips());
308 const char* const bag_of_chips3_array
= "\0bag of chips3";
309 const std::string bag_of_chips3_string
=
310 std::string(bag_of_chips3_array
, sizeof(bag_of_chips3_array
));
311 dir()->set_bag_of_chips(bag_of_chips3_string
);
312 // Restore the directory from disk. Make sure that nothing's changed.
315 ReadTransaction
trans(FROM_HERE
, dir().get());
316 EXPECT_EQ("April 10th", dir()->store_birthday());
317 EXPECT_EQ(bag_of_chips3_string
, dir()->bag_of_chips());
321 TEST_F(OnDiskSyncableDirectoryTest
,
322 TestSimpleFieldsPreservedDuringSaveChanges
) {
323 Id update_id
= TestIdFactory::FromNumber(1);
325 EntryKernel create_pre_save
, update_pre_save
;
326 EntryKernel create_post_save
, update_post_save
;
327 std::string create_name
= "Create";
330 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
332 &trans
, CREATE
, BOOKMARKS
, trans
.root_id(), create_name
);
333 MutableEntry
update(&trans
, CREATE_NEW_UPDATE_ITEM
, update_id
);
334 create
.PutIsUnsynced(true);
335 update
.PutIsUnappliedUpdate(true);
336 sync_pb::EntitySpecifics specifics
;
337 specifics
.mutable_bookmark()->set_favicon("PNG");
338 specifics
.mutable_bookmark()->set_url("http://nowhere");
339 create
.PutSpecifics(specifics
);
340 update
.PutServerSpecifics(specifics
);
341 create_pre_save
= create
.GetKernelCopy();
342 update_pre_save
= update
.GetKernelCopy();
343 create_id
= create
.GetId();
346 dir()->SaveChanges();
348 new Directory(new OnDiskDirectoryBackingStore(kDirectoryName
, file_path_
),
349 unrecoverable_error_handler(),
354 ASSERT_TRUE(dir().get());
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
) {
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);
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());
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
));
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
) {
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);
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
{
548 void SetUp() override
{ ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir()); }
550 void TearDown() override
{}
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
), &handler_
,
567 DirOpenResult result
=
568 dir
.Open("ScopeTest", &delegate_
, NullTransactionObserver());
569 ASSERT_EQ(result
, OPENED
);
572 // Destroying the directory should have released the backing database file.
573 ASSERT_TRUE(base::DeleteFile(path
, true));
576 class SyncableClientTagTest
: public SyncableDirectoryTest
{
578 static const int kBaseVersion
= 1;
579 const char* test_name_
;
580 const char* test_tag_
;
582 SyncableClientTagTest() : test_name_("test_name"), test_tag_("dietcoke") {}
584 bool CreateWithDefaultTag(Id id
, bool deleted
) {
585 WriteTransaction
wtrans(FROM_HERE
, UNITTEST
, dir().get());
586 MutableEntry
me(&wtrans
, CREATE
, PREFERENCES
,
587 wtrans
.root_id(), test_name_
);
590 if (id
.ServerKnows()) {
591 me
.PutBaseVersion(kBaseVersion
);
593 me
.PutIsUnsynced(true);
594 me
.PutIsDel(deleted
);
596 return me
.PutUniqueClientTag(test_tag_
);
599 // Verify an entry exists with the default tag.
600 void VerifyTag(Id id
, bool deleted
) {
601 // Should still be present and valid in the client tag index.
602 ReadTransaction
trans(FROM_HERE
, dir().get());
603 Entry
me(&trans
, GET_BY_CLIENT_TAG
, test_tag_
);
605 EXPECT_EQ(me
.GetId(), id
);
606 EXPECT_EQ(me
.GetUniqueClientTag(), test_tag_
);
607 EXPECT_EQ(me
.GetIsDel(), deleted
);
609 // We only sync deleted items that the server knew about.
610 if (me
.GetId().ServerKnows() || !me
.GetIsDel()) {
611 EXPECT_EQ(me
.GetIsUnsynced(), true);
616 TestIdFactory factory_
;
619 TEST_F(SyncableClientTagTest
, TestClientTagClear
) {
620 Id server_id
= factory_
.NewServerId();
621 EXPECT_TRUE(CreateWithDefaultTag(server_id
, false));
623 WriteTransaction
trans(FROM_HERE
, UNITTEST
, dir().get());
624 MutableEntry
me(&trans
, GET_BY_CLIENT_TAG
, test_tag_
);
625 EXPECT_TRUE(me
.good());
626 me
.PutUniqueClientTag(std::string());
629 ReadTransaction
trans(FROM_HERE
, dir().get());
630 Entry
by_tag(&trans
, GET_BY_CLIENT_TAG
, test_tag_
);
631 EXPECT_FALSE(by_tag
.good());
633 Entry
by_id(&trans
, GET_BY_ID
, server_id
);
634 EXPECT_TRUE(by_id
.good());
635 EXPECT_TRUE(by_id
.GetUniqueClientTag().empty());
639 TEST_F(SyncableClientTagTest
, TestClientTagIndexServerId
) {
640 Id server_id
= factory_
.NewServerId();
641 EXPECT_TRUE(CreateWithDefaultTag(server_id
, false));
642 VerifyTag(server_id
, false);
645 TEST_F(SyncableClientTagTest
, TestClientTagIndexClientId
) {
646 Id client_id
= factory_
.NewLocalId();
647 EXPECT_TRUE(CreateWithDefaultTag(client_id
, false));
648 VerifyTag(client_id
, false);
651 TEST_F(SyncableClientTagTest
, TestDeletedClientTagIndexClientId
) {
652 Id client_id
= factory_
.NewLocalId();
653 EXPECT_TRUE(CreateWithDefaultTag(client_id
, true));
654 VerifyTag(client_id
, true);
657 TEST_F(SyncableClientTagTest
, TestDeletedClientTagIndexServerId
) {
658 Id server_id
= factory_
.NewServerId();
659 EXPECT_TRUE(CreateWithDefaultTag(server_id
, true));
660 VerifyTag(server_id
, true);
663 TEST_F(SyncableClientTagTest
, TestClientTagIndexDuplicateServer
) {
664 EXPECT_TRUE(CreateWithDefaultTag(factory_
.NewServerId(), true));
665 EXPECT_FALSE(CreateWithDefaultTag(factory_
.NewServerId(), true));
666 EXPECT_FALSE(CreateWithDefaultTag(factory_
.NewServerId(), false));
667 EXPECT_FALSE(CreateWithDefaultTag(factory_
.NewLocalId(), false));
668 EXPECT_FALSE(CreateWithDefaultTag(factory_
.NewLocalId(), true));
671 } // namespace syncable
672 } // namespace syncer