2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-25 20:02:19 +00:00

test(fips): move public key fallback cases to Bats

This commit is contained in:
Vinícius Ferrão
2026-10-19 17:21:12 -03:00
parent e07f488643
commit 9eec586e7b
2 changed files with 80 additions and 49 deletions
@@ -0,0 +1,80 @@
#!/usr/bin/env bats
load 'helpers/shell_source'
setup()
{
mkdir -p "$BATS_TEST_TMPDIR/bin"
cat >"$BATS_TEST_TMPDIR/bin/openssl" <<'SH'
#!/bin/sh
printf '%s\n' "$1" >> "$CALLS"
if [ "$1" = "$ACCEPT" ]; then
printf 'public key\n'
exit 0
fi
printf '%s failed\n' "$1" >&2
exit 73
SH
chmod 0755 "$BATS_TEST_TMPDIR/bin/openssl"
export PATH="$BATS_TEST_TMPDIR/bin:$PATH"
export CALLS="$BATS_TEST_TMPDIR/calls"
export RESULT="$BATS_TEST_TMPDIR/result"
}
check_fallback()
{
local shell="$1" library="$2" accept="$3" calls="$4" mode
local expected_status=0
export ACCEPT="$accept"
printf '%s' "$calls" >"$BATS_TEST_TMPDIR/expected-calls"
if [ "$accept" = none ]; then
expected_status=73
printf 'ec failed\n' >"$BATS_TEST_TMPDIR/expected-result"
else
printf 'public key\n' >"$BATS_TEST_TMPDIR/expected-result"
fi
for mode in 0 1; do
printf '# %s policy mode %s\n' "$library" "$mode" >&3
: >"$CALLS"
run "$shell" -c '
exec >"$RESULT" 2>&1
. "$1"
xcat_discovery_public_key "$2" unused-key
' fips-fallback-test "$(repo_path "$library")" "$mode"
[ "$status" -eq "$expected_status" ]
cmp "$BATS_TEST_TMPDIR/expected-result" "$RESULT"
cmp "$BATS_TEST_TMPDIR/expected-calls" "$CALLS"
done
}
@test "Genesis exports through pkey without trying fallbacks" {
check_fallback sh xCAT-genesis-scripts/usr/lib/xcat/fips.sh pkey $'pkey\n'
}
@test "Genesis falls back to rsa and stops on success" {
check_fallback sh xCAT-genesis-scripts/usr/lib/xcat/fips.sh rsa $'pkey\nrsa\n'
}
@test "Genesis falls back to ec and hides earlier errors" {
check_fallback sh xCAT-genesis-scripts/usr/lib/xcat/fips.sh ec $'pkey\nrsa\nec\n'
}
@test "Genesis returns the final error when all exporters fail" {
check_fallback sh xCAT-genesis-scripts/usr/lib/xcat/fips.sh none $'pkey\nrsa\nec\n'
}
@test "Cumulus exports through pkey without trying fallbacks" {
check_fallback bash xCAT/postscripts/xcatlib.sh pkey $'pkey\n'
}
@test "Cumulus falls back to rsa and stops on success" {
check_fallback bash xCAT/postscripts/xcatlib.sh rsa $'pkey\nrsa\n'
}
@test "Cumulus falls back to ec and hides earlier errors" {
check_fallback bash xCAT/postscripts/xcatlib.sh ec $'pkey\nrsa\nec\n'
}
@test "Cumulus returns the final error when all exporters fail" {
check_fallback bash xCAT/postscripts/xcatlib.sh none $'pkey\nrsa\nec\n'
}
-49
View File
@@ -1,49 +0,0 @@
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";
use File::Path qw(make_path);
use File::Slurper qw(read_text write_text);
use File::Temp qw(tempdir);
use Test::More;
use XCAT::Test::File qw(repo_path);
use XCAT::Test::Process qw(run_command);
my $root = tempdir(CLEANUP => 1);
make_path("$root/bin");
write_text("$root/bin/openssl", <<'SH');
#!/bin/sh
printf '%s\n' "$1" >> "$CALLS"
if [ "$1" = "$ACCEPT" ]; then
printf 'public key\n'
exit 0
fi
printf '%s failed\n' "$1" >&2
exit 73
SH
chmod 0755, "$root/bin/openssl" or die "chmod openssl: $!";
local $ENV{PATH} = "$root/bin:$ENV{PATH}";
local $ENV{CALLS} = "$root/calls";
for my $library ('xCAT-genesis-scripts/usr/lib/xcat/fips.sh', 'xCAT/postscripts/xcatlib.sh') {
my $shell = $library =~ /genesis/ ? 'sh' : 'bash';
for my $mode (0, 1) {
for my $case (['pkey', "pkey\n"], ['rsa', "pkey\nrsa\n"],
['ec', "pkey\nrsa\nec\n"], ['none', "pkey\nrsa\nec\n"]) {
my ($accept, $calls) = @$case;
local $ENV{ACCEPT} = $accept;
write_text($ENV{CALLS}, '');
my ($rc, $output) = run_command($shell, '-c',
'. "$1"; xcat_discovery_public_key "$2" unused-key',
'fips-fallback-test', repo_path($library), $mode);
is($rc, $accept eq 'none' ? 73 : 0, "$library mode $mode exports through $accept");
is($output, $accept eq 'none' ? "ec failed\n" : "public key\n",
'only the final error or successful key reaches the caller');
is(read_text($ENV{CALLS}), $calls, 'fallbacks run in order and stop on success');
}
}
}
done_testing();