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.
5 #ifndef SQL_TEST_SQL_TEST_BASE_H_
6 #define SQL_TEST_SQL_TEST_BASE_H_
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"
18 // Base class for SQL tests.
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
27 class SQLTestBase
: public testing::Test
{
30 ~SQLTestBase() override
;
33 TYPE_OVERWRITE_AND_TRUNCATE
,
37 // Returns the path to the database.
38 base::FilePath
db_path();
40 // Returns a connection to the database at db_path().
41 sql::Connection
& db();
43 // Closes the current connection to the database and reopens it.
46 // Proxying method around base::PathExists.
47 bool GetPathExists(const base::FilePath
& path
);
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.
56 // Returns false if any error occurs accessing the file.
57 bool CorruptSizeInHeaderOfDB();
59 // Writes junk to the start of the file.
60 void WriteJunkToDatabase(WriteJunkType type
);
62 // Sets the database file size to 0.
63 void TruncateDatabase();
65 // Overridden from testing::Test:
66 void SetUp() override
;
67 void TearDown() override
;
70 base::ScopedTempDir temp_dir_
;
73 DISALLOW_COPY_AND_ASSIGN(SQLTestBase
);
78 #endif // SQL_TEST_SQL_TEST_BASE_H_