!******************************************************************************!
! Tests retrieval of client alias/DN on WindowsCAPI (beta version)
!
! @bug      NXM-3813 SSLUtilities support for validating client certificate
! @author:  Jeanette McCall
! @since    NeXtMidas 4.1.1
!******************************************************************************!
startmacro cs:truststore cs:passwd
  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)

  say "truststore path is: ^truststore"
  say "password is: ^passwd"

  invoke dnProp nxm.sys.lib.SSLUtilities.getCertificateProperty("DN")

  invoke results nxm.sys.lib.SSLUtilities.validateLocalCerts("^truststore", passwd, dnProp, true)
  res results

endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
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
    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