| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "sql/test/test_helpers.h" | 5 #include "sql/test/test_helpers.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> |
| 10 #include <string> | 11 #include <string> |
| 11 | 12 |
| 12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_file.h" | 14 #include "base/files/scoped_file.h" |
| 14 #include "sql/connection.h" | 15 #include "sql/connection.h" |
| 15 #include "sql/statement.h" | 16 #include "sql/statement.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 int page_size = 0; | 129 int page_size = 0; |
| 129 if (!GetPageSize(&db, &page_size)) | 130 if (!GetPageSize(&db, &page_size)) |
| 130 return false; | 131 return false; |
| 131 | 132 |
| 132 int page_number = 0; | 133 int page_number = 0; |
| 133 if (!GetRootPage(&db, tree_name, &page_number)) | 134 if (!GetRootPage(&db, tree_name, &page_number)) |
| 134 return false; | 135 return false; |
| 135 | 136 |
| 136 // SQLite uses 1-based page numbering. | 137 // SQLite uses 1-based page numbering. |
| 137 const long int page_ofs = (page_number - 1) * page_size; | 138 const long int page_ofs = (page_number - 1) * page_size; |
| 138 scoped_ptr<char[]> page_buf(new char[page_size]); | 139 std::unique_ptr<char[]> page_buf(new char[page_size]); |
| 139 | 140 |
| 140 // Get the page into page_buf. | 141 // Get the page into page_buf. |
| 141 base::ScopedFILE file(base::OpenFile(db_path, "rb+")); | 142 base::ScopedFILE file(base::OpenFile(db_path, "rb+")); |
| 142 if (!file.get()) | 143 if (!file.get()) |
| 143 return false; | 144 return false; |
| 144 if (0 != fseek(file.get(), page_ofs, SEEK_SET)) | 145 if (0 != fseek(file.get(), page_ofs, SEEK_SET)) |
| 145 return false; | 146 return false; |
| 146 if (1u != fread(page_buf.get(), page_size, 1, file.get())) | 147 if (1u != fread(page_buf.get(), page_size, 1, file.get())) |
| 147 return false; | 148 return false; |
| 148 | 149 |
| 149 // Require the page to be a leaf node. A multilevel tree would be | 150 // Require the page to be a leaf node. A multilevel tree would be |
| 150 // very hard to restore correctly. | 151 // very hard to restore correctly. |
| 151 if (page_buf[0] != 0xD && page_buf[0] != 0xA) | 152 if (page_buf[0] != 0xD && page_buf[0] != 0xA) |
| 152 return false; | 153 return false; |
| 153 | 154 |
| 154 // The update has to work, and make changes. | 155 // The update has to work, and make changes. |
| 155 if (!db.Execute(update_sql)) | 156 if (!db.Execute(update_sql)) |
| 156 return false; | 157 return false; |
| 157 if (db.GetLastChangeCount() == 0) | 158 if (db.GetLastChangeCount() == 0) |
| 158 return false; | 159 return false; |
| 159 | 160 |
| 160 // Ensure that the database is fully flushed. | 161 // Ensure that the database is fully flushed. |
| 161 db.Close(); | 162 db.Close(); |
| 162 | 163 |
| 163 // Check that the stored page actually changed. This catches usage | 164 // Check that the stored page actually changed. This catches usage |
| 164 // errors where |update_sql| is not related to |tree_name|. | 165 // errors where |update_sql| is not related to |tree_name|. |
| 165 scoped_ptr<char[]> check_page_buf(new char[page_size]); | 166 std::unique_ptr<char[]> check_page_buf(new char[page_size]); |
| 166 // The on-disk data should have changed. | 167 // The on-disk data should have changed. |
| 167 if (0 != fflush(file.get())) | 168 if (0 != fflush(file.get())) |
| 168 return false; | 169 return false; |
| 169 if (0 != fseek(file.get(), page_ofs, SEEK_SET)) | 170 if (0 != fseek(file.get(), page_ofs, SEEK_SET)) |
| 170 return false; | 171 return false; |
| 171 if (1u != fread(check_page_buf.get(), page_size, 1, file.get())) | 172 if (1u != fread(check_page_buf.get(), page_size, 1, file.get())) |
| 172 return false; | 173 return false; |
| 173 if (!memcmp(check_page_buf.get(), page_buf.get(), page_size)) | 174 if (!memcmp(check_page_buf.get(), page_buf.get(), page_size)) |
| 174 return false; | 175 return false; |
| 175 | 176 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 sql::Statement statement(db->GetUniqueStatement("PRAGMA integrity_check")); | 252 sql::Statement statement(db->GetUniqueStatement("PRAGMA integrity_check")); |
| 252 | 253 |
| 253 // SQLite should always return a row of data. | 254 // SQLite should always return a row of data. |
| 254 EXPECT_TRUE(statement.Step()); | 255 EXPECT_TRUE(statement.Step()); |
| 255 | 256 |
| 256 return statement.ColumnString(0); | 257 return statement.ColumnString(0); |
| 257 } | 258 } |
| 258 | 259 |
| 259 } // namespace test | 260 } // namespace test |
| 260 } // namespace sql | 261 } // namespace sql |
| OLD | NEW |