This source file includes following definitions.
- SqliteMasterCount
- IgnoreErrorCallback
- ErrorCallbackSetHelper
- ErrorCallbackResetHelper
- SetUp
- TearDown
- db
- db_path
- RazeErrorCallback
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/scoped_file.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "sql/connection.h"
#include "sql/meta_table.h"
#include "sql/statement.h"
#include "sql/test/error_callback_support.h"
#include "sql/test/scoped_error_ignorer.h"
#include "sql/test/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/sqlite/sqlite3.h"
namespace {
int SqliteMasterCount(sql::Connection* db) {
const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master";
sql::Statement s(db->GetUniqueStatement(kMasterCount));
return s.Step() ? s.ColumnInt(0) : -1;
}
class RefCounter {
public:
RefCounter(size_t* counter)
: counter_(counter) {
(*counter_)++;
}
RefCounter(const RefCounter& other)
: counter_(other.counter_) {
(*counter_)++;
}
~RefCounter() {
(*counter_)--;
}
private:
size_t* counter_;
DISALLOW_ASSIGN(RefCounter);
};
void IgnoreErrorCallback(int error, sql::Statement* stmt) {
}
void ErrorCallbackSetHelper(sql::Connection* db,
size_t* counter,
const RefCounter& r,
int error, sql::Statement* stmt) {
EXPECT_GT(*counter, 0u);
db->set_error_callback(base::Bind(&IgnoreErrorCallback));
EXPECT_GT(*counter, 0u);
}
void ErrorCallbackResetHelper(sql::Connection* db,
size_t* counter,
const RefCounter& r,
int error, sql::Statement* stmt) {
EXPECT_GT(*counter, 0u);
db->reset_error_callback();
EXPECT_GT(*counter, 0u);
}
#if defined(OS_POSIX)
class ScopedUmaskSetter {
public:
explicit ScopedUmaskSetter(mode_t target_mask) {
old_umask_ = umask(target_mask);
}
~ScopedUmaskSetter() { umask(old_umask_); }
private:
mode_t old_umask_;
DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter);
};
#endif
class SQLConnectionTest : public testing::Test {
public:
virtual void SetUp() {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
db_path_ = temp_dir_.path().AppendASCII("SQLConnectionTest.db");
ASSERT_TRUE(db_.Open(db_path_));
}
virtual void TearDown() {
db_.Close();
}
sql::Connection& db() { return db_; }
const base::FilePath& db_path() { return db_path_; }
void RazeErrorCallback(int expected_error, int error, sql::Statement* stmt) {
EXPECT_EQ(expected_error, error);
db_.RazeAndClose();
}
private:
sql::Connection db_;
base::FilePath db_path_;
base::ScopedTempDir temp_dir_;
};
TEST_F(SQLConnectionTest, Execute) {
ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
EXPECT_EQ(SQLITE_OK, db().GetErrorCode());
ASSERT_EQ(SQLITE_ERROR,
db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b"));
EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode());
}
TEST_F(SQLConnectionTest, ExecuteWithErrorCode) {
ASSERT_EQ(SQLITE_OK,
db().ExecuteAndReturnErrorCode("CREATE TABLE foo (a, b)"));
ASSERT_EQ(SQLITE_ERROR,
db().ExecuteAndReturnErrorCode("CREATE TABLE TABLE"));
ASSERT_EQ(SQLITE_ERROR,
db().ExecuteAndReturnErrorCode(
"INSERT INTO foo(a, b) VALUES (1, 2, 3, 4)"));
}
TEST_F(SQLConnectionTest, CachedStatement) {
sql::StatementID id1("foo", 12);
ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)"));
{
sql::Statement s(db().GetCachedStatement(id1, "SELECT a FROM foo"));
ASSERT_TRUE(s.is_valid());
ASSERT_TRUE(s.Step());
EXPECT_EQ(12, s.ColumnInt(0));
}
EXPECT_TRUE(db().HasCachedStatement(id1));
{
sql::Statement s(db().GetCachedStatement(id1, "something invalid("));
ASSERT_TRUE(s.is_valid());
ASSERT_TRUE(s.Step());
EXPECT_EQ(12, s.ColumnInt(0));
}
EXPECT_FALSE(db().HasCachedStatement(SQL_FROM_HERE));
}
TEST_F(SQLConnectionTest, IsSQLValidTest) {
ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
ASSERT_TRUE(db().IsSQLValid("SELECT a FROM foo"));
ASSERT_FALSE(db().IsSQLValid("SELECT no_exist FROM foo"));
}
TEST_F(SQLConnectionTest, DoesStuffExist) {
EXPECT_FALSE(db().DoesTableExist("foo"));
ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
EXPECT_TRUE(db().DoesTableExist("foo"));
EXPECT_FALSE(db().DoesTableExist("FOO"));
EXPECT_FALSE(db().DoesColumnExist("foo", "bar"));
EXPECT_TRUE(db().DoesColumnExist("foo", "a"));
EXPECT_FALSE(db().DoesColumnExist("bar", "b"));
}
TEST_F(SQLConnectionTest, GetLastInsertRowId) {
ASSERT_TRUE(db().Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"));
ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)"));
int64 row = db().GetLastInsertRowId();
EXPECT_LT(0, row);
sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo WHERE id=?"));
s.BindInt64(0, row);
ASSERT_TRUE(s.Step());
EXPECT_EQ(12, s.ColumnInt(0));
}
TEST_F(SQLConnectionTest, Rollback) {
ASSERT_TRUE(db().BeginTransaction());
ASSERT_TRUE(db().BeginTransaction());
EXPECT_EQ(2, db().transaction_nesting());
db().RollbackTransaction();
EXPECT_FALSE(db().CommitTransaction());
EXPECT_TRUE(db().BeginTransaction());
}
TEST_F(SQLConnectionTest, ScopedIgnoreError) {
const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)";
ASSERT_TRUE(db().Execute(kCreateSql));
ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)"));
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_CONSTRAINT);
ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)"));
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
TEST_F(SQLConnectionTest, ErrorCallback) {
const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)";
ASSERT_TRUE(db().Execute(kCreateSql));
ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)"));
int error = SQLITE_OK;
{
sql::ScopedErrorCallback sec(
&db(), base::Bind(&sql::CaptureErrorCallback, &error));
EXPECT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)"));
EXPECT_EQ(SQLITE_CONSTRAINT, error);
}
{
error = SQLITE_OK;
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_CONSTRAINT);
ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)"));
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
EXPECT_EQ(SQLITE_OK, error);
}
{
size_t count = 0;
sql::ScopedErrorCallback sec(
&db(), base::Bind(&ErrorCallbackSetHelper,
&db(), &count, RefCounter(&count)));
EXPECT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)"));
}
{
size_t count = 0;
sql::ScopedErrorCallback sec(
&db(), base::Bind(&ErrorCallbackResetHelper,
&db(), &count, RefCounter(&count)));
EXPECT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)"));
}
}
TEST_F(SQLConnectionTest, Raze) {
const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
ASSERT_TRUE(db().Execute(kCreateSql));
ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)"));
int pragma_auto_vacuum = 0;
{
sql::Statement s(db().GetUniqueStatement("PRAGMA auto_vacuum"));
ASSERT_TRUE(s.Step());
pragma_auto_vacuum = s.ColumnInt(0);
ASSERT_TRUE(pragma_auto_vacuum == 0 || pragma_auto_vacuum == 1);
}
const int kExpectedPageCount = 2 + pragma_auto_vacuum;
{
sql::Statement s(db().GetUniqueStatement("PRAGMA page_count"));
ASSERT_TRUE(s.Step());
EXPECT_EQ(kExpectedPageCount, s.ColumnInt(0));
}
{
sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master"));
ASSERT_TRUE(s.Step());
EXPECT_EQ("table", s.ColumnString(0));
EXPECT_EQ("foo", s.ColumnString(1));
EXPECT_EQ("foo", s.ColumnString(2));
EXPECT_EQ(kExpectedPageCount, s.ColumnInt(3));
EXPECT_EQ(kCreateSql, s.ColumnString(4));
}
ASSERT_TRUE(db().Raze());
{
sql::Statement s(db().GetUniqueStatement("PRAGMA page_count"));
ASSERT_TRUE(s.Step());
EXPECT_EQ(1, s.ColumnInt(0));
}
ASSERT_EQ(0, SqliteMasterCount(&db()));
{
sql::Statement s(db().GetUniqueStatement("PRAGMA auto_vacuum"));
ASSERT_TRUE(s.Step());
EXPECT_EQ(pragma_auto_vacuum, s.ColumnInt(0));
}
}
TEST_F(SQLConnectionTest, RazePageSize) {
int default_page_size = 0;
{
sql::Statement s(db().GetUniqueStatement("PRAGMA page_size"));
ASSERT_TRUE(s.Step());
default_page_size = s.ColumnInt(0);
}
ASSERT_GT(default_page_size, 0);
const int kPageSize = 2 * default_page_size;
db().Close();
db().set_page_size(kPageSize);
ASSERT_TRUE(db().Open(db_path()));
sql::Statement s(db().GetUniqueStatement("PRAGMA page_size"));
ASSERT_TRUE(s.Step());
ASSERT_EQ(kPageSize, s.ColumnInt(0));
ASSERT_TRUE(db().Raze());
s.Reset(true);
ASSERT_TRUE(s.Step());
ASSERT_EQ(kPageSize, s.ColumnInt(0));
}
TEST_F(SQLConnectionTest, RazeMultiple) {
const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
ASSERT_TRUE(db().Execute(kCreateSql));
sql::Connection other_db;
ASSERT_TRUE(other_db.Open(db_path()));
ASSERT_EQ(1, SqliteMasterCount(&other_db));
ASSERT_TRUE(db().Raze());
ASSERT_EQ(0, SqliteMasterCount(&other_db));
}
TEST_F(SQLConnectionTest, RazeLocked) {
const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
ASSERT_TRUE(db().Execute(kCreateSql));
sql::Connection other_db;
ASSERT_TRUE(other_db.Open(db_path()));
ASSERT_TRUE(other_db.BeginTransaction());
const char* kInsertSql = "INSERT INTO foo VALUES (1, 'data')";
ASSERT_TRUE(other_db.Execute(kInsertSql));
ASSERT_FALSE(db().Raze());
ASSERT_TRUE(other_db.CommitTransaction());
ASSERT_TRUE(db().Raze());
ASSERT_TRUE(db().Execute(kCreateSql));
ASSERT_TRUE(db().Execute(kInsertSql));
const char *kQuery = "SELECT COUNT(*) FROM foo";
sql::Statement s(other_db.GetUniqueStatement(kQuery));
ASSERT_TRUE(s.Step());
ASSERT_FALSE(db().Raze());
ASSERT_FALSE(s.Step());
ASSERT_TRUE(db().Raze());
}
TEST_F(SQLConnectionTest, RazeEmptyDB) {
const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
ASSERT_TRUE(db().Execute(kCreateSql));
db().Close();
{
base::ScopedFILE file(base::OpenFile(db_path(), "rb+"));
ASSERT_TRUE(file.get() != NULL);
ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET));
ASSERT_TRUE(base::TruncateFile(file.get()));
}
ASSERT_TRUE(db().Open(db_path()));
ASSERT_TRUE(db().Raze());
EXPECT_EQ(0, SqliteMasterCount(&db()));
}
TEST_F(SQLConnectionTest, RazeNOTADB) {
db().Close();
sql::Connection::Delete(db_path());
ASSERT_FALSE(base::PathExists(db_path()));
{
base::ScopedFILE file(base::OpenFile(db_path(), "wb"));
ASSERT_TRUE(file.get() != NULL);
const char* kJunk = "This is the hour of our discontent.";
fputs(kJunk, file.get());
}
ASSERT_TRUE(base::PathExists(db_path()));
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_IOERR_SHORT_READ);
EXPECT_TRUE(db().Open(db_path()));
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
EXPECT_TRUE(db().Raze());
db().Close();
EXPECT_TRUE(db().Open(db_path()));
EXPECT_EQ(0, SqliteMasterCount(&db()));
}
TEST_F(SQLConnectionTest, RazeNOTADB2) {
const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
ASSERT_TRUE(db().Execute(kCreateSql));
ASSERT_EQ(1, SqliteMasterCount(&db()));
db().Close();
{
base::ScopedFILE file(base::OpenFile(db_path(), "rb+"));
ASSERT_TRUE(file.get() != NULL);
ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET));
const char* kJunk = "This is the hour of our discontent.";
fputs(kJunk, file.get());
}
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_NOTADB);
EXPECT_TRUE(db().Open(db_path()));
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
EXPECT_TRUE(db().Raze());
db().Close();
EXPECT_TRUE(db().Open(db_path()));
EXPECT_EQ(0, SqliteMasterCount(&db()));
}
TEST_F(SQLConnectionTest, RazeCallbackReopen) {
const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
ASSERT_TRUE(db().Execute(kCreateSql));
ASSERT_EQ(1, SqliteMasterCount(&db()));
db().Close();
ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path()));
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_CORRUPT);
ASSERT_TRUE(db().Open(db_path()));
ASSERT_FALSE(db().Execute("PRAGMA auto_vacuum"));
db().Close();
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
db().set_error_callback(base::Bind(&SQLConnectionTest::RazeErrorCallback,
base::Unretained(this),
SQLITE_CORRUPT));
ASSERT_TRUE(db().Open(db_path()));
ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum"));
EXPECT_EQ(0, SqliteMasterCount(&db()));
}
TEST_F(SQLConnectionTest, RazeAndClose) {
const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)";
ASSERT_TRUE(db().Execute(kCreateSql));
ASSERT_TRUE(db().Execute(kPopulateSql));
ASSERT_TRUE(db().RazeAndClose());
ASSERT_FALSE(db().is_open());
db().Close();
ASSERT_TRUE(db().Open(db_path()));
ASSERT_EQ(0, SqliteMasterCount(&db()));
ASSERT_TRUE(db().Execute(kCreateSql));
ASSERT_TRUE(db().Execute(kPopulateSql));
ASSERT_TRUE(db().BeginTransaction());
ASSERT_TRUE(db().RazeAndClose());
ASSERT_FALSE(db().is_open());
ASSERT_FALSE(db().CommitTransaction());
db().Close();
ASSERT_TRUE(db().Open(db_path()));
ASSERT_EQ(0, SqliteMasterCount(&db()));
}
TEST_F(SQLConnectionTest, RazeAndCloseDiagnostics) {
const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)";
const char* kSimpleSql = "SELECT 1";
ASSERT_TRUE(db().Execute(kCreateSql));
ASSERT_TRUE(db().Execute(kPopulateSql));
db().Preload();
ASSERT_TRUE(db().DoesTableExist("foo"));
ASSERT_TRUE(db().IsSQLValid(kSimpleSql));
ASSERT_EQ(SQLITE_OK, db().ExecuteAndReturnErrorCode(kSimpleSql));
ASSERT_TRUE(db().Execute(kSimpleSql));
ASSERT_TRUE(db().is_open());
{
sql::Statement s(db().GetUniqueStatement(kSimpleSql));
ASSERT_TRUE(s.Step());
}
{
sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql));
ASSERT_TRUE(s.Step());
}
ASSERT_TRUE(db().BeginTransaction());
ASSERT_TRUE(db().CommitTransaction());
ASSERT_TRUE(db().BeginTransaction());
db().RollbackTransaction();
ASSERT_TRUE(db().RazeAndClose());
db().Preload();
ASSERT_FALSE(db().DoesTableExist("foo"));
ASSERT_FALSE(db().IsSQLValid(kSimpleSql));
ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(kSimpleSql));
ASSERT_FALSE(db().Execute(kSimpleSql));
ASSERT_FALSE(db().is_open());
{
sql::Statement s(db().GetUniqueStatement(kSimpleSql));
ASSERT_FALSE(s.Step());
}
{
sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql));
ASSERT_FALSE(s.Step());
}
ASSERT_FALSE(db().BeginTransaction());
ASSERT_FALSE(db().CommitTransaction());
ASSERT_FALSE(db().BeginTransaction());
db().RollbackTransaction();
db().Close();
#if !defined(OS_ANDROID) && !defined(OS_IOS)
if (DLOG_IS_ON(FATAL)) {
ASSERT_DEATH({
db().IsSQLValid(kSimpleSql);
}, "Illegal use of connection without a db");
}
#endif
}
#if defined(OS_ANDROID)
TEST_F(SQLConnectionTest, SetTempDirForSQL) {
sql::MetaTable meta_table;
ASSERT_TRUE(meta_table.Init(&db(), 4, 4));
}
#endif
TEST_F(SQLConnectionTest, Delete) {
EXPECT_TRUE(db().Execute("CREATE TABLE x (x)"));
db().Close();
base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal"));
ASSERT_TRUE(base::PathExists(db_path()));
ASSERT_TRUE(base::PathExists(journal));
sql::Connection::Delete(db_path());
EXPECT_FALSE(base::PathExists(db_path()));
EXPECT_FALSE(base::PathExists(journal));
}
#if defined(OS_POSIX)
TEST_F(SQLConnectionTest, UserPermission) {
db().Close();
sql::Connection::Delete(db_path());
ASSERT_FALSE(base::PathExists(db_path()));
ScopedUmaskSetter permissive_umask(S_IWGRP | S_IWOTH);
ASSERT_TRUE(db().Open(db_path()));
EXPECT_TRUE(db().Execute("CREATE TABLE x (x)"));
base::FilePath journal(db_path().value() + FILE_PATH_LITERAL("-journal"));
int mode;
ASSERT_TRUE(base::PathExists(db_path()));
ASSERT_TRUE(base::PathExists(journal));
mode = base::FILE_PERMISSION_MASK;
EXPECT_TRUE(base::GetPosixFilePermissions(db_path(), &mode));
ASSERT_NE((mode & base::FILE_PERMISSION_USER_MASK), mode);
mode = base::FILE_PERMISSION_MASK;
EXPECT_TRUE(base::GetPosixFilePermissions(journal, &mode));
ASSERT_NE((mode & base::FILE_PERMISSION_USER_MASK), mode);
db().Close();
db().set_restrict_to_user();
ASSERT_TRUE(db().Open(db_path()));
ASSERT_TRUE(base::PathExists(db_path()));
ASSERT_TRUE(base::PathExists(journal));
mode = base::FILE_PERMISSION_MASK;
EXPECT_TRUE(base::GetPosixFilePermissions(db_path(), &mode));
ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode);
mode = base::FILE_PERMISSION_MASK;
EXPECT_TRUE(base::GetPosixFilePermissions(journal, &mode));
ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode);
db().Close();
sql::Connection::Delete(db_path());
ASSERT_TRUE(db().Open(db_path()));
ASSERT_TRUE(base::PathExists(db_path()));
ASSERT_FALSE(base::PathExists(journal));
mode = base::FILE_PERMISSION_MASK;
EXPECT_TRUE(base::GetPosixFilePermissions(db_path(), &mode));
ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode);
EXPECT_TRUE(db().Execute("CREATE TABLE x (x)"));
ASSERT_TRUE(base::PathExists(journal));
mode = base::FILE_PERMISSION_MASK;
EXPECT_TRUE(base::GetPosixFilePermissions(journal, &mode));
ASSERT_EQ((mode & base::FILE_PERMISSION_USER_MASK), mode);
}
#endif
TEST_F(SQLConnectionTest, Poison) {
EXPECT_TRUE(db().Execute("CREATE TABLE x (x)"));
EXPECT_TRUE(db().IsSQLValid("INSERT INTO x VALUES ('x')"));
EXPECT_TRUE(db().Execute("INSERT INTO x VALUES ('x')"));
{
sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM x"));
ASSERT_TRUE(s.is_valid());
ASSERT_TRUE(s.Step());
}
sql::Statement valid_statement(
db().GetUniqueStatement("SELECT COUNT(*) FROM sqlite_master"));
ASSERT_TRUE(valid_statement.is_valid());
ASSERT_TRUE(valid_statement.Step());
valid_statement.Reset(true);
db().Poison();
EXPECT_FALSE(db().IsSQLValid("INSERT INTO x VALUES ('x')"));
EXPECT_FALSE(db().Execute("INSERT INTO x VALUES ('x')"));
{
sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM x"));
ASSERT_FALSE(s.is_valid());
ASSERT_FALSE(s.Step());
}
ASSERT_FALSE(valid_statement.is_valid());
ASSERT_FALSE(valid_statement.Step());
}
TEST_F(SQLConnectionTest, Attach) {
EXPECT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
base::FilePath attach_path =
db_path().DirName().AppendASCII("SQLConnectionAttach.db");
const char kAttachmentPoint[] = "other";
{
sql::Connection other_db;
ASSERT_TRUE(other_db.Open(attach_path));
EXPECT_TRUE(other_db.Execute("CREATE TABLE bar (a, b)"));
EXPECT_TRUE(other_db.Execute("INSERT INTO bar VALUES ('hello', 'world')"));
}
EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar"));
EXPECT_TRUE(db().BeginTransaction());
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_ERROR);
EXPECT_FALSE(db().AttachDatabase(attach_path, kAttachmentPoint));
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
db().RollbackTransaction();
EXPECT_TRUE(db().AttachDatabase(attach_path, kAttachmentPoint));
EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar"));
EXPECT_TRUE(db().Execute("INSERT INTO foo SELECT a, b FROM other.bar"));
{
sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM foo"));
ASSERT_TRUE(s.Step());
EXPECT_EQ(1, s.ColumnInt(0));
}
EXPECT_TRUE(db().BeginTransaction());
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_ERROR);
EXPECT_FALSE(db().DetachDatabase(kAttachmentPoint));
EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar"));
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
db().RollbackTransaction();
EXPECT_TRUE(db().DetachDatabase(kAttachmentPoint));
EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar"));
}
TEST_F(SQLConnectionTest, Basic_QuickIntegrityCheck) {
const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
ASSERT_TRUE(db().Execute(kCreateSql));
EXPECT_TRUE(db().QuickIntegrityCheck());
db().Close();
ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path()));
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_CORRUPT);
ASSERT_TRUE(db().Open(db_path()));
EXPECT_FALSE(db().QuickIntegrityCheck());
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
}
TEST_F(SQLConnectionTest, Basic_FullIntegrityCheck) {
const std::string kOk("ok");
std::vector<std::string> messages;
const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)";
ASSERT_TRUE(db().Execute(kCreateSql));
EXPECT_TRUE(db().FullIntegrityCheck(&messages));
EXPECT_EQ(1u, messages.size());
EXPECT_EQ(kOk, messages[0]);
db().Close();
ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path()));
{
sql::ScopedErrorIgnorer ignore_errors;
ignore_errors.IgnoreError(SQLITE_CORRUPT);
ASSERT_TRUE(db().Open(db_path()));
EXPECT_TRUE(db().FullIntegrityCheck(&messages));
EXPECT_LT(1u, messages.size());
EXPECT_NE(kOk, messages[0]);
ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
}
}
}