| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
CRYPT=${CRYPT:-./crypt} |
| 4 |
ASKPASS=${ASKPASS:-./askpass} |
| 5 |
|
| 6 |
oneTimeSetUp() { |
| 7 |
export OUTDIR="$SHUNIT_TMPDIR/out" |
| 8 |
export BINDIR="$SHUNIT_TMPDIR/bin" |
| 9 |
export PATH="$BINDIR:$PATH" |
| 10 |
|
| 11 |
# Directory for output data (dropped after each test) |
| 12 |
mkdir -p $OUTDIR |
| 13 |
|
| 14 |
# Test helper scripts |
| 15 |
mkdir -p $BINDIR |
| 16 |
cat >$BINDIR/askpass <<END |
| 17 |
#!/bin/sh |
| 18 |
echo -n "\${FAKE_ASKPASS_ANSWER:-foobar}" |
| 19 |
END |
| 20 |
cat >$BINDIR/cryptsetup <<END |
| 21 |
#!/bin/sh |
| 22 |
echo "\$@" >>$OUTDIR/cryptsetup |
| 23 |
END |
| 24 |
chmod 755 $BINDIR/* |
| 25 |
} |
| 26 |
|
| 27 |
setUp() { |
| 28 |
# Overrides for askpass |
| 29 |
export DIVERTED_ASKPASS="$BINDIR/askpass" |
| 30 |
export CRYPT_HELPER="$CRYPT" |
| 31 |
export NUKE_PASSWORD_HASH_PATH="$SHUNIT_TMPDIR/password_hash" |
| 32 |
export CRYPTTAB_SOURCE="$SHUNIT_TMPDIR/device" |
| 33 |
touch $CRYPTTAB_SOURCE |
| 34 |
|
| 35 |
# Clean up some environment variables that might be set by tests |
| 36 |
unset FAKE_ASKPASS_ANSWER |
| 37 |
} |
| 38 |
|
| 39 |
tearDown() { |
| 40 |
if [ -d "$OUTDIR" ]; then |
| 41 |
rm -f $OUTDIR/* |
| 42 |
fi |
| 43 |
rm -f $NUKE_PASSWORD_HASH_PATH |
| 44 |
} |
| 45 |
|
| 46 |
testCryptNoArgsOutput() { |
| 47 |
output=$($CRYPT 2>&1 </dev/null) |
| 48 |
if ! echo "$output" | grep -q "Usage:"; then |
| 49 |
fail 'Does not print Usage output without argument' |
| 50 |
fi |
| 51 |
} |
| 52 |
testCryptNoArgsExitCode() { |
| 53 |
$CRYPT >/dev/null 2>&1 </dev/null |
| 54 |
assertEquals 'Bad exit code when ran without argument' 1 $? |
| 55 |
} |
| 56 |
|
| 57 |
testCryptBadArgOutput() { |
| 58 |
output=$($CRYPT --doesnotexist 2>&1 </dev/null) |
| 59 |
if ! echo "$output" | grep -q "Usage:"; then |
| 60 |
fail 'Does not print Usage output with invalid argument' |
| 61 |
fi |
| 62 |
} |
| 63 |
testCryptBadArgExitCode() { |
| 64 |
$CRYPT --doesnotexist >/dev/null 2>&1 </dev/null |
| 65 |
assertEquals 'Bad exit code when ran with invalid argument' 1 $? |
| 66 |
} |
| 67 |
|
| 68 |
testCryptHelpOutput() { |
| 69 |
output=$($CRYPT --help 2>&1 </dev/null) |
| 70 |
if ! echo "$output" | grep -q "Usage:"; then |
| 71 |
fail 'Does not print Usage output with --help' |
| 72 |
fi |
| 73 |
} |
| 74 |
testCryptHelpExitCode() { |
| 75 |
$CRYPT --help >/dev/null 2>&1 </dev/null |
| 76 |
assertEquals 'Bad exit code when ran with --help' 0 $? |
| 77 |
} |
| 78 |
|
| 79 |
testCryptGenerateEmptyPassword() { |
| 80 |
output_stdout=$($CRYPT --generate 'h/' 2>/dev/null </dev/null) |
| 81 |
output_stderr=$($CRYPT --generate 'h/' 2>&1 >/dev/null </dev/null) |
| 82 |
exit_code=$? |
| 83 |
assertNull "'crypt --generate </dev/null' unexpectedly generated something" "$output_stdout" |
| 84 |
if ! echo "$output_stderr" | grep -q "ERROR:"; then |
| 85 |
fail "'crypt --generate </dev/null' did not print any error message" |
| 86 |
fi |
| 87 |
assertEquals "'crypt --generate </dev/null' has a bad exit code" 1 $exit_code |
| 88 |
} |
| 89 |
|
| 90 |
do_testCryptGenerate() { |
| 91 |
output=$(echo "foobar" | $CRYPT --generate $1) |
| 92 |
exit_code=$? |
| 93 |
assertNotNull "'echo foobar | crypt --generate $1' ($2) provided no output" "$output" |
| 94 |
assertEquals "'echo foobar | crypt --generate $1' ($2) returned bad exit code" 0 $exit_code |
| 95 |
} |
| 96 |
|
| 97 |
testCryptGenerateBasic() { |
| 98 |
do_testCryptGenerate 'h/' 'DES' |
| 99 |
} |
| 100 |
testCryptGenerateMD5() { |
| 101 |
do_testCryptGenerate '$1$h/$' 'MD5' |
| 102 |
} |
| 103 |
testCryptGenerateSHA256() { |
| 104 |
do_testCryptGenerate '$5$h/$' 'SHA-256' |
| 105 |
} |
| 106 |
testCryptGenerateSHA512() { |
| 107 |
do_testCryptGenerate '$6$h/$' 'SHA-512' |
| 108 |
} |
| 109 |
|
| 110 |
testCryptGenerateBadSalt() { |
| 111 |
output_stdout=$(echo foobar | $CRYPT --generate '$999$foobar$' 2>/dev/null) |
| 112 |
output_stderr=$(echo foobar | $CRYPT --generate '$999$foobar$' 2>&1 >/dev/null) |
| 113 |
exit_code=$? |
| 114 |
assertNull "'echo foobar | crypt --generate <bad-salt>' unexpectedly generated something" "$output_stdout" |
| 115 |
if ! echo "$output_stderr" | grep -q "ERROR:"; then |
| 116 |
fail "'echo foobar | crypt --generate <bad-salt>' did not print any error message" |
| 117 |
fi |
| 118 |
assertEquals "'echo foobar | crypt --generate <bad-salt>' has a bad exit code" 1 $exit_code |
| 119 |
} |
| 120 |
|
| 121 |
testCryptGenerateNoSaltSupplied() { |
| 122 |
output_stdout=$(echo foobar | $CRYPT --generate 2>/dev/null) |
| 123 |
output_stderr=$(echo foobar | $CRYPT --generate 2>&1 >/dev/null) |
| 124 |
exit_code=$? |
| 125 |
assertNull "'echo foobar | crypt --generate' generated noise on stderr" "$output_stderr" |
| 126 |
if ! echo "$output_stdout" | grep -q '^\$6\$'; then |
| 127 |
fail "'echo foobar | crypt --generate' did not generate a SHA-512 based hash ($output_stdout)" |
| 128 |
fi |
| 129 |
assertEquals "'echo foobar | crypt --generate' has a bad exit code" 0 $exit_code |
| 130 |
} |
| 131 |
|
| 132 |
testCryptGenerateEmptySaltSupplied() { |
| 133 |
output_stdout=$(echo foobar | $CRYPT --generate '' 2>/dev/null) |
| 134 |
output_stderr=$(echo foobar | $CRYPT --generate '' 2>&1 >/dev/null) |
| 135 |
exit_code=$? |
| 136 |
assertNull "'echo foobar | crypt --generate ''' generated noise on stderr" "$output_stderr" |
| 137 |
if ! echo "$output_stdout" | grep -q '^\$6\$'; then |
| 138 |
fail "'echo foobar | crypt --generate ''' did not generate a SHA-512 based hash ($output_stdout)" |
| 139 |
fi |
| 140 |
assertEquals "'echo foobar | crypt --generate ''' has a bad exit code" 0 $exit_code |
| 141 |
} |
| 142 |
|
| 143 |
testCryptGenerateNoSaltRandomness() { |
| 144 |
output1=$(echo foobar | $CRYPT --generate 2>/dev/null) |
| 145 |
output2=$(echo foobar | $CRYPT --generate 2>/dev/null) |
| 146 |
salt1="$(echo $output1 | cut -d$ -f1-3)"'$' |
| 147 |
salt2="$(echo $output2 | cut -d$ -f1-3)"'$' |
| 148 |
assertNotEquals "Two consecutive runs of 'echo foobar | $CRYPT --generate' generated the same salt" "$output1" "$output2" |
| 149 |
output3=$(echo foobar | $CRYPT --generate "$salt1") |
| 150 |
assertEquals "'echo foobar | $CRYPT --generate <salt>' did not recreate the original hash" "$output1" "$output3" |
| 151 |
} |
| 152 |
|
| 153 |
testCryptCheckEmptyPassword() { |
| 154 |
output_stdout=$($CRYPT --check 'h/GdiFWQsXxA.' 2>/dev/null </dev/null) |
| 155 |
output_stderr=$($CRYPT --check 'h/GdiFWQsXxA.' 2>&1 >/dev/null </dev/null) |
| 156 |
exit_code=$? |
| 157 |
assertNull "'crypt --check <hash> </dev/null' unexpectedly generated something" "$output_stdout" |
| 158 |
if ! echo "$output_stderr" | grep -q "ERROR:"; then |
| 159 |
fail "'crypt --check <hash> </dev/null' did not print any error message" |
| 160 |
fi |
| 161 |
assertEquals "'crypt --check <hash> </dev/null' has a bad exit code" 1 $exit_code |
| 162 |
} |
| 163 |
|
| 164 |
testCryptCheckNoHashSupplied() { |
| 165 |
output_stdout=$(echo "foobar" | $CRYPT --check 2>/dev/null) |
| 166 |
output_stderr=$(echo "foobar" | $CRYPT --check 2>&1 >/dev/null) |
| 167 |
if ! echo "$output_stderr" | grep -q "ERROR:"; then |
| 168 |
fail "'echo foobar | crypt --check' did not print any error message" |
| 169 |
fi |
| 170 |
assertNull "'echo foobar | crypt --check' unexpectly returned something on stdout" "$output_stdout" |
| 171 |
} |
| 172 |
|
| 173 |
testCryptCheckGoodPassword() { |
| 174 |
output=$(echo "foobar" | $CRYPT --check '$6$dkcZzIkv$Ju7XCIc4igWvht3bOu266vvRam6IdnIFxoyonDt.6JZl8NfCaukACeIRYVW7WQtrUtqN2TrWSgEFnXumuTiN41' 2>&1) |
| 175 |
exit_code=$? |
| 176 |
assertNull "'echo foobar | $CRYPT --check <good-hash>' printed unexpected output" "$output" |
| 177 |
assertEquals "'echo foobar | $CRYPT --check <good-hash>' did not exit with" 0 $exit_code |
| 178 |
} |
| 179 |
|
| 180 |
testCryptCheckBadPassword() { |
| 181 |
output=$(echo "foobar-bad" | $CRYPT --check '$6$dkcZzIkv$Ju7XCIc4igWvht3bOu266vvRam6IdnIFxoyonDt.6JZl8NfCaukACeIRYVW7WQtrUtqN2TrWSgEFnXumuTiN41' 2>&1) |
| 182 |
exit_code=$? |
| 183 |
assertNull "'echo foobar | $CRYPT --check <bad-hash>' printed unexpected output" "$output" |
| 184 |
assertEquals "'echo foobar | $CRYPT --check <bad-hash>' did not exit with" 1 $exit_code |
| 185 |
} |
| 186 |
|
| 187 |
testCryptGenerateCheckRoundtrip() { |
| 188 |
for salt in '' 'h/' '$1$abcd$' '$5$12345678$' '$6$deadbeef$' |
| 189 |
do |
| 190 |
password="haX0rd3ad" |
| 191 |
|
| 192 |
password_hash=$(echo $password | $CRYPT --generate "$salt") |
| 193 |
exit_code=$? |
| 194 |
assertEquals "'echo $password | $CRYPT --generate $salt' did not exit with" 0 $exit_code |
| 195 |
|
| 196 |
echo $password | $CRYPT --check "$password_hash" |
| 197 |
exit_code=$? |
| 198 |
assertEquals "'echo $password | $CRYPT --check $password_hash' did not exit with" 0 $exit_code |
| 199 |
done |
| 200 |
} |
| 201 |
|
| 202 |
testAskPassWarnsAboutMissingCrypttabSource() { |
| 203 |
export CRYPTTAB_SOURCE=/does/not/exist |
| 204 |
|
| 205 |
$ASKPASS >$OUTDIR/log 2>&1 |
| 206 |
|
| 207 |
if ! grep -q 'WARNING: $CRYPTTAB_SOURCE' $OUTDIR/log; then |
| 208 |
fail "askpass should complain of missing CRYPTTAB_SOURCE" |
| 209 |
fi |
| 210 |
} |
| 211 |
|
| 212 |
testAskPassWarnsAboutMissingCryptHelper() { |
| 213 |
export CRYPT_HELPER=/does/not/exist |
| 214 |
|
| 215 |
$ASKPASS >$OUTDIR/log 2>&1 |
| 216 |
|
| 217 |
if ! grep -q "WARNING: $CRYPT_HELPER" $OUTDIR/log; then |
| 218 |
fail "askpass should complain of missing \$CRYPT_HELPER" |
| 219 |
fi |
| 220 |
} |
| 221 |
|
| 222 |
testAskPassCallsCryptsetupErase() { |
| 223 |
# Setup the password and its matching hash |
| 224 |
export FAKE_ASKPASS_ANSWER="foobar" |
| 225 |
echo '$6$dkcZzIkv$Ju7XCIc4igWvht3bOu266vvRam6IdnIFxoyonDt.6JZl8NfCaukACeIRYVW7WQtrUtqN2TrWSgEFnXumuTiN41' >$NUKE_PASSWORD_HASH_PATH |
| 226 |
|
| 227 |
$ASKPASS >$OUTDIR/log 2>&1 |
| 228 |
|
| 229 |
touch $OUTDIR/cryptsetup |
| 230 |
if ! grep -q "erase $CRYPTTAB_SOURCE" $OUTDIR/cryptsetup; then |
| 231 |
echo "Output of askpass:" |
| 232 |
cat $OUTDIR/log |
| 233 |
echo "" |
| 234 |
echo "Cryptsetup log:" |
| 235 |
cat $OUTDIR/cryptsetup |
| 236 |
fail "cryptsetup erase has not been called by askpass" |
| 237 |
fi |
| 238 |
} |
| 239 |
|
| 240 |
testAskPassWithoutPasswordHash() { |
| 241 |
# No password_hash is created |
| 242 |
|
| 243 |
$ASKPASS >$OUTDIR/log 2>&1 |
| 244 |
|
| 245 |
assertFalse 'cryptsetup was unexpectly run' "[ -e $OUTDIR/cryptsetup ]" |
| 246 |
} |
| 247 |
|
| 248 |
testAskPassWithNonMatchingPasswordHash() { |
| 249 |
# Setup the password and a non-matching hash |
| 250 |
export FAKE_ASKPASS_ANSWER="this-is-not-the-good-password" |
| 251 |
echo '$6$dkcZzIkv$Ju7XCIc4igWvht3bOu266vvRam6IdnIFxoyonDt.6JZl8NfCaukACeIRYVW7WQtrUtqN2TrWSgEFnXumuTiN41' >$NUKE_PASSWORD_HASH_PATH |
| 252 |
|
| 253 |
$ASKPASS >$OUTDIR/log 2>&1 |
| 254 |
|
| 255 |
assertFalse 'cryptsetup was unexpectly run' "[ -e $OUTDIR/cryptsetup ]" |
| 256 |
} |
| 257 |
|
| 258 |
testAskPassReturnsPassword() { |
| 259 |
export FAKE_ASKPASS_ANSWER="my-password" |
| 260 |
|
| 261 |
OUT=$($ASKPASS 2>/dev/null) |
| 262 |
|
| 263 |
assertEquals "askpass did not print the password" "$FAKE_ASKPASS_ANSWER" "$OUT" |
| 264 |
} |
| 265 |
|
| 266 |
. shunit2 |