diff options
author | michaeln@chromium.org <michaeln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-16 18:36:52 +0000 |
---|---|---|
committer | michaeln@chromium.org <michaeln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-16 18:36:52 +0000 |
commit | 579446c1f83325c74e26e61b47a11c149e491a66 (patch) | |
tree | a297376e9fd53b8c24acb9b240ee880494c5567c /sql | |
parent | 3e0d61e2bb88eb80058b63ac6a8b9ebff64cc4eb (diff) | |
download | chromium_src-579446c1f83325c74e26e61b47a11c149e491a66.zip chromium_src-579446c1f83325c74e26e61b47a11c149e491a66.tar.gz chromium_src-579446c1f83325c74e26e61b47a11c149e491a66.tar.bz2 |
AppCache: Run a quick integrity check on the sqlite database when opening. If the test fails, delete the appcache directory and start afresh.
NOTRY=true
TBR=sky
BUG=318544
Review URL: https://codereview.chromium.org/104593010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@240937 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sql')
-rw-r--r-- | sql/connection.cc | 19 | ||||
-rw-r--r-- | sql/connection.h | 19 | ||||
-rw-r--r-- | sql/connection_unittest.cc | 44 |
3 files changed, 73 insertions, 9 deletions
diff --git a/sql/connection.cc b/sql/connection.cc index 6e02cd7..0caa9ed 100644 --- a/sql/connection.cc +++ b/sql/connection.cc @@ -1050,9 +1050,21 @@ int Connection::OnSqliteError(int err, sql::Statement *stmt, const char* sql) { return err; } -// TODO(shess): Allow specifying integrity_check versus quick_check. +bool Connection::FullIntegrityCheck(std::vector<std::string>* messages) { + return IntegrityCheckHelper("PRAGMA integrity_check", messages); +} + +bool Connection::QuickIntegrityCheck() { + std::vector<std::string> messages; + if (!IntegrityCheckHelper("PRAGMA quick_check", &messages)) + return false; + return messages.size() == 1 && messages[0] == "ok"; +} + // TODO(shess): Allow specifying maximum results (default 100 lines). -bool Connection::IntegrityCheck(std::vector<std::string>* messages) { +bool Connection::IntegrityCheckHelper( + const char* pragma_sql, + std::vector<std::string>* messages) { messages->clear(); // This has the side effect of setting SQLITE_RecoveryMode, which @@ -1065,8 +1077,7 @@ bool Connection::IntegrityCheck(std::vector<std::string>* messages) { bool ret = false; { - const char kSql[] = "PRAGMA integrity_check"; - sql::Statement stmt(GetUniqueStatement(kSql)); + sql::Statement stmt(GetUniqueStatement(pragma_sql)); // The pragma appears to return all results (up to 100 by default) // as a single string. This doesn't appear to be an API contract, diff --git a/sql/connection.h b/sql/connection.h index ec76e3b..54c3f8b 100644 --- a/sql/connection.h +++ b/sql/connection.h @@ -150,11 +150,16 @@ class SQL_EXPORT Connection { // histogram is recorded. void AddTaggedHistogram(const std::string& name, size_t sample) const; - // Run "PRAGMA integrity_check" and post each line of results into - // |messages|. Returns the success of running the statement - per - // the SQLite documentation, if no errors are found the call should - // succeed, and a single value "ok" should be in messages. - bool IntegrityCheck(std::vector<std::string>* messages); + // Run "PRAGMA integrity_check" and post each line of + // results into |messages|. Returns the success of running the + // statement - per the SQLite documentation, if no errors are found the + // call should succeed, and a single value "ok" should be in messages. + bool FullIntegrityCheck(std::vector<std::string>* messages); + + // Runs "PRAGMA quick_check" and, unlike the FullIntegrityCheck method, + // interprets the results returning true if the the statement executes + // without error and results in a single "ok" value. + bool QuickIntegrityCheck() WARN_UNUSED_RESULT; // Initialization ------------------------------------------------------------ @@ -540,6 +545,10 @@ class SQL_EXPORT Connection { // case for const functions). scoped_refptr<StatementRef> GetUntrackedStatement(const char* sql) const; + bool IntegrityCheckHelper( + const char* pragma_sql, + std::vector<std::string>* messages) WARN_UNUSED_RESULT; + // The actual sqlite database. Will be NULL before Init has been called or if // Init resulted in an error. sqlite3* db_; diff --git a/sql/connection_unittest.cc b/sql/connection_unittest.cc index 5aa7a9b..304ddcd 100644 --- a/sql/connection_unittest.cc +++ b/sql/connection_unittest.cc @@ -822,4 +822,48 @@ TEST_F(SQLConnectionTest, Attach) { EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); } +TEST_F(SQLConnectionTest, Basic_QuickIntegrityCheck) { + const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; + ASSERT_TRUE(db().Execute(kCreateSql)); + EXPECT_TRUE(db().QuickIntegrityCheck()); + db().Close(); + + ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path())); + + { + sql::ScopedErrorIgnorer ignore_errors; + ignore_errors.IgnoreError(SQLITE_CORRUPT); + ASSERT_TRUE(db().Open(db_path())); + EXPECT_FALSE(db().QuickIntegrityCheck()); + ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); + } +} + +TEST_F(SQLConnectionTest, Basic_FullIntegrityCheck) { + const std::string kOk("ok"); + std::vector<std::string> messages; + + const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; + ASSERT_TRUE(db().Execute(kCreateSql)); + EXPECT_TRUE(db().FullIntegrityCheck(&messages)); + EXPECT_EQ(1u, messages.size()); + EXPECT_EQ(kOk, messages[0]); + db().Close(); + + ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path())); + + { + sql::ScopedErrorIgnorer ignore_errors; + ignore_errors.IgnoreError(SQLITE_CORRUPT); + ASSERT_TRUE(db().Open(db_path())); + EXPECT_TRUE(db().FullIntegrityCheck(&messages)); + EXPECT_LT(1u, messages.size()); + EXPECT_NE(kOk, messages[0]); + ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); + } + + // TODO(shess): CorruptTableOrIndex could be used to produce a + // file that would pass the quick check and fail the full check. +} + } // namespace |