1package entropy 2 3import ( 4 "github.com/nbutton23/zxcvbn-go/match" 5 "github.com/stretchr/testify/assert" 6 "testing" 7) 8 9func TestDictionaryEntropyCalculation(t *testing.T) { 10 match := match.Match{ 11 Pattern: "dictionary", 12 I: 0, 13 J: 4, 14 Token: "first", 15 } 16 17 entropy := DictionaryEntropy(match, float64(20)) 18 19 assert.Equal(t, 4.321928094887363, entropy) 20} 21 22func TestSpatialEntropyCalculation(t *testing.T) { 23 matchPlain := match.Match{ 24 Pattern: "spatial", 25 I: 0, 26 J: 5, 27 Token: "asdfgh", 28 DictionaryName: "qwerty", 29 } 30 entropy := SpatialEntropy(matchPlain, 0, 0) 31 assert.Equal(t, 9.754887502163468, entropy) 32 33 matchShift := match.Match{ 34 Pattern: "spatial", 35 I: 0, 36 J: 5, 37 Token: "asdFgh", 38 DictionaryName: "qwerty", 39 } 40 entropyShift := SpatialEntropy(matchShift, 0, 1) 41 assert.Equal(t, 12.562242424221072, entropyShift) 42 43 matchTurn := match.Match{ 44 Pattern: "spatial", 45 I: 0, 46 J: 5, 47 Token: "asdcxz", 48 DictionaryName: "qwerty", 49 } 50 entropyTurn := SpatialEntropy(matchTurn, 2, 0) 51 assert.Equal(t, 14.080500893768884, entropyTurn) 52} 53 54func TestRepeatMatchEntropyCalculation(t *testing.T) { 55 matchRepeat := match.Match{ 56 Pattern: "repeat", 57 I: 0, 58 J: 4, 59 Token: "aaaaa", 60 } 61 entropy := RepeatEntropy(matchRepeat) 62 assert.Equal(t, 7.022367813028454, entropy) 63} 64 65func TestSequenceCalculation(t *testing.T) { 66 matchLower := match.Match{ 67 Pattern: "sequence", 68 I: 0, 69 J: 4, 70 Token: "jklmn", 71 } 72 entropy := SequenceEntropy(matchLower, len("abcdefghijklmnopqrstuvwxyz"), true) 73 assert.Equal(t, 7.022367813028454, entropy) 74 75 matchUpper := match.Match{ 76 Pattern: "sequence", 77 I: 0, 78 J: 4, 79 Token: "JKLMN", 80 } 81 entropy = SequenceEntropy(matchUpper, len("abcdefghijklmnopqrstuvwxyz"), true) 82 assert.Equal(t, 8.022367813028454, entropy) 83 84 matchUpperDec := match.Match{ 85 Pattern: "sequence", 86 I: 0, 87 J: 4, 88 Token: "JKLMN", 89 } 90 entropy = SequenceEntropy(matchUpperDec, len("abcdefghijklmnopqrstuvwxyz"), false) 91 assert.Equal(t, 9.022367813028454, entropy) 92 93 matchDigit := match.Match{ 94 Pattern: "sequence", 95 I: 0, 96 J: 4, 97 Token: "34567", 98 } 99 entropy = SequenceEntropy(matchDigit, 10, true) 100 assert.Equal(t, 5.643856189774724, entropy) 101} 102