check_gpg_key_expiration.sh/test/test.sh
2022-03-06 14:52:00 +01:00

134 lines
4.2 KiB
Bash

cd $(dirname $0)
function echoerr { echo "$@" 1>&2; }
if [ -d tmp ]; then
rm -rf tmp
fi
mkdir tmp
chmod 700 tmp
function generateKey {
local expiry=$1
cat <<EOF | gpg --gen-key --homedir tmp --batch
%pubring tmp/pubkey
%no-protection
%transient-key
Key-Type: 1
Key-Length: 1024
Name-Real: Test
Name-Email: bla@test
Subkey-Type: 1
Subkey-Length: 1024
Expire-Date: ${expiry}
EOF
}
function testNotExpired {
generateKey 8d
output=$(../check_gpg_key_expiration.sh -u tmp/pubkey -w 7 -c 2)
exit=$?
if [ $exit -ne 0 ]; then
echoerr "testNotExpired failed. Exit code mismatch"
echoerr "Expected: 0"
echoerr "Actual: $exit"
fi
if [[ "$output" != "OK | "* ]]; then
echoerr "testNotExpired failed. Output did not match!"
echoerr "Expected: OK | *"
echoerr "Actual: $output"
fi
echo && echo
}
function testExpireWarn {
generateKey 3d
output=$(../check_gpg_key_expiration.sh -u tmp/pubkey -w 7 -c 2)
exit=$?
if [ $exit -ne 1 ]; then
echoerr "testExpireWarn failed. Exit code mismatch"
echoerr "Expected: 1"
echoerr "Actual: $exit"
fi
if [[ "$output" != "WARNING - Key "*" will expire at "*" | "* ]]; then
echoerr "testExpireWarn failed. Output did not match!"
echoerr "Expected: WARNING - Key **************** will expire at *"
echoerr "Actual: $output"
fi
echo && echo
}
function testExpireCritical {
generateKey 1d
output=$(../check_gpg_key_expiration.sh -u tmp/pubkey -w 7 -c 2)
exit=$?
if [ $exit -ne 2 ]; then
echoerr "testExpireCritical failed. Exit code mismatch"
echoerr "Expected: 2"
echoerr "Actual: $exit"
fi
if [[ "$output" != "CRITICAL - Key "*" will expire at "*" | "* ]]; then
echoerr "testExpireCritical failed. Output did not match!"
echoerr "Expected: CRITICAL - Key **************** will expire at *"
echoerr "Actual: $output"
fi
echo && echo
}
function testExpired {
output=$(../check_gpg_key_expiration.sh -u expiredkey -w 7 -c 2)
exit=$?
if [ $exit -ne 2 ]; then
echoerr "testExpired failed. Exit code mismatch"
echoerr "Expected: 2"
echoerr "Actual: $exit"
fi
if [[ "$output" != "CRITICAL - Key DAD15AEAD4609B87 expired at 2022-03-07T14:32:37. Key 2A8350447459F0C6 expired at 2022-03-07T14:32:37 | 'DAD15AEAD4609B87 expiration date'=2022-03-07T14:32:37 'DAD15AEAD4609B87 remaining'="*";604800s;172800s '2A8350447459F0C6 expiration date'=2022-03-07T14:32:37 '2A8350447459F0C6 remaining'="*";604800s;172800s" ]]; then
echoerr "testExpired failed. Output did not match!"
echoerr "Expected: CRITICAL - Key DAD15AEAD4609B87 expired at 2022-03-07T14:32:37. Key 2A8350447459F0C6 expired at 2022-03-07T14:32:37 | 'DAD15AEAD4609B87 expiration date'=2022-03-07T14:32:37 'DAD15AEAD4609B87 remaining'=*;604800s;172800s '2A8350447459F0C6 expiration date'=2022-03-07T14:32:37 '2A8350447459F0C6 remaining'=*;604800s;172800s"
echoerr "Actual: $output"
fi
echo && echo
}
function testFileDoesNotExist {
output=$(../check_gpg_key_expiration.sh -u this_file_does_not_exist_blablabla_43t0zq84whtrshq3tptsth)
exit=$?
if [ $exit -ne 3 ]; then
echoerr "testFileDoesNotExist failed. Exit code mismatch"
echoerr "Expected: 3"
echoerr "Actual: $exit"
fi
if [[ "$output" != "ERROR - Downloading failed with "* ]]; then
echoerr "testFileDoesNotExist failed. Output did not match!"
echoerr "Expected: ERROR - Downloading failed with *"
echoerr "Actual: $output"
fi
}
function testFileIsNotAGpgKey {
output=$(../check_gpg_key_expiration.sh -u test.sh)
exit=$?
if [ $exit -ne 3 ]; then
echoerr "testFileIsNotAGpgKey failed. Exit code mismatch"
echoerr "Expected: 3"
echoerr "Actual: $exit"
fi
if [[ "$output" != "ERROR - gpg failed with "* ]]; then
echoerr "testFileIsNotAGpgKey failed. Output did not match!"
echoerr "Expected: ERROR - gpg failed with *"
echoerr "Actual: $output"
fi
}
testNotExpired
testExpireWarn
testExpireCritical
testExpired
testFileDoesNotExist
testFileIsNotAGpgKey