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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/files/scoped_file.h" | 10 #include "base/files/scoped_file.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 } | 62 } |
63 | 63 |
64 } // namespace | 64 } // namespace |
65 | 65 |
66 namespace sql { | 66 namespace sql { |
67 namespace test { | 67 namespace test { |
68 | 68 |
69 bool CorruptSizeInHeader(const base::FilePath& db_path) { | 69 bool CorruptSizeInHeader(const base::FilePath& db_path) { |
70 // See http://www.sqlite.org/fileformat.html#database_header | 70 // See http://www.sqlite.org/fileformat.html#database_header |
71 const size_t kHeaderSize = 100; | 71 const size_t kHeaderSize = 100; |
72 const size_t kPageSizeOffset = 16; | |
73 const size_t kFileChangeCountOffset = 24; | |
74 const size_t kPageCountOffset = 28; | |
75 const size_t kVersionValidForOffset = 92; // duplicate kFileChangeCountOffset | |
76 | 72 |
77 unsigned char header[kHeaderSize]; | 73 unsigned char header[kHeaderSize]; |
78 | 74 |
79 base::ScopedFILE file(base::OpenFile(db_path, "rb+")); | 75 base::ScopedFILE file(base::OpenFile(db_path, "rb+")); |
80 if (!file.get()) | 76 if (!file.get()) |
81 return false; | 77 return false; |
82 | 78 |
83 if (0 != fseek(file.get(), 0, SEEK_SET)) | 79 if (0 != fseek(file.get(), 0, SEEK_SET)) |
84 return false; | 80 return false; |
85 if (1u != fread(header, sizeof(header), 1, file.get())) | 81 if (1u != fread(header, sizeof(header), 1, file.get())) |
86 return false; | 82 return false; |
87 | 83 |
88 int64_t db_size = 0; | 84 int64_t db_size = 0; |
89 if (!base::GetFileSize(db_path, &db_size)) | 85 if (!base::GetFileSize(db_path, &db_size)) |
90 return false; | 86 return false; |
91 | 87 |
88 CorruptSizeInHeaderMemory(header, db_size); | |
89 | |
90 if (0 != fseek(file.get(), 0, SEEK_SET)) | |
91 return false; | |
92 if (1u != fwrite(header, sizeof(header), 1, file.get())) | |
93 return false; | |
94 | |
95 return true; | |
96 } | |
97 | |
98 void CorruptSizeInHeaderMemory(unsigned char* header, int64_t db_size) { | |
Scott Hess - ex-Googler
2015/06/17 19:32:59
Makes sense to me!
| |
99 const size_t kPageSizeOffset = 16; | |
100 const size_t kFileChangeCountOffset = 24; | |
101 const size_t kPageCountOffset = 28; | |
102 const size_t kVersionValidForOffset = 92; // duplicate kFileChangeCountOffset | |
103 | |
92 const unsigned page_size = ReadBigEndian(header + kPageSizeOffset, 2); | 104 const unsigned page_size = ReadBigEndian(header + kPageSizeOffset, 2); |
93 | 105 |
94 // One larger than the expected size. | 106 // One larger than the expected size. |
95 const unsigned page_count = | 107 const unsigned page_count = |
96 static_cast<unsigned>((db_size + page_size) / page_size); | 108 static_cast<unsigned>((db_size + page_size) / page_size); |
97 WriteBigEndian(page_count, header + kPageCountOffset, 4); | 109 WriteBigEndian(page_count, header + kPageCountOffset, 4); |
98 | 110 |
99 // Update change count so outstanding readers know the info changed. | 111 // Update change count so outstanding readers know the info changed. |
100 // Both spots must match for the page count to be considered valid. | 112 // Both spots must match for the page count to be considered valid. |
101 unsigned change_count = ReadBigEndian(header + kFileChangeCountOffset, 4); | 113 unsigned change_count = ReadBigEndian(header + kFileChangeCountOffset, 4); |
102 WriteBigEndian(change_count + 1, header + kFileChangeCountOffset, 4); | 114 WriteBigEndian(change_count + 1, header + kFileChangeCountOffset, 4); |
103 WriteBigEndian(change_count + 1, header + kVersionValidForOffset, 4); | 115 WriteBigEndian(change_count + 1, header + kVersionValidForOffset, 4); |
104 | |
105 if (0 != fseek(file.get(), 0, SEEK_SET)) | |
106 return false; | |
107 if (1u != fwrite(header, sizeof(header), 1, file.get())) | |
108 return false; | |
109 | |
110 return true; | |
111 } | 116 } |
112 | 117 |
113 bool CorruptTableOrIndex(const base::FilePath& db_path, | 118 bool CorruptTableOrIndex(const base::FilePath& db_path, |
114 const char* tree_name, | 119 const char* tree_name, |
115 const char* update_sql) { | 120 const char* update_sql) { |
116 sql::Connection db; | 121 sql::Connection db; |
117 if (!db.Open(db_path)) | 122 if (!db.Open(db_path)) |
118 return false; | 123 return false; |
119 | 124 |
120 int page_size = 0; | 125 int page_size = 0; |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 sql::Statement statement(db->GetUniqueStatement("PRAGMA integrity_check")); | 248 sql::Statement statement(db->GetUniqueStatement("PRAGMA integrity_check")); |
244 | 249 |
245 // SQLite should always return a row of data. | 250 // SQLite should always return a row of data. |
246 EXPECT_TRUE(statement.Step()); | 251 EXPECT_TRUE(statement.Step()); |
247 | 252 |
248 return statement.ColumnString(0); | 253 return statement.ColumnString(0); |
249 } | 254 } |
250 | 255 |
251 } // namespace test | 256 } // namespace test |
252 } // namespace sql | 257 } // namespace sql |
OLD | NEW |