From 6166e776461c4474fd9d5795dec1245c8e831011 Mon Sep 17 00:00:00 2001 From: Dave Date: Sat, 27 Jun 2015 23:46:46 +0200 Subject: [PATCH] unit tests --- src/nodash/test/NoUserTest.java | 9 +- src/nodash/test/NoUtilTest.java | 326 ++++++++++++++++++++++++++++++++ 2 files changed, 334 insertions(+), 1 deletion(-) create mode 100644 src/nodash/test/NoUtilTest.java diff --git a/src/nodash/test/NoUserTest.java b/src/nodash/test/NoUserTest.java index 2fa5902..0fe8594 100644 --- a/src/nodash/test/NoUserTest.java +++ b/src/nodash/test/NoUserTest.java @@ -8,12 +8,19 @@ import java.util.Arrays; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; +import nodash.core.NoCore; import nodash.models.NoUser; import org.apache.commons.codec.binary.Base64; +import org.junit.Before; import org.junit.Test; public class NoUserTest { + + @Before + public void setup() { + NoCore.setup(); + } @Test public void testNoUser() { @@ -77,7 +84,7 @@ public class NoUserTest { } catch (IllegalBlockSizeException e) { fail("IllegalBlockSizeException encountered."); } catch (BadPaddingException e) { - fail("BadPaddingException encountered."); + // Do nothing, correct } catch (ClassNotFoundException e) { fail("ClassNotFoundException encountered."); } catch (IOException e) { diff --git a/src/nodash/test/NoUtilTest.java b/src/nodash/test/NoUtilTest.java new file mode 100644 index 0000000..3993325 --- /dev/null +++ b/src/nodash/test/NoUtilTest.java @@ -0,0 +1,326 @@ +package nodash.test; + +import static org.junit.Assert.*; + +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.util.Arrays; + +import javax.crypto.BadPaddingException; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; + +import nodash.core.NoCore; +import nodash.core.NoUtil; +import nodash.exceptions.NoDashFatalException; + +import org.junit.Before; +import org.junit.Test; + +public class NoUtilTest { + + @Before + public void setup() throws NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException { + NoCore.setup(); + } + + @Test + public void testBytesToChars() { + byte[] bytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; + char[] chars = NoUtil.bytesToChars(bytes); + assertTrue(Arrays.equals(bytes, NoUtil.charsToBytes(chars))); + + try { + NoUtil.bytesToChars(null); + fail("Allowed null parameter without thrown exception."); + } catch (NullPointerException e) { + // Do nothing, correct + } + } + + @Test + public void testCharsToBytes() { + char[] chars = {'s', 'o', 'm', 'e', 'c', 'h', 'a', 'r', 's'}; + byte[] bytes = NoUtil.charsToBytes(chars); + assertTrue(Arrays.equals(chars, NoUtil.bytesToChars(bytes))); + + try { + NoUtil.charsToBytes(null); + fail("Allowed null parameter without thrown exception."); + } catch (NullPointerException e) { + // Do nothing, correct + } + } + + @Test + public void testWipeBytes() { + final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; + final byte[] expectedBlankBytes = {'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'}; + byte[] bytes = Arrays.copyOf(originalBytes, originalBytes.length); + + NoUtil.wipeBytes(bytes); + assertFalse(Arrays.equals(originalBytes, bytes)); + assertTrue(Arrays.equals(bytes, expectedBlankBytes)); + + try { + NoUtil.wipeBytes(null); + fail("Allowed null parameter without thrown exception."); + } catch (NullPointerException e) { + // Do nothing, correct + } + } + + @Test + public void testWipeChars() { + final char[] originalChars = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; + final char[] expectedBlankChars = {'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'}; + char[] chars = Arrays.copyOf(originalChars, originalChars.length); + + NoUtil.wipeChars(chars); + assertFalse(Arrays.equals(originalChars, chars)); + assertTrue(Arrays.equals(chars, expectedBlankChars)); + + try { + NoUtil.wipeChars(null); + fail("Allowed null parameter without thrown exception."); + } catch (NullPointerException e) { + // Do nothing, correct + } + } + + @Test + public void testGetHashFromByteArray() { + byte[] bytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; + byte[] hash = NoUtil.getHashFromByteArray(bytes); + + assertNotNull(hash); + assertEquals(hash.length, 64); + + byte[] bytesEqual = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; + byte[] bytesNotEqual = {'S', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; + + assertTrue(Arrays.equals(hash, NoUtil.getHashFromByteArray(bytesEqual))); + assertFalse(Arrays.equals(hash, NoUtil.getHashFromByteArray(bytesNotEqual))); + + try { + NoUtil.getHashFromByteArray(null); + fail("Allowed null parameter without thrown exception."); + } catch (NullPointerException e) { + // Do nothing, correct + } + } + + @Test + public void testByteKeyEncryptionDecryptionAES() { + final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; + final byte[] originalByteKey = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; + + byte[] bytes = Arrays.copyOf(originalBytes, originalBytes.length); + byte[] byteKey = Arrays.copyOf(originalByteKey, originalByteKey.length); + + byte[] encryptedByByteKey = NoUtil.encrypt(bytes, byteKey); + + try { + NoUtil.decrypt(encryptedByByteKey, new byte[] {'b', 'a', 'd', 'k', 'e', 'y'}); + fail("Did not throw BadPaddingException while decrypting with bad key."); + } catch (IllegalBlockSizeException e) { + fail("Did not throw BadPaddingException while decrypting with bad key."); + } catch (BadPaddingException e) { + // Do nothing, correct + } + + byte[] decryptedByByteKey; + try { + decryptedByByteKey = NoUtil.decrypt(encryptedByByteKey, byteKey); + } catch (IllegalBlockSizeException | BadPaddingException e) { + fail("Encountered an error of type " + e.getClass().getSimpleName() + + " while decrypting with valid password"); + decryptedByByteKey = new byte[] {}; + } + + assertTrue(Arrays.equals(originalBytes, decryptedByByteKey)); + + byte[] nullByte = null; + try { + NoUtil.decrypt(nullByte, nullByte); + fail("Allowed null parameter without thrown exception."); + } catch (NullPointerException e) { + // Do nothing, correct + } catch (IllegalBlockSizeException e) { + fail("Allowed null parameter without thrown exception."); + } catch (BadPaddingException e) { + fail("Allowed null parameter without thrown exception."); + } + + try { + NoUtil.decrypt(bytes, nullByte); + fail("Allowed null parameter without thrown exception."); + } catch (NullPointerException e) { + // Do nothing, correct + } catch (IllegalBlockSizeException e) { + fail("Allowed null parameter without thrown exception."); + } catch (BadPaddingException e) { + fail("Allowed null parameter without thrown exception."); + } + + try { + NoUtil.decrypt(nullByte, bytes); + fail("Allowed null parameter without thrown exception."); + } catch (NullPointerException e) { + // Do nothing, correct + } catch (IllegalBlockSizeException e) { + fail("Allowed null parameter without thrown exception."); + } catch (BadPaddingException e) { + fail("Allowed null parameter without thrown exception."); + } + } + + @Test + public void testCharKeyEncryptionDecryptionAES() { + final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; + final char[] originalCharKey = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; + + byte[] bytes = Arrays.copyOf(originalBytes, originalBytes.length); + char[] charKey = Arrays.copyOf(originalCharKey, originalCharKey.length); + + byte[] encryptedByCharKey = NoUtil.encrypt(bytes, charKey); + + try { + NoUtil.decrypt(encryptedByCharKey, new byte[] {'b', 'a', 'd', 'k', 'e', 'y'}); + fail("Did not throw BadPaddingException while decrypting with bad key."); + } catch (IllegalBlockSizeException e) { + fail("Did not throw BadPaddingException while decrypting with bad key."); + } catch (BadPaddingException e) { + // Do nothing, correct + } + + byte[] decryptedByCharKey; + try { + decryptedByCharKey = NoUtil.decrypt(encryptedByCharKey, charKey); + } catch (IllegalBlockSizeException | BadPaddingException e) { + fail("Encountered an error of type " + e.getClass().getSimpleName() + + " while decrypting with valid password"); + decryptedByCharKey = new byte[] {}; + } + + assertTrue(Arrays.equals(originalBytes, decryptedByCharKey)); + + byte[] nullByte = null; + byte[] nullChar = null; + try { + NoUtil.decrypt(nullByte, nullChar); + fail("Allowed null parameter without thrown exception."); + } catch (NullPointerException e) { + // Do nothing, correct + } catch (IllegalBlockSizeException e) { + fail("Allowed null parameter without thrown exception."); + } catch (BadPaddingException e) { + fail("Allowed null parameter without thrown exception."); + } + + try { + NoUtil.decrypt(bytes, nullChar); + fail("Allowed null parameter without thrown exception."); + } catch (NullPointerException e) { + // Do nothing, correct + } catch (IllegalBlockSizeException e) { + fail("Allowed null parameter without thrown exception."); + } catch (BadPaddingException e) { + fail("Allowed null parameter without thrown exception."); + } + + try { + NoUtil.decrypt(nullByte, new char[] {'c', 'h', 'a', 'r'}); + fail("Allowed null parameter without thrown exception."); + } catch (NullPointerException e) { + // Do nothing, correct + } catch (IllegalBlockSizeException e) { + fail("Allowed null parameter without thrown exception."); + } catch (BadPaddingException e) { + fail("Allowed null parameter without thrown exception."); + } + } + + @Test + public void testNoKeyEncryptionDecryptionAES() { + final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; + byte[] bytes = Arrays.copyOf(originalBytes, originalBytes.length); + + byte[] encrypted = NoUtil.encrypt(bytes); + + byte[] decrypted; + try { + decrypted = NoUtil.decrypt(encrypted); + } catch (IllegalBlockSizeException | BadPaddingException e) { + fail("Encountered an error of type " + e.getClass().getSimpleName() + + " while decrypting with valid password"); + decrypted = new byte[] {}; + } + + assertTrue(Arrays.equals(originalBytes, decrypted)); + + try { + NoUtil.decrypt(null); + fail("Allowed null parameter without thrown exception."); + } catch (NullPointerException e) { + // Do nothing, correct + } catch (IllegalBlockSizeException e) { + fail("Allowed null parameter without thrown exception."); + } catch (BadPaddingException e) { + fail("Allowed null parameter without thrown exception."); + } + } + + @Test + public void testEncryptionDecryptionRSA() { + KeyPairGenerator kpg; + try { + kpg = KeyPairGenerator.getInstance(NoUtil.KEYPAIR_ALGORITHM); + } catch (NoSuchAlgorithmException e) { + throw new NoDashFatalException("Value for KEYPAIR_ALGORITHM is not valid.", e); + } + + try { + kpg.initialize(NoUtil.RSA_STRENGTH, + SecureRandom.getInstance(NoUtil.SECURERANDOM_ALGORITHM, NoUtil.SECURERANDOM_PROVIDER)); + } catch (NoSuchAlgorithmException e) { + throw new NoDashFatalException("Value for SECURERANDOM_ALGORITHM not valid.", e); + } catch (NoSuchProviderException e) { + throw new NoDashFatalException("Value for SECURERANDOM_PROVIDER not valid.", e); + } + + KeyPair keyPair = kpg.generateKeyPair(); + KeyPair keyPair2 = kpg.generateKeyPair(); + + final byte[] originalBytes = {'s', 'o', 'm', 'e', 'b', 'y', 't', 'e', 's'}; + + byte[] bytes = Arrays.copyOf(originalBytes, originalBytes.length); + + byte[] encrypted = NoUtil.encryptRSA(bytes, keyPair.getPublic()); + try { + NoUtil.decryptRSA(encrypted, keyPair2.getPrivate()); + fail("Did not throw exception with incorrect private key."); + } catch (InvalidKeyException e) { + fail("Did not throw exception with incorrect private key."); + } catch (IllegalBlockSizeException e) { + fail("Did not throw exception with incorrect private key."); + } catch (BadPaddingException e) { + // Do nothing, correct + } + + byte[] decrypted; + try { + decrypted = NoUtil.decryptRSA(encrypted, keyPair.getPrivate()); + } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) { + fail("Threw exception with correct private key."); + decrypted = new byte[] {}; + } + + assertTrue(Arrays.equals(originalBytes, decrypted)); + } + +}