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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 return "nick@chromium.org"; | 39 return "nick@chromium.org"; |
40 } | 40 } |
41 | 41 |
42 base::FilePath GetDatabasePath() { | 42 base::FilePath GetDatabasePath() { |
43 return temp_dir_.path().Append(Directory::kSyncDatabaseFilename); | 43 return temp_dir_.path().Append(Directory::kSyncDatabaseFilename); |
44 } | 44 } |
45 | 45 |
46 static bool LoadAndIgnoreReturnedData(DirectoryBackingStore *dbs) { | 46 static bool LoadAndIgnoreReturnedData(DirectoryBackingStore *dbs) { |
47 Directory::MetahandlesMap tmp_handles_map; | 47 Directory::MetahandlesMap tmp_handles_map; |
48 JournalIndex delete_journals; | 48 JournalIndex delete_journals; |
| 49 MetahandleSet metahandles_to_purge; |
49 STLValueDeleter<Directory::MetahandlesMap> deleter(&tmp_handles_map); | 50 STLValueDeleter<Directory::MetahandlesMap> deleter(&tmp_handles_map); |
50 Directory::KernelLoadInfo kernel_load_info; | 51 Directory::KernelLoadInfo kernel_load_info; |
51 return dbs->Load(&tmp_handles_map, &delete_journals, &kernel_load_info) == | 52 return dbs->Load(&tmp_handles_map, &delete_journals, &metahandles_to_purge, |
52 OPENED; | 53 &kernel_load_info) == OPENED; |
53 } | 54 } |
54 | 55 |
55 void SetUpVersion67Database(sql::Connection* connection); | 56 void SetUpVersion67Database(sql::Connection* connection); |
56 void SetUpVersion68Database(sql::Connection* connection); | 57 void SetUpVersion68Database(sql::Connection* connection); |
57 void SetUpVersion69Database(sql::Connection* connection); | 58 void SetUpVersion69Database(sql::Connection* connection); |
58 void SetUpVersion70Database(sql::Connection* connection); | 59 void SetUpVersion70Database(sql::Connection* connection); |
59 void SetUpVersion71Database(sql::Connection* connection); | 60 void SetUpVersion71Database(sql::Connection* connection); |
60 void SetUpVersion72Database(sql::Connection* connection); | 61 void SetUpVersion72Database(sql::Connection* connection); |
61 void SetUpVersion73Database(sql::Connection* connection); | 62 void SetUpVersion73Database(sql::Connection* connection); |
62 void SetUpVersion74Database(sql::Connection* connection); | 63 void SetUpVersion74Database(sql::Connection* connection); |
(...skipping 3132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3195 | 3196 |
3196 scoped_ptr<TestDirectoryBackingStore> dbs( | 3197 scoped_ptr<TestDirectoryBackingStore> dbs( |
3197 new TestDirectoryBackingStore(GetUsername(), &connection)); | 3198 new TestDirectoryBackingStore(GetUsername(), &connection)); |
3198 ASSERT_FALSE(dbs->needs_column_refresh_); | 3199 ASSERT_FALSE(dbs->needs_column_refresh_); |
3199 ASSERT_TRUE(dbs->MigrateVersion78To79()); | 3200 ASSERT_TRUE(dbs->MigrateVersion78To79()); |
3200 ASSERT_EQ(79, dbs->GetVersion()); | 3201 ASSERT_EQ(79, dbs->GetVersion()); |
3201 ASSERT_FALSE(dbs->needs_column_refresh_); | 3202 ASSERT_FALSE(dbs->needs_column_refresh_); |
3202 | 3203 |
3203 // Ensure the next_id has been incremented. | 3204 // Ensure the next_id has been incremented. |
3204 Directory::MetahandlesMap handles_map; | 3205 Directory::MetahandlesMap handles_map; |
3205 JournalIndex delete_journals;; | 3206 JournalIndex delete_journals; |
| 3207 MetahandleSet metahandles_to_purge; |
3206 STLValueDeleter<Directory::MetahandlesMap> deleter(&handles_map); | 3208 STLValueDeleter<Directory::MetahandlesMap> deleter(&handles_map); |
3207 Directory::KernelLoadInfo load_info; | 3209 Directory::KernelLoadInfo load_info; |
3208 | 3210 |
3209 s.Clear(); | 3211 s.Clear(); |
3210 ASSERT_TRUE(dbs->Load(&handles_map, &delete_journals, &load_info)); | 3212 ASSERT_TRUE(dbs->Load(&handles_map, &delete_journals, &metahandles_to_purge, |
| 3213 &load_info)); |
3211 EXPECT_LE(load_info.kernel_info.next_id, kInitialNextId - 65536); | 3214 EXPECT_LE(load_info.kernel_info.next_id, kInitialNextId - 65536); |
3212 } | 3215 } |
3213 | 3216 |
3214 TEST_F(DirectoryBackingStoreTest, MigrateVersion79To80) { | 3217 TEST_F(DirectoryBackingStoreTest, MigrateVersion79To80) { |
3215 sql::Connection connection; | 3218 sql::Connection connection; |
3216 ASSERT_TRUE(connection.OpenInMemory()); | 3219 ASSERT_TRUE(connection.OpenInMemory()); |
3217 SetUpVersion79Database(&connection); | 3220 SetUpVersion79Database(&connection); |
3218 | 3221 |
3219 scoped_ptr<TestDirectoryBackingStore> dbs( | 3222 scoped_ptr<TestDirectoryBackingStore> dbs( |
3220 new TestDirectoryBackingStore(GetUsername(), &connection)); | 3223 new TestDirectoryBackingStore(GetUsername(), &connection)); |
3221 ASSERT_FALSE(dbs->needs_column_refresh_); | 3224 ASSERT_FALSE(dbs->needs_column_refresh_); |
3222 ASSERT_TRUE(dbs->MigrateVersion79To80()); | 3225 ASSERT_TRUE(dbs->MigrateVersion79To80()); |
3223 ASSERT_EQ(80, dbs->GetVersion()); | 3226 ASSERT_EQ(80, dbs->GetVersion()); |
3224 ASSERT_FALSE(dbs->needs_column_refresh_); | 3227 ASSERT_FALSE(dbs->needs_column_refresh_); |
3225 | 3228 |
3226 // Ensure the bag_of_chips has been set. | 3229 // Ensure the bag_of_chips has been set. |
3227 Directory::MetahandlesMap handles_map; | 3230 Directory::MetahandlesMap handles_map; |
3228 JournalIndex delete_journals;; | 3231 JournalIndex delete_journals; |
| 3232 MetahandleSet metahandles_to_purge; |
3229 STLValueDeleter<Directory::MetahandlesMap> deleter(&handles_map); | 3233 STLValueDeleter<Directory::MetahandlesMap> deleter(&handles_map); |
3230 Directory::KernelLoadInfo load_info; | 3234 Directory::KernelLoadInfo load_info; |
3231 | 3235 |
3232 ASSERT_TRUE(dbs->Load(&handles_map, &delete_journals, &load_info)); | 3236 ASSERT_TRUE(dbs->Load(&handles_map, &delete_journals, &metahandles_to_purge, |
| 3237 &load_info)); |
3233 // Check that the initial value is the serialization of an empty ChipBag. | 3238 // Check that the initial value is the serialization of an empty ChipBag. |
3234 sync_pb::ChipBag chip_bag; | 3239 sync_pb::ChipBag chip_bag; |
3235 std::string serialized_chip_bag; | 3240 std::string serialized_chip_bag; |
3236 ASSERT_TRUE(chip_bag.SerializeToString(&serialized_chip_bag)); | 3241 ASSERT_TRUE(chip_bag.SerializeToString(&serialized_chip_bag)); |
3237 EXPECT_EQ(serialized_chip_bag, load_info.kernel_info.bag_of_chips); | 3242 EXPECT_EQ(serialized_chip_bag, load_info.kernel_info.bag_of_chips); |
3238 } | 3243 } |
3239 | 3244 |
3240 TEST_F(DirectoryBackingStoreTest, MigrateVersion80To81) { | 3245 TEST_F(DirectoryBackingStoreTest, MigrateVersion80To81) { |
3241 sql::Connection connection; | 3246 sql::Connection connection; |
3242 ASSERT_TRUE(connection.OpenInMemory()); | 3247 ASSERT_TRUE(connection.OpenInMemory()); |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3431 // Insert row with bad position. | 3436 // Insert row with bad position. |
3432 sql::Statement s(connection.GetUniqueStatement( | 3437 sql::Statement s(connection.GetUniqueStatement( |
3433 "INSERT INTO metas " | 3438 "INSERT INTO metas " |
3434 "( id, metahandle, is_dir, ctime, mtime," | 3439 "( id, metahandle, is_dir, ctime, mtime," |
3435 " unique_position, server_unique_position) " | 3440 " unique_position, server_unique_position) " |
3436 "VALUES('c-invalid', 9999, 1, 0, 0, 'BAD_POS', 'BAD_POS')")); | 3441 "VALUES('c-invalid', 9999, 1, 0, 0, 'BAD_POS', 'BAD_POS')")); |
3437 ASSERT_TRUE(s.Run()); | 3442 ASSERT_TRUE(s.Run()); |
3438 | 3443 |
3439 // Trying to unpack this entry should signal that the DB is corrupted. | 3444 // Trying to unpack this entry should signal that the DB is corrupted. |
3440 Directory::MetahandlesMap handles_map; | 3445 Directory::MetahandlesMap handles_map; |
3441 JournalIndex delete_journals;; | 3446 JournalIndex delete_journals; |
| 3447 MetahandleSet metahandles_to_purge; |
3442 STLValueDeleter<Directory::MetahandlesMap> deleter(&handles_map); | 3448 STLValueDeleter<Directory::MetahandlesMap> deleter(&handles_map); |
3443 Directory::KernelLoadInfo kernel_load_info; | 3449 Directory::KernelLoadInfo kernel_load_info; |
3444 ASSERT_EQ(FAILED_DATABASE_CORRUPT, | 3450 ASSERT_EQ(FAILED_DATABASE_CORRUPT, |
3445 dbs->Load(&handles_map, &delete_journals, &kernel_load_info)); | 3451 dbs->Load(&handles_map, &delete_journals, &metahandles_to_purge, |
| 3452 &kernel_load_info)); |
3446 } | 3453 } |
3447 | 3454 |
3448 TEST_P(MigrationTest, ToCurrentVersion) { | 3455 TEST_P(MigrationTest, ToCurrentVersion) { |
3449 sql::Connection connection; | 3456 sql::Connection connection; |
3450 ASSERT_TRUE(connection.OpenInMemory()); | 3457 ASSERT_TRUE(connection.OpenInMemory()); |
3451 switch (GetParam()) { | 3458 switch (GetParam()) { |
3452 case 67: | 3459 case 67: |
3453 SetUpVersion67Database(&connection); | 3460 SetUpVersion67Database(&connection); |
3454 break; | 3461 break; |
3455 case 68: | 3462 case 68: |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3522 // If you see this error, it may mean that you've increased the | 3529 // If you see this error, it may mean that you've increased the |
3523 // database version number but you haven't finished adding unit tests | 3530 // database version number but you haven't finished adding unit tests |
3524 // for the database migration code. You need to need to supply a | 3531 // for the database migration code. You need to need to supply a |
3525 // SetUpVersionYDatabase function with a dump of the test database | 3532 // SetUpVersionYDatabase function with a dump of the test database |
3526 // at the new schema. See the MigrateToLatestAndDump test case. | 3533 // at the new schema. See the MigrateToLatestAndDump test case. |
3527 FAIL() << "Need to supply database dump for version " << GetParam(); | 3534 FAIL() << "Need to supply database dump for version " << GetParam(); |
3528 } | 3535 } |
3529 | 3536 |
3530 syncable::Directory::KernelLoadInfo dir_info; | 3537 syncable::Directory::KernelLoadInfo dir_info; |
3531 Directory::MetahandlesMap handles_map; | 3538 Directory::MetahandlesMap handles_map; |
3532 JournalIndex delete_journals;; | 3539 JournalIndex delete_journals; |
| 3540 MetahandleSet metahandles_to_purge; |
3533 STLValueDeleter<Directory::MetahandlesMap> index_deleter(&handles_map); | 3541 STLValueDeleter<Directory::MetahandlesMap> index_deleter(&handles_map); |
3534 | 3542 |
3535 { | 3543 { |
3536 scoped_ptr<TestDirectoryBackingStore> dbs( | 3544 scoped_ptr<TestDirectoryBackingStore> dbs( |
3537 new TestDirectoryBackingStore(GetUsername(), &connection)); | 3545 new TestDirectoryBackingStore(GetUsername(), &connection)); |
3538 ASSERT_EQ(OPENED, dbs->Load(&handles_map, &delete_journals, &dir_info)); | 3546 ASSERT_EQ(OPENED, dbs->Load(&handles_map, &delete_journals, |
| 3547 &metahandles_to_purge, &dir_info)); |
| 3548 if (!metahandles_to_purge.empty()) |
| 3549 dbs->DeleteEntries(metahandles_to_purge); |
3539 ASSERT_FALSE(dbs->needs_column_refresh_); | 3550 ASSERT_FALSE(dbs->needs_column_refresh_); |
3540 ASSERT_EQ(kCurrentDBVersion, dbs->GetVersion()); | 3551 ASSERT_EQ(kCurrentDBVersion, dbs->GetVersion()); |
3541 } | 3552 } |
3542 | 3553 |
3543 // Columns deleted in Version 67. | 3554 // Columns deleted in Version 67. |
3544 ASSERT_FALSE(connection.DoesColumnExist("metas", "name")); | 3555 ASSERT_FALSE(connection.DoesColumnExist("metas", "name")); |
3545 ASSERT_FALSE(connection.DoesColumnExist("metas", "unsanitized_name")); | 3556 ASSERT_FALSE(connection.DoesColumnExist("metas", "unsanitized_name")); |
3546 ASSERT_FALSE(connection.DoesColumnExist("metas", "server_name")); | 3557 ASSERT_FALSE(connection.DoesColumnExist("metas", "server_name")); |
3547 | 3558 |
3548 // Columns added in Version 68. | 3559 // Columns added in Version 68. |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3896 | 3907 |
3897 TEST_F(DirectoryBackingStoreTest, DeleteEntries) { | 3908 TEST_F(DirectoryBackingStoreTest, DeleteEntries) { |
3898 sql::Connection connection; | 3909 sql::Connection connection; |
3899 ASSERT_TRUE(connection.OpenInMemory()); | 3910 ASSERT_TRUE(connection.OpenInMemory()); |
3900 | 3911 |
3901 SetUpCurrentDatabaseAndCheckVersion(&connection); | 3912 SetUpCurrentDatabaseAndCheckVersion(&connection); |
3902 scoped_ptr<TestDirectoryBackingStore> dbs( | 3913 scoped_ptr<TestDirectoryBackingStore> dbs( |
3903 new TestDirectoryBackingStore(GetUsername(), &connection)); | 3914 new TestDirectoryBackingStore(GetUsername(), &connection)); |
3904 Directory::MetahandlesMap handles_map; | 3915 Directory::MetahandlesMap handles_map; |
3905 JournalIndex delete_journals; | 3916 JournalIndex delete_journals; |
| 3917 MetahandleSet metahandles_to_purge; |
3906 Directory::KernelLoadInfo kernel_load_info; | 3918 Directory::KernelLoadInfo kernel_load_info; |
3907 STLValueDeleter<Directory::MetahandlesMap> index_deleter(&handles_map); | 3919 STLValueDeleter<Directory::MetahandlesMap> index_deleter(&handles_map); |
3908 | 3920 |
3909 dbs->Load(&handles_map, &delete_journals, &kernel_load_info); | 3921 dbs->Load(&handles_map, &delete_journals, &metahandles_to_purge, |
| 3922 &kernel_load_info); |
3910 size_t initial_size = handles_map.size(); | 3923 size_t initial_size = handles_map.size(); |
3911 ASSERT_LT(0U, initial_size) << "Test requires handles_map to delete."; | 3924 ASSERT_LT(0U, initial_size) << "Test requires handles_map to delete."; |
3912 int64 first_to_die = handles_map.begin()->second->ref(META_HANDLE); | 3925 int64 first_to_die = handles_map.begin()->second->ref(META_HANDLE); |
3913 MetahandleSet to_delete; | 3926 MetahandleSet to_delete; |
3914 to_delete.insert(first_to_die); | 3927 to_delete.insert(first_to_die); |
3915 EXPECT_TRUE(dbs->DeleteEntries(TestDirectoryBackingStore::METAS_TABLE, | 3928 EXPECT_TRUE(dbs->DeleteEntries(to_delete)); |
3916 to_delete)); | |
3917 | 3929 |
3918 STLDeleteValues(&handles_map); | 3930 STLDeleteValues(&handles_map); |
3919 dbs->LoadEntries(&handles_map); | 3931 metahandles_to_purge.clear(); |
| 3932 dbs->LoadEntries(&handles_map, &metahandles_to_purge); |
3920 | 3933 |
3921 EXPECT_EQ(initial_size - 1, handles_map.size()); | 3934 EXPECT_EQ(initial_size - 1, handles_map.size()); |
3922 bool delete_failed = false; | 3935 bool delete_failed = false; |
3923 for (Directory::MetahandlesMap::iterator it = handles_map.begin(); | 3936 for (Directory::MetahandlesMap::iterator it = handles_map.begin(); |
3924 it != handles_map.end(); ++it) { | 3937 it != handles_map.end(); ++it) { |
3925 if (it->first == first_to_die) { | 3938 if (it->first == first_to_die) { |
3926 delete_failed = true; | 3939 delete_failed = true; |
3927 break; | 3940 break; |
3928 } | 3941 } |
3929 } | 3942 } |
3930 EXPECT_FALSE(delete_failed); | 3943 EXPECT_FALSE(delete_failed); |
3931 | 3944 |
3932 to_delete.clear(); | 3945 to_delete.clear(); |
3933 for (Directory::MetahandlesMap::iterator it = handles_map.begin(); | 3946 for (Directory::MetahandlesMap::iterator it = handles_map.begin(); |
3934 it != handles_map.end(); ++it) { | 3947 it != handles_map.end(); ++it) { |
3935 to_delete.insert(it->first); | 3948 to_delete.insert(it->first); |
3936 } | 3949 } |
3937 | 3950 |
3938 EXPECT_TRUE(dbs->DeleteEntries(TestDirectoryBackingStore::METAS_TABLE, | 3951 EXPECT_TRUE(dbs->DeleteEntries(to_delete)); |
3939 to_delete)); | |
3940 | 3952 |
3941 STLDeleteValues(&handles_map); | 3953 STLDeleteValues(&handles_map); |
3942 dbs->LoadEntries(&handles_map); | 3954 metahandles_to_purge.clear(); |
| 3955 dbs->LoadEntries(&handles_map, &metahandles_to_purge); |
3943 EXPECT_EQ(0U, handles_map.size()); | 3956 EXPECT_EQ(0U, handles_map.size()); |
3944 } | 3957 } |
3945 | 3958 |
3946 TEST_F(DirectoryBackingStoreTest, GenerateCacheGUID) { | 3959 TEST_F(DirectoryBackingStoreTest, GenerateCacheGUID) { |
3947 const std::string& guid1 = TestDirectoryBackingStore::GenerateCacheGUID(); | 3960 const std::string& guid1 = TestDirectoryBackingStore::GenerateCacheGUID(); |
3948 const std::string& guid2 = TestDirectoryBackingStore::GenerateCacheGUID(); | 3961 const std::string& guid2 = TestDirectoryBackingStore::GenerateCacheGUID(); |
3949 EXPECT_EQ(24U, guid1.size()); | 3962 EXPECT_EQ(24U, guid1.size()); |
3950 EXPECT_EQ(24U, guid2.size()); | 3963 EXPECT_EQ(24U, guid2.size()); |
3951 // In theory this test can fail, but it won't before the universe | 3964 // In theory this test can fail, but it won't before the universe |
3952 // dies of heat death. | 3965 // dies of heat death. |
3953 EXPECT_NE(guid1, guid2); | 3966 EXPECT_NE(guid1, guid2); |
3954 } | 3967 } |
3955 | 3968 |
3956 } // namespace syncable | 3969 } // namespace syncable |
3957 } // namespace syncer | 3970 } // namespace syncer |
OLD | NEW |