#!/bin/bash

## Copyright (C) 2012 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

set -e
set -o pipefail
set -o errtrace

gpg_bash_lib_mydir="$( cd "$( dirname "$0" )" && pwd )"
cd "$gpg_bash_lib_mydir"
cd ../../../../

source "./usr/libexec/gpg-bash-lib/source_all"

trap "gpg_bash_lib_function_error_handler" ERR
set +e

gpg_bash_lib_function_unit_test_sanity_tests() {
   if ! command -v faketime >/dev/null ; then
      echo "ERROR $0: faketime not installed!" >&2
      exit 2
   fi
}

gpg_bash_lib_function_unit_test_genkey() {
   gpg_bash_lib_unit_test_homedir="$(mktemp --directory)"
   safe-rm --recursive --force -- "$gpg_bash_lib_unit_test_homedir/gpg"
   mkdir --parents "$gpg_bash_lib_unit_test_homedir/gpg"
   chmod --recursive 700 "$gpg_bash_lib_unit_test_homedir/gpg"
   test -d "$gpg_bash_lib_unit_test_homedir/gpg"

   ## It is just a locally used test key that is discarded after testing,
   ## so we can use a weak key to speed up key generation.
   echo "
      Key-Type: RSA
      Key-Length: 1024
      Subkey-Type: RSA
      Subkey-Length: 1024
      Name-Real: auto generated test key for unit test
      Name-Email: unit@test.key
      Expire-Date: 2020-12-12
      Preferences: SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES CAST5 ZLIB BZIP2 ZIP Uncompressed
   " | faketime -f "-61d" \
         gpg \
                                 --no-options \
                                 --no-emit-version \
                                 --no-comments \
                                 --display-charset utf-8 \
                                 --personal-digest-preferences SHA512 \
                                 --cert-digest-algo SHA512 \
                                 --default-preference-list "SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES CAST5 ZLIB BZIP2 ZIP Uncompressed" \
                                 --keyserver-options no-honor-keyserver-url \
                                 --fixed-list-mode \
                                 --keyid-format 0xlong \
                                 --list-options show-uid-validity \
                                 --sig-notation issuer-fpr@notations.openpgp.fifthhorseman.net=%g \
                                 --no-default-keyring \
                                 --homedir "$gpg_bash_lib_unit_test_homedir/gpg" \
                                 --batch \
                                 --passphrase "" \
                                 --pinentry-mode loopback \
                                 --gen-key
}

