~ubuntu-branches/ubuntu/trusty/kstart/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/krenew/daemon-t

  • Committer: Package Import Robot
  • Author(s): Russ Allbery
  • Date: 2012-02-04 18:05:16 UTC
  • mfrom: (1.1.11) (2.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20120204180516-hjeqs2sl3hpvc46p
Tags: 4.1-2
* Update to debhelper compatibility level V9.
  - Enable compiler hardening flags, including bindnow and PIE.
* Move single-debian-patch to local-options and patch-header to
  local-patch-header so that they only apply to the packages I build and
  NMUs get regular version-numbered patches.

Show diffs side-by-side

added added

removed removed

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