2222package com .wolfssl .provider .jsse .test ;
2323
2424import static org .junit .Assert .assertNotNull ;
25+ import static org .junit .Assert .assertEquals ;
26+ import static org .junit .Assert .assertTrue ;
27+ import static org .junit .Assert .assertFalse ;
2528import static org .junit .Assert .fail ;
2629
2730import java .io .FileInputStream ;
3841import java .security .NoSuchAlgorithmException ;
3942import java .security .NoSuchProviderException ;
4043import java .security .UnrecoverableKeyException ;
44+ import java .security .Principal ;
45+ import javax .security .auth .Subject ;
4146import java .security .cert .CertificateEncodingException ;
4247import java .security .cert .CertificateException ;
4348import java .security .cert .CertificateExpiredException ;
5459import javax .net .ssl .SSLPeerUnverifiedException ;
5560
5661import org .junit .BeforeClass ;
62+ import org .junit .Before ;
5763import org .junit .Test ;
5864
5965import com .wolfssl .WolfSSL ;
@@ -67,6 +73,11 @@ public class WolfSSLX509Test {
6773 private String provider = "wolfJSSE" ;
6874 private javax .security .cert .X509Certificate [] certs ;
6975
76+ /* Common test setup fields */
77+ private WolfSSLX509 x509 ;
78+ private Principal issuerDN ;
79+ private Principal subjectDN ;
80+
7081 @ BeforeClass
7182 public static void testProviderInstallationAtRuntime ()
7283 throws NoSuchProviderException {
@@ -88,6 +99,13 @@ public static void testProviderInstallationAtRuntime()
8899 }
89100 }
90101
102+ @ Before
103+ public void setupCommonX509Objects () throws Exception {
104+ byte [] der = tf .getCert ("server" );
105+ x509 = new WolfSSLX509 (der );
106+ issuerDN = x509 .getIssuerDN ();
107+ subjectDN = x509 .getSubjectDN ();
108+ }
91109
92110 @ Test
93111 public void testServerParsing () {
@@ -389,25 +407,25 @@ public void testVerifyProvider() {
389407 server .verify (ca .getPublicKey (), sigProvider .getName ());
390408 } catch (InvalidKeyException | SignatureException |
391409 NoSuchProviderException e ) {
392- error ("\t ... failed" );
410+ error ("\t \t \t ... failed" );
393411 fail ("failed to verify certificate" );
394412 }
395413
396414 try {
397415 server .verify (server .getPublicKey (), sigProvider );
398- error ("\t ... failed" );
416+ error ("\t \t \t ... failed" );
399417 fail ("able to verify when should not have been" );
400418 } catch (InvalidKeyException | SignatureException e ) {
401419 /* expected fail case */
402420 }
403421
404422 } catch (KeyStoreException | NoSuchAlgorithmException |
405423 CertificateException | IOException | WolfSSLException e ) {
406- error ("\t ... failed" );
424+ error ("\t \t \t ... failed" );
407425 e .printStackTrace ();
408426 fail ("general failure" );
409427 }
410- pass ("\t ... passed" );
428+ pass ("\t \t \t ... passed" );
411429 }
412430
413431
@@ -671,6 +689,189 @@ public void testSubjectAlternativeNames() {
671689 pass ("\t ... passed" );
672690 }
673691
692+ @ Test
693+ public void testWolfSSLPrincipalToString () {
694+ System .out .print ("\t WolfSSLPrincipal toString" );
695+
696+ try {
697+ assertNotNull ("issuerDN should not be null" , issuerDN );
698+ assertNotNull ("subjectDN should not be null" , subjectDN );
699+
700+ /* Test that toString() returns a non-null, non-empty string */
701+ String issuerString = issuerDN .toString ();
702+ String subjectString = subjectDN .toString ();
703+
704+ assertNotNull ("issuerDN.toString() should not be null" ,
705+ issuerString );
706+ assertNotNull ("subjectDN.toString() should not be null" ,
707+ subjectString );
708+ assertFalse ("issuerDN.toString() should not be empty" ,
709+ issuerString .isEmpty ());
710+ assertFalse ("subjectDN.toString() should not be empty" ,
711+ subjectString .isEmpty ());
712+
713+ /* Test that toString() returns the same value as getName() */
714+ assertEquals ("issuerDN.toString() should equal getName()" ,
715+ issuerDN .getName (), issuerString );
716+ assertEquals ("subjectDN.toString() should equal getName()" ,
717+ subjectDN .getName (), subjectString );
718+
719+ } catch (Exception e ) {
720+ error ("\t ... failed" );
721+ fail ("Exception during WolfSSLPrincipal toString test: " +
722+ e .getMessage ());
723+ }
724+
725+ pass ("\t ... passed" );
726+ }
727+
728+ @ Test
729+ public void testWolfSSLPrincipalGetName () {
730+ System .out .print ("\t WolfSSLPrincipal getName" );
731+
732+ try {
733+ assertNotNull ("issuerDN should not be null" , issuerDN );
734+ assertNotNull ("subjectDN should not be null" , subjectDN );
735+
736+ /* Test that getName() returns formatted DN strings */
737+ String issuerName = issuerDN .getName ();
738+ String subjectName = subjectDN .getName ();
739+
740+ assertNotNull ("issuerDN.getName() should not be null" ,
741+ issuerName );
742+ assertNotNull ("subjectDN.getName() should not be null" ,
743+ subjectName );
744+ assertFalse ("issuerDN.getName() should not be empty" ,
745+ issuerName .isEmpty ());
746+ assertFalse ("subjectDN.getName() should not be empty" ,
747+ subjectName .isEmpty ());
748+
749+ /* Test that names contain expected DN components */
750+ /* Names should be in "DN=value, DN=value" format,
751+ * not "/DN=value" */
752+ assertTrue ("issuer name should not start with /" ,
753+ !issuerName .startsWith ("/" ));
754+ assertTrue ("subject name should not start with /" ,
755+ !subjectName .startsWith ("/" ));
756+
757+ } catch (Exception e ) {
758+ error ("\t ... failed" );
759+ fail ("Exception during WolfSSLPrincipal getName test: " +
760+ e .getMessage ());
761+ }
762+
763+ pass ("\t ... passed" );
764+ }
765+
766+ @ Test
767+ public void testWolfSSLPrincipalInterfaceImplementation () {
768+ System .out .print ("\t WolfSSLPrincipal interface" );
769+
770+ try {
771+ assertNotNull ("issuerDN should not be null" , issuerDN );
772+ assertNotNull ("subjectDN should not be null" , subjectDN );
773+
774+ /* Test that objects implement Principal interface */
775+ assertTrue ("issuerDN should implement Principal" ,
776+ issuerDN instanceof Principal );
777+ assertTrue ("subjectDN should implement Principal" ,
778+ subjectDN instanceof Principal );
779+
780+ /* Test equals() behavior -
781+ * same principal should be equal to itself */
782+ assertTrue ("issuerDN should equal itself" ,
783+ issuerDN .equals (issuerDN ));
784+ assertTrue ("subjectDN should equal itself" ,
785+ subjectDN .equals (subjectDN ));
786+
787+ /* Test hashCode() consistency */
788+ int issuerHash1 = issuerDN .hashCode ();
789+ int issuerHash2 = issuerDN .hashCode ();
790+ int subjectHash1 = subjectDN .hashCode ();
791+ int subjectHash2 = subjectDN .hashCode ();
792+
793+ assertEquals ("issuerDN hashCode should be consistent" ,
794+ issuerHash1 , issuerHash2 );
795+ assertEquals ("subjectDN hashCode should be consistent" ,
796+ subjectHash1 , subjectHash2 );
797+
798+ } catch (Exception e ) {
799+ error ("\t ... failed" );
800+ fail ("Exception during WolfSSLPrincipal interface test: " +
801+ e .getMessage ());
802+ }
803+
804+ pass ("\t ... passed" );
805+ }
806+
807+ @ Test
808+ public void testWolfSSLPrincipalImplies () {
809+ Subject emptySubject , subjectWithPrincipals ;
810+
811+ System .out .print ("\t WolfSSLPrincipal implies" );
812+
813+ try {
814+ assertNotNull ("issuerDN should not be null" , issuerDN );
815+ assertNotNull ("subjectDN should not be null" , subjectDN );
816+
817+ /* Test implies() with null Subject */
818+ assertTrue ("implies(null) should return false" ,
819+ !issuerDN .implies (null ));
820+ assertTrue ("implies(null) should return false" ,
821+ !subjectDN .implies (null ));
822+
823+ /* Test implies() with empty Subject */
824+ emptySubject = new Subject ();
825+ assertTrue ("implies(empty Subject) should return false" ,
826+ !issuerDN .implies (emptySubject ));
827+ assertTrue ("implies(empty Subject) should return false" ,
828+ !subjectDN .implies (emptySubject ));
829+
830+ /* Test implies() with Subject containing different principals */
831+ subjectWithPrincipals = new Subject ();
832+ subjectWithPrincipals .getPrincipals ().add (issuerDN );
833+ subjectWithPrincipals .getPrincipals ().add (subjectDN );
834+
835+ /* Default implementation should return true when Subject
836+ * contains the same principal (equals() returns true) */
837+ assertTrue ("implies() should return true when Subject " +
838+ "contains this principal" ,
839+ issuerDN .implies (subjectWithPrincipals ));
840+ assertTrue ("implies() should return true when Subject " +
841+ "contains this principal" ,
842+ subjectDN .implies (subjectWithPrincipals ));
843+
844+ /* Test implies() with Subject containing only one principal */
845+ Subject subjectWithIssuerOnly = new Subject ();
846+ subjectWithIssuerOnly .getPrincipals ().add (issuerDN );
847+
848+ assertTrue ("issuerDN.implies() should return true when Subject " +
849+ "contains issuerDN" ,
850+ issuerDN .implies (subjectWithIssuerOnly ));
851+ assertTrue ("subjectDN.implies() should return false when " +
852+ "Subject doesn't contain subjectDN" ,
853+ !subjectDN .implies (subjectWithIssuerOnly ));
854+
855+ /* Test implies() with Subject containing only other principal */
856+ Subject subjectWithSubjectOnly = new Subject ();
857+ subjectWithSubjectOnly .getPrincipals ().add (subjectDN );
858+
859+ assertTrue ("subjectDN.implies() should return true when " +
860+ "Subject contains subjectDN" ,
861+ subjectDN .implies (subjectWithSubjectOnly ));
862+ assertTrue ("issuerDN.implies() should return false when " +
863+ "Subject doesn't contain issuerDN" ,
864+ !issuerDN .implies (subjectWithSubjectOnly ));
865+
866+ } catch (Exception e ) {
867+ error ("\t ... failed" );
868+ fail ("Exception during WolfSSLPrincipal implies test: " +
869+ e .getMessage ());
870+ }
871+
872+ pass ("\t ... passed" );
873+ }
874+
674875 private void pass (String msg ) {
675876 WolfSSLTestFactory .pass (msg );
676877 }
0 commit comments