2015-10-05 19:56:21 +00:00
|
|
|
package matching
|
2016-01-13 22:02:53 +00:00
|
|
|
|
2015-10-05 19:56:21 +00:00
|
|
|
import (
|
2016-05-29 21:52:34 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-01-19 18:59:29 +00:00
|
|
|
"github.com/nbutton23/zxcvbn-go/match"
|
|
|
|
"strings"
|
2015-10-05 19:56:21 +00:00
|
|
|
"testing"
|
2016-01-29 21:53:09 +00:00
|
|
|
"fmt"
|
2016-03-08 16:57:50 +00:00
|
|
|
"encoding/json"
|
2015-10-05 19:56:21 +00:00
|
|
|
)
|
2016-01-13 22:02:53 +00:00
|
|
|
|
2015-10-05 19:56:21 +00:00
|
|
|
//DateSepMatch("1991-09-11jibjab11.9.1991")
|
2016-01-13 22:02:53 +00:00
|
|
|
//[{date 16 25 . 9 11 1991} {date 0 10 - 9 11 1991}]
|
2015-10-05 19:56:21 +00:00
|
|
|
|
2016-01-13 22:02:53 +00:00
|
|
|
func TestDateSepMatch(t *testing.T) {
|
2016-03-08 16:57:50 +00:00
|
|
|
matches := dateSepMatchHelper("1991-09-11jibjab11.9.1991")
|
2015-10-05 19:56:21 +00:00
|
|
|
|
|
|
|
assert.Len(t, matches, 2, "Length should be 2")
|
|
|
|
|
|
|
|
for _, match := range matches {
|
2016-01-13 22:02:53 +00:00
|
|
|
if match.Separator == "." {
|
2015-10-05 19:56:21 +00:00
|
|
|
assert.Equal(t, 16, match.I)
|
|
|
|
assert.Equal(t, 25, match.J)
|
|
|
|
assert.Equal(t, int64(9), match.Day)
|
|
|
|
assert.Equal(t, int64(11), match.Month)
|
|
|
|
assert.Equal(t, int64(1991), match.Year)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, 0, match.I)
|
|
|
|
assert.Equal(t, 10, match.J)
|
|
|
|
assert.Equal(t, int64(9), match.Day)
|
|
|
|
assert.Equal(t, int64(11), match.Month)
|
|
|
|
assert.Equal(t, int64(1991), match.Year)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:02:53 +00:00
|
|
|
}
|
2016-01-19 18:59:29 +00:00
|
|
|
|
|
|
|
func TestRepeatMatch(t *testing.T) {
|
|
|
|
//aaaBbBb
|
2016-03-08 16:57:50 +00:00
|
|
|
matches := repeatMatch("aaabBbB")
|
2016-01-19 18:59:29 +00:00
|
|
|
|
|
|
|
assert.Len(t, matches, 2, "Lenght should be 2")
|
|
|
|
|
|
|
|
for _, match := range matches {
|
2016-01-21 22:26:33 +00:00
|
|
|
if strings.ToLower(match.DictionaryName) == "b" {
|
2016-01-19 18:59:29 +00:00
|
|
|
assert.Equal(t, 3, match.I)
|
|
|
|
assert.Equal(t, 6, match.J)
|
|
|
|
assert.Equal(t, "bBbB", match.Token)
|
2016-01-21 22:26:33 +00:00
|
|
|
assert.NotZero(t, match.Entropy, "Entropy should be set")
|
2016-01-19 18:59:29 +00:00
|
|
|
} else {
|
|
|
|
assert.Equal(t, 0, match.I)
|
|
|
|
assert.Equal(t, 2, match.J)
|
|
|
|
assert.Equal(t, "aaa", match.Token)
|
2016-01-21 22:26:33 +00:00
|
|
|
assert.NotZero(t, match.Entropy, "Entropy should be set")
|
|
|
|
|
2016-01-19 18:59:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSequenceMatch(t *testing.T) {
|
|
|
|
//abcdjibjacLMNOPjibjac1234 => abcd LMNOP 1234
|
|
|
|
|
2016-03-08 16:57:50 +00:00
|
|
|
matches := sequenceMatch("abcdjibjacLMNOPjibjac1234")
|
2016-01-19 18:59:29 +00:00
|
|
|
assert.Len(t, matches, 3, "Lenght should be 2")
|
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
if match.DictionaryName == "lower" {
|
|
|
|
assert.Equal(t, 0, match.I)
|
|
|
|
assert.Equal(t, 3, match.J)
|
|
|
|
assert.Equal(t, "abcd", match.Token)
|
2016-01-21 22:26:33 +00:00
|
|
|
assert.NotZero(t, match.Entropy, "Entropy should be set")
|
2016-01-19 18:59:29 +00:00
|
|
|
} else if match.DictionaryName == "upper" {
|
|
|
|
assert.Equal(t, 10, match.I)
|
|
|
|
assert.Equal(t, 14, match.J)
|
|
|
|
assert.Equal(t, "LMNOP", match.Token)
|
2016-01-21 22:26:33 +00:00
|
|
|
assert.NotZero(t, match.Entropy, "Entropy should be set")
|
2016-01-19 18:59:29 +00:00
|
|
|
} else if match.DictionaryName == "digits" {
|
|
|
|
assert.Equal(t, 21, match.I)
|
|
|
|
assert.Equal(t, 24, match.J)
|
|
|
|
assert.Equal(t, "1234", match.Token)
|
2016-01-21 22:26:33 +00:00
|
|
|
assert.NotZero(t, match.Entropy, "Entropy should be set")
|
2016-01-19 18:59:29 +00:00
|
|
|
} else {
|
|
|
|
assert.True(t, false, "Unknow dictionary")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpatialMatchQwerty(t *testing.T) {
|
2016-03-08 16:57:50 +00:00
|
|
|
matches := spatialMatch("qwerty")
|
2016-01-19 18:59:29 +00:00
|
|
|
assert.Len(t, matches, 1, "Lenght should be 1")
|
2016-01-21 22:26:33 +00:00
|
|
|
assert.NotZero(t, matches[0].Entropy, "Entropy should be set")
|
2016-01-19 18:59:29 +00:00
|
|
|
|
2016-03-08 16:57:50 +00:00
|
|
|
matches = spatialMatch("asdf")
|
2016-01-19 18:59:29 +00:00
|
|
|
assert.Len(t, matches, 1, "Lenght should be 1")
|
2016-01-21 22:26:33 +00:00
|
|
|
assert.NotZero(t, matches[0].Entropy, "Entropy should be set")
|
2016-01-19 18:59:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSpatialMatchDvorak(t *testing.T) {
|
2016-03-08 16:57:50 +00:00
|
|
|
matches := spatialMatch("aoeuidhtns")
|
2016-01-19 18:59:29 +00:00
|
|
|
assert.Len(t, matches, 1, "Lenght should be 1")
|
2016-01-21 22:26:33 +00:00
|
|
|
assert.NotZero(t, matches[0].Entropy, "Entropy should be set")
|
2016-01-19 18:59:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDictionaryMatch(t *testing.T) {
|
|
|
|
var matches []match.Match
|
|
|
|
for _, dicMatcher := range DICTIONARY_MATCHERS {
|
|
|
|
matchesTemp := dicMatcher("first")
|
|
|
|
matches = append(matches, matchesTemp...)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, matches, 4, "Lenght should be 4")
|
2016-01-21 22:26:33 +00:00
|
|
|
for _, match := range matches {
|
|
|
|
assert.NotZero(t, match.Entropy, "Entropy should be set")
|
|
|
|
|
|
|
|
}
|
2016-01-19 18:59:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDateWithoutSepMatch(t *testing.T) {
|
|
|
|
matches := dateWithoutSepMatch("11091991")
|
|
|
|
assert.Len(t, matches, 1, "Lenght should be 1")
|
|
|
|
}
|
2016-01-28 00:11:50 +00:00
|
|
|
|
|
|
|
//l33t
|
|
|
|
func TestLeetSubTable(t *testing.T){
|
|
|
|
subs := relevantL33tSubtable("password")
|
|
|
|
assert.Len(t, subs, 0, "password should produce no leet subs")
|
|
|
|
|
|
|
|
subs = relevantL33tSubtable("p4ssw0rd")
|
|
|
|
assert.Len(t, subs, 2, "p4ssw0rd should produce 2 subs")
|
2016-01-29 18:25:32 +00:00
|
|
|
|
|
|
|
subs = relevantL33tSubtable("1eet")
|
|
|
|
assert.Len(t, subs, 2, "1eet should produce 2 subs")
|
|
|
|
assert.Equal(t, subs["i"][0], "1")
|
|
|
|
assert.Equal(t, subs["l"][0], "1")
|
|
|
|
|
|
|
|
|
|
|
|
subs = relevantL33tSubtable("4pple@pple")
|
|
|
|
assert.Len(t, subs, 1, "4pple@pple should produce 1 subs")
|
|
|
|
assert.Len(t, subs["a"], 2)
|
|
|
|
|
|
|
|
|
2016-01-28 00:11:50 +00:00
|
|
|
}
|
2016-01-29 18:25:32 +00:00
|
|
|
|
|
|
|
func TestPermutationsOfLeetSubstitutions(t *testing.T){
|
|
|
|
password := "p4ssw0rd" //[passw0rd, password, p4ssword]
|
|
|
|
possibleSubs := relevantL33tSubtable(password)
|
|
|
|
|
|
|
|
permutations := getAllPermutationsOfLeetSubstitutions(password, possibleSubs)
|
|
|
|
|
|
|
|
assert.Len(t, permutations, 3, "There should be 3 permutations for "+password)
|
|
|
|
|
|
|
|
password = "p4$sw0rd" //[pa$sw0rd, passw0rd, password, pa$sword, p4ssw0rd, p4ssword, p4$sword]
|
|
|
|
possibleSubs = relevantL33tSubtable(password)
|
|
|
|
|
|
|
|
permutations = getAllPermutationsOfLeetSubstitutions(password, possibleSubs)
|
|
|
|
assert.Len(t, permutations, 7, "There should be 7 (? check my math) permutations for "+password)
|
|
|
|
|
|
|
|
password = "p4$$w0rd" //[pa$sw0rd, passw0rd, password, pa$sword, p4ssw0rd, p4ssword, p4$sword]
|
|
|
|
possibleSubs = relevantL33tSubtable(password)
|
|
|
|
|
|
|
|
permutations = getAllPermutationsOfLeetSubstitutions(password, possibleSubs)
|
|
|
|
assert.Len(t, permutations, 15, "Check my math 2*2*2*2 - 1 "+password)
|
|
|
|
|
|
|
|
|
|
|
|
password = "1337"
|
|
|
|
possibleSubs = relevantL33tSubtable(password)
|
|
|
|
permutations = getAllPermutationsOfLeetSubstitutions(password, possibleSubs)
|
|
|
|
assert.Len(t, permutations, 35, "check my math 3*2*2*3 -1 ")
|
2016-01-29 21:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLeet(t *testing.T){
|
2016-03-08 16:57:50 +00:00
|
|
|
password := "1337"
|
2016-01-29 21:53:09 +00:00
|
|
|
matches := l33tMatch(password)
|
2016-03-08 16:57:50 +00:00
|
|
|
bytes, _ := json.Marshal(matches)
|
|
|
|
fmt.Println(string(bytes))
|
2016-01-29 21:53:09 +00:00
|
|
|
|
|
|
|
fmt.Println(matches[0].J)
|
|
|
|
}
|