~seirdy/moac

ref: v0.3.2 moac/givens_test.go -rw-r--r-- 2.5 KiB
91b75711Rohan Kumar Doc: Add FAQs to the README 8 months ago
                                                                                
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
102
103
104
105
106
107
108
package moac_test

import (
	"errors"
	"math"
	"testing"

	"git.sr.ht/~seirdy/moac"
)

const margin = 0.0025 // acceptable error

type givensTestCase struct {
	name    string
	given   moac.Givens
	quantum bool
	// Expected values should be within 10% error
	expectedBF    float64
	expectedME    float64
	expectedErrBF error
}

func givensTestCases() []givensTestCase {
	return []givensTestCase{
		{ // from README
			name:    "hitchhiker",
			quantum: true,
			given: moac.Givens{
				Mass:     5.97e24,
				Time:     1.45e17,
				Password: "ȣMǚHǨȎ#ŕģ=ʬƦQoţ}tʂŦȃťŇ+ħHǰĸȵʣɐɼŋĬŧǺʀǜǬɰ'ʮ0ʡěɱ6ȫŭ",
			},
			expectedBF:    0.0986,
			expectedME:    427.3,
			expectedErrBF: nil,
		},
		{ // from blog post: https://seirdy.one/2021/01/12/password-strength.html
			name:    "universe",
			quantum: false,
			given: moac.Givens{
				// default mass is the mass of the observable universe
				Entropy: 510,
			},
			expectedBF:    9.527e-62,
			expectedME:    307.3,
			expectedErrBF: nil,
		},
		{ // Should use the default provided entropy but fall back to the
			// lower computed value
			name:    "only energy",
			quantum: false,
			given: moac.Givens{
				Energy: 4e52,
			},
			expectedBF:    0.0134,
			expectedME:    250,
			expectedErrBF: nil,
		},
		{
			name:          "Mising energy, mass",
			quantum:       false,
			given:         moac.Givens{},
			expectedBF:    0,
			expectedME:    307.3,
			expectedErrBF: moac.ErrMissingPE,
		},
		{
			name:    "Mising password",
			quantum: false,
			given: moac.Givens{
				Mass:             0,
				GuessesPerSecond: 0,
				Entropy:          0,
				Time:             0,
				Power:            0,
				EnergyPerGuess:   0,
			},
			expectedBF:    0,
			expectedME:    307.3,
			expectedErrBF: moac.ErrMissingPE,
		},
	}
}

func TestBruteForceability(t *testing.T) {
	for _, test := range givensTestCases() {
		t.Run(test.name, func(t *testing.T) {
			got, err := moac.BruteForceability(&test.given, test.quantum)
			if !errors.Is(err, test.expectedErrBF) {
				t.Fatalf("BruteForceability() = %v", err)
			}
			if math.Abs(got-test.expectedBF)/test.expectedBF > margin {
				t.Errorf("Bruteforceability() = %.4g; want %.4g", got, test.expectedBF)
			}
		})
	}
}

func TestMinEntropy(t *testing.T) {
	for _, test := range givensTestCases() {
		t.Run(test.name, func(t *testing.T) {
			got := moac.MinEntropy(&test.given, test.quantum)
			if math.Abs(got-test.expectedME)/test.expectedME > margin {
				t.Errorf("MinEntropy() = %.4g; want %.4g", got, test.expectedME)
			}
		})
	}
}