forked from sec51/twofactor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
recover_test.go
135 lines (107 loc) · 2.57 KB
/
recover_test.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package twofactor
import (
"regexp"
"strings"
"testing"
)
func TestGenerateRecoveryCodes(t *testing.T) {
t.Parallel()
codes, err := GenerateRecoveryCodes()
if err != nil {
t.Fatal(err)
}
if len(codes) != 10 {
t.Error("it should create 10 codes, got:", len(codes))
}
rgx := regexp.MustCompile(`^[0-9A-Z]{6}-[0-9A-Z]{6}$`)
for _, c := range codes {
if !rgx.MatchString(c) {
t.Errorf("code %s did not match regexp", c)
}
if !ValidRecoveryCode(c) {
t.Errorf("code %s did not match format", c)
}
}
}
func TestHashRecoveryCodes(t *testing.T) {
t.Parallel()
codes, err := GenerateRecoveryCodes()
if err != nil {
t.Fatal(err)
}
if len(codes) != 10 {
t.Error("it should create 10 codes, got:", len(codes))
}
cryptedCodes, err := BCryptRecoveryCodes(codes)
if err != nil {
t.Fatal(err)
}
for _, c := range cryptedCodes {
if !strings.HasPrefix(c, "$2a$10$") {
t.Error("code did not look like bcrypt:", c)
}
}
}
func TestUseRecoveryCode(t *testing.T) {
t.Parallel()
codes, err := GenerateRecoveryCodes()
if err != nil {
t.Fatal(err)
}
if len(codes) != 10 {
t.Error("it should create 10 codes, got:", len(codes))
}
cryptedCodes, err := BCryptRecoveryCodes(codes)
if err != nil {
t.Fatal(err)
}
for _, c := range cryptedCodes {
if !strings.HasPrefix(c, "$2a$10$") {
t.Error("code did not look like bcrypt:", c)
}
}
remaining, ok := UseRecoveryCode(cryptedCodes, codes[4])
if !ok {
t.Error("should have used a code")
}
if want, got := len(cryptedCodes)-1, len(remaining); want != got {
t.Error("want:", want, "got:", got)
}
if cryptedCodes[4] == remaining[4] {
t.Error("it should have used number 4")
}
remaining, ok = UseRecoveryCode(remaining, codes[0])
if !ok {
t.Error("should have used a code")
}
if want, got := len(cryptedCodes)-2, len(remaining); want != got {
t.Error("want:", want, "got:", got)
}
if cryptedCodes[0] == remaining[0] {
t.Error("it should have used number 0")
}
remaining, ok = UseRecoveryCode(remaining, codes[len(codes)-1])
if !ok {
t.Error("should have used a code")
}
if want, got := len(cryptedCodes)-3, len(remaining); want != got {
t.Error("want:", want, "got:", got)
}
if cryptedCodes[len(cryptedCodes)-1] == remaining[len(remaining)-1] {
t.Error("it should have used number 0")
}
}
func BenchmarkGenerateRecoveryCodes(b *testing.B) {
b.SetParallelism(1)
for i := 0; i < b.N; i++ {
codes, err := GenerateRecoveryCodes()
if err != nil {
b.Fatal(err)
}
cryptedCodes, err := BCryptRecoveryCodes(codes)
if err != nil {
b.Fatal(err)
}
_ = cryptedCodes
}
}