Chromium Code Reviews| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/memory_mapped_file.h" | |
| 9 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 10 #include "sql/connection.h" | 11 #include "sql/connection.h" |
| 11 #include "sql/statement.h" | 12 #include "sql/statement.h" |
| 12 #include "sql/test/sql_test_base.h" | 13 #include "sql/test/sql_test_base.h" |
| 13 #include "sql/test/test_helpers.h" | 14 #include "sql/test/test_helpers.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/sqlite/sqlite3.h" | 16 #include "third_party/sqlite/sqlite3.h" |
| 16 | 17 |
| 17 // Test that certain features are/are-not enabled in our SQLite. | 18 // Test that certain features are/are-not enabled in our SQLite. |
| 18 | 19 |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 136 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)")); | 137 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)")); |
| 137 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); | 138 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); |
| 138 EXPECT_EQ(2u, rows); | 139 EXPECT_EQ(2u, rows); |
| 139 | 140 |
| 140 // Deleting the parent should cascade, i.e., delete the children as well. | 141 // Deleting the parent should cascade, i.e., delete the children as well. |
| 141 ASSERT_TRUE(db().Execute("DELETE FROM parents")); | 142 ASSERT_TRUE(db().Execute("DELETE FROM parents")); |
| 142 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); | 143 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows)); |
| 143 EXPECT_EQ(0u, rows); | 144 EXPECT_EQ(0u, rows); |
| 144 } | 145 } |
| 145 | 146 |
| 147 #if defined(MOJO_APPTEST_IMPL) || defined(OS_IOS) | |
| 148 // If the platform cannot support SQLite mmap'ed I/O, make sure SQLite isn't | |
| 149 // offering to support it. | |
| 150 TEST_F(SQLiteFeaturesTest, NoMmap) { | |
| 151 // For recent versions of SQLite, SQLITE_MAX_MMAP_SIZE=0 can be used to | |
| 152 // disable mmap support. Alternately, sqlite3_config() could be used. In | |
| 153 // that case, the pragma will run successfully, but the size will always be 0. | |
| 154 // | |
| 155 // The SQLite embedded in older iOS releases predates the addition of mmap | |
| 156 // support. In that case the pragma will run without error, but no results | |
| 157 // are returned when querying the value. | |
| 158 // | |
| 159 // MojoVFS implements a no-op for xFileControl(). PRAGMA mmap_size is | |
| 160 // implemented in terms of SQLITE_FCNTL_MMAP_SIZE. In that case, the pragma | |
| 161 // will succeed but with no effect. | |
| 162 ignore_result(db().Execute("PRAGMA mmap_size = 1048576")); | |
| 163 sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); | |
| 164 ASSERT_TRUE(!s.Step() || !s.ColumnInt64(0)); | |
| 165 } | |
| 166 #else | |
| 167 // Verify that OS file writes are reflected in the memory mapping of a | |
| 168 // memory-mapped file. Normally SQLite writes to memory-mapped files using | |
| 169 // memcpy(), which should stay consistent. Our SQLite is slightly patched to | |
| 170 // mmap read only, then write using OS file writes. If the memory-mapped | |
| 171 // version doesn't reflect the OS file writes, SQLite's memory-mapped I/O should | |
| 172 // be disabled on this platform using SQLITE_MAX_MMAP_SIZE=0. | |
| 173 TEST_F(SQLiteFeaturesTest, Mmap) { | |
| 174 // Try to turn on mmap'ed I/O. | |
| 175 ignore_result(db().Execute("PRAGMA mmap_size = 1048576")); | |
| 176 { | |
| 177 sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size")); | |
| 178 | |
| 179 #if !defined(USE_SYSTEM_SQLITE) | |
| 180 // With Chromium's version of SQLite, the setting should always be non-zero. | |
| 181 ASSERT_TRUE(s.Step()); | |
| 182 ASSERT_GT(s.ColumnInt64(0), 0); | |
| 183 #else | |
| 184 // With the system SQLite, don't verify underlying mmap functionality if the | |
| 185 // SQLite is too old to support mmap, or if mmap is disabled (see NoMmap | |
| 186 // test). USE_SYSTEM_SQLITE is not bundled into the NoMmap case because | |
| 187 // whether mmap is enabled or not is outside of Chromium's control. | |
| 188 if (!s.Step() || !s.ColumnInt64(0)) | |
| 189 return; | |
| 190 #endif | |
| 191 } | |
| 192 db().Close(); | |
|
Scott Hess - ex-Googler
2015/12/15 19:05:46
From here this code is just a move from connection
| |
| 193 | |
| 194 const uint32 kFlags = | |
| 195 base::File::FLAG_OPEN|base::File::FLAG_READ|base::File::FLAG_WRITE; | |
| 196 char buf[4096]; | |
| 197 | |
| 198 // Create a file with a block of '0', a block of '1', and a block of '2'. | |
| 199 { | |
| 200 base::File f(db_path(), kFlags); | |
| 201 ASSERT_TRUE(f.IsValid()); | |
| 202 memset(buf, '0', sizeof(buf)); | |
| 203 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); | |
| 204 | |
| 205 memset(buf, '1', sizeof(buf)); | |
| 206 ASSERT_EQ(f.Write(1*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); | |
| 207 | |
| 208 memset(buf, '2', sizeof(buf)); | |
| 209 ASSERT_EQ(f.Write(2*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); | |
| 210 } | |
| 211 | |
| 212 // mmap the file and verify that everything looks right. | |
| 213 { | |
| 214 base::MemoryMappedFile m; | |
| 215 ASSERT_TRUE(m.Initialize(db_path())); | |
| 216 | |
| 217 memset(buf, '0', sizeof(buf)); | |
| 218 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); | |
| 219 | |
| 220 memset(buf, '1', sizeof(buf)); | |
| 221 ASSERT_EQ(0, memcmp(buf, m.data() + 1*sizeof(buf), sizeof(buf))); | |
| 222 | |
| 223 memset(buf, '2', sizeof(buf)); | |
| 224 ASSERT_EQ(0, memcmp(buf, m.data() + 2*sizeof(buf), sizeof(buf))); | |
| 225 | |
| 226 // Scribble some '3' into the first page of the file, and verify that it | |
| 227 // looks the same in the memory mapping. | |
| 228 { | |
| 229 base::File f(db_path(), kFlags); | |
| 230 ASSERT_TRUE(f.IsValid()); | |
| 231 memset(buf, '3', sizeof(buf)); | |
| 232 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf)); | |
| 233 } | |
| 234 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf))); | |
| 235 | |
| 236 // Repeat with a single '4' in case page-sized blocks are different. | |
| 237 const size_t kOffset = 1*sizeof(buf) + 123; | |
| 238 ASSERT_NE('4', m.data()[kOffset]); | |
| 239 { | |
| 240 base::File f(db_path(), kFlags); | |
| 241 ASSERT_TRUE(f.IsValid()); | |
| 242 buf[0] = '4'; | |
| 243 ASSERT_EQ(f.Write(kOffset, buf, 1), 1); | |
| 244 } | |
| 245 ASSERT_EQ('4', m.data()[kOffset]); | |
| 246 } | |
| 247 } | |
| 248 #endif | |
| 249 | |
| 146 } // namespace | 250 } // namespace |
| OLD | NEW |