-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathfilter_test.go
More file actions
65 lines (54 loc) · 1.22 KB
/
filter_test.go
File metadata and controls
65 lines (54 loc) · 1.22 KB
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
package conntrack
import (
"slices"
"testing"
"github.com/stretchr/testify/assert"
"github.com/ti-mo/netfilter"
)
func TestFilterMarshal(t *testing.T) {
f := NewFilter().
Mark(0xf0000000).MarkMask(0x0000000f).
Zone(42).
Status(StatusDying).StatusMask(0xdeadbeef)
want := []netfilter.Attribute{
{
Type: uint16(ctaStatus),
Data: []byte{0, 0, 0x2, 0},
},
{
Type: uint16(ctaMark),
Data: []byte{0xf0, 0, 0, 0},
},
{
Type: uint16(ctaZone),
Data: []byte{0, 42},
},
{
Type: uint16(ctaMarkMask),
Data: []byte{0, 0, 0, 0x0f},
},
{
Type: uint16(ctaStatusMask),
Data: []byte{0xde, 0xad, 0xbe, 0xef},
},
}
got := f.marshal()
slices.SortStableFunc(got, func(a, b netfilter.Attribute) int {
return int(a.Type) - int(b.Type)
})
assert.Equal(t, want, got)
}
func TestFilterMutate(t *testing.T) {
f := NewFilter().
Mark(1).
Family(1)
mod := f.
Mark(2).
Family(2)
// Ensure original filter is unchanged.
assert.NotEqual(t, f, mod)
assert.Equal(t, []byte{0, 0, 0, 1}, f.(*filter).f[ctaMark])
assert.Equal(t, netfilter.ProtoFamily(1), f.(*filter).l3)
assert.Equal(t, []byte{0, 0, 0, 2}, mod.(*filter).f[ctaMark])
assert.Equal(t, netfilter.ProtoFamily(2), mod.(*filter).l3)
}