chromium/components/media_router/common/providers/cast/channel/BUILD.gn

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

import("//testing/libfuzzer/fuzzer_test.gni")
import("//third_party/libprotobuf-mutator/fuzzable_proto_library.gni")
import("//third_party/openscreen/src/build_overrides/data_headers_template.gni")
import("//third_party/protobuf/proto_library.gni")

source_set("capability") {
  public = [ "cast_device_capability.h" ]
  deps = [ "//base" ]
}

static_library("channel") {
  sources = [
    "cast_auth_util.cc",
    "cast_auth_util.h",
    "cast_channel_enum.cc",
    "cast_channel_enum.h",
    "cast_channel_metrics.cc",
    "cast_channel_metrics.h",
    "cast_framer.cc",
    "cast_framer.h",
    "cast_message_handler.cc",
    "cast_message_handler.h",
    "cast_message_util.cc",
    "cast_message_util.h",
    "cast_socket.cc",
    "cast_socket.h",
    "cast_socket_service.cc",
    "cast_socket_service.h",
    "cast_transport.cc",
    "cast_transport.h",
    "enum_table.cc",
    "enum_table.h",
    "keep_alive_delegate.cc",
    "keep_alive_delegate.h",
    "keep_alive_handler.cc",
    "keep_alive_handler.h",
    "logger.cc",
    "logger.h",
    "mojo_data_pump.cc",
    "mojo_data_pump.h",
  ]
  deps = [
    ":capability",
    "//base",
    "//build:chromeos_buildflags",
    "//components/keyed_service/content",
    "//components/keyed_service/core",
    "//components/media_router/common/providers/cast/certificate",
    "//components/media_router/common/providers/cast/certificate:openscreen_certificate_verifier",
    "//components/openscreen_platform",
    "//components/prefs",
    "//components/version_info",
    "//content/public/browser",
    "//crypto",
    "//net",
    "//services/data_decoder/public/cpp",
    "//third_party/openscreen/src/cast/common/channel/proto:channel_proto",
  ]
  public_deps = [
    "//services/data_decoder/public/cpp",
    "//services/network/public/cpp",
    "//services/network/public/mojom",
  ]
  configs += [ "//build/config/compiler:wexit_time_destructors" ]
}

static_library("test_support") {
  testonly = true
  sources = [
    "cast_test_util.cc",
    "cast_test_util.h",
  ]
  deps = [
    ":channel",
    "//base",
    "//net",
    "//net:test_support",
    "//services/service_manager/public/cpp",
    "//testing/gmock",
    "//testing/gtest",
    "//third_party/openscreen/src/cast/common/channel/proto:channel_proto",
  ]
}

source_set("unit_tests") {
  testonly = true
  sources = [
    "cast_auth_util_unittest.cc",
    "cast_framer_unittest.cc",
    "cast_message_handler_unittest.cc",
    "cast_message_util_unittest.cc",
    "cast_socket_service_unittest.cc",
    "cast_socket_unittest.cc",
    "cast_transport_unittest.cc",
    "enum_table_unittest.cc",
    "keep_alive_delegate_unittest.cc",
    "logger_unittest.cc",
  ]
  deps = [
    ":channel",
    ":test_support",
    "//base/test:test_support",
    "//components/media_router/common/providers/cast/certificate",
    "//components/media_router/common/providers/cast/certificate:certificate_reader",
    "//components/media_router/common/providers/cast/certificate:test_support",
    "//components/prefs",
    "//components/prefs:test_support",
    "//content/test:test_support",
    "//net:test_support",
    "//services/data_decoder/public/cpp",
    "//services/data_decoder/public/cpp:test_support",
    "//testing/gmock",
    "//testing/gtest",
    "//third_party/openscreen/src/cast/common/certificate/proto:certificate_unittest_proto",
    "//third_party/openscreen/src/cast/common/channel/proto:channel_proto",
  ]
}

# TODO(issuetracker.google.com/185815206): Fuzzers should also work with
# openscreen code.
if (use_fuzzing_engine) {
  source_set("cast_auth_util_fuzzer_shared") {
    sources = [
      "cast_auth_util_fuzzer_shared.cc",
      "cast_auth_util_fuzzer_shared.h",
    ]
    public_deps = [ ":channel_fuzzer_inputs" ]
    deps = [
      "//third_party/openscreen/src/cast/common/channel/proto:channel_proto",
    ]
  }

  data_headers("cast_auth_util_fuzzer_certs") {
    namespace = "cast"
    sources = [ "//components/test/data/media_router/common/providers/cast/certificate/certificates/chromecast_gen1.pem" ]
  }

  fuzzable_proto_library("channel_fuzzer_inputs") {
    sources = [ "proto/fuzzer_inputs.proto" ]
    import_dirs = [
      "//third_party/openscreen/src/cast/common/certificate/proto",
      "//third_party/openscreen/src/cast/common/channel/proto",
    ]
    link_deps = [
      "//third_party/openscreen/src/cast/common/certificate/proto:certificate_proto",
      "//third_party/openscreen/src/cast/common/channel/proto:channel_proto",
    ]
    proto_out_dir =
        "components/media_router/common/providers/cast/channel/fuzz_proto"
  }

  protoc_convert("cast_auth_util_fuzzer_convert_corpus") {
    sources = [
      "test/data/error.textproto",
      "test/data/good.textproto",
    ]

    inputs = [ "proto/fuzzer_inputs.proto" ]

    output_pattern = "$target_gen_dir/cast_auth_util_fuzzer_corpus/{{source_name_part}}.binarypb"

    args = [
      "--encode=cast_channel.fuzz.CastAuthUtilInputs",
      "-I",
      rebase_path("//third_party/openscreen/src/cast/common/channel/proto"),
      "-I",
      rebase_path("//third_party/openscreen/src/cast/common/certificate/proto"),
      "-I",
      rebase_path("proto"),
      "fuzzer_inputs.proto",
    ]
  }
}

