2016-01-21 22:26:33 +00:00
|
|
|
package entropy
|
|
|
|
|
|
|
|
import (
|
2022-03-11 18:37:20 +00:00
|
|
|
"github.com/status-im/zxcvbn-go/match"
|
2016-06-19 13:33:56 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-01-21 22:26:33 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDictionaryEntropyCalculation(t *testing.T) {
|
|
|
|
match := match.Match{
|
|
|
|
Pattern: "dictionary",
|
|
|
|
I: 0,
|
|
|
|
J: 4,
|
|
|
|
Token: "first",
|
|
|
|
}
|
|
|
|
|
|
|
|
entropy := DictionaryEntropy(match, float64(20))
|
|
|
|
|
|
|
|
assert.Equal(t, 4.321928094887363, entropy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpatialEntropyCalculation(t *testing.T) {
|
|
|
|
matchPlain := match.Match{
|
2016-06-19 13:33:56 +00:00
|
|
|
Pattern: "spatial",
|
|
|
|
I: 0,
|
|
|
|
J: 5,
|
|
|
|
Token: "asdfgh",
|
|
|
|
DictionaryName: "qwerty",
|
2016-01-21 22:26:33 +00:00
|
|
|
}
|
|
|
|
entropy := SpatialEntropy(matchPlain, 0, 0)
|
2016-01-21 22:40:34 +00:00
|
|
|
assert.Equal(t, 9.754887502163468, entropy)
|
2016-01-21 22:26:33 +00:00
|
|
|
|
|
|
|
matchShift := match.Match{
|
2016-06-19 13:33:56 +00:00
|
|
|
Pattern: "spatial",
|
|
|
|
I: 0,
|
|
|
|
J: 5,
|
|
|
|
Token: "asdFgh",
|
|
|
|
DictionaryName: "qwerty",
|
2016-01-21 22:26:33 +00:00
|
|
|
}
|
|
|
|
entropyShift := SpatialEntropy(matchShift, 0, 1)
|
2016-01-21 22:40:34 +00:00
|
|
|
assert.Equal(t, 12.562242424221072, entropyShift)
|
2016-01-21 22:26:33 +00:00
|
|
|
|
|
|
|
matchTurn := match.Match{
|
2016-06-19 13:33:56 +00:00
|
|
|
Pattern: "spatial",
|
|
|
|
I: 0,
|
|
|
|
J: 5,
|
|
|
|
Token: "asdcxz",
|
|
|
|
DictionaryName: "qwerty",
|
2016-01-21 22:26:33 +00:00
|
|
|
}
|
|
|
|
entropyTurn := SpatialEntropy(matchTurn, 2, 0)
|
2016-01-21 22:40:34 +00:00
|
|
|
assert.Equal(t, 14.080500893768884, entropyTurn)
|
2016-01-21 22:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepeatMatchEntropyCalculation(t *testing.T) {
|
|
|
|
matchRepeat := match.Match{
|
|
|
|
Pattern: "repeat",
|
|
|
|
I: 0,
|
|
|
|
J: 4,
|
|
|
|
Token: "aaaaa",
|
|
|
|
}
|
|
|
|
entropy := RepeatEntropy(matchRepeat)
|
|
|
|
assert.Equal(t, 7.022367813028454, entropy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSequenceCalculation(t *testing.T) {
|
|
|
|
matchLower := match.Match{
|
|
|
|
Pattern: "sequence",
|
|
|
|
I: 0,
|
|
|
|
J: 4,
|
|
|
|
Token: "jklmn",
|
|
|
|
}
|
|
|
|
entropy := SequenceEntropy(matchLower, len("abcdefghijklmnopqrstuvwxyz"), true)
|
|
|
|
assert.Equal(t, 7.022367813028454, entropy)
|
|
|
|
|
|
|
|
matchUpper := match.Match{
|
|
|
|
Pattern: "sequence",
|
|
|
|
I: 0,
|
|
|
|
J: 4,
|
|
|
|
Token: "JKLMN",
|
|
|
|
}
|
|
|
|
entropy = SequenceEntropy(matchUpper, len("abcdefghijklmnopqrstuvwxyz"), true)
|
|
|
|
assert.Equal(t, 8.022367813028454, entropy)
|
|
|
|
|
|
|
|
matchUpperDec := match.Match{
|
|
|
|
Pattern: "sequence",
|
|
|
|
I: 0,
|
|
|
|
J: 4,
|
|
|
|
Token: "JKLMN",
|
|
|
|
}
|
|
|
|
entropy = SequenceEntropy(matchUpperDec, len("abcdefghijklmnopqrstuvwxyz"), false)
|
|
|
|
assert.Equal(t, 9.022367813028454, entropy)
|
|
|
|
|
|
|
|
matchDigit := match.Match{
|
|
|
|
Pattern: "sequence",
|
|
|
|
I: 0,
|
|
|
|
J: 4,
|
|
|
|
Token: "34567",
|
|
|
|
}
|
|
|
|
entropy = SequenceEntropy(matchDigit, 10, true)
|
|
|
|
assert.Equal(t, 5.643856189774724, entropy)
|
|
|
|
}
|