| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "sql/connection.h" | 9 #include "sql/connection.h" |
| 10 #include "sql/meta_table.h" | 10 #include "sql/meta_table.h" |
| 11 #include "sql/statement.h" | 11 #include "sql/statement.h" |
| 12 #include "sql/test/error_callback_support.h" | 12 #include "sql/test/error_callback_support.h" |
| 13 #include "sql/test/scoped_error_ignorer.h" | 13 #include "sql/test/scoped_error_ignorer.h" |
| 14 #include "sql/test/test_helpers.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/sqlite/sqlite3.h" | 16 #include "third_party/sqlite/sqlite3.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // Helper to return the count of items in sqlite_master. Return -1 in | 20 // Helper to return the count of items in sqlite_master. Return -1 in |
| 20 // case of error. | 21 // case of error. |
| 21 int SqliteMasterCount(sql::Connection* db) { | 22 int SqliteMasterCount(sql::Connection* db) { |
| 22 const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master"; | 23 const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master"; |
| 23 sql::Statement s(db->GetUniqueStatement(kMasterCount)); | 24 sql::Statement s(db->GetUniqueStatement(kMasterCount)); |
| (...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 | 496 |
| 496 // Now empty, the open should succeed with an empty database. | 497 // Now empty, the open should succeed with an empty database. |
| 497 EXPECT_TRUE(db().Open(db_path())); | 498 EXPECT_TRUE(db().Open(db_path())); |
| 498 EXPECT_EQ(0, SqliteMasterCount(&db())); | 499 EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 499 } | 500 } |
| 500 | 501 |
| 501 // Test that a callback from Open() can raze the database. This is | 502 // Test that a callback from Open() can raze the database. This is |
| 502 // essential for cases where the Open() can fail entirely, so the | 503 // essential for cases where the Open() can fail entirely, so the |
| 503 // Raze() cannot happen later. Additionally test that when the | 504 // Raze() cannot happen later. Additionally test that when the |
| 504 // callback does this during Open(), the open is retried and succeeds. | 505 // callback does this during Open(), the open is retried and succeeds. |
| 505 // | |
| 506 // Most corruptions seen in the wild seem to happen when two pages in | |
| 507 // the database were not written transactionally (the transaction | |
| 508 // changed both, but one wasn't successfully written for some reason). | |
| 509 // A special case of that is when the header indicates that the | |
| 510 // database contains more pages than are in the file. This breaks | |
| 511 // things at a very basic level, verify that Raze() can handle it. | |
| 512 TEST_F(SQLConnectionTest, RazeCallbackReopen) { | 506 TEST_F(SQLConnectionTest, RazeCallbackReopen) { |
| 513 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 507 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 514 ASSERT_TRUE(db().Execute(kCreateSql)); | 508 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 515 ASSERT_EQ(1, SqliteMasterCount(&db())); | 509 ASSERT_EQ(1, SqliteMasterCount(&db())); |
| 516 int page_size = 0; | |
| 517 { | |
| 518 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size")); | |
| 519 ASSERT_TRUE(s.Step()); | |
| 520 page_size = s.ColumnInt(0); | |
| 521 } | |
| 522 db().Close(); | 510 db().Close(); |
| 523 | 511 |
| 524 // Trim a single page from the end of the file. | 512 // Corrupt the database so that nothing works, including PRAGMAs. |
| 525 { | 513 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path())); |
| 526 file_util::ScopedFILE file(file_util::OpenFile(db_path(), "rb+")); | |
| 527 ASSERT_TRUE(file.get() != NULL); | |
| 528 ASSERT_EQ(0, fseek(file.get(), -page_size, SEEK_END)); | |
| 529 ASSERT_TRUE(file_util::TruncateFile(file.get())); | |
| 530 } | |
| 531 | 514 |
| 532 // Open() will succeed, even though the PRAGMA calls within will | 515 // Open() will succeed, even though the PRAGMA calls within will |
| 533 // fail with SQLITE_CORRUPT, as will this PRAGMA. | 516 // fail with SQLITE_CORRUPT, as will this PRAGMA. |
| 534 { | 517 { |
| 535 sql::ScopedErrorIgnorer ignore_errors; | 518 sql::ScopedErrorIgnorer ignore_errors; |
| 536 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 519 ignore_errors.IgnoreError(SQLITE_CORRUPT); |
| 537 ASSERT_TRUE(db().Open(db_path())); | 520 ASSERT_TRUE(db().Open(db_path())); |
| 538 ASSERT_FALSE(db().Execute("PRAGMA auto_vacuum")); | 521 ASSERT_FALSE(db().Execute("PRAGMA auto_vacuum")); |
| 539 db().Close(); | 522 db().Close(); |
| 540 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 523 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 833 } | 816 } |
| 834 | 817 |
| 835 // Detach succeeds outside of a transaction. | 818 // Detach succeeds outside of a transaction. |
| 836 db().RollbackTransaction(); | 819 db().RollbackTransaction(); |
| 837 EXPECT_TRUE(db().DetachDatabase(kAttachmentPoint)); | 820 EXPECT_TRUE(db().DetachDatabase(kAttachmentPoint)); |
| 838 | 821 |
| 839 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); | 822 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 840 } | 823 } |
| 841 | 824 |
| 842 } // namespace | 825 } // namespace |
| OLD | NEW |