chromium/sql/recovery_unittest.cc

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "sql/recovery.h"

#include <stddef.h>

#include <cstdint>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/path_service.h"
#include "base/ranges/algorithm.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/buildflag.h"
#include "sql/database.h"
#include "sql/meta_table.h"
#include "sql/sql_features.h"
#include "sql/sqlite_result_code.h"
#include "sql/sqlite_result_code_values.h"
#include "sql/statement.h"
#include "sql/test/scoped_error_expecter.h"
#include "sql/test/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/sqlite/sqlite3.h"

namespace sql {

namespace {

ExecuteWithResult;
ExecuteWithResults;

constexpr char kRecoveryResultHistogramName[] =;
constexpr char kRecoveryResultCodeHistogramName[] =;

// Dump consistent human-readable representation of the database
// schema.  For tables or indices, this will contain the sql command
// to create the table or index.  For certain automatic SQLite
// structures with no sql, the name is used.
std::string GetSchema(Database* db) {}

// Parameterized to test with and without WAL mode enabled.
class SqlRecoveryTest : public testing::Test,
                        public testing::WithParamInterface<bool> {};

#if BUILDFLAG(IS_FUCHSIA)
// WAL + recovery is not supported on Fuchsia, so only test without WAL mode.
INSTANTIATE_TEST_SUITE_P(All, SqlRecoveryTest, testing::Values(false));
#else
INSTANTIATE_TEST_SUITE_P();
#endif

TEST_P(SqlRecoveryTest, ShouldAttemptRecovery) {}

TEST_P(SqlRecoveryTest, RecoverCorruptIndex) {}

TEST_P(SqlRecoveryTest, RecoverCorruptTable) {}

TEST_P(SqlRecoveryTest, Meta) {}

// Baseline AutoRecoverTable() test.
TEST_P(SqlRecoveryTest, AutoRecoverTable) {}

// Test that default values correctly replace nulls.  The recovery
// virtual table reads directly from the database, so DEFAULT is not
// interpreted at that level.
TEST_P(SqlRecoveryTest, AutoRecoverTableWithDefault) {}

// Test AutoRecoverTable with a ROWID alias.
TEST_P(SqlRecoveryTest, AutoRecoverTableWithRowid) {}

void TestRecoverDatabase(Database& db,
                         const base::FilePath& db_path,
                         bool with_meta,
                         base::OnceClosure run_recovery) {}

TEST_P(SqlRecoveryTest, RecoverDatabase) {}

TEST_P(SqlRecoveryTest, RecoverDatabaseMeta) {}

TEST_P(SqlRecoveryTest, RecoverIfPossible) {}

TEST_P(SqlRecoveryTest, RecoverIfPossibleMeta) {}

TEST_P(SqlRecoveryTest, RecoverIfPossibleWithoutErrorCallback) {}

TEST_P(SqlRecoveryTest, RecoverIfPossibleWithErrorCallback) {}

TEST_P(SqlRecoveryTest, RecoverIfPossibleWithClosedDatabase) {}

TEST_P(SqlRecoveryTest, RecoverIfPossibleWithPerDatabaseUma) {}

TEST_P(SqlRecoveryTest, RecoverDatabaseWithView) {}

// When RecoverDatabase() encounters SQLITE_NOTADB, the database is deleted.
TEST_P(SqlRecoveryTest, RecoverDatabaseDelete) {}

// Allow callers to validate the database between recovery and commit.
TEST_P(SqlRecoveryTest, BeginRecoverDatabase) {}

TEST_P(SqlRecoveryTest, AttachFailure) {}

// Helper for SqlRecoveryTest.PageSize.  Creates a fresh db based on db_prefix,
// with the given initial page size, and verifies it against the expected size.
// Then changes to the final page size and recovers, verifying that the
// recovered database ends up with the expected final page size.
void TestPageSize(const base::FilePath& db_prefix,
                  int initial_page_size,
                  const std::string& expected_initial_page_size,
                  int final_page_size,
                  const std::string& expected_final_page_size) {}

// Verify that Recovery maintains the page size, and the virtual table
// works with page sizes other than SQLite's default.  Also verify the case
// where the default page size has changed.
TEST_P(SqlRecoveryTest, PageSize) {}

TEST_P(SqlRecoveryTest, CannotRecoverClosedDb) {}

TEST_P(SqlRecoveryTest, CannotRecoverDbWithErrorCallback) {}

// TODO(crbug.com/40199997): Ideally this would be a
// `SqlRecoveryTest`, but `Recovery::RecoverDatabase()` does not DCHECK
// that it is passed a non-null database pointer and will instead likely result
// in unexpected behavior or crashes.
TEST_P(SqlRecoveryTest, CannotRecoverNullDb) {}

// TODO(crbug.com/40199997): Ideally this would be a
// `SqlRecoveryTest`, but `Recovery::RecoverDatabase()` does not DCHECK
// whether the database is in-memory and will instead likely result in
// unexpected behavior or crashes.
TEST_P(SqlRecoveryTest, CannotRecoverInMemoryDb) {}

// This test mimics the case where a database that was using WAL mode crashed,
// then next Chrome launch the database is not opened in WAL mode. This may
// occur when e.g. WAL mode if configured via Finch and the user not in the
// experiment group on the second launch of Chrome.
TEST_P(SqlRecoveryTest, PRE_RecoverFormerlyWalDbAfterCrash) {}

TEST_P(SqlRecoveryTest, RecoverFormerlyWalDbAfterCrash) {}

}  // namespace

}  // namespace sql