OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "components/history/core/browser/history_types.h" | 12 #include "components/history/core/browser/history_types.h" |
13 #include "components/history/core/browser/top_sites_database.h" | 13 #include "components/history/core/browser/top_sites_database.h" |
14 #include "components/history/core/test/database_test_utils.h" | 14 #include "components/history/core/test/database_test_utils.h" |
15 #include "components/history/core/test/thumbnail-inl.h" | 15 #include "components/history/core/test/thumbnail-inl.h" |
16 #include "sql/connection.h" | 16 #include "sql/connection.h" |
17 #include "sql/recovery.h" | 17 #include "sql/recovery.h" |
18 #include "sql/test/scoped_error_ignorer.h" | 18 #include "sql/test/scoped_error_expecter.h" |
19 #include "sql/test/test_helpers.h" | 19 #include "sql/test/test_helpers.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "third_party/sqlite/sqlite3.h" | 21 #include "third_party/sqlite/sqlite3.h" |
22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 // URL with url_rank 0 in golden files. | 26 // URL with url_rank 0 in golden files. |
27 const GURL kUrl0 = GURL("http://www.google.com/"); | 27 const GURL kUrl0 = GURL("http://www.google.com/"); |
28 | 28 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 return; | 148 return; |
149 | 149 |
150 // Create an example database. | 150 // Create an example database. |
151 EXPECT_TRUE(CreateDatabaseFromSQL(file_name_, "TopSites.v1.sql")); | 151 EXPECT_TRUE(CreateDatabaseFromSQL(file_name_, "TopSites.v1.sql")); |
152 | 152 |
153 // Corrupt the database by adjusting the header size. | 153 // Corrupt the database by adjusting the header size. |
154 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); | 154 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); |
155 | 155 |
156 // Database is unusable at the SQLite level. | 156 // Database is unusable at the SQLite level. |
157 { | 157 { |
158 sql::ScopedErrorIgnorer ignore_errors; | 158 sql::test::ScopedErrorExpecter expecter; |
159 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 159 expecter.ExpectError(SQLITE_CORRUPT); |
160 sql::Connection raw_db; | 160 sql::Connection raw_db; |
161 EXPECT_TRUE(raw_db.Open(file_name_)); | 161 EXPECT_TRUE(raw_db.Open(file_name_)); |
162 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); | 162 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); |
163 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 163 ASSERT_TRUE(expecter.SawExpectedErrors()); |
164 } | 164 } |
165 | 165 |
166 // Corruption should be detected and recovered during Init(). | 166 // Corruption should be detected and recovered during Init(). |
167 { | 167 { |
168 sql::ScopedErrorIgnorer ignore_errors; | 168 sql::test::ScopedErrorExpecter expecter; |
169 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 169 expecter.ExpectError(SQLITE_CORRUPT); |
170 | 170 |
171 TopSitesDatabase db; | 171 TopSitesDatabase db; |
172 ASSERT_TRUE(db.Init(file_name_)); | 172 ASSERT_TRUE(db.Init(file_name_)); |
173 VerifyTablesAndColumns(db.db_.get()); | 173 VerifyTablesAndColumns(db.db_.get()); |
174 VerifyDatabaseEmpty(db.db_.get()); | 174 VerifyDatabaseEmpty(db.db_.get()); |
175 | 175 |
176 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 176 ASSERT_TRUE(expecter.SawExpectedErrors()); |
177 } | 177 } |
178 } | 178 } |
179 | 179 |
180 TEST_F(TopSitesDatabaseTest, Recovery2) { | 180 TEST_F(TopSitesDatabaseTest, Recovery2) { |
181 // Recovery module only supports some platforms at this time. | 181 // Recovery module only supports some platforms at this time. |
182 if (!sql::Recovery::FullRecoverySupported()) | 182 if (!sql::Recovery::FullRecoverySupported()) |
183 return; | 183 return; |
184 | 184 |
185 // Create an example database. | 185 // Create an example database. |
186 EXPECT_TRUE(CreateDatabaseFromSQL(file_name_, "TopSites.v2.sql")); | 186 EXPECT_TRUE(CreateDatabaseFromSQL(file_name_, "TopSites.v2.sql")); |
187 | 187 |
188 // Corrupt the database by adjusting the header. | 188 // Corrupt the database by adjusting the header. |
189 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); | 189 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); |
190 | 190 |
191 // Database is unusable at the SQLite level. | 191 // Database is unusable at the SQLite level. |
192 { | 192 { |
193 sql::ScopedErrorIgnorer ignore_errors; | 193 sql::test::ScopedErrorExpecter expecter; |
194 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 194 expecter.ExpectError(SQLITE_CORRUPT); |
195 sql::Connection raw_db; | 195 sql::Connection raw_db; |
196 EXPECT_TRUE(raw_db.Open(file_name_)); | 196 EXPECT_TRUE(raw_db.Open(file_name_)); |
197 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); | 197 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); |
198 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 198 ASSERT_TRUE(expecter.SawExpectedErrors()); |
199 } | 199 } |
200 | 200 |
201 // Corruption should be detected and recovered during Init(). After recovery, | 201 // Corruption should be detected and recovered during Init(). After recovery, |
202 // the Version2 checks should work. | 202 // the Version2 checks should work. |
203 { | 203 { |
204 sql::ScopedErrorIgnorer ignore_errors; | 204 sql::test::ScopedErrorExpecter expecter; |
205 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 205 expecter.ExpectError(SQLITE_CORRUPT); |
206 | 206 |
207 TopSitesDatabase db; | 207 TopSitesDatabase db; |
208 ASSERT_TRUE(db.Init(file_name_)); | 208 ASSERT_TRUE(db.Init(file_name_)); |
209 | 209 |
210 VerifyTablesAndColumns(db.db_.get()); | 210 VerifyTablesAndColumns(db.db_.get()); |
211 | 211 |
212 // Basic operational check. | 212 // Basic operational check. |
213 MostVisitedURLList urls; | 213 MostVisitedURLList urls; |
214 std::map<GURL, Images> thumbnails; | 214 std::map<GURL, Images> thumbnails; |
215 db.GetPageThumbnails(&urls, &thumbnails); | 215 db.GetPageThumbnails(&urls, &thumbnails); |
216 ASSERT_EQ(3u, urls.size()); | 216 ASSERT_EQ(3u, urls.size()); |
217 ASSERT_EQ(3u, thumbnails.size()); | 217 ASSERT_EQ(3u, thumbnails.size()); |
218 EXPECT_EQ(kUrl0, urls[0].url); // [0] because of url_rank. | 218 EXPECT_EQ(kUrl0, urls[0].url); // [0] because of url_rank. |
219 // kGoogleThumbnail includes nul terminator. | 219 // kGoogleThumbnail includes nul terminator. |
220 ASSERT_EQ(sizeof(kGoogleThumbnail) - 1, | 220 ASSERT_EQ(sizeof(kGoogleThumbnail) - 1, |
221 thumbnails[urls[0].url].thumbnail->size()); | 221 thumbnails[urls[0].url].thumbnail->size()); |
222 EXPECT_TRUE(!memcmp(thumbnails[urls[0].url].thumbnail->front(), | 222 EXPECT_TRUE(!memcmp(thumbnails[urls[0].url].thumbnail->front(), |
223 kGoogleThumbnail, sizeof(kGoogleThumbnail) - 1)); | 223 kGoogleThumbnail, sizeof(kGoogleThumbnail) - 1)); |
224 | 224 |
225 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 225 ASSERT_TRUE(expecter.SawExpectedErrors()); |
226 } | 226 } |
227 } | 227 } |
228 | 228 |
229 TEST_F(TopSitesDatabaseTest, Recovery3) { | 229 TEST_F(TopSitesDatabaseTest, Recovery3) { |
230 // Recovery module only supports some platforms at this time. | 230 // Recovery module only supports some platforms at this time. |
231 if (!sql::Recovery::FullRecoverySupported()) | 231 if (!sql::Recovery::FullRecoverySupported()) |
232 return; | 232 return; |
233 | 233 |
234 // Create an example database. | 234 // Create an example database. |
235 EXPECT_TRUE(CreateDatabaseFromSQL(file_name_, "TopSites.v3.sql")); | 235 EXPECT_TRUE(CreateDatabaseFromSQL(file_name_, "TopSites.v3.sql")); |
236 | 236 |
237 // Corrupt the database by adjusting the header. | 237 // Corrupt the database by adjusting the header. |
238 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); | 238 EXPECT_TRUE(sql::test::CorruptSizeInHeader(file_name_)); |
239 | 239 |
240 // Database is unusable at the SQLite level. | 240 // Database is unusable at the SQLite level. |
241 { | 241 { |
242 sql::ScopedErrorIgnorer ignore_errors; | 242 sql::test::ScopedErrorExpecter expecter; |
243 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 243 expecter.ExpectError(SQLITE_CORRUPT); |
244 sql::Connection raw_db; | 244 sql::Connection raw_db; |
245 EXPECT_TRUE(raw_db.Open(file_name_)); | 245 EXPECT_TRUE(raw_db.Open(file_name_)); |
246 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); | 246 EXPECT_FALSE(raw_db.IsSQLValid("PRAGMA integrity_check")); |
247 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 247 ASSERT_TRUE(expecter.SawExpectedErrors()); |
248 } | 248 } |
249 | 249 |
250 // Corruption should be detected and recovered during Init(). | 250 // Corruption should be detected and recovered during Init(). |
251 { | 251 { |
252 sql::ScopedErrorIgnorer ignore_errors; | 252 sql::test::ScopedErrorExpecter expecter; |
253 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 253 expecter.ExpectError(SQLITE_CORRUPT); |
254 | 254 |
255 TopSitesDatabase db; | 255 TopSitesDatabase db; |
256 ASSERT_TRUE(db.Init(file_name_)); | 256 ASSERT_TRUE(db.Init(file_name_)); |
257 | 257 |
258 MostVisitedURLList urls; | 258 MostVisitedURLList urls; |
259 std::map<GURL, Images> thumbnails; | 259 std::map<GURL, Images> thumbnails; |
260 db.GetPageThumbnails(&urls, &thumbnails); | 260 db.GetPageThumbnails(&urls, &thumbnails); |
261 ASSERT_EQ(3u, urls.size()); | 261 ASSERT_EQ(3u, urls.size()); |
262 ASSERT_EQ(3u, thumbnails.size()); | 262 ASSERT_EQ(3u, thumbnails.size()); |
263 EXPECT_EQ(kUrl0, urls[0].url); // [0] because of url_rank. | 263 EXPECT_EQ(kUrl0, urls[0].url); // [0] because of url_rank. |
264 // kGoogleThumbnail includes nul terminator. | 264 // kGoogleThumbnail includes nul terminator. |
265 ASSERT_EQ(sizeof(kGoogleThumbnail) - 1, | 265 ASSERT_EQ(sizeof(kGoogleThumbnail) - 1, |
266 thumbnails[urls[0].url].thumbnail->size()); | 266 thumbnails[urls[0].url].thumbnail->size()); |
267 EXPECT_TRUE(!memcmp(thumbnails[urls[0].url].thumbnail->front(), | 267 EXPECT_TRUE(!memcmp(thumbnails[urls[0].url].thumbnail->front(), |
268 kGoogleThumbnail, sizeof(kGoogleThumbnail) - 1)); | 268 kGoogleThumbnail, sizeof(kGoogleThumbnail) - 1)); |
269 | 269 |
270 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 270 ASSERT_TRUE(expecter.SawExpectedErrors()); |
271 } | 271 } |
272 | 272 |
273 // Double-check database integrity. | 273 // Double-check database integrity. |
274 { | 274 { |
275 sql::Connection raw_db; | 275 sql::Connection raw_db; |
276 EXPECT_TRUE(raw_db.Open(file_name_)); | 276 EXPECT_TRUE(raw_db.Open(file_name_)); |
277 ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db)); | 277 ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db)); |
278 } | 278 } |
279 | 279 |
280 // Corrupt the thumnails.url auto-index by deleting an element from the table | 280 // Corrupt the thumnails.url auto-index by deleting an element from the table |
(...skipping 14 matching lines...) Expand all Loading... |
295 EXPECT_TRUE(raw_db.Open(file_name_)); | 295 EXPECT_TRUE(raw_db.Open(file_name_)); |
296 ASSERT_NE("ok", sql::test::IntegrityCheck(&raw_db)); | 296 ASSERT_NE("ok", sql::test::IntegrityCheck(&raw_db)); |
297 } | 297 } |
298 | 298 |
299 // Open the database and access the corrupt index. | 299 // Open the database and access the corrupt index. |
300 { | 300 { |
301 TopSitesDatabase db; | 301 TopSitesDatabase db; |
302 ASSERT_TRUE(db.Init(file_name_)); | 302 ASSERT_TRUE(db.Init(file_name_)); |
303 | 303 |
304 { | 304 { |
305 sql::ScopedErrorIgnorer ignore_errors; | 305 sql::test::ScopedErrorExpecter expecter; |
306 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 306 expecter.ExpectError(SQLITE_CORRUPT); |
307 | 307 |
308 // Data for kUrl1 was deleted, but the index entry remains, this will | 308 // Data for kUrl1 was deleted, but the index entry remains, this will |
309 // throw SQLITE_CORRUPT. The corruption handler will recover the database | 309 // throw SQLITE_CORRUPT. The corruption handler will recover the database |
310 // and poison the handle, so the outer call fails. | 310 // and poison the handle, so the outer call fails. |
311 EXPECT_EQ(TopSitesDatabase::kRankOfNonExistingURL, | 311 EXPECT_EQ(TopSitesDatabase::kRankOfNonExistingURL, |
312 db.GetURLRank(MostVisitedURL(kUrl1, base::string16()))); | 312 db.GetURLRank(MostVisitedURL(kUrl1, base::string16()))); |
313 | 313 |
314 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 314 ASSERT_TRUE(expecter.SawExpectedErrors()); |
315 } | 315 } |
316 } | 316 } |
317 | 317 |
318 // Check that the database is recovered at the SQLite level. | 318 // Check that the database is recovered at the SQLite level. |
319 { | 319 { |
320 sql::Connection raw_db; | 320 sql::Connection raw_db; |
321 EXPECT_TRUE(raw_db.Open(file_name_)); | 321 EXPECT_TRUE(raw_db.Open(file_name_)); |
322 ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db)); | 322 ASSERT_EQ("ok", sql::test::IntegrityCheck(&raw_db)); |
323 } | 323 } |
324 | 324 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 db.RemoveURL(url2); | 460 db.RemoveURL(url2); |
461 | 461 |
462 db.GetPageThumbnails(&urls, &thumbnails); | 462 db.GetPageThumbnails(&urls, &thumbnails); |
463 ASSERT_EQ(4u, urls.size()); | 463 ASSERT_EQ(4u, urls.size()); |
464 ASSERT_EQ(4u, thumbnails.size()); | 464 ASSERT_EQ(4u, thumbnails.size()); |
465 EXPECT_EQ(mapsUrl, urls[0].url); | 465 EXPECT_EQ(mapsUrl, urls[0].url); |
466 EXPECT_EQ(kUrl0, urls[1].url); | 466 EXPECT_EQ(kUrl0, urls[1].url); |
467 } | 467 } |
468 | 468 |
469 } // namespace history | 469 } // namespace history |
OLD | NEW |