]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/tools/wordlist-sqlite-t
Fix perltidy error in wordlist-sqlite test
[kerberos/krb5-strength.git] / tests / tools / wordlist-sqlite-t
1 #!/usr/bin/perl
2 #
3 # Test suite for krb5-strength-wordlist SQLite database generation
4 #
5 # Written by Russ Allbery <eagle@eyrie.org>
6 # Copyright 2014
7 #     The Board of Trustees of the Leland Stanford Junior University
8 #
9 # See LICENSE for licensing terms.
10
11 use 5.006;
12 use strict;
13 use warnings;
14
15 use lib "$ENV{SOURCE}/tap/perl";
16
17 use Test::More;
18 use Test::RRA qw(use_prereq);
19 use Test::RRA::Automake qw(automake_setup test_file_path test_tmpdir);
20
21 # Load prerequisite modules.
22 use_prereq('DBI');
23 use_prereq('DBD::SQLite');
24 use_prereq('IPC::Run',     'run');
25 use_prereq('Perl6::Slurp', 'slurp');
26
27 # Set up for testing of an Automake project.
28 automake_setup();
29
30 # Run krb5-strength-wordlist on the given word list, generating a SQLite
31 # dictionary in a temporary directory and returning its path.  Ensure that
32 # krb5-strength exits successfully with no output.  For planning purposes,
33 # this function will report three tests.  Calls BAIL_OUT if the output file
34 # already exists and can't be deleted.
35 #
36 # $input - Input wordlist file, used to form the output file name
37 #
38 # Returns: Path to new temporary SQLite dictionary
39 sub run_wordlist {
40     my ($input) = @_;
41     my $output = test_tmpdir() . '/wordlist.sqlite';
42
43     # Find the krb5-strength-wordlist program in the distribution.
44     my $wordlist = test_file_path('../tools/krb5-strength-wordlist');
45
46     # Ensure the output file does not exist.
47     if (-f $output) {
48         unlnk($output) or BAIL_OUT("cannot delete $output: $!");
49     }
50
51     # Run the program, capturing its output and status.
52     my ($out, $err);
53     run([$wordlist, '-s', $output, $input], \undef, \$out, \$err);
54     my $status = ($? >> 8);
55
56     # Check the results.
57     is($status, 0,   'krb5-strength-wordlist -s');
58     is($out,    q{}, '...with no output');
59     is($err,    q{}, '...and no errors');
60
61     # Return the newly-created database.
62     return $output;
63 }
64
65 # Read the word list that we'll use for testing so that we can validate the
66 # contents of the generated SQLite database.
67 my $wordlist = test_file_path('data/wordlist');
68 my @words    = slurp($wordlist);
69 chomp(@words);
70
71 # Declare the plan now that we know how many tests there will be.  There is
72 # one test for each word, plus four for creating the database and another for
73 # checking that it contains the right passwords.
74 plan tests => 5 + scalar(@words);
75
76 # Build the SQLite database.
77 my $dictionary = run_wordlist($wordlist);
78
79 # Ensure that we can open the result as a SQLite database.
80 my $options = { PrintError => 1, RaiseError => 1, AutoCommit => 1 };
81 my $dbh = DBI->connect("dbi:SQLite:dbname=$dictionary", q{}, q{}, $options);
82 ok(defined($dbh), 'Opening SQLite database succeeded');
83
84 # Walk through every row in the passwords table and ensure that the drowssap
85 # column is the reverse of the password column.  Accumulate the passwords so
86 # that we can check against the contents of the word list.
87 my $sql      = 'SELECT PASSWORD, DROWSSAP FROM PASSWORDS';
88 my $data_ref = $dbh->selectall_arrayref($sql);
89 my @got;
90 for my $row (@{$data_ref}) {
91     my ($password, $drowssap) = @{$row};
92     push(@got, $password);
93     is($drowssap, scalar(reverse($password)), "Reversal for $password");
94 }
95 $dbh->disconnect;
96
97 # Ensure that the list of passwords in the database are what we expected.
98 is_deeply(\@got, \@words, 'Passwords in dictionary');
99
100 # Remove the files created by the test.
101 unlink($dictionary);