[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" |
Victor Costan | 613b430 | 2018-11-20 05:32:43 | [diff] [blame] | 15 | #include "base/test/gtest_util.h" |
Devlin Cronin | 147687f | 2018-06-05 18:03:56 | [diff] [blame] | 16 | #include "base/test/metrics/histogram_tester.h" |
Victor Costan | 4c2f3e92 | 2018-08-21 04:47:59 | [diff] [blame] | 17 | #include "base/test/scoped_feature_list.h" |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 18 | #include "base/trace_event/process_memory_dump.h" |
Scott Graham | 57ee5482 | 2017-09-13 06:37:56 | [diff] [blame] | 19 | #include "build/build_config.h" |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 20 | #include "sql/database.h" |
| 21 | #include "sql/database_memory_dump_provider.h" |
[email protected] | 1348765a | 2012-07-24 08:25:53 | [diff] [blame] | 22 | #include "sql/meta_table.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 23 | #include "sql/statement.h" |
Victor Costan | d1a217b | 2019-04-02 02:44:21 | [diff] [blame] | 24 | #include "sql/test/database_test_peer.h" |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 25 | #include "sql/test/error_callback_support.h" |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 26 | #include "sql/test/scoped_error_expecter.h" |
Scott Graham | 47ed2c3 | 2017-09-15 02:17:07 | [diff] [blame] | 27 | #include "sql/test/sql_test_base.h" |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 28 | #include "sql/test/test_helpers.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 29 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 30 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 31 | |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 32 | namespace sql { |
Victor Costan | 7f6abbbe | 2018-07-29 02:57:27 | [diff] [blame] | 33 | |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 34 | namespace { |
| 35 | |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 36 | using sql::test::ExecuteWithResult; |
| 37 | |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 38 | // Helper to return the count of items in sqlite_master. Return -1 in |
| 39 | // case of error. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 40 | int SqliteMasterCount(sql::Database* db) { |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 41 | const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master"; |
| 42 | sql::Statement s(db->GetUniqueStatement(kMasterCount)); |
| 43 | return s.Step() ? s.ColumnInt(0) : -1; |
| 44 | } |
| 45 | |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 46 | // Track the number of valid references which share the same pointer. |
| 47 | // This is used to allow testing an implicitly use-after-free case by |
| 48 | // explicitly having the ref count live longer than the object. |
| 49 | class RefCounter { |
| 50 | public: |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 51 | RefCounter(size_t* counter) : counter_(counter) { (*counter_)++; } |
| 52 | RefCounter(const RefCounter& other) : counter_(other.counter_) { |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 53 | (*counter_)++; |
| 54 | } |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 55 | ~RefCounter() { (*counter_)--; } |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 56 | |
| 57 | private: |
| 58 | size_t* counter_; |
| 59 | |
| 60 | DISALLOW_ASSIGN(RefCounter); |
| 61 | }; |
| 62 | |
| 63 | // Empty callback for implementation of ErrorCallbackSetHelper(). |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 64 | void IgnoreErrorCallback(int error, sql::Statement* stmt) {} |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 65 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 66 | void ErrorCallbackSetHelper(sql::Database* db, |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 67 | size_t* counter, |
| 68 | const RefCounter& r, |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 69 | int error, |
| 70 | sql::Statement* stmt) { |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 71 | // The ref count should not go to zero when changing the callback. |
| 72 | EXPECT_GT(*counter, 0u); |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 73 | db->set_error_callback(base::BindRepeating(&IgnoreErrorCallback)); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 74 | EXPECT_GT(*counter, 0u); |
| 75 | } |
| 76 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 77 | void ErrorCallbackResetHelper(sql::Database* db, |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 78 | size_t* counter, |
| 79 | const RefCounter& r, |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 80 | int error, |
| 81 | sql::Statement* stmt) { |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 82 | // The ref count should not go to zero when clearing the callback. |
| 83 | EXPECT_GT(*counter, 0u); |
| 84 | db->reset_error_callback(); |
| 85 | EXPECT_GT(*counter, 0u); |
| 86 | } |
| 87 | |
shess | 1cf87f2 | 2016-10-25 22:18:29 | [diff] [blame] | 88 | // Handle errors by blowing away the database. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 89 | void RazeErrorCallback(sql::Database* db, |
shess | 1cf87f2 | 2016-10-25 22:18:29 | [diff] [blame] | 90 | int expected_error, |
| 91 | int error, |
| 92 | sql::Statement* stmt) { |
| 93 | // Nothing here needs extended errors at this time. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 94 | EXPECT_EQ(expected_error, expected_error & 0xff); |
| 95 | EXPECT_EQ(expected_error, error & 0xff); |
shess | 1cf87f2 | 2016-10-25 22:18:29 | [diff] [blame] | 96 | db->RazeAndClose(); |
| 97 | } |
| 98 | |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 99 | #if defined(OS_POSIX) |
| 100 | // Set a umask and restore the old mask on destruction. Cribbed from |
| 101 | // shared_memory_unittest.cc. Used by POSIX-only UserPermission test. |
| 102 | class ScopedUmaskSetter { |
| 103 | public: |
| 104 | explicit ScopedUmaskSetter(mode_t target_mask) { |
| 105 | old_umask_ = umask(target_mask); |
| 106 | } |
| 107 | ~ScopedUmaskSetter() { umask(old_umask_); } |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 108 | |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 109 | private: |
| 110 | mode_t old_umask_; |
| 111 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter); |
| 112 | }; |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 113 | #endif // defined(OS_POSIX) |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 114 | |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 115 | } // namespace |
| 116 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 117 | using SQLDatabaseTest = sql::SQLTestBase; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 118 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 119 | TEST_F(SQLDatabaseTest, Execute) { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 120 | // Valid statement should return true. |
| 121 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 122 | EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); |
| 123 | |
| 124 | // Invalid statement should fail. |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 125 | ASSERT_EQ(SQLITE_ERROR, |
| 126 | db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 127 | EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode()); |
| 128 | } |
| 129 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 130 | TEST_F(SQLDatabaseTest, ExecuteWithErrorCode) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 131 | ASSERT_EQ(SQLITE_OK, |
| 132 | db().ExecuteAndReturnErrorCode("CREATE TABLE foo (a, b)")); |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 133 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode("CREATE TABLE TABLE")); |
| 134 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( |
| 135 | "INSERT INTO foo(a, b) VALUES (1, 2, 3, 4)")); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 136 | } |
| 137 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 138 | TEST_F(SQLDatabaseTest, CachedStatement) { |
Victor Costan | 12daa3ac9 | 2018-07-19 01:05:58 | [diff] [blame] | 139 | sql::StatementID id1 = SQL_FROM_HERE; |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 140 | sql::StatementID id2 = SQL_FROM_HERE; |
Victor Costan | 613b430 | 2018-11-20 05:32:43 | [diff] [blame] | 141 | static const char kId1Sql[] = "SELECT a FROM foo"; |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 142 | static const char kId2Sql[] = "SELECT b FROM foo"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 143 | |
| 144 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 145 | ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)")); |
| 146 | |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 147 | sqlite3_stmt* raw_id1_statement; |
| 148 | sqlite3_stmt* raw_id2_statement; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 149 | { |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 150 | scoped_refptr<sql::Database::StatementRef> ref_from_id1 = |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 151 | db().GetCachedStatement(id1, kId1Sql); |
| 152 | raw_id1_statement = ref_from_id1->stmt(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 153 | |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 154 | sql::Statement from_id1(std::move(ref_from_id1)); |
| 155 | ASSERT_TRUE(from_id1.is_valid()); |
| 156 | ASSERT_TRUE(from_id1.Step()); |
| 157 | EXPECT_EQ(12, from_id1.ColumnInt(0)); |
| 158 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 159 | scoped_refptr<sql::Database::StatementRef> ref_from_id2 = |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 160 | db().GetCachedStatement(id2, kId2Sql); |
| 161 | raw_id2_statement = ref_from_id2->stmt(); |
| 162 | EXPECT_NE(raw_id1_statement, raw_id2_statement); |
| 163 | |
| 164 | sql::Statement from_id2(std::move(ref_from_id2)); |
| 165 | ASSERT_TRUE(from_id2.is_valid()); |
| 166 | ASSERT_TRUE(from_id2.Step()); |
| 167 | EXPECT_EQ(13, from_id2.ColumnInt(0)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 170 | { |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 171 | scoped_refptr<sql::Database::StatementRef> ref_from_id1 = |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 172 | db().GetCachedStatement(id1, kId1Sql); |
| 173 | EXPECT_EQ(raw_id1_statement, ref_from_id1->stmt()) |
| 174 | << "statement was not cached"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 175 | |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 176 | sql::Statement from_id1(std::move(ref_from_id1)); |
| 177 | ASSERT_TRUE(from_id1.is_valid()); |
| 178 | ASSERT_TRUE(from_id1.Step()) << "cached statement was not reset"; |
| 179 | EXPECT_EQ(12, from_id1.ColumnInt(0)); |
| 180 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 181 | scoped_refptr<sql::Database::StatementRef> ref_from_id2 = |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 182 | db().GetCachedStatement(id2, kId2Sql); |
| 183 | EXPECT_EQ(raw_id2_statement, ref_from_id2->stmt()) |
| 184 | << "statement was not cached"; |
| 185 | |
| 186 | sql::Statement from_id2(std::move(ref_from_id2)); |
| 187 | ASSERT_TRUE(from_id2.is_valid()); |
| 188 | ASSERT_TRUE(from_id2.Step()) << "cached statement was not reset"; |
| 189 | EXPECT_EQ(13, from_id2.ColumnInt(0)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 190 | } |
Victor Costan | 613b430 | 2018-11-20 05:32:43 | [diff] [blame] | 191 | |
| 192 | EXPECT_DCHECK_DEATH(db().GetCachedStatement(id1, kId2Sql)) |
| 193 | << "Using a different SQL with the same statement ID should DCHECK"; |
| 194 | EXPECT_DCHECK_DEATH(db().GetCachedStatement(id2, kId1Sql)) |
| 195 | << "Using a different SQL with the same statement ID should DCHECK"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 196 | } |
| 197 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 198 | TEST_F(SQLDatabaseTest, IsSQLValidTest) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 199 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 200 | ASSERT_TRUE(db().IsSQLValid("SELECT a FROM foo")); |
| 201 | ASSERT_FALSE(db().IsSQLValid("SELECT no_exist FROM foo")); |
| 202 | } |
| 203 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 204 | TEST_F(SQLDatabaseTest, DoesTableExist) { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 205 | EXPECT_FALSE(db().DoesTableExist("foo")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 206 | EXPECT_FALSE(db().DoesTableExist("foo_index")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 207 | |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 208 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 209 | ASSERT_TRUE(db().Execute("CREATE INDEX foo_index ON foo (a)")); |
| 210 | EXPECT_TRUE(db().DoesTableExist("foo")); |
| 211 | EXPECT_FALSE(db().DoesTableExist("foo_index")); |
Victor Costan | f85512e5 | 2019-04-10 20:51:36 | [diff] [blame] | 212 | |
| 213 | // DoesTableExist() is case-sensitive. |
| 214 | EXPECT_FALSE(db().DoesTableExist("Foo")); |
| 215 | EXPECT_FALSE(db().DoesTableExist("FOO")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 216 | } |
| 217 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 218 | TEST_F(SQLDatabaseTest, DoesIndexExist) { |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 219 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 220 | EXPECT_FALSE(db().DoesIndexExist("foo")); |
| 221 | EXPECT_FALSE(db().DoesIndexExist("foo_ubdex")); |
| 222 | |
| 223 | ASSERT_TRUE(db().Execute("CREATE INDEX foo_index ON foo (a)")); |
| 224 | EXPECT_TRUE(db().DoesIndexExist("foo_index")); |
| 225 | EXPECT_FALSE(db().DoesIndexExist("foo")); |
Victor Costan | f85512e5 | 2019-04-10 20:51:36 | [diff] [blame] | 226 | |
| 227 | // DoesIndexExist() is case-sensitive. |
| 228 | EXPECT_FALSE(db().DoesIndexExist("Foo_index")); |
| 229 | EXPECT_FALSE(db().DoesIndexExist("Foo_Index")); |
| 230 | EXPECT_FALSE(db().DoesIndexExist("FOO_INDEX")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 231 | } |
| 232 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 233 | TEST_F(SQLDatabaseTest, DoesViewExist) { |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 234 | EXPECT_FALSE(db().DoesViewExist("voo")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 235 | ASSERT_TRUE(db().Execute("CREATE VIEW voo (a) AS SELECT 1")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 236 | EXPECT_FALSE(db().DoesIndexExist("voo")); |
| 237 | EXPECT_FALSE(db().DoesTableExist("voo")); |
| 238 | EXPECT_TRUE(db().DoesViewExist("voo")); |
Victor Costan | f85512e5 | 2019-04-10 20:51:36 | [diff] [blame] | 239 | |
| 240 | // DoesTableExist() is case-sensitive. |
| 241 | EXPECT_FALSE(db().DoesViewExist("Voo")); |
| 242 | EXPECT_FALSE(db().DoesViewExist("VOO")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 243 | } |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 244 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 245 | TEST_F(SQLDatabaseTest, DoesColumnExist) { |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 246 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 247 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 248 | EXPECT_FALSE(db().DoesColumnExist("foo", "bar")); |
| 249 | EXPECT_TRUE(db().DoesColumnExist("foo", "a")); |
| 250 | |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 251 | ASSERT_FALSE(db().DoesTableExist("bar")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 252 | EXPECT_FALSE(db().DoesColumnExist("bar", "b")); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 253 | |
Victor Costan | f85512e5 | 2019-04-10 20:51:36 | [diff] [blame] | 254 | // SQLite resolves table/column names without case sensitivity. |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 255 | EXPECT_TRUE(db().DoesColumnExist("FOO", "A")); |
Victor Costan | f85512e5 | 2019-04-10 20:51:36 | [diff] [blame] | 256 | EXPECT_TRUE(db().DoesColumnExist("FOO", "a")); |
| 257 | EXPECT_TRUE(db().DoesColumnExist("foo", "A")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 258 | } |
| 259 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 260 | TEST_F(SQLDatabaseTest, GetLastInsertRowId) { |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 261 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)")); |
| 262 | |
| 263 | ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); |
| 264 | |
| 265 | // Last insert row ID should be valid. |
tfarina | 720d4f3 | 2015-05-11 22:31:26 | [diff] [blame] | 266 | int64_t row = db().GetLastInsertRowId(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 267 | EXPECT_LT(0, row); |
| 268 | |
| 269 | // It should be the primary key of the row we just inserted. |
| 270 | sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo WHERE id=?")); |
| 271 | s.BindInt64(0, row); |
| 272 | ASSERT_TRUE(s.Step()); |
| 273 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 274 | } |
[email protected] | 44ad7d90 | 2012-03-23 00:09:05 | [diff] [blame] | 275 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 276 | TEST_F(SQLDatabaseTest, Rollback) { |
[email protected] | 44ad7d90 | 2012-03-23 00:09:05 | [diff] [blame] | 277 | ASSERT_TRUE(db().BeginTransaction()); |
| 278 | ASSERT_TRUE(db().BeginTransaction()); |
| 279 | EXPECT_EQ(2, db().transaction_nesting()); |
| 280 | db().RollbackTransaction(); |
| 281 | EXPECT_FALSE(db().CommitTransaction()); |
| 282 | EXPECT_TRUE(db().BeginTransaction()); |
| 283 | } |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 284 | |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 285 | // Test the scoped error expecter by attempting to insert a duplicate |
[email protected] | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 286 | // value into an index. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 287 | TEST_F(SQLDatabaseTest, ScopedErrorExpecter) { |
[email protected] | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 288 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
| 289 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 290 | ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
| 291 | |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 292 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 293 | sql::test::ScopedErrorExpecter expecter; |
| 294 | expecter.ExpectError(SQLITE_CONSTRAINT); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 295 | ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 296 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | // Test that clients of GetUntrackedStatement() can test corruption-handling |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 301 | // with ScopedErrorExpecter. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 302 | TEST_F(SQLDatabaseTest, ScopedIgnoreUntracked) { |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 303 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
| 304 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 305 | ASSERT_FALSE(db().DoesTableExist("bar")); |
| 306 | ASSERT_TRUE(db().DoesTableExist("foo")); |
| 307 | ASSERT_TRUE(db().DoesColumnExist("foo", "id")); |
| 308 | db().Close(); |
| 309 | |
| 310 | // Corrupt the database so that nothing works, including PRAGMAs. |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 311 | ASSERT_TRUE(CorruptSizeInHeaderOfDB()); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 312 | |
| 313 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 314 | sql::test::ScopedErrorExpecter expecter; |
| 315 | expecter.ExpectError(SQLITE_CORRUPT); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 316 | ASSERT_TRUE(db().Open(db_path())); |
| 317 | ASSERT_FALSE(db().DoesTableExist("bar")); |
| 318 | ASSERT_FALSE(db().DoesTableExist("foo")); |
| 319 | ASSERT_FALSE(db().DoesColumnExist("foo", "id")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 320 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 321 | } |
[email protected] | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 322 | } |
| 323 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 324 | TEST_F(SQLDatabaseTest, ErrorCallback) { |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 325 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
| 326 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 327 | ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
| 328 | |
| 329 | int error = SQLITE_OK; |
| 330 | { |
| 331 | sql::ScopedErrorCallback sec( |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 332 | &db(), base::BindRepeating(&sql::CaptureErrorCallback, &error)); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 333 | EXPECT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 334 | |
| 335 | // Later versions of SQLite throw SQLITE_CONSTRAINT_UNIQUE. The specific |
| 336 | // sub-error isn't really important. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 337 | EXPECT_EQ(SQLITE_CONSTRAINT, (error & 0xff)); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | // Callback is no longer in force due to reset. |
| 341 | { |
| 342 | error = SQLITE_OK; |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 343 | sql::test::ScopedErrorExpecter expecter; |
| 344 | expecter.ExpectError(SQLITE_CONSTRAINT); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 345 | ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 346 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 347 | EXPECT_EQ(SQLITE_OK, error); |
| 348 | } |
| 349 | |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 350 | // base::BindRepeating() can curry arguments to be passed by const reference |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 351 | // to the callback function. If the callback function calls |
| 352 | // re/set_error_callback(), the storage for those arguments can be |
| 353 | // deleted while the callback function is still executing. |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 354 | // |
| 355 | // RefCounter() counts how many objects are live using an external |
| 356 | // count. The same counter is passed to the callback, so that it |
| 357 | // can check directly even if the RefCounter object is no longer |
| 358 | // live. |
| 359 | { |
| 360 | size_t count = 0; |
| 361 | sql::ScopedErrorCallback sec( |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 362 | &db(), base::BindRepeating(&ErrorCallbackSetHelper, &db(), &count, |
| 363 | RefCounter(&count))); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 364 | |
| 365 | EXPECT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
| 366 | } |
| 367 | |
| 368 | // Same test, but reset_error_callback() case. |
| 369 | { |
| 370 | size_t count = 0; |
| 371 | sql::ScopedErrorCallback sec( |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 372 | &db(), base::BindRepeating(&ErrorCallbackResetHelper, &db(), &count, |
| 373 | RefCounter(&count))); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 374 | |
| 375 | EXPECT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
| 376 | } |
| 377 | } |
| 378 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 379 | // Test that sql::Database::Raze() results in a database without the |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 380 | // tables from the original database. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 381 | TEST_F(SQLDatabaseTest, Raze) { |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 382 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 383 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 384 | ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); |
| 385 | |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 386 | int pragma_auto_vacuum = 0; |
| 387 | { |
| 388 | sql::Statement s(db().GetUniqueStatement("PRAGMA auto_vacuum")); |
| 389 | ASSERT_TRUE(s.Step()); |
| 390 | pragma_auto_vacuum = s.ColumnInt(0); |
| 391 | ASSERT_TRUE(pragma_auto_vacuum == 0 || pragma_auto_vacuum == 1); |
| 392 | } |
| 393 | |
| 394 | // If auto_vacuum is set, there's an extra page to maintain a freelist. |
| 395 | const int kExpectedPageCount = 2 + pragma_auto_vacuum; |
| 396 | |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 397 | { |
| 398 | sql::Statement s(db().GetUniqueStatement("PRAGMA page_count")); |
| 399 | ASSERT_TRUE(s.Step()); |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 400 | EXPECT_EQ(kExpectedPageCount, s.ColumnInt(0)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | { |
| 404 | sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); |
| 405 | ASSERT_TRUE(s.Step()); |
| 406 | EXPECT_EQ("table", s.ColumnString(0)); |
| 407 | EXPECT_EQ("foo", s.ColumnString(1)); |
| 408 | EXPECT_EQ("foo", s.ColumnString(2)); |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 409 | // Table "foo" is stored in the last page of the file. |
| 410 | EXPECT_EQ(kExpectedPageCount, s.ColumnInt(3)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 411 | EXPECT_EQ(kCreateSql, s.ColumnString(4)); |
| 412 | } |
| 413 | |
| 414 | ASSERT_TRUE(db().Raze()); |
| 415 | |
| 416 | { |
| 417 | sql::Statement s(db().GetUniqueStatement("PRAGMA page_count")); |
| 418 | ASSERT_TRUE(s.Step()); |
| 419 | EXPECT_EQ(1, s.ColumnInt(0)); |
| 420 | } |
| 421 | |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 422 | ASSERT_EQ(0, SqliteMasterCount(&db())); |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 423 | |
| 424 | { |
| 425 | sql::Statement s(db().GetUniqueStatement("PRAGMA auto_vacuum")); |
| 426 | ASSERT_TRUE(s.Step()); |
[email protected] | 6d42f15 | 2012-11-10 00:38:24 | [diff] [blame] | 427 | // The new database has the same auto_vacuum as a fresh database. |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 428 | EXPECT_EQ(pragma_auto_vacuum, s.ColumnInt(0)); |
| 429 | } |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 430 | } |
| 431 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 432 | // Helper for SQLDatabaseTest.RazePageSize. Creates a fresh db based on |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 433 | // db_prefix, with the given initial page size, and verifies it against the |
| 434 | // expected size. Then changes to the final page size and razes, verifying that |
| 435 | // the fresh database ends up with the expected final page size. |
| 436 | void TestPageSize(const base::FilePath& db_prefix, |
| 437 | int initial_page_size, |
| 438 | const std::string& expected_initial_page_size, |
| 439 | int final_page_size, |
| 440 | const std::string& expected_final_page_size) { |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 441 | static const char kCreateSql[] = "CREATE TABLE x (t TEXT)"; |
| 442 | static const char kInsertSql1[] = "INSERT INTO x VALUES ('This is a test')"; |
| 443 | static const char kInsertSql2[] = "INSERT INTO x VALUES ('That was a test')"; |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 444 | |
| 445 | const base::FilePath db_path = db_prefix.InsertBeforeExtensionASCII( |
Raul Tambre | 6c708e3 | 2019-02-08 22:35:14 | [diff] [blame] | 446 | base::NumberToString(initial_page_size)); |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 447 | sql::Database::Delete(db_path); |
| 448 | sql::Database db; |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 449 | db.set_page_size(initial_page_size); |
| 450 | ASSERT_TRUE(db.Open(db_path)); |
| 451 | ASSERT_TRUE(db.Execute(kCreateSql)); |
| 452 | ASSERT_TRUE(db.Execute(kInsertSql1)); |
| 453 | ASSERT_TRUE(db.Execute(kInsertSql2)); |
| 454 | ASSERT_EQ(expected_initial_page_size, |
| 455 | ExecuteWithResult(&db, "PRAGMA page_size")); |
| 456 | |
| 457 | // Raze will use the page size set in the connection object, which may not |
| 458 | // match the file's page size. |
| 459 | db.set_page_size(final_page_size); |
| 460 | ASSERT_TRUE(db.Raze()); |
| 461 | |
| 462 | // SQLite 3.10.2 (at least) has a quirk with the sqlite3_backup() API (used by |
| 463 | // Raze()) which causes the destination database to remember the previous |
| 464 | // page_size, even if the overwriting database changed the page_size. Access |
| 465 | // the actual database to cause the cached value to be updated. |
| 466 | EXPECT_EQ("0", ExecuteWithResult(&db, "SELECT COUNT(*) FROM sqlite_master")); |
| 467 | |
| 468 | EXPECT_EQ(expected_final_page_size, |
| 469 | ExecuteWithResult(&db, "PRAGMA page_size")); |
| 470 | EXPECT_EQ("1", ExecuteWithResult(&db, "PRAGMA page_count")); |
| 471 | } |
| 472 | |
| 473 | // Verify that sql::Recovery maintains the page size, and the virtual table |
| 474 | // works with page sizes other than SQLite's default. Also verify the case |
| 475 | // where the default page size has changed. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 476 | TEST_F(SQLDatabaseTest, RazePageSize) { |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 477 | const std::string default_page_size = |
| 478 | ExecuteWithResult(&db(), "PRAGMA page_size"); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 479 | |
Victor Costan | 7f6abbbe | 2018-07-29 02:57:27 | [diff] [blame] | 480 | // Sync uses 32k pages. |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 481 | EXPECT_NO_FATAL_FAILURE( |
| 482 | TestPageSize(db_path(), 32768, "32768", 32768, "32768")); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 483 | |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 484 | // Many clients use 4k pages. This is the SQLite default after 3.12.0. |
| 485 | EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path(), 4096, "4096", 4096, "4096")); |
| 486 | |
| 487 | // 1k is the default page size before 3.12.0. |
| 488 | EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path(), 1024, "1024", 1024, "1024")); |
| 489 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 490 | EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path(), 2048, "2048", 4096, "4096")); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 491 | |
Victor Costan | 7f6abbbe | 2018-07-29 02:57:27 | [diff] [blame] | 492 | // Databases with no page size specified should result in the default |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 493 | // page size. 2k has never been the default page size. |
| 494 | ASSERT_NE("2048", default_page_size); |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 495 | EXPECT_NO_FATAL_FAILURE(TestPageSize( |
| 496 | db_path(), 2048, "2048", Database::kDefaultPageSize, default_page_size)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // Test that Raze() results are seen in other connections. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 500 | TEST_F(SQLDatabaseTest, RazeMultiple) { |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 501 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 502 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 503 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 504 | sql::Database other_db; |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 505 | ASSERT_TRUE(other_db.Open(db_path())); |
| 506 | |
| 507 | // Check that the second connection sees the table. |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 508 | ASSERT_EQ(1, SqliteMasterCount(&other_db)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 509 | |
| 510 | ASSERT_TRUE(db().Raze()); |
| 511 | |
| 512 | // The second connection sees the updated database. |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 513 | ASSERT_EQ(0, SqliteMasterCount(&other_db)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 514 | } |
| 515 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 516 | TEST_F(SQLDatabaseTest, RazeLocked) { |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 517 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 518 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 519 | |
| 520 | // Open a transaction and write some data in a second connection. |
| 521 | // This will acquire a PENDING or EXCLUSIVE transaction, which will |
| 522 | // cause the raze to fail. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 523 | sql::Database other_db; |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 524 | ASSERT_TRUE(other_db.Open(db_path())); |
| 525 | ASSERT_TRUE(other_db.BeginTransaction()); |
| 526 | const char* kInsertSql = "INSERT INTO foo VALUES (1, 'data')"; |
| 527 | ASSERT_TRUE(other_db.Execute(kInsertSql)); |
| 528 | |
| 529 | ASSERT_FALSE(db().Raze()); |
| 530 | |
| 531 | // Works after COMMIT. |
| 532 | ASSERT_TRUE(other_db.CommitTransaction()); |
| 533 | ASSERT_TRUE(db().Raze()); |
| 534 | |
| 535 | // Re-create the database. |
| 536 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 537 | ASSERT_TRUE(db().Execute(kInsertSql)); |
| 538 | |
| 539 | // An unfinished read transaction in the other connection also |
| 540 | // blocks raze. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 541 | const char* kQuery = "SELECT COUNT(*) FROM foo"; |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 542 | sql::Statement s(other_db.GetUniqueStatement(kQuery)); |
| 543 | ASSERT_TRUE(s.Step()); |
| 544 | ASSERT_FALSE(db().Raze()); |
| 545 | |
| 546 | // Complete the statement unlocks the database. |
| 547 | ASSERT_FALSE(s.Step()); |
| 548 | ASSERT_TRUE(db().Raze()); |
| 549 | } |
| 550 | |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 551 | // Verify that Raze() can handle an empty file. SQLite should treat |
| 552 | // this as an empty database. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 553 | TEST_F(SQLDatabaseTest, RazeEmptyDB) { |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 554 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 555 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 556 | db().Close(); |
| 557 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 558 | TruncateDatabase(); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 559 | |
| 560 | ASSERT_TRUE(db().Open(db_path())); |
| 561 | ASSERT_TRUE(db().Raze()); |
| 562 | EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 563 | } |
| 564 | |
| 565 | // Verify that Raze() can handle a file of junk. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 566 | TEST_F(SQLDatabaseTest, RazeNOTADB) { |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 567 | db().Close(); |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 568 | sql::Database::Delete(db_path()); |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 569 | ASSERT_FALSE(GetPathExists(db_path())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 570 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 571 | WriteJunkToDatabase(SQLTestBase::TYPE_OVERWRITE_AND_TRUNCATE); |
| 572 | ASSERT_TRUE(GetPathExists(db_path())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 573 | |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 574 | // SQLite will successfully open the handle, but fail when running PRAGMA |
| 575 | // statements that access the database. |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 576 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 577 | sql::test::ScopedErrorExpecter expecter; |
Victor Costan | 42988a9 | 2018-02-06 02:22:14 | [diff] [blame] | 578 | expecter.ExpectError(SQLITE_NOTADB); |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 579 | |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 580 | EXPECT_TRUE(db().Open(db_path())); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 581 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 582 | } |
| 583 | EXPECT_TRUE(db().Raze()); |
| 584 | db().Close(); |
| 585 | |
| 586 | // Now empty, the open should open an empty database. |
| 587 | EXPECT_TRUE(db().Open(db_path())); |
| 588 | EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 589 | } |
| 590 | |
| 591 | // Verify that Raze() can handle a database overwritten with garbage. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 592 | TEST_F(SQLDatabaseTest, RazeNOTADB2) { |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 593 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 594 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 595 | ASSERT_EQ(1, SqliteMasterCount(&db())); |
| 596 | db().Close(); |
| 597 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 598 | WriteJunkToDatabase(SQLTestBase::TYPE_OVERWRITE); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 599 | |
| 600 | // SQLite will successfully open the handle, but will fail with |
| 601 | // SQLITE_NOTADB on pragma statemenets which attempt to read the |
| 602 | // corrupted header. |
| 603 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 604 | sql::test::ScopedErrorExpecter expecter; |
| 605 | expecter.ExpectError(SQLITE_NOTADB); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 606 | EXPECT_TRUE(db().Open(db_path())); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 607 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 608 | } |
| 609 | EXPECT_TRUE(db().Raze()); |
| 610 | db().Close(); |
| 611 | |
| 612 | // Now empty, the open should succeed with an empty database. |
| 613 | EXPECT_TRUE(db().Open(db_path())); |
| 614 | EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 615 | } |
| 616 | |
| 617 | // Test that a callback from Open() can raze the database. This is |
| 618 | // essential for cases where the Open() can fail entirely, so the |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 619 | // Raze() cannot happen later. Additionally test that when the |
| 620 | // callback does this during Open(), the open is retried and succeeds. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 621 | TEST_F(SQLDatabaseTest, RazeCallbackReopen) { |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 622 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 623 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 624 | ASSERT_EQ(1, SqliteMasterCount(&db())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 625 | db().Close(); |
| 626 | |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 627 | // Corrupt the database so that nothing works, including PRAGMAs. |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 628 | ASSERT_TRUE(CorruptSizeInHeaderOfDB()); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 629 | |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 630 | // Open() will succeed, even though the PRAGMA calls within will |
| 631 | // fail with SQLITE_CORRUPT, as will this PRAGMA. |
| 632 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 633 | sql::test::ScopedErrorExpecter expecter; |
| 634 | expecter.ExpectError(SQLITE_CORRUPT); |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 635 | ASSERT_TRUE(db().Open(db_path())); |
| 636 | ASSERT_FALSE(db().Execute("PRAGMA auto_vacuum")); |
| 637 | db().Close(); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 638 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 639 | } |
| 640 | |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 641 | db().set_error_callback( |
| 642 | base::BindRepeating(&RazeErrorCallback, &db(), SQLITE_CORRUPT)); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 643 | |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 644 | // When the PRAGMA calls in Open() raise SQLITE_CORRUPT, the error |
| 645 | // callback will call RazeAndClose(). Open() will then fail and be |
| 646 | // retried. The second Open() on the empty database will succeed |
| 647 | // cleanly. |
| 648 | ASSERT_TRUE(db().Open(db_path())); |
| 649 | ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum")); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 650 | EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 651 | } |
| 652 | |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 653 | // Basic test of RazeAndClose() operation. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 654 | TEST_F(SQLDatabaseTest, RazeAndClose) { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 655 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 656 | const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)"; |
| 657 | |
| 658 | // Test that RazeAndClose() closes the database, and that the |
| 659 | // database is empty when re-opened. |
| 660 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 661 | ASSERT_TRUE(db().Execute(kPopulateSql)); |
| 662 | ASSERT_TRUE(db().RazeAndClose()); |
| 663 | ASSERT_FALSE(db().is_open()); |
| 664 | db().Close(); |
| 665 | ASSERT_TRUE(db().Open(db_path())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 666 | ASSERT_EQ(0, SqliteMasterCount(&db())); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 667 | |
| 668 | // Test that RazeAndClose() can break transactions. |
| 669 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 670 | ASSERT_TRUE(db().Execute(kPopulateSql)); |
| 671 | ASSERT_TRUE(db().BeginTransaction()); |
| 672 | ASSERT_TRUE(db().RazeAndClose()); |
| 673 | ASSERT_FALSE(db().is_open()); |
| 674 | ASSERT_FALSE(db().CommitTransaction()); |
| 675 | db().Close(); |
| 676 | ASSERT_TRUE(db().Open(db_path())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 677 | ASSERT_EQ(0, SqliteMasterCount(&db())); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | // Test that various operations fail without crashing after |
| 681 | // RazeAndClose(). |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 682 | TEST_F(SQLDatabaseTest, RazeAndCloseDiagnostics) { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 683 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 684 | const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)"; |
| 685 | const char* kSimpleSql = "SELECT 1"; |
| 686 | |
| 687 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 688 | ASSERT_TRUE(db().Execute(kPopulateSql)); |
| 689 | |
| 690 | // Test baseline expectations. |
| 691 | db().Preload(); |
| 692 | ASSERT_TRUE(db().DoesTableExist("foo")); |
| 693 | ASSERT_TRUE(db().IsSQLValid(kSimpleSql)); |
| 694 | ASSERT_EQ(SQLITE_OK, db().ExecuteAndReturnErrorCode(kSimpleSql)); |
| 695 | ASSERT_TRUE(db().Execute(kSimpleSql)); |
| 696 | ASSERT_TRUE(db().is_open()); |
| 697 | { |
| 698 | sql::Statement s(db().GetUniqueStatement(kSimpleSql)); |
| 699 | ASSERT_TRUE(s.Step()); |
| 700 | } |
| 701 | { |
| 702 | sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
| 703 | ASSERT_TRUE(s.Step()); |
| 704 | } |
| 705 | ASSERT_TRUE(db().BeginTransaction()); |
| 706 | ASSERT_TRUE(db().CommitTransaction()); |
| 707 | ASSERT_TRUE(db().BeginTransaction()); |
| 708 | db().RollbackTransaction(); |
| 709 | |
| 710 | ASSERT_TRUE(db().RazeAndClose()); |
| 711 | |
| 712 | // At this point, they should all fail, but not crash. |
| 713 | db().Preload(); |
| 714 | ASSERT_FALSE(db().DoesTableExist("foo")); |
| 715 | ASSERT_FALSE(db().IsSQLValid(kSimpleSql)); |
| 716 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(kSimpleSql)); |
| 717 | ASSERT_FALSE(db().Execute(kSimpleSql)); |
| 718 | ASSERT_FALSE(db().is_open()); |
| 719 | { |
| 720 | sql::Statement s(db().GetUniqueStatement(kSimpleSql)); |
| 721 | ASSERT_FALSE(s.Step()); |
| 722 | } |
| 723 | { |
| 724 | sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
| 725 | ASSERT_FALSE(s.Step()); |
| 726 | } |
| 727 | ASSERT_FALSE(db().BeginTransaction()); |
| 728 | ASSERT_FALSE(db().CommitTransaction()); |
| 729 | ASSERT_FALSE(db().BeginTransaction()); |
| 730 | db().RollbackTransaction(); |
| 731 | |
| 732 | // Close normally to reset the poisoned flag. |
| 733 | db().Close(); |
| 734 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 735 | // DEATH tests not supported on Android, iOS, or Fuchsia. |
Scott Graham | 57ee5482 | 2017-09-13 06:37:56 | [diff] [blame] | 736 | #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_FUCHSIA) |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 737 | // Once the real Close() has been called, various calls enforce API |
| 738 | // usage by becoming fatal in debug mode. Since DEATH tests are |
| 739 | // expensive, just test one of them. |
| 740 | if (DLOG_IS_ON(FATAL)) { |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 741 | ASSERT_DEATH({ db().IsSQLValid(kSimpleSql); }, |
| 742 | "Illegal use of Database without a db"); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 743 | } |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 744 | #endif // !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_FUCHSIA) |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | // TODO(shess): Spin up a background thread to hold other_db, to more |
| 748 | // closely match real life. That would also allow testing |
| 749 | // RazeWithTimeout(). |
| 750 | |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 751 | // On Windows, truncate silently fails against a memory-mapped file. One goal |
| 752 | // of Raze() is to truncate the file to remove blocks which generate I/O errors. |
| 753 | // Test that Raze() turns off memory mapping so that the file is truncated. |
| 754 | // [This would not cover the case of multiple connections where one of the other |
| 755 | // connections is memory-mapped. That is infrequent in Chromium.] |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 756 | TEST_F(SQLDatabaseTest, RazeTruncate) { |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 757 | // The empty database has 0 or 1 pages. Raze() should leave it with exactly 1 |
| 758 | // page. Not checking directly because auto_vacuum on Android adds a freelist |
| 759 | // page. |
| 760 | ASSERT_TRUE(db().Raze()); |
| 761 | int64_t expected_size; |
| 762 | ASSERT_TRUE(base::GetFileSize(db_path(), &expected_size)); |
| 763 | ASSERT_GT(expected_size, 0); |
| 764 | |
| 765 | // Cause the database to take a few pages. |
| 766 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 767 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 768 | for (size_t i = 0; i < 24; ++i) { |
| 769 | ASSERT_TRUE( |
| 770 | db().Execute("INSERT INTO foo (value) VALUES (randomblob(1024))")); |
| 771 | } |
| 772 | int64_t db_size; |
| 773 | ASSERT_TRUE(base::GetFileSize(db_path(), &db_size)); |
| 774 | ASSERT_GT(db_size, expected_size); |
| 775 | |
| 776 | // Make a query covering most of the database file to make sure that the |
| 777 | // blocks are actually mapped into memory. Empirically, the truncate problem |
| 778 | // doesn't seem to happen if no blocks are mapped. |
| 779 | EXPECT_EQ("24576", |
| 780 | ExecuteWithResult(&db(), "SELECT SUM(LENGTH(value)) FROM foo")); |
| 781 | |
| 782 | ASSERT_TRUE(db().Raze()); |
| 783 | ASSERT_TRUE(base::GetFileSize(db_path(), &db_size)); |
| 784 | ASSERT_EQ(expected_size, db_size); |
| 785 | } |
| 786 | |
[email protected] | 1348765a | 2012-07-24 08:25:53 | [diff] [blame] | 787 | #if defined(OS_ANDROID) |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 788 | TEST_F(SQLDatabaseTest, SetTempDirForSQL) { |
[email protected] | 1348765a | 2012-07-24 08:25:53 | [diff] [blame] | 789 | sql::MetaTable meta_table; |
| 790 | // Below call needs a temporary directory in sqlite3 |
| 791 | // On Android, it can pass only when the temporary directory is set. |
| 792 | // Otherwise, sqlite3 doesn't find the correct directory to store |
| 793 | // temporary files and will report the error 'unable to open |
| 794 | // database file'. |
| 795 | ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); |
| 796 | } |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 797 | #endif // defined(OS_ANDROID) |
[email protected] | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 798 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 799 | TEST_F(SQLDatabaseTest, DeleteNonWal) { |
[email protected] | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 800 | EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); |
| 801 | db().Close(); |
| 802 | |
| 803 | // Should have both a main database file and a journal file because |
shess | 2c21ecf | 2015-06-02 01:31:09 | [diff] [blame] | 804 | // of journal_mode TRUNCATE. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 805 | base::FilePath journal_path = sql::Database::JournalPath(db_path()); |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 806 | ASSERT_TRUE(GetPathExists(db_path())); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 807 | ASSERT_TRUE(GetPathExists(journal_path)); |
[email protected] | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 808 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 809 | sql::Database::Delete(db_path()); |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 810 | EXPECT_FALSE(GetPathExists(db_path())); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 811 | EXPECT_FALSE(GetPathExists(journal_path)); |
[email protected] | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 812 | } |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 813 | |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 814 | #if defined(OS_POSIX) // This test operates on POSIX file permissions. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 815 | TEST_F(SQLDatabaseTest, PosixFilePermissions) { |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 816 | db().Close(); |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 817 | sql::Database::Delete(db_path()); |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 818 | ASSERT_FALSE(GetPathExists(db_path())); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 819 | |
| 820 | // If the bots all had a restrictive umask setting such that databases are |
| 821 | // always created with only the owner able to read them, then the code could |
| 822 | // break without breaking the tests. Temporarily provide a more permissive |
| 823 | // umask. |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 824 | ScopedUmaskSetter permissive_umask(S_IWGRP | S_IWOTH); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 825 | |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 826 | ASSERT_TRUE(db().Open(db_path())); |
| 827 | |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 828 | // Cause the journal file to be created. If the default journal_mode is |
| 829 | // changed back to DELETE, this test will need to be updated. |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 830 | EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); |
| 831 | |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 832 | int mode; |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 833 | ASSERT_TRUE(GetPathExists(db_path())); |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 834 | EXPECT_TRUE(base::GetPosixFilePermissions(db_path(), &mode)); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 835 | ASSERT_EQ(mode, 0600); |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 836 | |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 837 | { |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 838 | base::FilePath journal_path = sql::Database::JournalPath(db_path()); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 839 | DLOG(ERROR) << "journal_path: " << journal_path; |
| 840 | ASSERT_TRUE(GetPathExists(journal_path)); |
| 841 | EXPECT_TRUE(base::GetPosixFilePermissions(journal_path, &mode)); |
| 842 | ASSERT_EQ(mode, 0600); |
| 843 | } |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 844 | |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 845 | // Reopen the database and turn on WAL mode. |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 846 | db().Close(); |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 847 | sql::Database::Delete(db_path()); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 848 | ASSERT_FALSE(GetPathExists(db_path())); |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 849 | ASSERT_TRUE(db().Open(db_path())); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 850 | ASSERT_TRUE(db().Execute("PRAGMA journal_mode = WAL")); |
| 851 | ASSERT_EQ("wal", ExecuteWithResult(&db(), "PRAGMA journal_mode")); |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 852 | |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 853 | // The WAL file is created lazily on first change. |
| 854 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 855 | |
| 856 | { |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 857 | base::FilePath wal_path = sql::Database::WriteAheadLogPath(db_path()); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 858 | ASSERT_TRUE(GetPathExists(wal_path)); |
| 859 | EXPECT_TRUE(base::GetPosixFilePermissions(wal_path, &mode)); |
| 860 | ASSERT_EQ(mode, 0600); |
| 861 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 862 | base::FilePath shm_path = sql::Database::SharedMemoryFilePath(db_path()); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 863 | ASSERT_TRUE(GetPathExists(shm_path)); |
| 864 | EXPECT_TRUE(base::GetPosixFilePermissions(shm_path, &mode)); |
| 865 | ASSERT_EQ(mode, 0600); |
| 866 | } |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 867 | } |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 868 | #endif // defined(OS_POSIX) |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 869 | |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 870 | // Test that errors start happening once Poison() is called. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 871 | TEST_F(SQLDatabaseTest, Poison) { |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 872 | EXPECT_TRUE(db().Execute("CREATE TABLE x (x)")); |
| 873 | |
| 874 | // Before the Poison() call, things generally work. |
| 875 | EXPECT_TRUE(db().IsSQLValid("INSERT INTO x VALUES ('x')")); |
| 876 | EXPECT_TRUE(db().Execute("INSERT INTO x VALUES ('x')")); |
| 877 | { |
| 878 | sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM x")); |
| 879 | ASSERT_TRUE(s.is_valid()); |
| 880 | ASSERT_TRUE(s.Step()); |
| 881 | } |
| 882 | |
| 883 | // Get a statement which is valid before and will exist across Poison(). |
| 884 | sql::Statement valid_statement( |
| 885 | db().GetUniqueStatement("SELECT COUNT(*) FROM sqlite_master")); |
| 886 | ASSERT_TRUE(valid_statement.is_valid()); |
| 887 | ASSERT_TRUE(valid_statement.Step()); |
| 888 | valid_statement.Reset(true); |
| 889 | |
| 890 | db().Poison(); |
| 891 | |
| 892 | // After the Poison() call, things fail. |
| 893 | EXPECT_FALSE(db().IsSQLValid("INSERT INTO x VALUES ('x')")); |
| 894 | EXPECT_FALSE(db().Execute("INSERT INTO x VALUES ('x')")); |
| 895 | { |
| 896 | sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM x")); |
| 897 | ASSERT_FALSE(s.is_valid()); |
| 898 | ASSERT_FALSE(s.Step()); |
| 899 | } |
| 900 | |
| 901 | // The existing statement has become invalid. |
| 902 | ASSERT_FALSE(valid_statement.is_valid()); |
| 903 | ASSERT_FALSE(valid_statement.Step()); |
shess | 644fc8a | 2016-02-26 18:15:58 | [diff] [blame] | 904 | |
| 905 | // Test that poisoning the database during a transaction works (with errors). |
| 906 | // RazeErrorCallback() poisons the database, the extra COMMIT causes |
| 907 | // CommitTransaction() to throw an error while commiting. |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 908 | db().set_error_callback( |
| 909 | base::BindRepeating(&RazeErrorCallback, &db(), SQLITE_ERROR)); |
shess | 644fc8a | 2016-02-26 18:15:58 | [diff] [blame] | 910 | db().Close(); |
| 911 | ASSERT_TRUE(db().Open(db_path())); |
| 912 | EXPECT_TRUE(db().BeginTransaction()); |
| 913 | EXPECT_TRUE(db().Execute("INSERT INTO x VALUES ('x')")); |
| 914 | EXPECT_TRUE(db().Execute("COMMIT")); |
| 915 | EXPECT_FALSE(db().CommitTransaction()); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 916 | } |
| 917 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 918 | TEST_F(SQLDatabaseTest, AttachDatabase) { |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 919 | EXPECT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 920 | |
| 921 | // Create a database to attach to. |
| 922 | base::FilePath attach_path = |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 923 | db_path().DirName().AppendASCII("SQLDatabaseAttach.db"); |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 924 | static const char kAttachmentPoint[] = "other"; |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 925 | { |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 926 | sql::Database other_db; |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 927 | ASSERT_TRUE(other_db.Open(attach_path)); |
| 928 | EXPECT_TRUE(other_db.Execute("CREATE TABLE bar (a, b)")); |
| 929 | EXPECT_TRUE(other_db.Execute("INSERT INTO bar VALUES ('hello', 'world')")); |
| 930 | } |
| 931 | |
| 932 | // Cannot see the attached database, yet. |
| 933 | EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 934 | |
Victor Costan | 7f6abbbe | 2018-07-29 02:57:27 | [diff] [blame] | 935 | EXPECT_TRUE( |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 936 | DatabaseTestPeer::AttachDatabase(&db(), attach_path, kAttachmentPoint)); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 937 | EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 938 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 939 | // Queries can touch both databases after the ATTACH. |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 940 | EXPECT_TRUE(db().Execute("INSERT INTO foo SELECT a, b FROM other.bar")); |
| 941 | { |
| 942 | sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM foo")); |
| 943 | ASSERT_TRUE(s.Step()); |
| 944 | EXPECT_EQ(1, s.ColumnInt(0)); |
| 945 | } |
| 946 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 947 | EXPECT_TRUE(DatabaseTestPeer::DetachDatabase(&db(), kAttachmentPoint)); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 948 | EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 949 | } |
| 950 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 951 | TEST_F(SQLDatabaseTest, AttachDatabaseWithOpenTransaction) { |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 952 | EXPECT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 953 | |
| 954 | // Create a database to attach to. |
| 955 | base::FilePath attach_path = |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 956 | db_path().DirName().AppendASCII("SQLDatabaseAttach.db"); |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 957 | static const char kAttachmentPoint[] = "other"; |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 958 | { |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 959 | sql::Database other_db; |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 960 | ASSERT_TRUE(other_db.Open(attach_path)); |
| 961 | EXPECT_TRUE(other_db.Execute("CREATE TABLE bar (a, b)")); |
| 962 | EXPECT_TRUE(other_db.Execute("INSERT INTO bar VALUES ('hello', 'world')")); |
| 963 | } |
| 964 | |
| 965 | // Cannot see the attached database, yet. |
| 966 | EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 967 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 968 | // Attach succeeds in a transaction. |
| 969 | EXPECT_TRUE(db().BeginTransaction()); |
Victor Costan | 7f6abbbe | 2018-07-29 02:57:27 | [diff] [blame] | 970 | EXPECT_TRUE( |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 971 | DatabaseTestPeer::AttachDatabase(&db(), attach_path, kAttachmentPoint)); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 972 | EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 973 | |
| 974 | // Queries can touch both databases after the ATTACH. |
| 975 | EXPECT_TRUE(db().Execute("INSERT INTO foo SELECT a, b FROM other.bar")); |
| 976 | { |
| 977 | sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM foo")); |
| 978 | ASSERT_TRUE(s.Step()); |
| 979 | EXPECT_EQ(1, s.ColumnInt(0)); |
| 980 | } |
| 981 | |
| 982 | // Detaching the same database fails, database is locked in the transaction. |
| 983 | { |
| 984 | sql::test::ScopedErrorExpecter expecter; |
| 985 | expecter.ExpectError(SQLITE_ERROR); |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 986 | EXPECT_FALSE(DatabaseTestPeer::DetachDatabase(&db(), kAttachmentPoint)); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 987 | EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 988 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 989 | } |
| 990 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 991 | // Detach succeeds when the transaction is closed. |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 992 | db().RollbackTransaction(); |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 993 | EXPECT_TRUE(DatabaseTestPeer::DetachDatabase(&db(), kAttachmentPoint)); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 994 | EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
| 995 | } |
| 996 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 997 | TEST_F(SQLDatabaseTest, Basic_QuickIntegrityCheck) { |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 998 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 999 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 1000 | EXPECT_TRUE(db().QuickIntegrityCheck()); |
| 1001 | db().Close(); |
| 1002 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 1003 | ASSERT_TRUE(CorruptSizeInHeaderOfDB()); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1004 | |
| 1005 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1006 | sql::test::ScopedErrorExpecter expecter; |
| 1007 | expecter.ExpectError(SQLITE_CORRUPT); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1008 | ASSERT_TRUE(db().Open(db_path())); |
| 1009 | EXPECT_FALSE(db().QuickIntegrityCheck()); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1010 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1014 | TEST_F(SQLDatabaseTest, Basic_FullIntegrityCheck) { |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1015 | const std::string kOk("ok"); |
| 1016 | std::vector<std::string> messages; |
| 1017 | |
| 1018 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 1019 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 1020 | EXPECT_TRUE(db().FullIntegrityCheck(&messages)); |
| 1021 | EXPECT_EQ(1u, messages.size()); |
| 1022 | EXPECT_EQ(kOk, messages[0]); |
| 1023 | db().Close(); |
| 1024 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 1025 | ASSERT_TRUE(CorruptSizeInHeaderOfDB()); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1026 | |
| 1027 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1028 | sql::test::ScopedErrorExpecter expecter; |
| 1029 | expecter.ExpectError(SQLITE_CORRUPT); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1030 | ASSERT_TRUE(db().Open(db_path())); |
| 1031 | EXPECT_TRUE(db().FullIntegrityCheck(&messages)); |
| 1032 | EXPECT_LT(1u, messages.size()); |
| 1033 | EXPECT_NE(kOk, messages[0]); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1034 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | // TODO(shess): CorruptTableOrIndex could be used to produce a |
| 1038 | // file that would pass the quick check and fail the full check. |
| 1039 | } |
| 1040 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1041 | TEST_F(SQLDatabaseTest, OnMemoryDump) { |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 1042 | base::trace_event::MemoryDumpArgs args = { |
| 1043 | base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; |
erikchen | f62ea04 | 2018-05-25 21:30:57 | [diff] [blame] | 1044 | base::trace_event::ProcessMemoryDump pmd(args); |
ssid | 3be5b1ec | 2016-01-13 14:21:57 | [diff] [blame] | 1045 | ASSERT_TRUE(db().memory_dump_provider_->OnMemoryDump(args, &pmd)); |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 1046 | EXPECT_GE(pmd.allocator_dumps().size(), 1u); |
| 1047 | } |
| 1048 | |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1049 | // Test that the functions to collect diagnostic data run to completion, without |
| 1050 | // worrying too much about what they generate (since that will change). |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1051 | TEST_F(SQLDatabaseTest, CollectDiagnosticInfo) { |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1052 | const std::string corruption_info = db().CollectCorruptionInfo(); |
| 1053 | EXPECT_NE(std::string::npos, corruption_info.find("SQLITE_CORRUPT")); |
| 1054 | EXPECT_NE(std::string::npos, corruption_info.find("integrity_check")); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1055 | |
| 1056 | // A statement to see in the results. |
| 1057 | const char* kSimpleSql = "SELECT 'mountain'"; |
| 1058 | Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
| 1059 | |
| 1060 | // Error includes the statement. |
| 1061 | const std::string readonly_info = db().CollectErrorInfo(SQLITE_READONLY, &s); |
| 1062 | EXPECT_NE(std::string::npos, readonly_info.find(kSimpleSql)); |
| 1063 | |
| 1064 | // Some other error doesn't include the statment. |
| 1065 | // TODO(shess): This is weak. |
Victor Costan | bd62311 | 2018-07-18 04:17:27 | [diff] [blame] | 1066 | const std::string full_info = db().CollectErrorInfo(SQLITE_FULL, nullptr); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1067 | EXPECT_EQ(std::string::npos, full_info.find(kSimpleSql)); |
| 1068 | |
| 1069 | // A table to see in the SQLITE_ERROR results. |
| 1070 | EXPECT_TRUE(db().Execute("CREATE TABLE volcano (x)")); |
| 1071 | |
| 1072 | // Version info to see in the SQLITE_ERROR results. |
| 1073 | sql::MetaTable meta_table; |
| 1074 | ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); |
| 1075 | |
| 1076 | const std::string error_info = db().CollectErrorInfo(SQLITE_ERROR, &s); |
| 1077 | EXPECT_NE(std::string::npos, error_info.find(kSimpleSql)); |
| 1078 | EXPECT_NE(std::string::npos, error_info.find("volcano")); |
| 1079 | EXPECT_NE(std::string::npos, error_info.find("version: 4")); |
| 1080 | } |
| 1081 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1082 | // Test that a fresh database has mmap enabled by default, if mmap'ed I/O is |
| 1083 | // enabled by SQLite. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1084 | TEST_F(SQLDatabaseTest, MmapInitiallyEnabled) { |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1085 | { |
| 1086 | sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); |
Victor Costan | 42988a9 | 2018-02-06 02:22:14 | [diff] [blame] | 1087 | ASSERT_TRUE(s.Step()) |
| 1088 | << "All supported SQLite versions should have mmap support"; |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1089 | |
| 1090 | // If mmap I/O is not on, attempt to turn it on. If that succeeds, then |
| 1091 | // Open() should have turned it on. If mmap support is disabled, 0 is |
| 1092 | // returned. If the VFS does not understand SQLITE_FCNTL_MMAP_SIZE (for |
| 1093 | // instance MojoVFS), -1 is returned. |
| 1094 | if (s.ColumnInt(0) <= 0) { |
| 1095 | ASSERT_TRUE(db().Execute("PRAGMA mmap_size = 1048576")); |
| 1096 | s.Reset(true); |
| 1097 | ASSERT_TRUE(s.Step()); |
| 1098 | EXPECT_LE(s.ColumnInt(0), 0); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | // Test that explicit disable prevents mmap'ed I/O. |
| 1103 | db().Close(); |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1104 | sql::Database::Delete(db_path()); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1105 | db().set_mmap_disabled(); |
| 1106 | ASSERT_TRUE(db().Open(db_path())); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1107 | EXPECT_EQ("0", ExecuteWithResult(&db(), "PRAGMA mmap_size")); |
| 1108 | } |
| 1109 | |
| 1110 | // Test whether a fresh database gets mmap enabled when using alternate status |
| 1111 | // storage. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1112 | TEST_F(SQLDatabaseTest, MmapInitiallyEnabledAltStatus) { |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1113 | // Re-open fresh database with alt-status flag set. |
| 1114 | db().Close(); |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1115 | sql::Database::Delete(db_path()); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1116 | db().set_mmap_alt_status(); |
| 1117 | ASSERT_TRUE(db().Open(db_path())); |
| 1118 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1119 | { |
| 1120 | sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); |
Victor Costan | 42988a9 | 2018-02-06 02:22:14 | [diff] [blame] | 1121 | ASSERT_TRUE(s.Step()) |
| 1122 | << "All supported SQLite versions should have mmap support"; |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1123 | |
| 1124 | // If mmap I/O is not on, attempt to turn it on. If that succeeds, then |
| 1125 | // Open() should have turned it on. If mmap support is disabled, 0 is |
| 1126 | // returned. If the VFS does not understand SQLITE_FCNTL_MMAP_SIZE (for |
| 1127 | // instance MojoVFS), -1 is returned. |
| 1128 | if (s.ColumnInt(0) <= 0) { |
| 1129 | ASSERT_TRUE(db().Execute("PRAGMA mmap_size = 1048576")); |
| 1130 | s.Reset(true); |
| 1131 | ASSERT_TRUE(s.Step()); |
| 1132 | EXPECT_LE(s.ColumnInt(0), 0); |
| 1133 | } |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1134 | } |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1135 | |
| 1136 | // Test that explicit disable overrides set_mmap_alt_status(). |
| 1137 | db().Close(); |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1138 | sql::Database::Delete(db_path()); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1139 | db().set_mmap_disabled(); |
| 1140 | ASSERT_TRUE(db().Open(db_path())); |
| 1141 | EXPECT_EQ("0", ExecuteWithResult(&db(), "PRAGMA mmap_size")); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1142 | } |
| 1143 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1144 | TEST_F(SQLDatabaseTest, GetAppropriateMmapSize) { |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1145 | const size_t kMmapAlot = 25 * 1024 * 1024; |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1146 | int64_t mmap_status = MetaTable::kMmapFailure; |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1147 | |
| 1148 | // If there is no meta table (as for a fresh database), assume that everything |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1149 | // should be mapped, and the status of the meta table is not affected. |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1150 | ASSERT_TRUE(!db().DoesTableExist("meta")); |
| 1151 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1152 | ASSERT_TRUE(!db().DoesTableExist("meta")); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1153 | |
| 1154 | // When the meta table is first created, it sets up to map everything. |
| 1155 | MetaTable().Init(&db(), 1, 1); |
| 1156 | ASSERT_TRUE(db().DoesTableExist("meta")); |
| 1157 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1158 | ASSERT_TRUE(MetaTable::GetMmapStatus(&db(), &mmap_status)); |
| 1159 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1160 | |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1161 | // Preload with partial progress of one page. Should map everything. |
| 1162 | ASSERT_TRUE(db().Execute("REPLACE INTO meta VALUES ('mmap_status', 1)")); |
| 1163 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1164 | ASSERT_TRUE(MetaTable::GetMmapStatus(&db(), &mmap_status)); |
| 1165 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1166 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1167 | // Failure status maps nothing. |
| 1168 | ASSERT_TRUE(db().Execute("REPLACE INTO meta VALUES ('mmap_status', -2)")); |
| 1169 | ASSERT_EQ(0UL, db().GetAppropriateMmapSize()); |
| 1170 | |
| 1171 | // Re-initializing the meta table does not re-create the key if the table |
| 1172 | // already exists. |
| 1173 | ASSERT_TRUE(db().Execute("DELETE FROM meta WHERE key = 'mmap_status'")); |
| 1174 | MetaTable().Init(&db(), 1, 1); |
| 1175 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1176 | ASSERT_TRUE(MetaTable::GetMmapStatus(&db(), &mmap_status)); |
| 1177 | ASSERT_EQ(0, mmap_status); |
| 1178 | |
| 1179 | // With no key, map everything and create the key. |
| 1180 | // TODO(shess): This really should be "maps everything after validating it", |
| 1181 | // but that is more complicated to structure. |
| 1182 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1183 | ASSERT_TRUE(MetaTable::GetMmapStatus(&db(), &mmap_status)); |
| 1184 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1185 | } |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1186 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1187 | TEST_F(SQLDatabaseTest, GetAppropriateMmapSizeAltStatus) { |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1188 | const size_t kMmapAlot = 25 * 1024 * 1024; |
| 1189 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1190 | // At this point, Database still expects a future [meta] table. |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1191 | ASSERT_FALSE(db().DoesTableExist("meta")); |
| 1192 | ASSERT_FALSE(db().DoesViewExist("MmapStatus")); |
| 1193 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1194 | ASSERT_FALSE(db().DoesTableExist("meta")); |
| 1195 | ASSERT_FALSE(db().DoesViewExist("MmapStatus")); |
| 1196 | |
| 1197 | // Using alt status, everything should be mapped, with state in the view. |
| 1198 | db().set_mmap_alt_status(); |
| 1199 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1200 | ASSERT_FALSE(db().DoesTableExist("meta")); |
| 1201 | ASSERT_TRUE(db().DoesViewExist("MmapStatus")); |
Raul Tambre | 6c708e3 | 2019-02-08 22:35:14 | [diff] [blame] | 1202 | EXPECT_EQ(base::NumberToString(MetaTable::kMmapSuccess), |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1203 | ExecuteWithResult(&db(), "SELECT * FROM MmapStatus")); |
| 1204 | |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1205 | // Also maps everything when kMmapSuccess is already in the view. |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1206 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1207 | |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1208 | // Preload with partial progress of one page. Should map everything. |
| 1209 | ASSERT_TRUE(db().Execute("DROP VIEW MmapStatus")); |
| 1210 | ASSERT_TRUE(db().Execute("CREATE VIEW MmapStatus (value) AS SELECT 1")); |
| 1211 | ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
Raul Tambre | 6c708e3 | 2019-02-08 22:35:14 | [diff] [blame] | 1212 | EXPECT_EQ(base::NumberToString(MetaTable::kMmapSuccess), |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1213 | ExecuteWithResult(&db(), "SELECT * FROM MmapStatus")); |
| 1214 | |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1215 | // Failure status leads to nothing being mapped. |
| 1216 | ASSERT_TRUE(db().Execute("DROP VIEW MmapStatus")); |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1217 | ASSERT_TRUE(db().Execute("CREATE VIEW MmapStatus (value) AS SELECT -2")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1218 | ASSERT_EQ(0UL, db().GetAppropriateMmapSize()); |
Raul Tambre | 6c708e3 | 2019-02-08 22:35:14 | [diff] [blame] | 1219 | EXPECT_EQ(base::NumberToString(MetaTable::kMmapFailure), |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1220 | ExecuteWithResult(&db(), "SELECT * FROM MmapStatus")); |
| 1221 | } |
| 1222 | |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1223 | // To prevent invalid SQL from accidentally shipping to production, prepared |
Sigurdur Asgeirsson | 8d82bd0 | 2017-09-25 21:05:52 | [diff] [blame] | 1224 | // statements which fail to compile with SQLITE_ERROR call DLOG(DCHECK). This |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1225 | // case cannot be suppressed with an error callback. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1226 | TEST_F(SQLDatabaseTest, CompileError) { |
| 1227 | // DEATH tests not supported on Android, iOS, or Fuchsia. |
Scott Graham | 57ee5482 | 2017-09-13 06:37:56 | [diff] [blame] | 1228 | #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_FUCHSIA) |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1229 | if (DLOG_IS_ON(FATAL)) { |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 1230 | db().set_error_callback(base::BindRepeating(&IgnoreErrorCallback)); |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1231 | ASSERT_DEATH({ db().GetUniqueStatement("SELECT x"); }, |
| 1232 | "SQL compile error no such column: x"); |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1233 | } |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1234 | #endif // !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_FUCHSIA) |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1235 | } |
| 1236 | |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1237 | } // namespace sql |