#!/bin/sh

# Some simple tests of readpe against a simple PE executable

TESTSRC="$AUTOPKGTEST_TMP/hello.c"
TESTEXE="$AUTOPKGTEST_TMP/hello.exe"

set -e

retval=0

# We don't want plugins from the build directory for the
# installed package.
rm -f pev.conf

if type valgrind ; then
    VALGRIND=valgrind
else
    VALGRIND=
fi

# Create test source code on autopkg's temp dir.
cat <<EOF > "$AUTOPKGTEST_TMP/hello.c"
#include <stdio.h>

int main() {
    printf("hello PE world\n");
    return 0;
}
EOF

# Compile test code with ASLR enabled.
x86_64-w64-mingw32-gcc -Wl,--high-entropy-va "${TESTSRC}" -o "${TESTEXE}"

if $VALGRIND pesec "$TESTEXE" | grep ASLR; then
    echo "success: pesec reported ASLR status"
else
    echo "error: pesec did not report ASLR status"
    retval=1
fi

if $VALGRIND pehash "$TESTEXE" | grep sha256:; then
    echo "success: pehash reported hash"
else
    echo "error: pehash did not report hash"
    retval=1
fi

exit $retval
