Add test case to 'cidr2netmask'

'cidr2netmask' of function doesn't have enough test case
and test code isn't smart.
This patch adds test case and refactors test code.

Change-Id: Iab20ef06fe78316a78198ab75c0afe738a577dd6
Closes-bug: #1457989
This commit is contained in:
Mahito 2015-05-22 10:37:23 -07:00 committed by Mahito OGURA
parent 165afa2377
commit d82d3f1340

View File

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