3 # Test suite for krb5-strength-wordlist SQLite database generation
5 # Written by Russ Allbery <eagle@eyrie.org>
6 # Copyright 2020 Russ Allbery <eagle@eyrie.org>
8 # The Board of Trustees of the Leland Stanford Junior University
10 # SPDX-License-Identifier: MIT
16 use lib "$ENV{SOURCE}/tap/perl";
18 use Test::RRA qw(use_prereq);
19 use Test::RRA::Automake qw(automake_setup test_file_path test_tmpdir);
23 # Load prerequisite modules.
25 use_prereq('DBD::SQLite');
26 use_prereq('IPC::Run', 'run');
27 use_prereq('Perl6::Slurp', 'slurp');
29 # Set up for testing of an Automake project.
32 # Run krb5-strength-wordlist on the given word list, generating a SQLite
33 # dictionary in a temporary directory and returning its path. Ensure that
34 # krb5-strength exits successfully with no output. For planning purposes,
35 # this function will report three tests. Calls BAIL_OUT if the output file
36 # already exists and can't be deleted.
38 # $input - Input wordlist file, used to form the output file name
40 # Returns: Path to new temporary SQLite dictionary
43 my $output = test_tmpdir() . '/wordlist.sqlite';
45 # Find the krb5-strength-wordlist program in the distribution.
46 my $wordlist = test_file_path('../tools/krb5-strength-wordlist');
48 # Ensure the output file does not exist.
50 unlink($output) or BAIL_OUT("cannot delete $output: $!");
53 # Run the program, capturing its output and status.
55 run([$wordlist, '-s', $output, $input], \undef, \$out, \$err);
56 my $status = ($? >> 8);
59 is($status, 0, 'krb5-strength-wordlist -s');
60 is($out, q{}, '...with no output');
61 is($err, q{}, '...and no errors');
63 # Return the newly-created database.
67 # Read the word list that we'll use for testing so that we can validate the
68 # contents of the generated SQLite database.
69 my $wordlist = test_file_path('data/wordlist');
70 my @words = slurp($wordlist);
73 # Declare the plan now that we know how many tests there will be. There is
74 # one test for each word, plus four for creating the database and another for
75 # checking that it contains the right passwords.
76 plan tests => 5 + scalar(@words);
78 # Build the SQLite database.
79 my $dictionary = run_wordlist($wordlist);
81 # Ensure that we can open the result as a SQLite database.
82 my $options = { PrintError => 1, RaiseError => 1, AutoCommit => 1 };
83 my $dbh = DBI->connect("dbi:SQLite:dbname=$dictionary", q{}, q{}, $options);
84 ok(defined($dbh), 'Opening SQLite database succeeded');
86 # Walk through every row in the passwords table and ensure that the drowssap
87 # column is the reverse of the password column. Accumulate the passwords so
88 # that we can check against the contents of the word list.
89 my $sql = 'SELECT PASSWORD, DROWSSAP FROM PASSWORDS';
90 my $data_ref = $dbh->selectall_arrayref($sql);
92 for my $row (@{$data_ref}) {
93 my ($password, $drowssap) = @{$row};
94 push(@got, $password);
95 is($drowssap, scalar(reverse($password)), "Reversal for $password");
99 # Ensure that the list of passwords in the database are what we expected.
100 is_deeply(\@got, \@words, 'Passwords in dictionary');
102 # Remove the files created by the test.