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

import("//testing/test.gni")
import("//third_party/libsrtp/options.gni")

declare_args() {
  # Tests may not be appropriate for some build environments, e.g. Windows.
  # Rather than enumerate valid options, we just let clients ask for them.
  build_libsrtp_tests = false
}

config("external_ssl_library") {
  if (libsrtp_ssl_root != "") {
    include_dirs = [ libsrtp_ssl_root ]
  }
  libs = [
    "crypto",
    "ssl",
  ]
}

config("libsrtp_config") {
  defines = [
    # Package identity
    "PACKAGE_STRING=\"24b3bf8f19b6f5ab4cd2bcceb4f4064e\"",
    "PACKAGE_VERSION=\"24b3bf8f19b6f5ab4cd2bcceb4f4064e\"",

    # Config
    "HAVE_CONFIG_H",
    "OPENSSL",
    "GCM",

    # Platform properties
    "HAVE_STDLIB_H",
    "HAVE_STRING_H",
    "HAVE_STDINT_H",
    "HAVE_INTTYPES_H",
    "HAVE_INT16_T",
    "HAVE_INT32_T",
    "HAVE_INT8_T",
    "HAVE_UINT16_T",
    "HAVE_UINT32_T",
    "HAVE_UINT64_T",
    "HAVE_UINT8_T",
  ]

  if (is_posix || is_fuchsia) {
    defines += [
      "HAVE_ARPA_INET_H",
      "HAVE_NETINET_IN_H",
      "HAVE_SYS_TYPES_H",
      "HAVE_UNISTD_H",
    ]
  }
  if (is_win) {
    defines += [ "HAVE_WINSOCK2_H" ]
  }
  # cryptex is supported since v2.8.0.
  defines += [ "LIBSRTP_HAVE_CRYPTEX" ]

  # Supports clients depending on SRTP_RELATIVE_PATH.
  # TODO(mattdr): Remove SRTP_RELATIVE_PATH and this config.
  include_dirs = [
    ".",
    "config",
    "include",
    "crypto/include",
  ]
}

config("libsrtp_warnings") {
  cflags = []
  if (is_win) {
    cflags += [ "/wd4018" ]  # Signed/unsigned mismatch in comparison
  }
}

static_library("libsrtp") {
  configs -= [ "//build/config/compiler:chromium_code" ]
  configs += [ "//build/config/compiler:no_chromium_code" ]
  configs += [ ":libsrtp_warnings" ]
  public_configs = [ ":libsrtp_config" ]
  sources = [
    # includes
    "include/srtp.h",

    # headers
    "crypto/cipher/cipher_test_cases.h",
    "crypto/hash/auth_test_cases.h",
    "crypto/include/aes.h",
    "crypto/include/aes_gcm.h",
    "crypto/include/aes_icm.h",
    "crypto/include/aes_icm_ext.h",
    "crypto/include/alloc.h",
    "crypto/include/auth.h",
    "crypto/include/cipher.h",
    "crypto/include/cipher_priv.h",
    "crypto/include/cipher_types.h",
    "crypto/include/crypto_kernel.h",
    "crypto/include/crypto_types.h",
    "crypto/include/datatypes.h",
    "crypto/include/err.h",
    "crypto/include/integers.h",
    "crypto/include/key.h",
    "crypto/include/null_auth.h",
    "crypto/include/null_cipher.h",
    "crypto/include/rdb.h",
    "crypto/include/rdbx.h",
    "include/srtp_priv.h",

    # sources
    "crypto/cipher/aes_gcm_ossl.c",
    "crypto/cipher/aes_icm_ossl.c",
    "crypto/cipher/cipher.c",
    "crypto/cipher/cipher_test_cases.c",
    "crypto/cipher/null_cipher.c",
    "crypto/hash/auth.c",
    "crypto/hash/auth_test_cases.c",
    "crypto/hash/hmac_ossl.c",
    "crypto/hash/null_auth.c",
    "crypto/kernel/alloc.c",
    "crypto/kernel/crypto_kernel.c",
    "crypto/kernel/err.c",
    "crypto/kernel/key.c",
    "crypto/math/datatypes.c",
    "crypto/replay/rdb.c",
    "crypto/replay/rdbx.c",
    "srtp/srtp.c",

    # TODO(bugs.webrtc.org/11915): The following files are not needed by
    # WebRTC and they are not part of the build and the checkout.
    # Adding them to the build, requires the addition of some conditionals
    # since these files include duplicated symbols and cannot be built
    # together.
    #
    # "crypto/include/hmac.h",
    # "crypto/include/sha1.h",
    # "crypto/cipher/aes.c",
    # "crypto/cipher/aes_icm.c",
    # "crypto/hash/hmac.c",
    # "crypto/hash/sha1.c",
    # "crypto/cipher/aes_gcm_nss.c",
    # "crypto/cipher/aes_icm_nss.c",
  ]

  deps = []
  if (libsrtp_build_boringssl) {
    deps += [ "//third_party/boringssl:boringssl" ]
  } else {
    configs += [ ":external_ssl_library" ]
  }
}

