OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/sync/syncable/directory_backing_store.h" | 5 #include "components/sync/syncable/directory_backing_store.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 } | 69 } |
70 | 70 |
71 base::FilePath GetDatabasePath() { | 71 base::FilePath GetDatabasePath() { |
72 return temp_dir_.path().Append(Directory::kSyncDatabaseFilename); | 72 return temp_dir_.path().Append(Directory::kSyncDatabaseFilename); |
73 } | 73 } |
74 | 74 |
75 static bool LoadAndIgnoreReturnedData(DirectoryBackingStore *dbs) { | 75 static bool LoadAndIgnoreReturnedData(DirectoryBackingStore *dbs) { |
76 Directory::MetahandlesMap tmp_handles_map; | 76 Directory::MetahandlesMap tmp_handles_map; |
77 JournalIndex delete_journals; | 77 JournalIndex delete_journals; |
78 MetahandleSet metahandles_to_purge; | 78 MetahandleSet metahandles_to_purge; |
79 STLValueDeleter<Directory::MetahandlesMap> deleter(&tmp_handles_map); | 79 base::STLValueDeleter<Directory::MetahandlesMap> deleter(&tmp_handles_map); |
80 Directory::KernelLoadInfo kernel_load_info; | 80 Directory::KernelLoadInfo kernel_load_info; |
81 return dbs->Load(&tmp_handles_map, &delete_journals, &metahandles_to_purge, | 81 return dbs->Load(&tmp_handles_map, &delete_journals, &metahandles_to_purge, |
82 &kernel_load_info) == OPENED; | 82 &kernel_load_info) == OPENED; |
83 } | 83 } |
84 | 84 |
85 void SetUpVersion67Database(sql::Connection* connection); | 85 void SetUpVersion67Database(sql::Connection* connection); |
86 void SetUpVersion68Database(sql::Connection* connection); | 86 void SetUpVersion68Database(sql::Connection* connection); |
87 void SetUpVersion69Database(sql::Connection* connection); | 87 void SetUpVersion69Database(sql::Connection* connection); |
88 void SetUpVersion70Database(sql::Connection* connection); | 88 void SetUpVersion70Database(sql::Connection* connection); |
89 void SetUpVersion71Database(sql::Connection* connection); | 89 void SetUpVersion71Database(sql::Connection* connection); |
(...skipping 3233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3323 new TestDirectoryBackingStore(GetUsername(), &connection)); | 3323 new TestDirectoryBackingStore(GetUsername(), &connection)); |
3324 ASSERT_FALSE(dbs->needs_column_refresh()); | 3324 ASSERT_FALSE(dbs->needs_column_refresh()); |
3325 ASSERT_TRUE(dbs->MigrateVersion79To80()); | 3325 ASSERT_TRUE(dbs->MigrateVersion79To80()); |
3326 ASSERT_EQ(80, dbs->GetVersion()); | 3326 ASSERT_EQ(80, dbs->GetVersion()); |
3327 ASSERT_FALSE(dbs->needs_column_refresh()); | 3327 ASSERT_FALSE(dbs->needs_column_refresh()); |
3328 | 3328 |
3329 // Ensure the bag_of_chips has been set. | 3329 // Ensure the bag_of_chips has been set. |
3330 Directory::MetahandlesMap handles_map; | 3330 Directory::MetahandlesMap handles_map; |
3331 JournalIndex delete_journals; | 3331 JournalIndex delete_journals; |
3332 MetahandleSet metahandles_to_purge; | 3332 MetahandleSet metahandles_to_purge; |
3333 STLValueDeleter<Directory::MetahandlesMap> deleter(&handles_map); | 3333 base::STLValueDeleter<Directory::MetahandlesMap> deleter(&handles_map); |
3334 Directory::KernelLoadInfo load_info; | 3334 Directory::KernelLoadInfo load_info; |
3335 | 3335 |
3336 ASSERT_TRUE(dbs->Load(&handles_map, &delete_journals, &metahandles_to_purge, | 3336 ASSERT_TRUE(dbs->Load(&handles_map, &delete_journals, &metahandles_to_purge, |
3337 &load_info)); | 3337 &load_info)); |
3338 // Check that the initial value is the serialization of an empty ChipBag. | 3338 // Check that the initial value is the serialization of an empty ChipBag. |
3339 sync_pb::ChipBag chip_bag; | 3339 sync_pb::ChipBag chip_bag; |
3340 std::string serialized_chip_bag; | 3340 std::string serialized_chip_bag; |
3341 ASSERT_TRUE(chip_bag.SerializeToString(&serialized_chip_bag)); | 3341 ASSERT_TRUE(chip_bag.SerializeToString(&serialized_chip_bag)); |
3342 EXPECT_EQ(serialized_chip_bag, load_info.kernel_info.bag_of_chips); | 3342 EXPECT_EQ(serialized_chip_bag, load_info.kernel_info.bag_of_chips); |
3343 } | 3343 } |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3562 "INSERT INTO metas " | 3562 "INSERT INTO metas " |
3563 "( id, metahandle, is_dir, ctime, mtime," | 3563 "( id, metahandle, is_dir, ctime, mtime," |
3564 " unique_position, server_unique_position) " | 3564 " unique_position, server_unique_position) " |
3565 "VALUES('c-invalid', 9999, 1, 0, 0, 'BAD_POS', 'BAD_POS')")); | 3565 "VALUES('c-invalid', 9999, 1, 0, 0, 'BAD_POS', 'BAD_POS')")); |
3566 ASSERT_TRUE(s.Run()); | 3566 ASSERT_TRUE(s.Run()); |
3567 | 3567 |
3568 // Trying to unpack this entry should signal that the DB is corrupted. | 3568 // Trying to unpack this entry should signal that the DB is corrupted. |
3569 Directory::MetahandlesMap handles_map; | 3569 Directory::MetahandlesMap handles_map; |
3570 JournalIndex delete_journals; | 3570 JournalIndex delete_journals; |
3571 MetahandleSet metahandles_to_purge; | 3571 MetahandleSet metahandles_to_purge; |
3572 STLValueDeleter<Directory::MetahandlesMap> deleter(&handles_map); | 3572 base::STLValueDeleter<Directory::MetahandlesMap> deleter(&handles_map); |
3573 Directory::KernelLoadInfo kernel_load_info; | 3573 Directory::KernelLoadInfo kernel_load_info; |
3574 ASSERT_EQ(FAILED_DATABASE_CORRUPT, | 3574 ASSERT_EQ(FAILED_DATABASE_CORRUPT, |
3575 dbs->Load(&handles_map, &delete_journals, &metahandles_to_purge, | 3575 dbs->Load(&handles_map, &delete_journals, &metahandles_to_purge, |
3576 &kernel_load_info)); | 3576 &kernel_load_info)); |
3577 } | 3577 } |
3578 | 3578 |
3579 TEST_P(MigrationTest, ToCurrentVersion) { | 3579 TEST_P(MigrationTest, ToCurrentVersion) { |
3580 sql::Connection connection; | 3580 sql::Connection connection; |
3581 ASSERT_TRUE(connection.Open(GetDatabasePath())); | 3581 ASSERT_TRUE(connection.Open(GetDatabasePath())); |
3582 // Assume all old versions have an old page size. | 3582 // Assume all old versions have an old page size. |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3661 // SetUpVersionYDatabase function with a dump of the test database | 3661 // SetUpVersionYDatabase function with a dump of the test database |
3662 // at the new schema. See the MigrateToLatestAndDump test case. | 3662 // at the new schema. See the MigrateToLatestAndDump test case. |
3663 FAIL() << "Need to supply database dump for version " << GetParam(); | 3663 FAIL() << "Need to supply database dump for version " << GetParam(); |
3664 } | 3664 } |
3665 connection.Close(); | 3665 connection.Close(); |
3666 | 3666 |
3667 syncable::Directory::KernelLoadInfo dir_info; | 3667 syncable::Directory::KernelLoadInfo dir_info; |
3668 Directory::MetahandlesMap handles_map; | 3668 Directory::MetahandlesMap handles_map; |
3669 JournalIndex delete_journals; | 3669 JournalIndex delete_journals; |
3670 MetahandleSet metahandles_to_purge; | 3670 MetahandleSet metahandles_to_purge; |
3671 STLValueDeleter<Directory::MetahandlesMap> index_deleter(&handles_map); | 3671 base::STLValueDeleter<Directory::MetahandlesMap> index_deleter(&handles_map); |
3672 | 3672 |
3673 { | 3673 { |
3674 std::unique_ptr<OnDiskDirectoryBackingStore> dbs( | 3674 std::unique_ptr<OnDiskDirectoryBackingStore> dbs( |
3675 new OnDiskDirectoryBackingStore(GetUsername(), GetDatabasePath())); | 3675 new OnDiskDirectoryBackingStore(GetUsername(), GetDatabasePath())); |
3676 ASSERT_EQ(OPENED, dbs->Load(&handles_map, &delete_journals, | 3676 ASSERT_EQ(OPENED, dbs->Load(&handles_map, &delete_journals, |
3677 &metahandles_to_purge, &dir_info)); | 3677 &metahandles_to_purge, &dir_info)); |
3678 if (!metahandles_to_purge.empty()) | 3678 if (!metahandles_to_purge.empty()) |
3679 dbs->DeleteEntries(DirectoryBackingStore::METAS_TABLE, | 3679 dbs->DeleteEntries(DirectoryBackingStore::METAS_TABLE, |
3680 metahandles_to_purge); | 3680 metahandles_to_purge); |
3681 ASSERT_FALSE(dbs->needs_column_refresh()); | 3681 ASSERT_FALSE(dbs->needs_column_refresh()); |
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4079 sql::Connection connection; | 4079 sql::Connection connection; |
4080 ASSERT_TRUE(connection.OpenInMemory()); | 4080 ASSERT_TRUE(connection.OpenInMemory()); |
4081 | 4081 |
4082 SetUpCurrentDatabaseAndCheckVersion(&connection); | 4082 SetUpCurrentDatabaseAndCheckVersion(&connection); |
4083 std::unique_ptr<TestDirectoryBackingStore> dbs( | 4083 std::unique_ptr<TestDirectoryBackingStore> dbs( |
4084 new TestDirectoryBackingStore(GetUsername(), &connection)); | 4084 new TestDirectoryBackingStore(GetUsername(), &connection)); |
4085 Directory::MetahandlesMap handles_map; | 4085 Directory::MetahandlesMap handles_map; |
4086 JournalIndex delete_journals; | 4086 JournalIndex delete_journals; |
4087 MetahandleSet metahandles_to_purge; | 4087 MetahandleSet metahandles_to_purge; |
4088 Directory::KernelLoadInfo kernel_load_info; | 4088 Directory::KernelLoadInfo kernel_load_info; |
4089 STLValueDeleter<Directory::MetahandlesMap> index_deleter(&handles_map); | 4089 base::STLValueDeleter<Directory::MetahandlesMap> index_deleter(&handles_map); |
4090 | 4090 |
4091 dbs->Load(&handles_map, &delete_journals, &metahandles_to_purge, | 4091 dbs->Load(&handles_map, &delete_journals, &metahandles_to_purge, |
4092 &kernel_load_info); | 4092 &kernel_load_info); |
4093 size_t initial_size = handles_map.size(); | 4093 size_t initial_size = handles_map.size(); |
4094 ASSERT_LT(0U, initial_size) << "Test requires handles_map to delete."; | 4094 ASSERT_LT(0U, initial_size) << "Test requires handles_map to delete."; |
4095 int64_t first_to_die = handles_map.begin()->second->ref(META_HANDLE); | 4095 int64_t first_to_die = handles_map.begin()->second->ref(META_HANDLE); |
4096 MetahandleSet to_delete; | 4096 MetahandleSet to_delete; |
4097 to_delete.insert(first_to_die); | 4097 to_delete.insert(first_to_die); |
4098 EXPECT_TRUE(dbs->DeleteEntries(to_delete)); | 4098 EXPECT_TRUE(dbs->DeleteEntries(to_delete)); |
4099 | 4099 |
4100 STLDeleteValues(&handles_map); | 4100 base::STLDeleteValues(&handles_map); |
4101 metahandles_to_purge.clear(); | 4101 metahandles_to_purge.clear(); |
4102 dbs->LoadEntries(&handles_map, &metahandles_to_purge); | 4102 dbs->LoadEntries(&handles_map, &metahandles_to_purge); |
4103 | 4103 |
4104 EXPECT_EQ(initial_size - 1, handles_map.size()); | 4104 EXPECT_EQ(initial_size - 1, handles_map.size()); |
4105 bool delete_failed = false; | 4105 bool delete_failed = false; |
4106 for (Directory::MetahandlesMap::iterator it = handles_map.begin(); | 4106 for (Directory::MetahandlesMap::iterator it = handles_map.begin(); |
4107 it != handles_map.end(); ++it) { | 4107 it != handles_map.end(); ++it) { |
4108 if (it->first == first_to_die) { | 4108 if (it->first == first_to_die) { |
4109 delete_failed = true; | 4109 delete_failed = true; |
4110 break; | 4110 break; |
4111 } | 4111 } |
4112 } | 4112 } |
4113 EXPECT_FALSE(delete_failed); | 4113 EXPECT_FALSE(delete_failed); |
4114 | 4114 |
4115 to_delete.clear(); | 4115 to_delete.clear(); |
4116 for (Directory::MetahandlesMap::iterator it = handles_map.begin(); | 4116 for (Directory::MetahandlesMap::iterator it = handles_map.begin(); |
4117 it != handles_map.end(); ++it) { | 4117 it != handles_map.end(); ++it) { |
4118 to_delete.insert(it->first); | 4118 to_delete.insert(it->first); |
4119 } | 4119 } |
4120 | 4120 |
4121 EXPECT_TRUE(dbs->DeleteEntries(to_delete)); | 4121 EXPECT_TRUE(dbs->DeleteEntries(to_delete)); |
4122 | 4122 |
4123 STLDeleteValues(&handles_map); | 4123 base::STLDeleteValues(&handles_map); |
4124 metahandles_to_purge.clear(); | 4124 metahandles_to_purge.clear(); |
4125 dbs->LoadEntries(&handles_map, &metahandles_to_purge); | 4125 dbs->LoadEntries(&handles_map, &metahandles_to_purge); |
4126 EXPECT_EQ(0U, handles_map.size()); | 4126 EXPECT_EQ(0U, handles_map.size()); |
4127 } | 4127 } |
4128 | 4128 |
4129 TEST_F(DirectoryBackingStoreTest, GenerateCacheGUID) { | 4129 TEST_F(DirectoryBackingStoreTest, GenerateCacheGUID) { |
4130 const std::string& guid1 = TestDirectoryBackingStore::GenerateCacheGUID(); | 4130 const std::string& guid1 = TestDirectoryBackingStore::GenerateCacheGUID(); |
4131 const std::string& guid2 = TestDirectoryBackingStore::GenerateCacheGUID(); | 4131 const std::string& guid2 = TestDirectoryBackingStore::GenerateCacheGUID(); |
4132 EXPECT_EQ(24U, guid1.size()); | 4132 EXPECT_EQ(24U, guid1.size()); |
4133 EXPECT_EQ(24U, guid2.size()); | 4133 EXPECT_EQ(24U, guid2.size()); |
4134 // In theory this test can fail, but it won't before the universe | 4134 // In theory this test can fail, but it won't before the universe |
4135 // dies of heat death. | 4135 // dies of heat death. |
4136 EXPECT_NE(guid1, guid2); | 4136 EXPECT_NE(guid1, guid2); |
4137 } | 4137 } |
4138 | 4138 |
4139 TEST_F(DirectoryBackingStoreTest, IncreaseDatabasePageSizeFrom4KTo32K) { | 4139 TEST_F(DirectoryBackingStoreTest, IncreaseDatabasePageSizeFrom4KTo32K) { |
4140 sql::Connection connection; | 4140 sql::Connection connection; |
4141 ASSERT_TRUE(connection.Open(GetDatabasePath())); | 4141 ASSERT_TRUE(connection.Open(GetDatabasePath())); |
4142 | 4142 |
4143 SetUpCurrentDatabaseAndCheckVersion(&connection); | 4143 SetUpCurrentDatabaseAndCheckVersion(&connection); |
4144 std::unique_ptr<TestDirectoryBackingStore> dbs( | 4144 std::unique_ptr<TestDirectoryBackingStore> dbs( |
4145 new TestDirectoryBackingStore(GetUsername(), &connection)); | 4145 new TestDirectoryBackingStore(GetUsername(), &connection)); |
4146 Directory::MetahandlesMap handles_map; | 4146 Directory::MetahandlesMap handles_map; |
4147 JournalIndex delete_journals; | 4147 JournalIndex delete_journals; |
4148 MetahandleSet metahandles_to_purge; | 4148 MetahandleSet metahandles_to_purge; |
4149 Directory::KernelLoadInfo kernel_load_info; | 4149 Directory::KernelLoadInfo kernel_load_info; |
4150 STLValueDeleter<Directory::MetahandlesMap> index_deleter(&handles_map); | 4150 base::STLValueDeleter<Directory::MetahandlesMap> index_deleter(&handles_map); |
4151 | 4151 |
4152 DirOpenResult open_result = dbs->Load( | 4152 DirOpenResult open_result = dbs->Load( |
4153 &handles_map, &delete_journals, &metahandles_to_purge, &kernel_load_info); | 4153 &handles_map, &delete_journals, &metahandles_to_purge, &kernel_load_info); |
4154 EXPECT_EQ(open_result, OPENED); | 4154 EXPECT_EQ(open_result, OPENED); |
4155 | 4155 |
4156 // Set up database's page size to 4096 | 4156 // Set up database's page size to 4096 |
4157 EXPECT_TRUE(dbs->db_->Execute("PRAGMA page_size=4096;")); | 4157 EXPECT_TRUE(dbs->db_->Execute("PRAGMA page_size=4096;")); |
4158 EXPECT_TRUE(dbs->Vacuum()); | 4158 EXPECT_TRUE(dbs->Vacuum()); |
4159 | 4159 |
4160 // Check if update is successful. | 4160 // Check if update is successful. |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4264 ASSERT_FALSE(dbs->SaveChanges(snapshot)); | 4264 ASSERT_FALSE(dbs->SaveChanges(snapshot)); |
4265 // At this point the handler has been posted but not executed. | 4265 // At this point the handler has been posted but not executed. |
4266 ASSERT_FALSE(was_called); | 4266 ASSERT_FALSE(was_called); |
4267 // Pump the message loop and see that it is executed. | 4267 // Pump the message loop and see that it is executed. |
4268 base::RunLoop().RunUntilIdle(); | 4268 base::RunLoop().RunUntilIdle(); |
4269 ASSERT_TRUE(was_called); | 4269 ASSERT_TRUE(was_called); |
4270 } | 4270 } |
4271 | 4271 |
4272 } // namespace syncable | 4272 } // namespace syncable |
4273 } // namespace syncer | 4273 } // namespace syncer |
OLD | NEW |