blob: 199d9446c73b14ba452297f1c9b317ddcd1565c1 [file] [log] [blame]
[email protected]60e60dd2012-04-28 08:16:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]67361b32011-04-12 20:13:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avi0b519202015-12-21 07:25:195#include <stddef.h>
6#include <stdint.h>
7
[email protected]67361b32011-04-12 20:13:068#include <string>
9
[email protected]526b4662013-06-14 04:09:1210#include "base/bind.h"
thestig22dfc4012014-09-05 08:29:4411#include "base/files/file_util.h"
shess53adf162015-12-17 22:07:2612#include "base/files/memory_mapped_file.h"
[email protected]ea1a3f62012-11-16 20:34:2313#include "base/files/scoped_temp_dir.h"
[email protected]f0a54b22011-07-19 18:40:2114#include "sql/connection.h"
15#include "sql/statement.h"
erg102ceb412015-06-20 01:38:1316#include "sql/test/sql_test_base.h"
engedybe80d53e2015-01-22 09:54:5017#include "sql/test/test_helpers.h"
[email protected]67361b32011-04-12 20:13:0618#include "testing/gtest/include/gtest/gtest.h"
19#include "third_party/sqlite/sqlite3.h"
20
rohitrao83d6b83a2016-06-21 07:25:5721#if defined(OS_IOS)
22#include "base/ios/ios_util.h"
23#endif
24
[email protected]67361b32011-04-12 20:13:0625// Test that certain features are/are-not enabled in our SQLite.
26
27namespace {
28
[email protected]526b4662013-06-14 04:09:1229void 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]67361b32011-04-12 20:13:0635
erg102ceb412015-06-20 01:38:1336class SQLiteFeaturesTest : public sql::SQLTestBase {
[email protected]67361b32011-04-12 20:13:0637 public:
[email protected]49dc4f22012-10-17 17:41:1638 SQLiteFeaturesTest() : error_(SQLITE_OK) {}
[email protected]67361b32011-04-12 20:13:0639
dcheng1b3b125e2014-12-22 23:00:2440 void SetUp() override {
erg102ceb412015-06-20 01:38:1341 SQLTestBase::SetUp();
[email protected]99034682012-06-13 03:31:1642
[email protected]49dc4f22012-10-17 17:41:1643 // The error delegate will set |error_| and |sql_text_| when any sqlite
44 // statement operation returns an error code.
erg102ceb412015-06-20 01:38:1345 db().set_error_callback(
46 base::Bind(&CaptureErrorCallback, &error_, &sql_text_));
[email protected]67361b32011-04-12 20:13:0647 }
48
dcheng1b3b125e2014-12-22 23:00:2449 void TearDown() override {
[email protected]67361b32011-04-12 20:13:0650 // If any error happened the original sql statement can be found in
[email protected]49dc4f22012-10-17 17:41:1651 // |sql_text_|.
engedybe80d53e2015-01-22 09:54:5052 EXPECT_EQ(SQLITE_OK, error_) << sql_text_;
erg102ceb412015-06-20 01:38:1353
54 SQLTestBase::TearDown();
[email protected]67361b32011-04-12 20:13:0655 }
56
Scott Hessdcf120482015-02-10 21:33:2957 int error() { return error_; }
[email protected]67361b32011-04-12 20:13:0658
[email protected]67361b32011-04-12 20:13:0659 private:
[email protected]49dc4f22012-10-17 17:41:1660 // 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]67361b32011-04-12 20:13:0664};
65
66// Do not include fts1 support, it is not useful, and nobody is
67// looking at it.
68TEST_F(SQLiteFeaturesTest, NoFTS1) {
[email protected]eff1fa522011-12-12 23:50:5969 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
70 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
[email protected]67361b32011-04-12 20:13:0671}
72
shess37437cb2015-03-11 20:24:4673// Do not include fts2 support, it is not useful, and nobody is
74// looking at it.
75TEST_F(SQLiteFeaturesTest, NoFTS2) {
76 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
77 "CREATE VIRTUAL TABLE foo USING fts2(x)"));
[email protected]67361b32011-04-12 20:13:0678}
shess355d9a1e2015-01-10 00:42:2979
shess37437cb2015-03-11 20:24:4680// fts3 used to be used for history files, and may also be used by WebDatabase
81// clients.
[email protected]67361b32011-04-12 20:13:0682TEST_F(SQLiteFeaturesTest, FTS3) {
83 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
84}
85
shess355d9a1e2015-01-10 00:42:2986#if !defined(USE_SYSTEM_SQLITE)
shess37437cb2015-03-11 20:24:4687// 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.
shess355d9a1e2015-01-10 00:42:2990TEST_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
shess156733db2015-01-21 21:52:24104#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.
108TEST_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
engedybe80d53e2015-01-22 09:54:50121// Ensure that our SQLite version has working foreign key support with cascade
122// delete support.
123TEST_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 Hessdcf120482015-02-10 21:33:29132 // 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));
engedybe80d53e2015-01-22 09:54:50136
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
shess53adf162015-12-17 22:07:26154#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.
157TEST_F(SQLiteFeaturesTest, NoMmap) {
rohitrao83d6b83a2016-06-21 07:25:57158#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
shess53adf162015-12-17 22:07:26165 // 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}
rohitrao83d6b83a2016-06-21 07:25:57180#endif
181
182#if !defined(MOJO_APPTEST_IMPL)
shess53adf162015-12-17 22:07:26183// 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.
189TEST_F(SQLiteFeaturesTest, Mmap) {
rohitrao83d6b83a2016-06-21 07:25:57190#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
shess53adf162015-12-17 22:07:26197 // 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
avi0b519202015-12-21 07:25:19217 const uint32_t kFlags =
218 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE;
shess53adf162015-12-17 22:07:26219 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
shess001ae162016-10-20 04:04:32273// 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.
276TEST_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]67361b32011-04-12 20:13:06306} // namespace