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" |
(...skipping 836 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
847 db().IsSQLValid(kSimpleSql); | 847 db().IsSQLValid(kSimpleSql); |
848 }, "Illegal use of connection without a db"); | 848 }, "Illegal use of connection without a db"); |
849 } | 849 } |
850 #endif | 850 #endif |
851 } | 851 } |
852 | 852 |
853 // TODO(shess): Spin up a background thread to hold other_db, to more | 853 // TODO(shess): Spin up a background thread to hold other_db, to more |
854 // closely match real life. That would also allow testing | 854 // closely match real life. That would also allow testing |
855 // RazeWithTimeout(). | 855 // RazeWithTimeout(). |
856 | 856 |
| 857 // On Windows, truncate silently fails against a memory-mapped file. One goal |
| 858 // of Raze() is to truncate the file to remove blocks which generate I/O errors. |
| 859 // Test that Raze() turns off memory mapping so that the file is truncated. |
| 860 // [This would not cover the case of multiple connections where one of the other |
| 861 // connections is memory-mapped. That is infrequent in Chromium.] |
| 862 TEST_F(SQLConnectionTest, RazeTruncate) { |
| 863 // The empty database has 0 or 1 pages. Raze() should leave it with exactly 1 |
| 864 // page. Not checking directly because auto_vacuum on Android adds a freelist |
| 865 // page. |
| 866 ASSERT_TRUE(db().Raze()); |
| 867 int64_t expected_size; |
| 868 ASSERT_TRUE(base::GetFileSize(db_path(), &expected_size)); |
| 869 ASSERT_GT(expected_size, 0); |
| 870 |
| 871 // Cause the database to take a few pages. |
| 872 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 873 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 874 for (size_t i = 0; i < 24; ++i) { |
| 875 ASSERT_TRUE( |
| 876 db().Execute("INSERT INTO foo (value) VALUES (randomblob(1024))")); |
| 877 } |
| 878 int64_t db_size; |
| 879 ASSERT_TRUE(base::GetFileSize(db_path(), &db_size)); |
| 880 ASSERT_GT(db_size, expected_size); |
| 881 |
| 882 // Make a query covering most of the database file to make sure that the |
| 883 // blocks are actually mapped into memory. Empirically, the truncate problem |
| 884 // doesn't seem to happen if no blocks are mapped. |
| 885 EXPECT_EQ("24576", |
| 886 ExecuteWithResult(&db(), "SELECT SUM(LENGTH(value)) FROM foo")); |
| 887 |
| 888 ASSERT_TRUE(db().Raze()); |
| 889 ASSERT_TRUE(base::GetFileSize(db_path(), &db_size)); |
| 890 ASSERT_EQ(expected_size, db_size); |
| 891 } |
| 892 |
857 #if defined(OS_ANDROID) | 893 #if defined(OS_ANDROID) |
858 TEST_F(SQLConnectionTest, SetTempDirForSQL) { | 894 TEST_F(SQLConnectionTest, SetTempDirForSQL) { |
859 | 895 |
860 sql::MetaTable meta_table; | 896 sql::MetaTable meta_table; |
861 // Below call needs a temporary directory in sqlite3 | 897 // Below call needs a temporary directory in sqlite3 |
862 // On Android, it can pass only when the temporary directory is set. | 898 // On Android, it can pass only when the temporary directory is set. |
863 // Otherwise, sqlite3 doesn't find the correct directory to store | 899 // Otherwise, sqlite3 doesn't find the correct directory to store |
864 // temporary files and will report the error 'unable to open | 900 // temporary files and will report the error 'unable to open |
865 // database file'. | 901 // database file'. |
866 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); | 902 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); |
(...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1626 if (DLOG_IS_ON(FATAL)) { | 1662 if (DLOG_IS_ON(FATAL)) { |
1627 db().set_error_callback(base::Bind(&IgnoreErrorCallback)); | 1663 db().set_error_callback(base::Bind(&IgnoreErrorCallback)); |
1628 ASSERT_DEATH({ | 1664 ASSERT_DEATH({ |
1629 db().GetUniqueStatement("SELECT x"); | 1665 db().GetUniqueStatement("SELECT x"); |
1630 }, "SQL compile error no such column: x"); | 1666 }, "SQL compile error no such column: x"); |
1631 } | 1667 } |
1632 #endif | 1668 #endif |
1633 } | 1669 } |
1634 | 1670 |
1635 } // namespace sql | 1671 } // namespace sql |
OLD | NEW |