OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef SQL_TEST_SQL_TEST_BASE_H_ |
| 6 #define SQL_TEST_SQL_TEST_BASE_H_ |
| 7 |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "sql/connection.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace sql { |
| 15 |
| 16 class Connection; |
| 17 |
| 18 // Base class for SQL tests. |
| 19 // |
| 20 // WARNING: We want to run the same gtest based unit test code both against |
| 21 // chromium (which uses this implementation here), and the mojo code (which |
| 22 // uses a different class named SQLTestBase). These two classes need to have |
| 23 // the same interface because we compile time switch them based on a |
| 24 // #define. We need to have two different implementations because the mojo |
| 25 // version derives from mojo::test::ApplicationTestBase instead of |
| 26 // testing::Test. |
| 27 class SQLTestBase : public testing::Test { |
| 28 public: |
| 29 SQLTestBase(); |
| 30 ~SQLTestBase() override; |
| 31 |
| 32 enum WriteJunkType { |
| 33 TYPE_OVERWRITE_AND_TRUNCATE, |
| 34 TYPE_OVERWRITE |
| 35 }; |
| 36 |
| 37 // Returns the path to the database. |
| 38 base::FilePath db_path(); |
| 39 |
| 40 // Returns a connection to the database at db_path(). |
| 41 sql::Connection& db(); |
| 42 |
| 43 // Closes the current connection to the database and reopens it. |
| 44 bool Reopen(); |
| 45 |
| 46 // Proxying method around base::PathExists. |
| 47 bool GetPathExists(const base::FilePath& path); |
| 48 |
| 49 // SQLite stores the database size in the header, and if the actual |
| 50 // OS-derived size is smaller, the database is considered corrupt. |
| 51 // [This case is actually a common form of corruption in the wild.] |
| 52 // This helper sets the in-header size to one page larger than the |
| 53 // actual file size. The resulting file will return SQLITE_CORRUPT |
| 54 // for most operations unless PRAGMA writable_schema is turned ON. |
| 55 // |
| 56 // Returns false if any error occurs accessing the file. |
| 57 bool CorruptSizeInHeaderOfDB(); |
| 58 |
| 59 // Writes junk to the start of the file. |
| 60 void WriteJunkToDatabase(WriteJunkType type); |
| 61 |
| 62 // Sets the database file size to 0. |
| 63 void TruncateDatabase(); |
| 64 |
| 65 // Overridden from testing::Test: |
| 66 void SetUp() override; |
| 67 void TearDown() override; |
| 68 |
| 69 private: |
| 70 base::ScopedTempDir temp_dir_; |
| 71 sql::Connection db_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(SQLTestBase); |
| 74 }; |
| 75 |
| 76 } // namespace sql |
| 77 |
| 78 #endif // SQL_TEST_SQL_TEST_BASE_H_ |
OLD | NEW |