1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
# Shared Perl functions for tests.
#
# Collects a set of useful utility functions for tests, used by the Perl test
# suite programs. These are intentionally put into the main package rather
# than in a separate namespace.
#
# Written by Russ Allbery <eagle@eyrie.org>
# Copyright 2021 Russ Allbery <eagle@eyrie.org>
# Copyright 2007-2009
# The Board of Trustees of the Leland Stanford Junior University
#
# SPDX-License-Identifier: MIT
require 5.006;
use strict;
use Test::More;
# Make a call to a command with the given arguments. Returns the standard
# output, the standard error, and the exit status as a list.
sub command {
my ($command, @args) = @_;
my $pid = fork;
if (not defined $pid) {
BAIL_OUT ("cannot fork: $!");
} elsif ($pid == 0) {
open (STDOUT, '>', 'command.out')
or BAIL_OUT ("cannot create command.out: $!");
open (STDERR, '>', 'command.err')
or BAIL_OUT ("cannot create command.err: $!");
exec ($command, @args)
or BAIL_OUT ("cannot run $command: $!");
} else {
waitpid ($pid, 0);
}
my $status = ($? >> 8);
local $/;
open (OUT, '<', 'command.out') or BAIL_OUT ("cannot open command.out: $!");
my $output = <OUT>;
close OUT;
open (ERR, '<', 'command.err') or BAIL_OUT ("cannot open command.err: $!");
my $error = <ERR>;
close ERR;
unlink ('command.out', 'command.err');
return ($output, $error, $status);
}
# Returns the one-line contents of a file as a string, removing the newline.
sub contents {
my ($file) = @_;
open (FILE, '<', $file) or BAIL_OUT ("cannot open $file: $!");
my $data = <FILE>;
close FILE;
chomp $data;
return $data;
}
# Given a keytab file, a principal, and additional options for kinit, try
# authenticating with kinit. This is used to do things like get renewable
# tickets. Returns true if successful, false otherwise.
sub kinit {
my ($file, $principal, @opts) = @_;
my @commands = (
"kinit -k -t $file @opts $principal ",
"kinit -t $file @opts $principal ",
"kinit -T /bin/true -k -K $file @opts $principal",
);
for my $command (@commands) {
if (system ("$command >/dev/null 2>&1 </dev/null") == 0) {
return 1;
}
}
return 0;
}
# Run klist and return the default principal, the first service principal
# found, and the flags. Returns both as undef if klist fails.
sub klist {
my $output = `klist -f -5 2>&1`;
return unless $? == 0;
my ($default) = ($output =~ /^(?:Default p|\s*P)rincipal: (\S+)/m);
my ($service) = ($output =~ / Service principal\n(?:\S+\s+){4}(\S+)/);
unless ($service) {
($service) = ($output =~ / Principal\n(?:\S+\s+){9}(\S+)/);
}
unless ($service) {
($service) = ($output =~ / Principal\n(?:\S+\s+){7}(\S+)/);
}
my ($flags) = ($output =~ /\sFlags: (\S+)/);
unless ($flags) {
($flags) = ($output =~ / Flags\s+Principal\n(?:\S+\s+){8}(\S+)/);
}
unless ($flags) {
($flags) = ($output =~ / Flags\s+Principal\n(?:\S+\s+){6}(\S+)/);
}
return wantarray ? ($default, $service, $flags) : $default;
}
# Run tokens and return true if we have an AFS token, false otherwise.
sub tokens {
my $output = `tokens 2>&1`;
return unless $? == 0;
return ($output =~ /^(User\'s \([^\)]+\) )?(\S+ )?[Tt]okens for /m) ? 1 : 0;
}
|