-
Notifications
You must be signed in to change notification settings - Fork 29
/
arping_test.go
91 lines (73 loc) · 1.67 KB
/
arping_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
package arping
import (
"net"
"runtime"
"strings"
"testing"
"time"
)
func TestPingWithInvalidIP(t *testing.T) {
ip := net.ParseIP("invalid")
_, _, err := Ping(ip)
if err == nil {
t.Error("error expected")
}
validateInvalidV4AddrErr(t, err)
}
func TestPingWithV6IP(t *testing.T) {
ip := net.ParseIP("fe80::e2cb:4eff:fed5:ca4e")
_, _, err := Ping(ip)
if err == nil {
t.Error("error expected")
}
validateInvalidV4AddrErr(t, err)
}
func TestGratuitousArpWithInvalidIP(t *testing.T) {
ip := net.ParseIP("invalid")
err := GratuitousArp(ip)
if err == nil {
t.Error("error expected")
}
validateInvalidV4AddrErr(t, err)
}
func TestGratuitousArpWithV6IP(t *testing.T) {
ip := net.ParseIP("fe80::e2cb:4eff:fed5:ca4e")
err := GratuitousArp(ip)
if err == nil {
t.Error("error expected")
}
validateInvalidV4AddrErr(t, err)
}
func TestGoroutinesDoesNotLeak(t *testing.T) {
ip := net.ParseIP("127.0.0.1")
SetTimeout(time.Duration(10 * time.Millisecond))
spawnNumGoroutines := 5
for i := 0; i < spawnNumGoroutines; i++ {
_, _, err := Ping(ip)
if err != ErrTimeout {
t.Fatalf("timeout error expected, but not received - received err: %v", err)
}
}
ok := make(chan bool, 1)
go func() {
for {
if runtime.NumGoroutine() < spawnNumGoroutines {
ok <- true
return
}
time.Sleep(100 * time.Millisecond)
}
}()
select {
case <-ok:
// ok
case <-time.After(30 * time.Second):
t.Fatalf("timeout waiting for goroutine cleanup - num goroutines: %d",
runtime.NumGoroutine())
}
}
func validateInvalidV4AddrErr(t *testing.T, err error) {
if !strings.Contains(err.Error(), "not a valid v4 Address") {
t.Errorf("unexpected error: %s", err)
}
}