#!/bin/sh
# Extract the signature in the PEM format from the PE/CAB/MSI file.

. $(dirname $0)/../test_library
script_path=$(pwd)

# PE file
test_name="261. Extract the PEM signature from the PE file"
printf "\n%s\n" "$test_name"
if test -s "test.exe"
  then
    ../../osslsigncode sign -h sha512 \
        -st "1556668800" \
        -certs "${script_path}/../certs/cert.pem" -key "${script_path}/../certs/key.pem" \
        -in "test.exe" -out "test_261.exe" && \
    ../../osslsigncode extract-signature -pem \
        -in "test_261.exe" -out "sign_pe.pem"
    verify_signature "$?" "261" "exe" "success" "@2019-09-01 12:00:00" \
	"sha256sum" "ASCII" "SHA512" "UNUSED_PATTERN"
    test_result "$?" "$test_name"
  else
    printf "Test skipped\n"
  fi

# CAB file
test_name="262. Extract the PEM signature from the CAB file"
printf "\n%s\n" "$test_name"
if [ -s "test.ex_" ]
  then
    ../../osslsigncode sign -h sha512 \
        -st "1556668800" \
        -certs "${script_path}/../certs/cert.pem" -key "${script_path}/../certs/key.pem" \
        -in "test.ex_" -out "test_262.ex_" && \
    ../../osslsigncode extract-signature -pem \
        -in "test_262.ex_" -out "sign_cab.pem"
    verify_signature "$?" "262" "ex_" "success" "@2019-09-01 12:00:00" \
	"sha256sum" "ASCII" "SHA512" "UNUSED_PATTERN"
    test_result "$?" "$test_name"
  else
    printf "Test skipped\n"
  fi

# MSI file
test_name="263. Extract the PEM signature from the MSI file"
printf "\n%s\n" "$test_name"
if test -s "sample.msi"
  then
    ../../osslsigncode sign -h sha512 \
        -st "1556668800" \
        -certs "${script_path}/../certs/cert.pem" -key "${script_path}/../certs/key.pem" \
        -in "sample.msi" -out "test_263.msi" && \
    ../../osslsigncode extract-signature -pem \
        -in "test_263.msi" -out "sign_msi.pem"
    verify_signature "$?" "263" "msi" "success" "@2019-09-01 12:00:00" \
	"sha256sum" "ASCII" "SHA512" "UNUSED_PATTERN"
    test_result "$?" "$test_name"
  else
    printf "Test skipped\n"
  fi

exit 0
