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

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: iOS SQLite doesn't support column names in view definition. 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("TestShortcuts1.db"));
Mark P 2016/04/08 20:12:06 This probably should be "3", not "1". (Otherwise
Scott Hess - ex-Googler 2016/04/15 00:38:14 Copy/pasta. Since it's in a unique temp dir, I'll
315 ASSERT_TRUE(sql::test::CreateDatabaseFromSQL(db_path, sql_path));
316 ASSERT_TRUE(sql::test::CorruptSizeInHeader(db_path));
317
318 // The database is broken.
Mark P 2016/04/08 20:12:06 How about putting a blank line after 315, then add
Scott Hess - ex-Googler 2016/04/15 00:38:14 Done.
319 {
320 sql::ScopedErrorIgnorer ignore_errors;
321 ignore_errors.IgnoreError(SQLITE_CORRUPT);
322
323 sql::Connection connection;
324 ASSERT_TRUE(connection.Open(db_path));
325 ASSERT_FALSE(connection.Execute("SELECT count(*) FROM omni_box_shortcuts"));
Mark P 2016/04/08 20:12:06 Why does this return failure if we're ignoring the
Scott Hess - ex-Googler 2016/04/15 00:38:14 Production code in debug mode fails with a FATAL,
326
327 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
Mark P 2016/04/08 20:12:06 Side comment: the comment by the declaration of Ch
Scott Hess - ex-Googler 2016/04/15 00:38:14 I don't really follow. It says it's used to check
328 }
329
330 // sql::Connection::Open() will hit the corruption, the error callback will
Mark P 2016/04/08 20:12:06 sql::Connection::Open() will hit -> The sql::Conne
Scott Hess - ex-Googler 2016/04/15 00:38:14 Done.
331 // recover and poison the database, then Open() will retry, allowing Init() to
332 // succeed.
333 {
334 sql::ScopedErrorIgnorer ignore_errors;
335 ignore_errors.IgnoreError(SQLITE_CORRUPT);
336
337 scoped_refptr<ShortcutsDatabase> db(new ShortcutsDatabase(db_path));
338 ASSERT_TRUE(db->Init());
339
340 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
Mark P 2016/04/08 20:12:06 I think it's odd that we still see this error. Sh
Scott Hess - ex-Googler 2016/04/15 00:38:14 The error will be thrown in the first Open(), the
341 }
342
343 CheckV2ColumnExistence(db_path, true);
344
345 // All of the data should have been recovered.
346 sql::Connection connection;
347 ASSERT_TRUE(connection.Open(db_path));
348 sql::Statement statement(connection.GetUniqueStatement(
Mark P 2016/04/08 20:12:06 Please use an identical call to the one you used t
Scott Hess - ex-Googler 2016/04/15 00:38:14 Done.
349 "SELECT COUNT(*) FROM omni_box_shortcuts"));
350 ASSERT_TRUE(statement.Step());
351 EXPECT_EQ(5, statement.ColumnInt(0));
Mark P 2016/04/08 20:12:06 Consider using something like EXPECT_EQ(arraysize(
Scott Hess - ex-Googler 2016/04/15 00:38:14 CountRecords() runs against the sql::Connection co
352 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698