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