[email protected] | 60e60dd | 2012-04-28 08:16:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 8 | #include <string> |
| 9 | |
[email protected] | 526b466 | 2013-06-14 04:09:12 | [diff] [blame] | 10 | #include "base/bind.h" |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 11 | #include "base/files/file_util.h" |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 12 | #include "base/files/memory_mapped_file.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 13 | #include "base/files/scoped_temp_dir.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 14 | #include "sql/connection.h" |
| 15 | #include "sql/statement.h" |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 16 | #include "sql/test/sql_test_base.h" |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 17 | #include "sql/test/test_helpers.h" |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | #include "third_party/sqlite/sqlite3.h" |
| 20 | |
rohitrao | 83d6b83a | 2016-06-21 07:25:57 | [diff] [blame] | 21 | #if defined(OS_IOS) |
| 22 | #include "base/ios/ios_util.h" |
| 23 | #endif |
| 24 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 25 | #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] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 33 | // Test that certain features are/are-not enabled in our SQLite. |
| 34 | |
| 35 | namespace { |
| 36 | |
[email protected] | 526b466 | 2013-06-14 04:09:12 | [diff] [blame] | 37 | void CaptureErrorCallback(int* error_pointer, std::string* sql_text, |
| 38 | int error, sql::Statement* stmt) { |
| 39 | *error_pointer = error; |
| 40 | const char* text = stmt ? stmt->GetSQLStatement() : NULL; |
| 41 | *sql_text = text ? text : "no statement available"; |
| 42 | } |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 43 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 44 | class SQLiteFeaturesTest : public sql::SQLTestBase { |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 45 | public: |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 46 | SQLiteFeaturesTest() : error_(SQLITE_OK) {} |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 47 | |
dcheng | 1b3b125e | 2014-12-22 23:00:24 | [diff] [blame] | 48 | void SetUp() override { |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 49 | SQLTestBase::SetUp(); |
[email protected] | 9903468 | 2012-06-13 03:31:16 | [diff] [blame] | 50 | |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 51 | // The error delegate will set |error_| and |sql_text_| when any sqlite |
| 52 | // statement operation returns an error code. |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 53 | db().set_error_callback( |
| 54 | base::Bind(&CaptureErrorCallback, &error_, &sql_text_)); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 55 | } |
| 56 | |
dcheng | 1b3b125e | 2014-12-22 23:00:24 | [diff] [blame] | 57 | void TearDown() override { |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 58 | // If any error happened the original sql statement can be found in |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 59 | // |sql_text_|. |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 60 | EXPECT_EQ(SQLITE_OK, error_) << sql_text_; |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 61 | |
| 62 | SQLTestBase::TearDown(); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 63 | } |
| 64 | |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 65 | int error() { return error_; } |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 66 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 67 | private: |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 68 | // The error code of the most recent error. |
| 69 | int error_; |
| 70 | // Original statement which has caused the error. |
| 71 | std::string sql_text_; |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | // Do not include fts1 support, it is not useful, and nobody is |
| 75 | // looking at it. |
| 76 | TEST_F(SQLiteFeaturesTest, NoFTS1) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 77 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( |
| 78 | "CREATE VIRTUAL TABLE foo USING fts1(x)")); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 79 | } |
| 80 | |
shess | 37437cb | 2015-03-11 20:24:46 | [diff] [blame] | 81 | // Do not include fts2 support, it is not useful, and nobody is |
| 82 | // looking at it. |
| 83 | TEST_F(SQLiteFeaturesTest, NoFTS2) { |
| 84 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( |
| 85 | "CREATE VIRTUAL TABLE foo USING fts2(x)")); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 86 | } |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 87 | |
shess | 37437cb | 2015-03-11 20:24:46 | [diff] [blame] | 88 | // fts3 used to be used for history files, and may also be used by WebDatabase |
| 89 | // clients. |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 90 | TEST_F(SQLiteFeaturesTest, FTS3) { |
| 91 | ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)")); |
| 92 | } |
| 93 | |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 94 | #if !defined(USE_SYSTEM_SQLITE) |
shess | 37437cb | 2015-03-11 20:24:46 | [diff] [blame] | 95 | // Originally history used fts2, which Chromium patched to treat "foo*" as a |
| 96 | // prefix search, though the icu tokenizer would return it as two tokens {"foo", |
| 97 | // "*"}. Test that fts3 works correctly. |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 98 | TEST_F(SQLiteFeaturesTest, FTS3_Prefix) { |
| 99 | const char kCreateSql[] = |
| 100 | "CREATE VIRTUAL TABLE foo USING fts3(x, tokenize icu)"; |
| 101 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 102 | |
| 103 | ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')")); |
| 104 | |
| 105 | sql::Statement s(db().GetUniqueStatement( |
| 106 | "SELECT x FROM foo WHERE x MATCH 'te*'")); |
| 107 | ASSERT_TRUE(s.Step()); |
| 108 | EXPECT_EQ("test", s.ColumnString(0)); |
| 109 | } |
| 110 | #endif |
| 111 | |
shess | 156733db | 2015-01-21 21:52:24 | [diff] [blame] | 112 | #if !defined(USE_SYSTEM_SQLITE) |
| 113 | // Verify that Chromium's SQLite is compiled with HAVE_USLEEP defined. With |
| 114 | // HAVE_USLEEP, SQLite uses usleep() with millisecond granularity. Otherwise it |
| 115 | // uses sleep() with second granularity. |
| 116 | TEST_F(SQLiteFeaturesTest, UsesUsleep) { |
| 117 | base::TimeTicks before = base::TimeTicks::Now(); |
| 118 | sqlite3_sleep(1); |
| 119 | base::TimeDelta delta = base::TimeTicks::Now() - before; |
| 120 | |
| 121 | // It is not impossible for this to be over 1000 if things are compiled the |
| 122 | // right way. But it is very unlikely, most platforms seem to be around |
| 123 | // <TBD>. |
| 124 | LOG(ERROR) << "Milliseconds: " << delta.InMilliseconds(); |
| 125 | EXPECT_LT(delta.InMilliseconds(), 1000); |
| 126 | } |
| 127 | #endif |
| 128 | |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 129 | // Ensure that our SQLite version has working foreign key support with cascade |
| 130 | // delete support. |
| 131 | TEST_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)")); |
| 138 | |
| 139 | // Inserting without a matching parent should fail with constraint violation. |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 140 | // Mask off any extended error codes for USE_SYSTEM_SQLITE. |
| 141 | int insertErr = db().ExecuteAndReturnErrorCode( |
| 142 | "INSERT INTO children VALUES (10, 1)"); |
| 143 | EXPECT_EQ(SQLITE_CONSTRAINT, (insertErr&0xff)); |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 144 | |
| 145 | size_t rows; |
| 146 | EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); |
| 147 | EXPECT_EQ(0u, rows); |
| 148 | |
| 149 | // Inserting with a matching parent should work. |
| 150 | ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)")); |
| 151 | EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)")); |
| 152 | EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)")); |
| 153 | EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); |
| 154 | EXPECT_EQ(2u, rows); |
| 155 | |
| 156 | // Deleting the parent should cascade, i.e., delete the children as well. |
| 157 | ASSERT_TRUE(db().Execute("DELETE FROM parents")); |
| 158 | EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); |
| 159 | EXPECT_EQ(0u, rows); |
| 160 | } |
| 161 | |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 162 | #if defined(MOJO_APPTEST_IMPL) || defined(OS_IOS) |
| 163 | // If the platform cannot support SQLite mmap'ed I/O, make sure SQLite isn't |
| 164 | // offering to support it. |
| 165 | TEST_F(SQLiteFeaturesTest, NoMmap) { |
rohitrao | 83d6b83a | 2016-06-21 07:25:57 | [diff] [blame] | 166 | #if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE) |
| 167 | if (base::ios::IsRunningOnIOS10OrLater()) { |
| 168 | // iOS 10 added mmap support for sqlite. |
| 169 | return; |
| 170 | } |
| 171 | #endif |
| 172 | |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 173 | // For recent versions of SQLite, SQLITE_MAX_MMAP_SIZE=0 can be used to |
| 174 | // disable mmap support. Alternately, sqlite3_config() could be used. In |
| 175 | // that case, the pragma will run successfully, but the size will always be 0. |
| 176 | // |
| 177 | // The SQLite embedded in older iOS releases predates the addition of mmap |
| 178 | // support. In that case the pragma will run without error, but no results |
| 179 | // are returned when querying the value. |
| 180 | // |
| 181 | // MojoVFS implements a no-op for xFileControl(). PRAGMA mmap_size is |
| 182 | // implemented in terms of SQLITE_FCNTL_MMAP_SIZE. In that case, the pragma |
| 183 | // will succeed but with no effect. |
| 184 | ignore_result(db().Execute("PRAGMA mmap_size = 1048576")); |
| 185 | sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); |
| 186 | ASSERT_TRUE(!s.Step() || !s.ColumnInt64(0)); |
| 187 | } |
rohitrao | 83d6b83a | 2016-06-21 07:25:57 | [diff] [blame] | 188 | #endif |
| 189 | |
| 190 | #if !defined(MOJO_APPTEST_IMPL) |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 191 | // Verify that OS file writes are reflected in the memory mapping of a |
| 192 | // memory-mapped file. Normally SQLite writes to memory-mapped files using |
| 193 | // memcpy(), which should stay consistent. Our SQLite is slightly patched to |
| 194 | // mmap read only, then write using OS file writes. If the memory-mapped |
| 195 | // version doesn't reflect the OS file writes, SQLite's memory-mapped I/O should |
| 196 | // be disabled on this platform using SQLITE_MAX_MMAP_SIZE=0. |
| 197 | TEST_F(SQLiteFeaturesTest, Mmap) { |
rohitrao | 83d6b83a | 2016-06-21 07:25:57 | [diff] [blame] | 198 | #if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE) |
| 199 | if (!base::ios::IsRunningOnIOS10OrLater()) { |
| 200 | // iOS9's sqlite does not support mmap, so this test must be skipped. |
| 201 | return; |
| 202 | } |
| 203 | #endif |
| 204 | |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 205 | // Try to turn on mmap'ed I/O. |
| 206 | ignore_result(db().Execute("PRAGMA mmap_size = 1048576")); |
| 207 | { |
| 208 | sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); |
| 209 | |
| 210 | #if !defined(USE_SYSTEM_SQLITE) |
| 211 | // With Chromium's version of SQLite, the setting should always be non-zero. |
| 212 | ASSERT_TRUE(s.Step()); |
| 213 | ASSERT_GT(s.ColumnInt64(0), 0); |
| 214 | #else |
| 215 | // With the system SQLite, don't verify underlying mmap functionality if the |
| 216 | // SQLite is too old to support mmap, or if mmap is disabled (see NoMmap |
| 217 | // test). USE_SYSTEM_SQLITE is not bundled into the NoMmap case because |
| 218 | // whether mmap is enabled or not is outside of Chromium's control. |
| 219 | if (!s.Step() || !s.ColumnInt64(0)) |
| 220 | return; |
| 221 | #endif |
| 222 | } |
| 223 | db().Close(); |
| 224 | |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 225 | const uint32_t kFlags = |
| 226 | base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE; |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 227 | char buf[4096]; |
| 228 | |
| 229 | // Create a file with a block of '0', a block of '1', and a block of '2'. |
| 230 | { |
| 231 | base::File f(db_path(), kFlags); |
| 232 | ASSERT_TRUE(f.IsValid()); |
| 233 | memset(buf, '0', sizeof(buf)); |
| 234 | ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 235 | |
| 236 | memset(buf, '1', sizeof(buf)); |
| 237 | ASSERT_EQ(f.Write(1*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 238 | |
| 239 | memset(buf, '2', sizeof(buf)); |
| 240 | ASSERT_EQ(f.Write(2*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 241 | } |
| 242 | |
| 243 | // mmap the file and verify that everything looks right. |
| 244 | { |
| 245 | base::MemoryMappedFile m; |
| 246 | ASSERT_TRUE(m.Initialize(db_path())); |
| 247 | |
| 248 | memset(buf, '0', sizeof(buf)); |
| 249 | ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); |
| 250 | |
| 251 | memset(buf, '1', sizeof(buf)); |
| 252 | ASSERT_EQ(0, memcmp(buf, m.data() + 1*sizeof(buf), sizeof(buf))); |
| 253 | |
| 254 | memset(buf, '2', sizeof(buf)); |
| 255 | ASSERT_EQ(0, memcmp(buf, m.data() + 2*sizeof(buf), sizeof(buf))); |
| 256 | |
| 257 | // Scribble some '3' into the first page of the file, and verify that it |
| 258 | // looks the same in the memory mapping. |
| 259 | { |
| 260 | base::File f(db_path(), kFlags); |
| 261 | ASSERT_TRUE(f.IsValid()); |
| 262 | memset(buf, '3', sizeof(buf)); |
| 263 | ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 264 | } |
| 265 | ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); |
| 266 | |
| 267 | // Repeat with a single '4' in case page-sized blocks are different. |
| 268 | const size_t kOffset = 1*sizeof(buf) + 123; |
| 269 | ASSERT_NE('4', m.data()[kOffset]); |
| 270 | { |
| 271 | base::File f(db_path(), kFlags); |
| 272 | ASSERT_TRUE(f.IsValid()); |
| 273 | buf[0] = '4'; |
| 274 | ASSERT_EQ(f.Write(kOffset, buf, 1), 1); |
| 275 | } |
| 276 | ASSERT_EQ('4', m.data()[kOffset]); |
| 277 | } |
| 278 | } |
| 279 | #endif |
| 280 | |
shess | 001ae16 | 2016-10-20 04:04:32 | [diff] [blame] | 281 | // Verify that http://crbug.com/248608 is fixed. In this bug, the |
| 282 | // compiled regular expression is effectively cached with the prepared |
| 283 | // statement, causing errors if the regular expression is rebound. |
| 284 | TEST_F(SQLiteFeaturesTest, CachedRegexp) { |
| 285 | ASSERT_TRUE(db().Execute("CREATE TABLE r (id INTEGER UNIQUE, x TEXT)")); |
| 286 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (1, 'this is a test')")); |
| 287 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (2, 'that was a test')")); |
| 288 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (3, 'this is a stickup')")); |
| 289 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (4, 'that sucks')")); |
| 290 | |
| 291 | const char* kSimpleSql = "SELECT SUM(id) FROM r WHERE x REGEXP ?"; |
| 292 | sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
| 293 | |
| 294 | s.BindString(0, "this.*"); |
| 295 | ASSERT_TRUE(s.Step()); |
| 296 | EXPECT_EQ(4, s.ColumnInt(0)); |
| 297 | |
| 298 | s.Reset(true); |
| 299 | s.BindString(0, "that.*"); |
| 300 | ASSERT_TRUE(s.Step()); |
| 301 | EXPECT_EQ(6, s.ColumnInt(0)); |
| 302 | |
| 303 | s.Reset(true); |
| 304 | s.BindString(0, ".*test"); |
| 305 | ASSERT_TRUE(s.Step()); |
| 306 | EXPECT_EQ(3, s.ColumnInt(0)); |
| 307 | |
| 308 | s.Reset(true); |
| 309 | s.BindString(0, ".* s[a-z]+"); |
| 310 | ASSERT_TRUE(s.Step()); |
| 311 | EXPECT_EQ(7, s.ColumnInt(0)); |
| 312 | } |
| 313 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 314 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 315 | base::ScopedCFTypeRef<CFURLRef> CFURLRefForPath(const base::FilePath& path){ |
| 316 | base::ScopedCFTypeRef<CFStringRef> urlString( |
| 317 | CFStringCreateWithFileSystemRepresentation( |
| 318 | kCFAllocatorDefault, path.value().c_str())); |
| 319 | base::ScopedCFTypeRef<CFURLRef> url( |
| 320 | CFURLCreateWithFileSystemPath(kCFAllocatorDefault, urlString, |
| 321 | kCFURLPOSIXPathStyle, FALSE)); |
| 322 | return url; |
| 323 | } |
| 324 | |
| 325 | // If a database file is marked to be excluded from Time Machine, verify that |
| 326 | // journal files are also excluded. |
| 327 | // TODO(shess): Disabled because CSBackupSetItemExcluded() does not work on the |
| 328 | // bots, though it's fine on dev machines. See <http://crbug.com/410350>. |
| 329 | TEST_F(SQLiteFeaturesTest, DISABLED_TimeMachine) { |
| 330 | ASSERT_TRUE(db().Execute("CREATE TABLE t (id INTEGER PRIMARY KEY)")); |
| 331 | db().Close(); |
| 332 | |
| 333 | base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal")); |
| 334 | ASSERT_TRUE(GetPathExists(db_path())); |
| 335 | ASSERT_TRUE(GetPathExists(journal)); |
| 336 | |
| 337 | base::ScopedCFTypeRef<CFURLRef> dbURL(CFURLRefForPath(db_path())); |
| 338 | base::ScopedCFTypeRef<CFURLRef> journalURL(CFURLRefForPath(journal)); |
| 339 | |
| 340 | // Not excluded to start. |
| 341 | EXPECT_FALSE(CSBackupIsItemExcluded(dbURL, NULL)); |
| 342 | EXPECT_FALSE(CSBackupIsItemExcluded(journalURL, NULL)); |
| 343 | |
| 344 | // Exclude the main database file. |
| 345 | EXPECT_TRUE(base::mac::SetFileBackupExclusion(db_path())); |
| 346 | |
| 347 | Boolean excluded_by_path = FALSE; |
| 348 | EXPECT_TRUE(CSBackupIsItemExcluded(dbURL, &excluded_by_path)); |
| 349 | EXPECT_FALSE(excluded_by_path); |
| 350 | EXPECT_FALSE(CSBackupIsItemExcluded(journalURL, NULL)); |
| 351 | |
| 352 | EXPECT_TRUE(db().Open(db_path())); |
| 353 | ASSERT_TRUE(db().Execute("INSERT INTO t VALUES (1)")); |
| 354 | EXPECT_TRUE(CSBackupIsItemExcluded(dbURL, &excluded_by_path)); |
| 355 | EXPECT_FALSE(excluded_by_path); |
| 356 | EXPECT_TRUE(CSBackupIsItemExcluded(journalURL, &excluded_by_path)); |
| 357 | EXPECT_FALSE(excluded_by_path); |
| 358 | |
| 359 | // TODO(shess): In WAL mode this will touch -wal and -shm files. -shm files |
| 360 | // could be always excluded. |
| 361 | } |
| 362 | #endif |
| 363 | |
shess | e812793 | 2017-03-27 19:16:10 | [diff] [blame] | 364 | #if !defined(USE_SYSTEM_SQLITE) |
| 365 | // Test that Chromium's patch to make auto_vacuum integrate with |
| 366 | // SQLITE_FCNTL_CHUNK_SIZE is working. |
| 367 | TEST_F(SQLiteFeaturesTest, SmartAutoVacuum) { |
| 368 | // Turn on auto_vacuum, and set the page size low to make results obvious. |
| 369 | // These settings require re-writing the database, which VACUUM does. |
| 370 | ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum = FULL")); |
| 371 | ASSERT_TRUE(db().Execute("PRAGMA page_size = 1024")); |
| 372 | ASSERT_TRUE(db().Execute("VACUUM")); |
| 373 | |
| 374 | // Code-coverage of the PRAGMA set/get implementation. |
| 375 | const char kPragmaSql[] = "PRAGMA auto_vacuum_slack_pages"; |
| 376 | ASSERT_EQ("0", sql::test::ExecuteWithResult(&db(), kPragmaSql)); |
| 377 | ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 4")); |
| 378 | ASSERT_EQ("4", sql::test::ExecuteWithResult(&db(), kPragmaSql)); |
| 379 | // Max out at 255. |
| 380 | ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 1000")); |
| 381 | ASSERT_EQ("255", sql::test::ExecuteWithResult(&db(), kPragmaSql)); |
| 382 | ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 0")); |
| 383 | |
| 384 | // With page_size=1024, the following will insert rows which take up an |
| 385 | // overflow page, plus a small header in a b-tree node. An empty table takes |
| 386 | // a single page, so for small row counts each insert will add one page, and |
| 387 | // each delete will remove one page. |
| 388 | const char kCreateSql[] = "CREATE TABLE t (id INTEGER PRIMARY KEY, value)"; |
| 389 | const char kInsertSql[] = "INSERT INTO t (value) VALUES (randomblob(980))"; |
| 390 | #if !defined(OS_WIN) |
| 391 | const char kDeleteSql[] = "DELETE FROM t WHERE id = (SELECT MIN(id) FROM t)"; |
| 392 | #endif |
| 393 | |
| 394 | // This database will be 34 overflow pages plus the table's root page plus the |
| 395 | // SQLite header page plus the freelist page. |
| 396 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 397 | { |
| 398 | sql::Statement s(db().GetUniqueStatement(kInsertSql)); |
| 399 | for (int i = 0; i < 34; ++i) { |
| 400 | s.Reset(true); |
| 401 | ASSERT_TRUE(s.Run()); |
| 402 | } |
| 403 | } |
| 404 | ASSERT_EQ("37", sql::test::ExecuteWithResult(&db(), "PRAGMA page_count")); |
| 405 | |
| 406 | // http://sqlite.org/mmap.html indicates that Windows will silently fail when |
| 407 | // truncating a memory-mapped file. That pretty much invalidates these tests |
| 408 | // against the actual file size. |
| 409 | #if !defined(OS_WIN) |
| 410 | // Each delete will delete a single page, including crossing a |
| 411 | // multiple-of-four boundary. |
| 412 | { |
| 413 | sql::Statement s(db().GetUniqueStatement(kDeleteSql)); |
| 414 | for (int i = 0; i < 5; ++i) { |
| 415 | int64_t file_size_before, file_size_after; |
| 416 | ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before)); |
| 417 | |
| 418 | s.Reset(true); |
| 419 | ASSERT_TRUE(s.Run()); |
| 420 | |
| 421 | ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after)); |
| 422 | ASSERT_EQ(file_size_after, file_size_before - 1024); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // Turn on "smart" auto-vacuum to remove 4 pages at a time. |
| 427 | ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum_slack_pages = 4")); |
| 428 | |
| 429 | // No pages removed, then four deleted at once. |
| 430 | { |
| 431 | sql::Statement s(db().GetUniqueStatement(kDeleteSql)); |
| 432 | for (int i = 0; i < 3; ++i) { |
| 433 | int64_t file_size_before, file_size_after; |
| 434 | ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before)); |
| 435 | |
| 436 | s.Reset(true); |
| 437 | ASSERT_TRUE(s.Run()); |
| 438 | |
| 439 | ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after)); |
| 440 | ASSERT_EQ(file_size_after, file_size_before); |
| 441 | } |
| 442 | |
| 443 | int64_t file_size_before, file_size_after; |
| 444 | ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_before)); |
| 445 | |
| 446 | s.Reset(true); |
| 447 | ASSERT_TRUE(s.Run()); |
| 448 | |
| 449 | ASSERT_TRUE(base::GetFileSize(db_path(), &file_size_after)); |
| 450 | ASSERT_EQ(file_size_after, file_size_before - 4096); |
| 451 | } |
| 452 | #endif |
| 453 | } |
| 454 | #endif // !defined(USE_SYSTEM_SQLITE) |
| 455 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 456 | } // namespace |