-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipv4_test.go
103 lines (93 loc) · 2.19 KB
/
ipv4_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
package water
import (
"context"
"net"
"testing"
"time"
"github.com/Doridian/water/waterutil"
)
const BUFFERSIZE = 1522
func startRead(t *testing.T, ifce *Interface) (dataChan <-chan []byte, errChan <-chan error) {
dataCh := make(chan []byte)
errCh := make(chan error)
go func() {
for {
buffer := make([]byte, BUFFERSIZE)
n, err := ifce.Read(buffer)
if err != nil {
errCh <- err
} else {
buffer = buffer[:n:n]
dataCh <- buffer
}
}
}()
return dataCh, errCh
}
func waitForPingOrBust(t *testing.T,
isTAP bool,
expectBroadcast bool,
expectSrc net.IP,
expectDest net.IP,
dataCh <-chan []byte, errCh <-chan error) {
waitForPintTimeout := time.NewTimer(8 * time.Second).C
readFrame:
for {
select {
case buffer := <-dataCh:
var packet []byte
if isTAP {
ethertype := waterutil.MACEthertype(buffer)
if ethertype != waterutil.IPv4 {
continue readFrame
}
if expectBroadcast && !waterutil.IsMACBroadcast(waterutil.MACDestination(buffer)) {
continue readFrame
}
packet = waterutil.MACPayload(buffer)
} else {
packet = buffer
}
if waterutil.IPVersion(packet) != 4 {
continue readFrame
}
if !waterutil.IPv4Source(packet).Equal(expectSrc) {
continue readFrame
}
if !waterutil.IPv4Destination(packet).Equal(expectDest) {
continue readFrame
}
if waterutil.IPv4Protocol(packet) != waterutil.ICMP {
continue readFrame
}
t.Logf("received broadcast frame: %#v\n", buffer)
break readFrame
case err := <-errCh:
t.Fatalf("read error: %v", err)
case <-waitForPintTimeout:
t.Fatal("Waiting for broadcast packet timeout")
}
}
}
func TestCloseUnblockPendingRead(t *testing.T) {
ifce, err := New(Config{DeviceType: TUN})
if err != nil {
t.Fatalf("creating TUN error: %v\n", err)
}
c := make(chan struct{})
go func() {
_, _ = ifce.Read(make([]byte, 1<<16))
close(c)
}()
// make sure ifce.Close() happens after ifce.Read() blocks
time.Sleep(1 * time.Second)
_ = ifce.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
select {
case <-c:
t.Log("Pending Read unblocked")
case <-ctx.Done():
t.Fatal("Timeouted, pending read blocked")
}
}