# this file is a library sourced from recipes/*

result_path=$(pwd)
cd $(dirname "$0")/../
script_path=$(pwd)
cd "${result_path}"

result_logs() {
  # $1 = test name
  # $2 = status: "ok" / "failed" / "configuration failed" / "expected error"
  #              "skipped" / "netcat failed" / "shouldn't work"
  # $3 = file name: "stunnel" / "error"

  if [ "$2" = "expected error" ]
    then # expected error - it's ok
      printf "%-35s\t%s\n" "test $1" "ok"
     else
      printf "%-35s\t%s\n" "test $1" "$2"
     fi
  if [ "$2" != "ok" ]
    then
      printf "%-35s\t%s\n" "test $1" "$2" >> "results.log"
    fi
  if [ "$2" = "failed" ] || [ "$2" = "configuration failed" ] || [ "$2" = "shouldn't work" ]
    then # file with stunnel error logs
      printf "%-35s\t%s\n" "error logs" "logs/$1.log"
      cat "$3.log" > "$1.log"
    else
      cat "temp.log" 2>> "stderr_nc.log" | head -n1 >> "results.log"
    fi
  if [ "$2" = "netcat failed" ]
    then
      printf "\n%s\n" "Netcat failed" >> "stderr_nc.log"
    fi
  return 0
}

exit_logs() {
  # $1 = test name
  # $2 = status

  case "$2" in
    "ok") result_logs "$1" "ok" "UNUSED PATTERN";;
    "failed") result_logs "$1" "failed" "stunnel";;
    "configuration failed") result_logs "$1" "configuration failed" "error";;
    "expected error") result_logs "$1" "expected error" "UNUSED PATTERN";;
    "skipped") result_logs "$1" "skipped" "error";;
    "netcat failed") result_logs "$1" "netcat failed" "stunnel";;
    "shouldn't work") result_logs "$1" "shouldn't work" "stunnel";;
    *) echo "$1 exit_logs error"
  esac
  return 0
}

clean_logs() {
  cat "stderr_nc.log" >> "stderr.log"
  rm -f "stderr_nc.log"
  rm -f "stunnel.log"
  rm -f "temp.log"
  rm -f "error.log"
  rm -f "stunnel.conf"
  rm -f "nodata"
  return 0
}

finding_text() {
  # $1 = to find (yes) or not to find (no)
  # $2 = pattern
  # $3 = file 1
  # $4 = file 2

  local result=0
  if grep -q "$2" "$3" "$4"
    then
      if [ $1 = "yes" ]
        then # to find
          exit_code="ok"
        else # not to find
          exit_code="failed"
          result=1
        fi
    else # no matching
      if [ $1 = "yes" ]
        then # to find
          exit_code="failed"
          result=1
        fi
    fi
  return $result
}

no_file() {
  # $1 = file

  local result=0
  if [ -s "$1" ]
    then
      exit_code="configuration failed"
      result=1
    fi
  return $result
}

waiting_for() {
  # waiting for string $2 to appear in the file $1.log

  mkfifo "fifo" 2>> "stderr_nc.log"
  (cat "$1.log"; tail -f "$1.log") > "fifo" 2>> "stderr_nc.log" &
  pid_tail=$!
  (sleep 3; echo "TIMEOUT") > "fifo" &
  pid_timeout=$!
  grep -q -e "$2" -e "TIMEOUT" "fifo"
  pid_children=$(ps -o pid,ppid | \
    awk -v ppid1="${pid_tail}" -v ppid2="${pid_timeout}" \
      '{if ($2==ppid1 || $2==ppid2) print $1}')
  kill -TERM ${pid_tail} ${pid_timeout} ${pid_children} 2>> "stderr_nc.log"
  wait ${pid_tail} ${pid_timeout} 2>> "stderr_nc.log"
  rm -f "fifo"
  return 0
}

find_free() {
  # finding a free port
  # $1 = mynetstat name: "netstat -a -n" / "ss -a -n -l" / "lsof -i -n -P"

  while $mynetstat $opt_net | grep "$http_find" | grep "LISTEN" >> "stderr_nc.log"
    do
      http_find=$((http_find+1))
    done
  return 0
}