fuzzer_test("cast_auth_util_fuzzer") {
  sources = [ "cast_auth_util_fuzzer.cc" ]
  deps = [
    ":cast_auth_util_fuzzer_certs",
    ":cast_auth_util_fuzzer_shared",
    ":channel",
    ":channel_fuzzer_inputs",
    "//components/media_router/common/providers/cast/certificate",
    "//components/media_router/common/providers/cast/certificate:certificate_reader",
    "//components/media_router/common/providers/cast/certificate:test_support",
    "//net:test_support",
    "//net/data/ssl/certificates:generate_fuzzer_cert_includes",
    "//third_party/libprotobuf-mutator",
    "//third_party/openscreen/src/cast/common/certificate/proto:certificate_proto",
  ]
  seed_corpus = "$target_gen_dir/cast_auth_util_fuzzer_corpus"
  seed_corpus_deps = [ ":cast_auth_util_fuzzer_convert_corpus" ]
}

# TODO(crbug.com/1291729): Rename target to cast_framer_ingest_fuzzer.  The name
# is left unchanged for now to avoid the need to get reviews for
# various files that include it.
fuzzer_test("cast_message_fuzzer") {
  sources = [ "cast_framer_ingest_fuzzer.cc" ]
  deps = [
    ":test_support",
    "//base",
    "//components/media_router/common/providers/cast/channel",
    "//net",
    "//third_party/openscreen/src/cast/common/channel/proto:channel_proto",
  ]
  seed_corpus = "fuzz_corpus"

  # See MessageFramer::MessageHeader::max_message_size()
  libfuzzer_options = [ "max_len=65535" ]
}

fuzzer_test("cast_framer_serialize_fuzzer") {
  sources = [ "cast_framer_serialize_fuzzer.cc" ]
  deps = [
    ":channel",
    ":channel_fuzzer_inputs",
    "//third_party/libprotobuf-mutator",
    "//third_party/openscreen/src/cast/common/channel/proto:channel_proto",
  ]
}

fuzzer_test("cast_message_util_fuzzer") {
  sources = [ "cast_message_util_fuzzer.cc" ]
  dict = "fuzz.dict"
  deps = [
    ":channel",
    ":channel_fuzzer_inputs",
    "//third_party/libprotobuf-mutator",
    "//third_party/openscreen/src/cast/common/channel/proto:channel_proto",
  ]
}

fuzzer_test("openscreen_cast_auth_util_fuzzer") {
  sources = [ "openscreen_cast_auth_util_fuzzer.cc" ]
  deps = [
    ":cast_auth_util_fuzzer_certs",
    ":cast_auth_util_fuzzer_shared",
    ":channel_fuzzer_inputs",
    "//base",
    "//components/media_router/common/providers/cast/certificate:certificate_reader",
    "//components/openscreen_platform",
    "//net/data/ssl/certificates:generate_fuzzer_cert_includes",
    "//third_party/boringssl",
    "//third_party/libprotobuf-mutator",
    "//third_party/openscreen/src/cast/common:certificate_boringssl",
    "//third_party/openscreen/src/cast/common/certificate/proto:certificate_proto",
    "//third_party/openscreen/src/cast/sender:channel",
  ]
  seed_corpus = "$target_gen_dir/cast_auth_util_fuzzer_corpus"
  seed_corpus_deps = [ ":cast_auth_util_fuzzer_convert_corpus" ]
}

fuzzer_test("openscreen_message_framer_deserialize_fuzzer") {
  sources = [ "openscreen_message_framer_deserialize_fuzzer.cc" ]
  deps = [
    "//components/openscreen_platform",
    "//third_party/openscreen/src/cast/common:channel",
  ]
  seed_corpus = "fuzz_corpus"

  # NOTE: Max message size is 65536 + 4, so this allows some overhang.
  libfuzzer_options = [ "max_len=65600" ]
}

fuzzer_test("openscreen_message_framer_serialize_fuzzer") {
  sources = [ "openscreen_message_framer_serialize_fuzzer.cc" ]
  deps = [
    ":channel_fuzzer_inputs",
    "//components/openscreen_platform",
    "//third_party/libprotobuf-mutator",
    "//third_party/openscreen/src/cast/common:channel",
  ]
}