| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # For the license, see the LICENSE file in the root directory. |
| 4 | |
| 5 | ROOT=${abs_top_builddir:-$(dirname "$0")/..} |
| 6 | TESTDIR=${abs_top_testdir:-$(dirname "$0")} |
| 7 | |
| 8 | source "${TESTDIR}/common" |
| 9 | |
| 10 | trap "cleanup" SIGTERM EXIT |
| 11 | |
| 12 | function cleanup() |
| 13 | { |
| 14 | rm -rf "${workdir}" |
| 15 | } |
| 16 | |
| 17 | workdir="$(mktemp -d)" || exit 1 |
| 18 | |
| 19 | # Simple tests with options and combination of options |
| 20 | |
| 21 | if ! ${SWTPM_SETUP} --help &>/dev/null; then |
| 22 | echo "Error: Displaying help screen failed" |
| 23 | exit 1 |
| 24 | fi |
| 25 | |
| 26 | if ! ${SWTPM_SETUP} --version &>/dev/null; then |
| 27 | echo "Error: Displaying version info failed" |
| 28 | exit 1 |
| 29 | fi |
| 30 | |
| 31 | # Omit --tpm-state |
| 32 | if ${SWTPM_SETUP} --overwrite 2>/dev/null; then |
| 33 | echo "Error: Should have failed without --tpmstate option" |
| 34 | exit 1 |
| 35 | fi |
| 36 | |
| 37 | # Options that require --tpm2 |
| 38 | for opt in --ecc --create-spk --reconfigure --allow-signing --decryption '--pcr-banks -'; do |
| 39 | if ${SWTPM_SETUP} --tpmstate "dir://${workdir}" --overwrite ${opt:+${opt}} 2>/dev/null; then |
| 40 | echo "Error: Option ${opt} should have required --tpm2" |
| 41 | exit 1 |
| 42 | fi |
| 43 | done |
| 44 | |
| 45 | # Unreasonble RSA key size |
| 46 | if ${SWTPM_SETUP} --tpmstate "dir://${workdir}" --overwrite --tpm2 --rsa-keysize 2222 &>/dev/null; then |
| 47 | echo "Error: Should have failed with unreasonable key size" |
| 48 | exit 1 |
| 49 | fi |
| 50 | |
| 51 | # Unsupported option with --tpm2 |
| 52 | if ${SWTPM_SETUP} --tpmstate "dir://${workdir}" --overwrite --tpm2 --take-ownership 2>/dev/null; then |
| 53 | echo "Error: Option ${opt} should have failed with --tpm2" |
| 54 | exit 1 |
| 55 | fi |
| 56 | |
| 57 | # Unsupported cipher |
| 58 | if ${SWTPM_SETUP} --tpmstate "dir://${workdir}" --overwrite --cipher aes-192-cbc &>/dev/null; then |
| 59 | echo "Error: Should have failed on unsupported cipher aes-192-cbc" |
| 60 | exit 1 |
| 61 | fi |
| 62 | |
| 63 | # Unsupported option combination |
| 64 | if ${SWTPM_SETUP} --tpmstate "dir://${workdir}" --overwrite --tpm2 --create-ek-cert --reconfigure 2>/dev/null; then |
| 65 | echo "Error: Should have failed on unsupported option combination" |
| 66 | exit 1 |
| 67 | fi |
| 68 | |
| 69 | echo "Test 1: Ok" |
| 70 | cleanup |
| 71 | |
| 72 | if [ "$(id -u)" -eq 0 ]; then |
| 73 | echo "Skipping fruther tests: Not running tests as root." |
| 74 | exit 0 |
| 75 | fi |
| 76 | |
| 77 | FILES="swtpm-localca.conf swtpm-localca.options swtpm_setup.conf" |
| 78 | |
| 79 | if ! XDG_CONFIG_HOME="${workdir}" ${SWTPM_SETUP} \ |
| 80 | --create-config-files 1>/dev/null; |
| 81 | then |
| 82 | echo "Error: Could not created config files (without parameters)" |
| 83 | exit 1 |
| 84 | fi |
| 85 | for f in ${FILES}; do |
| 86 | if ! [ -f "${workdir}/${f}" ]; then |
| 87 | echo "Error: File ${workdir}/${f} was not created" |
| 88 | exit 1 |
| 89 | fi |
| 90 | done |
| 91 | if ! [ -d "${workdir}/var/lib/swtpm-localca" ]; then |
| 92 | echo "Error: Directory var/lib/swtpm-localca was not created" |
| 93 | exit 1 |
| 94 | fi |
| 95 | |
| 96 | if ! XDG_CONFIG_HOME="${workdir}" ${SWTPM_SETUP} \ |
| 97 | --create-config-files skip-if-exist 1>/dev/null; |
| 98 | then |
| 99 | echo "Error: skip-if-exists should have exit'ed with 0." |
| 100 | exit 1 |
| 101 | fi |
| 102 | |
| 103 | echo "Test 2: Ok" |
| 104 | cleanup |
| 105 | |
| 106 | if ! XDG_CONFIG_HOME="${workdir}" ${SWTPM_SETUP} \ |
| 107 | --create-config-files skip-if-exist 1>/dev/null; |
| 108 | then |
| 109 | echo "Error: skip-if-exists should have exit'ed with 0." |
| 110 | exit 1 |
| 111 | fi |
| 112 | for f in ${FILES}; do |
| 113 | if ! [ -f "${workdir}/${f}" ]; then |
| 114 | echo "Error: File ${workdir}/${f} was not created" |
| 115 | exit 1 |
| 116 | fi |
| 117 | done |
| 118 | if ! [ -d "${workdir}/var/lib/swtpm-localca" ]; then |
| 119 | echo "Error: Directory var/lib/swtpm-localca was not created" |
| 120 | exit 1 |
| 121 | fi |
| 122 | |
| 123 | echo "Test 3: Ok" |
| 124 | cleanup |
| 125 | |
| 126 | exit 0 |