-
Notifications
You must be signed in to change notification settings - Fork 13
/
util_test.go
70 lines (65 loc) · 2.03 KB
/
util_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
package htpasswd
import (
"testing"
)
func testParserGood(t *testing.T, name string, accept PasswdParser, reject PasswdParser, hashed string, passwd string) {
if accept != nil {
ep, err := accept(hashed)
if err != nil {
t.Errorf("%s accept (%s) failed to parse: %s", name, hashed, err.Error())
} else if ep == nil {
t.Errorf("%s accept (%s) failed to yield an EncodedPasswd", name, hashed)
} else {
if !ep.MatchesPassword(passwd) {
t.Errorf("%s accept (%s) failed to match password (%s)", name, hashed, passwd)
}
if ep.MatchesPassword(passwd + "not") {
t.Errorf("%s accept (%s) failed by matching password (%s)", name, hashed, passwd+"not")
}
}
}
if reject != nil {
ep, err := reject(hashed)
if ep != nil {
t.Errorf("%s reject (%s) yielded an EncodedPasswd", name, hashed)
} else if err == nil {
t.Errorf("%s reject (%s) did not return an error", name, hashed)
}
}
}
func testParserBad(t *testing.T, name string, accept PasswdParser, reject PasswdParser, hashed string) {
if accept != nil {
ep, err := accept(hashed)
if ep != nil {
t.Errorf("%s accept (%s) yielded a EncodedPasswd", name, hashed)
} else if err == nil {
t.Errorf("%s accept (%s) did not return an error", name, hashed)
}
}
if reject != nil {
ep, err := reject(hashed)
if ep != nil {
t.Errorf("%s reject (%s) yielded a EncodedPasswd", name, hashed)
} else if err == nil {
t.Errorf("%s reject (%s) did not return an error", name, hashed)
}
}
}
func testParserNot(t *testing.T, name string, accept PasswdParser, reject PasswdParser, hashed string) {
if accept != nil {
ep, err := accept(hashed)
if ep != nil {
t.Errorf("%s accept (%s) yielded a EncodedPasswd", name, hashed)
} else if err != nil {
t.Errorf("%s accept (%s) errored instead of ignoring", name, hashed)
}
}
if reject != nil {
ep, err := reject(hashed)
if ep != nil {
t.Errorf("%s reject (%s) yielded a EncodedPasswd", name, hashed)
} else if err != nil {
t.Errorf("%s reject (%s) errored instead of ignoring", name, hashed)
}
}
}