blob: f8c2058967e111f5d6ab43cf86a8bcccdf158e00 [file] [log] [blame]
Dean Troyerd4f69b22013-07-24 12:24:43 -05001#!/usr/bin/env bash
2
3# Tests for DevStack functions
4# address_in_net()
5
6TOP=$(cd $(dirname "$0")/.. && pwd)
7
8# Import common functions
9source $TOP/functions
10
Ian Wienandf56348b2015-04-17 12:58:56 +100011source $TOP/tests/unittest.sh
Dean Troyerd4f69b22013-07-24 12:24:43 -050012
13echo "Testing IP addr functions"
14
Mahitod82d3f12015-05-22 10:37:23 -070015function 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
49test_cidr2netmask
Dean Troyerd4f69b22013-07-24 12:24:43 -050050
51if [[ $(maskip 169.254.169.254 240.0.0.0) == 160.0.0.0 ]]; then
Ian Wienandf56348b2015-04-17 12:58:56 +100052 passed "maskip(): /4...OK"
Dean Troyerd4f69b22013-07-24 12:24:43 -050053else
Ian Wienandf56348b2015-04-17 12:58:56 +100054 failed "maskip(): /4...failed"
Dean Troyerd4f69b22013-07-24 12:24:43 -050055fi
56if [[ $(maskip 169.254.169.254 255.0.0.0) == 169.0.0.0 ]]; then
Ian Wienandf56348b2015-04-17 12:58:56 +100057 passed "maskip(): /8...OK"
Dean Troyerd4f69b22013-07-24 12:24:43 -050058else
Ian Wienandf56348b2015-04-17 12:58:56 +100059 failed "maskip(): /8...failed"
Dean Troyerd4f69b22013-07-24 12:24:43 -050060fi
61if [[ $(maskip 169.254.169.254 255.240.0.0) == 169.240.0.0 ]]; then
Ian Wienandf56348b2015-04-17 12:58:56 +100062 passed "maskip(): /12...OK"
Dean Troyerd4f69b22013-07-24 12:24:43 -050063else
Ian Wienandf56348b2015-04-17 12:58:56 +100064 failed "maskip(): /12...failed"
Dean Troyerd4f69b22013-07-24 12:24:43 -050065fi
66if [[ $(maskip 169.254.169.254 255.255.0.0) == 169.254.0.0 ]]; then
Ian Wienandf56348b2015-04-17 12:58:56 +100067 passed "maskip(): /16...OK"
Dean Troyerd4f69b22013-07-24 12:24:43 -050068else
Ian Wienandf56348b2015-04-17 12:58:56 +100069 failed "maskip(): /16...failed"
Dean Troyerd4f69b22013-07-24 12:24:43 -050070fi
71if [[ $(maskip 169.254.169.254 255.255.240.0) == 169.254.160.0 ]]; then
Ian Wienandf56348b2015-04-17 12:58:56 +100072 passed "maskip(): /20...OK"
Dean Troyerd4f69b22013-07-24 12:24:43 -050073else
Ian Wienandf56348b2015-04-17 12:58:56 +100074 failed "maskip(): /20...failed"
Dean Troyerd4f69b22013-07-24 12:24:43 -050075fi
76if [[ $(maskip 169.254.169.254 255.255.255.0) == 169.254.169.0 ]]; then
Ian Wienandf56348b2015-04-17 12:58:56 +100077 passed "maskip(): /24...OK"
Dean Troyerd4f69b22013-07-24 12:24:43 -050078else
Ian Wienandf56348b2015-04-17 12:58:56 +100079 failed "maskip(): /24...failed"
Dean Troyerd4f69b22013-07-24 12:24:43 -050080fi
81if [[ $(maskip 169.254.169.254 255.255.255.240) == 169.254.169.240 ]]; then
Ian Wienandf56348b2015-04-17 12:58:56 +100082 passed "maskip(): /28...OK"
Dean Troyerd4f69b22013-07-24 12:24:43 -050083else
Ian Wienandf56348b2015-04-17 12:58:56 +100084 failed "maskip(): /28...failed"
Dean Troyerd4f69b22013-07-24 12:24:43 -050085fi
86if [[ $(maskip 169.254.169.254 255.255.255.255) == 169.254.169.254 ]]; then
Ian Wienandf56348b2015-04-17 12:58:56 +100087 passed "maskip(): /32...OK"
Dean Troyerd4f69b22013-07-24 12:24:43 -050088else
Ian Wienandf56348b2015-04-17 12:58:56 +100089 failed "maskip(): /32...failed"
Dean Troyerd4f69b22013-07-24 12:24:43 -050090fi
91
92for 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 Wienandf56348b2015-04-17 12:58:56 +100095 passed "OK"
Dean Troyerd4f69b22013-07-24 12:24:43 -050096 else
Ian Wienandf56348b2015-04-17 12:58:56 +100097 failed "address_in_net() failed on /$mask"
Dean Troyerd4f69b22013-07-24 12:24:43 -050098 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 Wienandf56348b2015-04-17 12:58:56 +1000102 passed "OK"
Dean Troyerd4f69b22013-07-24 12:24:43 -0500103 else
Ian Wienandf56348b2015-04-17 12:58:56 +1000104 failed "address_in_net() failed on /$mask"
Dean Troyerd4f69b22013-07-24 12:24:43 -0500105 fi
106done
Ian Wienandf56348b2015-04-17 12:58:56 +1000107
108report_results