!******************************************************************************!
! This test is to be run by Jenkins only. This tests two way authentication over https
! It will start up a server running client authentication over https.
! This test should be run as jenkins-slave user.
! This requires:
! 1) A remote server to run nextmidas on
! 2) p12 cert that is signed by the same rootCA on both the local and remote sever
! 3) SSLUtilities.properties with the cert signed by the rootCA and
!    the keystore with the rootCA added to it.
! 4) if testing on a remote server, please make sure the firewall opens up port 2222 
!    (at least to the remote server that will try to connect)
!
! @bug      NXM-3313, NXM-3322
! @author:  Kevin Esterheld
! @since    NeXtMidas 3.9.0
!******************************************************************************!
startmacro
  switch DEBUG         debug    GET DEF=false    SDEF=true
  switch DEBUGCLIENT   debugc   GET DEF=^debug   SDEF=true
  switch VERBOSE       verbose  GET DEF=false    SDEF=true
  switch VERBOSECLIENT verbosec GET DEF=^verbose SDEF=true
  switch INSECURE      insecure GET DEF=false    SDEF=true
  switch TESTNSS       testnss  GET DEF=false    SDEF=true
  switch VMPASS        vmpass   GET DEF=""

  say "************************* ^{reg.main.name}: Running with insecure mode - ^{insecure} ************************"
  say "************************* ^{reg.main.name}: Running with nss           - ^{testnss} ************************"

  env get HOME home
  env get HOSTNAME hostname

  invoke javalocation nxm.sys.lib.Shell.getJavaHome()

  ! make sure there is at least one file in the write directory
  set testfile "test_files_junk1.tmp"
  waveform testfile

  ! Check if testing with nss
  if testnss then
    res propsFile "SSLUtilitiesNSS.properties"
  else
    res propsFile "SSLUtilitiesHTTPSERV.properties"
  endif

  ! This will load ssl
  if debug then
    say "^{reg.main.name}: Running SSLCONFIGURE /INSECURE=^{insecure} PROPSFILE=^{propsFile}...\n"
  endif
  SSLCONFIGURE /INSECURE=^{insecure} PROPSFILE=^{propsFile}

  ! Serve up the write directory
  fname datdir aux.write opt=aux

  ! Start the server with one way auth, i.e. we do not authenticate clients
  ! or two way auth, i.e. authenticate clients
  if debug then
    say "^{reg.main.name}: Running HTTPSERV/id=hs/https/oneway=^{insecure}/bg port=2222 dir=datdir name=mydir/...\n"
  endif

  HTTPSERV/id=hs/https/oneway=^{insecure}/bg port=2222 dir=datdir name=mydir/

  invoke javaMajVer   nxm.sys.lib.Shell.getJavaMajorVersion()
  if javaMajVer < 11 then
    if debug then
      say "^{reg.main.name}: Running $sshpass -p ^{vmpass} ssh canextmidas ^{javalocation} -cp "^{home}nxmcerts/httpservcerts/jars/nxm-sys.jar" nxm.sys.lib.NeXtMidas -aux.homepath="classpath:/nxm/sys/test/" -path=SYS -cmd="%TEST_HTTPSERV_NSS_INTEGRATION_CLIENT/debug=^{debugc}/verbose=^{verbosec}/remotehost=^{hostname}/testnss=^{testnss}"...\n"
    endif
    $sshpass -p ^{vmpass} ssh canextmidas ^{javalocation} -cp "^{home}nxmcerts/httpservcerts/jars/nxm-sys.jar" nxm.sys.lib.NeXtMidas -aux.homepath="classpath:/nxm/sys/test/" -path=SYS -cmd="%TEST_HTTPSERV_NSS_INTEGRATION_CLIENT/debug=^{debugc}/verbose=^{verbosec}/remotehost=^{hostname}/testnss=^{testnss}"
  else
    if debug then
      say "^{reg.main.name}: Running $sshpass -p ^{vmpass} ssh canextmidas ^{javalocation} --add-opens java.logging/java.util.logging=ALL-UNNAMED --add-opens java.base/java.security=ALL-UNNAMED -cp "^{home}nxmcerts/httpservcerts/jars/nxm-sys.jar" nxm.sys.lib.NeXtMidas -aux.homepath="classpath:/nxm/sys/test/" -path=SYS -cmd="%TEST_HTTPSERV_NSS_INTEGRATION_CLIENT/debug=^{debugc}/verbose=^{verbosec}/remotehost=^{hostname}/testnss=^{testnss}"...\n"
    endif
    $sshpass -p ^{vmpass} ssh canextmidas ^{javalocation} --add-opens java.logging/java.util.logging=ALL-UNNAMED --add-opens java.base/java.security=ALL-UNNAMED -cp "^{home}nxmcerts/httpservcerts/jars/nxm-sys.jar" nxm.sys.lib.NeXtMidas -aux.homepath="classpath:/nxm/sys/test/" -path=SYS -cmd="%TEST_HTTPSERV_NSS_INTEGRATION_CLIENT/debug=^{debugc}/verbose=^{verbosec}/remotehost=^{hostname}/testnss=^{testnss}"
  endif
  invoke ,, reg.hs.close()

  say "****************************** End of ^{reg.main.name} ***************************\n"
  erase testfile
endmacro