-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_extra_test.go
More file actions
89 lines (78 loc) · 1.68 KB
/
Copy pathutils_extra_test.go
File metadata and controls
89 lines (78 loc) · 1.68 KB
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
package rawsocket
import (
"net"
"testing"
"github.com/google/gopacket"
)
func TestSendPacket_Success(t *testing.T) {
ch := make(chan WrappedPacket, 1)
wp := WrappedPacket{}
if !sendPacket(ch, wp) {
t.Fatal("expected sendPacket to return true on open channel")
}
select {
case <-ch:
default:
t.Fatal("expected packet in channel")
}
}
func TestSendPacket_ClosedChannel(t *testing.T) {
ch := make(chan WrappedPacket, 1)
close(ch)
if sendPacket(ch, WrappedPacket{}) {
t.Fatal("expected sendPacket to return false on closed channel")
}
}
func TestPacketIter_StopsOnError(t *testing.T) {
called := 0
fetch := func() (gopacket.Packet, *net.IPAddr, error) {
called++
return nil, nil, errTestSentinel
}
ch := make(chan WrappedPacket, 1)
packetIter(ch, fetch)
if called != 1 {
t.Fatalf("expected fetch called once, got %d", called)
}
}
func TestPacketIter_SendsPackets(t *testing.T) {
count := 0
fetch := func() (gopacket.Packet, *net.IPAddr, error) {
count++
if count > 3 {
return nil, nil, errTestSentinel
}
return nil, nil, nil
}
ch := make(chan WrappedPacket, 4)
packetIter(ch, fetch)
received := 0
loop:
for {
select {
case <-ch:
received++
default:
break loop
}
}
if received != 3 {
t.Fatalf("expected 3 packets, got %d", received)
}
}
func TestPacketIter_StopsOnClosedChannel(t *testing.T) {
fetch := func() (gopacket.Packet, *net.IPAddr, error) {
return nil, nil, nil
}
ch := make(chan WrappedPacket, 1)
done := make(chan struct{})
go func() {
close(ch)
done <- struct{}{}
}()
<-done
packetIter(ch, fetch)
}
var errTestSentinel = sentinelErr{}
type sentinelErr struct{}
func (sentinelErr) Error() string { return "sentinel" }