forked from rfjakob/cshatag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.sh
executable file
·380 lines (311 loc) · 8.43 KB
/
test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/bin/bash
#
# Copyright (C) 2018 Tim Schlueter
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# In addition, as a special exception, the author of this program
# gives permission to link the code portions of this program with the
# OpenSSL library under certain conditions as described in each file,
# and distribute linked combinations including the two.
# You must obey the GNU General Public License in all respects for all
# of the code used other than OpenSSL. If you modify this file(s)
# with this exception, you may extend this exception to your version
# of the file(s), but you are not obligated to do so. If you do not
# wish to do so, delete this exception statement from your version.
# If you delete this exception statement from all source files in the
# program, then also delete it here.
#
# This is a simple sanity test for b2tag
#
RET=0
# Allow overriding the test file any test message: simply set the
# corresponding environment variable.
TEST_FILE=${TEST_FILE:-test.txt}
TEST_MESSAGE=${TEST_MESSAGE:-The quick brown fox jumped over the lazy dog.}
DEFAULT_ALG=blake2b
function fail() {
echo "$*" >&2
return 1
}
function info() {
if [[ -n $V && $V -ge 1 ]]; then
echo "$*"
fi
}
function to_hex() {
xxd -p | tr -d '\n'
}
function from_hex() {
xxd -p -r
}
# Rudimentary *sum function for blake2s. The coreutils b2sum utility only
# works with blake2b hashes, so we have to use openssl to verify blake2s.
function b2ssum() (
set -e
if [[ ! $1 = -c ]]; then
openssl dgst -r -blake2s256 "$@" | sed -r 's/ \*/ /'
return 0
fi
# $1 == -c
shift
hashes=$(cat "$@") \
|| fail "Failed to read expected hashes: $?"
while IFS='' read -r line; do
test -n "$line" || continue
file=$(sed -r "s/^\S+ [ *](.+)$/\1/" <<<"$line")
expect=$(sed -r "s/^(\S+) [ *].+/\1/" <<<"$line")
hash=$(b2ssum "$file" | sed -r "s/^(\S+) [ *].+/\1/")
if [[ ! $expect = $hash ]]; then
fail "$file: FAILED"
return 1
fi
done <<<"$hashes"
return 0
)
function hash() {
local alg="${1:-$DEFAULT_ALG}"
local prog
shift
case "$alg" in
md5|sha1|sha256|sha512)
prog=${alg}sum
;;
blake2|blake2b|blake2b512)
prog=b2sum
;;
blake2s|blake2s256)
# Stub function since we can't use b2sum
prog=b2ssum
;;
*)
fail "Unknown hash algorithm: $alg"
return 1
;;
esac
"${prog}" "$@" | cut -d' ' -f1
}
function get_mtime() {
stat --format='%.Y' "$@"
}
function attr_name() {
local attr
case "$1" in
blake2|blake2b|blake2b512)
attr=blake2b512
;;
blake2s|blake2s256)
attr=blake2s256
;;
'')
[[ -n $DEFAULT_ALG ]] || return 1
attr_name "$DEFAULT_ALG"
return $?
;;
*)
attr="$1"
;;
esac
echo "user.shatag.$attr"
}
function get_attr_hex() {
local attr=$(attr_name "$1")
local file="$2"
[[ $# -eq 2 ]] || return 1
# getfattr can't mix --only-values with --encoding
getfattr --only-values --name="$attr" "$file" 2>/dev/null | to_hex
}
function set_attr_hex() {
local attr=$(attr_name "$1")
local val="$2"
local file="$3"
[[ $# -eq 3 ]] || return 1
setfattr --name="$attr" --value="0x$val" "$file"
}
function clear_attr() {
local attr=$(attr_name "$1")
local file="$2"
[[ $# -ge 2 ]] || return 1
setfattr --remove="$attr" "$file" 2>/dev/null
# Return success if the attribute doesn't exist, and failure if it does
! getfattr --name="$attr" "$file" &>/dev/null
}
function print_ts() {
local ts_hex="$1"
if [[ -z $ts_hex ]]; then
echo "Timestamp: <empty>"
elif [[ $ts_hex =~ ^(3[0-9]|2[Ee])+$ ]]; then
echo "Timestamp: $(from_hex <<<"$ts_hex")"
else
echo "Timestamp:"
# Do a pretty hex dump rather than the plain hex
from_hex <<<"$ts_hex" | xxd
fi
}
function print_hash() {
local hash_hex="$1"
local alg="$2"
if [[ -z $hash_hex ]]; then
echo "${alg^} hash: <empty>"
elif [[ $hash_hex =~ ^(3[0-9]|[46][1-6])+$ ]]; then
echo "${alg^} hash: $(from_hex <<<"$hash_hex")"
else
echo "${alg^} hash:"
# Do a pretty hex dump rather than the plain hex
from_hex <<<"$hash_hex" | xxd
fi
}
function check_ts() {
local err expect file ts ts_hex
file="$1"
# Blank expect = timestamp should be unset
expect="$2"
ts_hex=$(get_attr_hex ts "$file")
err=$?
if [[ -z $expect ]]; then
if [[ $err -eq 0 ]]; then
fail "Timestamp already set."
print_ts "$ts_hex"
return 1
elif [[ -n $ts_hex ]]; then
fail "Timestamp is not empty."
print_ts "$ts_hex"
return 1
fi
return 0
fi
if [[ $err -ne 0 ]]; then
fail "Could not read timestamp attribute: $err"
return 1
elif [[ -z $ts_hex ]]; then
fail "Empty timestamp attribute (expected $expect)"
return 1
elif [[ $ts_hex =~ ^(3[0-9]|2[Ee])+$ ]]; then
ts=$(from_hex <<<"$ts_hex")
if [[ ! $expect = $ts ]]; then
fail "Timestamp mismatch: '$expect' != '$ts'"
return 1
fi
else
fail "Timestamp contains non-numeric characters (0-9 and .)"
print_ts "$ts_hex"
return 1
fi
return 0
}
function check_hash() {
local alg err expect file hash hash_hex
file="$1"
# Blank expect = hash should be unset
expect="$2"
alg="${3:-$DEFAULT_ALG}"
hash_hex=$(get_attr_hex "$alg" "$file")
err=$?
if [[ -z $expect ]]; then
if [[ $err -eq 0 ]]; then
fail "${alg^} hash already set."
print_hash "$hash_hex" "$alg"
return 1
elif [[ -n $hash_hex ]]; then
fail "${alg^} hash is not empty."
print_hash "$hash_hex" "$alg"
return 1
fi
return 0
fi
if [[ $err -ne 0 ]]; then
fail "Could not read $alg hash attribute: $err"
return 1
elif [[ -z $hash_hex ]]; then
fail "Empty $alg hash attribute (expected $expect)"
return 1
elif [[ $hash_hex =~ ^(3[0-9]|[46][1-6])+$ ]]; then
if [[ $expect = * ]]; then
return 0
fi
hash=$(from_hex <<<"$hash_hex")
if [[ ! $expect = $hash ]]; then
fail "${alg^} hash mismatch: '$expect' != '$hash'"
return 1
fi
else
fail "${alg^} hash contains non-numeric characters (0-9 and .)"
print_hash "$hash_hex" "$alg"
return 1
fi
return 0
}
set -o pipefail
# set -x if the V environment variable is >= 2
if [[ -n $V && $V -ge 2 ]]; then
set -x
else
args=-q
fi
if [[ -e $TEST_FILE ]]; then
fail "Warning: test.txt already exists. Removing."
rm "$TEST_FILE" \
|| fail "Could not remove old test file" \
|| let RET++
fi
echo "$TEST_MESSAGE" > "$TEST_FILE" \
|| fail "Could not create test file: $?" \
|| let RET++
check_ts "$TEST_FILE" "" || let RET++
check_hash "$TEST_FILE" "" $ALG || let RET++
# Test setup: create the test file, hash the test message, and grab the
# test file's modified time
for ALG in '' blake2b blake2s md5 sha1 sha256 sha512; do
HASH=$(echo "$TEST_MESSAGE" | hash $ALG) \
|| fail "Could not generate $ALG reference hash: $?" \
|| let RET++
MTIME=$(get_mtime "$TEST_FILE") \
|| fail "Could not read test file mtime: $?" \
|| let RET++
ALG_NAME="${ALG:-default}"
# Make sure the newly-created test file doesn't have the shatag xattrs
info "Test sanity ($ALG_NAME)"
check_ts "$TEST_FILE" "" || let RET++
check_hash "$TEST_FILE" "" $ALG || let RET++
# Make sure b2tag doesn't add xattrs when -n is given
info "Test dry-run ($ALG_NAME)"
./b2tag -n $args --$ALG "$TEST_FILE" \
|| fail "b2tag returned failure: $?" \
|| let RET++
check_ts "$TEST_FILE" "" || let RET++
check_hash "$TEST_FILE" "" $ALG || let RET++
# Make sure b2tag adds the proper xattrs
info "Test new file ($ALG_NAME)"
./b2tag $args --$ALG "$TEST_FILE" \
|| fail "b2tag returned failure: $?" \
|| let RET++
check_ts "$TEST_FILE" "$MTIME" || let RET++
check_hash "$TEST_FILE" "$HASH" $ALG || let RET++
# Print test
info "Test verify hashes with <hash>sum ($ALG_NAME)"
./b2tag -p $args --$ALG "$TEST_FILE" | hash "$ALG" -c - >/dev/null \
|| fail "hash verification failed: ${PIPESTATUS[*]}" \
|| let RET++
(( RET == 0 )) || break
clear_attr ts "$TEST_FILE"
clear_attr "$ALG" "$TEST_FILE"
done
# If the test was successful, remove the test file
if [[ $RET -eq 0 ]]; then
echo "All tests successful"
rm -f "$TEST_FILE"
else
fail "$RET test failures"
fi
exit $RET