File: test_helpers.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (139 lines) | stat: -rw-r--r-- 5,740 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SQL_TEST_TEST_HELPERS_H_
#define SQL_TEST_TEST_HELPERS_H_

#include <stddef.h>
#include <stdint.h>

#include <string>

#include "base/strings/string_piece_forward.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

// Collection of test-only convenience functions.

namespace base {
class FilePath;
}

namespace sql {
class Database;
}

namespace sql::test {

// Read a database's page size. Returns nullopt in case of error.
absl::optional<int> ReadDatabasePageSize(const base::FilePath& db_path);

// SQLite stores the database size in the header, and if the actual
// OS-derived size is smaller, the database is considered corrupt.
// [This case is actually a common form of corruption in the wild.]
// This helper sets the in-header size to one page larger than the
// actual file size.  The resulting file will return SQLITE_CORRUPT
// for most operations unless PRAGMA writable_schema is turned ON.
//
// This function operates on the raw database file, outstanding database
// connections may not see the change because of the database cache.  See
// CorruptSizeInHeaderWithLock().
//
// Returns false if any error occurs accessing the file.
[[nodiscard]] bool CorruptSizeInHeader(const base::FilePath& db_path);

// Call CorruptSizeInHeader() while holding a SQLite-compatible lock
// on the database.  This can be used to corrupt a database which is
// already open elsewhere.  Blocks until a write lock can be acquired.
[[nodiscard]] bool CorruptSizeInHeaderWithLock(const base::FilePath& db_path);

// Simulates total index corruption by zeroing the root page of an index B-tree.
//
// The corrupted database will still open successfully. SELECTs on the table
// associated with the index will work, as long as they don't access the index.
// However, any query that accesses the index will fail with SQLITE_CORRUPT.
// DROPping the table or the index will fail.
[[nodiscard]] bool CorruptIndexRootPage(const base::FilePath& db_path,
                                        base::StringPiece index_name);

// Return the number of tables in sqlite_schema.
[[nodiscard]] size_t CountSQLTables(sql::Database* db);

// Return the number of indices in sqlite_schema.
[[nodiscard]] size_t CountSQLIndices(sql::Database* db);

// Returns the number of columns in the named table.  0 indicates an
// error (probably no such table).
[[nodiscard]] size_t CountTableColumns(sql::Database* db, const char* table);

// Sets |*count| to the number of rows in |table|.  Returns false in
// case of error, such as the table not existing.
bool CountTableRows(sql::Database* db, const char* table, size_t* count);

// Creates a SQLite database at |db_path| from the sqlite .dump output
// at |sql_path|.  Returns false if |db_path| already exists, or if
// sql_path does not exist or cannot be read, or if there is an error
// executing the statements.
[[nodiscard]] bool CreateDatabaseFromSQL(const base::FilePath& db_path,
                                         const base::FilePath& sql_path);

// Test-friendly wrapper around sql::Database::IntegrityCheck().
[[nodiscard]] std::string IntegrityCheck(sql::Database& db);

// ExecuteWithResult() executes |sql| and returns the first column of the first
// row as a string.  The empty string is returned for no rows.  This makes it
// easier to test simple query results using EXPECT_EQ().  For instance:
//   EXPECT_EQ("1024", ExecuteWithResult(db, "PRAGMA page_size"));
//
// ExecuteWithResults() stringifies a larger result set by putting |column_sep|
// between columns and |row_sep| between rows.  For instance:
//   EXPECT_EQ("1,3,5", ExecuteWithResults(
//       db, "SELECT id FROM t ORDER BY id", "|", ","));
// Note that EXPECT_EQ() can nicely diff when using \n as |row_sep|.
//
// To test NULL, use the COALESCE() function:
//   EXPECT_EQ("<NULL>", ExecuteWithResult(
//       db, "SELECT c || '<NULL>' FROM t WHERE id = 1"));
// To test blobs use the HEX() function.
std::string ExecuteWithResult(sql::Database* db, const char* sql);
std::string ExecuteWithResults(sql::Database* db,
                               const char* sql,
                               const char* column_sep,
                               const char* row_sep);

// Returns the database size, in pages. Crashes on SQLite errors.
int GetPageCount(sql::Database* db);

// Column information returned by GetColumnInfo.
//
// C++ wrapper around the out-params of sqlite3_table_column_metadata().
struct ColumnInfo {
  // Retrieves schema information for a column in a table.
  //
  // Crashes on SQLite errors.
  //
  // |db_name| should be "main" for the connection's main (opened) database, and
  // "temp" for the connection's temporary (in-memory) database.
  //
  // This is a static method rather than a function so it can be listed in the
  // InternalApiToken access control list.
  [[nodiscard]] static ColumnInfo Create(sql::Database* db,
                                         const std::string& db_name,
                                         const std::string& table_name,
                                         const std::string& column_name);

  // The native data type. Example: "INTEGER".
  std::string data_type;
  // Default collation sequence for sorting. Example: "BINARY".
  std::string collation_sequence;
  // True if the column has a "NOT NULL" constraint.
  bool has_non_null_constraint;
  // True if the column is included in the table's PRIMARY KEY.
  bool is_in_primary_key;
  // True if the column is AUTOINCREMENT.
  bool is_auto_incremented;
};

}  // namespace sql::test

#endif  // SQL_TEST_TEST_HELPERS_H_