Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: sync/syncable/syncable_unittest.cc

Issue 2130453004: [Sync] Move //sync to //components/sync. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sync/syncable/syncable_read_transaction.cc ('k') | sync/syncable/syncable_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include <stdint.h>
6
7 #include <memory>
8 #include <string>
9
10 #include "base/compiler_specific.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/location.h"
15 #include "base/logging.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/stl_util.h"
18 #include "base/synchronization/condition_variable.h"
19 #include "base/test/values_test_util.h"
20 #include "base/threading/platform_thread.h"
21 #include "base/values.h"
22 #include "build/build_config.h"
23 #include "sync/protocol/bookmark_specifics.pb.h"
24 #include "sync/syncable/directory_backing_store.h"
25 #include "sync/syncable/directory_change_delegate.h"
26 #include "sync/syncable/directory_unittest.h"
27 #include "sync/syncable/in_memory_directory_backing_store.h"
28 #include "sync/syncable/metahandle_set.h"
29 #include "sync/syncable/mutable_entry.h"
30 #include "sync/syncable/on_disk_directory_backing_store.h"
31 #include "sync/syncable/syncable_proto_util.h"
32 #include "sync/syncable/syncable_read_transaction.h"
33 #include "sync/syncable/syncable_util.h"
34 #include "sync/syncable/syncable_write_transaction.h"
35 #include "sync/test/engine/test_id_factory.h"
36 #include "sync/test/engine/test_syncable_utils.h"
37 #include "sync/test/fake_encryptor.h"
38 #include "sync/test/null_directory_change_delegate.h"
39 #include "sync/test/null_transaction_observer.h"
40 #include "sync/util/test_unrecoverable_error_handler.h"
41 #include "testing/gtest/include/gtest/gtest.h"
42
43 namespace syncer {
44 namespace syncable {
45
46 using base::ExpectDictBooleanValue;
47 using base::ExpectDictStringValue;
48
49 // An OnDiskDirectoryBackingStore that can be set to always fail SaveChanges.
50 class TestBackingStore : public OnDiskDirectoryBackingStore {
51 public:
52 TestBackingStore(const std::string& dir_name,
53 const base::FilePath& backing_filepath);
54
55 ~TestBackingStore() override;
56
57 bool SaveChanges(const Directory::SaveChangesSnapshot& snapshot) override;
58
59 void StartFailingSaveChanges() { fail_save_changes_ = true; }
60
61 private:
62 bool fail_save_changes_;
63 };
64
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) {
69 }
70
71 TestBackingStore::~TestBackingStore() { }
72
73 bool TestBackingStore::SaveChanges(
74 const Directory::SaveChangesSnapshot& snapshot) {
75 if (fail_save_changes_) {
76 return false;
77 } else {
78 return OnDiskDirectoryBackingStore::SaveChanges(snapshot);
79 }
80 }
81
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);
91
92 ~TestDirectory() override;
93
94 void StartFailingSaveChanges() {
95 backing_store_->StartFailingSaveChanges();
96 }
97
98 private:
99 TestDirectory(Encryptor* encryptor,
100 const WeakHandle<UnrecoverableErrorHandler>& handler,
101 TestBackingStore* backing_store);
102
103 TestBackingStore* backing_store_;
104 };
105
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);
114 }
115
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) {}
122
123 TestDirectory::~TestDirectory() { }
124
125 // crbug.com/144422
126 #if defined(OS_ANDROID)
127 #define MAYBE_FailInitialWrite DISABLED_FailInitialWrite
128 #else
129 #define MAYBE_FailInitialWrite FailInitialWrite
130 #endif
131 TEST(OnDiskSyncableDirectory, MAYBE_FailInitialWrite) {
132 base::MessageLoop message_loop;
133 FakeEncryptor encryptor;
134 TestUnrecoverableErrorHandler handler;
135 base::ScopedTempDir temp_dir;
136 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
137 base::FilePath file_path = temp_dir.path().Append(
138 FILE_PATH_LITERAL("Test.sqlite3"));
139 std::string name = "user@x.com";
140 NullDirectoryChangeDelegate delegate;
141
142 std::unique_ptr<TestDirectory> test_dir(TestDirectory::Create(
143 &encryptor, MakeWeakHandle(handler.GetWeakPtr()), name, file_path));
144
145 test_dir->StartFailingSaveChanges();
146 ASSERT_EQ(FAILED_INITIAL_WRITE, test_dir->Open(name, &delegate,
147 NullTransactionObserver()));
148 }
149
150 // A variant of SyncableDirectoryTest that uses a real sqlite database.
151 class OnDiskSyncableDirectoryTest : public SyncableDirectoryTest {
152 protected:
153 // SetUp() is called before each test case is run.
154 // The sqlite3 DB is deleted before each test is run.
155 void SetUp() override {
156 SyncableDirectoryTest::SetUp();
157 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
158 file_path_ = temp_dir_.path().Append(
159 FILE_PATH_LITERAL("Test.sqlite3"));
160 base::DeleteFile(file_path_, false);
161 CreateDirectory();
162 }
163
164 void TearDown() override {
165 // This also closes file handles.
166 dir()->SaveChanges();
167 dir().reset();
168 base::DeleteFile(file_path_, false);
169 SyncableDirectoryTest::TearDown();
170 }
171
172 // Creates a new directory. Deletes the old directory, if it exists.
173 void CreateDirectory() {
174 test_directory_ = TestDirectory::Create(
175 encryptor(),
176 MakeWeakHandle(unrecoverable_error_handler()->GetWeakPtr()),
177 kDirectoryName, file_path_);
178 dir().reset(test_directory_);
179 ASSERT_TRUE(dir().get());
180 ASSERT_EQ(OPENED,
181 dir()->Open(kDirectoryName,
182 directory_change_delegate(),
183 NullTransactionObserver()));
184 ASSERT_TRUE(dir()->good());
185 }
186
187 void SaveAndReloadDir() {
188 dir()->SaveChanges();
189 CreateDirectory();
190 }
191
192 void StartFailingSaveChanges() {
193 test_directory_->StartFailingSaveChanges();
194 }
195
196 TestDirectory* test_directory_; // mirrors std::unique_ptr<Directory> dir_
197 base::ScopedTempDir temp_dir_;
198 base::FilePath file_path_;
199 };
200
201 sync_pb::DataTypeContext BuildContext(ModelType type) {
202 sync_pb::DataTypeContext context;
203 context.set_context("context");
204 context.set_data_type_id(GetSpecificsFieldNumberFromModelType(type));
205 return context;
206 }
207
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);
215
216 ModelTypeSet types_to_purge(PREFERENCES, AUTOFILL);
217
218 dir()->SetDownloadProgress(BOOKMARKS, BuildProgress(BOOKMARKS));
219 dir()->SetDownloadProgress(PREFERENCES, BuildProgress(PREFERENCES));
220 dir()->SetDownloadProgress(AUTOFILL, BuildProgress(AUTOFILL));
221
222 TestIdFactory id_factory;
223 // Create some items for each type.
224 {
225 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
226
227 dir()->SetDataTypeContext(&trans, BOOKMARKS, BuildContext(BOOKMARKS));
228 dir()->SetDataTypeContext(&trans, PREFERENCES, BuildContext(PREFERENCES));
229 dir()->SetDataTypeContext(&trans, AUTOFILL, BuildContext(AUTOFILL));
230
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);
235
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);
243
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);
249
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);
256
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);
262
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);
269
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);
275 }
276
277 dir()->SaveChanges();
278 {
279 ReadTransaction trans(FROM_HERE, dir().get());
280 MetahandleSet all_set;
281 GetAllMetaHandles(&trans, &all_set);
282 ASSERT_EQ(10U, all_set.size());
283 }
284
285 dir()->PurgeEntriesWithTypeIn(types_to_purge, ModelTypeSet(), ModelTypeSet());
286
287 // We first query the in-memory data, and then reload the directory (without
288 // saving) to verify that disk does not still have the data.
289 CheckPurgeEntriesWithTypeInSucceeded(types_to_purge, true);
290 SaveAndReloadDir();
291 CheckPurgeEntriesWithTypeInSucceeded(types_to_purge, false);
292 }
293
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);
300 {
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());
304 }
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();
311 {
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());
315 }
316 const char* const bag_of_chips3_array = "\0bag of chips3";
317 const std::string bag_of_chips3_string =
318 std::string(bag_of_chips3_array, sizeof(bag_of_chips3_array));
319 dir()->set_bag_of_chips(bag_of_chips3_string);
320 // Restore the directory from disk. Make sure that nothing's changed.
321 SaveAndReloadDir();
322 {
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());
326 }
327 }
328
329 TEST_F(OnDiskSyncableDirectoryTest,
330 TestSimpleFieldsPreservedDuringSaveChanges) {
331 Id update_id = TestIdFactory::FromNumber(1);
332 Id create_id;
333 EntryKernel create_pre_save, update_pre_save;
334 EntryKernel create_post_save, update_post_save;
335 std::string create_name = "Create";
336
337 {
338 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
339 MutableEntry create(
340 &trans, CREATE, BOOKMARKS, trans.root_id(), create_name);
341 MutableEntry update(&trans, CREATE_NEW_UPDATE_ITEM, update_id);
342 create.PutIsUnsynced(true);
343 update.PutIsUnappliedUpdate(true);
344 sync_pb::EntitySpecifics specifics;
345 specifics.mutable_bookmark()->set_favicon("PNG");
346 specifics.mutable_bookmark()->set_url("http://nowhere");
347 create.PutSpecifics(specifics);
348 update.PutServerSpecifics(specifics);
349 create_pre_save = create.GetKernelCopy();
350 update_pre_save = update.GetKernelCopy();
351 create_id = create.GetId();
352 }
353
354 dir()->SaveChanges();
355 dir().reset(
356 new Directory(new OnDiskDirectoryBackingStore(kDirectoryName, file_path_),
357 MakeWeakHandle(unrecoverable_error_handler()->GetWeakPtr()),
358 base::Closure(), NULL, NULL));
359
360 ASSERT_TRUE(dir().get());
361 ASSERT_EQ(OPENED,
362 dir()->Open(kDirectoryName,
363 directory_change_delegate(),
364 NullTransactionObserver()));
365 ASSERT_TRUE(dir()->good());
366
367 {
368 ReadTransaction trans(FROM_HERE, dir().get());
369 Entry create(&trans, GET_BY_ID, create_id);
370 EXPECT_EQ(1, CountEntriesWithName(&trans, trans.root_id(), create_name));
371 Entry update(&trans, GET_BY_ID, update_id);
372 create_post_save = create.GetKernelCopy();
373 update_post_save = update.GetKernelCopy();
374 }
375 int i = BEGIN_FIELDS;
376 for ( ; i < INT64_FIELDS_END ; ++i) {
377 EXPECT_EQ(
378 create_pre_save.ref((Int64Field)i) + (i == TRANSACTION_VERSION ? 1 : 0),
379 create_post_save.ref((Int64Field)i))
380 << "int64_t field #" << i << " changed during save/load";
381 EXPECT_EQ(update_pre_save.ref((Int64Field)i),
382 update_post_save.ref((Int64Field)i))
383 << "int64_t field #" << i << " changed during save/load";
384 }
385 for ( ; i < TIME_FIELDS_END ; ++i) {
386 EXPECT_EQ(create_pre_save.ref((TimeField)i),
387 create_post_save.ref((TimeField)i))
388 << "time field #" << i << " changed during save/load";
389 EXPECT_EQ(update_pre_save.ref((TimeField)i),
390 update_post_save.ref((TimeField)i))
391 << "time field #" << i << " changed during save/load";
392 }
393 for ( ; i < ID_FIELDS_END ; ++i) {
394 EXPECT_EQ(create_pre_save.ref((IdField)i),
395 create_post_save.ref((IdField)i))
396 << "id field #" << i << " changed during save/load";
397 EXPECT_EQ(update_pre_save.ref((IdField)i),
398 update_pre_save.ref((IdField)i))
399 << "id field #" << i << " changed during save/load";
400 }
401 for ( ; i < BIT_FIELDS_END ; ++i) {
402 EXPECT_EQ(create_pre_save.ref((BitField)i),
403 create_post_save.ref((BitField)i))
404 << "Bit field #" << i << " changed during save/load";
405 EXPECT_EQ(update_pre_save.ref((BitField)i),
406 update_post_save.ref((BitField)i))
407 << "Bit field #" << i << " changed during save/load";
408 }
409 for ( ; i < STRING_FIELDS_END ; ++i) {
410 EXPECT_EQ(create_pre_save.ref((StringField)i),
411 create_post_save.ref((StringField)i))
412 << "String field #" << i << " changed during save/load";
413 EXPECT_EQ(update_pre_save.ref((StringField)i),
414 update_post_save.ref((StringField)i))
415 << "String field #" << i << " changed during save/load";
416 }
417 for ( ; i < PROTO_FIELDS_END; ++i) {
418 EXPECT_EQ(create_pre_save.ref((ProtoField)i).SerializeAsString(),
419 create_post_save.ref((ProtoField)i).SerializeAsString())
420 << "Blob field #" << i << " changed during save/load";
421 EXPECT_EQ(update_pre_save.ref((ProtoField)i).SerializeAsString(),
422 update_post_save.ref((ProtoField)i).SerializeAsString())
423 << "Blob field #" << i << " changed during save/load";
424 }
425 for ( ; i < UNIQUE_POSITION_FIELDS_END; ++i) {
426 EXPECT_TRUE(create_pre_save.ref((UniquePositionField)i).Equals(
427 create_post_save.ref((UniquePositionField)i)))
428 << "Position field #" << i << " changed during save/load";
429 EXPECT_TRUE(update_pre_save.ref((UniquePositionField)i).Equals(
430 update_post_save.ref((UniquePositionField)i)))
431 << "Position field #" << i << " changed during save/load";
432 }
433 }
434
435 TEST_F(OnDiskSyncableDirectoryTest, TestSaveChangesFailure) {
436 int64_t handle1 = 0;
437 // Set up an item using a regular, saveable directory.
438 {
439 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
440
441 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), "aguilera");
442 ASSERT_TRUE(e1.good());
443 EXPECT_TRUE(e1.GetKernelCopy().is_dirty());
444 handle1 = e1.GetMetahandle();
445 e1.PutBaseVersion(1);
446 e1.PutIsDir(true);
447 e1.PutId(TestIdFactory::FromNumber(101));
448 EXPECT_TRUE(e1.GetKernelCopy().is_dirty());
449 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
450 }
451 ASSERT_TRUE(dir()->SaveChanges());
452
453 // Make sure the item is no longer dirty after saving,
454 // and make a modification.
455 {
456 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
457
458 MutableEntry aguilera(&trans, GET_BY_HANDLE, handle1);
459 ASSERT_TRUE(aguilera.good());
460 EXPECT_FALSE(aguilera.GetKernelCopy().is_dirty());
461 EXPECT_EQ(aguilera.GetNonUniqueName(), "aguilera");
462 aguilera.PutNonUniqueName("overwritten");
463 EXPECT_TRUE(aguilera.GetKernelCopy().is_dirty());
464 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
465 }
466 ASSERT_TRUE(dir()->SaveChanges());
467
468 // Now do some operations when SaveChanges() will fail.
469 StartFailingSaveChanges();
470 ASSERT_TRUE(dir()->good());
471
472 int64_t handle2 = 0;
473 {
474 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
475
476 MutableEntry aguilera(&trans, GET_BY_HANDLE, handle1);
477 ASSERT_TRUE(aguilera.good());
478 EXPECT_FALSE(aguilera.GetKernelCopy().is_dirty());
479 EXPECT_EQ(aguilera.GetNonUniqueName(), "overwritten");
480 EXPECT_FALSE(aguilera.GetKernelCopy().is_dirty());
481 EXPECT_FALSE(IsInDirtyMetahandles(handle1));
482 aguilera.PutNonUniqueName("christina");
483 EXPECT_TRUE(aguilera.GetKernelCopy().is_dirty());
484 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
485
486 // New item.
487 MutableEntry kids_on_block(
488 &trans, CREATE, BOOKMARKS, trans.root_id(), "kids");
489 ASSERT_TRUE(kids_on_block.good());
490 handle2 = kids_on_block.GetMetahandle();
491 kids_on_block.PutBaseVersion(1);
492 kids_on_block.PutIsDir(true);
493 kids_on_block.PutId(TestIdFactory::FromNumber(102));
494 EXPECT_TRUE(kids_on_block.GetKernelCopy().is_dirty());
495 EXPECT_TRUE(IsInDirtyMetahandles(handle2));
496 }
497
498 // We are using an unsaveable directory, so this can't succeed. However,
499 // the HandleSaveChangesFailure code path should have been triggered.
500 ASSERT_FALSE(dir()->SaveChanges());
501
502 // Make sure things were rolled back and the world is as it was before call.
503 {
504 ReadTransaction trans(FROM_HERE, dir().get());
505 Entry e1(&trans, GET_BY_HANDLE, handle1);
506 ASSERT_TRUE(e1.good());
507 EntryKernel aguilera = e1.GetKernelCopy();
508 Entry kids(&trans, GET_BY_HANDLE, handle2);
509 ASSERT_TRUE(kids.good());
510 EXPECT_TRUE(kids.GetKernelCopy().is_dirty());
511 EXPECT_TRUE(IsInDirtyMetahandles(handle2));
512 EXPECT_TRUE(aguilera.is_dirty());
513 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
514 }
515 }
516
517 TEST_F(OnDiskSyncableDirectoryTest, TestSaveChangesFailureWithPurge) {
518 int64_t handle1 = 0;
519 // Set up an item and progress marker using a regular, saveable directory.
520 dir()->SetDownloadProgress(BOOKMARKS, BuildProgress(BOOKMARKS));
521 {
522 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
523
524 MutableEntry e1(&trans, CREATE, BOOKMARKS, trans.root_id(), "aguilera");
525 ASSERT_TRUE(e1.good());
526 EXPECT_TRUE(e1.GetKernelCopy().is_dirty());
527 handle1 = e1.GetMetahandle();
528 e1.PutBaseVersion(1);
529 e1.PutIsDir(true);
530 e1.PutId(TestIdFactory::FromNumber(101));
531 sync_pb::EntitySpecifics bookmark_specs;
532 AddDefaultFieldValue(BOOKMARKS, &bookmark_specs);
533 e1.PutSpecifics(bookmark_specs);
534 e1.PutServerSpecifics(bookmark_specs);
535 e1.PutId(TestIdFactory::FromNumber(101));
536 EXPECT_TRUE(e1.GetKernelCopy().is_dirty());
537 EXPECT_TRUE(IsInDirtyMetahandles(handle1));
538 }
539 ASSERT_TRUE(dir()->SaveChanges());
540
541 // Now do some operations while SaveChanges() is set to fail.
542 StartFailingSaveChanges();
543 ASSERT_TRUE(dir()->good());
544
545 ModelTypeSet set(BOOKMARKS);
546 dir()->PurgeEntriesWithTypeIn(set, ModelTypeSet(), ModelTypeSet());
547 EXPECT_TRUE(IsInMetahandlesToPurge(handle1));
548 ASSERT_FALSE(dir()->SaveChanges());
549 EXPECT_TRUE(IsInMetahandlesToPurge(handle1));
550 }
551
552 class SyncableDirectoryManagement : public testing::Test {
553 public:
554 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
555
556 void TearDown() override {}
557
558 protected:
559 base::MessageLoop message_loop_;
560 base::ScopedTempDir temp_dir_;
561 FakeEncryptor encryptor_;
562 TestUnrecoverableErrorHandler handler_;
563 NullDirectoryChangeDelegate delegate_;
564 };
565
566 TEST_F(SyncableDirectoryManagement, TestFileRelease) {
567 base::FilePath path =
568 temp_dir_.path().Append(Directory::kSyncDatabaseFilename);
569
570 {
571 Directory dir(new OnDiskDirectoryBackingStore("ScopeTest", path),
572 MakeWeakHandle(handler_.GetWeakPtr()), base::Closure(), NULL,
573 NULL);
574 DirOpenResult result =
575 dir.Open("ScopeTest", &delegate_, NullTransactionObserver());
576 ASSERT_EQ(result, OPENED);
577 }
578
579 // Destroying the directory should have released the backing database file.
580 ASSERT_TRUE(base::DeleteFile(path, true));
581 }
582
583 class SyncableClientTagTest : public SyncableDirectoryTest {
584 public:
585 static const int kBaseVersion = 1;
586 const char* test_name_;
587 const char* test_tag_;
588
589 SyncableClientTagTest() : test_name_("test_name"), test_tag_("dietcoke") {}
590
591 bool CreateWithDefaultTag(Id id, bool deleted) {
592 WriteTransaction wtrans(FROM_HERE, UNITTEST, dir().get());
593 MutableEntry me(&wtrans, CREATE, PREFERENCES,
594 wtrans.root_id(), test_name_);
595 CHECK(me.good());
596 me.PutId(id);
597 if (id.ServerKnows()) {
598 me.PutBaseVersion(kBaseVersion);
599 }
600 me.PutIsUnsynced(true);
601 me.PutIsDel(deleted);
602 me.PutIsDir(false);
603 return me.PutUniqueClientTag(test_tag_);
604 }
605
606 // Verify an entry exists with the default tag.
607 void VerifyTag(Id id, bool deleted) {
608 // Should still be present and valid in the client tag index.
609 ReadTransaction trans(FROM_HERE, dir().get());
610 Entry me(&trans, GET_BY_CLIENT_TAG, test_tag_);
611 CHECK(me.good());
612 EXPECT_EQ(me.GetId(), id);
613 EXPECT_EQ(me.GetUniqueClientTag(), test_tag_);
614 EXPECT_EQ(me.GetIsDel(), deleted);
615
616 // We only sync deleted items that the server knew about.
617 if (me.GetId().ServerKnows() || !me.GetIsDel()) {
618 EXPECT_EQ(me.GetIsUnsynced(), true);
619 }
620 }
621
622 protected:
623 TestIdFactory factory_;
624 };
625
626 TEST_F(SyncableClientTagTest, TestClientTagClear) {
627 Id server_id = factory_.NewServerId();
628 EXPECT_TRUE(CreateWithDefaultTag(server_id, false));
629 {
630 WriteTransaction trans(FROM_HERE, UNITTEST, dir().get());
631 MutableEntry me(&trans, GET_BY_CLIENT_TAG, test_tag_);
632 EXPECT_TRUE(me.good());
633 me.PutUniqueClientTag(std::string());
634 }
635 {
636 ReadTransaction trans(FROM_HERE, dir().get());
637 Entry by_tag(&trans, GET_BY_CLIENT_TAG, test_tag_);
638 EXPECT_FALSE(by_tag.good());
639
640 Entry by_id(&trans, GET_BY_ID, server_id);
641 EXPECT_TRUE(by_id.good());
642 EXPECT_TRUE(by_id.GetUniqueClientTag().empty());
643 }
644 }
645
646 TEST_F(SyncableClientTagTest, TestClientTagIndexServerId) {
647 Id server_id = factory_.NewServerId();
648 EXPECT_TRUE(CreateWithDefaultTag(server_id, false));
649 VerifyTag(server_id, false);
650 }
651
652 TEST_F(SyncableClientTagTest, TestClientTagIndexClientId) {
653 Id client_id = factory_.NewLocalId();
654 EXPECT_TRUE(CreateWithDefaultTag(client_id, false));
655 VerifyTag(client_id, false);
656 }
657
658 TEST_F(SyncableClientTagTest, TestDeletedClientTagIndexClientId) {
659 Id client_id = factory_.NewLocalId();
660 EXPECT_TRUE(CreateWithDefaultTag(client_id, true));
661 VerifyTag(client_id, true);
662 }
663
664 TEST_F(SyncableClientTagTest, TestDeletedClientTagIndexServerId) {
665 Id server_id = factory_.NewServerId();
666 EXPECT_TRUE(CreateWithDefaultTag(server_id, true));
667 VerifyTag(server_id, true);
668 }
669
670 TEST_F(SyncableClientTagTest, TestClientTagIndexDuplicateServer) {
671 EXPECT_TRUE(CreateWithDefaultTag(factory_.NewServerId(), true));
672 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), true));
673 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewServerId(), false));
674 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), false));
675 EXPECT_FALSE(CreateWithDefaultTag(factory_.NewLocalId(), true));
676 }
677
678 } // namespace syncable
679 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/syncable/syncable_read_transaction.cc ('k') | sync/syncable/syncable_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698