|
| 1 | +/* |
| 2 | + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except |
| 5 | + * in compliance with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://aws.amazon.com/apache2.0 |
| 8 | + * |
| 9 | + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package com.amazonaws.encryptionsdk; |
| 15 | + |
| 16 | +import com.amazonaws.encryptionsdk.internal.StaticMasterKey; |
| 17 | +import com.amazonaws.encryptionsdk.internal.VersionInfo; |
| 18 | +import com.amazonaws.encryptionsdk.model.CiphertextHeaders; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import com.amazonaws.encryptionsdk.exception.BadCiphertextException; |
| 23 | + |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Arrays; |
| 27 | + |
| 28 | +import static org.junit.Assert.*; |
| 29 | +import static org.mockito.Mockito.spy; |
| 30 | + |
| 31 | +public class ParsedCiphertextTest extends CiphertextHeaders { |
| 32 | + private StaticMasterKey masterKeyProvider; |
| 33 | + private AwsCrypto encryptionClient_; |
| 34 | + |
| 35 | + @Before |
| 36 | + public void init() { |
| 37 | + masterKeyProvider = spy(new StaticMasterKey("testmaterial")); |
| 38 | + |
| 39 | + encryptionClient_ = new AwsCrypto(); |
| 40 | + encryptionClient_.setEncryptionAlgorithm(CryptoAlgorithm.ALG_AES_128_GCM_IV12_TAG16_HKDF_SHA256); |
| 41 | + } |
| 42 | + |
| 43 | + @Test() |
| 44 | + public void goodParsedCiphertext() { |
| 45 | + final int byteSize = 0; |
| 46 | + final int frameSize = 0; |
| 47 | + final byte[] plaintextBytes = new byte[byteSize]; |
| 48 | + |
| 49 | + final Map<String, String> encryptionContext = new HashMap<String, String>(1); |
| 50 | + encryptionContext.put("ENC1", "ParsedCiphertext test with %d" + byteSize); |
| 51 | + |
| 52 | + encryptionClient_.setEncryptionFrameSize(frameSize); |
| 53 | + |
| 54 | + final byte[] cipherText = encryptionClient_.encryptData( |
| 55 | + masterKeyProvider, |
| 56 | + plaintextBytes, |
| 57 | + encryptionContext).getResult(); |
| 58 | + final ParsedCiphertext pCt = new ParsedCiphertext(cipherText); |
| 59 | + |
| 60 | + assertNotNull(pCt.getCiphertext()); |
| 61 | + assertTrue(pCt.getOffset() > 0); |
| 62 | + } |
| 63 | + |
| 64 | + @Test(expected = BadCiphertextException.class) |
| 65 | + public void incompleteZeroByteCiphertext() { |
| 66 | + final byte[] cipherText = {}; |
| 67 | + ParsedCiphertext pCt = new ParsedCiphertext(cipherText); |
| 68 | + } |
| 69 | + |
| 70 | + @Test(expected = BadCiphertextException.class) |
| 71 | + public void incompleteSingleByteCiphertext() { |
| 72 | + final byte[] cipherText = {VersionInfo.CURRENT_CIPHERTEXT_VERSION}; |
| 73 | + ParsedCiphertext pCt = new ParsedCiphertext(cipherText); |
| 74 | + } |
| 75 | + |
| 76 | + @Test(expected = BadCiphertextException.class) |
| 77 | + public void incompleteCiphertext() { |
| 78 | + final int byteSize = 0; |
| 79 | + final int frameSize = 0; |
| 80 | + final byte[] plaintextBytes = new byte[byteSize]; |
| 81 | + |
| 82 | + final Map<String, String> encryptionContext = new HashMap<String, String>(1); |
| 83 | + encryptionContext.put("ENC1", "ParsedCiphertext test with %d" + byteSize); |
| 84 | + |
| 85 | + encryptionClient_.setEncryptionFrameSize(frameSize); |
| 86 | + |
| 87 | + final byte[] cipherText = encryptionClient_.encryptData( |
| 88 | + masterKeyProvider, |
| 89 | + plaintextBytes, |
| 90 | + encryptionContext).getResult(); |
| 91 | + ParsedCiphertext pCt = new ParsedCiphertext(cipherText); |
| 92 | + |
| 93 | + byte[] incompleteCiphertext = Arrays.copyOf(pCt.getCiphertext(), pCt.getOffset() - 1); |
| 94 | + ParsedCiphertext badPCt = new ParsedCiphertext(incompleteCiphertext); |
| 95 | + } |
| 96 | +} |
0 commit comments