-
Notifications
You must be signed in to change notification settings - Fork 96
/
user-patterns_test.go
71 lines (58 loc) · 1.89 KB
/
user-patterns_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
package jsluice
import (
"strings"
"testing"
)
func TestParseUserPatterns(t *testing.T) {
testData := strings.NewReader(`[
{"name": "httpAuth", "value": "/[a-z0-9_/\\.:-]+@[a-z0-9-]+\\.[a-z0-9.-]+"},
{"name": "base64", "value": "^(eyJ|YTo|Tzo|PD[89]|aHR0cHM6L|aHR0cDo|rO0)[%a-zA-Z0-9+/]+={0,2}"}
]`)
patterns, err := ParseUserPatterns(testData)
if err != nil {
t.Errorf("want nil error for ParseUserPatterns(testData); have %s", err)
}
if len(patterns) != 2 {
t.Errorf("want 2 patterns from ParseUserPatterns(testData); have %d", len(patterns))
}
cases := []struct {
i int
in string
expected bool
}{
{0, "//someuser:[email protected]", true},
{0, "https://someuser:[email protected]", true},
{0, "[email protected]", false},
{1, "eyJmb28iOiAxMjN9Cg==", true},
{1, "eyJ:-)b28iOiAxMjN9Cg==", false},
{1, "foobareyJmb28iOiAxMjN9Cg==", false},
}
for _, c := range cases {
if patterns[c.i].MatchValue(c.in) != c.expected {
t.Errorf(
"Want %t for (%s).MatchValue(%s); have %t",
c.expected, patterns[c.i].reValue, c.in, !c.expected,
)
}
}
}
func TestParseUserPatternsBadPattern(t *testing.T) {
testData := strings.NewReader(`[
{"name": "httpAuth", "pattern": "/[a-z0-9_/\\.:-]+@[a-z0-9-]+\\.[a-z0-9.-]+"},
{"name": "base64", "pattern": "^(eyJ|YTo|Tzo|PD[89]|aHR0cHM6L|aHR0cDo|rO0[%a-zA-Z0-9+/]+={0,2}"}
]`)
_, err := ParseUserPatterns(testData)
if err == nil {
t.Error("want non-nil error for ParseUserPatterns(testData) with bad pattern; but have nil", err)
}
}
func TestParseUserPatternsBadJSON(t *testing.T) {
testData := strings.NewReader(`[
{"name": "httpAuth", "pattern": "/[a-z0-9_/\\.:-]+@[a-z0-9-]+\\.[a-z0-9.-]+"},
{"name": "base64", "pattern": "^(eyJ|YTo|Tzo|PD
]`)
_, err := ParseUserPatterns(testData)
if err == nil {
t.Error("want non-nil error for ParseUserPatterns(testData) with bad JSON; but have nil", err)
}
}