| 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" |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 } | 418 } |
| 419 | 419 |
| 420 // Verify that Raze() can handle an empty file. SQLite should treat | 420 // Verify that Raze() can handle an empty file. SQLite should treat |
| 421 // this as an empty database. | 421 // this as an empty database. |
| 422 TEST_F(SQLConnectionTest, RazeEmptyDB) { | 422 TEST_F(SQLConnectionTest, RazeEmptyDB) { |
| 423 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 423 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 424 ASSERT_TRUE(db().Execute(kCreateSql)); | 424 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 425 db().Close(); | 425 db().Close(); |
| 426 | 426 |
| 427 { | 427 { |
| 428 file_util::ScopedFILE file(file_util::OpenFile(db_path(), "r+")); | 428 file_util::ScopedFILE file(file_util::OpenFile(db_path(), "rb+")); |
| 429 ASSERT_TRUE(file.get() != NULL); | 429 ASSERT_TRUE(file.get() != NULL); |
| 430 ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET)); | 430 ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET)); |
| 431 ASSERT_TRUE(file_util::TruncateFile(file.get())); | 431 ASSERT_TRUE(file_util::TruncateFile(file.get())); |
| 432 } | 432 } |
| 433 | 433 |
| 434 ASSERT_TRUE(db().Open(db_path())); | 434 ASSERT_TRUE(db().Open(db_path())); |
| 435 ASSERT_TRUE(db().Raze()); | 435 ASSERT_TRUE(db().Raze()); |
| 436 EXPECT_EQ(0, SqliteMasterCount(&db())); | 436 EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 437 } | 437 } |
| 438 | 438 |
| 439 // Verify that Raze() can handle a file of junk. | 439 // Verify that Raze() can handle a file of junk. |
| 440 TEST_F(SQLConnectionTest, RazeNOTADB) { | 440 TEST_F(SQLConnectionTest, RazeNOTADB) { |
| 441 db().Close(); | 441 db().Close(); |
| 442 sql::Connection::Delete(db_path()); | 442 sql::Connection::Delete(db_path()); |
| 443 ASSERT_FALSE(base::PathExists(db_path())); | 443 ASSERT_FALSE(base::PathExists(db_path())); |
| 444 | 444 |
| 445 { | 445 { |
| 446 file_util::ScopedFILE file(file_util::OpenFile(db_path(), "w")); | 446 file_util::ScopedFILE file(file_util::OpenFile(db_path(), "wb")); |
| 447 ASSERT_TRUE(file.get() != NULL); | 447 ASSERT_TRUE(file.get() != NULL); |
| 448 | 448 |
| 449 const char* kJunk = "This is the hour of our discontent."; | 449 const char* kJunk = "This is the hour of our discontent."; |
| 450 fputs(kJunk, file.get()); | 450 fputs(kJunk, file.get()); |
| 451 } | 451 } |
| 452 ASSERT_TRUE(base::PathExists(db_path())); | 452 ASSERT_TRUE(base::PathExists(db_path())); |
| 453 | 453 |
| 454 // SQLite will successfully open the handle, but will fail with | 454 // SQLite will successfully open the handle, but will fail with |
| 455 // SQLITE_IOERR_SHORT_READ on pragma statemenets which read the | 455 // SQLITE_IOERR_SHORT_READ on pragma statemenets which read the |
| 456 // header. | 456 // header. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 469 } | 469 } |
| 470 | 470 |
| 471 // Verify that Raze() can handle a database overwritten with garbage. | 471 // Verify that Raze() can handle a database overwritten with garbage. |
| 472 TEST_F(SQLConnectionTest, RazeNOTADB2) { | 472 TEST_F(SQLConnectionTest, RazeNOTADB2) { |
| 473 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 473 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 474 ASSERT_TRUE(db().Execute(kCreateSql)); | 474 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 475 ASSERT_EQ(1, SqliteMasterCount(&db())); | 475 ASSERT_EQ(1, SqliteMasterCount(&db())); |
| 476 db().Close(); | 476 db().Close(); |
| 477 | 477 |
| 478 { | 478 { |
| 479 file_util::ScopedFILE file(file_util::OpenFile(db_path(), "r+")); | 479 file_util::ScopedFILE file(file_util::OpenFile(db_path(), "rb+")); |
| 480 ASSERT_TRUE(file.get() != NULL); | 480 ASSERT_TRUE(file.get() != NULL); |
| 481 ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET)); | 481 ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET)); |
| 482 | 482 |
| 483 const char* kJunk = "This is the hour of our discontent."; | 483 const char* kJunk = "This is the hour of our discontent."; |
| 484 fputs(kJunk, file.get()); | 484 fputs(kJunk, file.get()); |
| 485 } | 485 } |
| 486 | 486 |
| 487 // SQLite will successfully open the handle, but will fail with | 487 // SQLite will successfully open the handle, but will fail with |
| 488 // SQLITE_NOTADB on pragma statemenets which attempt to read the | 488 // SQLITE_NOTADB on pragma statemenets which attempt to read the |
| 489 // corrupted header. | 489 // corrupted header. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 519 int page_size = 0; | 519 int page_size = 0; |
| 520 { | 520 { |
| 521 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size")); | 521 sql::Statement s(db().GetUniqueStatement("PRAGMA page_size")); |
| 522 ASSERT_TRUE(s.Step()); | 522 ASSERT_TRUE(s.Step()); |
| 523 page_size = s.ColumnInt(0); | 523 page_size = s.ColumnInt(0); |
| 524 } | 524 } |
| 525 db().Close(); | 525 db().Close(); |
| 526 | 526 |
| 527 // Trim a single page from the end of the file. | 527 // Trim a single page from the end of the file. |
| 528 { | 528 { |
| 529 file_util::ScopedFILE file(file_util::OpenFile(db_path(), "r+")); | 529 file_util::ScopedFILE file(file_util::OpenFile(db_path(), "rb+")); |
| 530 ASSERT_TRUE(file.get() != NULL); | 530 ASSERT_TRUE(file.get() != NULL); |
| 531 ASSERT_EQ(0, fseek(file.get(), -page_size, SEEK_END)); | 531 ASSERT_EQ(0, fseek(file.get(), -page_size, SEEK_END)); |
| 532 ASSERT_TRUE(file_util::TruncateFile(file.get())); | 532 ASSERT_TRUE(file_util::TruncateFile(file.get())); |
| 533 } | 533 } |
| 534 | 534 |
| 535 // Open() will succeed, even though the PRAGMA calls within will | 535 // Open() will succeed, even though the PRAGMA calls within will |
| 536 // fail with SQLITE_CORRUPT, as will this PRAGMA. | 536 // fail with SQLITE_CORRUPT, as will this PRAGMA. |
| 537 { | 537 { |
| 538 sql::ScopedErrorIgnorer ignore_errors; | 538 sql::ScopedErrorIgnorer ignore_errors; |
| 539 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 539 ignore_errors.IgnoreError(SQLITE_CORRUPT); |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 836 } | 836 } |
| 837 | 837 |
| 838 // Detach succeeds outside of a transaction. | 838 // Detach succeeds outside of a transaction. |
| 839 db().RollbackTransaction(); | 839 db().RollbackTransaction(); |
| 840 EXPECT_TRUE(db().DetachDatabase(kAttachmentPoint)); | 840 EXPECT_TRUE(db().DetachDatabase(kAttachmentPoint)); |
| 841 | 841 |
| 842 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); | 842 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 843 } | 843 } |
| 844 | 844 |
| 845 } // namespace | 845 } // namespace |
| OLD | NEW |