[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 | #include "sql/test/test_helpers.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 9 | #include "base/files/file_util.h" |
[email protected] | b9b4a57 | 2014-03-17 23:11:12 | [diff] [blame] | 10 | #include "base/files/scoped_file.h" |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 11 | #include "sql/connection.h" |
| 12 | #include "sql/statement.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | size_t CountSQLItemsOfType(sql::Connection* db, const char* type) { |
| 18 | const char kTypeSQL[] = "SELECT COUNT(*) FROM sqlite_master WHERE type = ?"; |
| 19 | sql::Statement s(db->GetUniqueStatement(kTypeSQL)); |
| 20 | s.BindCString(0, type); |
| 21 | EXPECT_TRUE(s.Step()); |
| 22 | return s.ColumnInt(0); |
| 23 | } |
| 24 | |
[email protected] | ae4f162 | 2013-12-08 06:49:12 | [diff] [blame] | 25 | // Get page size for the database. |
| 26 | bool GetPageSize(sql::Connection* db, int* page_size) { |
| 27 | sql::Statement s(db->GetUniqueStatement("PRAGMA page_size")); |
| 28 | if (!s.Step()) |
| 29 | return false; |
| 30 | *page_size = s.ColumnInt(0); |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | // Get |name|'s root page number in the database. |
| 35 | bool GetRootPage(sql::Connection* db, const char* name, int* page_number) { |
| 36 | const char kPageSql[] = "SELECT rootpage FROM sqlite_master WHERE name = ?"; |
| 37 | sql::Statement s(db->GetUniqueStatement(kPageSql)); |
| 38 | s.BindString(0, name); |
| 39 | if (!s.Step()) |
| 40 | return false; |
| 41 | *page_number = s.ColumnInt(0); |
| 42 | return true; |
| 43 | } |
| 44 | |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 45 | // Helper for reading a number from the SQLite header. |
[email protected] | d9806a97 | 2014-02-26 18:14:57 | [diff] [blame] | 46 | // See base/big_endian.h. |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 47 | unsigned ReadBigEndian(unsigned char* buf, size_t bytes) { |
| 48 | unsigned r = buf[0]; |
| 49 | for (size_t i = 1; i < bytes; i++) { |
| 50 | r <<= 8; |
| 51 | r |= buf[i]; |
| 52 | } |
| 53 | return r; |
| 54 | } |
| 55 | |
| 56 | // Helper for writing a number to the SQLite header. |
| 57 | void WriteBigEndian(unsigned val, unsigned char* buf, size_t bytes) { |
| 58 | for (size_t i = 0; i < bytes; i++) { |
| 59 | buf[bytes - i - 1] = (val & 0xFF); |
| 60 | val >>= 8; |
| 61 | } |
| 62 | } |
| 63 | |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 64 | } // namespace |
| 65 | |
| 66 | namespace sql { |
| 67 | namespace test { |
| 68 | |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 69 | bool CorruptSizeInHeader(const base::FilePath& db_path) { |
| 70 | // See http://www.sqlite.org/fileformat.html#database_header |
| 71 | const size_t kHeaderSize = 100; |
| 72 | const size_t kPageSizeOffset = 16; |
| 73 | const size_t kFileChangeCountOffset = 24; |
| 74 | const size_t kPageCountOffset = 28; |
| 75 | const size_t kVersionValidForOffset = 92; // duplicate kFileChangeCountOffset |
| 76 | |
| 77 | unsigned char header[kHeaderSize]; |
| 78 | |
[email protected] | b9b4a57 | 2014-03-17 23:11:12 | [diff] [blame] | 79 | base::ScopedFILE file(base::OpenFile(db_path, "rb+")); |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 80 | if (!file.get()) |
| 81 | return false; |
| 82 | |
| 83 | if (0 != fseek(file.get(), 0, SEEK_SET)) |
| 84 | return false; |
| 85 | if (1u != fread(header, sizeof(header), 1, file.get())) |
| 86 | return false; |
| 87 | |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame^] | 88 | int64_t db_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 89 | if (!base::GetFileSize(db_path, &db_size)) |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 90 | return false; |
| 91 | |
| 92 | const unsigned page_size = ReadBigEndian(header + kPageSizeOffset, 2); |
| 93 | |
| 94 | // One larger than the expected size. |
pkasting | 2209d5e | 2014-12-12 22:25:38 | [diff] [blame] | 95 | const unsigned page_count = |
| 96 | static_cast<unsigned>((db_size + page_size) / page_size); |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 97 | WriteBigEndian(page_count, header + kPageCountOffset, 4); |
| 98 | |
| 99 | // Update change count so outstanding readers know the info changed. |
| 100 | // Both spots must match for the page count to be considered valid. |
| 101 | unsigned change_count = ReadBigEndian(header + kFileChangeCountOffset, 4); |
| 102 | WriteBigEndian(change_count + 1, header + kFileChangeCountOffset, 4); |
| 103 | WriteBigEndian(change_count + 1, header + kVersionValidForOffset, 4); |
| 104 | |
| 105 | if (0 != fseek(file.get(), 0, SEEK_SET)) |
| 106 | return false; |
| 107 | if (1u != fwrite(header, sizeof(header), 1, file.get())) |
| 108 | return false; |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | |
[email protected] | ae4f162 | 2013-12-08 06:49:12 | [diff] [blame] | 113 | bool CorruptTableOrIndex(const base::FilePath& db_path, |
| 114 | const char* tree_name, |
| 115 | const char* update_sql) { |
| 116 | sql::Connection db; |
| 117 | if (!db.Open(db_path)) |
| 118 | return false; |
| 119 | |
| 120 | int page_size = 0; |
| 121 | if (!GetPageSize(&db, &page_size)) |
| 122 | return false; |
| 123 | |
| 124 | int page_number = 0; |
| 125 | if (!GetRootPage(&db, tree_name, &page_number)) |
| 126 | return false; |
| 127 | |
| 128 | // SQLite uses 1-based page numbering. |
| 129 | const long int page_ofs = (page_number - 1) * page_size; |
| 130 | scoped_ptr<char[]> page_buf(new char[page_size]); |
| 131 | |
| 132 | // Get the page into page_buf. |
[email protected] | b9b4a57 | 2014-03-17 23:11:12 | [diff] [blame] | 133 | base::ScopedFILE file(base::OpenFile(db_path, "rb+")); |
[email protected] | ae4f162 | 2013-12-08 06:49:12 | [diff] [blame] | 134 | if (!file.get()) |
| 135 | return false; |
| 136 | if (0 != fseek(file.get(), page_ofs, SEEK_SET)) |
| 137 | return false; |
| 138 | if (1u != fread(page_buf.get(), page_size, 1, file.get())) |
| 139 | return false; |
| 140 | |
| 141 | // Require the page to be a leaf node. A multilevel tree would be |
| 142 | // very hard to restore correctly. |
| 143 | if (page_buf[0] != 0xD && page_buf[0] != 0xA) |
| 144 | return false; |
| 145 | |
| 146 | // The update has to work, and make changes. |
| 147 | if (!db.Execute(update_sql)) |
| 148 | return false; |
| 149 | if (db.GetLastChangeCount() == 0) |
| 150 | return false; |
| 151 | |
| 152 | // Ensure that the database is fully flushed. |
| 153 | db.Close(); |
| 154 | |
| 155 | // Check that the stored page actually changed. This catches usage |
| 156 | // errors where |update_sql| is not related to |tree_name|. |
| 157 | scoped_ptr<char[]> check_page_buf(new char[page_size]); |
| 158 | // The on-disk data should have changed. |
| 159 | if (0 != fflush(file.get())) |
| 160 | return false; |
| 161 | if (0 != fseek(file.get(), page_ofs, SEEK_SET)) |
| 162 | return false; |
| 163 | if (1u != fread(check_page_buf.get(), page_size, 1, file.get())) |
| 164 | return false; |
| 165 | if (!memcmp(check_page_buf.get(), page_buf.get(), page_size)) |
| 166 | return false; |
| 167 | |
| 168 | // Put the original page back. |
| 169 | if (0 != fseek(file.get(), page_ofs, SEEK_SET)) |
| 170 | return false; |
| 171 | if (1u != fwrite(page_buf.get(), page_size, 1, file.get())) |
| 172 | return false; |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 177 | size_t CountSQLTables(sql::Connection* db) { |
| 178 | return CountSQLItemsOfType(db, "table"); |
| 179 | } |
| 180 | |
| 181 | size_t CountSQLIndices(sql::Connection* db) { |
| 182 | return CountSQLItemsOfType(db, "index"); |
| 183 | } |
| 184 | |
| 185 | size_t CountTableColumns(sql::Connection* db, const char* table) { |
| 186 | // TODO(shess): sql::Connection::QuoteForSQL() would make sense. |
| 187 | std::string quoted_table; |
| 188 | { |
| 189 | const char kQuoteSQL[] = "SELECT quote(?)"; |
| 190 | sql::Statement s(db->GetUniqueStatement(kQuoteSQL)); |
| 191 | s.BindCString(0, table); |
| 192 | EXPECT_TRUE(s.Step()); |
| 193 | quoted_table = s.ColumnString(0); |
| 194 | } |
| 195 | |
| 196 | std::string sql = "PRAGMA table_info(" + quoted_table + ")"; |
| 197 | sql::Statement s(db->GetUniqueStatement(sql.c_str())); |
| 198 | size_t rows = 0; |
| 199 | while (s.Step()) { |
| 200 | ++rows; |
| 201 | } |
| 202 | EXPECT_TRUE(s.Succeeded()); |
| 203 | return rows; |
| 204 | } |
| 205 | |
[email protected] | fe4e3de | 2013-10-08 02:19:17 | [diff] [blame] | 206 | bool CountTableRows(sql::Connection* db, const char* table, size_t* count) { |
| 207 | // TODO(shess): Table should probably be quoted with [] or "". See |
| 208 | // http://www.sqlite.org/lang_keywords.html . Meanwhile, odd names |
| 209 | // will throw an error. |
| 210 | std::string sql = "SELECT COUNT(*) FROM "; |
| 211 | sql += table; |
| 212 | sql::Statement s(db->GetUniqueStatement(sql.c_str())); |
| 213 | if (!s.Step()) |
| 214 | return false; |
| 215 | |
pkasting | 2209d5e | 2014-12-12 22:25:38 | [diff] [blame] | 216 | *count = static_cast<size_t>(s.ColumnInt64(0)); |
[email protected] | fe4e3de | 2013-10-08 02:19:17 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 220 | bool CreateDatabaseFromSQL(const base::FilePath& db_path, |
| 221 | const base::FilePath& sql_path) { |
| 222 | if (base::PathExists(db_path) || !base::PathExists(sql_path)) |
| 223 | return false; |
| 224 | |
| 225 | std::string sql; |
| 226 | if (!base::ReadFileToString(sql_path, &sql)) |
| 227 | return false; |
| 228 | |
| 229 | sql::Connection db; |
| 230 | if (!db.Open(db_path)) |
| 231 | return false; |
| 232 | |
[email protected] | 29a4fd5 | 2013-10-16 05:49:05 | [diff] [blame] | 233 | // TODO(shess): Android defaults to auto_vacuum mode. |
| 234 | // Unfortunately, this makes certain kinds of tests which manipulate |
| 235 | // the raw database hard/impossible to write. |
| 236 | // http://crbug.com/307303 is for exploring this test issue. |
| 237 | ignore_result(db.Execute("PRAGMA auto_vacuum = 0")); |
| 238 | |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 239 | return db.Execute(sql.c_str()); |
| 240 | } |
| 241 | |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 242 | std::string IntegrityCheck(sql::Connection* db) { |
| 243 | sql::Statement statement(db->GetUniqueStatement("PRAGMA integrity_check")); |
| 244 | |
| 245 | // SQLite should always return a row of data. |
| 246 | EXPECT_TRUE(statement.Step()); |
| 247 | |
| 248 | return statement.ColumnString(0); |
| 249 | } |
| 250 | |
[email protected] | 43ffdd8 | 2013-09-10 23:44:50 | [diff] [blame] | 251 | } // namespace test |
| 252 | } // namespace sql |