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 531e268105SWilliam A. Kennington IIItest_mac_to_eui_48() { 541e268105SWilliam A. Kennington III str="$(mac_to_eui48 '12:34:56:78:90:af')" || fail 551e268105SWilliam A. Kennington III expect_streq "$str" '1234:5678:90af' 561e268105SWilliam A. Kennington III} 571e268105SWilliam A. Kennington III 581e268105SWilliam A. Kennington IIItest_eui_64() { 591e268105SWilliam A. Kennington III str="$(mac_to_eui64 '12:34:56:78:90:af')" || fail 601e268105SWilliam 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 117*80776788SWilliam A. Kennington IIItest_ip4_bytes_str() { 118*80776788SWilliam A. Kennington III in=(10 0 255 1) 119*80776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 120*80776788SWilliam A. Kennington III expect_streq "$str" '10.0.255.1' 121*80776788SWilliam A. Kennington III} 122*80776788SWilliam A. Kennington III 123*80776788SWilliam A. Kennington IIItest_ip6_bytes_str() { 124*80776788SWilliam A. Kennington III in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) 125*80776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 126*80776788SWilliam A. Kennington III expect_streq "$str" '::' 127*80776788SWilliam A. Kennington III in=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) 128*80776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 129*80776788SWilliam A. Kennington III expect_streq "$str" 'fd00::' 130*80776788SWilliam A. Kennington III in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0xfd) 131*80776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 132*80776788SWilliam A. Kennington III expect_streq "$str" '::fd' 133*80776788SWilliam A. Kennington III in=(0xfd 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1) 134*80776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 135*80776788SWilliam A. Kennington III expect_streq "$str" 'fd01::1' 136*80776788SWilliam A. Kennington III in=(0xfd 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1) 137*80776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 138*80776788SWilliam A. Kennington III expect_streq "$str" 'fd01::1:0:0:1' 139*80776788SWilliam A. Kennington III in=(0xfd 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1) 140*80776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 141*80776788SWilliam A. Kennington III expect_streq "$str" 'fd01:0:0:1:1::1' 142*80776788SWilliam A. Kennington III in=(0 1 0 1 0xdd 0xdd 0 1 0 1 0 1 0 1 0 1) 143*80776788SWilliam A. Kennington III str="$(ip_bytes_to_str in)" || fail 144*80776788SWilliam A. Kennington III expect_streq "$str" '1:1:dddd:1:1:1:1:1' 145*80776788SWilliam A. Kennington III} 146*80776788SWilliam A. Kennington III 1471e268105SWilliam A. Kennington IIItest_ipv6_pfx_concat() { 1481e268105SWilliam A. Kennington III # Invalid inputs 1491e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd/64' '1234:5678:90af' 1501e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::' '1234:5678:90af' 1511e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01:' '1234:5678:90af' 1521e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::/a0' '1234:5678:90af' 1531e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::/64' ':1234:5678:90af' 1541e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::/64' '::' 1551e268105SWilliam A. Kennington III 1561e268105SWilliam A. Kennington III # Too many address bits 1571e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01:1:1:1:1::/64' '1234:5678:90af' 1581e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::/64' '1:0:1234:5678:90af' 1591e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::/65' '1:1234:5678:90af' 1601e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::/72' '1:1234:5678:90af' 1611e268105SWilliam A. Kennington III 1621e268105SWilliam A. Kennington III str="$(ipv6_pfx_concat 'fd01::/64' '1')" || fail 1631e268105SWilliam A. Kennington III expect_streq "$str" 'fd01::1/64' 1641e268105SWilliam A. Kennington III str="$(ipv6_pfx_concat 'fd01::/72' '1234:5678:90af')" || fail 1651e268105SWilliam A. Kennington III expect_streq "$str" 'fd01::1234:5678:90af/72' 1661e268105SWilliam A. Kennington III str="$(ipv6_pfx_concat 'fd01:eeee:aaaa:cccc::/64' 'a:1234:5678:90af')" || fail 1671e268105SWilliam A. Kennington III expect_streq "$str" 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/64' 1681e268105SWilliam A. Kennington III} 1691e268105SWilliam A. Kennington III 1701e268105SWilliam A. Kennington IIItest_ipv6_pfx_to_cidr() { 1711e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_to_cidr 'z/64' 1721e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_to_cidr '64' 1731e268105SWilliam A. Kennington III 1741e268105SWilliam A. Kennington III cidr="$(ipv6_pfx_to_cidr 'fd01::/64')" || fail 1751e268105SWilliam A. Kennington III expect_numeq "$cidr" 64 1761e268105SWilliam A. Kennington III cidr="$(ipv6_pfx_to_cidr 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/128')" || fail 1771e268105SWilliam A. Kennington III expect_numeq "$cidr" 128 1781e268105SWilliam A. Kennington III} 1791e268105SWilliam A. Kennington III 1801e268105SWilliam A. Kennington IIIreturn 0 2>/dev/null 1811e268105SWilliam A. Kennington IIImain 182