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 // Common implementation of CorruptSizeInHeader() which operates on loaded
37 // memory. Shared between CorruptSizeInHeader() and the the mojo proxy testing
39 void CorruptSizeInHeaderMemory(unsigned char* header
, int64_t db_size
);
41 // Frequently corruption is a result of failure to atomically update
42 // pages in different structures. For instance, if an index update
43 // takes effect but the corresponding table update does not. This
44 // helper restores the prior version of a b-tree root after running an
45 // update which changed that b-tree. The named b-tree must exist and
46 // must be a leaf node (either index or table). Returns true if the
47 // on-disk file is successfully modified, and the restored page
48 // differs from the updated page.
50 // The resulting database should be possible to open, and many
51 // statements should work. SQLITE_CORRUPT will be thrown if a query
52 // through the index finds the row missing in the table.
54 // TODO(shess): It would be very helpful to allow a parameter to the
55 // sql statement. Perhaps a version with a string parameter would be
56 // sufficient, given affinity rules?
57 bool CorruptTableOrIndex(const base::FilePath
& db_path
,
58 const char* tree_name
,
59 const char* update_sql
) WARN_UNUSED_RESULT
;
61 // Return the number of tables in sqlite_master.
62 size_t CountSQLTables(sql::Connection
* db
) WARN_UNUSED_RESULT
;
64 // Return the number of indices in sqlite_master.
65 size_t CountSQLIndices(sql::Connection
* db
) WARN_UNUSED_RESULT
;
67 // Returns the number of columns in the named table. 0 indicates an
68 // error (probably no such table).
69 size_t CountTableColumns(sql::Connection
* db
, const char* table
)
72 // Sets |*count| to the number of rows in |table|. Returns false in
73 // case of error, such as the table not existing.
74 bool CountTableRows(sql::Connection
* db
, const char* table
, size_t* count
);
76 // Creates a SQLite database at |db_path| from the sqlite .dump output
77 // at |sql_path|. Returns false if |db_path| already exists, or if
78 // sql_path does not exist or cannot be read, or if there is an error
79 // executing the statements.
80 bool CreateDatabaseFromSQL(const base::FilePath
& db_path
,
81 const base::FilePath
& sql_path
) WARN_UNUSED_RESULT
;
83 // Return the results of running "PRAGMA integrity_check" on |db|.
84 // TODO(shess): sql::Connection::IntegrityCheck() is basically the
85 // same, but not as convenient for testing. Maybe combine.
86 std::string
IntegrityCheck(sql::Connection
* db
) WARN_UNUSED_RESULT
;
91 #endif // SQL_TEST_TEST_HELPERS_H_