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