-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_test.go
85 lines (69 loc) · 1.74 KB
/
string_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
package gopii
import (
"strconv"
"testing"
"github.com/matryer/is"
)
const (
typePlain = PartType(iota)
typeLogin
typePassword
)
const (
levelDevelopment = Level(4)
levelLog = Level(8)
levelAuditLog = Level(12)
levelInternet = Level(16)
levelMax = Level(100)
)
func TestString_String(t *testing.T) {
obscureFuncs := map[PartType]map[Level]ObscureFunc{
typePlain: {
levelMax: Plain(),
},
typeLogin: {
levelDevelopment: Plain(), // user123456789 -> user123456789
levelLog: KeepFirstLast(4, 3), // user123456789 -> user***789
levelAuditLog: KeepFirst(3), // user123456789 -> use***
levelInternet: KeepFirst(1), // user123456789 -> u***
},
typePassword: {
levelDevelopment: Plain(), // pass123456789 -> pass123456789
},
}
str := NewString(obscureFuncs,
"user123456789", typeLogin,
":", typePlain,
"pass123456789", typePassword,
)
tests := []struct {
outLevel Level
want string
}{
{0, "user123456789:pass123456789"},
{levelDevelopment, "user123456789:pass123456789"},
{levelLog - 2, "user***789:***"},
{levelLog, "user***789:***"},
{levelAuditLog - 2, "use***:***"},
{levelAuditLog, "use***:***"},
{levelInternet - 2, "u***:***"},
{levelInternet, "u***:***"},
{100, "***:***"},
}
for _, test := range tests {
t.Run("level "+strconv.Itoa(int(test.outLevel)), func(t *testing.T) {
is := is.New(t)
is.Equal(str.String(test.outLevel), test.want)
})
}
}
func TestString_String_NoFunc(t *testing.T) {
is := is.New(t)
obscureFuncs := map[PartType]map[Level]ObscureFunc{}
str := NewString(obscureFuncs,
"user123456789", typeLogin,
":", typePlain,
"pass123456789", typePassword,
)
is.Equal(str.String(levelLog), "*********")
}