| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <limits> | 5 #include <limits> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "chrome/browser/sync/syncable/directory_manager.h" | 10 #include "chrome/browser/sync/syncable/directory_manager.h" |
| 11 #include "chrome/browser/sync/util/character_set_converters.h" | 11 #include "chrome/browser/sync/util/character_set_converters.h" |
| 12 #include "chrome/browser/sync/util/user_settings.h" | 12 #include "chrome/browser/sync/util/user_settings.h" |
| 13 #include "chrome/common/sqlite_utils.h" | 13 #include "chrome/common/sqlite_utils.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 using browser_sync::APEncode; | 16 using browser_sync::APEncode; |
| 17 using browser_sync::APDecode; | 17 using browser_sync::APDecode; |
| 18 using browser_sync::ExecOrDie; | 18 using browser_sync::ExecOrDie; |
| 19 using browser_sync::FilePathToUTF8; | 19 using browser_sync::FilePathToUTF8; |
| 20 using browser_sync::UserSettings; | 20 using browser_sync::UserSettings; |
| 21 | 21 |
| 22 using std::numeric_limits; | 22 using std::numeric_limits; |
| 23 | 23 |
| 24 static const FilePath::CharType kV10UserSettingsDB[] = | 24 static const FilePath::CharType kV10UserSettingsDB[] = |
| 25 FILE_PATH_LITERAL("Version10Settings.sqlite3"); | 25 FILE_PATH_LITERAL("Version10Settings.sqlite3"); |
| 26 static const FilePath::CharType kV11UserSettingsDB[] = |
| 27 FILE_PATH_LITERAL("Version11Settings.sqlite3"); |
| 26 static const FilePath::CharType kOldStyleSyncDataDB[] = | 28 static const FilePath::CharType kOldStyleSyncDataDB[] = |
| 27 FILE_PATH_LITERAL("OldStyleSyncData.sqlite3"); | 29 FILE_PATH_LITERAL("OldStyleSyncData.sqlite3"); |
| 28 | 30 |
| 29 class UserSettingsTest : public testing::Test { | 31 class UserSettingsTest : public testing::Test { |
| 30 public: | 32 public: |
| 31 UserSettingsTest() : sync_data_("Some sync data") { } | 33 UserSettingsTest() : sync_data_("Some sync data") { } |
| 32 | 34 |
| 33 // Creates and populates the V10 database files within | 35 // Creates and populates the V10 database files within |
| 34 // |destination_directory|. | 36 // |destination_directory|. |
| 35 void SetUpVersion10Databases(const FilePath& destination_directory) { | 37 void SetUpVersion10Databases(const FilePath& destination_directory) { |
| 36 sqlite3* primer_handle = NULL; | 38 sqlite3* primer_handle = NULL; |
| 37 v10_user_setting_db_path_ = | 39 v10_user_setting_db_path_ = |
| 38 destination_directory.Append(FilePath(kV10UserSettingsDB)); | 40 destination_directory.Append(FilePath(kV10UserSettingsDB)); |
| 39 ASSERT_EQ(SQLITE_OK, OpenSqliteDb(v10_user_setting_db_path_, &primer_handle)
); | 41 ASSERT_EQ(SQLITE_OK, OpenSqliteDb(v10_user_setting_db_path_, |
| 42 &primer_handle)); |
| 40 old_style_sync_data_path_ = | 43 old_style_sync_data_path_ = |
| 41 destination_directory.Append(FilePath(kOldStyleSyncDataDB)); | 44 destination_directory.Append(FilePath(kOldStyleSyncDataDB)); |
| 42 | 45 |
| 43 ASSERT_EQ(sync_data_.length(), | 46 ASSERT_EQ(sync_data_.length(), |
| 44 static_cast<size_t>(file_util::WriteFile( | 47 static_cast<size_t>(file_util::WriteFile( |
| 45 old_style_sync_data_path_, sync_data_.data(), | 48 old_style_sync_data_path_, sync_data_.data(), |
| 46 sync_data_.length()))); | 49 sync_data_.length()))); |
| 47 | 50 |
| 48 // Create settings table. | 51 // Create settings table. |
| 49 ExecOrDie(primer_handle, "CREATE TABLE settings" | 52 ExecOrDie(primer_handle, "CREATE TABLE settings" |
| 50 " (email, key, value, " | 53 " (email, key, value, " |
| 51 " PRIMARY KEY(email, key) ON CONFLICT REPLACE)"); | 54 " PRIMARY KEY(email, key) ON CONFLICT REPLACE)"); |
| 52 | 55 // Add a blank signin table. |
| 56 ExecOrDie(primer_handle, "CREATE TABLE signin_types" |
| 57 " (signin, signin_type)"); |
| 53 // Create and populate version table. | 58 // Create and populate version table. |
| 54 ExecOrDie(primer_handle, "CREATE TABLE db_version ( version )"); | 59 ExecOrDie(primer_handle, "CREATE TABLE db_version ( version )"); |
| 55 { | 60 { |
| 56 SQLStatement statement; | 61 SQLStatement statement; |
| 57 const char query[] = "INSERT INTO db_version values ( ? )"; | 62 const char query[] = "INSERT INTO db_version values ( ? )"; |
| 58 statement.prepare(primer_handle, query); | 63 statement.prepare(primer_handle, query); |
| 59 statement.bind_int(0, 10); | 64 statement.bind_int(0, 10); |
| 60 if (SQLITE_DONE != statement.step()) { | 65 if (SQLITE_DONE != statement.step()) { |
| 61 LOG(FATAL) << query << "\n" << sqlite3_errmsg(primer_handle); | 66 LOG(FATAL) << query << "\n" << sqlite3_errmsg(primer_handle); |
| 62 } | 67 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 73 statement.bind_string(0, "foo@foo.com"); | 78 statement.bind_string(0, "foo@foo.com"); |
| 74 statement.bind_string(1, "foo@foo.com"); | 79 statement.bind_string(1, "foo@foo.com"); |
| 75 statement.bind_string(2, FilePathToUTF8(old_style_sync_data_path_)); | 80 statement.bind_string(2, FilePathToUTF8(old_style_sync_data_path_)); |
| 76 if (SQLITE_DONE != statement.step()) { | 81 if (SQLITE_DONE != statement.step()) { |
| 77 LOG(FATAL) << query << "\n" << sqlite3_errmsg(primer_handle); | 82 LOG(FATAL) << query << "\n" << sqlite3_errmsg(primer_handle); |
| 78 } | 83 } |
| 79 } | 84 } |
| 80 sqlite3_close(primer_handle); | 85 sqlite3_close(primer_handle); |
| 81 } | 86 } |
| 82 | 87 |
| 88 // Creates and populates the V11 database file within |
| 89 // |destination_directory|. |
| 90 void SetUpVersion11Database(const FilePath& destination_directory) { |
| 91 sqlite3* primer_handle = NULL; |
| 92 v11_user_setting_db_path_ = |
| 93 destination_directory.Append(FilePath(kV11UserSettingsDB)); |
| 94 ASSERT_EQ(SQLITE_OK, OpenSqliteDb(v11_user_setting_db_path_, |
| 95 &primer_handle)); |
| 96 |
| 97 // Create settings table. |
| 98 ExecOrDie(primer_handle, "CREATE TABLE settings" |
| 99 " (email, key, value, " |
| 100 " PRIMARY KEY(email, key) ON CONFLICT REPLACE)"); |
| 101 |
| 102 // Create and populate version table. |
| 103 ExecOrDie(primer_handle, "CREATE TABLE db_version ( version )"); |
| 104 { |
| 105 SQLStatement statement; |
| 106 const char query[] = "INSERT INTO db_version values ( ? )"; |
| 107 statement.prepare(primer_handle, query); |
| 108 statement.bind_int(0, 11); |
| 109 if (SQLITE_DONE != statement.step()) { |
| 110 LOG(FATAL) << query << "\n" << sqlite3_errmsg(primer_handle); |
| 111 } |
| 112 } |
| 113 |
| 114 ExecOrDie(primer_handle, "CREATE TABLE signin_types" |
| 115 " (signin, signin_type)"); |
| 116 |
| 117 { |
| 118 SQLStatement statement; |
| 119 const char query[] = "INSERT INTO signin_types values ( ?, ? )"; |
| 120 statement.prepare(primer_handle, query); |
| 121 statement.bind_string(0, "test"); |
| 122 statement.bind_string(1, "test"); |
| 123 if (SQLITE_DONE != statement.step()) { |
| 124 LOG(FATAL) << query << "\n" << sqlite3_errmsg(primer_handle); |
| 125 } |
| 126 } |
| 127 |
| 128 sqlite3_close(primer_handle); |
| 129 } |
| 130 |
| 83 const std::string& sync_data() const { return sync_data_; } | 131 const std::string& sync_data() const { return sync_data_; } |
| 84 const FilePath& v10_user_setting_db_path() const { | 132 const FilePath& v10_user_setting_db_path() const { |
| 85 return v10_user_setting_db_path_; | 133 return v10_user_setting_db_path_; |
| 86 } | 134 } |
| 135 const FilePath& v11_user_setting_db_path() const { |
| 136 return v11_user_setting_db_path_; |
| 137 } |
| 87 const FilePath& old_style_sync_data_path() const { | 138 const FilePath& old_style_sync_data_path() const { |
| 88 return old_style_sync_data_path_; | 139 return old_style_sync_data_path_; |
| 89 } | 140 } |
| 90 | 141 |
| 91 private: | 142 private: |
| 92 FilePath v10_user_setting_db_path_; | 143 FilePath v10_user_setting_db_path_; |
| 93 FilePath old_style_sync_data_path_; | 144 FilePath old_style_sync_data_path_; |
| 145 |
| 146 FilePath v11_user_setting_db_path_; |
| 147 |
| 94 std::string sync_data_; | 148 std::string sync_data_; |
| 95 }; | 149 }; |
| 96 | 150 |
| 97 TEST_F(UserSettingsTest, MigrateFromV10ToV11) { | 151 TEST_F(UserSettingsTest, MigrateFromV10ToV11) { |
| 98 ScopedTempDir temp_dir; | 152 ScopedTempDir temp_dir; |
| 99 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 153 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 100 SetUpVersion10Databases(temp_dir.path()); | 154 SetUpVersion10Databases(temp_dir.path()); |
| 101 { | 155 { |
| 102 // Create a UserSettings, which should trigger migration code. We do this | 156 // Create a UserSettings, which should trigger migration code. We do this |
| 103 // inside a scoped block so it closes itself and we can poke around to see | 157 // inside a scoped block so it closes itself and we can poke around to see |
| 104 // what happened later. | 158 // what happened later. |
| 105 UserSettings settings; | 159 UserSettings settings; |
| 106 settings.Init(v10_user_setting_db_path()); | 160 settings.Init(v10_user_setting_db_path()); |
| 107 } | 161 } |
| 108 | 162 |
| 109 // Now poke around using sqlite to see if UserSettings migrated properly. | 163 // Now poke around using sqlite to see if UserSettings migrated properly. |
| 110 sqlite3* handle = NULL; | 164 sqlite3* handle = NULL; |
| 111 ASSERT_EQ(SQLITE_OK, OpenSqliteDb(v10_user_setting_db_path(), &handle)); | 165 ASSERT_EQ(SQLITE_OK, OpenSqliteDb(v10_user_setting_db_path(), &handle)); |
| 112 | 166 |
| 113 // Note that we don't use ScopedStatement to avoid closing the sqlite handle | 167 // Note that we don't use ScopedStatement to avoid closing the sqlite handle |
| 114 // before finalizing the statement. | 168 // before finalizing the statement. |
| 115 { | 169 { |
| 116 SQLStatement version_query; | 170 SQLStatement version_query; |
| 117 version_query.prepare(handle, "SELECT version FROM db_version"); | 171 version_query.prepare(handle, "SELECT version FROM db_version"); |
| 118 ASSERT_EQ(SQLITE_ROW, version_query.step()); | 172 ASSERT_EQ(SQLITE_ROW, version_query.step()); |
| 119 const int version = version_query.column_int(0); | 173 const int version = version_query.column_int(0); |
| 120 EXPECT_EQ(11, version); | 174 EXPECT_GE(version, 11); |
| 121 } | 175 } |
| 122 | 176 |
| 123 EXPECT_FALSE(file_util::PathExists(old_style_sync_data_path())); | 177 EXPECT_FALSE(file_util::PathExists(old_style_sync_data_path())); |
| 124 | 178 |
| 125 FilePath new_style_path = temp_dir.path().Append( | 179 FilePath new_style_path = temp_dir.path().Append( |
| 126 syncable::DirectoryManager::GetSyncDataDatabaseFilename()); | 180 syncable::DirectoryManager::GetSyncDataDatabaseFilename()); |
| 127 | 181 |
| 128 std::string contents; | 182 std::string contents; |
| 129 ASSERT_TRUE(file_util::ReadFileToString(new_style_path, &contents)); | 183 ASSERT_TRUE(file_util::ReadFileToString(new_style_path, &contents)); |
| 130 EXPECT_TRUE(sync_data() == contents); | 184 EXPECT_TRUE(sync_data() == contents); |
| 131 sqlite3_close(handle); | 185 sqlite3_close(handle); |
| 132 } | 186 } |
| 133 | 187 |
| 188 TEST_F(UserSettingsTest, MigrateFromV11ToV12) { |
| 189 ScopedTempDir temp_dir; |
| 190 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 191 SetUpVersion11Database(temp_dir.path()); |
| 192 { |
| 193 UserSettings settings; |
| 194 settings.Init(v11_user_setting_db_path()); |
| 195 } |
| 196 sqlite3* handle = NULL; |
| 197 ASSERT_EQ(SQLITE_OK, OpenSqliteDb(v11_user_setting_db_path(), &handle)); |
| 198 |
| 199 { |
| 200 SQLStatement version_query; |
| 201 version_query.prepare(handle, "SELECT version FROM db_version"); |
| 202 ASSERT_EQ(SQLITE_ROW, version_query.step()); |
| 203 const int version = version_query.column_int(0); |
| 204 EXPECT_GE(version, 12); |
| 205 |
| 206 SQLStatement table_query; |
| 207 table_query.prepare(handle, "SELECT name FROM sqlite_master " |
| 208 "WHERE type='table' AND name='signin_types'"); |
| 209 ASSERT_NE(SQLITE_ROW, table_query.step()); |
| 210 } |
| 211 } |
| 212 |
| 134 TEST_F(UserSettingsTest, APEncode) { | 213 TEST_F(UserSettingsTest, APEncode) { |
| 135 string test; | 214 string test; |
| 136 char i; | 215 char i; |
| 137 for (i = numeric_limits<char>::min(); i < numeric_limits<char>::max(); ++i) | 216 for (i = numeric_limits<char>::min(); i < numeric_limits<char>::max(); ++i) |
| 138 test.push_back(i); | 217 test.push_back(i); |
| 139 test.push_back(i); | 218 test.push_back(i); |
| 140 const string encoded = APEncode(test); | 219 const string encoded = APEncode(test); |
| 141 const string decoded = APDecode(encoded); | 220 const string decoded = APDecode(encoded); |
| 142 ASSERT_EQ(test, decoded); | 221 ASSERT_EQ(test, decoded); |
| 143 } | 222 } |
| OLD | NEW |