blob: 0e32164acdc782ca32927ee20c438a1ffbaa3387 [file] [log] [blame]
[email protected]43ffdd82013-09-10 23:44:501// 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
avi0b519202015-12-21 07:25:197#include <stddef.h>
8#include <stdint.h>
9
mostynbd82cd9952016-04-11 20:05:3410#include <memory>
[email protected]43ffdd82013-09-10 23:44:5011#include <string>
12
thestig22dfc4012014-09-05 08:29:4413#include "base/files/file_util.h"
[email protected]b9b4a572014-03-17 23:11:1214#include "base/files/scoped_file.h"
[email protected]43ffdd82013-09-10 23:44:5015#include "sql/connection.h"
16#include "sql/statement.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace {
20
21size_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]ae4f1622013-12-08 06:49:1229// Get page size for the database.
30bool 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.
39bool 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]a8848a72013-11-18 04:18:4749// Helper for reading a number from the SQLite header.
[email protected]d9806a972014-02-26 18:14:5750// See base/big_endian.h.
[email protected]a8848a72013-11-18 04:18:4751unsigned 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.
61void 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]43ffdd82013-09-10 23:44:5068} // namespace
69
70namespace sql {
71namespace test {
72
[email protected]a8848a72013-11-18 04:18:4773bool CorruptSizeInHeader(const base::FilePath& db_path) {
74 // See http://www.sqlite.org/fileformat.html#database_header
75 const size_t kHeaderSize = 100;
[email protected]a8848a72013-11-18 04:18:4776
77 unsigned char header[kHeaderSize];
78
[email protected]b9b4a572014-03-17 23:11:1279 base::ScopedFILE file(base::OpenFile(db_path, "rb+"));
[email protected]a8848a72013-11-18 04:18:4780 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
tfarina720d4f32015-05-11 22:31:2688 int64_t db_size = 0;
[email protected]56285702013-12-04 18:22:4989 if (!base::GetFileSize(db_path, &db_size))
[email protected]a8848a72013-11-18 04:18:4790 return false;
91
erg102ceb412015-06-20 01:38:1392 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
shess4640cc22016-10-19 22:47:13102bool 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
erg102ceb412015-06-20 01:38:13115void 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]a8848a72013-11-18 04:18:47121 const unsigned page_size = ReadBigEndian(header + kPageSizeOffset, 2);
122
123 // One larger than the expected size.
pkasting2209d5e2014-12-12 22:25:38124 const unsigned page_count =
125 static_cast<unsigned>((db_size + page_size) / page_size);
[email protected]a8848a72013-11-18 04:18:47126 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]a8848a72013-11-18 04:18:47133}
134
[email protected]ae4f1622013-12-08 06:49:12135bool 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;
mostynbd82cd9952016-04-11 20:05:34152 std::unique_ptr<char[]> page_buf(new char[page_size]);
[email protected]ae4f1622013-12-08 06:49:12153
154 // Get the page into page_buf.
[email protected]b9b4a572014-03-17 23:11:12155 base::ScopedFILE file(base::OpenFile(db_path, "rb+"));
[email protected]ae4f1622013-12-08 06:49:12156 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|.
mostynbd82cd9952016-04-11 20:05:34179 std::unique_ptr<char[]> check_page_buf(new char[page_size]);
[email protected]ae4f1622013-12-08 06:49:12180 // 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]43ffdd82013-09-10 23:44:50199size_t CountSQLTables(sql::Connection* db) {
200 return CountSQLItemsOfType(db, "table");
201}
202
203size_t CountSQLIndices(sql::Connection* db) {
204 return CountSQLItemsOfType(db, "index");
205}
206
207size_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]fe4e3de2013-10-08 02:19:17228bool 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
pkasting2209d5e2014-12-12 22:25:38238 *count = static_cast<size_t>(s.ColumnInt64(0));
[email protected]fe4e3de2013-10-08 02:19:17239 return true;
240}
241
[email protected]43ffdd82013-09-10 23:44:50242bool 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]29a4fd52013-10-16 05:49:05255 // 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]43ffdd82013-09-10 23:44:50261 return db.Execute(sql.c_str());
262}
263
[email protected]a8848a72013-11-18 04:18:47264std::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
shess1f955b182016-10-25 22:59:09273std::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
278std::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]43ffdd82013-09-10 23:44:50296} // namespace test
297} // namespace sql