From dcdee8d21a8b429f3b7914787285aa1f72ac8bd4 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Sat, 5 Aug 2017 17:16:04 +0200 Subject: [PATCH] More cipher tests --- .../smackx/ciphers/AesGcmNoPaddingTest.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/ciphers/AesGcmNoPaddingTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/ciphers/AesGcmNoPaddingTest.java index aef86a359..980229959 100644 --- a/smack-experimental/src/test/java/org/jivesoftware/smackx/ciphers/AesGcmNoPaddingTest.java +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/ciphers/AesGcmNoPaddingTest.java @@ -59,8 +59,18 @@ public class AesGcmNoPaddingTest extends SmackTestSuite { assertEquals(256, aes256.getLength()); } + @Test(expected = NoSuchAlgorithmException.class) + public void invalidEncryptionCipher() throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException, InvalidKeyException, InvalidAlgorithmParameterException { + AesGcmNoPadding.createEncryptionKey("invalid"); + } + + @Test(expected = NoSuchAlgorithmException.class) + public void invalidDecryptionCipher() throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException, InvalidKeyException, InvalidAlgorithmParameterException { + AesGcmNoPadding.createDecryptionKey("invalid", null); + } + @Test - public void encryptionTest() throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException, InvalidKeyException, InvalidAlgorithmParameterException, BadPaddingException, IllegalBlockSizeException { + public void encryption128Test() throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException, InvalidKeyException, InvalidAlgorithmParameterException, BadPaddingException, IllegalBlockSizeException { AesGcmNoPadding aes1 = AesGcmNoPadding.createEncryptionKey(Aes128GcmNoPadding.NAMESPACE); AesGcmNoPadding aes2 = AesGcmNoPadding.createDecryptionKey(Aes128GcmNoPadding.NAMESPACE, aes1.getKeyAndIv()); @@ -73,4 +83,19 @@ public class AesGcmNoPaddingTest extends SmackTestSuite { byte[] dec = aes2.getCipher().doFinal(enc); assertTrue(Arrays.equals(dec, data)); } + + @Test + public void encryption256Test() throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException, InvalidKeyException, InvalidAlgorithmParameterException, BadPaddingException, IllegalBlockSizeException { + AesGcmNoPadding aes1 = AesGcmNoPadding.createEncryptionKey(Aes256GcmNoPadding.NAMESPACE); + AesGcmNoPadding aes2 = AesGcmNoPadding.createDecryptionKey(Aes256GcmNoPadding.NAMESPACE, aes1.getKeyAndIv()); + + byte[] data = new byte[4096]; + new Random().nextBytes(data); + + byte[] enc = aes1.getCipher().doFinal(data); + assertFalse(Arrays.equals(data, enc)); + + byte[] dec = aes2.getCipher().doFinal(enc); + assertTrue(Arrays.equals(dec, data)); + } }