]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/tools/wordlist-sqlite-t
Declare fast forward from 3.1-2
[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 2020 Russ Allbery <eagle@eyrie.org>
7 # Copyright 2014
8 #     The Board of Trustees of the Leland Stanford Junior University
9 #
10 # SPDX-License-Identifier: MIT
11
12 use 5.006;
13 use strict;
14 use warnings;
15
16 use lib "$ENV{SOURCE}/tap/perl";
17
18 use Test::RRA qw(use_prereq);
19 use Test::RRA::Automake qw(automake_setup test_file_path test_tmpdir);
20
21 use Test::More;
22
23 # Load prerequisite modules.
24 use_prereq('DBI');
25 use_prereq('DBD::SQLite');
26 use_prereq('IPC::Run',     'run');
27 use_prereq('Perl6::Slurp', 'slurp');
28
29 # Set up for testing of an Automake project.
30 automake_setup();
31
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.
37 #
38 # $input - Input wordlist file, used to form the output file name
39 #
40 # Returns: Path to new temporary SQLite dictionary
41 sub run_wordlist {
42     my ($input) = @_;
43     my $output = test_tmpdir() . '/wordlist.sqlite';
44
45     # Find the krb5-strength-wordlist program in the distribution.
46     my $wordlist = test_file_path('../tools/krb5-strength-wordlist');
47
48     # Ensure the output file does not exist.
49     if (-f $output) {
50         unlink($output) or BAIL_OUT("cannot delete $output: $!");
51     }
52
53     # Run the program, capturing its output and status.
54     my ($out, $err);
55     run([$wordlist, '-s', $output, $input], \undef, \$out, \$err);
56     my $status = ($? >> 8);
57
58     # Check the results.
59     is($status, 0,   'krb5-strength-wordlist -s');
60     is($out,    q{}, '...with no output');
61     is($err,    q{}, '...and no errors');
62
63     # Return the newly-created database.
64     return $output;
65 }
66
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);
71 chomp(@words);
72
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);
77
78 # Build the SQLite database.
79 my $dictionary = run_wordlist($wordlist);
80
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');
85
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);
91 my @got;
92 for my $row (@{$data_ref}) {
93     my ($password, $drowssap) = @{$row};
94     push(@got, $password);
95     is($drowssap, scalar(reverse($password)), "Reversal for $password");
96 }
97 $dbh->disconnect;
98
99 # Ensure that the list of passwords in the database are what we expected.
100 is_deeply(\@got, \@words, 'Passwords in dictionary');
101
102 # Remove the files created by the test.
103 unlink($dictionary);