[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 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 5 | #include "sql/database.h" |
| 6 | |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
Victor Costan | 5530932 | 2021-07-19 17:58:42 | [diff] [blame] | 9 | #include <cstdint> |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 10 | |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 11 | #include "base/bind.h" |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 13 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 14 | #include "base/logging.h" |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 15 | #include "base/strings/string_number_conversions.h" |
Victor Costan | c02b7af | 2021-07-09 13:26:45 | [diff] [blame] | 16 | #include "base/test/bind.h" |
Victor Costan | 613b430 | 2018-11-20 05:32:43 | [diff] [blame] | 17 | #include "base/test/gtest_util.h" |
Devlin Cronin | 147687f | 2018-06-05 18:03:56 | [diff] [blame] | 18 | #include "base/test/metrics/histogram_tester.h" |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 19 | #include "base/trace_event/process_memory_dump.h" |
Scott Graham | 57ee5482 | 2017-09-13 06:37:56 | [diff] [blame] | 20 | #include "build/build_config.h" |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 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" |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 23 | #include "sql/sql_features.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 24 | #include "sql/statement.h" |
Victor Costan | d1a217b | 2019-04-02 02:44:21 | [diff] [blame] | 25 | #include "sql/test/database_test_peer.h" |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 26 | #include "sql/test/error_callback_support.h" |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 27 | #include "sql/test/scoped_error_expecter.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 | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 40 | int SqliteMasterCount(Database* db) { |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 41 | const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 42 | Statement s(db->GetUniqueStatement(kMasterCount)); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 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 | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 51 | explicit RefCounter(size_t* counter) : counter_(counter) { (*counter_)++; } |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 52 | RefCounter(const RefCounter& other) : counter_(other.counter_) { |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 53 | (*counter_)++; |
| 54 | } |
Victor Costan | 00c7643 | 2021-07-07 16:55:58 | [diff] [blame] | 55 | RefCounter& operator=(const RefCounter&) = delete; |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 56 | ~RefCounter() { (*counter_)--; } |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 57 | |
| 58 | private: |
| 59 | size_t* counter_; |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | // Empty callback for implementation of ErrorCallbackSetHelper(). |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 63 | void IgnoreErrorCallback(int error, Statement* stmt) {} |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 64 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 65 | void ErrorCallbackSetHelper(Database* db, |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 66 | size_t* counter, |
| 67 | const RefCounter& r, |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 68 | int error, |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 69 | Statement* stmt) { |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 70 | // The ref count should not go to zero when changing the callback. |
| 71 | EXPECT_GT(*counter, 0u); |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 72 | db->set_error_callback(base::BindRepeating(&IgnoreErrorCallback)); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 73 | EXPECT_GT(*counter, 0u); |
| 74 | } |
| 75 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 76 | void ErrorCallbackResetHelper(Database* db, |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 77 | size_t* counter, |
| 78 | const RefCounter& r, |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 79 | int error, |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 80 | Statement* stmt) { |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 81 | // The ref count should not go to zero when clearing the callback. |
| 82 | EXPECT_GT(*counter, 0u); |
| 83 | db->reset_error_callback(); |
| 84 | EXPECT_GT(*counter, 0u); |
| 85 | } |
| 86 | |
shess | 1cf87f2 | 2016-10-25 22:18:29 | [diff] [blame] | 87 | // Handle errors by blowing away the database. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 88 | void RazeErrorCallback(Database* db, |
shess | 1cf87f2 | 2016-10-25 22:18:29 | [diff] [blame] | 89 | int expected_error, |
| 90 | int error, |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 91 | Statement* stmt) { |
shess | 1cf87f2 | 2016-10-25 22:18:29 | [diff] [blame] | 92 | // Nothing here needs extended errors at this time. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 93 | EXPECT_EQ(expected_error, expected_error & 0xff); |
| 94 | EXPECT_EQ(expected_error, error & 0xff); |
shess | 1cf87f2 | 2016-10-25 22:18:29 | [diff] [blame] | 95 | db->RazeAndClose(); |
| 96 | } |
| 97 | |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 98 | #if defined(OS_POSIX) |
| 99 | // Set a umask and restore the old mask on destruction. Cribbed from |
| 100 | // shared_memory_unittest.cc. Used by POSIX-only UserPermission test. |
| 101 | class ScopedUmaskSetter { |
| 102 | public: |
| 103 | explicit ScopedUmaskSetter(mode_t target_mask) { |
| 104 | old_umask_ = umask(target_mask); |
| 105 | } |
| 106 | ~ScopedUmaskSetter() { umask(old_umask_); } |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 107 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 108 | ScopedUmaskSetter(const ScopedUmaskSetter&) = delete; |
| 109 | ScopedUmaskSetter& operator=(const ScopedUmaskSetter&) = delete; |
| 110 | |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 111 | private: |
| 112 | mode_t old_umask_; |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 113 | }; |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 114 | #endif // defined(OS_POSIX) |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 115 | |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 116 | } // namespace |
| 117 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 118 | // We use the parameter to run all tests with WAL mode on and off. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 119 | class SQLDatabaseTest : public testing::Test, |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 120 | public testing::WithParamInterface<bool> { |
| 121 | public: |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 122 | enum class OverwriteType { |
| 123 | kTruncate, |
| 124 | kOverwrite, |
| 125 | }; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 126 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 127 | ~SQLDatabaseTest() override = default; |
| 128 | |
| 129 | void SetUp() override { |
| 130 | db_ = std::make_unique<Database>(GetDBOptions()); |
| 131 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 132 | db_path_ = temp_dir_.GetPath().AppendASCII("database_test.sqlite"); |
| 133 | ASSERT_TRUE(db_->Open(db_path_)); |
| 134 | } |
| 135 | |
| 136 | DatabaseOptions GetDBOptions() { |
| 137 | DatabaseOptions options; |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 138 | options.wal_mode = IsWALEnabled(); |
| 139 | // TODO(crbug.com/1120969): Remove after switching to exclusive mode on by |
| 140 | // default. |
| 141 | options.exclusive_locking = false; |
| 142 | #if defined(OS_FUCHSIA) // Exclusive mode needs to be enabled to enter WAL mode |
| 143 | // on Fuchsia |
| 144 | if (IsWALEnabled()) { |
| 145 | options.exclusive_locking = true; |
| 146 | } |
| 147 | #endif // defined(OS_FUCHSIA) |
| 148 | return options; |
| 149 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 150 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 151 | bool IsWALEnabled() { return GetParam(); } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 152 | |
| 153 | bool TruncateDatabase() { |
| 154 | base::File file(db_path_, |
| 155 | base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 156 | return file.SetLength(0); |
| 157 | } |
| 158 | |
| 159 | bool OverwriteDatabaseHeader(OverwriteType type) { |
| 160 | base::File file(db_path_, |
| 161 | base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 162 | if (type == OverwriteType::kTruncate) { |
| 163 | if (!file.SetLength(0)) |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | static constexpr char kText[] = "Now is the winter of our discontent."; |
| 168 | constexpr int kTextBytes = sizeof(kText) - 1; |
| 169 | return file.Write(0, kText, kTextBytes) == kTextBytes; |
| 170 | } |
| 171 | |
| 172 | protected: |
| 173 | base::ScopedTempDir temp_dir_; |
| 174 | base::FilePath db_path_; |
| 175 | std::unique_ptr<Database> db_; |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 176 | }; |
| 177 | |
Victor Costan | 289f2c8b | 2021-07-22 06:33:47 | [diff] [blame] | 178 | TEST_P(SQLDatabaseTest, Execute_ValidStatement) { |
| 179 | ASSERT_TRUE(db_->Execute("CREATE TABLE data(contents TEXT)")); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 180 | EXPECT_EQ(SQLITE_OK, db_->GetErrorCode()); |
Victor Costan | 289f2c8b | 2021-07-22 06:33:47 | [diff] [blame] | 181 | } |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 182 | |
Victor Costan | 289f2c8b | 2021-07-22 06:33:47 | [diff] [blame] | 183 | TEST_P(SQLDatabaseTest, Execute_InvalidStatement) { |
Victor Costan | 205b96dc | 2021-07-21 20:27:46 | [diff] [blame] | 184 | { |
| 185 | sql::test::ScopedErrorExpecter error_expecter; |
| 186 | error_expecter.ExpectError(SQLITE_ERROR); |
Victor Costan | 289f2c8b | 2021-07-22 06:33:47 | [diff] [blame] | 187 | EXPECT_FALSE(db_->Execute("CREATE TABLE data(")); |
Victor Costan | 205b96dc | 2021-07-21 20:27:46 | [diff] [blame] | 188 | EXPECT_TRUE(error_expecter.SawExpectedErrors()); |
| 189 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 190 | EXPECT_EQ(SQLITE_ERROR, db_->GetErrorCode()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 191 | } |
| 192 | |
Victor Costan | 289f2c8b | 2021-07-22 06:33:47 | [diff] [blame] | 193 | TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_OneLineValid) { |
| 194 | ASSERT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data(contents TEXT)")); |
| 195 | EXPECT_EQ(SQLITE_OK, db_->GetErrorCode()); |
| 196 | } |
| 197 | |
| 198 | TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_OneLineInvalid) { |
| 199 | ASSERT_FALSE(db_->ExecuteScriptForTesting("CREATE TABLE data(")); |
| 200 | EXPECT_EQ(SQLITE_ERROR, db_->GetErrorCode()); |
| 201 | } |
| 202 | |
| 203 | TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_ExtraContents) { |
| 204 | EXPECT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data1(id)")) |
| 205 | << "Minimal statement"; |
| 206 | EXPECT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data2(id);")) |
| 207 | << "Extra semicolon"; |
| 208 | EXPECT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data3(id) -- Comment")) |
| 209 | << "Trailing comment"; |
| 210 | |
| 211 | EXPECT_TRUE(db_->ExecuteScriptForTesting( |
| 212 | "CREATE TABLE data4(id);CREATE TABLE data5(id)")) |
| 213 | << "Extra statement without whitespace"; |
| 214 | EXPECT_TRUE(db_->ExecuteScriptForTesting( |
| 215 | "CREATE TABLE data6(id); CREATE TABLE data7(id)")) |
| 216 | << "Extra statement separated by whitespace"; |
| 217 | |
| 218 | EXPECT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data8(id);-- Comment")) |
| 219 | << "Comment without whitespace"; |
| 220 | EXPECT_TRUE( |
| 221 | db_->ExecuteScriptForTesting("CREATE TABLE data9(id); -- Comment")) |
| 222 | << "Comment sepatated by whitespace"; |
| 223 | } |
| 224 | |
| 225 | TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_MultipleValidLines) { |
| 226 | EXPECT_TRUE(db_->ExecuteScriptForTesting(R"( |
| 227 | CREATE TABLE data1(contents TEXT); |
| 228 | CREATE TABLE data2(contents TEXT); |
| 229 | CREATE TABLE data3(contents TEXT); |
| 230 | )")); |
| 231 | EXPECT_EQ(SQLITE_OK, db_->GetErrorCode()); |
| 232 | |
| 233 | // DoesColumnExist() is implemented directly on top of a SQLite call. The |
| 234 | // other schema functions use sql::Statement infrastructure to query the |
| 235 | // schema table. |
| 236 | EXPECT_TRUE(db_->DoesColumnExist("data1", "contents")); |
| 237 | EXPECT_TRUE(db_->DoesColumnExist("data2", "contents")); |
| 238 | EXPECT_TRUE(db_->DoesColumnExist("data3", "contents")); |
| 239 | } |
| 240 | |
| 241 | TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_StopsOnCompileError) { |
| 242 | EXPECT_FALSE(db_->ExecuteScriptForTesting(R"( |
| 243 | CREATE TABLE data1(contents TEXT); |
| 244 | CREATE TABLE data1(); |
| 245 | CREATE TABLE data3(contents TEXT); |
| 246 | )")); |
| 247 | EXPECT_EQ(SQLITE_ERROR, db_->GetErrorCode()); |
| 248 | |
| 249 | EXPECT_TRUE(db_->DoesColumnExist("data1", "contents")); |
| 250 | EXPECT_FALSE(db_->DoesColumnExist("data3", "contents")); |
| 251 | } |
| 252 | |
| 253 | TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_StopsOnStepError) { |
| 254 | EXPECT_FALSE(db_->ExecuteScriptForTesting(R"( |
| 255 | CREATE TABLE data1(contents TEXT UNIQUE); |
| 256 | INSERT INTO data1(contents) VALUES('value1'); |
| 257 | INSERT INTO data1(contents) VALUES('value1'); |
| 258 | CREATE TABLE data3(contents TEXT); |
| 259 | )")); |
| 260 | EXPECT_EQ(SQLITE_CONSTRAINT_UNIQUE, db_->GetErrorCode()); |
| 261 | |
| 262 | EXPECT_TRUE(db_->DoesColumnExist("data1", "contents")); |
| 263 | EXPECT_FALSE(db_->DoesColumnExist("data3", "contents")); |
| 264 | } |
| 265 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 266 | TEST_P(SQLDatabaseTest, CachedStatement) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 267 | StatementID id1 = SQL_FROM_HERE; |
| 268 | StatementID id2 = SQL_FROM_HERE; |
Victor Costan | 613b430 | 2018-11-20 05:32:43 | [diff] [blame] | 269 | static const char kId1Sql[] = "SELECT a FROM foo"; |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 270 | static const char kId2Sql[] = "SELECT b FROM foo"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 271 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 272 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
| 273 | ASSERT_TRUE(db_->Execute("INSERT INTO foo(a, b) VALUES (12, 13)")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 274 | |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 275 | sqlite3_stmt* raw_id1_statement; |
| 276 | sqlite3_stmt* raw_id2_statement; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 277 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 278 | scoped_refptr<Database::StatementRef> ref_from_id1 = |
| 279 | db_->GetCachedStatement(id1, kId1Sql); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 280 | raw_id1_statement = ref_from_id1->stmt(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 281 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 282 | Statement from_id1(std::move(ref_from_id1)); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 283 | ASSERT_TRUE(from_id1.is_valid()); |
| 284 | ASSERT_TRUE(from_id1.Step()); |
| 285 | EXPECT_EQ(12, from_id1.ColumnInt(0)); |
| 286 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 287 | scoped_refptr<Database::StatementRef> ref_from_id2 = |
| 288 | db_->GetCachedStatement(id2, kId2Sql); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 289 | raw_id2_statement = ref_from_id2->stmt(); |
| 290 | EXPECT_NE(raw_id1_statement, raw_id2_statement); |
| 291 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 292 | Statement from_id2(std::move(ref_from_id2)); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 293 | ASSERT_TRUE(from_id2.is_valid()); |
| 294 | ASSERT_TRUE(from_id2.Step()); |
| 295 | EXPECT_EQ(13, from_id2.ColumnInt(0)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 296 | } |
| 297 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 298 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 299 | scoped_refptr<Database::StatementRef> ref_from_id1 = |
| 300 | db_->GetCachedStatement(id1, kId1Sql); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 301 | EXPECT_EQ(raw_id1_statement, ref_from_id1->stmt()) |
| 302 | << "statement was not cached"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 303 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 304 | Statement from_id1(std::move(ref_from_id1)); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 305 | ASSERT_TRUE(from_id1.is_valid()); |
| 306 | ASSERT_TRUE(from_id1.Step()) << "cached statement was not reset"; |
| 307 | EXPECT_EQ(12, from_id1.ColumnInt(0)); |
| 308 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 309 | scoped_refptr<Database::StatementRef> ref_from_id2 = |
| 310 | db_->GetCachedStatement(id2, kId2Sql); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 311 | EXPECT_EQ(raw_id2_statement, ref_from_id2->stmt()) |
| 312 | << "statement was not cached"; |
| 313 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 314 | Statement from_id2(std::move(ref_from_id2)); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 315 | ASSERT_TRUE(from_id2.is_valid()); |
| 316 | ASSERT_TRUE(from_id2.Step()) << "cached statement was not reset"; |
| 317 | EXPECT_EQ(13, from_id2.ColumnInt(0)); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 318 | } |
Victor Costan | 613b430 | 2018-11-20 05:32:43 | [diff] [blame] | 319 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 320 | EXPECT_DCHECK_DEATH(db_->GetCachedStatement(id1, kId2Sql)) |
Victor Costan | 613b430 | 2018-11-20 05:32:43 | [diff] [blame] | 321 | << "Using a different SQL with the same statement ID should DCHECK"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 322 | EXPECT_DCHECK_DEATH(db_->GetCachedStatement(id2, kId1Sql)) |
Victor Costan | 613b430 | 2018-11-20 05:32:43 | [diff] [blame] | 323 | << "Using a different SQL with the same statement ID should DCHECK"; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 324 | } |
| 325 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 326 | TEST_P(SQLDatabaseTest, IsSQLValidTest) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 327 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
| 328 | ASSERT_TRUE(db_->IsSQLValid("SELECT a FROM foo")); |
| 329 | ASSERT_FALSE(db_->IsSQLValid("SELECT no_exist FROM foo")); |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 330 | } |
| 331 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 332 | TEST_P(SQLDatabaseTest, DoesTableExist) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 333 | EXPECT_FALSE(db_->DoesTableExist("foo")); |
| 334 | EXPECT_FALSE(db_->DoesTableExist("foo_index")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 335 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 336 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
| 337 | ASSERT_TRUE(db_->Execute("CREATE INDEX foo_index ON foo (a)")); |
| 338 | EXPECT_TRUE(db_->DoesTableExist("foo")); |
| 339 | EXPECT_FALSE(db_->DoesTableExist("foo_index")); |
Victor Costan | f85512e5 | 2019-04-10 20:51:36 | [diff] [blame] | 340 | |
| 341 | // DoesTableExist() is case-sensitive. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 342 | EXPECT_FALSE(db_->DoesTableExist("Foo")); |
| 343 | EXPECT_FALSE(db_->DoesTableExist("FOO")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 344 | } |
| 345 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 346 | TEST_P(SQLDatabaseTest, DoesIndexExist) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 347 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
| 348 | EXPECT_FALSE(db_->DoesIndexExist("foo")); |
| 349 | EXPECT_FALSE(db_->DoesIndexExist("foo_ubdex")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 350 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 351 | ASSERT_TRUE(db_->Execute("CREATE INDEX foo_index ON foo (a)")); |
| 352 | EXPECT_TRUE(db_->DoesIndexExist("foo_index")); |
| 353 | EXPECT_FALSE(db_->DoesIndexExist("foo")); |
Victor Costan | f85512e5 | 2019-04-10 20:51:36 | [diff] [blame] | 354 | |
| 355 | // DoesIndexExist() is case-sensitive. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 356 | EXPECT_FALSE(db_->DoesIndexExist("Foo_index")); |
| 357 | EXPECT_FALSE(db_->DoesIndexExist("Foo_Index")); |
| 358 | EXPECT_FALSE(db_->DoesIndexExist("FOO_INDEX")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 359 | } |
| 360 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 361 | TEST_P(SQLDatabaseTest, DoesViewExist) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 362 | EXPECT_FALSE(db_->DoesViewExist("voo")); |
| 363 | ASSERT_TRUE(db_->Execute("CREATE VIEW voo (a) AS SELECT 1")); |
| 364 | EXPECT_FALSE(db_->DoesIndexExist("voo")); |
| 365 | EXPECT_FALSE(db_->DoesTableExist("voo")); |
| 366 | EXPECT_TRUE(db_->DoesViewExist("voo")); |
Victor Costan | f85512e5 | 2019-04-10 20:51:36 | [diff] [blame] | 367 | |
| 368 | // DoesTableExist() is case-sensitive. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 369 | EXPECT_FALSE(db_->DoesViewExist("Voo")); |
| 370 | EXPECT_FALSE(db_->DoesViewExist("VOO")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 371 | } |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 372 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 373 | TEST_P(SQLDatabaseTest, DoesColumnExist) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 374 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 375 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 376 | EXPECT_FALSE(db_->DoesColumnExist("foo", "bar")); |
| 377 | EXPECT_TRUE(db_->DoesColumnExist("foo", "a")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 378 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 379 | ASSERT_FALSE(db_->DoesTableExist("bar")); |
| 380 | EXPECT_FALSE(db_->DoesColumnExist("bar", "b")); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 381 | |
Victor Costan | f85512e5 | 2019-04-10 20:51:36 | [diff] [blame] | 382 | // SQLite resolves table/column names without case sensitivity. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 383 | EXPECT_TRUE(db_->DoesColumnExist("FOO", "A")); |
| 384 | EXPECT_TRUE(db_->DoesColumnExist("FOO", "a")); |
| 385 | EXPECT_TRUE(db_->DoesColumnExist("foo", "A")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 386 | } |
| 387 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 388 | TEST_P(SQLDatabaseTest, GetLastInsertRowId) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 389 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 390 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 391 | ASSERT_TRUE(db_->Execute("INSERT INTO foo (value) VALUES (12)")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 392 | |
| 393 | // Last insert row ID should be valid. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 394 | int64_t row = db_->GetLastInsertRowId(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 395 | EXPECT_LT(0, row); |
| 396 | |
| 397 | // It should be the primary key of the row we just inserted. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 398 | Statement s(db_->GetUniqueStatement("SELECT value FROM foo WHERE id=?")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 399 | s.BindInt64(0, row); |
| 400 | ASSERT_TRUE(s.Step()); |
| 401 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 402 | } |
[email protected] | 44ad7d90 | 2012-03-23 00:09:05 | [diff] [blame] | 403 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 404 | TEST_P(SQLDatabaseTest, Rollback) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 405 | ASSERT_TRUE(db_->BeginTransaction()); |
| 406 | ASSERT_TRUE(db_->BeginTransaction()); |
| 407 | EXPECT_EQ(2, db_->transaction_nesting()); |
| 408 | db_->RollbackTransaction(); |
| 409 | EXPECT_FALSE(db_->CommitTransaction()); |
| 410 | EXPECT_TRUE(db_->BeginTransaction()); |
[email protected] | 44ad7d90 | 2012-03-23 00:09:05 | [diff] [blame] | 411 | } |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 412 | |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 413 | // Test the scoped error expecter by attempting to insert a duplicate |
[email protected] | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 414 | // value into an index. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 415 | TEST_P(SQLDatabaseTest, ScopedErrorExpecter) { |
[email protected] | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 416 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 417 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 418 | ASSERT_TRUE(db_->Execute("INSERT INTO foo (id) VALUES (12)")); |
[email protected] | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 419 | |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 420 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 421 | sql::test::ScopedErrorExpecter expecter; |
| 422 | expecter.ExpectError(SQLITE_CONSTRAINT); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 423 | ASSERT_FALSE(db_->Execute("INSERT INTO foo (id) VALUES (12)")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 424 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Victor Costan | 106e5007 | 2021-07-17 00:04:49 | [diff] [blame] | 428 | TEST_P(SQLDatabaseTest, SchemaIntrospectionUsesErrorExpecter) { |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 429 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 430 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 431 | ASSERT_FALSE(db_->DoesTableExist("bar")); |
| 432 | ASSERT_TRUE(db_->DoesTableExist("foo")); |
| 433 | ASSERT_TRUE(db_->DoesColumnExist("foo", "id")); |
| 434 | db_->Close(); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 435 | |
| 436 | // Corrupt the database so that nothing works, including PRAGMAs. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 437 | ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_)); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 438 | |
| 439 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 440 | sql::test::ScopedErrorExpecter expecter; |
| 441 | expecter.ExpectError(SQLITE_CORRUPT); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 442 | ASSERT_TRUE(db_->Open(db_path_)); |
| 443 | ASSERT_FALSE(db_->DoesTableExist("bar")); |
| 444 | ASSERT_FALSE(db_->DoesTableExist("foo")); |
| 445 | ASSERT_FALSE(db_->DoesColumnExist("foo", "id")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 446 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 447 | } |
[email protected] | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 448 | } |
| 449 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 450 | TEST_P(SQLDatabaseTest, ErrorCallback) { |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 451 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 452 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 453 | ASSERT_TRUE(db_->Execute("INSERT INTO foo (id) VALUES (12)")); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 454 | |
| 455 | int error = SQLITE_OK; |
| 456 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 457 | ScopedErrorCallback sec(db_.get(), |
| 458 | base::BindRepeating(&CaptureErrorCallback, &error)); |
| 459 | EXPECT_FALSE(db_->Execute("INSERT INTO foo (id) VALUES (12)")); |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 460 | |
| 461 | // Later versions of SQLite throw SQLITE_CONSTRAINT_UNIQUE. The specific |
| 462 | // sub-error isn't really important. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 463 | EXPECT_EQ(SQLITE_CONSTRAINT, (error & 0xff)); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | // Callback is no longer in force due to reset. |
| 467 | { |
| 468 | error = SQLITE_OK; |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 469 | sql::test::ScopedErrorExpecter expecter; |
| 470 | expecter.ExpectError(SQLITE_CONSTRAINT); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 471 | ASSERT_FALSE(db_->Execute("INSERT INTO foo (id) VALUES (12)")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 472 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 473 | EXPECT_EQ(SQLITE_OK, error); |
| 474 | } |
| 475 | |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 476 | // base::BindRepeating() can curry arguments to be passed by const reference |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 477 | // to the callback function. If the callback function calls |
| 478 | // re/set_error_callback(), the storage for those arguments can be |
| 479 | // deleted while the callback function is still executing. |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 480 | // |
| 481 | // RefCounter() counts how many objects are live using an external |
| 482 | // count. The same counter is passed to the callback, so that it |
| 483 | // can check directly even if the RefCounter object is no longer |
| 484 | // live. |
| 485 | { |
| 486 | size_t count = 0; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 487 | ScopedErrorCallback sec( |
| 488 | db_.get(), base::BindRepeating(&ErrorCallbackSetHelper, db_.get(), |
| 489 | &count, RefCounter(&count))); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 490 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 491 | EXPECT_FALSE(db_->Execute("INSERT INTO foo (id) VALUES (12)")); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // Same test, but reset_error_callback() case. |
| 495 | { |
| 496 | size_t count = 0; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 497 | ScopedErrorCallback sec( |
| 498 | db_.get(), base::BindRepeating(&ErrorCallbackResetHelper, db_.get(), |
| 499 | &count, RefCounter(&count))); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 500 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 501 | EXPECT_FALSE(db_->Execute("INSERT INTO foo (id) VALUES (12)")); |
[email protected] | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Victor Costan | c02b7af | 2021-07-09 13:26:45 | [diff] [blame] | 505 | TEST_P(SQLDatabaseTest, Execute_CompilationError) { |
| 506 | bool error_callback_called = false; |
| 507 | db_->set_error_callback(base::BindLambdaForTesting([&](int error, |
| 508 | sql::Statement* |
| 509 | statement) { |
| 510 | EXPECT_EQ(SQLITE_ERROR, error); |
| 511 | EXPECT_EQ(nullptr, statement); |
| 512 | EXPECT_FALSE(error_callback_called) |
| 513 | << "SQL compilation errors should call the error callback exactly once"; |
| 514 | error_callback_called = true; |
| 515 | })); |
| 516 | |
| 517 | { |
| 518 | sql::test::ScopedErrorExpecter expecter; |
| 519 | expecter.ExpectError(SQLITE_ERROR); |
| 520 | EXPECT_FALSE(db_->Execute("SELECT missing_column FROM missing_table")); |
| 521 | EXPECT_TRUE(expecter.SawExpectedErrors()); |
| 522 | } |
| 523 | |
| 524 | EXPECT_TRUE(error_callback_called) |
| 525 | << "SQL compilation errors should call the error callback"; |
| 526 | } |
| 527 | |
| 528 | TEST_P(SQLDatabaseTest, GetUniqueStatement_CompilationError) { |
| 529 | bool error_callback_called = false; |
| 530 | db_->set_error_callback(base::BindLambdaForTesting([&](int error, |
| 531 | sql::Statement* |
| 532 | statement) { |
| 533 | EXPECT_EQ(SQLITE_ERROR, error); |
| 534 | EXPECT_EQ(nullptr, statement); |
| 535 | EXPECT_FALSE(error_callback_called) |
| 536 | << "SQL compilation errors should call the error callback exactly once"; |
| 537 | error_callback_called = true; |
| 538 | })); |
| 539 | |
| 540 | { |
| 541 | sql::test::ScopedErrorExpecter expecter; |
| 542 | expecter.ExpectError(SQLITE_ERROR); |
| 543 | sql::Statement statement( |
| 544 | db_->GetUniqueStatement("SELECT missing_column FROM missing_table")); |
| 545 | EXPECT_FALSE(statement.is_valid()); |
| 546 | EXPECT_TRUE(expecter.SawExpectedErrors()); |
| 547 | } |
| 548 | |
| 549 | EXPECT_TRUE(error_callback_called) |
| 550 | << "SQL compilation errors should call the error callback"; |
| 551 | } |
| 552 | |
| 553 | TEST_P(SQLDatabaseTest, GetCachedStatement_CompilationError) { |
| 554 | bool error_callback_called = false; |
| 555 | db_->set_error_callback(base::BindLambdaForTesting([&](int error, |
| 556 | sql::Statement* |
| 557 | statement) { |
| 558 | EXPECT_EQ(SQLITE_ERROR, error); |
| 559 | EXPECT_EQ(nullptr, statement); |
| 560 | EXPECT_FALSE(error_callback_called) |
| 561 | << "SQL compilation errors should call the error callback exactly once"; |
| 562 | error_callback_called = true; |
| 563 | })); |
| 564 | |
| 565 | { |
| 566 | sql::test::ScopedErrorExpecter expecter; |
| 567 | expecter.ExpectError(SQLITE_ERROR); |
| 568 | sql::Statement statement(db_->GetCachedStatement( |
| 569 | SQL_FROM_HERE, "SELECT missing_column FROM missing_table")); |
| 570 | EXPECT_FALSE(statement.is_valid()); |
| 571 | EXPECT_TRUE(expecter.SawExpectedErrors()); |
| 572 | } |
| 573 | |
| 574 | EXPECT_TRUE(error_callback_called) |
| 575 | << "SQL compilation errors should call the error callback"; |
| 576 | } |
| 577 | |
Victor Costan | 5530932 | 2021-07-19 17:58:42 | [diff] [blame] | 578 | TEST_P(SQLDatabaseTest, GetUniqueStatement_ExtraContents) { |
| 579 | sql::Statement minimal(db_->GetUniqueStatement("SELECT 1")); |
| 580 | sql::Statement extra_semicolon(db_->GetUniqueStatement("SELECT 1;")); |
| 581 | |
| 582 | // It would be nice to flag trailing comments too, as they cost binary size. |
| 583 | // However, there's no easy way of doing that. |
| 584 | sql::Statement trailing_comment( |
| 585 | db_->GetUniqueStatement("SELECT 1 -- Comment")); |
| 586 | |
| 587 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("SELECT 1;SELECT 2")) |
| 588 | << "Extra statement without whitespace"; |
| 589 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("SELECT 1; SELECT 2")) |
| 590 | << "Extra statement separated by whitespace"; |
| 591 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("SELECT 1;-- Comment")) |
| 592 | << "Comment without whitespace"; |
| 593 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("SELECT 1; -- Comment")) |
| 594 | << "Comment separated by whitespace"; |
| 595 | } |
| 596 | |
| 597 | TEST_P(SQLDatabaseTest, GetCachedStatement_ExtraContents) { |
| 598 | sql::Statement minimal(db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1")); |
| 599 | sql::Statement extra_semicolon( |
| 600 | db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1;")); |
| 601 | |
| 602 | // It would be nice to flag trailing comments too, as they cost binary size. |
| 603 | // However, there's no easy way of doing that. |
| 604 | sql::Statement trailing_comment( |
| 605 | db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1 -- Comment")); |
| 606 | |
| 607 | EXPECT_DCHECK_DEATH( |
| 608 | db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1;SELECT 2")) |
| 609 | << "Extra statement without whitespace"; |
| 610 | EXPECT_DCHECK_DEATH( |
| 611 | db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1; SELECT 2")) |
| 612 | << "Extra statement separated by whitespace"; |
| 613 | EXPECT_DCHECK_DEATH( |
| 614 | db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1;-- Comment")) |
| 615 | << "Comment without whitespace"; |
| 616 | EXPECT_DCHECK_DEATH( |
| 617 | db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1; -- Comment")) |
| 618 | << "Comment separated by whitespace"; |
| 619 | } |
| 620 | |
| 621 | TEST_P(SQLDatabaseTest, IsSQLValid_ExtraContents) { |
| 622 | EXPECT_TRUE(db_->IsSQLValid("SELECT 1")); |
| 623 | EXPECT_TRUE(db_->IsSQLValid("SELECT 1;")) |
| 624 | << "Trailing semicolons are currently tolerated"; |
| 625 | |
| 626 | // It would be nice to flag trailing comments too, as they cost binary size. |
| 627 | // However, there's no easy way of doing that. |
| 628 | EXPECT_TRUE(db_->IsSQLValid("SELECT 1 -- Comment")) |
| 629 | << "Trailing comments are currently tolerated"; |
| 630 | |
| 631 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("SELECT 1;SELECT 2")) |
| 632 | << "Extra statement without whitespace"; |
| 633 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("SELECT 1; SELECT 2")) |
| 634 | << "Extra statement separated by whitespace"; |
| 635 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("SELECT 1;-- Comment")) |
| 636 | << "Comment without whitespace"; |
| 637 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("SELECT 1; -- Comment")) |
| 638 | << "Comment separated by whitespace"; |
| 639 | } |
| 640 | |
Victor Costan | 18f9e7a | 2021-07-22 06:34:17 | [diff] [blame] | 641 | TEST_P(SQLDatabaseTest, GetUniqueStatement_NoContents) { |
| 642 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("")) << "Empty string"; |
| 643 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement(" ")) << "Space"; |
| 644 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("\n")) << "Newline"; |
| 645 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("-- Comment")) << "Comment"; |
| 646 | } |
| 647 | |
| 648 | TEST_P(SQLDatabaseTest, GetCachedStatement_NoContents) { |
| 649 | EXPECT_DCHECK_DEATH(db_->GetCachedStatement(SQL_FROM_HERE, "")) |
| 650 | << "Empty string"; |
| 651 | EXPECT_DCHECK_DEATH(db_->GetCachedStatement(SQL_FROM_HERE, " ")) << "Space"; |
| 652 | EXPECT_DCHECK_DEATH(db_->GetCachedStatement(SQL_FROM_HERE, "\n")) |
| 653 | << "Newline"; |
| 654 | EXPECT_DCHECK_DEATH(db_->GetCachedStatement(SQL_FROM_HERE, "-- Comment")) |
| 655 | << "Comment"; |
| 656 | } |
| 657 | |
| 658 | TEST_P(SQLDatabaseTest, IsSQLValid_NoContents) { |
| 659 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("")) << "Empty string"; |
| 660 | EXPECT_DCHECK_DEATH(db_->IsSQLValid(" ")) << "Space"; |
| 661 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("\n")) << "Newline"; |
| 662 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("-- Comment")) << "Comment"; |
| 663 | } |
| 664 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 665 | // Test that Database::Raze() results in a database without the |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 666 | // tables from the original database. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 667 | TEST_P(SQLDatabaseTest, Raze) { |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 668 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 669 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 670 | ASSERT_TRUE(db_->Execute("INSERT INTO foo (value) VALUES (12)")); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 671 | |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 672 | int pragma_auto_vacuum = 0; |
| 673 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 674 | Statement s(db_->GetUniqueStatement("PRAGMA auto_vacuum")); |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 675 | ASSERT_TRUE(s.Step()); |
| 676 | pragma_auto_vacuum = s.ColumnInt(0); |
| 677 | ASSERT_TRUE(pragma_auto_vacuum == 0 || pragma_auto_vacuum == 1); |
| 678 | } |
| 679 | |
| 680 | // If auto_vacuum is set, there's an extra page to maintain a freelist. |
| 681 | const int kExpectedPageCount = 2 + pragma_auto_vacuum; |
| 682 | |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 683 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 684 | Statement s(db_->GetUniqueStatement("PRAGMA page_count")); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 685 | ASSERT_TRUE(s.Step()); |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 686 | EXPECT_EQ(kExpectedPageCount, s.ColumnInt(0)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 690 | Statement s(db_->GetUniqueStatement("SELECT * FROM sqlite_master")); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 691 | ASSERT_TRUE(s.Step()); |
| 692 | EXPECT_EQ("table", s.ColumnString(0)); |
| 693 | EXPECT_EQ("foo", s.ColumnString(1)); |
| 694 | EXPECT_EQ("foo", s.ColumnString(2)); |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 695 | // Table "foo" is stored in the last page of the file. |
| 696 | EXPECT_EQ(kExpectedPageCount, s.ColumnInt(3)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 697 | EXPECT_EQ(kCreateSql, s.ColumnString(4)); |
| 698 | } |
| 699 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 700 | ASSERT_TRUE(db_->Raze()); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 701 | |
| 702 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 703 | Statement s(db_->GetUniqueStatement("PRAGMA page_count")); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 704 | ASSERT_TRUE(s.Step()); |
| 705 | EXPECT_EQ(1, s.ColumnInt(0)); |
| 706 | } |
| 707 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 708 | ASSERT_EQ(0, SqliteMasterCount(db_.get())); |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 709 | |
| 710 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 711 | Statement s(db_->GetUniqueStatement("PRAGMA auto_vacuum")); |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 712 | ASSERT_TRUE(s.Step()); |
[email protected] | 6d42f15 | 2012-11-10 00:38:24 | [diff] [blame] | 713 | // The new database has the same auto_vacuum as a fresh database. |
[email protected] | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 714 | EXPECT_EQ(pragma_auto_vacuum, s.ColumnInt(0)); |
| 715 | } |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 716 | } |
| 717 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 718 | // Helper for SQLDatabaseTest.RazePageSize. Creates a fresh db based on |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 719 | // db_prefix, with the given initial page size, and verifies it against the |
| 720 | // expected size. Then changes to the final page size and razes, verifying that |
| 721 | // the fresh database ends up with the expected final page size. |
| 722 | void TestPageSize(const base::FilePath& db_prefix, |
| 723 | int initial_page_size, |
| 724 | const std::string& expected_initial_page_size, |
| 725 | int final_page_size, |
| 726 | const std::string& expected_final_page_size) { |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 727 | static const char kCreateSql[] = "CREATE TABLE x (t TEXT)"; |
| 728 | static const char kInsertSql1[] = "INSERT INTO x VALUES ('This is a test')"; |
| 729 | static const char kInsertSql2[] = "INSERT INTO x VALUES ('That was a test')"; |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 730 | |
| 731 | const base::FilePath db_path = db_prefix.InsertBeforeExtensionASCII( |
Raul Tambre | 6c708e3 | 2019-02-08 22:35:14 | [diff] [blame] | 732 | base::NumberToString(initial_page_size)); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 733 | Database::Delete(db_path); |
| 734 | Database db({.page_size = initial_page_size}); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 735 | ASSERT_TRUE(db.Open(db_path)); |
| 736 | ASSERT_TRUE(db.Execute(kCreateSql)); |
| 737 | ASSERT_TRUE(db.Execute(kInsertSql1)); |
| 738 | ASSERT_TRUE(db.Execute(kInsertSql2)); |
| 739 | ASSERT_EQ(expected_initial_page_size, |
| 740 | ExecuteWithResult(&db, "PRAGMA page_size")); |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 741 | db.Close(); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 742 | |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 743 | // Re-open the database while setting a new |options.page_size| in the object. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 744 | Database razed_db({.page_size = final_page_size}); |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 745 | ASSERT_TRUE(razed_db.Open(db_path)); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 746 | // Raze will use the page size set in the connection object, which may not |
| 747 | // match the file's page size. |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 748 | ASSERT_TRUE(razed_db.Raze()); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 749 | |
| 750 | // SQLite 3.10.2 (at least) has a quirk with the sqlite3_backup() API (used by |
| 751 | // Raze()) which causes the destination database to remember the previous |
| 752 | // page_size, even if the overwriting database changed the page_size. Access |
| 753 | // the actual database to cause the cached value to be updated. |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 754 | EXPECT_EQ("0", |
| 755 | ExecuteWithResult(&razed_db, "SELECT COUNT(*) FROM sqlite_master")); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 756 | |
| 757 | EXPECT_EQ(expected_final_page_size, |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 758 | ExecuteWithResult(&razed_db, "PRAGMA page_size")); |
| 759 | EXPECT_EQ("1", ExecuteWithResult(&razed_db, "PRAGMA page_count")); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 760 | } |
| 761 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 762 | // Verify that Recovery maintains the page size, and the virtual table |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 763 | // works with page sizes other than SQLite's default. Also verify the case |
| 764 | // where the default page size has changed. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 765 | TEST_P(SQLDatabaseTest, RazePageSize) { |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 766 | const std::string default_page_size = |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 767 | ExecuteWithResult(db_.get(), "PRAGMA page_size"); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 768 | |
Victor Costan | 7f6abbbe | 2018-07-29 02:57:27 | [diff] [blame] | 769 | // Sync uses 32k pages. |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 770 | EXPECT_NO_FATAL_FAILURE( |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 771 | TestPageSize(db_path_, 32768, "32768", 32768, "32768")); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 772 | |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 773 | // Many clients use 4k pages. This is the SQLite default after 3.12.0. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 774 | EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path_, 4096, "4096", 4096, "4096")); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 775 | |
| 776 | // 1k is the default page size before 3.12.0. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 777 | EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path_, 1024, "1024", 1024, "1024")); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 778 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 779 | EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path_, 2048, "2048", 4096, "4096")); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 780 | |
Victor Costan | 7f6abbbe | 2018-07-29 02:57:27 | [diff] [blame] | 781 | // Databases with no page size specified should result in the default |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 782 | // page size. 2k has never been the default page size. |
| 783 | ASSERT_NE("2048", default_page_size); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 784 | EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path_, 2048, "2048", |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 785 | DatabaseOptions::kDefaultPageSize, |
| 786 | default_page_size)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | // Test that Raze() results are seen in other connections. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 790 | TEST_P(SQLDatabaseTest, RazeMultiple) { |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 791 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 792 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 793 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 794 | Database other_db(GetDBOptions()); |
| 795 | ASSERT_TRUE(other_db.Open(db_path_)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 796 | |
| 797 | // Check that the second connection sees the table. |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 798 | ASSERT_EQ(1, SqliteMasterCount(&other_db)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 799 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 800 | ASSERT_TRUE(db_->Raze()); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 801 | |
| 802 | // The second connection sees the updated database. |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 803 | ASSERT_EQ(0, SqliteMasterCount(&other_db)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 804 | } |
| 805 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 806 | TEST_P(SQLDatabaseTest, RazeLocked) { |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 807 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 808 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 809 | |
| 810 | // Open a transaction and write some data in a second connection. |
| 811 | // This will acquire a PENDING or EXCLUSIVE transaction, which will |
| 812 | // cause the raze to fail. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 813 | Database other_db(GetDBOptions()); |
| 814 | ASSERT_TRUE(other_db.Open(db_path_)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 815 | ASSERT_TRUE(other_db.BeginTransaction()); |
| 816 | const char* kInsertSql = "INSERT INTO foo VALUES (1, 'data')"; |
| 817 | ASSERT_TRUE(other_db.Execute(kInsertSql)); |
| 818 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 819 | ASSERT_FALSE(db_->Raze()); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 820 | |
| 821 | // Works after COMMIT. |
| 822 | ASSERT_TRUE(other_db.CommitTransaction()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 823 | ASSERT_TRUE(db_->Raze()); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 824 | |
| 825 | // Re-create the database. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 826 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 827 | ASSERT_TRUE(db_->Execute(kInsertSql)); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 828 | |
| 829 | // An unfinished read transaction in the other connection also |
| 830 | // blocks raze. |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 831 | // This doesn't happen in WAL mode because reads are no longer blocked by |
| 832 | // write operations when using a WAL. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 833 | if (!IsWALEnabled()) { |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 834 | const char* kQuery = "SELECT COUNT(*) FROM foo"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 835 | Statement s(other_db.GetUniqueStatement(kQuery)); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 836 | ASSERT_TRUE(s.Step()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 837 | ASSERT_FALSE(db_->Raze()); |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 838 | |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 839 | // Completing the statement unlocks the database. |
| 840 | ASSERT_FALSE(s.Step()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 841 | ASSERT_TRUE(db_->Raze()); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 842 | } |
[email protected] | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 843 | } |
| 844 | |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 845 | // Verify that Raze() can handle an empty file. SQLite should treat |
| 846 | // this as an empty database. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 847 | TEST_P(SQLDatabaseTest, RazeEmptyDB) { |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 848 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 849 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 850 | db_->Close(); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 851 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 852 | ASSERT_TRUE(TruncateDatabase()); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 853 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 854 | ASSERT_TRUE(db_->Open(db_path_)); |
| 855 | ASSERT_TRUE(db_->Raze()); |
| 856 | EXPECT_EQ(0, SqliteMasterCount(db_.get())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | // Verify that Raze() can handle a file of junk. |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 860 | // Need exclusive mode off here as there are some subtleties (by design) around |
| 861 | // how the cache is used with it on which causes the test to fail. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 862 | TEST_P(SQLDatabaseTest, RazeNOTADB) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 863 | db_->Close(); |
| 864 | Database::Delete(db_path_); |
| 865 | ASSERT_FALSE(base::PathExists(db_path_)); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 866 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 867 | ASSERT_TRUE(OverwriteDatabaseHeader(OverwriteType::kTruncate)); |
| 868 | ASSERT_TRUE(base::PathExists(db_path_)); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 869 | |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 870 | // SQLite will successfully open the handle, but fail when running PRAGMA |
| 871 | // statements that access the database. |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 872 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 873 | sql::test::ScopedErrorExpecter expecter; |
Victor Costan | 42988a9 | 2018-02-06 02:22:14 | [diff] [blame] | 874 | expecter.ExpectError(SQLITE_NOTADB); |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 875 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 876 | EXPECT_TRUE(db_->Open(db_path_)); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 877 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 878 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 879 | EXPECT_TRUE(db_->Raze()); |
| 880 | db_->Close(); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 881 | |
| 882 | // Now empty, the open should open an empty database. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 883 | EXPECT_TRUE(db_->Open(db_path_)); |
| 884 | EXPECT_EQ(0, SqliteMasterCount(db_.get())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | // Verify that Raze() can handle a database overwritten with garbage. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 888 | TEST_P(SQLDatabaseTest, RazeNOTADB2) { |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 889 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 890 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 891 | ASSERT_EQ(1, SqliteMasterCount(db_.get())); |
| 892 | db_->Close(); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 893 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 894 | ASSERT_TRUE(OverwriteDatabaseHeader(OverwriteType::kOverwrite)); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 895 | |
| 896 | // SQLite will successfully open the handle, but will fail with |
| 897 | // SQLITE_NOTADB on pragma statemenets which attempt to read the |
| 898 | // corrupted header. |
| 899 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 900 | sql::test::ScopedErrorExpecter expecter; |
| 901 | expecter.ExpectError(SQLITE_NOTADB); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 902 | EXPECT_TRUE(db_->Open(db_path_)); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 903 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 904 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 905 | EXPECT_TRUE(db_->Raze()); |
| 906 | db_->Close(); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 907 | |
| 908 | // Now empty, the open should succeed with an empty database. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 909 | EXPECT_TRUE(db_->Open(db_path_)); |
| 910 | EXPECT_EQ(0, SqliteMasterCount(db_.get())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | // Test that a callback from Open() can raze the database. This is |
| 914 | // essential for cases where the Open() can fail entirely, so the |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 915 | // Raze() cannot happen later. Additionally test that when the |
| 916 | // callback does this during Open(), the open is retried and succeeds. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 917 | TEST_P(SQLDatabaseTest, RazeCallbackReopen) { |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 918 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 919 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 920 | ASSERT_EQ(1, SqliteMasterCount(db_.get())); |
| 921 | db_->Close(); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 922 | |
[email protected] | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 923 | // Corrupt the database so that nothing works, including PRAGMAs. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 924 | ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_)); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 925 | |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 926 | // Open() will succeed, even though the PRAGMA calls within will |
| 927 | // fail with SQLITE_CORRUPT, as will this PRAGMA. |
| 928 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 929 | sql::test::ScopedErrorExpecter expecter; |
| 930 | expecter.ExpectError(SQLITE_CORRUPT); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 931 | ASSERT_TRUE(db_->Open(db_path_)); |
| 932 | ASSERT_FALSE(db_->Execute("PRAGMA auto_vacuum")); |
| 933 | db_->Close(); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 934 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 935 | } |
| 936 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 937 | db_->set_error_callback( |
| 938 | base::BindRepeating(&RazeErrorCallback, db_.get(), SQLITE_CORRUPT)); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 939 | |
[email protected] | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 940 | // When the PRAGMA calls in Open() raise SQLITE_CORRUPT, the error |
| 941 | // callback will call RazeAndClose(). Open() will then fail and be |
| 942 | // retried. The second Open() on the empty database will succeed |
| 943 | // cleanly. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 944 | ASSERT_TRUE(db_->Open(db_path_)); |
| 945 | ASSERT_TRUE(db_->Execute("PRAGMA auto_vacuum")); |
| 946 | EXPECT_EQ(0, SqliteMasterCount(db_.get())); |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 947 | } |
| 948 | |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 949 | // Basic test of RazeAndClose() operation. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 950 | TEST_P(SQLDatabaseTest, RazeAndClose) { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 951 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 952 | const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)"; |
| 953 | |
| 954 | // Test that RazeAndClose() closes the database, and that the |
| 955 | // database is empty when re-opened. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 956 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 957 | ASSERT_TRUE(db_->Execute(kPopulateSql)); |
| 958 | ASSERT_TRUE(db_->RazeAndClose()); |
| 959 | ASSERT_FALSE(db_->is_open()); |
| 960 | db_->Close(); |
| 961 | ASSERT_TRUE(db_->Open(db_path_)); |
| 962 | ASSERT_EQ(0, SqliteMasterCount(db_.get())); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 963 | |
| 964 | // Test that RazeAndClose() can break transactions. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 965 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 966 | ASSERT_TRUE(db_->Execute(kPopulateSql)); |
| 967 | ASSERT_TRUE(db_->BeginTransaction()); |
| 968 | ASSERT_TRUE(db_->RazeAndClose()); |
| 969 | ASSERT_FALSE(db_->is_open()); |
| 970 | ASSERT_FALSE(db_->CommitTransaction()); |
| 971 | db_->Close(); |
| 972 | ASSERT_TRUE(db_->Open(db_path_)); |
| 973 | ASSERT_EQ(0, SqliteMasterCount(db_.get())); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | // Test that various operations fail without crashing after |
| 977 | // RazeAndClose(). |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 978 | TEST_P(SQLDatabaseTest, RazeAndCloseDiagnostics) { |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 979 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 980 | const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)"; |
| 981 | const char* kSimpleSql = "SELECT 1"; |
| 982 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 983 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 984 | ASSERT_TRUE(db_->Execute(kPopulateSql)); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 985 | |
| 986 | // Test baseline expectations. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 987 | db_->Preload(); |
| 988 | ASSERT_TRUE(db_->DoesTableExist("foo")); |
| 989 | ASSERT_TRUE(db_->IsSQLValid(kSimpleSql)); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 990 | ASSERT_TRUE(db_->Execute(kSimpleSql)); |
| 991 | ASSERT_TRUE(db_->is_open()); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 992 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 993 | Statement s(db_->GetUniqueStatement(kSimpleSql)); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 994 | ASSERT_TRUE(s.Step()); |
| 995 | } |
| 996 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 997 | Statement s(db_->GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 998 | ASSERT_TRUE(s.Step()); |
| 999 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1000 | ASSERT_TRUE(db_->BeginTransaction()); |
| 1001 | ASSERT_TRUE(db_->CommitTransaction()); |
| 1002 | ASSERT_TRUE(db_->BeginTransaction()); |
| 1003 | db_->RollbackTransaction(); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1004 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1005 | ASSERT_TRUE(db_->RazeAndClose()); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1006 | |
| 1007 | // At this point, they should all fail, but not crash. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1008 | db_->Preload(); |
| 1009 | ASSERT_FALSE(db_->DoesTableExist("foo")); |
| 1010 | ASSERT_FALSE(db_->IsSQLValid(kSimpleSql)); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1011 | ASSERT_FALSE(db_->Execute(kSimpleSql)); |
| 1012 | ASSERT_FALSE(db_->is_open()); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1013 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1014 | Statement s(db_->GetUniqueStatement(kSimpleSql)); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1015 | ASSERT_FALSE(s.Step()); |
| 1016 | } |
| 1017 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1018 | Statement s(db_->GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1019 | ASSERT_FALSE(s.Step()); |
| 1020 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1021 | ASSERT_FALSE(db_->BeginTransaction()); |
| 1022 | ASSERT_FALSE(db_->CommitTransaction()); |
| 1023 | ASSERT_FALSE(db_->BeginTransaction()); |
| 1024 | db_->RollbackTransaction(); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1025 | |
| 1026 | // Close normally to reset the poisoned flag. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1027 | db_->Close(); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1028 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1029 | // DEATH tests not supported on Android, iOS, or Fuchsia. |
Scott Graham | 57ee5482 | 2017-09-13 06:37:56 | [diff] [blame] | 1030 | #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_FUCHSIA) |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1031 | // Once the real Close() has been called, various calls enforce API |
| 1032 | // usage by becoming fatal in debug mode. Since DEATH tests are |
| 1033 | // expensive, just test one of them. |
| 1034 | if (DLOG_IS_ON(FATAL)) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1035 | ASSERT_DEATH({ db_->IsSQLValid(kSimpleSql); }, |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1036 | "Illegal use of Database without a db"); |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1037 | } |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1038 | #endif // !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_FUCHSIA) |
[email protected] | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | // TODO(shess): Spin up a background thread to hold other_db, to more |
| 1042 | // closely match real life. That would also allow testing |
| 1043 | // RazeWithTimeout(). |
| 1044 | |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1045 | // On Windows, truncate silently fails against a memory-mapped file. One goal |
| 1046 | // of Raze() is to truncate the file to remove blocks which generate I/O errors. |
| 1047 | // Test that Raze() turns off memory mapping so that the file is truncated. |
| 1048 | // [This would not cover the case of multiple connections where one of the other |
| 1049 | // connections is memory-mapped. That is infrequent in Chromium.] |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1050 | TEST_P(SQLDatabaseTest, RazeTruncate) { |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1051 | // The empty database has 0 or 1 pages. Raze() should leave it with exactly 1 |
| 1052 | // page. Not checking directly because auto_vacuum on Android adds a freelist |
| 1053 | // page. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1054 | ASSERT_TRUE(db_->Raze()); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1055 | int64_t expected_size; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1056 | ASSERT_TRUE(base::GetFileSize(db_path_, &expected_size)); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1057 | ASSERT_GT(expected_size, 0); |
| 1058 | |
| 1059 | // Cause the database to take a few pages. |
| 1060 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1061 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1062 | for (size_t i = 0; i < 24; ++i) { |
| 1063 | ASSERT_TRUE( |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1064 | db_->Execute("INSERT INTO foo (value) VALUES (randomblob(1024))")); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1065 | } |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1066 | |
| 1067 | // In WAL mode, writes don't reach the database file until a checkpoint |
| 1068 | // happens. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1069 | ASSERT_TRUE(db_->CheckpointDatabase()); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1070 | |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1071 | int64_t db_size; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1072 | ASSERT_TRUE(base::GetFileSize(db_path_, &db_size)); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1073 | ASSERT_GT(db_size, expected_size); |
| 1074 | |
| 1075 | // Make a query covering most of the database file to make sure that the |
| 1076 | // blocks are actually mapped into memory. Empirically, the truncate problem |
| 1077 | // doesn't seem to happen if no blocks are mapped. |
| 1078 | EXPECT_EQ("24576", |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1079 | ExecuteWithResult(db_.get(), "SELECT SUM(LENGTH(value)) FROM foo")); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1080 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1081 | ASSERT_TRUE(db_->Raze()); |
| 1082 | ASSERT_TRUE(base::GetFileSize(db_path_, &db_size)); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1083 | ASSERT_EQ(expected_size, db_size); |
| 1084 | } |
| 1085 | |
[email protected] | 1348765a | 2012-07-24 08:25:53 | [diff] [blame] | 1086 | #if defined(OS_ANDROID) |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1087 | TEST_P(SQLDatabaseTest, SetTempDirForSQL) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1088 | MetaTable meta_table; |
[email protected] | 1348765a | 2012-07-24 08:25:53 | [diff] [blame] | 1089 | // Below call needs a temporary directory in sqlite3 |
| 1090 | // On Android, it can pass only when the temporary directory is set. |
| 1091 | // Otherwise, sqlite3 doesn't find the correct directory to store |
| 1092 | // temporary files and will report the error 'unable to open |
| 1093 | // database file'. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1094 | ASSERT_TRUE(meta_table.Init(db_.get(), 4, 4)); |
[email protected] | 1348765a | 2012-07-24 08:25:53 | [diff] [blame] | 1095 | } |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1096 | #endif // defined(OS_ANDROID) |
[email protected] | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 1097 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1098 | TEST_P(SQLDatabaseTest, Delete) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1099 | EXPECT_TRUE(db_->Execute("CREATE TABLE x (x)")); |
| 1100 | db_->Close(); |
[email protected] | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 1101 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1102 | base::FilePath journal_path = Database::JournalPath(db_path_); |
| 1103 | base::FilePath wal_path = Database::WriteAheadLogPath(db_path_); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1104 | |
| 1105 | // Should have both a main database file and a journal file if |
| 1106 | // journal_mode is TRUNCATE. There is no WAL file as it is deleted on Close. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1107 | ASSERT_TRUE(base::PathExists(db_path_)); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1108 | if (!IsWALEnabled()) { // TRUNCATE mode |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1109 | ASSERT_TRUE(base::PathExists(journal_path)); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1110 | } |
[email protected] | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 1111 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1112 | Database::Delete(db_path_); |
| 1113 | EXPECT_FALSE(base::PathExists(db_path_)); |
| 1114 | EXPECT_FALSE(base::PathExists(journal_path)); |
| 1115 | EXPECT_FALSE(base::PathExists(wal_path)); |
[email protected] | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 1116 | } |
[email protected] | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1117 | |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 1118 | #if defined(OS_POSIX) // This test operates on POSIX file permissions. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1119 | TEST_P(SQLDatabaseTest, PosixFilePermissions) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1120 | db_->Close(); |
| 1121 | Database::Delete(db_path_); |
| 1122 | ASSERT_FALSE(base::PathExists(db_path_)); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 1123 | |
| 1124 | // If the bots all had a restrictive umask setting such that databases are |
| 1125 | // always created with only the owner able to read them, then the code could |
| 1126 | // break without breaking the tests. Temporarily provide a more permissive |
| 1127 | // umask. |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1128 | ScopedUmaskSetter permissive_umask(S_IWGRP | S_IWOTH); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 1129 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1130 | ASSERT_TRUE(db_->Open(db_path_)); |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1131 | |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 1132 | // Cause the journal file to be created. If the default journal_mode is |
| 1133 | // changed back to DELETE, this test will need to be updated. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1134 | EXPECT_TRUE(db_->Execute("CREATE TABLE x (x)")); |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1135 | |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1136 | int mode; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1137 | ASSERT_TRUE(base::PathExists(db_path_)); |
| 1138 | EXPECT_TRUE(base::GetPosixFilePermissions(db_path_, &mode)); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 1139 | ASSERT_EQ(mode, 0600); |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1140 | |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1141 | if (IsWALEnabled()) { // WAL mode |
| 1142 | // The WAL file is created lazily on first change. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1143 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1144 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1145 | base::FilePath wal_path = Database::WriteAheadLogPath(db_path_); |
| 1146 | ASSERT_TRUE(base::PathExists(wal_path)); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 1147 | EXPECT_TRUE(base::GetPosixFilePermissions(wal_path, &mode)); |
| 1148 | ASSERT_EQ(mode, 0600); |
| 1149 | |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 1150 | // The shm file doesn't exist in exclusive locking mode. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1151 | if (ExecuteWithResult(db_.get(), "PRAGMA locking_mode") == "normal") { |
| 1152 | base::FilePath shm_path = Database::SharedMemoryFilePath(db_path_); |
| 1153 | ASSERT_TRUE(base::PathExists(shm_path)); |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 1154 | EXPECT_TRUE(base::GetPosixFilePermissions(shm_path, &mode)); |
| 1155 | ASSERT_EQ(mode, 0600); |
| 1156 | } |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1157 | } else { // Truncate mode |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1158 | base::FilePath journal_path = Database::JournalPath(db_path_); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1159 | DLOG(ERROR) << "journal_path: " << journal_path; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1160 | ASSERT_TRUE(base::PathExists(journal_path)); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1161 | EXPECT_TRUE(base::GetPosixFilePermissions(journal_path, &mode)); |
| 1162 | ASSERT_EQ(mode, 0600); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 1163 | } |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1164 | } |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 1165 | #endif // defined(OS_POSIX) |
[email protected] | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1166 | |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1167 | // Test that errors start happening once Poison() is called. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1168 | TEST_P(SQLDatabaseTest, Poison) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1169 | EXPECT_TRUE(db_->Execute("CREATE TABLE x (x)")); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1170 | |
| 1171 | // Before the Poison() call, things generally work. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1172 | EXPECT_TRUE(db_->IsSQLValid("INSERT INTO x VALUES ('x')")); |
| 1173 | EXPECT_TRUE(db_->Execute("INSERT INTO x VALUES ('x')")); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1174 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1175 | Statement s(db_->GetUniqueStatement("SELECT COUNT(*) FROM x")); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1176 | ASSERT_TRUE(s.is_valid()); |
| 1177 | ASSERT_TRUE(s.Step()); |
| 1178 | } |
| 1179 | |
| 1180 | // Get a statement which is valid before and will exist across Poison(). |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1181 | Statement valid_statement( |
| 1182 | db_->GetUniqueStatement("SELECT COUNT(*) FROM sqlite_master")); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1183 | ASSERT_TRUE(valid_statement.is_valid()); |
| 1184 | ASSERT_TRUE(valid_statement.Step()); |
| 1185 | valid_statement.Reset(true); |
| 1186 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1187 | db_->Poison(); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1188 | |
| 1189 | // After the Poison() call, things fail. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1190 | EXPECT_FALSE(db_->IsSQLValid("INSERT INTO x VALUES ('x')")); |
| 1191 | EXPECT_FALSE(db_->Execute("INSERT INTO x VALUES ('x')")); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1192 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1193 | Statement s(db_->GetUniqueStatement("SELECT COUNT(*) FROM x")); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1194 | ASSERT_FALSE(s.is_valid()); |
| 1195 | ASSERT_FALSE(s.Step()); |
| 1196 | } |
| 1197 | |
| 1198 | // The existing statement has become invalid. |
| 1199 | ASSERT_FALSE(valid_statement.is_valid()); |
| 1200 | ASSERT_FALSE(valid_statement.Step()); |
shess | 644fc8a | 2016-02-26 18:15:58 | [diff] [blame] | 1201 | |
| 1202 | // Test that poisoning the database during a transaction works (with errors). |
| 1203 | // RazeErrorCallback() poisons the database, the extra COMMIT causes |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1204 | // CommitTransaction() to throw an error while committing. |
| 1205 | db_->set_error_callback( |
| 1206 | base::BindRepeating(&RazeErrorCallback, db_.get(), SQLITE_ERROR)); |
| 1207 | db_->Close(); |
| 1208 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1209 | EXPECT_TRUE(db_->BeginTransaction()); |
| 1210 | EXPECT_TRUE(db_->Execute("INSERT INTO x VALUES ('x')")); |
| 1211 | EXPECT_TRUE(db_->Execute("COMMIT")); |
| 1212 | EXPECT_FALSE(db_->CommitTransaction()); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1213 | } |
| 1214 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1215 | TEST_P(SQLDatabaseTest, AttachDatabase) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1216 | EXPECT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1217 | |
| 1218 | // Create a database to attach to. |
| 1219 | base::FilePath attach_path = |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1220 | db_path_.DirName().AppendASCII("SQLDatabaseAttach.db"); |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 1221 | static const char kAttachmentPoint[] = "other"; |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1222 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1223 | Database other_db; |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1224 | ASSERT_TRUE(other_db.Open(attach_path)); |
| 1225 | EXPECT_TRUE(other_db.Execute("CREATE TABLE bar (a, b)")); |
| 1226 | EXPECT_TRUE(other_db.Execute("INSERT INTO bar VALUES ('hello', 'world')")); |
| 1227 | } |
| 1228 | |
| 1229 | // Cannot see the attached database, yet. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1230 | EXPECT_FALSE(db_->IsSQLValid("SELECT count(*) from other.bar")); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1231 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1232 | EXPECT_TRUE(DatabaseTestPeer::AttachDatabase(db_.get(), attach_path, |
| 1233 | kAttachmentPoint)); |
| 1234 | EXPECT_TRUE(db_->IsSQLValid("SELECT count(*) from other.bar")); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1235 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1236 | // Queries can touch both databases after the ATTACH. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1237 | EXPECT_TRUE(db_->Execute("INSERT INTO foo SELECT a, b FROM other.bar")); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1238 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1239 | Statement s(db_->GetUniqueStatement("SELECT COUNT(*) FROM foo")); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1240 | ASSERT_TRUE(s.Step()); |
| 1241 | EXPECT_EQ(1, s.ColumnInt(0)); |
| 1242 | } |
| 1243 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1244 | EXPECT_TRUE(DatabaseTestPeer::DetachDatabase(db_.get(), kAttachmentPoint)); |
| 1245 | EXPECT_FALSE(db_->IsSQLValid("SELECT count(*) from other.bar")); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1246 | } |
| 1247 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1248 | TEST_P(SQLDatabaseTest, AttachDatabaseWithOpenTransaction) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1249 | EXPECT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1250 | |
| 1251 | // Create a database to attach to. |
| 1252 | base::FilePath attach_path = |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1253 | db_path_.DirName().AppendASCII("SQLDatabaseAttach.db"); |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 1254 | static const char kAttachmentPoint[] = "other"; |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1255 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1256 | Database other_db; |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1257 | ASSERT_TRUE(other_db.Open(attach_path)); |
| 1258 | EXPECT_TRUE(other_db.Execute("CREATE TABLE bar (a, b)")); |
| 1259 | EXPECT_TRUE(other_db.Execute("INSERT INTO bar VALUES ('hello', 'world')")); |
| 1260 | } |
| 1261 | |
| 1262 | // Cannot see the attached database, yet. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1263 | EXPECT_FALSE(db_->IsSQLValid("SELECT count(*) from other.bar")); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1264 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1265 | // Attach succeeds in a transaction. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1266 | EXPECT_TRUE(db_->BeginTransaction()); |
| 1267 | EXPECT_TRUE(DatabaseTestPeer::AttachDatabase(db_.get(), attach_path, |
| 1268 | kAttachmentPoint)); |
| 1269 | EXPECT_TRUE(db_->IsSQLValid("SELECT count(*) from other.bar")); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1270 | |
| 1271 | // Queries can touch both databases after the ATTACH. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1272 | EXPECT_TRUE(db_->Execute("INSERT INTO foo SELECT a, b FROM other.bar")); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1273 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1274 | Statement s(db_->GetUniqueStatement("SELECT COUNT(*) FROM foo")); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1275 | ASSERT_TRUE(s.Step()); |
| 1276 | EXPECT_EQ(1, s.ColumnInt(0)); |
| 1277 | } |
| 1278 | |
| 1279 | // Detaching the same database fails, database is locked in the transaction. |
| 1280 | { |
| 1281 | sql::test::ScopedErrorExpecter expecter; |
| 1282 | expecter.ExpectError(SQLITE_ERROR); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1283 | EXPECT_FALSE(DatabaseTestPeer::DetachDatabase(db_.get(), kAttachmentPoint)); |
| 1284 | EXPECT_TRUE(db_->IsSQLValid("SELECT count(*) from other.bar")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1285 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1286 | } |
| 1287 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1288 | // Detach succeeds when the transaction is closed. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1289 | db_->RollbackTransaction(); |
| 1290 | EXPECT_TRUE(DatabaseTestPeer::DetachDatabase(db_.get(), kAttachmentPoint)); |
| 1291 | EXPECT_FALSE(db_->IsSQLValid("SELECT count(*) from other.bar")); |
[email protected] | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1292 | } |
| 1293 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1294 | TEST_P(SQLDatabaseTest, Basic_QuickIntegrityCheck) { |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1295 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1296 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 1297 | EXPECT_TRUE(db_->QuickIntegrityCheck()); |
| 1298 | db_->Close(); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1299 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1300 | ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_)); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1301 | |
| 1302 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1303 | sql::test::ScopedErrorExpecter expecter; |
| 1304 | expecter.ExpectError(SQLITE_CORRUPT); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1305 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1306 | EXPECT_FALSE(db_->QuickIntegrityCheck()); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1307 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1308 | } |
| 1309 | } |
| 1310 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1311 | TEST_P(SQLDatabaseTest, Basic_FullIntegrityCheck) { |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1312 | const std::string kOk("ok"); |
| 1313 | std::vector<std::string> messages; |
| 1314 | |
| 1315 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1316 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 1317 | EXPECT_TRUE(db_->FullIntegrityCheck(&messages)); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1318 | EXPECT_EQ(1u, messages.size()); |
| 1319 | EXPECT_EQ(kOk, messages[0]); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1320 | db_->Close(); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1321 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1322 | ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_)); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1323 | |
| 1324 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1325 | sql::test::ScopedErrorExpecter expecter; |
| 1326 | expecter.ExpectError(SQLITE_CORRUPT); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1327 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1328 | EXPECT_TRUE(db_->FullIntegrityCheck(&messages)); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1329 | EXPECT_LT(1u, messages.size()); |
| 1330 | EXPECT_NE(kOk, messages[0]); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1331 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
[email protected] | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | // TODO(shess): CorruptTableOrIndex could be used to produce a |
| 1335 | // file that would pass the quick check and fail the full check. |
| 1336 | } |
| 1337 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1338 | TEST_P(SQLDatabaseTest, OnMemoryDump) { |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 1339 | base::trace_event::MemoryDumpArgs args = { |
| 1340 | base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; |
erikchen | f62ea04 | 2018-05-25 21:30:57 | [diff] [blame] | 1341 | base::trace_event::ProcessMemoryDump pmd(args); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1342 | ASSERT_TRUE(db_->memory_dump_provider_->OnMemoryDump(args, &pmd)); |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 1343 | EXPECT_GE(pmd.allocator_dumps().size(), 1u); |
| 1344 | } |
| 1345 | |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1346 | // Test that the functions to collect diagnostic data run to completion, without |
| 1347 | // worrying too much about what they generate (since that will change). |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1348 | TEST_P(SQLDatabaseTest, CollectDiagnosticInfo) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1349 | const std::string corruption_info = db_->CollectCorruptionInfo(); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1350 | EXPECT_NE(std::string::npos, corruption_info.find("SQLITE_CORRUPT")); |
| 1351 | EXPECT_NE(std::string::npos, corruption_info.find("integrity_check")); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1352 | |
| 1353 | // A statement to see in the results. |
| 1354 | const char* kSimpleSql = "SELECT 'mountain'"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1355 | Statement s(db_->GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1356 | |
| 1357 | // Error includes the statement. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1358 | const std::string readonly_info = db_->CollectErrorInfo(SQLITE_READONLY, &s); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1359 | EXPECT_NE(std::string::npos, readonly_info.find(kSimpleSql)); |
| 1360 | |
| 1361 | // Some other error doesn't include the statment. |
| 1362 | // TODO(shess): This is weak. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1363 | const std::string full_info = db_->CollectErrorInfo(SQLITE_FULL, nullptr); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1364 | EXPECT_EQ(std::string::npos, full_info.find(kSimpleSql)); |
| 1365 | |
| 1366 | // A table to see in the SQLITE_ERROR results. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1367 | EXPECT_TRUE(db_->Execute("CREATE TABLE volcano (x)")); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1368 | |
| 1369 | // Version info to see in the SQLITE_ERROR results. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1370 | MetaTable meta_table; |
| 1371 | ASSERT_TRUE(meta_table.Init(db_.get(), 4, 4)); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1372 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1373 | const std::string error_info = db_->CollectErrorInfo(SQLITE_ERROR, &s); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1374 | EXPECT_NE(std::string::npos, error_info.find(kSimpleSql)); |
| 1375 | EXPECT_NE(std::string::npos, error_info.find("volcano")); |
| 1376 | EXPECT_NE(std::string::npos, error_info.find("version: 4")); |
| 1377 | } |
| 1378 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1379 | // Test that a fresh database has mmap enabled by default, if mmap'ed I/O is |
| 1380 | // enabled by SQLite. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1381 | TEST_P(SQLDatabaseTest, MmapInitiallyEnabled) { |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1382 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1383 | Statement s(db_->GetUniqueStatement("PRAGMA mmap_size")); |
Victor Costan | 42988a9 | 2018-02-06 02:22:14 | [diff] [blame] | 1384 | ASSERT_TRUE(s.Step()) |
| 1385 | << "All supported SQLite versions should have mmap support"; |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1386 | |
| 1387 | // If mmap I/O is not on, attempt to turn it on. If that succeeds, then |
| 1388 | // Open() should have turned it on. If mmap support is disabled, 0 is |
| 1389 | // returned. If the VFS does not understand SQLITE_FCNTL_MMAP_SIZE (for |
| 1390 | // instance MojoVFS), -1 is returned. |
| 1391 | if (s.ColumnInt(0) <= 0) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1392 | ASSERT_TRUE(db_->Execute("PRAGMA mmap_size = 1048576")); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1393 | s.Reset(true); |
| 1394 | ASSERT_TRUE(s.Step()); |
| 1395 | EXPECT_LE(s.ColumnInt(0), 0); |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | // Test that explicit disable prevents mmap'ed I/O. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1400 | db_->Close(); |
| 1401 | Database::Delete(db_path_); |
| 1402 | db_->set_mmap_disabled(); |
| 1403 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1404 | EXPECT_EQ("0", ExecuteWithResult(db_.get(), "PRAGMA mmap_size")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | // Test whether a fresh database gets mmap enabled when using alternate status |
| 1408 | // storage. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1409 | TEST_P(SQLDatabaseTest, MmapInitiallyEnabledAltStatus) { |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1410 | // Re-open fresh database with alt-status flag set. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1411 | db_->Close(); |
| 1412 | Database::Delete(db_path_); |
Victor Costan | 8ec18ee4 | 2021-07-13 19:45:32 | [diff] [blame] | 1413 | |
| 1414 | DatabaseOptions options = GetDBOptions(); |
| 1415 | options.mmap_alt_status_discouraged = true; |
Victor Costan | fe078f9 | 2021-07-19 20:02:59 | [diff] [blame] | 1416 | options.enable_views_discouraged = true; |
Victor Costan | 8ec18ee4 | 2021-07-13 19:45:32 | [diff] [blame] | 1417 | db_ = std::make_unique<Database>(options); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1418 | ASSERT_TRUE(db_->Open(db_path_)); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1419 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1420 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1421 | Statement s(db_->GetUniqueStatement("PRAGMA mmap_size")); |
Victor Costan | 42988a9 | 2018-02-06 02:22:14 | [diff] [blame] | 1422 | ASSERT_TRUE(s.Step()) |
| 1423 | << "All supported SQLite versions should have mmap support"; |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1424 | |
| 1425 | // If mmap I/O is not on, attempt to turn it on. If that succeeds, then |
| 1426 | // Open() should have turned it on. If mmap support is disabled, 0 is |
| 1427 | // returned. If the VFS does not understand SQLITE_FCNTL_MMAP_SIZE (for |
| 1428 | // instance MojoVFS), -1 is returned. |
| 1429 | if (s.ColumnInt(0) <= 0) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1430 | ASSERT_TRUE(db_->Execute("PRAGMA mmap_size = 1048576")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1431 | s.Reset(true); |
| 1432 | ASSERT_TRUE(s.Step()); |
| 1433 | EXPECT_LE(s.ColumnInt(0), 0); |
| 1434 | } |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1435 | } |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1436 | |
| 1437 | // Test that explicit disable overrides set_mmap_alt_status(). |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1438 | db_->Close(); |
| 1439 | Database::Delete(db_path_); |
| 1440 | db_->set_mmap_disabled(); |
| 1441 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1442 | EXPECT_EQ("0", ExecuteWithResult(db_.get(), "PRAGMA mmap_size")); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1443 | } |
| 1444 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1445 | TEST_P(SQLDatabaseTest, GetAppropriateMmapSize) { |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1446 | const size_t kMmapAlot = 25 * 1024 * 1024; |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1447 | int64_t mmap_status = MetaTable::kMmapFailure; |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1448 | |
| 1449 | // If there is no meta table (as for a fresh database), assume that everything |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1450 | // should be mapped, and the status of the meta table is not affected. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1451 | ASSERT_TRUE(!db_->DoesTableExist("meta")); |
| 1452 | ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot); |
| 1453 | ASSERT_TRUE(!db_->DoesTableExist("meta")); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1454 | |
| 1455 | // When the meta table is first created, it sets up to map everything. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1456 | MetaTable().Init(db_.get(), 1, 1); |
| 1457 | ASSERT_TRUE(db_->DoesTableExist("meta")); |
| 1458 | ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot); |
| 1459 | ASSERT_TRUE(MetaTable::GetMmapStatus(db_.get(), &mmap_status)); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1460 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1461 | |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1462 | // Preload with partial progress of one page. Should map everything. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1463 | ASSERT_TRUE(db_->Execute("REPLACE INTO meta VALUES ('mmap_status', 1)")); |
| 1464 | ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot); |
| 1465 | ASSERT_TRUE(MetaTable::GetMmapStatus(db_.get(), &mmap_status)); |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1466 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1467 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1468 | // Failure status maps nothing. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1469 | ASSERT_TRUE(db_->Execute("REPLACE INTO meta VALUES ('mmap_status', -2)")); |
| 1470 | ASSERT_EQ(0UL, db_->GetAppropriateMmapSize()); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1471 | |
| 1472 | // Re-initializing the meta table does not re-create the key if the table |
| 1473 | // already exists. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1474 | ASSERT_TRUE(db_->Execute("DELETE FROM meta WHERE key = 'mmap_status'")); |
| 1475 | MetaTable().Init(db_.get(), 1, 1); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1476 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1477 | ASSERT_TRUE(MetaTable::GetMmapStatus(db_.get(), &mmap_status)); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1478 | ASSERT_EQ(0, mmap_status); |
| 1479 | |
| 1480 | // With no key, map everything and create the key. |
| 1481 | // TODO(shess): This really should be "maps everything after validating it", |
| 1482 | // but that is more complicated to structure. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1483 | ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot); |
| 1484 | ASSERT_TRUE(MetaTable::GetMmapStatus(db_.get(), &mmap_status)); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1485 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1486 | } |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1487 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1488 | TEST_P(SQLDatabaseTest, GetAppropriateMmapSizeAltStatus) { |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1489 | const size_t kMmapAlot = 25 * 1024 * 1024; |
| 1490 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1491 | // At this point, Database still expects a future [meta] table. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1492 | ASSERT_FALSE(db_->DoesTableExist("meta")); |
| 1493 | ASSERT_FALSE(db_->DoesViewExist("MmapStatus")); |
| 1494 | ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot); |
| 1495 | ASSERT_FALSE(db_->DoesTableExist("meta")); |
| 1496 | ASSERT_FALSE(db_->DoesViewExist("MmapStatus")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1497 | |
| 1498 | // Using alt status, everything should be mapped, with state in the view. |
Victor Costan | 8ec18ee4 | 2021-07-13 19:45:32 | [diff] [blame] | 1499 | DatabaseOptions options = GetDBOptions(); |
| 1500 | options.mmap_alt_status_discouraged = true; |
Victor Costan | fe078f9 | 2021-07-19 20:02:59 | [diff] [blame] | 1501 | options.enable_views_discouraged = true; |
Victor Costan | 8ec18ee4 | 2021-07-13 19:45:32 | [diff] [blame] | 1502 | db_ = std::make_unique<Database>(options); |
| 1503 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1504 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1505 | ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot); |
| 1506 | ASSERT_FALSE(db_->DoesTableExist("meta")); |
| 1507 | ASSERT_TRUE(db_->DoesViewExist("MmapStatus")); |
Raul Tambre | 6c708e3 | 2019-02-08 22:35:14 | [diff] [blame] | 1508 | EXPECT_EQ(base::NumberToString(MetaTable::kMmapSuccess), |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1509 | ExecuteWithResult(db_.get(), "SELECT * FROM MmapStatus")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1510 | |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1511 | // Also maps everything when kMmapSuccess is already in the view. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1512 | ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1513 | |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1514 | // Preload with partial progress of one page. Should map everything. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1515 | ASSERT_TRUE(db_->Execute("DROP VIEW MmapStatus")); |
| 1516 | ASSERT_TRUE(db_->Execute("CREATE VIEW MmapStatus (value) AS SELECT 1")); |
| 1517 | ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot); |
Raul Tambre | 6c708e3 | 2019-02-08 22:35:14 | [diff] [blame] | 1518 | EXPECT_EQ(base::NumberToString(MetaTable::kMmapSuccess), |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1519 | ExecuteWithResult(db_.get(), "SELECT * FROM MmapStatus")); |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1520 | |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1521 | // Failure status leads to nothing being mapped. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1522 | ASSERT_TRUE(db_->Execute("DROP VIEW MmapStatus")); |
| 1523 | ASSERT_TRUE(db_->Execute("CREATE VIEW MmapStatus (value) AS SELECT -2")); |
| 1524 | ASSERT_EQ(0UL, db_->GetAppropriateMmapSize()); |
Raul Tambre | 6c708e3 | 2019-02-08 22:35:14 | [diff] [blame] | 1525 | EXPECT_EQ(base::NumberToString(MetaTable::kMmapFailure), |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1526 | ExecuteWithResult(db_.get(), "SELECT * FROM MmapStatus")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1527 | } |
| 1528 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1529 | TEST_P(SQLDatabaseTest, GetMemoryUsage) { |
Victor Costan | d6e7325 | 2020-10-14 21:11:25 | [diff] [blame] | 1530 | // Databases with mmap enabled may not follow the assumptions below. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1531 | db_->Close(); |
| 1532 | db_->set_mmap_disabled(); |
| 1533 | ASSERT_TRUE(db_->Open(db_path_)); |
Victor Costan | d6e7325 | 2020-10-14 21:11:25 | [diff] [blame] | 1534 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1535 | int initial_memory = db_->GetMemoryUsage(); |
Victor Costan | d6e7325 | 2020-10-14 21:11:25 | [diff] [blame] | 1536 | EXPECT_GT(initial_memory, 0) |
| 1537 | << "SQLite should always use some memory for a database"; |
| 1538 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1539 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
| 1540 | ASSERT_TRUE(db_->Execute("INSERT INTO foo(a, b) VALUES (12, 13)")); |
Victor Costan | d6e7325 | 2020-10-14 21:11:25 | [diff] [blame] | 1541 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1542 | int post_query_memory = db_->GetMemoryUsage(); |
Victor Costan | d6e7325 | 2020-10-14 21:11:25 | [diff] [blame] | 1543 | EXPECT_GT(post_query_memory, initial_memory) |
| 1544 | << "Page cache usage should go up after executing queries"; |
| 1545 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1546 | db_->TrimMemory(); |
| 1547 | int post_trim_memory = db_->GetMemoryUsage(); |
Victor Costan | d6e7325 | 2020-10-14 21:11:25 | [diff] [blame] | 1548 | EXPECT_GT(post_query_memory, post_trim_memory) |
| 1549 | << "Page cache usage should go down after calling TrimMemory()"; |
| 1550 | } |
| 1551 | |
Victor Costan | 4e442d0 | 2021-07-20 17:43:13 | [diff] [blame] | 1552 | TEST_P(SQLDatabaseTest, DoubleQuotedStringLiteralsDisabledByDefault) { |
| 1553 | ASSERT_TRUE(db_->Execute("CREATE TABLE data(item TEXT NOT NULL);")); |
| 1554 | |
| 1555 | struct TestCase { |
| 1556 | const char* sql; |
| 1557 | bool is_valid; |
| 1558 | }; |
| 1559 | std::vector<TestCase> test_cases = { |
| 1560 | // DML tests. |
| 1561 | {"SELECT item FROM data WHERE item >= 'string literal'", true}, |
| 1562 | {"SELECT item FROM data WHERE item >= \"string literal\"", false}, |
| 1563 | {"INSERT INTO data(item) VALUES('string literal')", true}, |
| 1564 | {"INSERT INTO data(item) VALUES(\"string literal\")", false}, |
| 1565 | {"UPDATE data SET item = 'string literal'", true}, |
| 1566 | {"UPDATE data SET item = \"string literal\"", false}, |
| 1567 | {"DELETE FROM data WHERE item >= 'string literal'", true}, |
| 1568 | {"DELETE FROM data WHERE item >= \"string literal\"", false}, |
| 1569 | |
| 1570 | // DDL tests. |
| 1571 | {"CREATE INDEX data_item ON data(item) WHERE item >= 'string literal'", |
| 1572 | true}, |
| 1573 | {"CREATE INDEX data_item ON data(item) WHERE item >= \"string literal\"", |
| 1574 | false}, |
| 1575 | {"CREATE TABLE data2(item TEXT DEFAULT 'string literal')", true}, |
| 1576 | |
| 1577 | // This should be an invalid DDL statement, due to the double-quoted |
| 1578 | // string literal. However, SQLite currently parses it. |
| 1579 | {"CREATE TABLE data2(item TEXT DEFAULT \"string literal\")", true}, |
| 1580 | }; |
| 1581 | |
| 1582 | for (const TestCase& test_case : test_cases) { |
| 1583 | SCOPED_TRACE(test_case.sql); |
| 1584 | |
| 1585 | EXPECT_EQ(test_case.is_valid, db_->IsSQLValid(test_case.sql)); |
| 1586 | } |
| 1587 | } |
| 1588 | |
Victor Costan | 7c23482 | 2021-07-13 03:03:02 | [diff] [blame] | 1589 | TEST_P(SQLDatabaseTest, TriggersDisabledByDefault) { |
| 1590 | ASSERT_TRUE(db_->Execute("CREATE TABLE data(id INTEGER)")); |
| 1591 | |
| 1592 | // sqlite3_db_config() currently only disables running triggers. Schema |
| 1593 | // operations on triggers are still allowed. |
| 1594 | EXPECT_TRUE( |
| 1595 | db_->Execute("CREATE TRIGGER trigger AFTER INSERT ON data " |
| 1596 | "BEGIN DELETE FROM data; END")); |
| 1597 | |
| 1598 | ASSERT_TRUE(db_->Execute("INSERT INTO data(id) VALUES(42)")); |
| 1599 | |
| 1600 | Statement select(db_->GetUniqueStatement("SELECT id FROM data")); |
| 1601 | EXPECT_TRUE(select.Step()) |
| 1602 | << "If the trigger did not run, the table should not be empty."; |
| 1603 | EXPECT_EQ(42, select.ColumnInt64(0)); |
| 1604 | |
| 1605 | // sqlite3_db_config() currently only disables running triggers. Schema |
| 1606 | // operations on triggers are still allowed. |
| 1607 | EXPECT_TRUE(db_->Execute("DROP TRIGGER IF EXISTS trigger")); |
| 1608 | } |
| 1609 | |
Victor Costan | fe078f9 | 2021-07-19 20:02:59 | [diff] [blame] | 1610 | TEST_P(SQLDatabaseTest, ViewsDisabledByDefault) { |
| 1611 | EXPECT_FALSE(GetDBOptions().enable_views_discouraged); |
| 1612 | |
| 1613 | // sqlite3_db_config() currently only disables querying views. Schema |
| 1614 | // operations on views are still allowed. |
| 1615 | ASSERT_TRUE(db_->Execute("CREATE VIEW view(id) AS SELECT 1")); |
| 1616 | |
| 1617 | { |
| 1618 | sql::test::ScopedErrorExpecter expecter; |
| 1619 | expecter.ExpectError(SQLITE_ERROR); |
| 1620 | Statement select_from_view(db_->GetUniqueStatement("SELECT id FROM view")); |
| 1621 | EXPECT_FALSE(select_from_view.is_valid()); |
| 1622 | EXPECT_TRUE(expecter.SawExpectedErrors()); |
| 1623 | } |
| 1624 | |
| 1625 | // sqlite3_db_config() currently only disables querying views. Schema |
| 1626 | // operations on views are still allowed. |
| 1627 | EXPECT_TRUE(db_->Execute("DROP VIEW IF EXISTS view")); |
| 1628 | } |
| 1629 | |
| 1630 | TEST_P(SQLDatabaseTest, ViewsEnabled) { |
| 1631 | DatabaseOptions options = GetDBOptions(); |
| 1632 | options.enable_views_discouraged = true; |
| 1633 | db_ = std::make_unique<Database>(options); |
| 1634 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1635 | |
| 1636 | ASSERT_TRUE(db_->Execute("CREATE VIEW view(id) AS SELECT 1")); |
| 1637 | |
| 1638 | Statement select_from_view(db_->GetUniqueStatement("SELECT id FROM view")); |
| 1639 | ASSERT_TRUE(select_from_view.is_valid()); |
| 1640 | EXPECT_TRUE(select_from_view.Step()); |
| 1641 | EXPECT_EQ(1, select_from_view.ColumnInt64(0)); |
| 1642 | |
| 1643 | EXPECT_TRUE(db_->Execute("DROP VIEW IF EXISTS view")); |
| 1644 | } |
| 1645 | |
Victor Costan | 04fc909 | 2021-07-17 00:09:34 | [diff] [blame] | 1646 | TEST_P(SQLDatabaseTest, VirtualTablesDisabledByDefault) { |
| 1647 | EXPECT_FALSE(GetDBOptions().enable_virtual_tables_discouraged); |
| 1648 | |
| 1649 | // sqlite3_prepare_v3() currently only disables accessing virtual tables. |
| 1650 | // Schema operations on virtual tables are still allowed. |
| 1651 | ASSERT_TRUE(db_->Execute( |
| 1652 | "CREATE VIRTUAL TABLE fts_table USING fts3(data_table, content TEXT)")); |
| 1653 | |
| 1654 | { |
| 1655 | sql::test::ScopedErrorExpecter expecter; |
| 1656 | expecter.ExpectError(SQLITE_ERROR); |
| 1657 | Statement select_from_vtable(db_->GetUniqueStatement( |
| 1658 | "SELECT content FROM fts_table WHERE content MATCH 'pattern'")); |
| 1659 | EXPECT_FALSE(select_from_vtable.is_valid()); |
| 1660 | EXPECT_TRUE(expecter.SawExpectedErrors()); |
| 1661 | } |
| 1662 | |
| 1663 | // sqlite3_prepare_v3() currently only disables accessing virtual tables. |
| 1664 | // Schema operations on virtual tables are still allowed. |
| 1665 | EXPECT_TRUE(db_->Execute("DROP TABLE IF EXISTS fts_table")); |
| 1666 | } |
| 1667 | |
| 1668 | TEST_P(SQLDatabaseTest, VirtualTablesEnabled) { |
| 1669 | DatabaseOptions options = GetDBOptions(); |
| 1670 | options.enable_virtual_tables_discouraged = true; |
| 1671 | db_ = std::make_unique<Database>(options); |
| 1672 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1673 | |
| 1674 | ASSERT_TRUE(db_->Execute( |
| 1675 | "CREATE VIRTUAL TABLE fts_table USING fts3(data_table, content TEXT)")); |
| 1676 | |
| 1677 | Statement select_from_vtable(db_->GetUniqueStatement( |
| 1678 | "SELECT content FROM fts_table WHERE content MATCH 'pattern'")); |
| 1679 | ASSERT_TRUE(select_from_vtable.is_valid()); |
| 1680 | EXPECT_FALSE(select_from_vtable.Step()); |
| 1681 | |
| 1682 | EXPECT_TRUE(db_->Execute("DROP TABLE IF EXISTS fts_table")); |
| 1683 | } |
| 1684 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1685 | class SQLDatabaseTestExclusiveMode : public testing::Test, |
| 1686 | public testing::WithParamInterface<bool> { |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 1687 | public: |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1688 | ~SQLDatabaseTestExclusiveMode() override = default; |
| 1689 | |
| 1690 | void SetUp() override { |
| 1691 | db_ = std::make_unique<Database>(GetDBOptions()); |
| 1692 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 1693 | db_path_ = temp_dir_.GetPath().AppendASCII("recovery_test.sqlite"); |
| 1694 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1695 | } |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1696 | |
| 1697 | DatabaseOptions GetDBOptions() { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1698 | DatabaseOptions options; |
| 1699 | options.wal_mode = IsWALEnabled(); |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1700 | options.exclusive_locking = true; |
| 1701 | return options; |
| 1702 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1703 | |
| 1704 | bool IsWALEnabled() { return GetParam(); } |
| 1705 | |
| 1706 | protected: |
| 1707 | base::ScopedTempDir temp_dir_; |
| 1708 | base::FilePath db_path_; |
| 1709 | std::unique_ptr<Database> db_; |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 1710 | }; |
Victor Costan | b223079 | 2020-10-09 08:35:14 | [diff] [blame] | 1711 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1712 | TEST_P(SQLDatabaseTestExclusiveMode, LockingModeExclusive) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1713 | EXPECT_EQ(ExecuteWithResult(db_.get(), "PRAGMA locking_mode"), "exclusive"); |
Victor Costan | b223079 | 2020-10-09 08:35:14 | [diff] [blame] | 1714 | } |
| 1715 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1716 | TEST_P(SQLDatabaseTest, LockingModeNormal) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1717 | EXPECT_EQ(ExecuteWithResult(db_.get(), "PRAGMA locking_mode"), "normal"); |
Victor Costan | b223079 | 2020-10-09 08:35:14 | [diff] [blame] | 1718 | } |
| 1719 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1720 | TEST_P(SQLDatabaseTest, OpenedInCorrectMode) { |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 1721 | std::string expected_mode = IsWALEnabled() ? "wal" : "truncate"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1722 | EXPECT_EQ(ExecuteWithResult(db_.get(), "PRAGMA journal_mode"), expected_mode); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1723 | } |
| 1724 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1725 | TEST_P(SQLDatabaseTest, CheckpointDatabase) { |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 1726 | if (!IsWALEnabled()) |
| 1727 | return; |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1728 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1729 | base::FilePath wal_path = Database::WriteAheadLogPath(db_path_); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1730 | |
| 1731 | int64_t wal_size = 0; |
| 1732 | // WAL file initially empty. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1733 | EXPECT_TRUE(base::PathExists(wal_path)); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1734 | base::GetFileSize(wal_path, &wal_size); |
| 1735 | EXPECT_EQ(wal_size, 0); |
| 1736 | |
| 1737 | ASSERT_TRUE( |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1738 | db_->Execute("CREATE TABLE foo (id INTEGER UNIQUE, value INTEGER)")); |
| 1739 | ASSERT_TRUE(db_->Execute("INSERT INTO foo VALUES (1, 1)")); |
| 1740 | ASSERT_TRUE(db_->Execute("INSERT INTO foo VALUES (2, 2)")); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1741 | |
| 1742 | // Writes reach WAL file but not db file. |
| 1743 | base::GetFileSize(wal_path, &wal_size); |
| 1744 | EXPECT_GT(wal_size, 0); |
| 1745 | |
| 1746 | int64_t db_size = 0; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1747 | base::GetFileSize(db_path_, &db_size); |
| 1748 | EXPECT_EQ(db_size, db_->page_size()); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1749 | |
| 1750 | // Checkpoint database to immediately propagate writes to DB file. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1751 | EXPECT_TRUE(db_->CheckpointDatabase()); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1752 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1753 | base::GetFileSize(db_path_, &db_size); |
| 1754 | EXPECT_GT(db_size, db_->page_size()); |
| 1755 | EXPECT_EQ(ExecuteWithResult(db_.get(), "SELECT value FROM foo where id=1"), |
| 1756 | "1"); |
| 1757 | EXPECT_EQ(ExecuteWithResult(db_.get(), "SELECT value FROM foo where id=2"), |
| 1758 | "2"); |
Shubham Aggarwal | be4f97ce | 2020-06-19 15:58:57 | [diff] [blame] | 1759 | } |
| 1760 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1761 | TEST_P(SQLDatabaseTest, CorruptSizeInHeaderTest) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1762 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (x)")); |
| 1763 | ASSERT_TRUE(db_->Execute("CREATE TABLE bar (x)")); |
| 1764 | db_->Close(); |
Shubham Aggarwal | 1c06850ffa | 2020-07-07 13:42:57 | [diff] [blame] | 1765 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1766 | ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_)); |
Shubham Aggarwal | 1c06850ffa | 2020-07-07 13:42:57 | [diff] [blame] | 1767 | { |
| 1768 | sql::test::ScopedErrorExpecter expecter; |
| 1769 | expecter.ExpectError(SQLITE_CORRUPT); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1770 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1771 | EXPECT_FALSE(db_->Execute("INSERT INTO foo values (1)")); |
| 1772 | EXPECT_FALSE(db_->DoesTableExist("foo")); |
| 1773 | EXPECT_FALSE(db_->DoesTableExist("bar")); |
| 1774 | EXPECT_FALSE(db_->Execute("SELECT * FROM foo")); |
Shubham Aggarwal | 1c06850ffa | 2020-07-07 13:42:57 | [diff] [blame] | 1775 | EXPECT_TRUE(expecter.SawExpectedErrors()); |
| 1776 | } |
| 1777 | } |
| 1778 | |
Victor Costan | 15d905de | 2021-01-07 22:18:58 | [diff] [blame] | 1779 | // WAL mode is currently not supported on Fuchsia. |
| 1780 | #if !defined(OS_FUCHSIA) |
| 1781 | INSTANTIATE_TEST_SUITE_P(JournalMode, SQLDatabaseTest, testing::Bool()); |
| 1782 | INSTANTIATE_TEST_SUITE_P(JournalMode, |
| 1783 | SQLDatabaseTestExclusiveMode, |
| 1784 | testing::Bool()); |
| 1785 | #else |
| 1786 | INSTANTIATE_TEST_SUITE_P(JournalMode, SQLDatabaseTest, testing::Values(false)); |
| 1787 | INSTANTIATE_TEST_SUITE_P(JournalMode, |
| 1788 | SQLDatabaseTestExclusiveMode, |
| 1789 | testing::Values(false)); |
| 1790 | #endif |
| 1791 | |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1792 | } // namespace sql |