#!/bin/sh

# Checking if redirect TLS client connections works properly when the session is resumed.
# Do not redirect TLS client connections on certificate-based authentication success.
# [client_1] -> [server_1]
# Just "1" "accepted: new session negotiated" log for [server_1] service
# is expected, because the client presents the correct certificate.

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

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

  retry = yes

  [client_1]
  client = yes
  exec = ${script_path}/execute_read
  execArgs = execute_read ${result_path}/temp.log
  connect = 127.0.0.1:${https1}
;correct certificate
  cert = ${script_path}/certs/client_cert.pem

  [client_2]
  client = yes
  accept = 127.0.0.1:${http2}
  connect = 127.0.0.1:${https2}

  [server_1]
  accept = 127.0.0.1:${https1}
  exec = ${script_path}/execute
  execArgs = execute 048_resume_noredirect
  redirect = ${http2}
  cert = ${script_path}/certs/server_cert.pem
  verifyPeer = yes
  CAfile = ${script_path}/certs/PeerCerts.pem

  [server_2]
  accept = 127.0.0.1:${https2}
  cert = ${script_path}/certs/server_cert.pem
  exec = ${script_path}/execute
  execArgs = execute 048_resume_noredirect_error
EOT
}

if ! grep -q "FORK" "results.log"
  then
    test_log_for "048_resume_noredirect" "session" "1" "$1" "$2" "$3" 2>> "stderr.log"
    exit $?
  else # the resumption of the session does not work for the FORK model
    exit_logs "048_resume_noredirect" "skipped"
    exit 125
  fi
exit $?
