forked from ti-mo/netfilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
header_test.go
85 lines (62 loc) · 1.9 KB
/
header_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
package netfilter
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/mdlayher/netlink"
)
func TestHeaderMarshalTwoWay(t *testing.T) {
refHdr := Header{
SubsystemID: SubsystemID(NFSubsysCTNetlinkTimeout),
MessageType: MessageType(123),
Family: 255,
Version: 1,
ResourceID: 2,
}
refMsg := netlink.Message{Header: netlink.Header{Type: 0x087B}, Data: []byte{255, 1, 0, 2}}
var gotHdr Header
gotMsg := netlink.Message{Data: make([]byte, 4)}
assert.Nil(t, gotHdr.unmarshal(refMsg))
if diff := cmp.Diff(refHdr, gotHdr); diff != "" {
t.Fatalf("unexpected netfilter Header (-want, +got):\n %s", diff)
}
assert.Nil(t, gotHdr.marshal(&gotMsg))
if diff := cmp.Diff(refMsg, gotMsg); diff != "" {
t.Fatalf("unexpected netlink Message (-want, +got):\n %s", diff)
}
// unmarshal error
assert.Equal(t, errMessageLen, gotHdr.unmarshal(netlink.Message{}))
// marshal error
assert.Equal(t, errMessageLen, gotHdr.marshal(&netlink.Message{}))
}
func TestHeaderString(t *testing.T) {
ht := Header{
SubsystemID: NFSubsysIPSet,
MessageType: 123,
}
want := "<Subsystem: NFSubsysIPSet, Message Type: 123, Family: ProtoUnspec, Version: 0, ResourceID: 0>"
assert.Equal(t, want, ht.String())
}
func TestProtoFamilyString(t *testing.T) {
if ProtoFamily(255).String() == "" {
t.Fatal("ProtoFamily string representation empty - did you run `go generate`?")
}
pf := map[ProtoFamily]string{
ProtoUnspec: "ProtoUnspec",
ProtoInet: "ProtoInet",
ProtoIPv4: "ProtoIPv4",
ProtoARP: "ProtoARP",
ProtoNetDev: "ProtoNetDev",
ProtoBridge: "ProtoBridge",
ProtoIPv6: "ProtoIPv6",
ProtoDECNet: "ProtoDECNet",
}
for k, v := range pf {
assert.Equal(t, k.String(), v)
}
}
func TestSubsystemIDString(t *testing.T) {
if SubsystemID(255).String() == "" {
t.Fatal("SubsystemID string representation empty - did you run `go generate`?")
}
}