[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 | e3d9c4b | 2018-11-24 09:07:40 | [diff] [blame] | 179 | #if defined(OS_FUCHSIA) |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 180 | // If the platform cannot support SQLite mmap'ed I/O, make sure SQLite isn't |
| 181 | // offering to support it. |
| 182 | TEST_F(SQLiteFeaturesTest, NoMmap) { |
| 183 | // For recent versions of SQLite, SQLITE_MAX_MMAP_SIZE=0 can be used to |
| 184 | // disable mmap support. Alternately, sqlite3_config() could be used. In |
| 185 | // that case, the pragma will run successfully, but the size will always be 0. |
| 186 | // |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 187 | // MojoVFS implements a no-op for xFileControl(). PRAGMA mmap_size is |
| 188 | // implemented in terms of SQLITE_FCNTL_MMAP_SIZE. In that case, the pragma |
| 189 | // will succeed but with no effect. |
| 190 | ignore_result(db().Execute("PRAGMA mmap_size = 1048576")); |
| 191 | sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); |
| 192 | ASSERT_TRUE(!s.Step() || !s.ColumnInt64(0)); |
| 193 | } |
Victor Costan | e3d9c4b | 2018-11-24 09:07:40 | [diff] [blame] | 194 | #endif // defined(OS_FUCHSIA) |
rohitrao | 83d6b83a | 2016-06-21 07:25:57 | [diff] [blame] | 195 | |
Victor Costan | e3d9c4b | 2018-11-24 09:07:40 | [diff] [blame] | 196 | #if !defined(OS_FUCHSIA) |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 197 | // Verify that OS file writes are reflected in the memory mapping of a |
| 198 | // memory-mapped file. Normally SQLite writes to memory-mapped files using |
| 199 | // memcpy(), which should stay consistent. Our SQLite is slightly patched to |
| 200 | // mmap read only, then write using OS file writes. If the memory-mapped |
| 201 | // version doesn't reflect the OS file writes, SQLite's memory-mapped I/O should |
| 202 | // be disabled on this platform using SQLITE_MAX_MMAP_SIZE=0. |
| 203 | TEST_F(SQLiteFeaturesTest, Mmap) { |
| 204 | // Try to turn on mmap'ed I/O. |
| 205 | ignore_result(db().Execute("PRAGMA mmap_size = 1048576")); |
| 206 | { |
| 207 | sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); |
| 208 | |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 209 | ASSERT_TRUE(s.Step()); |
| 210 | ASSERT_GT(s.ColumnInt64(0), 0); |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 211 | } |
| 212 | db().Close(); |
| 213 | |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 214 | const uint32_t kFlags = |
| 215 | base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE; |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 216 | char buf[4096]; |
| 217 | |
| 218 | // Create a file with a block of '0', a block of '1', and a block of '2'. |
| 219 | { |
| 220 | base::File f(db_path(), kFlags); |
| 221 | ASSERT_TRUE(f.IsValid()); |
| 222 | memset(buf, '0', sizeof(buf)); |
| 223 | ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 224 | |
| 225 | memset(buf, '1', sizeof(buf)); |
| 226 | ASSERT_EQ(f.Write(1*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 227 | |
| 228 | memset(buf, '2', sizeof(buf)); |
| 229 | ASSERT_EQ(f.Write(2*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 230 | } |
| 231 | |
| 232 | // mmap the file and verify that everything looks right. |
| 233 | { |
| 234 | base::MemoryMappedFile m; |
| 235 | ASSERT_TRUE(m.Initialize(db_path())); |
| 236 | |
| 237 | memset(buf, '0', sizeof(buf)); |
| 238 | ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); |
| 239 | |
| 240 | memset(buf, '1', sizeof(buf)); |
| 241 | ASSERT_EQ(0, memcmp(buf, m.data() + 1*sizeof(buf), sizeof(buf))); |
| 242 | |
| 243 | memset(buf, '2', sizeof(buf)); |
| 244 | ASSERT_EQ(0, memcmp(buf, m.data() + 2*sizeof(buf), sizeof(buf))); |
| 245 | |
| 246 | // Scribble some '3' into the first page of the file, and verify that it |
| 247 | // looks the same in the memory mapping. |
| 248 | { |
| 249 | base::File f(db_path(), kFlags); |
| 250 | ASSERT_TRUE(f.IsValid()); |
| 251 | memset(buf, '3', sizeof(buf)); |
| 252 | ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 253 | } |
| 254 | ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); |
| 255 | |
| 256 | // Repeat with a single '4' in case page-sized blocks are different. |
| 257 | const size_t kOffset = 1*sizeof(buf) + 123; |
| 258 | ASSERT_NE('4', m.data()[kOffset]); |
| 259 | { |
| 260 | base::File f(db_path(), kFlags); |
| 261 | ASSERT_TRUE(f.IsValid()); |
| 262 | buf[0] = '4'; |
| 263 | ASSERT_EQ(f.Write(kOffset, buf, 1), 1); |
| 264 | } |
| 265 | ASSERT_EQ('4', m.data()[kOffset]); |
| 266 | } |
| 267 | } |
Victor Costan | e3d9c4b | 2018-11-24 09:07:40 | [diff] [blame] | 268 | #endif // !defined(OS_FUCHSIA) |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 269 | |
shess | 001ae16 | 2016-10-20 04:04:32 | [diff] [blame] | 270 | // Verify that http://crbug.com/248608 is fixed. In this bug, the |
| 271 | // compiled regular expression is effectively cached with the prepared |
| 272 | // statement, causing errors if the regular expression is rebound. |
| 273 | TEST_F(SQLiteFeaturesTest, CachedRegexp) { |
| 274 | ASSERT_TRUE(db().Execute("CREATE TABLE r (id INTEGER UNIQUE, x TEXT)")); |
| 275 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (1, 'this is a test')")); |
| 276 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (2, 'that was a test')")); |
| 277 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (3, 'this is a stickup')")); |
| 278 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (4, 'that sucks')")); |
| 279 | |
Victor Costan | 1d86835 | 2018-06-26 19:06:48 | [diff] [blame] | 280 | static const char kSimpleSql[] = "SELECT SUM(id) FROM r WHERE x REGEXP ?"; |
shess | 001ae16 | 2016-10-20 04:04:32 | [diff] [blame] | 281 | sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
| 282 | |
| 283 | s.BindString(0, "this.*"); |
| 284 | ASSERT_TRUE(s.Step()); |
| 285 | EXPECT_EQ(4, s.ColumnInt(0)); |
| 286 | |
| 287 | s.Reset(true); |
| 288 | s.BindString(0, "that.*"); |
| 289 | ASSERT_TRUE(s.Step()); |
| 290 | EXPECT_EQ(6, s.ColumnInt(0)); |
| 291 | |
| 292 | s.Reset(true); |
| 293 | s.BindString(0, ".*test"); |
| 294 | ASSERT_TRUE(s.Step()); |
| 295 | EXPECT_EQ(3, s.ColumnInt(0)); |
| 296 | |
| 297 | s.Reset(true); |
| 298 | s.BindString(0, ".* s[a-z]+"); |
| 299 | ASSERT_TRUE(s.Step()); |
| 300 | EXPECT_EQ(7, s.ColumnInt(0)); |
| 301 | } |
| 302 | |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 303 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 304 | // If a database file is marked to be excluded from Time Machine, verify that |
| 305 | // journal files are also excluded. |
Victor Costan | 653b14f | 2018-07-23 19:32:33 | [diff] [blame] | 306 | TEST_F(SQLiteFeaturesTest, TimeMachine) { |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 307 | ASSERT_TRUE(db().Execute("CREATE TABLE t (id INTEGER PRIMARY KEY)")); |
| 308 | db().Close(); |
| 309 | |
Victor Costan | 7e74dce | 2019-01-28 20:15:25 | [diff] [blame] | 310 | base::FilePath journal_path = sql::Database::JournalPath(db_path()); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 311 | ASSERT_TRUE(GetPathExists(db_path())); |
Victor Costan | 7e74dce | 2019-01-28 20:15:25 | [diff] [blame] | 312 | ASSERT_TRUE(GetPathExists(journal_path)); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 313 | |
| 314 | // Not excluded to start. |
Victor Costan | 7e74dce | 2019-01-28 20:15:25 | [diff] [blame] | 315 | EXPECT_FALSE(base::mac::GetFileBackupExclusion(db_path())); |
| 316 | EXPECT_FALSE(base::mac::GetFileBackupExclusion(journal_path)); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 317 | |
| 318 | // Exclude the main database file. |
| 319 | EXPECT_TRUE(base::mac::SetFileBackupExclusion(db_path())); |
| 320 | |
Victor Costan | 7e74dce | 2019-01-28 20:15:25 | [diff] [blame] | 321 | EXPECT_TRUE(base::mac::GetFileBackupExclusion(db_path())); |
| 322 | EXPECT_FALSE(base::mac::GetFileBackupExclusion(journal_path)); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 323 | |
| 324 | EXPECT_TRUE(db().Open(db_path())); |
| 325 | ASSERT_TRUE(db().Execute("INSERT INTO t VALUES (1)")); |
Victor Costan | 7e74dce | 2019-01-28 20:15:25 | [diff] [blame] | 326 | EXPECT_TRUE(base::mac::GetFileBackupExclusion(db_path())); |
| 327 | EXPECT_TRUE(base::mac::GetFileBackupExclusion(journal_path)); |
shess | 5f2c344 | 2017-01-24 02:15:10 | [diff] [blame] | 328 | |
| 329 | // TODO(shess): In WAL mode this will touch -wal and -shm files. -shm files |
| 330 | // could be always excluded. |
| 331 | } |
| 332 | #endif |
| 333 | |
Victor Costan | 86ca431 | 2018-02-07 21:34:10 | [diff] [blame] | 334 | #if !defined(OS_FUCHSIA) |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 335 | // SQLite WAL mode defaults to checkpointing the WAL on close. This would push |
| 336 | // additional work into Chromium shutdown. Verify that SQLite supports a config |
| 337 | // option to not checkpoint on close. |
| 338 | TEST_F(SQLiteFeaturesTest, WALNoClose) { |
Victor Costan | cfbfa60 | 2018-08-01 23:24:46 | [diff] [blame] | 339 | base::FilePath wal_path = sql::Database::WriteAheadLogPath(db_path()); |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 340 | |
| 341 | // Turn on WAL mode, then verify that the mode changed (WAL is supported). |
| 342 | ASSERT_TRUE(db().Execute("PRAGMA journal_mode = WAL")); |
| 343 | ASSERT_EQ("wal", ExecuteWithResult(&db(), "PRAGMA journal_mode")); |
| 344 | |
| 345 | // The WAL file is created lazily on first change. |
| 346 | ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 347 | |
| 348 | // By default, the WAL is checkpointed then deleted on close. |
| 349 | ASSERT_TRUE(GetPathExists(wal_path)); |
| 350 | db().Close(); |
| 351 | ASSERT_FALSE(GetPathExists(wal_path)); |
| 352 | |
| 353 | // Reopen and configure the database to not checkpoint WAL on close. |
| 354 | ASSERT_TRUE(Reopen()); |
| 355 | ASSERT_TRUE(db().Execute("PRAGMA journal_mode = WAL")); |
| 356 | ASSERT_TRUE(db().Execute("ALTER TABLE foo ADD COLUMN c")); |
Victor Costan | bd62311 | 2018-07-18 04:17:27 | [diff] [blame] | 357 | ASSERT_EQ(SQLITE_OK, |
| 358 | sqlite3_db_config(db().db_, SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, 1, |
| 359 | nullptr)); |
shess | f7fcc45 | 2017-04-19 22:10:41 | [diff] [blame] | 360 | ASSERT_TRUE(GetPathExists(wal_path)); |
| 361 | db().Close(); |
| 362 | ASSERT_TRUE(GetPathExists(wal_path)); |
| 363 | } |
| 364 | #endif |
| 365 | |
| 366 | } // namespace sql |