From 2aad520ca4084514fc6659819e4cdab7ae1dd297 Mon Sep 17 00:00:00 2001 From: Russ Allbery Date: Tue, 25 Mar 2014 11:21:34 -0700 Subject: [PATCH] Separate krb5-strength-wordlist filtering to another test Rather than merging the wordlist filtering test with the CDB test, move it to a different unit test program. This is probably overkill for the tiny test that we do, but oh well. It will make adding more tests later somewhat easier if we ever do. --- Makefile.am | 3 +- tests/TESTS | 1 + tests/tools/wordlist-cdb-t | 10 +---- tests/tools/wordlist-t | 88 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 10 deletions(-) create mode 100755 tests/tools/wordlist-t diff --git a/Makefile.am b/Makefile.am index 3debeb8..1aaaed1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -19,7 +19,8 @@ EXTRA_DIST = .gitignore LICENSE autogen cracklib/HISTORY cracklib/LICENCE \ tests/tap/perl/Test/RRA/Config.pm \ tests/tap/perl/Test/RRA/Automake.pm tests/tools/heimdal-history-t \ tests/tools/heimdal-strength-t tests/tools/wordlist-cdb-t \ - tests/util/xmalloc-t tools/heimdal-strength.pod + tests/tools/wordlist-t tests/util/xmalloc-t \ + tools/heimdal-strength.pod # Do this globally. Everything needs to find the Kerberos headers and # libraries, and if we're using the system CrackLib, TinyCDB, or SQLite, add diff --git a/tests/TESTS b/tests/TESTS index 72157f3..fd37488 100644 --- a/tests/TESTS +++ b/tests/TESTS @@ -11,6 +11,7 @@ portable/snprintf portable/strndup tools/heimdal-history tools/heimdal-strength +tools/wordlist tools/wordlist-cdb util/messages util/messages-krb5 diff --git a/tests/tools/wordlist-cdb-t b/tests/tools/wordlist-cdb-t index d6fde6a..32a6999 100755 --- a/tests/tools/wordlist-cdb-t +++ b/tests/tools/wordlist-cdb-t @@ -17,7 +17,7 @@ if ! command -v cdb >/dev/null 2>&1 ; then fi # Output the test plan. -plan 20 +plan 18 # Create a temporary directory and wordlist and ensure it's writable. tmpdir=`test_tmpdir` @@ -84,14 +84,6 @@ ok_program 'Database still contains happenstance' 0 '1' \ 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 '' \ - "$makelist" -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" diff --git a/tests/tools/wordlist-t b/tests/tools/wordlist-t new file mode 100755 index 0000000..030a960 --- /dev/null +++ b/tests/tools/wordlist-t @@ -0,0 +1,88 @@ +#!/usr/bin/perl +# +# Test suite for krb5-strength-wordlist filtering functions. +# +# Written by Russ Allbery +# Copyright 2014 +# The Board of Trustees of the Leland Stanford Junior University +# +# See LICENSE for licensing terms. + +use 5.006; +use strict; +use warnings; + +use lib "$ENV{SOURCE}/tap/perl"; + +use Encode qw(encode); +use Test::More; +use Test::RRA qw(use_prereq); +use Test::RRA::Automake qw(automake_setup test_file_path test_tmpdir); + +# Load prerequisite modules. +use_prereq('IPC::Run', 'run'); +use_prereq('Perl6::Slurp', 'slurp'); + +# Set up for testing of an Automake project. +automake_setup(); + +# Declare the plan. +plan tests => 5; + +# Run krb5-strength-wordlist with the given arguments and verify that it exits +# successfully with no output. For planning purposes, this function will +# report three tests. +# +# @args - Arguments to krb5-strength-wordlist +# +# Returns: undef +sub run_wordlist { + my (@args) = @_; + + # Find the krb5-strength-wordlist program in the distribution. + my $wordlist = test_file_path('../tools/krb5-strength-wordlist'); + + # Run the program, capturing its output and status. + my ($out, $err); + run([$wordlist, @args], \undef, \$out, \$err); + my $status = ($? >> 8); + + # Check the results. + is($status, 0, "krb5-strength-wordlist @args"); + is($out, q{}, '...with no output'); + is($err, q{}, '...and no errors'); + return; +} + +# Read the word list that we'll use for testing. +my @wordlist = slurp(test_file_path('data/wordlist')); + +# Generate a filtered version that should match the eventual output of +# krb5-strength-wordlist, removing words containing the letter d and any +# shorter than 8 characters. +my @filtered = grep { !m{d}xms && length($_) >= 8 } @wordlist; + +# Add a non-ASCII word to test non-ASCII filtering. +push(@wordlist, encode('UTF-8', "\N{U+0639}\N{U+0631}\N{U+0628}\N{U+649}")); + +# Write the new wordlist, including the non-ASCII word, to a new file. +my $tmpdir = test_tmpdir(); +open(my $wordlist_fh, q{>}, "$tmpdir/wordlist") + or BAIL_OUT("cannot create to $tmpdir/wordlist: $!"); +print {$wordlist_fh} join("\n", @wordlist), "\n" + or BAIL_OUT("cannot write to $tmpdir/wordlist: $!"); +close($wordlist_fh) + or BAIL_OUT("cannot flush $tmpdir/wordlist: $!"); + +# Generate a new, filtered word list. Remove non-ASCII, words containing the +# letter d, and words shorter than eight characters. +my @options = qw(-a -x .*d -l 8); +run_wordlist(@options, '-o', "$tmpdir/wordlist.new", "$tmpdir/wordlist"); + +# Verify that the new filtered list exists and has the correct content. +my @got = eval { slurp("$tmpdir/wordlist.new") }; +is($@, q{}, 'New word list exists'); +is_deeply(\@got, \@filtered, '...with correct contents'); + +# Remove the files created by the test. +unlink("$tmpdir/wordlist", "$tmpdir/wordlist.new"); -- 2.39.2