| 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" | |
| 8 #include "base/files/scoped_file.h" | 7 #include "base/files/scoped_file.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/metrics/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "base/test/histogram_tester.h" | 11 #include "base/test/histogram_tester.h" |
| 14 #include "base/trace_event/process_memory_dump.h" | 12 #include "base/trace_event/process_memory_dump.h" |
| 15 #include "sql/connection.h" | 13 #include "sql/connection.h" |
| 16 #include "sql/correct_sql_test_base.h" | 14 #include "sql/correct_sql_test_base.h" |
| 17 #include "sql/meta_table.h" | 15 #include "sql/meta_table.h" |
| 18 #include "sql/statement.h" | 16 #include "sql/statement.h" |
| 19 #include "sql/test/error_callback_support.h" | 17 #include "sql/test/error_callback_support.h" |
| 20 #include "sql/test/scoped_error_ignorer.h" | 18 #include "sql/test/scoped_error_ignorer.h" |
| 21 #include "sql/test/test_helpers.h" | 19 #include "sql/test/test_helpers.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 1272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1295 | 1293 |
| 1296 samples = tester.GetHistogramSamplesSinceCreation(kCommitTime); | 1294 samples = tester.GetHistogramSamplesSinceCreation(kCommitTime); |
| 1297 ASSERT_TRUE(samples); | 1295 ASSERT_TRUE(samples); |
| 1298 // 100 for commit adjust, 1 for measuring COMMIT. | 1296 // 100 for commit adjust, 1 for measuring COMMIT. |
| 1299 EXPECT_EQ(101, samples->sum()); | 1297 EXPECT_EQ(101, samples->sum()); |
| 1300 | 1298 |
| 1301 samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime); | 1299 samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime); |
| 1302 EXPECT_EQ(0, samples->sum()); | 1300 EXPECT_EQ(0, samples->sum()); |
| 1303 } | 1301 } |
| 1304 | 1302 |
| 1305 // Make sure that OS file writes to a mmap'ed file are reflected in the memory | |
| 1306 // mapping of a memory-mapped file. Normally SQLite writes to memory-mapped | |
| 1307 // files using memcpy(), which should stay consistent. Our SQLite is slightly | |
| 1308 // patched to mmap read only, then write using OS file writes. If the | |
| 1309 // memory-mapped version doesn't reflect the OS file writes, SQLite's | |
| 1310 // memory-mapped I/O should be disabled on this platform. | |
| 1311 #if !defined(MOJO_APPTEST_IMPL) | |
| 1312 TEST_F(SQLConnectionTest, MmapTest) { | |
| 1313 // Skip the test for platforms which don't enable memory-mapped I/O in SQLite, | |
| 1314 // or which don't even support the pragma. The former seems to apply to iOS, | |
| 1315 // the latter to older iOS. | |
| 1316 // TODO(shess): Disable test on iOS? Disable on USE_SYSTEM_SQLITE? | |
| 1317 { | |
| 1318 sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); | |
| 1319 if (!s.Step() || !s.ColumnInt64(0)) | |
| 1320 return; | |
| 1321 } | |
| 1322 | |
| 1323 // The test re-uses the database file to make sure it's representative of a | |
| 1324 // SQLite file, but will be storing incompatible data. | |
| 1325 db().Close(); | |
| 1326 | |
| 1327 const uint32 kFlags = | |
| 1328 base::File::FLAG_OPEN|base::File::FLAG_READ|base::File::FLAG_WRITE; | |
| 1329 char buf[4096]; | |
| 1330 | |
| 1331 // Create a file with a block of '0', a block of '1', and a block of '2'. | |
| 1332 { | |
| 1333 base::File f(db_path(), kFlags); | |
| 1334 ASSERT_TRUE(f.IsValid()); | |
| 1335 memset(buf, '0', sizeof(buf)); | |
| 1336 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); | |
| 1337 | |
| 1338 memset(buf, '1', sizeof(buf)); | |
| 1339 ASSERT_EQ(f.Write(1*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); | |
| 1340 | |
| 1341 memset(buf, '2', sizeof(buf)); | |
| 1342 ASSERT_EQ(f.Write(2*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); | |
| 1343 } | |
| 1344 | |
| 1345 // mmap the file and verify that everything looks right. | |
| 1346 { | |
| 1347 base::MemoryMappedFile m; | |
| 1348 ASSERT_TRUE(m.Initialize(db_path())); | |
| 1349 | |
| 1350 memset(buf, '0', sizeof(buf)); | |
| 1351 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); | |
| 1352 | |
| 1353 memset(buf, '1', sizeof(buf)); | |
| 1354 ASSERT_EQ(0, memcmp(buf, m.data() + 1*sizeof(buf), sizeof(buf))); | |
| 1355 | |
| 1356 memset(buf, '2', sizeof(buf)); | |
| 1357 ASSERT_EQ(0, memcmp(buf, m.data() + 2*sizeof(buf), sizeof(buf))); | |
| 1358 | |
| 1359 // Scribble some '3' into the first page of the file, and verify that it | |
| 1360 // looks the same in the memory mapping. | |
| 1361 { | |
| 1362 base::File f(db_path(), kFlags); | |
| 1363 ASSERT_TRUE(f.IsValid()); | |
| 1364 memset(buf, '3', sizeof(buf)); | |
| 1365 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); | |
| 1366 } | |
| 1367 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); | |
| 1368 | |
| 1369 // Repeat with a single '4' in case page-sized blocks are different. | |
| 1370 const size_t kOffset = 1*sizeof(buf) + 123; | |
| 1371 ASSERT_NE('4', m.data()[kOffset]); | |
| 1372 { | |
| 1373 base::File f(db_path(), kFlags); | |
| 1374 ASSERT_TRUE(f.IsValid()); | |
| 1375 buf[0] = '4'; | |
| 1376 ASSERT_EQ(f.Write(kOffset, buf, 1), 1); | |
| 1377 } | |
| 1378 ASSERT_EQ('4', m.data()[kOffset]); | |
| 1379 } | |
| 1380 } | |
| 1381 #endif | |
| 1382 | |
| 1383 TEST_F(SQLConnectionTest, OnMemoryDump) { | 1303 TEST_F(SQLConnectionTest, OnMemoryDump) { |
| 1384 base::trace_event::ProcessMemoryDump pmd(nullptr); | 1304 base::trace_event::ProcessMemoryDump pmd(nullptr); |
| 1385 base::trace_event::MemoryDumpArgs args = { | 1305 base::trace_event::MemoryDumpArgs args = { |
| 1386 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; | 1306 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; |
| 1387 ASSERT_TRUE(db().OnMemoryDump(args, &pmd)); | 1307 ASSERT_TRUE(db().OnMemoryDump(args, &pmd)); |
| 1388 EXPECT_GE(pmd.allocator_dumps().size(), 1u); | 1308 EXPECT_GE(pmd.allocator_dumps().size(), 1u); |
| 1389 } | 1309 } |
| 1390 | 1310 |
| 1391 // Test that the functions to collect diagnostic data run to completion, without | 1311 // Test that the functions to collect diagnostic data run to completion, without |
| 1392 // worrying too much about what they generate (since that will change). | 1312 // worrying too much about what they generate (since that will change). |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1455 | 1375 |
| 1456 // Old tag is still prevented. | 1376 // Old tag is still prevented. |
| 1457 db().Close(); | 1377 db().Close(); |
| 1458 db().set_histogram_tag("Test"); | 1378 db().set_histogram_tag("Test"); |
| 1459 ASSERT_TRUE(db().Open(db_path())); | 1379 ASSERT_TRUE(db().Open(db_path())); |
| 1460 EXPECT_FALSE(db().RegisterIntentToUpload()); | 1380 EXPECT_FALSE(db().RegisterIntentToUpload()); |
| 1461 } | 1381 } |
| 1462 #endif // !defined(MOJO_APPTEST_IMPL) | 1382 #endif // !defined(MOJO_APPTEST_IMPL) |
| 1463 | 1383 |
| 1464 } // namespace sql | 1384 } // namespace sql |
| OLD | NEW |