#!/usr/bin/perl -w
#
# Tests for krenew with AFS.
#
# Written by Russ Allbery <rra@stanford.edu>
# Copyright 2008, 2009
#     The Board of Trustees of the Leland Stanford Junior University
#
# See LICENSE for licensing terms.

use Test::More;

# The full path to the newly-built krenew client.
our $KRENEW = "$ENV{BUILD}/../krenew";

# The path to our data directory, which contains the keytab to use to test.
our $DATA = "$ENV{BUILD}/data";

# The path to a shell script that just prints something out.
our $FAKE_AKLOG = "$ENV{SOURCE}/data/fake-aklog";

# Load our test utility programs.
require "$ENV{SOURCE}/libtest.pl";

# Decide whether we have the configuration to run the tests.
my ($principal, $out, $err, $status);
if (not -f "$DATA/test.keytab" or not -f "$DATA/test.principal") {
    plan skip_all => 'no keytab configuration';
    exit 0;
} elsif (not tokens ()) {
    plan skip_all => 'no current AFS tokens';
    exit 0;
} else {
    $principal = contents ("$DATA/test.principal");
    $ENV{KRB5CCNAME} = 'krb5cc_test';
    unlink 'krb5cc_test';
    unless (kinit ("$DATA/test.keytab", $principal, '-r', '2h', '-l', '1h')) {
        plan skip_all => 'cannot get renewable tickets';
        exit 0;
    }
    ($out, $err, $status) = command ($KRENEW, '-t', '--', 'tokens');
    if ($err eq "krenew: cannot create PAG: AFS support is not available\n") {
        plan skip_all => 'not built with AFS support';
        exit 0;
    } else {
        plan tests => 15;
    }
}

# Now, see if we can renew tickets and get a token.
is ($status, 0, 'krenew -t succeeds');
is ($err, '', ' with no errors');
like ($out, qr/^(User\'s \([^\)]+\) )?[Tt]okens for /m,
      ' and the right output');

# Set the token program to something that doesn't obtain tokens.  Everything
# should still work, but we should have no tokens.
$ENV{AKLOG} = '/bin/true';
($out, $err, $status) = command ($KRENEW, '-t', '--', 'tokens');
is ($status, 0, 'krenew -t succeeds with no aklog');
is ($err, '', ' with no errors');
unlike ($out, qr/^(User\'s \([^\)]+\) )?[Tt]okens for /m,
        ' and we have no tokens');
delete $ENV{AKLOG};

# Make sure that we run the right aklog program.  We need to pass -K to reduce
# the wakeup period or krenew will want tickets that last longer than an hour
# (the default -K interval when running a command).
$ENV{AKLOG} = $FAKE_AKLOG;
($out, $err, $status) = command ($KRENEW, '-K', 10, '-t', '--', 'true');
is ($status, 0, 'krenew -t succeeds with fake aklog');
is ($err, '', ' with no errors');
is ($out, "Running fake aklog\n", ' and we ran the fake aklog');
delete $ENV{AKLOG};

# AKLOG should override KINIT_PROG.
$ENV{KINIT_PROG} = $FAKE_AKLOG;
$ENV{AKLOG} = '/bin/true';
($out, $err, $status) = command ($KRENEW, '-t', '--', 'true');
is ($status, 0, 'krenew -t succeeds with /bin/true aklog');
is ($err, '', ' with no errors');
is ($out, '', ' and we did not run KINIT_PROG');
delete $ENV{AKLOG};
delete $ENV{KINIT_PROG};

# KINIT_PROG should still work.
$ENV{KINIT_PROG} = $FAKE_AKLOG;
($out, $err, $status) = command ($KRENEW, '-K', 10, '-t', '--', 'true');
is ($status, 0, 'krenew -t succeeds with KINIT_PROG');
is ($err, '', ' with no errors');
is ($out, "Running fake aklog\n", ' and we ran the fake aklog');
delete $ENV{KINIT_PROG};

unlink 'krb5cc_test';
