Debian Codegraph @

src/luks/tests/get-hash-luks1

raw
1 #!/bin/bash -ex
2 #
3 # Copyright (c) 2026 Red Hat, Inc.
4 # Author: Sergio Arroutbi <[email protected]>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 TEST=$(basename "${0}")
20 . luks-common-test-functions
21
22 . clevis-luks-common-functions
23
24 on_exit() {
25 [ -d "${TMP}" ] && rm -rf "${TMP}"
26 }
27
28 trap 'on_exit' EXIT
29 trap 'exit' ERR
30
31 TMP="$(mktemp -d)"
32
33 # Test 1: clevis_luks_get_hash with empty argument should fail.
34 if clevis_luks_get_hash ""; then
35 error "${TEST}: clevis_luks_get_hash should fail with empty argument."
36 fi
37
38 # Test 2: clevis_luks_get_hash with non-existent device should fail.
39 if clevis_luks_get_hash "/dev/nonexistent-device-XYZ"; then
40 error "${TEST}: clevis_luks_get_hash should fail with non-existent device."
41 fi
42
43 # Test 3: LUKS1 device with default hash (sha256) — verify correct extraction.
44 DEV="${TMP}/luks1-device-sha256"
45 new_device_hash "luks1" "${DEV}" "sha256"
46
47 hash=$(clevis_luks_get_hash "${DEV}")
48 if [ "${hash}" != "sha256" ]; then
49 error "${TEST}: expected sha256, got '${hash}'."
50 fi
51
52 # Test 4: LUKS1 device with sha512 — verify correct extraction.
53 DEV="${TMP}/luks1-device-sha512"
54 new_device_hash "luks1" "${DEV}" "sha512"
55
56 hash=$(clevis_luks_get_hash "${DEV}")
57 if [ "${hash}" != "sha512" ]; then
58 error "${TEST}: expected sha512, got '${hash}'."
59 fi
60
61 # Test 5: LUKS1 device with sha384 — verify correct extraction.
62 DEV="${TMP}/luks1-device-sha384"
63 new_device_hash "luks1" "${DEV}" "sha384"
64
65 hash=$(clevis_luks_get_hash "${DEV}")
66 if [ "${hash}" != "sha384" ]; then
67 error "${TEST}: expected sha384, got '${hash}'."
68 fi
69
70 # Test 6: LUKS1 device with sha1 — verify correct extraction.
71 DEV="${TMP}/luks1-device-sha1"
72 new_device_hash "luks1" "${DEV}" "sha1"
73
74 hash=$(clevis_luks_get_hash "${DEV}")
75 if [ "${hash}" != "sha1" ]; then
76 error "${TEST}: expected sha1, got '${hash}'."
77 fi
78
79 # Test 7: LUKS1 device with ripemd160 — verify correct extraction.
80 DEV="${TMP}/luks1-device-ripemd160"
81 new_device_hash "luks1" "${DEV}" "ripemd160"
82
83 hash=$(clevis_luks_get_hash "${DEV}")
84 if [ "${hash}" != "ripemd160" ]; then
85 error "${TEST}: expected ripemd160, got '${hash}'."
86 fi
87
88 # Test 8: clevis_luks_get_hash with a non-LUKS file should fail.
89 DEV="${TMP}/not-a-luks-device"
90 fallocate -l64M "${DEV}" 2>/dev/null \
91 || dd if=/dev/zero of="${DEV}" bs=1M count=64 status=none
92 if clevis_luks_get_hash "${DEV}"; then
93 error "${TEST}: clevis_luks_get_hash should fail on a non-LUKS file."
94 fi