[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 | |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame^] | 35 | namespace sql { |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 36 | namespace { |
| 37 | |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame^] | 38 | using sql::test::ExecuteWithResult; |
| 39 | using sql::test::ExecuteWithResults; |
| 40 | |
[email protected] | 526b466 | 2013-06-14 04:09:12 | [diff] [blame] | 41 | void 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] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 47 | |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame^] | 48 | } // namespace |
| 49 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 50 | class SQLiteFeaturesTest : public sql::SQLTestBase { |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 51 | public: |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 52 | SQLiteFeaturesTest() : error_(SQLITE_OK) {} |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 53 | |
dcheng | 1b3b125e | 2014-12-22 23:00:24 | [diff] [blame] | 54 | void SetUp() override { |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 55 | SQLTestBase::SetUp(); |
[email protected] | 9903468 | 2012-06-13 03:31:16 | [diff] [blame] | 56 | |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 57 | // The error delegate will set |error_| and |sql_text_| when any sqlite |
| 58 | // statement operation returns an error code. |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 59 | db().set_error_callback( |
| 60 | base::Bind(&CaptureErrorCallback, &error_, &sql_text_)); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 61 | } |
| 62 | |
dcheng | 1b3b125e | 2014-12-22 23:00:24 | [diff] [blame] | 63 | void TearDown() override { |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 64 | // If any error happened the original sql statement can be found in |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 65 | // |sql_text_|. |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 66 | EXPECT_EQ(SQLITE_OK, error_) << sql_text_; |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 67 | |
| 68 | SQLTestBase::TearDown(); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 69 | } |
| 70 | |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 71 | int error() { return error_; } |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 72 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 73 | private: |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 74 | // 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] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | // Do not include fts1 support, it is not useful, and nobody is |
| 81 | // looking at it. |
| 82 | TEST_F(SQLiteFeaturesTest, NoFTS1) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 83 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( |
| 84 | "CREATE VIRTUAL TABLE foo USING fts1(x)")); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 85 | } |
| 86 | |
shess | 37437cb | 2015-03-11 20:24:46 | [diff] [blame] | 87 | // Do not include fts2 support, it is not useful, and nobody is |
| 88 | // looking at it. |
| 89 | TEST_F(SQLiteFeaturesTest, NoFTS2) { |
| 90 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( |
| 91 | "CREATE VIRTUAL TABLE foo USING fts2(x)")); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 92 | } |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 93 | |
shess | 37437cb | 2015-03-11 20:24:46 | [diff] [blame] | 94 | // fts3 used to be used for history files, and may also be used by WebDatabase |
| 95 | // clients. |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 96 | TEST_F(SQLiteFeaturesTest, FTS3) { |
| 97 | ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)")); |
| 98 | } |
| 99 | |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 100 | #if !defined(USE_SYSTEM_SQLITE) |
shess | 37437cb | 2015-03-11 20:24:46 | [diff] [blame] | 101 | // 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. |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 104 | TEST_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 | |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame^] | 111 | EXPECT_EQ("test", |
| 112 | ExecuteWithResult(&db(), "SELECT x FROM foo WHERE x MATCH 'te*'")); |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 113 | } |
| 114 | #endif |
| 115 | |
shess | 156733db | 2015-01-21 21:52:24 | [diff] [blame] | 116 | #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. |
| 120 | TEST_F(SQLiteFeaturesTest, UsesUsleep) { |
| 121 | base::TimeTicks before = base::TimeTicks::Now(); |
| 122 | sqlite3_sleep(1); |
| 123 | base::TimeDelta delta = base::TimeTicks::Now() - before; |
| 124 | |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame^] | 125 | // 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. |
shess | 156733db | 2015-01-21 21:52:24 | [diff] [blame] | 128 | EXPECT_LT(delta.InMilliseconds(), 1000); |
| 129 | } |
| 130 | #endif |
| 131 | |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 132 | // Ensure that our SQLite version has working foreign key support with cascade |
| 133 | // delete support. |
| 134 | TEST_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)")); |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame^] | 141 | const char kSelectParents[] = "SELECT * FROM parents ORDER BY id"; |
| 142 | const char kSelectChildren[] = "SELECT * FROM children ORDER BY id"; |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 143 | |
| 144 | // Inserting without a matching parent should fail with constraint violation. |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 145 | // Mask off any extended error codes for USE_SYSTEM_SQLITE. |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame^] | 146 | 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)); |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 151 | |
| 152 | // Inserting with a matching parent should work. |
| 153 | ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)")); |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame^] | 154 | EXPECT_EQ("1", ExecuteWithResults(&db(), kSelectParents, "|", "\n")); |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 155 | EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)")); |
| 156 | EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)")); |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame^] | 157 | EXPECT_EQ("11|1\n12|1", |
| 158 | ExecuteWithResults(&db(), kSelectChildren, "|", "\n")); |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 159 | |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame^] | 160 | // Deleting the parent should cascade, deleting the children as well. |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 161 | ASSERT_TRUE(db().Execute("DELETE FROM parents")); |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame^] | 162 | EXPECT_EQ("", ExecuteWithResult(&db(), kSelectParents)); |
| 163 | EXPECT_EQ("", ExecuteWithResult(&db(), kSelectChildren)); |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 164 | } |
| 165 | |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 166 | #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. |
| 169 | TEST_F(SQLiteFeaturesTest, NoMmap) { |
rohitrao | 83d6b83a | 2016-06-21 07:25:57 | [diff] [blame] | 170 | #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 | |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 177 | // 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 | } |
rohitrao | 83d6b83a | 2016-06-21 07:25:57 | [diff] [blame] | 192 | #endif |
| 193 | |
| 194 | #if !defined(MOJO_APPTEST_IMPL) |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 195 | // 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. |
| 201 | TEST_F(SQLiteFeaturesTest, Mmap) { |
rohitrao | 83d6b83a | 2016-06-21 07:25:57 | [diff] [blame] | 202 | #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 | |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 209 | // 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 | |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 229 | const uint32_t kFlags = |
| 230 | base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE; |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 231 | 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 | |
shess | 001ae16 | 2016-10-20 04:04:32 | [diff] [blame] | 285 | // 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. |
| 288 | TEST_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 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 318 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 319 | base::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>. |
| 333 | TEST_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 | |
shess | e812793 | 2017-03-27 19:16:10 | [diff] [blame] | 368 | #if !defined(USE_SYSTEM_SQLITE) |
| 369 | // Test that Chromium's patch to make auto_vacuum integrate with |
| 370 | // SQLITE_FCNTL_CHUNK_SIZE is working. |
| 371 | TEST_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 | |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame^] | 460 | #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. |
| 464 | TEST_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 |