#!/bin/sh
#
# Test suite for the CDB handling in the krb5-strength-wordlist utility.
#
# Written by Russ Allbery <eagle@eyrie.org>
# Copyright 2013-2014
#     The Board of Trustees of the Leland Stanford Junior University
#
# SPDX-License-Identifier: MIT

. "$SOURCE/tap/libtap.sh"
cd "$BUILD"

# We can't run this test without the cdb utility.
if ! command -v cdb >/dev/null 2>&1 ; then
    skip_all 'cdb utility required for test'
fi

# Output the test plan.
plan 18

# Create a temporary directory and wordlist and ensure it's writable.
tmpdir=`test_tmpdir`
wordlist=`test_file_path data/wordlist`
if [ -z "$wordlist" ] ; then
    bail 'cannot find data/wordlist in test suite'
fi
cp "$wordlist" "$tmpdir/wordlist"
chmod 644 "$tmpdir/wordlist"

# Add a non-ASCII word to the wordlist.
echo 'عربى' >> "$tmpdir/wordlist"

# Test generation of the basic cdb file.
makelist="$SOURCE/../tools/krb5-strength-wordlist"
ok_program 'Database generation' 0 '' \
    "$makelist" -c "$tmpdir/wordlist.cdb" "$tmpdir/wordlist"

# Check the contents.
ok_program 'Database contains password' 0 '1' \
    cdb -q "$tmpdir/wordlist.cdb" password
ok_program 'Database contains one' 0 '1' \
    cdb -q "$tmpdir/wordlist.cdb" one
ok_program 'Database does not contain three' 100 '' \
    cdb -q "$tmpdir/wordlist.cdb" three
ok_program 'Database contains non-ASCII password' 0 '1' \
    cdb -q "$tmpdir/wordlist.cdb" 'عربى'

# Regenerate the database, filtering out short passwords.
rm "$tmpdir/wordlist.cdb"
ok_program 'Database generation with no short passwords' 0 '' \
    "$makelist" -c "$tmpdir/wordlist.cdb" -l 8 "$tmpdir/wordlist"
ok_program 'Database still contains password' 0 '1' \
    cdb -q "$tmpdir/wordlist.cdb" password
ok_program 'Database does not contain one' 100 '' \
    cdb -q "$tmpdir/wordlist.cdb" one

# Regenerate the database, filtering out non-ASCII words.
rm "$tmpdir/wordlist.cdb"
ok_program 'Database generation with no non-ASCII' 0 '' \
    "$makelist" -c "$tmpdir/wordlist.cdb" -a "$tmpdir/wordlist"
ok_program 'Database still contains password' 0 '1' \
    cdb -q "$tmpdir/wordlist.cdb" password
ok_program 'Database does not contain non-ASCII password' 100 '' \
    cdb -q "$tmpdir/wordlist.cdb" 'عربى'

# Regenerate the database, filtering out long passwords.
rm "$tmpdir/wordlist.cdb"
ok_program 'Database generation with no long passwords' 0 '' \
    "$makelist" -c "$tmpdir/wordlist.cdb" -L 10 "$tmpdir/wordlist"
ok_program 'Database still contains bitterbane' 0 '1' \
    cdb -q "$tmpdir/wordlist.cdb" bitterbane
ok_program 'Database does not contain happenstance' 100 '' \
    cdb -q "$tmpdir/wordlist.cdb" happenstance

# Regenerate the database, filtering out words starting with b or ending in d.
rm "$tmpdir/wordlist.cdb"
ok_program 'Database generation with no b passwords' 0 '' \
    "$makelist" -c "$tmpdir/wordlist.cdb" -x '\Ab' -x '.*d' "$tmpdir/wordlist"
ok_program 'Database does not contain bitterbane' 100 '' \
    cdb -q "$tmpdir/wordlist.cdb" bitterbane
ok_program 'Database still contains happenstance' 0 '1' \
    cdb -q "$tmpdir/wordlist.cdb" happenstance
ok_program 'Database does not contain password' 100 '' \
    cdb -q "$tmpdir/wordlist.cdb" password

# Clean up.
rm -f "$tmpdir/wordlist.cdb"
rm -f "$tmpdir/wordlist"
rmdir "$tmpdir" 2>/dev/null || true
