# this file is a library sourced from recipes/*

result_path=$(pwd)
cd $(dirname "$0")/../
script_path=$(pwd)
cd "${result_path}"

test_result() {
#1 last exit status
#2 test name

  local result=0

  if [ "$1" -eq 0 ]
    then
      printf "%s\n" "Test succeeded"
    else
      printf "%s\n" "Test failed"
      printf "%-100s\t%s\n" "$2" "failed" 1>&3
      result=1
    fi
  return "$result"
}

modify_blob() {
# $1 test number
# $2 filename extension

  local result=0

  begin_blob=$(echo -n "---BEGIN_BLOB---" | xxd -p)
  modify_blob=$(echo -n "---MODIFIED_BLOB---" | xxd -p)
  zero_blob="00000000000000000000000000000000000000"
  xxd -p -c 1000 "test_$1.$2" | \
      sed "s/$begin_blob$zero_blob/$begin_blob$modify_blob/" | \
      xxd -p -r > "test_$1_modifed.$2"
  ../../osslsigncode verify -in "test_$1_modifed.$2" 2>> "verify.log" 1>&2
  result=$?
  if [ "$result" -ne 0 ] || \
      [ $(grep -e "Calculated DigitalSignature" -e "Calculated message digest" "verify.log" | uniq | wc -l) -ne 1 ]
    then
      result=$?
      cat "verify.log" >> "results.log"
      printf "Failed: verify error or non-unique message digests were found\n"
    else
      rm -f "test_$1_modifed.$2"
    fi
  return "$result"
}

verify_signature() {
# $1 sign exit code
# $2 test number
# $3 filename extension
# $4 modify requirement
# $5 sha256sum requirement
# $6 ASCII or HEX "$7 text" format
# $7 obligatory text1 searched in a binary file or verify.log
# $8 optional text2 searched in verify.log

  local result=0

  if [ "$1" -eq 0 ]
    then
      if [ "$3" != "ex_" ]
        then
          ../../osslsigncode verify -in "test_$2.$3" 2> "verify.log" 1>&2
          result=$?
        fi
      if [ "$6" = "ASCII" ]
          then
            searched_text=$(echo -n "$7" | xxd -p)
          else
            searched_text=$7
          fi
      if ! grep -q "$7" "verify.log" && \
          ! xxd -p -c 1000 "test_$2.$3" | grep "$searched_text" 2>> /dev/null 1>&2
        then
          result=1
          printf "Failed: $7 not found\n"
        elif [ "$4" = "MODIFY" ]
          then
            if [ "$3" != "ex_" ]
              then
                modify_blob "$2" "$3"
                result=$?
              else
                printf "MODIFY is not supported for CAB files\n"
              fi
        fi
      if [ "$result" -eq 0 ] && [ "$8" != "UNUSED_PATTERN" ] && ! grep -q "$8" "verify.log"
        then
          result=1
          printf "Failed: $8 not found\n"
        fi
      if [ "$result" -eq 0 ]
        then
          if [ "$5" = "sha256sum" ]
            then
              sha256sum "test_$2.$3" 2>> "sha256sum_$3.log" 1>&2
              if [ -s "test_$2_signed.$3" ]
                then
                  sha256sum "test_$2_signed.$3" 2>> "sha256sum_$3.log" 1>&2
                fi
            else
              rm -f "test_$2.$3" "test_$2_signed.$3"
            fi
        else
          cat "verify.log" >> "results.log"
        fi
    else
      result=1
    fi
  return "$result"
}

verify_no_signature() {
# $1 sign exit code
# $2 test number
# $3 filename extension
# $4 sha256sum requirement

  local result=0

  if [ "$1" -eq 0 ]
    then
      ../../osslsigncode verify -in "test_$2.$3" 2> "verify.log" 1>&2
      if grep -q -e "No signature found" -e "MSI file has no signature" "verify.log"
        then
          sha256sum "test_$2_signed.$3" 2>> "sha256sum_$3.log" 1>&2
        else
          result=1
          cat "verify.log" >> "results.log"
          printf "Failed: verify error or the signature was found\n"
        fi
    else
      result=1
    fi
  return "$result"
}
