@Test public void testOverlappingDecode() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt, crypt2; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); crypt2 = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); crypt2 = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] originalPlaintext = Hex.decode(plainText[i]); byte[] originalCiphertext = crypt2.encryptCopy(originalPlaintext); byte[] buf = new byte[originalPlaintext.length+1]; System.arraycopy(originalCiphertext, 0, buf, 0, originalCiphertext.length); crypt.decrypt(buf, 0, originalCiphertext.length, buf, 1); assertArrayEquals(originalPlaintext, Arrays.copyOfRange(buf, 1, buf.length)); } }
@Test public void testOverlappingEncode() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt, crypt2; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); crypt2 = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); crypt2 = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] originalPlaintext = Hex.decode(plainText[i]); byte[] originalCiphertext = crypt2.encryptCopy(originalPlaintext); byte[] buf = new byte[originalPlaintext.length+1]; System.arraycopy(originalPlaintext, 0, buf, 0, originalPlaintext.length); crypt.encrypt(buf, 0, originalPlaintext.length, buf, 1); assertArrayEquals(originalCiphertext, Arrays.copyOfRange(buf, 1, buf.length)); } }
@Test public void testEncryptByteArrayIntIntNullInput() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] nullArray = null; try{ crypt.encryptCopy(nullArray, 0, plainText[i].length()); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "NullPointerException"); }catch(IllegalArgumentException e) { }catch(NullPointerException e){} } }
@Test (expected = UnsupportedTypeException.class) public void testSetIVIvParameterSpecUnsupportedTypeException() throws GeneralSecurityException { int i = 0; CryptByteBuffer crypt = new CryptByteBuffer(cipherTypes[i], keys[i]); crypt.setIV(new IvParameterSpec(ivs[4])); fail("Expected UnsupportedTypeException"); }
@Test public void testEncryptByteArrayNullInput() throws GeneralSecurityException{ for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] nullArray = null; try{ crypt.encryptCopy(nullArray); fail("CryptByteBufferType: "+type.name()+": Expected NullPointerException"); }catch(NullPointerException e){} } }
@Test public void testDecryptByteArrayNullInput() throws GeneralSecurityException{ for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] nullArray = null; try{ crypt.decryptCopy(nullArray); fail("CryptByteBufferType: "+type.name()+": Expected NullPointerException"); }catch(NullPointerException e){} } }
@Test public void testDecryptByteArrayIntIntNullInput() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] nullArray = null; try{ crypt.decryptCopy(nullArray, 0, plainText[i].length()); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "NullPointerException"); }catch(NullPointerException e) { } catch(IllegalArgumentException e){ } } }
@Test public void testEncryptByteArrayIntIntLengthOutOfBounds() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } try{ crypt.encryptCopy(Hex.decode(plainText[i]), 0, plainText[i].length()+3); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "ArrayIndexOutOfBoundsException"); }catch(IllegalArgumentException e) { } catch(IndexOutOfBoundsException e){} } }
@Test public void testEncryptByteArrayIntIntOffsetOutOfBounds() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } try{ crypt.encryptCopy(Hex.decode(plainText[i]), -3, plainText[i].length()-3); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "ArrayIndexOutOfBoundsException"); }catch(IllegalArgumentException e) { }catch(IndexOutOfBoundsException e){ } } }
@Test public void testDecryptByteArrayIntIntOffsetOutOfBounds() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } try{ crypt.decryptCopy(Hex.decode(plainText[i]), -3, plainText[i].length()-3); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "ArrayIndexOutOfBoundsException"); }catch(IllegalArgumentException e) { } catch (IndexOutOfBoundsException e){ } } }
@Test public void testGenIV() throws InvalidKeyException, InvalidAlgorithmParameterException { int i = 4; CryptByteBuffer crypt = new CryptByteBuffer(cipherTypes[i], keys[i], ivs[i]); assertNotNull(crypt.genIV()); }
@Test (expected = UnsupportedTypeException.class) public void testGenIVUnsupportedTypeException() throws GeneralSecurityException { int i = 1; CryptByteBuffer crypt = new CryptByteBuffer(cipherTypes[i], keys[i]); crypt.genIV(); fail("Expected UnsupportedTypeException"); }
@Test public void testDecryptByteArrayIntIntLengthOutOfBounds() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } try{ crypt.decryptCopy(Hex.decode(plainText[i]), 0, plainText[i].length()+3); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "ArrayIndexOutOfBoundsException"); }catch(IllegalArgumentException e) { } catch (IndexOutOfBoundsException e){ } } }
@Test public void testSetIVIvParameterSpecNullInput() throws InvalidKeyException, InvalidAlgorithmParameterException { IvParameterSpec nullInput = null; int i = 4; CryptByteBuffer crypt = new CryptByteBuffer(cipherTypes[i], keys[i], ivs[i]); try{ crypt.setIV(nullInput); fail("Expected InvalidAlgorithmParameterException"); } catch (InvalidAlgorithmParameterException e){} }
@Test public void testGenIVLength() throws InvalidKeyException, InvalidAlgorithmParameterException { int i = 4; CryptByteBuffer crypt = new CryptByteBuffer(cipherTypes[i], keys[i], ivs[i]); assertEquals(crypt.genIV().getIV().length, cipherTypes[i].ivSize.intValue()); }
@Test public void testSuccessfulRoundTripInPlaceOffset() throws GeneralSecurityException { int header = 5; int footer = 5; for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] originalPlaintext = Hex.decode(plainText[i]); byte[] buffer = new byte[header+originalPlaintext.length+footer]; byte[] copyBuffer = buffer.clone(); System.arraycopy(originalPlaintext, 0, buffer, header, originalPlaintext.length); crypt.encrypt(buffer, footer, originalPlaintext.length); assertTrue(!Arrays.equals(buffer, copyBuffer)); crypt.decrypt(buffer, footer, originalPlaintext.length); assertArrayEquals("CryptByteBufferType: "+type.name(), originalPlaintext, Arrays.copyOfRange(buffer, footer, footer+originalPlaintext.length)); } }
@Test public void testSuccessfulRoundTripByteArray() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] decipheredtext = crypt.decryptCopy(crypt.encryptCopy(Hex.decode(plainText[i]))); assertArrayEquals("CryptByteBufferType: "+type.name(), Hex.decode(plainText[i]), decipheredtext); } }
@Test public void testGetIV() throws InvalidKeyException, InvalidAlgorithmParameterException { int i = 4; CryptByteBuffer crypt = new CryptByteBuffer(cipherTypes[i], keys[i], ivs[i]); assertArrayEquals(crypt.getIV().getIV(), ivs[i]); }
@Test public void testSetIVIvParameterSpec() throws InvalidKeyException, InvalidAlgorithmParameterException { int i = 4; CryptByteBuffer crypt = new CryptByteBuffer(cipherTypes[i], keys[i], ivs[i]); crypt.genIV(); crypt.setIV(new IvParameterSpec(ivs[i])); assertArrayEquals(ivs[i], crypt.getIV().getIV()); }
@Test public void testSuccessfulRoundTripInPlace() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] buffer = Hex.decode(plainText[i]); byte[] plaintextCopy = buffer.clone(); crypt.encrypt(buffer, 0, buffer.length); assertTrue(!Arrays.equals(buffer, plaintextCopy)); crypt.decrypt(buffer, 0, buffer.length); assertArrayEquals("CryptByteBufferType: "+type.name(), plaintextCopy, buffer); } }