| 1 |
package arping |
| 2 |
|
| 3 |
import ( |
| 4 |
"net" |
| 5 |
"runtime" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
func TestPingWithInvalidIP(t *testing.T) { |
| 12 |
ip := net.ParseIP("invalid") |
| 13 |
|
| 14 |
_, _, err := Ping(ip) |
| 15 |
if err == nil { |
| 16 |
t.Error("error expected") |
| 17 |
} |
| 18 |
|
| 19 |
validateInvalidV4AddrErr(t, err) |
| 20 |
} |
| 21 |
|
| 22 |
func TestPingWithV6IP(t *testing.T) { |
| 23 |
ip := net.ParseIP("fe80::e2cb:4eff:fed5:ca4e") |
| 24 |
|
| 25 |
_, _, err := Ping(ip) |
| 26 |
if err == nil { |
| 27 |
t.Error("error expected") |
| 28 |
} |
| 29 |
|
| 30 |
validateInvalidV4AddrErr(t, err) |
| 31 |
} |
| 32 |
|
| 33 |
func TestGratuitousArpWithInvalidIP(t *testing.T) { |
| 34 |
ip := net.ParseIP("invalid") |
| 35 |
|
| 36 |
err := GratuitousArp(ip) |
| 37 |
if err == nil { |
| 38 |
t.Error("error expected") |
| 39 |
} |
| 40 |
|
| 41 |
validateInvalidV4AddrErr(t, err) |
| 42 |
} |
| 43 |
|
| 44 |
func TestGratuitousArpWithV6IP(t *testing.T) { |
| 45 |
ip := net.ParseIP("fe80::e2cb:4eff:fed5:ca4e") |
| 46 |
|
| 47 |
err := GratuitousArp(ip) |
| 48 |
if err == nil { |
| 49 |
t.Error("error expected") |
| 50 |
} |
| 51 |
|
| 52 |
validateInvalidV4AddrErr(t, err) |
| 53 |
} |
| 54 |
|
| 55 |
func TestGoroutinesDoesNotLeak(t *testing.T) { |
| 56 |
ip := net.ParseIP("127.0.0.1") |
| 57 |
SetTimeout(time.Duration(10 * time.Millisecond)) |
| 58 |
|
| 59 |
spawnNumGoroutines := 5 |
| 60 |
for i := 0; i < spawnNumGoroutines; i++ { |
| 61 |
_, _, err := Ping(ip) |
| 62 |
if err != ErrTimeout { |
| 63 |
t.Fatalf("timeout error expected, but not received - received err: %v", err) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
ok := make(chan bool, 1) |
| 68 |
go func() { |
| 69 |
for { |
| 70 |
if runtime.NumGoroutine() < spawnNumGoroutines { |
| 71 |
ok <- true |
| 72 |
return |
| 73 |
} |
| 74 |
time.Sleep(100 * time.Millisecond) |
| 75 |
} |
| 76 |
}() |
| 77 |
|
| 78 |
select { |
| 79 |
case <-ok: |
| 80 |
|
| 81 |
case <-time.After(30 * time.Second): |
| 82 |
t.Fatalf("timeout waiting for goroutine cleanup - num goroutines: %d", |
| 83 |
runtime.NumGoroutine()) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func validateInvalidV4AddrErr(t *testing.T, err error) { |
| 88 |
if !strings.Contains(err.Error(), "not a valid v4 Address") { |
| 89 |
t.Errorf("unexpected error: %s", err) |
| 90 |
} |
| 91 |
} |