blob: 88f7802fc02d5a56fb98a5b3dab7f3268e7220c6 [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"
[email protected]f0a54b22011-07-19 18:40:2114#include "sql/connection.h"
15#include "sql/statement.h"
erg102ceb412015-06-20 01:38:1316#include "sql/test/sql_test_base.h"
engedybe80d53e2015-01-22 09:54:5017#include "sql/test/test_helpers.h"
[email protected]67361b32011-04-12 20:13:0618#include "testing/gtest/include/gtest/gtest.h"
19#include "third_party/sqlite/sqlite3.h"
20
rohitrao83d6b83a2016-06-21 07:25:5721#if defined(OS_IOS)
22#include "base/ios/ios_util.h"
23#endif
24
shess5f2c3442017-01-24 02:15:1025#if defined(OS_MACOSX) && !defined(OS_IOS)
26#include <CoreFoundation/CoreFoundation.h>
27#include <CoreServices/CoreServices.h>
28
29#include "base/mac/mac_util.h"
30#include "base/mac/scoped_cftyperef.h"
31#endif
32
[email protected]67361b32011-04-12 20:13:0633// Test that certain features are/are-not enabled in our SQLite.
34
shessf7fcc452017-04-19 22:10:4135namespace sql {
[email protected]67361b32011-04-12 20:13:0636namespace {
37
shessf7fcc452017-04-19 22:10:4138using sql::test::ExecuteWithResult;
39using sql::test::ExecuteWithResults;
40
[email protected]526b4662013-06-14 04:09:1241void CaptureErrorCallback(int* error_pointer, std::string* sql_text,
42 int error, sql::Statement* stmt) {
43 *error_pointer = error;
44 const char* text = stmt ? stmt->GetSQLStatement() : NULL;
45 *sql_text = text ? text : "no statement available";
46}
[email protected]67361b32011-04-12 20:13:0647
shessf7fcc452017-04-19 22:10:4148} // namespace
49
erg102ceb412015-06-20 01:38:1350class SQLiteFeaturesTest : public sql::SQLTestBase {
[email protected]67361b32011-04-12 20:13:0651 public:
[email protected]49dc4f22012-10-17 17:41:1652 SQLiteFeaturesTest() : error_(SQLITE_OK) {}
[email protected]67361b32011-04-12 20:13:0653
dcheng1b3b125e2014-12-22 23:00:2454 void SetUp() override {
erg102ceb412015-06-20 01:38:1355 SQLTestBase::SetUp();
[email protected]99034682012-06-13 03:31:1656
[email protected]49dc4f22012-10-17 17:41:1657 // The error delegate will set |error_| and |sql_text_| when any sqlite
58 // statement operation returns an error code.
erg102ceb412015-06-20 01:38:1359 db().set_error_callback(
60 base::Bind(&CaptureErrorCallback, &error_, &sql_text_));
[email protected]67361b32011-04-12 20:13:0661 }
62
dcheng1b3b125e2014-12-22 23:00:2463 void TearDown() override {
[email protected]67361b32011-04-12 20:13:0664 // If any error happened the original sql statement can be found in
[email protected]49dc4f22012-10-17 17:41:1665 // |sql_text_|.
engedybe80d53e2015-01-22 09:54:5066 EXPECT_EQ(SQLITE_OK, error_) << sql_text_;
erg102ceb412015-06-20 01:38:1367
68 SQLTestBase::TearDown();
[email protected]67361b32011-04-12 20:13:0669 }
70
Scott Hessdcf120482015-02-10 21:33:2971 int error() { return error_; }
[email protected]67361b32011-04-12 20:13:0672
[email protected]67361b32011-04-12 20:13:0673 private:
[email protected]49dc4f22012-10-17 17:41:1674 // The error code of the most recent error.
75 int error_;
76 // Original statement which has caused the error.
77 std::string sql_text_;
[email protected]67361b32011-04-12 20:13:0678};
79
80// Do not include fts1 support, it is not useful, and nobody is
81// looking at it.
82TEST_F(SQLiteFeaturesTest, NoFTS1) {
[email protected]eff1fa522011-12-12 23:50:5983 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
84 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
[email protected]67361b32011-04-12 20:13:0685}
86
shess37437cb2015-03-11 20:24:4687// Do not include fts2 support, it is not useful, and nobody is
88// looking at it.
89TEST_F(SQLiteFeaturesTest, NoFTS2) {
90 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
91 "CREATE VIRTUAL TABLE foo USING fts2(x)"));
[email protected]67361b32011-04-12 20:13:0692}
shess355d9a1e2015-01-10 00:42:2993
shess37437cb2015-03-11 20:24:4694// fts3 used to be used for history files, and may also be used by WebDatabase
95// clients.
[email protected]67361b32011-04-12 20:13:0696TEST_F(SQLiteFeaturesTest, FTS3) {
97 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
98}
99
shess355d9a1e2015-01-10 00:42:29100#if !defined(USE_SYSTEM_SQLITE)
shess37437cb2015-03-11 20:24:46101// Originally history used fts2, which Chromium patched to treat "foo*" as a
102// prefix search, though the icu tokenizer would return it as two tokens {"foo",
103// "*"}. Test that fts3 works correctly.
shess355d9a1e2015-01-10 00:42:29104TEST_F(SQLiteFeaturesTest, FTS3_Prefix) {
105 const char kCreateSql[] =
106 "CREATE VIRTUAL TABLE foo USING fts3(x, tokenize icu)";
107 ASSERT_TRUE(db().Execute(kCreateSql));
108
109 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
110
shessf7fcc452017-04-19 22:10:41111 EXPECT_EQ("test",
112 ExecuteWithResult(&db(), "SELECT x FROM foo WHERE x MATCH 'te*'"));
shess355d9a1e2015-01-10 00:42:29113}
114#endif
115
shess156733db2015-01-21 21:52:24116#if !defined(USE_SYSTEM_SQLITE)
117// Verify that Chromium's SQLite is compiled with HAVE_USLEEP defined. With
118// HAVE_USLEEP, SQLite uses usleep() with millisecond granularity. Otherwise it
119// uses sleep() with second granularity.
120TEST_F(SQLiteFeaturesTest, UsesUsleep) {
121 base::TimeTicks before = base::TimeTicks::Now();
122 sqlite3_sleep(1);
123 base::TimeDelta delta = base::TimeTicks::Now() - before;
124
shessf7fcc452017-04-19 22:10:41125 // It is not impossible for this to be over 1000 if things are compiled
126 // correctly, but that is very unlikely. Most platforms seem to be exactly
127 // 1ms, with the rest at 2ms, and the worst observed cases was ASAN at 7ms.
shess156733db2015-01-21 21:52:24128 EXPECT_LT(delta.InMilliseconds(), 1000);
129}
130#endif
131
engedybe80d53e2015-01-22 09:54:50132// Ensure that our SQLite version has working foreign key support with cascade
133// delete support.
134TEST_F(SQLiteFeaturesTest, ForeignKeySupport) {
135 ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1"));
136 ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)"));
137 ASSERT_TRUE(db().Execute(
138 "CREATE TABLE children ("
139 " id INTEGER PRIMARY KEY,"
140 " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)"));
shessf7fcc452017-04-19 22:10:41141 const char kSelectParents[] = "SELECT * FROM parents ORDER BY id";
142 const char kSelectChildren[] = "SELECT * FROM children ORDER BY id";
engedybe80d53e2015-01-22 09:54:50143
144 // Inserting without a matching parent should fail with constraint violation.
Scott Hessdcf120482015-02-10 21:33:29145 // Mask off any extended error codes for USE_SYSTEM_SQLITE.
shessf7fcc452017-04-19 22:10:41146 EXPECT_EQ("", ExecuteWithResult(&db(), kSelectParents));
147 const int insert_error =
148 db().ExecuteAndReturnErrorCode("INSERT INTO children VALUES (10, 1)");
149 EXPECT_EQ(SQLITE_CONSTRAINT, (insert_error & 0xff));
150 EXPECT_EQ("", ExecuteWithResult(&db(), kSelectChildren));
engedybe80d53e2015-01-22 09:54:50151
152 // Inserting with a matching parent should work.
153 ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)"));
shessf7fcc452017-04-19 22:10:41154 EXPECT_EQ("1", ExecuteWithResults(&db(), kSelectParents, "|", "\n"));
engedybe80d53e2015-01-22 09:54:50155 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)"));
156 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)"));
shessf7fcc452017-04-19 22:10:41157 EXPECT_EQ("11|1\n12|1",
158 ExecuteWithResults(&db(), kSelectChildren, "|", "\n"));
engedybe80d53e2015-01-22 09:54:50159
shessf7fcc452017-04-19 22:10:41160 // Deleting the parent should cascade, deleting the children as well.
engedybe80d53e2015-01-22 09:54:50161 ASSERT_TRUE(db().Execute("DELETE FROM parents"));
shessf7fcc452017-04-19 22:10:41162 EXPECT_EQ("", ExecuteWithResult(&db(), kSelectParents));
163 EXPECT_EQ("", ExecuteWithResult(&db(), kSelectChildren));
engedybe80d53e2015-01-22 09:54:50164}
165
shess53adf162015-12-17 22:07:26166#if defined(MOJO_APPTEST_IMPL) || defined(OS_IOS)
167// If the platform cannot support SQLite mmap'ed I/O, make sure SQLite isn't
168// offering to support it.
169TEST_F(SQLiteFeaturesTest, NoMmap) {
rohitrao83d6b83a2016-06-21 07:25:57170#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
171 if (base::ios::IsRunningOnIOS10OrLater()) {
172 // iOS 10 added mmap support for sqlite.
173 return;
174 }
175#endif
176
shess53adf162015-12-17 22:07:26177 // For recent versions of SQLite, SQLITE_MAX_MMAP_SIZE=0 can be used to
178 // disable mmap support. Alternately, sqlite3_config() could be used. In
179 // that case, the pragma will run successfully, but the size will always be 0.
180 //
181 // The SQLite embedded in older iOS releases predates the addition of mmap
182 // support. In that case the pragma will run without error, but no results
183 // are returned when querying the value.
184 //
185 // MojoVFS implements a no-op for xFileControl(). PRAGMA mmap_size is
186 // implemented in terms of SQLITE_FCNTL_MMAP_SIZE. In that case, the pragma
187 // will succeed but with no effect.
188 ignore_result(db().Execute("PRAGMA mmap_size = 1048576"));
189 sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size"));
190 ASSERT_TRUE(!s.Step() || !s.ColumnInt64(0));
191}
rohitrao83d6b83a2016-06-21 07:25:57192#endif
193
194#if !defined(MOJO_APPTEST_IMPL)
shess53adf162015-12-17 22:07:26195// Verify that OS file writes are reflected in the memory mapping of a
196// memory-mapped file. Normally SQLite writes to memory-mapped files using
197// memcpy(), which should stay consistent. Our SQLite is slightly patched to
198// mmap read only, then write using OS file writes. If the memory-mapped
199// version doesn't reflect the OS file writes, SQLite's memory-mapped I/O should
200// be disabled on this platform using SQLITE_MAX_MMAP_SIZE=0.
201TEST_F(SQLiteFeaturesTest, Mmap) {
rohitrao83d6b83a2016-06-21 07:25:57202#if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE)
203 if (!base::ios::IsRunningOnIOS10OrLater()) {
204 // iOS9's sqlite does not support mmap, so this test must be skipped.
205 return;
206 }
207#endif
208
shess53adf162015-12-17 22:07:26209 // Try to turn on mmap'ed I/O.
210 ignore_result(db().Execute("PRAGMA mmap_size = 1048576"));
211 {
212 sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size"));
213
214#if !defined(USE_SYSTEM_SQLITE)
215 // With Chromium's version of SQLite, the setting should always be non-zero.
216 ASSERT_TRUE(s.Step());
217 ASSERT_GT(s.ColumnInt64(0), 0);
218#else
219 // With the system SQLite, don't verify underlying mmap functionality if the
220 // SQLite is too old to support mmap, or if mmap is disabled (see NoMmap
221 // test). USE_SYSTEM_SQLITE is not bundled into the NoMmap case because
222 // whether mmap is enabled or not is outside of Chromium's control.
223 if (!s.Step() || !s.ColumnInt64(0))
224 return;
225#endif
226 }
227 db().Close();
228
avi0b519202015-12-21 07:25:19229 const uint32_t kFlags =
230 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE;
shess53adf162015-12-17 22:07:26231 char buf[4096];
232
233 // Create a file with a block of '0', a block of '1', and a block of '2'.
234 {
235 base::File f(db_path(), kFlags);
236 ASSERT_TRUE(f.IsValid());
237 memset(buf, '0', sizeof(buf));
238 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
239
240 memset(buf, '1', sizeof(buf));
241 ASSERT_EQ(f.Write(1*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
242
243 memset(buf, '2', sizeof(buf));
244 ASSERT_EQ(f.Write(2*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
245 }
246
247 // mmap the file and verify that everything looks right.
248 {
249 base::MemoryMappedFile m;
250 ASSERT_TRUE(m.Initialize(db_path()));
251
252 memset(buf, '0', sizeof(buf));
253 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf)));
254
255 memset(buf, '1', sizeof(buf));
256 ASSERT_EQ(0, memcmp(buf, m.data() + 1*sizeof(buf), sizeof(buf)));
257
258 memset(buf, '2', sizeof(buf));
259 ASSERT_EQ(0, memcmp(buf, m.data() + 2*sizeof(buf), sizeof(buf)));
260
261 // Scribble some '3' into the first page of the file, and verify that it
262 // looks the same in the memory mapping.
263 {
264 base::File f(db_path(), kFlags);
265 ASSERT_TRUE(f.IsValid());
266 memset(buf, '3', sizeof(buf));
267 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
268 }
269 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf)));
270
271 // Repeat with a single '4' in case page-sized blocks are different.
272 const size_t kOffset = 1*sizeof(buf) + 123;
273 ASSERT_NE('4', m.data()[kOffset]);
274 {
275 base::File f(db_path(), kFlags);
276 ASSERT_TRUE(f.IsValid());
277 buf[0] = '4';
278 ASSERT_EQ(f.Write(kOffset, buf, 1), 1);
279 }
280 ASSERT_EQ('4', m.data()[kOffset]);
281 }
282}
283#endif
284
shess001ae162016-10-20 04:04:32285// Verify that http://crbug.com/248608 is fixed. In this bug, the
286// compiled regular expression is effectively cached with the prepared
287// statement, causing errors if the regular expression is rebound.
288TEST_F(SQLiteFeaturesTest, CachedRegexp) {
289 ASSERT_TRUE(db().Execute("CREATE TABLE r (id INTEGER UNIQUE, x TEXT)"));
290 ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (1, 'this is a test')"));
291 ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (2, 'that was a test')"));
292 ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (3, 'this is a stickup')"));
293 ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (4, 'that sucks')"));
294
295 const char* kSimpleSql = "SELECT SUM(id) FROM r WHERE x REGEXP ?";
296 sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql));
297
298 s.BindString(0, "this.*");
299 ASSERT_TRUE(s.Step());
300 EXPECT_EQ(4, s.ColumnInt(0));
301
302 s.Reset(true);
303 s.BindString(0, "that.*");
304 ASSERT_TRUE(s.Step());
305 EXPECT_EQ(6, s.ColumnInt(0));
306
307 s.Reset(true);
308 s.BindString(0, ".*test");
309 ASSERT_TRUE(s.Step());
310 EXPECT_EQ(3, s.ColumnInt(0));
311
312 s.Reset(true);
313 s.BindString(0, ".* s[a-z]+");
314 ASSERT_TRUE(s.Step());
315 EXPECT_EQ(7, s.ColumnInt(0));
316}
317
shess5f2c3442017-01-24 02:15:10318#if defined(OS_MACOSX) && !defined(OS_IOS)
319base::ScopedCFTypeRef<CFURLRef> CFURLRefForPath(const base::FilePath& path){
320 base::ScopedCFTypeRef<CFStringRef> urlString(
321 CFStringCreateWithFileSystemRepresentation(
322 kCFAllocatorDefault, path.value().c_str()));
323 base::ScopedCFTypeRef<CFURLRef> url(
324 CFURLCreateWithFileSystemPath(kCFAllocatorDefault, urlString,
325 kCFURLPOSIXPathStyle, FALSE));
326 return url;
327}
328
329// If a database file is marked to be excluded from Time Machine, verify that
330// journal files are also excluded.
331// TODO(shess): Disabled because CSBackupSetItemExcluded() does not work on the
332// bots, though it's fine on dev machines. See <http://crbug.com/410350>.
333TEST_F(SQLiteFeaturesTest, DISABLED_TimeMachine) {
334 ASSERT_TRUE(db().Execute("CREATE TABLE t (id INTEGER PRIMARY KEY)"));
335 db().Close();
336
337 base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal"));
338 ASSERT_TRUE(GetPathExists(db_path()));
339 ASSERT_TRUE(GetPathExists(journal));
340
341 base::ScopedCFTypeRef<CFURLRef> dbURL(CFURLRefForPath(db_path()));
342 base::ScopedCFTypeRef<CFURLRef> journalURL(CFURLRefForPath(journal));
343
344 // Not excluded to start.
345 EXPECT_FALSE(CSBackupIsItemExcluded(dbURL, NULL));
346 EXPECT_FALSE(CSBackupIsItemExcluded(journalURL, NULL));
347
348 // Exclude the main database file.
349 EXPECT_TRUE(base::mac::SetFileBackupExclusion(db_path()));
350
351 Boolean excluded_by_path = FALSE;
352 EXPECT_TRUE(CSBackupIsItemExcluded(dbURL, &excluded_by_path));
353 EXPECT_FALSE(excluded_by_path);
354 EXPECT_FALSE(CSBackupIsItemExcluded(journalURL, NULL));
355
356 EXPECT_TRUE(db().Open(db_path()));
357 ASSERT_TRUE(db().Execute("INSERT INTO t VALUES (1)"));
358 EXPECT_TRUE(CSBackupIsItemExcluded(dbURL, &excluded_by_path));
359 EXPECT_FALSE(excluded_by_path);
360 EXPECT_TRUE(CSBackupIsItemExcluded(journalURL, &excluded_by_path));
361 EXPECT_FALSE(excluded_by_path);
362
363 // TODO(shess): In WAL mode this will touch -wal and -shm files. -shm files
364 // could be always excluded.
365}
366#endif
367
shesse8127932017-03-27 19:16:10368#if !defined(USE_SYSTEM_SQLITE)
369// Test that Chromium's patch to make auto_vacuum integrate with
370// SQLITE_FCNTL_CHUNK_SIZE is working.
371TEST_F(SQLiteFeaturesTest, SmartAutoVacuum) {
372 // Turn on auto_vacuum, and set the page size low to make results obvious.
373 // These settings require re-writing the database, which VACUUM does.
374 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum = FULL"));
375 ASSERT_TRUE(db().Execute("PRAGMA page_size = 1024"));
376 ASSERT_TRUE(db().Execute("VACUUM"));
377
378 // Code-coverage of the PRAGMA set/get implementation.
379 const char kPragmaSql[] = "PRAGMA auto_vacuum_slack_pages";
380 ASSERT_EQ("0", sql::test::ExecuteWithResult(&db(), kPragmaSql));
381 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 4"));
382 ASSERT_EQ("4", sql::test::ExecuteWithResult(&db(), kPragmaSql));
383 // Max out at 255.
384 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 1000"));
385 ASSERT_EQ("255", sql::test::ExecuteWithResult(&db(), kPragmaSql));
386 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 0"));
387
388 // With page_size=1024, the following will insert rows which take up an
389 // overflow page, plus a small header in a b-tree node. An empty table takes
390 // a single page, so for small row counts each insert will add one page, and
391 // each delete will remove one page.
392 const char kCreateSql[] = "CREATE TABLE t (id INTEGER PRIMARY KEY, value)";
393 const char kInsertSql[] = "INSERT INTO t (value) VALUES (randomblob(980))";
394#if !defined(OS_WIN)
395 const char kDeleteSql[] = "DELETE FROM t WHERE id = (SELECT MIN(id) FROM t)";
396#endif
397
398 // This database will be 34 overflow pages plus the table's root page plus the
399 // SQLite header page plus the freelist page.
400 ASSERT_TRUE(db().Execute(kCreateSql));
401 {
402 sql::Statement s(db().GetUniqueStatement(kInsertSql));
403 for (int i = 0; i < 34; ++i) {
404 s.Reset(true);
405 ASSERT_TRUE(s.Run());
406 }
407 }
408 ASSERT_EQ("37", sql::test::ExecuteWithResult(&db(), "PRAGMA page_count"));
409
410 // http://sqlite.org/mmap.html indicates that Windows will silently fail when
411 // truncating a memory-mapped file. That pretty much invalidates these tests
412 // against the actual file size.
413#if !defined(OS_WIN)
414 // Each delete will delete a single page, including crossing a
415 // multiple-of-four boundary.
416 {
417 sql::Statement s(db().GetUniqueStatement(kDeleteSql));
418 for (int i = 0; i < 5; ++i) {
419 int64_t file_size_before, file_size_after;
420 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before));
421
422 s.Reset(true);
423 ASSERT_TRUE(s.Run());
424
425 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after));
426 ASSERT_EQ(file_size_after, file_size_before - 1024);
427 }
428 }
429
430 // Turn on "smart" auto-vacuum to remove 4 pages at a time.
431 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 4"));
432
433 // No pages removed, then four deleted at once.
434 {
435 sql::Statement s(db().GetUniqueStatement(kDeleteSql));
436 for (int i = 0; i < 3; ++i) {
437 int64_t file_size_before, file_size_after;
438 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before));
439
440 s.Reset(true);
441 ASSERT_TRUE(s.Run());
442
443 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after));
444 ASSERT_EQ(file_size_after, file_size_before);
445 }
446
447 int64_t file_size_before, file_size_after;
448 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before));
449
450 s.Reset(true);
451 ASSERT_TRUE(s.Run());
452
453 ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after));
454 ASSERT_EQ(file_size_after, file_size_before - 4096);
455 }
456#endif
457}
458#endif // !defined(USE_SYSTEM_SQLITE)
459
shessf7fcc452017-04-19 22:10:41460#if !defined(USE_SYSTEM_SQLITE)
461// SQLite WAL mode defaults to checkpointing the WAL on close. This would push
462// additional work into Chromium shutdown. Verify that SQLite supports a config
463// option to not checkpoint on close.
464TEST_F(SQLiteFeaturesTest, WALNoClose) {
465 base::FilePath wal_path(db_path().value() + FILE_PATH_LITERAL("-wal"));
466
467 // Turn on WAL mode, then verify that the mode changed (WAL is supported).
468 ASSERT_TRUE(db().Execute("PRAGMA journal_mode = WAL"));
469 ASSERT_EQ("wal", ExecuteWithResult(&db(), "PRAGMA journal_mode"));
470
471 // The WAL file is created lazily on first change.
472 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
473
474 // By default, the WAL is checkpointed then deleted on close.
475 ASSERT_TRUE(GetPathExists(wal_path));
476 db().Close();
477 ASSERT_FALSE(GetPathExists(wal_path));
478
479 // Reopen and configure the database to not checkpoint WAL on close.
480 ASSERT_TRUE(Reopen());
481 ASSERT_TRUE(db().Execute("PRAGMA journal_mode = WAL"));
482 ASSERT_TRUE(db().Execute("ALTER TABLE foo ADD COLUMN c"));
483 ASSERT_EQ(
484 SQLITE_OK,
485 sqlite3_db_config(db().db_, SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, 1, NULL));
486 ASSERT_TRUE(GetPathExists(wal_path));
487 db().Close();
488 ASSERT_TRUE(GetPathExists(wal_path));
489}
490#endif
491
492} // namespace sql