Test all primitive operations.
// preparation
boolean noSuchAlg = false;
boolean noSuchPad = false;
boolean invKey = false;
boolean illState = false;
boolean shortBuf = false;
boolean illBlSize = false;
boolean badPad = false;
int outLen = 0;
outBuf1 = new byte[PLAIN_TEXT.length];
outBuf2 = new byte[PLAIN_TEXT.length];
assertNotNull("no memory for outBuf1", outBuf1);
assertNotNull("no memory for outBuf2", outBuf2);
cipher = null;
key = null;
// create cipher for not supported algorithm/mode
// -> NoSuchAlgorithmException should be thrown
try {
cipher = Cipher.getInstance(null);
} catch (NoSuchAlgorithmException e) {
noSuchAlg = true;
} catch (NoSuchPaddingException e) {
noSuchPad = true;
}
assertFalse("Cipher.getInstance(null): NoSuchPaddingException",
noSuchPad);
assertTrue("Cipher.getInstance(null): no NoSuchAlgorithmException",
noSuchAlg);
assertNull("Cipher.getInstance(null): returned not null", cipher);
noSuchAlg = false;
try {
cipher = Cipher.getInstance("");
} catch (NoSuchAlgorithmException e) {
noSuchAlg = true;
} catch (NoSuchPaddingException e) {
noSuchPad = true;
}
assertFalse("Cipher.getInstance(): NoSuchPaddingException",
noSuchPad);
assertTrue("Cipher.getInstance(): no NoSuchAlgorithmException",
noSuchAlg);
assertNull("Cipher.getInstance(): returned not null", cipher);
noSuchAlg = false;
try {
cipher = Cipher.getInstance("///");
} catch (NoSuchAlgorithmException e) {
noSuchAlg = true;
} catch (NoSuchPaddingException e) {
noSuchPad = true;
}
assertFalse("Cipher.getInstance(///): NoSuchPaddingException",
noSuchPad);
assertTrue("Cipher.getInstance(///): no NoSuchAlgorithmException",
noSuchAlg);
assertNull("Cipher.getInstance(///): returned not null", cipher);
noSuchAlg = false;
try {
cipher = Cipher.getInstance("a/b/c");
} catch (NoSuchAlgorithmException e) {
noSuchAlg = true;
} catch (NoSuchPaddingException e) {
noSuchPad = true;
}
assertFalse("Cipher.getInstance(a/b/c): NoSuchPaddingException",
noSuchPad);
assertTrue("Cipher.getInstance(a/b/c): no NoSuchAlgorithmException",
noSuchAlg);
assertNull("Cipher.getInstance(a/b/c): returned not null", cipher);
noSuchAlg = false;
try {
cipher = Cipher.getInstance("a/d/f/t/y");
} catch (NoSuchAlgorithmException e) {
noSuchAlg = true;
} catch (NoSuchPaddingException e) {
noSuchPad = true;
}
assertFalse("Cipher.getInstance(a/d/f/t/y): NoSuchPaddingException",
noSuchPad);
assertTrue("Cipher.getInstance(a/d/f/t/y): no NoSuchAlgorithmException",
noSuchAlg);
assertNull("Cipher.getInstance(a/d/f/t/y): returned not null", cipher);
noSuchAlg = false;
try {
cipher = Cipher.getInstance(ALGORITHM + "/zzz");
} catch (NoSuchAlgorithmException e) {
noSuchAlg = true;
} catch (NoSuchPaddingException e) {
noSuchPad = true;
}
assertFalse("Cipher.getInstance(ALG+/zzz): NoSuchPaddingException",
noSuchPad);
assertTrue("Cipher.getInstance(ALG+/zzz): no NoSuchAlgorithmException",
noSuchAlg);
assertNull("Cipher.getInstance(ALG+/zzz): returned not null", cipher);
noSuchAlg = false;
try {
cipher = Cipher.getInstance(ALGORITHM + MODE + PADDING + "/xxx");
} catch (NoSuchAlgorithmException e) {
noSuchAlg = true;
} catch (NoSuchPaddingException e) {
noSuchPad = true;
}
assertFalse("Cipher.getInstance(+/xxx): NoSuchPaddingException",
noSuchPad);
assertTrue("Cipher.getInstance(+/xxx): no NoSuchAlgorithmException",
noSuchAlg);
assertNull("Cipher.getInstance(+/xxx): returned not null", cipher);
noSuchAlg = false;
// create cipher with not supported padding
// -> NoSuchPaddingException should be thrown
try {
cipher = Cipher.getInstance(ALGORITHM + MODE + "/yyy");
} catch (NoSuchAlgorithmException e) {
noSuchAlg = true;
} catch (NoSuchPaddingException e) {
noSuchPad = true;
}
assertFalse("Cipher.getInstance(+/yyy): NoSuchAlgorithmException",
noSuchAlg);
assertTrue("Cipher.getInstance(+/yyy): no NoSuchPaddingException",
noSuchPad);
assertNull("Cipher.getInstance(+/yyy): returned not null", cipher);
noSuchPad = false;
// create cipher
try {
cipher = Cipher.getInstance(ALGORITHM + MODE + PADDING);
} catch (NoSuchAlgorithmException e) {
noSuchAlg = true;
} catch (NoSuchPaddingException e) {
noSuchPad = true;
}
assertFalse("NoSuchAlgorithmException exception", noSuchAlg);
assertFalse("NoSuchPaddingException exception", noSuchPad);
assertNotNull("Cipher.getInstance returned null", cipher);
// create key
key = new SecretKeySpec(SECRET_KEY, 0, SECRET_KEY.length, ALGORITHM);
assertNotNull("SecretKeySpec returned null", key);
assertEquals("wrong algorithm name", ALGORITHM.toUpperCase(),
key.getAlgorithm().toUpperCase());
assertTrue("wrong secret data", cmpArr(SECRET_KEY, key.getEncoded()));
// update() before init() -> IllegalStateException should be thrown
try {
outLen = cipher.update(PLAIN_TEXT, 0, PLAIN_TEXT.length,
outBuf1, 0);
} catch (IllegalStateException e) {
illState = true;
} catch (ShortBufferException e) {
shortBuf = true;
}
assertFalse("ShortBufferException exception", shortBuf);
assertTrue("no IllegalStateException after update()", illState);
illState = false;
// doFinal() before init() -> IllegalStateException should be thrown
try {
outLen = cipher.doFinal(PLAIN_TEXT, 0, PLAIN_TEXT.length,
outBuf2, 0);
} catch (IllegalStateException e) {
illState = true;
} catch (ShortBufferException e) {
shortBuf = true;
} catch (IllegalBlockSizeException e) {
illBlSize = true;
} catch (BadPaddingException e) {
badPad = true;
}
assertFalse("ShortBufferException exception", shortBuf);
assertFalse("IllegalBlockSizeException exception", illBlSize);
assertFalse("BadPaddingException exception", badPad);
assertTrue("no IllegalStateException after doFinal()", illState);
illState = false;
// init cipher for encryption
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (InvalidKeyException e) {
invKey = true;
}
assertFalse("InvalidKeyException exception", invKey);
// encrypt using update()
try {
outLen = cipher.update(PLAIN_TEXT, 0, PLAIN_TEXT.length,
outBuf1, 0);
} catch (IllegalStateException e) {
illState = true;
} catch (ShortBufferException e) {
shortBuf = true;
}
assertFalse("ShortBufferException exception", shortBuf);
assertFalse("IllegalStateException exception", illState);
assertEquals("outLen1 is not correct", PLAIN_TEXT.length, outLen);
// encrypt using doFinal()
try {
outLen = cipher.doFinal(PLAIN_TEXT, 0, PLAIN_TEXT.length,
outBuf2, 0);
} catch (IllegalStateException e) {
illState = true;
} catch (ShortBufferException e) {
shortBuf = true;
} catch (IllegalBlockSizeException e) {
illBlSize = true;
} catch (BadPaddingException e) {
badPad = true;
}
assertFalse("ShortBufferException exception", shortBuf);
assertFalse("IllegalBlockSizeException exception", illBlSize);
assertFalse("BadPaddingException exception", badPad);
assertFalse("IllegalStateException exception", illState);
assertEquals("outLen2 is not correct", PLAIN_TEXT.length, outLen);
// check encryption results
assertTrue("first part of encryption is wrong",
cmpArr(CIPHER_TEXT_PART_1, outBuf1));
assertTrue("second part of encryption is wrong",
cmpArr(CIPHER_TEXT_PART_2, outBuf2));
// init cipher for decryption
try {
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (InvalidKeyException e) {
invKey = true;
}
assertFalse("InvalidKeyException exception", invKey);
// decrypt using update()
try {
outLen = cipher.update(CIPHER_TEXT_PART_1, 0,
CIPHER_TEXT_PART_1.length, outBuf1, 0);
} catch (IllegalStateException e) {
illState = true;
} catch (ShortBufferException e) {
shortBuf = true;
}
assertFalse("ShortBufferException exception", shortBuf);
assertFalse("IllegalStateException exception", illState);
assertEquals("outLen1 is not correct", PLAIN_TEXT.length, outLen);
// decrypt using doFinal()
try {
outLen = cipher.doFinal(CIPHER_TEXT_PART_2, 0,
CIPHER_TEXT_PART_2.length, outBuf2, 0);
} catch (IllegalStateException e) {
illState = true;
} catch (ShortBufferException e) {
shortBuf = true;
} catch (IllegalBlockSizeException e) {
illBlSize = true;
} catch (BadPaddingException e) {
badPad = true;
}
assertFalse("ShortBufferException exception", shortBuf);
assertFalse("IllegalBlockSizeException exception", illBlSize);
assertFalse("BadPaddingException exception", badPad);
assertFalse("IllegalStateException exception", illState);
assertEquals("outLen2 is not correct", PLAIN_TEXT.length, outLen);
// check decryption results
assertTrue("first part of decryption is wrong",
cmpArr(PLAIN_TEXT, outBuf1));
assertTrue("second part of decryption is wrong",
cmpArr(PLAIN_TEXT, outBuf2));
// incorrect outBuf size -> ShortBufferException should be thrown
try {
outLen = cipher.update(PLAIN_TEXT, 0, PLAIN_TEXT.length,
outBuf1, 1);
} catch (IllegalStateException e) {
illState = true;
} catch (ShortBufferException e) {
shortBuf = true;
}
assertFalse("IllegalStateException exception", illState);
assertTrue("no ShortBufferException after update()", shortBuf);
shortBuf = false;
try {
outLen = cipher.doFinal(PLAIN_TEXT, 0, PLAIN_TEXT.length,
outBuf2, 1);
} catch (IllegalStateException e) {
illState = true;
} catch (ShortBufferException e) {
shortBuf = true;
} catch (IllegalBlockSizeException e) {
illBlSize = true;
} catch (BadPaddingException e) {
badPad = true;
}
assertFalse("IllegalStateException exception", illState);
assertFalse("IllegalBlockSizeException exception", illBlSize);
assertFalse("BadPaddingException exception", badPad);
assertTrue("no ShortBufferException after doFinal()", shortBuf);
shortBuf = false;