|
|
|
|
@ -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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|