forked from tatsushid/go-fastping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastping_test.go
434 lines (379 loc) · 9.52 KB
/
fastping_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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package fastping
import (
"net"
"sync"
"testing"
"time"
)
func TestSource(t *testing.T) {
for i, tt := range []struct {
firstAddr string
secondAddr string
invalid bool
}{
{firstAddr: "192.0.2.10", secondAddr: "192.0.2.20", invalid: false},
{firstAddr: "2001:0DB8::10", secondAddr: "2001:0DB8::20", invalid: false},
{firstAddr: "192.0.2", invalid: true},
} {
p := NewPinger()
origSource, err := p.Source(tt.firstAddr)
if tt.invalid {
if err == nil {
t.Errorf("[%d] Source should return an error but nothing: %v", i)
}
continue
}
if err != nil {
t.Errorf("[%d] Source address failed: %v", i, err)
}
if origSource != "" {
t.Errorf("[%d] Source returned an unexpected value: got %q, expected %q", i, origSource, "")
}
origSource, err = p.Source(tt.secondAddr)
if err != nil {
t.Errorf("[%d] Source address failed: %v", i, err)
}
if origSource != tt.firstAddr {
t.Errorf("[%d] Source returned an unexpected value: got %q, expected %q", i, origSource, tt.firstAddr)
}
}
v4Addr := "192.0.2.10"
v6Addr := "2001:0DB8::10"
p := NewPinger()
_, err := p.Source(v4Addr)
if err != nil {
t.Errorf("Source address failed: %v", err)
}
_, err = p.Source(v6Addr)
if err != nil {
t.Errorf("Source address failed: %v", err)
}
origSource, err := p.Source("")
if err != nil {
t.Errorf("Source address failed: %v", err)
}
if origSource != v4Addr {
t.Errorf("Source returned an unexpected value: got %q, expected %q", origSource, v4Addr)
}
}
func TestAddIP(t *testing.T) {
addIPTests := []struct {
host string
addr *net.IPAddr
expect bool
}{
{host: "127.0.0.1", addr: &net.IPAddr{IP: net.IPv4(127, 0, 0, 1)}, expect: true},
{host: "localhost", addr: &net.IPAddr{IP: net.IPv4(127, 0, 0, 1)}, expect: false},
}
p := NewPinger()
for _, tt := range addIPTests {
if ok := p.AddIP(tt.host); ok != nil {
if tt.expect != false {
t.Errorf("AddIP failed: got %v, expected %v", ok, tt.expect)
}
}
}
for _, tt := range addIPTests {
if tt.expect {
if !p.addrs[tt.host].IP.Equal(tt.addr.IP) {
t.Errorf("AddIP didn't save IPAddr: %v", tt.host)
}
}
}
}
func TestAddIPAddr(t *testing.T) {
addIPAddrTests := []*net.IPAddr{
{IP: net.IPv4(192, 0, 2, 10)},
{IP: net.IP{0x20, 0x01, 0x0D, 0xB8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10}},
}
p := NewPinger()
for i, tt := range addIPAddrTests {
p.AddIPAddr(tt)
if !p.addrs[tt.String()].IP.Equal(tt.IP) {
t.Errorf("[%d] AddIPAddr didn't save IPAddr: %v", i, tt.IP)
}
if len(tt.IP.To4()) == net.IPv4len {
if p.hasIPv4 != true {
t.Errorf("[%d] AddIPAddr didn't save IPAddr type: got %v, expected %v", i, p.hasIPv4, true)
}
} else if len(tt.IP) == net.IPv6len {
if p.hasIPv6 != true {
t.Errorf("[%d] AddIPAddr didn't save IPAddr type: got %v, expected %v", i, p.hasIPv6, true)
}
} else {
t.Errorf("[%d] AddIPAddr encounted an unexpected error", i)
}
}
}
func TestRemoveIP(t *testing.T) {
p := NewPinger()
if err := p.AddIP("127.0.0.1"); err != nil {
t.Fatalf("AddIP failed: %v", err)
}
if len(p.addrs) != 1 {
t.Fatalf("AddIP length check failed")
}
if err := p.RemoveIP("127.0"); err == nil {
t.Fatal("RemoveIP, invalid IP should fail")
}
if err := p.RemoveIP("127.0.0.1"); err != nil {
t.Fatalf("RemoveIP failed: %v", err)
}
if len(p.addrs) != 0 {
t.Fatalf("RemoveIP length check failed")
}
}
func TestRemoveIPAddr(t *testing.T) {
p := NewPinger()
if err := p.AddIP("127.0.0.1"); err != nil {
t.Fatalf("AddIP failed: %v", err)
}
if len(p.addrs) != 1 {
t.Fatalf("AddIP length check failed")
}
p.RemoveIPAddr(&net.IPAddr{IP: net.IPv4(127, 0, 0, 1)})
if len(p.addrs) != 0 {
t.Fatalf("RemoveIPAddr length check failed")
}
}
func TestRun(t *testing.T) {
for _, network := range []string{"ip", "udp"} {
p := NewPinger()
p.Network(network)
if err := p.AddIP("127.0.0.1"); err != nil {
t.Fatalf("AddIP failed: %v", err)
}
if err := p.AddIP("127.0.0.100"); err != nil {
t.Fatalf("AddIP failed: %v", err)
}
if err := p.AddIP("::1"); err != nil {
t.Fatalf("AddIP failed: %v", err)
}
found1, found100, foundv6 := false, false, false
called, idle := false, false
p.OnRecv = func(ip *net.IPAddr, d time.Duration) {
called = true
if ip.String() == "127.0.0.1" {
found1 = true
} else if ip.String() == "127.0.0.100" {
found100 = true
} else if ip.String() == "::1" {
foundv6 = true
}
}
p.OnIdle = func() {
idle = true
}
err := p.Run()
if err != nil {
t.Fatalf("Pinger returns error: %v", err)
}
if !called {
t.Fatalf("Pinger didn't get any responses")
}
if !idle {
t.Fatalf("Pinger didn't call OnIdle function")
}
if !found1 {
t.Fatalf("Pinger `127.0.0.1` didn't respond")
}
if found100 {
t.Fatalf("Pinger `127.0.0.100` responded")
}
if !foundv6 {
t.Fatalf("Pinger `::1` didn't responded")
}
}
}
func TestMultiRun(t *testing.T) {
for _, network := range []string{"ip", "udp"} {
p1 := NewPinger()
p1.Network(network)
p2 := NewPinger()
p2.Network(network)
if err := p1.AddIP("127.0.0.1"); err != nil {
t.Fatalf("AddIP 1 failed: %v", err)
}
if err := p2.AddIP("127.0.0.1"); err != nil {
t.Fatalf("AddIP 2 failed: %v", err)
}
var mu sync.Mutex
res1 := 0
p1.OnRecv = func(*net.IPAddr, time.Duration) {
mu.Lock()
res1++
mu.Unlock()
}
res2 := 0
p2.OnRecv = func(*net.IPAddr, time.Duration) {
mu.Lock()
res2++
mu.Unlock()
}
p1.MaxRTT, p2.MaxRTT = time.Millisecond*100, time.Millisecond*100
if err := p1.Run(); err != nil {
t.Fatalf("Pinger 1 returns error: %v", err)
}
if res1 == 0 {
t.Fatalf("Pinger 1 didn't get any responses")
}
if res2 > 0 {
t.Fatalf("Pinger 2 got response")
}
res1, res2 = 0, 0
if err := p2.Run(); err != nil {
t.Fatalf("Pinger 2 returns error: %v", err)
}
if res1 > 0 {
t.Fatalf("Pinger 1 got response")
}
if res2 == 0 {
t.Fatalf("Pinger 2 didn't get any responses")
}
res1, res2 = 0, 0
errch1, errch2 := make(chan error), make(chan error)
go func(ch chan error) {
err := p1.Run()
if err != nil {
ch <- err
}
}(errch1)
go func(ch chan error) {
err := p2.Run()
if err != nil {
ch <- err
}
}(errch2)
ticker := time.NewTicker(time.Millisecond * 200)
select {
case err := <-errch1:
t.Fatalf("Pinger 1 returns error: %v", err)
case err := <-errch2:
t.Fatalf("Pinger 2 returns error: %v", err)
case <-ticker.C:
break
}
mu.Lock()
defer mu.Unlock()
if res1 != 1 {
t.Fatalf("Pinger 1 didn't get correct response")
}
if res2 != 1 {
t.Fatalf("Pinger 2 didn't get correct response")
}
}
}
func TestRunLoop(t *testing.T) {
for _, network := range []string{"ip", "udp"} {
p := NewPinger()
p.Network(network)
if err := p.AddIP("127.0.0.1"); err != nil {
t.Fatalf("AddIP failed: %v", err)
}
p.MaxRTT = time.Millisecond * 100
recvCount, idleCount := 0, 0
p.OnRecv = func(*net.IPAddr, time.Duration) {
recvCount++
}
p.OnIdle = func() {
idleCount++
}
var err error
p.RunLoop()
ticker := time.NewTicker(time.Millisecond * 250)
select {
case <-p.Done():
if err = p.Err(); err != nil {
t.Fatalf("Pinger returns error %v", err)
}
case <-ticker.C:
break
}
ticker.Stop()
p.Stop()
if recvCount < 2 {
t.Fatalf("Pinger receive count less than 2")
}
if idleCount < 2 {
t.Fatalf("Pinger idle count less than 2")
}
}
}
func TestErr(t *testing.T) {
invalidSource := "192.0.2"
p := NewPinger()
p.ctx = newContext()
_ = p.listen("ip4:icmp", invalidSource)
if p.Err() == nil {
t.Errorf("Err should return an error but nothing")
}
}
func TestListen(t *testing.T) {
noSource := ""
invalidSource := "192.0.2"
p := NewPinger()
p.ctx = newContext()
conn := p.listen("ip4:icmp", noSource)
if conn == nil {
t.Errorf("listen failed: %v", p.Err())
} else {
conn.Close()
}
conn = p.listen("ip4:icmp", invalidSource)
if conn != nil {
t.Errorf("listen should return nothing but something: %v", conn)
conn.Close()
}
}
func TestTimeToBytes(t *testing.T) {
// 2009-11-10 23:00:00 +0000 UTC = 1257894000000000000
expect := []byte{0x11, 0x74, 0xef, 0xed, 0xab, 0x18, 0x60, 0x00}
tm, err := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z")
if err != nil {
t.Errorf("time.Parse failed: %v", err)
}
b := timeToBytes(tm)
for i := 0; i < 8; i++ {
if b[i] != expect[i] {
t.Errorf("timeToBytes failed: got %v, expected: %v", b, expect)
break
}
}
}
func TestBytesToTime(t *testing.T) {
// 2009-11-10 23:00:00 +0000 UTC = 1257894000000000000
b := []byte{0x11, 0x74, 0xef, 0xed, 0xab, 0x18, 0x60, 0x00}
expect, err := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z")
if err != nil {
t.Errorf("time.Parse failed: %v", err)
}
tm := bytesToTime(b)
if !tm.Equal(expect) {
t.Errorf("bytesToTime failed: got %v, expected: %v", tm.UTC(), expect.UTC())
}
}
func TestTimeToBytesToTime(t *testing.T) {
tm, err := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z")
if err != nil {
t.Errorf("time.Parse failed: %v", err)
}
b := timeToBytes(tm)
tm2 := bytesToTime(b)
if !tm.Equal(tm2) {
t.Errorf("bytesToTime failed: got %v, expected: %v", tm2.UTC(), tm.UTC())
}
}
func TestPayloadSizeDefault(t *testing.T) {
s := timeToBytes(time.Now())
d := append(s, byteSliceOfSize(8-TimeSliceLength)...)
if len(d) != 8 {
t.Errorf("Payload size incorrect: got %d, expected: %d", len(d), 8)
}
}
func TestPayloadSizeCustom(t *testing.T) {
s := timeToBytes(time.Now())
d := append(s, byteSliceOfSize(64-TimeSliceLength)...)
if len(d) != 64 {
t.Errorf("Payload size incorrect: got %d, expected: %d", len(d), 64)
}
}