Skip to content

Commit b30d2b5

Browse files
committed
Fix ipfix buffer use and mirror test
1 parent dc5bb67 commit b30d2b5

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

vflow/ipfix_test.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
)
3131

3232
func init() {
33-
opts = &Options{}
33+
opts = NewOptions()
3434
}
3535

3636
func TestMirrorIPFIX(t *testing.T) {
@@ -39,21 +39,27 @@ func TestMirrorIPFIX(t *testing.T) {
3939
fb = make(chan IPFIXUDPMsg)
4040
dst = net.ParseIP("127.0.0.1")
4141
ready = make(chan struct{})
42+
errCh = make(chan struct{})
4243
)
4344

4445
go func() {
4546
err := mirrorIPFIX(dst, 10024, msg)
4647
if err != nil {
4748
if strings.Contains(err.Error(), "not permitted") {
4849
t.Log(err)
49-
ready <- struct{}{}
50+
errCh <- struct{}{}
5051
} else {
5152
t.Fatal("unexpected error", err)
5253
}
5354
}
5455
}()
5556

56-
time.Sleep(2 * time.Second)
57+
select {
58+
case <-errCh:
59+
return
60+
case <-time.After(2 * time.Second):
61+
break
62+
}
5763

5864
go func() {
5965
b := make([]byte, 1500)
@@ -86,9 +92,11 @@ func TestMirrorIPFIX(t *testing.T) {
8692

8793
}()
8894

89-
_, ok := <-ready
90-
if ok {
91-
return
95+
select {
96+
case <-ready:
97+
break
98+
case <-time.After(2 * time.Second):
99+
t.Fatal("timeout")
92100
}
93101

94102
body := []byte("hello")
@@ -100,7 +108,15 @@ func TestMirrorIPFIX(t *testing.T) {
100108
},
101109
}
102110

103-
feedback := <-fb
111+
var feedback IPFIXUDPMsg
112+
select {
113+
case feedback = <-fb:
114+
break
115+
case <-errCh:
116+
return
117+
case <-time.After(2 * time.Second):
118+
t.Fatal("timeout")
119+
}
104120

105121
if string(feedback.body) != "hello" {
106122
t.Error("expect body is hello, got", string(feedback.body))

vflow/ipfix_unix.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ func mirrorIPFIX(dst net.IP, port int, ch chan IPFIXUDPMsg) error {
113113
copy(packet[ipHLen:ipHLen+8], udpHdr)
114114
copy(packet[ipHLen+8:], msg.body)
115115

116-
ipfixBuffer.Put(msg.body[:opts.IPFIXUDPSize])
116+
if cap(msg.body) >= opts.IPFIXUDPSize {
117+
ipfixBuffer.Put(msg.body[:opts.IPFIXUDPSize])
118+
}
117119

118120
if err = conn.Send(packet[0 : ipHLen+8+pLen]); err != nil {
119121
return err

0 commit comments

Comments
 (0)