| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
if ! [[ "$(uname -s)" =~ Linux ]]; then |
| 4 |
echo "This test currently only runs on Linux." |
| 5 |
exit 77 |
| 6 |
fi |
| 7 |
|
| 8 |
if [ "$(id -u)" -ne 0 ]; then |
| 9 |
echo "Need to be root to run this test." |
| 10 |
exit 77 |
| 11 |
fi |
| 12 |
|
| 13 |
STATEDIR="$(mktemp -d)" || exit 1 |
| 14 |
STATEIMG=$STATEDIR/tpm2.img |
| 15 |
STATEFILE="" |
| 16 |
|
| 17 |
trap "cleanup" SIGTERM EXIT |
| 18 |
function cleanup() |
| 19 |
{ |
| 20 |
rm -rf "$STATEDIR" |
| 21 |
if [ -n "$STATEFILE" ]; then |
| 22 |
losetup -d "$STATEFILE" |
| 23 |
fi |
| 24 |
} |
| 25 |
|
| 26 |
if [ -z "$(type -P fallocate)" ]; then |
| 27 |
echo "This test requires the fallocate tool." |
| 28 |
exit 77 |
| 29 |
fi |
| 30 |
|
| 31 |
if ! fallocate --help 2>&1 | grep -q -E "\--posix"; then |
| 32 |
echo "This test requires fallocate to support --posix." |
| 33 |
exit 77 |
| 34 |
fi |
| 35 |
|
| 36 |
if [ -z "$(type -P losetup)" ]; then |
| 37 |
echo "This test requires the losetup tool." |
| 38 |
exit 77 |
| 39 |
fi |
| 40 |
|
| 41 |
# allocate 4 MiB file |
| 42 |
fallocate --posix -l $((4 * 1024 * 1024)) "$STATEIMG" |
| 43 |
# and loop mount it |
| 44 |
STATEFILE=$(losetup --show -f "$STATEIMG") |
| 45 |
|
| 46 |
export SWTPM_TEST_LINEAR_FILE=1 |
| 47 |
export TPM_COMMAND_PORT=65462 |
| 48 |
export STATEFILE |
| 49 |
|
| 50 |
# don't exec so cleanup will remove the loop device |
| 51 |
"$(dirname "$0")/test_tpm2_save_load_state_2" |