-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipv4_linux_test.go
98 lines (79 loc) · 2.18 KB
/
ipv4_linux_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
package water
import (
"net"
"os/exec"
"testing"
)
func startPing(t *testing.T, dst net.IP, dashB bool) {
params := []string{"-c", "4", dst.String()}
if dashB {
params = append([]string{"-b"}, params...)
}
if err := exec.Command("ping", params...).Start(); err != nil {
t.Fatal(err)
}
}
func setupIfce(t *testing.T, ipNet net.IPNet, dev string) {
if err := exec.Command("ip", "link", "set", dev, "up").Run(); err != nil {
t.Fatal(err)
}
if err := exec.Command("ip", "addr", "add", ipNet.String(), "dev", dev).Run(); err != nil {
t.Fatal(err)
}
}
func teardownIfce(t *testing.T, ifce *Interface) {
if err := ifce.Close(); err != nil {
t.Fatal(err)
}
if err := exec.Command("ip", "link", "set", ifce.Name(), "down").Run(); err != nil {
t.Fatal(err)
}
}
func TestBroadcastTAP(t *testing.T) {
var (
self = net.IPv4(10, 0, 42, 1)
mask = net.IPv4Mask(255, 255, 255, 0)
brd = net.IPv4(10, 0, 42, 255)
)
ifce, err := New(Config{DeviceType: TAP})
if err != nil {
t.Fatalf("creating TAP error: %v\n", err)
}
defer teardownIfce(t, ifce)
dataCh, errCh := startRead(t, ifce)
setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
startPing(t, brd, true)
waitForPingOrBust(t, true, true, self, brd, dataCh, errCh)
}
func TestBroadcastTUN(t *testing.T) {
var (
self = net.IPv4(10, 0, 42, 1)
mask = net.IPv4Mask(255, 255, 255, 0)
brd = net.IPv4(10, 0, 42, 255)
)
ifce, err := New(Config{DeviceType: TUN})
if err != nil {
t.Fatalf("creating TUN error: %v\n", err)
}
defer teardownIfce(t, ifce)
dataCh, errCh := startRead(t, ifce)
setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
startPing(t, brd, true)
waitForPingOrBust(t, false, true, self, brd, dataCh, errCh)
}
func TestUnicastTUN(t *testing.T) {
var (
self = net.IPv4(10, 0, 42, 1)
mask = net.IPv4Mask(255, 255, 255, 0)
remote = net.IPv4(10, 0, 42, 2)
)
ifce, err := New(Config{DeviceType: TUN})
if err != nil {
t.Fatalf("creating TUN error: %v\n", err)
}
defer teardownIfce(t, ifce)
dataCh, errCh := startRead(t, ifce)
setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
startPing(t, remote, false)
waitForPingOrBust(t, false, false, self, remote, dataCh, errCh)
}