|
| 1 | +package com.ironcorelabs.tenantsecurity.kms.v1; |
| 2 | + |
| 3 | +import static org.testng.Assert.assertEquals; |
| 4 | +import static org.testng.Assert.assertTrue; |
| 5 | +import java.io.ByteArrayInputStream; |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.nio.ByteBuffer; |
| 8 | +import java.security.SecureRandom; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.stream.IntStream; |
| 11 | +import org.testng.annotations.Test; |
| 12 | +import com.ironcorelabs.tenantsecurity.kms.v1.exception.KmsException; |
| 13 | +import com.ironcorelabs.tenantsecurity.kms.v1.exception.SecurityEventException; |
| 14 | +import com.ironcorelabs.tenantsecurity.kms.v1.exception.TenantSecurityException; |
| 15 | +import com.ironcorelabs.tenantsecurity.kms.v1.exception.TspServiceException; |
| 16 | + |
| 17 | +@Test(groups = {"unit"}) |
| 18 | +public class ErrorResponseTest { |
| 19 | + |
| 20 | + public void exceptionFromErrorResponseTspServiceException() throws Exception { |
| 21 | + final String staticMsg = "static message"; |
| 22 | + final int staticHttpCode = 42; |
| 23 | + |
| 24 | + // TspServiceException |
| 25 | + ErrorResponse unableToMakeReqError = |
| 26 | + new ErrorResponse(TenantSecurityErrorCodes.UNABLE_TO_MAKE_REQUEST.getCode(), staticMsg); |
| 27 | + TenantSecurityException unableToMakeReqException = |
| 28 | + unableToMakeReqError.toTenantSecurityException(staticHttpCode); |
| 29 | + assertTspServiceException(staticMsg, staticHttpCode, unableToMakeReqException, |
| 30 | + TenantSecurityErrorCodes.UNABLE_TO_MAKE_REQUEST); |
| 31 | + |
| 32 | + ErrorResponse unknownErrResp = |
| 33 | + new ErrorResponse(TenantSecurityErrorCodes.UNKNOWN_ERROR.getCode(), staticMsg); |
| 34 | + TenantSecurityException unknownErrException = |
| 35 | + unknownErrResp.toTenantSecurityException(staticHttpCode); |
| 36 | + assertTspServiceException(staticMsg, staticHttpCode, unknownErrException, |
| 37 | + TenantSecurityErrorCodes.UNKNOWN_ERROR); |
| 38 | + |
| 39 | + ErrorResponse invalidRequestBody = |
| 40 | + new ErrorResponse(TenantSecurityErrorCodes.INVALID_REQUEST_BODY.getCode(), staticMsg); |
| 41 | + TenantSecurityException invalidRequestException = |
| 42 | + invalidRequestBody.toTenantSecurityException(staticHttpCode); |
| 43 | + assertTspServiceException(staticMsg, staticHttpCode, invalidRequestException, |
| 44 | + TenantSecurityErrorCodes.INVALID_REQUEST_BODY); |
| 45 | + |
| 46 | + ErrorResponse unauthorizedReqErrResp = |
| 47 | + new ErrorResponse(TenantSecurityErrorCodes.UNAUTHORIZED_REQUEST.getCode(), staticMsg); |
| 48 | + TenantSecurityException unauthorizedReqException = |
| 49 | + unauthorizedReqErrResp.toTenantSecurityException(staticHttpCode); |
| 50 | + assertTspServiceException(staticMsg, staticHttpCode, unauthorizedReqException, |
| 51 | + TenantSecurityErrorCodes.UNAUTHORIZED_REQUEST); |
| 52 | + |
| 53 | + // KmsException |
| 54 | + ErrorResponse noPrimaryKmsResp = new ErrorResponse( |
| 55 | + TenantSecurityErrorCodes.NO_PRIMARY_KMS_CONFIGURATION.getCode(), staticMsg); |
| 56 | + TenantSecurityException noPrimaryKmsException = |
| 57 | + noPrimaryKmsResp.toTenantSecurityException(staticHttpCode); |
| 58 | + assertKmsException(staticMsg, staticHttpCode, noPrimaryKmsException, |
| 59 | + TenantSecurityErrorCodes.NO_PRIMARY_KMS_CONFIGURATION); |
| 60 | + |
| 61 | + ErrorResponse unknownTenantError = new ErrorResponse( |
| 62 | + TenantSecurityErrorCodes.UNKNOWN_TENANT_OR_NO_ACTIVE_KMS_CONFIGURATIONS.getCode(), |
| 63 | + staticMsg); |
| 64 | + TenantSecurityException unknownTenantException = |
| 65 | + unknownTenantError.toTenantSecurityException(staticHttpCode); |
| 66 | + assertKmsException(staticMsg, staticHttpCode, unknownTenantException, |
| 67 | + TenantSecurityErrorCodes.UNKNOWN_TENANT_OR_NO_ACTIVE_KMS_CONFIGURATIONS); |
| 68 | + |
| 69 | + ErrorResponse kmsCfgDisabledError = |
| 70 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_CONFIGURATION_DISABLED.getCode(), staticMsg); |
| 71 | + TenantSecurityException kmsCfgDisabledException = |
| 72 | + kmsCfgDisabledError.toTenantSecurityException(staticHttpCode); |
| 73 | + assertKmsException(staticMsg, staticHttpCode, kmsCfgDisabledException, |
| 74 | + TenantSecurityErrorCodes.KMS_CONFIGURATION_DISABLED); |
| 75 | + |
| 76 | + ErrorResponse invalidEdekErrResp = |
| 77 | + new ErrorResponse(TenantSecurityErrorCodes.INVALID_PROVIDED_EDEK.getCode(), staticMsg); |
| 78 | + TenantSecurityException invalidEdekException = |
| 79 | + invalidEdekErrResp.toTenantSecurityException(staticHttpCode); |
| 80 | + assertKmsException(staticMsg, staticHttpCode, invalidEdekException, |
| 81 | + TenantSecurityErrorCodes.INVALID_PROVIDED_EDEK); |
| 82 | + |
| 83 | + ErrorResponse unwrapError = |
| 84 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_UNWRAP_FAILED.getCode(), staticMsg); |
| 85 | + TenantSecurityException unwrapException = unwrapError.toTenantSecurityException(staticHttpCode); |
| 86 | + assertKmsException(staticMsg, staticHttpCode, unwrapException, |
| 87 | + TenantSecurityErrorCodes.KMS_UNWRAP_FAILED); |
| 88 | + |
| 89 | + ErrorResponse wrapError = |
| 90 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_WRAP_FAILED.getCode(), staticMsg); |
| 91 | + TenantSecurityException kmsWrapException = wrapError.toTenantSecurityException(staticHttpCode); |
| 92 | + assertKmsException(staticMsg, staticHttpCode, kmsWrapException, |
| 93 | + TenantSecurityErrorCodes.KMS_WRAP_FAILED); |
| 94 | + |
| 95 | + ErrorResponse kmsAuthError = |
| 96 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_AUTHORIZATION_FAILED.getCode(), staticMsg); |
| 97 | + TenantSecurityException kmsAuthException = |
| 98 | + kmsAuthError.toTenantSecurityException(staticHttpCode); |
| 99 | + assertKmsException(staticMsg, staticHttpCode, kmsAuthException, |
| 100 | + TenantSecurityErrorCodes.KMS_AUTHORIZATION_FAILED); |
| 101 | + |
| 102 | + ErrorResponse kmsConfigInvalidError = |
| 103 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_CONFIGURATION_INVALID.getCode(), staticMsg); |
| 104 | + TenantSecurityException kmsConfigInvalidException = |
| 105 | + kmsConfigInvalidError.toTenantSecurityException(staticHttpCode); |
| 106 | + assertKmsException(staticMsg, staticHttpCode, kmsConfigInvalidException, |
| 107 | + TenantSecurityErrorCodes.KMS_CONFIGURATION_INVALID); |
| 108 | + |
| 109 | + ErrorResponse foo = |
| 110 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_ACCOUNT_ISSUE.getCode(), staticMsg); |
| 111 | + TenantSecurityException fooException = foo.toTenantSecurityException(staticHttpCode); |
| 112 | + assertKmsException(staticMsg, staticHttpCode, fooException, |
| 113 | + TenantSecurityErrorCodes.KMS_ACCOUNT_ISSUE); |
| 114 | + |
| 115 | + ErrorResponse kmsUnreachableError = |
| 116 | + new ErrorResponse(TenantSecurityErrorCodes.KMS_UNREACHABLE.getCode(), staticMsg); |
| 117 | + TenantSecurityException kmsUnreachableException = |
| 118 | + kmsUnreachableError.toTenantSecurityException(staticHttpCode); |
| 119 | + assertKmsException(staticMsg, staticHttpCode, kmsUnreachableException, |
| 120 | + TenantSecurityErrorCodes.KMS_UNREACHABLE); |
| 121 | + |
| 122 | + // SecurityEventException |
| 123 | + ErrorResponse securityEventRejectedError = |
| 124 | + new ErrorResponse(TenantSecurityErrorCodes.SECURITY_EVENT_REJECTED.getCode(), staticMsg); |
| 125 | + TenantSecurityException securityEventRejectedException = |
| 126 | + securityEventRejectedError.toTenantSecurityException(staticHttpCode); |
| 127 | + assertSecurityEventException(staticMsg, staticHttpCode, securityEventRejectedException, |
| 128 | + TenantSecurityErrorCodes.SECURITY_EVENT_REJECTED); |
| 129 | + } |
| 130 | + |
| 131 | + private void assertTspServiceException(String expectedMsg, int expectedHttpStatusCode, |
| 132 | + TenantSecurityException exception, TenantSecurityErrorCodes errorCode) { |
| 133 | + assertTenantSecurityException(expectedMsg, expectedHttpStatusCode, exception, errorCode); |
| 134 | + assertTrue(exception instanceof TspServiceException); |
| 135 | + } |
| 136 | + |
| 137 | + private void assertSecurityEventException(String expectedMsg, int expectedHttpStatusCode, |
| 138 | + TenantSecurityException exception, TenantSecurityErrorCodes errorCode) { |
| 139 | + assertTenantSecurityException(expectedMsg, expectedHttpStatusCode, exception, errorCode); |
| 140 | + assertTrue(exception instanceof SecurityEventException); |
| 141 | + } |
| 142 | + |
| 143 | + private void assertKmsException(String expectedMsg, int expectedHttpStatusCode, |
| 144 | + TenantSecurityException exception, TenantSecurityErrorCodes errorCode) { |
| 145 | + assertTenantSecurityException(expectedMsg, expectedHttpStatusCode, exception, errorCode); |
| 146 | + assertTrue(exception instanceof KmsException); |
| 147 | + } |
| 148 | + |
| 149 | + private void assertTenantSecurityException(String expectedMsg, int expectedHttpStatusCode, |
| 150 | + TenantSecurityException exception, TenantSecurityErrorCodes errorCode) { |
| 151 | + assertEquals(errorCode, exception.getErrorCode()); |
| 152 | + assertEquals(exception.getHttpResponseCode(), expectedHttpStatusCode); |
| 153 | + assertEquals(exception.getMessage(), expectedMsg); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments