OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sql/test/test_helpers.h" | 5 #include "sql/test/test_helpers.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "sql/connection.h" | 10 #include "sql/connection.h" |
11 #include "sql/statement.h" | 11 #include "sql/statement.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 size_t CountSQLItemsOfType(sql::Connection* db, const char* type) { | 16 size_t CountSQLItemsOfType(sql::Connection* db, const char* type) { |
17 const char kTypeSQL[] = "SELECT COUNT(*) FROM sqlite_master WHERE type = ?"; | 17 const char kTypeSQL[] = "SELECT COUNT(*) FROM sqlite_master WHERE type = ?"; |
18 sql::Statement s(db->GetUniqueStatement(kTypeSQL)); | 18 sql::Statement s(db->GetUniqueStatement(kTypeSQL)); |
19 s.BindCString(0, type); | 19 s.BindCString(0, type); |
20 EXPECT_TRUE(s.Step()); | 20 EXPECT_TRUE(s.Step()); |
21 return s.ColumnInt(0); | 21 return s.ColumnInt(0); |
22 } | 22 } |
23 | 23 |
| 24 // Helper for reading a number from the SQLite header. |
| 25 // See net/base/big_endian.h. |
| 26 unsigned ReadBigEndian(unsigned char* buf, size_t bytes) { |
| 27 unsigned r = buf[0]; |
| 28 for (size_t i = 1; i < bytes; i++) { |
| 29 r <<= 8; |
| 30 r |= buf[i]; |
| 31 } |
| 32 return r; |
| 33 } |
| 34 |
| 35 // Helper for writing a number to the SQLite header. |
| 36 void WriteBigEndian(unsigned val, unsigned char* buf, size_t bytes) { |
| 37 for (size_t i = 0; i < bytes; i++) { |
| 38 buf[bytes - i - 1] = (val & 0xFF); |
| 39 val >>= 8; |
| 40 } |
| 41 } |
| 42 |
24 } // namespace | 43 } // namespace |
25 | 44 |
26 namespace sql { | 45 namespace sql { |
27 namespace test { | 46 namespace test { |
28 | 47 |
| 48 bool CorruptSizeInHeader(const base::FilePath& db_path) { |
| 49 // See http://www.sqlite.org/fileformat.html#database_header |
| 50 const size_t kHeaderSize = 100; |
| 51 const size_t kPageSizeOffset = 16; |
| 52 const size_t kFileChangeCountOffset = 24; |
| 53 const size_t kPageCountOffset = 28; |
| 54 const size_t kVersionValidForOffset = 92; // duplicate kFileChangeCountOffset |
| 55 |
| 56 unsigned char header[kHeaderSize]; |
| 57 |
| 58 file_util::ScopedFILE file(file_util::OpenFile(db_path, "rb+")); |
| 59 if (!file.get()) |
| 60 return false; |
| 61 |
| 62 if (0 != fseek(file.get(), 0, SEEK_SET)) |
| 63 return false; |
| 64 if (1u != fread(header, sizeof(header), 1, file.get())) |
| 65 return false; |
| 66 |
| 67 int64 db_size = 0; |
| 68 if (!file_util::GetFileSize(db_path, &db_size)) |
| 69 return false; |
| 70 |
| 71 const unsigned page_size = ReadBigEndian(header + kPageSizeOffset, 2); |
| 72 |
| 73 // One larger than the expected size. |
| 74 const unsigned page_count = (db_size + page_size) / page_size; |
| 75 WriteBigEndian(page_count, header + kPageCountOffset, 4); |
| 76 |
| 77 // Update change count so outstanding readers know the info changed. |
| 78 // Both spots must match for the page count to be considered valid. |
| 79 unsigned change_count = ReadBigEndian(header + kFileChangeCountOffset, 4); |
| 80 WriteBigEndian(change_count + 1, header + kFileChangeCountOffset, 4); |
| 81 WriteBigEndian(change_count + 1, header + kVersionValidForOffset, 4); |
| 82 |
| 83 if (0 != fseek(file.get(), 0, SEEK_SET)) |
| 84 return false; |
| 85 if (1u != fwrite(header, sizeof(header), 1, file.get())) |
| 86 return false; |
| 87 |
| 88 return true; |
| 89 } |
| 90 |
29 size_t CountSQLTables(sql::Connection* db) { | 91 size_t CountSQLTables(sql::Connection* db) { |
30 return CountSQLItemsOfType(db, "table"); | 92 return CountSQLItemsOfType(db, "table"); |
31 } | 93 } |
32 | 94 |
33 size_t CountSQLIndices(sql::Connection* db) { | 95 size_t CountSQLIndices(sql::Connection* db) { |
34 return CountSQLItemsOfType(db, "index"); | 96 return CountSQLItemsOfType(db, "index"); |
35 } | 97 } |
36 | 98 |
37 size_t CountTableColumns(sql::Connection* db, const char* table) { | 99 size_t CountTableColumns(sql::Connection* db, const char* table) { |
38 // TODO(shess): sql::Connection::QuoteForSQL() would make sense. | 100 // TODO(shess): sql::Connection::QuoteForSQL() would make sense. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 | 146 |
85 // TODO(shess): Android defaults to auto_vacuum mode. | 147 // TODO(shess): Android defaults to auto_vacuum mode. |
86 // Unfortunately, this makes certain kinds of tests which manipulate | 148 // Unfortunately, this makes certain kinds of tests which manipulate |
87 // the raw database hard/impossible to write. | 149 // the raw database hard/impossible to write. |
88 // http://crbug.com/307303 is for exploring this test issue. | 150 // http://crbug.com/307303 is for exploring this test issue. |
89 ignore_result(db.Execute("PRAGMA auto_vacuum = 0")); | 151 ignore_result(db.Execute("PRAGMA auto_vacuum = 0")); |
90 | 152 |
91 return db.Execute(sql.c_str()); | 153 return db.Execute(sql.c_str()); |
92 } | 154 } |
93 | 155 |
| 156 std::string IntegrityCheck(sql::Connection* db) { |
| 157 sql::Statement statement(db->GetUniqueStatement("PRAGMA integrity_check")); |
| 158 |
| 159 // SQLite should always return a row of data. |
| 160 EXPECT_TRUE(statement.Step()); |
| 161 |
| 162 return statement.ColumnString(0); |
| 163 } |
| 164 |
94 } // namespace test | 165 } // namespace test |
95 } // namespace sql | 166 } // namespace sql |
OLD | NEW |