forked from intenthq/anon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anoncustom_test.go
178 lines (160 loc) · 10.1 KB
/
anoncustom_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFIO(t *testing.T) {
voc.reset()
name := "fio"
regexp := "([A-Z][a-zA-Z\\-]{1,})(\\s+[A-Z][a-zA-Z\\-]{1,})(\\s+[A-Z][a-zA-Z\\-]{1,})?"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("Replace FIO", func(t *testing.T) {
res, err := f("attribute=John Marshall Doe asdasd=Jehny Marshall")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "attribute=NTest0 NTest1 NTest2 asdasd=NTest3 NTest1", res, "should return changed input")
res, err = f("attribute=Alice Smith")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "attribute=NTest4 NTest5", res, "should return changed input")
})
}
func TestFIOInitials(t *testing.T) {
voc.reset()
name := "fio_initials"
regexp := "([A-Z][a-zA-Z\\-]{1,})(\\s+[A-Z]\\.)(\\s*[A-Z]\\.)?"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("Replace FIO", func(t *testing.T) {
res, err := f("attribute=John D. D.")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "attribute=NTest0 X.X.", res, "should return changed input")
res, err = f("attribute=Peter G.G.")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "attribute=NTest1 X.X.", res, "should return changed input")
})
}
func TestAnyName(t *testing.T) {
voc.reset()
name := "name"
regexp := "([A-Z][a-zA-Z\\-]{1,})"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("Replace Name", func(t *testing.T) {
res, err := f("attribute=John Doe")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "attribute=NTest0 NTest1", res, "should return changed input")
})
}
func TestIP(t *testing.T) {
voc.reset()
name := "ip"
regexp := "\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("Replace IP", func(t *testing.T) {
res, err := f("attribute=127.0.0.1")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "attribute=***.***.0.1", res, "should return changed input")
})
}
func TestPhone(t *testing.T) {
voc.reset()
name := "phone"
regexp := "\\b[1,3]9[0-9]{9,11}\\b"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("Replace Phone", func(t *testing.T) {
res, err := f("attribute=19251234567")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "attribute=1925*******", res, "should return changed input")
})
}
func TestOTP(t *testing.T) {
voc.reset()
name := "numbers"
regexp := "\\b[0-9]{4}\\b"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("Replace OTP", func(t *testing.T) {
res, err := f("otp=1234")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "otp=****", res, "should return changed input")
})
}
func TestBirthdate(t *testing.T) {
voc.reset()
name := "birthdate"
regexp := "\\b[0-9]{4}\\-[0-9]{1,2}\\-[0-9]{1,2}\\b"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("Replace birthdate", func(t *testing.T) {
res, err := f("bd=1970-12-12")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "bd=****-**-**", res, "should return changed input")
})
}
func TestBase64(t *testing.T) {
name := "clear"
regexp := "\\b[A-Za-z0-9+/]{1000,}(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("clear blob", func(t *testing.T) {
res, err := f("photo=iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAA3NCSVQICAjb4U/gAAAACXBIWXMAABuvAAAbrwFeGpEcAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAArtQTFRF////AAAAHR0bAAAAHR0bAAAAAAAAHR0bHR0bMzMzHR0bKysrHR0bJCQkHR0bICAgHR0bFBQUHR0bJCQSHR0bIiIiICAgHh4eHBwcHR0bGxsbGhoaGBgYHR0bIyMXHR0bISEWICAgHR0bHR0dHBwcHR0bHR0bHR0bICAYHh4eHBwcGxsbICAaHx8ZHh4YHh4eHR0bHR0dHR0bGxsbHR0bGxsbHx8aHx8aHR0bHh4ZHR0dHR0bHR0dHBwcHR0bHBwcHR0bGxsbHR0bGxsbHR0bHR0bHR0bHBwcHR0bGxsbHx8bHh4bHh4aHR0bHh4aHR0bHR0dHR0bHx8bHh4bHh4aHR0aHR0bHR0aHR0aHBwcHBwcHh4bHR0bHBwcHBwcHR0bHBwcHh4bHR0aHBwaHBwcHR0bHh4bHR0bHR0bHR0aHR0aHR0bHR0bHh4cHR0bHR0bHR0bHR0aHh4cHh4cHR0bHh4bHR0bHR0bHR0bHR0bHBwaHR0bHh4cHR0bHR0bHR0bHR0bHR0bHBwaHR0bHh4cHR0cHR0bHR0bHR0bHBwbHBwaHR0bHR0cHR0bHR0bHR0bHBwbHh4aHR0cHR0bHR0bHR0bHBwbHR0bHR0bHR0bHBwbHR0bHR0aHR0cHR0bHR0bHBwbHR0bHBwbH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1yQ/td89L/ohHS/6oX0v+yGdL/uh5DuFx4R0v3KJ0K6X3pFSPdrvwMjoevF54GFpu/V7wHS9fI7IQ2v//8PWkxwMwtFunIAAAAASUVORK5CYII=")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "photo=", res, "should return changed input")
})
}
func TestReplaceLogin(t *testing.T) {
name := "hash"
regexp := "\\basd[0-9]{1,6}\\b"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("clear blob", func(t *testing.T) {
res, err := f("cn=asd12345")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "cn=293662475dbe4235a50a011325b9e878194dfcca", res, "should return changed input")
})
}
func TestReplaceCard(t *testing.T) {
name := "card"
regexp := "\\b\\d{16}\\b"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("clear blob", func(t *testing.T) {
res, err := f("card: 1234123412341234")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "card: 1234********1234", res, "should return changed input")
})
}
func TestReplaceEmail(t *testing.T) {
name := "email"
regexp := "\\b[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+\\b"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("clear blob", func(t *testing.T) {
res, err := f("email: [email protected]")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "email: [email protected]", res, "should return changed input")
})
}
func TestComplex(t *testing.T) {
voc.reset()
name := "phone"
regexp := "\\b[7,8]9[0-9]{9,11}\\b"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("Replace Name and Phone", func(t *testing.T) {
res, err := f("attribute=89251234567")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "attribute=8925*******", res, "should return changed input")
})
}
func TestHostname(t *testing.T) {
vocHosts.reset()
name := "hostname"
regexp := "([a-zA-Z0-9-.]*google\\.com)|([a-zA-Z0-9-.]*go\\.gl)"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("Replace complex regexp", func(t *testing.T) {
res, err := f("server=maps.google.com server2=test.google.com")
assert.NoError(t, err, "should return no error")
assert.Equal(t, "server=NTesthost0 server2=NTesthost1", res, "should return changed input")
})
}
func TestReplaceMiddleGroup(t *testing.T) {
name := "replaceMiddleGroup"
regexp := "(\"value\"\\:\\s*\")([\\w\\W]*)(\")"
f, _ := custom([]CustomConfig{CustomConfig{Name: name, Regexp: regexp}})
t.Run("Replace middle grop regexp", func(t *testing.T) {
res, err := f(`"value": "login"`)
assert.NoError(t, err, "should return no error")
assert.Equal(t, `"value": "********"`, res, "should return changed input")
})
}