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" | |
15 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "third_party/sqlite/sqlite3.h" | 15 #include "third_party/sqlite/sqlite3.h" |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 // Helper to return the count of items in sqlite_master. Return -1 in | 19 // Helper to return the count of items in sqlite_master. Return -1 in |
21 // case of error. | 20 // case of error. |
22 int SqliteMasterCount(sql::Connection* db) { | 21 int SqliteMasterCount(sql::Connection* db) { |
23 const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master"; | 22 const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master"; |
24 sql::Statement s(db->GetUniqueStatement(kMasterCount)); | 23 sql::Statement s(db->GetUniqueStatement(kMasterCount)); |
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 | 495 |
497 // Now empty, the open should succeed with an empty database. | 496 // Now empty, the open should succeed with an empty database. |
498 EXPECT_TRUE(db().Open(db_path())); | 497 EXPECT_TRUE(db().Open(db_path())); |
499 EXPECT_EQ(0, SqliteMasterCount(&db())); | 498 EXPECT_EQ(0, SqliteMasterCount(&db())); |
500 } | 499 } |
501 | 500 |
502 // Test that a callback from Open() can raze the database. This is | 501 // Test that a callback from Open() can raze the database. This is |
503 // essential for cases where the Open() can fail entirely, so the | 502 // essential for cases where the Open() can fail entirely, so the |
504 // Raze() cannot happen later. Additionally test that when the | 503 // Raze() cannot happen later. Additionally test that when the |
505 // callback does this during Open(), the open is retried and succeeds. | 504 // 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. |
506 TEST_F(SQLConnectionTest, RazeCallbackReopen) { | 512 TEST_F(SQLConnectionTest, RazeCallbackReopen) { |
507 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 513 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
508 ASSERT_TRUE(db().Execute(kCreateSql)); | 514 ASSERT_TRUE(db().Execute(kCreateSql)); |
509 ASSERT_EQ(1, SqliteMasterCount(&db())); | 515 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 } |
510 db().Close(); | 522 db().Close(); |
511 | 523 |
512 // Corrupt the database so that nothing works, including PRAGMAs. | 524 // Trim a single page from the end of the file. |
513 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path())); | 525 { |
| 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 } |
514 | 531 |
515 // Open() will succeed, even though the PRAGMA calls within will | 532 // Open() will succeed, even though the PRAGMA calls within will |
516 // fail with SQLITE_CORRUPT, as will this PRAGMA. | 533 // fail with SQLITE_CORRUPT, as will this PRAGMA. |
517 { | 534 { |
518 sql::ScopedErrorIgnorer ignore_errors; | 535 sql::ScopedErrorIgnorer ignore_errors; |
519 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 536 ignore_errors.IgnoreError(SQLITE_CORRUPT); |
520 ASSERT_TRUE(db().Open(db_path())); | 537 ASSERT_TRUE(db().Open(db_path())); |
521 ASSERT_FALSE(db().Execute("PRAGMA auto_vacuum")); | 538 ASSERT_FALSE(db().Execute("PRAGMA auto_vacuum")); |
522 db().Close(); | 539 db().Close(); |
523 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 540 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
816 } | 833 } |
817 | 834 |
818 // Detach succeeds outside of a transaction. | 835 // Detach succeeds outside of a transaction. |
819 db().RollbackTransaction(); | 836 db().RollbackTransaction(); |
820 EXPECT_TRUE(db().DetachDatabase(kAttachmentPoint)); | 837 EXPECT_TRUE(db().DetachDatabase(kAttachmentPoint)); |
821 | 838 |
822 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); | 839 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
823 } | 840 } |
824 | 841 |
825 } // namespace | 842 } // namespace |
OLD | NEW |