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")); } @Test void getSHA1Hash() { assertEquals("356A192B7913B04C54574D18C28D46E6395428AB".toLowerCase(), PasswordValidator.getSHA1Hash("1")); assertEquals("A233F0E898ED0661D6D47ED0958F16B52E537231".toLowerCase(), PasswordValidator.getSHA1Hash("asdf12")); assertNull(PasswordValidator.getSHA1Hash("")); } }