-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers_test.go
More file actions
319 lines (300 loc) · 8.53 KB
/
helpers_test.go
File metadata and controls
319 lines (300 loc) · 8.53 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package normalizer
import (
"strings"
"testing"
)
func Test_collapseDots(t *testing.T) {
tests := []struct {
name string
s string
want string
}{
{"empty", "", ""},
{"simple", "a.b.c", "a.b.c"},
{"...", "...", "."},
{"test.com", "test....com", "test.com"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := collapseDots(tt.s); got != tt.want {
t.Errorf("collapseDots() = %v, want %v", got, tt.want)
}
})
}
}
func Test_collapseSpaces(t *testing.T) {
tests := []struct {
name string
s string
want string
}{
{"empty", "", ""},
{"simple", "a b c", "a b c"},
{"spaces", " ", " "},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := collapseSpaces(tt.s); got != tt.want {
t.Errorf("collapseSpaces() = %v, want %v", got, tt.want)
}
})
}
}
func Test_collapseDashes(t *testing.T) {
tests := []struct {
name string
s string
want string
}{
{"empty", "", ""},
{"simple", "a---b--c", "a-b-c"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := collapseDashes(tt.s); got != tt.want {
t.Errorf("collapseDashes() = %v, want %v", got, tt.want)
}
})
}
}
func Test_stripInvalidChars(t *testing.T) {
tests := []struct {
name string
s string
want string
}{
{"empty", "", ""},
{"simple", "a.b.c", "a.b.c"},
{"has invalid chars", "^&some_-domain.com!,", "some-domain.com"},
{"numbers", "123.456", "123.456"},
{"unicode letters", "пример.рф", "пример.рф"},
{"unicode with invalid", "тест$$$.рф", "тест.рф"},
{"emoji filtered", "😀example.com", "example.com"},
{"spaces removed", "foo bar com", "foobarcom"},
{"non-breaking hyphen", "go\u2011lang.org", "golang.org"},
{"en/em dash removed", "go\u2013long\u2014domain.com", "golongdomain.com"},
{"punctuation storm", "ex,am;ple:.co!m", "example.com"},
{"multiple symbols", "@@@exa!!!mple###.com$$$", "example.com"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := stripInvalidChars(tt.s); got != tt.want {
t.Errorf("stripInvalidChars() = %q, want %q", got, tt.want)
}
})
}
}
func Test_trimDashes(t *testing.T) {
tests := []struct {
name string
s string
want string
}{
{"empty", "", ""},
{"dash only", "---", ""},
{"simple", "--a.b.c-", "a.b.c"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := trimDashes(tt.s); got != tt.want {
t.Errorf("trimDashes() = %v, want %v", got, tt.want)
}
})
}
}
func Test_trimDots(t *testing.T) {
tests := []struct {
name string
s string
want string
}{
{"empty", "", ""},
{"dots only", "...", ""},
{"simple", "..a.b.c.", "a.b.c"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := trimDots(tt.s); got != tt.want {
t.Errorf("trimDots() = %v, want %v", got, tt.want)
}
})
}
}
func Test_trimSpaceArounDot(t *testing.T) {
tests := []struct {
name string
s string
want string
}{
{"empty", "", ""},
{"dot only", " . ", "."},
{"simple", " . a . b . c . ", ".a.b.c."},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := trimSpacesAroundDot(tt.s); got != tt.want {
t.Errorf("trimSpacesAroundDot() = %v, want %v", got, tt.want)
}
})
}
}
func Test_normalizeSpaces(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{"empty", "", ""},
{"ascii space", "a b c", "a b c"},
{"tabs", "a\t\tb\tc", "a b c"},
{"newlines", "a\nb\r\nc", "a b c"},
{"mixed ws", " a \t b \n c ", " a b c "},
{"nbsp", "a\u00A0\u00A0b\u00A0c", "a b c"},
{"thin/figure spaces", "a\u2009b\u2007c", "a b c"},
{"em space", "a\u2003\u2003b", "a b"},
{"nnbsp", "a\u202Fb\u202Fc", "a b c"},
{"zero width (kept, not Z)", "a\u200Db", "a\u200Db"},
{"leading/trailing", "\u2003 a \u2003", " a "},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := normalizeSpaces(tt.in); got != tt.want {
t.Errorf("normalizeSpaces() = %q, want %q", got, tt.want)
}
})
}
}
func Test_normalizeSepRuns(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{"empty", "", ""},
{"single dot", ".", "."},
{"single dash", "-", "-"},
{"dots only", "....", "."},
{"dashes only", "----", "-"},
{"dash then dot (pair)", "-.", "."},
{"dot then dash (pair)", ".-", "."},
{"mixed short", "-.-", "-"},
{"mixed long", "--.-.-", "-"},
{"dot-run between words", "foo...bar", "foo.bar"},
{"dash-run between words", "foo---bar", "foo-bar"},
{"mixed cluster between words", "foo--.-.-bar", "foo-bar"},
{"leading mixed", "-.-.-foo", "-foo"},
{"trailing mixed", "bar-.-.-", "bar-"},
{"around dot kept as dot", "foo-.bar.-baz", "foo.bar.baz"},
{"only mixed becomes dash", "-.-.-", "-"},
{"dot and dash mess full", "..foo--.-.-bar..", ".foo-bar."},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := normalizeSepRuns(tt.in); got != tt.want {
t.Errorf("normalizeSepRuns() = %q, want %q", got, tt.want)
}
})
}
}
func Test_normalizeDotLikes(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{"empty", "", ""},
{"plain dot", ".", "."},
{"ideographic full stop", "\u3002", "."},
{"fullwidth full stop", "\uFF0E", "."},
{"halfwidth middle dot", "\uFF61", "."},
{"ascii unchanged", "example.com", "example.com"},
{"ideographic dot in domain", "example\u3002com", "example.com"},
{"fullwidth dot in domain", "example\uFF0Ecom", "example.com"},
{"halfwidth dot in domain", "example\uFF61com", "example.com"},
{"mixed dotlikes", "a\u3002b\uFF0Ec\uFF61d", "a.b.c.d"},
{"no dotlikes", "foo-bar", "foo-bar"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := normalizeDotLikes(tt.in); got != tt.want {
t.Errorf("normalizeDotLikes() = %q, want %q", got, tt.want)
}
})
}
}
func Test_normalizeLabelsKeepPunycode(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{"empty", "", ""},
{"simple domain", "example.com", "example.com"},
{"preserve punycode", "xn--d1acufc.xn--p1ai", "xn--d1acufc.xn--p1ai"},
{"fix single-dash punycode", "xn-d1acufc.xn-p1ai", "xn--d1acufc.xn--p1ai"},
{"collapse extra dashes after xn", "xn----d1acufc", "xn--d1acufc"},
{"trim label edges", "-foo.-bar-", "foo.bar"},
{"remove empty labels", "a..b...c", "a.b.c"},
{"mix: puny + trims", "-xn---test-.", "xn--test"},
{"puny with edges", "-xn-d1acufc-", "xn--d1acufc"},
{"puny collapsed head", "xn-----abc", "xn--abc"},
{"puny just prefix", "xn--", "xn--"},
{"only dashes label", "----", ""},
{"inner double dashes kept", "foo--bar.com", "foo--bar.com"},
{"keep multiple labels", "sub.-mid-.tail.", "sub.mid.tail"},
{"leading/trailing dots", ".foo.bar.", "foo.bar"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := normalizeLabelsKeepPunycode(tt.in); got != tt.want {
t.Errorf("normalizeLabelsKeepPunycode() = %q, want %q", got, tt.want)
}
})
}
}
func Test_validateASCII(t *testing.T) {
makeDomain := func(labels ...string) string { return strings.Join(labels, ".") }
l63a := strings.Repeat("a", 63)
l63b := strings.Repeat("b", 63)
l63c := strings.Repeat("c", 63)
l61d := strings.Repeat("d", 61)
l62d := strings.Repeat("d", 62)
dom253 := makeDomain(l63a, l63b, l63c, l61d)
dom254 := makeDomain(l63a, l63b, l63c, l62d)
label64 := strings.Repeat("x", 64)
tests := []struct {
name string
ascii string
wantErr bool
}{
{"empty", "", true},
{"single label ok", "localhost", false},
{"simple domain ok", "example.com", false},
{"hyphens inside ok", "foo-bar.baz-qux", false},
{"starts with hyphen", "-abc.com", true},
{"ends with hyphen", "abc-.com", true},
{"double dot (empty label)", "a..b", true},
{"trailing dot (empty last label)", "example.com.", true},
{"leading dot (empty first label)", ".example.com", true},
{"label length = 63 ok", makeDomain(l63a, "com"), false},
{"label length > 63", makeDomain(label64, "com"), true},
{"domain length = 253 ok", dom253, false},
{"domain length > 253", dom254, true},
{"punycode label ok", "xn--d1acufc.xn--p1ai", false},
{"numeric label ok", "123.456", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var parts []string
if tt.ascii != "" {
parts = strings.Split(tt.ascii, ".")
} else {
parts = nil
}
err := validateASCII(tt.ascii, parts)
if (err != nil) != tt.wantErr {
t.Fatalf("validateASCII(%q) err=%v, wantErr=%v", tt.ascii, err, tt.wantErr)
}
})
}
}