gpg_bash_lib_function_unit_test_testrun_common() {
   echo "------------------------------------------------------------"

   safe-rm --force -- "$gpg_bash_lib_unit_test_homedir/test-file"
   safe-rm --force -- "$gpg_bash_lib_unit_test_homedir/test-file.asc"

   echo "test" > "$gpg_bash_lib_unit_test_homedir/test-file"
   test -f "$gpg_bash_lib_unit_test_homedir/test-file"

   faketime -f "$gpg_bash_lib_unit_test_faketime" \
      gpg \
         --armor \
         --no-options \
         --homedir "$gpg_bash_lib_unit_test_homedir/gpg" \
         --passphrase "" \
         --pinentry-mode loopback \
         --set-notation "file@name"="$(basename "$gpg_bash_lib_unit_test_homedir/test-file")" \
         --detach-sign "$gpg_bash_lib_unit_test_homedir/test-file"

   ## Convert keybox kbx file into keyring gpg file.
   gpg \
      --no-options \
      --homedir $gpg_bash_lib_unit_test_homedir/gpg \
      --export \
      > "$gpg_bash_lib_unit_test_homedir/gpg/pubring.gpg"

   gpg_bash_lib_input_temp_folder="$(mktemp --directory)"
   gpg_bash_lib_input_key_import_dir="$(mktemp --directory)"
   cp "$gpg_bash_lib_unit_test_homedir/gpg/pubring.gpg" "$gpg_bash_lib_input_key_import_dir/"
   gpg_bash_lib_input_file_name_enforce="true"
   gpg_bash_lib_input_cleanup="true"
   gpg_bash_lib_input_data_file="$gpg_bash_lib_unit_test_homedir/test-file"
   gpg_bash_lib_input_sig_file="$gpg_bash_lib_unit_test_homedir/test-file.asc"
   ## gpg_bash_lib_input_error_handler_extra='error_handler "$gpg_bash_lib_output_error_handler_message"'

   gpg_bash_lib_function_main_verify

   echo "\
gpg_bash_lib_output_failure_status: $gpg_bash_lib_output_failure_status
gpg_bash_lib_output_gpg_verify_exit_code: $gpg_bash_lib_output_gpg_verify_exit_code
gpg_bash_lib_output_goodsig_status: $gpg_bash_lib_output_goodsig_status
gpg_bash_lib_output_validsig_status: $gpg_bash_lib_output_validsig_status
gpg_bash_lib_output_fingerprint_in_hex: $gpg_bash_lib_output_fingerprint_in_hex
gpg_bash_lib_output_signed_on_unixtime: $gpg_bash_lib_output_signed_on_unixtime
gpg_bash_lib_output_signed_on_date: $gpg_bash_lib_output_signed_on_date
gpg_bash_lib_output_notation[$"file@name"]: ${gpg_bash_lib_output_notation[$"file@name"]}
gpg_bash_lib_output_file_name_tampering: $gpg_bash_lib_output_file_name_tampering
gpg_bash_lib_output_freshness_status: $gpg_bash_lib_output_freshness_status
gpg_bash_lib_output_freshness_detail: $gpg_bash_lib_output_freshness_detail
gpg_bash_lib_output_freshness_msg:
$gpg_bash_lib_output_freshness_msg
gpg_bash_lib_output_alright_status: $gpg_bash_lib_output_alright_status"

   echo "-----"

   if  [ ! "$gpg_bash_lib_output_freshness_detail" = "$gpg_bash_lib_unit_test_expected_freshness_detail_result" ]; then
      echo "\
gpg_bash_lib_unit_test_faketime: $gpg_bash_lib_unit_test_faketime
gpg_bash_lib_unit_test_expected_freshness_detail_result: $gpg_bash_lib_unit_test_expected_freshness_detail_result
-----
ERROR!" 2>&1
      exit 1
   fi
   echo "\
gpg_bash_lib_unit_test_faketime: $gpg_bash_lib_unit_test_faketime
gpg_bash_lib_unit_test_expected_freshness_detail_result: $gpg_bash_lib_unit_test_expected_freshness_detail_result
-----
PASS!"
}

gpg_bash_lib_function_unit_test_testrun_1() {
   gpg_bash_lib_unit_test_faketime="-2d"
   gpg_bash_lib_unit_test_expected_freshness_detail_result="current"
   gpg_bash_lib_function_unit_test_testrun_common
}

gpg_bash_lib_function_unit_test_testrun_2() {
   gpg_bash_lib_unit_test_faketime="+15m"
   gpg_bash_lib_unit_test_expected_freshness_detail_result="lenient"
   gpg_bash_lib_function_unit_test_testrun_common
}

gpg_bash_lib_function_unit_test_testrun_3() {
   gpg_bash_lib_unit_test_faketime="-31d"
   gpg_bash_lib_unit_test_expected_freshness_detail_result="outdated"
   gpg_bash_lib_function_unit_test_testrun_common
}

gpg_bash_lib_function_unit_test_testrun_4() {
   gpg_bash_lib_unit_test_faketime="+31d"
   gpg_bash_lib_unit_test_expected_freshness_detail_result="slow"
   gpg_bash_lib_function_unit_test_testrun_common
}

#gpg_bash_lib_input_error_handler_extra='error_handler "$gpg_bash_lib_output_error_handler_message"'
#non-existent

gpg_bash_lib_function_unit_test_sanity_tests
gpg_bash_lib_function_unit_test_genkey
gpg_bash_lib_function_unit_test_testrun_1
gpg_bash_lib_function_unit_test_testrun_2
gpg_bash_lib_function_unit_test_testrun_3
gpg_bash_lib_function_unit_test_testrun_4
