The duplicate VPN address checks only verify that the command fails, so
they also pass if it fails for an unrelated reason.

The creation checks use OVPN_NUM_PEERS + 1 as the new peer ID, but the
UDP server loads the whole peers file regardless of OVPN_NUM_PEERS.
When the variable is overridden with a smaller value the ID is already
in use, and ovpn_peer_add_mp fails with -EEXIST before looking at the
VPN address. The update checks use peer 2, which does not exist in TCP
mode with OVPN_NUM_PEERS=1, and would fail with -ENOENT.

Use a fixed ID that never clashes with the peers files, skip the update
checks when peer 2 is not configured, and make all of them require
"Address already in use" in the command output so that they only pass
on a real address conflict.

Fixes: 006208026819 ("selftests: ovpn: validate peer VPN addresses")
Link: 
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260921102215.3599702-1-antonio%40openvpn.net?part=11
Signed-off-by: Ralf Lici <[email protected]>
---
 tools/testing/selftests/net/ovpn/common.sh | 21 +++++++++++++++++
 tools/testing/selftests/net/ovpn/test.sh   | 26 ++++++++++++++++------
 2 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/net/ovpn/common.sh 
b/tools/testing/selftests/net/ovpn/common.sh
index da732c64ffea..7c8bf260a588 100644
--- a/tools/testing/selftests/net/ovpn/common.sh
+++ b/tools/testing/selftests/net/ovpn/common.sh
@@ -65,6 +65,19 @@ ovpn_cmd_run() {
        fi
 
        case "${mode}" in
+       fail:*)
+               # The mode is "fail:<msg>", as set by ovpn_cmd_fail_msg. Strip
+               # the "fail:" prefix with ${mode#fail:} to get the expected
+               # error message and look for it in the command output.
+               if [[ "${rc}" -eq 0 ]]; then
+                       ret=1
+               elif ! grep -qF "${mode#fail:}" "${output_file}"; then
+                       cat "${output_file}"
+                       printf '%s\n' \
+                               "${label}: expected error \"${mode#fail:}\": $*"
+                       ret=1
+               fi
+               ;;
        ok)
                if [[ "${rc}" -ne 0 ]]; then
                        cat "${output_file}"
@@ -100,6 +113,14 @@ ovpn_cmd_fail() {
        ovpn_cmd_run fail "$@"
 }
 
+# like ovpn_cmd_fail, but the command must also print the given error message
+ovpn_cmd_fail_msg() {
+       local msg="$1"
+
+       shift
+       ovpn_cmd_run "fail:${msg}" "$@"
+}
+
 ovpn_run_bg() {
        local pid_var="$1"
 
diff --git a/tools/testing/selftests/net/ovpn/test.sh 
b/tools/testing/selftests/net/ovpn/test.sh
index 0762fa83e4b5..5bfa3c8a5fc2 100755
--- a/tools/testing/selftests/net/ovpn/test.sh
+++ b/tools/testing/selftests/net/ovpn/test.sh
@@ -74,7 +74,10 @@ ovpn_set_peer_vpn_addr() {
 ovpn_run_vpn_addr_validation() {
        local addr
        local peer1_addr4
-       local test_peer_id=$((OVPN_NUM_PEERS + 1))
+       # must not match any peer ID in the peers files, regardless of
+       # OVPN_NUM_PEERS, as peer ID conflicts are checked before VPN addresses
+       local test_peer_id=100
+       local eaddrinuse="Address already in use"
        local test_peer_addr6="2001:db8::2"
        # Do not include 0.0.0.0 or :: here. They are invalid on creation, but
        # clear one address family on update and are valid if the other remains.
@@ -98,7 +101,8 @@ ovpn_run_vpn_addr_validation() {
                        ovpn_new_test_peer "${test_peer_id}" "${addr}"
        done
 
-       ovpn_cmd_fail "reject duplicate IPv4 address on peer creation" \
+       ovpn_cmd_fail_msg "${eaddrinuse}" \
+               "reject duplicate IPv4 address on peer creation" \
                ovpn_new_test_peer "${test_peer_id}" "${peer1_addr4}"
        ovpn_cmd_fail "reject clearing the last peer VPN address" \
                ovpn_set_peer_vpn_addr 1 0.0.0.0
@@ -108,15 +112,23 @@ ovpn_run_vpn_addr_validation() {
                        ovpn_set_peer_vpn_addr 1 "${addr}"
        done
 
-       ovpn_cmd_fail "reject duplicate IPv4 address on peer update" \
-               ovpn_set_peer_vpn_addr 2 "${peer1_addr4}"
+       # peer 2 only exists when at least two peers are configured
+       if [ "${OVPN_NUM_PEERS}" -ge 2 ]; then
+               ovpn_cmd_fail_msg "${eaddrinuse}" \
+                       "reject duplicate IPv4 address on peer update" \
+                       ovpn_set_peer_vpn_addr 2 "${peer1_addr4}"
+       fi
 
        ovpn_cmd_ok "add peer IPv6 address" \
                ovpn_set_peer_vpn_addr 1 "${test_peer_addr6}"
-       ovpn_cmd_fail "reject duplicate IPv6 address on peer creation" \
+       ovpn_cmd_fail_msg "${eaddrinuse}" \
+               "reject duplicate IPv6 address on peer creation" \
                ovpn_new_test_peer "${test_peer_id}" "${test_peer_addr6}"
-       ovpn_cmd_fail "reject duplicate IPv6 address on peer update" \
-               ovpn_set_peer_vpn_addr 2 "${test_peer_addr6}"
+       if [ "${OVPN_NUM_PEERS}" -ge 2 ]; then
+               ovpn_cmd_fail_msg "${eaddrinuse}" \
+                       "reject duplicate IPv6 address on peer update" \
+                       ovpn_set_peer_vpn_addr 2 "${test_peer_addr6}"
+       fi
 
        ovpn_cmd_ok "clear peer IPv4 address" \
                ovpn_set_peer_vpn_addr 1 0.0.0.0
-- 
2.56.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to