From 9eec586e7b6275fc25c9bcd657e5753898ee475e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Mon, 19 Oct 2026 17:21:12 -0300 Subject: [PATCH] test(fips): move public key fallback cases to Bats --- xCAT-test/bats/fips_public_key_fallback.bats | 80 ++++++++++++++++++++ xCAT-test/unit/fips_public_key_fallback.t | 49 ------------ 2 files changed, 80 insertions(+), 49 deletions(-) create mode 100644 xCAT-test/bats/fips_public_key_fallback.bats delete mode 100644 xCAT-test/unit/fips_public_key_fallback.t diff --git a/xCAT-test/bats/fips_public_key_fallback.bats b/xCAT-test/bats/fips_public_key_fallback.bats new file mode 100644 index 0000000000..8b471cde9d --- /dev/null +++ b/xCAT-test/bats/fips_public_key_fallback.bats @@ -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' +} diff --git a/xCAT-test/unit/fips_public_key_fallback.t b/xCAT-test/unit/fips_public_key_fallback.t deleted file mode 100644 index 790bc6d567..0000000000 --- a/xCAT-test/unit/fips_public_key_fallback.t +++ /dev/null @@ -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();