From: "flichtenheld (Code Review)" Date: Wed, 25 Mar 2026 12:13:44 +0000 References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: <6dd088ede90000872e0b47adc87f57d4f0577d83-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="U1S0rUfRRvs="; 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 Cc: openvpn-devel --U1S0rUfRRvs= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/1586?usp=3Demail to review the following change. 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, 57 insertions(+), 34 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/86/1586/1 diff --git a/tests/t_net.sh b/tests/t_net.sh index 8134832..d2c87ec 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,6 +64,8 @@ ## execution starts here +set +eu + # t_client.rc required only for RUN_SUDO definition if [ -r "${top_builddir}"/t_client.rc ]; then . "${top_builddir}"/t_client.rc @@ -79,6 +73,8 @@ . "${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 +125,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 +181,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"); - 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: newchange Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ibaf84cba0124320f7c49e28d1b3364dac9e8b6d9 Gerrit-Change-Number: 1586 Gerrit-PatchSet: 1 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos --U1S0rUfRRvs= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: plaisthos.

flichtenheld would like plaisthos to review this change.

V= iew Change

test_networking: Allow to test --netns
<= br>While here also do some cleanups:
- Run under set -eu to better catch= errors
- Some comment improvments
- Fix argument handling in test_ne= tworking, it
actually ignored the interface argument

Change-Id:= Ibaf84cba0124320f7c49e28d1b3364dac9e8b6d9
Signed-off-by: Frank Lichtenh= eld <frank@...2641...>
---
M tests/t_net.sh
M tests/unit_tes= ts/openvpn/test_networking.c
2 files changed, 57 insertions(+), 34 delet= ions(-)

git pull ssh://gerrit.openvpn.net:29418/=
openvpn refs/changes/86/1586/1
diff --git a/tests/t_net.sh b/tests/=
t_net.sh
index 8134832..d2c87ec 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&quo= t;ovpn-dummy0"
+= set -eu
+
<= span style=3D"color: hsl(120, 100%, 40%);">+IFACE=3D"${IFACE:-ovpn-dum= my0}"
+NETNS=3D$= {NETNS:-}
UNIT_TEST=3D"./unit_tests/openvpn/networkin= g_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 t= his test) is only
# used on Linux, therefore testing other= platform is not needed.
-# Note: statement= s in the rest of the script may not even pass syntax check on
-# solaris/bsd. It uses /bin/bash
if [ "$(uname -s)" !=3D "Linux" ]; then<= /span>
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[2]= =3D"ip route show dev $IFACE"
-GET_STATE[3]=3D"ip -6 route show dev $IFACE"
-
-LAST_STATE=3D$((${#GET_STATE[@]} - 1))
-
reload_d= ummy()
{
- $RUN_SUDO ip link del $IFACE
- $RUN_SUDO ip link add $IFACE address 00:11:22:33:44:55 t= ype dummy
- $RUN_SUD= O ip link set dev $IFACE state up
+ $RUN_SUDO $IP_EXEC ip link del $IFACE || true
<= span style=3D"color: hsl(120, 100%, 40%);">+ $RUN_SUDO $IP_EXEC ip link = add $IFACE address 00:11:22:33:44:55 type dummy

+ $RUN_SUDO $IP_EXEC ip link set dev $IFACE s= tate up

if [ $? -ne 0 ]; then=
- echo "can't= create interface $IFACE"
+ echo "can't create interface $IFACE $IP_NETNS&q= uot;
exit 1
fi
}

@@ -51,7 +43,7 @@
# the unit-= test prints to stdout the iproute command corresponding
= # to the sitnl operation being executed.
# F= ormat is "CMD: <commandhere>"
- OUT=3D$($RUN_SUDO $UNIT_TEST $k $IFACE)

+ OUT=3D$($RUN_SU= DO $UNIT_TEST $k $IFACE $NETNS)
# ensure unit test= worked properly
if [ $? -ne 0 ]; then
<= span> 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 comp= arison
@@ -72,6 +64,8 @@

= ## execution starts here

+set +eu
+
# t_client.rc required only for RUN_SUDO defi= nition
if [ -r "${top_builddir}"/t_client.rc ]; = then
. "${top_builddir}"/t_client.rc<= br>@@ -79,6 +73,8 @@
. "${srcdir}"/t_c= lient.rc
fi

+set -eu
+
if [ ! -x "$openvpn" ]; then
echo "no (executable) openvpn binary in current buil= d tree. FAIL." >&2
exit 1
= @@ -129,6 +125,24 @@
fi
fi
=
+IP_EXEC=3D&q= uot;$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 $NETN= S"
+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 expecte= d to be equal.
+types= et -a GET_STATE
+GET_= STATE[0]=3D"$IP_EXEC ip link show dev $IFACE | sed 's/^[0-9]\+: //= '"
+GET_STAT= E[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$((${#GE= T_STATE[@]} - 1))
+
for i in $(seq 0 $LAST_AUTO_TEST); do
= # reload dummy module to cleanup state
reload_dummy
@@ -167,7 +181,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}); d= o
$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/tes= ts/unit_tests/openvpn/test_networking.c
@@ -6,26 +6,27 @@
#include <cmocka.h>

static char *iface =3D "ovpn-dummy0";

+static openvpn_net_ctx_t *ctx =3D NULL;

static int
net__iface_up(boo= l up)
{
printf("CMD: ip link set= %s %s\n", iface, up ? "up" : "down");
<= span>

- 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_if= ace_new(ctx, name, type, NULL);
}
=
static int
net__iface_type(const char *name, co= nst char *type)
{
char ret_type[IFACE= _TYPE_LEN_MAX];
- in= t ret =3D net_iface_type(NULL, name, ret_type);
+ int ret =3D net_iface_type(ctx, name, ret_t= ype);
if (ret =3D=3D 0)
{<= br> assert_string_equal(type, ret_type);
@@ -= 37,7 +38,7 @@
static int
net__iface_del(c= onst char *name)
{
- return net_iface_del(NULL, name);
+ return net_iface_del(ctx, name);

}

static int
@@ -45,7 +46,7 @@
{
printf("CM= D: ip link set %s mtu %d\n", iface, mtu);

= - return net_iface_mtu_set(NUL= L, 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 b= roadcast +\n", addr_str, prefixlen, iface);
- return net_addr_v4_add(NUL= L, iface, &addr, prefixlen);
+ return net_addr_v4_add(ctx, iface, &addr, prefixlen);<= /span>
}

static int
= @@ -81,7 +82,7 @@

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


- ret= urn 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, &am= p;dst, prefixlen, NULL, iface, 0, metric);

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

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

<= span style=3D"color: hsl(0, 100%, 40%);">- return net_route_v4_add(NULL,= &dst, prefixlen, &gw, iface, 0, metric);
+ return net_route_v4_add(ctx, &dst, pr= efixlen, &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");

- return net_rout= e_v6_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
<= span style=3D"color: hsl(120, 100%, 40%);">+ return net_route_v6_add(ctx= , &dst, prefixlen, &gw, iface, 0, metric);
}


static void
@@ -232,11 +233,1= 9 @@
return 0;
}

- if (argc >= ; 3)
+ if (argc &g= t;=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 change 1586. To unsubscribe, = or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ibaf84cba0124320f7c49e28d1b3= 364dac9e8b6d9
Gerrit-Change-Number: 1586
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
= --U1S0rUfRRvs=--