-
Notifications
You must be signed in to change notification settings - Fork 0
/
redactor_test.go
287 lines (276 loc) · 7.59 KB
/
redactor_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package redact
import "testing"
func assertRedaction(t *testing.T, redaction *Redactor, input, expected string) {
inputByte := []byte(input)
actual := redaction.RedactAll(inputByte)
if string(actual) == expected {
return
}
t.Helper()
t.Errorf("\n"+
"Expected: %s\n"+
"Actual: %s",
expected,
actual,
)
}
func BenchmarkThing(b *testing.B) {
redaction := New()
b.ReportAllocs()
b.ResetTimer()
input := []byte("+8014890464 Hello 6749-3-2345 (801)4890464there, my name is stuff. 1200 East 1200 North Mapleton " +
"1(385)6668330. 18014890464 371449635398431numMayers 12, 1970ber is 647-48-6867. I " +
"9/1/2020 to fill these wo04/16/1999rds in with other 371 449 635 398 1945/05/01 " +
"[email protected]. 4111-111-111-111-111 pasting 02/14/1900the 647-21-12398 best of6011111111111117 the" +
"valid, and Jan 32, 1990someMarch 12, 2020 are not. 647489009 This is a vDecember 111, 2000ey fun task to do. " +
"647 40 4444 1+(801)4890464 647-48-9012")
for n := 0; n < b.N; n++ {
_ = redaction.RedactAll(input)
}
}
func TestRedactCreditCard_Valid_Redaction(t *testing.T) {
t.Parallel()
redaction := New()
assertRedaction(t, redaction,
"4111 1111 1111 1101 111 4111-1111-1111-1111. taco ",
"*********************** *******************. taco ",
)
assertRedaction(t, redaction,
"taco 6011-0009-9013-9424",
"taco *******************",
)
assertRedaction(t, redaction,
"6011-0009-9013-9424 ",
"******************* ",
)
assertRedaction(t, redaction,
" 4111111111111101111 ",
" ******************* ",
)
assertRedaction(t, redaction,
"4111111111111101111",
"*******************",
)
assertRedaction(t, redaction,
"taco 3714-496353-98431 - O ",
"taco ***************** - O ",
)
assertRedaction(t, redaction,
"6011-0009-9013-9424 taco.",
"******************* taco.",
)
assertRedaction(t, redaction,
"4111111111111101111TEST",
"*******************TEST",
)
assertRedaction(t, redaction,
"411 1111 1111 1110 1111ST",
"***********************ST",
)
}
func TestRedactCreditCard_Invalid_NoRedaction(t *testing.T) {
t.Parallel()
redaction := New()
assertRedaction(t, redaction,
"6011 0009-9013-9424 taco.",
"6011 0009-9013-9424 taco.",
)
assertRedaction(t, redaction,
"601100 099 013 9424 taco.",
"601100 099 013 9424 taco.",
)
assertRedaction(t, redaction,
"6011 0009 9013 9424 taco.",
"6011 0009 9013 9424 taco.",
)
assertRedaction(t, redaction,
"423432343 111110101 ",
"423432343 111110101 ",
)
assertRedaction(t, redaction,
"523d0555760656D3FC1D315E8",
"523d0555760656D3FC1D315E8",
)
assertRedaction(t, redaction,
"4111 1111 1111 11 10 111",
"4111 1111 1111 11 10 111",
)
assertRedaction(t, redaction,
"4111 1111 1111 1110-111",
"4111 1111 1111 1110-111",
)
assertRedaction(t, redaction,
"1234-1234-1243-1234 ",
"1234-1234-1243-1234 ",
)
assertRedaction(t, redaction,
"4011111111111101111",
"4011111111111101111",
)
assertRedaction(t, redaction,
"6011-0o09-9013-9424 ",
"6011-0o09-9013-9424 ",
)
assertRedaction(t, redaction,
"+41 11 111 11 0",
"+41 11 111 11 0",
)
}
func TestRedactEmail_Valid_Redaction(t *testing.T) {
t.Parallel()
redaction := New()
assertRedaction(t, redaction,
"****@test.com",
)
assertRedaction(t, redaction,
"Blah [email protected], our employee's email is test@gmail. and we have one more which may or not be an email test@test taco",
"Blah *********@gmail.com, our employee's email is ****@gmail. and we have one more which may or not be an email ****@test taco",
)
}
func TestRedactEmail_Invalid_NoRedaction(t *testing.T) {
t.Parallel()
redaction := New()
assertRedaction(t, redaction,
"Blah test.gmail.com",
"Blah test.gmail.com",
)
}
func TestRedactPhone_Valid_Redaction(t *testing.T) {
t.Parallel()
redaction := New()
assertRedaction(t, redaction,
"801-111-1111 +1(801)111-1111 taco",
"************ +************** taco",
)
assertRedaction(t, redaction,
"+1(801)111-1111 taco",
"+************** taco",
)
assertRedaction(t, redaction,
"801-111-1111 and (801) 111-1111 +1(801)111-1111 taco",
"************ and (801) 111-1111 +************** taco",
)
assertRedaction(t, redaction,
"Blah 801-111-1111 and (801) 111-1111 +1(801)111-1111 taco",
"Blah ************ and (801) 111-1111 +************** taco",
)
}
func TestRedactPhone_Invalid_NoRedaction(t *testing.T) {
t.Parallel()
redaction := New()
assertRedaction(t, redaction,
"40512-4618",
"40512-4618",
)
assertRedaction(t, redaction,
"405-124618",
"405-124618",
)
assertRedaction(t, redaction,
"This is not valid: 801 111 1111",
"This is not valid: 801 111 1111",
)
}
func TestRedactSSN_Valid_Redaction(t *testing.T) {
t.Parallel()
redaction := New()
assertRedaction(t, redaction,
"Blah 123-12-1234.",
"Blah ***********.",
)
assertRedaction(t, redaction,
"123 12 1234 taco",
"*********** taco",
)
}
func TestRedactSSN_Invalid_NoRedaction(t *testing.T) {
t.Parallel()
redaction := New()
assertRedaction(t, redaction,
" 123-121234 taco",
" 123-121234 taco",
)
assertRedaction(t, redaction,
"450 900 100",
"450 900 100",
)
}
func TestRedactDOB_Valid_Redaction(t *testing.T) {
t.Parallel()
redaction := New()
assertRedaction(t, redaction,
"stuff February 1, 2020 ",
"stuff **************** ",
)
assertRedaction(t, redaction,
"Feb 01, 2012",
"************",
)
assertRedaction(t, redaction,
" APRIL 3, 2019",
" *************",
)
assertRedaction(t, redaction,
"Blah 12-01-1998 and 12/01/1998 ",
"Blah ********** and ********** ",
)
assertRedaction(t, redaction,
"Blah 12-12-1998 and 01/01/1998 ",
"Blah ********** and ********** ",
)
assertRedaction(t, redaction,
"1982/11/8",
"*********",
)
assertRedaction(t, redaction,
"Jan 1, 2021 ",
"*********** ",
)
}
func TestRedactDOB_Invalid_NoRedaction(t *testing.T) {
t.Parallel()
redaction := New()
assertRedaction(t, redaction,
" Apr 39, 2021 ",
" Apr 39, 2021 ",
)
assertRedaction(t, redaction,
"April 21, 2025",
"April 21, 2025",
)
assertRedaction(t, redaction,
" 7/13/2023",
" 7/13/2023",
)
assertRedaction(t, redaction,
"30-12-12",
"30-12-12",
)
assertRedaction(t, redaction,
"1/12/2123",
"1/12/2123",
)
assertRedaction(t, redaction,
"[5-4-212/80]",
"[5-4-212/80]",
)
}
func TestRedactMaxLength(t *testing.T) {
t.Parallel()
redaction := New()
assertRedaction(t, redaction,
"93087097919637351852097735514114210893391460226901438681143583309305538438469497356679189567980884261"+
"29971266084194311662842664602358893723650394247864792157944906026710951003240151287380372698948380361"+
"55164632037694293000947090031773467719395901857854689140563439380055959763377841900038677981407681556"+
"31549026189478389556566369157325037087613078605916540148639313073551273625059514900828293261488835150"+
"67313603759318833137870438235772016686252244297993834557723091360234446940034078073980985453311613448"+
"99214471370356055168014161500038074791912958123329574660979012768873774058837975111111121212121212121212"+
"99214471370356055168014161500038074791912958123329574660979012768873774058837975111111121212121212121212",
"930870979196373518520977355141142108933914602269014386811435833093055384384694973566791895679808842612"+
"997126608419431166284266460235889372365039424786479215794490602671095100324015128738037269894838036155164632037"+
"694293000947090031773467719395901857854689140563439380055959763377841900038677981407681556315490261894783895565"+
"663691573250370876130786059165401486393130735512736250595149008282932614888351506731360375931883313787043823577"+
"20166862522442979938345577230913602344469400340780739809854533116134489921447",
)
}