if (build_libsrtp_tests) {
  config("srtp_test_config") {
    include_dirs = [ "test" ]
    configs = [ ":libsrtp_warnings" ]
  }

  template("srtp_test") {
    test("${target_name}") {
      output_name = invoker.target_name
      output_dir = "${root_out_dir}/srtp_tests"
      configs -= [ "//build/config/compiler:chromium_code" ]
      configs += [ "//build/config/compiler:no_chromium_code" ]
      configs += [ ":srtp_test_config" ]
      deps = [ ":libsrtp" ]
      sources = [
                  "test/getopt_s.c",
                  "test/getopt_s.h",
                  "test/ut_sim.c",
                  "test/ut_sim.h",
                  "test/util.c",
                  "test/util.h",
                ] + invoker.sources
      testonly = true
    }
  }

  srtp_test("rdbx_driver") {
    sources = [ "test/rdbx_driver.c" ]
  }
  srtp_test("replay_driver") {
    sources = [ "test/replay_driver.c" ]
  }
  srtp_test("roc_driver") {
    sources = [ "test/roc_driver.c" ]
  }
  srtp_test("srtp_driver") {
    sources = [ "test/srtp_driver.c" ]
  }
  srtp_test("test_srtp") {
    sources = [
      "test/cutest.h",
      "test/test_srtp.c",
    ]
  }
  srtp_test("cipher_driver") {
    sources = [ "crypto/test/cipher_driver.c" ]
  }
  srtp_test("datatypes_driver") {
    sources = [ "crypto/test/datatypes_driver.c" ]
  }
  srtp_test("kernel_driver") {
    sources = [ "crypto/test/kernel_driver.c" ]
  }

  executable("rtpw") {
    configs -= [ "//build/config/compiler:chromium_code" ]
    configs += [ "//build/config/compiler:no_chromium_code" ]
    configs += [ ":srtp_test_config" ]
    output_dir = "${root_out_dir}/srtp_tests"
    deps = [
      ":libsrtp",
      ":cipher_driver"
    ]
    sources = [
      "test/getopt_s.c",
      "test/getopt_s.h",
      "test/rtp.c",
      "test/rtp.h",
      "test/rtpw.c",
      "test/util.c",
      "test/util.h",

      # The original Makefile includes this file, but all the functions rtpw
      # uses have been moved to util.c.
      "crypto/math/datatypes.c",
    ]
    defines = [ "HAVE_SYS_SOCKET_H" ]
    testonly = true
  }

  copy("test_scripts") {
    sources = [
      "run_all_tests.sh",
      "test/rtpw_test.sh",
      "test/rtpw_test_gcm.sh",
    ]
    outputs = [ "${root_out_dir}/srtp_tests/{{source_file_part}}" ]
    testonly = true
  }

  copy("test_data") {
    sources = [ "test/words.txt" ]
    outputs = [ "${root_out_dir}/srtp_tests/{{source_file_part}}" ]
    testonly = true
  }

  group("srtp_tests") {
    deps = [
      ":cipher_driver",
      ":datatypes_driver",
      ":kernel_driver",
      ":rdbx_driver",
      ":replay_driver",
      ":roc_driver",
      ":rtpw",
      ":srtp_driver",
      ":test_data",
      ":test_scripts",
      ":test_srtp",
    ]
    testonly = true
  }
}
