blob: a9b455d6bb8abe87d54d0586b599e0ab3eb3b9bd [file] [log] [blame]
[email protected]43ffdd82013-09-10 23:44:501// 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
avi0b519202015-12-21 07:25:198#include <stddef.h>
9#include <stdint.h>
10
[email protected]a8848a72013-11-18 04:18:4711#include <string>
12
Victor Costan63b2c4c42022-02-26 03:43:4413#include "base/strings/string_piece_forward.h"
Victor Costan335b8222022-03-22 08:15:1114#include "third_party/abseil-cpp/absl/types/optional.h"
[email protected]43ffdd82013-09-10 23:44:5015
16// Collection of test-only convenience functions.
17
18namespace base {
19class FilePath;
20}
21
22namespace sql {
Victor Costancfbfa602018-08-01 23:24:4623class Database;
[email protected]43ffdd82013-09-10 23:44:5024}
25
Victor Costan63b2c4c42022-02-26 03:43:4426namespace sql::test {
[email protected]43ffdd82013-09-10 23:44:5027
Victor Costan335b8222022-03-22 08:15:1128// Read a database's page size. Returns nullopt in case of error.
29absl::optional<int> ReadDatabasePageSize(const base::FilePath& db_path);
30
[email protected]a8848a72013-11-18 04:18:4731// 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//
shess4640cc22016-10-19 22:47:1338// 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]a8848a72013-11-18 04:18:4742// Returns false if any error occurs accessing the file.
Daniel Cheng1ac0cad2022-01-14 00:19:5343[[nodiscard]] bool CorruptSizeInHeader(const base::FilePath& db_path);
[email protected]a8848a72013-11-18 04:18:4744
shess4640cc22016-10-19 22:47:1345// 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 Cheng1ac0cad2022-01-14 00:19:5348[[nodiscard]] bool CorruptSizeInHeaderWithLock(const base::FilePath& db_path);
shess4640cc22016-10-19 22:47:1349
Victor Costan63b2c4c42022-02-26 03:43:4450// Simulates total index corruption by zeroing the root page of an index B-tree.
[email protected]ae4f1622013-12-08 06:49:1251//
Victor Costan63b2c4c42022-02-26 03:43:4452// 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]ae4f1622013-12-08 06:49:1258
John Delaney86dbec62021-08-24 15:05:2159// Return the number of tables in sqlite_schema.
Daniel Cheng1ac0cad2022-01-14 00:19:5360[[nodiscard]] size_t CountSQLTables(sql::Database* db);
[email protected]43ffdd82013-09-10 23:44:5061
John Delaney86dbec62021-08-24 15:05:2162// Return the number of indices in sqlite_schema.
Daniel Cheng1ac0cad2022-01-14 00:19:5363[[nodiscard]] size_t CountSQLIndices(sql::Database* db);
[email protected]43ffdd82013-09-10 23:44:5064
65// Returns the number of columns in the named table. 0 indicates an
66// error (probably no such table).
Daniel Cheng1ac0cad2022-01-14 00:19:5367[[nodiscard]] size_t CountTableColumns(sql::Database* db, const char* table);
[email protected]43ffdd82013-09-10 23:44:5068
[email protected]fe4e3de2013-10-08 02:19:1769// Sets |*count| to the number of rows in |table|. Returns false in
70// case of error, such as the table not existing.
Victor Costancfbfa602018-08-01 23:24:4671bool CountTableRows(sql::Database* db, const char* table, size_t* count);
[email protected]fe4e3de2013-10-08 02:19:1772
[email protected]43ffdd82013-09-10 23:44:5073// 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 Cheng1ac0cad2022-01-14 00:19:5377[[nodiscard]] bool CreateDatabaseFromSQL(const base::FilePath& db_path,
78 const base::FilePath& sql_path);
[email protected]43ffdd82013-09-10 23:44:5079
Victor Costane06e6842022-03-09 20:36:0580// Test-friendly wrapper around sql::Database::IntegrityCheck().
81[[nodiscard]] std::string IntegrityCheck(sql::Database& db);
[email protected]a8848a72013-11-18 04:18:4782
shess1f955b182016-10-25 22:59:0983// 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 Costancfbfa602018-08-01 23:24:4698std::string ExecuteWithResult(sql::Database* db, const char* sql);
99std::string ExecuteWithResults(sql::Database* db,
shess1f955b182016-10-25 22:59:09100 const char* sql,
101 const char* column_sep,
102 const char* row_sep);
103
Victor Costan455989b2019-05-13 21:43:15104// Returns the database size, in pages. Crashes on SQLite errors.
105int GetPageCount(sql::Database* db);
106
107// Column information returned by GetColumnInfo.
108//
109// C++ wrapper around the out-params of sqlite3_table_column_metadata().
110struct 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 Cheng1ac0cad2022-01-14 00:19:53120 [[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 Costan455989b2019-05-13 21:43:15124
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 Costan63b2c4c42022-02-26 03:43:44137} // namespace sql::test
[email protected]43ffdd82013-09-10 23:44:50138
139#endif // SQL_TEST_TEST_HELPERS_H_