[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 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 25 | // Test that certain features are/are-not enabled in our SQLite. |
| 26 | |
| 27 | namespace { |
| 28 | |
[email protected] | 526b466 | 2013-06-14 04:09:12 | [diff] [blame] | 29 | void CaptureErrorCallback(int* error_pointer, std::string* sql_text, |
| 30 | int error, sql::Statement* stmt) { |
| 31 | *error_pointer = error; |
| 32 | const char* text = stmt ? stmt->GetSQLStatement() : NULL; |
| 33 | *sql_text = text ? text : "no statement available"; |
| 34 | } |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 35 | |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 36 | class SQLiteFeaturesTest : public sql::SQLTestBase { |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 37 | public: |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 38 | SQLiteFeaturesTest() : error_(SQLITE_OK) {} |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 39 | |
dcheng | 1b3b125e | 2014-12-22 23:00:24 | [diff] [blame] | 40 | void SetUp() override { |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 41 | SQLTestBase::SetUp(); |
[email protected] | 9903468 | 2012-06-13 03:31:16 | [diff] [blame] | 42 | |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 43 | // The error delegate will set |error_| and |sql_text_| when any sqlite |
| 44 | // statement operation returns an error code. |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 45 | db().set_error_callback( |
| 46 | base::Bind(&CaptureErrorCallback, &error_, &sql_text_)); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 47 | } |
| 48 | |
dcheng | 1b3b125e | 2014-12-22 23:00:24 | [diff] [blame] | 49 | void TearDown() override { |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 50 | // If any error happened the original sql statement can be found in |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 51 | // |sql_text_|. |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 52 | EXPECT_EQ(SQLITE_OK, error_) << sql_text_; |
erg | 102ceb41 | 2015-06-20 01:38:13 | [diff] [blame] | 53 | |
| 54 | SQLTestBase::TearDown(); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 55 | } |
| 56 | |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 57 | int error() { return error_; } |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 58 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 59 | private: |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 60 | // The error code of the most recent error. |
| 61 | int error_; |
| 62 | // Original statement which has caused the error. |
| 63 | std::string sql_text_; |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // Do not include fts1 support, it is not useful, and nobody is |
| 67 | // looking at it. |
| 68 | TEST_F(SQLiteFeaturesTest, NoFTS1) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 69 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( |
| 70 | "CREATE VIRTUAL TABLE foo USING fts1(x)")); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 71 | } |
| 72 | |
shess | 37437cb | 2015-03-11 20:24:46 | [diff] [blame] | 73 | // Do not include fts2 support, it is not useful, and nobody is |
| 74 | // looking at it. |
| 75 | TEST_F(SQLiteFeaturesTest, NoFTS2) { |
| 76 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( |
| 77 | "CREATE VIRTUAL TABLE foo USING fts2(x)")); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 78 | } |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 79 | |
shess | 37437cb | 2015-03-11 20:24:46 | [diff] [blame] | 80 | // fts3 used to be used for history files, and may also be used by WebDatabase |
| 81 | // clients. |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 82 | TEST_F(SQLiteFeaturesTest, FTS3) { |
| 83 | ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)")); |
| 84 | } |
| 85 | |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 86 | #if !defined(USE_SYSTEM_SQLITE) |
shess | 37437cb | 2015-03-11 20:24:46 | [diff] [blame] | 87 | // Originally history used fts2, which Chromium patched to treat "foo*" as a |
| 88 | // prefix search, though the icu tokenizer would return it as two tokens {"foo", |
| 89 | // "*"}. Test that fts3 works correctly. |
shess | 355d9a1e | 2015-01-10 00:42:29 | [diff] [blame] | 90 | TEST_F(SQLiteFeaturesTest, FTS3_Prefix) { |
| 91 | const char kCreateSql[] = |
| 92 | "CREATE VIRTUAL TABLE foo USING fts3(x, tokenize icu)"; |
| 93 | ASSERT_TRUE(db().Execute(kCreateSql)); |
| 94 | |
| 95 | ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')")); |
| 96 | |
| 97 | sql::Statement s(db().GetUniqueStatement( |
| 98 | "SELECT x FROM foo WHERE x MATCH 'te*'")); |
| 99 | ASSERT_TRUE(s.Step()); |
| 100 | EXPECT_EQ("test", s.ColumnString(0)); |
| 101 | } |
| 102 | #endif |
| 103 | |
shess | 156733db | 2015-01-21 21:52:24 | [diff] [blame] | 104 | #if !defined(USE_SYSTEM_SQLITE) |
| 105 | // Verify that Chromium's SQLite is compiled with HAVE_USLEEP defined. With |
| 106 | // HAVE_USLEEP, SQLite uses usleep() with millisecond granularity. Otherwise it |
| 107 | // uses sleep() with second granularity. |
| 108 | TEST_F(SQLiteFeaturesTest, UsesUsleep) { |
| 109 | base::TimeTicks before = base::TimeTicks::Now(); |
| 110 | sqlite3_sleep(1); |
| 111 | base::TimeDelta delta = base::TimeTicks::Now() - before; |
| 112 | |
| 113 | // It is not impossible for this to be over 1000 if things are compiled the |
| 114 | // right way. But it is very unlikely, most platforms seem to be around |
| 115 | // <TBD>. |
| 116 | LOG(ERROR) << "Milliseconds: " << delta.InMilliseconds(); |
| 117 | EXPECT_LT(delta.InMilliseconds(), 1000); |
| 118 | } |
| 119 | #endif |
| 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)")); |
| 130 | |
| 131 | // Inserting without a matching parent should fail with constraint violation. |
Scott Hess | dcf12048 | 2015-02-10 21:33:29 | [diff] [blame] | 132 | // Mask off any extended error codes for USE_SYSTEM_SQLITE. |
| 133 | int insertErr = db().ExecuteAndReturnErrorCode( |
| 134 | "INSERT INTO children VALUES (10, 1)"); |
| 135 | EXPECT_EQ(SQLITE_CONSTRAINT, (insertErr&0xff)); |
engedy | be80d53e | 2015-01-22 09:54:50 | [diff] [blame] | 136 | |
| 137 | size_t rows; |
| 138 | EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); |
| 139 | EXPECT_EQ(0u, rows); |
| 140 | |
| 141 | // Inserting with a matching parent should work. |
| 142 | ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)")); |
| 143 | EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)")); |
| 144 | EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)")); |
| 145 | EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); |
| 146 | EXPECT_EQ(2u, rows); |
| 147 | |
| 148 | // Deleting the parent should cascade, i.e., delete the children as well. |
| 149 | ASSERT_TRUE(db().Execute("DELETE FROM parents")); |
| 150 | EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); |
| 151 | EXPECT_EQ(0u, rows); |
| 152 | } |
| 153 | |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 154 | #if defined(MOJO_APPTEST_IMPL) || defined(OS_IOS) |
| 155 | // If the platform cannot support SQLite mmap'ed I/O, make sure SQLite isn't |
| 156 | // offering to support it. |
| 157 | TEST_F(SQLiteFeaturesTest, NoMmap) { |
rohitrao | 83d6b83a | 2016-06-21 07:25:57 | [diff] [blame] | 158 | #if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE) |
| 159 | if (base::ios::IsRunningOnIOS10OrLater()) { |
| 160 | // iOS 10 added mmap support for sqlite. |
| 161 | return; |
| 162 | } |
| 163 | #endif |
| 164 | |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 165 | // For recent versions of SQLite, SQLITE_MAX_MMAP_SIZE=0 can be used to |
| 166 | // disable mmap support. Alternately, sqlite3_config() could be used. In |
| 167 | // that case, the pragma will run successfully, but the size will always be 0. |
| 168 | // |
| 169 | // The SQLite embedded in older iOS releases predates the addition of mmap |
| 170 | // support. In that case the pragma will run without error, but no results |
| 171 | // are returned when querying the value. |
| 172 | // |
| 173 | // MojoVFS implements a no-op for xFileControl(). PRAGMA mmap_size is |
| 174 | // implemented in terms of SQLITE_FCNTL_MMAP_SIZE. In that case, the pragma |
| 175 | // will succeed but with no effect. |
| 176 | ignore_result(db().Execute("PRAGMA mmap_size = 1048576")); |
| 177 | sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); |
| 178 | ASSERT_TRUE(!s.Step() || !s.ColumnInt64(0)); |
| 179 | } |
rohitrao | 83d6b83a | 2016-06-21 07:25:57 | [diff] [blame] | 180 | #endif |
| 181 | |
| 182 | #if !defined(MOJO_APPTEST_IMPL) |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 183 | // Verify that OS file writes are reflected in the memory mapping of a |
| 184 | // memory-mapped file. Normally SQLite writes to memory-mapped files using |
| 185 | // memcpy(), which should stay consistent. Our SQLite is slightly patched to |
| 186 | // mmap read only, then write using OS file writes. If the memory-mapped |
| 187 | // version doesn't reflect the OS file writes, SQLite's memory-mapped I/O should |
| 188 | // be disabled on this platform using SQLITE_MAX_MMAP_SIZE=0. |
| 189 | TEST_F(SQLiteFeaturesTest, Mmap) { |
rohitrao | 83d6b83a | 2016-06-21 07:25:57 | [diff] [blame] | 190 | #if defined(OS_IOS) && defined(USE_SYSTEM_SQLITE) |
| 191 | if (!base::ios::IsRunningOnIOS10OrLater()) { |
| 192 | // iOS9's sqlite does not support mmap, so this test must be skipped. |
| 193 | return; |
| 194 | } |
| 195 | #endif |
| 196 | |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 197 | // Try to turn on mmap'ed I/O. |
| 198 | ignore_result(db().Execute("PRAGMA mmap_size = 1048576")); |
| 199 | { |
| 200 | sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); |
| 201 | |
| 202 | #if !defined(USE_SYSTEM_SQLITE) |
| 203 | // With Chromium's version of SQLite, the setting should always be non-zero. |
| 204 | ASSERT_TRUE(s.Step()); |
| 205 | ASSERT_GT(s.ColumnInt64(0), 0); |
| 206 | #else |
| 207 | // With the system SQLite, don't verify underlying mmap functionality if the |
| 208 | // SQLite is too old to support mmap, or if mmap is disabled (see NoMmap |
| 209 | // test). USE_SYSTEM_SQLITE is not bundled into the NoMmap case because |
| 210 | // whether mmap is enabled or not is outside of Chromium's control. |
| 211 | if (!s.Step() || !s.ColumnInt64(0)) |
| 212 | return; |
| 213 | #endif |
| 214 | } |
| 215 | db().Close(); |
| 216 | |
avi | 0b51920 | 2015-12-21 07:25:19 | [diff] [blame] | 217 | const uint32_t kFlags = |
| 218 | base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE; |
shess | 53adf16 | 2015-12-17 22:07:26 | [diff] [blame] | 219 | char buf[4096]; |
| 220 | |
| 221 | // Create a file with a block of '0', a block of '1', and a block of '2'. |
| 222 | { |
| 223 | base::File f(db_path(), kFlags); |
| 224 | ASSERT_TRUE(f.IsValid()); |
| 225 | memset(buf, '0', sizeof(buf)); |
| 226 | ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 227 | |
| 228 | memset(buf, '1', sizeof(buf)); |
| 229 | ASSERT_EQ(f.Write(1*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 230 | |
| 231 | memset(buf, '2', sizeof(buf)); |
| 232 | ASSERT_EQ(f.Write(2*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 233 | } |
| 234 | |
| 235 | // mmap the file and verify that everything looks right. |
| 236 | { |
| 237 | base::MemoryMappedFile m; |
| 238 | ASSERT_TRUE(m.Initialize(db_path())); |
| 239 | |
| 240 | memset(buf, '0', sizeof(buf)); |
| 241 | ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); |
| 242 | |
| 243 | memset(buf, '1', sizeof(buf)); |
| 244 | ASSERT_EQ(0, memcmp(buf, m.data() + 1*sizeof(buf), sizeof(buf))); |
| 245 | |
| 246 | memset(buf, '2', sizeof(buf)); |
| 247 | ASSERT_EQ(0, memcmp(buf, m.data() + 2*sizeof(buf), sizeof(buf))); |
| 248 | |
| 249 | // Scribble some '3' into the first page of the file, and verify that it |
| 250 | // looks the same in the memory mapping. |
| 251 | { |
| 252 | base::File f(db_path(), kFlags); |
| 253 | ASSERT_TRUE(f.IsValid()); |
| 254 | memset(buf, '3', sizeof(buf)); |
| 255 | ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); |
| 256 | } |
| 257 | ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); |
| 258 | |
| 259 | // Repeat with a single '4' in case page-sized blocks are different. |
| 260 | const size_t kOffset = 1*sizeof(buf) + 123; |
| 261 | ASSERT_NE('4', m.data()[kOffset]); |
| 262 | { |
| 263 | base::File f(db_path(), kFlags); |
| 264 | ASSERT_TRUE(f.IsValid()); |
| 265 | buf[0] = '4'; |
| 266 | ASSERT_EQ(f.Write(kOffset, buf, 1), 1); |
| 267 | } |
| 268 | ASSERT_EQ('4', m.data()[kOffset]); |
| 269 | } |
| 270 | } |
| 271 | #endif |
| 272 | |
shess | 001ae16 | 2016-10-20 04:04:32 | [diff] [blame] | 273 | // Verify that http://crbug.com/248608 is fixed. In this bug, the |
| 274 | // compiled regular expression is effectively cached with the prepared |
| 275 | // statement, causing errors if the regular expression is rebound. |
| 276 | TEST_F(SQLiteFeaturesTest, CachedRegexp) { |
| 277 | ASSERT_TRUE(db().Execute("CREATE TABLE r (id INTEGER UNIQUE, x TEXT)")); |
| 278 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (1, 'this is a test')")); |
| 279 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (2, 'that was a test')")); |
| 280 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (3, 'this is a stickup')")); |
| 281 | ASSERT_TRUE(db().Execute("INSERT INTO r VALUES (4, 'that sucks')")); |
| 282 | |
| 283 | const char* kSimpleSql = "SELECT SUM(id) FROM r WHERE x REGEXP ?"; |
| 284 | sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
| 285 | |
| 286 | s.BindString(0, "this.*"); |
| 287 | ASSERT_TRUE(s.Step()); |
| 288 | EXPECT_EQ(4, s.ColumnInt(0)); |
| 289 | |
| 290 | s.Reset(true); |
| 291 | s.BindString(0, "that.*"); |
| 292 | ASSERT_TRUE(s.Step()); |
| 293 | EXPECT_EQ(6, s.ColumnInt(0)); |
| 294 | |
| 295 | s.Reset(true); |
| 296 | s.BindString(0, ".*test"); |
| 297 | ASSERT_TRUE(s.Step()); |
| 298 | EXPECT_EQ(3, s.ColumnInt(0)); |
| 299 | |
| 300 | s.Reset(true); |
| 301 | s.BindString(0, ".* s[a-z]+"); |
| 302 | ASSERT_TRUE(s.Step()); |
| 303 | EXPECT_EQ(7, s.ColumnInt(0)); |
| 304 | } |
| 305 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 306 | } // namespace |