#!/bin/sh
# Add an authenticode timestamp to the PE/CAB/MSI file.

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

# PE file
test_name="371. Add an authenticode timestamp to the PE file"
printf "\n%s\n" "$test_name"
if test -s "test.exe" && ! grep -q "no libcurl available" "results.log"
  then
    ../../osslsigncode sign -h sha256 \
        -st "1556668800" \
        -certs "${script_path}/../certs/cert.pem" -key "${script_path}/../certs/key.pem" \
        -in "test.exe" -out "test_371_signed.exe" && \
    ../../osslsigncode add \
        -t http://time.certum.pl/ \
        -t http://timestamp.digicert.com/ \
        -verbose \
        -in "test_371_signed.exe" -out "test_371.exe"
    verify_signature "$?" "371" "exe" "success" "@2019-09-01 12:00:00" \
	"UNUSED_PATTERN" "ASCII" "Timestamp Server Signature" "UNUSED_PATTERN"
    test_result "$?" "$test_name"
  else
    printf "Test skipped\n"
  fi

# CAB file
test_name="372. Add an authenticode timestamp to the CAB file"
printf "\n%s\n" "$test_name"
if test -s "test.ex_" && ! grep -q "no libcurl available" "results.log"
  then
    ../../osslsigncode sign -h sha256 \
        -st "1556668800" \
        -certs "${script_path}/../certs/cert.pem" -key "${script_path}/../certs/key.pem" \
        -in "test.ex_" -out "test_372_signed.ex_" && \
    ../../osslsigncode add \
        -t http://time.certum.pl/ \
        -t http://timestamp.digicert.com/ \
        -verbose \
        -in "test_372_signed.ex_" -out "test_372.ex_"
    verify_signature "$?" "372" "ex_" "success" "@2019-09-01 12:00:00" \
	"UNUSED_PATTERN" "ASCII" "Timestamp Server Signature" "UNUSED_PATTERN"
    test_result "$?" "$test_name"
  else
    printf "Test skipped\n"
  fi

# MSI file
test_name="373. Add an authenticode timestamp to the MSI file"
printf "\n%s\n" "$test_name"
if test -s "sample.msi" && ! grep -q "no libcurl available" "results.log"
  then
    ../../osslsigncode sign -h sha256 \
        -st "1556668800" \
        -certs "${script_path}/../certs/cert.pem" -key "${script_path}/../certs/key.pem" \
        -in "sample.msi" -out "test_373_signed.msi" && \
    ../../osslsigncode add \
        -t http://time.certum.pl/ \
        -t http://timestamp.digicert.com/ \
        -verbose \
        -in "test_373_signed.msi" -out "test_373.msi"
    verify_signature "$?" "373" "msi" "success" "@2019-09-01 12:00:00" \
	"UNUSED_PATTERN" "ASCII" "Timestamp Server Signature" "UNUSED_PATTERN"
    test_result "$?" "$test_name"
  else
    printf "Test skipped\n"
  fi

exit 0
