-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnfchains_test.go
65 lines (62 loc) · 1.53 KB
/
nfchains_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
package nftableslib
import (
"testing"
"github.com/google/nftables"
)
func TestChains(t *testing.T) {
tests := []struct {
name string
chain string
attributes *ChainAttributes
success bool
}{
{
name: "Base chain, correct attributes",
chain: "chain-1",
attributes: &ChainAttributes{
Hook: nftables.ChainHookInput,
Priority: nftables.ChainPriorityFilter,
Type: nftables.ChainTypeFilter,
},
success: true,
},
{
name: "Base chain, missing type",
chain: "chain-2",
attributes: &ChainAttributes{
Hook: nftables.ChainHookInput,
Priority: nftables.ChainPriorityFilter,
Type: "",
},
success: false,
},
{
name: "Regular chain",
chain: "chain-3",
attributes: nil,
success: true,
},
}
conn := InitConn()
if conn == nil {
t.Fatal("initialization of netlink connection failed")
}
nft := InitNFTables(conn)
nft.Tables().Create("test", nftables.TableFamilyIPv4)
tbl, err := nft.Tables().Table("test", nftables.TableFamilyIPv4)
if err != nil {
t.Fatalf("failed to get chain interface for table test of type nftables.TableFamilyIPv4")
}
defer nft.Tables().DeleteImm("test", nftables.TableFamilyIPv4)
for _, tt := range tests {
err := tbl.Chains().Create(tt.chain, tt.attributes)
if err != nil && tt.success {
t.Errorf("test: %s failed with error: %+v but supposed to succeed", tt.name, err)
continue
}
if err == nil && !tt.success {
t.Errorf("test: \"%s\" succeed but supposed to fail", tt.name)
continue
}
}
}