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

import("//content/test/fuzzer/content_mojolpm_fuzzer_test.gni")
import("//mojo/public/tools/bindings/mojom.gni")
import("//testing/libfuzzer/fuzzer_test.gni")

source_set("indexed_db") {
  public = [ "indexed_db_control_wrapper.h" ]

  sources = [
    "file_path_util.cc",
    "file_path_util.h",
    "file_stream_reader_to_data_pipe.cc",
    "file_stream_reader_to_data_pipe.h",
    "indexed_db_context_impl.cc",
    "indexed_db_context_impl.h",
    "indexed_db_control_wrapper.cc",
    "indexed_db_data_format_version.cc",
    "indexed_db_data_format_version.h",
    "indexed_db_data_loss_info.h",
    "indexed_db_database_error.cc",
    "indexed_db_database_error.h",
    "indexed_db_external_object.cc",
    "indexed_db_external_object.h",
    "indexed_db_external_object_storage.cc",
    "indexed_db_external_object_storage.h",
    "indexed_db_leveldb_coding.cc",
    "indexed_db_leveldb_coding.h",
    "indexed_db_reporting.cc",
    "indexed_db_reporting.h",
    "indexed_db_value.cc",
    "indexed_db_value.h",
    "instance/backing_store.h",
    "instance/backing_store_pre_close_task_queue.cc",
    "instance/backing_store_pre_close_task_queue.h",
    "instance/backing_store_util.cc",
    "instance/backing_store_util.h",
    "instance/blob_endpoint.h",
    "instance/blob_reader.cc",
    "instance/blob_reader.h",
    "instance/bucket_context.cc",
    "instance/bucket_context.h",
    "instance/callback_helpers.h",
    "instance/connection.cc",
    "instance/connection.h",
    "instance/connection_coordinator.cc",
    "instance/connection_coordinator.h",
    "instance/cursor.cc",
    "instance/cursor.h",
    "instance/database.cc",
    "instance/database.h",
    "instance/database_callbacks.cc",
    "instance/database_callbacks.h",
    "instance/index_writer.cc",
    "instance/index_writer.h",
    "instance/leveldb/active_blob_registry.cc",
    "instance/leveldb/active_blob_registry.h",
    "instance/leveldb/backing_store.cc",
    "instance/leveldb/backing_store.h",
    "instance/leveldb/compaction_task.cc",
    "instance/leveldb/compaction_task.h",
    "instance/leveldb/indexed_db_leveldb_operations.cc",
    "instance/leveldb/indexed_db_leveldb_operations.h",
    "instance/leveldb/tombstone_sweeper.cc",
    "instance/leveldb/tombstone_sweeper.h",
    "instance/lock_request_data.cc",
    "instance/lock_request_data.h",
    "instance/pending_connection.cc",
    "instance/pending_connection.h",
    "instance/record.cc",
    "instance/record.h",
    "instance/sqlite/active_blob_streamer.cc",
    "instance/sqlite/active_blob_streamer.h",
    "instance/sqlite/backing_store_cursor_impl.cc",
    "instance/sqlite/backing_store_cursor_impl.h",
    "instance/sqlite/backing_store_database_impl.cc",
    "instance/sqlite/backing_store_database_impl.h",
    "instance/sqlite/backing_store_impl.cc",
    "instance/sqlite/backing_store_impl.h",
    "instance/sqlite/backing_store_transaction_impl.cc",
    "instance/sqlite/backing_store_transaction_impl.h",
    "instance/sqlite/blob_writer.cc",
    "instance/sqlite/blob_writer.h",
    "instance/sqlite/database_connection.cc",
    "instance/sqlite/database_connection.h",
    "instance/transaction.cc",
    "instance/transaction.h",
    "mock_browsertest_indexed_db_class_factory.cc",
    "mock_browsertest_indexed_db_class_factory.h",
    "status.cc",
    "status.h",
  ]

  deps = [
    "//base",
    "//components/base32",
    "//components/services/storage",
    "//components/services/storage/public/cpp",
    "//components/services/storage/public/mojom",
    "//content:export",
    "//content/public/common:common_sources",
    "//mojo:buildflags",
    "//net",
    "//storage/browser",
    "//third_party/blink/public/common",
    "//third_party/leveldatabase",
    "//third_party/snappy",
    "//third_party/zlib/google:zip",
    "//third_party/zstd:decompress",
    "//v8:v8_version",
  ]

  if (!is_android && !is_fuchsia) {
    deps += [ "//third_party/zstd:compress" ]
  }

  configs += [ "//content:content_implementation" ]

  friend = [
    ":*",
    "//content/test/*",
  ]
}

source_set("indexed_db_fuzzer_support") {
  public_deps = [
    "//base",
    "//components/services/storage",
    "//content/browser/indexed_db",
    "//content/public/browser:browser_sources",
    "//content/test:test_support",
    "//third_party/blink/public/common:headers",
  ]
  testonly = true
}

fuzzer_test("indexed_db_leveldb_coding_encodeidbkey_fuzzer") {
  sources = [ "indexed_db_leveldb_coding_encodeidbkey_fuzzer.cc" ]
  deps = [ ":indexed_db_fuzzer_support" ]
  seed_corpus = "fuzzer_corpus/indexed_db_key"
}

fuzzer_test("indexed_db_leveldb_coding_encodeidbkeypath_fuzzer") {
  sources = [ "indexed_db_leveldb_coding_encodeidbkeypath_fuzzer.cc" ]
  deps = [ ":indexed_db_fuzzer_support" ]
  seed_corpus = "fuzzer_corpus/indexed_db_key_path"
}

