#
# Copyright 2015-2018 Yubico AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

find_program (BASH_PROGRAM bash)

add_test(
  NAME engine_tests
  COMMAND ${BASH_PROGRAM} ${CMAKE_CURRENT_SOURCE_DIR}/engine_tests.sh
  )

set(
  SKIPPED_TESTS
  Slot.NoInit
  PKCS11Test.EnumerateMechanisms

  # YH requires login for C_GenerateRandom
  ReadOnlySessionTest.GenerateRandom
  ReadOnlySessionTest.GenerateRandomNone

  ReadOnlySessionTest.UserLoginWrongPIN
  ReadOnlySessionTest.SOLoginFail
  ReadOnlySessionTest.CreateKeyPairObjects
  ReadOnlySessionTest.CreateSecretKeyAttributes
  ReadOnlySessionTest.SignVerifyRecover
  ReadOnlySessionTest.GenerateKeyInvalid
  ReadOnlySessionTest.GenerateKeyPairInvalid
  ReadOnlySessionTest.WrapUnwrap
  ReadOnlySessionTest.WrapInvalid
  ReadOnlySessionTest.UnwrapInvalid
  ReadWriteSessionTest.CreateCopyDestroyObject
  ReadWriteSessionTest.SetLatchingAttribute
  ReadWriteSessionTest.FindObjectSubset
  ReadWriteSessionTest.ReadOnlySessionSOLoginFail
  ReadWriteSessionTest.SOLogin
  ReadWriteSessionTest.TookanAttackA1
  ReadWriteSessionTest.TookanAttackA3
  ReadWriteSessionTest.TookanAttackA4
  ReadWriteSessionTest.TookanAttackA5a
  ReadWriteSessionTest.TookanAttackA5b
  ReadWriteSessionTest.PublicExponent4Bytes
  ReadWriteSessionTest.ExtractKeys
  ReadWriteSessionTest.AsymmetricTokenKeyPair
  RWUserSessionTest.SOLoginFail
  DataObjectTest.CopyDestroyObjectInvalid
  DataObjectTest.GetMultipleAttributes
  DataObjectTest.GetSetAttributeInvalid
  RWSOSessionTest.SOSessionFail
  RWSOSessionTest.UserLoginFail
  RWEitherSessionTest.TookanAttackA2
  KeyPairTest.EncryptDecrypt
  Ciphers/SecretKeyTest.EncryptDecrypt/0
  Ciphers/SecretKeyTest.EncryptDecrypt/1
  Ciphers/SecretKeyTest.EncryptDecrypt/2
  Ciphers/SecretKeyTest.EncryptDecrypt/3
  Ciphers/SecretKeyTest.EncryptFailDecrypt/0
  Ciphers/SecretKeyTest.EncryptFailDecrypt/1
  Ciphers/SecretKeyTest.EncryptFailDecrypt/2
  Ciphers/SecretKeyTest.EncryptFailDecrypt/3
  Ciphers/SecretKeyTest.EncryptDecryptGetSpace/0
  Ciphers/SecretKeyTest.EncryptDecryptGetSpace/1
  Ciphers/SecretKeyTest.EncryptDecryptGetSpace/2
  Ciphers/SecretKeyTest.EncryptDecryptGetSpace/3
  Ciphers/SecretKeyTest.EncryptDecryptParts/0
  Ciphers/SecretKeyTest.EncryptDecryptParts/1
  Ciphers/SecretKeyTest.EncryptDecryptParts/2
  Ciphers/SecretKeyTest.EncryptDecryptParts/3
  Ciphers/SecretKeyTest.EncryptDecryptInitInvalid/0
  Ciphers/SecretKeyTest.EncryptDecryptInitInvalid/1
  Ciphers/SecretKeyTest.EncryptDecryptInitInvalid/2
  Ciphers/SecretKeyTest.EncryptDecryptInitInvalid/3
  Ciphers/SecretKeyTest.EncryptErrors/0
  Ciphers/SecretKeyTest.EncryptErrors/1
  Ciphers/SecretKeyTest.EncryptErrors/2
  Ciphers/SecretKeyTest.EncryptErrors/3
  Ciphers/SecretKeyTest.DecryptErrors/0
  Ciphers/SecretKeyTest.DecryptErrors/1
  Ciphers/SecretKeyTest.DecryptErrors/2
  Ciphers/SecretKeyTest.DecryptErrors/3
  Ciphers/SecretKeyTest.EncryptUpdateErrors/0
  Ciphers/SecretKeyTest.EncryptUpdateErrors/1
  Ciphers/SecretKeyTest.EncryptUpdateErrors/2
  Ciphers/SecretKeyTest.EncryptUpdateErrors/3
  Ciphers/SecretKeyTest.EncryptModePolicing1/0
  Ciphers/SecretKeyTest.EncryptModePolicing1/1
  Ciphers/SecretKeyTest.EncryptModePolicing1/2
  Ciphers/SecretKeyTest.EncryptModePolicing1/3
  Ciphers/SecretKeyTest.EncryptModePolicing2/0
  Ciphers/SecretKeyTest.EncryptModePolicing2/1
  Ciphers/SecretKeyTest.EncryptModePolicing2/2
  Ciphers/SecretKeyTest.EncryptModePolicing2/3
  Ciphers/SecretKeyTest.EncryptInvalidIV/0
  Ciphers/SecretKeyTest.EncryptInvalidIV/1
  Ciphers/SecretKeyTest.EncryptInvalidIV/2
  Ciphers/SecretKeyTest.EncryptInvalidIV/3
  Ciphers/SecretKeyTest.DecryptInvalidIV/0
  Ciphers/SecretKeyTest.DecryptInvalidIV/1
  Ciphers/SecretKeyTest.DecryptInvalidIV/2
  Ciphers/SecretKeyTest.DecryptInvalidIV/3
  Ciphers/SecretKeyTest.DecryptInvalidIV/3
  Ciphers/SecretKeyTest.DecryptUpdateErrors/0
  Ciphers/SecretKeyTest.DecryptUpdateErrors/1
  Ciphers/SecretKeyTest.DecryptUpdateErrors/2
  Ciphers/SecretKeyTest.DecryptUpdateErrors/3
  Ciphers/SecretKeyTest.EncryptFinalImmediate/0
  Ciphers/SecretKeyTest.EncryptFinalImmediate/1
  Ciphers/SecretKeyTest.EncryptFinalImmediate/2
  Ciphers/SecretKeyTest.EncryptFinalImmediate/3
  Ciphers/SecretKeyTest.EncryptFinalErrors1/0
  Ciphers/SecretKeyTest.EncryptFinalErrors1/1
  Ciphers/SecretKeyTest.EncryptFinalErrors1/2
  Ciphers/SecretKeyTest.EncryptFinalErrors1/3
  Ciphers/SecretKeyTest.EncryptFinalErrors2/0
  Ciphers/SecretKeyTest.EncryptFinalErrors2/1
  Ciphers/SecretKeyTest.EncryptFinalErrors2/2
  Ciphers/SecretKeyTest.EncryptFinalErrors2/3
  Ciphers/SecretKeyTest.DecryptFinalErrors1/0
  Ciphers/SecretKeyTest.DecryptFinalErrors1/1
  Ciphers/SecretKeyTest.DecryptFinalErrors1/2
  Ciphers/SecretKeyTest.DecryptFinalErrors1/3
  Ciphers/SecretKeyTest.DecryptFinalErrors2/0
  Ciphers/SecretKeyTest.DecryptFinalErrors2/1
  Ciphers/SecretKeyTest.DecryptFinalErrors2/2
  Ciphers/SecretKeyTest.DecryptFinalErrors2/3
  Digests/DigestTest.DigestKey/0
  Digests/DigestTest.DigestKey/1
  Digests/DigestTest.DigestKey/2
  Digests/DigestTest.DigestKey/3
  Digests/DigestTest.DigestKey/4
  Digests/DigestTest.DigestKeyInvalid/0
  Digests/DigestTest.DigestKeyInvalid/1
  Digests/DigestTest.DigestKeyInvalid/2
  Digests/DigestTest.DigestKeyInvalid/3
  Digests/DigestTest.DigestKeyInvalid/4
  Signatures/SignTest.SignVerify/0
  Signatures/SignTest.SignFailVerifyWrong/0
  Signatures/SignTest.SignFailVerifyShort/0
  Duals/DualSecretKeyTest.DigestEncrypt/0
  Duals/DualSecretKeyTest.DigestEncrypt/1
  Duals/DualSecretKeyTest.DigestEncrypt/2
  Duals/DualSecretKeyTest.DigestEncrypt/3
  Duals/DualSecretKeyTest.DigestEncrypt/4
  Duals/DualSecretKeyTest.DigestEncrypt/5
  Init.InitArgsBadReserved
  )
