This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnet_test.go
386 lines (325 loc) · 8.83 KB
/
net_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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package aci
import (
"fmt"
"testing"
)
func TestFQDN(t *testing.T) {
var f FQDN
_ = f.Len()
_ = f.Keyword()
_ = f.Eq()
_ = f.Ne()
_ = f.Valid()
f = DNS()
var typ string = f.Keyword().String()
if f.len() != 0 {
t.Errorf("%s failed: unexpected %T length: want '%d', got '%d'",
t.Name(), f, 0, f.len())
return
}
if err := f.Valid(); err == nil {
t.Errorf("%s failed: empty %T deemed valid", t.Name(), f)
return
}
f.Set()
f.Set(``)
f.Set(`-www-`, `-example`, `com-`)
f.Set(`www`, `example`, `com`)
want := `www.example.com`
got := f.String()
if want != got {
t.Errorf("%s failed; want '%s', got '%s'", t.Name(), want, got)
return
}
absurd := `eeeeeeeeeeeeeeeeeeeeeeeee#eee^eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeexample`
if validLabel(absurd) {
t.Errorf("%s failed: bogus %T label accepted as valid (%s)",
t.Name(), absurd, absurd)
return
}
var F FQDN
if F.String() != badFQDN {
t.Errorf("%s failed: unexpected string result; want '%s', got '%s'",
t.Name(), badFQDN, F)
return
}
F.Set(`www`).Set(`$&^#*(`).Set(absurd).Set(`example`).Set(``).Set(`com`)
if llen := F.Len(); llen != 3 {
t.Errorf("%s failed; want '%d', got '%d'", t.Name(), 3, llen)
return
}
// try every comparison operator supported in
// this context ...
brm := F.BRM()
for i := 0; i < brm.Len(); i++ {
cop, meth := brm.Index(i + 1)
wcop := sprintf("( %s %s \"www.example.com\" )", f.Keyword(), cop)
if T := meth(); T.Paren().String() != wcop {
err := unexpectedStringResult(F.String(), wcop, T.String())
t.Errorf("%s [%s] multival failed [%s rule]; %s, %s: %v",
t.Name(), F.Keyword(), cop.Context(), cop.Description(), typ, err)
return
}
}
}
func TestDNS_alternativeFQDN(t *testing.T) {
want := `www.example.com`
got := DNS(`www.example.com`)
if want != got.String() {
t.Errorf("%s failed; want '%s', got '%s'", t.Name(), want, got)
return
}
}
func TestIPAddr_BRM(t *testing.T) {
var i IPAddr
_ = i.Len()
_ = i.Eq()
_ = i.Ne()
_ = i.Valid()
_ = i.Keyword()
if !i.IsZero() {
t.Errorf("%s failed: non-zero %T instance", t.Name(), i)
return
}
if got := i.String(); got != badAddr {
t.Errorf("%s failed: unexpected string result; want '%s', got '%s'",
t.Name(), badAddr, got)
return
}
var typ string = i.Keyword().String()
if !i.unique(`192.168.0`) {
t.Errorf("%s failed; uniqueness check returned bogus result",
t.Name())
return
}
i.Set(`192.168.0`)
i.Set(`12.3.45.*`)
i.Set(`12.3.45.*`) // duplicate
i.Set(`10.0.0.0/8`)
i.Valid()
i.unique(`10.0.0.0/8`)
if lens := i.Len(); lens != 3 {
t.Errorf("%s failed: bad %T length; want '%d', got '%d'", t.Name(), i, 3, lens)
return
}
if cond := i.Ne(); cond.IsZero() {
t.Errorf("%s failed: nil %T instance!", t.Name(), cond)
return
}
// try every comparison operator supported in
// this context ...
brm := i.BRM()
for j := 0; j < brm.Len(); j++ {
cop, meth := brm.Index(j + 1)
if meth == nil {
t.Errorf("%s [%s] multival failed: expected %s method (%T), got nil",
t.Name(), i.Keyword(), cop.Context(), meth)
return
}
wcop := sprintf("( %s %s %q )", i.Keyword(), cop, i)
if T := meth(); T.Paren().String() != wcop {
err := unexpectedStringResult(i.String(), wcop, T.String())
t.Errorf("%s [%s] multival failed [%s rule]: %v",
t.Name(), i.Keyword(), typ, err)
return
}
}
}
func ExampleFQDN_Eq() {
var f FQDN = DNS()
// Can be set incrementally ...
f.Set(`www`)
f.Set(`example`)
f.Set(`com`)
// OR as a whole value ...
// f.Set(`www.example.com`)
fmt.Printf("%s", f.Eq())
// Output: dns = "www.example.com"
}
func ExampleFQDN_Ne() {
var f FQDN = DNS()
// Can be set incrementally ...
f.Set(`www`)
f.Set(`example`)
f.Set(`com`)
// OR as a whole value ...
// f.Set(`www.example.com`)
fmt.Printf("%s", f.Ne())
// Output: dns != "www.example.com"
}
func ExampleIPAddr_Set() {
var i IPAddr
i.Set(`192.168.0`).Set(`12.3.45.*`).Set(`10.0.0.0/8`)
neq := i.Ne()
neq.Paren()
fmt.Printf("%s", neq)
// Output: ( ip != "192.168.0,12.3.45.*,10.0.0.0/8" )
}
func ExampleIPAddr_Eq_oneShot() {
fmt.Printf("%s", IP(`192.168.0`, `12.3.45.*`, `10.0.0.0/8`).Eq())
// Output: ip = "192.168.0,12.3.45.*,10.0.0.0/8"
}
/*
This example demonstrates the creation of an instance of IPAddr, which
is used in a variety of contexts.
In this example, a string name is fed to the package level IP function to form
a complete IPAddr instance, which is then shown in string representation.
*/
func ExampleIP() {
ip := IP(`10.0.0.1`)
fmt.Printf("%s", ip)
// Output: 10.0.0.1
}
/*
This example demonstrates the string representation of the receiver instance.
*/
func ExampleIPAddr_String() {
fmt.Printf("%s", IP(`192.168.56.7`))
// Output: 192.168.56.7
}
/*
This example demonstrates the use of the useless Keyword method, as IPAddr
instances do not have any knowledge of Keywords at this time.
*/
func ExampleIPAddr_Keyword() {
fmt.Printf("%v", IP(`10.0`).Keyword())
// Output: ip
}
/*
This example demonstrates the use of the useless Kind method, as this information
is normally derived from a Keyword, which the receiver does not have.
*/
func ExampleIPAddr_Kind() {
fmt.Printf("%s", IP(`192.168.1`).Kind())
// Output: ip
}
/*
This example demonstrates the use of the useless Len method, as this information
is only made available to satisfy Go's interface signature requirements as they
pertain to the IPAddrContext interface.
*/
func ExampleIPAddr_Len() {
fmt.Printf("%d", IP(`10.8.`).Len())
// Output: 1
}
/*
This example demonstrates a check of the receiver for "nilness".
*/
func ExampleIPAddr_IsZero() {
fmt.Printf("%t", IP(`192.168.7.*,10.,172.12.*,8.8.8.8`).IsZero())
// Output: false
}
/*
This example demonstrates a check of the receiver for an aberrant state.
*/
func ExampleIPAddr_Valid() {
ip := IP(``)
fmt.Printf("Valid: %t", ip.Valid() == nil)
// Output: Valid: false
}
func ExampleIPAddr_Eq() {
var i IPAddr
i.Set(`192.8.`).Set(`10.7.0`)
fmt.Printf("%s", i.Eq())
// Output: ip = "192.8.,10.7.0"
}
func ExampleIPAddr_Ne() {
var i IPAddr
i.Set(`10.8.`)
fmt.Printf("%s", i.Ne())
// Output: ip != "10.8."
}
func ExampleFQDN_Set() {
var i FQDN
i.Set(`*`).Set(`example`).Set(`com`)
fmt.Printf("%s", i)
// Output: *.example.com
}
func ExampleFQDN_Eq_oneShot() {
fmt.Printf("%s", DNS(`www`, `example`, `com`).Eq())
// Output: dns = "www.example.com"
}
/*
This example demonstrates the creation of an instance of FQDN, which
is used in a variety of contexts.
In this example, a string name is fed to the package level IP function to form
a complete FQDN instance, which is then shown in string representation.
*/
func ExampleDNS() {
i := DNS(`example.com`)
fmt.Printf("%s", i)
// Output: example.com
}
/*
This example demonstrates the string representation of the receiver instance.
*/
func ExampleFQDN_String() {
fmt.Printf("%s", DNS(`example.com`))
// Output: example.com
}
/*
This example demonstrates the use of the useless Keyword method, as FQDN
instances do not have any knowledge of Keywords at this time.
*/
func ExampleFQDN_Keyword() {
fmt.Printf("%v", DNS(`example.com`).Keyword())
// Output: dns
}
/*
This example demonstrates a check of the receiver for "nilness".
*/
func ExampleFQDN_IsZero() {
fmt.Printf("%t", DNS(`www.www.www.www.www.example.com`).IsZero())
// Output: false
}
/*
This example demonstrates a check of the receiver for an aberrant state.
*/
func ExampleFQDN_Valid() {
i := DNS(``)
fmt.Printf("Valid: %t", i.Valid() == nil)
// Output: Valid: false
}
/*
This example demonstrates the SHA-1 hash comparison between two (2)
IPAddr instances using the Compare method.
*/
func ExampleIPAddr_Compare() {
addr1 := IP(`10.1.,192.168.`)
addr2 := IP(`10.1.,192.168.1.`)
fmt.Printf("Hashes are equal: %t", addr1.Compare(addr2))
// Output: Hashes are equal: false
}
/*
This example demonstrates the SHA-1 hash comparison between two (2)
FQDN instances using the Compare method.
*/
func ExampleFQDN_Compare() {
addr1 := DNS(`www`, `example`, `com`)
addr2 := DNS(`www.example.com`)
fmt.Printf("Hashes are equal: %t", addr1.Compare(addr2))
// Output: Hashes are equal: true
}
func ExampleFQDN_BRM() {
var host FQDN
host.Set(`www.example.com`)
cops := host.BRM()
fmt.Printf("%T allows Eq: %t", host, cops.Contains(`=`))
// Output: aci.FQDN allows Eq: true
}
func ExampleFQDN_Len() {
var host FQDN
host.Set(`www`)
host.Set(`example`)
host.Set(`com`)
//host.Set(`www.example.com`) // same!
fmt.Printf("%T contains %d DNS labels", host, host.Len())
// Output: aci.FQDN contains 3 DNS labels
}
func ExampleIPAddr_BRM() {
var address IPAddr
address.Set(`192.168.0`)
cops := address.BRM()
fmt.Printf("%T allows Eq: %t", address, cops.Contains(`=`))
// Output: aci.IPAddr allows Eq: true
}