check_ports() {
  # seting the initial ports
  # $1 = test name

  result=0
  printf "\n%s\n" "test $1" >> "stderr_nc.log"
  http_find=8080
  find_free
  http_nc=$http_find
  http_find=4567
  find_free
  https_free=$http_find

  http1=$((http_nc+1))
  http2=$((http_nc+2))
  http3=$((http_nc+3))
  https1=4433
  https2=4434
  https3=4435
  return 0
}

bind_http_ports(){
  grep "Binding service \[client.*to" "error.log" >> "stderr_nc.log"
  grep "Binding service \[client.*failed" "error.log" >> "stderr_nc.log"
  while [ -s "error.log" ] && grep -q "Binding service \[client.*failed" "error.log"
    do
      if [ $http_bind -eq $http1 ]
        then
          http1=$((http1+1))
          http2=$((http2+1))
          http3=$((http3+1))
        elif [ $http_bind -eq $http2 ]
          then
            http2=$((http2+1))
            http3=$((http3+1))
        else
          http3=$((http3+1))
        fi
      http_bind=$((http_bind+1))
      start 2> "error.log"
    grep "Binding service \[client.*to" "error.log" >> "stderr_nc.log"
    grep "Binding service \[client.*failed" "error.log" >> "stderr_nc.log"
    done
  return 0
}

bind_https_ports(){
  grep "Binding service \[server.*to" "error.log" >> "stderr_nc.log"
  grep "Binding service \[server.*failed" "error.log" >> "stderr_nc.log"
  while [ -s "error.log" ] && grep -q "Binding service \[server.*failed" "error.log"
    do
      if [ $https_bind -eq $https1 ]
        then
          https1=$((https1+1))
          https2=$((https2+1))
          https3=$((https3+1))
        elif [ $https_bind -eq $https2 ]
          then
            https2=$((https2+1))
            https3=$((https3+1))
        else
          https3=$((https3+1))
        fi
      https_bind=$((https_bind+1))
      start 2> "error.log"
      grep "Binding service \[server.*to" "error.log" >> "stderr_nc.log"
      grep "Binding service \[server.*failed" "error.log" >> "stderr_nc.log"
    done
  return 0
}

start_stunnel() {
  # running stunnel until to bind free ports
  # $1 = test name

  start 2> "error.log"
  http_bind=$http1
  bind_http_ports
  http_bind=$http2
  bind_http_ports
  http_bind=$http3
  bind_http_ports

  https_bind=$https1
  bind_https_ports
  https_bind=$https2
  bind_https_ports
  https_bind=$https3
  bind_https_ports

  printf "\n%s %s %s %s %s %s %s\n" "test $1 ports: $http_nc $http1 $http2 $http3 $https1 $https2 $https3 $https_free" >> "stderr_nc.log"
  return 0
}

close_process() {
  # $1 = file name
  # $2 = process pid

  wait $2
  local result=$?
  case $result in
    "0") ;; # expected exit status of the stunnel process
    "127") ;; # non-existent stunnel process
    "139") echo "INTERNAL ERROR: $1 exit status $result Segmentation fault (core dumped)" >> "$1.log";;
    *) echo "INTERNAL ERROR: $1 exit status $result" >> "$1.log"
  esac
  return $result
}

killing_stunnel() {
  # $1 = file name

  local result=0
  local pid_stunnel=$(tail "$1.pid")
  if kill -TERM ${pid_stunnel} 2>> "stderr_nc.log"
    then
      close_process $1 ${pid_stunnel}
    else
      exit_code="failed"
      result=1
    fi
  return $result
}

reload_stunnel() {
  # $1 = test name

  local result=0
  printf "\n%s\n" "test $1 - reload stunnel" >> "stderr_nc.log"
  if [ ! -s "error.log" ]
    then
      change_config
      waiting_for "stunnel" "stunnel.pid"
      kill -HUP $(tail "stunnel.pid") 2>> "stderr_nc.log"
      waiting_for "stunnel" "127.0.0.1:$http1"
    else
      printf "\n%s" "$1 error: failed to reload the configuration file" >> "error.log"
      result=1
    fi
  return $result
}

check_listening() {
  # waiting for netcat listening on port
  # $1 = port number

  result=0
  while [ $result -eq 0 ] && ! $mynetstat $opt_net | grep "$1" | grep "LISTEN" >> "stderr_nc.log"
    do
      printf "\n%s\n" "waiting for netcat listening on $1" >> "stderr_nc.log"
      if grep -q -e "failed:" -e "usage" -e "QUITTING" -e "invalid" -e "command not found" "stderr_nc.log"
        then
          result=1
        fi
    done
  return $result
}

