11e268105SWilliam A. Kennington III#!/bin/bash 21e268105SWilliam A. Kennington III# Copyright 2021 Google LLC 31e268105SWilliam A. Kennington III# 41e268105SWilliam A. Kennington III# Licensed under the Apache License, Version 2.0 (the "License"); 51e268105SWilliam A. Kennington III# you may not use this file except in compliance with the License. 61e268105SWilliam A. Kennington III# You may obtain a copy of the License at 71e268105SWilliam A. Kennington III# 81e268105SWilliam A. Kennington III# http://www.apache.org/licenses/LICENSE-2.0 91e268105SWilliam A. Kennington III# 101e268105SWilliam A. Kennington III# Unless required by applicable law or agreed to in writing, software 111e268105SWilliam A. Kennington III# distributed under the License is distributed on an "AS IS" BASIS, 121e268105SWilliam A. Kennington III# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131e268105SWilliam A. Kennington III# See the License for the specific language governing permissions and 141e268105SWilliam A. Kennington III# limitations under the License. 151e268105SWilliam A. Kennington III 161e268105SWilliam A. Kennington IIIcd "$(dirname "$0")" 171e268105SWilliam A. Kennington IIIif [ -e ../network-sh.bb ]; then 181e268105SWilliam A. Kennington III source '../../test/test-sh/lib.sh' 191e268105SWilliam A. Kennington IIIelse 201e268105SWilliam A. Kennington III source "$SYSROOT/usr/share/test/lib.sh" 211e268105SWilliam A. Kennington IIIfi 221e268105SWilliam A. Kennington IIIsource lib.sh 231e268105SWilliam A. Kennington III 2470264b98SWilliam A. Kennington IIIexpect_array_numeq() { 2570264b98SWilliam A. Kennington III local -n a1="$1" 2670264b98SWilliam A. Kennington III local -n a2="$2" 2770264b98SWilliam A. Kennington III 2870264b98SWilliam A. Kennington III if (( "${#a1[@]}" != "${#a2[@]}" )); then 2970264b98SWilliam A. Kennington III echo " Line ${BASH_LINENO[0]} Array Size ${#a1[@]} != ${#a2[@]}" >&2 3070264b98SWilliam A. Kennington III test_err=1 3170264b98SWilliam A. Kennington III else 3270264b98SWilliam A. Kennington III local i 3370264b98SWilliam A. Kennington III for (( i=0; i < ${#a1[@]}; ++i )); do 3470264b98SWilliam A. Kennington III expect_numeq "${a1[$i]}" "${a2[$i]}" 3570264b98SWilliam A. Kennington III done 3670264b98SWilliam A. Kennington III fi 3770264b98SWilliam A. Kennington III} 3870264b98SWilliam A. Kennington III 391e268105SWilliam A. Kennington IIItest_mac_to_bytes() { 401e268105SWilliam A. Kennington III out=() 411e268105SWilliam A. Kennington III expect_err 1 mac_to_bytes out '' 421e268105SWilliam A. Kennington III expect_err 1 mac_to_bytes out '00' 431e268105SWilliam A. Kennington III expect_err 1 mac_to_bytes out '12:34:56:78:90:' 441e268105SWilliam A. Kennington III expect_err 1 mac_to_bytes out ':12:34:56:78:90' 451e268105SWilliam A. Kennington III expect_err 1 mac_to_bytes out '12:34:56:78:90:0:' 461e268105SWilliam A. Kennington III expect_err 1 mac_to_bytes out '12:34:56:78:90:0:2' 471e268105SWilliam A. Kennington III 481e268105SWilliam A. Kennington III expect_err 0 mac_to_bytes out 'a2:0:f:de:0:29' 491e268105SWilliam A. Kennington III expected=(0xa2 0 0xf 0xde 0 0x29) 5070264b98SWilliam A. Kennington III expect_array_numeq out expected 511e268105SWilliam A. Kennington III} 521e268105SWilliam A. Kennington III 53*6ca70339SWilliam A. Kennington IIItest_mac_to_eui48() { 541e268105SWilliam A. Kennington III str="$(mac_to_eui48 '12:34:56:78:90:af')" || fail 55*6ca70339SWilliam A. Kennington III expect_streq "$str" '::1234:5678:90af' 561e268105SWilliam A. Kennington III} 571e268105SWilliam A. Kennington III 58*6ca70339SWilliam A. Kennington IIItest_mac_to_eui64() { 591e268105SWilliam A. Kennington III str="$(mac_to_eui64 '12:34:56:78:90:af')" || fail 60*6ca70339SWilliam A. Kennington III expect_streq "$str" '::1334:56ff:fe78:90af' 611e268105SWilliam A. Kennington III} 621e268105SWilliam A. Kennington III 6370264b98SWilliam A. Kennington IIItest_ip4_to_bytes() { 6470264b98SWilliam A. Kennington III out=() 6570264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '' 6670264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0.0.' 6770264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '.0.1.1' 6870264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0.0' 6970264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0..0' 7070264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '.10.0.0.0' 7170264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0.0.0.' 7270264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0.0.256' 7370264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0.0.0.256' 7470264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0.0.0.1' 7570264b98SWilliam A. Kennington III 7670264b98SWilliam A. Kennington III expect_err 0 ip_to_bytes out '10.0.0.1' 7770264b98SWilliam A. Kennington III expected=(10 0 0 1) 7870264b98SWilliam A. Kennington III expect_array_numeq out expected 7970264b98SWilliam A. Kennington III} 8070264b98SWilliam A. Kennington III 8170264b98SWilliam A. Kennington IIItest_ip6_to_bytes() { 8270264b98SWilliam A. Kennington III out=() 8370264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '' 8470264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out ':::' 8570264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '::z' 8670264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '1::1::1' 8770264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '1:1:1' 8870264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out ':1::1' 8970264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '1::1:' 9070264b98SWilliam A. Kennington III 9170264b98SWilliam A. Kennington III expect_err 0 ip_to_bytes out '::' 9270264b98SWilliam A. Kennington III expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) 9370264b98SWilliam A. Kennington III expect_array_numeq out expected 9470264b98SWilliam A. Kennington III out=() 9570264b98SWilliam A. Kennington III 9670264b98SWilliam A. Kennington III expect_err 0 ip_to_bytes out '::1' 9770264b98SWilliam A. Kennington III expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1) 9870264b98SWilliam A. Kennington III expect_array_numeq out expected 9970264b98SWilliam A. Kennington III out=() 10070264b98SWilliam A. Kennington III 10170264b98SWilliam A. Kennington III expect_err 0 ip_to_bytes out 'fd00::' 10270264b98SWilliam A. Kennington III expected=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) 10370264b98SWilliam A. Kennington III expect_array_numeq out expected 10470264b98SWilliam A. Kennington III out=() 10570264b98SWilliam A. Kennington III 10670264b98SWilliam A. Kennington III expect_err 0 ip_to_bytes out 'fd00:ffee::ddff:22' 10770264b98SWilliam A. Kennington III expected=(0xfd 0 0xff 0xee 0 0 0 0 0 0 0 0 0xdd 0xff 0 0x22) 10870264b98SWilliam A. Kennington III expect_array_numeq out expected 10970264b98SWilliam A. Kennington III out=() 11070264b98SWilliam A. Kennington III 11170264b98SWilliam A. Kennington III expect_err 0 ip_to_bytes out '1:2:3:4:5:6:7:8' 11270264b98SWilliam A. Kennington III expected=(0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8) 11370264b98SWilliam A. Kennington III expect_array_numeq out expected 11470264b98SWilliam A. Kennington III out=() 11570264b98SWilliam A. Kennington III} 11670264b98SWilliam A. Kennington III 11780776788SWilliam A. Kennington IIItest_ip4_bytes_str() { 11880776788SWilliam A. Kennington III in=(10 0 255 1) 11980776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 12080776788SWilliam A. Kennington III expect_streq "$str" '10.0.255.1' 12180776788SWilliam A. Kennington III} 12280776788SWilliam A. Kennington III 12380776788SWilliam A. Kennington IIItest_ip6_bytes_str() { 12480776788SWilliam A. Kennington III in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) 12580776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 12680776788SWilliam A. Kennington III expect_streq "$str" '::' 12780776788SWilliam A. Kennington III in=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) 12880776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 12980776788SWilliam A. Kennington III expect_streq "$str" 'fd00::' 13080776788SWilliam A. Kennington III in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0xfd) 13180776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 13280776788SWilliam A. Kennington III expect_streq "$str" '::fd' 13380776788SWilliam A. Kennington III in=(0xfd 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1) 13480776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 13580776788SWilliam A. Kennington III expect_streq "$str" 'fd01::1' 13680776788SWilliam A. Kennington III in=(0xfd 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1) 13780776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 13880776788SWilliam A. Kennington III expect_streq "$str" 'fd01::1:0:0:1' 13980776788SWilliam A. Kennington III in=(0xfd 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1) 14080776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 14180776788SWilliam A. Kennington III expect_streq "$str" 'fd01:0:0:1:1::1' 14280776788SWilliam A. Kennington III in=(0 1 0 1 0xdd 0xdd 0 1 0 1 0 1 0 1 0 1) 14380776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 14480776788SWilliam A. Kennington III expect_streq "$str" '1:1:dddd:1:1:1:1:1' 14580776788SWilliam A. Kennington III} 14680776788SWilliam A. Kennington III 147*6ca70339SWilliam A. Kennington IIItest_ip_pfx_concat() { 1481e268105SWilliam A. Kennington III # Invalid inputs 149*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_concat 'fd/64' '::1234:5678:90af' 150*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_concat 'fd01::' '::1234:5678:90af' 151*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_concat 'fd01:' '::1234:5678:90af' 152*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_concat 'fd01::/a0' '::1234:5678:90af' 153*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_concat 'fd01::/64' ':1234:5678:90af' 154*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_concat 'fd01::/64' '' 155*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_concat 'fd01::/129' '::1' 1561e268105SWilliam A. Kennington III 1571e268105SWilliam A. Kennington III # Too many address bits 158*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_concat 'fd01:1:1:1:1::/64' '::1234:5678:90af' 159*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_concat 'fd01::/64' '::1:0:1234:5678:90af' 160*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_concat 'fd01::/79' '::3:1234:5678:90af' 161*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_concat 'fd01::/15' '::3:1234:5678:90af' 162*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_concat '10.0.0.1/31' '0.0.0.0' 1631e268105SWilliam A. Kennington III 164*6ca70339SWilliam A. Kennington III str="$(ip_pfx_concat '::1/128' '::0')" || fail 165*6ca70339SWilliam A. Kennington III expect_streq "$str" '::1/128' 166*6ca70339SWilliam A. Kennington III str="$(ip_pfx_concat 'fd01::/64' '::1')" || fail 1671e268105SWilliam A. Kennington III expect_streq "$str" 'fd01::1/64' 168*6ca70339SWilliam A. Kennington III str="$(ip_pfx_concat 'fd01::/127' '::1')" || fail 169*6ca70339SWilliam A. Kennington III expect_streq "$str" 'fd01::1/127' 170*6ca70339SWilliam A. Kennington III str="$(ip_pfx_concat 'fd02::/15' '::1')" || fail 171*6ca70339SWilliam A. Kennington III expect_streq "$str" 'fd02::1/15' 172*6ca70339SWilliam A. Kennington III str="$(ip_pfx_concat 'fd01::/72' '::1234:5678:90af')" || fail 1731e268105SWilliam A. Kennington III expect_streq "$str" 'fd01::1234:5678:90af/72' 174*6ca70339SWilliam A. Kennington III str="$(ip_pfx_concat 'fd01:eeee:aaaa:cccc::/64' '::a:1234:5678:90af')" || fail 1751e268105SWilliam A. Kennington III expect_streq "$str" 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/64' 176*6ca70339SWilliam A. Kennington III str="$(ip_pfx_concat 'fd01::fd00:0:0:0/80' '::1')" || fail 177*6ca70339SWilliam A. Kennington III expect_streq "$str" 'fd01::fd00:0:0:1/80' 178*6ca70339SWilliam A. Kennington III 179*6ca70339SWilliam A. Kennington III str="$(ip_pfx_concat '10.0.0.0/24' '0.0.0.1')" || fail 180*6ca70339SWilliam A. Kennington III expect_streq "$str" '10.0.0.1/24' 1811e268105SWilliam A. Kennington III} 1821e268105SWilliam A. Kennington III 183*6ca70339SWilliam A. Kennington IIItest_ip_pfx_to_cidr() { 184*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_to_cidr 'z/64' 185*6ca70339SWilliam A. Kennington III expect_err 1 ip_pfx_to_cidr '64' 1861e268105SWilliam A. Kennington III 187*6ca70339SWilliam A. Kennington III cidr="$(ip_pfx_to_cidr 'fd01::/64')" || fail 1881e268105SWilliam A. Kennington III expect_numeq "$cidr" 64 189*6ca70339SWilliam A. Kennington III cidr="$(ip_pfx_to_cidr 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/128')" || fail 1901e268105SWilliam A. Kennington III expect_numeq "$cidr" 128 191*6ca70339SWilliam A. Kennington III cidr="$(ip_pfx_to_cidr '10.0.0.1/24')" || fail 192*6ca70339SWilliam A. Kennington III expect_numeq "$cidr" 24 193*6ca70339SWilliam A. Kennington III} 194*6ca70339SWilliam A. Kennington III 195*6ca70339SWilliam A. Kennington IIItest_normalize_ip() { 196*6ca70339SWilliam A. Kennington III ip="$(normalize_ip 'fd01:1::0:0:1')" || fail 197*6ca70339SWilliam A. Kennington III expect_streq "$ip" 'fd01:1::1' 1981e268105SWilliam A. Kennington III} 1991e268105SWilliam A. Kennington III 2001e268105SWilliam A. Kennington IIIreturn 0 2>/dev/null 2011e268105SWilliam A. Kennington IIImain 202