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 // Do not include fts2 support, it is not useful, and nobody is
70 TEST_F(SQLiteFeaturesTest
, NoFTS2
) {
71 ASSERT_EQ(SQLITE_ERROR
, db().ExecuteAndReturnErrorCode(
72 "CREATE VIRTUAL TABLE foo USING fts2(x)"));
75 // fts3 used to be used for history files, and may also be used by WebDatabase
77 TEST_F(SQLiteFeaturesTest
, FTS3
) {
78 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
81 #if !defined(USE_SYSTEM_SQLITE)
82 // Originally history used fts2, which Chromium patched to treat "foo*" as a
83 // prefix search, though the icu tokenizer would return it as two tokens {"foo",
84 // "*"}. Test that fts3 works correctly.
85 TEST_F(SQLiteFeaturesTest
, FTS3_Prefix
) {
86 const char kCreateSql
[] =
87 "CREATE VIRTUAL TABLE foo USING fts3(x, tokenize icu)";
88 ASSERT_TRUE(db().Execute(kCreateSql
));
90 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
92 sql::Statement
s(db().GetUniqueStatement(
93 "SELECT x FROM foo WHERE x MATCH 'te*'"));
94 ASSERT_TRUE(s
.Step());
95 EXPECT_EQ("test", s
.ColumnString(0));
99 #if !defined(USE_SYSTEM_SQLITE)
100 // Verify that Chromium's SQLite is compiled with HAVE_USLEEP defined. With
101 // HAVE_USLEEP, SQLite uses usleep() with millisecond granularity. Otherwise it
102 // uses sleep() with second granularity.
103 TEST_F(SQLiteFeaturesTest
, UsesUsleep
) {
104 base::TimeTicks before
= base::TimeTicks::Now();
106 base::TimeDelta delta
= base::TimeTicks::Now() - before
;
108 // It is not impossible for this to be over 1000 if things are compiled the
109 // right way. But it is very unlikely, most platforms seem to be around
111 LOG(ERROR
) << "Milliseconds: " << delta
.InMilliseconds();
112 EXPECT_LT(delta
.InMilliseconds(), 1000);
116 // Ensure that our SQLite version has working foreign key support with cascade
118 TEST_F(SQLiteFeaturesTest
, ForeignKeySupport
) {
119 ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1"));
120 ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)"));
121 ASSERT_TRUE(db().Execute(
122 "CREATE TABLE children ("
123 " id INTEGER PRIMARY KEY,"
124 " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)"));
126 // Inserting without a matching parent should fail with constraint violation.
127 // Mask off any extended error codes for USE_SYSTEM_SQLITE.
128 int insertErr
= db().ExecuteAndReturnErrorCode(
129 "INSERT INTO children VALUES (10, 1)");
130 EXPECT_EQ(SQLITE_CONSTRAINT
, (insertErr
&0xff));
133 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows
));
136 // Inserting with a matching parent should work.
137 ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)"));
138 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)"));
139 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)"));
140 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows
));
143 // Deleting the parent should cascade, i.e., delete the children as well.
144 ASSERT_TRUE(db().Execute("DELETE FROM parents"));
145 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows
));