You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.4 KiB

  1. import org.junit.jupiter.api.Test;
  2. import static org.junit.jupiter.api.Assertions.*;
  3. class PasswordValidatorTest {
  4. @Test
  5. void validate() {
  6. PasswordValidator passwordValidator = new PasswordValidator();
  7. assertFalse(passwordValidator.validate(""));
  8. // test minimum password length
  9. passwordValidator.setMinLength(6);
  10. passwordValidator.setRequireUppercase(false);
  11. passwordValidator.setRequireLowercase(false);
  12. passwordValidator.setRequireDigit(false);
  13. assertFalse(passwordValidator.validate("abcde"));
  14. assertTrue(passwordValidator.validate("abcdef"));
  15. assertTrue(passwordValidator.validate("abcdefg"));
  16. // test uppercase requirement
  17. passwordValidator.setRequireUppercase(true);
  18. passwordValidator.setRequireLowercase(false);
  19. passwordValidator.setRequireDigit(false);
  20. assertFalse(passwordValidator.validate("abcdef"));
  21. assertTrue(passwordValidator.validate("abCdef"));
  22. assertTrue(passwordValidator.validate("ABCDEF"));
  23. // test lowercase requirement
  24. passwordValidator.setRequireUppercase(true);
  25. passwordValidator.setRequireLowercase(true);
  26. passwordValidator.setRequireDigit(false);
  27. assertFalse(passwordValidator.validate("abcdef"));
  28. assertTrue(passwordValidator.validate("abCdef"));
  29. assertFalse(passwordValidator.validate("ABCDEF"));
  30. // test digits requirement
  31. passwordValidator.setRequireUppercase(true);
  32. passwordValidator.setRequireLowercase(true);
  33. passwordValidator.setRequireDigit(true);
  34. assertFalse(passwordValidator.validate("8"));
  35. assertFalse(passwordValidator.validate("12345678"));
  36. assertFalse(passwordValidator.validate("abcdef"));
  37. assertFalse(passwordValidator.validate("abcdef8"));
  38. assertFalse(passwordValidator.validate("abCdef"));
  39. assertFalse(passwordValidator.validate("ABCDEF"));
  40. assertFalse(passwordValidator.validate("ABCDEF8"));
  41. assertTrue(passwordValidator.validate("abCDE8F"));
  42. }
  43. @Test
  44. void getSHA1Hash() {
  45. assertEquals("356A192B7913B04C54574D18C28D46E6395428AB".toLowerCase(), PasswordValidator.getSHA1Hash("1"));
  46. assertEquals("A233F0E898ED0661D6D47ED0958F16B52E537231".toLowerCase(), PasswordValidator.getSHA1Hash("asdf12"));
  47. assertNull(PasswordValidator.getSHA1Hash(""));
  48. }
  49. }