#!/bin/sh
# autopkgtest for libzpc2-provider: hbkzpc OpenSSL provider module
#
# This is a load/registration smoke test for the zpcprovider.so module.
# It intentionally does NOT perform actual protected-key crypto
# operations (sign/keygen/etc.), since those require real IBM Z CPACF
# hardware (and, for some origin types, an Ultravisor Secure Execution
# guest), which is not guaranteed to be available/enabled on every
# autopkgtest worker. Instead, it verifies that:
#  - the provider module can be loaded by OpenSSL at all (i.e. dynamic
#    linking against libcrypto/libjson-c works, and OSSL_provider_init
#    succeeds and reports itself as active), and
#  - the provider registers its expected key-management/signature
#    algorithms (EC, ED25519, ED448) under its "hbkzpc" provider name.
#
# This package is only built for s390x, so this test is automatically
# skipped by autopkgtest on any other architecture.

set -e

MODDIR="/usr/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/ossl-modules"

test -f "$MODDIR/zpcprovider.so"

echo "== provider load and status =="
openssl list -provider-path "$MODDIR" -provider zpcprovider -providers -verbose

openssl list -provider-path "$MODDIR" -provider zpcprovider -providers -verbose \
	| grep -A5 '^  zpcprovider$' \
	| grep -q 'name: hbkzpc'

echo "== registered key managers =="
openssl list -provider-path "$MODDIR" -provider zpcprovider -key-managers -verbose \
	| tee "$AUTOPKGTEST_TMP/keymgmt.out"
grep -E '@ zpcprovider$' "$AUTOPKGTEST_TMP/keymgmt.out" > "$AUTOPKGTEST_TMP/keymgmt.zpcprovider.out"
grep -qw 'EC' "$AUTOPKGTEST_TMP/keymgmt.zpcprovider.out"
grep -qw 'ED25519' "$AUTOPKGTEST_TMP/keymgmt.zpcprovider.out"
grep -qw 'ED448' "$AUTOPKGTEST_TMP/keymgmt.zpcprovider.out"

echo "== registered signature algorithms =="
openssl list -provider-path "$MODDIR" -provider zpcprovider -signature-algorithms -verbose \
	| tee "$AUTOPKGTEST_TMP/sig.out"
grep -E '@ zpcprovider$' "$AUTOPKGTEST_TMP/sig.out" > "$AUTOPKGTEST_TMP/sig.zpcprovider.out"
grep -qw 'ECDSA' "$AUTOPKGTEST_TMP/sig.zpcprovider.out"
grep -qw 'ED25519' "$AUTOPKGTEST_TMP/sig.zpcprovider.out"
grep -qw 'ED448' "$AUTOPKGTEST_TMP/sig.zpcprovider.out"

echo "PASS: hbkzpc provider load/registration smoke test"