string(REPLACE ";" ":" SKIPPED_TESTS_STR "${SKIPPED_TESTS}")

if (DEFINED ENV{PKCS11TEST_PATH})
  if (NOT EXISTS $ENV{PKCS11TEST_PATH}/pkcs11test)
    message(FATAL_ERROR "PKCS11TEST_PATH defined, but $ENV{PKCS11TEST_PATH}/pkcs11test doesn't exist")
  endif ()
else ()
  message(WARNING "PKCS11TEST_PATH not defined, assuming that pkcs11test is in PATH")
endif ()

message(STATUS "Using PKCS11_TESTER: $ENV{PKCS11TEST_PATH}/pkcs11test")

if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  set(LIBEXT "dylib")
elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
  set(LIBEXT "so")
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  set(LIBEXT "so")
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
  set(LIBEXT "dll")
endif ()

add_test(
  NAME pkcs11test
  COMMAND ${BASH_PROGRAM} ${CMAKE_CURRENT_SOURCE_DIR}/pkcs11test.sh
  )

set_property(
  TEST pkcs11test
  APPEND PROPERTY ENVIRONMENT
  SKIPPED_TESTS=${SKIPPED_TESTS_STR})

set_property(
  TEST pkcs11test
  APPEND PROPERTY ENVIRONMENT
  BINDIR=${CMAKE_BINARY_DIR})

