|
| 1 | +package org.cryptomator.siv; |
| 2 | +/******************************************************************************* |
| 3 | + * Copyright (c) 2016 Sebastian Stenzel |
| 4 | + * This file is licensed under the terms of the MIT license. |
| 5 | + * See the LICENSE.txt file for more info. |
| 6 | + * |
| 7 | + * Contributors: |
| 8 | + * Sebastian Stenzel - initial API and implementation |
| 9 | + ******************************************************************************/ |
| 10 | + |
| 11 | +import org.bouncycastle.crypto.DataLengthException; |
| 12 | +import org.bouncycastle.crypto.params.AsymmetricKeyParameter; |
| 13 | +import org.bouncycastle.crypto.params.KeyParameter; |
| 14 | +import org.junit.Assert; |
| 15 | +import org.junit.Rule; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.rules.ExpectedException; |
| 18 | + |
| 19 | +public class JceAesBlockCipherTest { |
| 20 | + |
| 21 | + @Rule |
| 22 | + public final ExpectedException thrown = ExpectedException.none(); |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testInitWithNullParam() { |
| 26 | + JceAesBlockCipher cipher = new JceAesBlockCipher(); |
| 27 | + thrown.expect(IllegalArgumentException.class); |
| 28 | + thrown.expectMessage("missing parameter of type KeyParameter"); |
| 29 | + cipher.init(true, null); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testInitWithMissingKey() { |
| 34 | + JceAesBlockCipher cipher = new JceAesBlockCipher(); |
| 35 | + thrown.expect(IllegalArgumentException.class); |
| 36 | + thrown.expectMessage("missing parameter of type KeyParameter"); |
| 37 | + cipher.init(true, new AsymmetricKeyParameter(true)); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testInitWithInvalidKey() { |
| 42 | + JceAesBlockCipher cipher = new JceAesBlockCipher(); |
| 43 | + thrown.expect(IllegalArgumentException.class); |
| 44 | + thrown.expectMessage("Invalid key"); |
| 45 | + cipher.init(true, new KeyParameter(new byte[7])); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testInitForEncryption() { |
| 50 | + JceAesBlockCipher cipher = new JceAesBlockCipher(); |
| 51 | + cipher.init(true, new KeyParameter(new byte[16])); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testInitForDecryption() { |
| 56 | + JceAesBlockCipher cipher = new JceAesBlockCipher(); |
| 57 | + cipher.init(false, new KeyParameter(new byte[16])); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testGetAlgorithmName() { |
| 62 | + JceAesBlockCipher cipher = new JceAesBlockCipher(); |
| 63 | + Assert.assertEquals("AES", cipher.getAlgorithmName()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testGetBlockSize() { |
| 68 | + JceAesBlockCipher cipher = new JceAesBlockCipher(); |
| 69 | + Assert.assertEquals(16, cipher.getBlockSize()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testProcessBlockWithUninitializedCipher() { |
| 74 | + JceAesBlockCipher cipher = new JceAesBlockCipher(); |
| 75 | + thrown.expect(IllegalStateException.class); |
| 76 | + cipher.processBlock(new byte[16], 0, new byte[16], 0); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testProcessBlockWithUnsufficientInput() { |
| 81 | + JceAesBlockCipher cipher = new JceAesBlockCipher(); |
| 82 | + cipher.init(true, new KeyParameter(new byte[16])); |
| 83 | + thrown.expect(DataLengthException.class); |
| 84 | + thrown.expectMessage("Insufficient data in 'in'"); |
| 85 | + cipher.processBlock(new byte[16], 1, new byte[16], 0); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testProcessBlockWithUnsufficientOutput() { |
| 90 | + JceAesBlockCipher cipher = new JceAesBlockCipher(); |
| 91 | + cipher.init(true, new KeyParameter(new byte[16])); |
| 92 | + thrown.expect(DataLengthException.class); |
| 93 | + thrown.expectMessage("Insufficient space in 'out'"); |
| 94 | + cipher.processBlock(new byte[16], 0, new byte[16], 1); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testProcessBlock() { |
| 99 | + JceAesBlockCipher cipher = new JceAesBlockCipher(); |
| 100 | + cipher.init(true, new KeyParameter(new byte[16])); |
| 101 | + byte[] ciphertext = new byte[16]; |
| 102 | + int encrypted = cipher.processBlock(new byte[20], 0, ciphertext, 0); |
| 103 | + Assert.assertEquals(16, encrypted); |
| 104 | + |
| 105 | + cipher.init(false, new KeyParameter(new byte[16])); |
| 106 | + byte[] cleartext = new byte[16]; |
| 107 | + int decrypted = cipher.processBlock(ciphertext, 0, cleartext, 0); |
| 108 | + Assert.assertEquals(16, decrypted); |
| 109 | + Assert.assertArrayEquals(new byte[16], cleartext); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testResetBeforeInitDoesNotThrowExceptions() { |
| 114 | + JceAesBlockCipher cipher = new JceAesBlockCipher(); |
| 115 | + cipher.reset(); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testResetAfterInitDoesNotThrowExceptions() { |
| 120 | + JceAesBlockCipher cipher = new JceAesBlockCipher(); |
| 121 | + cipher.init(true, new KeyParameter(new byte[16])); |
| 122 | + cipher.reset(); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments