Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: components/omnibox/browser/shortcuts_database_unittest.cc

Issue 1832173002: [sql] Database recovery system for Shortcuts. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments from #9 and #10. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/omnibox/browser/shortcuts_database.h" 5 #include "components/omnibox/browser/shortcuts_database.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/format_macros.h" 10 #include "base/format_macros.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/path_service.h" 12 #include "base/path_service.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "build/build_config.h" 16 #include "build/build_config.h"
17 #include "components/omnibox/browser/autocomplete_match_type.h" 17 #include "components/omnibox/browser/autocomplete_match_type.h"
18 #include "components/omnibox/browser/shortcuts_constants.h" 18 #include "components/omnibox/browser/shortcuts_constants.h"
19 #include "sql/statement.h" 19 #include "sql/statement.h"
20 #include "sql/test/scoped_error_ignorer.h"
20 #include "sql/test/test_helpers.h" 21 #include "sql/test/test_helpers.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/base/page_transition_types.h" 23 #include "ui/base/page_transition_types.h"
23 24
24 using base::ASCIIToUTF16; 25 using base::ASCIIToUTF16;
25 26
26 // Helpers -------------------------------------------------------------------- 27 // Helpers --------------------------------------------------------------------
27 28
28 namespace { 29 namespace {
29 30
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 sql::Statement statement(connection.GetUniqueStatement( 293 sql::Statement statement(connection.GetUniqueStatement(
293 "SELECT count(1) FROM omni_box_shortcuts WHERE type in (9, 10, 11, 12)")); 294 "SELECT count(1) FROM omni_box_shortcuts WHERE type in (9, 10, 11, 12)"));
294 ASSERT_TRUE(statement.is_valid()); 295 ASSERT_TRUE(statement.is_valid());
295 while (statement.Step()) 296 while (statement.Step())
296 EXPECT_EQ(0, statement.ColumnInt(0)); 297 EXPECT_EQ(0, statement.ColumnInt(0));
297 EXPECT_TRUE(statement.Succeeded()); 298 EXPECT_TRUE(statement.Succeeded());
298 #if !defined(OS_WIN) 299 #if !defined(OS_WIN)
299 EXPECT_TRUE(temp_dir.Delete()); 300 EXPECT_TRUE(temp_dir.Delete());
300 #endif 301 #endif
301 } 302 }
303
304 TEST(ShortcutsDatabaseMigrationTest, Recovery1) {
305 #if defined(OS_ANDROID)
306 char kBasename[] = "Shortcuts.v1.sql";
307 #else
308 char kBasename[] = "Shortcuts.no_fill_into_edit.sql";
309 #endif
310 // Use the pre-v0 test file to create a test database in a temp dir.
311 base::FilePath sql_path = GetTestDataDir().AppendASCII(kBasename);
312 base::ScopedTempDir temp_dir;
313 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
314 base::FilePath db_path(temp_dir.path().AppendASCII("TestShortcuts.db"));
Mark P 2016/04/19 23:34:28 If it doesn't matter that they're different, pleas
Scott Hess - ex-Googler 2016/05/13 21:24:35 Done.
315 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path));
316
317 // Break the database.
318 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path));
319
320 // Verify that the database is broken.
321 {
322 sql::ScopedErrorIgnorer ignore_errors;
323 ignore_errors.IgnoreError(SQLITE_CORRUPT);
324
325 sql::Connection connection;
326 ASSERT_TRUE(connection.Open(db_path));
327 sql::Statement statement(connection.GetUniqueStatement(
Mark P 2016/04/19 23:34:28 Thank you for the explanation. That said, my orig
Scott Hess - ex-Googler 2016/05/13 21:24:36 Done. Pulled up the statement sql, noted that the
328 "SELECT COUNT(*) FROM omni_box_shortcuts"));
329 ASSERT_FALSE(statement.is_valid());
330
331 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
Mark P 2016/04/19 23:34:28 I don't agree. I see at least two ways to underst
Scott Hess - ex-Googler 2016/05/13 21:24:36 Obviously there's a terminology problem, here, but
332 }
333
334 // The sql::Connection::Open() in ShortcutsDatabase::Init() will hit the
Mark P 2016/04/19 23:34:28 nit: in -> call in
Scott Hess - ex-Googler 2016/05/13 21:24:36 Done.
335 // corruption, the error callback will recover and poison the database, then
336 // Open() will retry, allowing Init() to succeed.
337 {
338 sql::ScopedErrorIgnorer ignore_errors;
339 ignore_errors.IgnoreError(SQLITE_CORRUPT);
340
341 scoped_refptr<ShortcutsDatabase> db(new ShortcutsDatabase(db_path));
342 ASSERT_TRUE(db->Init());
343
344 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
Mark P 2016/04/19 23:34:28 Given this, I don't like this test's structure. I
Scott Hess - ex-Googler 2016/05/13 21:24:36 I'm not sure what you're referring to with "clears
345 }
346
347 CheckV2ColumnExistence(db_path, true);
348
349 // All of the data should have been recovered.
350 {
351 sql::Connection connection;
352 ASSERT_TRUE(connection.Open(db_path));
353 sql::Statement statement(connection.GetUniqueStatement(
354 "SELECT COUNT(*) FROM omni_box_shortcuts"));
Mark P 2016/04/19 23:34:28 Almost done. I'd like exactly the same tests, i.e
Scott Hess - ex-Googler 2016/05/13 21:24:36 Done.
355 ASSERT_TRUE(statement.Step());
356 EXPECT_EQ(5, statement.ColumnInt(0));
Mark P 2016/04/19 23:34:27 I don't have a strong preference for how you fix i
Scott Hess - ex-Googler 2016/05/13 21:24:36 It's dependent on the contents of the golden file.
357 }
358 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698