-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomain_test.go
353 lines (315 loc) · 6.78 KB
/
domain_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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// CGo binding for Avahi
//
// Copyright (C) 2024 and up by Alexander Pevzner ([email protected])
// See LICENSE for license terms and conditions
//
// Domain name test
//
//go:build linux || freebsd
package avahi
import (
"reflect"
"testing"
)
// TestDomainFrom tests DomainFrom function
func TestDomainFrom(t *testing.T) {
type testData struct {
labels []string
domain string
}
tests := []testData{
{
labels: []string{`example`, `com`},
domain: `example.com`,
},
{
labels: []string{`My.Service`, `example`, `com`},
domain: `My\.Service.example.com`,
},
{
labels: []string{`My\Service`, `example`, `com`},
domain: `My\\Service.example.com`,
},
}
for _, test := range tests {
domain := DomainFrom(test.labels)
if domain != test.domain {
t.Errorf("%q:\n"+
"expected: %q\n"+
"present: %q\n",
test.labels, test.domain, domain)
}
}
}
// TestDomainSlice tests DomainSlice function
func TestDomainSlice(t *testing.T) {
type testData struct {
domain string
labels []string
}
tests := []testData{
{
domain: `example.com`,
labels: []string{"example", "com"},
},
{
domain: `ex\?ample.com`,
labels: nil,
},
}
for _, test := range tests {
labels := DomainSlice(test.domain)
if !reflect.DeepEqual(labels, test.labels) {
t.Errorf("%q:\n"+
"expected: %q\n"+
"present: %q\n",
test.domain, test.labels, labels)
}
}
}
// TestDomainEqual tests DomainEqual function
func TestDomainEqual(t *testing.T) {
type testData struct {
d1, d2 string
equal bool
}
tests := []testData{
{
// Equal domains
d1: `example.com`,
d2: `example.com`,
equal: true,
},
{
// Different domains
d1: `www.example.com`,
d2: `xxx.example.com`,
equal: false,
},
{
// Different number of labels
d1: `www.example.com`,
d2: `example.com`,
equal: false,
},
{
// ASCII case must be ignored
d1: `ExAmPlE.CoM`,
d2: `eXaMpLe.cOm`,
equal: true,
},
{
// UTF-8 must not be a problem
d1: `пример.example.com`,
d2: `пример.example.com`,
equal: true,
},
{
// UTF-8 case must not be ignored
d1: `пример.example.com`,
d2: `ПРИМЕР.example.com`,
equal: false,
},
{
// Invalid domains are never equal
d1: `ex\?ample.com`,
d2: `ex\?ample.com`,
equal: false,
},
}
for _, test := range tests {
equal := DomainEqual(test.d1, test.d2)
if equal != test.equal {
t.Errorf("%q vs %q:\n"+
"expected: %v\n"+
"present: %v\n",
test.d1, test.d2, test.equal, equal)
}
}
}
// TestDomainNormalize tests DomainNormalize function
func TestDomainNormalize(t *testing.T) {
type testData struct {
in, out string
}
tests := []testData{
{
in: `example.com`,
out: `example.com`,
},
{
in: `ex\?ample.com`,
out: ``,
},
{
in: `ex ample.com`,
out: `ex ample.com`,
},
}
for _, test := range tests {
out := DomainNormalize(test.in)
if out != test.out {
t.Errorf("%q:\n"+
"expected: %q\n"+
"present: %q\n",
test.in, test.out, out)
}
}
}
// TestDomainToLowerUpper tests DomainToLower and DomainToUpper function
func TestDomainToLowerUpper(t *testing.T) {
type testData struct {
domain string
lower, upper string
}
tests := []testData{
{
domain: `1.2.3.example.com`,
lower: `1.2.3.example.com`,
upper: `1.2.3.EXAMPLE.COM`,
},
{
domain: `1.2.3.EXAMPLE.COM`,
lower: `1.2.3.example.com`,
upper: `1.2.3.EXAMPLE.COM`,
},
{
domain: `привет.example.com`,
lower: `привет.example.com`,
upper: `привет.EXAMPLE.COM`,
},
{
domain: `ПРИВЕТ.example.com`,
lower: `ПРИВЕТ.example.com`,
upper: `ПРИВЕТ.EXAMPLE.COM`,
},
}
for _, test := range tests {
lower := DomainToLower(test.domain)
upper := DomainToUpper(test.domain)
if lower != test.lower {
t.Errorf("DomainToLower(%q):\n"+
"expected: %q\n"+
"present: %q\n",
test.domain, test.lower, lower)
}
if upper != test.upper {
t.Errorf("DomainToUpper(%q):\n"+
"expected: %q\n"+
"present: %q\n",
test.domain, test.upper, upper)
}
}
}
// TestDomainServiceNameSplit tests DomainServiceNameSplit function
func TestDomainServiceNameSplit(t *testing.T) {
type testData struct {
input string
instance, svctype, domain string
}
tests := []testData{
{
// Full name
input: `Kyocera ECOSYS M2040dn._ipp._tcp.local`,
instance: "Kyocera ECOSYS M2040dn",
svctype: "_ipp._tcp",
domain: "local",
},
{
// Missed domain
input: `Kyocera ECOSYS M2040dn._ipp._tcp`,
instance: "Kyocera ECOSYS M2040dn",
svctype: "_ipp._tcp",
domain: "",
},
{
// Long domain
input: `Kyocera ECOSYS M2040dn._ipp._tcp.example.com`,
instance: "Kyocera ECOSYS M2040dn",
svctype: "_ipp._tcp",
domain: "example.com",
},
{
// Service type with subtype
input: `Kyocera ECOSYS M2040dn._subtype._ipp._tcp.local`,
instance: "Kyocera ECOSYS M2040dn",
svctype: "_subtype._ipp._tcp",
domain: "local",
},
{
// Invalid service type
input: `Kyocera ECOSYS M2040dn._tcp.local`,
instance: "",
svctype: "",
domain: "",
},
{
// Invalid input domain
input: `www.ex\?ample.com`,
instance: "",
svctype: "",
domain: "",
},
}
for _, test := range tests {
instance, svctype, domain := DomainServiceNameSplit(test.input)
if instance != test.instance ||
svctype != test.svctype ||
domain != test.domain {
t.Errorf("%q:\n"+
"expected: %q %q %q\n"+
"present: %q %q %q\n",
test.input,
test.instance, test.svctype, test.domain,
instance, svctype, domain)
}
}
}
// TestDomainServiceNameJoin tests DomainServiceNameJoin function
func TestDomainServiceNameJoin(t *testing.T) {
type testData struct {
instance, svctype, domain string
output string
}
tests := []testData{
{
// Normal case
instance: "Kyocera ECOSYS M2040dn",
svctype: "_ipp._tcp",
domain: "local",
output: `Kyocera ECOSYS M2040dn._ipp._tcp.local`,
},
{
// Empty instance not allowed
instance: "",
svctype: "_ipp._tcp",
domain: "local",
output: ``,
},
{
// Empty service type not allowed
instance: "Kyocera ECOSYS M2040dn",
svctype: "",
domain: "local",
output: ``,
},
{
// Empty domain is allowed
instance: "Kyocera ECOSYS M2040dn",
svctype: "_ipp._tcp",
domain: "",
output: `Kyocera ECOSYS M2040dn._ipp._tcp`,
},
}
for _, test := range tests {
output := DomainServiceNameJoin(test.instance,
test.svctype, test.domain)
if output != test.output {
t.Errorf("[%q %q %q]:\n"+
"expected: %q\n"+
"present: %q\n",
test.instance, test.svctype, test.domain,
test.output, output)
}
}
}