William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright 2021 Google LLC |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | cd "$(dirname "$0")" |
| 17 | if [ -e ../network-sh.bb ]; then |
| 18 | source '../../test/test-sh/lib.sh' |
| 19 | else |
| 20 | source "$SYSROOT/usr/share/test/lib.sh" |
| 21 | fi |
| 22 | source lib.sh |
| 23 | |
William A. Kennington III | 70264b9 | 2021-05-07 03:07:31 -0700 | [diff] [blame] | 24 | expect_array_numeq() { |
| 25 | local -n a1="$1" |
| 26 | local -n a2="$2" |
| 27 | |
| 28 | if (( "${#a1[@]}" != "${#a2[@]}" )); then |
| 29 | echo " Line ${BASH_LINENO[0]} Array Size ${#a1[@]} != ${#a2[@]}" >&2 |
| 30 | test_err=1 |
| 31 | else |
| 32 | local i |
| 33 | for (( i=0; i < ${#a1[@]}; ++i )); do |
| 34 | expect_numeq "${a1[$i]}" "${a2[$i]}" |
| 35 | done |
| 36 | fi |
| 37 | } |
| 38 | |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 39 | test_mac_to_bytes() { |
| 40 | out=() |
| 41 | expect_err 1 mac_to_bytes out '' |
| 42 | expect_err 1 mac_to_bytes out '00' |
| 43 | expect_err 1 mac_to_bytes out '12:34:56:78:90:' |
| 44 | expect_err 1 mac_to_bytes out ':12:34:56:78:90' |
| 45 | expect_err 1 mac_to_bytes out '12:34:56:78:90:0:' |
| 46 | expect_err 1 mac_to_bytes out '12:34:56:78:90:0:2' |
| 47 | |
| 48 | expect_err 0 mac_to_bytes out 'a2:0:f:de:0:29' |
| 49 | expected=(0xa2 0 0xf 0xde 0 0x29) |
William A. Kennington III | 70264b9 | 2021-05-07 03:07:31 -0700 | [diff] [blame] | 50 | expect_array_numeq out expected |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 51 | } |
| 52 | |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 53 | test_mac_to_eui48() { |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 54 | str="$(mac_to_eui48 '12:34:56:78:90:af')" || fail |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 55 | expect_streq "$str" '::1234:5678:90af' |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 56 | } |
| 57 | |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 58 | test_mac_to_eui64() { |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 59 | str="$(mac_to_eui64 '12:34:56:78:90:af')" || fail |
William A. Kennington III | fc8c436 | 2022-07-20 12:35:50 -0700 | [diff] [blame] | 60 | expect_streq "$str" '::1034:56ff:fe78:90af' |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 61 | } |
| 62 | |
William A. Kennington III | 70264b9 | 2021-05-07 03:07:31 -0700 | [diff] [blame] | 63 | test_ip4_to_bytes() { |
| 64 | out=() |
| 65 | expect_err 1 ip_to_bytes out '' |
| 66 | expect_err 1 ip_to_bytes out '10.0.0.' |
| 67 | expect_err 1 ip_to_bytes out '.0.1.1' |
| 68 | expect_err 1 ip_to_bytes out '10.0.0' |
| 69 | expect_err 1 ip_to_bytes out '10.0..0' |
| 70 | expect_err 1 ip_to_bytes out '.10.0.0.0' |
| 71 | expect_err 1 ip_to_bytes out '10.0.0.0.' |
| 72 | expect_err 1 ip_to_bytes out '10.0.0.256' |
| 73 | expect_err 1 ip_to_bytes out '10.0.0.0.256' |
| 74 | expect_err 1 ip_to_bytes out '10.0.0.0.1' |
| 75 | |
| 76 | expect_err 0 ip_to_bytes out '10.0.0.1' |
| 77 | expected=(10 0 0 1) |
| 78 | expect_array_numeq out expected |
| 79 | } |
| 80 | |
| 81 | test_ip6_to_bytes() { |
| 82 | out=() |
| 83 | expect_err 1 ip_to_bytes out '' |
| 84 | expect_err 1 ip_to_bytes out ':::' |
| 85 | expect_err 1 ip_to_bytes out '::z' |
| 86 | expect_err 1 ip_to_bytes out '1::1::1' |
| 87 | expect_err 1 ip_to_bytes out '1:1:1' |
| 88 | expect_err 1 ip_to_bytes out ':1::1' |
| 89 | expect_err 1 ip_to_bytes out '1::1:' |
| 90 | |
| 91 | expect_err 0 ip_to_bytes out '::' |
| 92 | expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |
| 93 | expect_array_numeq out expected |
| 94 | out=() |
| 95 | |
| 96 | expect_err 0 ip_to_bytes out '::1' |
| 97 | expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1) |
| 98 | expect_array_numeq out expected |
| 99 | out=() |
| 100 | |
| 101 | expect_err 0 ip_to_bytes out 'fd00::' |
| 102 | expected=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |
| 103 | expect_array_numeq out expected |
| 104 | out=() |
| 105 | |
| 106 | expect_err 0 ip_to_bytes out 'fd00:ffee::ddff:22' |
| 107 | expected=(0xfd 0 0xff 0xee 0 0 0 0 0 0 0 0 0xdd 0xff 0 0x22) |
| 108 | expect_array_numeq out expected |
| 109 | out=() |
| 110 | |
| 111 | expect_err 0 ip_to_bytes out '1:2:3:4:5:6:7:8' |
| 112 | expected=(0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8) |
| 113 | expect_array_numeq out expected |
| 114 | out=() |
| 115 | } |
| 116 | |
William A. Kennington III | 8077678 | 2021-05-10 03:07:14 -0700 | [diff] [blame] | 117 | test_ip4_bytes_str() { |
| 118 | in=(10 0 255 1) |
| 119 | str="$(ip_bytes_to_str in)" || fail |
| 120 | expect_streq "$str" '10.0.255.1' |
| 121 | } |
| 122 | |
| 123 | test_ip6_bytes_str() { |
| 124 | in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |
| 125 | str="$(ip_bytes_to_str in)" || fail |
| 126 | expect_streq "$str" '::' |
| 127 | in=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |
| 128 | str="$(ip_bytes_to_str in)" || fail |
| 129 | expect_streq "$str" 'fd00::' |
| 130 | in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0xfd) |
| 131 | str="$(ip_bytes_to_str in)" || fail |
| 132 | expect_streq "$str" '::fd' |
| 133 | in=(0xfd 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1) |
| 134 | str="$(ip_bytes_to_str in)" || fail |
| 135 | expect_streq "$str" 'fd01::1' |
| 136 | in=(0xfd 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1) |
| 137 | str="$(ip_bytes_to_str in)" || fail |
| 138 | expect_streq "$str" 'fd01::1:0:0:1' |
| 139 | in=(0xfd 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1) |
| 140 | str="$(ip_bytes_to_str in)" || fail |
| 141 | expect_streq "$str" 'fd01:0:0:1:1::1' |
| 142 | in=(0 1 0 1 0xdd 0xdd 0 1 0 1 0 1 0 1 0 1) |
| 143 | str="$(ip_bytes_to_str in)" || fail |
| 144 | expect_streq "$str" '1:1:dddd:1:1:1:1:1' |
| 145 | } |
| 146 | |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 147 | test_ip_pfx_concat() { |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 148 | # Invalid inputs |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 149 | expect_err 1 ip_pfx_concat 'fd/64' '::1234:5678:90af' |
| 150 | expect_err 1 ip_pfx_concat 'fd01::' '::1234:5678:90af' |
| 151 | expect_err 1 ip_pfx_concat 'fd01:' '::1234:5678:90af' |
| 152 | expect_err 1 ip_pfx_concat 'fd01::/a0' '::1234:5678:90af' |
| 153 | expect_err 1 ip_pfx_concat 'fd01::/64' ':1234:5678:90af' |
| 154 | expect_err 1 ip_pfx_concat 'fd01::/64' '' |
| 155 | expect_err 1 ip_pfx_concat 'fd01::/129' '::1' |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 156 | |
| 157 | # Too many address bits |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 158 | expect_err 1 ip_pfx_concat 'fd01:1:1:1:1::/64' '::1234:5678:90af' |
| 159 | expect_err 1 ip_pfx_concat 'fd01::/64' '::1:0:1234:5678:90af' |
| 160 | expect_err 1 ip_pfx_concat 'fd01::/79' '::3:1234:5678:90af' |
| 161 | expect_err 1 ip_pfx_concat 'fd01::/15' '::3:1234:5678:90af' |
| 162 | expect_err 1 ip_pfx_concat '10.0.0.1/31' '0.0.0.0' |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 163 | |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 164 | str="$(ip_pfx_concat '::1/128' '::0')" || fail |
| 165 | expect_streq "$str" '::1/128' |
| 166 | str="$(ip_pfx_concat 'fd01::/64' '::1')" || fail |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 167 | expect_streq "$str" 'fd01::1/64' |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 168 | str="$(ip_pfx_concat 'fd01::/127' '::1')" || fail |
| 169 | expect_streq "$str" 'fd01::1/127' |
| 170 | str="$(ip_pfx_concat 'fd02::/15' '::1')" || fail |
| 171 | expect_streq "$str" 'fd02::1/15' |
| 172 | str="$(ip_pfx_concat 'fd01::/72' '::1234:5678:90af')" || fail |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 173 | expect_streq "$str" 'fd01::1234:5678:90af/72' |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 174 | str="$(ip_pfx_concat 'fd01:eeee:aaaa:cccc::/64' '::a:1234:5678:90af')" || fail |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 175 | expect_streq "$str" 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/64' |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 176 | str="$(ip_pfx_concat 'fd01::fd00:0:0:0/80' '::1')" || fail |
| 177 | expect_streq "$str" 'fd01::fd00:0:0:1/80' |
| 178 | |
| 179 | str="$(ip_pfx_concat '10.0.0.0/24' '0.0.0.1')" || fail |
| 180 | expect_streq "$str" '10.0.0.1/24' |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 181 | } |
| 182 | |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 183 | test_ip_pfx_to_cidr() { |
| 184 | expect_err 1 ip_pfx_to_cidr 'z/64' |
| 185 | expect_err 1 ip_pfx_to_cidr '64' |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 186 | |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 187 | cidr="$(ip_pfx_to_cidr 'fd01::/64')" || fail |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 188 | expect_numeq "$cidr" 64 |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 189 | cidr="$(ip_pfx_to_cidr 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/128')" || fail |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 190 | expect_numeq "$cidr" 128 |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 191 | cidr="$(ip_pfx_to_cidr '10.0.0.1/24')" || fail |
| 192 | expect_numeq "$cidr" 24 |
| 193 | } |
| 194 | |
| 195 | test_normalize_ip() { |
| 196 | ip="$(normalize_ip 'fd01:1::0:0:1')" || fail |
| 197 | expect_streq "$ip" 'fd01:1::1' |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return 0 2>/dev/null |
| 201 | main |