Chromium Code Reviews| 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/files/file_util.h" | 6 #include "base/files/file_util.h" |
| 7 #include "base/files/scoped_file.h" | 7 #include "base/files/scoped_file.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
| 11 #include "base/test/histogram_tester.h" | 11 #include "base/test/histogram_tester.h" |
| 12 #include "sql/connection.h" | 12 #include "sql/connection.h" |
| 13 #include "sql/correct_sql_test_base.h" | |
| 13 #include "sql/meta_table.h" | 14 #include "sql/meta_table.h" |
| 14 #include "sql/proxy.h" | 15 #include "sql/proxy.h" |
| 15 #include "sql/statement.h" | 16 #include "sql/statement.h" |
| 16 #include "sql/test/error_callback_support.h" | 17 #include "sql/test/error_callback_support.h" |
| 17 #include "sql/test/scoped_error_ignorer.h" | 18 #include "sql/test/scoped_error_ignorer.h" |
| 18 #include "sql/test/test_helpers.h" | 19 #include "sql/test/test_helpers.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/sqlite/sqlite3.h" | 21 #include "third_party/sqlite/sqlite3.h" |
| 21 | 22 |
| 22 namespace sql { | 23 namespace sql { |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 explicit ScopedUmaskSetter(mode_t target_mask) { | 201 explicit ScopedUmaskSetter(mode_t target_mask) { |
| 201 old_umask_ = umask(target_mask); | 202 old_umask_ = umask(target_mask); |
| 202 } | 203 } |
| 203 ~ScopedUmaskSetter() { umask(old_umask_); } | 204 ~ScopedUmaskSetter() { umask(old_umask_); } |
| 204 private: | 205 private: |
| 205 mode_t old_umask_; | 206 mode_t old_umask_; |
| 206 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter); | 207 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter); |
| 207 }; | 208 }; |
| 208 #endif | 209 #endif |
| 209 | 210 |
| 210 class SQLConnectionTest : public testing::Test { | 211 class SQLConnectionTest : public sql::SQLTestBase { |
| 211 public: | 212 public: |
| 212 void SetUp() override { | 213 void SetUp() override { |
| 213 // Any macro histograms which fire before the recorder is initialized cannot | 214 // Any macro histograms which fire before the recorder is initialized cannot |
| 214 // be tested. So this needs to be ahead of Open(). | 215 // be tested. So this needs to be ahead of Open(). |
| 215 base::StatisticsRecorder::Initialize(); | 216 base::StatisticsRecorder::Initialize(); |
| 216 | 217 |
| 217 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 218 SQLTestBase::SetUp(); |
| 218 db_path_ = temp_dir_.path().AppendASCII("SQLConnectionTest.db"); | |
| 219 ASSERT_TRUE(db_.Open(db_path_)); | |
| 220 } | 219 } |
| 221 | 220 |
| 222 void TearDown() override { db_.Close(); } | |
| 223 | |
| 224 sql::Connection& db() { return db_; } | |
| 225 const base::FilePath& db_path() { return db_path_; } | |
| 226 | |
| 227 // Handle errors by blowing away the database. | 221 // Handle errors by blowing away the database. |
| 228 void RazeErrorCallback(int expected_error, int error, sql::Statement* stmt) { | 222 void RazeErrorCallback(int expected_error, int error, sql::Statement* stmt) { |
| 229 EXPECT_EQ(expected_error, error); | 223 EXPECT_EQ(expected_error, error); |
| 230 db_.RazeAndClose(); | 224 db().RazeAndClose(); |
| 231 } | 225 } |
| 232 | |
| 233 private: | |
| 234 sql::Connection db_; | |
| 235 base::FilePath db_path_; | |
| 236 base::ScopedTempDir temp_dir_; | |
| 237 }; | 226 }; |
| 238 | 227 |
| 239 TEST_F(SQLConnectionTest, Execute) { | 228 TEST_F(SQLConnectionTest, Execute) { |
| 240 // Valid statement should return true. | 229 // Valid statement should return true. |
| 241 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | 230 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 242 EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); | 231 EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); |
| 243 | 232 |
| 244 // Invalid statement should fail. | 233 // Invalid statement should fail. |
| 245 ASSERT_EQ(SQLITE_ERROR, | 234 ASSERT_EQ(SQLITE_ERROR, |
| 246 db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b")); | 235 db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b")); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 359 // with ScopedErrorIgnorer. | 348 // with ScopedErrorIgnorer. |
| 360 TEST_F(SQLConnectionTest, ScopedIgnoreUntracked) { | 349 TEST_F(SQLConnectionTest, ScopedIgnoreUntracked) { |
| 361 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; | 350 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
| 362 ASSERT_TRUE(db().Execute(kCreateSql)); | 351 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 363 ASSERT_FALSE(db().DoesTableExist("bar")); | 352 ASSERT_FALSE(db().DoesTableExist("bar")); |
| 364 ASSERT_TRUE(db().DoesTableExist("foo")); | 353 ASSERT_TRUE(db().DoesTableExist("foo")); |
| 365 ASSERT_TRUE(db().DoesColumnExist("foo", "id")); | 354 ASSERT_TRUE(db().DoesColumnExist("foo", "id")); |
| 366 db().Close(); | 355 db().Close(); |
| 367 | 356 |
| 368 // Corrupt the database so that nothing works, including PRAGMAs. | 357 // Corrupt the database so that nothing works, including PRAGMAs. |
| 369 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path())); | 358 ASSERT_TRUE(CorruptSizeInHeaderOfPath(db_path())); |
| 370 | 359 |
| 371 { | 360 { |
| 372 sql::ScopedErrorIgnorer ignore_errors; | 361 sql::ScopedErrorIgnorer ignore_errors; |
| 373 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 362 ignore_errors.IgnoreError(SQLITE_CORRUPT); |
| 374 ASSERT_TRUE(db().Open(db_path())); | 363 ASSERT_TRUE(db().Open(db_path())); |
| 375 ASSERT_FALSE(db().DoesTableExist("bar")); | 364 ASSERT_FALSE(db().DoesTableExist("bar")); |
| 376 ASSERT_FALSE(db().DoesTableExist("foo")); | 365 ASSERT_FALSE(db().DoesTableExist("foo")); |
| 377 ASSERT_FALSE(db().DoesColumnExist("foo", "id")); | 366 ASSERT_FALSE(db().DoesColumnExist("foo", "id")); |
| 378 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 367 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
| 379 } | 368 } |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 527 | 516 |
| 528 // Check that the second connection sees the table. | 517 // Check that the second connection sees the table. |
| 529 ASSERT_EQ(1, SqliteMasterCount(&other_db)); | 518 ASSERT_EQ(1, SqliteMasterCount(&other_db)); |
| 530 | 519 |
| 531 ASSERT_TRUE(db().Raze()); | 520 ASSERT_TRUE(db().Raze()); |
| 532 | 521 |
| 533 // The second connection sees the updated database. | 522 // The second connection sees the updated database. |
| 534 ASSERT_EQ(0, SqliteMasterCount(&other_db)); | 523 ASSERT_EQ(0, SqliteMasterCount(&other_db)); |
| 535 } | 524 } |
| 536 | 525 |
| 526 // TODO(erg): Enable this in the next patch once I add locking. | |
| 527 #if !defined(MOJO_APPTEST_IMPL) | |
| 537 TEST_F(SQLConnectionTest, RazeLocked) { | 528 TEST_F(SQLConnectionTest, RazeLocked) { |
| 538 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 529 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 539 ASSERT_TRUE(db().Execute(kCreateSql)); | 530 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 540 | 531 |
| 541 // Open a transaction and write some data in a second connection. | 532 // Open a transaction and write some data in a second connection. |
| 542 // This will acquire a PENDING or EXCLUSIVE transaction, which will | 533 // This will acquire a PENDING or EXCLUSIVE transaction, which will |
| 543 // cause the raze to fail. | 534 // cause the raze to fail. |
| 544 sql::Connection other_db; | 535 sql::Connection other_db; |
| 545 ASSERT_TRUE(other_db.Open(db_path())); | 536 ASSERT_TRUE(other_db.Open(db_path())); |
| 546 ASSERT_TRUE(other_db.BeginTransaction()); | 537 ASSERT_TRUE(other_db.BeginTransaction()); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 561 // blocks raze. | 552 // blocks raze. |
| 562 const char *kQuery = "SELECT COUNT(*) FROM foo"; | 553 const char *kQuery = "SELECT COUNT(*) FROM foo"; |
| 563 sql::Statement s(other_db.GetUniqueStatement(kQuery)); | 554 sql::Statement s(other_db.GetUniqueStatement(kQuery)); |
| 564 ASSERT_TRUE(s.Step()); | 555 ASSERT_TRUE(s.Step()); |
| 565 ASSERT_FALSE(db().Raze()); | 556 ASSERT_FALSE(db().Raze()); |
| 566 | 557 |
| 567 // Complete the statement unlocks the database. | 558 // Complete the statement unlocks the database. |
| 568 ASSERT_FALSE(s.Step()); | 559 ASSERT_FALSE(s.Step()); |
| 569 ASSERT_TRUE(db().Raze()); | 560 ASSERT_TRUE(db().Raze()); |
| 570 } | 561 } |
| 562 #endif | |
| 571 | 563 |
| 572 // Verify that Raze() can handle an empty file. SQLite should treat | 564 // Verify that Raze() can handle an empty file. SQLite should treat |
| 573 // this as an empty database. | 565 // this as an empty database. |
| 574 TEST_F(SQLConnectionTest, RazeEmptyDB) { | 566 TEST_F(SQLConnectionTest, RazeEmptyDB) { |
| 575 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 567 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 576 ASSERT_TRUE(db().Execute(kCreateSql)); | 568 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 577 db().Close(); | 569 db().Close(); |
| 578 | 570 |
| 579 { | 571 TruncateDatabase(); |
| 580 base::ScopedFILE file(base::OpenFile(db_path(), "rb+")); | |
| 581 ASSERT_TRUE(file.get() != NULL); | |
| 582 ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET)); | |
| 583 ASSERT_TRUE(base::TruncateFile(file.get())); | |
| 584 } | |
| 585 | 572 |
| 586 ASSERT_TRUE(db().Open(db_path())); | 573 ASSERT_TRUE(db().Open(db_path())); |
| 587 ASSERT_TRUE(db().Raze()); | 574 ASSERT_TRUE(db().Raze()); |
| 588 EXPECT_EQ(0, SqliteMasterCount(&db())); | 575 EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 589 } | 576 } |
| 590 | 577 |
| 591 // Verify that Raze() can handle a file of junk. | 578 // Verify that Raze() can handle a file of junk. |
| 592 TEST_F(SQLConnectionTest, RazeNOTADB) { | 579 TEST_F(SQLConnectionTest, RazeNOTADB) { |
| 593 db().Close(); | 580 db().Close(); |
| 594 sql::Connection::Delete(db_path()); | 581 sql::Connection::Delete(db_path()); |
| 595 ASSERT_FALSE(base::PathExists(db_path())); | 582 ASSERT_FALSE(GetPathExists(db_path())); |
| 596 | 583 |
| 597 { | 584 WriteJunkToDatabase(SQLTestBase::TYPE_TRUNCATE_AND_CREATE); |
| 598 base::ScopedFILE file(base::OpenFile(db_path(), "wb")); | 585 ASSERT_TRUE(GetPathExists(db_path())); |
| 599 ASSERT_TRUE(file.get() != NULL); | |
| 600 | |
| 601 const char* kJunk = "This is the hour of our discontent."; | |
| 602 fputs(kJunk, file.get()); | |
| 603 } | |
| 604 ASSERT_TRUE(base::PathExists(db_path())); | |
| 605 | 586 |
| 606 // SQLite will successfully open the handle, but fail when running PRAGMA | 587 // SQLite will successfully open the handle, but fail when running PRAGMA |
| 607 // statements that access the database. | 588 // statements that access the database. |
| 608 { | 589 { |
| 609 sql::ScopedErrorIgnorer ignore_errors; | 590 sql::ScopedErrorIgnorer ignore_errors; |
| 610 | 591 |
| 611 // Earlier versions of Chromium compiled against SQLite 3.6.7.3, which | 592 // Earlier versions of Chromium compiled against SQLite 3.6.7.3, which |
| 612 // returned SQLITE_IOERR_SHORT_READ in this case. Some platforms may still | 593 // returned SQLITE_IOERR_SHORT_READ in this case. Some platforms may still |
| 613 // compile against an earlier SQLite via USE_SYSTEM_SQLITE. | 594 // compile against an earlier SQLite via USE_SYSTEM_SQLITE. |
| 614 if (ignore_errors.SQLiteLibVersionNumber() < 3008005) { | 595 if (ignore_errors.SQLiteLibVersionNumber() < 3008005) { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 628 EXPECT_EQ(0, SqliteMasterCount(&db())); | 609 EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 629 } | 610 } |
| 630 | 611 |
| 631 // Verify that Raze() can handle a database overwritten with garbage. | 612 // Verify that Raze() can handle a database overwritten with garbage. |
| 632 TEST_F(SQLConnectionTest, RazeNOTADB2) { | 613 TEST_F(SQLConnectionTest, RazeNOTADB2) { |
| 633 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 614 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 634 ASSERT_TRUE(db().Execute(kCreateSql)); | 615 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 635 ASSERT_EQ(1, SqliteMasterCount(&db())); | 616 ASSERT_EQ(1, SqliteMasterCount(&db())); |
| 636 db().Close(); | 617 db().Close(); |
| 637 | 618 |
| 638 { | 619 WriteJunkToDatabase(SQLTestBase::TYPE_OPEN_AND_APPEND); |
|
Scott Hess - ex-Googler
2015/06/17 19:32:58
"wb" was truncate, but the "rb+" case is read/writ
Elliot Glaysher
2015/06/17 21:59:34
Changed name.
("TYPE_OPEN_AND_APPEND" actually op
| |
| 639 base::ScopedFILE file(base::OpenFile(db_path(), "rb+")); | |
| 640 ASSERT_TRUE(file.get() != NULL); | |
| 641 ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET)); | |
| 642 | |
| 643 const char* kJunk = "This is the hour of our discontent."; | |
| 644 fputs(kJunk, file.get()); | |
| 645 } | |
| 646 | 620 |
| 647 // SQLite will successfully open the handle, but will fail with | 621 // SQLite will successfully open the handle, but will fail with |
| 648 // SQLITE_NOTADB on pragma statemenets which attempt to read the | 622 // SQLITE_NOTADB on pragma statemenets which attempt to read the |
| 649 // corrupted header. | 623 // corrupted header. |
| 650 { | 624 { |
| 651 sql::ScopedErrorIgnorer ignore_errors; | 625 sql::ScopedErrorIgnorer ignore_errors; |
| 652 ignore_errors.IgnoreError(SQLITE_NOTADB); | 626 ignore_errors.IgnoreError(SQLITE_NOTADB); |
| 653 EXPECT_TRUE(db().Open(db_path())); | 627 EXPECT_TRUE(db().Open(db_path())); |
| 654 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 628 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
| 655 } | 629 } |
| 656 EXPECT_TRUE(db().Raze()); | 630 EXPECT_TRUE(db().Raze()); |
| 657 db().Close(); | 631 db().Close(); |
| 658 | 632 |
| 659 // Now empty, the open should succeed with an empty database. | 633 // Now empty, the open should succeed with an empty database. |
| 660 EXPECT_TRUE(db().Open(db_path())); | 634 EXPECT_TRUE(db().Open(db_path())); |
| 661 EXPECT_EQ(0, SqliteMasterCount(&db())); | 635 EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 662 } | 636 } |
| 663 | 637 |
| 664 // Test that a callback from Open() can raze the database. This is | 638 // Test that a callback from Open() can raze the database. This is |
| 665 // essential for cases where the Open() can fail entirely, so the | 639 // essential for cases where the Open() can fail entirely, so the |
| 666 // Raze() cannot happen later. Additionally test that when the | 640 // Raze() cannot happen later. Additionally test that when the |
| 667 // callback does this during Open(), the open is retried and succeeds. | 641 // callback does this during Open(), the open is retried and succeeds. |
| 668 TEST_F(SQLConnectionTest, RazeCallbackReopen) { | 642 TEST_F(SQLConnectionTest, RazeCallbackReopen) { |
| 669 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 643 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 670 ASSERT_TRUE(db().Execute(kCreateSql)); | 644 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 671 ASSERT_EQ(1, SqliteMasterCount(&db())); | 645 ASSERT_EQ(1, SqliteMasterCount(&db())); |
| 672 db().Close(); | 646 db().Close(); |
| 673 | 647 |
| 674 // Corrupt the database so that nothing works, including PRAGMAs. | 648 // Corrupt the database so that nothing works, including PRAGMAs. |
| 675 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path())); | 649 ASSERT_TRUE(CorruptSizeInHeaderOfPath(db_path())); |
| 676 | 650 |
| 677 // Open() will succeed, even though the PRAGMA calls within will | 651 // Open() will succeed, even though the PRAGMA calls within will |
| 678 // fail with SQLITE_CORRUPT, as will this PRAGMA. | 652 // fail with SQLITE_CORRUPT, as will this PRAGMA. |
| 679 { | 653 { |
| 680 sql::ScopedErrorIgnorer ignore_errors; | 654 sql::ScopedErrorIgnorer ignore_errors; |
| 681 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 655 ignore_errors.IgnoreError(SQLITE_CORRUPT); |
| 682 ASSERT_TRUE(db().Open(db_path())); | 656 ASSERT_TRUE(db().Open(db_path())); |
| 683 ASSERT_FALSE(db().Execute("PRAGMA auto_vacuum")); | 657 ASSERT_FALSE(db().Execute("PRAGMA auto_vacuum")); |
| 684 db().Close(); | 658 db().Close(); |
| 685 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 659 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 810 } | 784 } |
| 811 #endif | 785 #endif |
| 812 | 786 |
| 813 TEST_F(SQLConnectionTest, Delete) { | 787 TEST_F(SQLConnectionTest, Delete) { |
| 814 EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); | 788 EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); |
| 815 db().Close(); | 789 db().Close(); |
| 816 | 790 |
| 817 // Should have both a main database file and a journal file because | 791 // Should have both a main database file and a journal file because |
| 818 // of journal_mode TRUNCATE. | 792 // of journal_mode TRUNCATE. |
| 819 base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal")); | 793 base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal")); |
| 820 ASSERT_TRUE(base::PathExists(db_path())); | 794 ASSERT_TRUE(GetPathExists(db_path())); |
| 821 ASSERT_TRUE(base::PathExists(journal)); | 795 ASSERT_TRUE(GetPathExists(journal)); |
| 822 | 796 |
| 823 sql::Connection::Delete(db_path()); | 797 sql::Connection::Delete(db_path()); |
| 824 EXPECT_FALSE(base::PathExists(db_path())); | 798 EXPECT_FALSE(GetPathExists(db_path())); |
| 825 EXPECT_FALSE(base::PathExists(journal)); | 799 EXPECT_FALSE(GetPathExists(journal)); |
| 826 } | 800 } |
| 827 | 801 |
| 828 #if defined(OS_POSIX) | 802 // This test manually sets on disk permissions; this doesn't apply to the mojo |
| 803 // fork. | |
| 804 #if defined(OS_POSIX) && !defined(MOJO_APPTEST_IMPL) | |
| 829 // Test that set_restrict_to_user() trims database permissions so that | 805 // Test that set_restrict_to_user() trims database permissions so that |
| 830 // only the owner (and root) can read. | 806 // only the owner (and root) can read. |
| 831 TEST_F(SQLConnectionTest, UserPermission) { | 807 TEST_F(SQLConnectionTest, UserPermission) { |
| 832 // If the bots all had a restrictive umask setting such that | 808 // If the bots all had a restrictive umask setting such that |
| 833 // databases are always created with only the owner able to read | 809 // databases are always created with only the owner able to read |
| 834 // them, then the code could break without breaking the tests. | 810 // them, then the code could break without breaking the tests. |
| 835 // Temporarily provide a more permissive umask. | 811 // Temporarily provide a more permissive umask. |
| 836 db().Close(); | 812 db().Close(); |
| 837 sql::Connection::Delete(db_path()); | 813 sql::Connection::Delete(db_path()); |
| 838 ASSERT_FALSE(base::PathExists(db_path())); | 814 ASSERT_FALSE(GetPathExists(db_path())); |
| 839 ScopedUmaskSetter permissive_umask(S_IWGRP | S_IWOTH); | 815 ScopedUmaskSetter permissive_umask(S_IWGRP | S_IWOTH); |
| 840 ASSERT_TRUE(db().Open(db_path())); | 816 ASSERT_TRUE(db().Open(db_path())); |
| 841 | 817 |
| 842 // Cause the journal file to be created. If the default | 818 // Cause the journal file to be created. If the default |
| 843 // journal_mode is changed back to DELETE, then parts of this test | 819 // journal_mode is changed back to DELETE, then parts of this test |
| 844 // will need to be updated. | 820 // will need to be updated. |
| 845 EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); | 821 EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); |
| 846 | 822 |
| 847 base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal")); | 823 base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal")); |
| 848 int mode; | 824 int mode; |
| 849 | 825 |
| 850 // Given a permissive umask, the database is created with permissive | 826 // Given a permissive umask, the database is created with permissive |
| 851 // read access for the database and journal. | 827 // read access for the database and journal. |
| 852 ASSERT_TRUE(base::PathExists(db_path())); | 828 ASSERT_TRUE(GetPathExists(db_path())); |
| 853 ASSERT_TRUE(base::PathExists(journal)); | 829 ASSERT_TRUE(GetPathExists(journal)); |
| 854 mode = base::FILE_PERMISSION_MASK; | 830 mode = base::FILE_PERMISSION_MASK; |
| 855 EXPECT_TRUE(base::GetPosixFilePermissions(db_path(), &mode)); | 831 EXPECT_TRUE(base::GetPosixFilePermissions(db_path(), &mode)); |
| 856 ASSERT_NE((mode & base::FILE_PERMISSION_USER_MASK), mode); | 832 ASSERT_NE((mode & base::FILE_PERMISSION_USER_MASK), mode); |
| 857 mode = base::FILE_PERMISSION_MASK; | 833 mode = base::FILE_PERMISSION_MASK; |
| 858 EXPECT_TRUE(base::GetPosixFilePermissions(journal, &mode)); | 834 EXPECT_TRUE(base::GetPosixFilePermissions(journal, &mode)); |
| 859 ASSERT_NE((mode & base::FILE_PERMISSION_USER_MASK), mode); | 835 ASSERT_NE((mode & base::FILE_PERMISSION_USER_MASK), mode); |
| 860 | 836 |
| 861 // Re-open with restricted permissions and verify that the modes | 837 // Re-open with restricted permissions and verify that the modes |
| 862 // changed for both the main database and the journal. | 838 // changed for both the main database and the journal. |
| 863 db().Close(); | 839 db().Close(); |
| 864 db().set_restrict_to_user(); | 840 db().set_restrict_to_user(); |
| 865 ASSERT_TRUE(db().Open(db_path())); | 841 ASSERT_TRUE(db().Open(db_path())); |
| 866 ASSERT_TRUE(base::PathExists(db_path())); | 842 ASSERT_TRUE(GetPathExists(db_path())); |
| 867 ASSERT_TRUE(base::PathExists(journal)); | 843 ASSERT_TRUE(GetPathExists(journal)); |
| 868 mode = base::FILE_PERMISSION_MASK; | 844 mode = base::FILE_PERMISSION_MASK; |
| 869 EXPECT_TRUE(base::GetPosixFilePermissions(db_path(), &mode)); | 845 EXPECT_TRUE(base::GetPosixFilePermissions(db_path(), &mode)); |
| 870 ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode); | 846 ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode); |
| 871 mode = base::FILE_PERMISSION_MASK; | 847 mode = base::FILE_PERMISSION_MASK; |
| 872 EXPECT_TRUE(base::GetPosixFilePermissions(journal, &mode)); | 848 EXPECT_TRUE(base::GetPosixFilePermissions(journal, &mode)); |
| 873 ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode); | 849 ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode); |
| 874 | 850 |
| 875 // Delete and re-create the database, the restriction should still apply. | 851 // Delete and re-create the database, the restriction should still apply. |
| 876 db().Close(); | 852 db().Close(); |
| 877 sql::Connection::Delete(db_path()); | 853 sql::Connection::Delete(db_path()); |
| 878 ASSERT_TRUE(db().Open(db_path())); | 854 ASSERT_TRUE(db().Open(db_path())); |
| 879 ASSERT_TRUE(base::PathExists(db_path())); | 855 ASSERT_TRUE(GetPathExists(db_path())); |
| 880 ASSERT_FALSE(base::PathExists(journal)); | 856 ASSERT_FALSE(GetPathExists(journal)); |
| 881 mode = base::FILE_PERMISSION_MASK; | 857 mode = base::FILE_PERMISSION_MASK; |
| 882 EXPECT_TRUE(base::GetPosixFilePermissions(db_path(), &mode)); | 858 EXPECT_TRUE(base::GetPosixFilePermissions(db_path(), &mode)); |
| 883 ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode); | 859 ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode); |
| 884 | 860 |
| 885 // Verify that journal creation inherits the restriction. | 861 // Verify that journal creation inherits the restriction. |
| 886 EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); | 862 EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); |
| 887 ASSERT_TRUE(base::PathExists(journal)); | 863 ASSERT_TRUE(GetPathExists(journal)); |
| 888 mode = base::FILE_PERMISSION_MASK; | 864 mode = base::FILE_PERMISSION_MASK; |
| 889 EXPECT_TRUE(base::GetPosixFilePermissions(journal, &mode)); | 865 EXPECT_TRUE(base::GetPosixFilePermissions(journal, &mode)); |
| 890 ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode); | 866 ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode); |
| 891 } | 867 } |
| 892 #endif // defined(OS_POSIX) | 868 #endif // defined(OS_POSIX) |
| 893 | 869 |
| 894 // Test that errors start happening once Poison() is called. | 870 // Test that errors start happening once Poison() is called. |
| 895 TEST_F(SQLConnectionTest, Poison) { | 871 TEST_F(SQLConnectionTest, Poison) { |
| 896 EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); | 872 EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); |
| 897 | 873 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 983 | 959 |
| 984 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); | 960 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 985 } | 961 } |
| 986 | 962 |
| 987 TEST_F(SQLConnectionTest, Basic_QuickIntegrityCheck) { | 963 TEST_F(SQLConnectionTest, Basic_QuickIntegrityCheck) { |
| 988 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 964 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 989 ASSERT_TRUE(db().Execute(kCreateSql)); | 965 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 990 EXPECT_TRUE(db().QuickIntegrityCheck()); | 966 EXPECT_TRUE(db().QuickIntegrityCheck()); |
| 991 db().Close(); | 967 db().Close(); |
| 992 | 968 |
| 993 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path())); | 969 ASSERT_TRUE(CorruptSizeInHeaderOfPath(db_path())); |
| 994 | 970 |
| 995 { | 971 { |
| 996 sql::ScopedErrorIgnorer ignore_errors; | 972 sql::ScopedErrorIgnorer ignore_errors; |
| 997 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 973 ignore_errors.IgnoreError(SQLITE_CORRUPT); |
| 998 ASSERT_TRUE(db().Open(db_path())); | 974 ASSERT_TRUE(db().Open(db_path())); |
| 999 EXPECT_FALSE(db().QuickIntegrityCheck()); | 975 EXPECT_FALSE(db().QuickIntegrityCheck()); |
| 1000 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 976 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
| 1001 } | 977 } |
| 1002 } | 978 } |
| 1003 | 979 |
| 1004 TEST_F(SQLConnectionTest, Basic_FullIntegrityCheck) { | 980 TEST_F(SQLConnectionTest, Basic_FullIntegrityCheck) { |
| 1005 const std::string kOk("ok"); | 981 const std::string kOk("ok"); |
| 1006 std::vector<std::string> messages; | 982 std::vector<std::string> messages; |
| 1007 | 983 |
| 1008 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 984 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1009 ASSERT_TRUE(db().Execute(kCreateSql)); | 985 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 1010 EXPECT_TRUE(db().FullIntegrityCheck(&messages)); | 986 EXPECT_TRUE(db().FullIntegrityCheck(&messages)); |
| 1011 EXPECT_EQ(1u, messages.size()); | 987 EXPECT_EQ(1u, messages.size()); |
| 1012 EXPECT_EQ(kOk, messages[0]); | 988 EXPECT_EQ(kOk, messages[0]); |
| 1013 db().Close(); | 989 db().Close(); |
| 1014 | 990 |
| 1015 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path())); | 991 ASSERT_TRUE(CorruptSizeInHeaderOfPath(db_path())); |
| 1016 | 992 |
| 1017 { | 993 { |
| 1018 sql::ScopedErrorIgnorer ignore_errors; | 994 sql::ScopedErrorIgnorer ignore_errors; |
| 1019 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 995 ignore_errors.IgnoreError(SQLITE_CORRUPT); |
| 1020 ASSERT_TRUE(db().Open(db_path())); | 996 ASSERT_TRUE(db().Open(db_path())); |
| 1021 EXPECT_TRUE(db().FullIntegrityCheck(&messages)); | 997 EXPECT_TRUE(db().FullIntegrityCheck(&messages)); |
| 1022 EXPECT_LT(1u, messages.size()); | 998 EXPECT_LT(1u, messages.size()); |
| 1023 EXPECT_NE(kOk, messages[0]); | 999 EXPECT_NE(kOk, messages[0]); |
| 1024 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 1000 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
| 1025 } | 1001 } |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1317 samples = tester.GetHistogramSamplesSinceCreation(kCommitTime); | 1293 samples = tester.GetHistogramSamplesSinceCreation(kCommitTime); |
| 1318 ASSERT_TRUE(samples); | 1294 ASSERT_TRUE(samples); |
| 1319 // 100 for commit adjust, 1 for measuring COMMIT. | 1295 // 100 for commit adjust, 1 for measuring COMMIT. |
| 1320 EXPECT_EQ(101, samples->sum()); | 1296 EXPECT_EQ(101, samples->sum()); |
| 1321 | 1297 |
| 1322 samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime); | 1298 samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime); |
| 1323 EXPECT_TRUE(!samples || samples->sum() == 0); | 1299 EXPECT_TRUE(!samples || samples->sum() == 0); |
| 1324 } | 1300 } |
| 1325 | 1301 |
| 1326 } // namespace | 1302 } // namespace |
| OLD | NEW |