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