|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package com.amazonaws.encryptionsdk; |
| 5 | + |
| 6 | +import com.amazonaws.encryptionsdk.model.DecryptionMaterials; |
| 7 | +import com.amazonaws.encryptionsdk.model.DecryptionMaterialsHandler; |
| 8 | +import com.amazonaws.encryptionsdk.model.DecryptionMaterialsRequest; |
| 9 | +import com.amazonaws.encryptionsdk.model.KeyBlob; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import java.security.PublicKey; |
| 13 | +import java.util.*; |
| 14 | + |
| 15 | +import static org.junit.Assert.assertEquals; |
| 16 | +import static org.junit.Assert.assertTrue; |
| 17 | +import static org.mockito.Mockito.mock; |
| 18 | +import static org.mockito.Mockito.when; |
| 19 | + |
| 20 | +public class CMMHandlerTest { |
| 21 | + |
| 22 | + // |
| 23 | + private static final CryptoAlgorithm SOME_CRYPTO_ALGORITHM = CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384; |
| 24 | + private static final List<KeyBlob> SOME_EDK_LIST = new ArrayList<>(Collections.singletonList(new KeyBlob())); |
| 25 | + private static final CommitmentPolicy SOME_COMMITMENT_POLICY = CommitmentPolicy.RequireEncryptRequireDecrypt; |
| 26 | + private static final Map<String, String> SOME_NON_EMPTY_ENCRYPTION_CONTEXT = new HashMap<>(); |
| 27 | + |
| 28 | + static {{ |
| 29 | + SOME_NON_EMPTY_ENCRYPTION_CONTEXT.put("SomeKey", "SomeValue"); |
| 30 | + }} |
| 31 | + |
| 32 | + private static final DecryptionMaterialsRequest SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC = |
| 33 | + DecryptionMaterialsRequest.newBuilder() |
| 34 | + .setAlgorithm(SOME_CRYPTO_ALGORITHM) |
| 35 | + // Given: Request has some non-empty encryption context |
| 36 | + .setEncryptionContext(SOME_NON_EMPTY_ENCRYPTION_CONTEXT) |
| 37 | + .setReproducedEncryptionContext(new HashMap<>()) |
| 38 | + .setEncryptedDataKeys(SOME_EDK_LIST) |
| 39 | + .build(); |
| 40 | + |
| 41 | + private static final DecryptionMaterialsRequest SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC = |
| 42 | + DecryptionMaterialsRequest.newBuilder() |
| 43 | + .setAlgorithm(SOME_CRYPTO_ALGORITHM) |
| 44 | + // Given: Request has empty encryption context |
| 45 | + .setEncryptionContext(new HashMap<>()) |
| 46 | + .setReproducedEncryptionContext(new HashMap<>()) |
| 47 | + .setEncryptedDataKeys(SOME_EDK_LIST) |
| 48 | + .build(); |
| 49 | + |
| 50 | + @Test |
| 51 | + public void GIVEN_CMM_does_not_add_encryption_context_AND_request_has_nonempty_encryption_context_WHEN_decryptMaterials_THEN_output_has_nonempty_encryption_context() { |
| 52 | + CryptoMaterialsManager anyNativeCMM = mock(CryptoMaterialsManager.class); |
| 53 | + |
| 54 | + // Given: native CMM does not set an encryptionContext on returned DecryptionMaterials objects |
| 55 | + DecryptionMaterials someDecryptionMaterialsWithoutEC = DecryptionMaterials.newBuilder() |
| 56 | + .setDataKey(mock(DataKey.class)) |
| 57 | + .setTrailingSignatureKey(mock(PublicKey.class)) |
| 58 | + .setEncryptionContext(new HashMap<>()).build(); |
| 59 | + // Given: request with nonempty encryption context |
| 60 | + when(anyNativeCMM.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC)) |
| 61 | + .thenReturn(someDecryptionMaterialsWithoutEC); |
| 62 | + |
| 63 | + // When: decryptMaterials |
| 64 | + CMMHandler handlerUnderTest = new CMMHandler(anyNativeCMM); |
| 65 | + DecryptionMaterialsHandler output = handlerUnderTest.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC, |
| 66 | + SOME_COMMITMENT_POLICY); |
| 67 | + |
| 68 | + // Then: output DecryptionMaterialsHandler has encryption context |
| 69 | + assertEquals(SOME_NON_EMPTY_ENCRYPTION_CONTEXT, output.getEncryptionContext()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void GIVEN_CMM_does_not_add_encryption_context_AND_request_has_empty_encryption_context_WHEN_decryptMaterials_THEN_output_has_empty_encryption_context() { |
| 74 | + CryptoMaterialsManager anyNativeCMM = mock(CryptoMaterialsManager.class); |
| 75 | + |
| 76 | + // Given: native CMM does not set an encryptionContext on returned DecryptionMaterials objects |
| 77 | + DecryptionMaterials someDecryptionMaterialsWithoutEC = DecryptionMaterials.newBuilder() |
| 78 | + .setDataKey(mock(DataKey.class)) |
| 79 | + .setTrailingSignatureKey(mock(PublicKey.class)) |
| 80 | + .setEncryptionContext(new HashMap<>()).build(); |
| 81 | + // Given: request with empty encryption context |
| 82 | + when(anyNativeCMM.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC)) |
| 83 | + .thenReturn(someDecryptionMaterialsWithoutEC); |
| 84 | + |
| 85 | + // When: decryptMaterials |
| 86 | + CMMHandler handlerUnderTest = new CMMHandler(anyNativeCMM); |
| 87 | + DecryptionMaterialsHandler output = handlerUnderTest.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC, |
| 88 | + SOME_COMMITMENT_POLICY); |
| 89 | + |
| 90 | + // Then: output DecryptionMaterialsHandler has empty encryption context |
| 91 | + assertTrue(output.getEncryptionContext().isEmpty()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void GIVEN_CMM_adds_encryption_context_AND_request_has_nonempty_encryption_context_WHEN_decryptMaterials_THEN_output_has_nonempty_encryption_context() { |
| 96 | + CryptoMaterialsManager anyNativeCMM = mock(CryptoMaterialsManager.class); |
| 97 | + |
| 98 | + // Given: native CMM sets encryptionContext on returned DecryptionMaterials objects |
| 99 | + DecryptionMaterials someDecryptionMaterialsWithoutEC = DecryptionMaterials.newBuilder() |
| 100 | + .setDataKey(mock(DataKey.class)) |
| 101 | + .setTrailingSignatureKey(mock(PublicKey.class)) |
| 102 | + .setEncryptionContext(SOME_NON_EMPTY_ENCRYPTION_CONTEXT).build(); |
| 103 | + // Given: request with nonempty encryption context |
| 104 | + when(anyNativeCMM.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC)) |
| 105 | + .thenReturn(someDecryptionMaterialsWithoutEC); |
| 106 | + |
| 107 | + // When: decryptMaterials |
| 108 | + CMMHandler handlerUnderTest = new CMMHandler(anyNativeCMM); |
| 109 | + DecryptionMaterialsHandler output = handlerUnderTest.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_NON_EMPTY_EC, |
| 110 | + SOME_COMMITMENT_POLICY); |
| 111 | + |
| 112 | + // Then: output DecryptionMaterialsHandler has nonempty encryption context |
| 113 | + assertEquals(SOME_NON_EMPTY_ENCRYPTION_CONTEXT, output.getEncryptionContext()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void GIVEN_CMM_adds_encryption_context_AND_request_has_empty_encryption_context_WHEN_decryptMaterials_THEN_output_has_empty_encryption_context() { |
| 118 | + CryptoMaterialsManager anyNativeCMM = mock(CryptoMaterialsManager.class); |
| 119 | + |
| 120 | + // Given: native CMM sets encryptionContext on returned DecryptionMaterials objects |
| 121 | + DecryptionMaterials someDecryptionMaterialsWithoutEC = DecryptionMaterials.newBuilder() |
| 122 | + .setDataKey(mock(DataKey.class)) |
| 123 | + .setTrailingSignatureKey(mock(PublicKey.class)) |
| 124 | + .setEncryptionContext(new HashMap<>()).build(); |
| 125 | + // Given: request with empty encryption context |
| 126 | + when(anyNativeCMM.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC)) |
| 127 | + .thenReturn(someDecryptionMaterialsWithoutEC); |
| 128 | + |
| 129 | + // When: decryptMaterials |
| 130 | + CMMHandler handlerUnderTest = new CMMHandler(anyNativeCMM); |
| 131 | + DecryptionMaterialsHandler output = handlerUnderTest.decryptMaterials(SOME_DECRYPTION_MATERIALS_REQUEST_EMPTY_EC, |
| 132 | + SOME_COMMITMENT_POLICY); |
| 133 | + |
| 134 | + // Then: output DecryptionMaterialsHandler has empty encryption context |
| 135 | + assertTrue(output.getEncryptionContext().isEmpty()); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments