Debian Codegraph @

src/luks/tests/list-multiple-slots-luks2

raw
1 #!/bin/bash -ex
2 # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
3 #
4 # Copyright (c) 2024 Oldřich Jedlička
5 # Author: Oldřich Jedlička <[email protected]>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 TEST=$(basename "${0}")
22 . luks-common-test-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 ADV="${TMP}/adv.jws"
34 tang_create_adv "${TMP}" "${ADV}"
35 PIN1="sss"
36 PINS1="sss tang"
37 CFG1=$(printf '
38 {
39 "t": 1,
40 "pins": {
41 "tang": [
42 {
43 "url": "ADDR","adv": "%s"
44 }
45 ]
46 }
47 }
48 ' "${ADV}")
49 PIN2="null"
50 PINS2="null"
51 CFG2='{}'
52 ALLPINS="null sss tang"
53
54 # LUKS2
55 DEV="${TMP}/luks1-device"
56 new_device "luks2" "${DEV}"
57
58 if ! clevis luks bind -f -d "${DEV}" "${PIN1}" "${CFG1}" <<< "${DEFAULT_PASS}"; then
59 error "${TEST}: Binding is expected to succeed when given a correct (${DEFAULT_PASS}) password."
60 fi
61
62 if ! clevis luks bind -f -d "${DEV}" "${PIN2}" "${CFG2}" <<< "${DEFAULT_PASS}"; then
63 error "${TEST}: Binding is expected to succeed for null pin."
64 fi
65
66 test_values() {
67 local SLT="$1"
68 local slot="$2"
69 local pin="$3"
70 local cfg="$4"
71
72 case $SLT in
73 1)
74 if [[ "${slot}" != "${SLT}:" ]]; then
75 error "${TEST}: slot (${slot}) is expected to be ${SLT}"
76 fi
77
78 if [[ "${pin}" != "${PIN1}" ]]; then
79 error "${TEST}: pin (${pin}) is expected to be '${PIN1}'"
80 fi
81
82 to_remove_from_cfg=$(printf ',"adv": "%s"' "${ADV}")
83 cfg_for_cmp=${CFG1//"${to_remove_from_cfg}"/} #"
84 if ! pin_cfg_equal "${cfg}" "${cfg_for_cmp}"; then
85 error "${TEST}: config obtained from clevis luks list (${cfg}) is expected to match the one used to bind the test (${cfg_for_cmp})"
86 fi
87 ;;
88 2)
89 if [[ "${slot}" != "${SLT}:" ]]; then
90 error "${TEST}: slot (${slot}) is expected to be ${SLT}"
91 fi
92
93 if [[ "${pin}" != "${PIN2}" ]]; then
94 error "${TEST}: pin (${pin}) is expected to be '${PIN2}'"
95 fi
96
97 if ! pin_cfg_equal "${cfg}" "${CFG2}"; then
98 error "${TEST}: config obtained from clevis luks list (${cfg}) is expected to match the one used to bind the test (${CFG2})"
99 fi
100 ;;
101 *)
102 error "${TEST}: unexpected slot ${SLT}"
103 ;;
104 esac
105 }
106
107 test_pin_values() {
108 local SLT="$1"
109 local slot="$2"
110 local pins="$3"
111
112 case $SLT in
113 1)
114 if [[ "${slot}" != "${SLT}:" ]]; then
115 error "${TEST}: used pins slot (${slot}) is expected to be ${SLT}"
116 fi
117
118 if [[ "${pins}" != "${PINS1}" ]]; then
119 error "${TEST}: used pins (${pins}) are expected to be '${PINS1}'"
120 fi
121 ;;
122 2)
123 if [[ "${slot}" != "${SLT}:" ]]; then
124 error "${TEST}: used pins slot (${slot}) is expected to be ${SLT}"
125 fi
126
127 if [[ "${pins}" != "${PINS2}" ]]; then
128 error "${TEST}: used pins (${pins}) are expected to be '${PINS2}'"
129 fi
130 ;;
131 *)
132 error "${TEST}: unexpected slot ${SLT}"
133 ;;
134 esac
135 }
136
137 SLT=1
138 if ! read -r slot pin cfg < <(clevis luks list -d "${DEV}" -s "${SLT}"); then
139 error "${TEST}: clevis luks list is expected to succeed for device(${DEV}) and slot (${SLT})"
140 fi
141
142 test_values "${SLT}" "${slot}" "${pin}" "${cfg}"
143
144 if ! read -r slot pins < <(clevis luks list -d "${DEV}" -s "${SLT}" -p); then
145 error "${TEST}: clevis luks list -p is expected to succeed for device(${DEV}) and slot (${SLT})"
146 fi
147
148 test_pin_values "${SLT}" "${slot}" "${pins}"
149
150 SLT=2
151 if ! read -r slot pin cfg < <(clevis luks list -d "${DEV}" -s "${SLT}"); then
152 error "${TEST}: clevis luks list is expected to succeed for device(${DEV}) and slot (${SLT})"
153 fi
154
155 test_values "${SLT}" "${slot}" "${pin}" "${cfg}"
156
157 if ! read -r slot pins < <(clevis luks list -d "${DEV}" -s "${SLT}" -p); then
158 error "${TEST}: clevis luks list -p is expected to succeed for device(${DEV}) and slot (${SLT})"
159 fi
160
161 test_pin_values "${SLT}" "${slot}" "${pins}"
162
163 # Test both slots reading
164 if ! slots=$(clevis luks list -d "${DEV}"); then
165 error "${TEST}: clevis luks list is expected to succeed for device(${DEV})"
166 fi
167
168 read_slots=
169 while read -r slot pin cfg; do
170 read_slots="${read_slots}${read_slots:+ }${slot%:}"
171 test_values "${slot%:}" "${slot}" "${pin}" "${cfg}"
172 done <<< "$slots"
173
174 if [[ "${read_slots}" != "1 2" ]]; then
175 error "${TEST}: clevis luks list did not return all expected slots (1 2), it was (${read_slots}) for device(${DEV})"
176 fi
177
178 if ! slots=$(clevis luks list -d "${DEV}" -p); then
179 error "${TEST}: clevis luks list -p is expected to succeed for device(${DEV})"
180 fi
181
182 read_slots=
183 while read -r slot pins; do
184 read_slots="${read_slots}${read_slots:+ }${slot%:}"
185 test_pin_values "${slot%:}" "${slot}" "${pins}"
186 done <<< "$slots"
187
188 if [[ "${read_slots}" != "1 2" ]]; then
189 error "${TEST}: clevis luks list -p did not return all expected slots (1 2), it was (${read_slots}) for device(${DEV})"
190 fi
191
192 # Test clevis_luks_read_used_pins
193 . clevis-luks-common-functions
194
195 if ! pins=$(clevis_luks_read_used_pins "${DEV}"); then
196 error "${TEST}: clevis_luks_read_used_pins is expected to succeed for device(${DEV})"
197 fi
198
199 if [[ "${pins}" != "${ALLPINS}" ]]; then
200 error "${TEST}: clevis_luks_read_used_pins did not return all expected pins (${ALLPINS}), it was (${pins}) for device(${DEV})"
201 fi