blob: 64cc7e3fe180e4c721d4a628f8a3d3b78ce73c44 [file] [log] [blame]
[email protected]60e60dd2012-04-28 08:16:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]67361b32011-04-12 20:13:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avi0b519202015-12-21 07:25:195#include <stddef.h>
6#include <stdint.h>
7
[email protected]67361b32011-04-12 20:13:068#include <string>
9
[email protected]526b4662013-06-14 04:09:1210#include "base/bind.h"
thestig22dfc4012014-09-05 08:29:4411#include "base/files/file_util.h"
shess53adf162015-12-17 22:07:2612#include "base/files/memory_mapped_file.h"
[email protected]ea1a3f62012-11-16 20:34:2313#include "base/files/scoped_temp_dir.h"
Scott Graham502836c2017-09-14 23:26:2314#include "build/build_config.h"
[email protected]f0a54b22011-07-19 18:40:2115#include "sql/connection.h"
16#include "sql/statement.h"
erg102ceb412015-06-20 01:38:1317#include "sql/test/sql_test_base.h"
engedybe80d53e2015-01-22 09:54:5018#include "sql/test/test_helpers.h"
[email protected]67361b32011-04-12 20:13:0619#include "testing/gtest/include/gtest/gtest.h"
20#include "third_party/sqlite/sqlite3.h"
21
shess5f2c3442017-01-24 02:15:1022#if defined(OS_MACOSX) && !defined(OS_IOS)
23#include <CoreFoundation/CoreFoundation.h>
24#include <CoreServices/CoreServices.h>
25
26#include "base/mac/mac_util.h"
27#include "base/mac/scoped_cftyperef.h"
28#endif
29
[email protected]67361b32011-04-12 20:13:0630// Test that certain features are/are-not enabled in our SQLite.
31
shessf7fcc452017-04-19 22:10:4132namespace sql {
[email protected]67361b32011-04-12 20:13:0633namespace {
34
shessf7fcc452017-04-19 22:10:4135using sql::test::ExecuteWithResult;
36using sql::test::ExecuteWithResults;
37
[email protected]526b4662013-06-14 04:09:1238void CaptureErrorCallback(int* error_pointer, std::string* sql_text,
39 int error, sql::Statement* stmt) {
40 *error_pointer = error;
Victor Costanbd623112018-07-18 04:17:2741 const char* text = stmt ? stmt->GetSQLStatement() : nullptr;
[email protected]526b4662013-06-14 04:09:1242 *sql_text = text ? text : "no statement available";
43}
[email protected]67361b32011-04-12 20:13:0644
shessf7fcc452017-04-19 22:10:4145} // namespace
46
erg102ceb412015-06-20 01:38:1347class SQLiteFeaturesTest : public sql::SQLTestBase {
[email protected]67361b32011-04-12 20:13:0648 public:
[email protected]49dc4f22012-10-17 17:41:1649 SQLiteFeaturesTest() : error_(SQLITE_OK) {}
[email protected]67361b32011-04-12 20:13:0650
dcheng1b3b125e2014-12-22 23:00:2451 void SetUp() override {
erg102ceb412015-06-20 01:38:1352 SQLTestBase::SetUp();
[email protected]99034682012-06-13 03:31:1653
[email protected]49dc4f22012-10-17 17:41:1654 // The error delegate will set |error_| and |sql_text_| when any sqlite
55 // statement operation returns an error code.
erg102ceb412015-06-20 01:38:1356 db().set_error_callback(
tzikd16d2192018-03-07 08:58:3657 base::BindRepeating(&CaptureErrorCallback, &error_, &sql_text_));
[email protected]67361b32011-04-12 20:13:0658 }
59
dcheng1b3b125e2014-12-22 23:00:2460 void TearDown() override {
[email protected]67361b32011-04-12 20:13:0661 // If any error happened the original sql statement can be found in
[email protected]49dc4f22012-10-17 17:41:1662 // |sql_text_|.
engedybe80d53e2015-01-22 09:54:5063 EXPECT_EQ(SQLITE_OK, error_) << sql_text_;
erg102ceb412015-06-20 01:38:1364
65 SQLTestBase::TearDown();
[email protected]67361b32011-04-12 20:13:0666 }
67
Scott Hessdcf120482015-02-10 21:33:2968 int error() { return error_; }
[email protected]67361b32011-04-12 20:13:0669
[email protected]67361b32011-04-12 20:13:0670 private:
[email protected]49dc4f22012-10-17 17:41:1671 // The error code of the most recent error.
72 int error_;
73 // Original statement which has caused the error.
74 std::string sql_text_;
[email protected]67361b32011-04-12 20:13:0675};
76
77// Do not include fts1 support, it is not useful, and nobody is
78// looking at it.
79TEST_F(SQLiteFeaturesTest, NoFTS1) {
[email protected]eff1fa522011-12-12 23:50:5980 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
81 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
[email protected]67361b32011-04-12 20:13:0682}
83
shess37437cb2015-03-11 20:24:4684// Do not include fts2 support, it is not useful, and nobody is
85// looking at it.
86TEST_F(SQLiteFeaturesTest, NoFTS2) {
87 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
88 "CREATE VIRTUAL TABLE foo USING fts2(x)"));
[email protected]67361b32011-04-12 20:13:0689}
shess355d9a1e2015-01-10 00:42:2990
shess37437cb2015-03-11 20:24:4691// fts3 used to be used for history files, and may also be used by WebDatabase
92// clients.
[email protected]67361b32011-04-12 20:13:0693TEST_F(SQLiteFeaturesTest, FTS3) {
94 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
95}
96
shess37437cb2015-03-11 20:24:4697// Originally history used fts2, which Chromium patched to treat "foo*" as a
98// prefix search, though the icu tokenizer would return it as two tokens {"foo",
99// "*"}. Test that fts3 works correctly.
shess355d9a1e2015-01-10 00:42:29100TEST_F(SQLiteFeaturesTest, FTS3_Prefix) {
Victor Costan1d868352018-06-26 19:06:48101 static const char kCreateSql[] =
shess355d9a1e2015-01-10 00:42:29102 "CREATE VIRTUAL TABLE foo USING fts3(x, tokenize icu)";
103 ASSERT_TRUE(db().Execute(kCreateSql));
104
105 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
106
shessf7fcc452017-04-19 22:10:41107 EXPECT_EQ("test",
108 ExecuteWithResult(&db(), "SELECT x FROM foo WHERE x MATCH 'te*'"));
shess355d9a1e2015-01-10 00:42:29109}
shess355d9a1e2015-01-10 00:42:29110
shess156733db2015-01-21 21:52:24111// Verify that Chromium's SQLite is compiled with HAVE_USLEEP defined. With
112// HAVE_USLEEP, SQLite uses usleep() with millisecond granularity. Otherwise it
113// uses sleep() with second granularity.
114TEST_F(SQLiteFeaturesTest, UsesUsleep) {
115 base::TimeTicks before = base::TimeTicks::Now();
116 sqlite3_sleep(1);
117 base::TimeDelta delta = base::TimeTicks::Now() - before;
118
shessf7fcc452017-04-19 22:10:41119 // It is not impossible for this to be over 1000 if things are compiled
120 // correctly, but that is very unlikely. Most platforms seem to be exactly
121 // 1ms, with the rest at 2ms, and the worst observed cases was ASAN at 7ms.
shess156733db2015-01-21 21:52:24122 EXPECT_LT(delta.InMilliseconds(), 1000);
123}
shess156733db2015-01-21 21:52:24124
engedybe80d53e2015-01-22 09:54:50125// Ensure that our SQLite version has working foreign key support with cascade
126// delete support.
127TEST_F(SQLiteFeaturesTest, ForeignKeySupport) {
128 ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1"));
129 ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)"));
130 ASSERT_TRUE(db().Execute(
131 "CREATE TABLE children ("
132 " id INTEGER PRIMARY KEY,"
133 " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)"));
Victor Costan1d868352018-06-26 19:06:48134 static const char kSelectParentsSql[] = "SELECT * FROM parents ORDER BY id";
135 static const char kSelectChildrenSql[] = "SELECT * FROM children ORDER BY id";
engedybe80d53e2015-01-22 09:54:50136
137 // Inserting without a matching parent should fail with constraint violation.
Victor Costan1d868352018-06-26 19:06:48138 EXPECT_EQ("", ExecuteWithResult(&db(), kSelectParentsSql));
shessf7fcc452017-04-19 22:10:41139 const int insert_error =
140 db().ExecuteAndReturnErrorCode("INSERT INTO children VALUES (10, 1)");
Victor Costan86ca4312018-02-07 21:34:10141 EXPECT_EQ(SQLITE_CONSTRAINT | SQLITE_CONSTRAINT_FOREIGNKEY, insert_error);
Victor Costan1d868352018-06-26 19:06:48142 EXPECT_EQ("", ExecuteWithResult(&db(), kSelectChildrenSql));
engedybe80d53e2015-01-22 09:54:50143
144 // Inserting with a matching parent should work.
145 ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)"));
Victor Costan1d868352018-06-26 19:06:48146 EXPECT_EQ("1", ExecuteWithResults(&db(), kSelectParentsSql, "|", "\n"));
engedybe80d53e2015-01-22 09:54:50147 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)"));
148 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)"));
shessf7fcc452017-04-19 22:10:41149 EXPECT_EQ("11|1\n12|1",
Victor Costan1d868352018-06-26 19:06:48150 ExecuteWithResults(&db(), kSelectChildrenSql, "|", "\n"));
engedybe80d53e2015-01-22 09:54:50151
shessf7fcc452017-04-19 22:10:41152 // Deleting the parent should cascade, deleting the children as well.
engedybe80d53e2015-01-22 09:54:50153 ASSERT_TRUE(db().Execute("DELETE FROM parents"));
Victor Costan1d868352018-06-26 19:06:48154 EXPECT_EQ("", ExecuteWithResult(&db(), kSelectParentsSql));
155 EXPECT_EQ("", ExecuteWithResult(&db(), kSelectChildrenSql));
engedybe80d53e2015-01-22 09:54:50156}
157
Victor Costanbc7ab062018-04-20 19:03:56158// Ensure that our SQLite version supports booleans.
159TEST_F(SQLiteFeaturesTest, BooleanSupport) {
160 ASSERT_TRUE(
161 db().Execute("CREATE TABLE flags ("
162 " id INTEGER PRIMARY KEY,"
163 " true_flag BOOL NOT NULL DEFAULT TRUE,"
164 " false_flag BOOL NOT NULL DEFAULT FALSE)"));
165 ASSERT_TRUE(db().Execute(
166 "ALTER TABLE flags ADD COLUMN true_flag2 BOOL NOT NULL DEFAULT TRUE"));
167 ASSERT_TRUE(db().Execute(
168 "ALTER TABLE flags ADD COLUMN false_flag2 BOOL NOT NULL DEFAULT FALSE"));
169 ASSERT_TRUE(db().Execute("INSERT INTO flags (id) VALUES (1)"));
170
171 sql::Statement s(db().GetUniqueStatement(
172 "SELECT true_flag, false_flag, true_flag2, false_flag2"
173 " FROM flags WHERE id=1;"));
174 ASSERT_TRUE(s.Step());
175
Victor Costanf087c842018-04-27 00:47:50176 EXPECT_TRUE(s.ColumnBool(0)) << " default TRUE at table creation time";
Victor Costanbc7ab062018-04-20 19:03:56177 EXPECT_TRUE(!s.ColumnBool(1)) << " default FALSE at table creation time";
178
Victor Costanf087c842018-04-27 00:47:50179 EXPECT_TRUE(s.ColumnBool(2)) << " default TRUE added by altering the table";
Victor Costanbc7ab062018-04-20 19:03:56180 EXPECT_TRUE(!s.ColumnBool(3)) << " default FALSE added by altering the table";
181}
182
Victor Costan42988a92018-02-06 02:22:14183#if defined(OS_FUCHSIA)
shess53adf162015-12-17 22:07:26184// If the platform cannot support SQLite mmap'ed I/O, make sure SQLite isn't
185// offering to support it.
186TEST_F(SQLiteFeaturesTest, NoMmap) {
187 // For recent versions of SQLite, SQLITE_MAX_MMAP_SIZE=0 can be used to
188 // disable mmap support. Alternately, sqlite3_config() could be used. In
189 // that case, the pragma will run successfully, but the size will always be 0.
190 //
shess53adf162015-12-17 22:07:26191 // MojoVFS implements a no-op for xFileControl(). PRAGMA mmap_size is
192 // implemented in terms of SQLITE_FCNTL_MMAP_SIZE. In that case, the pragma
193 // will succeed but with no effect.
194 ignore_result(db().Execute("PRAGMA mmap_size = 1048576"));
195 sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size"));
196 ASSERT_TRUE(!s.Step() || !s.ColumnInt64(0));
197}
Victor Costan42988a92018-02-06 02:22:14198#endif // defined(OS_FUCHSIA)
rohitrao83d6b83a2016-06-21 07:25:57199
Scott Graham47ed2c32017-09-15 02:17:07200#if !defined(OS_FUCHSIA)
shess53adf162015-12-17 22:07:26201// Verify that OS file writes are reflected in the memory mapping of a
202// memory-mapped file. Normally SQLite writes to memory-mapped files using
203// memcpy(), which should stay consistent. Our SQLite is slightly patched to
204// mmap read only, then write using OS file writes. If the memory-mapped
205// version doesn't reflect the OS file writes, SQLite's memory-mapped I/O should
206// be disabled on this platform using SQLITE_MAX_MMAP_SIZE=0.
207TEST_F(SQLiteFeaturesTest, Mmap) {
208 // Try to turn on mmap'ed I/O.
209 ignore_result(db().Execute("PRAGMA mmap_size = 1048576"));
210 {
211 sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size"));
212
shess53adf162015-12-17 22:07:26213 ASSERT_TRUE(s.Step());
214 ASSERT_GT(s.ColumnInt64(0), 0);
shess53adf162015-12-17 22:07:26215 }
216 db().Close();
217
avi0b519202015-12-21 07:25:19218 const uint32_t kFlags =
219 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE;
shess53adf162015-12-17 22:07:26220 char buf[4096];
221
222 // Create a file with a block of '0', a block of '1', and a block of '2'.
223 {
224 base::File f(db_path(), kFlags);
225 ASSERT_TRUE(f.IsValid());
226 memset(buf, '0', sizeof(buf));
227 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
228
229 memset(buf, '1', sizeof(buf));
230 ASSERT_EQ(f.Write(1*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
231
232 memset(buf, '2', sizeof(buf));
233 ASSERT_EQ(f.Write(2*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
234 }
235
236 // mmap the file and verify that everything looks right.
237 {
238 base::MemoryMappedFile m;
239 ASSERT_TRUE(m.Initialize(db_path()));
240
241 memset(buf, '0', sizeof(buf));
242 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf)));
243
244 memset(buf, '1', sizeof(buf));
245 ASSERT_EQ(0, memcmp(buf, m.data() + 1*sizeof(buf), sizeof(buf)));
246
247 memset(buf, '2', sizeof(buf));
248 ASSERT_EQ(0, memcmp(buf, m.data() + 2*sizeof(buf), sizeof(buf)));
249
250 // Scribble some '3' into the first page of the file, and verify that it
251 // looks the same in the memory mapping.
252 {
253 base::File f(db_path(), kFlags);
254 ASSERT_TRUE(f.IsValid());
255 memset(buf, '3', sizeof(buf));
256 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
257 }
258 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf)));
259
260 // Repeat with a single '4' in case page-sized blocks are different.
261 const size_t kOffset = 1*sizeof(buf) + 123;
262 ASSERT_NE('4', m.data()[kOffset]);
263 {
264 base::File f(db_path(), kFlags);
265 ASSERT_TRUE(f.IsValid());
266 buf[0] = '4';
267 ASSERT_EQ(f.Write(kOffset, buf, 1), 1);
268 }
269 ASSERT_EQ('4', m.data()[kOffset]);
270 }
271}
Scott Graham47ed2c32017-09-15 02:17:07272#endif // !defined(OS_FUCHSIA)
shess53adf162015-12-17 22:07:26273
shess001ae162016-10-20 04:04:32274// Verify that http://crbug.com/248608 is fixed. In this bug, the
275// compiled regular expression is effectively cached with the prepared
276// statement, causing errors if the regular expression is rebound.
277TEST_F(SQLiteFeaturesTest, CachedRegexp) {
278 ASSERT_TRUE(db().Execute("CREATE TABLE r (id INTEGER UNIQUE, x TEXT)"));
279 ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (1, 'this is a test')"));
280 ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (2, 'that was a test')"));
281 ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (3, 'this is a stickup')"));
282 ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (4, 'that sucks')"));
283
Victor Costan1d868352018-06-26 19:06:48284 static const char kSimpleSql[] = "SELECT SUM(id) FROM r WHERE x REGEXP ?";
shess001ae162016-10-20 04:04:32285 sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql));
286
287 s.BindString(0, "this.*");
288 ASSERT_TRUE(s.Step());
289 EXPECT_EQ(4, s.ColumnInt(0));
290
291 s.Reset(true);
292 s.BindString(0, "that.*");
293 ASSERT_TRUE(s.Step());
294 EXPECT_EQ(6, s.ColumnInt(0));
295
296 s.Reset(true);
297 s.BindString(0, ".*test");
298 ASSERT_TRUE(s.Step());
299 EXPECT_EQ(3, s.ColumnInt(0));
300
301 s.Reset(true);
302 s.BindString(0, ".* s[a-z]+");
303 ASSERT_TRUE(s.Step());
304 EXPECT_EQ(7, s.ColumnInt(0));
305}
306
shess5f2c3442017-01-24 02:15:10307#if defined(OS_MACOSX) && !defined(OS_IOS)
308base::ScopedCFTypeRef<CFURLRef> CFURLRefForPath(const base::FilePath& path){
309 base::ScopedCFTypeRef<CFStringRef> urlString(
310 CFStringCreateWithFileSystemRepresentation(
311 kCFAllocatorDefault, path.value().c_str()));
312 base::ScopedCFTypeRef<CFURLRef> url(
313 CFURLCreateWithFileSystemPath(kCFAllocatorDefault, urlString,
314 kCFURLPOSIXPathStyle, FALSE));
315 return url;
316}
317
318// If a database file is marked to be excluded from Time Machine, verify that
319// journal files are also excluded.
Victor Costan653b14f2018-07-23 19:32:33320TEST_F(SQLiteFeaturesTest, TimeMachine) {
shess5f2c3442017-01-24 02:15:10321 ASSERT_TRUE(db().Execute("CREATE TABLE t (id INTEGER PRIMARY KEY)"));
322 db().Close();
323
Victor Costance678e72018-07-24 10:25:00324 base::FilePath journal = sql::Connection::JournalPath(db_path());
shess5f2c3442017-01-24 02:15:10325 ASSERT_TRUE(GetPathExists(db_path()));
326 ASSERT_TRUE(GetPathExists(journal));
327
328 base::ScopedCFTypeRef<CFURLRef> dbURL(CFURLRefForPath(db_path()));
329 base::ScopedCFTypeRef<CFURLRef> journalURL(CFURLRefForPath(journal));
330
331 // Not excluded to start.
Victor Costanbd623112018-07-18 04:17:27332 EXPECT_FALSE(CSBackupIsItemExcluded(dbURL, nullptr));
333 EXPECT_FALSE(CSBackupIsItemExcluded(journalURL, nullptr));
shess5f2c3442017-01-24 02:15:10334
335 // Exclude the main database file.
336 EXPECT_TRUE(base::mac::SetFileBackupExclusion(db_path()));
337
338 Boolean excluded_by_path = FALSE;
339 EXPECT_TRUE(CSBackupIsItemExcluded(dbURL, &excluded_by_path));
340 EXPECT_FALSE(excluded_by_path);
Victor Costanbd623112018-07-18 04:17:27341 EXPECT_FALSE(CSBackupIsItemExcluded(journalURL, nullptr));
shess5f2c3442017-01-24 02:15:10342
343 EXPECT_TRUE(db().Open(db_path()));
344 ASSERT_TRUE(db().Execute("INSERT INTO t VALUES (1)"));
345 EXPECT_TRUE(CSBackupIsItemExcluded(dbURL, &excluded_by_path));
346 EXPECT_FALSE(excluded_by_path);
347 EXPECT_TRUE(CSBackupIsItemExcluded(journalURL, &excluded_by_path));
348 EXPECT_FALSE(excluded_by_path);
349
350 // TODO(shess): In WAL mode this will touch -wal and -shm files. -shm files
351 // could be always excluded.
352}
353#endif
354
shesse8127932017-03-27 19:16:10355// Test that Chromium's patch to make auto_vacuum integrate with
356// SQLITE_FCNTL_CHUNK_SIZE is working.
357TEST_F(SQLiteFeaturesTest, SmartAutoVacuum) {
358 // Turn on auto_vacuum, and set the page size low to make results obvious.
359 // These settings require re-writing the database, which VACUUM does.
360 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum = FULL"));
361 ASSERT_TRUE(db().Execute("PRAGMA page_size = 1024"));
362 ASSERT_TRUE(db().Execute("VACUUM"));
363
364 // Code-coverage of the PRAGMA set/get implementation.
Victor Costan1d868352018-06-26 19:06:48365 static const char kPragmaSql[] = "PRAGMA auto_vacuum_slack_pages";
shesse8127932017-03-27 19:16:10366 ASSERT_EQ("0", sql::test::ExecuteWithResult(&db(), kPragmaSql));
367 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 4"));
368 ASSERT_EQ("4", sql::test::ExecuteWithResult(&db(), kPragmaSql));
369 // Max out at 255.
370 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 1000"));
371 ASSERT_EQ("255", sql::test::ExecuteWithResult(&db(), kPragmaSql));
372 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 0"));
373
374 // With page_size=1024, the following will insert rows which take up an
375 // overflow page, plus a small header in a b-tree node. An empty table takes
376 // a single page, so for small row counts each insert will add one page, and
377 // each delete will remove one page.
Victor Costan1d868352018-06-26 19:06:48378 static const char kCreateSql[] =
379 "CREATE TABLE t (id INTEGER PRIMARY KEY, value)";
380 static const char kInsertSql[] =
381 "INSERT INTO t (value) VALUES (randomblob(980))";
shesse8127932017-03-27 19:16:10382#if !defined(OS_WIN)
Victor Costan1d868352018-06-26 19:06:48383 static const char kDeleteSql[] =
384 "DELETE FROM t WHERE id = (SELECT MIN(id) FROM t)";
shesse8127932017-03-27 19:16:10385#endif
386
387 // This database will be 34 overflow pages plus the table's root page plus the
388 // SQLite header page plus the freelist page.
389 ASSERT_TRUE(db().Execute(kCreateSql));
390 {
391 sql::Statement s(db().GetUniqueStatement(kInsertSql));
392 for (int i = 0; i < 34; ++i) {
393 s.Reset(true);
394 ASSERT_TRUE(s.Run());
395 }
396 }
397 ASSERT_EQ("37", sql::test::ExecuteWithResult(&db(), "PRAGMA page_count"));
398
399 // http://sqlite.org/mmap.html indicates that Windows will silently fail when
400 // truncating a memory-mapped file. That pretty much invalidates these tests
401 // against the actual file size.
402#if !defined(OS_WIN)
403 // Each delete will delete a single page, including crossing a
404 // multiple-of-four boundary.
405 {
406 sql::Statement s(db().GetUniqueStatement(kDeleteSql));
407 for (int i = 0; i < 5; ++i) {
408 int64_t file_size_before, file_size_after;
409 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before));
410
411 s.Reset(true);
412 ASSERT_TRUE(s.Run());
413
414 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after));
415 ASSERT_EQ(file_size_after, file_size_before - 1024);
416 }
417 }
418
419 // Turn on "smart" auto-vacuum to remove 4 pages at a time.
420 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 4"));
421
422 // No pages removed, then four deleted at once.
423 {
424 sql::Statement s(db().GetUniqueStatement(kDeleteSql));
425 for (int i = 0; i < 3; ++i) {
426 int64_t file_size_before, file_size_after;
427 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before));
428
429 s.Reset(true);
430 ASSERT_TRUE(s.Run());
431
432 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after));
433 ASSERT_EQ(file_size_after, file_size_before);
434 }
435
436 int64_t file_size_before, file_size_after;
437 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before));
438
439 s.Reset(true);
440 ASSERT_TRUE(s.Run());
441
442 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after));
443 ASSERT_EQ(file_size_after, file_size_before - 4096);
444 }
445#endif
446}
shesse8127932017-03-27 19:16:10447
Victor Costan86ca4312018-02-07 21:34:10448#if !defined(OS_FUCHSIA)
shessf7fcc452017-04-19 22:10:41449// SQLite WAL mode defaults to checkpointing the WAL on close. This would push
450// additional work into Chromium shutdown. Verify that SQLite supports a config
451// option to not checkpoint on close.
452TEST_F(SQLiteFeaturesTest, WALNoClose) {
Victor Costance678e72018-07-24 10:25:00453 base::FilePath wal_path = sql::Connection::WriteAheadLogPath(db_path());
shessf7fcc452017-04-19 22:10:41454
455 // Turn on WAL mode, then verify that the mode changed (WAL is supported).
456 ASSERT_TRUE(db().Execute("PRAGMA journal_mode = WAL"));
457 ASSERT_EQ("wal", ExecuteWithResult(&db(), "PRAGMA journal_mode"));
458
459 // The WAL file is created lazily on first change.
460 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
461
462 // By default, the WAL is checkpointed then deleted on close.
463 ASSERT_TRUE(GetPathExists(wal_path));
464 db().Close();
465 ASSERT_FALSE(GetPathExists(wal_path));
466
467 // Reopen and configure the database to not checkpoint WAL on close.
468 ASSERT_TRUE(Reopen());
469 ASSERT_TRUE(db().Execute("PRAGMA journal_mode = WAL"));
470 ASSERT_TRUE(db().Execute("ALTER TABLE foo ADD COLUMN c"));
Victor Costanbd623112018-07-18 04:17:27471 ASSERT_EQ(SQLITE_OK,
472 sqlite3_db_config(db().db_, SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, 1,
473 nullptr));
shessf7fcc452017-04-19 22:10:41474 ASSERT_TRUE(GetPathExists(wal_path));
475 db().Close();
476 ASSERT_TRUE(GetPathExists(wal_path));
477}
478#endif
479
480} // namespace sql