Skip to content

Commit 203bb9d

Browse files
Phil SutterFlorian Westphal
authored andcommitted
selftests: netfilter: Extend nft_audit.sh
Add tests for sets and elements and deletion of all kinds. Also reorder rule reset tests: By moving the bulk rule add command up, the two 'reset rules' tests become identical. While at it, fix for a failing bulk rule add test's error status getting lost due to its use in a pipe. Avoid this by using a temporary file. Headings in diff output for failing tests contain no useful data, strip them. Signed-off-by: Phil Sutter <[email protected]> Signed-off-by: Florian Westphal <[email protected]>
1 parent cf791b2 commit 203bb9d

File tree

1 file changed

+81
-16
lines changed

1 file changed

+81
-16
lines changed

tools/testing/selftests/netfilter/nft_audit.sh

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ nft --version >/dev/null 2>&1 || {
1212
}
1313

1414
logfile=$(mktemp)
15+
rulefile=$(mktemp)
1516
echo "logging into $logfile"
1617
./audit_logread >"$logfile" &
1718
logread_pid=$!
18-
trap 'kill $logread_pid; rm -f $logfile' EXIT
19+
trap 'kill $logread_pid; rm -f $logfile $rulefile' EXIT
1920
exec 3<"$logfile"
2021

2122
do_test() { # (cmd, log)
@@ -26,12 +27,14 @@ do_test() { # (cmd, log)
2627
res=$(diff -a -u <(echo "$2") - <&3)
2728
[ $? -eq 0 ] && { echo "OK"; return; }
2829
echo "FAIL"
29-
echo "$res"
30-
((RC++))
30+
grep -v '^\(---\|+++\|@@\)' <<< "$res"
31+
((RC--))
3132
}
3233

3334
nft flush ruleset
3435

36+
# adding tables, chains and rules
37+
3538
for table in t1 t2; do
3639
do_test "nft add table $table" \
3740
"table=$table family=2 entries=1 op=nft_register_table"
@@ -62,6 +65,28 @@ for table in t1 t2; do
6265
"table=$table family=2 entries=6 op=nft_register_rule"
6366
done
6467

68+
for ((i = 0; i < 500; i++)); do
69+
echo "add rule t2 c3 counter accept comment \"rule $i\""
70+
done >$rulefile
71+
do_test "nft -f $rulefile" \
72+
'table=t2 family=2 entries=500 op=nft_register_rule'
73+
74+
# adding sets and elements
75+
76+
settype='type inet_service; counter'
77+
setelem='{ 22, 80, 443 }'
78+
setblock="{ $settype; elements = $setelem; }"
79+
do_test "nft add set t1 s $setblock" \
80+
"table=t1 family=2 entries=4 op=nft_register_set"
81+
82+
do_test "nft add set t1 s2 $setblock; add set t1 s3 { $settype; }" \
83+
"table=t1 family=2 entries=5 op=nft_register_set"
84+
85+
do_test "nft add element t1 s3 $setelem" \
86+
"table=t1 family=2 entries=3 op=nft_register_setelem"
87+
88+
# resetting rules
89+
6590
do_test 'nft reset rules t1 c2' \
6691
'table=t1 family=2 entries=3 op=nft_reset_rule'
6792

@@ -70,19 +95,6 @@ do_test 'nft reset rules table t1' \
7095
table=t1 family=2 entries=3 op=nft_reset_rule
7196
table=t1 family=2 entries=3 op=nft_reset_rule'
7297

73-
do_test 'nft reset rules' \
74-
'table=t1 family=2 entries=3 op=nft_reset_rule
75-
table=t1 family=2 entries=3 op=nft_reset_rule
76-
table=t1 family=2 entries=3 op=nft_reset_rule
77-
table=t2 family=2 entries=3 op=nft_reset_rule
78-
table=t2 family=2 entries=3 op=nft_reset_rule
79-
table=t2 family=2 entries=3 op=nft_reset_rule'
80-
81-
for ((i = 0; i < 500; i++)); do
82-
echo "add rule t2 c3 counter accept comment \"rule $i\""
83-
done | do_test 'nft -f -' \
84-
'table=t2 family=2 entries=500 op=nft_register_rule'
85-
8698
do_test 'nft reset rules t2 c3' \
8799
'table=t2 family=2 entries=189 op=nft_reset_rule
88100
table=t2 family=2 entries=188 op=nft_reset_rule
@@ -105,4 +117,57 @@ table=t2 family=2 entries=180 op=nft_reset_rule
105117
table=t2 family=2 entries=188 op=nft_reset_rule
106118
table=t2 family=2 entries=135 op=nft_reset_rule'
107119

120+
# resetting sets and elements
121+
122+
elem=(22 ,80 ,443)
123+
relem=""
124+
for i in {1..3}; do
125+
relem+="${elem[((i - 1))]}"
126+
do_test "nft reset element t1 s { $relem }" \
127+
"table=t1 family=2 entries=$i op=nft_reset_setelem"
128+
done
129+
130+
do_test 'nft reset set t1 s' \
131+
'table=t1 family=2 entries=3 op=nft_reset_setelem'
132+
133+
# deleting rules
134+
135+
readarray -t handles < <(nft -a list chain t1 c1 | \
136+
sed -n 's/.*counter.* handle \(.*\)$/\1/p')
137+
138+
do_test "nft delete rule t1 c1 handle ${handles[0]}" \
139+
'table=t1 family=2 entries=1 op=nft_unregister_rule'
140+
141+
cmd='delete rule t1 c1 handle'
142+
do_test "nft $cmd ${handles[1]}; $cmd ${handles[2]}" \
143+
'table=t1 family=2 entries=2 op=nft_unregister_rule'
144+
145+
do_test 'nft flush chain t1 c2' \
146+
'table=t1 family=2 entries=3 op=nft_unregister_rule'
147+
148+
do_test 'nft flush table t2' \
149+
'table=t2 family=2 entries=509 op=nft_unregister_rule'
150+
151+
# deleting chains
152+
153+
do_test 'nft delete chain t2 c2' \
154+
'table=t2 family=2 entries=1 op=nft_unregister_chain'
155+
156+
# deleting sets and elements
157+
158+
do_test 'nft delete element t1 s { 22 }' \
159+
'table=t1 family=2 entries=1 op=nft_unregister_setelem'
160+
161+
do_test 'nft delete element t1 s { 80, 443 }' \
162+
'table=t1 family=2 entries=2 op=nft_unregister_setelem'
163+
164+
do_test 'nft flush set t1 s2' \
165+
'table=t1 family=2 entries=3 op=nft_unregister_setelem'
166+
167+
do_test 'nft delete set t1 s2' \
168+
'table=t1 family=2 entries=1 op=nft_unregister_set'
169+
170+
do_test 'nft delete set t1 s3' \
171+
'table=t1 family=2 entries=1 op=nft_unregister_set'
172+
108173
exit $RC

0 commit comments

Comments
 (0)