forked from ti-mo/conntrack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats_test.go
151 lines (127 loc) · 3.22 KB
/
stats_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
package conntrack
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mdlayher/netlink"
"github.com/stretchr/testify/assert"
"github.com/ti-mo/netfilter"
)
func TestStatsUnmarshal(t *testing.T) {
nfa := []netfilter.Attribute{
{
Type: uint16(ctaStatsFound),
Data: []byte{0x01, 0xab, 0xcd, 0xef},
},
{
Type: uint16(ctaStatsInvalid),
Data: []byte{0x02, 0xab, 0xcd, 0xef},
},
{
Type: uint16(ctaStatsIgnore),
Data: []byte{0x03, 0xab, 0xcd, 0xef},
},
{
Type: uint16(ctaStatsInsert),
Data: []byte{0x04, 0xab, 0xcd, 0xef},
},
{
Type: uint16(ctaStatsInsertFailed),
Data: []byte{0x05, 0xab, 0xcd, 0xef},
},
{
Type: uint16(ctaStatsDrop),
Data: []byte{0x06, 0xab, 0xcd, 0xef},
},
{
Type: uint16(ctaStatsEarlyDrop),
Data: []byte{0x07, 0xab, 0xcd, 0xef},
},
{
Type: uint16(ctaStatsError),
Data: []byte{0x08, 0xab, 0xcd, 0xef},
},
{
Type: uint16(ctaStatsSearchRestart),
Data: []byte{0x09, 0xab, 0xcd, 0xef},
},
{Type: uint16(ctaStatsSearched)},
{Type: uint16(ctaStatsNew)},
{Type: uint16(ctaStatsDelete)},
{Type: uint16(ctaStatsDeleteList)},
}
want := Stats{
Found: 0x01abcdef,
Invalid: 0x02abcdef,
Ignore: 0x03abcdef,
Insert: 0x04abcdef,
InsertFailed: 0x05abcdef,
Drop: 0x06abcdef,
EarlyDrop: 0x07abcdef,
Error: 0x08abcdef,
SearchRestart: 0x09abcdef,
}
var s Stats
s.unmarshal(nfa)
if diff := cmp.Diff(want, s); diff != "" {
t.Fatalf("unexpected unmarshal (-want +got):\n%s", diff)
}
}
func TestUnmarshalStatsError(t *testing.T) {
_, err := unmarshalStats([]netlink.Message{{}})
assert.EqualError(t, err, "unmarshaling netfilter header: expected at least 4 bytes in netlink message payload")
}
func TestStatsExpectUnmarshal(t *testing.T) {
nfa := []netfilter.Attribute{
{
Type: uint16(ctaStatsExpNew),
Data: []byte{0x01, 0xab, 0xcd, 0xef},
},
{
Type: uint16(ctaStatsExpCreate),
Data: []byte{0x02, 0xab, 0xcd, 0xef},
},
{
Type: uint16(ctaStatsExpDelete),
Data: []byte{0x03, 0xab, 0xcd, 0xef},
},
}
want := StatsExpect{
New: 0x01abcdef,
Create: 0x02abcdef,
Delete: 0x03abcdef,
}
var se StatsExpect
se.unmarshal(nfa)
if diff := cmp.Diff(want, se); diff != "" {
t.Fatalf("unexpected unmarshal (-want +got):\n%s", diff)
}
}
func TestUnmarshalStatsExpectError(t *testing.T) {
_, err := unmarshalStatsExpect([]netlink.Message{{}})
assert.EqualError(t, err, "unmarshaling netfilter header: expected at least 4 bytes in netlink message payload")
}
func TestStatsGlobalUnmarshal(t *testing.T) {
nfa := []netfilter.Attribute{
{
Type: uint16(ctaStatsGlobalEntries),
Data: []byte{0x01, 0xab, 0xcd, 0xef},
},
{
Type: uint16(ctaStatsGlobalMaxEntries),
Data: []byte{0x02, 0xab, 0xcd, 0xef},
},
}
want := StatsGlobal{
Entries: 0x01abcdef,
MaxEntries: 0x02abcdef,
}
var sg StatsGlobal
sg.unmarshal(nfa)
if diff := cmp.Diff(want, sg); diff != "" {
t.Fatalf("unexpected unmarshal (-want +got):\n%s", diff)
}
}
func TestUnmarshalStatsGlobalError(t *testing.T) {
_, err := unmarshalStatsGlobal(netlink.Message{})
assert.EqualError(t, err, "unmarshaling netfilter header: expected at least 4 bytes in netlink message payload")
}