Avi Drissman | 69b874f | 2022-09-15 19:11:14 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
brettw@chromium.org | 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> |
Dan McArdle | 4feabeb5 | 2024-01-03 15:17:13 | [diff] [blame] | 9 | |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | #include <utility> |
| 13 | #include <vector> |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 14 | |
Ho Cheung | 4752435 | 2023-08-08 03:41:10 | [diff] [blame] | 15 | #include "base/containers/contains.h" |
Will Harris | 711a5ec | 2023-04-04 21:43:37 | [diff] [blame] | 16 | #include "base/files/file.h" |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 17 | #include "base/files/file_util.h" |
brettw@chromium.org | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 18 | #include "base/files/scoped_temp_dir.h" |
Avi Drissman | 2e85357a | 2023-01-12 21:57:32 | [diff] [blame] | 19 | #include "base/functional/bind.h" |
| 20 | #include "base/functional/callback_helpers.h" |
shess@chromium.org | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 21 | #include "base/logging.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 22 | #include "base/memory/raw_ptr.h" |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 23 | #include "base/sequence_checker.h" |
Will Harris | 711a5ec | 2023-04-04 21:43:37 | [diff] [blame] | 24 | #include "base/strings/strcat.h" |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 25 | #include "base/strings/string_number_conversions.h" |
Victor Costan | c02b7af | 2021-07-09 13:26:45 | [diff] [blame] | 26 | #include "base/test/bind.h" |
Victor Costan | 613b430 | 2018-11-20 05:32:43 | [diff] [blame] | 27 | #include "base/test/gtest_util.h" |
Devlin Cronin | 147687f | 2018-06-05 18:03:56 | [diff] [blame] | 28 | #include "base/test/metrics/histogram_tester.h" |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 29 | #include "base/thread_annotations.h" |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 30 | #include "base/trace_event/process_memory_dump.h" |
Scott Graham | 57ee5482 | 2017-09-13 06:37:56 | [diff] [blame] | 31 | #include "build/build_config.h" |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 32 | #include "sql/database_memory_dump_provider.h" |
yongsheng.zhu@intel.com | 1348765 | 2012-07-24 08:25:53 | [diff] [blame] | 33 | #include "sql/meta_table.h" |
Dan McArdle | 4feabeb5 | 2024-01-03 15:17:13 | [diff] [blame] | 34 | #include "sql/recovery.h" |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 35 | #include "sql/sql_features.h" |
brettw@chromium.org | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 36 | #include "sql/statement.h" |
Victor Costan | d1a217b | 2019-04-02 02:44:21 | [diff] [blame] | 37 | #include "sql/test/database_test_peer.h" |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 38 | #include "sql/test/scoped_error_expecter.h" |
kinuko@chromium.org | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 39 | #include "sql/test/test_helpers.h" |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 40 | #include "sql/transaction.h" |
Victor Costan | 4b67f54 | 2022-02-24 17:40:55 | [diff] [blame] | 41 | #include "testing/gmock/include/gmock/gmock.h" |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 42 | #include "testing/gtest/include/gtest/gtest.h" |
phajdan.jr@chromium.org | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 43 | #include "third_party/sqlite/sqlite3.h" |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 44 | |
shess | 58b8df8 | 2015-06-03 00:19:32 | [diff] [blame] | 45 | namespace sql { |
Victor Costan | 7f6abbbe | 2018-07-29 02:57:27 | [diff] [blame] | 46 | |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 47 | namespace { |
| 48 | |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 49 | using sql::test::ExecuteWithResult; |
| 50 | |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 51 | // Helper to return the count of items in sqlite_schema. Return -1 in |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 52 | // case of error. |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 53 | int SqliteSchemaCount(Database* db) { |
| 54 | const char* kSchemaCount = "SELECT COUNT(*) FROM sqlite_schema"; |
| 55 | Statement s(db->GetUniqueStatement(kSchemaCount)); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 56 | return s.Step() ? s.ColumnInt(0) : -1; |
| 57 | } |
| 58 | |
shess | 1cf87f2 | 2016-10-25 22:18:29 | [diff] [blame] | 59 | // Handle errors by blowing away the database. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 60 | void RazeErrorCallback(Database* db, |
shess | 1cf87f2 | 2016-10-25 22:18:29 | [diff] [blame] | 61 | int expected_error, |
| 62 | int error, |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 63 | Statement* stmt) { |
shess | 1cf87f2 | 2016-10-25 22:18:29 | [diff] [blame] | 64 | // Nothing here needs extended errors at this time. |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 65 | EXPECT_EQ(expected_error, expected_error & 0xff); |
| 66 | EXPECT_EQ(expected_error, error & 0xff); |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 67 | db->RazeAndPoison(); |
shess | 1cf87f2 | 2016-10-25 22:18:29 | [diff] [blame] | 68 | } |
| 69 | |
Xiaohan Wang | 7d09c5e | 2022-01-08 02:37:36 | [diff] [blame] | 70 | #if BUILDFLAG(IS_POSIX) |
shess@chromium.org | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 71 | // Set a umask and restore the old mask on destruction. Cribbed from |
| 72 | // shared_memory_unittest.cc. Used by POSIX-only UserPermission test. |
| 73 | class ScopedUmaskSetter { |
| 74 | public: |
| 75 | explicit ScopedUmaskSetter(mode_t target_mask) { |
| 76 | old_umask_ = umask(target_mask); |
| 77 | } |
| 78 | ~ScopedUmaskSetter() { umask(old_umask_); } |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 79 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 80 | ScopedUmaskSetter(const ScopedUmaskSetter&) = delete; |
| 81 | ScopedUmaskSetter& operator=(const ScopedUmaskSetter&) = delete; |
| 82 | |
shess@chromium.org | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 83 | private: |
| 84 | mode_t old_umask_; |
shess@chromium.org | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 85 | }; |
Xiaohan Wang | 7d09c5e | 2022-01-08 02:37:36 | [diff] [blame] | 86 | #endif // BUILDFLAG(IS_POSIX) |
shess@chromium.org | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 87 | |
Etienne Bergeron | 14c5d71 | 2023-11-13 15:01:15 | [diff] [blame] | 88 | bool IsOpenedInCorrectJournalMode(Database* db, bool is_wal) { |
| 89 | std::string expected_mode = is_wal ? "wal" : "truncate"; |
| 90 | return ExecuteWithResult(db, "PRAGMA journal_mode") == expected_mode; |
| 91 | } |
| 92 | |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 93 | } // namespace |
| 94 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 95 | // 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] | 96 | class SQLDatabaseTest : public testing::Test, |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 97 | public testing::WithParamInterface<bool> { |
| 98 | public: |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 99 | enum class OverwriteType { |
| 100 | kTruncate, |
| 101 | kOverwrite, |
| 102 | }; |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 103 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 104 | ~SQLDatabaseTest() override = default; |
| 105 | |
| 106 | void SetUp() override { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 107 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 108 | db_path_ = temp_dir_.GetPath().AppendASCII("database_test.sqlite"); |
Dan McArdle | 4feabeb5 | 2024-01-03 15:17:13 | [diff] [blame] | 109 | CreateFreshDB(); |
| 110 | } |
| 111 | |
| 112 | // Resets the database handle and deletes the backing file. On return, `db_` |
| 113 | // has just been opened on a fresh temp file named by `db_path_`. |
| 114 | void CreateFreshDB() { |
| 115 | ASSERT_FALSE(db_path_.empty()); |
| 116 | |
| 117 | db_.reset(); |
| 118 | ASSERT_TRUE(base::DeleteFile(db_path_)); |
| 119 | |
| 120 | db_ = std::make_unique<Database>(GetDBOptions()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 121 | ASSERT_TRUE(db_->Open(db_path_)); |
Dan McArdle | 4feabeb5 | 2024-01-03 15:17:13 | [diff] [blame] | 122 | ASSERT_TRUE(base::PathExists(db_path_)); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | DatabaseOptions GetDBOptions() { |
| 126 | DatabaseOptions options; |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 127 | options.wal_mode = IsWALEnabled(); |
| 128 | // TODO(crbug.com/1120969): Remove after switching to exclusive mode on by |
| 129 | // default. |
| 130 | options.exclusive_locking = false; |
Xiaohan Wang | 7d09c5e | 2022-01-08 02:37:36 | [diff] [blame] | 131 | #if BUILDFLAG(IS_FUCHSIA) // Exclusive mode needs to be enabled to enter WAL |
| 132 | // mode on Fuchsia |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 133 | if (IsWALEnabled()) { |
| 134 | options.exclusive_locking = true; |
| 135 | } |
Xiaohan Wang | 7d09c5e | 2022-01-08 02:37:36 | [diff] [blame] | 136 | #endif // BUILDFLAG(IS_FUCHSIA) |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 137 | return options; |
| 138 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 139 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 140 | bool IsWALEnabled() { return GetParam(); } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 141 | |
| 142 | bool TruncateDatabase() { |
| 143 | base::File file(db_path_, |
| 144 | base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 145 | return file.SetLength(0); |
| 146 | } |
| 147 | |
| 148 | bool OverwriteDatabaseHeader(OverwriteType type) { |
| 149 | base::File file(db_path_, |
| 150 | base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 151 | if (type == OverwriteType::kTruncate) { |
| 152 | if (!file.SetLength(0)) |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | static constexpr char kText[] = "Now is the winter of our discontent."; |
| 157 | constexpr int kTextBytes = sizeof(kText) - 1; |
| 158 | return file.Write(0, kText, kTextBytes) == kTextBytes; |
| 159 | } |
| 160 | |
| 161 | protected: |
| 162 | base::ScopedTempDir temp_dir_; |
| 163 | base::FilePath db_path_; |
| 164 | std::unique_ptr<Database> db_; |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 165 | }; |
| 166 | |
Victor Costan | 289f2c8b | 2021-07-22 06:33:47 | [diff] [blame] | 167 | TEST_P(SQLDatabaseTest, Execute_ValidStatement) { |
| 168 | ASSERT_TRUE(db_->Execute("CREATE TABLE data(contents TEXT)")); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 169 | EXPECT_EQ(SQLITE_OK, db_->GetErrorCode()); |
Victor Costan | 289f2c8b | 2021-07-22 06:33:47 | [diff] [blame] | 170 | } |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 171 | |
Victor Costan | 289f2c8b | 2021-07-22 06:33:47 | [diff] [blame] | 172 | TEST_P(SQLDatabaseTest, Execute_InvalidStatement) { |
Victor Costan | 205b96dc | 2021-07-21 20:27:46 | [diff] [blame] | 173 | { |
| 174 | sql::test::ScopedErrorExpecter error_expecter; |
| 175 | error_expecter.ExpectError(SQLITE_ERROR); |
Victor Costan | 289f2c8b | 2021-07-22 06:33:47 | [diff] [blame] | 176 | EXPECT_FALSE(db_->Execute("CREATE TABLE data(")); |
Victor Costan | 205b96dc | 2021-07-21 20:27:46 | [diff] [blame] | 177 | EXPECT_TRUE(error_expecter.SawExpectedErrors()); |
| 178 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 179 | EXPECT_EQ(SQLITE_ERROR, db_->GetErrorCode()); |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 180 | } |
| 181 | |
Victor Costan | 289f2c8b | 2021-07-22 06:33:47 | [diff] [blame] | 182 | TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_OneLineValid) { |
| 183 | ASSERT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data(contents TEXT)")); |
| 184 | EXPECT_EQ(SQLITE_OK, db_->GetErrorCode()); |
| 185 | } |
| 186 | |
| 187 | TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_OneLineInvalid) { |
| 188 | ASSERT_FALSE(db_->ExecuteScriptForTesting("CREATE TABLE data(")); |
| 189 | EXPECT_EQ(SQLITE_ERROR, db_->GetErrorCode()); |
| 190 | } |
| 191 | |
| 192 | TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_ExtraContents) { |
| 193 | EXPECT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data1(id)")) |
| 194 | << "Minimal statement"; |
| 195 | EXPECT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data2(id);")) |
| 196 | << "Extra semicolon"; |
| 197 | EXPECT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data3(id) -- Comment")) |
| 198 | << "Trailing comment"; |
| 199 | |
| 200 | EXPECT_TRUE(db_->ExecuteScriptForTesting( |
| 201 | "CREATE TABLE data4(id);CREATE TABLE data5(id)")) |
| 202 | << "Extra statement without whitespace"; |
| 203 | EXPECT_TRUE(db_->ExecuteScriptForTesting( |
| 204 | "CREATE TABLE data6(id); CREATE TABLE data7(id)")) |
| 205 | << "Extra statement separated by whitespace"; |
| 206 | |
| 207 | EXPECT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data8(id);-- Comment")) |
| 208 | << "Comment without whitespace"; |
| 209 | EXPECT_TRUE( |
| 210 | db_->ExecuteScriptForTesting("CREATE TABLE data9(id); -- Comment")) |
| 211 | << "Comment sepatated by whitespace"; |
| 212 | } |
| 213 | |
| 214 | TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_MultipleValidLines) { |
| 215 | EXPECT_TRUE(db_->ExecuteScriptForTesting(R"( |
| 216 | CREATE TABLE data1(contents TEXT); |
| 217 | CREATE TABLE data2(contents TEXT); |
| 218 | CREATE TABLE data3(contents TEXT); |
| 219 | )")); |
| 220 | EXPECT_EQ(SQLITE_OK, db_->GetErrorCode()); |
| 221 | |
| 222 | // DoesColumnExist() is implemented directly on top of a SQLite call. The |
| 223 | // other schema functions use sql::Statement infrastructure to query the |
| 224 | // schema table. |
| 225 | EXPECT_TRUE(db_->DoesColumnExist("data1", "contents")); |
| 226 | EXPECT_TRUE(db_->DoesColumnExist("data2", "contents")); |
| 227 | EXPECT_TRUE(db_->DoesColumnExist("data3", "contents")); |
| 228 | } |
| 229 | |
| 230 | TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_StopsOnCompileError) { |
| 231 | EXPECT_FALSE(db_->ExecuteScriptForTesting(R"( |
| 232 | CREATE TABLE data1(contents TEXT); |
| 233 | CREATE TABLE data1(); |
| 234 | CREATE TABLE data3(contents TEXT); |
| 235 | )")); |
| 236 | EXPECT_EQ(SQLITE_ERROR, db_->GetErrorCode()); |
| 237 | |
| 238 | EXPECT_TRUE(db_->DoesColumnExist("data1", "contents")); |
| 239 | EXPECT_FALSE(db_->DoesColumnExist("data3", "contents")); |
| 240 | } |
| 241 | |
| 242 | TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_StopsOnStepError) { |
| 243 | EXPECT_FALSE(db_->ExecuteScriptForTesting(R"( |
| 244 | CREATE TABLE data1(contents TEXT UNIQUE); |
| 245 | INSERT INTO data1(contents) VALUES('value1'); |
| 246 | INSERT INTO data1(contents) VALUES('value1'); |
| 247 | CREATE TABLE data3(contents TEXT); |
| 248 | )")); |
| 249 | EXPECT_EQ(SQLITE_CONSTRAINT_UNIQUE, db_->GetErrorCode()); |
| 250 | |
| 251 | EXPECT_TRUE(db_->DoesColumnExist("data1", "contents")); |
| 252 | EXPECT_FALSE(db_->DoesColumnExist("data3", "contents")); |
| 253 | } |
| 254 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 255 | TEST_P(SQLDatabaseTest, CachedStatement) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 256 | StatementID id1 = SQL_FROM_HERE; |
| 257 | StatementID id2 = SQL_FROM_HERE; |
Victor Costan | 613b430 | 2018-11-20 05:32:43 | [diff] [blame] | 258 | static const char kId1Sql[] = "SELECT a FROM foo"; |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 259 | static const char kId2Sql[] = "SELECT b FROM foo"; |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 260 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 261 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
| 262 | ASSERT_TRUE(db_->Execute("INSERT INTO foo(a, b) VALUES (12, 13)")); |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 263 | |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 264 | sqlite3_stmt* raw_id1_statement; |
| 265 | sqlite3_stmt* raw_id2_statement; |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 266 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 267 | scoped_refptr<Database::StatementRef> ref_from_id1 = |
| 268 | db_->GetCachedStatement(id1, kId1Sql); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 269 | raw_id1_statement = ref_from_id1->stmt(); |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 270 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 271 | Statement from_id1(std::move(ref_from_id1)); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 272 | ASSERT_TRUE(from_id1.is_valid()); |
| 273 | ASSERT_TRUE(from_id1.Step()); |
| 274 | EXPECT_EQ(12, from_id1.ColumnInt(0)); |
| 275 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 276 | scoped_refptr<Database::StatementRef> ref_from_id2 = |
| 277 | db_->GetCachedStatement(id2, kId2Sql); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 278 | raw_id2_statement = ref_from_id2->stmt(); |
| 279 | EXPECT_NE(raw_id1_statement, raw_id2_statement); |
| 280 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 281 | Statement from_id2(std::move(ref_from_id2)); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 282 | ASSERT_TRUE(from_id2.is_valid()); |
| 283 | ASSERT_TRUE(from_id2.Step()); |
| 284 | EXPECT_EQ(13, from_id2.ColumnInt(0)); |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 285 | } |
| 286 | |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 287 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 288 | scoped_refptr<Database::StatementRef> ref_from_id1 = |
| 289 | db_->GetCachedStatement(id1, kId1Sql); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 290 | EXPECT_EQ(raw_id1_statement, ref_from_id1->stmt()) |
| 291 | << "statement was not cached"; |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 292 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 293 | Statement from_id1(std::move(ref_from_id1)); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 294 | ASSERT_TRUE(from_id1.is_valid()); |
| 295 | ASSERT_TRUE(from_id1.Step()) << "cached statement was not reset"; |
| 296 | EXPECT_EQ(12, from_id1.ColumnInt(0)); |
| 297 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 298 | scoped_refptr<Database::StatementRef> ref_from_id2 = |
| 299 | db_->GetCachedStatement(id2, kId2Sql); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 300 | EXPECT_EQ(raw_id2_statement, ref_from_id2->stmt()) |
| 301 | << "statement was not cached"; |
| 302 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 303 | Statement from_id2(std::move(ref_from_id2)); |
Victor Costan | 87cf8c7 | 2018-07-19 19:36:04 | [diff] [blame] | 304 | ASSERT_TRUE(from_id2.is_valid()); |
| 305 | ASSERT_TRUE(from_id2.Step()) << "cached statement was not reset"; |
| 306 | EXPECT_EQ(13, from_id2.ColumnInt(0)); |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 307 | } |
Victor Costan | 613b430 | 2018-11-20 05:32:43 | [diff] [blame] | 308 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 309 | EXPECT_DCHECK_DEATH(db_->GetCachedStatement(id1, kId2Sql)) |
Victor Costan | 613b430 | 2018-11-20 05:32:43 | [diff] [blame] | 310 | << "Using a different SQL with the same statement ID should DCHECK"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 311 | EXPECT_DCHECK_DEATH(db_->GetCachedStatement(id2, kId1Sql)) |
Victor Costan | 613b430 | 2018-11-20 05:32:43 | [diff] [blame] | 312 | << "Using a different SQL with the same statement ID should DCHECK"; |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 313 | } |
| 314 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 315 | TEST_P(SQLDatabaseTest, IsSQLValidTest) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 316 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
| 317 | ASSERT_TRUE(db_->IsSQLValid("SELECT a FROM foo")); |
| 318 | ASSERT_FALSE(db_->IsSQLValid("SELECT no_exist FROM foo")); |
shess@chromium.org | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 319 | } |
| 320 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 321 | TEST_P(SQLDatabaseTest, DoesTableExist) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 322 | EXPECT_FALSE(db_->DoesTableExist("foo")); |
| 323 | EXPECT_FALSE(db_->DoesTableExist("foo_index")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 324 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 325 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
| 326 | ASSERT_TRUE(db_->Execute("CREATE INDEX foo_index ON foo (a)")); |
| 327 | EXPECT_TRUE(db_->DoesTableExist("foo")); |
| 328 | EXPECT_FALSE(db_->DoesTableExist("foo_index")); |
Victor Costan | f85512e5 | 2019-04-10 20:51:36 | [diff] [blame] | 329 | |
| 330 | // DoesTableExist() is case-sensitive. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 331 | EXPECT_FALSE(db_->DoesTableExist("Foo")); |
| 332 | EXPECT_FALSE(db_->DoesTableExist("FOO")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 333 | } |
| 334 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 335 | TEST_P(SQLDatabaseTest, DoesIndexExist) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 336 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
| 337 | EXPECT_FALSE(db_->DoesIndexExist("foo")); |
| 338 | EXPECT_FALSE(db_->DoesIndexExist("foo_ubdex")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 339 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 340 | ASSERT_TRUE(db_->Execute("CREATE INDEX foo_index ON foo (a)")); |
| 341 | EXPECT_TRUE(db_->DoesIndexExist("foo_index")); |
| 342 | EXPECT_FALSE(db_->DoesIndexExist("foo")); |
Victor Costan | f85512e5 | 2019-04-10 20:51:36 | [diff] [blame] | 343 | |
| 344 | // DoesIndexExist() is case-sensitive. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 345 | EXPECT_FALSE(db_->DoesIndexExist("Foo_index")); |
| 346 | EXPECT_FALSE(db_->DoesIndexExist("Foo_Index")); |
| 347 | EXPECT_FALSE(db_->DoesIndexExist("FOO_INDEX")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 348 | } |
| 349 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 350 | TEST_P(SQLDatabaseTest, DoesViewExist) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 351 | EXPECT_FALSE(db_->DoesViewExist("voo")); |
| 352 | ASSERT_TRUE(db_->Execute("CREATE VIEW voo (a) AS SELECT 1")); |
| 353 | EXPECT_FALSE(db_->DoesIndexExist("voo")); |
| 354 | EXPECT_FALSE(db_->DoesTableExist("voo")); |
| 355 | EXPECT_TRUE(db_->DoesViewExist("voo")); |
Victor Costan | f85512e5 | 2019-04-10 20:51:36 | [diff] [blame] | 356 | |
| 357 | // DoesTableExist() is case-sensitive. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 358 | EXPECT_FALSE(db_->DoesViewExist("Voo")); |
| 359 | EXPECT_FALSE(db_->DoesViewExist("VOO")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 360 | } |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 361 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 362 | TEST_P(SQLDatabaseTest, DoesColumnExist) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 363 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
Victor Costan | 70bedf2 | 2018-07-18 21:21:14 | [diff] [blame] | 364 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 365 | EXPECT_FALSE(db_->DoesColumnExist("foo", "bar")); |
| 366 | EXPECT_TRUE(db_->DoesColumnExist("foo", "a")); |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 367 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 368 | ASSERT_FALSE(db_->DoesTableExist("bar")); |
| 369 | EXPECT_FALSE(db_->DoesColumnExist("bar", "b")); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 370 | |
Victor Costan | f85512e5 | 2019-04-10 20:51:36 | [diff] [blame] | 371 | // SQLite resolves table/column names without case sensitivity. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 372 | EXPECT_TRUE(db_->DoesColumnExist("FOO", "A")); |
| 373 | EXPECT_TRUE(db_->DoesColumnExist("FOO", "a")); |
| 374 | EXPECT_TRUE(db_->DoesColumnExist("foo", "A")); |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 375 | } |
| 376 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 377 | TEST_P(SQLDatabaseTest, GetLastInsertRowId) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 378 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)")); |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 379 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 380 | ASSERT_TRUE(db_->Execute("INSERT INTO foo (value) VALUES (12)")); |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 381 | |
| 382 | // Last insert row ID should be valid. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 383 | int64_t row = db_->GetLastInsertRowId(); |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 384 | EXPECT_LT(0, row); |
| 385 | |
| 386 | // It should be the primary key of the row we just inserted. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 387 | Statement s(db_->GetUniqueStatement("SELECT value FROM foo WHERE id=?")); |
brettw@chromium.org | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 388 | s.BindInt64(0, row); |
| 389 | ASSERT_TRUE(s.Step()); |
| 390 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 391 | } |
michaelbai@chromium.org | 44ad7d90 | 2012-03-23 00:09:05 | [diff] [blame] | 392 | |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 393 | // Test the scoped error expecter by attempting to insert a duplicate |
shess@chromium.org | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 394 | // value into an index. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 395 | TEST_P(SQLDatabaseTest, ScopedErrorExpecter) { |
shess@chromium.org | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 396 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 397 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 398 | ASSERT_TRUE(db_->Execute("INSERT INTO foo (id) VALUES (12)")); |
shess@chromium.org | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 399 | |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 400 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 401 | sql::test::ScopedErrorExpecter expecter; |
| 402 | expecter.ExpectError(SQLITE_CONSTRAINT); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 403 | ASSERT_FALSE(db_->Execute("INSERT INTO foo (id) VALUES (12)")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 404 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Victor Costan | 106e500 | 2021-07-17 00:04:49 | [diff] [blame] | 408 | TEST_P(SQLDatabaseTest, SchemaIntrospectionUsesErrorExpecter) { |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 409 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 410 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 411 | ASSERT_FALSE(db_->DoesTableExist("bar")); |
| 412 | ASSERT_TRUE(db_->DoesTableExist("foo")); |
| 413 | ASSERT_TRUE(db_->DoesColumnExist("foo", "id")); |
| 414 | db_->Close(); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 415 | |
| 416 | // Corrupt the database so that nothing works, including PRAGMAs. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 417 | ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_)); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 418 | |
| 419 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 420 | sql::test::ScopedErrorExpecter expecter; |
| 421 | expecter.ExpectError(SQLITE_CORRUPT); |
Dan McArdle | 4feabeb5 | 2024-01-03 15:17:13 | [diff] [blame] | 422 | ASSERT_FALSE(db_->Open(db_path_)); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 423 | ASSERT_FALSE(db_->DoesTableExist("bar")); |
| 424 | ASSERT_FALSE(db_->DoesTableExist("foo")); |
| 425 | ASSERT_FALSE(db_->DoesColumnExist("foo", "id")); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 426 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
shess | 92a2ab1 | 2015-04-09 01:59:47 | [diff] [blame] | 427 | } |
shess@chromium.org | 4350e32 | 2013-06-18 22:18:10 | [diff] [blame] | 428 | } |
| 429 | |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 430 | TEST_P(SQLDatabaseTest, SetErrorCallback) { |
| 431 | static constexpr char kCreateSql[] = |
| 432 | "CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 433 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 434 | ASSERT_TRUE(db_->Execute("INSERT INTO rows(id) VALUES(12)")); |
shess@chromium.org | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 435 | |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 436 | bool error_callback_called = false; |
shess@chromium.org | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 437 | int error = SQLITE_OK; |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 438 | db_->set_error_callback(base::BindLambdaForTesting( |
| 439 | [&](int sqlite_error, sql::Statement* statement) { |
| 440 | error_callback_called = true; |
| 441 | error = sqlite_error; |
| 442 | })); |
| 443 | EXPECT_FALSE(db_->Execute("INSERT INTO rows(id) VALUES(12)")) |
| 444 | << "Inserting a duplicate primary key should have failed"; |
| 445 | EXPECT_TRUE(error_callback_called) |
| 446 | << "Execute() should report errors to the database error callback"; |
| 447 | EXPECT_EQ(SQLITE_CONSTRAINT_PRIMARYKEY, error) |
| 448 | << "Execute() should report errors to the database error callback"; |
| 449 | } |
Dominique Fauteux-Chapleau | 2896365 | 2022-03-08 19:27:30 | [diff] [blame] | 450 | |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 451 | TEST_P(SQLDatabaseTest, SetErrorCallbackDchecksOnExistingCallback) { |
| 452 | db_->set_error_callback(base::DoNothing()); |
| 453 | EXPECT_DCHECK_DEATH(db_->set_error_callback(base::DoNothing())) |
| 454 | << "set_error_callback() should DCHECK if error callback already exists"; |
| 455 | } |
Dominique Fauteux-Chapleau | 2896365 | 2022-03-08 19:27:30 | [diff] [blame] | 456 | |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 457 | TEST_P(SQLDatabaseTest, ResetErrorCallback) { |
| 458 | static constexpr char kCreateSql[] = |
| 459 | "CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)"; |
| 460 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 461 | ASSERT_TRUE(db_->Execute("INSERT INTO rows(id) VALUES(12)")); |
| 462 | |
| 463 | bool error_callback_called = false; |
| 464 | int error = SQLITE_OK; |
| 465 | db_->set_error_callback( |
| 466 | base::BindLambdaForTesting([&](int sqlite_error, Statement* statement) { |
| 467 | error_callback_called = true; |
| 468 | error = sqlite_error; |
| 469 | })); |
| 470 | db_->reset_error_callback(); |
| 471 | |
Dominique Fauteux-Chapleau | 2896365 | 2022-03-08 19:27:30 | [diff] [blame] | 472 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 473 | sql::test::ScopedErrorExpecter expecter; |
| 474 | expecter.ExpectError(SQLITE_CONSTRAINT); |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 475 | EXPECT_FALSE(db_->Execute("INSERT INTO rows(id) VALUES(12)")) |
| 476 | << "Inserting a duplicate primary key should have failed"; |
| 477 | EXPECT_TRUE(expecter.SawExpectedErrors()) |
| 478 | << "Inserting a duplicate primary key should have failed"; |
| 479 | } |
| 480 | EXPECT_FALSE(error_callback_called) |
| 481 | << "Execute() should not report errors after reset_error_callback()"; |
| 482 | EXPECT_EQ(SQLITE_OK, error) |
| 483 | << "Execute() should not report errors after reset_error_callback()"; |
| 484 | } |
| 485 | |
Dan McArdle | 643e874 | 2024-01-31 18:52:43 | [diff] [blame] | 486 | // Regression test for https://crbug.com/1522873 |
| 487 | TEST_P(SQLDatabaseTest, ErrorCallbackThatClosesDb) { |
| 488 | for (const bool reopen_db : {false, true}) { |
| 489 | SCOPED_TRACE(::testing::Message() << "reopen_db: " << reopen_db); |
| 490 | // Ensure that `db_` is fresh in this iteration. |
| 491 | CreateFreshDB(); |
| 492 | static constexpr char kCreateSql[] = |
| 493 | "CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)"; |
| 494 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 495 | ASSERT_TRUE(db_->Execute("INSERT INTO rows(id) VALUES(12)")); |
| 496 | |
| 497 | bool error_callback_called = false; |
| 498 | int error = SQLITE_OK; |
| 499 | db_->set_error_callback( |
| 500 | base::BindLambdaForTesting([&](int sqlite_error, Statement* statement) { |
| 501 | error_callback_called = true; |
| 502 | error = sqlite_error; |
| 503 | db_->Close(); |
| 504 | if (reopen_db) { |
| 505 | ASSERT_TRUE(db_->Open(db_path_)); |
| 506 | } |
| 507 | })); |
| 508 | |
| 509 | { |
| 510 | sql::test::ScopedErrorExpecter expecter; |
| 511 | expecter.ExpectError(SQLITE_CONSTRAINT); |
| 512 | EXPECT_FALSE(db_->Execute("INSERT INTO rows(id) VALUES(12)")) |
| 513 | << "Inserting a duplicate primary key should have failed"; |
| 514 | EXPECT_TRUE(expecter.SawExpectedErrors()) |
| 515 | << "Inserting a duplicate primary key should have failed"; |
| 516 | } |
| 517 | EXPECT_TRUE(error_callback_called); |
| 518 | EXPECT_EQ(SQLITE_CONSTRAINT_PRIMARYKEY, error); |
| 519 | EXPECT_EQ(db_->is_open(), reopen_db); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | // Regression test for https://crbug.com/1522873 |
| 524 | TEST_P(SQLDatabaseTest, ErrorCallbackThatFreesDatabase) { |
| 525 | static constexpr char kCreateSql[] = |
| 526 | "CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)"; |
| 527 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 528 | ASSERT_TRUE(db_->Execute("INSERT INTO rows(id) VALUES(12)")); |
| 529 | |
| 530 | bool error_callback_called = false; |
| 531 | int error = SQLITE_OK; |
| 532 | db_->set_error_callback( |
| 533 | base::BindLambdaForTesting([&](int sqlite_error, Statement* statement) { |
| 534 | error_callback_called = true; |
| 535 | error = sqlite_error; |
| 536 | db_.reset(); |
| 537 | })); |
| 538 | |
| 539 | { |
| 540 | sql::test::ScopedErrorExpecter expecter; |
| 541 | expecter.ExpectError(SQLITE_CONSTRAINT); |
| 542 | EXPECT_FALSE(db_->Execute("INSERT INTO rows(id) VALUES(12)")) |
| 543 | << "Inserting a duplicate primary key should have failed"; |
| 544 | EXPECT_TRUE(expecter.SawExpectedErrors()) |
| 545 | << "Inserting a duplicate primary key should have failed"; |
| 546 | } |
| 547 | EXPECT_TRUE(error_callback_called); |
| 548 | EXPECT_EQ(SQLITE_CONSTRAINT_PRIMARYKEY, error); |
| 549 | } |
| 550 | |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 551 | // Sets a flag to true/false to track being alive. |
| 552 | class LifeTracker { |
| 553 | public: |
| 554 | explicit LifeTracker(bool* flag_ptr) : flag_ptr_(flag_ptr) { |
| 555 | DCHECK(flag_ptr != nullptr); |
| 556 | DCHECK(!*flag_ptr) |
| 557 | << "LifeTracker's flag should be set to false prior to construction"; |
| 558 | *flag_ptr_ = true; |
shess@chromium.org | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 559 | } |
| 560 | |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 561 | LifeTracker(LifeTracker&& rhs) { |
| 562 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 563 | DCHECK_CALLED_ON_VALID_SEQUENCE(rhs.sequence_checker_); |
| 564 | flag_ptr_ = rhs.flag_ptr_; |
| 565 | rhs.flag_ptr_ = nullptr; |
shess@chromium.org | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 566 | } |
| 567 | |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 568 | // base::RepeatingCallback only requires move-construction support. |
| 569 | LifeTracker& operator=(const LifeTracker& rhs) = delete; |
shess@chromium.org | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 570 | |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 571 | ~LifeTracker() { |
| 572 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 573 | if (flag_ptr_) |
| 574 | *flag_ptr_ = false; |
shess@chromium.org | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 575 | } |
Victor Costan | d09770c | 2022-03-08 23:14:46 | [diff] [blame] | 576 | |
| 577 | private: |
| 578 | SEQUENCE_CHECKER(sequence_checker_); |
| 579 | raw_ptr<bool> flag_ptr_ GUARDED_BY_CONTEXT(sequence_checker_); |
| 580 | }; |
| 581 | |
| 582 | // base::BindRepeating() can curry arguments to be passed by const reference to |
| 583 | // the callback function. If the error callback function calls |
| 584 | // reset_error_callback() and the Database doesn't hang onto the callback while |
| 585 | // running it, the storage for those arguments may be deleted while the callback |
| 586 | // function is executing. This test ensures that the database is hanging onto |
| 587 | // the callback while running it. |
| 588 | TEST_P(SQLDatabaseTest, ErrorCallbackStorageProtectedWhileRun) { |
| 589 | bool is_alive = false; |
| 590 | db_->set_error_callback(base::BindRepeating( |
| 591 | [](Database* db, bool* life_tracker_is_alive, |
| 592 | const LifeTracker& life_tracker, int sqlite_error, |
| 593 | Statement* statement) { |
| 594 | EXPECT_TRUE(*life_tracker_is_alive) |
| 595 | << "The error callback storage should be alive when it is Run()"; |
| 596 | db->reset_error_callback(); |
| 597 | EXPECT_TRUE(*life_tracker_is_alive) |
| 598 | << "The error storage should remain alive during Run() after " |
| 599 | << "reset_error_callback()"; |
| 600 | }, |
| 601 | base::Unretained(db_.get()), base::Unretained(&is_alive), |
| 602 | LifeTracker(&is_alive))); |
| 603 | |
| 604 | EXPECT_TRUE(is_alive) |
| 605 | << "The error callback storage should be alive after creation"; |
| 606 | EXPECT_FALSE(db_->Execute("INSERT INTO rows(id) VALUES(12)")); |
| 607 | EXPECT_FALSE(is_alive) |
| 608 | << "The error callback storage should be released after Run() completes"; |
shess@chromium.org | 98cf300 | 2013-07-12 01:38:56 | [diff] [blame] | 609 | } |
| 610 | |
Victor Costan | c02b7af | 2021-07-09 13:26:45 | [diff] [blame] | 611 | TEST_P(SQLDatabaseTest, Execute_CompilationError) { |
| 612 | bool error_callback_called = false; |
| 613 | db_->set_error_callback(base::BindLambdaForTesting([&](int error, |
| 614 | sql::Statement* |
| 615 | statement) { |
| 616 | EXPECT_EQ(SQLITE_ERROR, error); |
| 617 | EXPECT_EQ(nullptr, statement); |
| 618 | EXPECT_FALSE(error_callback_called) |
| 619 | << "SQL compilation errors should call the error callback exactly once"; |
| 620 | error_callback_called = true; |
| 621 | })); |
| 622 | |
| 623 | { |
| 624 | sql::test::ScopedErrorExpecter expecter; |
| 625 | expecter.ExpectError(SQLITE_ERROR); |
| 626 | EXPECT_FALSE(db_->Execute("SELECT missing_column FROM missing_table")); |
| 627 | EXPECT_TRUE(expecter.SawExpectedErrors()); |
| 628 | } |
| 629 | |
| 630 | EXPECT_TRUE(error_callback_called) |
| 631 | << "SQL compilation errors should call the error callback"; |
| 632 | } |
| 633 | |
| 634 | TEST_P(SQLDatabaseTest, GetUniqueStatement_CompilationError) { |
| 635 | bool error_callback_called = false; |
| 636 | db_->set_error_callback(base::BindLambdaForTesting([&](int error, |
| 637 | sql::Statement* |
| 638 | statement) { |
| 639 | EXPECT_EQ(SQLITE_ERROR, error); |
| 640 | EXPECT_EQ(nullptr, statement); |
| 641 | EXPECT_FALSE(error_callback_called) |
| 642 | << "SQL compilation errors should call the error callback exactly once"; |
| 643 | error_callback_called = true; |
| 644 | })); |
| 645 | |
| 646 | { |
| 647 | sql::test::ScopedErrorExpecter expecter; |
| 648 | expecter.ExpectError(SQLITE_ERROR); |
| 649 | sql::Statement statement( |
| 650 | db_->GetUniqueStatement("SELECT missing_column FROM missing_table")); |
| 651 | EXPECT_FALSE(statement.is_valid()); |
| 652 | EXPECT_TRUE(expecter.SawExpectedErrors()); |
| 653 | } |
| 654 | |
| 655 | EXPECT_TRUE(error_callback_called) |
| 656 | << "SQL compilation errors should call the error callback"; |
| 657 | } |
| 658 | |
| 659 | TEST_P(SQLDatabaseTest, GetCachedStatement_CompilationError) { |
| 660 | bool error_callback_called = false; |
| 661 | db_->set_error_callback(base::BindLambdaForTesting([&](int error, |
| 662 | sql::Statement* |
| 663 | statement) { |
| 664 | EXPECT_EQ(SQLITE_ERROR, error); |
| 665 | EXPECT_EQ(nullptr, statement); |
| 666 | EXPECT_FALSE(error_callback_called) |
| 667 | << "SQL compilation errors should call the error callback exactly once"; |
| 668 | error_callback_called = true; |
| 669 | })); |
| 670 | |
| 671 | { |
| 672 | sql::test::ScopedErrorExpecter expecter; |
| 673 | expecter.ExpectError(SQLITE_ERROR); |
| 674 | sql::Statement statement(db_->GetCachedStatement( |
| 675 | SQL_FROM_HERE, "SELECT missing_column FROM missing_table")); |
| 676 | EXPECT_FALSE(statement.is_valid()); |
| 677 | EXPECT_TRUE(expecter.SawExpectedErrors()); |
| 678 | } |
| 679 | |
| 680 | EXPECT_TRUE(error_callback_called) |
| 681 | << "SQL compilation errors should call the error callback"; |
| 682 | } |
| 683 | |
Victor Costan | 5530932 | 2021-07-19 17:58:42 | [diff] [blame] | 684 | TEST_P(SQLDatabaseTest, GetUniqueStatement_ExtraContents) { |
| 685 | sql::Statement minimal(db_->GetUniqueStatement("SELECT 1")); |
| 686 | sql::Statement extra_semicolon(db_->GetUniqueStatement("SELECT 1;")); |
| 687 | |
| 688 | // It would be nice to flag trailing comments too, as they cost binary size. |
| 689 | // However, there's no easy way of doing that. |
| 690 | sql::Statement trailing_comment( |
| 691 | db_->GetUniqueStatement("SELECT 1 -- Comment")); |
| 692 | |
| 693 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("SELECT 1;SELECT 2")) |
| 694 | << "Extra statement without whitespace"; |
| 695 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("SELECT 1; SELECT 2")) |
| 696 | << "Extra statement separated by whitespace"; |
| 697 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("SELECT 1;-- Comment")) |
| 698 | << "Comment without whitespace"; |
| 699 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("SELECT 1; -- Comment")) |
| 700 | << "Comment separated by whitespace"; |
| 701 | } |
| 702 | |
| 703 | TEST_P(SQLDatabaseTest, GetCachedStatement_ExtraContents) { |
| 704 | sql::Statement minimal(db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1")); |
| 705 | sql::Statement extra_semicolon( |
| 706 | db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1;")); |
| 707 | |
| 708 | // It would be nice to flag trailing comments too, as they cost binary size. |
| 709 | // However, there's no easy way of doing that. |
| 710 | sql::Statement trailing_comment( |
| 711 | db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1 -- Comment")); |
| 712 | |
| 713 | EXPECT_DCHECK_DEATH( |
| 714 | db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1;SELECT 2")) |
| 715 | << "Extra statement without whitespace"; |
| 716 | EXPECT_DCHECK_DEATH( |
| 717 | db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1; SELECT 2")) |
| 718 | << "Extra statement separated by whitespace"; |
| 719 | EXPECT_DCHECK_DEATH( |
| 720 | db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1;-- Comment")) |
| 721 | << "Comment without whitespace"; |
| 722 | EXPECT_DCHECK_DEATH( |
| 723 | db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1; -- Comment")) |
| 724 | << "Comment separated by whitespace"; |
| 725 | } |
| 726 | |
| 727 | TEST_P(SQLDatabaseTest, IsSQLValid_ExtraContents) { |
| 728 | EXPECT_TRUE(db_->IsSQLValid("SELECT 1")); |
| 729 | EXPECT_TRUE(db_->IsSQLValid("SELECT 1;")) |
| 730 | << "Trailing semicolons are currently tolerated"; |
| 731 | |
| 732 | // It would be nice to flag trailing comments too, as they cost binary size. |
| 733 | // However, there's no easy way of doing that. |
| 734 | EXPECT_TRUE(db_->IsSQLValid("SELECT 1 -- Comment")) |
| 735 | << "Trailing comments are currently tolerated"; |
| 736 | |
| 737 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("SELECT 1;SELECT 2")) |
| 738 | << "Extra statement without whitespace"; |
| 739 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("SELECT 1; SELECT 2")) |
| 740 | << "Extra statement separated by whitespace"; |
| 741 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("SELECT 1;-- Comment")) |
| 742 | << "Comment without whitespace"; |
| 743 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("SELECT 1; -- Comment")) |
| 744 | << "Comment separated by whitespace"; |
| 745 | } |
| 746 | |
Victor Costan | 18f9e7a | 2021-07-22 06:34:17 | [diff] [blame] | 747 | TEST_P(SQLDatabaseTest, GetUniqueStatement_NoContents) { |
| 748 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("")) << "Empty string"; |
| 749 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement(" ")) << "Space"; |
| 750 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("\n")) << "Newline"; |
| 751 | EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("-- Comment")) << "Comment"; |
| 752 | } |
| 753 | |
| 754 | TEST_P(SQLDatabaseTest, GetCachedStatement_NoContents) { |
| 755 | EXPECT_DCHECK_DEATH(db_->GetCachedStatement(SQL_FROM_HERE, "")) |
| 756 | << "Empty string"; |
| 757 | EXPECT_DCHECK_DEATH(db_->GetCachedStatement(SQL_FROM_HERE, " ")) << "Space"; |
| 758 | EXPECT_DCHECK_DEATH(db_->GetCachedStatement(SQL_FROM_HERE, "\n")) |
| 759 | << "Newline"; |
| 760 | EXPECT_DCHECK_DEATH(db_->GetCachedStatement(SQL_FROM_HERE, "-- Comment")) |
| 761 | << "Comment"; |
| 762 | } |
| 763 | |
ssid | 583f55e | 2022-04-21 19:59:00 | [diff] [blame] | 764 | TEST_P(SQLDatabaseTest, GetReadonlyStatement) { |
| 765 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 766 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 767 | ASSERT_TRUE(db_->Execute("INSERT INTO foo (value) VALUES (12)")); |
| 768 | |
| 769 | // PRAGMA statements do not change the database file. |
| 770 | { |
| 771 | Statement s(db_->GetReadonlyStatement("PRAGMA analysis_limit")); |
| 772 | ASSERT_TRUE(s.Step()); |
| 773 | } |
| 774 | { |
| 775 | Statement s(db_->GetReadonlyStatement("PRAGMA analysis_limit=100")); |
| 776 | ASSERT_TRUE(s.is_valid()); |
| 777 | } |
| 778 | |
| 779 | // Create and insert statements should fail, while the same queries as unique |
| 780 | // statement succeeds. |
| 781 | { |
| 782 | Statement s(db_->GetReadonlyStatement( |
| 783 | "CREATE TABLE IF NOT EXISTS foo (id INTEGER PRIMARY KEY, value)")); |
| 784 | ASSERT_FALSE(s.is_valid()); |
| 785 | Statement s1(db_->GetUniqueStatement( |
| 786 | "CREATE TABLE IF NOT EXISTS foo (id INTEGER PRIMARY KEY, value)")); |
| 787 | ASSERT_TRUE(s1.is_valid()); |
| 788 | } |
| 789 | { |
| 790 | Statement s( |
| 791 | db_->GetReadonlyStatement("INSERT INTO foo (value) VALUES (12)")); |
| 792 | ASSERT_FALSE(s.is_valid()); |
| 793 | Statement s1( |
| 794 | db_->GetUniqueStatement("INSERT INTO foo (value) VALUES (12)")); |
| 795 | ASSERT_TRUE(s1.is_valid()); |
| 796 | } |
| 797 | { |
| 798 | Statement s( |
| 799 | db_->GetReadonlyStatement("CREATE VIRTUAL TABLE bar USING module")); |
| 800 | ASSERT_FALSE(s.is_valid()); |
| 801 | Statement s1( |
| 802 | db_->GetUniqueStatement("CREATE VIRTUAL TABLE bar USING module")); |
| 803 | ASSERT_TRUE(s1.is_valid()); |
| 804 | } |
| 805 | |
| 806 | // Select statement is successful. |
| 807 | { |
| 808 | Statement s(db_->GetReadonlyStatement("SELECT * FROM foo")); |
| 809 | ASSERT_TRUE(s.Step()); |
| 810 | EXPECT_EQ(s.ColumnInt(1), 12); |
| 811 | } |
| 812 | } |
| 813 | |
Victor Costan | 18f9e7a | 2021-07-22 06:34:17 | [diff] [blame] | 814 | TEST_P(SQLDatabaseTest, IsSQLValid_NoContents) { |
| 815 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("")) << "Empty string"; |
| 816 | EXPECT_DCHECK_DEATH(db_->IsSQLValid(" ")) << "Space"; |
| 817 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("\n")) << "Newline"; |
| 818 | EXPECT_DCHECK_DEATH(db_->IsSQLValid("-- Comment")) << "Comment"; |
| 819 | } |
| 820 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 821 | // Test that Database::Raze() results in a database without the |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 822 | // tables from the original database. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 823 | TEST_P(SQLDatabaseTest, Raze) { |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 824 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 825 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
| 826 | ASSERT_TRUE(db_->Execute("INSERT INTO foo (value) VALUES (12)")); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 827 | |
yfriedman@chromium.org | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 828 | int pragma_auto_vacuum = 0; |
| 829 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 830 | Statement s(db_->GetUniqueStatement("PRAGMA auto_vacuum")); |
yfriedman@chromium.org | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 831 | ASSERT_TRUE(s.Step()); |
| 832 | pragma_auto_vacuum = s.ColumnInt(0); |
| 833 | ASSERT_TRUE(pragma_auto_vacuum == 0 || pragma_auto_vacuum == 1); |
| 834 | } |
| 835 | |
| 836 | // If auto_vacuum is set, there's an extra page to maintain a freelist. |
| 837 | const int kExpectedPageCount = 2 + pragma_auto_vacuum; |
| 838 | |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 839 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 840 | Statement s(db_->GetUniqueStatement("PRAGMA page_count")); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 841 | ASSERT_TRUE(s.Step()); |
yfriedman@chromium.org | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 842 | EXPECT_EQ(kExpectedPageCount, s.ColumnInt(0)); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | { |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 846 | Statement s(db_->GetUniqueStatement("SELECT * FROM sqlite_schema")); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 847 | ASSERT_TRUE(s.Step()); |
| 848 | EXPECT_EQ("table", s.ColumnString(0)); |
| 849 | EXPECT_EQ("foo", s.ColumnString(1)); |
| 850 | EXPECT_EQ("foo", s.ColumnString(2)); |
yfriedman@chromium.org | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 851 | // Table "foo" is stored in the last page of the file. |
| 852 | EXPECT_EQ(kExpectedPageCount, s.ColumnInt(3)); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 853 | EXPECT_EQ(kCreateSql, s.ColumnString(4)); |
| 854 | } |
| 855 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 856 | ASSERT_TRUE(db_->Raze()); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 857 | |
| 858 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 859 | Statement s(db_->GetUniqueStatement("PRAGMA page_count")); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 860 | ASSERT_TRUE(s.Step()); |
| 861 | EXPECT_EQ(1, s.ColumnInt(0)); |
| 862 | } |
| 863 | |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 864 | ASSERT_EQ(0, SqliteSchemaCount(db_.get())); |
yfriedman@chromium.org | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 865 | |
| 866 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 867 | Statement s(db_->GetUniqueStatement("PRAGMA auto_vacuum")); |
yfriedman@chromium.org | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 868 | ASSERT_TRUE(s.Step()); |
shess@chromium.org | 6d42f15 | 2012-11-10 00:38:24 | [diff] [blame] | 869 | // The new database has the same auto_vacuum as a fresh database. |
yfriedman@chromium.org | 69c5845 | 2012-08-06 19:22:42 | [diff] [blame] | 870 | EXPECT_EQ(pragma_auto_vacuum, s.ColumnInt(0)); |
| 871 | } |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 872 | } |
| 873 | |
Victor Costan | cdef384 | 2022-03-28 18:51:08 | [diff] [blame] | 874 | TEST_P(SQLDatabaseTest, RazeDuringSelect) { |
| 875 | ASSERT_TRUE( |
| 876 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")); |
| 877 | ASSERT_TRUE(db_->Execute("INSERT INTO rows(id) VALUES(1)")); |
| 878 | ASSERT_TRUE(db_->Execute("INSERT INTO rows(id) VALUES(2)")); |
| 879 | |
| 880 | { |
| 881 | // SELECT implicitly creates a transaction while it's executing. This |
| 882 | // implicit transaction will not be caught by Raze()'s checks. |
| 883 | Statement select(db_->GetUniqueStatement("SELECT id FROM rows")); |
| 884 | ASSERT_TRUE(select.Step()); |
| 885 | EXPECT_FALSE(db_->Raze()) << "Raze() should fail while SELECT is executing"; |
| 886 | } |
| 887 | |
| 888 | { |
| 889 | Statement count(db_->GetUniqueStatement("SELECT COUNT(*) FROM rows")); |
| 890 | ASSERT_TRUE(count.Step()); |
| 891 | EXPECT_EQ(2, count.ColumnInt(0)) << "Raze() deleted some data"; |
| 892 | } |
| 893 | } |
| 894 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 895 | // Helper for SQLDatabaseTest.RazePageSize. Creates a fresh db based on |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 896 | // db_prefix, with the given initial page size, and verifies it against the |
| 897 | // expected size. Then changes to the final page size and razes, verifying that |
| 898 | // the fresh database ends up with the expected final page size. |
| 899 | void TestPageSize(const base::FilePath& db_prefix, |
| 900 | int initial_page_size, |
| 901 | const std::string& expected_initial_page_size, |
| 902 | int final_page_size, |
| 903 | const std::string& expected_final_page_size) { |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 904 | static const char kCreateSql[] = "CREATE TABLE x (t TEXT)"; |
| 905 | static const char kInsertSql1[] = "INSERT INTO x VALUES ('This is a test')"; |
| 906 | static const char kInsertSql2[] = "INSERT INTO x VALUES ('That was a test')"; |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 907 | |
| 908 | const base::FilePath db_path = db_prefix.InsertBeforeExtensionASCII( |
Raul Tambre | 6c708e3 | 2019-02-08 22:35:14 | [diff] [blame] | 909 | base::NumberToString(initial_page_size)); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 910 | Database::Delete(db_path); |
| 911 | Database db({.page_size = initial_page_size}); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 912 | ASSERT_TRUE(db.Open(db_path)); |
| 913 | ASSERT_TRUE(db.Execute(kCreateSql)); |
| 914 | ASSERT_TRUE(db.Execute(kInsertSql1)); |
| 915 | ASSERT_TRUE(db.Execute(kInsertSql2)); |
| 916 | ASSERT_EQ(expected_initial_page_size, |
| 917 | ExecuteWithResult(&db, "PRAGMA page_size")); |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 918 | db.Close(); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 919 | |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 920 | // 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] | 921 | Database razed_db({.page_size = final_page_size}); |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 922 | ASSERT_TRUE(razed_db.Open(db_path)); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 923 | // Raze will use the page size set in the connection object, which may not |
| 924 | // match the file's page size. |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 925 | ASSERT_TRUE(razed_db.Raze()); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 926 | |
| 927 | // SQLite 3.10.2 (at least) has a quirk with the sqlite3_backup() API (used by |
| 928 | // Raze()) which causes the destination database to remember the previous |
| 929 | // page_size, even if the overwriting database changed the page_size. Access |
| 930 | // the actual database to cause the cached value to be updated. |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 931 | EXPECT_EQ("0", |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 932 | ExecuteWithResult(&razed_db, "SELECT COUNT(*) FROM sqlite_schema")); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 933 | |
| 934 | EXPECT_EQ(expected_final_page_size, |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 935 | ExecuteWithResult(&razed_db, "PRAGMA page_size")); |
| 936 | EXPECT_EQ("1", ExecuteWithResult(&razed_db, "PRAGMA page_count")); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 937 | } |
| 938 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 939 | // Verify that Recovery maintains the page size, and the virtual table |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 940 | // works with page sizes other than SQLite's default. Also verify the case |
| 941 | // where the default page size has changed. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 942 | TEST_P(SQLDatabaseTest, RazePageSize) { |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 943 | const std::string default_page_size = |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 944 | ExecuteWithResult(db_.get(), "PRAGMA page_size"); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 945 | |
Victor Costan | 7f6abbbe | 2018-07-29 02:57:27 | [diff] [blame] | 946 | // Sync uses 32k pages. |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 947 | EXPECT_NO_FATAL_FAILURE( |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 948 | TestPageSize(db_path_, 32768, "32768", 32768, "32768")); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 949 | |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 950 | // 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] | 951 | EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path_, 4096, "4096", 4096, "4096")); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 952 | |
| 953 | // 1k is the default page size before 3.12.0. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 954 | EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path_, 1024, "1024", 1024, "1024")); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 955 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 956 | EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path_, 2048, "2048", 4096, "4096")); |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 957 | |
Victor Costan | 7f6abbbe | 2018-07-29 02:57:27 | [diff] [blame] | 958 | // Databases with no page size specified should result in the default |
shess | 7e2baba | 2016-10-27 23:46:05 | [diff] [blame] | 959 | // page size. 2k has never been the default page size. |
| 960 | ASSERT_NE("2048", default_page_size); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 961 | EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path_, 2048, "2048", |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 962 | DatabaseOptions::kDefaultPageSize, |
| 963 | default_page_size)); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | // Test that Raze() results are seen in other connections. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 967 | TEST_P(SQLDatabaseTest, RazeMultiple) { |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 968 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 969 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 970 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 971 | Database other_db(GetDBOptions()); |
| 972 | ASSERT_TRUE(other_db.Open(db_path_)); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 973 | |
| 974 | // Check that the second connection sees the table. |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 975 | ASSERT_EQ(1, SqliteSchemaCount(&other_db)); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 976 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 977 | ASSERT_TRUE(db_->Raze()); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 978 | |
| 979 | // The second connection sees the updated database. |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 980 | ASSERT_EQ(0, SqliteSchemaCount(&other_db)); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 981 | } |
| 982 | |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 983 | TEST_P(SQLDatabaseTest, Raze_OtherConnectionHasWriteLock) { |
| 984 | ASSERT_TRUE(db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY)")); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 985 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 986 | Database other_db(GetDBOptions()); |
| 987 | ASSERT_TRUE(other_db.Open(db_path_)); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 988 | |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 989 | Transaction other_db_transaction(&other_db); |
| 990 | ASSERT_TRUE(other_db_transaction.Begin()); |
| 991 | ASSERT_TRUE(other_db.Execute("INSERT INTO rows(id) VALUES(1)")); |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 992 | |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 993 | EXPECT_FALSE(db_->Raze()) |
| 994 | << "Raze() should fail while another connection has a write lock"; |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 995 | |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 996 | ASSERT_TRUE(other_db_transaction.Commit()); |
| 997 | EXPECT_TRUE(db_->Raze()) |
| 998 | << "Raze() should succeed after the other connection releases the lock"; |
shess@chromium.org | 8e0c0128 | 2012-04-06 19:36:49 | [diff] [blame] | 999 | } |
| 1000 | |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1001 | TEST_P(SQLDatabaseTest, Raze_OtherConnectionHasReadLock) { |
| 1002 | ASSERT_TRUE(db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY)")); |
| 1003 | ASSERT_TRUE(db_->Execute("INSERT INTO rows(id) VALUES(1)")); |
| 1004 | |
| 1005 | if (IsWALEnabled()) { |
| 1006 | // In WAL mode, read transactions in other connections do not block a write |
| 1007 | // transaction. |
| 1008 | return; |
| 1009 | } |
| 1010 | |
| 1011 | Database other_db(GetDBOptions()); |
| 1012 | ASSERT_TRUE(other_db.Open(db_path_)); |
| 1013 | |
| 1014 | Statement select(other_db.GetUniqueStatement("SELECT id FROM rows")); |
| 1015 | ASSERT_TRUE(select.Step()); |
| 1016 | EXPECT_FALSE(db_->Raze()) |
| 1017 | << "Raze() should fail while another connection has a read lock"; |
| 1018 | |
| 1019 | ASSERT_FALSE(select.Step()) |
| 1020 | << "The SELECT statement should not produce more than one row"; |
| 1021 | EXPECT_TRUE(db_->Raze()) |
| 1022 | << "Raze() should succeed after the other connection releases the lock"; |
| 1023 | } |
| 1024 | |
| 1025 | TEST_P(SQLDatabaseTest, Raze_EmptyDatabaseFile) { |
| 1026 | ASSERT_TRUE( |
| 1027 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1028 | db_->Close(); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1029 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1030 | ASSERT_TRUE(TruncateDatabase()); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1031 | ASSERT_TRUE(db_->Open(db_path_)) |
| 1032 | << "Failed to reopen database after truncating"; |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1033 | |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1034 | EXPECT_TRUE(db_->Raze()) << "Raze() failed on an empty file"; |
| 1035 | EXPECT_TRUE( |
| 1036 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")) |
| 1037 | << "Raze() did not produce a healthy empty database"; |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | // Verify that Raze() can handle a file of junk. |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 1041 | // Need exclusive mode off here as there are some subtleties (by design) around |
| 1042 | // 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] | 1043 | TEST_P(SQLDatabaseTest, RazeNOTADB) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1044 | db_->Close(); |
| 1045 | Database::Delete(db_path_); |
| 1046 | ASSERT_FALSE(base::PathExists(db_path_)); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1047 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1048 | ASSERT_TRUE(OverwriteDatabaseHeader(OverwriteType::kTruncate)); |
| 1049 | ASSERT_TRUE(base::PathExists(db_path_)); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1050 | |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 1051 | // SQLite will successfully open the handle, but fail when running PRAGMA |
| 1052 | // statements that access the database. |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1053 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1054 | sql::test::ScopedErrorExpecter expecter; |
Victor Costan | 42988a9 | 2018-02-06 02:22:14 | [diff] [blame] | 1055 | expecter.ExpectError(SQLITE_NOTADB); |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 1056 | |
Dan McArdle | 4feabeb5 | 2024-01-03 15:17:13 | [diff] [blame] | 1057 | EXPECT_FALSE(db_->Open(db_path_)); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1058 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1059 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1060 | EXPECT_TRUE(db_->Raze()); |
| 1061 | db_->Close(); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1062 | |
| 1063 | // Now empty, the open should open an empty database. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1064 | EXPECT_TRUE(db_->Open(db_path_)); |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 1065 | EXPECT_EQ(0, SqliteSchemaCount(db_.get())); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | // Verify that Raze() can handle a database overwritten with garbage. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1069 | TEST_P(SQLDatabaseTest, RazeNOTADB2) { |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1070 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1071 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 1072 | ASSERT_EQ(1, SqliteSchemaCount(db_.get())); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1073 | db_->Close(); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1074 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1075 | ASSERT_TRUE(OverwriteDatabaseHeader(OverwriteType::kOverwrite)); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1076 | |
| 1077 | // SQLite will successfully open the handle, but will fail with |
| 1078 | // SQLITE_NOTADB on pragma statemenets which attempt to read the |
| 1079 | // corrupted header. |
| 1080 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1081 | sql::test::ScopedErrorExpecter expecter; |
| 1082 | expecter.ExpectError(SQLITE_NOTADB); |
Dan McArdle | 4feabeb5 | 2024-01-03 15:17:13 | [diff] [blame] | 1083 | EXPECT_FALSE(db_->Open(db_path_)); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1084 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1085 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1086 | EXPECT_TRUE(db_->Raze()); |
| 1087 | db_->Close(); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1088 | |
| 1089 | // Now empty, the open should succeed with an empty database. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1090 | EXPECT_TRUE(db_->Open(db_path_)); |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 1091 | EXPECT_EQ(0, SqliteSchemaCount(db_.get())); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | // Test that a callback from Open() can raze the database. This is |
| 1095 | // essential for cases where the Open() can fail entirely, so the |
shess@chromium.org | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 1096 | // Raze() cannot happen later. Additionally test that when the |
| 1097 | // callback does this during Open(), the open is retried and succeeds. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1098 | TEST_P(SQLDatabaseTest, RazeCallbackReopen) { |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1099 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1100 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 1101 | ASSERT_EQ(1, SqliteSchemaCount(db_.get())); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1102 | db_->Close(); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1103 | |
kinuko@chromium.org | a8848a7 | 2013-11-18 04:18:47 | [diff] [blame] | 1104 | // Corrupt the database so that nothing works, including PRAGMAs. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1105 | ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_)); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1106 | |
shess@chromium.org | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 1107 | // Open() will succeed, even though the PRAGMA calls within will |
| 1108 | // fail with SQLITE_CORRUPT, as will this PRAGMA. |
| 1109 | { |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1110 | sql::test::ScopedErrorExpecter expecter; |
| 1111 | expecter.ExpectError(SQLITE_CORRUPT); |
Dan McArdle | 4feabeb5 | 2024-01-03 15:17:13 | [diff] [blame] | 1112 | ASSERT_FALSE(db_->Open(db_path_)); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1113 | ASSERT_FALSE(db_->Execute("PRAGMA auto_vacuum")); |
| 1114 | db_->Close(); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1115 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
shess@chromium.org | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 1116 | } |
| 1117 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1118 | db_->set_error_callback( |
| 1119 | base::BindRepeating(&RazeErrorCallback, db_.get(), SQLITE_CORRUPT)); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1120 | |
shess@chromium.org | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 1121 | // When the PRAGMA calls in Open() raise SQLITE_CORRUPT, the error |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1122 | // callback will call RazeAndPoison(). Open() will then fail and be |
shess@chromium.org | fed734a | 2013-07-17 04:45:13 | [diff] [blame] | 1123 | // retried. The second Open() on the empty database will succeed |
| 1124 | // cleanly. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1125 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1126 | ASSERT_TRUE(db_->Execute("PRAGMA auto_vacuum")); |
John Delaney | 86dbec6 | 2021-08-24 15:05:21 | [diff] [blame] | 1127 | EXPECT_EQ(0, SqliteSchemaCount(db_.get())); |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1128 | } |
| 1129 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1130 | TEST_P(SQLDatabaseTest, RazeAndPoison_DeletesData) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1131 | ASSERT_TRUE( |
| 1132 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")); |
| 1133 | ASSERT_TRUE(db_->Execute("INSERT INTO rows(id) VALUES(12)")); |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1134 | ASSERT_TRUE(db_->RazeAndPoison()); |
shess@chromium.org | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1135 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1136 | // We need to call Close() in order to re-Open(). |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1137 | db_->Close(); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1138 | ASSERT_TRUE(db_->Open(db_path_)) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1139 | << "RazeAndPoison() did not produce a healthy database"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1140 | EXPECT_TRUE( |
| 1141 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1142 | << "RazeAndPoison() did not produce a healthy empty database"; |
shess@chromium.org | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1143 | } |
| 1144 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1145 | TEST_P(SQLDatabaseTest, RazeAndPoison_IsOpen) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1146 | ASSERT_TRUE( |
| 1147 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")); |
| 1148 | ASSERT_TRUE(db_->Execute("INSERT INTO rows(id) VALUES(12)")); |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1149 | ASSERT_TRUE(db_->RazeAndPoison()); |
shess@chromium.org | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1150 | |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1151 | EXPECT_FALSE(db_->is_open()) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1152 | << "RazeAndPoison() did not mark the database as closed"; |
shess@chromium.org | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1153 | } |
| 1154 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1155 | TEST_P(SQLDatabaseTest, RazeAndPoison_Reopen_NoChanges) { |
| 1156 | ASSERT_TRUE(db_->RazeAndPoison()); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1157 | EXPECT_FALSE( |
| 1158 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1159 | << "Execute() should return false after RazeAndPoison()"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1160 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1161 | // We need to call Close() in order to re-Open(). |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1162 | db_->Close(); |
| 1163 | ASSERT_TRUE(db_->Open(db_path_)) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1164 | << "RazeAndPoison() did not produce a healthy database"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1165 | EXPECT_TRUE( |
| 1166 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1167 | << "Execute() returned false but went through after RazeAndPoison()"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1168 | } |
| 1169 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1170 | TEST_P(SQLDatabaseTest, RazeAndPoison_OpenTransaction) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1171 | ASSERT_TRUE( |
| 1172 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")); |
| 1173 | ASSERT_TRUE(db_->Execute("INSERT INTO rows(id) VALUES(12)")); |
| 1174 | |
| 1175 | Transaction transaction(db_.get()); |
| 1176 | ASSERT_TRUE(transaction.Begin()); |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1177 | ASSERT_TRUE(db_->RazeAndPoison()); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1178 | |
| 1179 | EXPECT_FALSE(db_->is_open()) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1180 | << "RazeAndPoison() did not mark the database as closed"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1181 | EXPECT_FALSE(transaction.Commit()) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1182 | << "RazeAndPoison() did not cancel the transaction"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1183 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1184 | // We need to call Close() in order to re-Open(). |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1185 | db_->Close(); |
| 1186 | |
| 1187 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1188 | EXPECT_TRUE( |
| 1189 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1190 | << "RazeAndPoison() did not produce a healthy empty database"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1191 | } |
| 1192 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1193 | TEST_P(SQLDatabaseTest, RazeAndPoison_Preload_NoCrash) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1194 | db_->Preload(); |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1195 | db_->RazeAndPoison(); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1196 | db_->Preload(); |
| 1197 | } |
| 1198 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1199 | TEST_P(SQLDatabaseTest, RazeAndPoison_DoesTableExist) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1200 | ASSERT_TRUE( |
| 1201 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")); |
| 1202 | ASSERT_TRUE(db_->DoesTableExist("rows")) << "Incorrect test setup"; |
| 1203 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1204 | ASSERT_TRUE(db_->RazeAndPoison()); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1205 | EXPECT_FALSE(db_->DoesTableExist("rows")) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1206 | << "DoesTableExist() should return false after RazeAndPoison()"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1207 | } |
| 1208 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1209 | TEST_P(SQLDatabaseTest, RazeAndPoison_IsSQLValid) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1210 | ASSERT_TRUE(db_->IsSQLValid("SELECT 1")) << "Incorrect test setup"; |
| 1211 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1212 | ASSERT_TRUE(db_->RazeAndPoison()); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1213 | EXPECT_FALSE(db_->IsSQLValid("SELECT 1")) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1214 | << "IsSQLValid() should return false after RazeAndPoison()"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1215 | } |
| 1216 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1217 | TEST_P(SQLDatabaseTest, RazeAndPoison_Execute) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1218 | ASSERT_TRUE(db_->Execute("SELECT 1")) << "Incorrect test setup"; |
| 1219 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1220 | ASSERT_TRUE(db_->RazeAndPoison()); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1221 | EXPECT_FALSE(db_->Execute("SELECT 1")) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1222 | << "Execute() should return false after RazeAndPoison()"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1223 | } |
| 1224 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1225 | TEST_P(SQLDatabaseTest, RazeAndPoison_GetUniqueStatement) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1226 | { |
| 1227 | Statement select(db_->GetUniqueStatement("SELECT 1")); |
| 1228 | ASSERT_TRUE(select.Step()) << "Incorrect test setup"; |
| 1229 | } |
| 1230 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1231 | ASSERT_TRUE(db_->RazeAndPoison()); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1232 | { |
| 1233 | Statement select(db_->GetUniqueStatement("SELECT 1")); |
| 1234 | EXPECT_FALSE(select.Step()) |
| 1235 | << "GetUniqueStatement() should return an invalid Statement after " |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1236 | << "RazeAndPoison()"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1240 | TEST_P(SQLDatabaseTest, RazeAndPoison_GetCachedStatement) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1241 | { |
| 1242 | Statement select(db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1")); |
| 1243 | ASSERT_TRUE(select.Step()) << "Incorrect test setup"; |
| 1244 | } |
| 1245 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1246 | ASSERT_TRUE(db_->RazeAndPoison()); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1247 | { |
| 1248 | Statement select(db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1")); |
| 1249 | EXPECT_FALSE(select.Step()) |
| 1250 | << "GetCachedStatement() should return an invalid Statement after " |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1251 | << "RazeAndPoison()"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1252 | } |
| 1253 | } |
| 1254 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1255 | TEST_P(SQLDatabaseTest, RazeAndPoison_InvalidatesUniqueStatement) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1256 | Statement select(db_->GetUniqueStatement("SELECT 1")); |
| 1257 | ASSERT_TRUE(select.is_valid()) << "Incorrect test setup"; |
| 1258 | ASSERT_TRUE(select.Step()) << "Incorrect test setup"; |
| 1259 | select.Reset(/*clear_bound_vars=*/true); |
| 1260 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1261 | ASSERT_TRUE(db_->RazeAndPoison()); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1262 | EXPECT_FALSE(select.is_valid()) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1263 | << "RazeAndPoison() should invalidate live Statements"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1264 | EXPECT_FALSE(select.Step()) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1265 | << "RazeAndPoison() should invalidate live Statements"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1266 | } |
| 1267 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1268 | TEST_P(SQLDatabaseTest, RazeAndPoison_InvalidatesCachedStatement) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1269 | Statement select(db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1")); |
| 1270 | ASSERT_TRUE(select.is_valid()) << "Incorrect test setup"; |
| 1271 | ASSERT_TRUE(select.Step()) << "Incorrect test setup"; |
| 1272 | select.Reset(/*clear_bound_vars=*/true); |
| 1273 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1274 | ASSERT_TRUE(db_->RazeAndPoison()); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1275 | EXPECT_FALSE(select.is_valid()) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1276 | << "RazeAndPoison() should invalidate live Statements"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1277 | EXPECT_FALSE(select.Step()) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1278 | << "RazeAndPoison() should invalidate live Statements"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1279 | } |
| 1280 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1281 | TEST_P(SQLDatabaseTest, RazeAndPoison_TransactionBegin) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1282 | { |
| 1283 | Transaction transaction(db_.get()); |
| 1284 | ASSERT_TRUE(transaction.Begin()) << "Incorrect test setup"; |
| 1285 | ASSERT_TRUE(transaction.Commit()) << "Incorrect test setup"; |
| 1286 | } |
| 1287 | |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1288 | ASSERT_TRUE(db_->RazeAndPoison()); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1289 | { |
| 1290 | Transaction transaction(db_.get()); |
| 1291 | EXPECT_FALSE(transaction.Begin()) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1292 | << "Transaction::Begin() should return false after RazeAndPoison()"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1293 | EXPECT_FALSE(transaction.IsActiveForTesting()) |
Andrew Paseltiner | d309fec | 2023-03-28 16:59:55 | [diff] [blame] | 1294 | << "RazeAndPoison() should block transactions from starting"; |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | TEST_P(SQLDatabaseTest, Close_IsSQLValid) { |
| 1299 | ASSERT_TRUE(db_->IsSQLValid("SELECT 1")) << "Incorrect test setup"; |
| 1300 | |
| 1301 | db_->Close(); |
| 1302 | |
| 1303 | EXPECT_DCHECK_DEATH_WITH({ std::ignore = db_->IsSQLValid("SELECT 1"); }, |
| 1304 | "Illegal use of Database without a db"); |
| 1305 | } |
shess@chromium.org | 41a97c81 | 2013-02-07 02:35:38 | [diff] [blame] | 1306 | |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1307 | // On Windows, truncate silently fails against a memory-mapped file. One goal |
| 1308 | // of Raze() is to truncate the file to remove blocks which generate I/O errors. |
| 1309 | // Test that Raze() turns off memory mapping so that the file is truncated. |
| 1310 | // [This would not cover the case of multiple connections where one of the other |
| 1311 | // connections is memory-mapped. That is infrequent in Chromium.] |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1312 | TEST_P(SQLDatabaseTest, RazeTruncate) { |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1313 | // The empty database has 0 or 1 pages. Raze() should leave it with exactly 1 |
| 1314 | // page. Not checking directly because auto_vacuum on Android adds a freelist |
| 1315 | // page. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1316 | ASSERT_TRUE(db_->Raze()); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1317 | int64_t expected_size; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1318 | ASSERT_TRUE(base::GetFileSize(db_path_, &expected_size)); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1319 | ASSERT_GT(expected_size, 0); |
| 1320 | |
| 1321 | // Cause the database to take a few pages. |
| 1322 | const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1323 | ASSERT_TRUE(db_->Execute(kCreateSql)); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1324 | for (size_t i = 0; i < 24; ++i) { |
| 1325 | ASSERT_TRUE( |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1326 | db_->Execute("INSERT INTO foo (value) VALUES (randomblob(1024))")); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1327 | } |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 1328 | |
| 1329 | // In WAL mode, writes don't reach the database file until a checkpoint |
| 1330 | // happens. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1331 | ASSERT_TRUE(db_->CheckpointDatabase()); |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 1332 | |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1333 | int64_t db_size; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1334 | ASSERT_TRUE(base::GetFileSize(db_path_, &db_size)); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1335 | ASSERT_GT(db_size, expected_size); |
| 1336 | |
| 1337 | // Make a query covering most of the database file to make sure that the |
| 1338 | // blocks are actually mapped into memory. Empirically, the truncate problem |
| 1339 | // doesn't seem to happen if no blocks are mapped. |
| 1340 | EXPECT_EQ("24576", |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1341 | ExecuteWithResult(db_.get(), "SELECT SUM(LENGTH(value)) FROM foo")); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1342 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1343 | ASSERT_TRUE(db_->Raze()); |
| 1344 | ASSERT_TRUE(base::GetFileSize(db_path_, &db_size)); |
shess | 92a6fb2 | 2017-04-23 04:33:30 | [diff] [blame] | 1345 | ASSERT_EQ(expected_size, db_size); |
| 1346 | } |
| 1347 | |
Xiaohan Wang | 7d09c5e | 2022-01-08 02:37:36 | [diff] [blame] | 1348 | #if BUILDFLAG(IS_ANDROID) |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1349 | TEST_P(SQLDatabaseTest, SetTempDirForSQL) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1350 | MetaTable meta_table; |
yongsheng.zhu@intel.com | 1348765 | 2012-07-24 08:25:53 | [diff] [blame] | 1351 | // Below call needs a temporary directory in sqlite3 |
| 1352 | // On Android, it can pass only when the temporary directory is set. |
| 1353 | // Otherwise, sqlite3 doesn't find the correct directory to store |
| 1354 | // temporary files and will report the error 'unable to open |
| 1355 | // database file'. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1356 | ASSERT_TRUE(meta_table.Init(db_.get(), 4, 4)); |
yongsheng.zhu@intel.com | 1348765 | 2012-07-24 08:25:53 | [diff] [blame] | 1357 | } |
Xiaohan Wang | 7d09c5e | 2022-01-08 02:37:36 | [diff] [blame] | 1358 | #endif // BUILDFLAG(IS_ANDROID) |
shess@chromium.org | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 1359 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1360 | TEST_P(SQLDatabaseTest, Delete) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1361 | EXPECT_TRUE(db_->Execute("CREATE TABLE x (x)")); |
| 1362 | db_->Close(); |
shess@chromium.org | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 1363 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1364 | base::FilePath journal_path = Database::JournalPath(db_path_); |
| 1365 | base::FilePath wal_path = Database::WriteAheadLogPath(db_path_); |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 1366 | |
| 1367 | // Should have both a main database file and a journal file if |
| 1368 | // 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] | 1369 | ASSERT_TRUE(base::PathExists(db_path_)); |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 1370 | if (!IsWALEnabled()) { // TRUNCATE mode |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1371 | ASSERT_TRUE(base::PathExists(journal_path)); |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 1372 | } |
shess@chromium.org | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 1373 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1374 | Database::Delete(db_path_); |
| 1375 | EXPECT_FALSE(base::PathExists(db_path_)); |
| 1376 | EXPECT_FALSE(base::PathExists(journal_path)); |
| 1377 | EXPECT_FALSE(base::PathExists(wal_path)); |
shess@chromium.org | 8d2e39e | 2013-06-24 05:55:08 | [diff] [blame] | 1378 | } |
shess@chromium.org | 7bae574 | 2013-07-10 20:46:16 | [diff] [blame] | 1379 | |
Xiaohan Wang | 7d09c5e | 2022-01-08 02:37:36 | [diff] [blame] | 1380 | #if BUILDFLAG(IS_POSIX) // This test operates on POSIX file permissions. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1381 | TEST_P(SQLDatabaseTest, PosixFilePermissions) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1382 | db_->Close(); |
| 1383 | Database::Delete(db_path_); |
| 1384 | ASSERT_FALSE(base::PathExists(db_path_)); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 1385 | |
| 1386 | // If the bots all had a restrictive umask setting such that databases are |
| 1387 | // always created with only the owner able to read them, then the code could |
| 1388 | // break without breaking the tests. Temporarily provide a more permissive |
| 1389 | // umask. |
shess@chromium.org | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1390 | ScopedUmaskSetter permissive_umask(S_IWGRP | S_IWOTH); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 1391 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1392 | ASSERT_TRUE(db_->Open(db_path_)); |
shess@chromium.org | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1393 | |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 1394 | // Cause the journal file to be created. If the default journal_mode is |
| 1395 | // changed back to DELETE, this test will need to be updated. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1396 | EXPECT_TRUE(db_->Execute("CREATE TABLE x (x)")); |
shess@chromium.org | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1397 | |
shess@chromium.org | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1398 | int mode; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1399 | ASSERT_TRUE(base::PathExists(db_path_)); |
| 1400 | EXPECT_TRUE(base::GetPosixFilePermissions(db_path_, &mode)); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 1401 | ASSERT_EQ(mode, 0600); |
shess@chromium.org | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1402 | |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 1403 | if (IsWALEnabled()) { // WAL mode |
| 1404 | // The WAL file is created lazily on first change. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1405 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
shess@chromium.org | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1406 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1407 | base::FilePath wal_path = Database::WriteAheadLogPath(db_path_); |
| 1408 | ASSERT_TRUE(base::PathExists(wal_path)); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 1409 | EXPECT_TRUE(base::GetPosixFilePermissions(wal_path, &mode)); |
| 1410 | ASSERT_EQ(mode, 0600); |
| 1411 | |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 1412 | // The shm file doesn't exist in exclusive locking mode. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1413 | if (ExecuteWithResult(db_.get(), "PRAGMA locking_mode") == "normal") { |
| 1414 | base::FilePath shm_path = Database::SharedMemoryFilePath(db_path_); |
| 1415 | ASSERT_TRUE(base::PathExists(shm_path)); |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 1416 | EXPECT_TRUE(base::GetPosixFilePermissions(shm_path, &mode)); |
| 1417 | ASSERT_EQ(mode, 0600); |
| 1418 | } |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 1419 | } else { // Truncate mode |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1420 | base::FilePath journal_path = Database::JournalPath(db_path_); |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 1421 | DLOG(ERROR) << "journal_path: " << journal_path; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1422 | ASSERT_TRUE(base::PathExists(journal_path)); |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 1423 | EXPECT_TRUE(base::GetPosixFilePermissions(journal_path, &mode)); |
| 1424 | ASSERT_EQ(mode, 0600); |
Victor Costan | ce678e7 | 2018-07-24 10:25:00 | [diff] [blame] | 1425 | } |
shess@chromium.org | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1426 | } |
Xiaohan Wang | 7d09c5e | 2022-01-08 02:37:36 | [diff] [blame] | 1427 | #endif // BUILDFLAG(IS_POSIX) |
shess@chromium.org | 81a2a60 | 2013-07-17 19:10:36 | [diff] [blame] | 1428 | |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1429 | TEST_P(SQLDatabaseTest, Poison_IsOpen) { |
| 1430 | db_->Poison(); |
| 1431 | EXPECT_FALSE(db_->is_open()) |
| 1432 | << "Poison() did not mark the database as closed"; |
| 1433 | } |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1434 | |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1435 | TEST_P(SQLDatabaseTest, Poison_Close_Reopen_NoChanges) { |
| 1436 | db_->Poison(); |
| 1437 | EXPECT_FALSE( |
| 1438 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")) |
| 1439 | << "Execute() should return false after Poison()"; |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1440 | |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1441 | db_->Close(); |
| 1442 | ASSERT_TRUE(db_->Open(db_path_)) << "Poison() damaged the database"; |
| 1443 | EXPECT_TRUE( |
| 1444 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")) |
| 1445 | << "Execute() returned false but went through after Poison()"; |
| 1446 | } |
| 1447 | |
| 1448 | TEST_P(SQLDatabaseTest, Poison_Preload_NoCrash) { |
| 1449 | db_->Preload(); |
| 1450 | db_->Poison(); |
| 1451 | db_->Preload(); |
| 1452 | } |
| 1453 | |
| 1454 | TEST_P(SQLDatabaseTest, Poison_DoesTableExist) { |
| 1455 | ASSERT_TRUE( |
| 1456 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")); |
| 1457 | ASSERT_TRUE(db_->DoesTableExist("rows")) << "Incorrect test setup"; |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1458 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1459 | db_->Poison(); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1460 | EXPECT_FALSE(db_->DoesTableExist("rows")) |
| 1461 | << "DoesTableExist() should return false after Poison()"; |
| 1462 | } |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1463 | |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1464 | TEST_P(SQLDatabaseTest, Poison_IsSQLValid) { |
| 1465 | ASSERT_TRUE(db_->IsSQLValid("SELECT 1")) << "Incorrect test setup"; |
| 1466 | |
| 1467 | db_->Poison(); |
| 1468 | EXPECT_FALSE(db_->IsSQLValid("SELECT 1")) |
| 1469 | << "IsSQLValid() should return false after Poison()"; |
| 1470 | } |
| 1471 | |
| 1472 | TEST_P(SQLDatabaseTest, Poison_Execute) { |
| 1473 | ASSERT_TRUE(db_->Execute("SELECT 1")) << "Incorrect test setup"; |
| 1474 | |
| 1475 | db_->Poison(); |
| 1476 | EXPECT_FALSE(db_->Execute("SELECT 1")) |
| 1477 | << "Execute() should return false after Poison()"; |
| 1478 | } |
| 1479 | |
| 1480 | TEST_P(SQLDatabaseTest, Poison_GetUniqueStatement) { |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1481 | { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1482 | Statement select(db_->GetUniqueStatement("SELECT 1")); |
| 1483 | ASSERT_TRUE(select.Step()) << "Incorrect test setup"; |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1484 | } |
| 1485 | |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1486 | db_->Poison(); |
| 1487 | { |
| 1488 | Statement select(db_->GetUniqueStatement("SELECT 1")); |
| 1489 | EXPECT_FALSE(select.Step()) |
| 1490 | << "GetUniqueStatement() should return an invalid Statement after " |
| 1491 | << "Poison()"; |
| 1492 | } |
| 1493 | } |
shess | 644fc8a | 2016-02-26 18:15:58 | [diff] [blame] | 1494 | |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1495 | TEST_P(SQLDatabaseTest, Poison_GetCachedStatement) { |
| 1496 | { |
| 1497 | Statement select(db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1")); |
| 1498 | ASSERT_TRUE(select.Step()) << "Incorrect test setup"; |
| 1499 | } |
| 1500 | |
| 1501 | db_->Poison(); |
| 1502 | { |
| 1503 | Statement select(db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1")); |
| 1504 | EXPECT_FALSE(select.Step()) |
| 1505 | << "GetCachedStatement() should return an invalid Statement after " |
| 1506 | << "Poison()"; |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | TEST_P(SQLDatabaseTest, Poison_InvalidatesUniqueStatement) { |
| 1511 | Statement select(db_->GetUniqueStatement("SELECT 1")); |
| 1512 | ASSERT_TRUE(select.is_valid()) << "Incorrect test setup"; |
| 1513 | ASSERT_TRUE(select.Step()) << "Incorrect test setup"; |
| 1514 | select.Reset(/*clear_bound_vars=*/true); |
| 1515 | |
| 1516 | db_->Poison(); |
| 1517 | EXPECT_FALSE(select.is_valid()) |
| 1518 | << "Poison() should invalidate live Statements"; |
| 1519 | EXPECT_FALSE(select.Step()) << "Poison() should invalidate live Statements"; |
| 1520 | } |
| 1521 | |
| 1522 | TEST_P(SQLDatabaseTest, Poison_InvalidatesCachedStatement) { |
| 1523 | Statement select(db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1")); |
| 1524 | ASSERT_TRUE(select.is_valid()) << "Incorrect test setup"; |
| 1525 | ASSERT_TRUE(select.Step()) << "Incorrect test setup"; |
| 1526 | select.Reset(/*clear_bound_vars=*/true); |
| 1527 | |
| 1528 | db_->Poison(); |
| 1529 | EXPECT_FALSE(select.is_valid()) |
| 1530 | << "Poison() should invalidate live Statements"; |
| 1531 | EXPECT_FALSE(select.Step()) << "Poison() should invalidate live Statements"; |
| 1532 | } |
| 1533 | |
| 1534 | TEST_P(SQLDatabaseTest, Poison_TransactionBegin) { |
| 1535 | { |
| 1536 | Transaction transaction(db_.get()); |
| 1537 | ASSERT_TRUE(transaction.Begin()) << "Incorrect test setup"; |
| 1538 | ASSERT_TRUE(transaction.Commit()) << "Incorrect test setup"; |
| 1539 | } |
| 1540 | |
| 1541 | db_->Poison(); |
| 1542 | { |
| 1543 | Transaction transaction(db_.get()); |
| 1544 | EXPECT_FALSE(transaction.Begin()) |
| 1545 | << "Transaction::Begin() should return false after Poison()"; |
| 1546 | EXPECT_FALSE(transaction.IsActiveForTesting()) |
| 1547 | << "Poison() should block transactions from starting"; |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | TEST_P(SQLDatabaseTest, Poison_OpenTransaction) { |
| 1552 | Transaction transaction(db_.get()); |
| 1553 | ASSERT_TRUE(transaction.Begin()); |
| 1554 | |
| 1555 | db_->Poison(); |
| 1556 | EXPECT_FALSE(transaction.Commit()) |
| 1557 | << "Poison() did not cancel the transaction"; |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1558 | } |
| 1559 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1560 | TEST_P(SQLDatabaseTest, AttachDatabase) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1561 | ASSERT_TRUE( |
| 1562 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")); |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1563 | |
| 1564 | // Create a database to attach to. |
| 1565 | base::FilePath attach_path = |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1566 | db_path_.DirName().AppendASCII("attach_database_test.db"); |
| 1567 | static constexpr char kAttachmentPoint[] = "other"; |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1568 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1569 | Database other_db; |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1570 | ASSERT_TRUE(other_db.Open(attach_path)); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1571 | ASSERT_TRUE( |
| 1572 | other_db.Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")); |
| 1573 | ASSERT_TRUE(other_db.Execute("INSERT INTO rows VALUES(42)")); |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | // Cannot see the attached database, yet. |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1577 | EXPECT_FALSE(db_->IsSQLValid("SELECT COUNT(*) from other.rows")); |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1578 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1579 | EXPECT_TRUE(DatabaseTestPeer::AttachDatabase(db_.get(), attach_path, |
| 1580 | kAttachmentPoint)); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1581 | EXPECT_TRUE(db_->IsSQLValid("SELECT COUNT(*) from other.rows")); |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1582 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1583 | // Queries can touch both databases after the ATTACH. |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1584 | EXPECT_TRUE(db_->Execute("INSERT INTO rows SELECT id FROM other.rows")); |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1585 | { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1586 | Statement select(db_->GetUniqueStatement("SELECT COUNT(*) FROM rows")); |
| 1587 | ASSERT_TRUE(select.Step()); |
| 1588 | EXPECT_EQ(1, select.ColumnInt(0)); |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1589 | } |
| 1590 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1591 | EXPECT_TRUE(DatabaseTestPeer::DetachDatabase(db_.get(), kAttachmentPoint)); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1592 | EXPECT_FALSE(db_->IsSQLValid("SELECT COUNT(*) from other.rows")); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1593 | } |
| 1594 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1595 | TEST_P(SQLDatabaseTest, AttachDatabaseWithOpenTransaction) { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1596 | ASSERT_TRUE( |
| 1597 | db_->Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1598 | |
| 1599 | // Create a database to attach to. |
| 1600 | base::FilePath attach_path = |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1601 | db_path_.DirName().AppendASCII("attach_database_test.db"); |
| 1602 | static constexpr char kAttachmentPoint[] = "other"; |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1603 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1604 | Database other_db; |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1605 | ASSERT_TRUE(other_db.Open(attach_path)); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1606 | ASSERT_TRUE( |
| 1607 | other_db.Execute("CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL)")); |
| 1608 | ASSERT_TRUE(other_db.Execute("INSERT INTO rows VALUES(42)")); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | // Cannot see the attached database, yet. |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1612 | EXPECT_FALSE(db_->IsSQLValid("SELECT COUNT(*) from other.rows")); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1613 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1614 | // Attach succeeds in a transaction. |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1615 | Transaction transaction(db_.get()); |
| 1616 | EXPECT_TRUE(transaction.Begin()); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1617 | EXPECT_TRUE(DatabaseTestPeer::AttachDatabase(db_.get(), attach_path, |
| 1618 | kAttachmentPoint)); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1619 | EXPECT_TRUE(db_->IsSQLValid("SELECT COUNT(*) from other.rows")); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1620 | |
| 1621 | // Queries can touch both databases after the ATTACH. |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1622 | EXPECT_TRUE(db_->Execute("INSERT INTO rows SELECT id FROM other.rows")); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1623 | { |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1624 | Statement select(db_->GetUniqueStatement("SELECT COUNT(*) FROM rows")); |
| 1625 | ASSERT_TRUE(select.Step()); |
| 1626 | EXPECT_EQ(1, select.ColumnInt(0)); |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | // Detaching the same database fails, database is locked in the transaction. |
| 1630 | { |
| 1631 | sql::test::ScopedErrorExpecter expecter; |
| 1632 | expecter.ExpectError(SQLITE_ERROR); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1633 | EXPECT_FALSE(DatabaseTestPeer::DetachDatabase(db_.get(), kAttachmentPoint)); |
shess | 97681440 | 2016-06-21 06:56:25 | [diff] [blame] | 1634 | ASSERT_TRUE(expecter.SawExpectedErrors()); |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1635 | } |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1636 | EXPECT_TRUE(db_->IsSQLValid("SELECT COUNT(*) from other.rows")); |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1637 | |
Victor Costan | 8a87f7e5 | 2017-11-10 01:29:30 | [diff] [blame] | 1638 | // Detach succeeds when the transaction is closed. |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1639 | transaction.Rollback(); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1640 | EXPECT_TRUE(DatabaseTestPeer::DetachDatabase(db_.get(), kAttachmentPoint)); |
Victor Costan | e7944b4e | 2022-03-30 20:06:22 | [diff] [blame] | 1641 | EXPECT_FALSE(db_->IsSQLValid("SELECT COUNT(*) from other.rows")); |
shess@chromium.org | 8d40941 | 2013-07-19 18:25:30 | [diff] [blame] | 1642 | } |
| 1643 | |
Victor Costan | 4b67f54 | 2022-02-24 17:40:55 | [diff] [blame] | 1644 | TEST_P(SQLDatabaseTest, FullIntegrityCheck) { |
| 1645 | static constexpr char kTableSql[] = |
| 1646 | "CREATE TABLE rows(id INTEGER PRIMARY KEY NOT NULL, value TEXT NOT NULL)"; |
| 1647 | ASSERT_TRUE(db_->Execute(kTableSql)); |
Victor Costan | 8c3a992 | 2022-02-26 03:50:52 | [diff] [blame] | 1648 | ASSERT_TRUE(db_->Execute("CREATE INDEX rows_by_value ON rows(value)")); |
| 1649 | |
Victor Costan | 4b67f54 | 2022-02-24 17:40:55 | [diff] [blame] | 1650 | { |
| 1651 | std::vector<std::string> messages; |
| 1652 | EXPECT_TRUE(db_->FullIntegrityCheck(&messages)) |
| 1653 | << "FullIntegrityCheck() failed before database was corrupted"; |
| 1654 | EXPECT_THAT(messages, testing::ElementsAre("ok")) |
| 1655 | << "FullIntegrityCheck() should report ok before database is corrupted"; |
| 1656 | } |
michaeln@chromium.org | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1657 | |
Victor Costan | 8c3a992 | 2022-02-26 03:50:52 | [diff] [blame] | 1658 | db_->Close(); |
| 1659 | ASSERT_TRUE(sql::test::CorruptIndexRootPage(db_path_, "rows_by_value")); |
| 1660 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1661 | |
Victor Costan | 4b67f54 | 2022-02-24 17:40:55 | [diff] [blame] | 1662 | { |
| 1663 | std::vector<std::string> messages; |
| 1664 | EXPECT_TRUE(db_->FullIntegrityCheck(&messages)) |
| 1665 | << "FullIntegrityCheck() failed on corrupted database"; |
| 1666 | EXPECT_THAT(messages, testing::Not(testing::ElementsAre("ok"))) |
| 1667 | << "FullIntegrityCheck() should not report ok for a corrupted database"; |
michaeln@chromium.org | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1668 | } |
michaeln@chromium.org | 579446c | 2013-12-16 18:36:52 | [diff] [blame] | 1669 | } |
| 1670 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1671 | TEST_P(SQLDatabaseTest, OnMemoryDump) { |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 1672 | base::trace_event::MemoryDumpArgs args = { |
Ho Cheung | adbf3fb | 2023-09-08 02:01:11 | [diff] [blame] | 1673 | base::trace_event::MemoryDumpLevelOfDetail::kDetailed}; |
erikchen | f62ea04 | 2018-05-25 21:30:57 | [diff] [blame] | 1674 | base::trace_event::ProcessMemoryDump pmd(args); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1675 | ASSERT_TRUE(db_->memory_dump_provider_->OnMemoryDump(args, &pmd)); |
ssid | 9f8022f | 2015-10-12 17:49:03 | [diff] [blame] | 1676 | EXPECT_GE(pmd.allocator_dumps().size(), 1u); |
| 1677 | } |
| 1678 | |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1679 | // Test that the functions to collect diagnostic data run to completion, without |
| 1680 | // worrying too much about what they generate (since that will change). |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1681 | TEST_P(SQLDatabaseTest, CollectDiagnosticInfo) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1682 | const std::string corruption_info = db_->CollectCorruptionInfo(); |
Ho Cheung | 4752435 | 2023-08-08 03:41:10 | [diff] [blame] | 1683 | EXPECT_TRUE(base::Contains(corruption_info, "SQLITE_CORRUPT")); |
| 1684 | EXPECT_TRUE(base::Contains(corruption_info, "integrity_check")); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1685 | |
| 1686 | // A statement to see in the results. |
| 1687 | const char* kSimpleSql = "SELECT 'mountain'"; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1688 | Statement s(db_->GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1689 | |
| 1690 | // Error includes the statement. |
Tommy C. Li | 7803636c | 2022-07-27 21:47:22 | [diff] [blame] | 1691 | { |
| 1692 | DatabaseDiagnostics diagnostics; |
| 1693 | const std::string readonly_info = |
| 1694 | db_->CollectErrorInfo(SQLITE_READONLY, &s, &diagnostics); |
Ho Cheung | 4752435 | 2023-08-08 03:41:10 | [diff] [blame] | 1695 | EXPECT_TRUE(base::Contains(readonly_info, kSimpleSql)); |
Tommy C. Li | 7803636c | 2022-07-27 21:47:22 | [diff] [blame] | 1696 | EXPECT_EQ(diagnostics.sql_statement, kSimpleSql); |
| 1697 | } |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1698 | |
Tommy C. Li | 7803636c | 2022-07-27 21:47:22 | [diff] [blame] | 1699 | // Some other error doesn't include the statement. |
| 1700 | { |
| 1701 | DatabaseDiagnostics diagnostics; |
| 1702 | const std::string full_info = |
| 1703 | db_->CollectErrorInfo(SQLITE_FULL, nullptr, &diagnostics); |
Ho Cheung | 4752435 | 2023-08-08 03:41:10 | [diff] [blame] | 1704 | EXPECT_FALSE(base::Contains(full_info, kSimpleSql)); |
Tommy C. Li | 7803636c | 2022-07-27 21:47:22 | [diff] [blame] | 1705 | EXPECT_TRUE(diagnostics.sql_statement.empty()); |
| 1706 | } |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1707 | |
| 1708 | // A table to see in the SQLITE_ERROR results. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1709 | EXPECT_TRUE(db_->Execute("CREATE TABLE volcano (x)")); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1710 | |
| 1711 | // Version info to see in the SQLITE_ERROR results. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1712 | MetaTable meta_table; |
| 1713 | ASSERT_TRUE(meta_table.Init(db_.get(), 4, 4)); |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1714 | |
Tommy C. Li | 7803636c | 2022-07-27 21:47:22 | [diff] [blame] | 1715 | { |
| 1716 | DatabaseDiagnostics diagnostics; |
| 1717 | const std::string error_info = |
| 1718 | db_->CollectErrorInfo(SQLITE_ERROR, &s, &diagnostics); |
Ho Cheung | 4752435 | 2023-08-08 03:41:10 | [diff] [blame] | 1719 | EXPECT_TRUE(base::Contains(error_info, kSimpleSql)); |
| 1720 | EXPECT_TRUE(base::Contains(error_info, "volcano")); |
| 1721 | EXPECT_TRUE(base::Contains(error_info, "version: 4")); |
Tommy C. Li | 7803636c | 2022-07-27 21:47:22 | [diff] [blame] | 1722 | EXPECT_EQ(diagnostics.sql_statement, kSimpleSql); |
| 1723 | EXPECT_EQ(diagnostics.version, 4); |
| 1724 | |
| 1725 | ASSERT_EQ(diagnostics.schema_sql_rows.size(), 2U); |
| 1726 | EXPECT_EQ(diagnostics.schema_sql_rows[0], "CREATE TABLE volcano (x)"); |
| 1727 | EXPECT_EQ(diagnostics.schema_sql_rows[1], |
| 1728 | "CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, " |
| 1729 | "value LONGVARCHAR)"); |
| 1730 | |
| 1731 | ASSERT_EQ(diagnostics.schema_other_row_names.size(), 1U); |
| 1732 | EXPECT_EQ(diagnostics.schema_other_row_names[0], "sqlite_autoindex_meta_1"); |
| 1733 | } |
Tommy C. Li | 400c0f1 | 2022-09-13 22:39:28 | [diff] [blame] | 1734 | |
| 1735 | // Test that an error message is included in the diagnostics. |
| 1736 | { |
| 1737 | sql::test::ScopedErrorExpecter error_expecter; |
| 1738 | error_expecter.ExpectError(SQLITE_ERROR); |
| 1739 | EXPECT_FALSE( |
| 1740 | db_->Execute("INSERT INTO volcano VALUES ('bound_value1', 42, 1234)")); |
| 1741 | EXPECT_TRUE(error_expecter.SawExpectedErrors()); |
| 1742 | |
| 1743 | DatabaseDiagnostics diagnostics; |
| 1744 | const std::string error_info = |
| 1745 | db_->CollectErrorInfo(SQLITE_ERROR, &s, &diagnostics); |
| 1746 | // Expect that the error message contains the table name and a column error. |
Ho Cheung | 4752435 | 2023-08-08 03:41:10 | [diff] [blame] | 1747 | EXPECT_TRUE(base::Contains(diagnostics.error_message, "table")); |
| 1748 | EXPECT_TRUE(base::Contains(diagnostics.error_message, "volcano")); |
| 1749 | EXPECT_TRUE(base::Contains(diagnostics.error_message, "column")); |
Tommy C. Li | 400c0f1 | 2022-09-13 22:39:28 | [diff] [blame] | 1750 | |
| 1751 | // Expect that bound values are not present. |
Ho Cheung | 4752435 | 2023-08-08 03:41:10 | [diff] [blame] | 1752 | EXPECT_FALSE(base::Contains(diagnostics.error_message, "bound_value1")); |
| 1753 | EXPECT_FALSE(base::Contains(diagnostics.error_message, "42")); |
| 1754 | EXPECT_FALSE(base::Contains(diagnostics.error_message, "1234")); |
Tommy C. Li | 400c0f1 | 2022-09-13 22:39:28 | [diff] [blame] | 1755 | } |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 1756 | } |
| 1757 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1758 | // Test that a fresh database has mmap enabled by default, if mmap'ed I/O is |
| 1759 | // enabled by SQLite. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1760 | TEST_P(SQLDatabaseTest, MmapInitiallyEnabled) { |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1761 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1762 | Statement s(db_->GetUniqueStatement("PRAGMA mmap_size")); |
Victor Costan | 42988a9 | 2018-02-06 02:22:14 | [diff] [blame] | 1763 | ASSERT_TRUE(s.Step()) |
| 1764 | << "All supported SQLite versions should have mmap support"; |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1765 | |
| 1766 | // If mmap I/O is not on, attempt to turn it on. If that succeeds, then |
| 1767 | // Open() should have turned it on. If mmap support is disabled, 0 is |
| 1768 | // returned. If the VFS does not understand SQLITE_FCNTL_MMAP_SIZE (for |
| 1769 | // instance MojoVFS), -1 is returned. |
| 1770 | if (s.ColumnInt(0) <= 0) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1771 | ASSERT_TRUE(db_->Execute("PRAGMA mmap_size = 1048576")); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1772 | s.Reset(true); |
| 1773 | ASSERT_TRUE(s.Step()); |
| 1774 | EXPECT_LE(s.ColumnInt(0), 0); |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | // Test that explicit disable prevents mmap'ed I/O. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1779 | db_->Close(); |
| 1780 | Database::Delete(db_path_); |
| 1781 | db_->set_mmap_disabled(); |
| 1782 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1783 | EXPECT_EQ("0", ExecuteWithResult(db_.get(), "PRAGMA mmap_size")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | // Test whether a fresh database gets mmap enabled when using alternate status |
| 1787 | // storage. |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1788 | TEST_P(SQLDatabaseTest, MmapInitiallyEnabledAltStatus) { |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1789 | // Re-open fresh database with alt-status flag set. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1790 | db_->Close(); |
| 1791 | Database::Delete(db_path_); |
Victor Costan | 8ec18ee4 | 2021-07-13 19:45:32 | [diff] [blame] | 1792 | |
| 1793 | DatabaseOptions options = GetDBOptions(); |
| 1794 | options.mmap_alt_status_discouraged = true; |
Victor Costan | fe078f9 | 2021-07-19 20:02:59 | [diff] [blame] | 1795 | options.enable_views_discouraged = true; |
Victor Costan | 8ec18ee4 | 2021-07-13 19:45:32 | [diff] [blame] | 1796 | db_ = std::make_unique<Database>(options); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1797 | ASSERT_TRUE(db_->Open(db_path_)); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1798 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1799 | { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1800 | Statement s(db_->GetUniqueStatement("PRAGMA mmap_size")); |
Victor Costan | 42988a9 | 2018-02-06 02:22:14 | [diff] [blame] | 1801 | ASSERT_TRUE(s.Step()) |
| 1802 | << "All supported SQLite versions should have mmap support"; |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1803 | |
| 1804 | // If mmap I/O is not on, attempt to turn it on. If that succeeds, then |
| 1805 | // Open() should have turned it on. If mmap support is disabled, 0 is |
| 1806 | // returned. If the VFS does not understand SQLITE_FCNTL_MMAP_SIZE (for |
| 1807 | // instance MojoVFS), -1 is returned. |
| 1808 | if (s.ColumnInt(0) <= 0) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1809 | ASSERT_TRUE(db_->Execute("PRAGMA mmap_size = 1048576")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1810 | s.Reset(true); |
| 1811 | ASSERT_TRUE(s.Step()); |
| 1812 | EXPECT_LE(s.ColumnInt(0), 0); |
| 1813 | } |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1814 | } |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1815 | |
| 1816 | // Test that explicit disable overrides set_mmap_alt_status(). |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1817 | db_->Close(); |
| 1818 | Database::Delete(db_path_); |
| 1819 | db_->set_mmap_disabled(); |
| 1820 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1821 | EXPECT_EQ("0", ExecuteWithResult(db_.get(), "PRAGMA mmap_size")); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1822 | } |
| 1823 | |
Victor Costan | dc72e8d | 2022-03-14 18:14:51 | [diff] [blame] | 1824 | TEST_P(SQLDatabaseTest, ComputeMmapSizeForOpen) { |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1825 | const size_t kMmapAlot = 25 * 1024 * 1024; |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1826 | int64_t mmap_status = MetaTable::kMmapFailure; |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1827 | |
| 1828 | // If there is no meta table (as for a fresh database), assume that everything |
shess | 9e77283d | 2016-06-13 23:53:20 | [diff] [blame] | 1829 | // should be mapped, and the status of the meta table is not affected. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1830 | ASSERT_TRUE(!db_->DoesTableExist("meta")); |
Victor Costan | dc72e8d | 2022-03-14 18:14:51 | [diff] [blame] | 1831 | ASSERT_GT(db_->ComputeMmapSizeForOpen(), kMmapAlot); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1832 | ASSERT_TRUE(!db_->DoesTableExist("meta")); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1833 | |
| 1834 | // When the meta table is first created, it sets up to map everything. |
Andrew Paseltiner | ef652c9 | 2023-02-08 21:26:32 | [diff] [blame] | 1835 | ASSERT_TRUE(MetaTable().Init(db_.get(), 1, 1)); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1836 | ASSERT_TRUE(db_->DoesTableExist("meta")); |
Victor Costan | dc72e8d | 2022-03-14 18:14:51 | [diff] [blame] | 1837 | ASSERT_GT(db_->ComputeMmapSizeForOpen(), kMmapAlot); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1838 | ASSERT_TRUE(MetaTable::GetMmapStatus(db_.get(), &mmap_status)); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1839 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1840 | |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1841 | // Preload with partial progress of one page. Should map everything. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1842 | ASSERT_TRUE(db_->Execute("REPLACE INTO meta VALUES ('mmap_status', 1)")); |
Victor Costan | dc72e8d | 2022-03-14 18:14:51 | [diff] [blame] | 1843 | ASSERT_GT(db_->ComputeMmapSizeForOpen(), kMmapAlot); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1844 | ASSERT_TRUE(MetaTable::GetMmapStatus(db_.get(), &mmap_status)); |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1845 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1846 | |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1847 | // Failure status maps nothing. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1848 | ASSERT_TRUE(db_->Execute("REPLACE INTO meta VALUES ('mmap_status', -2)")); |
Victor Costan | dc72e8d | 2022-03-14 18:14:51 | [diff] [blame] | 1849 | ASSERT_EQ(0UL, db_->ComputeMmapSizeForOpen()); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1850 | |
| 1851 | // Re-initializing the meta table does not re-create the key if the table |
| 1852 | // already exists. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1853 | ASSERT_TRUE(db_->Execute("DELETE FROM meta WHERE key = 'mmap_status'")); |
Andrew Paseltiner | ef652c9 | 2023-02-08 21:26:32 | [diff] [blame] | 1854 | ASSERT_TRUE(MetaTable().Init(db_.get(), 1, 1)); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1855 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1856 | ASSERT_TRUE(MetaTable::GetMmapStatus(db_.get(), &mmap_status)); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1857 | ASSERT_EQ(0, mmap_status); |
| 1858 | |
| 1859 | // With no key, map everything and create the key. |
| 1860 | // TODO(shess): This really should be "maps everything after validating it", |
| 1861 | // but that is more complicated to structure. |
Victor Costan | dc72e8d | 2022-03-14 18:14:51 | [diff] [blame] | 1862 | ASSERT_GT(db_->ComputeMmapSizeForOpen(), kMmapAlot); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1863 | ASSERT_TRUE(MetaTable::GetMmapStatus(db_.get(), &mmap_status)); |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1864 | ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1865 | } |
shess | 9bf2c67 | 2015-12-18 01:18:08 | [diff] [blame] | 1866 | |
Victor Costan | dc72e8d | 2022-03-14 18:14:51 | [diff] [blame] | 1867 | TEST_P(SQLDatabaseTest, ComputeMmapSizeForOpenAltStatus) { |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1868 | const size_t kMmapAlot = 25 * 1024 * 1024; |
| 1869 | |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 1870 | // At this point, Database still expects a future [meta] table. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1871 | ASSERT_FALSE(db_->DoesTableExist("meta")); |
| 1872 | ASSERT_FALSE(db_->DoesViewExist("MmapStatus")); |
Victor Costan | dc72e8d | 2022-03-14 18:14:51 | [diff] [blame] | 1873 | ASSERT_GT(db_->ComputeMmapSizeForOpen(), kMmapAlot); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1874 | ASSERT_FALSE(db_->DoesTableExist("meta")); |
| 1875 | ASSERT_FALSE(db_->DoesViewExist("MmapStatus")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1876 | |
| 1877 | // Using alt status, everything should be mapped, with state in the view. |
Victor Costan | 8ec18ee4 | 2021-07-13 19:45:32 | [diff] [blame] | 1878 | DatabaseOptions options = GetDBOptions(); |
| 1879 | options.mmap_alt_status_discouraged = true; |
Victor Costan | fe078f9 | 2021-07-19 20:02:59 | [diff] [blame] | 1880 | options.enable_views_discouraged = true; |
Victor Costan | 8ec18ee4 | 2021-07-13 19:45:32 | [diff] [blame] | 1881 | db_ = std::make_unique<Database>(options); |
| 1882 | ASSERT_TRUE(db_->Open(db_path_)); |
| 1883 | |
Victor Costan | dc72e8d | 2022-03-14 18:14:51 | [diff] [blame] | 1884 | ASSERT_GT(db_->ComputeMmapSizeForOpen(), kMmapAlot); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1885 | ASSERT_FALSE(db_->DoesTableExist("meta")); |
| 1886 | ASSERT_TRUE(db_->DoesViewExist("MmapStatus")); |
Raul Tambre | 6c708e3 | 2019-02-08 22:35:14 | [diff] [blame] | 1887 | EXPECT_EQ(base::NumberToString(MetaTable::kMmapSuccess), |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1888 | ExecuteWithResult(db_.get(), "SELECT * FROM MmapStatus")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1889 | |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1890 | // Also maps everything when kMmapSuccess is already in the view. |
Victor Costan | dc72e8d | 2022-03-14 18:14:51 | [diff] [blame] | 1891 | ASSERT_GT(db_->ComputeMmapSizeForOpen(), kMmapAlot); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1892 | |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1893 | // Preload with partial progress of one page. Should map everything. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1894 | ASSERT_TRUE(db_->Execute("DROP VIEW MmapStatus")); |
| 1895 | ASSERT_TRUE(db_->Execute("CREATE VIEW MmapStatus (value) AS SELECT 1")); |
Victor Costan | dc72e8d | 2022-03-14 18:14:51 | [diff] [blame] | 1896 | ASSERT_GT(db_->ComputeMmapSizeForOpen(), kMmapAlot); |
Raul Tambre | 6c708e3 | 2019-02-08 22:35:14 | [diff] [blame] | 1897 | EXPECT_EQ(base::NumberToString(MetaTable::kMmapSuccess), |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1898 | ExecuteWithResult(db_.get(), "SELECT * FROM MmapStatus")); |
shess | a7b07acd | 2017-03-19 23:59:38 | [diff] [blame] | 1899 | |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1900 | // Failure status leads to nothing being mapped. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1901 | ASSERT_TRUE(db_->Execute("DROP VIEW MmapStatus")); |
| 1902 | ASSERT_TRUE(db_->Execute("CREATE VIEW MmapStatus (value) AS SELECT -2")); |
Victor Costan | dc72e8d | 2022-03-14 18:14:51 | [diff] [blame] | 1903 | ASSERT_EQ(0UL, db_->ComputeMmapSizeForOpen()); |
Raul Tambre | 6c708e3 | 2019-02-08 22:35:14 | [diff] [blame] | 1904 | EXPECT_EQ(base::NumberToString(MetaTable::kMmapFailure), |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1905 | ExecuteWithResult(db_.get(), "SELECT * FROM MmapStatus")); |
shess | a62504d | 2016-11-07 19:26:12 | [diff] [blame] | 1906 | } |
| 1907 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 1908 | TEST_P(SQLDatabaseTest, GetMemoryUsage) { |
Victor Costan | d6e7325 | 2020-10-14 21:11:25 | [diff] [blame] | 1909 | // Databases with mmap enabled may not follow the assumptions below. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1910 | db_->Close(); |
| 1911 | db_->set_mmap_disabled(); |
| 1912 | ASSERT_TRUE(db_->Open(db_path_)); |
Victor Costan | d6e7325 | 2020-10-14 21:11:25 | [diff] [blame] | 1913 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1914 | int initial_memory = db_->GetMemoryUsage(); |
Victor Costan | d6e7325 | 2020-10-14 21:11:25 | [diff] [blame] | 1915 | EXPECT_GT(initial_memory, 0) |
| 1916 | << "SQLite should always use some memory for a database"; |
| 1917 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1918 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)")); |
| 1919 | ASSERT_TRUE(db_->Execute("INSERT INTO foo(a, b) VALUES (12, 13)")); |
Victor Costan | d6e7325 | 2020-10-14 21:11:25 | [diff] [blame] | 1920 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1921 | int post_query_memory = db_->GetMemoryUsage(); |
Victor Costan | d6e7325 | 2020-10-14 21:11:25 | [diff] [blame] | 1922 | EXPECT_GT(post_query_memory, initial_memory) |
| 1923 | << "Page cache usage should go up after executing queries"; |
| 1924 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 1925 | db_->TrimMemory(); |
| 1926 | int post_trim_memory = db_->GetMemoryUsage(); |
Victor Costan | d6e7325 | 2020-10-14 21:11:25 | [diff] [blame] | 1927 | EXPECT_GT(post_query_memory, post_trim_memory) |
| 1928 | << "Page cache usage should go down after calling TrimMemory()"; |
| 1929 | } |
| 1930 | |
Victor Costan | 4e442d0 | 2021-07-20 17:43:13 | [diff] [blame] | 1931 | TEST_P(SQLDatabaseTest, DoubleQuotedStringLiteralsDisabledByDefault) { |
| 1932 | ASSERT_TRUE(db_->Execute("CREATE TABLE data(item TEXT NOT NULL);")); |
| 1933 | |
| 1934 | struct TestCase { |
| 1935 | const char* sql; |
| 1936 | bool is_valid; |
| 1937 | }; |
| 1938 | std::vector<TestCase> test_cases = { |
| 1939 | // DML tests. |
| 1940 | {"SELECT item FROM data WHERE item >= 'string literal'", true}, |
| 1941 | {"SELECT item FROM data WHERE item >= \"string literal\"", false}, |
| 1942 | {"INSERT INTO data(item) VALUES('string literal')", true}, |
| 1943 | {"INSERT INTO data(item) VALUES(\"string literal\")", false}, |
| 1944 | {"UPDATE data SET item = 'string literal'", true}, |
| 1945 | {"UPDATE data SET item = \"string literal\"", false}, |
| 1946 | {"DELETE FROM data WHERE item >= 'string literal'", true}, |
| 1947 | {"DELETE FROM data WHERE item >= \"string literal\"", false}, |
| 1948 | |
| 1949 | // DDL tests. |
| 1950 | {"CREATE INDEX data_item ON data(item) WHERE item >= 'string literal'", |
| 1951 | true}, |
| 1952 | {"CREATE INDEX data_item ON data(item) WHERE item >= \"string literal\"", |
| 1953 | false}, |
| 1954 | {"CREATE TABLE data2(item TEXT DEFAULT 'string literal')", true}, |
| 1955 | |
| 1956 | // This should be an invalid DDL statement, due to the double-quoted |
| 1957 | // string literal. However, SQLite currently parses it. |
| 1958 | {"CREATE TABLE data2(item TEXT DEFAULT \"string literal\")", true}, |
| 1959 | }; |
| 1960 | |
| 1961 | for (const TestCase& test_case : test_cases) { |
| 1962 | SCOPED_TRACE(test_case.sql); |
| 1963 | |
| 1964 | EXPECT_EQ(test_case.is_valid, db_->IsSQLValid(test_case.sql)); |
| 1965 | } |
| 1966 | } |
| 1967 | |
Victor Costan | 54d7138 | 2022-03-21 21:47:18 | [diff] [blame] | 1968 | TEST_P(SQLDatabaseTest, ForeignKeyEnforcementDisabledByDefault) { |
Victor Costan | 54d7138 | 2022-03-21 21:47:18 | [diff] [blame] | 1969 | ASSERT_TRUE(db_->Execute("CREATE TABLE targets(id INTEGER PRIMARY KEY)")); |
| 1970 | // sqlite3_db_config() currently only disables foreign key enforcement. Schema |
| 1971 | // operations on foreign keys are still allowed. |
| 1972 | ASSERT_TRUE( |
| 1973 | db_->Execute("CREATE TABLE refs(" |
| 1974 | "id INTEGER PRIMARY KEY," |
| 1975 | "target_id INTEGER REFERENCES targets(id))")); |
| 1976 | |
| 1977 | ASSERT_TRUE(db_->Execute("INSERT INTO targets(id) VALUES(42)")); |
| 1978 | ASSERT_TRUE(db_->Execute("INSERT INTO refs(id, target_id) VALUES(42, 42)")); |
| 1979 | |
| 1980 | EXPECT_TRUE(db_->Execute("DELETE FROM targets WHERE id=42")) |
| 1981 | << "Foreign key enforcement is not disabled"; |
| 1982 | } |
| 1983 | |
Victor Costan | 7c23482 | 2021-07-13 03:03:02 | [diff] [blame] | 1984 | TEST_P(SQLDatabaseTest, TriggersDisabledByDefault) { |
| 1985 | ASSERT_TRUE(db_->Execute("CREATE TABLE data(id INTEGER)")); |
| 1986 | |
| 1987 | // sqlite3_db_config() currently only disables running triggers. Schema |
| 1988 | // operations on triggers are still allowed. |
| 1989 | EXPECT_TRUE( |
| 1990 | db_->Execute("CREATE TRIGGER trigger AFTER INSERT ON data " |
| 1991 | "BEGIN DELETE FROM data; END")); |
| 1992 | |
| 1993 | ASSERT_TRUE(db_->Execute("INSERT INTO data(id) VALUES(42)")); |
| 1994 | |
| 1995 | Statement select(db_->GetUniqueStatement("SELECT id FROM data")); |
| 1996 | EXPECT_TRUE(select.Step()) |
| 1997 | << "If the trigger did not run, the table should not be empty."; |
| 1998 | EXPECT_EQ(42, select.ColumnInt64(0)); |
| 1999 | |
| 2000 | // sqlite3_db_config() currently only disables running triggers. Schema |
| 2001 | // operations on triggers are still allowed. |
| 2002 | EXPECT_TRUE(db_->Execute("DROP TRIGGER IF EXISTS trigger")); |
| 2003 | } |
| 2004 | |
Etienne Bergeron | 14c5d71 | 2023-11-13 15:01:15 | [diff] [blame] | 2005 | // This test ensures that a database can be open/create with a journal mode and |
| 2006 | // can be re-open later with a different journal mode. |
| 2007 | TEST_P(SQLDatabaseTest, ReOpenWithDifferentJournalMode) { |
| 2008 | const bool is_wal = IsWALEnabled(); |
| 2009 | const base::FilePath journal_path = Database::JournalPath(db_path_); |
| 2010 | const base::FilePath wal_path = Database::WriteAheadLogPath(db_path_); |
| 2011 | |
| 2012 | ASSERT_TRUE(db_->Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)")); |
| 2013 | ASSERT_TRUE(db_->Execute("INSERT INTO foo (value) VALUES (12)")); |
| 2014 | |
| 2015 | // Last insert row ID should be valid. |
| 2016 | int64_t row = db_->GetLastInsertRowId(); |
| 2017 | EXPECT_LT(0, row); |
| 2018 | |
| 2019 | // It should be the primary key of the row we just inserted. |
| 2020 | { |
| 2021 | Statement s(db_->GetUniqueStatement("SELECT value FROM foo WHERE id=?")); |
| 2022 | s.BindInt64(0, row); |
| 2023 | ASSERT_TRUE(s.Step()); |
| 2024 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 2025 | } |
| 2026 | |
| 2027 | // Ensure appropriate journal mode and the journal file exists. |
| 2028 | EXPECT_TRUE(IsOpenedInCorrectJournalMode(db_.get(), is_wal)); |
| 2029 | EXPECT_EQ(base::PathExists(wal_path), is_wal); |
| 2030 | |
| 2031 | db_->Close(); |
| 2032 | if (is_wal) { |
| 2033 | // The WAL journal file is removed on database close. Database that enable |
| 2034 | // WAL mode can use a different journal mode on a subsequent database open. |
| 2035 | EXPECT_FALSE(base::PathExists(wal_path)); |
| 2036 | } else { |
| 2037 | // The Rollback journal should have a zero size when pending operations |
| 2038 | // are completed. |
| 2039 | int64_t journal_size = 0; |
| 2040 | base::GetFileSize(journal_path, &journal_size); |
| 2041 | EXPECT_EQ(journal_size, 0); |
| 2042 | } |
| 2043 | |
| 2044 | // Re-open the database with a different mode (Rollback vs WAL). |
| 2045 | DatabaseOptions options = GetDBOptions(); |
| 2046 | options.wal_mode = !is_wal; |
| 2047 | #if BUILDFLAG(IS_FUCHSIA) |
| 2048 | // Exclusive mode needs to be enabled to enter WAL mode on Fuchsia. |
| 2049 | if (options.wal_mode) { |
| 2050 | options.exclusive_locking = true; |
| 2051 | } |
| 2052 | #endif // BUILDFLAG(IS_FUCHSIA) |
| 2053 | |
| 2054 | db_ = std::make_unique<Database>(options); |
| 2055 | ASSERT_TRUE(db_->Open(db_path_)); |
| 2056 | |
| 2057 | // The value for the last inserted row should be valid. |
| 2058 | { |
| 2059 | Statement s(db_->GetUniqueStatement("SELECT value FROM foo WHERE id=?")); |
| 2060 | s.BindInt64(0, row); |
| 2061 | ASSERT_TRUE(s.Step()); |
| 2062 | EXPECT_EQ(12, s.ColumnInt(0)); |
| 2063 | } |
| 2064 | |
| 2065 | // Ensure appropriate journal file exists. |
| 2066 | EXPECT_TRUE(IsOpenedInCorrectJournalMode(db_.get(), options.wal_mode)); |
| 2067 | EXPECT_EQ(base::PathExists(wal_path), options.wal_mode); |
| 2068 | } |
| 2069 | |
Will Harris | 711a5ec | 2023-04-04 21:43:37 | [diff] [blame] | 2070 | #if BUILDFLAG(IS_WIN) |
| 2071 | |
| 2072 | class SQLDatabaseTestExclusiveFileLockMode |
| 2073 | : public testing::Test, |
| 2074 | public testing::WithParamInterface<::testing::tuple<bool, bool>> { |
| 2075 | public: |
| 2076 | ~SQLDatabaseTestExclusiveFileLockMode() override = default; |
| 2077 | |
| 2078 | void SetUp() override { |
| 2079 | db_ = std::make_unique<Database>(GetDBOptions()); |
| 2080 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 2081 | db_path_ = temp_dir_.GetPath().AppendASCII("maybelocked.sqlite"); |
| 2082 | ASSERT_TRUE(db_->Open(db_path_)); |
| 2083 | } |
| 2084 | |
| 2085 | DatabaseOptions GetDBOptions() { |
| 2086 | DatabaseOptions options; |
| 2087 | options.wal_mode = IsWALEnabled(); |
| 2088 | options.exclusive_locking = true; |
| 2089 | options.exclusive_database_file_lock = IsExclusivelockEnabled(); |
| 2090 | return options; |
| 2091 | } |
| 2092 | |
| 2093 | bool IsWALEnabled() { return std::get<0>(GetParam()); } |
| 2094 | bool IsExclusivelockEnabled() { return std::get<1>(GetParam()); } |
| 2095 | |
| 2096 | protected: |
| 2097 | base::ScopedTempDir temp_dir_; |
| 2098 | base::FilePath db_path_; |
| 2099 | std::unique_ptr<Database> db_; |
| 2100 | }; |
| 2101 | |
| 2102 | TEST_P(SQLDatabaseTestExclusiveFileLockMode, BasicStatement) { |
| 2103 | ASSERT_TRUE(db_->Execute("CREATE TABLE data(contents TEXT)")); |
| 2104 | EXPECT_EQ(SQLITE_OK, db_->GetErrorCode()); |
| 2105 | |
| 2106 | ASSERT_TRUE(base::PathExists(db_path_)); |
| 2107 | base::File open_db(db_path_, base::File::Flags::FLAG_OPEN_ALWAYS | |
| 2108 | base::File::Flags::FLAG_READ); |
| 2109 | |
| 2110 | // If exclusive lock is enabled, then the test should not be able to re-open |
| 2111 | // the database file, on Windows only. |
| 2112 | EXPECT_EQ(IsExclusivelockEnabled(), !open_db.IsValid()); |
| 2113 | } |
| 2114 | |
| 2115 | INSTANTIATE_TEST_SUITE_P( |
| 2116 | All, |
| 2117 | SQLDatabaseTestExclusiveFileLockMode, |
| 2118 | ::testing::Combine(::testing::Bool(), ::testing::Bool()), |
| 2119 | [](const auto& info) { |
| 2120 | return base::StrCat( |
| 2121 | {std::get<0>(info.param) ? "WALEnabled" : "WALDisabled", |
| 2122 | std::get<1>(info.param) ? "ExclusiveLock" : "NoExclusiveLock"}); |
| 2123 | }); |
| 2124 | |
| 2125 | #else |
| 2126 | |
| 2127 | TEST(SQLInvalidDatabaseFlagsDeathTest, ExclusiveDatabaseLock) { |
| 2128 | base::ScopedTempDir temp_dir; |
| 2129 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 2130 | auto db_path = temp_dir.GetPath().AppendASCII("database_test_locked.sqlite"); |
| 2131 | |
| 2132 | Database db({.exclusive_database_file_lock = true}); |
| 2133 | |
| 2134 | EXPECT_CHECK_DEATH_WITH( |
| 2135 | { std::ignore = db.Open(db_path); }, |
| 2136 | "exclusive_database_file_lock is only supported on Windows"); |
| 2137 | } |
| 2138 | |
| 2139 | #endif // BUILDFLAG(IS_WIN) |
| 2140 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2141 | class SQLDatabaseTestExclusiveMode : public testing::Test, |
| 2142 | public testing::WithParamInterface<bool> { |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 2143 | public: |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2144 | ~SQLDatabaseTestExclusiveMode() override = default; |
| 2145 | |
| 2146 | void SetUp() override { |
| 2147 | db_ = std::make_unique<Database>(GetDBOptions()); |
| 2148 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 2149 | db_path_ = temp_dir_.GetPath().AppendASCII("recovery_test.sqlite"); |
| 2150 | ASSERT_TRUE(db_->Open(db_path_)); |
| 2151 | } |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 2152 | |
| 2153 | DatabaseOptions GetDBOptions() { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2154 | DatabaseOptions options; |
| 2155 | options.wal_mode = IsWALEnabled(); |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 2156 | options.exclusive_locking = true; |
| 2157 | return options; |
| 2158 | } |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2159 | |
| 2160 | bool IsWALEnabled() { return GetParam(); } |
| 2161 | |
| 2162 | protected: |
| 2163 | base::ScopedTempDir temp_dir_; |
| 2164 | base::FilePath db_path_; |
| 2165 | std::unique_ptr<Database> db_; |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 2166 | }; |
Victor Costan | b223079 | 2020-10-09 08:35:14 | [diff] [blame] | 2167 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 2168 | TEST_P(SQLDatabaseTestExclusiveMode, LockingModeExclusive) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2169 | EXPECT_EQ(ExecuteWithResult(db_.get(), "PRAGMA locking_mode"), "exclusive"); |
Victor Costan | b223079 | 2020-10-09 08:35:14 | [diff] [blame] | 2170 | } |
| 2171 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 2172 | TEST_P(SQLDatabaseTest, LockingModeNormal) { |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2173 | EXPECT_EQ(ExecuteWithResult(db_.get(), "PRAGMA locking_mode"), "normal"); |
Victor Costan | b223079 | 2020-10-09 08:35:14 | [diff] [blame] | 2174 | } |
| 2175 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 2176 | TEST_P(SQLDatabaseTest, OpenedInCorrectMode) { |
Etienne Bergeron | 14c5d71 | 2023-11-13 15:01:15 | [diff] [blame] | 2177 | EXPECT_TRUE(IsOpenedInCorrectJournalMode(db_.get(), IsWALEnabled())); |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 2178 | } |
| 2179 | |
Shubham Aggarwal | 00370898 | 2020-10-28 17:57:54 | [diff] [blame] | 2180 | TEST_P(SQLDatabaseTest, CheckpointDatabase) { |
Shubham Aggarwal | 7b60fe6e | 2020-10-15 06:00:28 | [diff] [blame] | 2181 | if (!IsWALEnabled()) |
| 2182 | return; |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 2183 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2184 | base::FilePath wal_path = Database::WriteAheadLogPath(db_path_); |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 2185 | |
| 2186 | int64_t wal_size = 0; |
| 2187 | // WAL file initially empty. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2188 | EXPECT_TRUE(base::PathExists(wal_path)); |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 2189 | base::GetFileSize(wal_path, &wal_size); |
| 2190 | EXPECT_EQ(wal_size, 0); |
| 2191 | |
| 2192 | ASSERT_TRUE( |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2193 | db_->Execute("CREATE TABLE foo (id INTEGER UNIQUE, value INTEGER)")); |
| 2194 | ASSERT_TRUE(db_->Execute("INSERT INTO foo VALUES (1, 1)")); |
| 2195 | ASSERT_TRUE(db_->Execute("INSERT INTO foo VALUES (2, 2)")); |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 2196 | |
| 2197 | // Writes reach WAL file but not db file. |
| 2198 | base::GetFileSize(wal_path, &wal_size); |
| 2199 | EXPECT_GT(wal_size, 0); |
| 2200 | |
| 2201 | int64_t db_size = 0; |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2202 | base::GetFileSize(db_path_, &db_size); |
| 2203 | EXPECT_EQ(db_size, db_->page_size()); |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 2204 | |
| 2205 | // Checkpoint database to immediately propagate writes to DB file. |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2206 | EXPECT_TRUE(db_->CheckpointDatabase()); |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 2207 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2208 | base::GetFileSize(db_path_, &db_size); |
| 2209 | EXPECT_GT(db_size, db_->page_size()); |
| 2210 | EXPECT_EQ(ExecuteWithResult(db_.get(), "SELECT value FROM foo where id=1"), |
| 2211 | "1"); |
| 2212 | EXPECT_EQ(ExecuteWithResult(db_.get(), "SELECT value FROM foo where id=2"), |
| 2213 | "2"); |
Shubham Aggarwal | be4f97c | 2020-06-19 15:58:57 | [diff] [blame] | 2214 | } |
| 2215 | |
Victor Costan | 3c80271 | 2022-02-24 17:33:06 | [diff] [blame] | 2216 | TEST_P(SQLDatabaseTest, OpenFailsAfterCorruptSizeInHeader) { |
| 2217 | // The database file ends up empty if we don't create at least one table. |
| 2218 | ASSERT_TRUE( |
| 2219 | db_->Execute("CREATE TABLE rows(i INTEGER PRIMARY KEY NOT NULL)")); |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2220 | db_->Close(); |
Shubham Aggarwal | 1c06850ffa | 2020-07-07 13:42:57 | [diff] [blame] | 2221 | |
Victor Costan | 49a903a | 2021-05-07 22:21:00 | [diff] [blame] | 2222 | ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_)); |
Shubham Aggarwal | 1c06850ffa | 2020-07-07 13:42:57 | [diff] [blame] | 2223 | { |
| 2224 | sql::test::ScopedErrorExpecter expecter; |
| 2225 | expecter.ExpectError(SQLITE_CORRUPT); |
Dan McArdle | 4feabeb5 | 2024-01-03 15:17:13 | [diff] [blame] | 2226 | ASSERT_FALSE(db_->Open(db_path_)); |
Shubham Aggarwal | 1c06850ffa | 2020-07-07 13:42:57 | [diff] [blame] | 2227 | EXPECT_TRUE(expecter.SawExpectedErrors()); |
| 2228 | } |
| 2229 | } |
| 2230 | |
Dan McArdle | 4feabeb5 | 2024-01-03 15:17:13 | [diff] [blame] | 2231 | TEST_P(SQLDatabaseTest, OpenWithRecoveryHandlesCorruption) { |
| 2232 | for (const bool corrupt_after_recovery : {false, true}) { |
| 2233 | SCOPED_TRACE(::testing::Message() |
| 2234 | << "corrupt_after_recovery: " << corrupt_after_recovery); |
| 2235 | // Ensure that `db_` is fresh in this iteration. |
| 2236 | CreateFreshDB(); |
| 2237 | // The database file ends up empty if we don't create at least one table. |
| 2238 | ASSERT_TRUE( |
| 2239 | db_->Execute("CREATE TABLE rows(i INTEGER PRIMARY KEY NOT NULL)")); |
| 2240 | db_->Close(); |
| 2241 | |
| 2242 | ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_)); |
| 2243 | |
| 2244 | size_t error_count = 0; |
| 2245 | auto callback = base::BindLambdaForTesting([&](int error, Statement* stmt) { |
| 2246 | error_count++; |
| 2247 | ASSERT_TRUE(BuiltInRecovery::RecoverIfPossible( |
| 2248 | db_.get(), error, sql::BuiltInRecovery::Strategy::kRecoverOrRaze)); |
| 2249 | if (corrupt_after_recovery) { |
| 2250 | // Corrupt the file again after temporarily recovering it. |
| 2251 | ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_)); |
| 2252 | } |
| 2253 | }); |
| 2254 | db_->set_error_callback(std::move(callback)); |
| 2255 | |
| 2256 | { |
| 2257 | sql::test::ScopedErrorExpecter expecter; |
| 2258 | expecter.ExpectError(SQLITE_CORRUPT); |
| 2259 | |
| 2260 | // When `corrupt_after_recovery` is true, `Database::Open()` will return |
| 2261 | // false because both attempts at opening the database will fail. When the |
| 2262 | // database is *not* corrupted after recovery, recovery will succeed and |
| 2263 | // thus `Database::Open()`'s second attempt at opening the database will |
| 2264 | // succeed. |
| 2265 | ASSERT_EQ(db_->Open(db_path_), !corrupt_after_recovery); |
| 2266 | EXPECT_TRUE(expecter.SawExpectedErrors()); |
| 2267 | } |
| 2268 | EXPECT_EQ(error_count, 1u); |
| 2269 | EXPECT_FALSE(db_->has_error_callback()); |
| 2270 | } |
| 2271 | } |
| 2272 | |
Victor Costan | 3c80271 | 2022-02-24 17:33:06 | [diff] [blame] | 2273 | TEST_P(SQLDatabaseTest, ExecuteFailsAfterCorruptSizeInHeader) { |
| 2274 | ASSERT_TRUE( |
| 2275 | db_->Execute("CREATE TABLE rows(i INTEGER PRIMARY KEY NOT NULL)")); |
| 2276 | constexpr static char kSelectSql[] = "SELECT * from rows"; |
| 2277 | EXPECT_TRUE(db_->Execute(kSelectSql)) |
| 2278 | << "The test Execute() statement fails before the header is corrupted"; |
| 2279 | db_->Close(); |
| 2280 | |
| 2281 | ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_)); |
| 2282 | { |
| 2283 | sql::test::ScopedErrorExpecter expecter; |
| 2284 | expecter.ExpectError(SQLITE_CORRUPT); |
Dan McArdle | 4feabeb5 | 2024-01-03 15:17:13 | [diff] [blame] | 2285 | ASSERT_FALSE(db_->Open(db_path_)); |
Victor Costan | 3c80271 | 2022-02-24 17:33:06 | [diff] [blame] | 2286 | EXPECT_TRUE(expecter.SawExpectedErrors()) |
| 2287 | << "Database::Open() did not encounter SQLITE_CORRUPT"; |
| 2288 | } |
| 2289 | { |
| 2290 | sql::test::ScopedErrorExpecter expecter; |
| 2291 | expecter.ExpectError(SQLITE_CORRUPT); |
| 2292 | EXPECT_FALSE(db_->Execute(kSelectSql)); |
| 2293 | EXPECT_TRUE(expecter.SawExpectedErrors()) |
| 2294 | << "Database::Execute() did not encounter SQLITE_CORRUPT"; |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | TEST_P(SQLDatabaseTest, SchemaFailsAfterCorruptSizeInHeader) { |
| 2299 | ASSERT_TRUE( |
| 2300 | db_->Execute("CREATE TABLE rows(i INTEGER PRIMARY KEY NOT NULL)")); |
| 2301 | ASSERT_TRUE(db_->DoesTableExist("rows")) |
| 2302 | << "The test schema check fails before the header is corrupted"; |
| 2303 | db_->Close(); |
| 2304 | |
| 2305 | ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_)); |
| 2306 | { |
| 2307 | sql::test::ScopedErrorExpecter expecter; |
| 2308 | expecter.ExpectError(SQLITE_CORRUPT); |
Dan McArdle | 4feabeb5 | 2024-01-03 15:17:13 | [diff] [blame] | 2309 | ASSERT_FALSE(db_->Open(db_path_)); |
Victor Costan | 3c80271 | 2022-02-24 17:33:06 | [diff] [blame] | 2310 | EXPECT_TRUE(expecter.SawExpectedErrors()) |
| 2311 | << "Database::Open() did not encounter SQLITE_CORRUPT"; |
| 2312 | } |
| 2313 | { |
| 2314 | sql::test::ScopedErrorExpecter expecter; |
| 2315 | expecter.ExpectError(SQLITE_CORRUPT); |
| 2316 | EXPECT_FALSE(db_->DoesTableExist("rows")); |
| 2317 | EXPECT_TRUE(expecter.SawExpectedErrors()) |
| 2318 | << "Database::DoesTableExist() did not encounter SQLITE_CORRUPT"; |
| 2319 | } |
| 2320 | } |
| 2321 | |
a20.singh | b0ca112 | 2022-05-26 03:32:45 | [diff] [blame] | 2322 | TEST(SQLEmptyPathDatabaseTest, EmptyPathTest) { |
| 2323 | Database db; |
| 2324 | EXPECT_TRUE(db.OpenInMemory()); |
| 2325 | EXPECT_TRUE(db.is_open()); |
| 2326 | EXPECT_TRUE(db.DbPath().empty()); |
| 2327 | } |
| 2328 | |
Victor Costan | 15d905de | 2021-01-07 22:18:58 | [diff] [blame] | 2329 | // WAL mode is currently not supported on Fuchsia. |
Xiaohan Wang | 7d09c5e | 2022-01-08 02:37:36 | [diff] [blame] | 2330 | #if !BUILDFLAG(IS_FUCHSIA) |
Victor Costan | 15d905de | 2021-01-07 22:18:58 | [diff] [blame] | 2331 | INSTANTIATE_TEST_SUITE_P(JournalMode, SQLDatabaseTest, testing::Bool()); |
| 2332 | INSTANTIATE_TEST_SUITE_P(JournalMode, |
| 2333 | SQLDatabaseTestExclusiveMode, |
| 2334 | testing::Bool()); |
| 2335 | #else |
| 2336 | INSTANTIATE_TEST_SUITE_P(JournalMode, SQLDatabaseTest, testing::Values(false)); |
| 2337 | INSTANTIATE_TEST_SUITE_P(JournalMode, |
| 2338 | SQLDatabaseTestExclusiveMode, |
| 2339 | testing::Values(false)); |
| 2340 | #endif |
shess | c8cd2a16 | 2015-10-22 20:30:46 | [diff] [blame] | 2341 | } // namespace sql |