| 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 "sql/connection.h" | 12 #include "sql/connection.h" |
| 15 #include "sql/correct_sql_test_base.h" | 13 #include "sql/correct_sql_test_base.h" |
| 16 #include "sql/meta_table.h" | 14 #include "sql/meta_table.h" |
| 17 #include "sql/statement.h" | 15 #include "sql/statement.h" |
| 18 #include "sql/test/error_callback_support.h" | 16 #include "sql/test/error_callback_support.h" |
| 19 #include "sql/test/scoped_error_ignorer.h" | 17 #include "sql/test/scoped_error_ignorer.h" |
| 20 #include "sql/test/test_helpers.h" | 18 #include "sql/test/test_helpers.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "third_party/sqlite/sqlite3.h" | 20 #include "third_party/sqlite/sqlite3.h" |
| (...skipping 1270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1293 | 1291 |
| 1294 samples = tester.GetHistogramSamplesSinceCreation(kCommitTime); | 1292 samples = tester.GetHistogramSamplesSinceCreation(kCommitTime); |
| 1295 ASSERT_TRUE(samples); | 1293 ASSERT_TRUE(samples); |
| 1296 // 100 for commit adjust, 1 for measuring COMMIT. | 1294 // 100 for commit adjust, 1 for measuring COMMIT. |
| 1297 EXPECT_EQ(101, samples->sum()); | 1295 EXPECT_EQ(101, samples->sum()); |
| 1298 | 1296 |
| 1299 samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime); | 1297 samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime); |
| 1300 EXPECT_EQ(0, samples->sum()); | 1298 EXPECT_EQ(0, samples->sum()); |
| 1301 } | 1299 } |
| 1302 | 1300 |
| 1303 // Make sure that OS file writes to a mmap'ed file are reflected in the memory | |
| 1304 // mapping of a memory-mapped file. Normally SQLite writes to memory-mapped | |
| 1305 // files using memcpy(), which should stay consistent. Our SQLite is slightly | |
| 1306 // patched to mmap read only, then write using OS file writes. If the | |
| 1307 // memory-mapped version doesn't reflect the OS file writes, SQLite's | |
| 1308 // memory-mapped I/O should be disabled on this platform. | |
| 1309 TEST_F(SQLConnectionTest, MmapTest) { | |
| 1310 // Skip the test for platforms which don't enable memory-mapped I/O in SQLite, | |
| 1311 // or which don't even support the pragma. The former seems to apply to iOS, | |
| 1312 // the latter to older iOS. | |
| 1313 // TODO(shess): Disable test on iOS? Disable on USE_SYSTEM_SQLITE? | |
| 1314 { | |
| 1315 sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); | |
| 1316 if (!s.Step() || !s.ColumnInt64(0)) | |
| 1317 return; | |
| 1318 } | |
| 1319 | |
| 1320 // The test re-uses the database file to make sure it's representative of a | |
| 1321 // SQLite file, but will be storing incompatible data. | |
| 1322 db().Close(); | |
| 1323 | |
| 1324 const uint32 kFlags = | |
| 1325 base::File::FLAG_OPEN|base::File::FLAG_READ|base::File::FLAG_WRITE; | |
| 1326 char buf[4096]; | |
| 1327 | |
| 1328 // Create a file with a block of '0', a block of '1', and a block of '2'. | |
| 1329 { | |
| 1330 base::File f(db_path(), kFlags); | |
| 1331 ASSERT_TRUE(f.IsValid()); | |
| 1332 memset(buf, '0', sizeof(buf)); | |
| 1333 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); | |
| 1334 | |
| 1335 memset(buf, '1', sizeof(buf)); | |
| 1336 ASSERT_EQ(f.Write(1*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); | |
| 1337 | |
| 1338 memset(buf, '2', sizeof(buf)); | |
| 1339 ASSERT_EQ(f.Write(2*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); | |
| 1340 } | |
| 1341 | |
| 1342 // mmap the file and verify that everything looks right. | |
| 1343 { | |
| 1344 base::MemoryMappedFile m; | |
| 1345 ASSERT_TRUE(m.Initialize(db_path())); | |
| 1346 | |
| 1347 memset(buf, '0', sizeof(buf)); | |
| 1348 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); | |
| 1349 | |
| 1350 memset(buf, '1', sizeof(buf)); | |
| 1351 ASSERT_EQ(0, memcmp(buf, m.data() + 1*sizeof(buf), sizeof(buf))); | |
| 1352 | |
| 1353 memset(buf, '2', sizeof(buf)); | |
| 1354 ASSERT_EQ(0, memcmp(buf, m.data() + 2*sizeof(buf), sizeof(buf))); | |
| 1355 | |
| 1356 // Scribble some '3' into the first page of the file, and verify that it | |
| 1357 // looks the same in the memory mapping. | |
| 1358 { | |
| 1359 base::File f(db_path(), kFlags); | |
| 1360 ASSERT_TRUE(f.IsValid()); | |
| 1361 memset(buf, '3', sizeof(buf)); | |
| 1362 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); | |
| 1363 } | |
| 1364 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); | |
| 1365 | |
| 1366 // Repeat with a single '4' in case page-sized blocks are different. | |
| 1367 const size_t kOffset = 1*sizeof(buf) + 123; | |
| 1368 ASSERT_NE('4', m.data()[kOffset]); | |
| 1369 { | |
| 1370 base::File f(db_path(), kFlags); | |
| 1371 ASSERT_TRUE(f.IsValid()); | |
| 1372 buf[0] = '4'; | |
| 1373 ASSERT_EQ(f.Write(kOffset, buf, 1), 1); | |
| 1374 } | |
| 1375 ASSERT_EQ('4', m.data()[kOffset]); | |
| 1376 } | |
| 1377 } | |
| 1378 | |
| 1379 } // namespace | 1301 } // namespace |
| OLD | NEW |