blob: c5805ebaa8f5921c7ec3f0f4ec4f27937167aa29 [file] [log] [blame]
[email protected]44ad7d902012-03-23 00:09:051// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Victor Costan49a903a2021-05-07 22:21:005#include "sql/database.h"
6
avi0b519202015-12-21 07:25:197#include <stddef.h>
8#include <stdint.h>
Victor Costan55309322021-07-19 17:58:429#include <cstdint>
avi0b519202015-12-21 07:25:1910
[email protected]7bae5742013-07-10 20:46:1611#include "base/bind.h"
thestig22dfc4012014-09-05 08:29:4412#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:2313#include "base/files/scoped_temp_dir.h"
[email protected]41a97c812013-02-07 02:35:3814#include "base/logging.h"
shess7e2baba2016-10-27 23:46:0515#include "base/strings/string_number_conversions.h"
Victor Costanc02b7af2021-07-09 13:26:4516#include "base/test/bind.h"
Victor Costan613b4302018-11-20 05:32:4317#include "base/test/gtest_util.h"
Devlin Cronin147687f2018-06-05 18:03:5618#include "base/test/metrics/histogram_tester.h"
ssid9f8022f2015-10-12 17:49:0319#include "base/trace_event/process_memory_dump.h"
Scott Graham57ee54822017-09-13 06:37:5620#include "build/build_config.h"
Victor Costancfbfa602018-08-01 23:24:4621#include "sql/database_memory_dump_provider.h"
[email protected]1348765a2012-07-24 08:25:5322#include "sql/meta_table.h"
Shubham Aggarwalbe4f97ce2020-06-19 15:58:5723#include "sql/sql_features.h"
[email protected]ea1a3f62012-11-16 20:34:2324#include "sql/statement.h"
Victor Costand1a217b2019-04-02 02:44:2125#include "sql/test/database_test_peer.h"
[email protected]98cf3002013-07-12 01:38:5626#include "sql/test/error_callback_support.h"
shess976814402016-06-21 06:56:2527#include "sql/test/scoped_error_expecter.h"
[email protected]a8848a72013-11-18 04:18:4728#include "sql/test/test_helpers.h"
[email protected]e5ffd0e42009-09-11 21:30:5629#include "testing/gtest/include/gtest/gtest.h"
[email protected]e33cba42010-08-18 23:37:0330#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5631
shess58b8df82015-06-03 00:19:3232namespace sql {
Victor Costan7f6abbbe2018-07-29 02:57:2733
[email protected]7bae5742013-07-10 20:46:1634namespace {
35
shess7e2baba2016-10-27 23:46:0536using sql::test::ExecuteWithResult;
37
[email protected]7bae5742013-07-10 20:46:1638// Helper to return the count of items in sqlite_master. Return -1 in
39// case of error.
Victor Costan49a903a2021-05-07 22:21:0040int SqliteMasterCount(Database* db) {
[email protected]7bae5742013-07-10 20:46:1641 const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master";
Victor Costan49a903a2021-05-07 22:21:0042 Statement s(db->GetUniqueStatement(kMasterCount));
[email protected]7bae5742013-07-10 20:46:1643 return s.Step() ? s.ColumnInt(0) : -1;
44}
45
[email protected]98cf3002013-07-12 01:38:5646// Track the number of valid references which share the same pointer.
47// This is used to allow testing an implicitly use-after-free case by
48// explicitly having the ref count live longer than the object.
49class RefCounter {
50 public:
Victor Costan49a903a2021-05-07 22:21:0051 explicit RefCounter(size_t* counter) : counter_(counter) { (*counter_)++; }
Victor Costancfbfa602018-08-01 23:24:4652 RefCounter(const RefCounter& other) : counter_(other.counter_) {
[email protected]98cf3002013-07-12 01:38:5653 (*counter_)++;
54 }
Victor Costan00c76432021-07-07 16:55:5855 RefCounter& operator=(const RefCounter&) = delete;
Victor Costancfbfa602018-08-01 23:24:4656 ~RefCounter() { (*counter_)--; }
[email protected]98cf3002013-07-12 01:38:5657
58 private:
59 size_t* counter_;
[email protected]98cf3002013-07-12 01:38:5660};
61
62// Empty callback for implementation of ErrorCallbackSetHelper().
Victor Costan49a903a2021-05-07 22:21:0063void IgnoreErrorCallback(int error, Statement* stmt) {}
[email protected]98cf3002013-07-12 01:38:5664
Victor Costan49a903a2021-05-07 22:21:0065void ErrorCallbackSetHelper(Database* db,
[email protected]98cf3002013-07-12 01:38:5666 size_t* counter,
67 const RefCounter& r,
Victor Costancfbfa602018-08-01 23:24:4668 int error,
Victor Costan49a903a2021-05-07 22:21:0069 Statement* stmt) {
[email protected]98cf3002013-07-12 01:38:5670 // The ref count should not go to zero when changing the callback.
71 EXPECT_GT(*counter, 0u);
tzikd16d2192018-03-07 08:58:3672 db->set_error_callback(base::BindRepeating(&IgnoreErrorCallback));
[email protected]98cf3002013-07-12 01:38:5673 EXPECT_GT(*counter, 0u);
74}
75
Victor Costan49a903a2021-05-07 22:21:0076void ErrorCallbackResetHelper(Database* db,
[email protected]98cf3002013-07-12 01:38:5677 size_t* counter,
78 const RefCounter& r,
Victor Costancfbfa602018-08-01 23:24:4679 int error,
Victor Costan49a903a2021-05-07 22:21:0080 Statement* stmt) {
[email protected]98cf3002013-07-12 01:38:5681 // The ref count should not go to zero when clearing the callback.
82 EXPECT_GT(*counter, 0u);
83 db->reset_error_callback();
84 EXPECT_GT(*counter, 0u);
85}
86
shess1cf87f22016-10-25 22:18:2987// Handle errors by blowing away the database.
Victor Costan49a903a2021-05-07 22:21:0088void RazeErrorCallback(Database* db,
shess1cf87f22016-10-25 22:18:2989 int expected_error,
90 int error,
Victor Costan49a903a2021-05-07 22:21:0091 Statement* stmt) {
shess1cf87f22016-10-25 22:18:2992 // Nothing here needs extended errors at this time.
Victor Costancfbfa602018-08-01 23:24:4693 EXPECT_EQ(expected_error, expected_error & 0xff);
94 EXPECT_EQ(expected_error, error & 0xff);
shess1cf87f22016-10-25 22:18:2995 db->RazeAndClose();
96}
97
[email protected]81a2a602013-07-17 19:10:3698#if defined(OS_POSIX)
99// Set a umask and restore the old mask on destruction. Cribbed from
100// shared_memory_unittest.cc. Used by POSIX-only UserPermission test.
101class ScopedUmaskSetter {
102 public:
103 explicit ScopedUmaskSetter(mode_t target_mask) {
104 old_umask_ = umask(target_mask);
105 }
106 ~ScopedUmaskSetter() { umask(old_umask_); }
Victor Costancfbfa602018-08-01 23:24:46107
Victor Costan49a903a2021-05-07 22:21:00108 ScopedUmaskSetter(const ScopedUmaskSetter&) = delete;
109 ScopedUmaskSetter& operator=(const ScopedUmaskSetter&) = delete;
110
[email protected]81a2a602013-07-17 19:10:36111 private:
112 mode_t old_umask_;
[email protected]81a2a602013-07-17 19:10:36113};
Victor Costan8a87f7e52017-11-10 01:29:30114#endif // defined(OS_POSIX)
[email protected]81a2a602013-07-17 19:10:36115
shessc8cd2a162015-10-22 20:30:46116} // namespace
117
Shubham Aggarwal003708982020-10-28 17:57:54118// We use the parameter to run all tests with WAL mode on and off.
Victor Costan49a903a2021-05-07 22:21:00119class SQLDatabaseTest : public testing::Test,
Shubham Aggarwal003708982020-10-28 17:57:54120 public testing::WithParamInterface<bool> {
121 public:
Victor Costan49a903a2021-05-07 22:21:00122 enum class OverwriteType {
123 kTruncate,
124 kOverwrite,
125 };
[email protected]e5ffd0e42009-09-11 21:30:56126
Victor Costan49a903a2021-05-07 22:21:00127 ~SQLDatabaseTest() override = default;
128
129 void SetUp() override {
130 db_ = std::make_unique<Database>(GetDBOptions());
131 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
132 db_path_ = temp_dir_.GetPath().AppendASCII("database_test.sqlite");
133 ASSERT_TRUE(db_->Open(db_path_));
134 }
135
136 DatabaseOptions GetDBOptions() {
137 DatabaseOptions options;
Shubham Aggarwal003708982020-10-28 17:57:54138 options.wal_mode = IsWALEnabled();
139 // TODO(crbug.com/1120969): Remove after switching to exclusive mode on by
140 // default.
141 options.exclusive_locking = false;
142#if defined(OS_FUCHSIA) // Exclusive mode needs to be enabled to enter WAL mode
143 // on Fuchsia
144 if (IsWALEnabled()) {
145 options.exclusive_locking = true;
146 }
147#endif // defined(OS_FUCHSIA)
148 return options;
149 }
Victor Costan49a903a2021-05-07 22:21:00150
Shubham Aggarwal003708982020-10-28 17:57:54151 bool IsWALEnabled() { return GetParam(); }
Victor Costan49a903a2021-05-07 22:21:00152
153 bool TruncateDatabase() {
154 base::File file(db_path_,
155 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
156 return file.SetLength(0);
157 }
158
159 bool OverwriteDatabaseHeader(OverwriteType type) {
160 base::File file(db_path_,
161 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
162 if (type == OverwriteType::kTruncate) {
163 if (!file.SetLength(0))
164 return false;
165 }
166
167 static constexpr char kText[] = "Now is the winter of our discontent.";
168 constexpr int kTextBytes = sizeof(kText) - 1;
169 return file.Write(0, kText, kTextBytes) == kTextBytes;
170 }
171
172 protected:
173 base::ScopedTempDir temp_dir_;
174 base::FilePath db_path_;
175 std::unique_ptr<Database> db_;
Shubham Aggarwal003708982020-10-28 17:57:54176};
177
Victor Costan289f2c8b2021-07-22 06:33:47178TEST_P(SQLDatabaseTest, Execute_ValidStatement) {
179 ASSERT_TRUE(db_->Execute("CREATE TABLE data(contents TEXT)"));
Victor Costan49a903a2021-05-07 22:21:00180 EXPECT_EQ(SQLITE_OK, db_->GetErrorCode());
Victor Costan289f2c8b2021-07-22 06:33:47181}
[email protected]e5ffd0e42009-09-11 21:30:56182
Victor Costan289f2c8b2021-07-22 06:33:47183TEST_P(SQLDatabaseTest, Execute_InvalidStatement) {
Victor Costan205b96dc2021-07-21 20:27:46184 {
185 sql::test::ScopedErrorExpecter error_expecter;
186 error_expecter.ExpectError(SQLITE_ERROR);
Victor Costan289f2c8b2021-07-22 06:33:47187 EXPECT_FALSE(db_->Execute("CREATE TABLE data("));
Victor Costan205b96dc2021-07-21 20:27:46188 EXPECT_TRUE(error_expecter.SawExpectedErrors());
189 }
Victor Costan49a903a2021-05-07 22:21:00190 EXPECT_EQ(SQLITE_ERROR, db_->GetErrorCode());
[email protected]e5ffd0e42009-09-11 21:30:56191}
192
Victor Costan289f2c8b2021-07-22 06:33:47193TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_OneLineValid) {
194 ASSERT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data(contents TEXT)"));
195 EXPECT_EQ(SQLITE_OK, db_->GetErrorCode());
196}
197
198TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_OneLineInvalid) {
199 ASSERT_FALSE(db_->ExecuteScriptForTesting("CREATE TABLE data("));
200 EXPECT_EQ(SQLITE_ERROR, db_->GetErrorCode());
201}
202
203TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_ExtraContents) {
204 EXPECT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data1(id)"))
205 << "Minimal statement";
206 EXPECT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data2(id);"))
207 << "Extra semicolon";
208 EXPECT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data3(id) -- Comment"))
209 << "Trailing comment";
210
211 EXPECT_TRUE(db_->ExecuteScriptForTesting(
212 "CREATE TABLE data4(id);CREATE TABLE data5(id)"))
213 << "Extra statement without whitespace";
214 EXPECT_TRUE(db_->ExecuteScriptForTesting(
215 "CREATE TABLE data6(id); CREATE TABLE data7(id)"))
216 << "Extra statement separated by whitespace";
217
218 EXPECT_TRUE(db_->ExecuteScriptForTesting("CREATE TABLE data8(id);-- Comment"))
219 << "Comment without whitespace";
220 EXPECT_TRUE(
221 db_->ExecuteScriptForTesting("CREATE TABLE data9(id); -- Comment"))
222 << "Comment sepatated by whitespace";
223}
224
225TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_MultipleValidLines) {
226 EXPECT_TRUE(db_->ExecuteScriptForTesting(R"(
227 CREATE TABLE data1(contents TEXT);
228 CREATE TABLE data2(contents TEXT);
229 CREATE TABLE data3(contents TEXT);
230 )"));
231 EXPECT_EQ(SQLITE_OK, db_->GetErrorCode());
232
233 // DoesColumnExist() is implemented directly on top of a SQLite call. The
234 // other schema functions use sql::Statement infrastructure to query the
235 // schema table.
236 EXPECT_TRUE(db_->DoesColumnExist("data1", "contents"));
237 EXPECT_TRUE(db_->DoesColumnExist("data2", "contents"));
238 EXPECT_TRUE(db_->DoesColumnExist("data3", "contents"));
239}
240
241TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_StopsOnCompileError) {
242 EXPECT_FALSE(db_->ExecuteScriptForTesting(R"(
243 CREATE TABLE data1(contents TEXT);
244 CREATE TABLE data1();
245 CREATE TABLE data3(contents TEXT);
246 )"));
247 EXPECT_EQ(SQLITE_ERROR, db_->GetErrorCode());
248
249 EXPECT_TRUE(db_->DoesColumnExist("data1", "contents"));
250 EXPECT_FALSE(db_->DoesColumnExist("data3", "contents"));
251}
252
253TEST_P(SQLDatabaseTest, ExecuteScriptForTesting_StopsOnStepError) {
254 EXPECT_FALSE(db_->ExecuteScriptForTesting(R"(
255 CREATE TABLE data1(contents TEXT UNIQUE);
256 INSERT INTO data1(contents) VALUES('value1');
257 INSERT INTO data1(contents) VALUES('value1');
258 CREATE TABLE data3(contents TEXT);
259 )"));
260 EXPECT_EQ(SQLITE_CONSTRAINT_UNIQUE, db_->GetErrorCode());
261
262 EXPECT_TRUE(db_->DoesColumnExist("data1", "contents"));
263 EXPECT_FALSE(db_->DoesColumnExist("data3", "contents"));
264}
265
Shubham Aggarwal003708982020-10-28 17:57:54266TEST_P(SQLDatabaseTest, CachedStatement) {
Victor Costan49a903a2021-05-07 22:21:00267 StatementID id1 = SQL_FROM_HERE;
268 StatementID id2 = SQL_FROM_HERE;
Victor Costan613b4302018-11-20 05:32:43269 static const char kId1Sql[] = "SELECT a FROM foo";
Victor Costan87cf8c72018-07-19 19:36:04270 static const char kId2Sql[] = "SELECT b FROM foo";
[email protected]e5ffd0e42009-09-11 21:30:56271
Victor Costan49a903a2021-05-07 22:21:00272 ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)"));
273 ASSERT_TRUE(db_->Execute("INSERT INTO foo(a, b) VALUES (12, 13)"));
[email protected]e5ffd0e42009-09-11 21:30:56274
Victor Costan87cf8c72018-07-19 19:36:04275 sqlite3_stmt* raw_id1_statement;
276 sqlite3_stmt* raw_id2_statement;
[email protected]e5ffd0e42009-09-11 21:30:56277 {
Victor Costan49a903a2021-05-07 22:21:00278 scoped_refptr<Database::StatementRef> ref_from_id1 =
279 db_->GetCachedStatement(id1, kId1Sql);
Victor Costan87cf8c72018-07-19 19:36:04280 raw_id1_statement = ref_from_id1->stmt();
[email protected]e5ffd0e42009-09-11 21:30:56281
Victor Costan49a903a2021-05-07 22:21:00282 Statement from_id1(std::move(ref_from_id1));
Victor Costan87cf8c72018-07-19 19:36:04283 ASSERT_TRUE(from_id1.is_valid());
284 ASSERT_TRUE(from_id1.Step());
285 EXPECT_EQ(12, from_id1.ColumnInt(0));
286
Victor Costan49a903a2021-05-07 22:21:00287 scoped_refptr<Database::StatementRef> ref_from_id2 =
288 db_->GetCachedStatement(id2, kId2Sql);
Victor Costan87cf8c72018-07-19 19:36:04289 raw_id2_statement = ref_from_id2->stmt();
290 EXPECT_NE(raw_id1_statement, raw_id2_statement);
291
Victor Costan49a903a2021-05-07 22:21:00292 Statement from_id2(std::move(ref_from_id2));
Victor Costan87cf8c72018-07-19 19:36:04293 ASSERT_TRUE(from_id2.is_valid());
294 ASSERT_TRUE(from_id2.Step());
295 EXPECT_EQ(13, from_id2.ColumnInt(0));
[email protected]e5ffd0e42009-09-11 21:30:56296 }
297
[email protected]e5ffd0e42009-09-11 21:30:56298 {
Victor Costan49a903a2021-05-07 22:21:00299 scoped_refptr<Database::StatementRef> ref_from_id1 =
300 db_->GetCachedStatement(id1, kId1Sql);
Victor Costan87cf8c72018-07-19 19:36:04301 EXPECT_EQ(raw_id1_statement, ref_from_id1->stmt())
302 << "statement was not cached";
[email protected]e5ffd0e42009-09-11 21:30:56303
Victor Costan49a903a2021-05-07 22:21:00304 Statement from_id1(std::move(ref_from_id1));
Victor Costan87cf8c72018-07-19 19:36:04305 ASSERT_TRUE(from_id1.is_valid());
306 ASSERT_TRUE(from_id1.Step()) << "cached statement was not reset";
307 EXPECT_EQ(12, from_id1.ColumnInt(0));
308
Victor Costan49a903a2021-05-07 22:21:00309 scoped_refptr<Database::StatementRef> ref_from_id2 =
310 db_->GetCachedStatement(id2, kId2Sql);
Victor Costan87cf8c72018-07-19 19:36:04311 EXPECT_EQ(raw_id2_statement, ref_from_id2->stmt())
312 << "statement was not cached";
313
Victor Costan49a903a2021-05-07 22:21:00314 Statement from_id2(std::move(ref_from_id2));
Victor Costan87cf8c72018-07-19 19:36:04315 ASSERT_TRUE(from_id2.is_valid());
316 ASSERT_TRUE(from_id2.Step()) << "cached statement was not reset";
317 EXPECT_EQ(13, from_id2.ColumnInt(0));
[email protected]e5ffd0e42009-09-11 21:30:56318 }
Victor Costan613b4302018-11-20 05:32:43319
Victor Costan49a903a2021-05-07 22:21:00320 EXPECT_DCHECK_DEATH(db_->GetCachedStatement(id1, kId2Sql))
Victor Costan613b4302018-11-20 05:32:43321 << "Using a different SQL with the same statement ID should DCHECK";
Victor Costan49a903a2021-05-07 22:21:00322 EXPECT_DCHECK_DEATH(db_->GetCachedStatement(id2, kId1Sql))
Victor Costan613b4302018-11-20 05:32:43323 << "Using a different SQL with the same statement ID should DCHECK";
[email protected]e5ffd0e42009-09-11 21:30:56324}
325
Shubham Aggarwal003708982020-10-28 17:57:54326TEST_P(SQLDatabaseTest, IsSQLValidTest) {
Victor Costan49a903a2021-05-07 22:21:00327 ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)"));
328 ASSERT_TRUE(db_->IsSQLValid("SELECT a FROM foo"));
329 ASSERT_FALSE(db_->IsSQLValid("SELECT no_exist FROM foo"));
[email protected]eff1fa522011-12-12 23:50:59330}
331
Shubham Aggarwal003708982020-10-28 17:57:54332TEST_P(SQLDatabaseTest, DoesTableExist) {
Victor Costan49a903a2021-05-07 22:21:00333 EXPECT_FALSE(db_->DoesTableExist("foo"));
334 EXPECT_FALSE(db_->DoesTableExist("foo_index"));
shessa62504d2016-11-07 19:26:12335
Victor Costan49a903a2021-05-07 22:21:00336 ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)"));
337 ASSERT_TRUE(db_->Execute("CREATE INDEX foo_index ON foo (a)"));
338 EXPECT_TRUE(db_->DoesTableExist("foo"));
339 EXPECT_FALSE(db_->DoesTableExist("foo_index"));
Victor Costanf85512e52019-04-10 20:51:36340
341 // DoesTableExist() is case-sensitive.
Victor Costan49a903a2021-05-07 22:21:00342 EXPECT_FALSE(db_->DoesTableExist("Foo"));
343 EXPECT_FALSE(db_->DoesTableExist("FOO"));
Victor Costan70bedf22018-07-18 21:21:14344}
345
Shubham Aggarwal003708982020-10-28 17:57:54346TEST_P(SQLDatabaseTest, DoesIndexExist) {
Victor Costan49a903a2021-05-07 22:21:00347 ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)"));
348 EXPECT_FALSE(db_->DoesIndexExist("foo"));
349 EXPECT_FALSE(db_->DoesIndexExist("foo_ubdex"));
Victor Costan70bedf22018-07-18 21:21:14350
Victor Costan49a903a2021-05-07 22:21:00351 ASSERT_TRUE(db_->Execute("CREATE INDEX foo_index ON foo (a)"));
352 EXPECT_TRUE(db_->DoesIndexExist("foo_index"));
353 EXPECT_FALSE(db_->DoesIndexExist("foo"));
Victor Costanf85512e52019-04-10 20:51:36354
355 // DoesIndexExist() is case-sensitive.
Victor Costan49a903a2021-05-07 22:21:00356 EXPECT_FALSE(db_->DoesIndexExist("Foo_index"));
357 EXPECT_FALSE(db_->DoesIndexExist("Foo_Index"));
358 EXPECT_FALSE(db_->DoesIndexExist("FOO_INDEX"));
Victor Costan70bedf22018-07-18 21:21:14359}
360
Shubham Aggarwal003708982020-10-28 17:57:54361TEST_P(SQLDatabaseTest, DoesViewExist) {
Victor Costan49a903a2021-05-07 22:21:00362 EXPECT_FALSE(db_->DoesViewExist("voo"));
363 ASSERT_TRUE(db_->Execute("CREATE VIEW voo (a) AS SELECT 1"));
364 EXPECT_FALSE(db_->DoesIndexExist("voo"));
365 EXPECT_FALSE(db_->DoesTableExist("voo"));
366 EXPECT_TRUE(db_->DoesViewExist("voo"));
Victor Costanf85512e52019-04-10 20:51:36367
368 // DoesTableExist() is case-sensitive.
Victor Costan49a903a2021-05-07 22:21:00369 EXPECT_FALSE(db_->DoesViewExist("Voo"));
370 EXPECT_FALSE(db_->DoesViewExist("VOO"));
Victor Costan70bedf22018-07-18 21:21:14371}
[email protected]e5ffd0e42009-09-11 21:30:56372
Shubham Aggarwal003708982020-10-28 17:57:54373TEST_P(SQLDatabaseTest, DoesColumnExist) {
Victor Costan49a903a2021-05-07 22:21:00374 ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)"));
Victor Costan70bedf22018-07-18 21:21:14375
Victor Costan49a903a2021-05-07 22:21:00376 EXPECT_FALSE(db_->DoesColumnExist("foo", "bar"));
377 EXPECT_TRUE(db_->DoesColumnExist("foo", "a"));
[email protected]e5ffd0e42009-09-11 21:30:56378
Victor Costan49a903a2021-05-07 22:21:00379 ASSERT_FALSE(db_->DoesTableExist("bar"));
380 EXPECT_FALSE(db_->DoesColumnExist("bar", "b"));
shess92a2ab12015-04-09 01:59:47381
Victor Costanf85512e52019-04-10 20:51:36382 // SQLite resolves table/column names without case sensitivity.
Victor Costan49a903a2021-05-07 22:21:00383 EXPECT_TRUE(db_->DoesColumnExist("FOO", "A"));
384 EXPECT_TRUE(db_->DoesColumnExist("FOO", "a"));
385 EXPECT_TRUE(db_->DoesColumnExist("foo", "A"));
[email protected]e5ffd0e42009-09-11 21:30:56386}
387
Shubham Aggarwal003708982020-10-28 17:57:54388TEST_P(SQLDatabaseTest, GetLastInsertRowId) {
Victor Costan49a903a2021-05-07 22:21:00389 ASSERT_TRUE(db_->Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"));
[email protected]e5ffd0e42009-09-11 21:30:56390
Victor Costan49a903a2021-05-07 22:21:00391 ASSERT_TRUE(db_->Execute("INSERT INTO foo (value) VALUES (12)"));
[email protected]e5ffd0e42009-09-11 21:30:56392
393 // Last insert row ID should be valid.
Victor Costan49a903a2021-05-07 22:21:00394 int64_t row = db_->GetLastInsertRowId();
[email protected]e5ffd0e42009-09-11 21:30:56395 EXPECT_LT(0, row);
396
397 // It should be the primary key of the row we just inserted.
Victor Costan49a903a2021-05-07 22:21:00398 Statement s(db_->GetUniqueStatement("SELECT value FROM foo WHERE id=?"));
[email protected]e5ffd0e42009-09-11 21:30:56399 s.BindInt64(0, row);
400 ASSERT_TRUE(s.Step());
401 EXPECT_EQ(12, s.ColumnInt(0));
402}
[email protected]44ad7d902012-03-23 00:09:05403
Shubham Aggarwal003708982020-10-28 17:57:54404TEST_P(SQLDatabaseTest, Rollback) {
Victor Costan49a903a2021-05-07 22:21:00405 ASSERT_TRUE(db_->BeginTransaction());
406 ASSERT_TRUE(db_->BeginTransaction());
407 EXPECT_EQ(2, db_->transaction_nesting());
408 db_->RollbackTransaction();
409 EXPECT_FALSE(db_->CommitTransaction());
410 EXPECT_TRUE(db_->BeginTransaction());
[email protected]44ad7d902012-03-23 00:09:05411}
[email protected]8e0c01282012-04-06 19:36:49412
shess976814402016-06-21 06:56:25413// Test the scoped error expecter by attempting to insert a duplicate
[email protected]4350e322013-06-18 22:18:10414// value into an index.
Shubham Aggarwal003708982020-10-28 17:57:54415TEST_P(SQLDatabaseTest, ScopedErrorExpecter) {
[email protected]4350e322013-06-18 22:18:10416 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)";
Victor Costan49a903a2021-05-07 22:21:00417 ASSERT_TRUE(db_->Execute(kCreateSql));
418 ASSERT_TRUE(db_->Execute("INSERT INTO foo (id) VALUES (12)"));
[email protected]4350e322013-06-18 22:18:10419
shess92a2ab12015-04-09 01:59:47420 {
shess976814402016-06-21 06:56:25421 sql::test::ScopedErrorExpecter expecter;
422 expecter.ExpectError(SQLITE_CONSTRAINT);
Victor Costan49a903a2021-05-07 22:21:00423 ASSERT_FALSE(db_->Execute("INSERT INTO foo (id) VALUES (12)"));
shess976814402016-06-21 06:56:25424 ASSERT_TRUE(expecter.SawExpectedErrors());
shess92a2ab12015-04-09 01:59:47425 }
426}
427
Victor Costan106e50072021-07-17 00:04:49428TEST_P(SQLDatabaseTest, SchemaIntrospectionUsesErrorExpecter) {
shess92a2ab12015-04-09 01:59:47429 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)";
Victor Costan49a903a2021-05-07 22:21:00430 ASSERT_TRUE(db_->Execute(kCreateSql));
431 ASSERT_FALSE(db_->DoesTableExist("bar"));
432 ASSERT_TRUE(db_->DoesTableExist("foo"));
433 ASSERT_TRUE(db_->DoesColumnExist("foo", "id"));
434 db_->Close();
shess92a2ab12015-04-09 01:59:47435
436 // Corrupt the database so that nothing works, including PRAGMAs.
Victor Costan49a903a2021-05-07 22:21:00437 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_));
shess92a2ab12015-04-09 01:59:47438
439 {
shess976814402016-06-21 06:56:25440 sql::test::ScopedErrorExpecter expecter;
441 expecter.ExpectError(SQLITE_CORRUPT);
Victor Costan49a903a2021-05-07 22:21:00442 ASSERT_TRUE(db_->Open(db_path_));
443 ASSERT_FALSE(db_->DoesTableExist("bar"));
444 ASSERT_FALSE(db_->DoesTableExist("foo"));
445 ASSERT_FALSE(db_->DoesColumnExist("foo", "id"));
shess976814402016-06-21 06:56:25446 ASSERT_TRUE(expecter.SawExpectedErrors());
shess92a2ab12015-04-09 01:59:47447 }
[email protected]4350e322013-06-18 22:18:10448}
449
Shubham Aggarwal003708982020-10-28 17:57:54450TEST_P(SQLDatabaseTest, ErrorCallback) {
[email protected]98cf3002013-07-12 01:38:56451 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)";
Victor Costan49a903a2021-05-07 22:21:00452 ASSERT_TRUE(db_->Execute(kCreateSql));
453 ASSERT_TRUE(db_->Execute("INSERT INTO foo (id) VALUES (12)"));
[email protected]98cf3002013-07-12 01:38:56454
455 int error = SQLITE_OK;
456 {
Victor Costan49a903a2021-05-07 22:21:00457 ScopedErrorCallback sec(db_.get(),
458 base::BindRepeating(&CaptureErrorCallback, &error));
459 EXPECT_FALSE(db_->Execute("INSERT INTO foo (id) VALUES (12)"));
Scott Hessdcf120482015-02-10 21:33:29460
461 // Later versions of SQLite throw SQLITE_CONSTRAINT_UNIQUE. The specific
462 // sub-error isn't really important.
Victor Costancfbfa602018-08-01 23:24:46463 EXPECT_EQ(SQLITE_CONSTRAINT, (error & 0xff));
[email protected]98cf3002013-07-12 01:38:56464 }
465
466 // Callback is no longer in force due to reset.
467 {
468 error = SQLITE_OK;
shess976814402016-06-21 06:56:25469 sql::test::ScopedErrorExpecter expecter;
470 expecter.ExpectError(SQLITE_CONSTRAINT);
Victor Costan49a903a2021-05-07 22:21:00471 ASSERT_FALSE(db_->Execute("INSERT INTO foo (id) VALUES (12)"));
shess976814402016-06-21 06:56:25472 ASSERT_TRUE(expecter.SawExpectedErrors());
[email protected]98cf3002013-07-12 01:38:56473 EXPECT_EQ(SQLITE_OK, error);
474 }
475
tzikd16d2192018-03-07 08:58:36476 // base::BindRepeating() can curry arguments to be passed by const reference
[email protected]81a2a602013-07-17 19:10:36477 // to the callback function. If the callback function calls
478 // re/set_error_callback(), the storage for those arguments can be
479 // deleted while the callback function is still executing.
[email protected]98cf3002013-07-12 01:38:56480 //
481 // RefCounter() counts how many objects are live using an external
482 // count. The same counter is passed to the callback, so that it
483 // can check directly even if the RefCounter object is no longer
484 // live.
485 {
486 size_t count = 0;
Victor Costan49a903a2021-05-07 22:21:00487 ScopedErrorCallback sec(
488 db_.get(), base::BindRepeating(&ErrorCallbackSetHelper, db_.get(),
489 &count, RefCounter(&count)));
[email protected]98cf3002013-07-12 01:38:56490
Victor Costan49a903a2021-05-07 22:21:00491 EXPECT_FALSE(db_->Execute("INSERT INTO foo (id) VALUES (12)"));
[email protected]98cf3002013-07-12 01:38:56492 }
493
494 // Same test, but reset_error_callback() case.
495 {
496 size_t count = 0;
Victor Costan49a903a2021-05-07 22:21:00497 ScopedErrorCallback sec(
498 db_.get(), base::BindRepeating(&ErrorCallbackResetHelper, db_.get(),
499 &count, RefCounter(&count)));
[email protected]98cf3002013-07-12 01:38:56500
Victor Costan49a903a2021-05-07 22:21:00501 EXPECT_FALSE(db_->Execute("INSERT INTO foo (id) VALUES (12)"));
[email protected]98cf3002013-07-12 01:38:56502 }
503}
504
Victor Costanc02b7af2021-07-09 13:26:45505TEST_P(SQLDatabaseTest, Execute_CompilationError) {
506 bool error_callback_called = false;
507 db_->set_error_callback(base::BindLambdaForTesting([&](int error,
508 sql::Statement*
509 statement) {
510 EXPECT_EQ(SQLITE_ERROR, error);
511 EXPECT_EQ(nullptr, statement);
512 EXPECT_FALSE(error_callback_called)
513 << "SQL compilation errors should call the error callback exactly once";
514 error_callback_called = true;
515 }));
516
517 {
518 sql::test::ScopedErrorExpecter expecter;
519 expecter.ExpectError(SQLITE_ERROR);
520 EXPECT_FALSE(db_->Execute("SELECT missing_column FROM missing_table"));
521 EXPECT_TRUE(expecter.SawExpectedErrors());
522 }
523
524 EXPECT_TRUE(error_callback_called)
525 << "SQL compilation errors should call the error callback";
526}
527
528TEST_P(SQLDatabaseTest, GetUniqueStatement_CompilationError) {
529 bool error_callback_called = false;
530 db_->set_error_callback(base::BindLambdaForTesting([&](int error,
531 sql::Statement*
532 statement) {
533 EXPECT_EQ(SQLITE_ERROR, error);
534 EXPECT_EQ(nullptr, statement);
535 EXPECT_FALSE(error_callback_called)
536 << "SQL compilation errors should call the error callback exactly once";
537 error_callback_called = true;
538 }));
539
540 {
541 sql::test::ScopedErrorExpecter expecter;
542 expecter.ExpectError(SQLITE_ERROR);
543 sql::Statement statement(
544 db_->GetUniqueStatement("SELECT missing_column FROM missing_table"));
545 EXPECT_FALSE(statement.is_valid());
546 EXPECT_TRUE(expecter.SawExpectedErrors());
547 }
548
549 EXPECT_TRUE(error_callback_called)
550 << "SQL compilation errors should call the error callback";
551}
552
553TEST_P(SQLDatabaseTest, GetCachedStatement_CompilationError) {
554 bool error_callback_called = false;
555 db_->set_error_callback(base::BindLambdaForTesting([&](int error,
556 sql::Statement*
557 statement) {
558 EXPECT_EQ(SQLITE_ERROR, error);
559 EXPECT_EQ(nullptr, statement);
560 EXPECT_FALSE(error_callback_called)
561 << "SQL compilation errors should call the error callback exactly once";
562 error_callback_called = true;
563 }));
564
565 {
566 sql::test::ScopedErrorExpecter expecter;
567 expecter.ExpectError(SQLITE_ERROR);
568 sql::Statement statement(db_->GetCachedStatement(
569 SQL_FROM_HERE, "SELECT missing_column FROM missing_table"));
570 EXPECT_FALSE(statement.is_valid());
571 EXPECT_TRUE(expecter.SawExpectedErrors());
572 }
573
574 EXPECT_TRUE(error_callback_called)
575 << "SQL compilation errors should call the error callback";
576}
577
Victor Costan55309322021-07-19 17:58:42578TEST_P(SQLDatabaseTest, GetUniqueStatement_ExtraContents) {
579 sql::Statement minimal(db_->GetUniqueStatement("SELECT 1"));
580 sql::Statement extra_semicolon(db_->GetUniqueStatement("SELECT 1;"));
581
582 // It would be nice to flag trailing comments too, as they cost binary size.
583 // However, there's no easy way of doing that.
584 sql::Statement trailing_comment(
585 db_->GetUniqueStatement("SELECT 1 -- Comment"));
586
587 EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("SELECT 1;SELECT 2"))
588 << "Extra statement without whitespace";
589 EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("SELECT 1; SELECT 2"))
590 << "Extra statement separated by whitespace";
591 EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("SELECT 1;-- Comment"))
592 << "Comment without whitespace";
593 EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("SELECT 1; -- Comment"))
594 << "Comment separated by whitespace";
595}
596
597TEST_P(SQLDatabaseTest, GetCachedStatement_ExtraContents) {
598 sql::Statement minimal(db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1"));
599 sql::Statement extra_semicolon(
600 db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1;"));
601
602 // It would be nice to flag trailing comments too, as they cost binary size.
603 // However, there's no easy way of doing that.
604 sql::Statement trailing_comment(
605 db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1 -- Comment"));
606
607 EXPECT_DCHECK_DEATH(
608 db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1;SELECT 2"))
609 << "Extra statement without whitespace";
610 EXPECT_DCHECK_DEATH(
611 db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1; SELECT 2"))
612 << "Extra statement separated by whitespace";
613 EXPECT_DCHECK_DEATH(
614 db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1;-- Comment"))
615 << "Comment without whitespace";
616 EXPECT_DCHECK_DEATH(
617 db_->GetCachedStatement(SQL_FROM_HERE, "SELECT 1; -- Comment"))
618 << "Comment separated by whitespace";
619}
620
621TEST_P(SQLDatabaseTest, IsSQLValid_ExtraContents) {
622 EXPECT_TRUE(db_->IsSQLValid("SELECT 1"));
623 EXPECT_TRUE(db_->IsSQLValid("SELECT 1;"))
624 << "Trailing semicolons are currently tolerated";
625
626 // It would be nice to flag trailing comments too, as they cost binary size.
627 // However, there's no easy way of doing that.
628 EXPECT_TRUE(db_->IsSQLValid("SELECT 1 -- Comment"))
629 << "Trailing comments are currently tolerated";
630
631 EXPECT_DCHECK_DEATH(db_->IsSQLValid("SELECT 1;SELECT 2"))
632 << "Extra statement without whitespace";
633 EXPECT_DCHECK_DEATH(db_->IsSQLValid("SELECT 1; SELECT 2"))
634 << "Extra statement separated by whitespace";
635 EXPECT_DCHECK_DEATH(db_->IsSQLValid("SELECT 1;-- Comment"))
636 << "Comment without whitespace";
637 EXPECT_DCHECK_DEATH(db_->IsSQLValid("SELECT 1; -- Comment"))
638 << "Comment separated by whitespace";
639}
640
Victor Costan18f9e7a2021-07-22 06:34:17641TEST_P(SQLDatabaseTest, GetUniqueStatement_NoContents) {
642 EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("")) << "Empty string";
643 EXPECT_DCHECK_DEATH(db_->GetUniqueStatement(" ")) << "Space";
644 EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("\n")) << "Newline";
645 EXPECT_DCHECK_DEATH(db_->GetUniqueStatement("-- Comment")) << "Comment";
646}
647
648TEST_P(SQLDatabaseTest, GetCachedStatement_NoContents) {
649 EXPECT_DCHECK_DEATH(db_->GetCachedStatement(SQL_FROM_HERE, ""))
650 << "Empty string";
651 EXPECT_DCHECK_DEATH(db_->GetCachedStatement(SQL_FROM_HERE, " ")) << "Space";
652 EXPECT_DCHECK_DEATH(db_->GetCachedStatement(SQL_FROM_HERE, "\n"))
653 << "Newline";
654 EXPECT_DCHECK_DEATH(db_->GetCachedStatement(SQL_FROM_HERE, "-- Comment"))
655 << "Comment";
656}
657
658TEST_P(SQLDatabaseTest, IsSQLValid_NoContents) {
659 EXPECT_DCHECK_DEATH(db_->IsSQLValid("")) << "Empty string";
660 EXPECT_DCHECK_DEATH(db_->IsSQLValid(" ")) << "Space";
661 EXPECT_DCHECK_DEATH(db_->IsSQLValid("\n")) << "Newline";
662 EXPECT_DCHECK_DEATH(db_->IsSQLValid("-- Comment")) << "Comment";
663}
664
Victor Costan49a903a2021-05-07 22:21:00665// Test that Database::Raze() results in a database without the
[email protected]8e0c01282012-04-06 19:36:49666// tables from the original database.
Shubham Aggarwal003708982020-10-28 17:57:54667TEST_P(SQLDatabaseTest, Raze) {
[email protected]8e0c01282012-04-06 19:36:49668 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
Victor Costan49a903a2021-05-07 22:21:00669 ASSERT_TRUE(db_->Execute(kCreateSql));
670 ASSERT_TRUE(db_->Execute("INSERT INTO foo (value) VALUES (12)"));
[email protected]8e0c01282012-04-06 19:36:49671
[email protected]69c58452012-08-06 19:22:42672 int pragma_auto_vacuum = 0;
673 {
Victor Costan49a903a2021-05-07 22:21:00674 Statement s(db_->GetUniqueStatement("PRAGMA auto_vacuum"));
[email protected]69c58452012-08-06 19:22:42675 ASSERT_TRUE(s.Step());
676 pragma_auto_vacuum = s.ColumnInt(0);
677 ASSERT_TRUE(pragma_auto_vacuum == 0 || pragma_auto_vacuum == 1);
678 }
679
680 // If auto_vacuum is set, there's an extra page to maintain a freelist.
681 const int kExpectedPageCount = 2 + pragma_auto_vacuum;
682
[email protected]8e0c01282012-04-06 19:36:49683 {
Victor Costan49a903a2021-05-07 22:21:00684 Statement s(db_->GetUniqueStatement("PRAGMA page_count"));
[email protected]8e0c01282012-04-06 19:36:49685 ASSERT_TRUE(s.Step());
[email protected]69c58452012-08-06 19:22:42686 EXPECT_EQ(kExpectedPageCount, s.ColumnInt(0));
[email protected]8e0c01282012-04-06 19:36:49687 }
688
689 {
Victor Costan49a903a2021-05-07 22:21:00690 Statement s(db_->GetUniqueStatement("SELECT * FROM sqlite_master"));
[email protected]8e0c01282012-04-06 19:36:49691 ASSERT_TRUE(s.Step());
692 EXPECT_EQ("table", s.ColumnString(0));
693 EXPECT_EQ("foo", s.ColumnString(1));
694 EXPECT_EQ("foo", s.ColumnString(2));
[email protected]69c58452012-08-06 19:22:42695 // Table "foo" is stored in the last page of the file.
696 EXPECT_EQ(kExpectedPageCount, s.ColumnInt(3));
[email protected]8e0c01282012-04-06 19:36:49697 EXPECT_EQ(kCreateSql, s.ColumnString(4));
698 }
699
Victor Costan49a903a2021-05-07 22:21:00700 ASSERT_TRUE(db_->Raze());
[email protected]8e0c01282012-04-06 19:36:49701
702 {
Victor Costan49a903a2021-05-07 22:21:00703 Statement s(db_->GetUniqueStatement("PRAGMA page_count"));
[email protected]8e0c01282012-04-06 19:36:49704 ASSERT_TRUE(s.Step());
705 EXPECT_EQ(1, s.ColumnInt(0));
706 }
707
Victor Costan49a903a2021-05-07 22:21:00708 ASSERT_EQ(0, SqliteMasterCount(db_.get()));
[email protected]69c58452012-08-06 19:22:42709
710 {
Victor Costan49a903a2021-05-07 22:21:00711 Statement s(db_->GetUniqueStatement("PRAGMA auto_vacuum"));
[email protected]69c58452012-08-06 19:22:42712 ASSERT_TRUE(s.Step());
[email protected]6d42f152012-11-10 00:38:24713 // The new database has the same auto_vacuum as a fresh database.
[email protected]69c58452012-08-06 19:22:42714 EXPECT_EQ(pragma_auto_vacuum, s.ColumnInt(0));
715 }
[email protected]8e0c01282012-04-06 19:36:49716}
717
Victor Costancfbfa602018-08-01 23:24:46718// Helper for SQLDatabaseTest.RazePageSize. Creates a fresh db based on
shess7e2baba2016-10-27 23:46:05719// db_prefix, with the given initial page size, and verifies it against the
720// expected size. Then changes to the final page size and razes, verifying that
721// the fresh database ends up with the expected final page size.
722void TestPageSize(const base::FilePath& db_prefix,
723 int initial_page_size,
724 const std::string& expected_initial_page_size,
725 int final_page_size,
726 const std::string& expected_final_page_size) {
Victor Costan1d868352018-06-26 19:06:48727 static const char kCreateSql[] = "CREATE TABLE x (t TEXT)";
728 static const char kInsertSql1[] = "INSERT INTO x VALUES ('This is a test')";
729 static const char kInsertSql2[] = "INSERT INTO x VALUES ('That was a test')";
shess7e2baba2016-10-27 23:46:05730
731 const base::FilePath db_path = db_prefix.InsertBeforeExtensionASCII(
Raul Tambre6c708e32019-02-08 22:35:14732 base::NumberToString(initial_page_size));
Victor Costan49a903a2021-05-07 22:21:00733 Database::Delete(db_path);
734 Database db({.page_size = initial_page_size});
shess7e2baba2016-10-27 23:46:05735 ASSERT_TRUE(db.Open(db_path));
736 ASSERT_TRUE(db.Execute(kCreateSql));
737 ASSERT_TRUE(db.Execute(kInsertSql1));
738 ASSERT_TRUE(db.Execute(kInsertSql2));
739 ASSERT_EQ(expected_initial_page_size,
740 ExecuteWithResult(&db, "PRAGMA page_size"));
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28741 db.Close();
shess7e2baba2016-10-27 23:46:05742
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28743 // Re-open the database while setting a new |options.page_size| in the object.
Victor Costan49a903a2021-05-07 22:21:00744 Database razed_db({.page_size = final_page_size});
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28745 ASSERT_TRUE(razed_db.Open(db_path));
shess7e2baba2016-10-27 23:46:05746 // Raze will use the page size set in the connection object, which may not
747 // match the file's page size.
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28748 ASSERT_TRUE(razed_db.Raze());
shess7e2baba2016-10-27 23:46:05749
750 // SQLite 3.10.2 (at least) has a quirk with the sqlite3_backup() API (used by
751 // Raze()) which causes the destination database to remember the previous
752 // page_size, even if the overwriting database changed the page_size. Access
753 // the actual database to cause the cached value to be updated.
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28754 EXPECT_EQ("0",
755 ExecuteWithResult(&razed_db, "SELECT COUNT(*) FROM sqlite_master"));
shess7e2baba2016-10-27 23:46:05756
757 EXPECT_EQ(expected_final_page_size,
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28758 ExecuteWithResult(&razed_db, "PRAGMA page_size"));
759 EXPECT_EQ("1", ExecuteWithResult(&razed_db, "PRAGMA page_count"));
shess7e2baba2016-10-27 23:46:05760}
761
Victor Costan49a903a2021-05-07 22:21:00762// Verify that Recovery maintains the page size, and the virtual table
shess7e2baba2016-10-27 23:46:05763// works with page sizes other than SQLite's default. Also verify the case
764// where the default page size has changed.
Shubham Aggarwal003708982020-10-28 17:57:54765TEST_P(SQLDatabaseTest, RazePageSize) {
shess7e2baba2016-10-27 23:46:05766 const std::string default_page_size =
Victor Costan49a903a2021-05-07 22:21:00767 ExecuteWithResult(db_.get(), "PRAGMA page_size");
[email protected]8e0c01282012-04-06 19:36:49768
Victor Costan7f6abbbe2018-07-29 02:57:27769 // Sync uses 32k pages.
shess7e2baba2016-10-27 23:46:05770 EXPECT_NO_FATAL_FAILURE(
Victor Costan49a903a2021-05-07 22:21:00771 TestPageSize(db_path_, 32768, "32768", 32768, "32768"));
[email protected]8e0c01282012-04-06 19:36:49772
shess7e2baba2016-10-27 23:46:05773 // Many clients use 4k pages. This is the SQLite default after 3.12.0.
Victor Costan49a903a2021-05-07 22:21:00774 EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path_, 4096, "4096", 4096, "4096"));
shess7e2baba2016-10-27 23:46:05775
776 // 1k is the default page size before 3.12.0.
Victor Costan49a903a2021-05-07 22:21:00777 EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path_, 1024, "1024", 1024, "1024"));
shess7e2baba2016-10-27 23:46:05778
Victor Costan49a903a2021-05-07 22:21:00779 EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path_, 2048, "2048", 4096, "4096"));
shess7e2baba2016-10-27 23:46:05780
Victor Costan7f6abbbe2018-07-29 02:57:27781 // Databases with no page size specified should result in the default
shess7e2baba2016-10-27 23:46:05782 // page size. 2k has never been the default page size.
783 ASSERT_NE("2048", default_page_size);
Victor Costan49a903a2021-05-07 22:21:00784 EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path_, 2048, "2048",
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28785 DatabaseOptions::kDefaultPageSize,
786 default_page_size));
[email protected]8e0c01282012-04-06 19:36:49787}
788
789// Test that Raze() results are seen in other connections.
Shubham Aggarwal003708982020-10-28 17:57:54790TEST_P(SQLDatabaseTest, RazeMultiple) {
[email protected]8e0c01282012-04-06 19:36:49791 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
Victor Costan49a903a2021-05-07 22:21:00792 ASSERT_TRUE(db_->Execute(kCreateSql));
[email protected]8e0c01282012-04-06 19:36:49793
Victor Costan49a903a2021-05-07 22:21:00794 Database other_db(GetDBOptions());
795 ASSERT_TRUE(other_db.Open(db_path_));
[email protected]8e0c01282012-04-06 19:36:49796
797 // Check that the second connection sees the table.
[email protected]7bae5742013-07-10 20:46:16798 ASSERT_EQ(1, SqliteMasterCount(&other_db));
[email protected]8e0c01282012-04-06 19:36:49799
Victor Costan49a903a2021-05-07 22:21:00800 ASSERT_TRUE(db_->Raze());
[email protected]8e0c01282012-04-06 19:36:49801
802 // The second connection sees the updated database.
[email protected]7bae5742013-07-10 20:46:16803 ASSERT_EQ(0, SqliteMasterCount(&other_db));
[email protected]8e0c01282012-04-06 19:36:49804}
805
Shubham Aggarwal003708982020-10-28 17:57:54806TEST_P(SQLDatabaseTest, RazeLocked) {
[email protected]8e0c01282012-04-06 19:36:49807 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
Victor Costan49a903a2021-05-07 22:21:00808 ASSERT_TRUE(db_->Execute(kCreateSql));
[email protected]8e0c01282012-04-06 19:36:49809
810 // Open a transaction and write some data in a second connection.
811 // This will acquire a PENDING or EXCLUSIVE transaction, which will
812 // cause the raze to fail.
Victor Costan49a903a2021-05-07 22:21:00813 Database other_db(GetDBOptions());
814 ASSERT_TRUE(other_db.Open(db_path_));
[email protected]8e0c01282012-04-06 19:36:49815 ASSERT_TRUE(other_db.BeginTransaction());
816 const char* kInsertSql = "INSERT INTO foo VALUES (1, 'data')";
817 ASSERT_TRUE(other_db.Execute(kInsertSql));
818
Victor Costan49a903a2021-05-07 22:21:00819 ASSERT_FALSE(db_->Raze());
[email protected]8e0c01282012-04-06 19:36:49820
821 // Works after COMMIT.
822 ASSERT_TRUE(other_db.CommitTransaction());
Victor Costan49a903a2021-05-07 22:21:00823 ASSERT_TRUE(db_->Raze());
[email protected]8e0c01282012-04-06 19:36:49824
825 // Re-create the database.
Victor Costan49a903a2021-05-07 22:21:00826 ASSERT_TRUE(db_->Execute(kCreateSql));
827 ASSERT_TRUE(db_->Execute(kInsertSql));
[email protected]8e0c01282012-04-06 19:36:49828
829 // An unfinished read transaction in the other connection also
830 // blocks raze.
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57831 // This doesn't happen in WAL mode because reads are no longer blocked by
832 // write operations when using a WAL.
Shubham Aggarwal003708982020-10-28 17:57:54833 if (!IsWALEnabled()) {
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57834 const char* kQuery = "SELECT COUNT(*) FROM foo";
Victor Costan49a903a2021-05-07 22:21:00835 Statement s(other_db.GetUniqueStatement(kQuery));
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57836 ASSERT_TRUE(s.Step());
Victor Costan49a903a2021-05-07 22:21:00837 ASSERT_FALSE(db_->Raze());
[email protected]8e0c01282012-04-06 19:36:49838
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57839 // Completing the statement unlocks the database.
840 ASSERT_FALSE(s.Step());
Victor Costan49a903a2021-05-07 22:21:00841 ASSERT_TRUE(db_->Raze());
Shubham Aggarwalbe4f97ce2020-06-19 15:58:57842 }
[email protected]8e0c01282012-04-06 19:36:49843}
844
[email protected]7bae5742013-07-10 20:46:16845// Verify that Raze() can handle an empty file. SQLite should treat
846// this as an empty database.
Shubham Aggarwal003708982020-10-28 17:57:54847TEST_P(SQLDatabaseTest, RazeEmptyDB) {
[email protected]7bae5742013-07-10 20:46:16848 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
Victor Costan49a903a2021-05-07 22:21:00849 ASSERT_TRUE(db_->Execute(kCreateSql));
850 db_->Close();
[email protected]7bae5742013-07-10 20:46:16851
Victor Costan49a903a2021-05-07 22:21:00852 ASSERT_TRUE(TruncateDatabase());
[email protected]7bae5742013-07-10 20:46:16853
Victor Costan49a903a2021-05-07 22:21:00854 ASSERT_TRUE(db_->Open(db_path_));
855 ASSERT_TRUE(db_->Raze());
856 EXPECT_EQ(0, SqliteMasterCount(db_.get()));
[email protected]7bae5742013-07-10 20:46:16857}
858
859// Verify that Raze() can handle a file of junk.
Shubham Aggarwal7b60fe6e2020-10-15 06:00:28860// Need exclusive mode off here as there are some subtleties (by design) around
861// how the cache is used with it on which causes the test to fail.
Shubham Aggarwal003708982020-10-28 17:57:54862TEST_P(SQLDatabaseTest, RazeNOTADB) {
Victor Costan49a903a2021-05-07 22:21:00863 db_->Close();
864 Database::Delete(db_path_);
865 ASSERT_FALSE(base::PathExists(db_path_));
[email protected]7bae5742013-07-10 20:46:16866
Victor Costan49a903a2021-05-07 22:21:00867 ASSERT_TRUE(OverwriteDatabaseHeader(OverwriteType::kTruncate));
868 ASSERT_TRUE(base::PathExists(db_path_));
[email protected]7bae5742013-07-10 20:46:16869
Scott Hessdcf120482015-02-10 21:33:29870 // SQLite will successfully open the handle, but fail when running PRAGMA
871 // statements that access the database.
[email protected]7bae5742013-07-10 20:46:16872 {
shess976814402016-06-21 06:56:25873 sql::test::ScopedErrorExpecter expecter;
Victor Costan42988a92018-02-06 02:22:14874 expecter.ExpectError(SQLITE_NOTADB);
Scott Hessdcf120482015-02-10 21:33:29875
Victor Costan49a903a2021-05-07 22:21:00876 EXPECT_TRUE(db_->Open(db_path_));
shess976814402016-06-21 06:56:25877 ASSERT_TRUE(expecter.SawExpectedErrors());
[email protected]7bae5742013-07-10 20:46:16878 }
Victor Costan49a903a2021-05-07 22:21:00879 EXPECT_TRUE(db_->Raze());
880 db_->Close();
[email protected]7bae5742013-07-10 20:46:16881
882 // Now empty, the open should open an empty database.
Victor Costan49a903a2021-05-07 22:21:00883 EXPECT_TRUE(db_->Open(db_path_));
884 EXPECT_EQ(0, SqliteMasterCount(db_.get()));
[email protected]7bae5742013-07-10 20:46:16885}
886
887// Verify that Raze() can handle a database overwritten with garbage.
Shubham Aggarwal003708982020-10-28 17:57:54888TEST_P(SQLDatabaseTest, RazeNOTADB2) {
[email protected]7bae5742013-07-10 20:46:16889 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
Victor Costan49a903a2021-05-07 22:21:00890 ASSERT_TRUE(db_->Execute(kCreateSql));
891 ASSERT_EQ(1, SqliteMasterCount(db_.get()));
892 db_->Close();
[email protected]7bae5742013-07-10 20:46:16893
Victor Costan49a903a2021-05-07 22:21:00894 ASSERT_TRUE(OverwriteDatabaseHeader(OverwriteType::kOverwrite));
[email protected]7bae5742013-07-10 20:46:16895
896 // SQLite will successfully open the handle, but will fail with
897 // SQLITE_NOTADB on pragma statemenets which attempt to read the
898 // corrupted header.
899 {
shess976814402016-06-21 06:56:25900 sql::test::ScopedErrorExpecter expecter;
901 expecter.ExpectError(SQLITE_NOTADB);
Victor Costan49a903a2021-05-07 22:21:00902 EXPECT_TRUE(db_->Open(db_path_));
shess976814402016-06-21 06:56:25903 ASSERT_TRUE(expecter.SawExpectedErrors());
[email protected]7bae5742013-07-10 20:46:16904 }
Victor Costan49a903a2021-05-07 22:21:00905 EXPECT_TRUE(db_->Raze());
906 db_->Close();
[email protected]7bae5742013-07-10 20:46:16907
908 // Now empty, the open should succeed with an empty database.
Victor Costan49a903a2021-05-07 22:21:00909 EXPECT_TRUE(db_->Open(db_path_));
910 EXPECT_EQ(0, SqliteMasterCount(db_.get()));
[email protected]7bae5742013-07-10 20:46:16911}
912
913// Test that a callback from Open() can raze the database. This is
914// essential for cases where the Open() can fail entirely, so the
[email protected]fed734a2013-07-17 04:45:13915// Raze() cannot happen later. Additionally test that when the
916// callback does this during Open(), the open is retried and succeeds.
Shubham Aggarwal003708982020-10-28 17:57:54917TEST_P(SQLDatabaseTest, RazeCallbackReopen) {
[email protected]7bae5742013-07-10 20:46:16918 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
Victor Costan49a903a2021-05-07 22:21:00919 ASSERT_TRUE(db_->Execute(kCreateSql));
920 ASSERT_EQ(1, SqliteMasterCount(db_.get()));
921 db_->Close();
[email protected]7bae5742013-07-10 20:46:16922
[email protected]a8848a72013-11-18 04:18:47923 // Corrupt the database so that nothing works, including PRAGMAs.
Victor Costan49a903a2021-05-07 22:21:00924 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_));
[email protected]7bae5742013-07-10 20:46:16925
[email protected]fed734a2013-07-17 04:45:13926 // Open() will succeed, even though the PRAGMA calls within will
927 // fail with SQLITE_CORRUPT, as will this PRAGMA.
928 {
shess976814402016-06-21 06:56:25929 sql::test::ScopedErrorExpecter expecter;
930 expecter.ExpectError(SQLITE_CORRUPT);
Victor Costan49a903a2021-05-07 22:21:00931 ASSERT_TRUE(db_->Open(db_path_));
932 ASSERT_FALSE(db_->Execute("PRAGMA auto_vacuum"));
933 db_->Close();
shess976814402016-06-21 06:56:25934 ASSERT_TRUE(expecter.SawExpectedErrors());
[email protected]fed734a2013-07-17 04:45:13935 }
936
Victor Costan49a903a2021-05-07 22:21:00937 db_->set_error_callback(
938 base::BindRepeating(&RazeErrorCallback, db_.get(), SQLITE_CORRUPT));
[email protected]7bae5742013-07-10 20:46:16939
[email protected]fed734a2013-07-17 04:45:13940 // When the PRAGMA calls in Open() raise SQLITE_CORRUPT, the error
941 // callback will call RazeAndClose(). Open() will then fail and be
942 // retried. The second Open() on the empty database will succeed
943 // cleanly.
Victor Costan49a903a2021-05-07 22:21:00944 ASSERT_TRUE(db_->Open(db_path_));
945 ASSERT_TRUE(db_->Execute("PRAGMA auto_vacuum"));
946 EXPECT_EQ(0, SqliteMasterCount(db_.get()));
[email protected]7bae5742013-07-10 20:46:16947}
948
[email protected]41a97c812013-02-07 02:35:38949// Basic test of RazeAndClose() operation.
Shubham Aggarwal003708982020-10-28 17:57:54950TEST_P(SQLDatabaseTest, RazeAndClose) {
[email protected]41a97c812013-02-07 02:35:38951 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
952 const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)";
953
954 // Test that RazeAndClose() closes the database, and that the
955 // database is empty when re-opened.
Victor Costan49a903a2021-05-07 22:21:00956 ASSERT_TRUE(db_->Execute(kCreateSql));
957 ASSERT_TRUE(db_->Execute(kPopulateSql));
958 ASSERT_TRUE(db_->RazeAndClose());
959 ASSERT_FALSE(db_->is_open());
960 db_->Close();
961 ASSERT_TRUE(db_->Open(db_path_));
962 ASSERT_EQ(0, SqliteMasterCount(db_.get()));
[email protected]41a97c812013-02-07 02:35:38963
964 // Test that RazeAndClose() can break transactions.
Victor Costan49a903a2021-05-07 22:21:00965 ASSERT_TRUE(db_->Execute(kCreateSql));
966 ASSERT_TRUE(db_->Execute(kPopulateSql));
967 ASSERT_TRUE(db_->BeginTransaction());
968 ASSERT_TRUE(db_->RazeAndClose());
969 ASSERT_FALSE(db_->is_open());
970 ASSERT_FALSE(db_->CommitTransaction());
971 db_->Close();
972 ASSERT_TRUE(db_->Open(db_path_));
973 ASSERT_EQ(0, SqliteMasterCount(db_.get()));
[email protected]41a97c812013-02-07 02:35:38974}
975
976// Test that various operations fail without crashing after
977// RazeAndClose().
Shubham Aggarwal003708982020-10-28 17:57:54978TEST_P(SQLDatabaseTest, RazeAndCloseDiagnostics) {
[email protected]41a97c812013-02-07 02:35:38979 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
980 const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)";
981 const char* kSimpleSql = "SELECT 1";
982
Victor Costan49a903a2021-05-07 22:21:00983 ASSERT_TRUE(db_->Execute(kCreateSql));
984 ASSERT_TRUE(db_->Execute(kPopulateSql));
[email protected]41a97c812013-02-07 02:35:38985
986 // Test baseline expectations.
Victor Costan49a903a2021-05-07 22:21:00987 db_->Preload();
988 ASSERT_TRUE(db_->DoesTableExist("foo"));
989 ASSERT_TRUE(db_->IsSQLValid(kSimpleSql));
Victor Costan49a903a2021-05-07 22:21:00990 ASSERT_TRUE(db_->Execute(kSimpleSql));
991 ASSERT_TRUE(db_->is_open());
[email protected]41a97c812013-02-07 02:35:38992 {
Victor Costan49a903a2021-05-07 22:21:00993 Statement s(db_->GetUniqueStatement(kSimpleSql));
[email protected]41a97c812013-02-07 02:35:38994 ASSERT_TRUE(s.Step());
995 }
996 {
Victor Costan49a903a2021-05-07 22:21:00997 Statement s(db_->GetCachedStatement(SQL_FROM_HERE, kSimpleSql));
[email protected]41a97c812013-02-07 02:35:38998 ASSERT_TRUE(s.Step());
999 }
Victor Costan49a903a2021-05-07 22:21:001000 ASSERT_TRUE(db_->BeginTransaction());
1001 ASSERT_TRUE(db_->CommitTransaction());
1002 ASSERT_TRUE(db_->BeginTransaction());
1003 db_->RollbackTransaction();
[email protected]41a97c812013-02-07 02:35:381004
Victor Costan49a903a2021-05-07 22:21:001005 ASSERT_TRUE(db_->RazeAndClose());
[email protected]41a97c812013-02-07 02:35:381006
1007 // At this point, they should all fail, but not crash.
Victor Costan49a903a2021-05-07 22:21:001008 db_->Preload();
1009 ASSERT_FALSE(db_->DoesTableExist("foo"));
1010 ASSERT_FALSE(db_->IsSQLValid(kSimpleSql));
Victor Costan49a903a2021-05-07 22:21:001011 ASSERT_FALSE(db_->Execute(kSimpleSql));
1012 ASSERT_FALSE(db_->is_open());
[email protected]41a97c812013-02-07 02:35:381013 {
Victor Costan49a903a2021-05-07 22:21:001014 Statement s(db_->GetUniqueStatement(kSimpleSql));
[email protected]41a97c812013-02-07 02:35:381015 ASSERT_FALSE(s.Step());
1016 }
1017 {
Victor Costan49a903a2021-05-07 22:21:001018 Statement s(db_->GetCachedStatement(SQL_FROM_HERE, kSimpleSql));
[email protected]41a97c812013-02-07 02:35:381019 ASSERT_FALSE(s.Step());
1020 }
Victor Costan49a903a2021-05-07 22:21:001021 ASSERT_FALSE(db_->BeginTransaction());
1022 ASSERT_FALSE(db_->CommitTransaction());
1023 ASSERT_FALSE(db_->BeginTransaction());
1024 db_->RollbackTransaction();
[email protected]41a97c812013-02-07 02:35:381025
1026 // Close normally to reset the poisoned flag.
Victor Costan49a903a2021-05-07 22:21:001027 db_->Close();
[email protected]41a97c812013-02-07 02:35:381028
Victor Costancfbfa602018-08-01 23:24:461029// DEATH tests not supported on Android, iOS, or Fuchsia.
Scott Graham57ee54822017-09-13 06:37:561030#if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_FUCHSIA)
[email protected]41a97c812013-02-07 02:35:381031 // Once the real Close() has been called, various calls enforce API
1032 // usage by becoming fatal in debug mode. Since DEATH tests are
1033 // expensive, just test one of them.
1034 if (DLOG_IS_ON(FATAL)) {
Victor Costan49a903a2021-05-07 22:21:001035 ASSERT_DEATH({ db_->IsSQLValid(kSimpleSql); },
Victor Costancfbfa602018-08-01 23:24:461036 "Illegal use of Database without a db");
[email protected]41a97c812013-02-07 02:35:381037 }
Victor Costan8a87f7e52017-11-10 01:29:301038#endif // !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_FUCHSIA)
[email protected]41a97c812013-02-07 02:35:381039}
1040
1041// TODO(shess): Spin up a background thread to hold other_db, to more
1042// closely match real life. That would also allow testing
1043// RazeWithTimeout().
1044
shess92a6fb22017-04-23 04:33:301045// On Windows, truncate silently fails against a memory-mapped file. One goal
1046// of Raze() is to truncate the file to remove blocks which generate I/O errors.
1047// Test that Raze() turns off memory mapping so that the file is truncated.
1048// [This would not cover the case of multiple connections where one of the other
1049// connections is memory-mapped. That is infrequent in Chromium.]
Shubham Aggarwal003708982020-10-28 17:57:541050TEST_P(SQLDatabaseTest, RazeTruncate) {
shess92a6fb22017-04-23 04:33:301051 // The empty database has 0 or 1 pages. Raze() should leave it with exactly 1
1052 // page. Not checking directly because auto_vacuum on Android adds a freelist
1053 // page.
Victor Costan49a903a2021-05-07 22:21:001054 ASSERT_TRUE(db_->Raze());
shess92a6fb22017-04-23 04:33:301055 int64_t expected_size;
Victor Costan49a903a2021-05-07 22:21:001056 ASSERT_TRUE(base::GetFileSize(db_path_, &expected_size));
shess92a6fb22017-04-23 04:33:301057 ASSERT_GT(expected_size, 0);
1058
1059 // Cause the database to take a few pages.
1060 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
Victor Costan49a903a2021-05-07 22:21:001061 ASSERT_TRUE(db_->Execute(kCreateSql));
shess92a6fb22017-04-23 04:33:301062 for (size_t i = 0; i < 24; ++i) {
1063 ASSERT_TRUE(
Victor Costan49a903a2021-05-07 22:21:001064 db_->Execute("INSERT INTO foo (value) VALUES (randomblob(1024))"));
shess92a6fb22017-04-23 04:33:301065 }
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571066
1067 // In WAL mode, writes don't reach the database file until a checkpoint
1068 // happens.
Victor Costan49a903a2021-05-07 22:21:001069 ASSERT_TRUE(db_->CheckpointDatabase());
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571070
shess92a6fb22017-04-23 04:33:301071 int64_t db_size;
Victor Costan49a903a2021-05-07 22:21:001072 ASSERT_TRUE(base::GetFileSize(db_path_, &db_size));
shess92a6fb22017-04-23 04:33:301073 ASSERT_GT(db_size, expected_size);
1074
1075 // Make a query covering most of the database file to make sure that the
1076 // blocks are actually mapped into memory. Empirically, the truncate problem
1077 // doesn't seem to happen if no blocks are mapped.
1078 EXPECT_EQ("24576",
Victor Costan49a903a2021-05-07 22:21:001079 ExecuteWithResult(db_.get(), "SELECT SUM(LENGTH(value)) FROM foo"));
shess92a6fb22017-04-23 04:33:301080
Victor Costan49a903a2021-05-07 22:21:001081 ASSERT_TRUE(db_->Raze());
1082 ASSERT_TRUE(base::GetFileSize(db_path_, &db_size));
shess92a6fb22017-04-23 04:33:301083 ASSERT_EQ(expected_size, db_size);
1084}
1085
[email protected]1348765a2012-07-24 08:25:531086#if defined(OS_ANDROID)
Shubham Aggarwal003708982020-10-28 17:57:541087TEST_P(SQLDatabaseTest, SetTempDirForSQL) {
Victor Costan49a903a2021-05-07 22:21:001088 MetaTable meta_table;
[email protected]1348765a2012-07-24 08:25:531089 // Below call needs a temporary directory in sqlite3
1090 // On Android, it can pass only when the temporary directory is set.
1091 // Otherwise, sqlite3 doesn't find the correct directory to store
1092 // temporary files and will report the error 'unable to open
1093 // database file'.
Victor Costan49a903a2021-05-07 22:21:001094 ASSERT_TRUE(meta_table.Init(db_.get(), 4, 4));
[email protected]1348765a2012-07-24 08:25:531095}
Victor Costan8a87f7e52017-11-10 01:29:301096#endif // defined(OS_ANDROID)
[email protected]8d2e39e2013-06-24 05:55:081097
Shubham Aggarwal003708982020-10-28 17:57:541098TEST_P(SQLDatabaseTest, Delete) {
Victor Costan49a903a2021-05-07 22:21:001099 EXPECT_TRUE(db_->Execute("CREATE TABLE x (x)"));
1100 db_->Close();
[email protected]8d2e39e2013-06-24 05:55:081101
Victor Costan49a903a2021-05-07 22:21:001102 base::FilePath journal_path = Database::JournalPath(db_path_);
1103 base::FilePath wal_path = Database::WriteAheadLogPath(db_path_);
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571104
1105 // Should have both a main database file and a journal file if
1106 // journal_mode is TRUNCATE. There is no WAL file as it is deleted on Close.
Victor Costan49a903a2021-05-07 22:21:001107 ASSERT_TRUE(base::PathExists(db_path_));
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571108 if (!IsWALEnabled()) { // TRUNCATE mode
Victor Costan49a903a2021-05-07 22:21:001109 ASSERT_TRUE(base::PathExists(journal_path));
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571110 }
[email protected]8d2e39e2013-06-24 05:55:081111
Victor Costan49a903a2021-05-07 22:21:001112 Database::Delete(db_path_);
1113 EXPECT_FALSE(base::PathExists(db_path_));
1114 EXPECT_FALSE(base::PathExists(journal_path));
1115 EXPECT_FALSE(base::PathExists(wal_path));
[email protected]8d2e39e2013-06-24 05:55:081116}
[email protected]7bae5742013-07-10 20:46:161117
Victor Costance678e72018-07-24 10:25:001118#if defined(OS_POSIX) // This test operates on POSIX file permissions.
Shubham Aggarwal003708982020-10-28 17:57:541119TEST_P(SQLDatabaseTest, PosixFilePermissions) {
Victor Costan49a903a2021-05-07 22:21:001120 db_->Close();
1121 Database::Delete(db_path_);
1122 ASSERT_FALSE(base::PathExists(db_path_));
Victor Costance678e72018-07-24 10:25:001123
1124 // If the bots all had a restrictive umask setting such that databases are
1125 // always created with only the owner able to read them, then the code could
1126 // break without breaking the tests. Temporarily provide a more permissive
1127 // umask.
[email protected]81a2a602013-07-17 19:10:361128 ScopedUmaskSetter permissive_umask(S_IWGRP | S_IWOTH);
Victor Costance678e72018-07-24 10:25:001129
Victor Costan49a903a2021-05-07 22:21:001130 ASSERT_TRUE(db_->Open(db_path_));
[email protected]81a2a602013-07-17 19:10:361131
Victor Costance678e72018-07-24 10:25:001132 // Cause the journal file to be created. If the default journal_mode is
1133 // changed back to DELETE, this test will need to be updated.
Victor Costan49a903a2021-05-07 22:21:001134 EXPECT_TRUE(db_->Execute("CREATE TABLE x (x)"));
[email protected]81a2a602013-07-17 19:10:361135
[email protected]81a2a602013-07-17 19:10:361136 int mode;
Victor Costan49a903a2021-05-07 22:21:001137 ASSERT_TRUE(base::PathExists(db_path_));
1138 EXPECT_TRUE(base::GetPosixFilePermissions(db_path_, &mode));
Victor Costance678e72018-07-24 10:25:001139 ASSERT_EQ(mode, 0600);
[email protected]81a2a602013-07-17 19:10:361140
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571141 if (IsWALEnabled()) { // WAL mode
1142 // The WAL file is created lazily on first change.
Victor Costan49a903a2021-05-07 22:21:001143 ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)"));
[email protected]81a2a602013-07-17 19:10:361144
Victor Costan49a903a2021-05-07 22:21:001145 base::FilePath wal_path = Database::WriteAheadLogPath(db_path_);
1146 ASSERT_TRUE(base::PathExists(wal_path));
Victor Costance678e72018-07-24 10:25:001147 EXPECT_TRUE(base::GetPosixFilePermissions(wal_path, &mode));
1148 ASSERT_EQ(mode, 0600);
1149
Shubham Aggarwal7b60fe6e2020-10-15 06:00:281150 // The shm file doesn't exist in exclusive locking mode.
Victor Costan49a903a2021-05-07 22:21:001151 if (ExecuteWithResult(db_.get(), "PRAGMA locking_mode") == "normal") {
1152 base::FilePath shm_path = Database::SharedMemoryFilePath(db_path_);
1153 ASSERT_TRUE(base::PathExists(shm_path));
Shubham Aggarwal7b60fe6e2020-10-15 06:00:281154 EXPECT_TRUE(base::GetPosixFilePermissions(shm_path, &mode));
1155 ASSERT_EQ(mode, 0600);
1156 }
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571157 } else { // Truncate mode
Victor Costan49a903a2021-05-07 22:21:001158 base::FilePath journal_path = Database::JournalPath(db_path_);
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571159 DLOG(ERROR) << "journal_path: " << journal_path;
Victor Costan49a903a2021-05-07 22:21:001160 ASSERT_TRUE(base::PathExists(journal_path));
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571161 EXPECT_TRUE(base::GetPosixFilePermissions(journal_path, &mode));
1162 ASSERT_EQ(mode, 0600);
Victor Costance678e72018-07-24 10:25:001163 }
[email protected]81a2a602013-07-17 19:10:361164}
Wez35539132018-07-17 11:26:051165#endif // defined(OS_POSIX)
[email protected]81a2a602013-07-17 19:10:361166
[email protected]8d409412013-07-19 18:25:301167// Test that errors start happening once Poison() is called.
Shubham Aggarwal003708982020-10-28 17:57:541168TEST_P(SQLDatabaseTest, Poison) {
Victor Costan49a903a2021-05-07 22:21:001169 EXPECT_TRUE(db_->Execute("CREATE TABLE x (x)"));
[email protected]8d409412013-07-19 18:25:301170
1171 // Before the Poison() call, things generally work.
Victor Costan49a903a2021-05-07 22:21:001172 EXPECT_TRUE(db_->IsSQLValid("INSERT INTO x VALUES ('x')"));
1173 EXPECT_TRUE(db_->Execute("INSERT INTO x VALUES ('x')"));
[email protected]8d409412013-07-19 18:25:301174 {
Victor Costan49a903a2021-05-07 22:21:001175 Statement s(db_->GetUniqueStatement("SELECT COUNT(*) FROM x"));
[email protected]8d409412013-07-19 18:25:301176 ASSERT_TRUE(s.is_valid());
1177 ASSERT_TRUE(s.Step());
1178 }
1179
1180 // Get a statement which is valid before and will exist across Poison().
Victor Costan49a903a2021-05-07 22:21:001181 Statement valid_statement(
1182 db_->GetUniqueStatement("SELECT COUNT(*) FROM sqlite_master"));
[email protected]8d409412013-07-19 18:25:301183 ASSERT_TRUE(valid_statement.is_valid());
1184 ASSERT_TRUE(valid_statement.Step());
1185 valid_statement.Reset(true);
1186
Victor Costan49a903a2021-05-07 22:21:001187 db_->Poison();
[email protected]8d409412013-07-19 18:25:301188
1189 // After the Poison() call, things fail.
Victor Costan49a903a2021-05-07 22:21:001190 EXPECT_FALSE(db_->IsSQLValid("INSERT INTO x VALUES ('x')"));
1191 EXPECT_FALSE(db_->Execute("INSERT INTO x VALUES ('x')"));
[email protected]8d409412013-07-19 18:25:301192 {
Victor Costan49a903a2021-05-07 22:21:001193 Statement s(db_->GetUniqueStatement("SELECT COUNT(*) FROM x"));
[email protected]8d409412013-07-19 18:25:301194 ASSERT_FALSE(s.is_valid());
1195 ASSERT_FALSE(s.Step());
1196 }
1197
1198 // The existing statement has become invalid.
1199 ASSERT_FALSE(valid_statement.is_valid());
1200 ASSERT_FALSE(valid_statement.Step());
shess644fc8a2016-02-26 18:15:581201
1202 // Test that poisoning the database during a transaction works (with errors).
1203 // RazeErrorCallback() poisons the database, the extra COMMIT causes
Victor Costan49a903a2021-05-07 22:21:001204 // CommitTransaction() to throw an error while committing.
1205 db_->set_error_callback(
1206 base::BindRepeating(&RazeErrorCallback, db_.get(), SQLITE_ERROR));
1207 db_->Close();
1208 ASSERT_TRUE(db_->Open(db_path_));
1209 EXPECT_TRUE(db_->BeginTransaction());
1210 EXPECT_TRUE(db_->Execute("INSERT INTO x VALUES ('x')"));
1211 EXPECT_TRUE(db_->Execute("COMMIT"));
1212 EXPECT_FALSE(db_->CommitTransaction());
[email protected]8d409412013-07-19 18:25:301213}
1214
Shubham Aggarwal003708982020-10-28 17:57:541215TEST_P(SQLDatabaseTest, AttachDatabase) {
Victor Costan49a903a2021-05-07 22:21:001216 EXPECT_TRUE(db_->Execute("CREATE TABLE foo (a, b)"));
[email protected]8d409412013-07-19 18:25:301217
1218 // Create a database to attach to.
1219 base::FilePath attach_path =
Victor Costan49a903a2021-05-07 22:21:001220 db_path_.DirName().AppendASCII("SQLDatabaseAttach.db");
Victor Costan1d868352018-06-26 19:06:481221 static const char kAttachmentPoint[] = "other";
[email protected]8d409412013-07-19 18:25:301222 {
Victor Costan49a903a2021-05-07 22:21:001223 Database other_db;
[email protected]8d409412013-07-19 18:25:301224 ASSERT_TRUE(other_db.Open(attach_path));
1225 EXPECT_TRUE(other_db.Execute("CREATE TABLE bar (a, b)"));
1226 EXPECT_TRUE(other_db.Execute("INSERT INTO bar VALUES ('hello', 'world')"));
1227 }
1228
1229 // Cannot see the attached database, yet.
Victor Costan49a903a2021-05-07 22:21:001230 EXPECT_FALSE(db_->IsSQLValid("SELECT count(*) from other.bar"));
[email protected]8d409412013-07-19 18:25:301231
Victor Costan49a903a2021-05-07 22:21:001232 EXPECT_TRUE(DatabaseTestPeer::AttachDatabase(db_.get(), attach_path,
1233 kAttachmentPoint));
1234 EXPECT_TRUE(db_->IsSQLValid("SELECT count(*) from other.bar"));
[email protected]8d409412013-07-19 18:25:301235
Victor Costan8a87f7e52017-11-10 01:29:301236 // Queries can touch both databases after the ATTACH.
Victor Costan49a903a2021-05-07 22:21:001237 EXPECT_TRUE(db_->Execute("INSERT INTO foo SELECT a, b FROM other.bar"));
[email protected]8d409412013-07-19 18:25:301238 {
Victor Costan49a903a2021-05-07 22:21:001239 Statement s(db_->GetUniqueStatement("SELECT COUNT(*) FROM foo"));
[email protected]8d409412013-07-19 18:25:301240 ASSERT_TRUE(s.Step());
1241 EXPECT_EQ(1, s.ColumnInt(0));
1242 }
1243
Victor Costan49a903a2021-05-07 22:21:001244 EXPECT_TRUE(DatabaseTestPeer::DetachDatabase(db_.get(), kAttachmentPoint));
1245 EXPECT_FALSE(db_->IsSQLValid("SELECT count(*) from other.bar"));
Victor Costan8a87f7e52017-11-10 01:29:301246}
1247
Shubham Aggarwal003708982020-10-28 17:57:541248TEST_P(SQLDatabaseTest, AttachDatabaseWithOpenTransaction) {
Victor Costan49a903a2021-05-07 22:21:001249 EXPECT_TRUE(db_->Execute("CREATE TABLE foo (a, b)"));
Victor Costan8a87f7e52017-11-10 01:29:301250
1251 // Create a database to attach to.
1252 base::FilePath attach_path =
Victor Costan49a903a2021-05-07 22:21:001253 db_path_.DirName().AppendASCII("SQLDatabaseAttach.db");
Victor Costan1d868352018-06-26 19:06:481254 static const char kAttachmentPoint[] = "other";
Victor Costan8a87f7e52017-11-10 01:29:301255 {
Victor Costan49a903a2021-05-07 22:21:001256 Database other_db;
Victor Costan8a87f7e52017-11-10 01:29:301257 ASSERT_TRUE(other_db.Open(attach_path));
1258 EXPECT_TRUE(other_db.Execute("CREATE TABLE bar (a, b)"));
1259 EXPECT_TRUE(other_db.Execute("INSERT INTO bar VALUES ('hello', 'world')"));
1260 }
1261
1262 // Cannot see the attached database, yet.
Victor Costan49a903a2021-05-07 22:21:001263 EXPECT_FALSE(db_->IsSQLValid("SELECT count(*) from other.bar"));
Victor Costan8a87f7e52017-11-10 01:29:301264
Victor Costan8a87f7e52017-11-10 01:29:301265 // Attach succeeds in a transaction.
Victor Costan49a903a2021-05-07 22:21:001266 EXPECT_TRUE(db_->BeginTransaction());
1267 EXPECT_TRUE(DatabaseTestPeer::AttachDatabase(db_.get(), attach_path,
1268 kAttachmentPoint));
1269 EXPECT_TRUE(db_->IsSQLValid("SELECT count(*) from other.bar"));
Victor Costan8a87f7e52017-11-10 01:29:301270
1271 // Queries can touch both databases after the ATTACH.
Victor Costan49a903a2021-05-07 22:21:001272 EXPECT_TRUE(db_->Execute("INSERT INTO foo SELECT a, b FROM other.bar"));
Victor Costan8a87f7e52017-11-10 01:29:301273 {
Victor Costan49a903a2021-05-07 22:21:001274 Statement s(db_->GetUniqueStatement("SELECT COUNT(*) FROM foo"));
Victor Costan8a87f7e52017-11-10 01:29:301275 ASSERT_TRUE(s.Step());
1276 EXPECT_EQ(1, s.ColumnInt(0));
1277 }
1278
1279 // Detaching the same database fails, database is locked in the transaction.
1280 {
1281 sql::test::ScopedErrorExpecter expecter;
1282 expecter.ExpectError(SQLITE_ERROR);
Victor Costan49a903a2021-05-07 22:21:001283 EXPECT_FALSE(DatabaseTestPeer::DetachDatabase(db_.get(), kAttachmentPoint));
1284 EXPECT_TRUE(db_->IsSQLValid("SELECT count(*) from other.bar"));
shess976814402016-06-21 06:56:251285 ASSERT_TRUE(expecter.SawExpectedErrors());
[email protected]8d409412013-07-19 18:25:301286 }
1287
Victor Costan8a87f7e52017-11-10 01:29:301288 // Detach succeeds when the transaction is closed.
Victor Costan49a903a2021-05-07 22:21:001289 db_->RollbackTransaction();
1290 EXPECT_TRUE(DatabaseTestPeer::DetachDatabase(db_.get(), kAttachmentPoint));
1291 EXPECT_FALSE(db_->IsSQLValid("SELECT count(*) from other.bar"));
[email protected]8d409412013-07-19 18:25:301292}
1293
Shubham Aggarwal003708982020-10-28 17:57:541294TEST_P(SQLDatabaseTest, Basic_QuickIntegrityCheck) {
[email protected]579446c2013-12-16 18:36:521295 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
Victor Costan49a903a2021-05-07 22:21:001296 ASSERT_TRUE(db_->Execute(kCreateSql));
1297 EXPECT_TRUE(db_->QuickIntegrityCheck());
1298 db_->Close();
[email protected]579446c2013-12-16 18:36:521299
Victor Costan49a903a2021-05-07 22:21:001300 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_));
[email protected]579446c2013-12-16 18:36:521301
1302 {
shess976814402016-06-21 06:56:251303 sql::test::ScopedErrorExpecter expecter;
1304 expecter.ExpectError(SQLITE_CORRUPT);
Victor Costan49a903a2021-05-07 22:21:001305 ASSERT_TRUE(db_->Open(db_path_));
1306 EXPECT_FALSE(db_->QuickIntegrityCheck());
shess976814402016-06-21 06:56:251307 ASSERT_TRUE(expecter.SawExpectedErrors());
[email protected]579446c2013-12-16 18:36:521308 }
1309}
1310
Shubham Aggarwal003708982020-10-28 17:57:541311TEST_P(SQLDatabaseTest, Basic_FullIntegrityCheck) {
[email protected]579446c2013-12-16 18:36:521312 const std::string kOk("ok");
1313 std::vector<std::string> messages;
1314
1315 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
Victor Costan49a903a2021-05-07 22:21:001316 ASSERT_TRUE(db_->Execute(kCreateSql));
1317 EXPECT_TRUE(db_->FullIntegrityCheck(&messages));
[email protected]579446c2013-12-16 18:36:521318 EXPECT_EQ(1u, messages.size());
1319 EXPECT_EQ(kOk, messages[0]);
Victor Costan49a903a2021-05-07 22:21:001320 db_->Close();
[email protected]579446c2013-12-16 18:36:521321
Victor Costan49a903a2021-05-07 22:21:001322 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_));
[email protected]579446c2013-12-16 18:36:521323
1324 {
shess976814402016-06-21 06:56:251325 sql::test::ScopedErrorExpecter expecter;
1326 expecter.ExpectError(SQLITE_CORRUPT);
Victor Costan49a903a2021-05-07 22:21:001327 ASSERT_TRUE(db_->Open(db_path_));
1328 EXPECT_TRUE(db_->FullIntegrityCheck(&messages));
[email protected]579446c2013-12-16 18:36:521329 EXPECT_LT(1u, messages.size());
1330 EXPECT_NE(kOk, messages[0]);
shess976814402016-06-21 06:56:251331 ASSERT_TRUE(expecter.SawExpectedErrors());
[email protected]579446c2013-12-16 18:36:521332 }
1333
1334 // TODO(shess): CorruptTableOrIndex could be used to produce a
1335 // file that would pass the quick check and fail the full check.
1336}
1337
Shubham Aggarwal003708982020-10-28 17:57:541338TEST_P(SQLDatabaseTest, OnMemoryDump) {
ssid9f8022f2015-10-12 17:49:031339 base::trace_event::MemoryDumpArgs args = {
1340 base::trace_event::MemoryDumpLevelOfDetail::DETAILED};
erikchenf62ea042018-05-25 21:30:571341 base::trace_event::ProcessMemoryDump pmd(args);
Victor Costan49a903a2021-05-07 22:21:001342 ASSERT_TRUE(db_->memory_dump_provider_->OnMemoryDump(args, &pmd));
ssid9f8022f2015-10-12 17:49:031343 EXPECT_GE(pmd.allocator_dumps().size(), 1u);
1344}
1345
shessc8cd2a162015-10-22 20:30:461346// Test that the functions to collect diagnostic data run to completion, without
1347// worrying too much about what they generate (since that will change).
Shubham Aggarwal003708982020-10-28 17:57:541348TEST_P(SQLDatabaseTest, CollectDiagnosticInfo) {
Victor Costan49a903a2021-05-07 22:21:001349 const std::string corruption_info = db_->CollectCorruptionInfo();
shessc8cd2a162015-10-22 20:30:461350 EXPECT_NE(std::string::npos, corruption_info.find("SQLITE_CORRUPT"));
1351 EXPECT_NE(std::string::npos, corruption_info.find("integrity_check"));
shessc8cd2a162015-10-22 20:30:461352
1353 // A statement to see in the results.
1354 const char* kSimpleSql = "SELECT 'mountain'";
Victor Costan49a903a2021-05-07 22:21:001355 Statement s(db_->GetCachedStatement(SQL_FROM_HERE, kSimpleSql));
shessc8cd2a162015-10-22 20:30:461356
1357 // Error includes the statement.
Victor Costan49a903a2021-05-07 22:21:001358 const std::string readonly_info = db_->CollectErrorInfo(SQLITE_READONLY, &s);
shessc8cd2a162015-10-22 20:30:461359 EXPECT_NE(std::string::npos, readonly_info.find(kSimpleSql));
1360
1361 // Some other error doesn't include the statment.
1362 // TODO(shess): This is weak.
Victor Costan49a903a2021-05-07 22:21:001363 const std::string full_info = db_->CollectErrorInfo(SQLITE_FULL, nullptr);
shessc8cd2a162015-10-22 20:30:461364 EXPECT_EQ(std::string::npos, full_info.find(kSimpleSql));
1365
1366 // A table to see in the SQLITE_ERROR results.
Victor Costan49a903a2021-05-07 22:21:001367 EXPECT_TRUE(db_->Execute("CREATE TABLE volcano (x)"));
shessc8cd2a162015-10-22 20:30:461368
1369 // Version info to see in the SQLITE_ERROR results.
Victor Costan49a903a2021-05-07 22:21:001370 MetaTable meta_table;
1371 ASSERT_TRUE(meta_table.Init(db_.get(), 4, 4));
shessc8cd2a162015-10-22 20:30:461372
Victor Costan49a903a2021-05-07 22:21:001373 const std::string error_info = db_->CollectErrorInfo(SQLITE_ERROR, &s);
shessc8cd2a162015-10-22 20:30:461374 EXPECT_NE(std::string::npos, error_info.find(kSimpleSql));
1375 EXPECT_NE(std::string::npos, error_info.find("volcano"));
1376 EXPECT_NE(std::string::npos, error_info.find("version: 4"));
1377}
1378
shess9bf2c672015-12-18 01:18:081379// Test that a fresh database has mmap enabled by default, if mmap'ed I/O is
1380// enabled by SQLite.
Shubham Aggarwal003708982020-10-28 17:57:541381TEST_P(SQLDatabaseTest, MmapInitiallyEnabled) {
shess9bf2c672015-12-18 01:18:081382 {
Victor Costan49a903a2021-05-07 22:21:001383 Statement s(db_->GetUniqueStatement("PRAGMA mmap_size"));
Victor Costan42988a92018-02-06 02:22:141384 ASSERT_TRUE(s.Step())
1385 << "All supported SQLite versions should have mmap support";
shess9bf2c672015-12-18 01:18:081386
1387 // If mmap I/O is not on, attempt to turn it on. If that succeeds, then
1388 // Open() should have turned it on. If mmap support is disabled, 0 is
1389 // returned. If the VFS does not understand SQLITE_FCNTL_MMAP_SIZE (for
1390 // instance MojoVFS), -1 is returned.
1391 if (s.ColumnInt(0) <= 0) {
Victor Costan49a903a2021-05-07 22:21:001392 ASSERT_TRUE(db_->Execute("PRAGMA mmap_size = 1048576"));
shess9bf2c672015-12-18 01:18:081393 s.Reset(true);
1394 ASSERT_TRUE(s.Step());
1395 EXPECT_LE(s.ColumnInt(0), 0);
1396 }
1397 }
1398
1399 // Test that explicit disable prevents mmap'ed I/O.
Victor Costan49a903a2021-05-07 22:21:001400 db_->Close();
1401 Database::Delete(db_path_);
1402 db_->set_mmap_disabled();
1403 ASSERT_TRUE(db_->Open(db_path_));
1404 EXPECT_EQ("0", ExecuteWithResult(db_.get(), "PRAGMA mmap_size"));
shessa62504d2016-11-07 19:26:121405}
1406
1407// Test whether a fresh database gets mmap enabled when using alternate status
1408// storage.
Shubham Aggarwal003708982020-10-28 17:57:541409TEST_P(SQLDatabaseTest, MmapInitiallyEnabledAltStatus) {
shessa62504d2016-11-07 19:26:121410 // Re-open fresh database with alt-status flag set.
Victor Costan49a903a2021-05-07 22:21:001411 db_->Close();
1412 Database::Delete(db_path_);
Victor Costan8ec18ee42021-07-13 19:45:321413
1414 DatabaseOptions options = GetDBOptions();
1415 options.mmap_alt_status_discouraged = true;
Victor Costanfe078f92021-07-19 20:02:591416 options.enable_views_discouraged = true;
Victor Costan8ec18ee42021-07-13 19:45:321417 db_ = std::make_unique<Database>(options);
Victor Costan49a903a2021-05-07 22:21:001418 ASSERT_TRUE(db_->Open(db_path_));
shessa62504d2016-11-07 19:26:121419
shess9bf2c672015-12-18 01:18:081420 {
Victor Costan49a903a2021-05-07 22:21:001421 Statement s(db_->GetUniqueStatement("PRAGMA mmap_size"));
Victor Costan42988a92018-02-06 02:22:141422 ASSERT_TRUE(s.Step())
1423 << "All supported SQLite versions should have mmap support";
shessa62504d2016-11-07 19:26:121424
1425 // If mmap I/O is not on, attempt to turn it on. If that succeeds, then
1426 // Open() should have turned it on. If mmap support is disabled, 0 is
1427 // returned. If the VFS does not understand SQLITE_FCNTL_MMAP_SIZE (for
1428 // instance MojoVFS), -1 is returned.
1429 if (s.ColumnInt(0) <= 0) {
Victor Costan49a903a2021-05-07 22:21:001430 ASSERT_TRUE(db_->Execute("PRAGMA mmap_size = 1048576"));
shessa62504d2016-11-07 19:26:121431 s.Reset(true);
1432 ASSERT_TRUE(s.Step());
1433 EXPECT_LE(s.ColumnInt(0), 0);
1434 }
shess9bf2c672015-12-18 01:18:081435 }
shessa62504d2016-11-07 19:26:121436
1437 // Test that explicit disable overrides set_mmap_alt_status().
Victor Costan49a903a2021-05-07 22:21:001438 db_->Close();
1439 Database::Delete(db_path_);
1440 db_->set_mmap_disabled();
1441 ASSERT_TRUE(db_->Open(db_path_));
1442 EXPECT_EQ("0", ExecuteWithResult(db_.get(), "PRAGMA mmap_size"));
shess9bf2c672015-12-18 01:18:081443}
1444
Shubham Aggarwal003708982020-10-28 17:57:541445TEST_P(SQLDatabaseTest, GetAppropriateMmapSize) {
shess9bf2c672015-12-18 01:18:081446 const size_t kMmapAlot = 25 * 1024 * 1024;
shess9e77283d2016-06-13 23:53:201447 int64_t mmap_status = MetaTable::kMmapFailure;
shess9bf2c672015-12-18 01:18:081448
1449 // If there is no meta table (as for a fresh database), assume that everything
shess9e77283d2016-06-13 23:53:201450 // should be mapped, and the status of the meta table is not affected.
Victor Costan49a903a2021-05-07 22:21:001451 ASSERT_TRUE(!db_->DoesTableExist("meta"));
1452 ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot);
1453 ASSERT_TRUE(!db_->DoesTableExist("meta"));
shess9bf2c672015-12-18 01:18:081454
1455 // When the meta table is first created, it sets up to map everything.
Victor Costan49a903a2021-05-07 22:21:001456 MetaTable().Init(db_.get(), 1, 1);
1457 ASSERT_TRUE(db_->DoesTableExist("meta"));
1458 ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot);
1459 ASSERT_TRUE(MetaTable::GetMmapStatus(db_.get(), &mmap_status));
shess9bf2c672015-12-18 01:18:081460 ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status);
1461
shessa7b07acd2017-03-19 23:59:381462 // Preload with partial progress of one page. Should map everything.
Victor Costan49a903a2021-05-07 22:21:001463 ASSERT_TRUE(db_->Execute("REPLACE INTO meta VALUES ('mmap_status', 1)"));
1464 ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot);
1465 ASSERT_TRUE(MetaTable::GetMmapStatus(db_.get(), &mmap_status));
shessa7b07acd2017-03-19 23:59:381466 ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status);
1467
shess9bf2c672015-12-18 01:18:081468 // Failure status maps nothing.
Victor Costan49a903a2021-05-07 22:21:001469 ASSERT_TRUE(db_->Execute("REPLACE INTO meta VALUES ('mmap_status', -2)"));
1470 ASSERT_EQ(0UL, db_->GetAppropriateMmapSize());
shess9bf2c672015-12-18 01:18:081471
1472 // Re-initializing the meta table does not re-create the key if the table
1473 // already exists.
Victor Costan49a903a2021-05-07 22:21:001474 ASSERT_TRUE(db_->Execute("DELETE FROM meta WHERE key = 'mmap_status'"));
1475 MetaTable().Init(db_.get(), 1, 1);
shess9bf2c672015-12-18 01:18:081476 ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status);
Victor Costan49a903a2021-05-07 22:21:001477 ASSERT_TRUE(MetaTable::GetMmapStatus(db_.get(), &mmap_status));
shess9bf2c672015-12-18 01:18:081478 ASSERT_EQ(0, mmap_status);
1479
1480 // With no key, map everything and create the key.
1481 // TODO(shess): This really should be "maps everything after validating it",
1482 // but that is more complicated to structure.
Victor Costan49a903a2021-05-07 22:21:001483 ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot);
1484 ASSERT_TRUE(MetaTable::GetMmapStatus(db_.get(), &mmap_status));
shess9bf2c672015-12-18 01:18:081485 ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status);
1486}
shess9bf2c672015-12-18 01:18:081487
Shubham Aggarwal003708982020-10-28 17:57:541488TEST_P(SQLDatabaseTest, GetAppropriateMmapSizeAltStatus) {
shessa62504d2016-11-07 19:26:121489 const size_t kMmapAlot = 25 * 1024 * 1024;
1490
Victor Costancfbfa602018-08-01 23:24:461491 // At this point, Database still expects a future [meta] table.
Victor Costan49a903a2021-05-07 22:21:001492 ASSERT_FALSE(db_->DoesTableExist("meta"));
1493 ASSERT_FALSE(db_->DoesViewExist("MmapStatus"));
1494 ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot);
1495 ASSERT_FALSE(db_->DoesTableExist("meta"));
1496 ASSERT_FALSE(db_->DoesViewExist("MmapStatus"));
shessa62504d2016-11-07 19:26:121497
1498 // Using alt status, everything should be mapped, with state in the view.
Victor Costan8ec18ee42021-07-13 19:45:321499 DatabaseOptions options = GetDBOptions();
1500 options.mmap_alt_status_discouraged = true;
Victor Costanfe078f92021-07-19 20:02:591501 options.enable_views_discouraged = true;
Victor Costan8ec18ee42021-07-13 19:45:321502 db_ = std::make_unique<Database>(options);
1503 ASSERT_TRUE(db_->Open(db_path_));
1504
Victor Costan49a903a2021-05-07 22:21:001505 ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot);
1506 ASSERT_FALSE(db_->DoesTableExist("meta"));
1507 ASSERT_TRUE(db_->DoesViewExist("MmapStatus"));
Raul Tambre6c708e32019-02-08 22:35:141508 EXPECT_EQ(base::NumberToString(MetaTable::kMmapSuccess),
Victor Costan49a903a2021-05-07 22:21:001509 ExecuteWithResult(db_.get(), "SELECT * FROM MmapStatus"));
shessa62504d2016-11-07 19:26:121510
shessa7b07acd2017-03-19 23:59:381511 // Also maps everything when kMmapSuccess is already in the view.
Victor Costan49a903a2021-05-07 22:21:001512 ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot);
shessa62504d2016-11-07 19:26:121513
shessa7b07acd2017-03-19 23:59:381514 // Preload with partial progress of one page. Should map everything.
Victor Costan49a903a2021-05-07 22:21:001515 ASSERT_TRUE(db_->Execute("DROP VIEW MmapStatus"));
1516 ASSERT_TRUE(db_->Execute("CREATE VIEW MmapStatus (value) AS SELECT 1"));
1517 ASSERT_GT(db_->GetAppropriateMmapSize(), kMmapAlot);
Raul Tambre6c708e32019-02-08 22:35:141518 EXPECT_EQ(base::NumberToString(MetaTable::kMmapSuccess),
Victor Costan49a903a2021-05-07 22:21:001519 ExecuteWithResult(db_.get(), "SELECT * FROM MmapStatus"));
shessa7b07acd2017-03-19 23:59:381520
shessa62504d2016-11-07 19:26:121521 // Failure status leads to nothing being mapped.
Victor Costan49a903a2021-05-07 22:21:001522 ASSERT_TRUE(db_->Execute("DROP VIEW MmapStatus"));
1523 ASSERT_TRUE(db_->Execute("CREATE VIEW MmapStatus (value) AS SELECT -2"));
1524 ASSERT_EQ(0UL, db_->GetAppropriateMmapSize());
Raul Tambre6c708e32019-02-08 22:35:141525 EXPECT_EQ(base::NumberToString(MetaTable::kMmapFailure),
Victor Costan49a903a2021-05-07 22:21:001526 ExecuteWithResult(db_.get(), "SELECT * FROM MmapStatus"));
shessa62504d2016-11-07 19:26:121527}
1528
Shubham Aggarwal003708982020-10-28 17:57:541529TEST_P(SQLDatabaseTest, GetMemoryUsage) {
Victor Costand6e73252020-10-14 21:11:251530 // Databases with mmap enabled may not follow the assumptions below.
Victor Costan49a903a2021-05-07 22:21:001531 db_->Close();
1532 db_->set_mmap_disabled();
1533 ASSERT_TRUE(db_->Open(db_path_));
Victor Costand6e73252020-10-14 21:11:251534
Victor Costan49a903a2021-05-07 22:21:001535 int initial_memory = db_->GetMemoryUsage();
Victor Costand6e73252020-10-14 21:11:251536 EXPECT_GT(initial_memory, 0)
1537 << "SQLite should always use some memory for a database";
1538
Victor Costan49a903a2021-05-07 22:21:001539 ASSERT_TRUE(db_->Execute("CREATE TABLE foo (a, b)"));
1540 ASSERT_TRUE(db_->Execute("INSERT INTO foo(a, b) VALUES (12, 13)"));
Victor Costand6e73252020-10-14 21:11:251541
Victor Costan49a903a2021-05-07 22:21:001542 int post_query_memory = db_->GetMemoryUsage();
Victor Costand6e73252020-10-14 21:11:251543 EXPECT_GT(post_query_memory, initial_memory)
1544 << "Page cache usage should go up after executing queries";
1545
Victor Costan49a903a2021-05-07 22:21:001546 db_->TrimMemory();
1547 int post_trim_memory = db_->GetMemoryUsage();
Victor Costand6e73252020-10-14 21:11:251548 EXPECT_GT(post_query_memory, post_trim_memory)
1549 << "Page cache usage should go down after calling TrimMemory()";
1550}
1551
Victor Costan4e442d02021-07-20 17:43:131552TEST_P(SQLDatabaseTest, DoubleQuotedStringLiteralsDisabledByDefault) {
1553 ASSERT_TRUE(db_->Execute("CREATE TABLE data(item TEXT NOT NULL);"));
1554
1555 struct TestCase {
1556 const char* sql;
1557 bool is_valid;
1558 };
1559 std::vector<TestCase> test_cases = {
1560 // DML tests.
1561 {"SELECT item FROM data WHERE item >= 'string literal'", true},
1562 {"SELECT item FROM data WHERE item >= \"string literal\"", false},
1563 {"INSERT INTO data(item) VALUES('string literal')", true},
1564 {"INSERT INTO data(item) VALUES(\"string literal\")", false},
1565 {"UPDATE data SET item = 'string literal'", true},
1566 {"UPDATE data SET item = \"string literal\"", false},
1567 {"DELETE FROM data WHERE item >= 'string literal'", true},
1568 {"DELETE FROM data WHERE item >= \"string literal\"", false},
1569
1570 // DDL tests.
1571 {"CREATE INDEX data_item ON data(item) WHERE item >= 'string literal'",
1572 true},
1573 {"CREATE INDEX data_item ON data(item) WHERE item >= \"string literal\"",
1574 false},
1575 {"CREATE TABLE data2(item TEXT DEFAULT 'string literal')", true},
1576
1577 // This should be an invalid DDL statement, due to the double-quoted
1578 // string literal. However, SQLite currently parses it.
1579 {"CREATE TABLE data2(item TEXT DEFAULT \"string literal\")", true},
1580 };
1581
1582 for (const TestCase& test_case : test_cases) {
1583 SCOPED_TRACE(test_case.sql);
1584
1585 EXPECT_EQ(test_case.is_valid, db_->IsSQLValid(test_case.sql));
1586 }
1587}
1588
Victor Costan7c234822021-07-13 03:03:021589TEST_P(SQLDatabaseTest, TriggersDisabledByDefault) {
1590 ASSERT_TRUE(db_->Execute("CREATE TABLE data(id INTEGER)"));
1591
1592 // sqlite3_db_config() currently only disables running triggers. Schema
1593 // operations on triggers are still allowed.
1594 EXPECT_TRUE(
1595 db_->Execute("CREATE TRIGGER trigger AFTER INSERT ON data "
1596 "BEGIN DELETE FROM data; END"));
1597
1598 ASSERT_TRUE(db_->Execute("INSERT INTO data(id) VALUES(42)"));
1599
1600 Statement select(db_->GetUniqueStatement("SELECT id FROM data"));
1601 EXPECT_TRUE(select.Step())
1602 << "If the trigger did not run, the table should not be empty.";
1603 EXPECT_EQ(42, select.ColumnInt64(0));
1604
1605 // sqlite3_db_config() currently only disables running triggers. Schema
1606 // operations on triggers are still allowed.
1607 EXPECT_TRUE(db_->Execute("DROP TRIGGER IF EXISTS trigger"));
1608}
1609
Victor Costanfe078f92021-07-19 20:02:591610TEST_P(SQLDatabaseTest, ViewsDisabledByDefault) {
1611 EXPECT_FALSE(GetDBOptions().enable_views_discouraged);
1612
1613 // sqlite3_db_config() currently only disables querying views. Schema
1614 // operations on views are still allowed.
1615 ASSERT_TRUE(db_->Execute("CREATE VIEW view(id) AS SELECT 1"));
1616
1617 {
1618 sql::test::ScopedErrorExpecter expecter;
1619 expecter.ExpectError(SQLITE_ERROR);
1620 Statement select_from_view(db_->GetUniqueStatement("SELECT id FROM view"));
1621 EXPECT_FALSE(select_from_view.is_valid());
1622 EXPECT_TRUE(expecter.SawExpectedErrors());
1623 }
1624
1625 // sqlite3_db_config() currently only disables querying views. Schema
1626 // operations on views are still allowed.
1627 EXPECT_TRUE(db_->Execute("DROP VIEW IF EXISTS view"));
1628}
1629
1630TEST_P(SQLDatabaseTest, ViewsEnabled) {
1631 DatabaseOptions options = GetDBOptions();
1632 options.enable_views_discouraged = true;
1633 db_ = std::make_unique<Database>(options);
1634 ASSERT_TRUE(db_->Open(db_path_));
1635
1636 ASSERT_TRUE(db_->Execute("CREATE VIEW view(id) AS SELECT 1"));
1637
1638 Statement select_from_view(db_->GetUniqueStatement("SELECT id FROM view"));
1639 ASSERT_TRUE(select_from_view.is_valid());
1640 EXPECT_TRUE(select_from_view.Step());
1641 EXPECT_EQ(1, select_from_view.ColumnInt64(0));
1642
1643 EXPECT_TRUE(db_->Execute("DROP VIEW IF EXISTS view"));
1644}
1645
Victor Costan04fc9092021-07-17 00:09:341646TEST_P(SQLDatabaseTest, VirtualTablesDisabledByDefault) {
1647 EXPECT_FALSE(GetDBOptions().enable_virtual_tables_discouraged);
1648
1649 // sqlite3_prepare_v3() currently only disables accessing virtual tables.
1650 // Schema operations on virtual tables are still allowed.
1651 ASSERT_TRUE(db_->Execute(
1652 "CREATE VIRTUAL TABLE fts_table USING fts3(data_table, content TEXT)"));
1653
1654 {
1655 sql::test::ScopedErrorExpecter expecter;
1656 expecter.ExpectError(SQLITE_ERROR);
1657 Statement select_from_vtable(db_->GetUniqueStatement(
1658 "SELECT content FROM fts_table WHERE content MATCH 'pattern'"));
1659 EXPECT_FALSE(select_from_vtable.is_valid());
1660 EXPECT_TRUE(expecter.SawExpectedErrors());
1661 }
1662
1663 // sqlite3_prepare_v3() currently only disables accessing virtual tables.
1664 // Schema operations on virtual tables are still allowed.
1665 EXPECT_TRUE(db_->Execute("DROP TABLE IF EXISTS fts_table"));
1666}
1667
1668TEST_P(SQLDatabaseTest, VirtualTablesEnabled) {
1669 DatabaseOptions options = GetDBOptions();
1670 options.enable_virtual_tables_discouraged = true;
1671 db_ = std::make_unique<Database>(options);
1672 ASSERT_TRUE(db_->Open(db_path_));
1673
1674 ASSERT_TRUE(db_->Execute(
1675 "CREATE VIRTUAL TABLE fts_table USING fts3(data_table, content TEXT)"));
1676
1677 Statement select_from_vtable(db_->GetUniqueStatement(
1678 "SELECT content FROM fts_table WHERE content MATCH 'pattern'"));
1679 ASSERT_TRUE(select_from_vtable.is_valid());
1680 EXPECT_FALSE(select_from_vtable.Step());
1681
1682 EXPECT_TRUE(db_->Execute("DROP TABLE IF EXISTS fts_table"));
1683}
1684
Victor Costan49a903a2021-05-07 22:21:001685class SQLDatabaseTestExclusiveMode : public testing::Test,
1686 public testing::WithParamInterface<bool> {
Shubham Aggarwal7b60fe6e2020-10-15 06:00:281687 public:
Victor Costan49a903a2021-05-07 22:21:001688 ~SQLDatabaseTestExclusiveMode() override = default;
1689
1690 void SetUp() override {
1691 db_ = std::make_unique<Database>(GetDBOptions());
1692 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
1693 db_path_ = temp_dir_.GetPath().AppendASCII("recovery_test.sqlite");
1694 ASSERT_TRUE(db_->Open(db_path_));
1695 }
Shubham Aggarwal003708982020-10-28 17:57:541696
1697 DatabaseOptions GetDBOptions() {
Victor Costan49a903a2021-05-07 22:21:001698 DatabaseOptions options;
1699 options.wal_mode = IsWALEnabled();
Shubham Aggarwal003708982020-10-28 17:57:541700 options.exclusive_locking = true;
1701 return options;
1702 }
Victor Costan49a903a2021-05-07 22:21:001703
1704 bool IsWALEnabled() { return GetParam(); }
1705
1706 protected:
1707 base::ScopedTempDir temp_dir_;
1708 base::FilePath db_path_;
1709 std::unique_ptr<Database> db_;
Shubham Aggarwal7b60fe6e2020-10-15 06:00:281710};
Victor Costanb2230792020-10-09 08:35:141711
Shubham Aggarwal003708982020-10-28 17:57:541712TEST_P(SQLDatabaseTestExclusiveMode, LockingModeExclusive) {
Victor Costan49a903a2021-05-07 22:21:001713 EXPECT_EQ(ExecuteWithResult(db_.get(), "PRAGMA locking_mode"), "exclusive");
Victor Costanb2230792020-10-09 08:35:141714}
1715
Shubham Aggarwal003708982020-10-28 17:57:541716TEST_P(SQLDatabaseTest, LockingModeNormal) {
Victor Costan49a903a2021-05-07 22:21:001717 EXPECT_EQ(ExecuteWithResult(db_.get(), "PRAGMA locking_mode"), "normal");
Victor Costanb2230792020-10-09 08:35:141718}
1719
Shubham Aggarwal003708982020-10-28 17:57:541720TEST_P(SQLDatabaseTest, OpenedInCorrectMode) {
Shubham Aggarwal7b60fe6e2020-10-15 06:00:281721 std::string expected_mode = IsWALEnabled() ? "wal" : "truncate";
Victor Costan49a903a2021-05-07 22:21:001722 EXPECT_EQ(ExecuteWithResult(db_.get(), "PRAGMA journal_mode"), expected_mode);
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571723}
1724
Shubham Aggarwal003708982020-10-28 17:57:541725TEST_P(SQLDatabaseTest, CheckpointDatabase) {
Shubham Aggarwal7b60fe6e2020-10-15 06:00:281726 if (!IsWALEnabled())
1727 return;
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571728
Victor Costan49a903a2021-05-07 22:21:001729 base::FilePath wal_path = Database::WriteAheadLogPath(db_path_);
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571730
1731 int64_t wal_size = 0;
1732 // WAL file initially empty.
Victor Costan49a903a2021-05-07 22:21:001733 EXPECT_TRUE(base::PathExists(wal_path));
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571734 base::GetFileSize(wal_path, &wal_size);
1735 EXPECT_EQ(wal_size, 0);
1736
1737 ASSERT_TRUE(
Victor Costan49a903a2021-05-07 22:21:001738 db_->Execute("CREATE TABLE foo (id INTEGER UNIQUE, value INTEGER)"));
1739 ASSERT_TRUE(db_->Execute("INSERT INTO foo VALUES (1, 1)"));
1740 ASSERT_TRUE(db_->Execute("INSERT INTO foo VALUES (2, 2)"));
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571741
1742 // Writes reach WAL file but not db file.
1743 base::GetFileSize(wal_path, &wal_size);
1744 EXPECT_GT(wal_size, 0);
1745
1746 int64_t db_size = 0;
Victor Costan49a903a2021-05-07 22:21:001747 base::GetFileSize(db_path_, &db_size);
1748 EXPECT_EQ(db_size, db_->page_size());
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571749
1750 // Checkpoint database to immediately propagate writes to DB file.
Victor Costan49a903a2021-05-07 22:21:001751 EXPECT_TRUE(db_->CheckpointDatabase());
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571752
Victor Costan49a903a2021-05-07 22:21:001753 base::GetFileSize(db_path_, &db_size);
1754 EXPECT_GT(db_size, db_->page_size());
1755 EXPECT_EQ(ExecuteWithResult(db_.get(), "SELECT value FROM foo where id=1"),
1756 "1");
1757 EXPECT_EQ(ExecuteWithResult(db_.get(), "SELECT value FROM foo where id=2"),
1758 "2");
Shubham Aggarwalbe4f97ce2020-06-19 15:58:571759}
1760
Shubham Aggarwal003708982020-10-28 17:57:541761TEST_P(SQLDatabaseTest, CorruptSizeInHeaderTest) {
Victor Costan49a903a2021-05-07 22:21:001762 ASSERT_TRUE(db_->Execute("CREATE TABLE foo (x)"));
1763 ASSERT_TRUE(db_->Execute("CREATE TABLE bar (x)"));
1764 db_->Close();
Shubham Aggarwal1c06850ffa2020-07-07 13:42:571765
Victor Costan49a903a2021-05-07 22:21:001766 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path_));
Shubham Aggarwal1c06850ffa2020-07-07 13:42:571767 {
1768 sql::test::ScopedErrorExpecter expecter;
1769 expecter.ExpectError(SQLITE_CORRUPT);
Victor Costan49a903a2021-05-07 22:21:001770 ASSERT_TRUE(db_->Open(db_path_));
1771 EXPECT_FALSE(db_->Execute("INSERT INTO foo values (1)"));
1772 EXPECT_FALSE(db_->DoesTableExist("foo"));
1773 EXPECT_FALSE(db_->DoesTableExist("bar"));
1774 EXPECT_FALSE(db_->Execute("SELECT * FROM foo"));
Shubham Aggarwal1c06850ffa2020-07-07 13:42:571775 EXPECT_TRUE(expecter.SawExpectedErrors());
1776 }
1777}
1778
Victor Costan15d905de2021-01-07 22:18:581779// WAL mode is currently not supported on Fuchsia.
1780#if !defined(OS_FUCHSIA)
1781INSTANTIATE_TEST_SUITE_P(JournalMode, SQLDatabaseTest, testing::Bool());
1782INSTANTIATE_TEST_SUITE_P(JournalMode,
1783 SQLDatabaseTestExclusiveMode,
1784 testing::Bool());
1785#else
1786INSTANTIATE_TEST_SUITE_P(JournalMode, SQLDatabaseTest, testing::Values(false));
1787INSTANTIATE_TEST_SUITE_P(JournalMode,
1788 SQLDatabaseTestExclusiveMode,
1789 testing::Values(false));
1790#endif
1791
shessc8cd2a162015-10-22 20:30:461792} // namespace sql