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
|
#!/usr/bin/env sh
# Copyright 2017 Branden Robinson
#
# This software may be copied, modified, and used for any purpose
# without fee, provided that (1) the above copyright notice is
# retained, and (2) modified versions are clearly marked as having
# been modified, with the modifier's name and the date included.
# Set MKDISK in the environment to test an out-of-tree build.
: ${MKDISK:=./mkdisk}
PROGNAME="${0##*/}"
DUMP_OUTPUT=
ACTUAL=
EXPECTED=
TEST_NUMBER=0
check () {
TEST_NUMBER=$(( TEST_NUMBER + 1 ))
if [ "$ACTUAL" != "$EXPECTED" ]; then
fail "$*"
fi
}
die () {
echo "$PROGNAME: error: $@" >&2
exit 2
}
fail () {
echo "$PROGNAME: test failure: $@"
if [ -n "$DUMP_OUTPUT" ]; then
EFILE=$(mktemp "$PROGNAME".$TEST_NUMBER.XXX.expected)
echo "$@" >> "$EFILE"
echo "$EXPECTED" >> "$EFILE"
echo "$PROGNAME: expected output left in \"$EFILE\"" >&2
AFILE=$(mktemp "$PROGNAME".$TEST_NUMBER.XXX.actual)
echo "$@" >> "$AFILE"
echo "$ACTUAL" >> "$AFILE"
echo "$PROGNAME: actual output left in \"$AFILE\"" >&2
else
cat <<EOF
********************************************************************************
======== EXPECTED OUTPUT:
$EXPECTED
======== ACTUAL OUTPUT:
$ACTUAL
********************************************************************************
EOF
fi
}
usage () {
test -n "$*" && echo "$PROGNAME: usage error: $*"
cat <<EOF
$PROGNAME: test the mkdisk utility
Synopsis: $PROGNAME [--dump-output]
$PROGNAME --help
Options:
--dump-output If a test fails, leave actual and expected results in files.
--help Display this usage message and exit.
Exit Status:
Any nonzero exit status is accompanied by one or more diagnostic messages.
0 All tests passed.
1 At least one test failed.
2 At least one test could not be run.
3 Usage error.
4 Trouble with getopt utility.
5 Trouble with mktemp utility.
Environment:
MKDISK=$MKDISK
MKDISK Tells $PROGNAME where to find the executable under test.
Files:
If --dump-output is specified and one or more tests fail, diagnostic
messages will show where the expected and actual outputs of the test cases
can be found. Only failing test cases are written. The files are placed
and named using the mktemp utility.
See also:
getopt(1), mktemp(1)
EOF
}
# Parse options.
if ! OPTIONS=$(getopt -o "" --long dump-output,help -n "$PROGNAME" -- $@); then
# getopt already emits a diagnostic. Just show the usage message.
usage >&2
exit 3
fi
eval set -- "$OPTIONS"
while [ -n "$1" ]; do
case "$1" in
(--dump-output)
DUMP_OUTPUT=yes
;;
(--help)
usage
exit 0
;;
(--)
shift
break
;;
(*)
die "problem with getopt"
;;
esac
shift
done
if ! [ -x "$MKDISK" ]; then
die "mkdisk executable \"$MKDISK\" does not exist or is not executable"
fi
# Test default no-clobber behavior and overriding flag.
EXPECTED="disk-1: File exists
disk-3: File exists
disk-k: File exists
disk-h: File exists"
ACTUAL=$(for TYPE in 1 3 k h; do
FILE=disk-$TYPE;
test -f $FILE && echo $FILE surprisingly exists;
"$MKDISK" -$TYPE $FILE;
"$MKDISK" -$TYPE $FILE || "$MKDISK" -f -$TYPE $FILE;
rm -f $FILE;
test -f $FILE && echo $FILE surprisingly exists;
done 2>&1)
check "destination file default no-clobber behavior and override flag"
# Test truncation of filename embedded in hard drive image header.
EXPECTED="1
12
123
1234
12345
123456
1234567
12345678
123456789
$MKDISK warning: truncating filename inside hard disk image header to 8 bytes"
ACTUAL=$(for F in 1 12 123 1234 12345 123456 1234567 12345678 123456789; do
echo $F; "$MKDISK" -h $F 2>&1; rm -f $F; done)
check "filename truncation"
EXPECTED="0000100 1 2 3 4 5 6 7 8 \0 \0 \0 \0 \0 \0 \0 \0
0000120 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
0000400
$MKDISK warning: truncating filename inside hard disk image header to 8 bytes
0000100 1 2 3 4 5 6 7 8 \0 \0 \0 \0 \0 \0 \0 \0
0000120 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
0000400"
ACTUAL=$(for F in 12345678 123456789; do
"$MKDISK" -h $F 2>&1; od -c "$F" +100; rm -f $F; done)
check "no garbage in reserved area when in normal (non-spill) mode"
# Test spillage of filename embedded in hard drive image header.
EXPECTED="1
12
123
1234
12345
123456
1234567
12345678
123456789
$MKDISK warning: filename 123456789 spilled into reserved part of hard disk image header"
ACTUAL=$(for F in 1 12 123 1234 12345 123456 1234567 12345678 123456789; do
echo $F; "$MKDISK" -S -h $F 2>&1; rm -f $F; done)
check "filename spillage"
EXPECTED="0000100 1 2 3 4 5 6 7 8 \0 \0 \0 \0 \0 \0 \0 \0
0000120 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
0000400
$MKDISK warning: truncating filename inside hard disk image header to 8 bytes
0000100 1 2 3 4 5 6 7 8 \0 \0 \0 \0 \0 \0 \0 \0
0000120 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
0000400"
ACTUAL=$(for F in 12345678 123456789; do
"$MKDISK" -h $F 2>&1; od -c "$F" +100; rm -f $F; done)
check "no garbage in reserved area when in spill mode"
# Test prevention of buffer (structure) overflow.
EXPECTED="BUFFER.OVERFLOW.PREVENTION.CHECK.RESERVED.AREA.PLUS.FILENAME.IS.192.BYTES.ux2pahohvae7Veocoo9OceeRi9AeThooxeesh0ueb1thu8dohthoom7GieCh3iethienemooyudev7jahr1woot5Jaegeeth1toONE.HUNDRED.NINETY2
$MKDISK warning: filename BUFFER.OVERFLOW.PREVENTION.CHECK.RESERVED.AREA.PLUS.FILENAME.IS.192.BYTES.ux2pahohvae7Veocoo9OceeRi9AeThooxeesh0ueb1thu8dohthoom7GieCh3iethienemooyudev7jahr1woot5Jaegeeth1toONE.HUNDRED.NINETY2 spilled into reserved part of hard disk image header
0000100 B U F F E R . O V E R F L O W .
0000120 P R E V E N T I O N . C H E C K
0000140 . R E S E R V E D . A R E A . P
0000160 L U S . F I L E N A M E . I S .
0000200 1 9 2 . B Y T E S . u x 2 p a h
0000220 o h v a e 7 V e o c o o 9 O c e
0000240 e R i 9 A e T h o o x e e s h 0
0000260 u e b 1 t h u 8 d o h t h o o m
0000300 7 G i e C h 3 i e t h i e n e m
0000320 o o y u d e v 7 j a h r 1 w o o
0000340 t 5 J a e g e e t h 1 t o O N E
0000360 . H U N D R E D . N I N E T Y 2
0000400
BUFFER.OVERFLOW.PREVENTION.CHECK.RESERVED.AREA.PLUS.FILENAME.IS.192.BYTES.guHoh6Quao8Iogh9oonee7yahreyahgh7ileedo1Poo3cahHeith3xaiphiequeiNgug0yoh0Xaepo9ahMohthie6ioPoh6ge9EONE.HUNDRED.NINETY23
$MKDISK error: spilled filename must be at most 192 bytes"
ACTUAL=$(for F in \
BUFFER.OVERFLOW.PREVENTION.CHECK.RESERVED.AREA.PLUS.FILENAME.IS.192.BYTES.ux2pahohvae7Veocoo9OceeRi9AeThooxeesh0ueb1thu8dohthoom7GieCh3iethienemooyudev7jahr1woot5Jaegeeth1toONE.HUNDRED.NINETY2 \
BUFFER.OVERFLOW.PREVENTION.CHECK.RESERVED.AREA.PLUS.FILENAME.IS.192.BYTES.guHoh6Quao8Iogh9oonee7yahreyahgh7ileedo1Poo3cahHeith3xaiphiequeiNgug0yoh0Xaepo9ahMohthie6ioPoh6ge9EONE.HUNDRED.NINETY23; \
do echo $F; "$MKDISK" -S -h $F 2>&1 && od -c $F +100; rm -f $F; done)
check "filename overflow of reserved structure field"
exit 0
# vim:set ai et sw=4 ts=4 tw=80:
|