selftests: mptcp: check output: catch cmd errors

Using '${?}' inside the if-statement to check the returned value from
the command that was evaluated as part of the if-statement is not
correct: here, '${?}' will be linked to the previous instruction, not
the one that is expected here (${cmd}).

Instead, simply mark the error, except if an error is expected. If
that's the case, 1 can be passed as the 4th argument of this helper.
Three checks from pm_netlink.sh expect an error.

While at it, improve the error message when the command unexpectedly
fails or succeeds.

Note that we could expect a specific returned value, but the checks
currently expecting an error can be used with 'ip mptcp' or 'pm_nl_ctl',
and these two tools don't return the same error code.

Fixes: 2d0c1d27ea ("selftests: mptcp: add mptcp_lib_check_output helper")
Cc: stable@vger.kernel.org
Reviewed-by: Mat Martineau <martineau@kernel.org>
Signed-off-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
Link: https://patch.msgid.link/20260505-net-mptcp-pm-fixes-7-1-rc3-v1-10-fca8091060a4@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Matthieu Baerts (NGI0)
2026-05-05 17:00:58 +02:00
committed by Jakub Kicinski
parent 166b783440
commit 65db7b27b9
2 changed files with 16 additions and 10 deletions

View File

@@ -474,20 +474,24 @@ mptcp_lib_wait_local_port_listen() {
wait_local_port_listen "${@}" "tcp"
}
# $1: error file, $2: cmd, $3: expected msg, [$4: expected error]
mptcp_lib_check_output() {
local err="${1}"
local cmd="${2}"
local expected="${3}"
local exp_error="${4:-0}"
local cmd_ret=0
local out
if ! out=$(${cmd} 2>"${err}"); then
cmd_ret=${?}
fi
out=$(${cmd} 2>"${err}") || cmd_ret=1
if [ ${cmd_ret} -ne 0 ]; then
mptcp_lib_pr_fail "command execution '${cmd}' stderr"
cat "${err}"
if [ "${cmd_ret}" != "${exp_error}" ]; then
mptcp_lib_pr_fail "unexpected returned code for '${cmd}', info:"
if [ "${exp_error}" = 0 ]; then
cat "${err}"
else
echo "${out}"
fi
return 2
elif [ "${out}" = "${expected}" ]; then
return 0

View File

@@ -122,10 +122,12 @@ check()
local cmd="$1"
local expected="$2"
local msg="$3"
local exp_error="$4"
local rc=0
mptcp_lib_print_title "$msg"
mptcp_lib_check_output "${err}" "${cmd}" "${expected}" || rc=${?}
mptcp_lib_check_output "${err}" "${cmd}" "${expected}" "${exp_error}" ||
rc=${?}
if [ ${rc} -eq 2 ]; then
mptcp_lib_result_fail "${msg} # error ${rc}"
ret=${KSFT_FAIL}
@@ -158,13 +160,13 @@ check "show_endpoints" \
"3,10.0.1.3,signal backup")" "dump addrs"
del_endpoint 2
check "get_endpoint 2" "" "simple del addr"
check "get_endpoint 2" "" "simple del addr" 1
check "show_endpoints" \
"$(format_endpoints "1,10.0.1.1" \
"3,10.0.1.3,signal backup")" "dump addrs after del"
add_endpoint 10.0.1.3 2>/dev/null
check "get_endpoint 4" "" "duplicate addr"
check "get_endpoint 4" "" "duplicate addr" 1
add_endpoint 10.0.1.4 flags signal
check "get_endpoint 4" "$(format_endpoints "4,10.0.1.4,signal")" "id addr increment"
@@ -173,7 +175,7 @@ for i in $(seq 5 9); do
add_endpoint "10.0.1.${i}" flags signal >/dev/null 2>&1
done
check "get_endpoint 9" "$(format_endpoints "9,10.0.1.9,signal")" "hard addr limit"
check "get_endpoint 10" "" "above hard addr limit"
check "get_endpoint 10" "" "above hard addr limit" 1
del_endpoint 9
for i in $(seq 10 255); do