-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathgeneric_test.go
More file actions
90 lines (83 loc) · 2.43 KB
/
Copy pathgeneric_test.go
File metadata and controls
90 lines (83 loc) · 2.43 KB
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
package masker
import "testing"
func TestParseGenericMask(t *testing.T) {
tests := []struct {
tag string
value string
want string
matched bool
wantErr bool
}{
{tag: "all", value: "hello", want: "*****", matched: true},
{tag: "all", value: "", want: "", matched: true},
{tag: "first-3", value: "hello", want: "***lo", matched: true},
{tag: "first-0", value: "hello", want: "hello", matched: true},
{tag: "first-10", value: "hello", want: "*****", matched: true},
{tag: "last-3", value: "hello", want: "he***", matched: true},
{tag: "last-0", value: "hello", want: "hello", matched: true},
{tag: "last-10", value: "hello", want: "*****", matched: true},
{tag: "name", value: "hello", want: "", matched: false},
{tag: "first-abc", value: "hello", want: "", matched: false, wantErr: true},
{tag: "last-abc", value: "hello", want: "", matched: false, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.tag+"/"+tt.value, func(t *testing.T) {
got, matched, err := parseGenericMask("*", tt.tag, tt.value)
if matched != tt.matched {
t.Errorf("matched = %v, want %v", matched, tt.matched)
}
if (err != nil) != tt.wantErr {
t.Errorf("err = %v, wantErr %v", err, tt.wantErr)
}
if matched && !tt.wantErr && got != tt.want {
t.Errorf("got = %v, want %v", got, tt.want)
}
})
}
}
func TestMarshal_GenericTags(t *testing.T) {
m := NewMaskerMarshaler()
tests := []struct {
tag MaskerType
value string
want string
}{
{MaskerTypeAll, "hello", "*****"},
{"first-2", "hello", "**llo"},
{"last-2", "hello", "hel**"},
}
for _, tt := range tests {
t.Run(string(tt.tag)+"/"+tt.value, func(t *testing.T) {
got, err := m.Marshal(tt.tag, tt.value)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tt.want {
t.Errorf("got = %v, want %v", got, tt.want)
}
})
}
}
func TestStruct_GenericTags(t *testing.T) {
type Secret struct {
SSN string `mask:"all"`
Code string `mask:"first-3"`
Suffix string `mask:"last-4"`
}
m := NewMaskerMarshaler()
in := &Secret{SSN: "123456789", Code: "ABCDEF", Suffix: "ABCDEF"}
out, err := m.Struct(in)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
s := out.(*Secret)
if s.SSN != "*********" {
t.Errorf("SSN got %v, want *********", s.SSN)
}
if s.Code != "***DEF" {
t.Errorf("Code got %v, want ***DEF", s.Code)
}
if s.Suffix != "AB****" {
t.Errorf("Suffix got %v, want AB****", s.Suffix)
}
}