|
| 1 | +package com.baeldung.passay; |
| 2 | + |
| 3 | +import org.cryptacular.bean.EncodingHashBean; |
| 4 | +import org.cryptacular.spec.CodecSpec; |
| 5 | +import org.cryptacular.spec.DigestSpec; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Test; |
| 8 | +import org.passay.DictionaryRule; |
| 9 | +import org.passay.DictionarySubstringRule; |
| 10 | +import org.passay.DigestHistoryRule; |
| 11 | +import org.passay.EnglishSequenceData; |
| 12 | +import org.passay.HistoryRule; |
| 13 | +import org.passay.IllegalCharacterRule; |
| 14 | +import org.passay.IllegalRegexRule; |
| 15 | +import org.passay.IllegalSequenceRule; |
| 16 | +import org.passay.NumberRangeRule; |
| 17 | +import org.passay.PasswordData; |
| 18 | +import org.passay.PasswordValidator; |
| 19 | +import org.passay.RepeatCharacterRegexRule; |
| 20 | +import org.passay.RuleResult; |
| 21 | +import org.passay.SourceRule; |
| 22 | +import org.passay.UsernameRule; |
| 23 | +import org.passay.WhitespaceRule; |
| 24 | +import org.passay.dictionary.ArrayWordList; |
| 25 | +import org.passay.dictionary.WordListDictionary; |
| 26 | + |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | +import static org.junit.Assert.assertFalse; |
| 32 | + |
| 33 | +public class NegativeMatchingRulesUnitTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void givenDictionaryRules_whenValidatePassword_thenFoundIllegalWordsFromDictionary() { |
| 37 | + ArrayWordList arrayWordList = new ArrayWordList(new String[] { "bar", "foobar" }); |
| 38 | + |
| 39 | + WordListDictionary wordListDictionary = new WordListDictionary(arrayWordList); |
| 40 | + |
| 41 | + DictionaryRule dictionaryRule = new DictionaryRule(wordListDictionary); |
| 42 | + DictionarySubstringRule dictionarySubstringRule = new DictionarySubstringRule(wordListDictionary); |
| 43 | + |
| 44 | + PasswordValidator passwordValidator = new PasswordValidator(dictionaryRule, dictionarySubstringRule); |
| 45 | + RuleResult validate = passwordValidator.validate(new PasswordData("foobar")); |
| 46 | + |
| 47 | + assertFalse(validate.isValid()); |
| 48 | + assertEquals("ILLEGAL_WORD:{matchingWord=foobar}", getDetail(validate, 0)); |
| 49 | + assertEquals("ILLEGAL_WORD:{matchingWord=bar}", getDetail(validate, 1)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenHistoryRule_whenValidatePassword_thenFoundIllegalWordsFromHistory() { |
| 54 | + HistoryRule historyRule = new HistoryRule(); |
| 55 | + |
| 56 | + PasswordData passwordData = new PasswordData("123"); |
| 57 | + passwordData.setPasswordReferences(new PasswordData.HistoricalReference("12345"), new PasswordData.HistoricalReference("1234"), new PasswordData.HistoricalReference("123")); |
| 58 | + |
| 59 | + PasswordValidator passwordValidator = new PasswordValidator(historyRule); |
| 60 | + |
| 61 | + RuleResult validate = passwordValidator.validate(passwordData); |
| 62 | + |
| 63 | + assertFalse(validate.isValid()); |
| 64 | + assertEquals("HISTORY_VIOLATION:{historySize=3}", getDetail(validate, 0)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void givenSeveralIllegalRules_whenValidatePassword_thenFoundSeveralIllegalPatterns() { |
| 69 | + IllegalCharacterRule illegalCharacterRule = new IllegalCharacterRule(new char[] { 'a' }); |
| 70 | + IllegalRegexRule illegalRegexRule = new IllegalRegexRule("\\w{2}\\d{2}"); |
| 71 | + IllegalSequenceRule illegalSequenceRule = new IllegalSequenceRule(EnglishSequenceData.Alphabetical, 3, true); |
| 72 | + NumberRangeRule numberRangeRule = new NumberRangeRule(1, 10); |
| 73 | + WhitespaceRule whitespaceRule = new WhitespaceRule(); |
| 74 | + |
| 75 | + PasswordValidator passwordValidator = new PasswordValidator(illegalCharacterRule, illegalRegexRule, illegalSequenceRule, numberRangeRule, whitespaceRule); |
| 76 | + |
| 77 | + RuleResult validate = passwordValidator.validate(new PasswordData("abcd22 ")); |
| 78 | + |
| 79 | + assertFalse(validate.isValid()); |
| 80 | + assertEquals("ILLEGAL_CHAR:{illegalCharacter=a, matchBehavior=contains}", getDetail(validate, 0)); |
| 81 | + assertEquals("ILLEGAL_MATCH:{match=cd22, pattern=\\w{2}\\d{2}}", getDetail(validate, 1)); |
| 82 | + assertEquals("ILLEGAL_ALPHABETICAL_SEQUENCE:{sequence=abc}", getDetail(validate, 2)); |
| 83 | + assertEquals("ILLEGAL_ALPHABETICAL_SEQUENCE:{sequence=bcd}", getDetail(validate, 3)); |
| 84 | + assertEquals("ILLEGAL_NUMBER_RANGE:{number=2, matchBehavior=contains}", getDetail(validate, 4)); |
| 85 | + assertEquals("ILLEGAL_WHITESPACE:{whitespaceCharacter= , matchBehavior=contains}", getDetail(validate, 5)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void givenSourceRule_whenValidatePassword_thenFoundIllegalWordsFromSource() { |
| 90 | + SourceRule sourceRule = new SourceRule(); |
| 91 | + |
| 92 | + PasswordData passwordData = new PasswordData("password"); |
| 93 | + passwordData.setPasswordReferences(new PasswordData.SourceReference("source", "password")); |
| 94 | + |
| 95 | + PasswordValidator passwordValidator = new PasswordValidator(sourceRule); |
| 96 | + RuleResult validate = passwordValidator.validate(passwordData); |
| 97 | + |
| 98 | + assertFalse(validate.isValid()); |
| 99 | + assertEquals("SOURCE_VIOLATION:{source=source}", getDetail(validate, 0)); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void givenRepeatCharacterRegexRuleRule_whenValidatePassword_thenFoundIllegalPatternMatches() { |
| 104 | + RepeatCharacterRegexRule repeatCharacterRegexRule = new RepeatCharacterRegexRule(3); |
| 105 | + |
| 106 | + PasswordValidator passwordValidator = new PasswordValidator(repeatCharacterRegexRule); |
| 107 | + |
| 108 | + RuleResult validate = passwordValidator.validate(new PasswordData("aaabbb")); |
| 109 | + |
| 110 | + assertFalse(validate.isValid()); |
| 111 | + assertEquals("ILLEGAL_MATCH:{match=aaa, pattern=([^\\x00-\\x1F])\\1{2}}", getDetail(validate, 0)); |
| 112 | + assertEquals("ILLEGAL_MATCH:{match=bbb, pattern=([^\\x00-\\x1F])\\1{2}}", getDetail(validate, 1)); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void givenUserNameRule_whenValidatePassword_thenFoundUserNameInPassword() { |
| 117 | + PasswordValidator passwordValidator = new PasswordValidator(new UsernameRule()); |
| 118 | + |
| 119 | + PasswordData passwordData = new PasswordData("testuser1234"); |
| 120 | + passwordData.setUsername("testuser"); |
| 121 | + |
| 122 | + RuleResult validate = passwordValidator.validate(passwordData); |
| 123 | + |
| 124 | + assertFalse(validate.isValid()); |
| 125 | + assertEquals("ILLEGAL_USERNAME:{username=testuser, matchBehavior=contains}", getDetail(validate, 0)); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void givenPasswordAndHashBeanAndEncryptedReferences_whenValidate_thenPasswordValidationShouldPass() { |
| 130 | + List<PasswordData.Reference> historicalReferences = Arrays.asList(new PasswordData.HistoricalReference("SHA256", "2e4551de804e27aacf20f9df5be3e8cd384ed64488b21ab079fb58e8c90068ab")); |
| 131 | + PasswordData passwordData = new PasswordData("example!"); |
| 132 | + passwordData.setPasswordReferences(historicalReferences); |
| 133 | + |
| 134 | + EncodingHashBean encodingHashBean = new EncodingHashBean(new CodecSpec("Base64"), new DigestSpec("SHA256"), 1, false); |
| 135 | + |
| 136 | + PasswordValidator passwordValidator = new PasswordValidator(new DigestHistoryRule(encodingHashBean)); |
| 137 | + |
| 138 | + RuleResult validate = passwordValidator.validate(passwordData); |
| 139 | + |
| 140 | + Assert.assertTrue(validate.isValid()); |
| 141 | + } |
| 142 | + |
| 143 | + private String getDetail(RuleResult validate, int i) { |
| 144 | + return validate.getDetails() |
| 145 | + .get(i) |
| 146 | + .toString(); |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments