Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/bind.h" | |
| 5 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 6 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 9 #include "sql/connection.h" | 10 #include "sql/connection.h" |
| 10 #include "sql/meta_table.h" | 11 #include "sql/meta_table.h" |
| 11 #include "sql/recovery.h" | 12 #include "sql/recovery.h" |
| 12 #include "sql/statement.h" | 13 #include "sql/statement.h" |
| 13 #include "sql/test/scoped_error_ignorer.h" | 14 #include "sql/test/scoped_error_ignorer.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 | 132 |
| 132 // Successfully recovered. | 133 // Successfully recovered. |
| 133 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass())); | 134 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass())); |
| 134 } | 135 } |
| 135 EXPECT_FALSE(db().is_open()); | 136 EXPECT_FALSE(db().is_open()); |
| 136 ASSERT_TRUE(Reopen()); | 137 ASSERT_TRUE(Reopen()); |
| 137 EXPECT_TRUE(db().is_open()); | 138 EXPECT_TRUE(db().is_open()); |
| 138 ASSERT_EQ("CREATE TABLE x (t TEXT)", GetSchema(&db())); | 139 ASSERT_EQ("CREATE TABLE x (t TEXT)", GetSchema(&db())); |
| 139 | 140 |
| 140 const char* kXSql = "SELECT * FROM x ORDER BY 1"; | 141 const char* kXSql = "SELECT * FROM x ORDER BY 1"; |
| 141 ASSERT_EQ(ExecuteWithResults(&db(), kXSql, "|", "\n"), | 142 ASSERT_EQ("That was a test", |
| 142 "That was a test"); | 143 ExecuteWithResults(&db(), kXSql, "|", "\n")); |
|
Scott Hess - ex-Googler
2013/07/23 22:32:05
Noticed that the expectation was in the wrong plac
| |
| 143 } | 144 } |
| 145 | |
| 146 // The recovery virtual table is only supported for our SQLite. | |
| 147 #if !defined(USE_SYSTEM_SQLITE) | |
| 148 | |
| 149 // Run recovery through its paces on a valid database. | |
| 150 TEST_F(SQLRecoveryTest, VirtualTable) { | |
| 151 const char kCreateSql[] = "CREATE TABLE x (t TEXT)"; | |
| 152 ASSERT_TRUE(db().Execute(kCreateSql)); | |
| 153 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES ('This is a test')")); | |
| 154 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES ('That was a test')")); | |
| 155 | |
| 156 // Successfully recover the database. | |
| 157 { | |
| 158 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(&db(), db_path()); | |
| 159 | |
| 160 // Tables to recover original DB, now at [corrupt]. | |
| 161 const char kRecoveryCreateSql[] = | |
| 162 "CREATE VIRTUAL TABLE temp.recover_x using recover(" | |
| 163 " corrupt.x," | |
| 164 " t TEXT STRICT" | |
| 165 ")"; | |
| 166 ASSERT_TRUE(recovery->db()->Execute(kRecoveryCreateSql)); | |
| 167 | |
| 168 // Re-create the original schema. | |
| 169 ASSERT_TRUE(recovery->db()->Execute(kCreateSql)); | |
| 170 | |
| 171 // Copy the data from the recovery tables to the new database. | |
| 172 const char kRecoveryCopySql[] = | |
| 173 "INSERT INTO x SELECT t FROM recover_x"; | |
| 174 ASSERT_TRUE(recovery->db()->Execute(kRecoveryCopySql)); | |
| 175 | |
| 176 // Successfully recovered. | |
| 177 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass())); | |
| 178 } | |
| 179 | |
| 180 // Since the database was not corrupt, the entire schema and all | |
| 181 // data should be recovered. | |
| 182 ASSERT_TRUE(Reopen()); | |
| 183 ASSERT_EQ("CREATE TABLE x (t TEXT)", GetSchema(&db())); | |
| 184 | |
| 185 const char* kXSql = "SELECT * FROM x ORDER BY 1"; | |
| 186 ASSERT_EQ("That was a test\nThis is a test", | |
| 187 ExecuteWithResults(&db(), kXSql, "|", "\n")); | |
| 188 } | |
| 189 | |
| 190 void RecoveryCallback(sql::Connection* db, const base::FilePath& db_path, | |
| 191 int* record_error, int error, sql::Statement* stmt) { | |
| 192 *record_error = error; | |
| 193 | |
| 194 // Clear the error callback to prevent reentrancy. | |
| 195 db->reset_error_callback(); | |
| 196 | |
| 197 scoped_ptr<sql::Recovery> recovery = sql::Recovery::Begin(db, db_path); | |
| 198 ASSERT_TRUE(recovery.get()); | |
| 199 | |
| 200 const char kCreateRecoveryTable[] = | |
| 201 "CREATE VIRTUAL TABLE temp.recover_x using recover(" | |
| 202 " corrupt.x," | |
| 203 " id INTEGER STRICT," | |
| 204 " v INTEGER STRICT" | |
| 205 ")"; | |
| 206 const char kCreateTable[] = "CREATE TABLE x (id INTEGER, v INTEGER)"; | |
| 207 const char kCreateIndex[] = "CREATE UNIQUE INDEX x_id ON x (id)"; | |
| 208 | |
| 209 // Replicate data over. | |
| 210 const char kInsertSql[] = | |
| 211 "INSERT OR REPLACE INTO x SELECT id, v FROM recover_x"; | |
| 212 | |
| 213 ASSERT_TRUE(recovery->db()->Execute(kCreateRecoveryTable)); | |
| 214 ASSERT_TRUE(recovery->db()->Execute(kCreateTable)); | |
| 215 ASSERT_TRUE(recovery->db()->Execute(kCreateIndex)); | |
| 216 ASSERT_TRUE(recovery->db()->Execute(kInsertSql)); | |
| 217 | |
| 218 ASSERT_TRUE(sql::Recovery::Recovered(recovery.Pass())); | |
| 219 } | |
| 220 | |
| 221 // Build a databases, corrupt it by making an index contain a | |
| 222 // reference to a deleted row, then recover it in response to a query | |
| 223 // which hits that row. | |
| 224 TEST_F(SQLRecoveryTest, RecoverCorruptIndex) { | |
| 225 const char kCreateTable[] = "CREATE TABLE x (id INTEGER, v INTEGER)"; | |
| 226 const char kCreateIndex[] = "CREATE UNIQUE INDEX x_id ON x (id)"; | |
| 227 ASSERT_TRUE(db().Execute(kCreateTable)); | |
| 228 ASSERT_TRUE(db().Execute(kCreateIndex)); | |
| 229 | |
| 230 // Insert a bit of data. | |
| 231 { | |
| 232 ASSERT_TRUE(db().BeginTransaction()); | |
| 233 | |
| 234 const char kInsertSql[] = "INSERT INTO x (id, v) VALUES (?, ?)"; | |
| 235 sql::Statement s(db().GetUniqueStatement(kInsertSql)); | |
| 236 for (int i = 0; i < 10; ++i) { | |
| 237 s.Reset(true); | |
| 238 s.BindInt(0, i); | |
| 239 s.BindInt(1, i); | |
| 240 EXPECT_FALSE(s.Step()); | |
| 241 EXPECT_TRUE(s.Succeeded()); | |
| 242 } | |
| 243 | |
| 244 ASSERT_TRUE(db().CommitTransaction()); | |
| 245 } | |
| 246 | |
| 247 int page_size = 0; | |
| 248 { | |
| 249 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size")); | |
| 250 ASSERT_TRUE(s.Step()); | |
| 251 page_size = s.ColumnInt(0); | |
| 252 } | |
| 253 | |
| 254 // Find the page the index is stored on. | |
| 255 int index_page = 0; | |
| 256 { | |
| 257 const char kPageSql[] = "SELECT rootpage FROM sqlite_master WHERE name = ?"; | |
| 258 sql::Statement s(db().GetUniqueStatement(kPageSql)); | |
| 259 s.BindString(0, "x_id"); | |
| 260 EXPECT_TRUE(s.Step()); | |
| 261 index_page = s.ColumnInt(0); | |
| 262 } | |
| 263 | |
| 264 // Capture the index page into |buf|. | |
| 265 scoped_ptr<char[]> buf(new char[page_size]); | |
| 266 { | |
| 267 file_util::ScopedFILE file(file_util::OpenFile(db_path(), "rb")); | |
| 268 ASSERT_TRUE(file.get() != NULL); | |
| 269 ASSERT_EQ(0, fseek(file.get(), (index_page-1)*page_size, SEEK_SET)); | |
| 270 EXPECT_EQ(1u, fread(buf.get(), page_size, 1, file.get())); | |
| 271 } | |
| 272 | |
| 273 // Delete the row from the table and index. | |
| 274 ASSERT_TRUE(db().Execute("DELETE FROM x WHERE id = 0")); | |
| 275 | |
| 276 // Close to clear and cached data. | |
| 277 db().Close(); | |
| 278 | |
| 279 // Put the stale index page back. This corrupts the database. | |
| 280 { | |
| 281 file_util::ScopedFILE file(file_util::OpenFile(db_path(), "rb+")); | |
| 282 ASSERT_TRUE(file.get() != NULL); | |
| 283 ASSERT_EQ(0, fseek(file.get(), (index_page-1)*page_size, SEEK_SET)); | |
| 284 EXPECT_EQ(1u, fwrite(buf.get(), page_size, 1, file.get())); | |
| 285 } | |
| 286 | |
| 287 ASSERT_TRUE(Reopen()); | |
| 288 | |
| 289 int error = SQLITE_OK; | |
| 290 db().set_error_callback(base::Bind(&RecoveryCallback, | |
| 291 &db(), db_path(), &error)); | |
| 292 | |
| 293 // This works before the callback is called. | |
| 294 const char kTrivialSql[] = "SELECT COUNT(*) FROM sqlite_master"; | |
| 295 EXPECT_TRUE(db().IsSQLValid(kTrivialSql)); | |
| 296 | |
| 297 // TODO(shess): Could this be delete? Anything which fails should work. | |
| 298 const char kSelectSql[] = "SELECT v FROM x WHERE id = 0"; | |
| 299 ASSERT_FALSE(db().Execute(kSelectSql)); | |
| 300 EXPECT_EQ(SQLITE_CORRUPT, error); | |
| 301 | |
| 302 // Database handle has been poisoned. | |
| 303 EXPECT_FALSE(db().IsSQLValid(kTrivialSql)); | |
| 304 | |
| 305 ASSERT_TRUE(Reopen()); | |
| 306 | |
| 307 // The recovered table should reflect the deletion. | |
| 308 const char kSelectAllSql[] = "SELECT v FROM x ORDER BY id"; | |
| 309 EXPECT_EQ("1,2,3,4,5,6,7,8,9", | |
| 310 ExecuteWithResults(&db(), kSelectAllSql, "|", ",")); | |
| 311 | |
| 312 // The earlier statement should now execute successfully, with no results. | |
| 313 EXPECT_EQ("", ExecuteWithResults(&db(), kSelectSql, "|", ",")); | |
| 314 } | |
| 315 | |
| 316 // Build a databases, corrupt it by making an table containing a row | |
| 317 // which is not referenced by the index, then recover it. | |
| 318 TEST_F(SQLRecoveryTest, RecoverCorruptTable) { | |
| 319 const char kCreateTable[] = "CREATE TABLE x (id INTEGER, v INTEGER)"; | |
| 320 const char kCreateIndex[] = "CREATE UNIQUE INDEX x_id ON x (id)"; | |
| 321 ASSERT_TRUE(db().Execute(kCreateTable)); | |
| 322 ASSERT_TRUE(db().Execute(kCreateIndex)); | |
| 323 | |
| 324 // Insert a bit of data. | |
| 325 { | |
| 326 ASSERT_TRUE(db().BeginTransaction()); | |
| 327 | |
| 328 const char kInsertSql[] = "INSERT INTO x (id, v) VALUES (?, ?)"; | |
| 329 sql::Statement s(db().GetUniqueStatement(kInsertSql)); | |
| 330 for (int i = 0; i < 10; ++i) { | |
| 331 s.Reset(true); | |
| 332 s.BindInt(0, i); | |
| 333 s.BindInt(1, i); | |
| 334 EXPECT_FALSE(s.Step()); | |
| 335 EXPECT_TRUE(s.Succeeded()); | |
| 336 } | |
| 337 | |
| 338 ASSERT_TRUE(db().CommitTransaction()); | |
| 339 } | |
| 340 | |
| 341 // Find the page the table is stored on. | |
| 342 int table_page = 0; | |
| 343 { | |
| 344 const char kPageSql[] = "SELECT rootpage FROM sqlite_master WHERE name = ?"; | |
| 345 sql::Statement s(db().GetUniqueStatement(kPageSql)); | |
| 346 s.BindString(0, "x"); | |
| 347 EXPECT_TRUE(s.Step()); | |
| 348 table_page = s.ColumnInt(0); | |
| 349 } | |
| 350 | |
| 351 int page_size = 0; | |
| 352 { | |
| 353 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size")); | |
| 354 ASSERT_TRUE(s.Step()); | |
| 355 page_size = s.ColumnInt(0); | |
| 356 } | |
| 357 | |
| 358 // Capture the table page into |buf|. | |
| 359 scoped_ptr<char[]> buf(new char[page_size]); | |
| 360 { | |
| 361 file_util::ScopedFILE file(file_util::OpenFile(db_path(), "rb")); | |
| 362 ASSERT_TRUE(file.get() != NULL); | |
| 363 ASSERT_EQ(0, fseek(file.get(), (table_page-1)*page_size, SEEK_SET)); | |
| 364 EXPECT_EQ(1u, fread(buf.get(), page_size, 1, file.get())); | |
| 365 } | |
| 366 | |
| 367 // Delete the row from the table and index. | |
| 368 ASSERT_TRUE(db().Execute("DELETE FROM x WHERE id = 0")); | |
| 369 | |
| 370 // Close to clear and cached data. | |
| 371 db().Close(); | |
| 372 | |
| 373 // Put the stale table page back. | |
| 374 { | |
| 375 file_util::ScopedFILE file(file_util::OpenFile(db_path(), "rb+")); | |
| 376 ASSERT_TRUE(file.get() != NULL); | |
| 377 ASSERT_EQ(0, fseek(file.get(), (table_page-1)*page_size, SEEK_SET)); | |
| 378 EXPECT_EQ(1u, fwrite(buf.get(), page_size, 1, file.get())); | |
| 379 } | |
| 380 | |
| 381 // At this point, the table contains a value not referenced by the | |
| 382 // index. | |
| 383 // TODO(shess): Figure out a query which causes SQLite to notice | |
| 384 // this organically. Meanwhile, just handle it manually. | |
| 385 | |
| 386 ASSERT_TRUE(Reopen()); | |
| 387 | |
| 388 // Index shows one less than originally inserted. | |
| 389 const char kCountSql[] = "SELECT COUNT (*) FROM x"; | |
| 390 EXPECT_EQ("9", ExecuteWithResults(&db(), kCountSql, "|", ",")); | |
| 391 | |
| 392 // But a table scan shows all of the original data. | |
| 393 const char kDistinctSql[] = "SELECT DISTINCT COUNT (id) FROM x"; | |
| 394 EXPECT_EQ("10", ExecuteWithResults(&db(), kDistinctSql, "|", ",")); | |
| 395 | |
| 396 // Insert id 0 again. Since it is not in the index, the insert | |
| 397 // succeeds, but results in a duplicate value in the table. | |
| 398 const char kInsertSql[] = "INSERT INTO x (id, v) VALUES (0, 100)"; | |
| 399 ASSERT_TRUE(db().Execute(kInsertSql)); | |
| 400 | |
| 401 // Values are in expected places. | |
| 402 EXPECT_EQ("10", ExecuteWithResults(&db(), kCountSql, "|", ",")); | |
| 403 EXPECT_EQ("11", ExecuteWithResults(&db(), kDistinctSql, "|", ",")); | |
| 404 | |
| 405 // This works before the callback is called. | |
| 406 const char kTrivialSql[] = "SELECT COUNT(*) FROM sqlite_master"; | |
| 407 EXPECT_TRUE(db().IsSQLValid(kTrivialSql)); | |
| 408 | |
| 409 // Call the recovery callback manually. | |
| 410 int error = SQLITE_OK; | |
| 411 RecoveryCallback(&db(), db_path(), &error, SQLITE_CORRUPT, NULL); | |
| 412 EXPECT_EQ(SQLITE_CORRUPT, error); | |
| 413 | |
| 414 // Database handle has been poisoned. | |
| 415 EXPECT_FALSE(db().IsSQLValid(kTrivialSql)); | |
| 416 | |
| 417 ASSERT_TRUE(Reopen()); | |
| 418 | |
| 419 // The recovered table has consistency between the index and the table. | |
| 420 EXPECT_EQ("10", ExecuteWithResults(&db(), kCountSql, "|", ",")); | |
| 421 EXPECT_EQ("10", ExecuteWithResults(&db(), kDistinctSql, "|", ",")); | |
| 422 | |
| 423 // The expected value was retained. | |
| 424 const char kSelectSql[] = "SELECT v FROM x WHERE id = 0"; | |
| 425 EXPECT_EQ("100", ExecuteWithResults(&db(), kSelectSql, "|", ",")); | |
| 426 } | |
| 427 #endif // !defined(USE_SYSTEM_SQLITE) | |
| 144 | 428 |
| 145 } // namespace | 429 } // namespace |
| OLD | NEW |