use of javax.crypto.ShortBufferException in project robovm by robovm.
the class CipherTest method testUpdatebyteArrayintintbyteArrayint.
/*
* Class under test for int update(byte[], int, int, byte[], int)
*/
public void testUpdatebyteArrayintintbyteArrayint() throws Exception {
byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
byte[] b1 = new byte[6];
Cipher c = Cipher.getInstance("DESede");
try {
c.update(b, 0, 10, b1, 5);
fail();
} catch (IllegalStateException expected) {
}
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
try {
c.update(b, 0, 10, b1, 5);
fail();
} catch (ShortBufferException expected) {
}
b1 = new byte[30];
c.update(b, 0, 10, b1, 5);
}
use of javax.crypto.ShortBufferException in project robovm by robovm.
the class CipherTest method test_.
public void test_() throws Exception {
byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
byte[] b1 = new byte[30];
Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
try {
c.update(b, 0, 10, b1);
fail();
} catch (IllegalStateException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
c.update(b, 0, 16, b1);
b1 = new byte[3];
try {
c.update(b, 3, 15, b1);
fail();
} catch (ShortBufferException expected) {
}
}
use of javax.crypto.ShortBufferException in project robovm by robovm.
the class CipherTest method test_doFinalLjava_nio_ByteBufferLjava_nio_ByteBuffer.
public void test_doFinalLjava_nio_ByteBufferLjava_nio_ByteBuffer() throws Exception {
byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
ByteBuffer bInput = ByteBuffer.allocate(64);
ByteBuffer bOutput = ByteBuffer.allocate(64);
AlgorithmParameterSpec ap = new IvParameterSpec(IV);
Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
bInput.put(b, 0, 10);
try {
c.doFinal(bInput, bOutput);
fail();
} catch (IllegalBlockSizeException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
try {
c.doFinal(bInput, bOutput);
fail();
} catch (IllegalStateException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
bInput = ByteBuffer.allocate(16);
bInput.put(b, 0, 16);
int len = c.doFinal(bInput, bOutput);
assertEquals(0, len);
c = Cipher.getInstance("DES/CBC/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
bInput = ByteBuffer.allocate(64);
try {
c.doFinal(bOutput, bInput);
fail();
} catch (BadPaddingException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
bInput.put(b, 0, 16);
try {
c.doFinal(bInput, bInput);
fail();
} catch (IllegalArgumentException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
bInput.put(b, 0, 16);
try {
c.doFinal(bInput, bOutput.asReadOnlyBuffer());
fail();
} catch (ReadOnlyBufferException expected) {
}
bInput.rewind();
bInput.put(b, 0, 16);
bOutput = ByteBuffer.allocate(8);
c = Cipher.getInstance("DESede");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
try {
c.doFinal(bInput, bOutput);
fail();
} catch (ShortBufferException expected) {
}
}
use of javax.crypto.ShortBufferException in project robovm by robovm.
the class myCipherSpi method testCipherSpi06.
/**
* Test for <code>engineDoFinal(ByteBuffer, ByteBuffer)</code> method
* Assertions:
* throws NullPointerException if one of these buffers is null;
* throws ShortBufferException is there is no space in output to hold result
*/
public void testCipherSpi06() throws BadPaddingException, ShortBufferException, IllegalBlockSizeException {
Mock_CipherSpi cSpi = new Mock_CipherSpi();
int len = 10;
byte[] bbuf = new byte[len];
for (int i = 0; i < bbuf.length; i++) {
bbuf[i] = (byte) i;
}
ByteBuffer bb1 = ByteBuffer.wrap(bbuf);
ByteBuffer bbNull = null;
try {
cSpi.engineDoFinal(bbNull, bb1);
fail("NullPointerException must be thrown");
} catch (NullPointerException e) {
}
try {
cSpi.engineDoFinal(bb1, bbNull);
fail("NullPointerException must be thrown");
} catch (NullPointerException e) {
}
ByteBuffer bb2 = ByteBuffer.allocate(len);
bb1.position(bb1.limit());
assertEquals("Incorrect result", 0, cSpi.engineDoFinal(bb1, bb2));
bb1.position(0);
bb2.position(len - 2);
try {
cSpi.engineDoFinal(bb1, bb2);
fail("ShortBufferException must be thrown. Output buffer remaining: ".concat(Integer.toString(bb2.remaining())));
} catch (ShortBufferException e) {
}
int pos = 5;
bb1.position(pos);
bb2.position(0);
assertTrue("Incorrect result", cSpi.engineDoFinal(bb1, bb2) > 0);
}
use of javax.crypto.ShortBufferException in project robovm by robovm.
the class myCipherSpi method testCipherSpi05.
/**
* Test for <code>engineUpdate(ByteBuffer, ByteBuffer)</code> method
* Assertions:
* throws NullPointerException if one of these buffers is null;
* throws ShortBufferException is there is no space in output to hold result
*/
public void testCipherSpi05() throws ShortBufferException {
Mock_CipherSpi cSpi = new Mock_CipherSpi();
byte[] bb = { (byte) 0, (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6, (byte) 7, (byte) 8, (byte) 9, (byte) 10 };
int pos = 5;
int len = bb.length;
ByteBuffer bbNull = null;
ByteBuffer bb1 = ByteBuffer.allocate(len);
bb1.put(bb);
bb1.position(0);
try {
cSpi.engineUpdate(bbNull, bb1);
fail("NullPointerException must be thrown");
} catch (NullPointerException e) {
}
try {
cSpi.engineUpdate(bb1, bbNull);
fail("NullPointerException must be thrown");
} catch (NullPointerException e) {
}
ByteBuffer bb2 = ByteBuffer.allocate(bb.length);
bb1.position(len);
assertEquals("Incorrect number of stored bytes", 0, cSpi.engineUpdate(bb1, bb2));
bb1.position(0);
bb2.position(len - 2);
try {
cSpi.engineUpdate(bb1, bb2);
fail("ShortBufferException bust be thrown. Output buffer remaining: ".concat(Integer.toString(bb2.remaining())));
} catch (ShortBufferException e) {
}
bb1.position(10);
bb2.position(0);
assertTrue("Incorrect number of stored bytes", cSpi.engineUpdate(bb1, bb2) > 0);
bb1.position(bb.length);
cSpi.engineUpdate(bb1, bb2);
bb1.position(pos);
bb2.position(0);
int res = cSpi.engineUpdate(bb1, bb2);
assertTrue("Incorrect result", res > 0);
}
Aggregations