blob: 4b92608c4670dda4c9f7221ecb54aae11feb464f [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;
41 const char* text = stmt ? stmt->GetSQLStatement() : NULL;
42 *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(
57 base::Bind(&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
shess355d9a1e2015-01-10 00:42:2997#if !defined(USE_SYSTEM_SQLITE)
shess37437cb2015-03-11 20:24:4698// Originally history used fts2, which Chromium patched to treat "foo*" as a
99// prefix search, though the icu tokenizer would return it as two tokens {"foo",
100// "*"}. Test that fts3 works correctly.
shess355d9a1e2015-01-10 00:42:29101TEST_F(SQLiteFeaturesTest, FTS3_Prefix) {
102 const char kCreateSql[] =
103 "CREATE VIRTUAL TABLE foo USING fts3(x, tokenize icu)";
104 ASSERT_TRUE(db().Execute(kCreateSql));
105
106 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
107
shessf7fcc452017-04-19 22:10:41108 EXPECT_EQ("test",
109 ExecuteWithResult(&db(), "SELECT x FROM foo WHERE x MATCH 'te*'"));
shess355d9a1e2015-01-10 00:42:29110}
111#endif
112
shess156733db2015-01-21 21:52:24113#if !defined(USE_SYSTEM_SQLITE)
114// Verify that Chromium's SQLite is compiled with HAVE_USLEEP defined. With
115// HAVE_USLEEP, SQLite uses usleep() with millisecond granularity. Otherwise it
116// uses sleep() with second granularity.
117TEST_F(SQLiteFeaturesTest, UsesUsleep) {
118 base::TimeTicks before = base::TimeTicks::Now();
119 sqlite3_sleep(1);
120 base::TimeDelta delta = base::TimeTicks::Now() - before;
121
shessf7fcc452017-04-19 22:10:41122 // It is not impossible for this to be over 1000 if things are compiled
123 // correctly, but that is very unlikely. Most platforms seem to be exactly
124 // 1ms, with the rest at 2ms, and the worst observed cases was ASAN at 7ms.
shess156733db2015-01-21 21:52:24125 EXPECT_LT(delta.InMilliseconds(), 1000);
126}
127#endif
128
engedybe80d53e2015-01-22 09:54:50129// Ensure that our SQLite version has working foreign key support with cascade
130// delete support.
131TEST_F(SQLiteFeaturesTest, ForeignKeySupport) {
132 ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1"));
133 ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)"));
134 ASSERT_TRUE(db().Execute(
135 "CREATE TABLE children ("
136 " id INTEGER PRIMARY KEY,"
137 " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)"));
shessf7fcc452017-04-19 22:10:41138 const char kSelectParents[] = "SELECT * FROM parents ORDER BY id";
139 const char kSelectChildren[] = "SELECT * FROM children ORDER BY id";
engedybe80d53e2015-01-22 09:54:50140
141 // Inserting without a matching parent should fail with constraint violation.
Scott Hessdcf120482015-02-10 21:33:29142 // Mask off any extended error codes for USE_SYSTEM_SQLITE.
shessf7fcc452017-04-19 22:10:41143 EXPECT_EQ("", ExecuteWithResult(&db(), kSelectParents));
144 const int insert_error =
145 db().ExecuteAndReturnErrorCode("INSERT INTO children VALUES (10, 1)");
146 EXPECT_EQ(SQLITE_CONSTRAINT, (insert_error & 0xff));
147 EXPECT_EQ("", ExecuteWithResult(&db(), kSelectChildren));
engedybe80d53e2015-01-22 09:54:50148
149 // Inserting with a matching parent should work.
150 ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)"));
shessf7fcc452017-04-19 22:10:41151 EXPECT_EQ("1", ExecuteWithResults(&db(), kSelectParents, "|", "\n"));
engedybe80d53e2015-01-22 09:54:50152 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)"));
153 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)"));
shessf7fcc452017-04-19 22:10:41154 EXPECT_EQ("11|1\n12|1",
155 ExecuteWithResults(&db(), kSelectChildren, "|", "\n"));
engedybe80d53e2015-01-22 09:54:50156
shessf7fcc452017-04-19 22:10:41157 // Deleting the parent should cascade, deleting the children as well.
engedybe80d53e2015-01-22 09:54:50158 ASSERT_TRUE(db().Execute("DELETE FROM parents"));
shessf7fcc452017-04-19 22:10:41159 EXPECT_EQ("", ExecuteWithResult(&db(), kSelectParents));
160 EXPECT_EQ("", ExecuteWithResult(&db(), kSelectChildren));
engedybe80d53e2015-01-22 09:54:50161}
162
Victor Costan42988a92018-02-06 02:22:14163#if defined(OS_FUCHSIA)
shess53adf162015-12-17 22:07:26164// If the platform cannot support SQLite mmap'ed I/O, make sure SQLite isn't
165// offering to support it.
166TEST_F(SQLiteFeaturesTest, NoMmap) {
shess53adf162015-12-17 22:07:26167 // For recent versions of SQLite, SQLITE_MAX_MMAP_SIZE=0 can be used to
168 // disable mmap support. Alternately, sqlite3_config() could be used. In
169 // that case, the pragma will run successfully, but the size will always be 0.
170 //
Victor Costan42988a92018-02-06 02:22:14171 // Historical note: The SQLite version bundled with iOS 9 and below does not
172 // have mmap support. Chrome now requires iOS 10 and above. This is only
173 // relevant when USE_SYSTEM_SQLITE is defined.
shess53adf162015-12-17 22:07:26174 //
175 // MojoVFS implements a no-op for xFileControl(). PRAGMA mmap_size is
176 // implemented in terms of SQLITE_FCNTL_MMAP_SIZE. In that case, the pragma
177 // will succeed but with no effect.
178 ignore_result(db().Execute("PRAGMA mmap_size = 1048576"));
179 sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size"));
180 ASSERT_TRUE(!s.Step() || !s.ColumnInt64(0));
181}
Victor Costan42988a92018-02-06 02:22:14182#endif // defined(OS_FUCHSIA)
rohitrao83d6b83a2016-06-21 07:25:57183
Scott Graham47ed2c32017-09-15 02:17:07184#if !defined(OS_FUCHSIA)
shess53adf162015-12-17 22:07:26185// Verify that OS file writes are reflected in the memory mapping of a
186// memory-mapped file. Normally SQLite writes to memory-mapped files using
187// memcpy(), which should stay consistent. Our SQLite is slightly patched to
188// mmap read only, then write using OS file writes. If the memory-mapped
189// version doesn't reflect the OS file writes, SQLite's memory-mapped I/O should
190// be disabled on this platform using SQLITE_MAX_MMAP_SIZE=0.
191TEST_F(SQLiteFeaturesTest, Mmap) {
shess53adf162015-12-17 22:07:26192 // Try to turn on mmap'ed I/O.
193 ignore_result(db().Execute("PRAGMA mmap_size = 1048576"));
194 {
195 sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size"));
196
Victor Costan42988a92018-02-06 02:22:14197 // Historical note: The SQLite version bundled with iOS 9 and below does
198 // not have mmap support. Chrome now requires iOS 10 and above. This is
199 // only relevant when USE_SYSTEM_SQLITE is defined.
200
shess53adf162015-12-17 22:07:26201 ASSERT_TRUE(s.Step());
202 ASSERT_GT(s.ColumnInt64(0), 0);
shess53adf162015-12-17 22:07:26203 }
204 db().Close();
205
avi0b519202015-12-21 07:25:19206 const uint32_t kFlags =
207 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE;
shess53adf162015-12-17 22:07:26208 char buf[4096];
209
210 // Create a file with a block of '0', a block of '1', and a block of '2'.
211 {
212 base::File f(db_path(), kFlags);
213 ASSERT_TRUE(f.IsValid());
214 memset(buf, '0', sizeof(buf));
215 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
216
217 memset(buf, '1', sizeof(buf));
218 ASSERT_EQ(f.Write(1*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
219
220 memset(buf, '2', sizeof(buf));
221 ASSERT_EQ(f.Write(2*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
222 }
223
224 // mmap the file and verify that everything looks right.
225 {
226 base::MemoryMappedFile m;
227 ASSERT_TRUE(m.Initialize(db_path()));
228
229 memset(buf, '0', sizeof(buf));
230 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf)));
231
232 memset(buf, '1', sizeof(buf));
233 ASSERT_EQ(0, memcmp(buf, m.data() + 1*sizeof(buf), sizeof(buf)));
234
235 memset(buf, '2', sizeof(buf));
236 ASSERT_EQ(0, memcmp(buf, m.data() + 2*sizeof(buf), sizeof(buf)));
237
238 // Scribble some '3' into the first page of the file, and verify that it
239 // looks the same in the memory mapping.
240 {
241 base::File f(db_path(), kFlags);
242 ASSERT_TRUE(f.IsValid());
243 memset(buf, '3', sizeof(buf));
244 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
245 }
246 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf)));
247
248 // Repeat with a single '4' in case page-sized blocks are different.
249 const size_t kOffset = 1*sizeof(buf) + 123;
250 ASSERT_NE('4', m.data()[kOffset]);
251 {
252 base::File f(db_path(), kFlags);
253 ASSERT_TRUE(f.IsValid());
254 buf[0] = '4';
255 ASSERT_EQ(f.Write(kOffset, buf, 1), 1);
256 }
257 ASSERT_EQ('4', m.data()[kOffset]);
258 }
259}
Scott Graham47ed2c32017-09-15 02:17:07260#endif // !defined(OS_FUCHSIA)
shess53adf162015-12-17 22:07:26261
shess001ae162016-10-20 04:04:32262// Verify that http://crbug.com/248608 is fixed. In this bug, the
263// compiled regular expression is effectively cached with the prepared
264// statement, causing errors if the regular expression is rebound.
265TEST_F(SQLiteFeaturesTest, CachedRegexp) {
266 ASSERT_TRUE(db().Execute("CREATE TABLE r (id INTEGER UNIQUE, x TEXT)"));
267 ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (1, 'this is a test')"));
268 ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (2, 'that was a test')"));
269 ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (3, 'this is a stickup')"));
270 ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (4, 'that sucks')"));
271
272 const char* kSimpleSql = "SELECT SUM(id) FROM r WHERE x REGEXP ?";
273 sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql));
274
275 s.BindString(0, "this.*");
276 ASSERT_TRUE(s.Step());
277 EXPECT_EQ(4, s.ColumnInt(0));
278
279 s.Reset(true);
280 s.BindString(0, "that.*");
281 ASSERT_TRUE(s.Step());
282 EXPECT_EQ(6, s.ColumnInt(0));
283
284 s.Reset(true);
285 s.BindString(0, ".*test");
286 ASSERT_TRUE(s.Step());
287 EXPECT_EQ(3, s.ColumnInt(0));
288
289 s.Reset(true);
290 s.BindString(0, ".* s[a-z]+");
291 ASSERT_TRUE(s.Step());
292 EXPECT_EQ(7, s.ColumnInt(0));
293}
294
shess5f2c3442017-01-24 02:15:10295#if defined(OS_MACOSX) && !defined(OS_IOS)
296base::ScopedCFTypeRef<CFURLRef> CFURLRefForPath(const base::FilePath& path){
297 base::ScopedCFTypeRef<CFStringRef> urlString(
298 CFStringCreateWithFileSystemRepresentation(
299 kCFAllocatorDefault, path.value().c_str()));
300 base::ScopedCFTypeRef<CFURLRef> url(
301 CFURLCreateWithFileSystemPath(kCFAllocatorDefault, urlString,
302 kCFURLPOSIXPathStyle, FALSE));
303 return url;
304}
305
306// If a database file is marked to be excluded from Time Machine, verify that
307// journal files are also excluded.
308// TODO(shess): Disabled because CSBackupSetItemExcluded() does not work on the
309// bots, though it's fine on dev machines. See <http://crbug.com/410350>.
310TEST_F(SQLiteFeaturesTest, DISABLED_TimeMachine) {
311 ASSERT_TRUE(db().Execute("CREATE TABLE t (id INTEGER PRIMARY KEY)"));
312 db().Close();
313
314 base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal"));
315 ASSERT_TRUE(GetPathExists(db_path()));
316 ASSERT_TRUE(GetPathExists(journal));
317
318 base::ScopedCFTypeRef<CFURLRef> dbURL(CFURLRefForPath(db_path()));
319 base::ScopedCFTypeRef<CFURLRef> journalURL(CFURLRefForPath(journal));
320
321 // Not excluded to start.
322 EXPECT_FALSE(CSBackupIsItemExcluded(dbURL, NULL));
323 EXPECT_FALSE(CSBackupIsItemExcluded(journalURL, NULL));
324
325 // Exclude the main database file.
326 EXPECT_TRUE(base::mac::SetFileBackupExclusion(db_path()));
327
328 Boolean excluded_by_path = FALSE;
329 EXPECT_TRUE(CSBackupIsItemExcluded(dbURL, &excluded_by_path));
330 EXPECT_FALSE(excluded_by_path);
331 EXPECT_FALSE(CSBackupIsItemExcluded(journalURL, NULL));
332
333 EXPECT_TRUE(db().Open(db_path()));
334 ASSERT_TRUE(db().Execute("INSERT INTO t VALUES (1)"));
335 EXPECT_TRUE(CSBackupIsItemExcluded(dbURL, &excluded_by_path));
336 EXPECT_FALSE(excluded_by_path);
337 EXPECT_TRUE(CSBackupIsItemExcluded(journalURL, &excluded_by_path));
338 EXPECT_FALSE(excluded_by_path);
339
340 // TODO(shess): In WAL mode this will touch -wal and -shm files. -shm files
341 // could be always excluded.
342}
343#endif
344
shesse8127932017-03-27 19:16:10345#if !defined(USE_SYSTEM_SQLITE)
346// Test that Chromium's patch to make auto_vacuum integrate with
347// SQLITE_FCNTL_CHUNK_SIZE is working.
348TEST_F(SQLiteFeaturesTest, SmartAutoVacuum) {
349 // Turn on auto_vacuum, and set the page size low to make results obvious.
350 // These settings require re-writing the database, which VACUUM does.
351 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum = FULL"));
352 ASSERT_TRUE(db().Execute("PRAGMA page_size = 1024"));
353 ASSERT_TRUE(db().Execute("VACUUM"));
354
355 // Code-coverage of the PRAGMA set/get implementation.
356 const char kPragmaSql[] = "PRAGMA auto_vacuum_slack_pages";
357 ASSERT_EQ("0", sql::test::ExecuteWithResult(&db(), kPragmaSql));
358 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 4"));
359 ASSERT_EQ("4", sql::test::ExecuteWithResult(&db(), kPragmaSql));
360 // Max out at 255.
361 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 1000"));
362 ASSERT_EQ("255", sql::test::ExecuteWithResult(&db(), kPragmaSql));
363 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 0"));
364
365 // With page_size=1024, the following will insert rows which take up an
366 // overflow page, plus a small header in a b-tree node. An empty table takes
367 // a single page, so for small row counts each insert will add one page, and
368 // each delete will remove one page.
369 const char kCreateSql[] = "CREATE TABLE t (id INTEGER PRIMARY KEY, value)";
370 const char kInsertSql[] = "INSERT INTO t (value) VALUES (randomblob(980))";
371#if !defined(OS_WIN)
372 const char kDeleteSql[] = "DELETE FROM t WHERE id = (SELECT MIN(id) FROM t)";
373#endif
374
375 // This database will be 34 overflow pages plus the table's root page plus the
376 // SQLite header page plus the freelist page.
377 ASSERT_TRUE(db().Execute(kCreateSql));
378 {
379 sql::Statement s(db().GetUniqueStatement(kInsertSql));
380 for (int i = 0; i < 34; ++i) {
381 s.Reset(true);
382 ASSERT_TRUE(s.Run());
383 }
384 }
385 ASSERT_EQ("37", sql::test::ExecuteWithResult(&db(), "PRAGMA page_count"));
386
387 // http://sqlite.org/mmap.html indicates that Windows will silently fail when
388 // truncating a memory-mapped file. That pretty much invalidates these tests
389 // against the actual file size.
390#if !defined(OS_WIN)
391 // Each delete will delete a single page, including crossing a
392 // multiple-of-four boundary.
393 {
394 sql::Statement s(db().GetUniqueStatement(kDeleteSql));
395 for (int i = 0; i < 5; ++i) {
396 int64_t file_size_before, file_size_after;
397 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before));
398
399 s.Reset(true);
400 ASSERT_TRUE(s.Run());
401
402 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after));
403 ASSERT_EQ(file_size_after, file_size_before - 1024);
404 }
405 }
406
407 // Turn on "smart" auto-vacuum to remove 4 pages at a time.
408 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 4"));
409
410 // No pages removed, then four deleted at once.
411 {
412 sql::Statement s(db().GetUniqueStatement(kDeleteSql));
413 for (int i = 0; i < 3; ++i) {
414 int64_t file_size_before, file_size_after;
415 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before));
416
417 s.Reset(true);
418 ASSERT_TRUE(s.Run());
419
420 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after));
421 ASSERT_EQ(file_size_after, file_size_before);
422 }
423
424 int64_t file_size_before, file_size_after;
425 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before));
426
427 s.Reset(true);
428 ASSERT_TRUE(s.Run());
429
430 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after));
431 ASSERT_EQ(file_size_after, file_size_before - 4096);
432 }
433#endif
434}
435#endif // !defined(USE_SYSTEM_SQLITE)
436
Scott Graham502836c2017-09-14 23:26:23437#if !defined(USE_SYSTEM_SQLITE) && !defined(OS_FUCHSIA)
shessf7fcc452017-04-19 22:10:41438// SQLite WAL mode defaults to checkpointing the WAL on close. This would push
439// additional work into Chromium shutdown. Verify that SQLite supports a config
440// option to not checkpoint on close.
441TEST_F(SQLiteFeaturesTest, WALNoClose) {
442 base::FilePath wal_path(db_path().value() + FILE_PATH_LITERAL("-wal"));
443
444 // Turn on WAL mode, then verify that the mode changed (WAL is supported).
445 ASSERT_TRUE(db().Execute("PRAGMA journal_mode = WAL"));
446 ASSERT_EQ("wal", ExecuteWithResult(&db(), "PRAGMA journal_mode"));
447
448 // The WAL file is created lazily on first change.
449 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
450
451 // By default, the WAL is checkpointed then deleted on close.
452 ASSERT_TRUE(GetPathExists(wal_path));
453 db().Close();
454 ASSERT_FALSE(GetPathExists(wal_path));
455
456 // Reopen and configure the database to not checkpoint WAL on close.
457 ASSERT_TRUE(Reopen());
458 ASSERT_TRUE(db().Execute("PRAGMA journal_mode = WAL"));
459 ASSERT_TRUE(db().Execute("ALTER TABLE foo ADD COLUMN c"));
460 ASSERT_EQ(
461 SQLITE_OK,
462 sqlite3_db_config(db().db_, SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, 1, NULL));
463 ASSERT_TRUE(GetPathExists(wal_path));
464 db().Close();
465 ASSERT_TRUE(GetPathExists(wal_path));
466}
467#endif
468
469} // namespace sql