connecting_ncat() {
  # $1 = test name
  # $2 = string to send

  local result=0
  mkfifo "nodata" 2>> "stderr_nc.log"
  printf "\n%s\n" "test $1 - netcat connection" >> "stderr_nc.log"
  if [ "$mynetcat" = "nc" ]
    then # nc
      if man "$mynetcat" | grep -q  "error to use this option in conjunction"
        then # BSD nc
              cat "nodata" | $mybuffer $opt_buf $mynetcat -l "$http_nc" -vvv > "temp.log" 2>> "stderr_nc.log" &
        else # traditional nc
              cat "nodata" | $mybuffer $opt_buf $mynetcat -l -p "$http_nc" -s 127.0.0.1 -vvv > "temp.log" 2>> "stderr_nc.log" &
        fi
      pid_nc=$!
      if check_listening "$http_nc"
        then
          printf "%-35s\t%s\n" "test $1" "$2" | $mynetcat 127.0.0.1 "$http1" -vv 1>&2 2>> "stderr_nc.log" &
          pid_nce=$!
          if [ "$2" = "shouldn't work" ]
            then
              waiting_for "stunnel" "Service .* finished"
            else
              waiting_for "temp" "test $1"
            fi
        else # nc failed
          exit_code="netcat failed"
          result=1
        fi
    else # ncat
      cat "nodata" | $mybuffer $opt_buf $mynetcat -l -p $http_nc -vvv > temp.log 2>> stderr_nc.log &
      pid_nc=$!
      if check_listening "$http_nc"
        then
          if ncat --version 2>&1 | grep -q -e 'Version [0-5]\.' -e 'Version [6]\.[0-1]' -e 'Version [6]\.[2][0-4]'
            then # ncat version < 6.25
              printf "%-35s\t%s\n" "test $1" "$2" | "$mynetcat" 127.0.0.1 "$http1" -vv  1>&2 2>> "stderr_nc.log" &
            else # ncat version >= 6.25
              printf "%-35s\t%s\n" "test $1" "$2" | "$mynetcat" 127.0.0.1 "$http1" -vv  1>&2 2>> "stderr_nc.log"
            fi
          pid_nce=$!
          if [ "$2" = "shouldn't work" ]
            then
              waiting_for "stunnel" "Service .* finished"
            else
              waiting_for "temp" "test $1"
            fi
        else # ncat failed
          exit_code="netcat failed"
          result=1
        fi
    fi
  kill -TERM ${pid_nc} ${pid_nce} 2>> "stderr_nc.log"
  echo "somedata" > "nodata" 2>> "stderr_nc.log"
  rm -f "nodata"
  return $result
}

sending_ncat() {
  # starting netcat for execute tests
  # $1 = test name

  mkfifo "nodata" 2>> "stderr_nc.log"
  cat "nodata" | $mybuffer $opt_buf $mynetcat 127.0.0.1 "$http1" -vv >"temp.log" 2>> "stderr_nc.log" &
  pid_nce=$(pgrep -P $!)
  waiting_for "temp" "test $1"
  kill -TERM ${pid_nce} 2>> "stderr_nc.log"
  echo "somedata" > "nodata" 2>> "stderr_nc.log"
  rm -f "nodata"
  return 0
}

expected_success() {
  # expects to send the message using stunnel
  # $1 = test name

  local result=0
  if [ "$1" != "040_reload" ]
    then
      check_ports "$1"
      start_stunnel "$1"
    fi
  if no_file "error.log"
    then
      if connecting_ncat "$1" "success"
        then
          finding_text "yes" "test $1.*success" "temp.log" "UNUSED PATTERN"
          result=$?
        else # ncat (nc) failed
          result=1
        fi
      if ! killing_stunnel stunnel
        then
          result=1
        fi
    else # configuration failed
      result=1
    fi
  if ! finding_text "no" "INTERNAL ERROR" "stunnel.log" "error.log"
    then
      result=1
    fi
  exit_logs "$1" "$exit_code"
  return $result
}

