#!/bin/bash

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

## {{{ test functions

gpg_bash_lib_function_unit_test_check_error_handler() {
   gpg_bash_lib_unit_test_current_trap="$(trap -p ERR)"
   true "gpg_bash_lib_unit_test_existing_trap_original: $gpg_bash_lib_unit_test_existing_trap_original"
   true "gpg_bash_lib_unit_test_existing_trap_by_gpg_bash_lib: $gpg_bash_lib_unit_test_existing_trap_by_gpg_bash_lib"
   true "gpg_bash_lib_unit_test_current_trap   : $gpg_bash_lib_unit_test_current_trap"
   if [ ! "$gpg_bash_lib_unit_test_existing_trap_original" = "$gpg_bash_lib_unit_test_current_trap" ]; then
      echo "ERROR #1 !" >&2
      exit 1
   fi
   if [ ! "$gpg_bash_lib_unit_test_existing_trap_by_gpg_bash_lib" = "$gpg_bash_lib_unit_test_current_trap" ]; then
      echo "ERROR #2 !" >&2
      exit 1
   fi
}

## }}}

## {{{ setup

gpg_bash_lib_function_unit_test_error_handler() {
   echo "$FUNCNAME ERROR #0 !" >&2
   exit 1
}

trap "gpg_bash_lib_function_unit_test_error_handler" ERR
gpg_bash_lib_unit_test_existing_trap_original="$(trap -p ERR)"

set -o errtrace

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

## }}}

## {{{ test 1

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

trap "gpg_bash_lib_function_unit_test_error_handler" ERR
gpg_bash_lib_unit_test_existing_trap_by_gpg_bash_lib="$(trap -p ERR)"

gpg_bash_lib_function_init

gpg_bash_lib_function_deinit

gpg_bash_lib_function_unit_test_check_error_handler

## pipefail was not set before, it should not be set now.

if test -o pipefail ; then
   echo "ERROR #4!" >&2
   exit 1
fi

## }}}

## {{{ test 2

set -o pipefail
set -o errtrace

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

gpg_bash_lib_function_init

gpg_bash_lib_function_deinit

gpg_bash_lib_unit_test_current_trap="$(trap -p ERR)"

gpg_bash_lib_function_unit_test_check_error_handler

## errtrace, pipefail was set before, it should be set now.

if ! test -o errtrace ; then
   echo "ERROR #5 !" >&2
   exit 1
fi

if ! test -o pipefail ; then
   echo "ERROR #6 !" >&2
   exit 1
fi

## }}}

gpg_bash_lib_function_unit_test_custom_error_handler() {
   true "$FUNCNAME: functional"
}

gpg_bash_lib_input_error_handler_extra="gpg_bash_lib_function_unit_test_custom_error_handler ; return 0"

gpg_bash_lib_function_error_handler

true "SUCCESS!"
