[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef SQL_TEST_TEST_HELPERS_H_ |
| 6 | #define SQL_TEST_TEST_HELPERS_H_ |
| 7 | |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 11 | #include <string> |
| 12 | |
Victor Costan | 63b2c4c4 | 2022-02-26 03:43:44 | [diff] [blame] | 13 | #include "base/strings/string_piece_forward.h" |
Victor Costan | 335b822 | 2022-03-22 08:15:11 | [diff] [blame] | 14 | #include "third_party/abseil-cpp/absl/types/optional.h" |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 15 | |
| 16 | // Collection of test-only convenience functions. |
| 17 | |
| 18 | namespace base { |
| 19 | class FilePath; |
| 20 | } |
| 21 | |
| 22 | namespace sql { |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 23 | class Database; |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 24 | } |
| 25 | |
Victor Costan | 63b2c4c4 | 2022-02-26 03:43:44 | [diff] [blame] | 26 | namespace sql::test { |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 27 | |
Victor Costan | 335b822 | 2022-03-22 08:15:11 | [diff] [blame] | 28 | // Read a database's page size. Returns nullopt in case of error. |
| 29 | absl::optional<int> ReadDatabasePageSize(const base::FilePath& db_path); |
| 30 | |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 31 | // SQLite stores the database size in the header, and if the actual |
| 32 | // OS-derived size is smaller, the database is considered corrupt. |
| 33 | // [This case is actually a common form of corruption in the wild.] |
| 34 | // This helper sets the in-header size to one page larger than the |
| 35 | // actual file size. The resulting file will return SQLITE_CORRUPT |
| 36 | // for most operations unless PRAGMA writable_schema is turned ON. |
| 37 | // |
shess | 4640cc2 | 2016-10-19 22:47:13 | [diff] [blame] | 38 | // This function operates on the raw database file, outstanding database |
| 39 | // connections may not see the change because of the database cache. See |
| 40 | // CorruptSizeInHeaderWithLock(). |
| 41 | // |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 42 | // Returns false if any error occurs accessing the file. |
Daniel Cheng | 1ac0cad | 2022-01-14 00:19:53 | [diff] [blame] | 43 | [[nodiscard]] bool CorruptSizeInHeader(const base::FilePath& db_path); |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 44 | |
shess | 4640cc2 | 2016-10-19 22:47:13 | [diff] [blame] | 45 | // Call CorruptSizeInHeader() while holding a SQLite-compatible lock |
| 46 | // on the database. This can be used to corrupt a database which is |
| 47 | // already open elsewhere. Blocks until a write lock can be acquired. |
Daniel Cheng | 1ac0cad | 2022-01-14 00:19:53 | [diff] [blame] | 48 | [[nodiscard]] bool CorruptSizeInHeaderWithLock(const base::FilePath& db_path); |
shess | 4640cc2 | 2016-10-19 22:47:13 | [diff] [blame] | 49 | |
Victor Costan | 63b2c4c4 | 2022-02-26 03:43:44 | [diff] [blame] | 50 | // Simulates total index corruption by zeroing the root page of an index B-tree. |
[email protected] | ae4f162 | 2013-12-08 06:49:12 | [diff] [blame] | 51 | // |
Victor Costan | 63b2c4c4 | 2022-02-26 03:43:44 | [diff] [blame] | 52 | // The corrupted database will still open successfully. SELECTs on the table |
| 53 | // associated with the index will work, as long as they don't access the index. |
| 54 | // However, any query that accesses the index will fail with SQLITE_CORRUPT. |
| 55 | // DROPping the table or the index will fail. |
| 56 | [[nodiscard]] bool CorruptIndexRootPage(const base::FilePath& db_path, |
| 57 | base::StringPiece index_name); |
[email protected] | ae4f162 | 2013-12-08 06:49:12 | [diff] [blame] | 58 | |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 59 | // Return the number of tables in sqlite_schema. |
Daniel Cheng | 1ac0cad | 2022-01-14 00:19:53 | [diff] [blame] | 60 | [[nodiscard]] size_t CountSQLTables(sql::Database* db); |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 61 | |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 62 | // Return the number of indices in sqlite_schema. |
Daniel Cheng | 1ac0cad | 2022-01-14 00:19:53 | [diff] [blame] | 63 | [[nodiscard]] size_t CountSQLIndices(sql::Database* db); |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 64 | |
| 65 | // Returns the number of columns in the named table. 0 indicates an |
| 66 | // error (probably no such table). |
Daniel Cheng | 1ac0cad | 2022-01-14 00:19:53 | [diff] [blame] | 67 | [[nodiscard]] size_t CountTableColumns(sql::Database* db, const char* table); |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 68 | |
[email protected] | fe4e3de | 2013-10-08 02:19:17 | [diff] [blame] | 69 | // Sets |*count| to the number of rows in |table|. Returns false in |
| 70 | // case of error, such as the table not existing. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 71 | bool CountTableRows(sql::Database* db, const char* table, size_t* count); |
[email protected] | fe4e3de | 2013-10-08 02:19:17 | [diff] [blame] | 72 | |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 73 | // Creates a SQLite database at |db_path| from the sqlite .dump output |
| 74 | // at |sql_path|. Returns false if |db_path| already exists, or if |
| 75 | // sql_path does not exist or cannot be read, or if there is an error |
| 76 | // executing the statements. |
Daniel Cheng | 1ac0cad | 2022-01-14 00:19:53 | [diff] [blame] | 77 | [[nodiscard]] bool CreateDatabaseFromSQL(const base::FilePath& db_path, |
| 78 | const base::FilePath& sql_path); |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 79 | |
Victor Costan | e06e684 | 2022-03-09 20:36:05 | [diff] [blame] | 80 | // Test-friendly wrapper around sql::Database::IntegrityCheck(). |
| 81 | [[nodiscard]] std::string IntegrityCheck(sql::Database& db); |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 82 | |
shess | 1f955b18 | 2016-10-25 22:59:09 | [diff] [blame] | 83 | // ExecuteWithResult() executes |sql| and returns the first column of the first |
| 84 | // row as a string. The empty string is returned for no rows. This makes it |
| 85 | // easier to test simple query results using EXPECT_EQ(). For instance: |
| 86 | // EXPECT_EQ("1024", ExecuteWithResult(db, "PRAGMA page_size")); |
| 87 | // |
| 88 | // ExecuteWithResults() stringifies a larger result set by putting |column_sep| |
| 89 | // between columns and |row_sep| between rows. For instance: |
| 90 | // EXPECT_EQ("1,3,5", ExecuteWithResults( |
| 91 | // db, "SELECT id FROM t ORDER BY id", "|", ",")); |
| 92 | // Note that EXPECT_EQ() can nicely diff when using \n as |row_sep|. |
| 93 | // |
| 94 | // To test NULL, use the COALESCE() function: |
| 95 | // EXPECT_EQ("<NULL>", ExecuteWithResult( |
| 96 | // db, "SELECT c || '<NULL>' FROM t WHERE id = 1")); |
| 97 | // To test blobs use the HEX() function. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 98 | std::string ExecuteWithResult(sql::Database* db, const char* sql); |
| 99 | std::string ExecuteWithResults(sql::Database* db, |
shess | 1f955b18 | 2016-10-25 22:59:09 | [diff] [blame] | 100 | const char* sql, |
| 101 | const char* column_sep, |
| 102 | const char* row_sep); |
| 103 | |
Victor Costan | 455989b | 2019-05-13 21:43:15 | [diff] [blame] | 104 | // Returns the database size, in pages. Crashes on SQLite errors. |
| 105 | int GetPageCount(sql::Database* db); |
| 106 | |
| 107 | // Column information returned by GetColumnInfo. |
| 108 | // |
| 109 | // C++ wrapper around the out-params of sqlite3_table_column_metadata(). |
| 110 | struct ColumnInfo { |
| 111 | // Retrieves schema information for a column in a table. |
| 112 | // |
| 113 | // Crashes on SQLite errors. |
| 114 | // |
| 115 | // |db_name| should be "main" for the connection's main (opened) database, and |
| 116 | // "temp" for the connection's temporary (in-memory) database. |
| 117 | // |
| 118 | // This is a static method rather than a function so it can be listed in the |
| 119 | // InternalApiToken access control list. |
Daniel Cheng | 1ac0cad | 2022-01-14 00:19:53 | [diff] [blame] | 120 | [[nodiscard]] static ColumnInfo Create(sql::Database* db, |
| 121 | const std::string& db_name, |
| 122 | const std::string& table_name, |
| 123 | const std::string& column_name); |
Victor Costan | 455989b | 2019-05-13 21:43:15 | [diff] [blame] | 124 | |
| 125 | // The native data type. Example: "INTEGER". |
| 126 | std::string data_type; |
| 127 | // Default collation sequence for sorting. Example: "BINARY". |
| 128 | std::string collation_sequence; |
| 129 | // True if the column has a "NOT NULL" constraint. |
| 130 | bool has_non_null_constraint; |
| 131 | // True if the column is included in the table's PRIMARY KEY. |
| 132 | bool is_in_primary_key; |
| 133 | // True if the column is AUTOINCREMENT. |
| 134 | bool is_auto_incremented; |
| 135 | }; |
| 136 | |
Victor Costan | 63b2c4c4 | 2022-02-26 03:43:44 | [diff] [blame] | 137 | } // namespace sql::test |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 138 | |
| 139 | #endif // SQL_TEST_TEST_HELPERS_H_ |