-
Notifications
You must be signed in to change notification settings - Fork 16
/
conn_test.go
128 lines (101 loc) · 3.6 KB
/
conn_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package netfilter
import (
"testing"
"time"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mdlayher/netlink"
"github.com/mdlayher/netlink/nltest"
)
var (
errNetlinkTest = "synthetic test error"
nlMsgReqAck = netlink.Message{
Header: netlink.Header{
Flags: netlink.Request | netlink.Acknowledge,
},
}
// Dummy connection that simply returns the input payload, with
// length and PID attributes automatically filled by the netlink library.
nlConnEcho = nltest.Dial(func(req []netlink.Message) ([]netlink.Message, error) {
return req, nil
})
connEcho = Conn{conn: nlConnEcho}
// Connection that returns error on any send.
nlConnError = nltest.Dial(func(_ []netlink.Message) ([]netlink.Message, error) {
return nil, errors.New(errNetlinkTest)
})
connErr = Conn{conn: nlConnError}
// Connection that returns a single message with a nlMsgErr that trips the netlink payload error check.
nlConnMsgError = nltest.Dial(func(_ []netlink.Message) ([]netlink.Message, error) {
resp := []netlink.Message{
{
Header: netlink.Header{Type: netlink.Error},
Data: []byte{1, 0, 0, 0},
},
}
return resp, nil
})
connErrMsg = Conn{conn: nlConnMsgError}
)
// No CAP_NET_ADMIN needed to simply open and close a netlink socket,
// so always test this, even when the other integration tests don't.
func TestConnDialClose(t *testing.T) {
c, err := Dial(nil)
require.NoError(t, err, "opening Conn")
err = c.Close()
require.NoError(t, err, "closing Conn")
}
// Attempt to open a Netlink socket into a netns that is highly unlikely
// to exist, so we can catch an error from Dial.
func TestConnDialError(t *testing.T) {
_, err := Dial(&netlink.Config{NetNS: 1337})
assert.EqualError(t, err, "setns: bad file descriptor")
}
func TestConnQuery(t *testing.T) {
// Expect no-op query to be successful.
_, err := connEcho.Query(nlMsgReqAck)
assert.NoError(t, err, "query error")
_, err = connErr.Query(nlMsgReqAck)
opErr, ok := errors.Cause(err).(*netlink.OpError)
require.True(t, ok)
assert.EqualError(t, opErr, "netlink receive: "+errNetlinkTest)
_, err = connErrMsg.Query(nlMsgReqAck)
opErr, ok = errors.Cause(err).(*netlink.OpError)
require.True(t, ok)
assert.EqualError(t, opErr, "netlink receive: errno -1")
}
func TestConnQueryMulticast(t *testing.T) {
// Dummy Conn initially marked as Multicast.
connMulticast := Conn{isMulticast: true}
assert.Equal(t, connMulticast.IsMulticast(), true)
_, err := connMulticast.Query(nlMsgReqAck)
assert.EqualError(t, err, errConnIsMulticast.Error())
err = connMulticast.JoinGroups(nil)
assert.EqualError(t, err, errNoMulticastGroups.Error())
}
func TestConnReceive(t *testing.T) {
// Inject a message directly into the nltest connection.
_, _ = connEcho.conn.Send(nlMsgReqAck)
// Drain the socket
_, err := connEcho.Receive()
if err != nil {
t.Fatalf("error in Receive: %v", err)
}
}
func TestConnDeadline(t *testing.T) {
c, err := Dial(nil)
require.NoError(t, err, "opening Conn")
require.NoError(t, c.SetDeadline(time.Now().Add(time.Second)), "setting global deadline")
require.NoError(t, c.SetReadDeadline(time.Now().Add(time.Second)), "setting read deadline")
require.NoError(t, c.SetWriteDeadline(time.Now().Add(time.Second)), "setting write deadline")
err = c.Close()
require.NoError(t, err, "closing Conn")
}
func TestConnBuffers(t *testing.T) {
c, err := Dial(nil)
require.NoError(t, err, "opening Conn")
require.NoError(t, c.SetReadBuffer(256), "setting read buffer")
require.NoError(t, c.SetWriteBuffer(256), "setting write buffer")
require.NoError(t, c.Close(), "closing Conn")
}