[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" |
Scott Graham | 502836c | 2017-09-14 23:26:23 | [diff] [blame] | 14 | #include "build/build_config.h" |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 15 | #include "sql/database.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 16 | #include "sql/statement.h" |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 17 | #include "sql/test/sql_test_base.h" |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 18 | #include "sql/test/test_helpers.h" |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | #include "third_party/sqlite/sqlite3.h" |
| 21 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 22 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 23 | #include "base/mac/mac_util.h" |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 24 | #endif |
| 25 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 26 | // Test that certain features are/are-not enabled in our SQLite. |
| 27 | |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 28 | namespace sql { |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 29 | namespace { |
| 30 | |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 31 | using sql::test::ExecuteWithResult; |
| 32 | using sql::test::ExecuteWithResults; |
| 33 | |
[email protected] | 526b466 | 2013-06-14 04:09:12 | [diff] [blame] | 34 | void CaptureErrorCallback(int* error_pointer, std::string* sql_text, |
| 35 | int error, sql::Statement* stmt) { |
| 36 | *error_pointer = error; |
Victor Costan | bd62311 | 2018-07-18 04:17:27 | [diff] [blame] | 37 | const char* text = stmt ? stmt->GetSQLStatement() : nullptr; |
[email protected] | 526b466 | 2013-06-14 04:09:12 | [diff] [blame] | 38 | *sql_text = text ? text : "no statement available"; |
| 39 | } |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 40 | |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 41 | } // namespace |
| 42 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 43 | class SQLiteFeaturesTest : public sql::SQLTestBase { |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 44 | public: |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 45 | SQLiteFeaturesTest() : error_(SQLITE_OK) {} |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 46 | |
dcheng | 1b3b125e | 2014-12-22 23:00:24 | [diff] [blame] | 47 | void SetUp() override { |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 48 | SQLTestBase::SetUp(); |
[email protected] | 9903468 | 2012-06-13 03:31:16 | [diff] [blame] | 49 | |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 50 | // The error delegate will set |error_| and |sql_text_| when any sqlite |
| 51 | // statement operation returns an error code. |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 52 | db().set_error_callback( |
tzik | d16d219 | 2018-03-07 08:58:36 | [diff] [blame] | 53 | base::BindRepeating(&CaptureErrorCallback, &error_, &sql_text_)); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 54 | } |
| 55 | |
dcheng | 1b3b125e | 2014-12-22 23:00:24 | [diff] [blame] | 56 | void TearDown() override { |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 57 | // If any error happened the original sql statement can be found in |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 58 | // |sql_text_|. |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 59 | EXPECT_EQ(SQLITE_OK, error_) << sql_text_; |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 60 | |
| 61 | SQLTestBase::TearDown(); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 62 | } |
| 63 | |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 64 | int error() { return error_; } |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 65 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 66 | private: |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 67 | // The error code of the most recent error. |
| 68 | int error_; |
| 69 | // Original statement which has caused the error. |
| 70 | std::string sql_text_; |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | // Do not include fts1 support, it is not useful, and nobody is |
| 74 | // looking at it. |
| 75 | TEST_F(SQLiteFeaturesTest, NoFTS1) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 76 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( |
| 77 | "CREATE VIRTUAL TABLE foo USING fts1(x)")); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 78 | } |
| 79 | |
shess | 37437cb | 2015-03-11 20:24:46 | [diff] [blame] | 80 | // Do not include fts2 support, it is not useful, and nobody is |
| 81 | // looking at it. |
| 82 | TEST_F(SQLiteFeaturesTest, NoFTS2) { |
| 83 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( |
| 84 | "CREATE VIRTUAL TABLE foo USING fts2(x)")); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 85 | } |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 86 | |
shess | 37437cb | 2015-03-11 20:24:46 | [diff] [blame] | 87 | // fts3 used to be used for history files, and may also be used by WebDatabase |
| 88 | // clients. |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 89 | TEST_F(SQLiteFeaturesTest, FTS3) { |
| 90 | ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)")); |
| 91 | } |
| 92 | |
shess | 37437cb | 2015-03-11 20:24:46 | [diff] [blame] | 93 | // Originally history used fts2, which Chromium patched to treat "foo*" as a |
| 94 | // prefix search, though the icu tokenizer would return it as two tokens {"foo", |
| 95 | // "*"}. Test that fts3 works correctly. |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 96 | TEST_F(SQLiteFeaturesTest, FTS3_Prefix) { |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 97 | static const char kCreateSql[] = |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 98 | "CREATE VIRTUAL TABLE foo USING fts3(x, tokenize icu)"; |
| 99 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 100 | |
| 101 | ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')")); |
| 102 | |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 103 | EXPECT_EQ("test", |
| 104 | ExecuteWithResult(&db(), "SELECT x FROM foo WHERE x MATCH 'te*'")); |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 105 | } |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 106 | |
shess | 156733db | 2015-01-21 21:52:24 | [diff] [blame] | 107 | // Verify that Chromium's SQLite is compiled with HAVE_USLEEP defined. With |
| 108 | // HAVE_USLEEP, SQLite uses usleep() with millisecond granularity. Otherwise it |
| 109 | // uses sleep() with second granularity. |
| 110 | TEST_F(SQLiteFeaturesTest, UsesUsleep) { |
| 111 | base::TimeTicks before = base::TimeTicks::Now(); |
| 112 | sqlite3_sleep(1); |
| 113 | base::TimeDelta delta = base::TimeTicks::Now() - before; |
| 114 | |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 115 | // It is not impossible for this to be over 1000 if things are compiled |
| 116 | // correctly, but that is very unlikely. Most platforms seem to be exactly |
| 117 | // 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] | 118 | EXPECT_LT(delta.InMilliseconds(), 1000); |
| 119 | } |
shess | 156733db | 2015-01-21 21:52:24 | [diff] [blame] | 120 | |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 121 | // Ensure that our SQLite version has working foreign key support with cascade |
| 122 | // delete support. |
| 123 | TEST_F(SQLiteFeaturesTest, ForeignKeySupport) { |
| 124 | ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1")); |
| 125 | ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)")); |
| 126 | ASSERT_TRUE(db().Execute( |
| 127 | "CREATE TABLE children (" |
| 128 | " id INTEGER PRIMARY KEY," |
| 129 | " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)")); |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 130 | static const char kSelectParentsSql[] = "SELECT * FROM parents ORDER BY id"; |
| 131 | static const char kSelectChildrenSql[] = "SELECT * FROM children ORDER BY id"; |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 132 | |
| 133 | // Inserting without a matching parent should fail with constraint violation. |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 134 | EXPECT_EQ("", ExecuteWithResult(&db(), kSelectParentsSql)); |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 135 | const int insert_error = |
| 136 | db().ExecuteAndReturnErrorCode("INSERT INTO children VALUES (10, 1)"); |
Victor Costan | 86ca431 | 2018-02-07 21:34:10 | [diff] [blame] | 137 | EXPECT_EQ(SQLITE_CONSTRAINT | SQLITE_CONSTRAINT_FOREIGNKEY, insert_error); |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 138 | EXPECT_EQ("", ExecuteWithResult(&db(), kSelectChildrenSql)); |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 139 | |
| 140 | // Inserting with a matching parent should work. |
| 141 | ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)")); |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 142 | EXPECT_EQ("1", ExecuteWithResults(&db(), kSelectParentsSql, "|", "\n")); |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 143 | EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)")); |
| 144 | EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)")); |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 145 | EXPECT_EQ("11|1\n12|1", |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 146 | ExecuteWithResults(&db(), kSelectChildrenSql, "|", "\n")); |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 147 | |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 148 | // Deleting the parent should cascade, deleting the children as well. |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 149 | ASSERT_TRUE(db().Execute("DELETE FROM parents")); |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 150 | EXPECT_EQ("", ExecuteWithResult(&db(), kSelectParentsSql)); |
| 151 | EXPECT_EQ("", ExecuteWithResult(&db(), kSelectChildrenSql)); |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 152 | } |
| 153 | |
Victor Costan | bc7ab06 | 2018-04-20 19:03:56 | [diff] [blame] | 154 | // Ensure that our SQLite version supports booleans. |
| 155 | TEST_F(SQLiteFeaturesTest, BooleanSupport) { |
| 156 | ASSERT_TRUE( |
| 157 | db().Execute("CREATE TABLE flags (" |
| 158 | " id INTEGER PRIMARY KEY," |
| 159 | " true_flag BOOL NOT NULL DEFAULT TRUE," |
| 160 | " false_flag BOOL NOT NULL DEFAULT FALSE)")); |
| 161 | ASSERT_TRUE(db().Execute( |
| 162 | "ALTER TABLE flags ADD COLUMN true_flag2 BOOL NOT NULL DEFAULT TRUE")); |
| 163 | ASSERT_TRUE(db().Execute( |
| 164 | "ALTER TABLE flags ADD COLUMN false_flag2 BOOL NOT NULL DEFAULT FALSE")); |
| 165 | ASSERT_TRUE(db().Execute("INSERT INTO flags (id) VALUES (1)")); |
| 166 | |
| 167 | sql::Statement s(db().GetUniqueStatement( |
| 168 | "SELECT true_flag, false_flag, true_flag2, false_flag2" |
| 169 | " FROM flags WHERE id=1;")); |
| 170 | ASSERT_TRUE(s.Step()); |
| 171 | |
Victor Costan | f087c84 | 2018-04-27 00:47:50 | [diff] [blame] | 172 | EXPECT_TRUE(s.ColumnBool(0)) << " default TRUE at table creation time"; |
Victor Costan | bc7ab06 | 2018-04-20 19:03:56 | [diff] [blame] | 173 | EXPECT_TRUE(!s.ColumnBool(1)) << " default FALSE at table creation time"; |
| 174 | |
Victor Costan | f087c84 | 2018-04-27 00:47:50 | [diff] [blame] | 175 | EXPECT_TRUE(s.ColumnBool(2)) << " default TRUE added by altering the table"; |
Victor Costan | bc7ab06 | 2018-04-20 19:03:56 | [diff] [blame] | 176 | EXPECT_TRUE(!s.ColumnBool(3)) << " default FALSE added by altering the table"; |
| 177 | } |
| 178 | |
Victor Costan | 02bf6b6d | 2019-05-21 23:27:27 | [diff] [blame] | 179 | TEST_F(SQLiteFeaturesTest, IcuEnabled) { |
| 180 | sql::Statement lower_en( |
| 181 | db().GetUniqueStatement("SELECT lower('I', 'en_us')")); |
| 182 | ASSERT_TRUE(lower_en.Step()); |
| 183 | EXPECT_EQ("i", lower_en.ColumnString(0)); |
| 184 | |
| 185 | sql::Statement lower_tr( |
| 186 | db().GetUniqueStatement("SELECT lower('I', 'tr_tr')")); |
| 187 | ASSERT_TRUE(lower_tr.Step()); |
| 188 | EXPECT_EQ("\u0131", lower_tr.ColumnString(0)); |
| 189 | } |
| 190 | |
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) { |
| 198 | // Try to turn on mmap'ed I/O. |
| 199 | ignore_result(db().Execute("PRAGMA mmap_size = 1048576")); |
| 200 | { |
| 201 | sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); |
| 202 | |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 203 | ASSERT_TRUE(s.Step()); |
| 204 | ASSERT_GT(s.ColumnInt64(0), 0); |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 205 | } |
| 206 | db().Close(); |
| 207 | |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 208 | const uint32_t kFlags = |
| 209 | base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE; |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 210 | char buf[4096]; |
| 211 | |
| 212 | // Create a file with a block of '0', a block of '1', and a block of '2'. |
| 213 | { |
| 214 | base::File f(db_path(), kFlags); |
| 215 | ASSERT_TRUE(f.IsValid()); |
| 216 | memset(buf, '0', sizeof(buf)); |
| 217 | ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 218 | |
| 219 | memset(buf, '1', sizeof(buf)); |
| 220 | ASSERT_EQ(f.Write(1*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 221 | |
| 222 | memset(buf, '2', sizeof(buf)); |
| 223 | ASSERT_EQ(f.Write(2*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 224 | } |
| 225 | |
| 226 | // mmap the file and verify that everything looks right. |
| 227 | { |
| 228 | base::MemoryMappedFile m; |
| 229 | ASSERT_TRUE(m.Initialize(db_path())); |
| 230 | |
| 231 | memset(buf, '0', sizeof(buf)); |
| 232 | ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); |
| 233 | |
| 234 | memset(buf, '1', sizeof(buf)); |
| 235 | ASSERT_EQ(0, memcmp(buf, m.data() + 1*sizeof(buf), sizeof(buf))); |
| 236 | |
| 237 | memset(buf, '2', sizeof(buf)); |
| 238 | ASSERT_EQ(0, memcmp(buf, m.data() + 2*sizeof(buf), sizeof(buf))); |
| 239 | |
| 240 | // Scribble some '3' into the first page of the file, and verify that it |
| 241 | // looks the same in the memory mapping. |
| 242 | { |
| 243 | base::File f(db_path(), kFlags); |
| 244 | ASSERT_TRUE(f.IsValid()); |
| 245 | memset(buf, '3', sizeof(buf)); |
| 246 | ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 247 | } |
| 248 | ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); |
| 249 | |
| 250 | // Repeat with a single '4' in case page-sized blocks are different. |
| 251 | const size_t kOffset = 1*sizeof(buf) + 123; |
| 252 | ASSERT_NE('4', m.data()[kOffset]); |
| 253 | { |
| 254 | base::File f(db_path(), kFlags); |
| 255 | ASSERT_TRUE(f.IsValid()); |
| 256 | buf[0] = '4'; |
| 257 | ASSERT_EQ(f.Write(kOffset, buf, 1), 1); |
| 258 | } |
| 259 | ASSERT_EQ('4', m.data()[kOffset]); |
| 260 | } |
| 261 | } |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 262 | |
shess | 001ae16 | 2016-10-20 04:04:32 | [diff] [blame] | 263 | // Verify that http://crbug.com/248608 is fixed. In this bug, the |
| 264 | // compiled regular expression is effectively cached with the prepared |
| 265 | // statement, causing errors if the regular expression is rebound. |
| 266 | TEST_F(SQLiteFeaturesTest, CachedRegexp) { |
| 267 | ASSERT_TRUE(db().Execute("CREATE TABLE r (id INTEGER UNIQUE, x TEXT)")); |
| 268 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (1, 'this is a test')")); |
| 269 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (2, 'that was a test')")); |
| 270 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (3, 'this is a stickup')")); |
| 271 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (4, 'that sucks')")); |
| 272 | |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 273 | static const char kSimpleSql[] = "SELECT SUM(id) FROM r WHERE x REGEXP ?"; |
shess | 001ae16 | 2016-10-20 04:04:32 | [diff] [blame] | 274 | sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
| 275 | |
| 276 | s.BindString(0, "this.*"); |
| 277 | ASSERT_TRUE(s.Step()); |
| 278 | EXPECT_EQ(4, s.ColumnInt(0)); |
| 279 | |
| 280 | s.Reset(true); |
| 281 | s.BindString(0, "that.*"); |
| 282 | ASSERT_TRUE(s.Step()); |
| 283 | EXPECT_EQ(6, s.ColumnInt(0)); |
| 284 | |
| 285 | s.Reset(true); |
| 286 | s.BindString(0, ".*test"); |
| 287 | ASSERT_TRUE(s.Step()); |
| 288 | EXPECT_EQ(3, s.ColumnInt(0)); |
| 289 | |
| 290 | s.Reset(true); |
| 291 | s.BindString(0, ".* s[a-z]+"); |
| 292 | ASSERT_TRUE(s.Step()); |
| 293 | EXPECT_EQ(7, s.ColumnInt(0)); |
| 294 | } |
| 295 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 296 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 297 | // If a database file is marked to be excluded from Time Machine, verify that |
| 298 | // journal files are also excluded. |
Victor Costan | 653b14f | 2018-07-23 19:32:33 | [diff] [blame] | 299 | TEST_F(SQLiteFeaturesTest, TimeMachine) { |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 300 | ASSERT_TRUE(db().Execute("CREATE TABLE t (id INTEGER PRIMARY KEY)")); |
| 301 | db().Close(); |
| 302 | |
Victor Costan | 7e74dce | 2019-01-28 20:15:25 | [diff] [blame] | 303 | base::FilePath journal_path = sql::Database::JournalPath(db_path()); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 304 | ASSERT_TRUE(GetPathExists(db_path())); |
Victor Costan | 7e74dce | 2019-01-28 20:15:25 | [diff] [blame] | 305 | ASSERT_TRUE(GetPathExists(journal_path)); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 306 | |
| 307 | // Not excluded to start. |
Victor Costan | 7e74dce | 2019-01-28 20:15:25 | [diff] [blame] | 308 | EXPECT_FALSE(base::mac::GetFileBackupExclusion(db_path())); |
| 309 | EXPECT_FALSE(base::mac::GetFileBackupExclusion(journal_path)); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 310 | |
| 311 | // Exclude the main database file. |
| 312 | EXPECT_TRUE(base::mac::SetFileBackupExclusion(db_path())); |
| 313 | |
Victor Costan | 7e74dce | 2019-01-28 20:15:25 | [diff] [blame] | 314 | EXPECT_TRUE(base::mac::GetFileBackupExclusion(db_path())); |
| 315 | EXPECT_FALSE(base::mac::GetFileBackupExclusion(journal_path)); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 316 | |
| 317 | EXPECT_TRUE(db().Open(db_path())); |
| 318 | ASSERT_TRUE(db().Execute("INSERT INTO t VALUES (1)")); |
Victor Costan | 7e74dce | 2019-01-28 20:15:25 | [diff] [blame] | 319 | EXPECT_TRUE(base::mac::GetFileBackupExclusion(db_path())); |
| 320 | EXPECT_TRUE(base::mac::GetFileBackupExclusion(journal_path)); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 321 | |
| 322 | // TODO(shess): In WAL mode this will touch -wal and -shm files. -shm files |
| 323 | // could be always excluded. |
| 324 | } |
| 325 | #endif |
| 326 | |
Victor Costan | 86ca431 | 2018-02-07 21:34:10 | [diff] [blame] | 327 | #if !defined(OS_FUCHSIA) |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 328 | // SQLite WAL mode defaults to checkpointing the WAL on close. This would push |
| 329 | // additional work into Chromium shutdown. Verify that SQLite supports a config |
| 330 | // option to not checkpoint on close. |
| 331 | TEST_F(SQLiteFeaturesTest, WALNoClose) { |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 332 | base::FilePath wal_path = sql::Database::WriteAheadLogPath(db_path()); |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 333 | |
| 334 | // Turn on WAL mode, then verify that the mode changed (WAL is supported). |
| 335 | ASSERT_TRUE(db().Execute("PRAGMA journal_mode = WAL")); |
| 336 | ASSERT_EQ("wal", ExecuteWithResult(&db(), "PRAGMA journal_mode")); |
| 337 | |
| 338 | // The WAL file is created lazily on first change. |
| 339 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 340 | |
| 341 | // By default, the WAL is checkpointed then deleted on close. |
| 342 | ASSERT_TRUE(GetPathExists(wal_path)); |
| 343 | db().Close(); |
| 344 | ASSERT_FALSE(GetPathExists(wal_path)); |
| 345 | |
| 346 | // Reopen and configure the database to not checkpoint WAL on close. |
| 347 | ASSERT_TRUE(Reopen()); |
| 348 | ASSERT_TRUE(db().Execute("PRAGMA journal_mode = WAL")); |
| 349 | ASSERT_TRUE(db().Execute("ALTER TABLE foo ADD COLUMN c")); |
Victor Costan | bd62311 | 2018-07-18 04:17:27 | [diff] [blame] | 350 | ASSERT_EQ(SQLITE_OK, |
| 351 | sqlite3_db_config(db().db_, SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, 1, |
| 352 | nullptr)); |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 353 | ASSERT_TRUE(GetPathExists(wal_path)); |
| 354 | db().Close(); |
| 355 | ASSERT_TRUE(GetPathExists(wal_path)); |
| 356 | } |
| 357 | #endif |
| 358 | |
| 359 | } // namespace sql |