#!/bin/sh
#
# Test suite for the cdbmake-wordlist utility.
#
# Written by Russ Allbery <eagle@eyrie.org>
# Copyright 2013
#     The Board of Trustees of the Leland Stanford Junior University
#
# See LICENSE for licensing terms.

. "$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 20

# 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.
cdbmake="$SOURCE/../tools/cdbmake-wordlist"
ok_program 'Database generation' 0 '' "$cdbmake" "$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.
ok_program 'Database generation with no short passwords' 0 '' \
    "$cdbmake" -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.
ok_program 'Database generation with no non-ASCII' 0 '' \
    "$cdbmake" -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.
ok_program 'Database generation with no long passwords' 0 '' \
    "$cdbmake" -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.
ok_program 'Database generation with no b passwords' 0 '' \
    "$cdbmake" -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

# Try filtering the wordlist into a new wordlist.
ok_program 'Wordlist filtering' 0 '' \
    "$cdbmake" -a -x '.*d' -l 8 -o "$tmpdir/wordlist.new" "$tmpdir/wordlist"
( echo 'bitterbane'; echo 'happenstance' ) > "$tmpdir/wordlist.expected"
ok_program 'Filtered wordlist is correct' 0 '' \
    cmp "$tmpdir/wordlist.expected" "$tmpdir/wordlist.new"
rm -f "$tmpdir/wordlist.expected" "$tmpdir/wordlist.new"

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