From df673cb19a1b36fcb0f151522e8d02a3e8b8cf19 Mon Sep 17 00:00:00 2001 From: cjmakes Date: Sat, 1 Feb 2025 11:23:18 -0800 Subject: [PATCH] openwrt: enable/disables radios via dhcp options --- openwrt/files/etc/udhcpc.user | 25 ++++++-- .../unit/openwrt/golden/ath79/etc/udhcpc.user | 25 ++++++-- tests/unit/openwrt/test_udhcpc.sh | 61 +++++++++++++++++++ tests/unit/openwrt/uci | 2 + tests/unit/openwrt/wifi | 0 5 files changed, 103 insertions(+), 10 deletions(-) create mode 100644 tests/unit/openwrt/test_udhcpc.sh create mode 100755 tests/unit/openwrt/uci create mode 100755 tests/unit/openwrt/wifi diff --git a/openwrt/files/etc/udhcpc.user b/openwrt/files/etc/udhcpc.user index ce3b7424..da809dc2 100644 --- a/openwrt/files/etc/udhcpc.user +++ b/openwrt/files/etc/udhcpc.user @@ -1,4 +1,8 @@ -#!/bin/sh +#!/bin/bash + +# set case insensitive match. This only works if a comparison is made with +# double brackets +shopt -s nocasematch case "$1" in # Same actions for renew or bound for the time being @@ -7,10 +11,21 @@ case "$1" in set > /tmp/dhcp.params radio0=`uci show 'wireless.radio0.channel'|cut -f 2 -d "'"` radio1=`uci show 'wireless.radio1.channel'|cut -f 2 -d "'"` - if [ ! -z "$opt224" ] || [ ! -z "$opt225" ]; then - if [ "$opt224" != "$radio0" ] || [ "$opt225" != "$radio1" ]; then - uci set 'wireless.radio0.channel'=$(printf %d "0x$opt224") - uci set 'wireless.radio1.channel'=$(printf %d "0x$opt225") + + if [[ ! -z "$opt224" ]] && [[ ! -z "$opt225" ]]; then + if [[ "$opt224" != "$radio0" ]] || [[ "$opt225" != "$radio1" ]]; then + if [[ "$opt224" != "off" ]]; then + uci set 'wireless.radio0.channel'=$(printf %d "0x$opt224") + uci set 'wireless.radio0.disabled'=0 + else + uci set 'wireless.radio0.disabled'=1 + fi + if [[ "$opt225" != "off" ]]; then + uci set 'wireless.radio1.channel'=$(printf %d "0x$opt225") + uci set 'wireless.radio1.disabled'=0 + else + uci set 'wireless.radio1.disabled'=1 + fi uci commit wifi reload fi diff --git a/tests/unit/openwrt/golden/ath79/etc/udhcpc.user b/tests/unit/openwrt/golden/ath79/etc/udhcpc.user index ce3b7424..da809dc2 100644 --- a/tests/unit/openwrt/golden/ath79/etc/udhcpc.user +++ b/tests/unit/openwrt/golden/ath79/etc/udhcpc.user @@ -1,4 +1,8 @@ -#!/bin/sh +#!/bin/bash + +# set case insensitive match. This only works if a comparison is made with +# double brackets +shopt -s nocasematch case "$1" in # Same actions for renew or bound for the time being @@ -7,10 +11,21 @@ case "$1" in set > /tmp/dhcp.params radio0=`uci show 'wireless.radio0.channel'|cut -f 2 -d "'"` radio1=`uci show 'wireless.radio1.channel'|cut -f 2 -d "'"` - if [ ! -z "$opt224" ] || [ ! -z "$opt225" ]; then - if [ "$opt224" != "$radio0" ] || [ "$opt225" != "$radio1" ]; then - uci set 'wireless.radio0.channel'=$(printf %d "0x$opt224") - uci set 'wireless.radio1.channel'=$(printf %d "0x$opt225") + + if [[ ! -z "$opt224" ]] && [[ ! -z "$opt225" ]]; then + if [[ "$opt224" != "$radio0" ]] || [[ "$opt225" != "$radio1" ]]; then + if [[ "$opt224" != "off" ]]; then + uci set 'wireless.radio0.channel'=$(printf %d "0x$opt224") + uci set 'wireless.radio0.disabled'=0 + else + uci set 'wireless.radio0.disabled'=1 + fi + if [[ "$opt225" != "off" ]]; then + uci set 'wireless.radio1.channel'=$(printf %d "0x$opt225") + uci set 'wireless.radio1.disabled'=0 + else + uci set 'wireless.radio1.disabled'=1 + fi uci commit wifi reload fi diff --git a/tests/unit/openwrt/test_udhcpc.sh b/tests/unit/openwrt/test_udhcpc.sh new file mode 100644 index 00000000..e272e5c0 --- /dev/null +++ b/tests/unit/openwrt/test_udhcpc.sh @@ -0,0 +1,61 @@ +#!/bin/bash +# +# Tests some scenarios of the udhcpc.user script. There is room for improvement +# here to mock out uci to return radio status as part of the test. There is +# also room to cover more of the udhcpcp.user script like setting the hostname +# and chaning the config version. +# +TOPDIR=$(git rev-parse --show-toplevel) +export PATH="$TOPDIR/tests/unit/openwrt:$PATH" + +test_dhcp(){ + testname=$1 + echo "Running $testname" + export UCILOG="$testname"_uci.log + export opt224=$2 # radio0 channel | off + export opt225=$3 # radio1 channel | off + + echo "" > $UCILOG + bash $TOPDIR/openwrt/files/etc/udhcpc.user renew + + if ! diff "$testname"_uci.log "$testname"_expected.log; then + echo "FAILED: $testname" + exit 1 + fi +} + + +cat > both_off_expected.log << EOF + +show wireless.radio0.channel +show wireless.radio1.channel +set wireless.radio0.disabled=1 +set wireless.radio1.disabled=1 +commit +EOF +test_dhcp "both_off" off OFF + +cat > enable_0_expected.log << EOF + +show wireless.radio0.channel +show wireless.radio1.channel +set wireless.radio0.channel=3 +set wireless.radio0.disabled=0 +set wireless.radio1.disabled=1 +commit +EOF +test_dhcp "enable_0" 3 OFF + +cat > enable_1_expected.log << EOF + +show wireless.radio0.channel +show wireless.radio1.channel +set wireless.radio0.channel=3 +set wireless.radio0.disabled=0 +set wireless.radio1.channel=4 +set wireless.radio1.disabled=0 +commit +EOF +test_dhcp "enable_1" 3 4 + +echo "PASS" diff --git a/tests/unit/openwrt/uci b/tests/unit/openwrt/uci new file mode 100755 index 00000000..243fe034 --- /dev/null +++ b/tests/unit/openwrt/uci @@ -0,0 +1,2 @@ +#!/bin/sh +echo $@ >> $UCILOG diff --git a/tests/unit/openwrt/wifi b/tests/unit/openwrt/wifi new file mode 100755 index 00000000..e69de29b