#!/usr/bin/perl -w
#
# Tests for krenew support of keyrings.
#
# Written by Russ Allbery <eagle@eyrie.org>
# Copyright 2011, 2014
#     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 k5start 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";

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

# Decide whether we have the configuration to run the tests.
my $principal;
if (not -f "$DATA/test.keytab" or not -f "$DATA/test.principal") {
    plan skip_all => 'no keytab configuration';
    exit 0;
} else {
    $principal = contents ("$DATA/test.principal");
    $ENV{KRB5CCNAME} = 'KEYRING:test';
    unless (kinit ("$DATA/test.keytab", $principal, '-r', '2h', '-l', '10m')) {
        plan skip_all => 'cannot get renewable tickets in keyring';
        exit 0;
    }
    unless (!-f 'KEYRING:test' && klist ()) {
        plan skip_all => 'cannot use keyring caches';
        exit 0;
    }
    plan tests => 5;
}

# Basic renewal test.
my ($out, $err, $status) = command ($KRENEW, '-v');
is ($status, 0, 'Basic krenew command succeeds');
is ($err, '', ' with no errors');
like ($out, qr/^krenew: renewing credentials for \Q$principal\E(\@\S+)?\n\z/,
      ' and the right output');
my ($default, $service) = klist ();
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, ' for the right principal');
like ($service, qr%^krbtgt/%, ' and the right service');
