File: zxcvbn_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 (93 lines) | stat: -rw-r--r-- 3,121 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
package zxcvbn

import (
	"testing"

	"fmt"
	"math"
	"strconv"
)

/**
Use these test to see how close to feature parity the library is.
*/

const (
	allowableError = float64(0.05)
)

type failedTest struct {
	Password string
	Expect   float64
	Actual   float64
	PError   float64
}

var failedTests []failedTest
var numTestRan int

func TestPasswordStrength(t *testing.T) {

	//	Expected calculated by running zxcvbn-python
	runTest(t, "zxcvbn", float64(6.845490050944376))
	runTest(t, "Tr0ub4dour&3", float64(17.296))
	runTest(t, "qwER43@!", float64(26.44))
	runTest(t, "correcthorsebatterystaple", float64(45.212))
	runTest(t, "coRrecth0rseba++ery9.23.2007staple$", float64(66.018))
	runTest(t, "D0g..................", float64(20.678))
	runTest(t, "abcdefghijk987654321", float64(11.951))
	runTest(t, "neverforget", float64(2)) // I think this is wrong. . .
	runTest(t, "13/3/1997", float64(2))   // I think this is wrong. . .
	runTest(t, "neverforget13/3/1997", float64(32.628))
	runTest(t, "1qaz2wsx3edc", float64(19.314))
	runTest(t, "temppass22", float64(22.179))
	runTest(t, "briansmith", float64(4.322))
	runTest(t, "briansmith4mayor", float64(18.64))
	runTest(t, "password1", float64(2.0))
	runTest(t, "viking", float64(7.531))
	runTest(t, "thx1138", float64(7.426))
	runTest(t, "ScoRpi0ns", float64(20.621))
	runTest(t, "do you know", float64(4.585))
	runTest(t, "ryanhunter2000", float64(14.506))
	runTest(t, "rianhunter2000", float64(21.734))
	runTest(t, "asdfghju7654rewq", float64(29.782))
	runTest(t, "AOEUIDHG&*()LS_", float64(33.254))
	runTest(t, "12345678", float64(1.585))
	runTest(t, "defghi6789", float64(12.607))
	runTest(t, "rosebud", float64(7.937))
	runTest(t, "Rosebud", float64(8.937))
	runTest(t, "ROSEBUD", float64(8.937))
	runTest(t, "rosebuD", float64(8.937))
	runTest(t, "ros3bud99", float64(19.276))
	runTest(t, "r0s3bud99", float64(19.276))
	runTest(t, "R0$38uD99", float64(34.822))
	runTest(t, "verlineVANDERMARK", float64(26.293))
	runTest(t, "eheuczkqyq", float64(42.813))
	runTest(t, "rWibMFACxAUGZmxhVncy", float64(104.551))
	runTest(t, "Ba9ZyWABu99[BK#6MBgbH88Tofv)vs$", float64(161.278))

	formatString := "%s : error should be less than %.2f \t Acctual error was: %.4f  \t Expected entropy %.4f \t Actual entropy %.4f \n"
	for _, test := range failedTests {
		fmt.Printf(formatString, test.Password, allowableError, test.PError, test.Expect, test.Actual)
	}

	pTestPassed := (float64(numTestRan-len(failedTests)) / float64(numTestRan)) * float64(100)

	fmt.Println("\n % of the test passed " + strconv.FormatFloat(pTestPassed, 'f', -1, 64))

}

func runTest(t *testing.T, password string, pythonEntropy float64) {
	//Calculated by running it through python-zxcvbn
	goEntropy := GoPasswordStrength(password, nil)
	perror := math.Abs(goEntropy-pythonEntropy) / pythonEntropy

	numTestRan++
	if perror > allowableError {
		failedTests = append(failedTests, failedTest{Password: password, Expect: pythonEntropy, Actual: goEntropy, PError: perror})
	}
}

func GoPasswordStrength(password string, userInputs []string) float64 {
	return PasswordStrength(password, userInputs).Entropy
}