#!/bin/sh
# Extracting the signature from the PE/MSI file.

. $(dirname $0)/../test_library

# PE file
test_name="311. Extracting the signature from the PE file"
printf "\n%s\n" "$test_name"
if [ -s "test.exe" ]
  then
    faketime -f '@2019-01-01 00:00:00' /bin/bash -c '
        script_path=$(pwd)
        ../../osslsigncode sign -h sha256 \
            -certs "${script_path}/../certs/cert.pem" -key "${script_path}/../certs/key.pem" \
            -in "test.exe" -out "test_311.exe" && \
        ../../osslsigncode extract-signature -pem \
            -in "test_311.exe" -out "sign_pe.pem"'
    verify_signature "$?" "311" "exe" "UNUSED_PATTERN" "sha256sum" "ASCII" "osslsigncode" "UNUSED_PATTERN"
    test_result "$?" "$test_name"
  else
    printf "Test skipped\n"
  fi

# CAB file
# Command is not supported for non-PE/non-MSI files

# MSI file
test_name="312. Extracting the signature from the MSI file"
printf "\n%s\n" "$test_name"
if [ -s "sample.msi" ]
  then
    faketime -f '@2019-01-01 00:00:00' /bin/bash -c '
        script_path=$(pwd)
        ../../osslsigncode sign -h sha256 \
            -certs "${script_path}/../certs/cert.pem" -key "${script_path}/../certs/key.pem" \
            -in "sample.msi" -out "test_312.msi" && \
        ../../osslsigncode extract-signature -pem \
            -in "test_312.msi" -out "sign_msi.pem"'
    verify_signature "$?" "312" "msi" "UNUSED_PATTERN" "sha256sum" "ASCII" "osslsigncode" "UNUSED_PATTERN"
    test_result "$?" "$test_name"
  else
    printf "Test skipped\n"
  fi

exit 0
