forked from mdlayher/netlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_linux_error_test.go
92 lines (83 loc) · 1.82 KB
/
conn_linux_error_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
// +build linux
package netlink_test
import (
"testing"
"github.com/mdlayher/netlink"
"github.com/mdlayher/netlink/nltest"
"golang.org/x/sys/unix"
)
func TestConnReceiveErrorLinux(t *testing.T) {
// Note: using *Conn instead of Linux-only *conn, to test
// error handling logic in *Conn.Receive
tests := []struct {
name string
msgs []netlink.Message
err error
}{
{
name: "ENOENT",
msgs: []netlink.Message{{
Header: netlink.Header{
Length: 20,
Type: netlink.HeaderTypeError,
Sequence: 1,
PID: 1,
},
// -2, little endian (ENOENT)
Data: []byte{0xfe, 0xff, 0xff, 0xff},
}},
err: unix.ENOENT,
},
{
name: "multipart done without error attached",
msgs: []netlink.Message{
{
Header: netlink.Header{
Flags: netlink.HeaderFlagsMulti,
},
},
{
Header: netlink.Header{
Type: netlink.HeaderTypeDone,
Flags: netlink.HeaderFlagsMulti,
},
},
},
},
{
name: "multipart done with error attached",
msgs: []netlink.Message{
{
Header: netlink.Header{
Flags: netlink.HeaderFlagsMulti,
},
},
{
Header: netlink.Header{
Type: netlink.HeaderTypeDone,
Flags: netlink.HeaderFlagsMulti,
},
Data: []byte{0xfc, 0xff, 0xff, 0xff},
},
},
// -4, little endian (EINTR)
err: unix.EINTR,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := nltest.Dial(func(_ netlink.Message) ([]netlink.Message, error) {
return tt.msgs, nil
})
defer c.Close()
// Need to prepopulate nltest's internal buffers by invoking the
// function once.
_, _ = c.Send(netlink.Message{})
_, err := c.Receive()
if want, got := tt.err, err; want != got {
t.Fatalf("unexpected error:\n- want: %v\n- got: %v",
want, got)
}
})
}
}