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