updated libraries

updated to JUNIT5
This commit is contained in:
Joern Muehlencord
2019-07-12 15:07:13 +02:00
parent 3ae4dba8fe
commit 24dc927ab7
57 changed files with 1196 additions and 1083 deletions

View File

@ -21,8 +21,9 @@
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>

View File

@ -15,8 +15,9 @@ package de.muehlencord.shared.security;
import static de.muehlencord.shared.security.Luhn.computeCheckDigit;
import static de.muehlencord.shared.security.Luhn.validateNumber;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/**
*

View File

@ -2,153 +2,153 @@ package de.muehlencord.shared.security;
import static de.muehlencord.shared.security.OldPasswordUtil.getScryptHash;
import static de.muehlencord.shared.security.OldPasswordUtil.validateScryptHash;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/**
*
* @author jomu
*/
public class OldPasswordUtilTest {
/**
* Test of createSaltString method, of class PasswordUtil.
*/
@Test
public void createSaltString() throws Exception {
System.out.println("createSaltString");
int saltLength = 40;
String result = OldPasswordUtil.createSaltString(saltLength);
assertNotNull(result);
}
/**
* Test of getMD5Password method, of class PasswordUtil.
*/
@Test
public void getMD5Password() throws Exception {
System.out.println("getMD5Password");
String plainTextPassword = "";
int saltLength = 40;
String[] result1 = OldPasswordUtil.getMD5Password(plainTextPassword, saltLength);
String password1 = result1[0];
String salt1 = result1[1];
assertNotNull(result1);
assertNotNull(password1);
assertNotNull(salt1);
String[] result2 = OldPasswordUtil.getMD5Password(plainTextPassword, saltLength);
String password2 = result2[0];
String salt2 = result2[1];
assertNotNull(result2);
assertNotNull(password2);
assertNotNull(salt2);
assertNotSame(result1, result2);
assertNotSame(password1, password2);
assertNotSame(salt1, salt2);
}
/**
* Test of createSaltString method, of class PasswordUtil.
*/
@Test
public void createSaltString() throws Exception {
System.out.println("createSaltString");
int saltLength = 40;
String result = OldPasswordUtil.createSaltString(saltLength);
assertNotNull(result);
}
/**
* Test of getMD5Password method, of class PasswordUtil.
*/
@Test
public void getMD5Password() throws Exception {
System.out.println("getMD5Password");
String plainTextPassword = "";
int saltLength = 40;
String[] result1 = OldPasswordUtil.getMD5Password(plainTextPassword, saltLength);
String password1 = result1[0];
String salt1 = result1[1];
assertNotNull(result1);
assertNotNull(password1);
assertNotNull(salt1);
String[] result2 = OldPasswordUtil.getMD5Password(plainTextPassword, saltLength);
String password2 = result2[0];
String salt2 = result2[1];
assertNotNull(result2);
assertNotNull(password2);
assertNotNull(salt2);
assertNotSame(result1, result2);
assertNotSame(password1, password2);
assertNotSame(salt1, salt2);
}
/**
* Test of checkPassword method, of class PasswordUtil.
*/
@Test
public void checkPassword() throws Exception {
System.out.println("checkPassword");
String plainTextPassword = "welcome";
String plainTextPassword2 = "this is not the correct password";
String[] data = OldPasswordUtil.getMD5Password(plainTextPassword, 40);
String cryptedPassword = data[0];
String salt = data[1];
String salt2 = OldPasswordUtil.createSaltString(40);
String salt3 = OldPasswordUtil.createSaltString(10);
boolean expResult = true;
boolean result = OldPasswordUtil.checkPassword(plainTextPassword, cryptedPassword, salt);
assertTrue(expResult == result);
expResult = false;
result = OldPasswordUtil.checkPassword(plainTextPassword2, cryptedPassword, salt);
assertTrue(expResult == result);
expResult = false;
result = OldPasswordUtil.checkPassword(plainTextPassword, cryptedPassword, salt2);
assertTrue(expResult == result);
expResult = false;
result = OldPasswordUtil.checkPassword(plainTextPassword, cryptedPassword, salt3);
assertTrue(expResult == result);
}
/**
* Test of checkPassword method, of class PasswordUtil.
*/
@Test
public void checkPassword() throws Exception {
System.out.println("checkPassword");
String plainTextPassword = "welcome";
String plainTextPassword2 = "this is not the correct password";
String[] data = OldPasswordUtil.getMD5Password(plainTextPassword, 40);
String cryptedPassword = data[0];
String salt = data[1];
String salt2 = OldPasswordUtil.createSaltString(40);
String salt3 = OldPasswordUtil.createSaltString(10);
boolean expResult = true;
boolean result = OldPasswordUtil.checkPassword(plainTextPassword, cryptedPassword, salt);
assertEquals(expResult, result);
expResult = false;
result = OldPasswordUtil.checkPassword(plainTextPassword2, cryptedPassword, salt);
assertEquals(expResult, result);
expResult = false;
result = OldPasswordUtil.checkPassword(plainTextPassword, cryptedPassword, salt2);
assertEquals(expResult, result);
expResult = false;
result = OldPasswordUtil.checkPassword(plainTextPassword, cryptedPassword, salt3);
assertEquals(expResult, result);
}
@Test
public void getRandomString() throws SecurityException {
System.out.println ("getRandomString");
System.out.println("getRandomString");
String randomString = OldPasswordUtil.getRandomString("test-", 32);
System.out.println(randomString);
assertNotNull(randomString);
assertTrue("string must start with prefix", randomString.startsWith("test"));
assertEquals("string length check", 32, randomString.length());
assertTrue(randomString.startsWith("test"), "string must start with prefix");
assertTrue(32 == randomString.length(), "string length check");
String randomString2 = OldPasswordUtil.getRandomString("test-", 32);
System.out.println(randomString2);
assertNotNull(randomString2);
assertTrue("string must start with prefix", randomString2.startsWith("test"));
assertEquals("string length check", 32, randomString2.length());
assertTrue(randomString2.startsWith("test"), "string must start with prefix");
assertTrue(32 == randomString2.length(), "string length check");
assertNotSame(randomString, randomString2);
}
@Test
public void getRandomStringBlankPrefix() throws SecurityException {
System.out.println ("getRandomStringBlankPrefix");
System.out.println("getRandomStringBlankPrefix");
String randomString = OldPasswordUtil.getRandomString("", 32);
System.out.println(randomString);
assertNotNull(randomString);
assertEquals("string length check", 32, randomString.length());
assertTrue(32 == randomString.length(), "string length check");
}
@Test
public void getRandomStringNullPrefix() throws SecurityException {
System.out.println ("getRandomStringNullPrefix");
System.out.println("getRandomStringNullPrefix");
String randomString = OldPasswordUtil.getRandomString(null, 32);
System.out.println(randomString);
assertNotNull(randomString);
assertEquals("string length check", 32, randomString.length());
}
assertTrue(32 == randomString.length(), "string length check");
}
/**
* test the hashPassword method
*/
@Test
public void testGetScryptHash() {
String hash1 = getScryptHash("secret");
String hash1 = getScryptHash("secret");
String hash2 = getScryptHash("secret");
System.out.println (hash1);
System.out.println (hash2);
assertNotNull (hash1);
assertNotNull (hash2);
System.out.println(hash1);
System.out.println(hash2);
assertNotNull(hash1);
assertNotNull(hash2);
// even if password is the same, the has must not be the same due to correct usage of salts
assertFalse (hash1.equals (hash2));
assertTrue (hash1.length() == 79);
assertTrue (hash2.length() == 79);
assertFalse(hash1.equals(hash2));
assertTrue(hash1.length() == 79);
assertTrue(hash2.length() == 79);
}
/**
* test for validating passwords
*/
@Test
public void testValidateScryptHash() {
String hash1 = getScryptHash("secret");
String hash1 = getScryptHash("secret");
String hash2 = getScryptHash("secret");
assertTrue ("hash must match if correct password is given",validateScryptHash("secret", hash1));
assertTrue ("hash must match if correct password is given", validateScryptHash("secret", hash2));
assertFalse ("hash must not match if wrong password is given", validateScryptHash("secret2", hash1));
}
assertTrue(validateScryptHash("secret", hash1), "hash must match if correct password is given");
assertTrue(validateScryptHash("secret", hash2), "hash must match if correct password is given");
assertFalse(validateScryptHash("secret2", hash1), "hash must not match if wrong password is given");
}
}
}

View File

@ -1,12 +1,11 @@
package de.muehlencord.shared.security;
import de.muehlencord.shared.security.PasswordUtil;
import java.security.SecureRandom;
import org.bouncycastle.util.encoders.Base64;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.BeforeClass;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
/**
*
@ -18,7 +17,7 @@ public class PasswordUtilTest {
private static String systemSalt64Coded;
private static byte[] systemSaltBytes;
@BeforeClass
@BeforeAll
public static void init() {
secureRandom = new SecureRandom();