Browse Source

Merge commit 'e5b8d493969786f7a0bf31bec92fd6797c3e8556' into HEAD

feature-password-validator-pwned-password-check
jenkins 3 years ago
parent
commit
02f4655625
  1. 57
      src/main/java/PasswordValidator.java
  2. 50
      src/test/java/PasswordValidatorTest.java

57
src/main/java/PasswordValidator.java

@ -0,0 +1,57 @@
import java.util.regex.Pattern;
public class PasswordValidator {
int minLength = 6;
boolean requireUppercase = true;
boolean requireLowercase = true;
boolean requireDigit = true;
private final Pattern uppercasePattern = Pattern.compile("^(?=.*[A-Z]).+$");
private final Pattern lowercasePattern = Pattern.compile("^(?=.*[a-z]).+$");
private final Pattern digitPattern = Pattern.compile("^(?=.*\\d).+$");
public boolean validate(String password) {
if (password.length() < minLength) {
return false;
} else if (requireUppercase && !uppercasePattern.matcher(password).matches()) {
return false;
} else if (requireLowercase && !lowercasePattern.matcher(password).matches()) {
return false;
} else if (requireDigit && !digitPattern.matcher(password).matches()) {
return false;
}
return true;
}
public int getMinLength() {
return minLength;
}
public void setMinLength(int minLength) {
this.minLength = minLength;
}
public boolean isRequireUppercase() {
return requireUppercase;
}
public void setRequireUppercase(boolean requireUppercase) {
this.requireUppercase = requireUppercase;
}
public boolean isRequireLowercase() {
return requireLowercase;
}
public void setRequireLowercase(boolean requireLowercase) {
this.requireLowercase = requireLowercase;
}
public boolean isRequireDigit() {
return requireDigit;
}
public void setRequireDigit(boolean requireDigit) {
this.requireDigit = requireDigit;
}
}

50
src/test/java/PasswordValidatorTest.java

@ -0,0 +1,50 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PasswordValidatorTest {
@Test
void validate() {
PasswordValidator passwordValidator = new PasswordValidator();
assertFalse(passwordValidator.validate(""));
// test minimum password length
passwordValidator.setMinLength(6);
passwordValidator.setRequireUppercase(false);
passwordValidator.setRequireLowercase(false);
passwordValidator.setRequireDigit(false);
assertFalse(passwordValidator.validate("abcde"));
assertTrue(passwordValidator.validate("abcdef"));
assertTrue(passwordValidator.validate("abcdefg"));
// test uppercase requirement
passwordValidator.setRequireUppercase(true);
passwordValidator.setRequireLowercase(false);
passwordValidator.setRequireDigit(false);
assertFalse(passwordValidator.validate("abcdef"));
assertTrue(passwordValidator.validate("abCdef"));
assertTrue(passwordValidator.validate("ABCDEF"));
// test lowercase requirement
passwordValidator.setRequireUppercase(true);
passwordValidator.setRequireLowercase(true);
passwordValidator.setRequireDigit(false);
assertFalse(passwordValidator.validate("abcdef"));
assertTrue(passwordValidator.validate("abCdef"));
assertFalse(passwordValidator.validate("ABCDEF"));
// test digits requirement
passwordValidator.setRequireUppercase(true);
passwordValidator.setRequireLowercase(true);
passwordValidator.setRequireDigit(true);
assertFalse(passwordValidator.validate("8"));
assertFalse(passwordValidator.validate("12345678"));
assertFalse(passwordValidator.validate("abcdef"));
assertFalse(passwordValidator.validate("abcdef8"));
assertFalse(passwordValidator.validate("abCdef"));
assertFalse(passwordValidator.validate("ABCDEF"));
assertFalse(passwordValidator.validate("ABCDEF8"));
assertTrue(passwordValidator.validate("abCDE8F"));
}
}
Loading…
Cancel
Save