expected_failure() {
  # $1 = test name

  local result=0
  check_ports "$1"
  start_stunnel "$1"
  if no_file "error.log"
    then
      if connecting_ncat "$1" "shouldn't work"
        then
          if ! finding_text "no" "test $1.*shouldn't work" "temp.log" "UNUSED PATTERN"
            then # ops...stunnel works
              exit_code="shouldn't work"
              result=1
            else
              exit_code="expected error"
            fi
        else # ncat (nc) failed
          result=1
        fi
      if ! killing_stunnel stunnel
        then
          result=1
        fi
    else # configuration failed, but it is ok
      exit_code="expected error"
    fi
  if ! finding_text "no" "INTERNAL ERROR" "stunnel.log" "error.log"
    then
      result=1
    fi
  exit_logs "$1" "$exit_code"
  return $result
}

execute_program() {
  # $1 = test name

  local result=0
  check_ports "$1"
  start_stunnel "$1"
  if no_file "error.log"
    then
      sending_ncat "$1"
      if ! killing_stunnel stunnel
        then
          result=1
        fi
      if [ $result -eq 0 ]
        then
          if finding_text "yes" "test $1.*success" "temp.log" "UNUSED PATTERN"
            then
              finding_text "no" "$1_error" "temp.log" "UNUSED PATTERN"
              result=$?
            else
              result=1
            fi
        fi
    else # configuration failed
      result=1
    fi
  if ! finding_text "no" "INTERNAL ERROR" "stunnel.log" "error.log"
    then
      result=1
    fi
  exit_logs "$1" "$exit_code"
  return $result
}

execute_connect() {
  # $1 = test name

  local result=0
  check_ports "$1"
  start_stunnel "$1"
  if [ "$1" = "042_inetd" ]
    then # inetd test
      mkfifo "nodata" 2>> "stderr_nc.log"
      start_inetd > "temp.log" 2>> "error.log" &
      pid_inetd=$!
    fi
  if no_file "error.log"
    then
      waiting_for "stunnel" "Service .* finished"
      if [ $1 = "042_inetd" ]
        then # inetd test
          close_process stunnel_inetd ${pid_inetd}
        fi
      finding_text "yes" "test $1.*success" "temp.log" "UNUSED PATTERN"
      result=$?
      if [ "$1" = "042_inetd" ]
        then # inetd test
          printf "%s\n" "*** inetd mode ***" >> "stunnel.log"
          cat "stunnel_inetd.log" >> "stunnel.log"
        fi
      if ! killing_stunnel stunnel
        then
          result=1
        fi
    else # configuration failed
      result=1
    fi
  if ! finding_text "no" "INTERNAL ERROR" "stunnel.log" "error.log"
    then
      result=1
    fi
  rm -f "stunnel_inetd.log"
  exit_logs "$1" "$exit_code"
  return $result
}

loop_prio() {
  # $1 = test name

  local result=0
  local i=1
  local max=12
  if [ $1 = "037_failover_prio1" ]
    then
      local serv="server_2\] accepted connection"
    else
      local serv="server_1\] accepted connection"
  fi
  check_ports "$1"
  start_stunnel "$1"
  if no_file "error.log"
    then
      waiting_for "stunnel" "Created pid file"
      mv "stunnel.log" "stunnel_0.log"
      kill -USR1 $(tail "stunnel.pid") 2>> "stderr_nc.log"
      while [ $i -le $max ] && [ $result -eq 0 ]
        do
          if connecting_ncat "$1" "success"
            then
              finding_text "yes" "test $1.*success" "temp.log" "UNUSED PATTERN"
              result=$?
              if [ $result -eq 0 ] && ! finding_text "no" "$serv" "stunnel.log" "UNUSED PATTERN"
                then # error - second server accepts a client
		  result=1
                fi
            else # ncat (nc) failed
              result=1
            fi
          mv "stunnel.log" "stunnel_$i.log"
          kill -USR1 $(tail "stunnel.pid") 2>> "stderr_nc.log"
          i=$((i + 1))
        done
      cat "stunnel_0.log" > "stunnel_all.log"
      rm -f "stunnel_0.log"
      local j=1
      while [ $j -lt $i ]
        do
          printf "%s\n" "*** connection $j ***" >> "stunnel_all.log"
          cat "stunnel_$j.log" >> "stunnel_all.log"
          rm -f "stunnel_$j.log"
          j=$((j + 1))
        done
      if ! killing_stunnel stunnel
        then
          result=1
        fi
      cat "stunnel.log" >> "stunnel_all.log"
      cat "stunnel_all.log" > "stunnel.log"
      rm -f "stunnel_all.log"
    else # configuration failed
      result=1
    fi
  if ! finding_text "no" "INTERNAL ERROR" "stunnel.log" "error.log"
    then
      result=1
    fi
  exit_logs "$1" "$exit_code"
  return $result
}

