#!/bin/sh

# Checking if the stunnel works correctly after reloading the configuration.
# kill -HUP PID is called in reload_stunnel().

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

set_config() {
#  client doesn't present any certificate
  echo "
  debug = debug
  syslog = no
  pid = ${result_path}/stunnel.pid
  output = ${result_path}/stunnel.log

  [client_1]
  client = yes
  accept = 127.0.0.1:${http1}
  connect = 127.0.0.1:${https1}

  [server_1]
  accept = 127.0.0.1:${https1}
  connect = 127.0.0.1:${http_nc}
  verifyPeer = yes
  CAfile = ${script_path}/certs/PeerCerts.pem
  cert = ${script_path}/certs/server_cert.pem" > "stunnel.conf"
}

change_config() {
#  client presents the valid certificate
  echo "
  debug = debug
  syslog = no
  pid = ${result_path}/stunnel.pid
  output = ${result_path}/stunnel.log

  [client_2]
  client = yes
  accept = 127.0.0.1:${http1}
  connect = 127.0.0.1:${https1}
  cert = ${script_path}/certs/client_cert.pem

  [server_2]
  accept = 127.0.0.1:${https1}
  connect = 127.0.0.1:${http_nc}
  verifyPeer = yes
  CAfile = ${script_path}/certs/PeerCerts.pem
  cert = ${script_path}/certs/server_cert.pem" > "stunnel.conf"
}

start() {
  set_config
  ../../src/stunnel stunnel.conf
}

myglobal "$1" "$2" "$3"
check_ports "040_reload"
start_stunnel "040_reload" 2> "error.log"
reload_stunnel "040_reload" 2>> "error.log"
test_log_for "040_reload" "success" "0" "$1" "$2" "$3" 2>> "stderr.log"
exit $?
