Debian Codegraph @

tests/krenew/daemon-t

raw
1 #!/usr/bin/perl -w
2 #
3 # Tests for krenew daemon functionality.
4 #
5 # Written by Russ Allbery <[email protected]>
6 # Copyright 2021 Russ Allbery <[email protected]>
7 # Copyright 2008-2009, 2011-2012, 2014
8 # The Board of Trustees of the Leland Stanford Junior University
9 #
10 # SPDX-License-Identifier: MIT
11
12 use Cwd;
13
14 use Test::More;
15
16 # The full path to the newly-built krenew client.
17 our $KRENEW = "$ENV{C_TAP_BUILD}/../commands/krenew";
18
19 # The path to our data directory, which contains the keytab to use to test.
20 our $DATA = "$ENV{C_TAP_BUILD}/data";
21
22 # The path to our temporary directory used for test ticket caches and the
23 # like.
24 our $TMP = "$ENV{C_TAP_BUILD}/tmp";
25 unless (-d $TMP) {
26 mkdir $TMP or BAIL_OUT ("cannot create $TMP: $!");
27 }
28
29 # Load our test utility programs.
30 require "$ENV{C_TAP_SOURCE}/libtest.pl";
31
32 # Decide whether we have the configuration to run the tests.
33 my ($cwd, $principal);
34 if (not -f "$DATA/test.keytab" or not -f "$DATA/test.principal") {
35 plan skip_all => 'no keytab configuration';
36 exit 0;
37 } else {
38 $principal = contents ("$DATA/test.principal");
39 $ENV{KRB5CCNAME} = "$TMP/krb5cc_test";
40 unlink "$TMP/krb5cc_test";
41 unless (kinit ("$DATA/test.keytab", $principal, '-r', '2h', '-l', '10m')) {
42 plan skip_all => 'cannot get renewable tickets';
43 exit 0;
44 }
45 plan tests => 94;
46 }
47
48 # Start a krenew daemon and be sure it gets tickets and stays running.
49 my $pid = fork;
50 if (!defined $pid) {
51 BAIL_OUT ("can't fork: $!");
52 } elsif ($pid == 0) {
53 open (STDERR, '>', "$TMP/krenew-errors")
54 or BAIL_OUT ("can't create $TMP/krenew-errors: $!");
55 exec ($KRENEW, '-K', 30, '-p', "$TMP/pid")
56 or BAIL_OUT ("can't run $KRENEW: $!");
57 }
58 my $tries = 0;
59 while (not -s "$TMP/pid" and $tries < 100) {
60 select (undef, undef, undef, 0.1);
61 $tries++;
62 }
63 if (-f "$TMP/pid") {
64 my $daemon = contents ("$TMP/pid");
65 is ($pid, $daemon, 'The right PID is written');
66 } else {
67 ok (0, 'The right PID is written');
68 }
69 ok (kill (0, $pid), ' and krenew is still running');
70 unlink "$TMP/krb5cc_test";
71 kill (14, $pid) or warn "Can't kill $pid: $!\n";
72 is (waitpid ($pid, 0), $pid, ' and it dies after failure to renew');
73 is (($? >> 8), 1, ' with non-zero exit status');
74 if (open (ERRORS, '<', "$TMP/krenew-errors")) {
75 like (scalar (<ERRORS>), qr/^krenew: error reading ticket cache: /,
76 ' and the correct error message');
77 } else {
78 ok (0, ' and the correct error message');
79 }
80 unlink "$TMP/krenew-errors";
81 ok (!-f "$TMP/pid", ' and the PID file was removed');
82 kinit ("$DATA/test.keytab", $principal, '-r', '2h', '-l', '10m');
83
84 # Try again with the -b flag.
85 my ($out, $err, $status)
86 = command ($KRENEW, '-bK', 30, '-p', "$TMP/pid");
87 is ($status, 0, 'Backgrounding krenew works');
88 is ($err, '', ' with no error output');
89 is ($out, '', ' and -q was added implicitly');
90 $tries = 0;
91 while (not -s "$TMP/pid" and $tries < 100) {
92 select (undef, undef, undef, 0.1);
93 $tries++;
94 }
95 $pid = contents ("$TMP/pid");
96 ok (kill (0, $pid), ' and the PID file is correct');
97 kill (15, $pid) or warn "Can't kill $pid: $!\n";
98 select (undef, undef, undef, 0.5);
99 ok (!kill (0, $pid), ' and it dies after SIGTERM');
100 ok (!-f "$TMP/pid", ' and the PID file was removed');
101
102 # Now try with -i. In this case, krenew should keep running even if the
103 # ticket cache disappears and be able to start refreshing it again when it
104 # reappears.
105 ($out, $err, $status) = command ($KRENEW, '-biK', 30, '-p', "$TMP/pid");
106 is ($status, 0, 'Backgrounding krenew works');
107 is ($err, '', ' with no error output');
108 is ($out, '', ' and -q was added implicitly');
109 $tries = 0;
110 while (not -s "$TMP/pid" and $tries < 100) {
111 select (undef, undef, undef, 0.1);
112 $tries++;
113 }
114 $pid = contents ("$TMP/pid");
115 ok (kill (0, $pid), ' and the PID file is correct');
116 unlink "$TMP/krb5cc_test";
117 kill (14, $pid) or warn "Can't kill $pid: $!\n";
118 select (undef, undef, undef, 0.5);
119 ok (kill (0, $pid), ' and it keeps running after failure to renew');
120 kinit ("$DATA/test.keytab", $principal, '-r', '2h', '-l', '10m');
121 my $time = (stat "$TMP/krb5cc_test")[9];
122 while (time == $time) {
123 select (undef, undef, undef, 0.1);
124 }
125 is ($time, (stat "$TMP/krb5cc_test")[9], 'Cache has not been touched');
126 kill (14, $pid) or warn "Can't kill $pid: $!\n";
127 $tries = 0;
128 while ($time >= (stat "$TMP/krb5cc_test")[9] && $tries < 10) {
129 select (undef, undef, undef, 0.5);
130 $tries++;
131 }
132 ok ($time < (stat "$TMP/krb5cc_test")[9], ' and is updated after SIGALRM');
133 kill (15, $pid) or warn "Can't kill $pid: $!\n";
134 select (undef, undef, undef, 0.5);
135 ok (!kill (0, $pid), ' and it dies after SIGTERM');
136 ok (!-f "$TMP/pid", ' and the PID file was removed');
137
138 # Check that krenew keeps running if the ticket cache directory is not
139 # writeable.
140 $pid = fork;
141 if (!defined $pid) {
142 BAIL_OUT ("can't fork: $!");
143 } elsif ($pid == 0) {
144 open (STDERR, '>', "$TMP/krenew-errors")
145 or BAIL_OUT ("can't create $TMP/krenew-errors: $!");
146 exec ($KRENEW, '-K', 30, '-p', "$TMP/pid")
147 or BAIL_OUT ("can't run $KRENEW: $!");
148 }
149 $tries = 0;
150 while (not -s "$TMP/pid" and $tries < 100) {
151 select (undef, undef, undef, 0.1);
152 $tries++;
153 }
154 $pid = contents ("$TMP/pid");
155 ok (kill (0, $pid), 'krenew -K 30 started');
156 chmod 0555, $TMP or BAIL_OUT ("cannot chmod $TMP: $!");
157 kill (14, $pid) or warn "Can't kill $pid: $!\n";
158 $tries = 0;
159 while (not -s "$TMP/krenew-errors" and $tries < 100) {
160 select (undef, undef, undef, 0.1);
161 $tries++;
162 }
163 ok (kill (0, $pid), ' and it keeps running on a non-writeable cache');
164 chmod 0755, $TMP or BAIL_OUT ("cannot chmod $TMP: $!");
165 if (open (ERRORS, '<', "$TMP/krenew-errors")) {
166 like (scalar (<ERRORS>), qr/^krenew: error reinitializing cache: /,
167 ' and the correct error message');
168 } else {
169 ok (0, ' and the correct error message');
170 }
171 unlink "$TMP/krenew-errors";
172 kill (15, $pid) or warn "Can't kill $pid: $!\n";
173 is (waitpid ($pid, 0), $pid, ' and it dies on SIGTERM');
174 ok (!-f "$TMP/pid", ' and the PID file was removed');
175
176 # If we do that again with -x, krenew should exit.
177 ($out, $err, $status) = command ($KRENEW, '-xbK', 30, '-p', "$TMP/pid");
178 is ($status, 0, 'krenew -xb works');
179 is ($err, '', ' with no error output');
180 is ($out, '', ' and no regular output');
181 $tries = 0;
182 while (not -s "$TMP/pid" and $tries < 100) {
183 select (undef, undef, undef, 0.1);
184 $tries++;
185 }
186 $pid = contents ("$TMP/pid");
187 ok (kill (0, $pid), 'krenew -xb started');
188 chmod 0555, $TMP or BAIL_OUT ("cannot chmod $TMP: $!");
189 kill (14, $pid) or warn "Can't kill $pid: $!\n";
190 $tries = 0;
191 while (kill (0, $pid) and $tries < 100) {
192 select (undef, undef, undef, 0.1);
193 $tries++;
194 }
195 ok (!kill (0, $pid), ' and it exits on a non-writeable cache');
196 chmod 0755, $TMP or BAIL_OUT ("cannot chmod $TMP: $!");
197 unlink "$TMP/pid";
198
199 # Now, run a command in the background.
200 unlink 'child-out';
201 ($out, $err, $status)
202 = command ($KRENEW, '-bK', 30, '-p', "$TMP/pid", '-c', "$TMP/child-pid",
203 '--', "$ENV{C_TAP_SOURCE}/data/command", "$TMP/child-out");
204 is ($status, 0, 'Backgrounding krenew works');
205 is ($err, '', ' with no error output');
206 is ($out, '', ' and output was redirected properly');
207 $tries = 0;
208 while (not -s "$TMP/child-pid" and $tries < 100) {
209 select (undef, undef, undef, 0.1);
210 $tries++;
211 }
212 $pid = contents ("$TMP/pid");
213 ok (kill (0, $pid), 'krenew is running');
214 $child = contents ("$TMP/child-pid");
215 ok (kill (0, $child), 'The child process is running');
216 $tries = 0;
217 while (not -s "$TMP/child-out" and $tries < 100) {
218 select (undef, undef, undef, 0.1);
219 $tries++;
220 }
221 kill (1, $pid) or warn "Cannot send HUP to parent $pid: $!\n";
222 select (undef, undef, undef, 0.1);
223 kill (2, $pid) or warn "Cannot send INT to parent $pid: $!\n";
224 select (undef, undef, undef, 0.1);
225 kill (15, $pid) or warn "Cannot send TERM to parent $pid: $!\n";
226 $tries = 0;
227 while (kill (0, $pid) and $tries < 100) {
228 select (undef, undef, undef, 0.1);
229 $tries++;
230 }
231 ok (!kill (0, $pid), 'krenew is no longer running');
232 ok (!kill (0, $child), 'The child process is no longer running');
233 open (OUT, '<', "$TMP/child-out")
234 or BAIL_OUT ("cannot open $TMP/child-out: $!");
235 is (scalar (<OUT>), "$child\n", 'Child PID is correct');
236 is (scalar (<OUT>), "/\n", 'Child working directory is /');
237 my $cache = scalar <OUT>;
238 like ($cache, qr%^/tmp/krb5cc_%, 'Child cache is correct');
239 is (scalar (<OUT>), "got SIGHUP\n", 'SIGHUP was recorded');
240 is (scalar (<OUT>), "got SIGINT\n", 'SIGINT was recorded');
241 is (scalar (<OUT>), "got SIGTERM\n", 'SIGTERM was recorded');
242 ok (eof OUT, 'No more child output written');
243 chomp $cache;
244 ok (! -f $cache, 'New child cache removed');
245 ok (!-f "$TMP/pid", ' and the PID file was removed');
246 ok (!-f "$TMP/child-pid", ' and the child PID file was removed');
247 unlink "$TMP/child-out";
248
249 # One more time to test propagation of QUIT signals.
250 ($out, $err, $status)
251 = command ($KRENEW, '-bK', 30, '-p', "$TMP/pid", '-c', "$TMP/child-pid",
252 '--', "$ENV{C_TAP_SOURCE}/data/command", "$TMP/child-out");
253 is ($status, 0, 'Backgrounding krenew works');
254 is ($err, '', ' with no error output');
255 is ($out, '', ' and output was redirected properly');
256 $tries = 0;
257 while (not -s "$TMP/child-pid" and $tries < 100) {
258 select (undef, undef, undef, 0.1);
259 $tries++;
260 }
261 $pid = contents ("$TMP/pid");
262 ok (kill (0, $pid), 'krenew is running');
263 $child = contents ("$TMP/child-pid");
264 ok (kill (0, $child), 'The child process is running');
265 $tries = 0;
266 while (not -s "$TMP/child-out" and $tries < 100) {
267 select (undef, undef, undef, 0.1);
268 $tries++;
269 }
270 kill (3, $pid) or warn "Cannot send QUIT to parent $pid: $!\n";
271 $tries = 0;
272 while (kill (0, $pid) and $tries < 100) {
273 select (undef, undef, undef, 0.1);
274 $tries++;
275 }
276 ok (!kill (0, $pid), 'krenew is no longer running');
277 ok (!kill (0, $child), 'The child process is no longer running');
278 open (OUT, '<', "$TMP/child-out")
279 or BAIL_OUT ("cannot open $TMP/child-out: $!");
280 is (scalar (<OUT>), "$child\n", 'Child PID is correct');
281 is (scalar (<OUT>), "/\n", 'Child working directory is /');
282 $cache = scalar <OUT>;
283 like ($cache, qr%^/tmp/krb5cc_%, 'Child cache is correct');
284 is (scalar (<OUT>), "got SIGQUIT\n", 'SIGQUIT was recorded');
285 ok (eof OUT, 'No more child output written');
286 close OUT;
287 chomp $cache;
288 ok (! -f $cache, 'New child cache removed');
289 ok (!-f "$TMP/pid", ' and the PID file was removed');
290 ok (!-f "$TMP/child-pid", ' and the child PID file was removed');
291 unlink "$TMP/child-out";
292
293 # Normally, if we are running a command and krenew has to exit because it
294 # can't renew the ticket cache any more, it should exit and leave the command
295 # running.
296 ($out, $err, $status)
297 = command ($KRENEW, '-bK', 30, '-p', "$TMP/pid", '-c', "$TMP/child-pid",
298 '--', "$ENV{C_TAP_SOURCE}/data/command", "$TMP/child-out");
299 is ($status, 0, 'Backgrounding krenew works');
300 is ($err, '', ' with no error output');
301 is ($out, '', ' and output was redirected properly');
302 $tries = 0;
303 while (not -s "$TMP/child-pid" and $tries < 100) {
304 select (undef, undef, undef, 0.1);
305 $tries++;
306 }
307 $pid = contents ("$TMP/pid");
308 ok (kill (0, $pid), 'krenew is running');
309 $child = contents ("$TMP/child-pid");
310 ok (kill (0, $child), 'The child process is running');
311 $tries = 0;
312 while (not -s "$TMP/child-out" and $tries < 100) {
313 select (undef, undef, undef, 0.1);
314 $tries++;
315 }
316 open (OUT, '<', "$TMP/child-out")
317 or BAIL_OUT ("cannot open $TMP/child-out: $!");
318 is (scalar (<OUT>), "$child\n", 'Child PID is correct');
319 is (scalar (<OUT>), "/\n", 'Child working directory is /');
320 $cache = scalar <OUT>;
321 chomp $cache;
322 like ($cache, qr%^/tmp/krb5cc_%, 'Child cache is correct');
323 unlink $cache;
324 kill (14, $pid) or warn "Can't kill $pid: $!\n";
325 select (undef, undef, undef, 0.2);
326 ok (!kill (0, $pid), 'krenew dies after failure to renew');
327 ok (kill (0, $child), ' and the child process is still running');
328 kill (15, $child) or warn "Can't kill child $child: $!\n";
329 select (undef, undef, undef, 0.2);
330 ok (!kill (0, $child), 'The child process is no longer running');
331 is (scalar (<OUT>), "got SIGTERM\n", 'SIGTERM was recorded');
332 ok (eof OUT, 'No more child output written');
333 close OUT;
334 ok (!-f "$TMP/pid", ' and the PID file was removed');
335 ok (!-f "$TMP/child-pid", ' and the child PID file was removed');
336 unlink "$TMP/child-out";
337
338 # If run with -s, krenew should instead kill the child process with HUP on
339 # failure to renew the ticket.
340 ($out, $err, $status)
341 = command ($KRENEW, '-bsK', 30, '-p', "$TMP/pid", '-c', "$TMP/child-pid",
342 '--', "$ENV{C_TAP_SOURCE}/data/command", "$TMP/child-out");
343 is ($status, 0, 'Backgrounding krenew -s works');
344 is ($err, '', ' with no error output');
345 is ($out, '', ' and output was redirected properly');
346 $tries = 0;
347 while (not -s "$TMP/child-pid" and $tries < 100) {
348 select (undef, undef, undef, 0.1);
349 $tries++;
350 }
351 $pid = contents ("$TMP/pid");
352 ok (kill (0, $pid), 'krenew is running');
353 $child = contents ("$TMP/child-pid");
354 ok (kill (0, $child), 'The child process is running');
355 $tries = 0;
356 while (not -s "$TMP/child-out" and $tries < 100) {
357 select (undef, undef, undef, 0.1);
358 $tries++;
359 }
360 open (OUT, '<', "$TMP/child-out")
361 or BAIL_OUT ("cannot open $TMP/child-out: $!");
362 is (scalar (<OUT>), "$child\n", 'Child PID is correct');
363 is (scalar (<OUT>), "/\n", 'Child working directory is /');
364 $cache = scalar <OUT>;
365 chomp $cache;
366 like ($cache, qr%^/tmp/krb5cc_%, 'Child cache is correct');
367 unlink $cache;
368 kill (14, $pid) or warn "Can't kill $pid: $!\n";
369 select (undef, undef, undef, 0.2);
370 ok (!kill (0, $pid), 'krenew dies after failure to renew');
371 ok (kill (0, $child), ' and the child process is still running');
372 kill (15, $child) or warn "Can't kill child $child: $!\n";
373 select (undef, undef, undef, 0.2);
374 ok (!kill (0, $child), 'The child process is no longer running');
375 is (scalar (<OUT>), "got SIGHUP\n", 'SIGHUP was recorded');
376 is (scalar (<OUT>), "got SIGTERM\n", 'SIGTERM was recorded');
377 ok (eof OUT, 'No more child output written');
378 close OUT;
379 ok (!-f "$TMP/pid", ' and the PID file was removed');
380 ok (!-f "$TMP/child-pid", ' and the child PID file was removed');
381 unlink "$TMP/child-out";
382
383 # Clean up.
384 unlink "$TMP/krb5cc_test", "$TMP/child-out";
385 rmdir $TMP;