-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtests.sh
More file actions
executable file
·482 lines (383 loc) · 10.2 KB
/
tests.sh
File metadata and controls
executable file
·482 lines (383 loc) · 10.2 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#! /usr/bin/env bash
# Globals
BDEVIN=
BDEVOUT=
Z=$(basename $0)
TESTDIR=/tmp/fastdd
Verbose=
set -o pipefail
die() {
echo "$Z: $@" 1>&2
exit 1
}
warn() {
echo "$Z: $@" 1>&2
}
uname=$(uname)
case $uname in
Linux*)
MD5=md5sum
filesz() {
local fn=$1
local sz=$(stat -c '%s' $fn)
echo $sz
return 0
}
;;
Darwin|OpenBSD)
MD5='md5 -q'
filesz() {
local fn=$1
eval $(stat -s $fn)
echo $st_size
return 0
}
;;
*) die "Don't know how to do md5 on $uname"
;;
esac
FASTDD=
for d in rel dbg; do
f=$(uname)-$d/fastdd
if [ -x $f ]; then
FASTDD=$f
break
fi
done
[ -z $FASTDD ] && die "can't find fastdd in $(uname)-rel or $(uname)-dbg"
main() {
local prev=
local opt=
local optarg=
local args=
for opt in $*; do
shift
if [ -n "$prev" ]; then
eval "$prev=\$opt"
prev=
continue
fi
case $opt in
-*=*) optarg=`echo "$opt" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
*) optarg= ;;
esac
case $opt in
--help|-h|--hel|--he|--h) usage ;;
--blockdev-in=*|--in-blockdev=*) BDEVIN=$optarg ;;
--blockdev-out=*|--out-blockdev=*) BDEVOUT=$optarg ;;
--blockdev-in|--in-blockdev|-b) prev=BDEVIN ;;
--blockdev-out|--out-blockdev|-B) prev=BDEVOUT ;;
--debug|-x) set -x ;;
--verbose|-v) Verbose=1 ;;
-*) die "Unknown option '$opt'" ;;
*)
args="$args $opt"
#for xx
#do
#args="$args $xx"
#done
#break
;;
esac
done
[ -z "$args" ] && usage
mkdir -p $TESTDIR
for f in $args; do
readtest $f
done
return 0
}
usage() {
cat <<EOF
$0 - Run dd vs. fastdd tests from a test-description file.
Usage: $0 [options] test-file [test-file ..]
Options:
-h, --help Show this help message and quit
-x Run in debug/trace mode [False]
-v, --verbose Run in verbose mode [False]
-b B, --blockdev-in=B Use 'B' as the input block dev []
-B B, --blockdev-out=B Use 'B' as the output block dev []
EOF
exit 0
}
# generate one test
onetest() {
local num=$1; shift # test number
local inf=$1; shift
local outf=$1; shift
local bs=$1; shift
local count=$1; shift
local args="$@"
local tmp="${TESTDIR}/${num}"
local src=$(makefile $inf i $tmp)
local dst=$(makefile $outf o $tmp)
local ddargs=$(ddargs $args)
local input=$tmp/input
local dsta=
local dstb=
local ddbs=$(parsesize $bs)
local ddcount=$(parsesize $count)
mkdir -p $tmp || die "$num: can't mkdir $tmp"
args="$args bs=$bs count=$count"
ddargs="$ddargs bs=$ddbs count=$ddcount"
# Generate input first.
geninput $input $bs $count
# file output when dest is a pipe
local pipea=
local pipeb=
# command prefixes and suffixes for fastdd & dd
local prefa=
local prefb=
local suffa=
local suffb=
# read from file or pipe and setup input command prefix
case $inf in
fil*)
prefa=""
#prefb="dd if=$input"
;;
pip*)
prefa="cat"
;;
block*|bdev)
input=$BDEVIN
prefa=""
;;
*) die "$num: Unknown source type '$inf' (not file|pipe|bdev)" ;;
esac
case $outf in
fil*)
dst=${tmp}/outf${RANDOM}
dsta=${dst}a
dstb=${dst}b
suffa=""
mda="$dsta"
mdb="$dstb"
;;
pip*)
# capture output via pipe into a file
pipea="$tmp/pipea${RANDOM}"
pipeb="$tmp/pipeb${RANDOM}"
suffa="cat"
mda="$pipea"
mdb="$pipeb"
;;
block*|bdev)
# don't write to _two_ block devices!
dsta=${BDEVOUT}
dstb=$tmp/block${RANDOM}
suffa=""
mda="$dsta"
mdb="$dstb"
;;
*) die "$num: Unknown dest type '$outf' (not file|pipe|bdev)"
;;
esac
# Run fastdd & dd; compare $mda vs. $mdb
#
# XXX Ugly code ahead; no clean way to express the 4 different
# combinations. ideally I'd like to have done 'eval "$prefa $args $suffa"'
case $prefa in
cat)
case $suffa in
cat)
#verbose " cat $input | fastdd $args | cat - > $pipea"
#verbose " cat $input | dd $ddargs | cat - > $pipeb"
(cat $input | $FASTDD -q $args | cat - > $pipea) || die "$num: can't run fastdd"
(cat $input | xdd $ddargs | cat - > $pipeb) || die "$num: can't run dd"
;;
*)
#verbose " cat $input | fastdd $args of=$dsta"
#verbose " cat $input | dd $ddargs of=$dstb"
(cat $input | $FASTDD -q $args of=$dsta) || die "$num: can't run fastdd"
(cat $input | xdd $ddargs of=$dstb) || die "$num: can't run dd"
;;
esac
;;
*)
case $suffa in
cat)
#verbose " fastdd if=$input $args | cat - > $pipea"
#verbose " dd if=$input $ddargs | cat - > $pipeb"
($FASTDD -q if=$input $args | cat - > $pipea) || die "$num: can't run fastdd"
(xdd if=$input $ddargs | cat - > $pipeb) || die "$num: can't run dd"
;;
*)
#verbose " fastdd if=$input $args of=$dsta"
#verbose " dd if=$input $ddargs of=$dstb"
$FASTDD -q if=$input $args of=$dsta || die "$num: can't run fastdd"
xdd if=$input $ddargs of=$dstb || die "$num: can't run dd"
;;
esac
;;
esac
#verbose " md5 $mda $mdb"
local x=$(xcksum $mda)
local y=$(xcksum $mdb)
if [ $x != $y ]; then
die "$num: checksum mismatch $mda vs. $mdb".
else
rm -rf $tmp
fi
return 0
}
# Generate an input file of a given size - only if needed
geninput() {
local fn=$1
local bs=$(parsesize $2)
local count=$(parsesize $3)
local want=$(( $bs * $count ))
local sz=
[ -f $fn ] || touch $fn
sz=$(filesz $fn)
if [ $sz -lt $want ]; then
# Make a suitable file
verbose " gen input bs=$bs count=$count file=$input"
xdd if=/dev/urandom of=$input bs=$want count=1 || die "Can't generate $want sized input"
fi
return 0
}
# convert fastdd syntax to dd syntax for the optional flags
ddargs() {
local a=
local v=
local s=
for a in "$@"; do
case $a in
iosize=*) ;;
# ignore this
iflag*) ;;
# Traditional dd doesn't handle 'trunc' or 'creat'. Only
# the "no*" equivalents.
oflag=*)
s=${a##oflag=}
case $s in
trunc|creat) ;;
*) v="$v conv=$s" ;;
esac
;;
*) v="$v $a" ;;
esac
done
echo $v
return 0
}
xdd() {
dd "$@" 2>/dev/null
return $?
}
# make a filename from a file type. e.g., from "file" - generate a
# random file name. For a blockdev, return the global $BLOCKDEV
makefile() {
local ty=$1
local pref=$2
local tmp=$3
local fn=
local dn=
case $ty in
file|fil*)
fn=${tmp}/${pref}f${RANDOM}
;;
pipe|pip*) fn=
;;
block*|bdev)
case $pref in
i*) fn=$BDEVIN ;;
o*) fn=$BDEVOUT ;;
*) ;;
esac
;;
*) die "Unknown type '$ty' (not file|pipe|bdev)" ;;
esac
echo $fn
return 0
}
readtest() {
local fn=$1; shift
local n=0
grep -v '#' $fn | while read ll; do
n=$(( $n + 1 ))
[ -z "$ll" ] && continue
#ll=$(echo $ll| sed -e 's/ */:/')
#IFS=:
set -- $ll
local src=$1; shift
local dst=$1; shift
local count=$1; shift
local bs=$1; shift
local args="$@"
if [ -z "$bs" ]; then
die "$fn: malformed line: $ll"
fi
if [ -z "$BDEVIN" ]; then
case $src in
bdev|block*)
warn "$fn: No blockdev specified; skipping [$ll] .."
continue
;;
*)
;;
esac
fi
if [ -z "$BDEVOUT" ]; then
case $dst in
bdev|block*)
warn "$fn: No blockdev specified; skipping [$ll] .."
continue
;;
*)
;;
esac
fi
verbose "$fn: $n ($src $dst $bs x $count)"
onetest $n $src $dst $bs $count $args
done
}
# calculate md5 and return it
xcksum() {
local fn=$1
set -- $($MD5 $fn)
echo $1
return 1
}
verbose() {
[ -n "$Verbose" ] && echo "$@"
}
# Size suffixes
_KB=1024
_MB=1048576
_GB=$(( 1024 * $_MB ))
_TB=$(( 1024 * $_GB ))
_PB=$(( 1024 * $_TB ))
# Parse a size suffix
parsesize() {
local s=$1
case $s in
[0-9]*[kK])
s=${s%%k}
s=${s%%K}
s=$(( $s * _KB ))
;;
[0-9]*M)
s=${s%%M}
s=$(( $s * _MB ))
;;
[0-9]*G)
s=${s%%G}
s=$(( $s * _GB ))
;;
[0-9]*T)
s=${s%%T}
s=$(( $s * _TB ))
;;
[0-9]*P)
s=${s%%P}
s=$(( $s * _PB ))
;;
[0-9]*) ;;
*) die "$s is not a number or size?"
esac
echo $s
return 0
}
main "$@"