1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "base/check.h"
10 #include "base/containers/span.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/test/bind.h"
14 #include "sql/database.h"
15 #include "sql/recovery.h"
16 #include "sql/statement.h"
20 // Does initialization and holds state that's shared across all runs.
24 CHECK(base::CreateTemporaryFile(&data_file_path_));
25 logging::SetMinLogLevel(logging::LOG_FATAL);
28 ~Environment() { base::DeleteFile(data_file_path_); }
30 const base::FilePath& data_file_path() const { return data_file_path_; }
33 base::FilePath data_file_path_;
38 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
39 static Environment env;
41 // Prepare fuzzed data file.
42 CHECK(base::WriteFile(env.data_file_path(), base::make_span(data, size)));
44 // Load database. Check there's no unrecoverable error.
45 sql::DatabaseOptions options;
46 sql::Database database(options);
47 bool should_attempt_recovery = false;
48 database.set_error_callback(
49 base::BindLambdaForTesting([&](int extended_error, sql::Statement*) {
50 should_attempt_recovery = sql::Recovery::ShouldRecover(extended_error);
52 std::ignore = database.Open(env.data_file_path());
55 if (should_attempt_recovery) {
56 database.reset_error_callback();
57 sql::Recovery::RecoverDatabase(&database, env.data_file_path());