diff options
Diffstat (limited to 'tests/run_test.sh')
| -rwxr-xr-x | tests/run_test.sh | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/tests/run_test.sh b/tests/run_test.sh new file mode 100755 index 0000000..5173fa3 --- /dev/null +++ b/tests/run_test.sh @@ -0,0 +1,106 @@ +#!/usr/bin/env bash +# --------------------------------------------------------------- +# Integration-test helper for OPC UA discovery. +# +# Usage: tests/run_test.sh <config_dir> <expected_policy> +# +# config_dir — directory containing server_lds.conf, +# server_register.conf, client_find_servers.conf +# expected_policy — security-policy string that must appear in +# the client's endpoint listing (e.g. +# "Basic256Sha256", "Aes128_Sha256_RsaOaep", +# or "None") +# +# Exit: 0 when all checks pass, 1 on any failure. +# --------------------------------------------------------------- +set -uo pipefail +# NOTE: we intentionally omit "set -e" so that every check runs +# even if the client itself returns non-zero. + +CONFIG_DIR="${1:?Usage: $0 <config_dir> <expected_policy>}" +EXPECTED_POLICY="${2:?Usage: $0 <config_dir> <expected_policy>}" + +LDS_PORT=14840 +SR_PORT=14841 +LDS_PID="" +SR_PID="" +TMPFILE="" +FAILURES=0 + +# ── cleanup ──────────────────────────────────────────────────── +cleanup() { + [ -n "$LDS_PID" ] && kill "$LDS_PID" 2>/dev/null && wait "$LDS_PID" 2>/dev/null + [ -n "$SR_PID" ] && kill "$SR_PID" 2>/dev/null && wait "$SR_PID" 2>/dev/null + [ -n "$TMPFILE" ] && rm -f "$TMPFILE" +} +trap cleanup EXIT + +# ── port check ───────────────────────────────────────────────── +for port in $LDS_PORT $SR_PORT; do + if ss -tlnp 2>/dev/null | grep -q ":${port} "; then + echo "FAIL: port $port is already in use" + exit 1 + fi +done + +# ── start LDS ────────────────────────────────────────────────── +build/ServerLDS "$CONFIG_DIR/server_lds.conf" >/dev/null 2>&1 & +LDS_PID=$! +sleep 2 +if ! kill -0 "$LDS_PID" 2>/dev/null; then + echo "FAIL: ServerLDS exited prematurely" + exit 1 +fi + +# ── start ServerRegister ─────────────────────────────────────── +build/ServerRegister "$CONFIG_DIR/server_register.conf" >/dev/null 2>&1 & +SR_PID=$! +sleep 3 +if ! kill -0 "$SR_PID" 2>/dev/null; then + echo "FAIL: ServerRegister exited prematurely" + exit 1 +fi + +# ── run client ───────────────────────────────────────────────── +TMPFILE=$(mktemp) +# UA_Log_Stdout writes to stdout; capture both stdout and stderr. +build/ClientFindServers "$CONFIG_DIR/client_find_servers.conf" >"$TMPFILE" 2>&1 +CLIENT_RC=$? +CLIENT_OUTPUT=$(<"$TMPFILE") + +# ── validation checks ───────────────────────────────────────── +check() { + local label="$1" result="$2" + if [ "$result" -eq 0 ]; then + echo "PASS: $label" + else + echo "FAIL: $label" + FAILURES=$((FAILURES + 1)) + fi +} + +# 1. Exit code +[ "$CLIENT_RC" -eq 0 ] +check "client exit code is 0 (got $CLIENT_RC)" $? + +# 2. FindServers returned the registered server +echo "$CLIENT_OUTPUT" | grep -q "urn:bobink.ServerRegister" +check "FindServers contains urn:bobink.ServerRegister" $? + +# 3. Client read the current time +echo "$CLIENT_OUTPUT" | grep -q "date is:" +check "client read current time" $? + +# 4. Endpoint lists expected security policy +echo "$CLIENT_OUTPUT" | grep -q "$EXPECTED_POLICY" +check "endpoint contains $EXPECTED_POLICY" $? + +# ── result ───────────────────────────────────────────────────── +if [ "$FAILURES" -ne 0 ]; then + echo "" + echo "--- client output ---" + echo "$CLIENT_OUTPUT" + echo "--- end ---" + exit 1 +fi +exit 0 |
