-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathtypes_test.go
More file actions
106 lines (97 loc) · 2.66 KB
/
types_test.go
File metadata and controls
106 lines (97 loc) · 2.66 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
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
package nftables
import (
"testing"
"github.com/mdlayher/netlink"
"golang.org/x/sys/unix"
)
func TestParseNftMsgType(t *testing.T) {
var tests = []struct {
name string
headerType netlink.HeaderType
wantErr bool
wantNftMsgType *nftMsgType
wantString string
}{
{
name: "InvalidSubsystem",
headerType: netlink.HeaderType(unix.NFNL_SUBSYS_CTNETLINK << 8),
wantErr: true,
wantNftMsgType: nil,
wantString: "",
},
{
name: "InvalidMsgType",
headerType: netlink.HeaderType(unix.NFNL_SUBSYS_NFTABLES<<8 | uint16(nftMsgMax+1)),
wantErr: true,
wantNftMsgType: nil,
wantString: "",
},
{
name: "NewTable",
headerType: netlink.HeaderType(unix.NFNL_SUBSYS_NFTABLES<<8 | uint16(nftMsgNewTable)),
wantErr: false,
wantNftMsgType: nftMsgNewTable.Ptr(),
wantString: "NFT_MSG_NEWTABLE",
},
{
name: "GetChain",
headerType: netlink.HeaderType(unix.NFNL_SUBSYS_NFTABLES<<8 | uint16(nftMsgGetChain)),
wantErr: false,
wantNftMsgType: nftMsgGetChain.Ptr(),
wantString: "NFT_MSG_GETCHAIN",
},
{
name: "DelSet",
headerType: netlink.HeaderType(unix.NFNL_SUBSYS_NFTABLES<<8 | uint16(nftMsgDelSet)),
wantErr: false,
wantNftMsgType: nftMsgDelSet.Ptr(),
wantString: "NFT_MSG_DELSET",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseNftMsgType(tt.headerType)
if (err != nil) != tt.wantErr {
t.Errorf("parseHeaderType() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && *got != *tt.wantNftMsgType {
t.Errorf("parseHeaderType() = %v, want %v", got, tt.wantNftMsgType)
}
if !tt.wantErr && got.String() != tt.wantString {
t.Errorf("nftMsgType.String() = %v, want %v", got.String(), tt.wantString)
}
})
}
}
func TestNftMsgHeaderType(t *testing.T) {
var tests = []struct {
name string
msgType nftMsgType
want netlink.HeaderType
}{
{
name: "nftMsgNewTable",
msgType: nftMsgNewTable,
want: netlink.HeaderType(unix.NFNL_SUBSYS_NFTABLES<<8 | uint16(nftMsgNewTable)),
},
{
name: "nftMsgGetChain",
msgType: nftMsgGetChain,
want: netlink.HeaderType(unix.NFNL_SUBSYS_NFTABLES<<8 | uint16(nftMsgGetChain)),
},
{
name: "nftMsgDelSet",
msgType: nftMsgDelSet,
want: netlink.HeaderType(unix.NFNL_SUBSYS_NFTABLES<<8 | uint16(nftMsgDelSet)),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.msgType.HeaderType()
if got != tt.want {
t.Errorf("HeaderType() = %v, want %v", got, tt.want)
}
})
}
}