Skip to content

Commit

Permalink
Merge pull request #433 from tiiuae/cbma-ba-functional-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TIISR authored Apr 4, 2024
2 parents c9839b9 + 2021db9 commit b90f274
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 10 deletions.
58 changes: 58 additions & 0 deletions common/tests/functional/test_cbma/cbma_check_connected.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/sh

TIMEOUT_SECONDS=10


if [ $# -eq 0 ]; then
echo "Usage: $0 <bat-iface> [<peer-mac>]"
exit 1
fi

check_dependencies() {
for t in batctl; do
if ! type $t >/dev/null 2>&1; then
echo "[!] FATAL: '$t' is missing!" >&2
exit 2
fi
done
}

check_interface() {
iface="$1"

if [ ! -e "/sys/class/net/${iface}/address" ]; then
echo "'$iface' does not exist" >&2
exit 3
fi
}

check_mac_address() {
mac="$1"

if [ -n "$mac" ] && ! echo "$mac" | grep -Eiqx '([0-9a-f]{2}:){5}[0-9a-f]{2}'; then
echo "'$mac' is not a valid MAC address" >&2
exit 4
fi
}

cbma_check_connected() {
iface="$1"
peer_mac="${2:-.}"

elapsed_seconds=0
while [ $((elapsed_seconds++)) -lt $TIMEOUT_SECONDS ]; do
peers="$(batctl meshif $iface n -H -n)"
if expr "$peers" : "$peer_mac" 2>/dev/null >&2; then
echo 'Pass'
return
fi
sleep 1
done
echo 'Fail'
exit 5
}

check_dependencies
check_interface $1
check_mac_address $2
cbma_check_connected $@
51 changes: 41 additions & 10 deletions common/tests/functional/test_cbma/cbma_check_running.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
#!/bin/sh

mdm_constants='/opt/mesh_com/modules/sc-mesh-secure-deployment/src/nats/src/constants.py'
lower_or_upper="$(awk "BEGIN{print toupper(\"$1\")}")"
MULTICAST_PORT=15002
MULTICAST_PREFIX='ff33'
TIMEOUT_SECONDS=10

if [ $# -ne 1 ] || case "$lower_or_upper" in LOWER|UPPER) false;; esac; then
echo "Usage: $0 <lower|upper>"

if [ $# -eq 0 ]; then
echo "Usage: $0 <iface> [<port>]"
exit 1
fi

port="$(awk "/^[[:space:]]*CBMA_PORT_${lower_or_upper}/{print \$NF}" $mdm_constants)"
check_dependencies() {
for t in tcpdump timeout; do
if ! type $t >/dev/null 2>&1; then
echo "[!] FATAL: '$t' is missing!" >&2
exit 2
fi
done
}

if ! ss -lp | grep -q "\\b$port\\b.*python"; then
echo "Fail"
exit 1
fi
echo "Pass"
check_interface() {
iface="$1"

if [ ! -e "/sys/class/net/${iface}/address" ]; then
echo "'$iface' does not exist" >&2
exit 3
fi
case "$iface" in bat*) MULTICAST_PORT=$((MULTICAST_PORT + 1));; esac
}

cbma_check_running() {
iface="$1"
port="${2:-$MULTICAST_PORT}"

packet="$(timeout $TIMEOUT_SECONDS tcpdump -c1 -i "$iface" -qQ out udp and port "$port" 2>/dev/null)"
mcast_group="$(expr "$packet" : '.*[[:space:]]\([^.]\+\).[0-9]\+:')"

if [ "${mcast_group%%:*}" != 'ff33' ]; then
echo 'Fail'
exit 4
fi
echo 'Pass'
}

check_dependencies
check_interface $1
cbma_check_running $@
68 changes: 68 additions & 0 deletions common/tests/functional/test_cbma/cbma_check_secured.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/sh

BATMAN_ETHER_PROTO='0x4305'
MACSEC_ETHER_PROTO='0x88e5'
MACSEC_PACKET_BYTES_OFFSET=36
TIMEOUT_SECONDS=10


if [ $# -eq 0 ]; then
echo "Usage: $0 <non-bat-iface> [<peer-mac>]"
exit 1
fi

check_dependencies() {
for t in tcpdump timeout; do
if ! type $t >/dev/null 2>&1; then
echo "[!] FATAL: '$t' is missing!" >&2
exit 2
fi
done
}

check_interface() {
iface="$1"

if [ ! -e "/sys/class/net/${iface}/address" ]; then
echo "'$iface' does not exist" >&2
exit 3
fi
case "$iface" in bat*) MULTICAST_PORT=$((MULTICAST_PORT + 1));; esac
}

check_mac_address() {
mac="$1"

if [ -n "$mac" ] && ! echo "$mac" | grep -Eiqx '([0-9a-f]{2}:){5}[0-9a-f]{2}'; then
echo "'$mac' is not a valid MAC address" >&2
exit 4
fi
}

cbma_check_secured() {
iface="$1"
peer_mac="$2"

if [ -n "$peer_mac" ]; then
capture_iface="lms$(echo $peer_mac | tr -d ':')"
else
capture_iface="lmb$(tr -d ':' < /sys/class/net/"$iface"/address)"
fi
if ! ( check_interface "$capture_iface" 2>/dev/null ); then
echo "'$peer_mac' peer is not connected"
exit 5
fi

packet="$(timeout $TIMEOUT_SECONDS tcpdump -c1 -i "$capture_iface" -qQ out ether proto $BATMAN_ETHER_PROTO and ether[$MACSEC_PACKET_BYTES_OFFSET:2] == $MACSEC_ETHER_PROTO 2>/dev/null)"

if [ -z "$packet" ]; then
echo 'Fail'
exit 6
fi
echo 'Pass'
}

check_dependencies
check_interface $1
check_mac_address $2
cbma_check_secured $@
17 changes: 17 additions & 0 deletions common/tests/functional/test_cbma/old-cbma/cbma_check_running.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

mdm_constants='/opt/mesh_com/modules/sc-mesh-secure-deployment/src/nats/src/constants.py'
lower_or_upper="$(awk "BEGIN{print toupper(\"$1\")}")"

if [ $# -ne 1 ] || case "$lower_or_upper" in LOWER|UPPER) false;; esac; then
echo "Usage: $0 <lower|upper>"
exit 1
fi

port="$(awk "/^[[:space:]]*CBMA_PORT_${lower_or_upper}/{print \$NF}" $mdm_constants)"

if ! ss -lp | grep -q "\\b$port\\b.*python"; then
echo "Fail"
exit 1
fi
echo "Pass"

0 comments on commit b90f274

Please sign in to comment.