[Downloads] Allow acquiring dangerous download file.
[chromium-blink-merge.git] / sql / statement_unittest.cc
blobffa8aef1f80d4f2e4fecf3a1532834e9d15fb4a2
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.
5 #include <string>
7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "sql/connection.h"
10 #include "sql/statement.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/sqlite/sqlite3.h"
14 namespace {
16 class StatementErrorHandler : public sql::ErrorDelegate {
17 public:
18 StatementErrorHandler(int* error, std::string* sql_text)
19 : error_(error),
20 sql_text_(sql_text) {}
22 virtual ~StatementErrorHandler() {}
24 virtual int OnError(int error, sql::Connection* connection,
25 sql::Statement* stmt) OVERRIDE {
26 *error_ = error;
27 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL;
28 *sql_text_ = sql_txt ? sql_txt : "no statement available";
29 return error;
32 private:
33 int* error_;
34 std::string* sql_text_;
36 DISALLOW_COPY_AND_ASSIGN(StatementErrorHandler);
39 class SQLStatementTest : public testing::Test {
40 public:
41 SQLStatementTest() : error_(SQLITE_OK) {}
43 virtual void SetUp() {
44 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
45 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
46 // The error delegate will set |error_| and |sql_text_| when any sqlite
47 // statement operation returns an error code.
48 db_.set_error_delegate(new StatementErrorHandler(&error_, &sql_text_));
51 virtual void TearDown() {
52 // If any error happened the original sql statement can be found in
53 // |sql_text_|.
54 EXPECT_EQ(SQLITE_OK, error_);
55 db_.Close();
58 sql::Connection& db() { return db_; }
60 int sqlite_error() const { return error_; }
62 void ResetError() {
63 error_ = SQLITE_OK;
64 sql_text_.clear();
67 private:
68 base::ScopedTempDir temp_dir_;
69 sql::Connection db_;
71 // The error code of the most recent error.
72 int error_;
73 // Original statement which caused the error.
74 std::string sql_text_;
77 } // namespace
79 TEST_F(SQLStatementTest, Assign) {
80 sql::Statement s;
81 EXPECT_FALSE(s.is_valid());
83 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
84 EXPECT_TRUE(s.is_valid());
87 TEST_F(SQLStatementTest, Run) {
88 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
89 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
91 sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?"));
92 EXPECT_FALSE(s.Succeeded());
94 // Stepping it won't work since we haven't bound the value.
95 EXPECT_FALSE(s.Step());
97 // Run should fail since this produces output, and we should use Step(). This
98 // gets a bit wonky since sqlite says this is OK so succeeded is set.
99 s.Reset(true);
100 s.BindInt(0, 3);
101 EXPECT_FALSE(s.Run());
102 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode());
103 EXPECT_TRUE(s.Succeeded());
105 // Resetting it should put it back to the previous state (not runnable).
106 s.Reset(true);
107 EXPECT_FALSE(s.Succeeded());
109 // Binding and stepping should produce one row.
110 s.BindInt(0, 3);
111 EXPECT_TRUE(s.Step());
112 EXPECT_TRUE(s.Succeeded());
113 EXPECT_EQ(12, s.ColumnInt(0));
114 EXPECT_FALSE(s.Step());
115 EXPECT_TRUE(s.Succeeded());
118 TEST_F(SQLStatementTest, BasicErrorCallback) {
119 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
120 EXPECT_EQ(SQLITE_OK, sqlite_error());
121 // Insert in the foo table the primary key. It is an error to insert
122 // something other than an number. This error causes the error callback
123 // handler to be called with SQLITE_MISMATCH as error code.
124 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
125 EXPECT_TRUE(s.is_valid());
126 s.BindCString(0, "bad bad");
127 EXPECT_FALSE(s.Run());
128 EXPECT_EQ(SQLITE_MISMATCH, sqlite_error());
129 ResetError();
132 TEST_F(SQLStatementTest, Reset) {
133 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
134 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
135 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)"));
137 sql::Statement s(db().GetUniqueStatement(
138 "SELECT b FROM foo WHERE a = ? "));
139 s.BindInt(0, 3);
140 ASSERT_TRUE(s.Step());
141 EXPECT_EQ(12, s.ColumnInt(0));
142 ASSERT_FALSE(s.Step());
144 s.Reset(false);
145 // Verify that we can get all rows again.
146 ASSERT_TRUE(s.Step());
147 EXPECT_EQ(12, s.ColumnInt(0));
148 EXPECT_FALSE(s.Step());
150 s.Reset(true);
151 ASSERT_FALSE(s.Step());