1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "sql/connection.h"
11 #include "sql/statement.h"
12 #include "sql/test/test_helpers.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/sqlite/sqlite3.h"
16 // Test that certain features are/are-not enabled in our SQLite.
20 void CaptureErrorCallback(int* error_pointer
, std::string
* sql_text
,
21 int error
, sql::Statement
* stmt
) {
22 *error_pointer
= error
;
23 const char* text
= stmt
? stmt
->GetSQLStatement() : NULL
;
24 *sql_text
= text
? text
: "no statement available";
27 class SQLiteFeaturesTest
: public testing::Test
{
29 SQLiteFeaturesTest() : error_(SQLITE_OK
) {}
31 void SetUp() override
{
32 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
33 ASSERT_TRUE(db_
.Open(temp_dir_
.path().AppendASCII("SQLStatementTest.db")));
35 // The error delegate will set |error_| and |sql_text_| when any sqlite
36 // statement operation returns an error code.
37 db_
.set_error_callback(base::Bind(&CaptureErrorCallback
,
38 &error_
, &sql_text_
));
41 void TearDown() override
{
42 // If any error happened the original sql statement can be found in
44 EXPECT_EQ(SQLITE_OK
, error_
) << sql_text_
;
48 sql::Connection
& db() { return db_
; }
49 int error() { return error_
; }
52 base::ScopedTempDir temp_dir_
;
55 // The error code of the most recent error.
57 // Original statement which has caused the error.
58 std::string sql_text_
;
61 // Do not include fts1 support, it is not useful, and nobody is
63 TEST_F(SQLiteFeaturesTest
, NoFTS1
) {
64 ASSERT_EQ(SQLITE_ERROR
, db().ExecuteAndReturnErrorCode(
65 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
68 #if defined(SQLITE_ENABLE_FTS2)
69 // fts2 is used for older history files, so we're signed on for keeping our
70 // version up-to-date.
71 // TODO(shess): Think up a crazy way to get out from having to support
73 TEST_F(SQLiteFeaturesTest
, FTS2
) {
74 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)"));
77 // A standard SQLite will not include our patch. This includes iOS.
78 #if !defined(USE_SYSTEM_SQLITE)
79 // Chromium fts2 was patched to treat "foo*" as a prefix search, though the icu
80 // tokenizer will return it as two tokens {"foo", "*"}.
81 TEST_F(SQLiteFeaturesTest
, FTS2_Prefix
) {
82 const char kCreateSql
[] =
83 "CREATE VIRTUAL TABLE foo USING fts2(x, tokenize icu)";
84 ASSERT_TRUE(db().Execute(kCreateSql
));
86 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
88 sql::Statement
s(db().GetUniqueStatement(
89 "SELECT x FROM foo WHERE x MATCH 'te*'"));
90 ASSERT_TRUE(s
.Step());
91 EXPECT_EQ("test", s
.ColumnString(0));
96 // fts3 is used for current history files, and also for WebDatabase.
97 TEST_F(SQLiteFeaturesTest
, FTS3
) {
98 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
101 #if !defined(USE_SYSTEM_SQLITE)
102 // Test that fts3 doesn't need fts2's patch (see above).
103 TEST_F(SQLiteFeaturesTest
, FTS3_Prefix
) {
104 const char kCreateSql
[] =
105 "CREATE VIRTUAL TABLE foo USING fts3(x, tokenize icu)";
106 ASSERT_TRUE(db().Execute(kCreateSql
));
108 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
110 sql::Statement
s(db().GetUniqueStatement(
111 "SELECT x FROM foo WHERE x MATCH 'te*'"));
112 ASSERT_TRUE(s
.Step());
113 EXPECT_EQ("test", s
.ColumnString(0));
117 #if !defined(USE_SYSTEM_SQLITE)
118 // Verify that Chromium's SQLite is compiled with HAVE_USLEEP defined. With
119 // HAVE_USLEEP, SQLite uses usleep() with millisecond granularity. Otherwise it
120 // uses sleep() with second granularity.
121 TEST_F(SQLiteFeaturesTest
, UsesUsleep
) {
122 base::TimeTicks before
= base::TimeTicks::Now();
124 base::TimeDelta delta
= base::TimeTicks::Now() - before
;
126 // It is not impossible for this to be over 1000 if things are compiled the
127 // right way. But it is very unlikely, most platforms seem to be around
129 LOG(ERROR
) << "Milliseconds: " << delta
.InMilliseconds();
130 EXPECT_LT(delta
.InMilliseconds(), 1000);
134 // Ensure that our SQLite version has working foreign key support with cascade
136 TEST_F(SQLiteFeaturesTest
, ForeignKeySupport
) {
137 ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1"));
138 ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)"));
139 ASSERT_TRUE(db().Execute(
140 "CREATE TABLE children ("
141 " id INTEGER PRIMARY KEY,"
142 " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)"));
144 // Inserting without a matching parent should fail with constraint violation.
145 // Mask off any extended error codes for USE_SYSTEM_SQLITE.
146 int insertErr
= db().ExecuteAndReturnErrorCode(
147 "INSERT INTO children VALUES (10, 1)");
148 EXPECT_EQ(SQLITE_CONSTRAINT
, (insertErr
&0xff));
151 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows
));
154 // Inserting with a matching parent should work.
155 ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)"));
156 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)"));
157 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)"));
158 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows
));
161 // Deleting the parent should cascade, i.e., delete the children as well.
162 ASSERT_TRUE(db().Execute("DELETE FROM parents"));
163 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows
));