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

import("//build/config/features.gni")
import("//build/config/sanitizers/sanitizers.gni")
import("//testing/libfuzzer/fuzzer_test.gni")
import("//testing/test.gni")

group("tests") {
  testonly = true
  deps = [ ":fuzzing_unittests" ]
}

# Basic smoke tests for fuzzing
test("fuzzing_unittests") {
  # The most basic smoketest for FUZZ_TEST macros.
  # The main purpose is to ensure that this builds in all configurations:
  # * on regular builders, this will produce a unit test executable
  # * on ASAN builders, this will produce a unit test executable which
  #   additionally has sancov instrumentation to support --fuzz=
  #   arguments
  # * on libfuzzer and centipede builders this will enable different
  #   options again.
  sources = [ "fuzztest_smoketest.cc" ]
  fuzztests = [ "FuzzTestSmokeTest.StringsAlwaysOccupyPositiveSpace" ]

  deps = [
    "//base/test:run_all_unittests",
    "//base/test:test_support",
    "//testing/gtest",
    "//third_party/fuzztest",
  ]
  data_deps = []
  data = []
  defines = []

  if (use_fuzzing_engine) {
    sources += [
      "fuzz_target.cc",
      "fuzz_target.h",
      "fuzzer_smoke_test.cc",
      "stacktrace_test.cc",
    ]

    deps += [
      "//base",
      "//testing/gmock",
      "//testing/libfuzzer:buildflags",
    ]

    data_deps += [
      ":stacktrace_test_fuzzer",
      "//testing/libfuzzer/fuzzers:empty_fuzzer",
      "//testing/libfuzzer/fuzzers:string_compare_fuzzer",
      "//testing/libfuzzer/fuzzers:string_compare_proto_fuzzer",
    ]

    # The __generate targets (the wrappers) are only generated on Linux, Mac,
    # and Windows (see _building_fuzztest_fuzzer in testing/test.gni).
    if (use_fuzztest_wrapper && (is_linux || is_mac || is_win)) {
      data_deps += [ ":stub_fuzztest_StubFuzzer_Stub_fuzzer__generate" ]
      defines += [ "USING_FUZZTEST_WRAPPER" ]
    }

    # TODO(crbug.com/526667982): Fix when Mac Arm64 builds are fixed.
    # The fuzzer depends on //content/public/browser, which is not supported in
    # builds where use_blink is false (such as standard iOS/Catalyst).
    if (!(is_mac && current_cpu == "arm64") && use_blink) {
      data_deps += [ "//testing/libfuzzer/fuzzers:lpm_empty_fuzzer" ]
      defines += [ "BUILD_LPM_EMPTY_FUZZER" ]
    }

    data += [
      "data/check",
      "data/uaf",
    ]

    # TODO(crbug.com/40603084): Get these tests working on Windows.
    if (!is_win) {
      sources += [ "fuzzer_launcher_test.cc" ]
      deps += [ "//testing/libfuzzer:buildflags" ]
      data_deps += [
        ":check_fuzzer_config",
        ":check_seed_corpus_archive",
        ":test_config_and_dict",
        ":test_config_and_seed_corpus",
        ":test_config_and_seed_corpuses",
        ":test_config_only",
        ":test_dict_from_subdir",
        ":test_dict_only",
      ]
    }
  }
}

# These fuzzer targets' outputs serve as inputs to `fuzzer_launcher_test.cc`,
# which checks that `.options`, `.dict` and corpus files are generated
# correctly. Only compile them when compiling the test itself.
if (use_fuzzing_engine && !is_win) {
  fuzzer_test("test_dict_only") {
    sources = [ "../fuzzers/empty_fuzzer.cc" ]
    dict = "test.dict"
    additional_configs = [ "//testing/libfuzzer:no_clusterfuzz" ]
  }

  fuzzer_test("test_config_only") {
    sources = [ "../fuzzers/empty_fuzzer.cc" ]
    libfuzzer_options = [
      "some_test_option=test_value",
      "max_len=1024",
    ]
    additional_configs = [ "//testing/libfuzzer:no_clusterfuzz" ]
  }

  fuzzer_test("test_config_and_dict") {
    sources = [ "../fuzzers/empty_fuzzer.cc" ]
    dict = "test.dict"
    libfuzzer_options = [
      "max_len=random(1337, 31337)",
      "timeout   =   666",
      "use_traces=1",
    ]
    additional_configs = [ "//testing/libfuzzer:no_clusterfuzz" ]
  }

  fuzzer_test("test_config_and_seed_corpus") {
    sources = [ "../fuzzers/empty_fuzzer.cc" ]
    seed_corpus = "test_corpus"
    libfuzzer_options = [
      "some_test_option=test_value",
      "max_len=1024",
    ]
    additional_configs = [ "//testing/libfuzzer:no_clusterfuzz" ]
  }

  fuzzer_test("test_config_and_seed_corpuses") {
    sources = [ "../fuzzers/empty_fuzzer.cc" ]
    seed_corpuses = [
      "test_corpus",
      "test_corpus_2",
    ]
    libfuzzer_options = [
      "some_test_option=another_test_value",
      "max_len=1337",
    ]
    additional_configs = [ "//testing/libfuzzer:no_clusterfuzz" ]
  }

  fuzzer_test("test_dict_from_subdir") {
    sources = [ "../fuzzers/empty_fuzzer.cc" ]
    dict = "dicts_subdir/test_subdir.dict"
    additional_configs = [ "//testing/libfuzzer:no_clusterfuzz" ]
  }

  copy("check_fuzzer_config") {
    sources = [ "check_fuzzer_config.py" ]
    outputs = [ "$root_build_dir/check_fuzzer_config.py" ]
  }

  copy("check_seed_corpus_archive") {
    sources = [ "check_seed_corpus_archive.py" ]
    outputs = [ "$root_build_dir/check_seed_corpus_archive.py" ]
  }
}

fuzzer_test("stacktrace_test_fuzzer") {
  sources = [ "stacktrace_test_fuzzer.cc" ]
  deps = [
    "//base",
    "//testing/libfuzzer:libfuzzer_base_wrappers",
  ]
  additional_configs = [ "//testing/libfuzzer:no_clusterfuzz" ]

  # TODO(crbug.com/509668952): Remove this when FuzzTest is supported by tests.
  is_fuzztest_compatible = false
}

# A stub fuzztest target used by
# FuzzerSmokeTest.WrapperDoesNotLeakChildOnSIGTERM to verify that the fuzztest
# wrapper does not leak child processes on SIGTERM.
test("stub_fuzztest") {
  sources = [ "stub_fuzztest.cc" ]
  fuzztests = [ "StubFuzzer.Stub" ]
  deps = [ "//third_party/fuzztest:fuzztest_gtest_main" ]
  configs += [ "//testing/libfuzzer:no_clusterfuzz" ]
}
