1 // Copyright 2013 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_TEST_HELPERS_H_
6 #define SQL_TEST_TEST_HELPERS_H_
10 #include "base/compiler_specific.h"
11 #include "base/files/file_path.h"
13 // Collection of test-only convenience functions.
26 // SQLite stores the database size in the header, and if the actual
27 // OS-derived size is smaller, the database is considered corrupt.
28 // [This case is actually a common form of corruption in the wild.]
29 // This helper sets the in-header size to one page larger than the
30 // actual file size. The resulting file will return SQLITE_CORRUPT
31 // for most operations unless PRAGMA writable_schema is turned ON.
33 // Returns false if any error occurs accessing the file.
34 bool CorruptSizeInHeader(const base::FilePath
& db_path
) WARN_UNUSED_RESULT
;
36 // Frequently corruption is a result of failure to atomically update
37 // pages in different structures. For instance, if an index update
38 // takes effect but the corresponding table update does not. This
39 // helper restores the prior version of a b-tree root after running an
40 // update which changed that b-tree. The named b-tree must exist and
41 // must be a leaf node (either index or table). Returns true if the
42 // on-disk file is successfully modified, and the restored page
43 // differs from the updated page.
45 // The resulting database should be possible to open, and many
46 // statements should work. SQLITE_CORRUPT will be thrown if a query
47 // through the index finds the row missing in the table.
49 // TODO(shess): It would be very helpful to allow a parameter to the
50 // sql statement. Perhaps a version with a string parameter would be
51 // sufficient, given affinity rules?
52 bool CorruptTableOrIndex(const base::FilePath
& db_path
,
53 const char* tree_name
,
54 const char* update_sql
) WARN_UNUSED_RESULT
;
56 // Return the number of tables in sqlite_master.
57 size_t CountSQLTables(sql::Connection
* db
) WARN_UNUSED_RESULT
;
59 // Return the number of indices in sqlite_master.
60 size_t CountSQLIndices(sql::Connection
* db
) WARN_UNUSED_RESULT
;
62 // Returns the number of columns in the named table. 0 indicates an
63 // error (probably no such table).
64 size_t CountTableColumns(sql::Connection
* db
, const char* table
)
67 // Sets |*count| to the number of rows in |table|. Returns false in
68 // case of error, such as the table not existing.
69 bool CountTableRows(sql::Connection
* db
, const char* table
, size_t* count
);
71 // Creates a SQLite database at |db_path| from the sqlite .dump output
72 // at |sql_path|. Returns false if |db_path| already exists, or if
73 // sql_path does not exist or cannot be read, or if there is an error
74 // executing the statements.
75 bool CreateDatabaseFromSQL(const base::FilePath
& db_path
,
76 const base::FilePath
& sql_path
) WARN_UNUSED_RESULT
;
78 // Return the results of running "PRAGMA integrity_check" on |db|.
79 // TODO(shess): sql::Connection::IntegrityCheck() is basically the
80 // same, but not as convenient for testing. Maybe combine.
81 std::string
IntegrityCheck(sql::Connection
* db
) WARN_UNUSED_RESULT
;
86 #endif // SQL_TEST_TEST_HELPERS_H_