Debian Codegraph @

nft/config/chain_test.go

raw symbols SCIP
1 /*
2 * This file is part of the go-nft project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Copyright 2021 Red Hat, Inc.
17 *
18 */
19
20 package config_test
21
22 import (
23 "fmt"
24 "strings"
25 "testing"
26
27 assert "github.com/stretchr/testify/require"
28
29 "github.com/networkplumbing/go-nft/nft"
30 "github.com/networkplumbing/go-nft/nft/schema"
31 )
32
33 type chainAction string
34
35 type chainActionFunc func(*nft.Config, *schema.Chain)
36
37 // Chain Actions
38 const (
39 chainADD chainAction = "add"
40 chainDELETE chainAction = "delete"
41 chainFLUSH chainAction = "flush"
42 )
43
44 const chainName = "test-chain"
45
46 func TestChain(t *testing.T) {
47 testAddBaseChains(t)
48 // Removal of base-chains is identical to the removal of regular-chains.
49 // Therefore, such scenarios are evaluated through the regular-chains actions
50 testRegularChainsActions(t)
51
52 testChainLookup(t)
53 }
54
55 func testAddBaseChains(t *testing.T) {
56 types := []nft.ChainType{
57 nft.TypeFilter,
58 nft.TypeNAT,
59 nft.TypeRoute,
60 }
61 hooks := []nft.ChainHook{
62 nft.HookPreRouting,
63 nft.HookInput,
64 nft.HookOutput,
65 nft.HookForward,
66 nft.HookPostRouting,
67 nft.HookIngress,
68 }
69 policies := []nft.ChainPolicy{
70 nft.PolicyAccept,
71 nft.PolicyDrop,
72 "",
73 }
74
75 table := nft.NewTable(tableName, nft.FamilyIP)
76
77 for _, ctype := range types {
78 for _, hook := range hooks {
79 for _, policy := range policies {
80 testName := createChainTestName(chainADD, ctype, hook, policy)
81
82 t.Run(testName, func(t *testing.T) {
83 priority := 100
84 chain := nft.NewChain(table, chainName, &ctype, &hook, &priority, &policy)
85 config := nft.NewConfig()
86 config.AddChain(chain)
87
88 serializedConfig, err := config.ToJSON()
89 assert.NoError(t, err)
90
91 chainArgs := fmt.Sprintf(
92 `"family":%q,"table":%q,"name":%q,"type":%q,"hook":%q,"prio":%d`,
93 table.Family, table.Name, chainName, ctype, hook, priority,
94 )
95 if policy != "" {
96 chainArgs += fmt.Sprintf(`,"policy":%q`, policy)
97 }
98 expected := []byte(fmt.Sprintf(`{"nftables":[{"chain":{%s}}]}`, chainArgs))
99 assert.Equal(t, string(expected), string(serializedConfig))
100 })
101 }
102 }
103 }
104 }
105
106 func testRegularChainsActions(t *testing.T) {
107 actions := map[chainAction]chainActionFunc{
108 chainADD: func(c *nft.Config, chain *schema.Chain) { c.AddChain(chain) },
109 chainDELETE: func(c *nft.Config, chain *schema.Chain) { c.DeleteChain(chain) },
110 chainFLUSH: func(c *nft.Config, chain *schema.Chain) { c.FlushChain(chain) },
111 }
112
113 table := nft.NewTable(tableName, nft.FamilyIP)
114 chain := nft.NewRegularChain(table, chainName)
115
116 for action, actionFunc := range actions {
117 testName := createChainTestName(action, "", "", "")
118
119 t.Run(testName, func(t *testing.T) {
120 config := nft.NewConfig()
121 actionFunc(config, chain)
122
123 serializedConfig, err := config.ToJSON()
124 assert.NoError(t, err)
125
126 chainArgs := fmt.Sprintf(`"family":%q,"table":%q,"name":%q`, table.Family, table.Name, chainName)
127 var expected []byte
128 if action == chainADD {
129 expected = []byte(fmt.Sprintf(`{"nftables":[{"chain":{%s}}]}`, chainArgs))
130 } else {
131 expected = []byte(fmt.Sprintf(`{"nftables":[{%q:{"chain":{%s}}}]}`, action, chainArgs))
132 }
133
134 assert.Equal(t, string(expected), string(serializedConfig))
135 })
136 }
137 }
138
139 func createChainTestName(action chainAction, ctype nft.ChainType, hook nft.ChainHook, policy nft.ChainPolicy) string {
140 args := []string{string(action)}
141 if ctype != "" {
142 args = append(args, string(ctype))
143 }
144 if hook != "" {
145 args = append(args, string(hook))
146 }
147 if policy != "" {
148 args = append(args, string(policy))
149 }
150
151 return strings.Join(args, " ")
152 }
153
154 func testChainLookup(t *testing.T) {
155 config := nft.NewConfig()
156 table_br := nft.NewTable("table-br", nft.FamilyBridge)
157 config.AddTable(table_br)
158
159 chainRegular := nft.NewRegularChain(table_br, "chain-regular")
160 config.AddChain(chainRegular)
161
162 ctype, hook, prio, policy := nft.TypeFilter, nft.HookPreRouting, 100, nft.PolicyAccept
163 chainBase := nft.NewChain(table_br, "chain-base", &ctype, &hook, &prio, &policy)
164 config.AddChain(chainBase)
165
166 t.Run("Lookup an existing regular chain", func(t *testing.T) {
167 chain := config.LookupChain(chainRegular)
168 assert.Equal(t, chainRegular, chain)
169 })
170
171 t.Run("Lookup an existing base chain", func(t *testing.T) {
172 chain := config.LookupChain(chainBase)
173 assert.Equal(t, chainBase, chain)
174 })
175
176 t.Run("Lookup a missing regular chain", func(t *testing.T) {
177 chain := nft.NewRegularChain(table_br, "chain-na")
178 assert.Nil(t, config.LookupChain(chain))
179 })
180
181 t.Run("Lookup a missing base chain", func(t *testing.T) {
182 inputHook := nft.HookInput
183 chain := nft.NewChain(table_br, "chain-base", &ctype, &inputHook, &prio, &policy)
184 assert.Nil(t, config.LookupChain(chain))
185 })
186 }