-
Notifications
You must be signed in to change notification settings - Fork 97
/
message_linux_test.go
161 lines (147 loc) · 3.38 KB
/
message_linux_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//go:build linux
// +build linux
package netlink
import (
"syscall"
"testing"
"unsafe"
"github.com/google/go-cmp/cmp"
"github.com/mdlayher/netlink/nlenc"
"golang.org/x/sys/unix"
)
func TestHeaderMemoryLayoutLinux(t *testing.T) {
var nh Header
var sh syscall.NlMsghdr
if want, got := unsafe.Sizeof(sh), unsafe.Sizeof(nh); want != got {
t.Fatalf("unexpected structure sizes:\n- want: %v\n- got: %v",
want, got)
}
sh = syscall.NlMsghdr{
Len: 0x10101010,
Type: 0x2020,
Flags: 0x3030,
Seq: 0x40404040,
Pid: 0x50505050,
}
nh = sysToHeader(sh)
if want, got := sh.Len, nh.Length; want != got {
t.Fatalf("unexpected header length:\n- want: %v\n- got: %v",
want, got)
}
if want, got := sh.Type, uint16(nh.Type); want != got {
t.Fatalf("unexpected header type:\n- want: %v\n- got: %v",
want, got)
}
if want, got := sh.Flags, uint16(nh.Flags); want != got {
t.Fatalf("unexpected header flags:\n- want: %v\n- got: %v",
want, got)
}
if want, got := sh.Seq, nh.Sequence; want != got {
t.Fatalf("unexpected header sequence:\n- want: %v\n- got: %v",
want, got)
}
if want, got := sh.Pid, nh.PID; want != got {
t.Fatalf("unexpected header PID:\n- want: %v\n- got: %v",
want, got)
}
}
func Test_checkMessageExtendedAcknowledgementTLVs(t *testing.T) {
tests := []struct {
name string
m Message
err *OpError
}{
{
name: "error",
m: Message{
Header: Header{
Type: Error,
// Indicate the use of extended acknowledgement.
Flags: AcknowledgeTLVs,
},
Data: packExtACK(
-1,
// The caller's request message with arbitrary bytes that we
// skip over when parsing the TLVs.
&Message{
Header: Header{Length: 4},
Data: []byte{0xff, 0xff, 0xff, 0xff},
},
// The actual extended acknowledgement TLVs.
[]Attribute{
{
Type: 1,
Data: nlenc.Bytes("bad request"),
},
{
Type: 2,
Data: nlenc.Uint32Bytes(2),
},
},
),
},
err: &OpError{
Op: "receive",
Err: unix.Errno(1),
Message: "bad request",
Offset: 2,
},
},
{
name: "done multi",
m: Message{
Header: Header{
Type: Done,
// Indicate the use of extended acknowledgement.
Flags: Multi | AcknowledgeTLVs,
},
Data: packExtACK(
-1,
// No message, straight to TLVs.
nil,
[]Attribute{
{
Type: 1,
Data: nlenc.Bytes("bad request"),
},
{
Type: 2,
Data: nlenc.Uint32Bytes(2),
},
},
),
},
err: &OpError{
Op: "receive",
Err: unix.Errno(1),
Message: "bad request",
Offset: 2,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if diff := cmp.Diff(tt.err, checkMessage(tt.m)); diff != "" {
t.Fatalf("unexpected OpError (-want +got):\n%s", diff)
}
})
}
}
// packExtACK packs an extended acknowledgement response.
func packExtACK(errno int32, m *Message, tlvs []Attribute) []byte {
b := nlenc.Int32Bytes(errno)
if m != nil {
// Copy the header length logic from Conn.
m.Header.Length = uint32(nlmsgAlign(nlmsgLength(len(m.Data))))
mb, err := m.MarshalBinary()
if err != nil {
panicf("failed to marshal message: %v", err)
}
b = append(b, mb...)
}
ab, err := MarshalAttributes(tlvs)
if err != nil {
panicf("failed to marshal attributes: %v", err)
}
return append(b, ab...)
}