diff --git a/tests/test_ip.sh b/tests/test_ip.sh index c53e80de36..f8c2058967 100755 --- a/tests/test_ip.sh +++ b/tests/test_ip.sh @@ -12,51 +12,41 @@ source $TOP/tests/unittest.sh echo "Testing IP addr functions" -if [[ $(cidr2netmask 4) == 240.0.0.0 ]]; then - passed "cidr2netmask(): /4...OK" -else - failed "cidr2netmask(): /4...failed" -fi -if [[ $(cidr2netmask 8) == 255.0.0.0 ]]; then - passed "cidr2netmask(): /8...OK" -else - failed "cidr2netmask(): /8...failed" -fi -if [[ $(cidr2netmask 12) == 255.240.0.0 ]]; then - passed "cidr2netmask(): /12...OK" -else - failed "cidr2netmask(): /12...failed" -fi -if [[ $(cidr2netmask 16) == 255.255.0.0 ]]; then - passed "cidr2netmask(): /16...OK" -else - failed "cidr2netmask(): /16...failed" -fi -if [[ $(cidr2netmask 20) == 255.255.240.0 ]]; then - passed "cidr2netmask(): /20...OK" -else - failed "cidr2netmask(): /20...failed" -fi -if [[ $(cidr2netmask 24) == 255.255.255.0 ]]; then - passed "cidr2netmask(): /24...OK" -else - failed "cidr2netmask(): /24...failed" -fi -if [[ $(cidr2netmask 28) == 255.255.255.240 ]]; then - passed "cidr2netmask(): /28...OK" -else - failed "cidr2netmask(): /28...failed" -fi -if [[ $(cidr2netmask 30) == 255.255.255.252 ]]; then - passed "cidr2netmask(): /30...OK" -else - failed "cidr2netmask(): /30...failed" -fi -if [[ $(cidr2netmask 32) == 255.255.255.255 ]]; then - passed "cidr2netmask(): /32...OK" -else - failed "cidr2netmask(): /32...failed" -fi +function test_cidr2netmask { + local mask=0 + local ips="128 192 224 240 248 252 254 255" + local ip + local msg + + msg="cidr2netmask(/0) == 0.0.0.0" + assert_equal "0.0.0.0" $(cidr2netmask $mask) "$msg" + + for ip in $ips; do + mask=$(( mask + 1 )) + msg="cidr2netmask(/$mask) == $ip.0.0.0" + assert_equal "$ip.0.0.0" $(cidr2netmask $mask) "$msg" + done + + for ip in $ips; do + mask=$(( mask + 1 )) + msg="cidr2netmask(/$mask) == 255.$ip.0.0" + assert_equal "255.$ip.0.0" $(cidr2netmask $mask) "$msg" + done + + for ip in $ips; do + mask=$(( mask + 1 )) + msg="cidr2netmask(/$mask) == 255.255.$ip.0" + assert_equal "255.255.$ip.0" $(cidr2netmask $mask) "$msg" + done + + for ip in $ips; do + mask=$(( mask + 1 )) + msg="cidr2netmask(/$mask) == 255.255.255.$ip" + assert_equal "255.255.255.$ip" $(cidr2netmask $mask) "$msg" + done +} + +test_cidr2netmask if [[ $(maskip 169.254.169.254 240.0.0.0) == 160.0.0.0 ]]; then passed "maskip(): /4...OK"