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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/memory_mapped_file.h" | 7 #include "base/files/memory_mapped_file.h" |
8 #include "base/files/scoped_file.h" | 8 #include "base/files/scoped_file.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 return t->cb_.Run(); | 129 return t->cb_.Run(); |
130 } | 130 } |
131 | 131 |
132 sqlite3* db_; | 132 sqlite3* db_; |
133 base::Callback<int(void)> cb_; | 133 base::Callback<int(void)> cb_; |
134 | 134 |
135 DISALLOW_COPY_AND_ASSIGN(ScopedCommitHook); | 135 DISALLOW_COPY_AND_ASSIGN(ScopedCommitHook); |
136 }; | 136 }; |
137 | 137 |
138 } // namespace test | 138 } // namespace test |
139 } // namespace sql | |
140 | 139 |
141 namespace { | 140 namespace { |
142 | 141 |
143 // Helper to return the count of items in sqlite_master. Return -1 in | 142 // Helper to return the count of items in sqlite_master. Return -1 in |
144 // case of error. | 143 // case of error. |
145 int SqliteMasterCount(sql::Connection* db) { | 144 int SqliteMasterCount(sql::Connection* db) { |
146 const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master"; | 145 const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master"; |
147 sql::Statement s(db->GetUniqueStatement(kMasterCount)); | 146 sql::Statement s(db->GetUniqueStatement(kMasterCount)); |
148 return s.Step() ? s.ColumnInt(0) : -1; | 147 return s.Step() ? s.ColumnInt(0) : -1; |
149 } | 148 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 explicit ScopedUmaskSetter(mode_t target_mask) { | 202 explicit ScopedUmaskSetter(mode_t target_mask) { |
204 old_umask_ = umask(target_mask); | 203 old_umask_ = umask(target_mask); |
205 } | 204 } |
206 ~ScopedUmaskSetter() { umask(old_umask_); } | 205 ~ScopedUmaskSetter() { umask(old_umask_); } |
207 private: | 206 private: |
208 mode_t old_umask_; | 207 mode_t old_umask_; |
209 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter); | 208 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter); |
210 }; | 209 }; |
211 #endif | 210 #endif |
212 | 211 |
| 212 // SQLite function to adjust mock time by |argv[0]| milliseconds. |
| 213 void sqlite_adjust_millis(sql::test::ScopedMockTimeSource* time_mock, |
| 214 sqlite3_context* context, |
| 215 int argc, sqlite3_value** argv) { |
| 216 int64 milliseconds = argc > 0 ? sqlite3_value_int64(argv[0]) : 1000; |
| 217 time_mock->adjust(base::TimeDelta::FromMilliseconds(milliseconds)); |
| 218 sqlite3_result_int64(context, milliseconds); |
| 219 } |
| 220 |
| 221 // Adjust mock time by |milliseconds| on commit. |
| 222 int adjust_commit_hook(sql::test::ScopedMockTimeSource* time_mock, |
| 223 int64 milliseconds) { |
| 224 time_mock->adjust(base::TimeDelta::FromMilliseconds(milliseconds)); |
| 225 return SQLITE_OK; |
| 226 } |
| 227 |
| 228 const char kCommitTime[] = "Sqlite.CommitTime.Test"; |
| 229 const char kAutoCommitTime[] = "Sqlite.AutoCommitTime.Test"; |
| 230 const char kUpdateTime[] = "Sqlite.UpdateTime.Test"; |
| 231 const char kQueryTime[] = "Sqlite.QueryTime.Test"; |
| 232 |
| 233 } // namespace |
| 234 |
213 class SQLConnectionTest : public sql::SQLTestBase { | 235 class SQLConnectionTest : public sql::SQLTestBase { |
214 public: | 236 public: |
215 void SetUp() override { | 237 void SetUp() override { |
216 // Any macro histograms which fire before the recorder is initialized cannot | 238 // Any macro histograms which fire before the recorder is initialized cannot |
217 // be tested. So this needs to be ahead of Open(). | 239 // be tested. So this needs to be ahead of Open(). |
218 base::StatisticsRecorder::Initialize(); | 240 base::StatisticsRecorder::Initialize(); |
219 | 241 |
220 SQLTestBase::SetUp(); | 242 SQLTestBase::SetUp(); |
221 } | 243 } |
222 | 244 |
(...skipping 917 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1140 sql::Connection::EVENT_STATEMENT_ROWS, 2); | 1162 sql::Connection::EVENT_STATEMENT_ROWS, 2); |
1141 tester.ExpectBucketCount(kGlobalHistogramName, | 1163 tester.ExpectBucketCount(kGlobalHistogramName, |
1142 sql::Connection::EVENT_STATEMENT_ROWS, 2); | 1164 sql::Connection::EVENT_STATEMENT_ROWS, 2); |
1143 tester.ExpectBucketCount(kHistogramName, | 1165 tester.ExpectBucketCount(kHistogramName, |
1144 sql::Connection::EVENT_STATEMENT_SUCCESS, 1); | 1166 sql::Connection::EVENT_STATEMENT_SUCCESS, 1); |
1145 tester.ExpectBucketCount(kGlobalHistogramName, | 1167 tester.ExpectBucketCount(kGlobalHistogramName, |
1146 sql::Connection::EVENT_STATEMENT_SUCCESS, 1); | 1168 sql::Connection::EVENT_STATEMENT_SUCCESS, 1); |
1147 } | 1169 } |
1148 } | 1170 } |
1149 | 1171 |
1150 // SQLite function to adjust mock time by |argv[0]| milliseconds. | |
1151 void sqlite_adjust_millis(sql::test::ScopedMockTimeSource* time_mock, | |
1152 sqlite3_context* context, | |
1153 int argc, sqlite3_value** argv) { | |
1154 int64 milliseconds = argc > 0 ? sqlite3_value_int64(argv[0]) : 1000; | |
1155 time_mock->adjust(base::TimeDelta::FromMilliseconds(milliseconds)); | |
1156 sqlite3_result_int64(context, milliseconds); | |
1157 } | |
1158 | |
1159 // Adjust mock time by |milliseconds| on commit. | |
1160 int adjust_commit_hook(sql::test::ScopedMockTimeSource* time_mock, | |
1161 int64 milliseconds) { | |
1162 time_mock->adjust(base::TimeDelta::FromMilliseconds(milliseconds)); | |
1163 return SQLITE_OK; | |
1164 } | |
1165 | |
1166 const char kCommitTime[] = "Sqlite.CommitTime.Test"; | |
1167 const char kAutoCommitTime[] = "Sqlite.AutoCommitTime.Test"; | |
1168 const char kUpdateTime[] = "Sqlite.UpdateTime.Test"; | |
1169 const char kQueryTime[] = "Sqlite.QueryTime.Test"; | |
1170 | |
1171 // Read-only query allocates time to QueryTime, but not others. | 1172 // Read-only query allocates time to QueryTime, but not others. |
1172 TEST_F(SQLConnectionTest, TimeQuery) { | 1173 TEST_F(SQLConnectionTest, TimeQuery) { |
1173 // Re-open with histogram tag. Use an in-memory database to minimize variance | 1174 // Re-open with histogram tag. Use an in-memory database to minimize variance |
1174 // due to filesystem. | 1175 // due to filesystem. |
1175 db().Close(); | 1176 db().Close(); |
1176 db().set_histogram_tag("Test"); | 1177 db().set_histogram_tag("Test"); |
1177 ASSERT_TRUE(db().OpenInMemory()); | 1178 ASSERT_TRUE(db().OpenInMemory()); |
1178 | 1179 |
1179 sql::test::ScopedMockTimeSource time_mock(db()); | 1180 sql::test::ScopedMockTimeSource time_mock(db()); |
1180 | 1181 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1380 #endif | 1381 #endif |
1381 | 1382 |
1382 TEST_F(SQLConnectionTest, OnMemoryDump) { | 1383 TEST_F(SQLConnectionTest, OnMemoryDump) { |
1383 base::trace_event::ProcessMemoryDump pmd(nullptr); | 1384 base::trace_event::ProcessMemoryDump pmd(nullptr); |
1384 base::trace_event::MemoryDumpArgs args = { | 1385 base::trace_event::MemoryDumpArgs args = { |
1385 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; | 1386 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; |
1386 ASSERT_TRUE(db().OnMemoryDump(args, &pmd)); | 1387 ASSERT_TRUE(db().OnMemoryDump(args, &pmd)); |
1387 EXPECT_GE(pmd.allocator_dumps().size(), 1u); | 1388 EXPECT_GE(pmd.allocator_dumps().size(), 1u); |
1388 } | 1389 } |
1389 | 1390 |
1390 } // namespace | 1391 // Test that the functions to collect diagnostic data run to completion, without |
| 1392 // worrying too much about what they generate (since that will change). |
| 1393 TEST_F(SQLConnectionTest, CollectDiagnosticInfo) { |
| 1394 const std::string corruption_info = db().CollectCorruptionInfo(); |
| 1395 EXPECT_NE(std::string::npos, corruption_info.find("SQLITE_CORRUPT")); |
| 1396 EXPECT_NE(std::string::npos, corruption_info.find("integrity_check")); |
| 1397 |
| 1398 // A statement to see in the results. |
| 1399 const char* kSimpleSql = "SELECT 'mountain'"; |
| 1400 Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
| 1401 |
| 1402 // Error includes the statement. |
| 1403 const std::string readonly_info = db().CollectErrorInfo(SQLITE_READONLY, &s); |
| 1404 EXPECT_NE(std::string::npos, readonly_info.find(kSimpleSql)); |
| 1405 |
| 1406 // Some other error doesn't include the statment. |
| 1407 // TODO(shess): This is weak. |
| 1408 const std::string full_info = db().CollectErrorInfo(SQLITE_FULL, NULL); |
| 1409 EXPECT_EQ(std::string::npos, full_info.find(kSimpleSql)); |
| 1410 |
| 1411 // A table to see in the SQLITE_ERROR results. |
| 1412 EXPECT_TRUE(db().Execute("CREATE TABLE volcano (x)")); |
| 1413 |
| 1414 // Version info to see in the SQLITE_ERROR results. |
| 1415 sql::MetaTable meta_table; |
| 1416 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); |
| 1417 |
| 1418 const std::string error_info = db().CollectErrorInfo(SQLITE_ERROR, &s); |
| 1419 EXPECT_NE(std::string::npos, error_info.find(kSimpleSql)); |
| 1420 EXPECT_NE(std::string::npos, error_info.find("volcano")); |
| 1421 EXPECT_NE(std::string::npos, error_info.find("version: 4")); |
| 1422 } |
| 1423 |
| 1424 TEST_F(SQLConnectionTest, RegisterIntentToUpload) { |
| 1425 base::FilePath breadcrumb_path( |
| 1426 db_path().DirName().Append(FILE_PATH_LITERAL("sqlite-diag"))); |
| 1427 |
| 1428 // No stale diagnostic store. |
| 1429 ASSERT_TRUE(!base::PathExists(breadcrumb_path)); |
| 1430 |
| 1431 // The histogram tag is required to enable diagnostic features. |
| 1432 EXPECT_FALSE(db().RegisterIntentToUpload()); |
| 1433 EXPECT_TRUE(!base::PathExists(breadcrumb_path)); |
| 1434 |
| 1435 db().Close(); |
| 1436 db().set_histogram_tag("Test"); |
| 1437 ASSERT_TRUE(db().Open(db_path())); |
| 1438 |
| 1439 // Should signal upload only once. |
| 1440 EXPECT_TRUE(db().RegisterIntentToUpload()); |
| 1441 EXPECT_TRUE(base::PathExists(breadcrumb_path)); |
| 1442 EXPECT_FALSE(db().RegisterIntentToUpload()); |
| 1443 |
| 1444 // Changing the histogram tag should allow new upload to succeed. |
| 1445 db().Close(); |
| 1446 db().set_histogram_tag("NewTest"); |
| 1447 ASSERT_TRUE(db().Open(db_path())); |
| 1448 EXPECT_TRUE(db().RegisterIntentToUpload()); |
| 1449 EXPECT_FALSE(db().RegisterIntentToUpload()); |
| 1450 |
| 1451 // Old tag is still prevented. |
| 1452 db().Close(); |
| 1453 db().set_histogram_tag("Test"); |
| 1454 ASSERT_TRUE(db().Open(db_path())); |
| 1455 EXPECT_FALSE(db().RegisterIntentToUpload()); |
| 1456 } |
| 1457 |
| 1458 } // namespace sql |
OLD | NEW |