-
Notifications
You must be signed in to change notification settings - Fork 23
/
conntrack_test.go
203 lines (187 loc) · 6.7 KB
/
conntrack_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package conntrack
import (
"net"
"testing"
"github.com/mdlayher/netlink"
"github.com/mdlayher/netlink/nltest"
)
func TestFlush(t *testing.T) {
tests := []struct {
name string
family Family
want []netlink.Message
}{
{name: "Flush IPv4", family: IPv4, want: []netlink.Message{
{
Header: netlink.Header{
Length: 20,
// NFNL_SUBSYS_CTNETLINK<<8|IPCTNL_MSG_CT_DELETE
Type: netlink.HeaderType(1<<8 | 2),
// NLM_F_REQUEST|NLM_F_ACK
Flags: netlink.Request | netlink.Acknowledge,
// Can and will be ignored
Sequence: 0,
// Can and will be ignored
PID: nltest.PID,
},
// nfgen_family=AF_INET, version=NFNETLINK_V0, res_id=htons(0)
Data: []byte{0x2, 0x0, 0x0, 0x0},
},
},
},
{name: "Flush IPv6", family: IPv6, want: []netlink.Message{
{
Header: netlink.Header{
Length: 20,
// NFNL_SUBSYS_CTNETLINK<<8|IPCTNL_MSG_CT_DELETE
Type: netlink.HeaderType(1<<8 | 2),
// NLM_F_REQUEST|NLM_F_ACK
Flags: netlink.Request | netlink.Acknowledge,
// Can and will be ignored
Sequence: 0,
// Can and will be ignored
PID: nltest.PID,
},
// nfgen_family=AF_INET6, version=NFNETLINK_V0, res_id=htons(0)
Data: []byte{0xA, 0x0, 0x0, 0x0},
},
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Fake a netfilter conntrack connection
nfct := &Nfct{}
AdjustWriteTimeout(nfct, func() error { return nil })
nfct.Con = nltest.Dial(func(reqs []netlink.Message) ([]netlink.Message, error) {
if len(reqs) == 0 {
return nil, nil
}
if len(reqs) != 1 {
t.Fatalf("Expected only one request, got %d", len(reqs))
}
// To ignore the Sequence number, we set it to the same value
tc.want[0].Header.Sequence = reqs[0].Header.Sequence
if len(reqs) != len(tc.want) {
t.Fatalf("differen length:\n- want: %#v\n- got: %#v\n", tc.want, reqs)
}
for i := 0; i < len(reqs); i++ {
if len(reqs[i].Data) != len(tc.want[i].Data) {
t.Fatalf("differen length:\n- want: %#v\n- got: %#v\n", tc.want[i], reqs[i])
}
if reqs[i].Header.Type != tc.want[i].Header.Type {
t.Fatalf("differen header types:\n- want: %#v\n- got: %#v\n", tc.want[i].Header.Type, reqs[i].Header.Type)
}
if reqs[i].Header.Flags != tc.want[i].Header.Flags {
t.Fatalf("differen header flags:\n- want: %#v\n- got: %#v\n", tc.want[i].Header.Flags, reqs[i].Header.Flags)
}
for j, v := range reqs[i].Data {
if v != tc.want[i].Data[j] {
t.Fatalf("unexpected reply:\n- want: %#v\n- got: %#v", tc.want[i].Data, reqs[i].Data)
}
}
}
return nil, nil
})
defer nfct.Con.Close()
if err := nfct.Flush(Conntrack, tc.family); err != nil {
t.Fatal(err)
}
})
}
}
func TestCreate(t *testing.T) {
srcIP := net.ParseIP("1.1.1.1")
dstIP := net.ParseIP("2.2.2.2")
var tcp uint8 = 17
var srcPort uint16 = 22
var dstPort uint16 = 10
var timeout uint32 = 100
var tcpState uint8 = 8
tests := []struct {
name string
attributes Con
want []netlink.Message
}{
{name: "noAttributes", want: []netlink.Message{
{
Header: netlink.Header{
Length: 20,
// NFNL_SUBSYS_CTNETLINK<<8|IPCTNL_MSG_CT_NEW
Type: netlink.HeaderType(1 << 8),
// NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK|NLM_F_EXCL
Flags: netlink.Request | netlink.Create | netlink.Acknowledge | netlink.Excl,
// Can and will be ignored
Sequence: 0,
// Can and will be ignored
PID: nltest.PID,
},
// nfgen_family=AF_INET, version=NFNETLINK_V0, NFNL_SUBSYS_CTNETLINK
Data: []byte{0x2, 0x0, 0x0, 0x1},
},
}},
// Example from libnetfilter_conntrack/utils/conntrack_create.c
{name: "conntrack_create.c", attributes: Con{
Origin: &IPTuple{Src: &srcIP, Dst: &dstIP, Proto: &ProtoTuple{Number: &tcp, SrcPort: &srcPort, DstPort: &dstPort}},
Reply: &IPTuple{Src: &dstIP, Dst: &srcIP, Proto: &ProtoTuple{Number: &tcp, SrcPort: &dstPort, DstPort: &srcPort}},
Timeout: &timeout,
ProtoInfo: &ProtoInfo{TCP: &TCPInfo{State: &tcpState}}},
want: []netlink.Message{
{
Header: netlink.Header{
Length: 80,
// NFNL_SUBSYS_CTNETLINK<<8|IPCTNL_MSG_CT_NEW
Type: netlink.HeaderType(1 << 8),
// NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK|NLM_F_EXCL
Flags: netlink.Request | netlink.Create | netlink.Acknowledge | netlink.Excl,
// Can and will be ignored
Sequence: 0,
// Can and will be ignored
PID: nltest.PID,
},
// nfgen_family=AF_INET, version=NFNETLINK_V0, NFNL_SUBSYS_CTNETLINK + netlinkes Attributes
Data: []byte{0x2, 0x0, 0x0, 0x1, 0x34, 0x0, 0x1, 0x80, 0x14, 0x0, 0x1, 0x80, 0x8, 0x0, 0x1, 0x0, 0x1, 0x1, 0x1, 0x1, 0x8, 0x0, 0x2, 0x0, 0x2, 0x2, 0x2, 0x2, 0x1c, 0x0, 0x2, 0x80, 0x5, 0x0, 0x1, 0x0, 0x11, 0x0, 0x0, 0x0, 0x6, 0x0, 0x2, 0x0, 0x0, 0x16, 0x0, 0x0, 0x6, 0x0, 0x3, 0x0, 0x0, 0xa, 0x0, 0x0, 0x34, 0x0, 0x2, 0x80, 0x14, 0x0, 0x1, 0x80, 0x8, 0x0, 0x1, 0x0, 0x2, 0x2, 0x2, 0x2, 0x8, 0x0, 0x2, 0x0, 0x1, 0x1, 0x1, 0x1, 0x1c, 0x0, 0x2, 0x80, 0x5, 0x0, 0x1, 0x0, 0x11, 0x0, 0x0, 0x0, 0x6, 0x0, 0x2, 0x0, 0x0, 0xa, 0x0, 0x0, 0x6, 0x0, 0x3, 0x0, 0x0, 0x16, 0x0, 0x0, 0x8, 0x0, 0x7, 0x0, 0x0, 0x0, 0x0, 0x64, 0x10, 0x0, 0x4, 0x80, 0xc, 0x0, 0x1, 0x80, 0x5, 0x0, 0x1, 0x0, 0x8, 0x0, 0x0, 0x0},
},
}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
nfct := &Nfct{}
AdjustWriteTimeout(nfct, func() error { return nil })
nfct.Con = nltest.Dial(func(reqs []netlink.Message) ([]netlink.Message, error) {
if len(reqs) == 0 {
return nil, nil
}
if len(reqs) != 1 {
t.Fatalf("Expected only one request, got %d", len(reqs))
}
// To ignore the Sequence number, we set it to the same value
tc.want[0].Header.Sequence = reqs[0].Header.Sequence
if len(reqs) != len(tc.want) {
t.Fatalf("differen length:\n- want: %#v\n- got: %#v\n", tc.want, reqs)
}
for i := 0; i < len(reqs); i++ {
if len(reqs[i].Data) != len(tc.want[i].Data) {
t.Fatalf("differen length:\n- want: %#v\n- got: %#v\n", tc.want[i], reqs[i])
}
if reqs[i].Header.Type != tc.want[i].Header.Type {
t.Fatalf("differen header types:\n- want: %#v\n- got: %#v\n", tc.want[i].Header.Type, reqs[i].Header.Type)
}
if reqs[i].Header.Flags != tc.want[i].Header.Flags {
t.Fatalf("differen header flags:\n- want: %#v\n- got: %#v\n", tc.want[i].Header.Flags, reqs[i].Header.Flags)
}
for j, v := range reqs[i].Data {
if v != tc.want[i].Data[j] {
t.Fatalf("unexpected reply:\n- want: %#v\n- got: %#v", tc.want[i].Data, reqs[i].Data)
}
}
}
return nil, nil
})
defer nfct.Con.Close()
if err := nfct.Create(Conntrack, IPv4, tc.attributes); err != nil {
t.Fatal(err)
}
})
}
}