|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/bin/bash -efu |
|
|
2 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 3 |
|
| 4 |
cwd="${0%/*}" |
| 5 |
|
| 6 |
tmpdir="$(mktemp -d)" |
| 7 |
trap 'rm -rf "$tmpdir"' EXIT |
| 8 |
|
| 9 |
tab=' ' |
| 10 |
mnt="$tmpdir/mnt" |
| 11 |
mkdir -p "$mnt" |
| 12 |
lukskeys="$tmpdir/luks.keys" |
| 13 |
LUKS_ROOT="/dev/sda2" |
| 14 |
ID_FS_UUID="target-uuid-1234" |
| 15 |
|
| 16 |
pkcs11_luks_key="$tmpdir/pkcs11-luks.key" |
| 17 |
|
| 18 |
# Mock functions needed by findkey |
| 19 |
message() { |
| 20 |
echo "message: $*" | sed "s#$tmpdir#<TMPDIR>#g" |
| 21 |
} |
| 22 |
|
| 23 |
get_dev() { |
| 24 |
local val="$1" |
| 25 |
[ "$val" = "UUID=$ID_FS_UUID" ] || [ "$val" = "$LUKS_ROOT" ] |
| 26 |
} |
| 27 |
|
| 28 |
mount() { |
| 29 |
echo "mount: $*" | sed "s#$tmpdir#<TMPDIR>#g" |
| 30 |
return 1 |
| 31 |
} |
| 32 |
|
| 33 |
read_pkcs11_key() { |
| 34 |
echo "read_pkcs11_key: $*" |
| 35 |
path="$pkcs11_luks_key" |
| 36 |
touch "$path" |
| 37 |
return 0 |
| 38 |
} |
| 39 |
|
| 40 |
# Extract findkey from 085-luks |
| 41 |
eval "$(sed -n '/^findkey() {/,/^}/p' features/luks/data/lib/uevent/handlers/085-luks)" |
| 42 |
|
| 43 |
run_test() { |
| 44 |
local desc="$1" |
| 45 |
echo "=== $desc ===" |
| 46 |
local rc=0 |
| 47 |
( findkey ) || rc=$? |
| 48 |
echo "rc=$rc" |
| 49 |
} |
| 50 |
|
| 51 |
mkdir -p "$tmpdir/keys" |
| 52 |
touch "$tmpdir/keys/target.key" |
| 53 |
touch "$tmpdir/keys/other.key" |
| 54 |
|
| 55 |
# |
| 56 |
# Regression tests for commit ba53c151: |
| 57 |
# When keydev is empty (key-path<TAB><TAB>UUID=...), IFS tab-splitting in bash |
| 58 |
# must not collapse consecutive tabs. Otherwise, UUID= is assigned to keydev |
| 59 |
# instead of luksdev, leading to an invalid mount attempt and early abort. |
| 60 |
# |
| 61 |
|
| 62 |
printf '%s\t\tUUID=%s\n' "$tmpdir/keys/target.key" "$ID_FS_UUID" > "$lukskeys" |
| 63 |
run_test "Test 1: Empty keydev with matching luksdev (key-path<TAB><TAB>UUID=...)" |
| 64 |
|
| 65 |
{ |
| 66 |
printf '%s\t\tUUID=%s\n' "$tmpdir/keys/other.key" "other-uuid-9999" |
| 67 |
printf '%s\t\tUUID=%s\n' "$tmpdir/keys/target.key" "$ID_FS_UUID" |
| 68 |
} > "$lukskeys" |
| 69 |
run_test "Test 2: Skip non-matching entry and find matching (empty keydev)" |
| 70 |
|
| 71 |
printf 'pkcs11:model=test;id=1\t\tUUID=%s\n' "$ID_FS_UUID" > "$lukskeys" |
| 72 |
run_test "Test 3: PKCS#11 key with empty keydev (filters/lukskeys format)" |
| 73 |
|
| 74 |
printf '%s\n' "$tmpdir/keys/target.key" > "$lukskeys" |
| 75 |
run_test "Test 4: Single field (key-path only)" |
| 76 |
|
| 77 |
mount() { |
| 78 |
echo "mount: $*" | sed "s#$tmpdir#<TMPDIR>#g" |
| 79 |
return 0 |
| 80 |
} |
| 81 |
mkdir -p "$tmpdir/mnt/custom" |
| 82 |
touch "$tmpdir/mnt/custom/key.bin" |
| 83 |
printf 'custom/key.bin\t/dev/sdb1\tUUID=%s\n' "$ID_FS_UUID" > "$lukskeys" |
| 84 |
run_test "Test 5: All 3 fields (key-path, keydev, luksdev)" |
| 85 |
|
| 86 |
printf '\t\tUUID=%s\n' "$ID_FS_UUID" > "$lukskeys" |
| 87 |
run_test "Test 6: Empty keypath error" |