File: entropyCalculator_test.go

package info (click to toggle)
golang-github-nbutton23-zxcvbn-go 0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,448 kB
  • sloc: makefile: 6
file content (101 lines) | stat: -rw-r--r-- 2,367 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package entropy

import (
	"github.com/nbutton23/zxcvbn-go/match"
	"github.com/stretchr/testify/assert"
	"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{
		Pattern:        "spatial",
		I:              0,
		J:              5,
		Token:          "asdfgh",
		DictionaryName: "qwerty",
	}
	entropy := SpatialEntropy(matchPlain, 0, 0)
	assert.Equal(t, 9.754887502163468, entropy)

	matchShift := match.Match{
		Pattern:        "spatial",
		I:              0,
		J:              5,
		Token:          "asdFgh",
		DictionaryName: "qwerty",
	}
	entropyShift := SpatialEntropy(matchShift, 0, 1)
	assert.Equal(t, 12.562242424221072, entropyShift)

	matchTurn := match.Match{
		Pattern:        "spatial",
		I:              0,
		J:              5,
		Token:          "asdcxz",
		DictionaryName: "qwerty",
	}
	entropyTurn := SpatialEntropy(matchTurn, 2, 0)
	assert.Equal(t, 14.080500893768884, entropyTurn)
}

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)
}