Skip to content

Commit a29da7f

Browse files
authored
MOSIP-30573: test case coverage for zkservice (#483)
* MOSIP-30573: test case coverage for KeyMigrtor Signed-off-by: nagendra0721 <[email protected]> * MOSIP-30573: test coverage for zkmanager Signed-off-by: nagendra0721 <[email protected]> * MOSIP-30573: test case for signature Signed-off-by: nagendra0721 <[email protected]> --------- Signed-off-by: nagendra0721 <[email protected]>
1 parent 9da9554 commit a29da7f

File tree

3 files changed

+1919
-2
lines changed

3 files changed

+1919
-2
lines changed

kernel/kernel-keymanager-service/src/test/java/io/mosip/kernel/signature/test/service/SignatureServiceTest.java

Lines changed: 267 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import io.mosip.kernel.core.signatureutil.model.SignatureResponse;
55
import io.mosip.kernel.core.util.CryptoUtil;
66
import io.mosip.kernel.core.util.DateUtils;
7+
import io.mosip.kernel.keymanagerservice.constant.KeymanagerErrorConstant;
78
import io.mosip.kernel.keymanagerservice.dto.KeyPairGenerateRequestDto;
89
import io.mosip.kernel.keymanagerservice.exception.KeymanagerServiceException;
910
import io.mosip.kernel.keymanagerservice.repository.KeyAliasRepository;
1011
import io.mosip.kernel.keymanagerservice.service.KeymanagerService;
1112
import io.mosip.kernel.keymanagerservice.test.KeymanagerTestBootApplication;
13+
import io.mosip.kernel.keymanagerservice.util.KeymanagerUtil;
14+
import io.mosip.kernel.signature.constant.SignatureErrorCode;
1215
import io.mosip.kernel.signature.constant.SignatureProviderEnum;
1316
import io.mosip.kernel.signature.dto.*;
1417
import io.mosip.kernel.signature.exception.RequestException;
@@ -28,8 +31,11 @@
2831
import java.security.KeyPair;
2932
import java.security.KeyPairGenerator;
3033
import java.security.NoSuchAlgorithmException;
31-
import java.util.HashMap;
32-
import java.util.Map;
34+
import java.security.cert.Certificate;
35+
import java.util.*;
36+
37+
import static org.junit.jupiter.api.Assertions.assertEquals;
38+
import static org.junit.jupiter.api.Assertions.assertThrows;
3339

3440

3541
@SpringBootTest(classes = { KeymanagerTestBootApplication.class })
@@ -48,6 +54,9 @@ public class SignatureServiceTest {
4854
@Autowired
4955
private KeyAliasRepository keyAliasRepository;
5056

57+
@Autowired
58+
KeymanagerUtil keymanagerUtil;
59+
5160
@Before
5261
public void setUp() {
5362
KeyPairGenerateRequestDto keyPairGenRequestDto = new KeyPairGenerateRequestDto();
@@ -204,6 +213,15 @@ public void testJwtVerify() {
204213
Assert.assertNotNull(verifyResponse);
205214
Assert.assertTrue(verifyResponse.isSignatureValid());
206215
Assert.assertEquals("Validation Successful", verifyResponse.getMessage());
216+
217+
keyPairGenRequestDto.setReferenceId("ED25519_SIGN");
218+
keymanagerService.generateECSignKey("CSR", keyPairGenRequestDto);
219+
jwtSignRequestDto.setReferenceId("ED25519_SIGN");
220+
signResponse = signatureService.jwtSign(jwtSignRequestDto);
221+
verifyRequestDto.setJwtSignatureData(signResponse.getJwtSignedData());
222+
verifyResponse = signatureService.jwtVerify(verifyRequestDto);
223+
224+
Assert.assertNotNull(verifyResponse);
207225
}
208226

209227
@Test
@@ -300,6 +318,18 @@ public void testJwsSign() {
300318
jwsSignRequestDto.setCertificateUrl("https:://test/certificate.com");
301319
response = signatureService.jwsSign(jwsSignRequestDto);
302320
Assert.assertNotNull(response);
321+
322+
jwsSignRequestDto.setApplicationId("");
323+
response = signatureService.jwsSign(jwsSignRequestDto);
324+
Assert.assertNotNull(response);
325+
326+
keyPairGenRequestDto.setApplicationId("TEST");
327+
keyPairGenRequestDto.setReferenceId("ED25519_SIGN");
328+
keymanagerService.generateECSignKey("CSR", keyPairGenRequestDto);
329+
jwsSignRequestDto.setApplicationId("TEST");
330+
jwsSignRequestDto.setReferenceId("ED25519_SIGN");
331+
response = signatureService.jwsSign(jwsSignRequestDto);
332+
Assert.assertNotNull(response);
303333
}
304334

305335
@Test
@@ -362,6 +392,15 @@ public void testSignv2() {
362392
Assert.assertNotNull(response);
363393
Assert.assertNotNull(response.getSignature());
364394
Assert.assertNotNull(response.getTimestamp());
395+
396+
keyPairGenRequestDto.setApplicationId("KERNEL");
397+
keyPairGenRequestDto.setReferenceId("SIGN");
398+
keymanagerService.generateMasterKey("CSR", keyPairGenRequestDto);
399+
400+
signRequestDto.setApplicationId("");
401+
response = signatureServicev2.signv2(signRequestDto);
402+
Assert.assertNotNull(response);
403+
Assert.assertNotNull(response.getSignature());
365404
}
366405

367406
@Test
@@ -440,6 +479,24 @@ public void testJwtSignV2() {
440479
JWTSignatureResponseDto response = signatureService.jwtSignV2(jwtSignRequestDto);
441480
Assert.assertNotNull(response);
442481
Assert.assertNotNull(response.getJwtSignedData());
482+
483+
keyPairGenRequestDto.setApplicationId("KERNEL");
484+
keyPairGenRequestDto.setReferenceId("SIGN");
485+
keymanagerService.generateMasterKey("CSR", keyPairGenRequestDto);
486+
487+
jwtSignRequestDto.setApplicationId(null);
488+
jwtSignRequestDto.setReferenceId(null);
489+
response = signatureService.jwtSignV2(jwtSignRequestDto);
490+
Assert.assertNotNull(response);
491+
Assert.assertNotNull(response.getJwtSignedData());
492+
493+
Map<String, String> additionalHeaders2 = new HashMap<>();
494+
additionalHeaders2.put("test", "header");
495+
additionalHeaders2.put("kid", "test");
496+
additionalHeaders2.put("aud", "test");
497+
jwtSignRequestDto.setAdditionalHeaders(additionalHeaders2);
498+
response = signatureService.jwtSignV2(jwtSignRequestDto);
499+
Assert.assertNotNull(response);
443500
}
444501

445502
@Test
@@ -593,6 +650,16 @@ public void testValidateTrustV2() {
593650

594651
String trustResult = signatureService.validateTrustV2(jwtVerifyRequestDto, null, null);
595652
Assert.assertEquals("TRUST_NOT_VERIFIED", trustResult);
653+
654+
jwtVerifyRequestDto.setValidateTrust(true);
655+
String pemCertificate = keymanagerService.getCertificate("TEST", Optional.empty()).getCertificate();
656+
List<Certificate> certificateList = new ArrayList<>(Collections.singleton(keymanagerUtil.convertToCertificate(pemCertificate)));
657+
trustResult = signatureService.validateTrustV2(jwtVerifyRequestDto, certificateList, pemCertificate);
658+
Assert.assertEquals("TRUST_NOT_VERIFIED_NO_DOMAIN", trustResult);
659+
660+
jwtVerifyRequestDto.setDomain("DEVICE");
661+
trustResult = signatureService.validateTrustV2(jwtVerifyRequestDto, certificateList, pemCertificate);
662+
Assert.assertEquals("TRUST_CERT_PATH_NOT_VALID", trustResult);
596663
}
597664

598665
@Test
@@ -623,6 +690,22 @@ public void testJwtVerifyV2() {
623690
Assert.assertNotNull(verifyResponse);
624691
Assert.assertTrue(verifyResponse.isSignatureValid());
625692
Assert.assertEquals("Validation Successful", verifyResponse.getMessage());
693+
694+
keyPairGenRequestDto.setApplicationId("KERNEL");
695+
keyPairGenRequestDto.setReferenceId("SIGN");
696+
keymanagerService.generateMasterKey("CSR", keyPairGenRequestDto);
697+
698+
jwtSignRequestDtoV2.setApplicationId("");
699+
jwtSignRequestDtoV2.setReferenceId("");
700+
jwtSignRequestDtoV2.setIncludeCertificateChain(false);
701+
signResponse = signatureService.jwtSignV2(jwtSignRequestDtoV2);
702+
703+
verifyRequestDto.setJwtSignatureData(signResponse.getJwtSignedData());
704+
verifyRequestDto.setApplicationId("");
705+
verifyResponse = signatureService.jwtVerifyV2(verifyRequestDto);
706+
Assert.assertNotNull(verifyResponse);
707+
Assert.assertTrue(verifyResponse.isSignatureValid());
708+
Assert.assertEquals("Validation Successful", verifyResponse.getMessage());
626709
}
627710

628711
@Test
@@ -665,5 +748,187 @@ public void testJwsSignV2() {
665748
jwsSignRequestDtoV2.setCertificateUrl("https:://test/certificate.com");
666749
response = signatureService.jwsSignV2(jwsSignRequestDtoV2);
667750
Assert.assertNotNull(response);
751+
752+
jwsSignRequestDtoV2.setApplicationId("");
753+
response = signatureService.jwsSignV2(jwsSignRequestDtoV2);
754+
Assert.assertNotNull(response);
755+
756+
keyPairGenRequestDto.setApplicationId("TEST");
757+
keyPairGenRequestDto.setReferenceId("ED25519_SIGN");
758+
keymanagerService.generateECSignKey("CSR", keyPairGenRequestDto);
759+
760+
jwsSignRequestDtoV2.setApplicationId("TEST");
761+
jwsSignRequestDtoV2.setReferenceId("ED25519_SIGN");
762+
response = signatureService.jwsSignV2(jwsSignRequestDtoV2);
763+
Assert.assertNotNull(response);
764+
}
765+
766+
@Test
767+
public void testJwtVerifyDefaultAppIDAndRefID() {
768+
KeyPairGenerateRequestDto keyPairGenRequestDto = new KeyPairGenerateRequestDto();
769+
keyPairGenRequestDto.setApplicationId("KERNEL");
770+
keyPairGenRequestDto.setReferenceId("SIGN");
771+
keymanagerService.generateMasterKey("CSR", keyPairGenRequestDto);
772+
773+
// First sign
774+
JWTSignatureRequestDto jwtSignRequestDto = new JWTSignatureRequestDto();
775+
jwtSignRequestDto.setDataToSign(CryptoUtil.encodeToURLSafeBase64("{\"test\":\"data\"}".getBytes()));
776+
jwtSignRequestDto.setIncludePayload(true);
777+
jwtSignRequestDto.setIncludeCertificate(false);
778+
JWTSignatureResponseDto signResponse = signatureService.jwtSign(jwtSignRequestDto);
779+
780+
// Then verify
781+
JWTSignatureVerifyRequestDto verifyRequestDto = new JWTSignatureVerifyRequestDto();
782+
verifyRequestDto.setJwtSignatureData(signResponse.getJwtSignedData());
783+
JWTSignatureVerifyResponseDto verifyResponse = signatureService.jwtVerify(verifyRequestDto);
784+
785+
Assert.assertNotNull(verifyResponse);
786+
Assert.assertTrue(verifyResponse.isSignatureValid());
787+
Assert.assertEquals("Validation Successful", verifyResponse.getMessage());
788+
}
789+
790+
@Test
791+
public void testJsonParsingError() {
792+
String signData = "ewogICJhbGciOiAiUlMyNTYiLAogIHg1YzogWwogICAgIlNmN21UV2pmOE91VWlVTksybXNYTXN2SEZhdjlmaGJJNkNvVlhyUlJPY0xPVFZrNk9lSSsrckZaQ0w4NDZsSk82MlpRTHZuZSs2IgogIF0sCiAgImtpZCI6ICJNQ1NTSjZBdjhiV0FZNzBXUk5nNHVPS04yLUhFMGRGOW1pWUI3Q2lqT1BzIgp9.eyAibW9kdWxlIjogIktleW1hbmFnZXIiLCAicHJvamVjdCI6ICJNb3NpcCIgfQ.bZIrGgpKoZAsL0NyKKshS78LzlvLp3xdlWiHtrB---UVL0cAenbMaxrjgWphQAzH4l2NCOz7BYeL1UN1sUvMOBCNfplRaG8aEDb4TTG6aQjMRXZg7LJJnuBQjuU4pdPLa8qYMBhW5nssc-WZ9DK4aLH2YW68FF4zUezvAsJWexftNkVE0n9Vf05sxI4olVh696t-xrNFsMDHlrHyOWVzkQOI6i9OMsyOqgBdo6hNJG7DXTzPRV_xKkiR3SGRP0AmF57zvS7kQm8SwkGQQE9rGYPqkLG1x_3pHL4P9NeqTT77kIcKR22lOyeWKcKR1NSzmDA_RKbJBD_w9kHF0hdytg";
793+
JWTSignatureVerifyRequestDto verifyRequestDto = new JWTSignatureVerifyRequestDto();
794+
verifyRequestDto.setJwtSignatureData(signData);
795+
796+
RequestException exception = assertThrows(RequestException.class, () -> {
797+
signatureService.jwtVerify(verifyRequestDto);
798+
});
799+
assertEquals(SignatureErrorCode.INVALID_VERIFY_INPUT.getErrorCode(), exception.getErrorCode());
800+
801+
exception = assertThrows(RequestException.class, () -> {
802+
signatureService.jwtVerifyV2(verifyRequestDto);
803+
});
804+
assertEquals(SignatureErrorCode.INVALID_VERIFY_INPUT.getErrorCode(), exception.getErrorCode());
805+
}
806+
807+
@Test
808+
public void testJWSsignEmptyDataException() {
809+
JWTSignatureRequestDto requestDto = new JWTSignatureRequestDto();
810+
requestDto.setDataToSign("");
811+
812+
RequestException exception = assertThrows(RequestException.class, () -> {
813+
signatureService.jwtSign(requestDto);
814+
});
815+
816+
assertEquals(SignatureErrorCode.INVALID_INPUT.getErrorCode(), exception.getErrorCode());
817+
}
818+
819+
@Test
820+
public void testSignV2EmptyDataException() {
821+
SignRequestDtoV2 requestDto = new SignRequestDtoV2();
822+
requestDto.setApplicationId("INVALID_APP_ID");
823+
824+
RequestException exception = assertThrows(RequestException.class, () -> {
825+
signatureServicev2.signv2(requestDto);
826+
});
827+
828+
assertEquals(SignatureErrorCode.SIGN_NOT_ALLOWED.getErrorCode(), exception.getErrorCode());
829+
830+
requestDto.setApplicationId("TEST");
831+
exception = assertThrows(RequestException.class, () -> {
832+
signatureServicev2.signv2(requestDto);
833+
});
834+
assertEquals(SignatureErrorCode.INVALID_INPUT.getErrorCode(), exception.getErrorCode());
835+
}
836+
837+
@Test
838+
public void rawSignException() {
839+
SignRequestDtoV2 requestDto = new SignRequestDtoV2();
840+
requestDto.setApplicationId("INVALID_APP_ID");
841+
842+
RequestException exception = assertThrows(RequestException.class, () -> {
843+
signatureServicev2.rawSign(requestDto);
844+
});
845+
846+
assertEquals(SignatureErrorCode.SIGN_NOT_ALLOWED.getErrorCode(), exception.getErrorCode());
847+
848+
requestDto.setApplicationId("TEST");
849+
exception = assertThrows(RequestException.class, () -> {
850+
signatureServicev2.rawSign(requestDto);
851+
});
852+
853+
assertEquals(SignatureErrorCode.INVALID_INPUT.getErrorCode(), exception.getErrorCode());
854+
855+
856+
KeyPairGenerateRequestDto keyPairGenRequestDto = new KeyPairGenerateRequestDto();
857+
keyPairGenRequestDto.setApplicationId("KERNEL");
858+
keyPairGenRequestDto.setReferenceId("SIGN");
859+
keymanagerService.generateMasterKey("CSR", keyPairGenRequestDto);
860+
861+
requestDto.setApplicationId(null);
862+
requestDto.setDataToSign("c2lnbiByYXcgZGF0YQ==");
863+
requestDto.setResponseEncodingFormat("INVALID_FORMAT");
864+
KeymanagerServiceException exception1 = assertThrows(KeymanagerServiceException.class, () -> {
865+
signatureServicev2.rawSign(requestDto);
866+
});
867+
assertEquals(KeymanagerErrorConstant.INVALID_FORMAT_ERROR.getErrorCode(), exception1.getErrorCode());
868+
869+
requestDto.setResponseEncodingFormat("base64url");
870+
SignResponseDtoV2 response = signatureServicev2.rawSign(requestDto);
871+
Assert.assertNotNull(response);
872+
}
873+
874+
@Test
875+
public void testJWTSignV2Exception() {
876+
JWTSignatureRequestDtoV2 requestDtoV2 = new JWTSignatureRequestDtoV2();
877+
requestDtoV2.setApplicationId("INVALID_APP_ID");
878+
879+
RequestException exception = assertThrows(RequestException.class, () -> {
880+
signatureService.jwtSignV2(requestDtoV2);
881+
});
882+
883+
assertEquals(SignatureErrorCode.SIGN_NOT_ALLOWED.getErrorCode(), exception.getErrorCode());
884+
885+
requestDtoV2.setApplicationId("TEST");
886+
exception = assertThrows(RequestException.class, () -> {
887+
signatureService.jwtSignV2(requestDtoV2);
888+
});
889+
assertEquals(SignatureErrorCode.INVALID_INPUT.getErrorCode(), exception.getErrorCode());
890+
891+
requestDtoV2.setDataToSign("c2lnbiByYXcgZGF0YQ==");
892+
exception = assertThrows(RequestException.class, () -> {
893+
signatureService.jwtSignV2(requestDtoV2);
894+
});
895+
assertEquals(SignatureErrorCode.INVALID_JSON.getErrorCode(), exception.getErrorCode());
896+
}
897+
898+
@Test
899+
public void testJWSsignV2Exception() {
900+
JWSSignatureRequestDtoV2 requestDtoV2 = new JWSSignatureRequestDtoV2();
901+
requestDtoV2.setApplicationId("INVALID_APP_ID");
902+
903+
RequestException exception = assertThrows(RequestException.class, () -> {
904+
signatureService.jwsSignV2(requestDtoV2);
905+
});
906+
907+
assertEquals(SignatureErrorCode.SIGN_NOT_ALLOWED.getErrorCode(), exception.getErrorCode());
908+
909+
requestDtoV2.setApplicationId("TEST");
910+
exception = assertThrows(RequestException.class, () -> {
911+
signatureService.jwsSignV2(requestDtoV2);
912+
});
913+
assertEquals(SignatureErrorCode.INVALID_INPUT.getErrorCode(), exception.getErrorCode());
914+
915+
requestDtoV2.setDataToSign("c2lnbiByYXcgZGF0YQ==");
916+
requestDtoV2.setValidateJson(true);
917+
exception = assertThrows(RequestException.class, () -> {
918+
signatureService.jwsSignV2(requestDtoV2);
919+
});
920+
assertEquals(SignatureErrorCode.INVALID_JSON.getErrorCode(), exception.getErrorCode());
921+
}
922+
923+
@Test
924+
public void testJWTVerifyV2EmptySignData() {
925+
JWTSignatureVerifyRequestDto verifyRequestDto = new JWTSignatureVerifyRequestDto();
926+
verifyRequestDto.setJwtSignatureData("");
927+
928+
RequestException exception = assertThrows(RequestException.class, () -> {
929+
signatureService.jwtVerifyV2(verifyRequestDto);
930+
});
931+
932+
assertEquals(SignatureErrorCode.INVALID_INPUT.getErrorCode(), exception.getErrorCode());
668933
}
669934
}

0 commit comments

Comments
 (0)