Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Tests for DevStack functions |
| 4 | # address_in_net() |
| 5 | |
| 6 | TOP=$(cd $(dirname "$0")/.. && pwd) |
| 7 | |
| 8 | # Import common functions |
| 9 | source $TOP/functions |
| 10 | |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 11 | source $TOP/tests/unittest.sh |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 12 | |
| 13 | echo "Testing IP addr functions" |
| 14 | |
Mahito | d82d3f1 | 2015-05-22 10:37:23 -0700 | [diff] [blame^] | 15 | function test_cidr2netmask { |
| 16 | local mask=0 |
| 17 | local ips="128 192 224 240 248 252 254 255" |
| 18 | local ip |
| 19 | local msg |
| 20 | |
| 21 | msg="cidr2netmask(/0) == 0.0.0.0" |
| 22 | assert_equal "0.0.0.0" $(cidr2netmask $mask) "$msg" |
| 23 | |
| 24 | for ip in $ips; do |
| 25 | mask=$(( mask + 1 )) |
| 26 | msg="cidr2netmask(/$mask) == $ip.0.0.0" |
| 27 | assert_equal "$ip.0.0.0" $(cidr2netmask $mask) "$msg" |
| 28 | done |
| 29 | |
| 30 | for ip in $ips; do |
| 31 | mask=$(( mask + 1 )) |
| 32 | msg="cidr2netmask(/$mask) == 255.$ip.0.0" |
| 33 | assert_equal "255.$ip.0.0" $(cidr2netmask $mask) "$msg" |
| 34 | done |
| 35 | |
| 36 | for ip in $ips; do |
| 37 | mask=$(( mask + 1 )) |
| 38 | msg="cidr2netmask(/$mask) == 255.255.$ip.0" |
| 39 | assert_equal "255.255.$ip.0" $(cidr2netmask $mask) "$msg" |
| 40 | done |
| 41 | |
| 42 | for ip in $ips; do |
| 43 | mask=$(( mask + 1 )) |
| 44 | msg="cidr2netmask(/$mask) == 255.255.255.$ip" |
| 45 | assert_equal "255.255.255.$ip" $(cidr2netmask $mask) "$msg" |
| 46 | done |
| 47 | } |
| 48 | |
| 49 | test_cidr2netmask |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 50 | |
| 51 | if [[ $(maskip 169.254.169.254 240.0.0.0) == 160.0.0.0 ]]; then |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 52 | passed "maskip(): /4...OK" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 53 | else |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 54 | failed "maskip(): /4...failed" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 55 | fi |
| 56 | if [[ $(maskip 169.254.169.254 255.0.0.0) == 169.0.0.0 ]]; then |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 57 | passed "maskip(): /8...OK" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 58 | else |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 59 | failed "maskip(): /8...failed" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 60 | fi |
| 61 | if [[ $(maskip 169.254.169.254 255.240.0.0) == 169.240.0.0 ]]; then |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 62 | passed "maskip(): /12...OK" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 63 | else |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 64 | failed "maskip(): /12...failed" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 65 | fi |
| 66 | if [[ $(maskip 169.254.169.254 255.255.0.0) == 169.254.0.0 ]]; then |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 67 | passed "maskip(): /16...OK" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 68 | else |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 69 | failed "maskip(): /16...failed" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 70 | fi |
| 71 | if [[ $(maskip 169.254.169.254 255.255.240.0) == 169.254.160.0 ]]; then |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 72 | passed "maskip(): /20...OK" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 73 | else |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 74 | failed "maskip(): /20...failed" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 75 | fi |
| 76 | if [[ $(maskip 169.254.169.254 255.255.255.0) == 169.254.169.0 ]]; then |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 77 | passed "maskip(): /24...OK" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 78 | else |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 79 | failed "maskip(): /24...failed" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 80 | fi |
| 81 | if [[ $(maskip 169.254.169.254 255.255.255.240) == 169.254.169.240 ]]; then |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 82 | passed "maskip(): /28...OK" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 83 | else |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 84 | failed "maskip(): /28...failed" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 85 | fi |
| 86 | if [[ $(maskip 169.254.169.254 255.255.255.255) == 169.254.169.254 ]]; then |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 87 | passed "maskip(): /32...OK" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 88 | else |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 89 | failed "maskip(): /32...failed" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 90 | fi |
| 91 | |
| 92 | for mask in 8 12 16 20 24 26 28; do |
| 93 | echo -n "address_in_net(): in /$mask..." |
| 94 | if address_in_net 10.10.10.1 10.10.10.0/$mask; then |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 95 | passed "OK" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 96 | else |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 97 | failed "address_in_net() failed on /$mask" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 98 | fi |
| 99 | |
| 100 | echo -n "address_in_net(): not in /$mask..." |
| 101 | if ! address_in_net 10.10.10.1 11.11.11.0/$mask; then |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 102 | passed "OK" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 103 | else |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 104 | failed "address_in_net() failed on /$mask" |
Dean Troyer | d4f69b2 | 2013-07-24 12:24:43 -0500 | [diff] [blame] | 105 | fi |
| 106 | done |
Ian Wienand | f56348b | 2015-04-17 12:58:56 +1000 | [diff] [blame] | 107 | |
| 108 | report_results |