| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# Tests for basic krenew functionality. |
| 4 |
# |
| 5 |
# Written by Russ Allbery <[email protected]> |
| 6 |
# Copyright 2015, 2021 Russ Allbery <[email protected]> |
| 7 |
# Copyright 2008-2009, 2011 |
| 8 |
# The Board of Trustees of the Leland Stanford Junior University |
| 9 |
# |
| 10 |
# SPDX-License-Identifier: MIT |
| 11 |
|
| 12 |
use IPC::Open3 qw(open3); |
| 13 |
use POSIX qw(SIGTERM); |
| 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 |
# Load our test utility programs. |
| 23 |
require "$ENV{C_TAP_SOURCE}/libtest.pl"; |
| 24 |
|
| 25 |
# Decide whether we have the configuration to run the tests. |
| 26 |
my $principal; |
| 27 |
if (not -f "$DATA/test.keytab" or not -f "$DATA/test.principal") { |
| 28 |
plan skip_all => 'no keytab configuration'; |
| 29 |
exit 0; |
| 30 |
} else { |
| 31 |
$principal = contents ("$DATA/test.principal"); |
| 32 |
$ENV{KRB5CCNAME} = 'krb5cc_test'; |
| 33 |
unlink 'krb5cc_test'; |
| 34 |
unless (kinit ("$DATA/test.keytab", $principal, '-r', '2h', '-l', '10m')) { |
| 35 |
plan skip_all => 'cannot get renewable tickets'; |
| 36 |
exit 0; |
| 37 |
} |
| 38 |
plan tests => 39; |
| 39 |
} |
| 40 |
|
| 41 |
# We should be okay for five minute tickets without doing anything. |
| 42 |
($out, $err, $status) = command ($KRENEW, '-vH', '5'); |
| 43 |
is ($status, 0, 'krenew -H 5 succeeds without reauthenticating'); |
| 44 |
is ($err, '', ' with no errors'); |
| 45 |
is ($out, '', ' and no output'); |
| 46 |
|
| 47 |
# We should be able to renew to get 30 minute tickets. |
| 48 |
($out, $err, $status) = command ($KRENEW, '-vH', '30'); |
| 49 |
is ($status, 0, 'krenew -H 30 succeeds with authentication'); |
| 50 |
is ($err, '', ' with no errors'); |
| 51 |
like ($out, |
| 52 |
qr/^krenew: renewing credentials for \Q$principal\E(\@\S+)?\n\z/, |
| 53 |
' and the right output'); |
| 54 |
|
| 55 |
# But we fail if we try to get 3 hour tickets, since we can't renew for |
| 56 |
# that long. |
| 57 |
($out, $err, $status) = command ($KRENEW, '-vH', '180'); |
| 58 |
is ($status, 1, 'krenew -H 180 fails'); |
| 59 |
is ($err, "krenew: ticket cannot be renewed for long enough\n", |
| 60 |
' with the right error'); |
| 61 |
is ($out, '', ' and no output'); |
| 62 |
|
| 63 |
# Test running a command. klist may fail if we have no K4 tickets since |
| 64 |
# we're not giving the -5 option; allow for that (we'll catch real |
| 65 |
# failures in the regex match on the output). |
| 66 |
($out, $err, $status) = command ($KRENEW, 'klist'); |
| 67 |
ok ($status == 0 || $status == 1, 'krenew with command succeeds'); |
| 68 |
ok ($err eq '' || $err eq "klist: You have no tickets cached\n", |
| 69 |
' with no or expected errors'); |
| 70 |
like ($out, qr,^\s*(Ticket|Credentials)\ cache: |
| 71 |
\ (FILE:)?/tmp/krb5cc_\d+_\S{6}\n |
| 72 |
\s*(Default\ )?[Pp]rincipal:\ \Q$principal\E(\@\S+)?\n,xm, |
| 73 |
' and the right output'); |
| 74 |
($cache) = ($out =~ /cache: (?:FILE:)?(\S+)/); |
| 75 |
ok (!$cache || !-f $cache, ' and the new cache file was deleted'); |
| 76 |
ok (-f 'krb5cc_test', ' but the default one was unaffected'); |
| 77 |
|
| 78 |
# Test running a command with --. |
| 79 |
($out, $err, $status) = command ($KRENEW, '--', 'klist', '-5'); |
| 80 |
is ($status, 0, 'krenew with command and -- succeeds'); |
| 81 |
is ($err, '', ' with no errors'); |
| 82 |
like ($out, qr,^\s*(Ticket|Credentials)\ cache: |
| 83 |
\ (FILE:)?/tmp/krb5cc_\d+_\S{6}\n |
| 84 |
\s*(Default\ )?[Pp]rincipal:\ \Q$principal\E(\@\S+)?\n,xm, |
| 85 |
' and the right output'); |
| 86 |
($cache) = ($out =~ /cache: (?:FILE:)?(\S+)/); |
| 87 |
ok (!$cache || !-f $cache, ' and the new cache file was deleted'); |
| 88 |
ok (-f 'krb5cc_test', ' but the default one was unaffected'); |
| 89 |
|
| 90 |
# Test propagation of exit status from a command. |
| 91 |
($out, $err, $status) |
| 92 |
= command ($KRENEW, '--', 'sh', '-c', 'klist -5; exit 3'); |
| 93 |
is ($status, 3, 'krenew of exit 3 returns correct exit status'); |
| 94 |
is ($err, '', ' with no errors'); |
| 95 |
like ($out, qr,^\s*(Ticket|Credentials)\ cache: |
| 96 |
\ (FILE:)?/tmp/krb5cc_\d+_\S{6}\n |
| 97 |
\s*(Default\ )?[Pp]rincipal:\ \Q$principal\E(\@\S+)?\n,xm, |
| 98 |
' and the right output'); |
| 99 |
($cache) = ($out =~ /cache: (?:FILE:)?(\S+)/); |
| 100 |
ok (!$cache || !-f $cache, ' and the new cache file was deleted'); |
| 101 |
ok (-f 'krb5cc_test', ' and the default cache file was unaffected'); |
| 102 |
|
| 103 |
# Test propagation of exit status from a command which is killed by signal. |
| 104 |
($out, $err, $status) = command ($KRENEW, '--', 'sh', '-c', 'kill $$'); |
| 105 |
is ($status, 128 + SIGTERM, 'krenew of kill $$ returns correct exit status'); |
| 106 |
is ($err, '', ' with no errors'); |
| 107 |
ok (-f 'krb5cc_test', ' and the default cache file was unaffected'); |
| 108 |
|
| 109 |
# Check exit status if the ticket cache doesn't exist. |
| 110 |
unlink 'krb5cc_test'; |
| 111 |
($out, $err, $status) = command ($KRENEW); |
| 112 |
is ($status, 1, 'krenew fails with no ticket cache'); |
| 113 |
is ($out, '', ' with no output'); |
| 114 |
like ($err, qr/^krenew: error reading ticket cache: /, ' and the right error'); |
| 115 |
($out, $err, $status) = command ($KRENEW, '-vH', 5); |
| 116 |
is ($status, 1, 'krenew -H fails with no ticket cache'); |
| 117 |
is ($out, '', ' with no output'); |
| 118 |
like ($err, qr/^krenew: error reading ticket cache: /, ' and the right error'); |
| 119 |
|
| 120 |
# If -i was given, we keep running and keep trying and have to be explicitly |
| 121 |
# killed. |
| 122 |
my $pid = open3 ('<&0', \*OUT, \*ERR, $KRENEW, '-vi') |
| 123 |
or BAIL_OUT ("can't run $KRENEW: $!"); |
| 124 |
select (undef, undef, undef, 1.0); |
| 125 |
kill (15, $pid); |
| 126 |
$out = <OUT>; |
| 127 |
$err = <ERR>; |
| 128 |
close OUT; |
| 129 |
close ERR; |
| 130 |
waitpid ($pid, 0); |
| 131 |
$status = ($? >> 8); |
| 132 |
is ($status, 1, 'krenew -i fails with no ticket cache'); |
| 133 |
is ($out, undef, ' with no output'); |
| 134 |
like ($err, qr/^krenew: error reading ticket cache: /, ' and the right error'); |
| 135 |
$pid = open3 ('<&0', \*OUT, \*ERR, $KRENEW, '-viH', 5) |
| 136 |
or BAIL_OUT ("can't run $KRENEW: $!"); |
| 137 |
select (undef, undef, undef, 1.0); |
| 138 |
kill (15, $pid); |
| 139 |
$out = <OUT>; |
| 140 |
$err = <ERR>; |
| 141 |
close OUT; |
| 142 |
close ERR; |
| 143 |
waitpid ($pid, 0); |
| 144 |
$status = ($? >> 8); |
| 145 |
is ($status, 1, 'krenew -iH fails with no ticket cache'); |
| 146 |
is ($out, undef, ' with no output'); |
| 147 |
like ($err, qr/^krenew: error reading ticket cache: /, ' and the right error'); |