OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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" |
(...skipping 22 matching lines...) Expand all Loading... |
33 UserSettingsTest() : sync_data_("Some sync data") { } | 33 UserSettingsTest() : sync_data_("Some sync data") { } |
34 | 34 |
35 // Creates and populates the V10 database files within | 35 // Creates and populates the V10 database files within |
36 // |destination_directory|. | 36 // |destination_directory|. |
37 void SetUpVersion10Databases(const FilePath& destination_directory) { | 37 void SetUpVersion10Databases(const FilePath& destination_directory) { |
38 sqlite3* primer_handle = NULL; | 38 sqlite3* primer_handle = NULL; |
39 v10_user_setting_db_path_ = | 39 v10_user_setting_db_path_ = |
40 destination_directory.Append(FilePath(kV10UserSettingsDB)); | 40 destination_directory.Append(FilePath(kV10UserSettingsDB)); |
41 ASSERT_EQ(SQLITE_OK, sqlite_utils::OpenSqliteDb(v10_user_setting_db_path_, | 41 ASSERT_EQ(SQLITE_OK, sqlite_utils::OpenSqliteDb(v10_user_setting_db_path_, |
42 &primer_handle)); | 42 &primer_handle)); |
| 43 sqlite_utils::scoped_sqlite_db_ptr db(primer_handle); |
| 44 |
43 old_style_sync_data_path_ = | 45 old_style_sync_data_path_ = |
44 destination_directory.Append(FilePath(kOldStyleSyncDataDB)); | 46 destination_directory.Append(FilePath(kOldStyleSyncDataDB)); |
45 | 47 |
46 ASSERT_EQ(sync_data_.length(), | 48 ASSERT_EQ(sync_data_.length(), |
47 static_cast<size_t>(file_util::WriteFile( | 49 static_cast<size_t>(file_util::WriteFile( |
48 old_style_sync_data_path_, sync_data_.data(), | 50 old_style_sync_data_path_, sync_data_.data(), |
49 sync_data_.length()))); | 51 sync_data_.length()))); |
50 | 52 |
51 // Create settings table. | 53 // Create settings table. |
52 ExecOrDie(primer_handle, "CREATE TABLE settings" | 54 ExecOrDie(primer_handle, "CREATE TABLE settings" |
(...skipping 22 matching lines...) Expand all Loading... |
75 SQLStatement statement; | 77 SQLStatement statement; |
76 const char query[] = "INSERT INTO shares values ( ?, ?, ? )"; | 78 const char query[] = "INSERT INTO shares values ( ?, ?, ? )"; |
77 statement.prepare(primer_handle, query); | 79 statement.prepare(primer_handle, query); |
78 statement.bind_string(0, "foo@foo.com"); | 80 statement.bind_string(0, "foo@foo.com"); |
79 statement.bind_string(1, "foo@foo.com"); | 81 statement.bind_string(1, "foo@foo.com"); |
80 statement.bind_string(2, FilePathToUTF8(old_style_sync_data_path_)); | 82 statement.bind_string(2, FilePathToUTF8(old_style_sync_data_path_)); |
81 if (SQLITE_DONE != statement.step()) { | 83 if (SQLITE_DONE != statement.step()) { |
82 LOG(FATAL) << query << "\n" << sqlite3_errmsg(primer_handle); | 84 LOG(FATAL) << query << "\n" << sqlite3_errmsg(primer_handle); |
83 } | 85 } |
84 } | 86 } |
85 sqlite3_close(primer_handle); | |
86 } | 87 } |
87 | 88 |
88 // Creates and populates the V11 database file within | 89 // Creates and populates the V11 database file within |
89 // |destination_directory|. | 90 // |destination_directory|. |
90 void SetUpVersion11Database(const FilePath& destination_directory) { | 91 void SetUpVersion11Database(const FilePath& destination_directory) { |
91 sqlite3* primer_handle = NULL; | 92 sqlite3* primer_handle = NULL; |
92 v11_user_setting_db_path_ = | 93 v11_user_setting_db_path_ = |
93 destination_directory.Append(FilePath(kV11UserSettingsDB)); | 94 destination_directory.Append(FilePath(kV11UserSettingsDB)); |
94 ASSERT_EQ(SQLITE_OK, sqlite_utils::OpenSqliteDb(v11_user_setting_db_path_, | 95 ASSERT_EQ(SQLITE_OK, sqlite_utils::OpenSqliteDb(v11_user_setting_db_path_, |
95 &primer_handle)); | 96 &primer_handle)); |
| 97 sqlite_utils::scoped_sqlite_db_ptr db(primer_handle); |
96 | 98 |
97 // Create settings table. | 99 // Create settings table. |
98 ExecOrDie(primer_handle, "CREATE TABLE settings" | 100 ExecOrDie(primer_handle, "CREATE TABLE settings" |
99 " (email, key, value, " | 101 " (email, key, value, " |
100 " PRIMARY KEY(email, key) ON CONFLICT REPLACE)"); | 102 " PRIMARY KEY(email, key) ON CONFLICT REPLACE)"); |
101 | 103 |
102 // Create and populate version table. | 104 // Create and populate version table. |
103 ExecOrDie(primer_handle, "CREATE TABLE db_version ( version )"); | 105 ExecOrDie(primer_handle, "CREATE TABLE db_version ( version )"); |
104 { | 106 { |
105 SQLStatement statement; | 107 SQLStatement statement; |
(...skipping 11 matching lines...) Expand all Loading... |
117 { | 119 { |
118 SQLStatement statement; | 120 SQLStatement statement; |
119 const char query[] = "INSERT INTO signin_types values ( ?, ? )"; | 121 const char query[] = "INSERT INTO signin_types values ( ?, ? )"; |
120 statement.prepare(primer_handle, query); | 122 statement.prepare(primer_handle, query); |
121 statement.bind_string(0, "test"); | 123 statement.bind_string(0, "test"); |
122 statement.bind_string(1, "test"); | 124 statement.bind_string(1, "test"); |
123 if (SQLITE_DONE != statement.step()) { | 125 if (SQLITE_DONE != statement.step()) { |
124 LOG(FATAL) << query << "\n" << sqlite3_errmsg(primer_handle); | 126 LOG(FATAL) << query << "\n" << sqlite3_errmsg(primer_handle); |
125 } | 127 } |
126 } | 128 } |
127 | |
128 sqlite3_close(primer_handle); | |
129 } | 129 } |
130 | 130 |
131 const std::string& sync_data() const { return sync_data_; } | 131 const std::string& sync_data() const { return sync_data_; } |
132 const FilePath& v10_user_setting_db_path() const { | 132 const FilePath& v10_user_setting_db_path() const { |
133 return v10_user_setting_db_path_; | 133 return v10_user_setting_db_path_; |
134 } | 134 } |
135 const FilePath& v11_user_setting_db_path() const { | 135 const FilePath& v11_user_setting_db_path() const { |
136 return v11_user_setting_db_path_; | 136 return v11_user_setting_db_path_; |
137 } | 137 } |
138 const FilePath& old_style_sync_data_path() const { | 138 const FilePath& old_style_sync_data_path() const { |
(...skipping 18 matching lines...) Expand all Loading... |
157 // 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 |
158 // what happened later. | 158 // what happened later. |
159 UserSettings settings; | 159 UserSettings settings; |
160 settings.Init(v10_user_setting_db_path()); | 160 settings.Init(v10_user_setting_db_path()); |
161 } | 161 } |
162 | 162 |
163 // Now poke around using sqlite to see if UserSettings migrated properly. | 163 // Now poke around using sqlite to see if UserSettings migrated properly. |
164 sqlite3* handle = NULL; | 164 sqlite3* handle = NULL; |
165 ASSERT_EQ(SQLITE_OK, sqlite_utils::OpenSqliteDb(v10_user_setting_db_path(), | 165 ASSERT_EQ(SQLITE_OK, sqlite_utils::OpenSqliteDb(v10_user_setting_db_path(), |
166 &handle)); | 166 &handle)); |
| 167 sqlite_utils::scoped_sqlite_db_ptr db(handle); |
167 | 168 |
168 // Note that we don't use ScopedStatement to avoid closing the sqlite handle | 169 // Note that we don't use ScopedStatement to avoid closing the sqlite handle |
169 // before finalizing the statement. | 170 // before finalizing the statement. |
170 { | 171 { |
171 SQLStatement version_query; | 172 SQLStatement version_query; |
172 version_query.prepare(handle, "SELECT version FROM db_version"); | 173 version_query.prepare(handle, "SELECT version FROM db_version"); |
173 ASSERT_EQ(SQLITE_ROW, version_query.step()); | 174 ASSERT_EQ(SQLITE_ROW, version_query.step()); |
174 const int version = version_query.column_int(0); | 175 const int version = version_query.column_int(0); |
175 EXPECT_GE(version, 11); | 176 EXPECT_GE(version, 11); |
176 } | 177 } |
177 | 178 |
178 EXPECT_FALSE(file_util::PathExists(old_style_sync_data_path())); | 179 EXPECT_FALSE(file_util::PathExists(old_style_sync_data_path())); |
179 | 180 |
180 FilePath new_style_path = temp_dir.path().Append( | 181 FilePath new_style_path = temp_dir.path().Append( |
181 syncable::DirectoryManager::GetSyncDataDatabaseFilename()); | 182 syncable::DirectoryManager::GetSyncDataDatabaseFilename()); |
182 | 183 |
183 std::string contents; | 184 std::string contents; |
184 ASSERT_TRUE(file_util::ReadFileToString(new_style_path, &contents)); | 185 ASSERT_TRUE(file_util::ReadFileToString(new_style_path, &contents)); |
185 EXPECT_TRUE(sync_data() == contents); | 186 EXPECT_TRUE(sync_data() == contents); |
186 sqlite3_close(handle); | |
187 } | 187 } |
188 | 188 |
189 TEST_F(UserSettingsTest, MigrateFromV11ToV12) { | 189 TEST_F(UserSettingsTest, MigrateFromV11ToV12) { |
190 ScopedTempDir temp_dir; | 190 ScopedTempDir temp_dir; |
191 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 191 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
192 SetUpVersion11Database(temp_dir.path()); | 192 SetUpVersion11Database(temp_dir.path()); |
193 { | 193 { |
194 UserSettings settings; | 194 UserSettings settings; |
195 settings.Init(v11_user_setting_db_path()); | 195 settings.Init(v11_user_setting_db_path()); |
196 } | 196 } |
197 sqlite3* handle = NULL; | 197 sqlite3* handle = NULL; |
198 ASSERT_EQ(SQLITE_OK, sqlite_utils::OpenSqliteDb(v11_user_setting_db_path(), | 198 ASSERT_EQ(SQLITE_OK, sqlite_utils::OpenSqliteDb(v11_user_setting_db_path(), |
199 &handle)); | 199 &handle)); |
| 200 sqlite_utils::scoped_sqlite_db_ptr db(handle); |
200 | 201 |
201 { | 202 { |
202 SQLStatement version_query; | 203 SQLStatement version_query; |
203 version_query.prepare(handle, "SELECT version FROM db_version"); | 204 version_query.prepare(handle, "SELECT version FROM db_version"); |
204 ASSERT_EQ(SQLITE_ROW, version_query.step()); | 205 ASSERT_EQ(SQLITE_ROW, version_query.step()); |
205 const int version = version_query.column_int(0); | 206 const int version = version_query.column_int(0); |
206 EXPECT_GE(version, 12); | 207 EXPECT_GE(version, 12); |
207 | 208 |
208 SQLStatement table_query; | 209 SQLStatement table_query; |
209 table_query.prepare(handle, "SELECT name FROM sqlite_master " | 210 table_query.prepare(handle, "SELECT name FROM sqlite_master " |
210 "WHERE type='table' AND name='signin_types'"); | 211 "WHERE type='table' AND name='signin_types'"); |
211 ASSERT_NE(SQLITE_ROW, table_query.step()); | 212 ASSERT_NE(SQLITE_ROW, table_query.step()); |
212 } | 213 } |
213 } | 214 } |
214 | 215 |
215 TEST_F(UserSettingsTest, APEncode) { | 216 TEST_F(UserSettingsTest, APEncode) { |
216 string test; | 217 string test; |
217 char i; | 218 char i; |
218 for (i = numeric_limits<char>::min(); i < numeric_limits<char>::max(); ++i) | 219 for (i = numeric_limits<char>::min(); i < numeric_limits<char>::max(); ++i) |
219 test.push_back(i); | 220 test.push_back(i); |
220 test.push_back(i); | 221 test.push_back(i); |
221 const string encoded = APEncode(test); | 222 const string encoded = APEncode(test); |
222 const string decoded = APDecode(encoded); | 223 const string decoded = APDecode(encoded); |
223 ASSERT_EQ(test, decoded); | 224 ASSERT_EQ(test, decoded); |
224 } | 225 } |
| 226 |
| 227 TEST_F(UserSettingsTest, PersistEmptyToken) { |
| 228 ScopedTempDir temp_dir; |
| 229 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 230 UserSettings settings; |
| 231 settings.Init(temp_dir.path().AppendASCII("UserSettings.sqlite3")); |
| 232 settings.SetAuthTokenForService("username", "service", ""); |
| 233 std::string username; |
| 234 std::string token; |
| 235 ASSERT_TRUE(settings.GetLastUserAndServiceToken("service", &username, |
| 236 &token)); |
| 237 EXPECT_EQ("", token); |
| 238 EXPECT_EQ("username", username); |
| 239 } |
| 240 |
| 241 TEST_F(UserSettingsTest, PersistNonEmptyToken) { |
| 242 ScopedTempDir temp_dir; |
| 243 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 244 UserSettings settings; |
| 245 settings.Init(temp_dir.path().AppendASCII("UserSettings.sqlite3")); |
| 246 settings.SetAuthTokenForService("username", "service", "012345beefbeef"); |
| 247 std::string username; |
| 248 std::string token; |
| 249 ASSERT_TRUE(settings.GetLastUserAndServiceToken("service", &username, |
| 250 &token)); |
| 251 EXPECT_EQ("012345beefbeef", token); |
| 252 EXPECT_EQ("username", username); |
| 253 } |
OLD | NEW |