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