fuzzer_test("indexed_db_leveldb_coding_decodeidbkey_fuzzer") {
  sources = [ "indexed_db_leveldb_coding_decodeidbkey_fuzzer.cc" ]
  deps = [ ":indexed_db_fuzzer_support" ]
  seed_corpus = "fuzzer_corpus/encoded_indexed_db_key"
}

fuzzer_test("indexed_db_leveldb_coding_decodeidbkeypath_fuzzer") {
  sources = [ "indexed_db_leveldb_coding_decodeidbkeypath_fuzzer.cc" ]
  deps = [ ":indexed_db_fuzzer_support" ]
  seed_corpus = "fuzzer_corpus/encoded_indexed_db_key_path"
}

content_mojolpm_fuzzer_test("idb_factory_mojolpm_fuzzer") {
  sources = [ "idb_factory_mojolpm_fuzzer.cc" ]

  proto_source = "idb_factory_mojolpm_fuzzer.proto"
  testcase_proto_kind = "content.fuzzing.idb_factory.proto.Testcase"

  deps = [
    ":indexed_db",
    "//content/browser:for_content_tests",
    "//content/public/browser:browser_sources",
    "//content/test/fuzzer:mojolpm_fuzzer_support",
    "//third_party/blink/public/mojom:mojom_modules_mojolpm",
  ]

  proto_deps = [ "//third_party/blink/public/mojom:mojom_modules_mojolpm" ]

  seed_corpus_sources = [
    "fuzzer_corpus/idb_factory_mojolpm_fuzzer/basic_blob.textproto",
    "fuzzer_corpus/idb_factory_mojolpm_fuzzer/basic_index_operations.textproto",
    "fuzzer_corpus/idb_factory_mojolpm_fuzzer/basic_put_get.textproto",
    "fuzzer_corpus/idb_factory_mojolpm_fuzzer/idb_factory.textproto",
    "fuzzer_corpus/idb_factory_mojolpm_fuzzer/index_cursor.textproto",
    "fuzzer_corpus/idb_factory_mojolpm_fuzzer/object_store_cursor.textproto",
  ]
}

source_set("unit_tests") {
  testonly = true

  # See content_unittests for justification.
  if (is_component_build) {
    check_includes = false
  }

  sources = [
    "file_path_util_unittest.cc",
    "file_stream_reader_to_data_pipe_unittest.cc",
    "indexed_db_context_unittest.cc",
    "indexed_db_leveldb_coding_unittest.cc",
    "indexed_db_quota_client_unittest.cc",
    "indexed_db_test_base.cc",
    "indexed_db_test_base.h",
    "indexed_db_unittest.cc",
    "instance/backing_store_pre_close_task_queue_unittest.cc",
    "instance/backing_store_test_base.cc",
    "instance/backing_store_test_base.h",
    "instance/backing_store_unittest.cc",
    "instance/bucket_context_unittest.cc",
    "instance/database_unittest.cc",
    "instance/fake_transaction.cc",
    "instance/fake_transaction.h",
    "instance/leveldb/active_blob_registry_unittest.cc",
    "instance/leveldb/cleanup_on_io_error_unittest.cc",
    "instance/leveldb/leveldb_backing_store_unittest.cc",
    "instance/leveldb/tombstone_sweeper_unittest.cc",
    "instance/mock_blob_storage_context.cc",
    "instance/mock_blob_storage_context.h",
    "instance/mock_file_system_access_context.cc",
    "instance/mock_file_system_access_context.h",
    "instance/sqlite/backing_store_sqlite_unittest.cc",
    "instance/sqlite/database_connection_unittest.cc",
    "instance/sqlite_backing_store_rollout_stage_unittest.cc",
    "instance/test_blob_consumer.cc",
    "instance/test_blob_consumer.h",
    "instance/transaction_unittest.cc",
    "mock_mojo_indexed_db_database_callbacks.cc",
    "mock_mojo_indexed_db_database_callbacks.h",
    "mock_mojo_indexed_db_factory_client.cc",
    "mock_mojo_indexed_db_factory_client.h",
  ]

  deps = [
    "//base",
    "//base/test:test_support",
    "//components/services/storage",
    "//components/services/storage:test_support",
    "//components/services/storage/privileged/mojom",
    "//components/services/storage/public/cpp",
    "//components/services/storage/public/mojom",
    "//content/public/common",

    # This dependency is here to satisfy `gn check` given this target's includes
    # of :indexed_db headers. This has to be done through //content/browser to
    # avoid doubling symbols.
    "//content/browser:for_content_tests",
    "//mojo/public/cpp/test_support:test_utils",
    "//sql:test_support",
    "//storage/browser",
    "//storage/browser:test_support",
    "//testing/gmock",
    "//testing/gtest",
    "//third_party/blink/public/common",
    "//third_party/fuzztest",
    "//third_party/leveldatabase",
  ]

  # TODO(crbug.com/40031409): Fix code that adds exit-time destructors and
  # enable the diagnostic by removing this line.
  configs += [ "//build/config/compiler:no_exit_time_destructors" ]
}

mojom("internals_mojo_bindings") {
  sources = [ "indexed_db_internals.mojom" ]
  public_deps = [
    "//components/services/storage/privileged/mojom",
    "//components/services/storage/public/mojom",
    "//mojo/public/mojom/base",
  ]
  webui_module_path = "/"
  generate_java = false
}
