!******************************************************************************!
! Tests SSLUtilities.initSSLContext and SSLUtilities.initAnyHostNameSSL
! arguments are passed on via SSLCONFIGURE
!
! @bug      NXM-3641 SSLUtilities.initSSLContext setting values to null
! @author:  Jeanette McCall
! @since    NeXtMidas 4.1.3
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct    GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline    GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)
  switch ANYHOSTNAME anyhostname GET DEF=FALSE SDEF=TRUE  ! run regular ssl, except do not do host name verification

  res homedir AUX.HOME
  res keystorefile     "^{homedir}temp_keystorefile.txt"
  res keystorepwdfile  "^{homedir}temp_keystorepwdfile.txt"
  res logfile          "^{homedir}temp_logfile_ssl_args.mlog"
  !res truststorefile   ram:truststorefile.txt
  env get IOOptions origIOOptions
  env set IOOptions +ClassBasedLoggerShouldUseMidasLogger
  res expectedException "java.io.IOException: Invalid keystore format"

  env set IOOptions +SameTimelineKeywords
  invoke levelCurrent nxm.sys.lib.SSLUtilities.getCertInfoLogLevel()
  invoke levelConfig  java.util.logging.Level.INFO
  invoke ,, nxm.sys.lib.SSLUtilities.setCertInfoLogLevel(levelConfig)
  file copy/f nxm.sys.test.text_file.txt "^keystorefile"
  file copy/f nxm.sys.test.text_file.txt "^keystorepwdfile"
  invoke midasContext reg.main.getMidas()
  new nxm.sys.lib.Logger(midasContext) logger
  res z:myFalse false
  invoke logger.open(logfile,XZ,myFalse)
  invoke oldlogger midasContext.getLogger()
  invoke ,, midasContext.setLogger(logger)

  sslconfigure/ANYHOSTNAME=^anyhostname PRIVKEYFILE=keystorefile PASSWORDFILE=keystorepwdfile

  invoke logger.close()
  ! If oldlogger is null, call clearLogger since oldlogger is a String containing "NULL"
  if (oldlogger ISNULL) then
    invoke ,, midasContext.clearLogger()
  else
    invoke ,, midasContext.setLogger(oldlogger)
  endif
  file open/d tag logfile
  result t:table tag.getDataTable(0)
  file close tag
  res msgXZ table.text
  res msg msgXZ.toString()
  invoke fileInfoStart msg.indexOf("keyStoreFile")
  invoke fileInfoEnd msg.indexOf("capiEnabled")
  invoke fileInfo msg.substring(fileInfoStart,fileInfoEnd)
  !res fileInfo
  invoke nullFound fileInfo.contains("null")
  res methodname "initSSLContext"
  if anyhostname ISTRUE then
    res methodname "initAnyHostNameSSL"
  endif
  ! NOTE: The original Midas logger must be restored before doing any asserts
  assert/text="^reg.main.name: ANYHOSTNAME=^{anyhostname} SSLUtilities.^{methodname}" nullFound ISFALSE
  env set IOOptions origIOOptions

  ! Cleanup
  erase keystorefile keystorepwdfile logfile
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  invoke cause emsg.data.getCause()
  ! Interested in root cause of Exception (may be wrapped)
  if cause ISNULL then
    res causeStr emsg.data.toString()
  else
    res causeStr cause.toString()
  endif
  ! Check if Exception is expected at this point in the test
  if expectedException REXISTS then
    ! If so, make sure it is the correct one
    assert/text="^reg.main.name: Expected ERROR is: ^{expectedException}" causeStr EQSS ^expectedException
    if "^expectedException" eqs "java.lang.IllegalStateException: Initialization already complete" then
      res emsg.data
    elseif "SSL init failed" NSUBS "^{emsg.data}" AND causeStr NEQSS ^expectedException then
      res emsg.data
    endif
    remove expectedException
  else
    ! Check for a common failure scenario (usually caused by a cert not signed by a trusted authority)
    ! (give a more detailed message and skip remaining authorization check tests we get this failure)
    if causeStr EQSS "java.net.SocketException: Broken pipe" then
      assert/text="^reg.main.name: Exception is probably due to a untrusted certificate: ^{emsg.data}" "FALSE" isTrue
      res connectingWithCertFailed true
    else 
      assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
      if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
    endif
  endif
return
