forked from ti-mo/conntrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_test.go
180 lines (159 loc) · 4.29 KB
/
event_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
package conntrack
import (
"testing"
"github.com/mdlayher/netlink"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ti-mo/netfilter"
)
var eventTypeTests = []struct {
name string
et eventType
nfh netfilter.Header
err error
}{
{
name: "error unmarshal not conntrack",
err: errNotConntrack,
},
{
name: "conntrack new",
nfh: netfilter.Header{
SubsystemID: netfilter.NFSubsysCTNetlink,
MessageType: netfilter.MessageType(ctNew),
Flags: netlink.Create | netlink.Excl,
},
et: EventNew,
},
{
name: "conntrack update",
nfh: netfilter.Header{
SubsystemID: netfilter.NFSubsysCTNetlink,
MessageType: netfilter.MessageType(ctNew),
},
et: EventUpdate,
},
{
name: "conntrack destroy",
nfh: netfilter.Header{
SubsystemID: netfilter.NFSubsysCTNetlink,
MessageType: netfilter.MessageType(ctDelete),
},
et: EventDestroy,
},
{
name: "error unmarshal conntrack unknown event",
nfh: netfilter.Header{
SubsystemID: netfilter.NFSubsysCTNetlink,
MessageType: 255,
},
err: errors.New("unknown event type 255"),
},
{
name: "conntrack exp new",
nfh: netfilter.Header{
SubsystemID: netfilter.NFSubsysCTNetlinkExp,
MessageType: netfilter.MessageType(ctExpNew),
},
et: EventExpNew,
},
{
name: "conntrack exp destroy",
nfh: netfilter.Header{
SubsystemID: netfilter.NFSubsysCTNetlinkExp,
MessageType: netfilter.MessageType(ctExpDelete),
},
et: EventExpDestroy,
},
{
name: "error unmarshal conntrack exp unknown event",
nfh: netfilter.Header{
SubsystemID: netfilter.NFSubsysCTNetlinkExp,
MessageType: 255,
},
err: errors.New("unknown event type 255"),
},
}
func TestEventTypeUnmarshal(t *testing.T) {
for _, tt := range eventTypeTests {
t.Run(tt.name, func(t *testing.T) {
var et eventType
err := et.unmarshal(tt.nfh)
if err != nil || tt.err != nil {
require.Error(t, err)
require.EqualError(t, tt.err, err.Error())
return
}
assert.Equal(t, tt.et, et, "event type mismatch")
})
}
}
func TestEventTypeString(t *testing.T) {
assert.Equal(t, "eventType(255)", eventType(255).String())
}
var eventTests = []struct {
name string
e Event
nfh netfilter.Header
nfattrs []netfilter.Attribute
err error
}{
{
name: "correct empty new flow event",
nfh: netfilter.Header{
SubsystemID: netfilter.NFSubsysCTNetlink,
Flags: netlink.Create | netlink.Excl,
},
e: Event{
Type: EventNew,
Flow: &Flow{},
},
},
{
name: "correct empty expect destroy event",
nfh: netfilter.Header{
SubsystemID: netfilter.NFSubsysCTNetlinkExp,
MessageType: netfilter.MessageType(ctExpDelete),
},
e: Event{
Type: EventExpDestroy,
Expect: &Expect{},
},
},
}
func TestEventUnmarshal(t *testing.T) {
for _, tt := range eventTests {
t.Run(tt.name, func(t *testing.T) {
// Re-use netfilter's MarshalNetlink because we don't want to roll binary netlink messages by hand.
nlm, err := netfilter.MarshalNetlink(tt.nfh, tt.nfattrs)
require.NoError(t, err)
var e Event
err = e.unmarshal(nlm)
if err != nil || tt.err != nil {
require.Error(t, err)
require.EqualError(t, tt.err, err.Error(), "unmarshal errors do not match")
return
}
assert.Equal(t, tt.e, e, "unexpected unmarshal")
})
}
}
func TestEventUnmarshalError(t *testing.T) {
// Unmarshal into event with existing Flow
eventExistingFlow := Event{Flow: &Flow{}}
assert.EqualError(t, eventExistingFlow.unmarshal(netlink.Message{}), errReusedEvent.Error())
// Netlink unmarshal error
emptyEvent := Event{}
assert.EqualError(t, emptyEvent.unmarshal(netlink.Message{}), "expected at least 4 bytes in netlink message payload")
// EventType unmarshal error, blank SubsystemID
assert.EqualError(t, emptyEvent.unmarshal(netlink.Message{
Header: netlink.Header{}, Data: []byte{1, 2, 3, 4}}), "trying to decode a non-conntrack or conntrack-exp message")
// Cause a random error during Flow unmarshal
assert.EqualError(t, emptyEvent.unmarshal(netlink.Message{
Header: netlink.Header{Type: netlink.HeaderType(netfilter.NFSubsysCTNetlink) << 8},
Data: []byte{
1, 2, 3, 4, // random 4-byte nfgenmsg
4, 0, 1, 0, // 4-byte (empty) netlink attribute of type 1
}}), "Tuple unmarshal: need a Nested attribute to decode this structure")
}