Skip to content

Commit 2f4bba4

Browse files
Fabian Frederickummakynes
authored andcommitted
selftests: netfilter: simplify command testing
Fix some shellcheck SC2181 warnings: "Check exit code directly with e.g. 'if mycmd;', not indirectly with $?." as suggested by Stefano Brivio. Signed-off-by: Fabian Frederick <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent d721b68 commit 2f4bba4

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

tools/testing/selftests/netfilter/nft_flowtable.sh

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ ns2out=""
2727
log_netns=$(sysctl -n net.netfilter.nf_log_all_netns)
2828

2929
checktool (){
30-
$1 > /dev/null 2>&1
31-
if [ $? -ne 0 ];then
30+
if ! $1 > /dev/null 2>&1; then
3231
echo "SKIP: Could not $2"
3332
exit $ksft_skip
3433
fi
@@ -187,15 +186,13 @@ if [ $? -ne 0 ]; then
187186
fi
188187

189188
# test basic connectivity
190-
ip netns exec ns1 ping -c 1 -q 10.0.2.99 > /dev/null
191-
if [ $? -ne 0 ];then
189+
if ! ip netns exec ns1 ping -c 1 -q 10.0.2.99 > /dev/null; then
192190
echo "ERROR: ns1 cannot reach ns2" 1>&2
193191
bash
194192
exit 1
195193
fi
196194

197-
ip netns exec ns2 ping -c 1 -q 10.0.1.99 > /dev/null
198-
if [ $? -ne 0 ];then
195+
if ! ip netns exec ns2 ping -c 1 -q 10.0.1.99 > /dev/null; then
199196
echo "ERROR: ns2 cannot reach ns1" 1>&2
200197
exit 1
201198
fi
@@ -230,8 +227,7 @@ check_transfer()
230227
out=$2
231228
what=$3
232229

233-
cmp "$in" "$out" > /dev/null 2>&1
234-
if [ $? -ne 0 ] ;then
230+
if ! cmp "$in" "$out" > /dev/null 2>&1; then
235231
echo "FAIL: file mismatch for $what" 1>&2
236232
ls -l "$in"
237233
ls -l "$out"
@@ -268,13 +264,11 @@ test_tcp_forwarding_ip()
268264

269265
wait
270266

271-
check_transfer "$ns1in" "$ns2out" "ns1 -> ns2"
272-
if [ $? -ne 0 ];then
267+
if ! check_transfer "$ns1in" "$ns2out" "ns1 -> ns2"; then
273268
lret=1
274269
fi
275270

276-
check_transfer "$ns2in" "$ns1out" "ns1 <- ns2"
277-
if [ $? -ne 0 ];then
271+
if ! check_transfer "$ns2in" "$ns1out" "ns1 <- ns2"; then
278272
lret=1
279273
fi
280274

@@ -308,8 +302,7 @@ make_file "$ns2in"
308302

309303
# First test:
310304
# No PMTU discovery, nsr1 is expected to fragment packets from ns1 to ns2 as needed.
311-
test_tcp_forwarding ns1 ns2
312-
if [ $? -eq 0 ] ;then
305+
if test_tcp_forwarding ns1 ns2; then
313306
echo "PASS: flow offloaded for ns1/ns2"
314307
else
315308
echo "FAIL: flow offload for ns1/ns2:" 1>&2
@@ -340,9 +333,7 @@ table ip nat {
340333
}
341334
EOF
342335

343-
test_tcp_forwarding_nat ns1 ns2
344-
345-
if [ $? -eq 0 ] ;then
336+
if test_tcp_forwarding_nat ns1 ns2; then
346337
echo "PASS: flow offloaded for ns1/ns2 with NAT"
347338
else
348339
echo "FAIL: flow offload for ns1/ns2 with NAT" 1>&2
@@ -354,17 +345,15 @@ fi
354345
# Same as second test, but with PMTU discovery enabled.
355346
handle=$(ip netns exec nsr1 nft -a list table inet filter | grep something-to-grep-for | cut -d \# -f 2)
356347

357-
ip netns exec nsr1 nft delete rule inet filter forward $handle
358-
if [ $? -ne 0 ] ;then
348+
if ! ip netns exec nsr1 nft delete rule inet filter forward $handle; then
359349
echo "FAIL: Could not delete large-packet accept rule"
360350
exit 1
361351
fi
362352

363353
ip netns exec ns1 sysctl net.ipv4.ip_no_pmtu_disc=0 > /dev/null
364354
ip netns exec ns2 sysctl net.ipv4.ip_no_pmtu_disc=0 > /dev/null
365355

366-
test_tcp_forwarding_nat ns1 ns2
367-
if [ $? -eq 0 ] ;then
356+
if test_tcp_forwarding_nat ns1 ns2; then
368357
echo "PASS: flow offloaded for ns1/ns2 with NAT and pmtu discovery"
369358
else
370359
echo "FAIL: flow offload for ns1/ns2 with NAT and pmtu discovery" 1>&2
@@ -410,8 +399,7 @@ ip -net ns2 route del 192.168.10.1 via 10.0.2.1
410399
ip -net ns2 route add default via 10.0.2.1
411400
ip -net ns2 route add default via dead:2::1
412401

413-
test_tcp_forwarding ns1 ns2
414-
if [ $? -eq 0 ] ;then
402+
if test_tcp_forwarding ns1 ns2; then
415403
echo "PASS: ipsec tunnel mode for ns1/ns2"
416404
else
417405
echo "FAIL: ipsec tunnel mode for ns1/ns2"

0 commit comments

Comments
 (0)