Debian Codegraph @

tests/tools/backend-t

raw
1 #!/usr/bin/perl
2 #
3 # Test suite for krb5-strength-backend.
4 #
5 # Written by Russ Allbery <[email protected]>
6 # Copyright 2013
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 File::Path qw(remove_tree);
18 use POSIX qw(strftime);
19 use Test::More tests => 30;
20 use Test::RRA qw(use_prereq);
21 use Test::RRA::Automake qw(test_file_path test_tmpdir);
22
23 use_prereq('IPC::Run', 'run');
24 use_prereq('Perl6::Slurp', 'slurp');
25
26 # Not actually used by the test, but required by krb5-sync-backend.
27 use_prereq('Net::Remctl::Backend');
28
29 # Run krb5-sync-backend and return the status, output, and error output as a
30 # list. Always uses a directory named 'queue' under test_tmpdir() as the
31 # queue directory.
32 #
33 # @args - Command-line arguments to pass in
34 #
35 # Returns: Exit status, stdout, and stderr as a list
36 # Throws: Text exception on failure to create the queue or run the program
37 sub run_backend {
38 my ($action, @args) = @_;
39
40 # Ensure the queue directory exists.
41 my $queue = test_tmpdir() . '/queue';
42 if (!-d $queue) {
43 mkdir($queue, 0777) or BAIL_OUT("cannot create $queue: $!");
44 }
45
46 # Add the queue option to the start of the arguments.
47 unshift(@args, '-d', $queue);
48
49 # If the action is password, we have to pass the password on standard
50 # input.
51 my $in;
52 if ($action eq 'password') {
53 $in = pop(@args);
54 }
55
56 # Run the command and capture the output. We add the queue option after
57 # the initial argument.
58 my $backend = test_file_path('../tools/krb5-sync-backend');
59 my ($out, $err);
60 run([$backend, $action, @args], \$in, \$out, \$err);
61 my $status = ($? >> 8);
62
63 # Return the results.
64 return ($status, $out, $err);
65 }
66
67 # Run a krb5-sync-backend command that should exit with success and produce no
68 # output and check those results with Test::More functions. Always uses a
69 # directory named 'queue' under test_tmpdir() as the queue directory.
70 #
71 # @args - Command-line arguments to pass in
72 #
73 # Returns: undef
74 # Throws: Text exception on failure to create the queue or run the program
75 sub run_backend_checked {
76 my (@args) = @_;
77
78 # Run the program and capture status and output.
79 my ($status, $out, $err) = run_backend(@args);
80
81 # Check that everything is as expected.
82 is($status, 0, "krb5-sync-backend @args succeeded");
83 is($out, q{}, '...with no output');
84 is($err, q{}, '...and no errors');
85 return;
86 }
87
88 # Given a user, an action, and (for a password change) the new password,
89 # locate that queued action and check that the correct information was stored
90 # for it. Expects the action to have been queued in the five seconds.
91 # Reports results with Test::More functions. Always uses a directory named
92 # 'queue' under test_tmpdir() as the queue directory.
93 #
94 # $user - User for which the action should be queued
95 # $action - The queued action, chosen from enable, disable, or password
96 # $password - For password actions, the queued password
97 #
98 # Returns: undef
99 # Throws: Text exception on system failures such as reading files
100 sub check_queued_action {
101 my ($user, $action, $password) = @_;
102 my $queue = test_tmpdir() . '/queue';
103
104 # Build the base portion of the expected filename, without the timestamp.
105 my $type = ($action eq 'disable') ? 'enable' : $action;
106 my $base = $queue . "/$user-ad-$type-";
107
108 # Locate the queue file. This doesn't deal with multiple files created
109 # with the same timestamp with a non-zero sequence number.
110 my $now = time;
111 my $path;
112 for my $time ($now - 10 .. $now + 1) {
113 my $candidate = $base . strftime('%Y%m%dT%H%M%SZ-00', gmtime($time));
114 if (-f $candidate) {
115 $path = $candidate;
116 last;
117 }
118 }
119 ok(defined($path), 'Queued change found');
120
121 # If we found a file, check the contents and delete the file.
122 SKIP: {
123 if (!defined($path)) {
124 my $count = ($action eq 'password') ? 5 : 4;
125 skip('No queued change found', $count);
126 }
127 my @data = slurp($path, { chomp => 1 });
128 is($data[0], $user, '...queued user is correct');
129 is($data[1], 'ad', '...queued domain is correct');
130 is($data[2], $action, '...queued operation is correct');
131 if ($action eq 'password') {
132 is(scalar(@data), 4, '...no extraneous data');
133 is($data[3], $password, '...queued password is correct');
134 } else {
135 is(scalar(@data), 3, '...no extraneous data');
136 }
137
138 # Unlink the file after checking. This lets us check later that no
139 # extraneous files were created in the queue.
140 if (!unlink($path)) {
141 diag("cannot delete queued change $path: $!");
142 }
143 }
144 return;
145 }
146
147 # Clean out any existing queue if one already exists.
148 my $queue = test_tmpdir() . '/queue';
149 if (-d $queue) {
150 remove_tree($queue);
151 }
152
153 # Start of testing. Try creating each type of change.
154 run_backend_checked('disable', 'test');
155 run_backend_checked('enable', 'longtest');
156 run_backend_checked('password', 'test', 'foobar');
157
158 # Check that the list output is now what we would expect. We can't check the
159 # timestamp directly without messing about with various improbable but
160 # possible time transitions, so just make sure it's in the correct format.
161 my $timestamp = qr{ \d{4}-\d\d-\d\d [ ] \d\d:\d\d:\d\d [ ] UTC }xms;
162 my $expected = qr{
163 \A
164 longtest [ ]{2} enable [ ]{4} ad [ ]{4} $timestamp \n
165 test [ ]{6} disable [ ]{3} ad [ ]{4} $timestamp \n
166 test [ ]{6} password [ ]{2} ad [ ]{4} $timestamp \n
167 \z
168 }xms;
169 my ($status, $out, $err) = run_backend('list');
170 is($status, 0, 'krb5-sync-backend list succeeded');
171 like($out, $expected, '...with correct output');
172 is($err, q{}, '...and no errors');
173
174 # Now check that the created queue files are all correct.
175 check_queued_action('test', 'disable');
176 check_queued_action('test', 'password', 'foobar');
177 check_queued_action('longtest', 'enable');
178
179 # Verify that the lock file exists and that there are no other queued files by
180 # removing the queue.
181 ok(unlink("$queue/.lock"), 'Lock file exists and can be removed');
182 ok(rmdir($queue), 'No extraneous files in the queue');