From: "flichtenheld (Code Review)" Date: Wed, 25 Mar 2026 15:38:33 +0000 In-Reply-To: References: Reply-To: frank@...2641..., arne-openvpn@...1227..., gianmarco@...2726..., openvpn-devel@lists.sourceforge.net Message-ID: <8e3274aec9da18aaac466d39a50c22aacaa33517-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="nY1LFMwbWag="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: test_networking: Allow to test --netns List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , its_Giaan Cc: openvpn-devel --nY1LFMwbWag= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, its_Giaan, plaisthos. Hello its_Giaan, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/1586?usp=3Demail to look at the new patch set (#2). The following approvals got outdated and were removed: Code-Review+2 by its_Giaan Change subject: test_networking: Allow to test --netns ...................................................................... test_networking: Allow to test --netns While here also do some cleanups: - Run under set -eu to better catch errors - Some comment improvments - Fix argument handling in test_networking, it actually ignored the interface argument Change-Id: Ibaf84cba0124320f7c49e28d1b3364dac9e8b6d9 Signed-off-by: Frank Lichtenheld --- M tests/t_net.sh M tests/unit_tests/openvpn/test_networking.c 2 files changed, 58 insertions(+), 34 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/86/1586/2 diff --git a/tests/t_net.sh b/tests/t_net.sh index 8134832..e388971 100755 --- a/tests/t_net.sh +++ b/tests/t_net.sh @@ -1,8 +1,14 @@ #!/usr/bin/env bash +# Requires bash due to array handling -IFACE=3D"ovpn-dummy0" +set -eu + +IFACE=3D"${IFACE:-ovpn-dummy0}" +NETNS=3D${NETNS:-} UNIT_TEST=3D"./unit_tests/openvpn/networking_testdriver" +# Tests 0 to LAST_AUTO_TEST output a ip command to compare results LAST_AUTO_TEST=3D7 +# Tests after LAST_AUTO_TEST are only executed, no comparison LAST_TEST=3D8 srcdir=3D"${srcdir:-.}" @@ -12,33 +18,19 @@ # bail out right away on non-linux. NetLink (the object of this test) is o= nly # used on Linux, therefore testing other platform is not needed. -# -# Note: statements in the rest of the script may not even pass syntax chec= k on -# solaris/bsd. It uses /bin/bash if [ "$(uname -s)" !=3D "Linux" ]; then echo "$0: this test runs only on Linux. SKIPPING TEST." exit 77 fi -# Commands used to retrieve the network state. -# State is retrieved after running sitnl and after running -# iproute commands. The two are then compared and expected to be equal. -typeset -a GET_STATE -GET_STATE[0]=3D"ip link show dev $IFACE | sed 's/^[0-9]\+: //'" -GET_STATE[1]=3D"ip addr show dev $IFACE | sed 's/^[0-9]\+: //'" -GET_STATE[2]=3D"ip route show dev $IFACE" -GET_STATE[3]=3D"ip -6 route show dev $IFACE" - -LAST_STATE=3D$((${#GET_STATE[@]} - 1)) - reload_dummy() { - $RUN_SUDO ip link del $IFACE - $RUN_SUDO ip link add $IFACE address 00:11:22:33:44:55 type dummy - $RUN_SUDO ip link set dev $IFACE state up + $RUN_SUDO $IP_EXEC ip link del $IFACE || true + $RUN_SUDO $IP_EXEC ip link add $IFACE address 00:11:22:33:44:55 type d= ummy + $RUN_SUDO $IP_EXEC ip link set dev $IFACE state up if [ $? -ne 0 ]; then - echo "can't create interface $IFACE" + echo "can't create interface $IFACE $IP_NETNS" exit 1 fi } @@ -51,7 +43,7 @@ # the unit-test prints to stdout the iproute command corresponding # to the sitnl operation being executed. # Format is "CMD: " - OUT=3D$($RUN_SUDO $UNIT_TEST $k $IFACE) + OUT=3D$($RUN_SUDO $UNIT_TEST $k $IFACE $NETNS) # ensure unit test worked properly if [ $? -ne 0 ]; then echo "unit-test $k errored out:" @@ -60,7 +52,7 @@ fi NEW=3D$(echo "$OUT" | sed -n 's/CMD: //p') - CMD=3D"$CMD $RUN_SUDO $NEW ;" + CMD=3D"$CMD $IP_EXEC $NEW ;" done # collect state for later comparison @@ -72,13 +64,18 @@ ## execution starts here +set +eu + # t_client.rc required only for RUN_SUDO definition +RUN_SUDO=3D if [ -r "${top_builddir}"/t_client.rc ]; then . "${top_builddir}"/t_client.rc elif [ -r "${srcdir}"/t_client.rc ]; then . "${srcdir}"/t_client.rc fi +set -eu + if [ ! -x "$openvpn" ]; then echo "no (executable) openvpn binary in current build tree. FAIL." >&2 exit 1 @@ -129,6 +126,24 @@ fi fi +IP_EXEC=3D"$RUN_SUDO" +if [ -n "$NETNS" ]; then + echo "Using netns $NETNS" + [ -f "/run/netns/$NETNS" ] || $RUN_SUDO ip netns add "$NETNS" + IP_EXEC=3D"$RUN_SUDO ip netns exec $NETNS" +fi + +# Commands used to retrieve the network state. +# State is retrieved after running sitnl and after running +# iproute commands. The two are then compared and expected to be equal. +typeset -a GET_STATE +GET_STATE[0]=3D"$IP_EXEC ip link show dev $IFACE | sed 's/^[0-9]\+: //'" +GET_STATE[1]=3D"$IP_EXEC ip addr show dev $IFACE | sed 's/^[0-9]\+: //'" +GET_STATE[2]=3D"$IP_EXEC ip route show dev $IFACE" +GET_STATE[3]=3D"$IP_EXEC ip -6 route show dev $IFACE" + +LAST_STATE=3D$((${#GET_STATE[@]} - 1)) + for i in $(seq 0 $LAST_AUTO_TEST); do # reload dummy module to cleanup state reload_dummy @@ -167,7 +182,7 @@ done # remove interface for good -$RUN_SUDO ip link del $IFACE +$IP_EXEC ip link del $IFACE for i in $(seq $(($LAST_AUTO_TEST + 1)) ${LAST_TEST}); do $RUN_SUDO $UNIT_TEST $i diff --git a/tests/unit_tests/openvpn/test_networking.c b/tests/unit_tests/= openvpn/test_networking.c index 1c9cac1..4e2cce8 100644 --- a/tests/unit_tests/openvpn/test_networking.c +++ b/tests/unit_tests/openvpn/test_networking.c @@ -6,26 +6,27 @@ #include static char *iface =3D "ovpn-dummy0"; +static openvpn_net_ctx_t *ctx =3D NULL; static int net__iface_up(bool up) { printf("CMD: ip link set %s %s\n", iface, up ? "up" : "down"); - return net_iface_up(NULL, iface, up); + return net_iface_up(ctx, iface, up); } static int net__iface_new(const char *name, const char *type) { - return net_iface_new(NULL, name, type, NULL); + return net_iface_new(ctx, name, type, NULL); } static int net__iface_type(const char *name, const char *type) { char ret_type[IFACE_TYPE_LEN_MAX]; - int ret =3D net_iface_type(NULL, name, ret_type); + int ret =3D net_iface_type(ctx, name, ret_type); if (ret =3D=3D 0) { assert_string_equal(type, ret_type); @@ -37,7 +38,7 @@ static int net__iface_del(const char *name) { - return net_iface_del(NULL, name); + return net_iface_del(ctx, name); } static int @@ -45,7 +46,7 @@ { printf("CMD: ip link set %s mtu %d\n", iface, mtu); - return net_iface_mtu_set(NULL, iface, mtu); + return net_iface_mtu_set(ctx, iface, mtu); } static int @@ -64,7 +65,7 @@ printf("CMD: ip addr add %s/%d dev %s broadcast +\n", addr_str, prefix= len, iface); - return net_addr_v4_add(NULL, iface, &addr, prefixlen); + return net_addr_v4_add(ctx, iface, &addr, prefixlen); } static int @@ -81,7 +82,7 @@ printf("CMD: ip -6 addr add %s/%d dev %s\n", addr_str, prefixlen, ifac= e); - return net_addr_v6_add(NULL, iface, &addr, prefixlen); + return net_addr_v6_add(ctx, iface, &addr, prefixlen); } static int @@ -110,7 +111,7 @@ } printf("\n"); - return net_route_v4_add(NULL, &dst, prefixlen, NULL, iface, 0, metric); + return net_route_v4_add(ctx, &dst, prefixlen, NULL, iface, 0, metric); } static int @@ -146,7 +147,7 @@ } printf("\n"); - return net_route_v4_add(NULL, &dst, prefixlen, &gw, iface, 0, metric); + return net_route_v4_add(ctx, &dst, prefixlen, &gw, iface, 0, metric); } static int @@ -173,7 +174,7 @@ } printf("\n"); - return net_route_v6_add(NULL, &dst, prefixlen, NULL, iface, 0, metric); + return net_route_v6_add(ctx, &dst, prefixlen, NULL, iface, 0, metric); } static int @@ -206,7 +207,7 @@ } printf("\n"); =20 - return net_route_v6_add(NULL, &dst, prefixlen, &gw, iface, 0, metric); + return net_route_v6_add(ctx, &dst, prefixlen, &gw, iface, 0, metric); } static void @@ -232,11 +233,19 @@ return 0; } - if (argc > 3) + if (argc >=3D 3) { iface =3D argv[2]; } + if (argc >=3D 4) + { + ctx =3D malloc(sizeof(*ctx)); + CLEAR(*ctx); + ctx->netns =3D argv[3]; + ctx->gc =3D gc_new(); + } + test =3D atoi(argv[1]); switch (test) { -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1586?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ibaf84cba0124320f7c49e28d1b3364dac9e8b6d9 Gerrit-Change-Number: 1586 Gerrit-PatchSet: 2 Gerrit-Owner: flichtenheld Gerrit-Reviewer: its_Giaan Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: its_Giaan Gerrit-Attention: flichtenheld --nY1LFMwbWag= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: flichtenheld, its_Giaan, plaisthos.

flichtenheld uploaded patch set #2 to this change.

<= p>View C= hange

The following approvals got outdated and were removed: Code-Review+2 by its_Giaan

test_networking: Allow to test =
--netns

While here also do some cleanups:
- Run under set -eu to = better catch errors
- Some comment improvments
- Fix argument handlin= g in test_networking, it
actually ignored the interface argument
Change-Id: Ibaf84cba0124320f7c49e28d1b3364dac9e8b6d9
Signed-off-by: Fr= ank Lichtenheld <frank@...2641...>
---
M tests/t_net.sh
M te= sts/unit_tests/openvpn/test_networking.c
2 files changed, 58 insertions(= +), 34 deletions(-)

git pull ssh://gerrit.openvp=
n.net:29418/openvpn refs/changes/86/1586/2
diff --git a/tests/t_net=
.sh b/tests/t_net.sh
index 8134832..e388971 100755--- a/tests/t_net.sh
+++ b/tests/t_net.sh@@ -1,8 +1,14 @@
#!/usr/bin/env bash
+# Requires bash due to array handl= ing

-= IFACE=3D"ovpn-dummy0"
+set -eu
+=
+IFACE=3D"${IFA= CE:-ovpn-dummy0}"
+# Tests 0 to LAST_AUTO_TEST output a ip command to compare resul= ts
LAST_AUTO_TEST=3D7
+# Tests after LAST_AUTO_TEST are only executed, no compa= rison
LAST_TEST=3D8

src= dir=3D"${srcdir:-.}"
@@ -12,33 +18,19 @@
# bail out right away on non-linux. NetLink (the= object of this test) is only
# used on Linux, therefore t= esting other platform is not needed.
-#
-# Not= e: statements in the rest of the script may not even pass syntax check on
-# solaris/bsd. It uses = /bin/bash
if [ "$(uname -s)" !=3D "Linux&qu= ot; ]; then
echo "$0: this test runs only on Linu= x. SKIPPING TEST."
exit 77
fi

-# Comm= ands used to retrieve the network state.
-# State is retrieved after running sitnl and after runni= ng
-# iproute commands.= The two are then compared and expected to be equal.
-typeset -a GET_STATE
-GET_STATE[0]=3D"ip link show dev $IFAC= E | sed 's/^[0-9]\+: //'"
-GET_STATE[1]=3D"ip addr show dev $IFACE | sed 's/= ^[0-9]\+: //'"
-GET_STATE[2]=3D"ip route show dev $IFACE"
-GET_STATE[3]=3D"ip -6 route show dev $= IFACE"
--LAST_STATE=3D$((${#GET_STATE[@]= } - 1))
-
reload_dummy()

{
- $RUN_SUDO ip link del $IFACE
- $RUN_SUDO ip link add $IFACE address 00:11:2= 2:33:44:55 type dummy
-= $RUN_SUDO ip link set dev $IFACE state up
+ $RUN_SUDO $IP_EXEC ip link del $IFACE || true=
+ $RUN_SUDO $IP_E= XEC ip link add $IFACE address 00:11:22:33:44:55 type dummy
+ $RUN_SUDO $IP_EXEC ip link set = dev $IFACE state up


if [ $? -ne 0 ];= then
- echo &qu= ot;can't create interface $IFACE"
+ echo "can't create interface $IFACE= $IP_NETNS"
exit 1
fi
}
@@ -51,7 +43,7 @@
= # the unit-test prints to stdout the iproute command corresponding<= br> # to the sitnl operation being executed.
= # Format is "CMD: <commandhere>"
- OUT=3D$($RUN_SUDO $UNIT_TEST $k= $IFACE)
+ OUT= =3D$($RUN_SUDO $UNIT_TEST $k $IFACE $NETNS)
# ensu= re unit test worked properly
if [ $? -ne 0 ]; then=
echo "unit-test $k errored out:"
@@ -60,7 +52,7 @@
fi

NEW=3D$(echo "$OUT" | sed -n 's/C= MD: //p')
- = CMD=3D"$CMD $RUN_SUDO $NEW ;"
+ CMD=3D"$CMD $IP_EXEC $NEW ;" done

# collect state fo= r later comparison
@@ -72,13 +64,18 @@

## execution starts here

+set +eu
+
# t_client.rc required only for= RUN_SUDO definition
= +RUN_SUDO=3D
if [ -r "${top_builddir}"/t_client.= rc ]; then
. "${top_builddir}"/t_client.rc
elif [ -r "${srcdir}"/t_client.rc ]; then<= br> . "${srcdir}"/t_client.rc
fi

+set -eu=
+
i= f [ ! -x "$openvpn" ]; then
echo "no (e= xecutable) openvpn binary in current build tree. FAIL." >&2
exit 1
@@ -129,6 +126,24 @@
fi

fi

+IP_EXEC=3D"$RUN_SUDO"
+if [ -n "$NETNS" ]; then=

+ echo "Usin= g netns $NETNS"
= + [ -f "/run/netns/$NETNS" ] || $RUN_SUDO ip netns add "$= NETNS"
+ IP_E= XEC=3D"$RUN_SUDO ip netns exec $NETNS"
+fi
+
+# Comma= nds used to retrieve the network state.
+# State is retrieved after running sitnl and after runn= ing
+# iproute comman= ds. The two are then compared and expected to be equal.
+typeset -a GET_STATE
+GET_STATE[0]=3D"$IP_EXEC ip link = show dev $IFACE | sed 's/^[0-9]\+: //'"
+GET_STATE[1]=3D"$IP_EXEC ip addr sho= w dev $IFACE | sed 's/^[0-9]\+: //'"
+GET_STATE[2]=3D"$IP_EXEC ip route show d= ev $IFACE"
+GET_= STATE[3]=3D"$IP_EXEC ip -6 route show dev $IFACE"
+

+LAST_STATE=3D$((${#GET_STATE[@]} - 1))
+

for i in $(seq 0 = $LAST_AUTO_TEST); do
# reload dummy module to cleanup = state
reload_dummy
@@ -167,7 +182,7 @@=
done

# remove interfac= e for good
-$RUN_SUDO i= p link del $IFACE
+$I= P_EXEC ip link del $IFACE

for i in $(seq= $(($LAST_AUTO_TEST + 1)) ${LAST_TEST}); do
$RUN_SUDO = $UNIT_TEST $i
diff --git a/tests/unit_tests/openvpn/test_ne= tworking.c b/tests/unit_tests/openvpn/test_networking.c
ind= ex 1c9cac1..4e2cce8 100644
--- a/tests/unit_tests/openvpn/t= est_networking.c
+++ b/tests/unit_tests/openvpn/test_networ= king.c
@@ -6,26 +6,27 @@
#include <cmoc= ka.h>

static char *iface =3D "ov= pn-dummy0";
+sta= tic openvpn_net_ctx_t *ctx =3D NULL;

sta= tic int
net__iface_up(bool up)
{ printf("CMD: ip link set %s %s\n", iface, up ? "= ;up" : "down");

- return net_iface_up(NULL, iface, up);=
+ return net_iface_up(ct= x, iface, up);
}

static= int
net__iface_new(const char *name, const char *type)
{
- r= eturn net_iface_new(NULL, name, type, NULL);
+ return net_iface_new(ctx, name, type, NULL);
}

static int
<= span> net__iface_type(const char *name, const char *type)

= {
char ret_type[IFACE_TYPE_LEN_MAX];
- int ret =3D net_iface_type(NULL, na= me, ret_type);
+ i= nt ret =3D net_iface_type(ctx, name, ret_type);
if (re= t =3D=3D 0)
{
assert_string_e= qual(type, ret_type);
@@ -37,7 +38,7 @@
st= atic int
net__iface_del(const char *name)
= {
- return net_ifac= e_del(NULL, name);
+ = return net_iface_del(ctx, name);
}

static int
@@ -45,7 +46,7 @@
{

printf("CMD: ip link set %s mtu %d\n", i= face, mtu);

- return net_iface_mtu_set(NULL, iface, mtu);
+ return net_iface_mtu_set(ctx, iface= , mtu);
}

static int
@@ -64,7 +65,7 @@

prin= tf("CMD: ip addr add %s/%d dev %s broadcast +\n", addr_str, prefi= xlen, iface);

- return net_addr_v4_add(NULL, iface, &addr, prefixlen);
+ return net_addr_v4= _add(ctx, iface, &addr, prefixlen);
}
=
static int
@@ -81,7 +82,7 @@
<= span>

printf("CMD: ip -6 addr add %s/%d dev %s\n= ", addr_str, prefixlen, iface);

- return net_addr_v6_add(NULL, iface, &a= mp;addr, prefixlen);
= + return net_addr_v6_add(ctx, iface, &addr, prefixlen);
}


static int
@@ -11= 0,7 +111,7 @@
}
printf("\n&q= uot;);

+ return n= et_route_v4_add(ctx, &dst, prefixlen, NULL, iface, 0, metric); }

static int
@@= -146,7 +147,7 @@
}
printf("= \n");

- return net_route_v4_add(NULL, &dst, prefixlen, &gw, ifac= e, 0, metric);
+ r= eturn net_route_v4_add(ctx, &dst, prefixlen, &gw, iface, 0, metric)= ;
}

static int@@ -173,7 +174,7 @@
}
pri= ntf("\n");

- return net_route_v6_add(NULL, &dst, prefixlen, NUL= L, iface, 0, metric);
+ return net_route_v6_add(ctx, &dst, prefixlen, NULL, iface, 0, met= ric);
}

static int

@@ -206,7 +207,7 @@
}
= printf("\n");

- return net_route_v6_add(NULL, &dst, prefixlen,= &gw, iface, 0, metric);
+ return net_route_v6_add(ctx, &dst, prefixlen, &gw, ifa= ce, 0, metric);
}

stati= c void
@@ -232,11 +233,19 @@
retur= n 0;
}

- if (argc > 3)
+ if (argc >=3D 3)
{
iface =3D argv[2];
}
<= span>

+ if (argc = >=3D 4)
+ {
+ ctx =3D malloc(= sizeof(*ctx));
+ = CLEAR(*ctx);
+ = ctx->netns =3D argv[3];
+ ctx->gc =3D gc_new();
+ }
+
test =3D atoi(argv[1]);
= switch (test)
{

= To view, visit change 1586. To unsubscribe, or for help writing mail filters, v= isit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ibaf84cba0124320f7c49e28d1b3= 364dac9e8b6d9
Gerrit-Change-Number: 1586
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: its_Giaan <gianmarco@...1664...= 26...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: its_Giaan <gianmarco@...1665...= 726...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
= --nY1LFMwbWag=--