set_property(
  TEST pkcs11test
  APPEND PROPERTY ENVIRONMENT
  LIBEXT=${LIBEXT})

set (
  SOURCE_ECDH_DERIVE
  ecdh_derive_test.c
  common.c
  )

set (
        SOURCE_ECDH_SP800
        ecdh_sp800_test.c
        common.c
)

set (
  SOURCE_AES_ENCRYPT
  aes_encrypt_test.c
  common.c
  )

set (
  SOURCE_RSA_ENC_TEST
  rsa_enc_test.c
  common.c
  )

set (
        SOURCE_RSA_PSS_SIGN_TEST
        pss_sign_test.c
        common.c
)

set (
        SOURCE_ASYM_WRAP_TEST
        asym_wrap_test.c
        common.c
)

set (
        SOURCE_PKCS11_INTERFACES_TEST
        pkcs11_interfaces_test.c
        common.c
)

if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows")
add_executable (aes_encrypt_test ${SOURCE_AES_ENCRYPT})
add_executable (ecdh_derive_test ${SOURCE_ECDH_DERIVE})
add_executable (ecdh_sp800_test ${SOURCE_ECDH_SP800})

target_link_libraries(
  aes_encrypt_test
  "-ldl")

add_test (
  NAME aes_encrypt_test
  COMMAND ${CMAKE_CURRENT_BINARY_DIR}/aes_encrypt_test ${CMAKE_CURRENT_BINARY_DIR}/../yubihsm_pkcs11.${LIBEXT}
  )
set_tests_properties(aes_encrypt_test PROPERTIES SKIP_RETURN_CODE 64)

target_link_libraries (
  ecdh_derive_test
  ${LIBCRYPTO_LDFLAGS}
  "-ldl")

target_link_libraries (
        ecdh_sp800_test
        ${LIBCRYPTO_LDFLAGS}
        "-ldl")

add_test (
  NAME ecdh_derive_test
  COMMAND ${CMAKE_CURRENT_BINARY_DIR}/ecdh_derive_test ${CMAKE_CURRENT_BINARY_DIR}/../yubihsm_pkcs11.${LIBEXT}
  )

add_test (
        NAME ecdh_sp800_test
        COMMAND ${CMAKE_CURRENT_BINARY_DIR}/ecdh_sp800_test ${CMAKE_CURRENT_BINARY_DIR}/../yubihsm_pkcs11.${LIBEXT}
)

if (NOT ${LIBCRYPTO_VERSION} VERSION_LESS 1.1)
  add_executable (rsa_enc_test ${SOURCE_RSA_ENC_TEST})
  target_link_libraries (
          rsa_enc_test
          ${LIBCRYPTO_LDFLAGS}
          "-ldl")
  add_test (
          NAME rsa_enc_test
          COMMAND ${CMAKE_CURRENT_BINARY_DIR}/rsa_enc_test ${CMAKE_CURRENT_BINARY_DIR}/../yubihsm_pkcs11.${LIBEXT}
  )


  add_executable (pss_sign_test ${SOURCE_RSA_PSS_SIGN_TEST})
  target_link_libraries (
          pss_sign_test
          ${LIBCRYPTO_LDFLAGS}
          "-ldl")
  add_test (
          NAME pss_sign_test
          COMMAND ${CMAKE_CURRENT_BINARY_DIR}/pss_sign_test ${CMAKE_CURRENT_BINARY_DIR}/../yubihsm_pkcs11.${LIBEXT}
  )

  add_executable (asym_wrap_test ${SOURCE_ASYM_WRAP_TEST})
  target_link_libraries (
          asym_wrap_test
          ${LIBCRYPTO_LDFLAGS}
          "-ldl")
  add_test (
          NAME asym_wrap_test
          COMMAND ${CMAKE_CURRENT_BINARY_DIR}/asym_wrap_test ${CMAKE_CURRENT_BINARY_DIR}/../yubihsm_pkcs11.${LIBEXT}
  )

  add_executable (pkcs11_interfaces_test ${SOURCE_PKCS11_INTERFACES_TEST})
  target_link_libraries (
          pkcs11_interfaces_test
          "-ldl")
  add_test (
          NAME pkcs11_interfaces_test
          COMMAND ${CMAKE_CURRENT_BINARY_DIR}/pkcs11_interfaces_test ${CMAKE_CURRENT_BINARY_DIR}/../yubihsm_pkcs11.${LIBEXT}
  )
endif(NOT ${LIBCRYPTO_VERSION} VERSION_LESS 1.1)

endif(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows")