loop_rr() {
  # $1 = test name

  local result=0
  local i=1
  local max=3
  local first=0
  local second=0
  local third=0
  check_ports "$1"
  start_stunnel "$1"
  if no_file "error.log"
    then
      waiting_for "stunnel" "Created pid file"
      mv "stunnel.log" "stunnel_0.log"
      kill -USR1 $(tail "stunnel.pid") 2>> "stderr_nc.log"
      while [ $i -le $max ] && [ $result -eq 0 ]
        do
          if connecting_ncat "$1" "success"
            then
              finding_text "yes" "test $1.*success" "temp.log" "UNUSED PATTERN"
              result=$?
            else # ncat (nc) failed
              result=1
            fi
          mv "stunnel.log" "stunnel_$i.log"
          kill -USR1 $(tail "stunnel.pid") 2>> "stderr_nc.log"
          i=$((i + 1))
        done
      cat "stunnel_0.log" > "stunnel_all.log"
      rm -f "stunnel_0.log"
      j=1
      while [ $j -lt $i ]
        do
          printf "%s\n" "*** connection $j ***" >> "stunnel_all.log"
          cat "stunnel_$j.log" >> "stunnel_all.log"
          rm -f "stunnel_$j.log"
          j=$((j + 1))
        done
      if ! killing_stunnel stunnel
        then
          result=1
        fi
      cat "stunnel.log" >> "stunnel_all.log"
      cat "stunnel_all.log" > "stunnel.log"
      rm -f "stunnel_all.log"
      if [ $result -eq 0 ]
        then
          first=$(grep -c "server_1\] accepted connection" "stunnel.log")
          second=$(grep -c "server_2\] accepted connection" "stunnel.log")
          third=$(grep -c "server_3\] accepted connection" "stunnel.log")
          product=$((first * second * third))
          if [ $product -ne 0 ]
            then # round robin
              printf "%-35s\t%s\n" "test $1: $first x $second x $third" "success" > "temp.log"
            else
              printf "%-35s\t%s\n" "test $1: $first x $second x $third" "failed" > "temp.log"
              exit_code="failed"
              result=1
            fi
        fi
    else # configuration failed
      result=1
    fi
  if ! finding_text "no" "INTERNAL ERROR" "stunnel.log" "error.log"
    then
      result=1
    fi
  exit_logs "$1" "$exit_code"
  return $result
}

loop_session() {
  # $1 = test name
  # $2 = number of connections

  local result=0
  local i=0
  local j=0
  local max=$((2*$2))
  check_ports "$1"
  start_stunnel "$1"
  if no_file "error.log"
    then
      waiting_for "stunnel" "Created pid file"
      while [ $i -le $max ]
        do
          i=$(grep -c "Retrying an exec+connect section" "stunnel.log")
        done
      if ! killing_stunnel stunnel
        then
          result=1
        fi
      if [ $result -eq 0 ]
        then
          finding_text "yes" "test $1.*success" "temp.log" "UNUSED PATTERN"
          result=$?
        fi
      j=$(grep -c "accepted: new session negotiated" "stunnel.log")
      if [ $result -eq 0 ] && [ $j -ne $2 ]
        then
          exit_code="failed"
          result=1
        fi
    else # configuration failed
      result=1
    fi
  if ! finding_text "no" "INTERNAL ERROR" "stunnel.log" "error.log"
    then
      result=1
    fi
  exit_logs "$1" "$exit_code"
  return $result
}

