From: Russ Allbery Date: Tue, 25 Mar 2014 18:40:17 +0000 (-0700) Subject: Add test for krb5-strength-wordlist SQLite generation X-Git-Tag: release/3.0~16 X-Git-Url: https://git.eyrie.org/?a=commitdiff_plain;h=73428d4724a14bcba1dfda2fff769618380e8a7b;p=kerberos%2Fkrb5-strength.git Add test for krb5-strength-wordlist SQLite generation --- diff --git a/Makefile.am b/Makefile.am index 1aaaed1..edfe82e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -19,8 +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/tools/wordlist-t tests/util/xmalloc-t \ - tools/heimdal-strength.pod + tests/tools/wordlist-sqlite-t 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 fd37488..58a834c 100644 --- a/tests/TESTS +++ b/tests/TESTS @@ -13,6 +13,7 @@ tools/heimdal-history tools/heimdal-strength tools/wordlist tools/wordlist-cdb +tools/wordlist-sqlite util/messages util/messages-krb5 util/xmalloc diff --git a/tests/tools/wordlist-sqlite-t b/tests/tools/wordlist-sqlite-t new file mode 100755 index 0000000..c3b5cf5 --- /dev/null +++ b/tests/tools/wordlist-sqlite-t @@ -0,0 +1,101 @@ +#!/usr/bin/perl +# +# Test suite for krb5-strength-wordlist SQLite database generation +# +# 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 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('DBI'); +use_prereq('DBD::SQLite'); +use_prereq('IPC::Run', 'run'); +use_prereq('Perl6::Slurp', 'slurp'); + +# Set up for testing of an Automake project. +automake_setup(); + +# Run krb5-strength-wordlist on the given word list, generating a SQLite +# dictionary in a temporary directory and returning its path. Ensure that +# krb5-strength exits successfully with no output. For planning purposes, +# this function will report three tests. Calls BAIL_OUT if the output file +# already exists and can't be deleted. +# +# $input - Input wordlist file, used to form the output file name +# +# Returns: Path to new temporary SQLite dictionary +sub run_wordlist { + my ($input) = @_; + my $output = test_tmpdir() . '/wordlist.sqlite'; + + # Find the krb5-strength-wordlist program in the distribution. + my $wordlist = test_file_path('../tools/krb5-strength-wordlist'); + + # Ensure the output file does not exist. + if (-f $output) { + unlnk($output) or BAIL_OUT("cannot delete $output: $!"); + } + + # Run the program, capturing its output and status. + my ($out, $err); + run([$wordlist, '-s', $output, $input], \undef, \$out, \$err); + my $status = ($? >> 8); + + # Check the results. + is($status, 0, 'krb5-strength-wordlist -s'); + is($out, q{}, '...with no output'); + is($err, q{}, '...and no errors'); + + # Return the newly-created database. + return $output; +} + +# Read the word list that we'll use for testing so that we can validate the +# contents of the generated SQLite database. +my $wordlist = test_file_path('data/wordlist'); +my @words = slurp($wordlist); +chomp(@words); + +# Declare the plan now that we know how many tests there will be. There is +# one test for each word, plus four for creating the database and another for +# checking that it contains the right passwords. +plan tests => 5 + scalar(@words); + +# Build the SQLite database. +my $dictionary = run_wordlist($wordlist); + +# Ensure that we can open the result as a SQLite database. +my $options = { PrintError => 1, RaiseError => 1, AutoCommit => 1 }; +my $dbh = DBI->connect("dbi:SQLite:dbname=$dictionary", q{}, q{}, $options); +ok(defined($dbh), 'Opening SQLite database succeeded'); + +# Walk through every row in the passwords table and ensure that the drowssap +# column is the reverse of the password column. Accumulate the passwords so +# that we can check against the contents of the word list. +my $sql = 'SELECT PASSWORD, DROWSSAP FROM PASSWORDS'; +my $data_ref = $dbh->selectall_arrayref($sql); +my @got; +for my $row (@{$data_ref}) { + my ($password, $drowssap) = @{$row}; + push(@got, $password); + is($drowssap, scalar(reverse($password)), "Reversal for $password"); +} +$dbh->disconnect; + +# Ensure that the list of passwords in the database are what we expected. +is_deeply(\@got, \@words, 'Passwords in dictionary'); + +# Remove the files created by the test. +unlink($dictionary);