Chromium Code Reviews| 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_MOJO_SQL_TEST_BASE_H_ | |
| 6 #define SQL_MOJO_SQL_TEST_BASE_H_ | |
| 7 | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "components/filesystem/public/interfaces/file_system.mojom.h" | |
| 11 #include "mojo/application/public/cpp/application_test_base.h" | |
| 12 #include "sql/connection.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 namespace sql { | |
| 16 | |
| 17 class Connection; | |
| 18 class ScopedMojoFilesystemVFS; | |
| 19 | |
| 20 // Base class for SQL tests. | |
| 21 // | |
| 22 // WARNING: We want to run the same gtest based unit test code both against | |
| 23 // chromium (which uses this implementation here), and the mojo code (which | |
| 24 // uses a different class named SQLTestBase). These two classes need to have | |
| 25 // the same interface because we compile time switch them based on a | |
| 26 // #define. We need to have two different implementations because the mojo | |
| 27 // version derives from mojo::test::ApplicationTestBase instead of | |
| 28 // testing::Test. | |
| 29 class SQLTestBase : public mojo::test::ApplicationTestBase { | |
| 30 public: | |
| 31 SQLTestBase(); | |
| 32 ~SQLTestBase() override; | |
| 33 | |
| 34 enum WriteJunkType { | |
| 35 TYPE_TRUNCATE_AND_CREATE, | |
| 36 TYPE_OPEN_NORMALLY | |
|
Scott Hess - ex-Googler
2015/06/19 20:48:02
How about TYPE_OVERWRITE and TYPE_OVERWRITE_AND_TR
| |
| 37 }; | |
| 38 | |
| 39 // Returns the path to the database. | |
| 40 base::FilePath db_path(); | |
| 41 | |
| 42 // Returns a connection to the database at db_path(). | |
| 43 sql::Connection& db(); | |
| 44 | |
| 45 // Closes the current connection to the database and reopens it. | |
| 46 bool Reopen(); | |
| 47 | |
| 48 // Proxying method around base::PathExists. | |
| 49 bool GetPathExists(const base::FilePath& path); | |
| 50 | |
| 51 // SQLite stores the database size in the header, and if the actual | |
| 52 // OS-derived size is smaller, the database is considered corrupt. | |
| 53 // [This case is actually a common form of corruption in the wild.] | |
| 54 // This helper sets the in-header size to one page larger than the | |
| 55 // actual file size. The resulting file will return SQLITE_CORRUPT | |
| 56 // for most operations unless PRAGMA writable_schema is turned ON. | |
| 57 // | |
| 58 // Returns false if any error occurs accessing the file. | |
| 59 bool CorruptSizeInHeaderOfDB(); | |
| 60 | |
| 61 // Writes junk to the start of the file. | |
| 62 void WriteJunkToDatabase(WriteJunkType type); | |
| 63 | |
| 64 // Sets the database file size to 0. | |
| 65 void TruncateDatabase(); | |
| 66 | |
| 67 // Overridden from testing::Test: | |
| 68 void SetUp() override; | |
| 69 void TearDown() override; | |
| 70 | |
| 71 protected: | |
| 72 filesystem::FileSystemPtr& files() { return files_; } | |
| 73 | |
| 74 private: | |
| 75 filesystem::FileSystemPtr files_; | |
| 76 | |
| 77 scoped_ptr<ScopedMojoFilesystemVFS> vfs_; | |
| 78 sql::Connection db_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(SQLTestBase); | |
| 81 }; | |
| 82 | |
| 83 } // namespace sql | |
| 84 | |
| 85 #endif // SQL_MOJO_SQL_TEST_BASE_H_ | |
| OLD | NEW |