two_instances() {
  # $1 = test name
  # $2 = number of new connections

  local result=0
  local i=0
  local j=0
  start_server 2> "error.log"
  if no_file "error.log"
    then
      waiting_for "stunnel_server" "Created pid file"
      start_stunnel "$1"
      if no_file "error.log"
        then
          while [ $i -le 2 ]
            do
              i=$(grep -c "Retrying an exec+connect section" "stunnel.log")
            done
          if ! killing_stunnel stunnel_server
            then
              result=1
            fi
          mv "stunnel_server.log" "stunnel_all.log"
          start_server 2>> "error.log"
          waiting_for "stunnel_server" "Service .* finished"
          if ! killing_stunnel stunnel_server
            then
              result=1
            fi
          cat "stunnel_server.log" >> "stunnel_all.log"
          if ! killing_stunnel stunnel
            then
              result=1
            fi
          cat "stunnel.log" >> "stunnel_all.log"
          cat "stunnel_all.log" > "stunnel.log"
          rm -f "stunnel_all.log"
          if [ $result -eq 0 ]
            then
              finding_text "yes" "test $1.*success" "temp.log" "UNUSED PATTERN"
              result=$?
            fi
          j=$(grep -c "accepted: new session negotiated" "stunnel.log")
          if [ $result -eq 0 ] && [ $j -ne $2 ]
            then
              exit_code="failed"
              result=1
            fi
        else # client configuration failed
          killing_stunnel stunnel_server
          exit_code="configuration failed"
          result=1
        fi
    else # server configuration failed
      cat "stunnel_server.log" >> "stunnel.log"
      result=1
    fi
  if ! finding_text "no" "INTERNAL ERROR" "stunnel.log" "error.log"
    then
      result=1
    fi
  rm -f "stunnel_server.log"
  exit_logs "$1" "$exit_code"
  return $result
}

resumption() {
  # $1 = test name
  # $2 = number of new connections

  local result=0
  local i=0
  local j=0
  check_ports "$1"
  start_stunnel "$1"
  if no_file "error.log"
    then
      waiting_for "stunnel" "Service .* finished"
      connecting_ncat "$1" "success"
      while [ $i -le $2 ]
        do
          i=$(grep -c "Retrying an exec+connect section" "stunnel.log")
        done
      if ! killing_stunnel stunnel
        then
          result=1
        fi
      if [ $result -eq 0 ]
        then
          finding_text "yes" "test $1.*success" "temp.log" "UNUSED PATTERN"
          result=$?
        fi
      j=$(grep -c "accepted: new session negotiated" "stunnel.log")
      if [ $result -eq 0 ] && [ $j -ne $2 ]
        then
          exit_code="failed"
          result=1
        fi
    else # configuration failed
      result=1
    fi
  if ! finding_text "no" "INTERNAL ERROR" "stunnel.log" "error.log"
    then
      result=1
    fi
  exit_logs "$1" "$exit_code"
  return $result
}

get_http() {
  # $1 = test name

  local result=0
  cat ${script_path}/http_request | start_inetd > "http.log" 2>> "error.log" &
  waiting_for "stunnel" "Service .* finished"
  if [ -s "http.log" ]
    then # file exists and is not empty
      printf "%-35s\t%s\n" "test $1" "success" > "temp.log"
      exit_code="ok"
    else # file does not exist, or is empty
      exit_code="failed"
      result=1
    fi
  rm -f "http.log"
  exit_logs "$1" "$exit_code"
  return $result
}

myglobal() {
  # $1 = mynetcat name: "ncat" / "nc"
  # $2 = mynetstat name: "netstat" / "ss" / "lsof"
  # $3 = mybuffer name: "stdbuf" / "unbuffer" / ""

  mynetcat="$1"
  mynetstat="$2"
  mybuffer="$3"

  case "$mynetstat" in
    "/mnt/c/Windows/System32/netstat.exe") opt_net="-a -n";;
    "netstat") opt_net="-a -n";;
    "ss") opt_net="-a -n -l";;
    "lsof") opt_net="-i -n -P";;
  esac

  case "$mybuffer" in
    "stdbuf") opt_buf="-o0";;
  esac
  return 0
}

test_log_for() {
  # $1 = test name
  # $2 = function name
  # $3 = number of connections for loop_session
  # $4 = mynetcat name: "ncat" / "nc"
  # $5 = mynetstat name: "netstat" / "ss" / "lsof"
  # $6 = mybuffer name: "stdbuf" / "unbuffer" / ""

  myglobal "$4" "$5" "$6"
  case "$2" in
    "success") expected_success "$1";;
    "failure") expected_failure "$1";;
    "execute") execute_program "$1";;
    "exe_con") execute_connect "$1";;
    "prio") loop_prio "$1";;
    "rr") loop_rr "$1";;
    "session") loop_session "$1" "$3";;
    "instances") two_instances "$1" "$3";;
    "resumption") resumption "$1" "$3";;
    "get_http") get_http "$1";;
  esac
  result=$?
  clean_logs
  return $result
}
