158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// found in the LICENSE file. 458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#ifndef SQL_TEST_TEST_HELPERS_H_ 658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#define SQL_TEST_TEST_HELPERS_H_ 758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include <string> 9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 1058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/basictypes.h" 1158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/compiler_specific.h" 12a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/files/file_path.h" 1358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 1458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Collection of test-only convenience functions. 1558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)namespace base { 1758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class FilePath; 1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} 1958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 2058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)namespace sql { 2158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class Connection; 2258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} 2358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 2458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)namespace sql { 2558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)namespace test { 2658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// SQLite stores the database size in the header, and if the actual 28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// OS-derived size is smaller, the database is considered corrupt. 29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// [This case is actually a common form of corruption in the wild.] 30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// This helper sets the in-header size to one page larger than the 31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// actual file size. The resulting file will return SQLITE_CORRUPT 32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// for most operations unless PRAGMA writable_schema is turned ON. 33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// 34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Returns false if any error occurs accessing the file. 35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool CorruptSizeInHeader(const base::FilePath& db_path) WARN_UNUSED_RESULT; 36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Frequently corruption is a result of failure to atomically update 38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// pages in different structures. For instance, if an index update 39a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// takes effect but the corresponding table update does not. This 40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// helper restores the prior version of a b-tree root after running an 41a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// update which changed that b-tree. The named b-tree must exist and 42a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// must be a leaf node (either index or table). Returns true if the 43a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// on-disk file is successfully modified, and the restored page 44a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// differs from the updated page. 45a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// 46a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// The resulting database should be possible to open, and many 47a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// statements should work. SQLITE_CORRUPT will be thrown if a query 48a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// through the index finds the row missing in the table. 49a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// 50a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// TODO(shess): It would be very helpful to allow a parameter to the 51a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// sql statement. Perhaps a version with a string parameter would be 52a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// sufficient, given affinity rules? 53a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)bool CorruptTableOrIndex(const base::FilePath& db_path, 54a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const char* tree_name, 55a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const char* update_sql) WARN_UNUSED_RESULT; 56a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 5758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Return the number of tables in sqlite_master. 5858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)size_t CountSQLTables(sql::Connection* db) WARN_UNUSED_RESULT; 5958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 6058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Return the number of indices in sqlite_master. 6158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)size_t CountSQLIndices(sql::Connection* db) WARN_UNUSED_RESULT; 6258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 6358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Returns the number of columns in the named table. 0 indicates an 6458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// error (probably no such table). 6558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)size_t CountTableColumns(sql::Connection* db, const char* table) 6658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) WARN_UNUSED_RESULT; 6758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Sets |*count| to the number of rows in |table|. Returns false in 694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// case of error, such as the table not existing. 704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool CountTableRows(sql::Connection* db, const char* table, size_t* count); 714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 7258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Creates a SQLite database at |db_path| from the sqlite .dump output 7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// at |sql_path|. Returns false if |db_path| already exists, or if 7458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// sql_path does not exist or cannot be read, or if there is an error 7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// executing the statements. 7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool CreateDatabaseFromSQL(const base::FilePath& db_path, 7758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const base::FilePath& sql_path) WARN_UNUSED_RESULT; 7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Return the results of running "PRAGMA integrity_check" on |db|. 80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// TODO(shess): sql::Connection::IntegrityCheck() is basically the 81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// same, but not as convenient for testing. Maybe combine. 82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)std::string IntegrityCheck(sql::Connection* db) WARN_UNUSED_RESULT; 83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 8458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} // namespace test 8558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} // namespace sql 8658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 8758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif // SQL_TEST_TEST_HELPERS_H_ 88