#2 - Added test to test the cypto library
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package net.locusworks.crypto.tests;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.locusworks.crypto.AES;
|
||||
|
||||
public class AESEncryptionTest {
|
||||
|
||||
@Test
|
||||
public void testEncryption() {
|
||||
try {
|
||||
String encrypted = AES.createInstance().encrypt("hello world");
|
||||
Assert.assertTrue(String.format("Encrypted String is not blank? :%s", encrypted), !StringUtils.isBlank(encrypted));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace(System.err);
|
||||
Assert.fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryption() {
|
||||
String testString ="hello world";
|
||||
try {
|
||||
AES aes = AES.createInstance();
|
||||
String encrypted = aes.encrypt(testString);
|
||||
Assert.assertTrue(String.format("Encrypted String is not blank? :%s", encrypted), !StringUtils.isBlank(encrypted));
|
||||
|
||||
String decrypted = aes.decrypt(encrypted);
|
||||
Assert.assertTrue(String.format("Decrypted String is not blank? :%s", decrypted), !StringUtils.isBlank(encrypted));
|
||||
|
||||
Assert.assertTrue("Test String and Original String the same? :%s", testString.equals(decrypted));
|
||||
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace(System.err);
|
||||
Assert.fail();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
35
src/test/java/net/locusworks/crypto/tests/HashSaltTest.java
Normal file
35
src/test/java/net/locusworks/crypto/tests/HashSaltTest.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package net.locusworks.crypto.tests;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.locusworks.crypto.HashSalt;
|
||||
|
||||
|
||||
public class HashSaltTest {
|
||||
|
||||
private static String samplePassword="Hello World";
|
||||
|
||||
@Test
|
||||
public void testEncryption() {
|
||||
try {
|
||||
String hashSalt = HashSalt.createHash(samplePassword);
|
||||
Assert.assertTrue(String.format("Encrypted String is not blank? :%s", hashSalt), !StringUtils.isBlank(hashSalt));
|
||||
} catch(Exception ex) {
|
||||
Assert.fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryption() {
|
||||
try {
|
||||
String hashSalt = HashSalt.createHash(samplePassword);
|
||||
boolean decrypted = HashSalt.validatePassword(samplePassword, hashSalt);
|
||||
Assert.assertTrue("Test String and Original String the same? :%s", decrypted);
|
||||
} catch(Exception ex) {
|
||||
Assert.fail();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
38
src/test/java/net/locusworks/crypto/tests/HashUtilsTest.java
Normal file
38
src/test/java/net/locusworks/crypto/tests/HashUtilsTest.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package net.locusworks.crypto.tests;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.locusworks.crypto.utils.HashUtils;
|
||||
|
||||
public class HashUtilsTest {
|
||||
|
||||
private static final String TEST_STRING = "Hello World";
|
||||
|
||||
@Test
|
||||
public void testMD5() throws Exception {
|
||||
String digestUtilsMD5 = DigestUtils.md5Hex(TEST_STRING.getBytes());
|
||||
String hashUtilsMD5 = HashUtils.hash("MD5", TEST_STRING);
|
||||
|
||||
assertTrue(digestUtilsMD5.equals(hashUtilsMD5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSHA1() throws Exception {
|
||||
String digestUtilsMD5 = DigestUtils.sha1Hex(TEST_STRING.getBytes());
|
||||
String hashUtilsMD5 = HashUtils.hash("SHA-1", TEST_STRING);
|
||||
|
||||
assertTrue(digestUtilsMD5.equals(hashUtilsMD5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSHA512() throws Exception {
|
||||
String digestUtilsMD5 = DigestUtils.sha512Hex(TEST_STRING.getBytes());
|
||||
String hashUtilsMD5 = HashUtils.hash("SHA-512", TEST_STRING);
|
||||
|
||||
assertTrue(digestUtilsMD5.equals(hashUtilsMD5));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package net.locusworks.crypto.tests;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.locusworks.crypto.utils.RandomString;
|
||||
|
||||
public class RandomStringTest {
|
||||
|
||||
@Test
|
||||
public void testStaticBytes() {
|
||||
for (Integer length = 3; length < 50; length++) {
|
||||
assertTrue(RandomString.getBytes(length).length == length);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticString() {
|
||||
for (Integer length = 3; length < 50; length++) {
|
||||
String random = RandomString.getString(length);
|
||||
assertTrue(random.length() == length);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user