| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_file.h" | 10 #include "base/files/scoped_file.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/metrics/statistics_recorder.h" | |
| 15 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
| 16 #include "base/trace_event/process_memory_dump.h" | 15 #include "base/trace_event/process_memory_dump.h" |
| 17 #include "sql/connection.h" | 16 #include "sql/connection.h" |
| 18 #include "sql/connection_memory_dump_provider.h" | 17 #include "sql/connection_memory_dump_provider.h" |
| 19 #include "sql/correct_sql_test_base.h" | 18 #include "sql/correct_sql_test_base.h" |
| 20 #include "sql/meta_table.h" | 19 #include "sql/meta_table.h" |
| 21 #include "sql/statement.h" | 20 #include "sql/statement.h" |
| 22 #include "sql/test/error_callback_support.h" | 21 #include "sql/test/error_callback_support.h" |
| 23 #include "sql/test/scoped_error_expecter.h" | 22 #include "sql/test/scoped_error_expecter.h" |
| 24 #include "sql/test/test_helpers.h" | 23 #include "sql/test/test_helpers.h" |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 void ErrorCallbackResetHelper(sql::Connection* db, | 193 void ErrorCallbackResetHelper(sql::Connection* db, |
| 195 size_t* counter, | 194 size_t* counter, |
| 196 const RefCounter& r, | 195 const RefCounter& r, |
| 197 int error, sql::Statement* stmt) { | 196 int error, sql::Statement* stmt) { |
| 198 // The ref count should not go to zero when clearing the callback. | 197 // The ref count should not go to zero when clearing the callback. |
| 199 EXPECT_GT(*counter, 0u); | 198 EXPECT_GT(*counter, 0u); |
| 200 db->reset_error_callback(); | 199 db->reset_error_callback(); |
| 201 EXPECT_GT(*counter, 0u); | 200 EXPECT_GT(*counter, 0u); |
| 202 } | 201 } |
| 203 | 202 |
| 203 // Handle errors by blowing away the database. |
| 204 void RazeErrorCallback(sql::Connection* db, |
| 205 int expected_error, |
| 206 int error, |
| 207 sql::Statement* stmt) { |
| 208 // Nothing here needs extended errors at this time. |
| 209 EXPECT_EQ(expected_error, expected_error&0xff); |
| 210 EXPECT_EQ(expected_error, error&0xff); |
| 211 db->RazeAndClose(); |
| 212 } |
| 213 |
| 204 #if defined(OS_POSIX) | 214 #if defined(OS_POSIX) |
| 205 // Set a umask and restore the old mask on destruction. Cribbed from | 215 // Set a umask and restore the old mask on destruction. Cribbed from |
| 206 // shared_memory_unittest.cc. Used by POSIX-only UserPermission test. | 216 // shared_memory_unittest.cc. Used by POSIX-only UserPermission test. |
| 207 class ScopedUmaskSetter { | 217 class ScopedUmaskSetter { |
| 208 public: | 218 public: |
| 209 explicit ScopedUmaskSetter(mode_t target_mask) { | 219 explicit ScopedUmaskSetter(mode_t target_mask) { |
| 210 old_umask_ = umask(target_mask); | 220 old_umask_ = umask(target_mask); |
| 211 } | 221 } |
| 212 ~ScopedUmaskSetter() { umask(old_umask_); } | 222 ~ScopedUmaskSetter() { umask(old_umask_); } |
| 213 private: | 223 private: |
| (...skipping 18 matching lines...) Expand all Loading... |
| 232 return SQLITE_OK; | 242 return SQLITE_OK; |
| 233 } | 243 } |
| 234 | 244 |
| 235 const char kCommitTime[] = "Sqlite.CommitTime.Test"; | 245 const char kCommitTime[] = "Sqlite.CommitTime.Test"; |
| 236 const char kAutoCommitTime[] = "Sqlite.AutoCommitTime.Test"; | 246 const char kAutoCommitTime[] = "Sqlite.AutoCommitTime.Test"; |
| 237 const char kUpdateTime[] = "Sqlite.UpdateTime.Test"; | 247 const char kUpdateTime[] = "Sqlite.UpdateTime.Test"; |
| 238 const char kQueryTime[] = "Sqlite.QueryTime.Test"; | 248 const char kQueryTime[] = "Sqlite.QueryTime.Test"; |
| 239 | 249 |
| 240 } // namespace | 250 } // namespace |
| 241 | 251 |
| 242 class SQLConnectionTest : public sql::SQLTestBase { | 252 using SQLConnectionTest = sql::SQLTestBase; |
| 243 public: | |
| 244 void SetUp() override { | |
| 245 // Any macro histograms which fire before the recorder is initialized cannot | |
| 246 // be tested. So this needs to be ahead of Open(). | |
| 247 base::StatisticsRecorder::Initialize(); | |
| 248 | |
| 249 SQLTestBase::SetUp(); | |
| 250 } | |
| 251 | |
| 252 // Handle errors by blowing away the database. | |
| 253 void RazeErrorCallback(int expected_error, int error, sql::Statement* stmt) { | |
| 254 // Nothing here needs extended errors at this time. | |
| 255 EXPECT_EQ(expected_error, expected_error&0xff); | |
| 256 EXPECT_EQ(expected_error, error&0xff); | |
| 257 db().RazeAndClose(); | |
| 258 } | |
| 259 }; | |
| 260 | 253 |
| 261 TEST_F(SQLConnectionTest, Execute) { | 254 TEST_F(SQLConnectionTest, Execute) { |
| 262 // Valid statement should return true. | 255 // Valid statement should return true. |
| 263 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | 256 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 264 EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); | 257 EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); |
| 265 | 258 |
| 266 // Invalid statement should fail. | 259 // Invalid statement should fail. |
| 267 ASSERT_EQ(SQLITE_ERROR, | 260 ASSERT_EQ(SQLITE_ERROR, |
| 268 db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b")); | 261 db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b")); |
| 269 EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode()); | 262 EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode()); |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 // fail with SQLITE_CORRUPT, as will this PRAGMA. | 678 // fail with SQLITE_CORRUPT, as will this PRAGMA. |
| 686 { | 679 { |
| 687 sql::test::ScopedErrorExpecter expecter; | 680 sql::test::ScopedErrorExpecter expecter; |
| 688 expecter.ExpectError(SQLITE_CORRUPT); | 681 expecter.ExpectError(SQLITE_CORRUPT); |
| 689 ASSERT_TRUE(db().Open(db_path())); | 682 ASSERT_TRUE(db().Open(db_path())); |
| 690 ASSERT_FALSE(db().Execute("PRAGMA auto_vacuum")); | 683 ASSERT_FALSE(db().Execute("PRAGMA auto_vacuum")); |
| 691 db().Close(); | 684 db().Close(); |
| 692 ASSERT_TRUE(expecter.SawExpectedErrors()); | 685 ASSERT_TRUE(expecter.SawExpectedErrors()); |
| 693 } | 686 } |
| 694 | 687 |
| 695 db().set_error_callback(base::Bind(&SQLConnectionTest::RazeErrorCallback, | 688 db().set_error_callback(base::Bind(&RazeErrorCallback, |
| 696 base::Unretained(this), | 689 &db(), |
| 697 SQLITE_CORRUPT)); | 690 SQLITE_CORRUPT)); |
| 698 | 691 |
| 699 // When the PRAGMA calls in Open() raise SQLITE_CORRUPT, the error | 692 // When the PRAGMA calls in Open() raise SQLITE_CORRUPT, the error |
| 700 // callback will call RazeAndClose(). Open() will then fail and be | 693 // callback will call RazeAndClose(). Open() will then fail and be |
| 701 // retried. The second Open() on the empty database will succeed | 694 // retried. The second Open() on the empty database will succeed |
| 702 // cleanly. | 695 // cleanly. |
| 703 ASSERT_TRUE(db().Open(db_path())); | 696 ASSERT_TRUE(db().Open(db_path())); |
| 704 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum")); | 697 ASSERT_TRUE(db().Execute("PRAGMA auto_vacuum")); |
| 705 EXPECT_EQ(0, SqliteMasterCount(&db())); | 698 EXPECT_EQ(0, SqliteMasterCount(&db())); |
| 706 } | 699 } |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 931 ASSERT_FALSE(s.Step()); | 924 ASSERT_FALSE(s.Step()); |
| 932 } | 925 } |
| 933 | 926 |
| 934 // The existing statement has become invalid. | 927 // The existing statement has become invalid. |
| 935 ASSERT_FALSE(valid_statement.is_valid()); | 928 ASSERT_FALSE(valid_statement.is_valid()); |
| 936 ASSERT_FALSE(valid_statement.Step()); | 929 ASSERT_FALSE(valid_statement.Step()); |
| 937 | 930 |
| 938 // Test that poisoning the database during a transaction works (with errors). | 931 // Test that poisoning the database during a transaction works (with errors). |
| 939 // RazeErrorCallback() poisons the database, the extra COMMIT causes | 932 // RazeErrorCallback() poisons the database, the extra COMMIT causes |
| 940 // CommitTransaction() to throw an error while commiting. | 933 // CommitTransaction() to throw an error while commiting. |
| 941 db().set_error_callback(base::Bind(&SQLConnectionTest::RazeErrorCallback, | 934 db().set_error_callback(base::Bind(&RazeErrorCallback, |
| 942 base::Unretained(this), | 935 &db(), |
| 943 SQLITE_ERROR)); | 936 SQLITE_ERROR)); |
| 944 db().Close(); | 937 db().Close(); |
| 945 ASSERT_TRUE(db().Open(db_path())); | 938 ASSERT_TRUE(db().Open(db_path())); |
| 946 EXPECT_TRUE(db().BeginTransaction()); | 939 EXPECT_TRUE(db().BeginTransaction()); |
| 947 EXPECT_TRUE(db().Execute("INSERT INTO x VALUES ('x')")); | 940 EXPECT_TRUE(db().Execute("INSERT INTO x VALUES ('x')")); |
| 948 EXPECT_TRUE(db().Execute("COMMIT")); | 941 EXPECT_TRUE(db().Execute("COMMIT")); |
| 949 EXPECT_FALSE(db().CommitTransaction()); | 942 EXPECT_FALSE(db().CommitTransaction()); |
| 950 } | 943 } |
| 951 | 944 |
| 952 // Test attaching and detaching databases from the connection. | 945 // Test attaching and detaching databases from the connection. |
| (...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1493 if (DLOG_IS_ON(FATAL)) { | 1486 if (DLOG_IS_ON(FATAL)) { |
| 1494 db().set_error_callback(base::Bind(&IgnoreErrorCallback)); | 1487 db().set_error_callback(base::Bind(&IgnoreErrorCallback)); |
| 1495 ASSERT_DEATH({ | 1488 ASSERT_DEATH({ |
| 1496 db().GetUniqueStatement("SELECT x"); | 1489 db().GetUniqueStatement("SELECT x"); |
| 1497 }, "SQL compile error no such column: x"); | 1490 }, "SQL compile error no such column: x"); |
| 1498 } | 1491 } |
| 1499 #endif | 1492 #endif |
| 1500 } | 1493 } |
| 1501 | 1494 |
| 1502 } // namespace sql | 1495 } // namespace sql |
| OLD | NEW |