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 | |
William A. Kennington III | 0320695 | 2023-06-05 14:16:02 -0700 | [diff] [blame] | 16 | cd "$(dirname "$0")" || exit |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 17 | if [ -e ../network-sh.bb ]; then |
William A. Kennington III | 0320695 | 2023-06-05 14:16:02 -0700 | [diff] [blame] | 18 | # shellcheck source=meta-google/recipes-google/test/test-sh/lib.sh |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 19 | source '../../test/test-sh/lib.sh' |
| 20 | else |
William A. Kennington III | 0320695 | 2023-06-05 14:16:02 -0700 | [diff] [blame] | 21 | # shellcheck source=meta-google/recipes-google/test/test-sh/lib.sh |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 22 | source "$SYSROOT/usr/share/test/lib.sh" |
| 23 | fi |
William A. Kennington III | 0320695 | 2023-06-05 14:16:02 -0700 | [diff] [blame] | 24 | # shellcheck source=meta-google/recipes-google/networking/network-sh/lib.sh |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 25 | source lib.sh |
| 26 | |
William A. Kennington III | 70264b9 | 2021-05-07 03:07:31 -0700 | [diff] [blame] | 27 | expect_array_numeq() { |
| 28 | local -n a1="$1" |
| 29 | local -n a2="$2" |
| 30 | |
| 31 | if (( "${#a1[@]}" != "${#a2[@]}" )); then |
| 32 | echo " Line ${BASH_LINENO[0]} Array Size ${#a1[@]} != ${#a2[@]}" >&2 |
| 33 | test_err=1 |
| 34 | else |
| 35 | local i |
| 36 | for (( i=0; i < ${#a1[@]}; ++i )); do |
| 37 | expect_numeq "${a1[$i]}" "${a2[$i]}" |
| 38 | done |
| 39 | fi |
| 40 | } |
| 41 | |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 42 | test_mac_to_bytes() { |
| 43 | out=() |
| 44 | expect_err 1 mac_to_bytes out '' |
| 45 | expect_err 1 mac_to_bytes out '00' |
| 46 | expect_err 1 mac_to_bytes out '12:34:56:78:90:' |
| 47 | expect_err 1 mac_to_bytes out ':12:34:56:78:90' |
| 48 | expect_err 1 mac_to_bytes out '12:34:56:78:90:0:' |
| 49 | expect_err 1 mac_to_bytes out '12:34:56:78:90:0:2' |
| 50 | |
| 51 | expect_err 0 mac_to_bytes out 'a2:0:f:de:0:29' |
| 52 | expected=(0xa2 0 0xf 0xde 0 0x29) |
William A. Kennington III | 70264b9 | 2021-05-07 03:07:31 -0700 | [diff] [blame] | 53 | expect_array_numeq out expected |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 54 | } |
| 55 | |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 56 | test_mac_to_eui48() { |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 57 | 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] | 58 | expect_streq "$str" '::1234:5678:90af' |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 59 | } |
| 60 | |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 61 | test_mac_to_eui64() { |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 62 | 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] | 63 | expect_streq "$str" '::1034:56ff:fe78:90af' |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 64 | } |
| 65 | |
William A. Kennington III | 70264b9 | 2021-05-07 03:07:31 -0700 | [diff] [blame] | 66 | test_ip4_to_bytes() { |
| 67 | out=() |
| 68 | expect_err 1 ip_to_bytes out '' |
| 69 | expect_err 1 ip_to_bytes out '10.0.0.' |
| 70 | expect_err 1 ip_to_bytes out '.0.1.1' |
| 71 | expect_err 1 ip_to_bytes out '10.0.0' |
| 72 | expect_err 1 ip_to_bytes out '10.0..0' |
| 73 | expect_err 1 ip_to_bytes out '.10.0.0.0' |
| 74 | expect_err 1 ip_to_bytes out '10.0.0.0.' |
| 75 | expect_err 1 ip_to_bytes out '10.0.0.256' |
| 76 | expect_err 1 ip_to_bytes out '10.0.0.0.256' |
| 77 | expect_err 1 ip_to_bytes out '10.0.0.0.1' |
| 78 | |
| 79 | expect_err 0 ip_to_bytes out '10.0.0.1' |
| 80 | expected=(10 0 0 1) |
| 81 | expect_array_numeq out expected |
| 82 | } |
| 83 | |
| 84 | test_ip6_to_bytes() { |
| 85 | out=() |
| 86 | expect_err 1 ip_to_bytes out '' |
| 87 | expect_err 1 ip_to_bytes out ':::' |
| 88 | expect_err 1 ip_to_bytes out '::z' |
| 89 | expect_err 1 ip_to_bytes out '1::1::1' |
| 90 | expect_err 1 ip_to_bytes out '1:1:1' |
| 91 | expect_err 1 ip_to_bytes out ':1::1' |
| 92 | expect_err 1 ip_to_bytes out '1::1:' |
| 93 | |
| 94 | expect_err 0 ip_to_bytes out '::' |
| 95 | expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |
| 96 | expect_array_numeq out expected |
| 97 | out=() |
| 98 | |
| 99 | expect_err 0 ip_to_bytes out '::1' |
| 100 | expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1) |
| 101 | expect_array_numeq out expected |
| 102 | out=() |
| 103 | |
| 104 | expect_err 0 ip_to_bytes out 'fd00::' |
| 105 | expected=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |
| 106 | expect_array_numeq out expected |
| 107 | out=() |
| 108 | |
| 109 | expect_err 0 ip_to_bytes out 'fd00:ffee::ddff:22' |
| 110 | expected=(0xfd 0 0xff 0xee 0 0 0 0 0 0 0 0 0xdd 0xff 0 0x22) |
| 111 | expect_array_numeq out expected |
| 112 | out=() |
| 113 | |
| 114 | expect_err 0 ip_to_bytes out '1:2:3:4:5:6:7:8' |
| 115 | expected=(0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8) |
| 116 | expect_array_numeq out expected |
William A. Kennington III | 0320695 | 2023-06-05 14:16:02 -0700 | [diff] [blame] | 117 | # shellcheck disable=SC2034 |
William A. Kennington III | 70264b9 | 2021-05-07 03:07:31 -0700 | [diff] [blame] | 118 | out=() |
| 119 | } |
| 120 | |
William A. Kennington III | 8077678 | 2021-05-10 03:07:14 -0700 | [diff] [blame] | 121 | test_ip4_bytes_str() { |
| 122 | in=(10 0 255 1) |
| 123 | str="$(ip_bytes_to_str in)" || fail |
| 124 | expect_streq "$str" '10.0.255.1' |
| 125 | } |
| 126 | |
| 127 | test_ip6_bytes_str() { |
| 128 | in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |
| 129 | str="$(ip_bytes_to_str in)" || fail |
| 130 | expect_streq "$str" '::' |
| 131 | in=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |
| 132 | str="$(ip_bytes_to_str in)" || fail |
| 133 | expect_streq "$str" 'fd00::' |
| 134 | in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0xfd) |
| 135 | str="$(ip_bytes_to_str in)" || fail |
| 136 | expect_streq "$str" '::fd' |
| 137 | in=(0xfd 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1) |
| 138 | str="$(ip_bytes_to_str in)" || fail |
| 139 | expect_streq "$str" 'fd01::1' |
| 140 | in=(0xfd 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1) |
| 141 | str="$(ip_bytes_to_str in)" || fail |
| 142 | expect_streq "$str" 'fd01::1:0:0:1' |
| 143 | in=(0xfd 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1) |
| 144 | str="$(ip_bytes_to_str in)" || fail |
| 145 | expect_streq "$str" 'fd01:0:0:1:1::1' |
William A. Kennington III | 0320695 | 2023-06-05 14:16:02 -0700 | [diff] [blame] | 146 | # shellcheck disable=SC2034 |
William A. Kennington III | 8077678 | 2021-05-10 03:07:14 -0700 | [diff] [blame] | 147 | in=(0 1 0 1 0xdd 0xdd 0 1 0 1 0 1 0 1 0 1) |
| 148 | str="$(ip_bytes_to_str in)" || fail |
| 149 | expect_streq "$str" '1:1:dddd:1:1:1:1:1' |
| 150 | } |
| 151 | |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 152 | test_ip_pfx_concat() { |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 153 | # Invalid inputs |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 154 | expect_err 1 ip_pfx_concat 'fd/64' '::1234:5678:90af' |
| 155 | expect_err 1 ip_pfx_concat 'fd01::' '::1234:5678:90af' |
| 156 | expect_err 1 ip_pfx_concat 'fd01:' '::1234:5678:90af' |
| 157 | expect_err 1 ip_pfx_concat 'fd01::/a0' '::1234:5678:90af' |
| 158 | expect_err 1 ip_pfx_concat 'fd01::/64' ':1234:5678:90af' |
| 159 | expect_err 1 ip_pfx_concat 'fd01::/64' '' |
| 160 | expect_err 1 ip_pfx_concat 'fd01::/129' '::1' |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 161 | |
| 162 | # Too many address bits |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 163 | expect_err 1 ip_pfx_concat 'fd01:1:1:1:1::/64' '::1234:5678:90af' |
| 164 | expect_err 1 ip_pfx_concat 'fd01::/64' '::1:0:1234:5678:90af' |
| 165 | expect_err 1 ip_pfx_concat 'fd01::/79' '::3:1234:5678:90af' |
| 166 | expect_err 1 ip_pfx_concat 'fd01::/15' '::3:1234:5678:90af' |
| 167 | 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] | 168 | |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 169 | str="$(ip_pfx_concat '::1/128' '::0')" || fail |
| 170 | expect_streq "$str" '::1/128' |
| 171 | str="$(ip_pfx_concat 'fd01::/64' '::1')" || fail |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 172 | expect_streq "$str" 'fd01::1/64' |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 173 | str="$(ip_pfx_concat 'fd01::/127' '::1')" || fail |
| 174 | expect_streq "$str" 'fd01::1/127' |
| 175 | str="$(ip_pfx_concat 'fd02::/15' '::1')" || fail |
| 176 | expect_streq "$str" 'fd02::1/15' |
| 177 | str="$(ip_pfx_concat 'fd01::/72' '::1234:5678:90af')" || fail |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 178 | expect_streq "$str" 'fd01::1234:5678:90af/72' |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 179 | 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] | 180 | 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] | 181 | str="$(ip_pfx_concat 'fd01::fd00:0:0:0/80' '::1')" || fail |
| 182 | expect_streq "$str" 'fd01::fd00:0:0:1/80' |
| 183 | |
| 184 | str="$(ip_pfx_concat '10.0.0.0/24' '0.0.0.1')" || fail |
| 185 | expect_streq "$str" '10.0.0.1/24' |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 186 | } |
| 187 | |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 188 | test_ip_pfx_to_cidr() { |
| 189 | expect_err 1 ip_pfx_to_cidr 'z/64' |
| 190 | expect_err 1 ip_pfx_to_cidr '64' |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 191 | |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 192 | cidr="$(ip_pfx_to_cidr 'fd01::/64')" || fail |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 193 | expect_numeq "$cidr" 64 |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 194 | 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] | 195 | expect_numeq "$cidr" 128 |
William A. Kennington III | 6ca7033 | 2021-05-10 03:14:42 -0700 | [diff] [blame] | 196 | cidr="$(ip_pfx_to_cidr '10.0.0.1/24')" || fail |
| 197 | expect_numeq "$cidr" 24 |
| 198 | } |
| 199 | |
| 200 | test_normalize_ip() { |
| 201 | ip="$(normalize_ip 'fd01:1::0:0:1')" || fail |
| 202 | expect_streq "$ip" 'fd01:1::1' |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 203 | } |
| 204 | |
William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 205 | main |