#!/bin/sh

# Checking if the PSK authentication works properly when the session is resumed.
# PSK identities and corresponding keys are stored in files specified with PSKsecrets.
# Just "1" "accepted: new session negotiated" log is expected for [server] service
# because the client presents the valid PSK.

. $(dirname $0)/../test_library

start() {
  ../../src/stunnel -fd 0 <<EOT
  debug = debug
  syslog = no
  pid = ${result_path}/stunnel.pid
  output = ${result_path}/stunnel.log

  delay = yes
  retry = yes

  [client]
  client = yes
  exec = ${script_path}/execute_read
  execArgs = execute_read ${result_path}/temp.log
  connect = 127.0.0.1:${https1}
  PSKsecrets = ${script_path}/certs/psk1.txt

  [server]
  accept = 127.0.0.1:${https1}
  exec = ${script_path}/execute
  execArgs = execute 046_resume_PSK
  PSKsecrets = ${script_path}/certs/secrets.txt
EOT
}

# The PSKsecrets option is only available when compiled with OpenSSL 1.0.0 and later.
# The resumption of the session does not work for the FORK model
if grep -q -e "OpenSSL [1-9]" "results.log" && ! grep -q "FORK" "results.log"
  then
    test_log_for "046_resume_PSK" "session" "1" "$1" "$2" "$3" 2>> "stderr.log"
    exit $?
  else
    exit_logs "046_resume_PSK" "skipped"
    exit 125
  fi
exit $?
