[email protected] | 44ad7d90 | 2012-03-23 00:09:05 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 8 | #include "base/bind.h" |
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] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 11 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 12 | #include "base/logging.h" |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 13 | #include "base/macros.h" |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
Devlin Cronin | 147687f | 2018-06-05 18:03:56 | [diff] [blame^] | 15 | #include "base/test/metrics/histogram_tester.h" |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 16 | #include "base/trace_event/process_memory_dump.h" |
Scott Graham | 57ee5482 | 2017-09-13 06:37:56 | [diff] [blame] | 17 | #include "build/build_config.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 18 | #include "sql/connection.h" |
ssid | 3be5b1ec | 2016-01-13 14:21:57 | [diff] [blame] | 19 | #include "sql/connection_memory_dump_provider.h" |
[email protected] | 1348765a | 2012-07-24 08:25:53 | [diff] [blame] | 20 | #include "sql/meta_table.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 21 | #include "sql/statement.h" |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 22 | #include "sql/test/error_callback_support.h" |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 23 | #include "sql/test/scoped_error_expecter.h" |
Scott Graham | 47ed2c3 | 2017-09-15 02:17:07 | [diff] [blame] | 24 | #include "sql/test/sql_test_base.h" |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 25 | #include "sql/test/test_helpers.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 26 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 27 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 28 | |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 29 | namespace sql { |
| 30 | namespace test { |
| 31 | |
| 32 | // Replaces the database time source with an object that steps forward 1ms on |
| 33 | // each check, and which can be jumped forward an arbitrary amount of time |
| 34 | // programmatically. |
| 35 | class ScopedMockTimeSource { |
| 36 | public: |
| 37 | ScopedMockTimeSource(Connection& db) |
| 38 | : db_(db), |
| 39 | delta_(base::TimeDelta::FromMilliseconds(1)) { |
| 40 | // Save the current source and replace it. |
| 41 | save_.swap(db_.clock_); |
| 42 | db_.clock_.reset(new MockTimeSource(*this)); |
| 43 | } |
| 44 | ~ScopedMockTimeSource() { |
| 45 | // Put original source back. |
| 46 | db_.clock_.swap(save_); |
| 47 | } |
| 48 | |
| 49 | void adjust(const base::TimeDelta& delta) { |
| 50 | current_time_ += delta; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | class MockTimeSource : public TimeSource { |
| 55 | public: |
| 56 | MockTimeSource(ScopedMockTimeSource& owner) |
| 57 | : owner_(owner) { |
| 58 | } |
Chris Watkins | cf617255 | 2017-11-27 03:25:18 | [diff] [blame] | 59 | ~MockTimeSource() override = default; |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 60 | |
| 61 | base::TimeTicks Now() override { |
| 62 | base::TimeTicks ret(owner_.current_time_); |
| 63 | owner_.current_time_ += owner_.delta_; |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | ScopedMockTimeSource& owner_; |
| 69 | DISALLOW_COPY_AND_ASSIGN(MockTimeSource); |
| 70 | }; |
| 71 | |
| 72 | Connection& db_; |
| 73 | |
| 74 | // Saves original source from |db_|. |
mostynb | d82cd995 | 2016-04-11 20:05:34 | [diff] [blame] | 75 | std::unique_ptr<TimeSource> save_; |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 76 | |
| 77 | // Current time returned by mock. |
| 78 | base::TimeTicks current_time_; |
| 79 | |
| 80 | // How far to jump on each Now() call. |
| 81 | base::TimeDelta delta_; |
| 82 | |
| 83 | DISALLOW_COPY_AND_ASSIGN(ScopedMockTimeSource); |
| 84 | }; |
| 85 | |
| 86 | // Allow a test to add a SQLite function in a scoped context. |
| 87 | class ScopedScalarFunction { |
| 88 | public: |
| 89 | ScopedScalarFunction( |
| 90 | sql::Connection& db, |
| 91 | const char* function_name, |
| 92 | int args, |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 93 | base::RepeatingCallback<void(sqlite3_context*, int, sqlite3_value**)> cb) |
| 94 | : db_(db.db_), function_name_(function_name), cb_(std::move(cb)) { |
shess | 6ce9d1f0 | 2015-09-02 19:37:43 | [diff] [blame] | 95 | sqlite3_create_function_v2(db_, function_name, args, SQLITE_UTF8, |
| 96 | this, &Run, NULL, NULL, NULL); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 97 | } |
| 98 | ~ScopedScalarFunction() { |
shess | 6ce9d1f0 | 2015-09-02 19:37:43 | [diff] [blame] | 99 | sqlite3_create_function_v2(db_, function_name_, 0, SQLITE_UTF8, |
| 100 | NULL, NULL, NULL, NULL, NULL); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private: |
| 104 | static void Run(sqlite3_context* context, int argc, sqlite3_value** argv) { |
| 105 | ScopedScalarFunction* t = static_cast<ScopedScalarFunction*>( |
| 106 | sqlite3_user_data(context)); |
| 107 | t->cb_.Run(context, argc, argv); |
| 108 | } |
| 109 | |
| 110 | sqlite3* db_; |
| 111 | const char* function_name_; |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 112 | base::RepeatingCallback<void(sqlite3_context*, int, sqlite3_value**)> cb_; |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 113 | |
| 114 | DISALLOW_COPY_AND_ASSIGN(ScopedScalarFunction); |
| 115 | }; |
| 116 | |
| 117 | // Allow a test to add a SQLite commit hook in a scoped context. |
| 118 | class ScopedCommitHook { |
| 119 | public: |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 120 | ScopedCommitHook(sql::Connection& db, base::RepeatingCallback<int()> cb) |
| 121 | : db_(db.db_), cb_(std::move(cb)) { |
shess | 6ce9d1f0 | 2015-09-02 19:37:43 | [diff] [blame] | 122 | sqlite3_commit_hook(db_, &Run, this); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 123 | } |
| 124 | ~ScopedCommitHook() { |
shess | 6ce9d1f0 | 2015-09-02 19:37:43 | [diff] [blame] | 125 | sqlite3_commit_hook(db_, NULL, NULL); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | private: |
| 129 | static int Run(void* p) { |
| 130 | ScopedCommitHook* t = static_cast<ScopedCommitHook*>(p); |
| 131 | return t->cb_.Run(); |
| 132 | } |
| 133 | |
| 134 | sqlite3* db_; |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 135 | base::RepeatingCallback<int(void)> cb_; |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 136 | |
| 137 | DISALLOW_COPY_AND_ASSIGN(ScopedCommitHook); |
| 138 | }; |
| 139 | |
| 140 | } // namespace test |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 141 | |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 142 | namespace { |
| 143 | |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 144 | using sql::test::ExecuteWithResults; |
| 145 | using sql::test::ExecuteWithResult; |
| 146 | |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 147 | // Helper to return the count of items in sqlite_master. Return -1 in |
| 148 | // case of error. |
| 149 | int SqliteMasterCount(sql::Connection* db) { |
| 150 | const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master"; |
| 151 | sql::Statement s(db->GetUniqueStatement(kMasterCount)); |
| 152 | return s.Step() ? s.ColumnInt(0) : -1; |
| 153 | } |
| 154 | |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 155 | // Track the number of valid references which share the same pointer. |
| 156 | // This is used to allow testing an implicitly use-after-free case by |
| 157 | // explicitly having the ref count live longer than the object. |
| 158 | class RefCounter { |
| 159 | public: |
| 160 | RefCounter(size_t* counter) |
| 161 | : counter_(counter) { |
| 162 | (*counter_)++; |
| 163 | } |
| 164 | RefCounter(const RefCounter& other) |
| 165 | : counter_(other.counter_) { |
| 166 | (*counter_)++; |
| 167 | } |
| 168 | ~RefCounter() { |
| 169 | (*counter_)--; |
| 170 | } |
| 171 | |
| 172 | private: |
| 173 | size_t* counter_; |
| 174 | |
| 175 | DISALLOW_ASSIGN(RefCounter); |
| 176 | }; |
| 177 | |
| 178 | // Empty callback for implementation of ErrorCallbackSetHelper(). |
| 179 | void IgnoreErrorCallback(int error, sql::Statement* stmt) { |
| 180 | } |
| 181 | |
| 182 | void ErrorCallbackSetHelper(sql::Connection* db, |
| 183 | size_t* counter, |
| 184 | const RefCounter& r, |
| 185 | int error, sql::Statement* stmt) { |
| 186 | // The ref count should not go to zero when changing the callback. |
| 187 | EXPECT_GT(*counter, 0u); |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 188 | db->set_error_callback(base::BindRepeating(&IgnoreErrorCallback)); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 189 | EXPECT_GT(*counter, 0u); |
| 190 | } |
| 191 | |
| 192 | void ErrorCallbackResetHelper(sql::Connection* db, |
| 193 | size_t* counter, |
| 194 | const RefCounter& r, |
| 195 | int error, sql::Statement* stmt) { |
| 196 | // The ref count should not go to zero when clearing the callback. |
| 197 | EXPECT_GT(*counter, 0u); |
| 198 | db->reset_error_callback(); |
| 199 | EXPECT_GT(*counter, 0u); |
| 200 | } |
| 201 | |
shess | 1cf87f2 | 2016-10-25 22:18:29 | [diff] [blame] | 202 | // Handle errors by blowing away the database. |
| 203 | void RazeErrorCallback(sql::Connection* db, |
| 204 | int expected_error, |
| 205 | int error, |
| 206 | sql::Statement* stmt) { |
| 207 | // Nothing here needs extended errors at this time. |
| 208 | EXPECT_EQ(expected_error, expected_error&0xff); |
| 209 | EXPECT_EQ(expected_error, error&0xff); |
| 210 | db->RazeAndClose(); |
| 211 | } |
| 212 | |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 213 | #if defined(OS_POSIX) |
| 214 | // Set a umask and restore the old mask on destruction. Cribbed from |
| 215 | // shared_memory_unittest.cc. Used by POSIX-only UserPermission test. |
| 216 | class ScopedUmaskSetter { |
| 217 | public: |
| 218 | explicit ScopedUmaskSetter(mode_t target_mask) { |
| 219 | old_umask_ = umask(target_mask); |
| 220 | } |
| 221 | ~ScopedUmaskSetter() { umask(old_umask_); } |
| 222 | private: |
| 223 | mode_t old_umask_; |
| 224 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter); |
| 225 | }; |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 226 | #endif // defined(OS_POSIX) |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 227 | |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 228 | // SQLite function to adjust mock time by |argv[0]| milliseconds. |
| 229 | void sqlite_adjust_millis(sql::test::ScopedMockTimeSource* time_mock, |
| 230 | sqlite3_context* context, |
| 231 | int argc, sqlite3_value** argv) { |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 232 | int64_t milliseconds = argc > 0 ? sqlite3_value_int64(argv[0]) : 1000; |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 233 | time_mock->adjust(base::TimeDelta::FromMilliseconds(milliseconds)); |
| 234 | sqlite3_result_int64(context, milliseconds); |
| 235 | } |
| 236 | |
| 237 | // Adjust mock time by |milliseconds| on commit. |
| 238 | int adjust_commit_hook(sql::test::ScopedMockTimeSource* time_mock, |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 239 | int64_t milliseconds) { |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 240 | time_mock->adjust(base::TimeDelta::FromMilliseconds(milliseconds)); |
| 241 | return SQLITE_OK; |
| 242 | } |
| 243 | |
| 244 | const char kCommitTime[] = "Sqlite.CommitTime.Test"; |
| 245 | const char kAutoCommitTime[] = "Sqlite.AutoCommitTime.Test"; |
| 246 | const char kUpdateTime[] = "Sqlite.UpdateTime.Test"; |
| 247 | const char kQueryTime[] = "Sqlite.QueryTime.Test"; |
| 248 | |
| 249 | } // namespace |
| 250 | |
shess | 1cf87f2 | 2016-10-25 22:18:29 | [diff] [blame] | 251 | using SQLConnectionTest = sql::SQLTestBase; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 252 | |
| 253 | TEST_F(SQLConnectionTest, Execute) { |
| 254 | // Valid statement should return true. |
| 255 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 256 | EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); |
| 257 | |
| 258 | // Invalid statement should fail. |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 259 | ASSERT_EQ(SQLITE_ERROR, |
| 260 | db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 261 | EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode()); |
| 262 | } |
| 263 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 264 | TEST_F(SQLConnectionTest, ExecuteWithErrorCode) { |
| 265 | ASSERT_EQ(SQLITE_OK, |
| 266 | db().ExecuteAndReturnErrorCode("CREATE TABLE foo (a, b)")); |
| 267 | ASSERT_EQ(SQLITE_ERROR, |
| 268 | db().ExecuteAndReturnErrorCode("CREATE TABLE TABLE")); |
| 269 | ASSERT_EQ(SQLITE_ERROR, |
| 270 | db().ExecuteAndReturnErrorCode( |
| 271 | "INSERT INTO foo(a, b) VALUES (1, 2, 3, 4)")); |
| 272 | } |
| 273 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 274 | TEST_F(SQLConnectionTest, CachedStatement) { |
| 275 | sql::StatementID id1("foo", 12); |
| 276 | |
| 277 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 278 | ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)")); |
| 279 | |
| 280 | // Create a new cached statement. |
| 281 | { |
| 282 | sql::Statement s(db().GetCachedStatement(id1, "SELECT a FROM foo")); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 283 | ASSERT_TRUE(s.is_valid()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 284 | |
| 285 | ASSERT_TRUE(s.Step()); |
| 286 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 287 | } |
| 288 | |
| 289 | // The statement should be cached still. |
| 290 | EXPECT_TRUE(db().HasCachedStatement(id1)); |
| 291 | |
| 292 | { |
| 293 | // Get the same statement using different SQL. This should ignore our |
| 294 | // SQL and use the cached one (so it will be valid). |
| 295 | sql::Statement s(db().GetCachedStatement(id1, "something invalid(")); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 296 | ASSERT_TRUE(s.is_valid()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 297 | |
| 298 | ASSERT_TRUE(s.Step()); |
| 299 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 300 | } |
| 301 | |
| 302 | // Make sure other statements aren't marked as cached. |
| 303 | EXPECT_FALSE(db().HasCachedStatement(SQL_FROM_HERE)); |
| 304 | } |
| 305 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 306 | TEST_F(SQLConnectionTest, IsSQLValidTest) { |
| 307 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 308 | ASSERT_TRUE(db().IsSQLValid("SELECT a FROM foo")); |
| 309 | ASSERT_FALSE(db().IsSQLValid("SELECT no_exist FROM foo")); |
| 310 | } |
| 311 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 312 | TEST_F(SQLConnectionTest, DoesStuffExist) { |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 313 | // Test DoesTableExist and DoesIndexExist. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 314 | EXPECT_FALSE(db().DoesTableExist("foo")); |
| 315 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 316 | ASSERT_TRUE(db().Execute("CREATE INDEX foo_a ON foo (a)")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 317 | EXPECT_FALSE(db().DoesIndexExist("foo")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 318 | EXPECT_TRUE(db().DoesTableExist("foo")); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 319 | EXPECT_TRUE(db().DoesIndexExist("foo_a")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 320 | EXPECT_FALSE(db().DoesTableExist("foo_a")); |
| 321 | |
| 322 | // Test DoesViewExist. The CREATE VIEW is an older form because some iOS |
| 323 | // versions use an earlier version of SQLite, and the difference isn't |
| 324 | // relevant for this test. |
| 325 | EXPECT_FALSE(db().DoesViewExist("voo")); |
| 326 | ASSERT_TRUE(db().Execute("CREATE VIEW voo AS SELECT 1")); |
| 327 | EXPECT_FALSE(db().DoesIndexExist("voo")); |
| 328 | EXPECT_FALSE(db().DoesTableExist("voo")); |
| 329 | EXPECT_TRUE(db().DoesViewExist("voo")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 330 | |
| 331 | // Test DoesColumnExist. |
| 332 | EXPECT_FALSE(db().DoesColumnExist("foo", "bar")); |
| 333 | EXPECT_TRUE(db().DoesColumnExist("foo", "a")); |
| 334 | |
[email protected] | e7afe245 | 2010-08-22 16:19:13 | [diff] [blame] | 335 | // Testing for a column on a nonexistent table. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 336 | EXPECT_FALSE(db().DoesColumnExist("bar", "b")); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 337 | |
| 338 | // Names are not case sensitive. |
| 339 | EXPECT_TRUE(db().DoesTableExist("FOO")); |
| 340 | EXPECT_TRUE(db().DoesColumnExist("FOO", "A")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | TEST_F(SQLConnectionTest, GetLastInsertRowId) { |
| 344 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)")); |
| 345 | |
| 346 | ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); |
| 347 | |
| 348 | // Last insert row ID should be valid. |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame] | 349 | int64_t row = db().GetLastInsertRowId(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 350 | EXPECT_LT(0, row); |
| 351 | |
| 352 | // It should be the primary key of the row we just inserted. |
| 353 | sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo WHERE id=?")); |
| 354 | s.BindInt64(0, row); |
| 355 | ASSERT_TRUE(s.Step()); |
| 356 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 357 | } |
[email protected] | 44ad7d90 | 2012-03-23 00:09:05 | [diff] [blame] | 358 | |
| 359 | TEST_F(SQLConnectionTest, Rollback) { |
| 360 | ASSERT_TRUE(db().BeginTransaction()); |
| 361 | ASSERT_TRUE(db().BeginTransaction()); |
| 362 | EXPECT_EQ(2, db().transaction_nesting()); |
| 363 | db().RollbackTransaction(); |
| 364 | EXPECT_FALSE(db().CommitTransaction()); |
| 365 | EXPECT_TRUE(db().BeginTransaction()); |
| 366 | } |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 367 | |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 368 | // Test the scoped error expecter by attempting to insert a duplicate |
[email protected] | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 369 | // value into an index. |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 370 | TEST_F(SQLConnectionTest, ScopedErrorExpecter) { |
[email protected] | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 371 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
| 372 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 373 | ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
| 374 | |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 375 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 376 | sql::test::ScopedErrorExpecter expecter; |
| 377 | expecter.ExpectError(SQLITE_CONSTRAINT); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 378 | ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 379 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | // Test that clients of GetUntrackedStatement() can test corruption-handling |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 384 | // with ScopedErrorExpecter. |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 385 | TEST_F(SQLConnectionTest, ScopedIgnoreUntracked) { |
| 386 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
| 387 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 388 | ASSERT_FALSE(db().DoesTableExist("bar")); |
| 389 | ASSERT_TRUE(db().DoesTableExist("foo")); |
| 390 | ASSERT_TRUE(db().DoesColumnExist("foo", "id")); |
| 391 | db().Close(); |
| 392 | |
| 393 | // Corrupt the database so that nothing works, including PRAGMAs. |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 394 | ASSERT_TRUE(CorruptSizeInHeaderOfDB()); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 395 | |
| 396 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 397 | sql::test::ScopedErrorExpecter expecter; |
| 398 | expecter.ExpectError(SQLITE_CORRUPT); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 399 | ASSERT_TRUE(db().Open(db_path())); |
| 400 | ASSERT_FALSE(db().DoesTableExist("bar")); |
| 401 | ASSERT_FALSE(db().DoesTableExist("foo")); |
| 402 | ASSERT_FALSE(db().DoesColumnExist("foo", "id")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 403 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 404 | } |
[email protected] | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 405 | } |
| 406 | |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 407 | TEST_F(SQLConnectionTest, ErrorCallback) { |
| 408 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
| 409 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 410 | ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
| 411 | |
| 412 | int error = SQLITE_OK; |
| 413 | { |
| 414 | sql::ScopedErrorCallback sec( |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 415 | &db(), base::BindRepeating(&sql::CaptureErrorCallback, &error)); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 416 | EXPECT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 417 | |
| 418 | // Later versions of SQLite throw SQLITE_CONSTRAINT_UNIQUE. The specific |
| 419 | // sub-error isn't really important. |
| 420 | EXPECT_EQ(SQLITE_CONSTRAINT, (error&0xff)); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | // Callback is no longer in force due to reset. |
| 424 | { |
| 425 | error = SQLITE_OK; |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 426 | sql::test::ScopedErrorExpecter expecter; |
| 427 | expecter.ExpectError(SQLITE_CONSTRAINT); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 428 | ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 429 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 430 | EXPECT_EQ(SQLITE_OK, error); |
| 431 | } |
| 432 | |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 433 | // base::BindRepeating() can curry arguments to be passed by const reference |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 434 | // to the callback function. If the callback function calls |
| 435 | // re/set_error_callback(), the storage for those arguments can be |
| 436 | // deleted while the callback function is still executing. |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 437 | // |
| 438 | // RefCounter() counts how many objects are live using an external |
| 439 | // count. The same counter is passed to the callback, so that it |
| 440 | // can check directly even if the RefCounter object is no longer |
| 441 | // live. |
| 442 | { |
| 443 | size_t count = 0; |
| 444 | sql::ScopedErrorCallback sec( |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 445 | &db(), base::BindRepeating(&ErrorCallbackSetHelper, &db(), &count, |
| 446 | RefCounter(&count))); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 447 | |
| 448 | EXPECT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
| 449 | } |
| 450 | |
| 451 | // Same test, but reset_error_callback() case. |
| 452 | { |
| 453 | size_t count = 0; |
| 454 | sql::ScopedErrorCallback sec( |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 455 | &db(), base::BindRepeating(&ErrorCallbackResetHelper, &db(), &count, |
| 456 | RefCounter(&count))); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 457 | |
| 458 | EXPECT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
| 459 | } |
| 460 | } |
| 461 | |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 462 | // Test that sql::Connection::Raze() results in a database without the |
| 463 | // tables from the original database. |
| 464 | TEST_F(SQLConnectionTest, Raze) { |
| 465 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 466 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 467 | ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); |
| 468 | |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 469 | int pragma_auto_vacuum = 0; |
| 470 | { |
| 471 | sql::Statement s(db().GetUniqueStatement("PRAGMA auto_vacuum")); |
| 472 | ASSERT_TRUE(s.Step()); |
| 473 | pragma_auto_vacuum = s.ColumnInt(0); |
| 474 | ASSERT_TRUE(pragma_auto_vacuum == 0 || pragma_auto_vacuum == 1); |
| 475 | } |
| 476 | |
| 477 | // If auto_vacuum is set, there's an extra page to maintain a freelist. |
| 478 | const int kExpectedPageCount = 2 + pragma_auto_vacuum; |
| 479 | |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 480 | { |
| 481 | sql::Statement s(db().GetUniqueStatement("PRAGMA page_count")); |
| 482 | ASSERT_TRUE(s.Step()); |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 483 | EXPECT_EQ(kExpectedPageCount, s.ColumnInt(0)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | { |
| 487 | sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); |
| 488 | ASSERT_TRUE(s.Step()); |
| 489 | EXPECT_EQ("table", s.ColumnString(0)); |
| 490 | EXPECT_EQ("foo", s.ColumnString(1)); |
| 491 | EXPECT_EQ("foo", s.ColumnString(2)); |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 492 | // Table "foo" is stored in the last page of the file. |
| 493 | EXPECT_EQ(kExpectedPageCount, s.ColumnInt(3)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 494 | EXPECT_EQ(kCreateSql, s.ColumnString(4)); |
| 495 | } |
| 496 | |
| 497 | ASSERT_TRUE(db().Raze()); |
| 498 | |
| 499 | { |
| 500 | sql::Statement s(db().GetUniqueStatement("PRAGMA page_count")); |
| 501 | ASSERT_TRUE(s.Step()); |
| 502 | EXPECT_EQ(1, s.ColumnInt(0)); |
| 503 | } |
| 504 | |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 505 | ASSERT_EQ(0, SqliteMasterCount(&db())); |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 506 | |
| 507 | { |
| 508 | sql::Statement s(db().GetUniqueStatement("PRAGMA auto_vacuum")); |
| 509 | ASSERT_TRUE(s.Step()); |
[email protected] | 6d42f15 | 2012-11-10 00:38:24 | [diff] [blame] | 510 | // The new database has the same auto_vacuum as a fresh database. |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 511 | EXPECT_EQ(pragma_auto_vacuum, s.ColumnInt(0)); |
| 512 | } |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 513 | } |
| 514 | |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 515 | // Helper for SQLConnectionTest.RazePageSize. Creates a fresh db based on |
| 516 | // db_prefix, with the given initial page size, and verifies it against the |
| 517 | // expected size. Then changes to the final page size and razes, verifying that |
| 518 | // the fresh database ends up with the expected final page size. |
| 519 | void TestPageSize(const base::FilePath& db_prefix, |
| 520 | int initial_page_size, |
| 521 | const std::string& expected_initial_page_size, |
| 522 | int final_page_size, |
| 523 | const std::string& expected_final_page_size) { |
| 524 | const char kCreateSql[] = "CREATE TABLE x (t TEXT)"; |
| 525 | const char kInsertSql1[] = "INSERT INTO x VALUES ('This is a test')"; |
| 526 | const char kInsertSql2[] = "INSERT INTO x VALUES ('That was a test')"; |
| 527 | |
| 528 | const base::FilePath db_path = db_prefix.InsertBeforeExtensionASCII( |
| 529 | base::IntToString(initial_page_size)); |
| 530 | sql::Connection::Delete(db_path); |
| 531 | sql::Connection db; |
| 532 | db.set_page_size(initial_page_size); |
| 533 | ASSERT_TRUE(db.Open(db_path)); |
| 534 | ASSERT_TRUE(db.Execute(kCreateSql)); |
| 535 | ASSERT_TRUE(db.Execute(kInsertSql1)); |
| 536 | ASSERT_TRUE(db.Execute(kInsertSql2)); |
| 537 | ASSERT_EQ(expected_initial_page_size, |
| 538 | ExecuteWithResult(&db, "PRAGMA page_size")); |
| 539 | |
| 540 | // Raze will use the page size set in the connection object, which may not |
| 541 | // match the file's page size. |
| 542 | db.set_page_size(final_page_size); |
| 543 | ASSERT_TRUE(db.Raze()); |
| 544 | |
| 545 | // SQLite 3.10.2 (at least) has a quirk with the sqlite3_backup() API (used by |
| 546 | // Raze()) which causes the destination database to remember the previous |
| 547 | // page_size, even if the overwriting database changed the page_size. Access |
| 548 | // the actual database to cause the cached value to be updated. |
| 549 | EXPECT_EQ("0", ExecuteWithResult(&db, "SELECT COUNT(*) FROM sqlite_master")); |
| 550 | |
| 551 | EXPECT_EQ(expected_final_page_size, |
| 552 | ExecuteWithResult(&db, "PRAGMA page_size")); |
| 553 | EXPECT_EQ("1", ExecuteWithResult(&db, "PRAGMA page_count")); |
| 554 | } |
| 555 | |
| 556 | // Verify that sql::Recovery maintains the page size, and the virtual table |
| 557 | // works with page sizes other than SQLite's default. Also verify the case |
| 558 | // where the default page size has changed. |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 559 | TEST_F(SQLConnectionTest, RazePageSize) { |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 560 | const std::string default_page_size = |
| 561 | ExecuteWithResult(&db(), "PRAGMA page_size"); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 562 | |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 563 | // The database should have the default page size after raze. |
| 564 | EXPECT_NO_FATAL_FAILURE( |
| 565 | TestPageSize(db_path(), 0, default_page_size, 0, default_page_size)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 566 | |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 567 | // Sync user 32k pages. |
| 568 | EXPECT_NO_FATAL_FAILURE( |
| 569 | TestPageSize(db_path(), 32768, "32768", 32768, "32768")); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 570 | |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 571 | // Many clients use 4k pages. This is the SQLite default after 3.12.0. |
| 572 | EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path(), 4096, "4096", 4096, "4096")); |
| 573 | |
| 574 | // 1k is the default page size before 3.12.0. |
| 575 | EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path(), 1024, "1024", 1024, "1024")); |
| 576 | |
| 577 | EXPECT_NO_FATAL_FAILURE( |
| 578 | TestPageSize(db_path(), 2048, "2048", 4096, "4096")); |
| 579 | |
| 580 | // Databases with no page size specified should result in the new default |
| 581 | // page size. 2k has never been the default page size. |
| 582 | ASSERT_NE("2048", default_page_size); |
| 583 | EXPECT_NO_FATAL_FAILURE( |
| 584 | TestPageSize(db_path(), 2048, "2048", 0, default_page_size)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | // Test that Raze() results are seen in other connections. |
| 588 | TEST_F(SQLConnectionTest, RazeMultiple) { |
| 589 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 590 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 591 | |
| 592 | sql::Connection other_db; |
| 593 | ASSERT_TRUE(other_db.Open(db_path())); |
| 594 | |
| 595 | // Check that the second connection sees the table. |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 596 | ASSERT_EQ(1, SqliteMasterCount(&other_db)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 597 | |
| 598 | ASSERT_TRUE(db().Raze()); |
| 599 | |
| 600 | // The second connection sees the updated database. |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 601 | ASSERT_EQ(0, SqliteMasterCount(&other_db)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | TEST_F(SQLConnectionTest, RazeLocked) { |
| 605 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 606 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 607 | |
| 608 | // Open a transaction and write some data in a second connection. |
| 609 | // This will acquire a PENDING or EXCLUSIVE transaction, which will |
| 610 | // cause the raze to fail. |
| 611 | sql::Connection other_db; |
| 612 | ASSERT_TRUE(other_db.Open(db_path())); |
| 613 | ASSERT_TRUE(other_db.BeginTransaction()); |
| 614 | const char* kInsertSql = "INSERT INTO foo VALUES (1, 'data')"; |
| 615 | ASSERT_TRUE(other_db.Execute(kInsertSql)); |
| 616 | |
| 617 | ASSERT_FALSE(db().Raze()); |
| 618 | |
| 619 | // Works after COMMIT. |
| 620 | ASSERT_TRUE(other_db.CommitTransaction()); |
| 621 | ASSERT_TRUE(db().Raze()); |
| 622 | |
| 623 | // Re-create the database. |
| 624 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 625 | ASSERT_TRUE(db().Execute(kInsertSql)); |
| 626 | |
| 627 | // An unfinished read transaction in the other connection also |
| 628 | // blocks raze. |
| 629 | const char *kQuery = "SELECT COUNT(*) FROM foo"; |
| 630 | sql::Statement s(other_db.GetUniqueStatement(kQuery)); |
| 631 | ASSERT_TRUE(s.Step()); |
| 632 | ASSERT_FALSE(db().Raze()); |
| 633 | |
| 634 | // Complete the statement unlocks the database. |
| 635 | ASSERT_FALSE(s.Step()); |
| 636 | ASSERT_TRUE(db().Raze()); |
| 637 | } |
| 638 | |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 639 | // Verify that Raze() can handle an empty file. SQLite should treat |
| 640 | // this as an empty database. |
| 641 | TEST_F(SQLConnectionTest, RazeEmptyDB) { |
| 642 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 643 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 644 | db().Close(); |
| 645 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 646 | TruncateDatabase(); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 647 | |
| 648 | ASSERT_TRUE(db().Open(db_path())); |
| 649 | ASSERT_TRUE(db().Raze()); |
| 650 | EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 651 | } |
| 652 | |
| 653 | // Verify that Raze() can handle a file of junk. |
| 654 | TEST_F(SQLConnectionTest, RazeNOTADB) { |
| 655 | db().Close(); |
| 656 | sql::Connection::Delete(db_path()); |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 657 | ASSERT_FALSE(GetPathExists(db_path())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 658 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 659 | WriteJunkToDatabase(SQLTestBase::TYPE_OVERWRITE_AND_TRUNCATE); |
| 660 | ASSERT_TRUE(GetPathExists(db_path())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 661 | |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 662 | // SQLite will successfully open the handle, but fail when running PRAGMA |
| 663 | // statements that access the database. |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 664 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 665 | sql::test::ScopedErrorExpecter expecter; |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 666 | |
Victor Costan | 42988a9 | 2018-02-06 02:22:14 | [diff] [blame] | 667 | // Old SQLite versions returned a different error code. |
| 668 | ASSERT_GE(expecter.SQLiteLibVersionNumber(), 3014000) |
| 669 | << "Chrome ships with SQLite 3.22.0+. The system SQLite version is " |
| 670 | << "only supported on iOS 10+, which ships with SQLite 3.14.0+"; |
| 671 | expecter.ExpectError(SQLITE_NOTADB); |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 672 | |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 673 | EXPECT_TRUE(db().Open(db_path())); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 674 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 675 | } |
| 676 | EXPECT_TRUE(db().Raze()); |
| 677 | db().Close(); |
| 678 | |
| 679 | // Now empty, the open should open an empty database. |
| 680 | EXPECT_TRUE(db().Open(db_path())); |
| 681 | EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 682 | } |
| 683 | |
| 684 | // Verify that Raze() can handle a database overwritten with garbage. |
| 685 | TEST_F(SQLConnectionTest, RazeNOTADB2) { |
| 686 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 687 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 688 | ASSERT_EQ(1, SqliteMasterCount(&db())); |
| 689 | db().Close(); |
| 690 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 691 | WriteJunkToDatabase(SQLTestBase::TYPE_OVERWRITE); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 692 | |
| 693 | // SQLite will successfully open the handle, but will fail with |
| 694 | // SQLITE_NOTADB on pragma statemenets which attempt to read the |
| 695 | // corrupted header. |
| 696 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 697 | sql::test::ScopedErrorExpecter expecter; |
| 698 | expecter.ExpectError(SQLITE_NOTADB); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 699 | EXPECT_TRUE(db().Open(db_path())); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 700 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 701 | } |
| 702 | EXPECT_TRUE(db().Raze()); |
| 703 | db().Close(); |
| 704 | |
| 705 | // Now empty, the open should succeed with an empty database. |
| 706 | EXPECT_TRUE(db().Open(db_path())); |
| 707 | EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 708 | } |
| 709 | |
| 710 | // Test that a callback from Open() can raze the database. This is |
| 711 | // essential for cases where the Open() can fail entirely, so the |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 712 | // Raze() cannot happen later. Additionally test that when the |
| 713 | // callback does this during Open(), the open is retried and succeeds. |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 714 | TEST_F(SQLConnectionTest, RazeCallbackReopen) { |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 715 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 716 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 717 | ASSERT_EQ(1, SqliteMasterCount(&db())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 718 | db().Close(); |
| 719 | |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 720 | // Corrupt the database so that nothing works, including PRAGMAs. |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 721 | ASSERT_TRUE(CorruptSizeInHeaderOfDB()); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 722 | |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 723 | // Open() will succeed, even though the PRAGMA calls within will |
| 724 | // fail with SQLITE_CORRUPT, as will this PRAGMA. |
| 725 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 726 | sql::test::ScopedErrorExpecter expecter; |
| 727 | expecter.ExpectError(SQLITE_CORRUPT); |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 728 | ASSERT_TRUE(db().Open(db_path())); |
| 729 | ASSERT_FALSE(db().Execute("PRAGMA auto_vacuum")); |
| 730 | db().Close(); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 731 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 732 | } |
| 733 | |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 734 | db().set_error_callback( |
| 735 | base::BindRepeating(&RazeErrorCallback, &db(), SQLITE_CORRUPT)); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 736 | |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 737 | // When the PRAGMA calls in Open() raise SQLITE_CORRUPT, the error |
| 738 | // callback will call RazeAndClose(). Open() will then fail and be |
| 739 | // retried. The second Open() on the empty database will succeed |
| 740 | // cleanly. |
| 741 | ASSERT_TRUE(db().Open(db_path())); |
| 742 | ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum")); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 743 | EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 744 | } |
| 745 | |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 746 | // Basic test of RazeAndClose() operation. |
| 747 | TEST_F(SQLConnectionTest, RazeAndClose) { |
| 748 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 749 | const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)"; |
| 750 | |
| 751 | // Test that RazeAndClose() closes the database, and that the |
| 752 | // database is empty when re-opened. |
| 753 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 754 | ASSERT_TRUE(db().Execute(kPopulateSql)); |
| 755 | ASSERT_TRUE(db().RazeAndClose()); |
| 756 | ASSERT_FALSE(db().is_open()); |
| 757 | db().Close(); |
| 758 | ASSERT_TRUE(db().Open(db_path())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 759 | ASSERT_EQ(0, SqliteMasterCount(&db())); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 760 | |
| 761 | // Test that RazeAndClose() can break transactions. |
| 762 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 763 | ASSERT_TRUE(db().Execute(kPopulateSql)); |
| 764 | ASSERT_TRUE(db().BeginTransaction()); |
| 765 | ASSERT_TRUE(db().RazeAndClose()); |
| 766 | ASSERT_FALSE(db().is_open()); |
| 767 | ASSERT_FALSE(db().CommitTransaction()); |
| 768 | db().Close(); |
| 769 | ASSERT_TRUE(db().Open(db_path())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 770 | ASSERT_EQ(0, SqliteMasterCount(&db())); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | // Test that various operations fail without crashing after |
| 774 | // RazeAndClose(). |
| 775 | TEST_F(SQLConnectionTest, RazeAndCloseDiagnostics) { |
| 776 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 777 | const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)"; |
| 778 | const char* kSimpleSql = "SELECT 1"; |
| 779 | |
| 780 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 781 | ASSERT_TRUE(db().Execute(kPopulateSql)); |
| 782 | |
| 783 | // Test baseline expectations. |
| 784 | db().Preload(); |
| 785 | ASSERT_TRUE(db().DoesTableExist("foo")); |
| 786 | ASSERT_TRUE(db().IsSQLValid(kSimpleSql)); |
| 787 | ASSERT_EQ(SQLITE_OK, db().ExecuteAndReturnErrorCode(kSimpleSql)); |
| 788 | ASSERT_TRUE(db().Execute(kSimpleSql)); |
| 789 | ASSERT_TRUE(db().is_open()); |
| 790 | { |
| 791 | sql::Statement s(db().GetUniqueStatement(kSimpleSql)); |
| 792 | ASSERT_TRUE(s.Step()); |
| 793 | } |
| 794 | { |
| 795 | sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
| 796 | ASSERT_TRUE(s.Step()); |
| 797 | } |
| 798 | ASSERT_TRUE(db().BeginTransaction()); |
| 799 | ASSERT_TRUE(db().CommitTransaction()); |
| 800 | ASSERT_TRUE(db().BeginTransaction()); |
| 801 | db().RollbackTransaction(); |
| 802 | |
| 803 | ASSERT_TRUE(db().RazeAndClose()); |
| 804 | |
| 805 | // At this point, they should all fail, but not crash. |
| 806 | db().Preload(); |
| 807 | ASSERT_FALSE(db().DoesTableExist("foo")); |
| 808 | ASSERT_FALSE(db().IsSQLValid(kSimpleSql)); |
| 809 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(kSimpleSql)); |
| 810 | ASSERT_FALSE(db().Execute(kSimpleSql)); |
| 811 | ASSERT_FALSE(db().is_open()); |
| 812 | { |
| 813 | sql::Statement s(db().GetUniqueStatement(kSimpleSql)); |
| 814 | ASSERT_FALSE(s.Step()); |
| 815 | } |
| 816 | { |
| 817 | sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
| 818 | ASSERT_FALSE(s.Step()); |
| 819 | } |
| 820 | ASSERT_FALSE(db().BeginTransaction()); |
| 821 | ASSERT_FALSE(db().CommitTransaction()); |
| 822 | ASSERT_FALSE(db().BeginTransaction()); |
| 823 | db().RollbackTransaction(); |
| 824 | |
| 825 | // Close normally to reset the poisoned flag. |
| 826 | db().Close(); |
| 827 | |
Scott Graham | 57ee5482 | 2017-09-13 06:37:56 | [diff] [blame] | 828 | // DEATH tests not supported on Android, iOS, or Fuchsia. |
| 829 | #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_FUCHSIA) |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 830 | // Once the real Close() has been called, various calls enforce API |
| 831 | // usage by becoming fatal in debug mode. Since DEATH tests are |
| 832 | // expensive, just test one of them. |
| 833 | if (DLOG_IS_ON(FATAL)) { |
| 834 | ASSERT_DEATH({ |
| 835 | db().IsSQLValid(kSimpleSql); |
| 836 | }, "Illegal use of connection without a db"); |
| 837 | } |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 838 | #endif // !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_FUCHSIA) |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | // TODO(shess): Spin up a background thread to hold other_db, to more |
| 842 | // closely match real life. That would also allow testing |
| 843 | // RazeWithTimeout(). |
| 844 | |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 845 | // On Windows, truncate silently fails against a memory-mapped file. One goal |
| 846 | // of Raze() is to truncate the file to remove blocks which generate I/O errors. |
| 847 | // Test that Raze() turns off memory mapping so that the file is truncated. |
| 848 | // [This would not cover the case of multiple connections where one of the other |
| 849 | // connections is memory-mapped. That is infrequent in Chromium.] |
| 850 | TEST_F(SQLConnectionTest, RazeTruncate) { |
| 851 | // The empty database has 0 or 1 pages. Raze() should leave it with exactly 1 |
| 852 | // page. Not checking directly because auto_vacuum on Android adds a freelist |
| 853 | // page. |
| 854 | ASSERT_TRUE(db().Raze()); |
| 855 | int64_t expected_size; |
| 856 | ASSERT_TRUE(base::GetFileSize(db_path(), &expected_size)); |
| 857 | ASSERT_GT(expected_size, 0); |
| 858 | |
| 859 | // Cause the database to take a few pages. |
| 860 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 861 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 862 | for (size_t i = 0; i < 24; ++i) { |
| 863 | ASSERT_TRUE( |
| 864 | db().Execute("INSERT INTO foo (value) VALUES (randomblob(1024))")); |
| 865 | } |
| 866 | int64_t db_size; |
| 867 | ASSERT_TRUE(base::GetFileSize(db_path(), &db_size)); |
| 868 | ASSERT_GT(db_size, expected_size); |
| 869 | |
| 870 | // Make a query covering most of the database file to make sure that the |
| 871 | // blocks are actually mapped into memory. Empirically, the truncate problem |
| 872 | // doesn't seem to happen if no blocks are mapped. |
| 873 | EXPECT_EQ("24576", |
| 874 | ExecuteWithResult(&db(), "SELECT SUM(LENGTH(value)) FROM foo")); |
| 875 | |
| 876 | ASSERT_TRUE(db().Raze()); |
| 877 | ASSERT_TRUE(base::GetFileSize(db_path(), &db_size)); |
| 878 | ASSERT_EQ(expected_size, db_size); |
| 879 | } |
| 880 | |
[email protected] | 1348765a | 2012-07-24 08:25:53 | [diff] [blame] | 881 | #if defined(OS_ANDROID) |
| 882 | TEST_F(SQLConnectionTest, SetTempDirForSQL) { |
| 883 | |
| 884 | sql::MetaTable meta_table; |
| 885 | // Below call needs a temporary directory in sqlite3 |
| 886 | // On Android, it can pass only when the temporary directory is set. |
| 887 | // Otherwise, sqlite3 doesn't find the correct directory to store |
| 888 | // temporary files and will report the error 'unable to open |
| 889 | // database file'. |
| 890 | ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); |
| 891 | } |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 892 | #endif // defined(OS_ANDROID) |
[email protected] | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 893 | |
| 894 | TEST_F(SQLConnectionTest, Delete) { |
| 895 | EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); |
| 896 | db().Close(); |
| 897 | |
| 898 | // Should have both a main database file and a journal file because |
shess | 2c21ecf | 2015-06-02 01:31:09 | [diff] [blame] | 899 | // of journal_mode TRUNCATE. |
[email protected] | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 900 | base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal")); |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 901 | ASSERT_TRUE(GetPathExists(db_path())); |
| 902 | ASSERT_TRUE(GetPathExists(journal)); |
[email protected] | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 903 | |
| 904 | sql::Connection::Delete(db_path()); |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 905 | EXPECT_FALSE(GetPathExists(db_path())); |
| 906 | EXPECT_FALSE(GetPathExists(journal)); |
[email protected] | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 907 | } |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 908 | |
Scott Graham | 47ed2c3 | 2017-09-15 02:17:07 | [diff] [blame] | 909 | // This test manually sets on disk permissions, these don't exist on Fuchsia. |
| 910 | #if defined(OS_POSIX) && !defined(OS_FUCHSIA) |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 911 | // Test that set_restrict_to_user() trims database permissions so that |
| 912 | // only the owner (and root) can read. |
| 913 | TEST_F(SQLConnectionTest, UserPermission) { |
| 914 | // If the bots all had a restrictive umask setting such that |
| 915 | // databases are always created with only the owner able to read |
| 916 | // them, then the code could break without breaking the tests. |
| 917 | // Temporarily provide a more permissive umask. |
| 918 | db().Close(); |
| 919 | sql::Connection::Delete(db_path()); |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 920 | ASSERT_FALSE(GetPathExists(db_path())); |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 921 | ScopedUmaskSetter permissive_umask(S_IWGRP | S_IWOTH); |
| 922 | ASSERT_TRUE(db().Open(db_path())); |
| 923 | |
| 924 | // Cause the journal file to be created. If the default |
| 925 | // journal_mode is changed back to DELETE, then parts of this test |
| 926 | // will need to be updated. |
| 927 | EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); |
| 928 | |
| 929 | base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal")); |
| 930 | int mode; |
| 931 | |
| 932 | // Given a permissive umask, the database is created with permissive |
| 933 | // read access for the database and journal. |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 934 | ASSERT_TRUE(GetPathExists(db_path())); |
| 935 | ASSERT_TRUE(GetPathExists(journal)); |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 936 | mode = base::FILE_PERMISSION_MASK; |
| 937 | EXPECT_TRUE(base::GetPosixFilePermissions(db_path(), &mode)); |
| 938 | ASSERT_NE((mode & base::FILE_PERMISSION_USER_MASK), mode); |
| 939 | mode = base::FILE_PERMISSION_MASK; |
| 940 | EXPECT_TRUE(base::GetPosixFilePermissions(journal, &mode)); |
| 941 | ASSERT_NE((mode & base::FILE_PERMISSION_USER_MASK), mode); |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 942 | |
| 943 | // Re-open with restricted permissions and verify that the modes |
| 944 | // changed for both the main database and the journal. |
| 945 | db().Close(); |
| 946 | db().set_restrict_to_user(); |
| 947 | ASSERT_TRUE(db().Open(db_path())); |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 948 | ASSERT_TRUE(GetPathExists(db_path())); |
| 949 | ASSERT_TRUE(GetPathExists(journal)); |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 950 | mode = base::FILE_PERMISSION_MASK; |
| 951 | EXPECT_TRUE(base::GetPosixFilePermissions(db_path(), &mode)); |
| 952 | ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode); |
| 953 | mode = base::FILE_PERMISSION_MASK; |
| 954 | EXPECT_TRUE(base::GetPosixFilePermissions(journal, &mode)); |
| 955 | ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode); |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 956 | |
| 957 | // Delete and re-create the database, the restriction should still apply. |
| 958 | db().Close(); |
| 959 | sql::Connection::Delete(db_path()); |
| 960 | ASSERT_TRUE(db().Open(db_path())); |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 961 | ASSERT_TRUE(GetPathExists(db_path())); |
| 962 | ASSERT_FALSE(GetPathExists(journal)); |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 963 | mode = base::FILE_PERMISSION_MASK; |
| 964 | EXPECT_TRUE(base::GetPosixFilePermissions(db_path(), &mode)); |
| 965 | ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode); |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 966 | |
| 967 | // Verify that journal creation inherits the restriction. |
| 968 | EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 969 | ASSERT_TRUE(GetPathExists(journal)); |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 970 | mode = base::FILE_PERMISSION_MASK; |
| 971 | EXPECT_TRUE(base::GetPosixFilePermissions(journal, &mode)); |
| 972 | ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode); |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 973 | } |
Scott Graham | 47ed2c3 | 2017-09-15 02:17:07 | [diff] [blame] | 974 | #endif // defined(OS_POSIX) && !defined(OS_FUCHSIA) |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 975 | |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 976 | // Test that errors start happening once Poison() is called. |
| 977 | TEST_F(SQLConnectionTest, Poison) { |
| 978 | EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); |
| 979 | |
| 980 | // Before the Poison() call, things generally work. |
| 981 | EXPECT_TRUE(db().IsSQLValid("INSERT INTO x VALUES ('x')")); |
| 982 | EXPECT_TRUE(db().Execute("INSERT INTO x VALUES ('x')")); |
| 983 | { |
| 984 | sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM x")); |
| 985 | ASSERT_TRUE(s.is_valid()); |
| 986 | ASSERT_TRUE(s.Step()); |
| 987 | } |
| 988 | |
| 989 | // Get a statement which is valid before and will exist across Poison(). |
| 990 | sql::Statement valid_statement( |
| 991 | db().GetUniqueStatement("SELECT COUNT(*) FROM sqlite_master")); |
| 992 | ASSERT_TRUE(valid_statement.is_valid()); |
| 993 | ASSERT_TRUE(valid_statement.Step()); |
| 994 | valid_statement.Reset(true); |
| 995 | |
| 996 | db().Poison(); |
| 997 | |
| 998 | // After the Poison() call, things fail. |
| 999 | EXPECT_FALSE(db().IsSQLValid("INSERT INTO x VALUES ('x')")); |
| 1000 | EXPECT_FALSE(db().Execute("INSERT INTO x VALUES ('x')")); |
| 1001 | { |
| 1002 | sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM x")); |
| 1003 | ASSERT_FALSE(s.is_valid()); |
| 1004 | ASSERT_FALSE(s.Step()); |
| 1005 | } |
| 1006 | |
| 1007 | // The existing statement has become invalid. |
| 1008 | ASSERT_FALSE(valid_statement.is_valid()); |
| 1009 | ASSERT_FALSE(valid_statement.Step()); |
shess | 644fc8a | 2016-02-26 18:15:58 | [diff] [blame] | 1010 | |
| 1011 | // Test that poisoning the database during a transaction works (with errors). |
| 1012 | // RazeErrorCallback() poisons the database, the extra COMMIT causes |
| 1013 | // CommitTransaction() to throw an error while commiting. |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 1014 | db().set_error_callback( |
| 1015 | base::BindRepeating(&RazeErrorCallback, &db(), SQLITE_ERROR)); |
shess | 644fc8a | 2016-02-26 18:15:58 | [diff] [blame] | 1016 | db().Close(); |
| 1017 | ASSERT_TRUE(db().Open(db_path())); |
| 1018 | EXPECT_TRUE(db().BeginTransaction()); |
| 1019 | EXPECT_TRUE(db().Execute("INSERT INTO x VALUES ('x')")); |
| 1020 | EXPECT_TRUE(db().Execute("COMMIT")); |
| 1021 | EXPECT_FALSE(db().CommitTransaction()); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1022 | } |
| 1023 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1024 | TEST_F(SQLConnectionTest, AttachDatabase) { |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1025 | EXPECT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 1026 | |
| 1027 | // Create a database to attach to. |
| 1028 | base::FilePath attach_path = |
| 1029 | db_path().DirName().AppendASCII("SQLConnectionAttach.db"); |
| 1030 | const char kAttachmentPoint[] = "other"; |
| 1031 | { |
| 1032 | sql::Connection other_db; |
| 1033 | ASSERT_TRUE(other_db.Open(attach_path)); |
| 1034 | EXPECT_TRUE(other_db.Execute("CREATE TABLE bar (a, b)")); |
| 1035 | EXPECT_TRUE(other_db.Execute("INSERT INTO bar VALUES ('hello', 'world')")); |
| 1036 | } |
| 1037 | |
| 1038 | // Cannot see the attached database, yet. |
| 1039 | EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 1040 | |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1041 | EXPECT_TRUE(db().AttachDatabase(attach_path, kAttachmentPoint)); |
| 1042 | EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 1043 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1044 | // Queries can touch both databases after the ATTACH. |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1045 | EXPECT_TRUE(db().Execute("INSERT INTO foo SELECT a, b FROM other.bar")); |
| 1046 | { |
| 1047 | sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM foo")); |
| 1048 | ASSERT_TRUE(s.Step()); |
| 1049 | EXPECT_EQ(1, s.ColumnInt(0)); |
| 1050 | } |
| 1051 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1052 | EXPECT_TRUE(db().DetachDatabase(kAttachmentPoint)); |
| 1053 | EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 1054 | } |
| 1055 | |
| 1056 | TEST_F(SQLConnectionTest, AttachDatabaseWithOpenTransaction) { |
| 1057 | EXPECT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 1058 | |
| 1059 | // Create a database to attach to. |
| 1060 | base::FilePath attach_path = |
| 1061 | db_path().DirName().AppendASCII("SQLConnectionAttach.db"); |
| 1062 | const char kAttachmentPoint[] = "other"; |
| 1063 | { |
| 1064 | sql::Connection other_db; |
| 1065 | ASSERT_TRUE(other_db.Open(attach_path)); |
| 1066 | EXPECT_TRUE(other_db.Execute("CREATE TABLE bar (a, b)")); |
| 1067 | EXPECT_TRUE(other_db.Execute("INSERT INTO bar VALUES ('hello', 'world')")); |
| 1068 | } |
| 1069 | |
| 1070 | // Cannot see the attached database, yet. |
| 1071 | EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 1072 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1073 | // Attach succeeds in a transaction. |
| 1074 | EXPECT_TRUE(db().BeginTransaction()); |
| 1075 | EXPECT_TRUE(db().AttachDatabase(attach_path, kAttachmentPoint)); |
| 1076 | EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 1077 | |
| 1078 | // Queries can touch both databases after the ATTACH. |
| 1079 | EXPECT_TRUE(db().Execute("INSERT INTO foo SELECT a, b FROM other.bar")); |
| 1080 | { |
| 1081 | sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM foo")); |
| 1082 | ASSERT_TRUE(s.Step()); |
| 1083 | EXPECT_EQ(1, s.ColumnInt(0)); |
| 1084 | } |
| 1085 | |
| 1086 | // Detaching the same database fails, database is locked in the transaction. |
| 1087 | { |
| 1088 | sql::test::ScopedErrorExpecter expecter; |
| 1089 | expecter.ExpectError(SQLITE_ERROR); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1090 | EXPECT_FALSE(db().DetachDatabase(kAttachmentPoint)); |
| 1091 | EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1092 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1093 | } |
| 1094 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1095 | // Detach succeeds when the transaction is closed. |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1096 | db().RollbackTransaction(); |
| 1097 | EXPECT_TRUE(db().DetachDatabase(kAttachmentPoint)); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1098 | EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 1099 | } |
| 1100 | |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1101 | TEST_F(SQLConnectionTest, Basic_QuickIntegrityCheck) { |
| 1102 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1103 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 1104 | EXPECT_TRUE(db().QuickIntegrityCheck()); |
| 1105 | db().Close(); |
| 1106 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 1107 | ASSERT_TRUE(CorruptSizeInHeaderOfDB()); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1108 | |
| 1109 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1110 | sql::test::ScopedErrorExpecter expecter; |
| 1111 | expecter.ExpectError(SQLITE_CORRUPT); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1112 | ASSERT_TRUE(db().Open(db_path())); |
| 1113 | EXPECT_FALSE(db().QuickIntegrityCheck()); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1114 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | TEST_F(SQLConnectionTest, Basic_FullIntegrityCheck) { |
| 1119 | const std::string kOk("ok"); |
| 1120 | std::vector<std::string> messages; |
| 1121 | |
| 1122 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1123 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 1124 | EXPECT_TRUE(db().FullIntegrityCheck(&messages)); |
| 1125 | EXPECT_EQ(1u, messages.size()); |
| 1126 | EXPECT_EQ(kOk, messages[0]); |
| 1127 | db().Close(); |
| 1128 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 1129 | ASSERT_TRUE(CorruptSizeInHeaderOfDB()); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1130 | |
| 1131 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1132 | sql::test::ScopedErrorExpecter expecter; |
| 1133 | expecter.ExpectError(SQLITE_CORRUPT); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1134 | ASSERT_TRUE(db().Open(db_path())); |
| 1135 | EXPECT_TRUE(db().FullIntegrityCheck(&messages)); |
| 1136 | EXPECT_LT(1u, messages.size()); |
| 1137 | EXPECT_NE(kOk, messages[0]); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1138 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | // TODO(shess): CorruptTableOrIndex could be used to produce a |
| 1142 | // file that would pass the quick check and fail the full check. |
| 1143 | } |
| 1144 | |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1145 | // Test Sqlite.Stats histogram for execute-oriented calls. |
| 1146 | TEST_F(SQLConnectionTest, EventsExecute) { |
| 1147 | // Re-open with histogram tag. |
| 1148 | db().Close(); |
| 1149 | db().set_histogram_tag("Test"); |
| 1150 | ASSERT_TRUE(db().Open(db_path())); |
| 1151 | |
| 1152 | // Open() uses Execute() extensively, don't track those calls. |
| 1153 | base::HistogramTester tester; |
| 1154 | |
| 1155 | const char kHistogramName[] = "Sqlite.Stats.Test"; |
| 1156 | const char kGlobalHistogramName[] = "Sqlite.Stats"; |
| 1157 | |
| 1158 | ASSERT_TRUE(db().BeginTransaction()); |
| 1159 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1160 | EXPECT_TRUE(db().Execute(kCreateSql)); |
| 1161 | EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (10, 'text')")); |
| 1162 | EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (11, 'text')")); |
| 1163 | EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (12, 'text')")); |
| 1164 | EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (13, 'text')")); |
| 1165 | EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (14, 'text')")); |
| 1166 | EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (15, 'text');" |
| 1167 | "INSERT INTO foo VALUES (16, 'text');" |
| 1168 | "INSERT INTO foo VALUES (17, 'text');" |
| 1169 | "INSERT INTO foo VALUES (18, 'text');" |
| 1170 | "INSERT INTO foo VALUES (19, 'text')")); |
| 1171 | ASSERT_TRUE(db().CommitTransaction()); |
| 1172 | ASSERT_TRUE(db().BeginTransaction()); |
| 1173 | EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (20, 'text')")); |
| 1174 | db().RollbackTransaction(); |
| 1175 | EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (20, 'text')")); |
| 1176 | EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (21, 'text')")); |
| 1177 | |
| 1178 | // The create, 5 inserts, multi-statement insert, rolled-back insert, 2 |
| 1179 | // inserts outside transaction. |
| 1180 | tester.ExpectBucketCount(kHistogramName, sql::Connection::EVENT_EXECUTE, 10); |
| 1181 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1182 | sql::Connection::EVENT_EXECUTE, 10); |
| 1183 | |
| 1184 | // All of the executes, with the multi-statement inserts broken out, plus one |
| 1185 | // for each begin, commit, and rollback. |
| 1186 | tester.ExpectBucketCount(kHistogramName, |
| 1187 | sql::Connection::EVENT_STATEMENT_RUN, 18); |
| 1188 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1189 | sql::Connection::EVENT_STATEMENT_RUN, 18); |
| 1190 | |
| 1191 | tester.ExpectBucketCount(kHistogramName, |
| 1192 | sql::Connection::EVENT_STATEMENT_ROWS, 0); |
| 1193 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1194 | sql::Connection::EVENT_STATEMENT_ROWS, 0); |
| 1195 | tester.ExpectBucketCount(kHistogramName, |
| 1196 | sql::Connection::EVENT_STATEMENT_SUCCESS, 18); |
| 1197 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1198 | sql::Connection::EVENT_STATEMENT_SUCCESS, 18); |
| 1199 | |
| 1200 | // The 2 inserts outside the transaction. |
| 1201 | tester.ExpectBucketCount(kHistogramName, |
| 1202 | sql::Connection::EVENT_CHANGES_AUTOCOMMIT, 2); |
| 1203 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1204 | sql::Connection::EVENT_CHANGES_AUTOCOMMIT, 2); |
| 1205 | |
| 1206 | // 11 inserts inside transactions. |
| 1207 | tester.ExpectBucketCount(kHistogramName, sql::Connection::EVENT_CHANGES, 11); |
| 1208 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1209 | sql::Connection::EVENT_CHANGES, 11); |
| 1210 | |
| 1211 | tester.ExpectBucketCount(kHistogramName, sql::Connection::EVENT_BEGIN, 2); |
| 1212 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1213 | sql::Connection::EVENT_BEGIN, 2); |
| 1214 | tester.ExpectBucketCount(kHistogramName, sql::Connection::EVENT_COMMIT, 1); |
| 1215 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1216 | sql::Connection::EVENT_COMMIT, 1); |
| 1217 | tester.ExpectBucketCount(kHistogramName, sql::Connection::EVENT_ROLLBACK, 1); |
| 1218 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1219 | sql::Connection::EVENT_ROLLBACK, 1); |
| 1220 | } |
| 1221 | |
| 1222 | // Test Sqlite.Stats histogram for prepared statements. |
| 1223 | TEST_F(SQLConnectionTest, EventsStatement) { |
| 1224 | // Re-open with histogram tag. |
| 1225 | db().Close(); |
| 1226 | db().set_histogram_tag("Test"); |
| 1227 | ASSERT_TRUE(db().Open(db_path())); |
| 1228 | |
| 1229 | const char kHistogramName[] = "Sqlite.Stats.Test"; |
| 1230 | const char kGlobalHistogramName[] = "Sqlite.Stats"; |
| 1231 | |
| 1232 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1233 | EXPECT_TRUE(db().Execute(kCreateSql)); |
| 1234 | EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (10, 'text')")); |
| 1235 | EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (11, 'text')")); |
| 1236 | EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (12, 'text')")); |
| 1237 | |
| 1238 | { |
| 1239 | base::HistogramTester tester; |
| 1240 | |
| 1241 | { |
| 1242 | sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo")); |
| 1243 | while (s.Step()) { |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | tester.ExpectBucketCount(kHistogramName, |
| 1248 | sql::Connection::EVENT_STATEMENT_RUN, 1); |
| 1249 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1250 | sql::Connection::EVENT_STATEMENT_RUN, 1); |
| 1251 | tester.ExpectBucketCount(kHistogramName, |
| 1252 | sql::Connection::EVENT_STATEMENT_ROWS, 3); |
| 1253 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1254 | sql::Connection::EVENT_STATEMENT_ROWS, 3); |
| 1255 | tester.ExpectBucketCount(kHistogramName, |
| 1256 | sql::Connection::EVENT_STATEMENT_SUCCESS, 1); |
| 1257 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1258 | sql::Connection::EVENT_STATEMENT_SUCCESS, 1); |
| 1259 | } |
| 1260 | |
| 1261 | { |
| 1262 | base::HistogramTester tester; |
| 1263 | |
| 1264 | { |
| 1265 | sql::Statement s(db().GetUniqueStatement( |
| 1266 | "SELECT value FROM foo WHERE id > 10")); |
| 1267 | while (s.Step()) { |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | tester.ExpectBucketCount(kHistogramName, |
| 1272 | sql::Connection::EVENT_STATEMENT_RUN, 1); |
| 1273 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1274 | sql::Connection::EVENT_STATEMENT_RUN, 1); |
| 1275 | tester.ExpectBucketCount(kHistogramName, |
| 1276 | sql::Connection::EVENT_STATEMENT_ROWS, 2); |
| 1277 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1278 | sql::Connection::EVENT_STATEMENT_ROWS, 2); |
| 1279 | tester.ExpectBucketCount(kHistogramName, |
| 1280 | sql::Connection::EVENT_STATEMENT_SUCCESS, 1); |
| 1281 | tester.ExpectBucketCount(kGlobalHistogramName, |
| 1282 | sql::Connection::EVENT_STATEMENT_SUCCESS, 1); |
| 1283 | } |
| 1284 | } |
| 1285 | |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1286 | // Read-only query allocates time to QueryTime, but not others. |
| 1287 | TEST_F(SQLConnectionTest, TimeQuery) { |
| 1288 | // Re-open with histogram tag. Use an in-memory database to minimize variance |
| 1289 | // due to filesystem. |
| 1290 | db().Close(); |
| 1291 | db().set_histogram_tag("Test"); |
| 1292 | ASSERT_TRUE(db().OpenInMemory()); |
| 1293 | |
| 1294 | sql::test::ScopedMockTimeSource time_mock(db()); |
| 1295 | |
| 1296 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1297 | EXPECT_TRUE(db().Execute(kCreateSql)); |
| 1298 | |
| 1299 | // Function to inject pauses into statements. |
| 1300 | sql::test::ScopedScalarFunction scoper( |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 1301 | db(), "milliadjust", 1, |
| 1302 | base::BindRepeating(&sqlite_adjust_millis, &time_mock)); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1303 | |
| 1304 | base::HistogramTester tester; |
| 1305 | |
| 1306 | EXPECT_TRUE(db().Execute("SELECT milliadjust(10)")); |
| 1307 | |
mostynb | d82cd995 | 2016-04-11 20:05:34 | [diff] [blame] | 1308 | std::unique_ptr<base::HistogramSamples> samples( |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1309 | tester.GetHistogramSamplesSinceCreation(kQueryTime)); |
| 1310 | ASSERT_TRUE(samples); |
| 1311 | // 10 for the adjust, 1 for the measurement. |
| 1312 | EXPECT_EQ(11, samples->sum()); |
| 1313 | |
| 1314 | samples = tester.GetHistogramSamplesSinceCreation(kUpdateTime); |
vabr | ae96043 | 2015-08-03 08:12:54 | [diff] [blame] | 1315 | EXPECT_EQ(0, samples->sum()); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1316 | |
| 1317 | samples = tester.GetHistogramSamplesSinceCreation(kCommitTime); |
vabr | ae96043 | 2015-08-03 08:12:54 | [diff] [blame] | 1318 | EXPECT_EQ(0, samples->sum()); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1319 | |
| 1320 | samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime); |
vabr | ae96043 | 2015-08-03 08:12:54 | [diff] [blame] | 1321 | EXPECT_EQ(0, samples->sum()); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | // Autocommit update allocates time to QueryTime, UpdateTime, and |
| 1325 | // AutoCommitTime. |
| 1326 | TEST_F(SQLConnectionTest, TimeUpdateAutocommit) { |
| 1327 | // Re-open with histogram tag. Use an in-memory database to minimize variance |
| 1328 | // due to filesystem. |
| 1329 | db().Close(); |
| 1330 | db().set_histogram_tag("Test"); |
| 1331 | ASSERT_TRUE(db().OpenInMemory()); |
| 1332 | |
| 1333 | sql::test::ScopedMockTimeSource time_mock(db()); |
| 1334 | |
| 1335 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1336 | EXPECT_TRUE(db().Execute(kCreateSql)); |
| 1337 | |
| 1338 | // Function to inject pauses into statements. |
| 1339 | sql::test::ScopedScalarFunction scoper( |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 1340 | db(), "milliadjust", 1, |
| 1341 | base::BindRepeating(&sqlite_adjust_millis, &time_mock)); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1342 | |
| 1343 | base::HistogramTester tester; |
| 1344 | |
| 1345 | EXPECT_TRUE(db().Execute("INSERT INTO foo VALUES (10, milliadjust(10))")); |
| 1346 | |
mostynb | d82cd995 | 2016-04-11 20:05:34 | [diff] [blame] | 1347 | std::unique_ptr<base::HistogramSamples> samples( |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1348 | tester.GetHistogramSamplesSinceCreation(kQueryTime)); |
| 1349 | ASSERT_TRUE(samples); |
| 1350 | // 10 for the adjust, 1 for the measurement. |
| 1351 | EXPECT_EQ(11, samples->sum()); |
| 1352 | |
| 1353 | samples = tester.GetHistogramSamplesSinceCreation(kUpdateTime); |
| 1354 | ASSERT_TRUE(samples); |
| 1355 | // 10 for the adjust, 1 for the measurement. |
| 1356 | EXPECT_EQ(11, samples->sum()); |
| 1357 | |
| 1358 | samples = tester.GetHistogramSamplesSinceCreation(kCommitTime); |
vabr | ae96043 | 2015-08-03 08:12:54 | [diff] [blame] | 1359 | EXPECT_EQ(0, samples->sum()); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1360 | |
| 1361 | samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime); |
| 1362 | ASSERT_TRUE(samples); |
| 1363 | // 10 for the adjust, 1 for the measurement. |
| 1364 | EXPECT_EQ(11, samples->sum()); |
| 1365 | } |
| 1366 | |
| 1367 | // Update with explicit transaction allocates time to QueryTime, UpdateTime, and |
| 1368 | // CommitTime. |
| 1369 | TEST_F(SQLConnectionTest, TimeUpdateTransaction) { |
| 1370 | // Re-open with histogram tag. Use an in-memory database to minimize variance |
| 1371 | // due to filesystem. |
| 1372 | db().Close(); |
| 1373 | db().set_histogram_tag("Test"); |
| 1374 | ASSERT_TRUE(db().OpenInMemory()); |
| 1375 | |
| 1376 | sql::test::ScopedMockTimeSource time_mock(db()); |
| 1377 | |
| 1378 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1379 | EXPECT_TRUE(db().Execute(kCreateSql)); |
| 1380 | |
| 1381 | // Function to inject pauses into statements. |
| 1382 | sql::test::ScopedScalarFunction scoper( |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 1383 | db(), "milliadjust", 1, |
| 1384 | base::BindRepeating(&sqlite_adjust_millis, &time_mock)); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1385 | |
| 1386 | base::HistogramTester tester; |
| 1387 | |
| 1388 | { |
| 1389 | // Make the commit slow. |
| 1390 | sql::test::ScopedCommitHook scoped_hook( |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 1391 | db(), base::BindRepeating(adjust_commit_hook, &time_mock, 100)); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1392 | ASSERT_TRUE(db().BeginTransaction()); |
| 1393 | EXPECT_TRUE(db().Execute( |
| 1394 | "INSERT INTO foo VALUES (11, milliadjust(10))")); |
| 1395 | EXPECT_TRUE(db().Execute( |
| 1396 | "UPDATE foo SET value = milliadjust(10) WHERE id = 11")); |
| 1397 | EXPECT_TRUE(db().CommitTransaction()); |
| 1398 | } |
| 1399 | |
mostynb | d82cd995 | 2016-04-11 20:05:34 | [diff] [blame] | 1400 | std::unique_ptr<base::HistogramSamples> samples( |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1401 | tester.GetHistogramSamplesSinceCreation(kQueryTime)); |
| 1402 | ASSERT_TRUE(samples); |
| 1403 | // 10 for insert adjust, 10 for update adjust, 100 for commit adjust, 1 for |
| 1404 | // measuring each of BEGIN, INSERT, UPDATE, and COMMIT. |
| 1405 | EXPECT_EQ(124, samples->sum()); |
| 1406 | |
| 1407 | samples = tester.GetHistogramSamplesSinceCreation(kUpdateTime); |
| 1408 | ASSERT_TRUE(samples); |
| 1409 | // 10 for insert adjust, 10 for update adjust, 100 for commit adjust, 1 for |
| 1410 | // measuring each of INSERT, UPDATE, and COMMIT. |
| 1411 | EXPECT_EQ(123, samples->sum()); |
| 1412 | |
| 1413 | samples = tester.GetHistogramSamplesSinceCreation(kCommitTime); |
| 1414 | ASSERT_TRUE(samples); |
| 1415 | // 100 for commit adjust, 1 for measuring COMMIT. |
| 1416 | EXPECT_EQ(101, samples->sum()); |
| 1417 | |
| 1418 | samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime); |
vabr | ae96043 | 2015-08-03 08:12:54 | [diff] [blame] | 1419 | EXPECT_EQ(0, samples->sum()); |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 1420 | } |
| 1421 | |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 1422 | TEST_F(SQLConnectionTest, OnMemoryDump) { |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 1423 | base::trace_event::MemoryDumpArgs args = { |
| 1424 | base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; |
erikchen | f62ea04 | 2018-05-25 21:30:57 | [diff] [blame] | 1425 | base::trace_event::ProcessMemoryDump pmd(args); |
ssid | 3be5b1ec | 2016-01-13 14:21:57 | [diff] [blame] | 1426 | ASSERT_TRUE(db().memory_dump_provider_->OnMemoryDump(args, &pmd)); |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 1427 | EXPECT_GE(pmd.allocator_dumps().size(), 1u); |
| 1428 | } |
| 1429 | |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1430 | // Test that the functions to collect diagnostic data run to completion, without |
| 1431 | // worrying too much about what they generate (since that will change). |
| 1432 | TEST_F(SQLConnectionTest, CollectDiagnosticInfo) { |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1433 | const std::string corruption_info = db().CollectCorruptionInfo(); |
| 1434 | EXPECT_NE(std::string::npos, corruption_info.find("SQLITE_CORRUPT")); |
| 1435 | EXPECT_NE(std::string::npos, corruption_info.find("integrity_check")); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1436 | |
| 1437 | // A statement to see in the results. |
| 1438 | const char* kSimpleSql = "SELECT 'mountain'"; |
| 1439 | Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
| 1440 | |
| 1441 | // Error includes the statement. |
| 1442 | const std::string readonly_info = db().CollectErrorInfo(SQLITE_READONLY, &s); |
| 1443 | EXPECT_NE(std::string::npos, readonly_info.find(kSimpleSql)); |
| 1444 | |
| 1445 | // Some other error doesn't include the statment. |
| 1446 | // TODO(shess): This is weak. |
| 1447 | const std::string full_info = db().CollectErrorInfo(SQLITE_FULL, NULL); |
| 1448 | EXPECT_EQ(std::string::npos, full_info.find(kSimpleSql)); |
| 1449 | |
| 1450 | // A table to see in the SQLITE_ERROR results. |
| 1451 | EXPECT_TRUE(db().Execute("CREATE TABLE volcano (x)")); |
| 1452 | |
| 1453 | // Version info to see in the SQLITE_ERROR results. |
| 1454 | sql::MetaTable meta_table; |
| 1455 | ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); |
| 1456 | |
| 1457 | const std::string error_info = db().CollectErrorInfo(SQLITE_ERROR, &s); |
| 1458 | EXPECT_NE(std::string::npos, error_info.find(kSimpleSql)); |
| 1459 | EXPECT_NE(std::string::npos, error_info.find("volcano")); |
| 1460 | EXPECT_NE(std::string::npos, error_info.find("version: 4")); |
| 1461 | } |
| 1462 | |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1463 | TEST_F(SQLConnectionTest, RegisterIntentToUpload) { |
| 1464 | base::FilePath breadcrumb_path( |
| 1465 | db_path().DirName().Append(FILE_PATH_LITERAL("sqlite-diag"))); |
| 1466 | |
| 1467 | // No stale diagnostic store. |
| 1468 | ASSERT_TRUE(!base::PathExists(breadcrumb_path)); |
| 1469 | |
| 1470 | // The histogram tag is required to enable diagnostic features. |
| 1471 | EXPECT_FALSE(db().RegisterIntentToUpload()); |
| 1472 | EXPECT_TRUE(!base::PathExists(breadcrumb_path)); |
| 1473 | |
| 1474 | db().Close(); |
| 1475 | db().set_histogram_tag("Test"); |
| 1476 | ASSERT_TRUE(db().Open(db_path())); |
| 1477 | |
| 1478 | // Should signal upload only once. |
| 1479 | EXPECT_TRUE(db().RegisterIntentToUpload()); |
| 1480 | EXPECT_TRUE(base::PathExists(breadcrumb_path)); |
| 1481 | EXPECT_FALSE(db().RegisterIntentToUpload()); |
| 1482 | |
| 1483 | // Changing the histogram tag should allow new upload to succeed. |
| 1484 | db().Close(); |
| 1485 | db().set_histogram_tag("NewTest"); |
| 1486 | ASSERT_TRUE(db().Open(db_path())); |
| 1487 | EXPECT_TRUE(db().RegisterIntentToUpload()); |
| 1488 | EXPECT_FALSE(db().RegisterIntentToUpload()); |
| 1489 | |
| 1490 | // Old tag is still prevented. |
| 1491 | db().Close(); |
| 1492 | db().set_histogram_tag("Test"); |
| 1493 | ASSERT_TRUE(db().Open(db_path())); |
| 1494 | EXPECT_FALSE(db().RegisterIntentToUpload()); |
| 1495 | } |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1496 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1497 | // Test that a fresh database has mmap enabled by default, if mmap'ed I/O is |
| 1498 | // enabled by SQLite. |
| 1499 | TEST_F(SQLConnectionTest, MmapInitiallyEnabled) { |
| 1500 | { |
| 1501 | sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); |
Victor Costan | 42988a9 | 2018-02-06 02:22:14 | [diff] [blame] | 1502 | ASSERT_TRUE(s.Step()) |
| 1503 | << "All supported SQLite versions should have mmap support"; |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1504 | |
| 1505 | // If mmap I/O is not on, attempt to turn it on. If that succeeds, then |
| 1506 | // Open() should have turned it on. If mmap support is disabled, 0 is |
| 1507 | // returned. If the VFS does not understand SQLITE_FCNTL_MMAP_SIZE (for |
| 1508 | // instance MojoVFS), -1 is returned. |
| 1509 | if (s.ColumnInt(0) <= 0) { |
| 1510 | ASSERT_TRUE(db().Execute("PRAGMA mmap_size = 1048576")); |
| 1511 | s.Reset(true); |
| 1512 | ASSERT_TRUE(s.Step()); |
| 1513 | EXPECT_LE(s.ColumnInt(0), 0); |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | // Test that explicit disable prevents mmap'ed I/O. |
| 1518 | db().Close(); |
| 1519 | sql::Connection::Delete(db_path()); |
| 1520 | db().set_mmap_disabled(); |
| 1521 | ASSERT_TRUE(db().Open(db_path())); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1522 | EXPECT_EQ("0", ExecuteWithResult(&db(), "PRAGMA mmap_size")); |
| 1523 | } |
| 1524 | |
| 1525 | // Test whether a fresh database gets mmap enabled when using alternate status |
| 1526 | // storage. |
| 1527 | TEST_F(SQLConnectionTest, MmapInitiallyEnabledAltStatus) { |
| 1528 | // Re-open fresh database with alt-status flag set. |
| 1529 | db().Close(); |
| 1530 | sql::Connection::Delete(db_path()); |
| 1531 | db().set_mmap_alt_status(); |
| 1532 | ASSERT_TRUE(db().Open(db_path())); |
| 1533 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1534 | { |
| 1535 | sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); |
Victor Costan | 42988a9 | 2018-02-06 02:22:14 | [diff] [blame] | 1536 | ASSERT_TRUE(s.Step()) |
| 1537 | << "All supported SQLite versions should have mmap support"; |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1538 | |
| 1539 | // If mmap I/O is not on, attempt to turn it on. If that succeeds, then |
| 1540 | // Open() should have turned it on. If mmap support is disabled, 0 is |
| 1541 | // returned. If the VFS does not understand SQLITE_FCNTL_MMAP_SIZE (for |
| 1542 | // instance MojoVFS), -1 is returned. |
| 1543 | if (s.ColumnInt(0) <= 0) { |
| 1544 | ASSERT_TRUE(db().Execute("PRAGMA mmap_size = 1048576")); |
| 1545 | s.Reset(true); |
| 1546 | ASSERT_TRUE(s.Step()); |
| 1547 | EXPECT_LE(s.ColumnInt(0), 0); |
| 1548 | } |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1549 | } |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1550 | |
| 1551 | // Test that explicit disable overrides set_mmap_alt_status(). |
| 1552 | db().Close(); |
| 1553 | sql::Connection::Delete(db_path()); |
| 1554 | db().set_mmap_disabled(); |
| 1555 | ASSERT_TRUE(db().Open(db_path())); |
| 1556 | EXPECT_EQ("0", ExecuteWithResult(&db(), "PRAGMA mmap_size")); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1557 | } |
| 1558 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1559 | TEST_F(SQLConnectionTest, GetAppropriateMmapSize) { |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1560 | const size_t kMmapAlot = 25 * 1024 * 1024; |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1561 | int64_t mmap_status = MetaTable::kMmapFailure; |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1562 | |
| 1563 | // If there is no meta table (as for a fresh database), assume that everything |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1564 | // should be mapped, and the status of the meta table is not affected. |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1565 | ASSERT_TRUE(!db().DoesTableExist("meta")); |
| 1566 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1567 | ASSERT_TRUE(!db().DoesTableExist("meta")); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1568 | |
| 1569 | // When the meta table is first created, it sets up to map everything. |
| 1570 | MetaTable().Init(&db(), 1, 1); |
| 1571 | ASSERT_TRUE(db().DoesTableExist("meta")); |
| 1572 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1573 | ASSERT_TRUE(MetaTable::GetMmapStatus(&db(), &mmap_status)); |
| 1574 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1575 | |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1576 | // Preload with partial progress of one page. Should map everything. |
| 1577 | ASSERT_TRUE(db().Execute("REPLACE INTO meta VALUES ('mmap_status', 1)")); |
| 1578 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1579 | ASSERT_TRUE(MetaTable::GetMmapStatus(&db(), &mmap_status)); |
| 1580 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1581 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1582 | // Failure status maps nothing. |
| 1583 | ASSERT_TRUE(db().Execute("REPLACE INTO meta VALUES ('mmap_status', -2)")); |
| 1584 | ASSERT_EQ(0UL, db().GetAppropriateMmapSize()); |
| 1585 | |
| 1586 | // Re-initializing the meta table does not re-create the key if the table |
| 1587 | // already exists. |
| 1588 | ASSERT_TRUE(db().Execute("DELETE FROM meta WHERE key = 'mmap_status'")); |
| 1589 | MetaTable().Init(&db(), 1, 1); |
| 1590 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1591 | ASSERT_TRUE(MetaTable::GetMmapStatus(&db(), &mmap_status)); |
| 1592 | ASSERT_EQ(0, mmap_status); |
| 1593 | |
| 1594 | // With no key, map everything and create the key. |
| 1595 | // TODO(shess): This really should be "maps everything after validating it", |
| 1596 | // but that is more complicated to structure. |
| 1597 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1598 | ASSERT_TRUE(MetaTable::GetMmapStatus(&db(), &mmap_status)); |
| 1599 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1600 | } |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1601 | |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1602 | TEST_F(SQLConnectionTest, GetAppropriateMmapSizeAltStatus) { |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1603 | const size_t kMmapAlot = 25 * 1024 * 1024; |
| 1604 | |
| 1605 | // At this point, Connection still expects a future [meta] table. |
| 1606 | ASSERT_FALSE(db().DoesTableExist("meta")); |
| 1607 | ASSERT_FALSE(db().DoesViewExist("MmapStatus")); |
| 1608 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1609 | ASSERT_FALSE(db().DoesTableExist("meta")); |
| 1610 | ASSERT_FALSE(db().DoesViewExist("MmapStatus")); |
| 1611 | |
| 1612 | // Using alt status, everything should be mapped, with state in the view. |
| 1613 | db().set_mmap_alt_status(); |
| 1614 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1615 | ASSERT_FALSE(db().DoesTableExist("meta")); |
| 1616 | ASSERT_TRUE(db().DoesViewExist("MmapStatus")); |
| 1617 | EXPECT_EQ(base::IntToString(MetaTable::kMmapSuccess), |
| 1618 | ExecuteWithResult(&db(), "SELECT * FROM MmapStatus")); |
| 1619 | |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1620 | // Also maps everything when kMmapSuccess is already in the view. |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1621 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1622 | |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1623 | // Preload with partial progress of one page. Should map everything. |
| 1624 | ASSERT_TRUE(db().Execute("DROP VIEW MmapStatus")); |
| 1625 | ASSERT_TRUE(db().Execute("CREATE VIEW MmapStatus (value) AS SELECT 1")); |
| 1626 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1627 | EXPECT_EQ(base::IntToString(MetaTable::kMmapSuccess), |
| 1628 | ExecuteWithResult(&db(), "SELECT * FROM MmapStatus")); |
| 1629 | |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1630 | // Failure status leads to nothing being mapped. |
| 1631 | ASSERT_TRUE(db().Execute("DROP VIEW MmapStatus")); |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1632 | ASSERT_TRUE(db().Execute("CREATE VIEW MmapStatus (value) AS SELECT -2")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1633 | ASSERT_EQ(0UL, db().GetAppropriateMmapSize()); |
| 1634 | EXPECT_EQ(base::IntToString(MetaTable::kMmapFailure), |
| 1635 | ExecuteWithResult(&db(), "SELECT * FROM MmapStatus")); |
| 1636 | } |
| 1637 | |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1638 | // To prevent invalid SQL from accidentally shipping to production, prepared |
Sigurdur Asgeirsson | 8d82bd0 | 2017-09-25 21:05:52 | [diff] [blame] | 1639 | // statements which fail to compile with SQLITE_ERROR call DLOG(DCHECK). This |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1640 | // case cannot be suppressed with an error callback. |
| 1641 | TEST_F(SQLConnectionTest, CompileError) { |
Scott Graham | 57ee5482 | 2017-09-13 06:37:56 | [diff] [blame] | 1642 | // DEATH tests not supported on Android, iOS, or Fuchsia. |
| 1643 | #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_FUCHSIA) |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1644 | if (DLOG_IS_ON(FATAL)) { |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 1645 | db().set_error_callback(base::BindRepeating(&IgnoreErrorCallback)); |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1646 | ASSERT_DEATH({ |
| 1647 | db().GetUniqueStatement("SELECT x"); |
| 1648 | }, "SQL compile error no such column: x"); |
| 1649 | } |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1650 | #endif // !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_FUCHSIA) |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1651 | } |
| 1652 | |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1653 | } // namespace sql |