|
| 1 | +package org.owasp.esapi.util; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import javax.crypto.Cipher; |
| 6 | +import javax.crypto.SecretKey; |
| 7 | + |
| 8 | +import org.junit.After; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | +import org.owasp.esapi.errors.EncryptionException; |
| 12 | + |
| 13 | +public class CryptoHelperTest { |
| 14 | + |
| 15 | + @Before |
| 16 | + public void setUp() throws Exception { |
| 17 | + } |
| 18 | + |
| 19 | + @After |
| 20 | + public void tearDown() throws Exception { |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public final void testEncryptDecrypt() { |
| 25 | + try { |
| 26 | + String orig = "An Extremely Simple Test"; |
| 27 | + String ciphertext = CryptoHelper.encrypt(orig); |
| 28 | + String plaintext = CryptoHelper.decrypt(ciphertext); |
| 29 | + assertTrue( orig.equals(plaintext) ); |
| 30 | + } catch (EncryptionException e) { |
| 31 | + // OK if not covered in code coverage -- not expected. |
| 32 | + fail("Caught unexpected EncryptionException; msg was " + e.getMessage() ); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public final void testGenerateSecretKeySunnyDay() { |
| 38 | + try { |
| 39 | + SecretKey key = |
| 40 | + CryptoHelper.generateSecretKey("AES", 128); |
| 41 | + assertTrue( key.getAlgorithm().equals("AES") ); |
| 42 | + assertTrue( 128/8 == key.getEncoded().length ); |
| 43 | + } catch (EncryptionException e) { |
| 44 | + // OK if not covered in code coverage -- not expected. |
| 45 | + fail("Caught unexpected EncryptionException; msg was " + e.getMessage() ); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test(expected=EncryptionException.class) |
| 50 | + public final void testGenerateSecretKeyEncryptionException() throws EncryptionException |
| 51 | + { |
| 52 | + SecretKey key = |
| 53 | + CryptoHelper.generateSecretKey("NoSuchAlg", 128); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public final void testOverwriteByteArrayByte() { |
| 58 | + byte[] secret = "secret password".getBytes(); |
| 59 | + int len = secret.length; |
| 60 | + CryptoHelper.overwrite(secret, (byte)'x'); |
| 61 | + assertTrue( secret.length == len ); // Length unchanged |
| 62 | + assertTrue( checkByteArray(secret, (byte)'x') ); // Filled with 'x' |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public final void testCopyByteArraySunnyDay() { |
| 67 | + byte[] src = new byte[20]; |
| 68 | + fillByteArray(src, (byte)'A'); |
| 69 | + byte[] dest = new byte[20]; |
| 70 | + fillByteArray(dest, (byte)'B'); |
| 71 | + CryptoHelper.copyByteArray(src, dest); |
| 72 | + assertTrue( checkByteArray(src, (byte)'A') ); // Still filled with 'A' |
| 73 | + assertTrue( checkByteArray(dest, (byte)'A') ); // Now filled with 'B' |
| 74 | + } |
| 75 | + |
| 76 | + @Test(expected=NullPointerException.class) |
| 77 | + public final void testCopyByteArraySrcNullPointerException() { |
| 78 | + byte[] ba = new byte[16]; |
| 79 | + CryptoHelper.copyByteArray(null, ba, ba.length); |
| 80 | + } |
| 81 | + |
| 82 | + @Test(expected=NullPointerException.class) |
| 83 | + public final void testCopyByteArrayDestNullPointerException() { |
| 84 | + byte[] ba = new byte[16]; |
| 85 | + CryptoHelper.copyByteArray(ba, null, ba.length); |
| 86 | + } |
| 87 | + |
| 88 | + @Test(expected=IndexOutOfBoundsException.class) |
| 89 | + public final void testCopyByteArrayIndexOutOfBoundsException() { |
| 90 | + byte[] ba8 = new byte[8]; |
| 91 | + byte[] ba16 = new byte[16]; |
| 92 | + CryptoHelper.copyByteArray(ba8, ba16, ba16.length); |
| 93 | + } |
| 94 | + |
| 95 | + private void fillByteArray(byte[] ba, byte b) { |
| 96 | + for(int i = 0; i < ba.length; i++ ) { |
| 97 | + ba[i] = b; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private boolean checkByteArray(byte[] ba, byte b) { |
| 102 | + for(int i = 0; i < ba.length; i++ ) { |
| 103 | + if ( ba[i] != b ) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + } |
| 107 | + return true; |
| 108 | + } |
| 109 | +} |
0 commit comments