3 # Test suite for basic Heimdal external strength checking functionality.
5 # Written by Russ Allbery <eagle@eyrie.org>
6 # Copyright 2009, 2012, 2013
7 # The Board of Trustees of the Leland Stanford Junior University
9 # See LICENSE for licensing terms.
15 use lib "$ENV{SOURCE}/tap/perl";
17 use File::Copy qw(copy);
18 use Test::RRA qw(use_prereq);
19 use Test::RRA::Automake qw(test_file_path);
21 use_prereq('File::Slurp');
22 use_prereq('IPC::Run', 'run');
24 use_prereq('Test::More', '0.87_01');
26 # Run the newly-built heimdal-strength command and return the status, output,
27 # and error output as a list.
29 # $principal - Principal to pass to the command
30 # $password - Password to pass to the command
32 # Returns: The exit status, standard output, and standard error as a list
33 # Throws: Text exception on failure to run the test program
34 sub run_heimdal_strength {
35 my ($principal, $password) = @_;
37 # Build the input to the strength checking program.
38 my $in = "principal: $principal\n";
39 $in .= "new-password: $password\n";
42 # Find the newly-built password checking program.
43 my $program = test_file_path('../tools/heimdal-strength');
45 # Run the password strength checker.
48 run([$program, $principal], \$in, \$out, \$err);
49 my $status = ($? >> 8);
52 return ($status, $out, $err);
55 # Run the newly-built heimdal-strength command to check a password and reports
56 # the results using Test::More. This uses the standard protocol for Heimdal
57 # external password strength checking programs.
59 # $test_ref - Reference to hash of test parameters
60 # name - The name of the test case
61 # principal - The principal changing its password
62 # password - The new password
63 # status - If present, the exit status (otherwise, it should be 0)
64 # error - If present, the expected rejection error
67 # Throws: Text exception on failure to run the test program
70 my $principal = $test_ref->{principal};
71 my $password = $test_ref->{password};
73 # Run the heimdal-strength command.
74 my ($status, $out, $err) = run_heimdal_strength($principal, $password);
77 # Check the results. If there is an error in the password, it should come
78 # on standard error; otherwise, standard output should be APPROVED. If
79 # there is a non-zero exit status, we expect the error on standard error
80 # and use that field to check for system errors.
81 is($status, $test_ref->{status} || 0, "$test_ref->{name} (status)");
82 if (defined($test_ref->{error})) {
83 is($err, $test_ref->{error}, '...error message');
84 is($out, q{}, '...no output');
86 is($err, q{}, '...no errors');
87 is($out, 'APPROVED', '...approved');
92 # Create a new krb5.conf file that includes arbitrary settings passed in via
95 # $settings_ref - Hash of keys and values to put into [appdefaults]
97 # Returns: Path to the new krb5.conf file
98 # Throws: Text exception if the new krb5.conf file cannot be created
99 sub create_krb5_conf {
100 my ($settings_ref) = @_;
102 # Paths for krb5.conf creation.
103 my $old = test_file_path('data/krb5.conf');
104 my $tmpdir = $ENV{BUILD} ? "$ENV{BUILD}/tmp" : 'tests/tmp';
105 my $new = "$tmpdir/krb5.conf";
107 # Create a temporary directory for the new file.
109 mkdir($tmpdir, 0777) or die "Cannot create $tmpdir: $!\n";
112 # Start with the testing krb5.conf file shipped in the package.
113 copy($old, $new) or die "Cannot copy $old to $new: $!\n";
115 # Append the local configuration.
116 open(my $config, '>>', $new) or die "Cannot append to $new: $!\n";
117 print {$config} "\n[appdefaults]\n krb5-strength = {\n"
118 or die "Cannot append to $new: $!\n";
119 for my $key (keys %{$settings_ref}) {
120 print {$config} q{ } x 8, $key, ' = ', $settings_ref->{$key}, "\n"
121 or die "Cannot append to $new: $!\n";
123 print {$config} " }\n"
124 or die "Cannot append to $new: $!\n";
125 close($config) or die "Cannot append to $new: $!\n";
127 # Return the path to the new file.
131 # Load a set of password test cases and return them as a list. The given file
132 # name is relative to data/passwords in the test suite.
134 # $file - The file name containing the test data in JSON
136 # Returns: List of anonymous hashes representing password test cases
137 # Throws: Text exception on failure to load the test data
138 sub load_password_tests {
140 my $path = test_file_path("data/passwords/$file");
142 # Load the test file data into memory.
143 my $testdata = read_file($path);
145 # Decode the JSON into Perl objects and return them.
146 my $json = JSON->new->utf8;
147 return $json->decode($testdata);
150 # Load the password tests from JSON. Accumulate a total count of tests for
153 for my $type (qw(cdb class cracklib length principal)) {
154 my $tests = load_password_tests("$type.json");
155 $tests{$type} = $tests;
156 $count += scalar(@{$tests});
159 # We run the principal tests twice, once for CrackLib and once for CDB.
160 $count += scalar(@{ $tests{principal} });
162 # We can now calculate our plan based on three tests for each password test.
163 plan(tests => $count * 3);
165 # Install the krb5.conf file with a configuration pointing to the test
166 # CrackLib dictionary.
167 my $datadir = $ENV{BUILD} ? "$ENV{BUILD}/data" : 'tests/data';
169 = create_krb5_conf({ password_dictionary => "$datadir/dictionary" });
170 local $ENV{KRB5_CONFIG} = $krb5_conf;
172 # Run the CrackLib password tests and based-on-principal tests from JSON.
173 note('CrackLib tests');
174 for my $test (@{ $tests{cracklib} }) {
175 check_password($test);
177 note('Generic tests with CrackLib');
178 for my $test (@{ $tests{principal} }) {
179 check_password($test);
182 # Install the krb5.conf file with a length restriction.
183 $krb5_conf = create_krb5_conf({ minimum_length => 12 });
184 local $ENV{KRB5_CONFIG} = $krb5_conf;
186 # Run the password length checks.
187 note('Password length checks');
188 for my $test (@{ $tests{length} }) {
189 check_password($test);
192 # Install the krb5.conf file for character class restrictions.
193 $krb5_conf = create_krb5_conf(
195 require_ascii_printable => 'true',
196 require_non_letter => 'true',
199 local $ENV{KRB5_CONFIG} = $krb5_conf;
201 # Run the character class tests.
202 note('Password character class checks');
203 for my $test (@{ $tests{class} }) {
204 check_password($test);
207 # Install the krb5.conf file with configuration pointing to the CDB
209 my $cdb_database = test_file_path('data/wordlist.cdb');
210 $krb5_conf = create_krb5_conf({ password_dictionary_cdb => $cdb_database });
211 local $ENV{KRB5_CONFIG} = $krb5_conf;
213 # Check whether we were built with CDB support. If so, run those tests.
214 my ($status, $output, $err) = run_heimdal_strength('test', 'password');
216 if ($status == 1 && $err =~ m{ not [ ] built [ ] with [ ] CDB }xms) {
217 my $total = scalar(@{ $tests{cdb} }) + scalar(@{ $tests{principal} });
218 skip('not built with CDB support', $total * 3);
221 # Run the CDB and principal password tests from JSON.
223 for my $test (@{ $tests{cdb} }) {
224 check_password($test);
226 note('Generic tests with CDB');
227 for my $test (@{ $tests{principal} }) {
228 check_password($test);
232 # Clean up our temporary krb5.conf file on any exit.
234 my $tmpdir = $ENV{BUILD} ? "$ENV{BUILD}/tmp" : 'tests/tmp';
235 my $config = "$tmpdir/krb5.conf";
237 unlink($config) or warn "Cannot remove $config\n";