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 24*70264b98SWilliam A. Kennington IIIexpect_array_numeq() { 25*70264b98SWilliam A. Kennington III local -n a1="$1" 26*70264b98SWilliam A. Kennington III local -n a2="$2" 27*70264b98SWilliam A. Kennington III 28*70264b98SWilliam A. Kennington III if (( "${#a1[@]}" != "${#a2[@]}" )); then 29*70264b98SWilliam A. Kennington III echo " Line ${BASH_LINENO[0]} Array Size ${#a1[@]} != ${#a2[@]}" >&2 30*70264b98SWilliam A. Kennington III test_err=1 31*70264b98SWilliam A. Kennington III else 32*70264b98SWilliam A. Kennington III local i 33*70264b98SWilliam A. Kennington III for (( i=0; i < ${#a1[@]}; ++i )); do 34*70264b98SWilliam A. Kennington III expect_numeq "${a1[$i]}" "${a2[$i]}" 35*70264b98SWilliam A. Kennington III done 36*70264b98SWilliam A. Kennington III fi 37*70264b98SWilliam A. Kennington III} 38*70264b98SWilliam 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) 50*70264b98SWilliam 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 63*70264b98SWilliam A. Kennington IIItest_ip4_to_bytes() { 64*70264b98SWilliam A. Kennington III out=() 65*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '' 66*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0.0.' 67*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '.0.1.1' 68*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0.0' 69*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0..0' 70*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '.10.0.0.0' 71*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0.0.0.' 72*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0.0.256' 73*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0.0.0.256' 74*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '10.0.0.0.1' 75*70264b98SWilliam A. Kennington III 76*70264b98SWilliam A. Kennington III expect_err 0 ip_to_bytes out '10.0.0.1' 77*70264b98SWilliam A. Kennington III expected=(10 0 0 1) 78*70264b98SWilliam A. Kennington III expect_array_numeq out expected 79*70264b98SWilliam A. Kennington III} 80*70264b98SWilliam A. Kennington III 81*70264b98SWilliam A. Kennington IIItest_ip6_to_bytes() { 82*70264b98SWilliam A. Kennington III out=() 83*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '' 84*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out ':::' 85*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '::z' 86*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '1::1::1' 87*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '1:1:1' 88*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out ':1::1' 89*70264b98SWilliam A. Kennington III expect_err 1 ip_to_bytes out '1::1:' 90*70264b98SWilliam A. Kennington III 91*70264b98SWilliam A. Kennington III expect_err 0 ip_to_bytes out '::' 92*70264b98SWilliam A. Kennington III expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) 93*70264b98SWilliam A. Kennington III expect_array_numeq out expected 94*70264b98SWilliam A. Kennington III out=() 95*70264b98SWilliam A. Kennington III 96*70264b98SWilliam A. Kennington III expect_err 0 ip_to_bytes out '::1' 97*70264b98SWilliam A. Kennington III expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1) 98*70264b98SWilliam A. Kennington III expect_array_numeq out expected 99*70264b98SWilliam A. Kennington III out=() 100*70264b98SWilliam A. Kennington III 101*70264b98SWilliam A. Kennington III expect_err 0 ip_to_bytes out 'fd00::' 102*70264b98SWilliam A. Kennington III expected=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) 103*70264b98SWilliam A. Kennington III expect_array_numeq out expected 104*70264b98SWilliam A. Kennington III out=() 105*70264b98SWilliam A. Kennington III 106*70264b98SWilliam A. Kennington III expect_err 0 ip_to_bytes out 'fd00:ffee::ddff:22' 107*70264b98SWilliam A. Kennington III expected=(0xfd 0 0xff 0xee 0 0 0 0 0 0 0 0 0xdd 0xff 0 0x22) 108*70264b98SWilliam A. Kennington III expect_array_numeq out expected 109*70264b98SWilliam A. Kennington III out=() 110*70264b98SWilliam A. Kennington III 111*70264b98SWilliam A. Kennington III expect_err 0 ip_to_bytes out '1:2:3:4:5:6:7:8' 112*70264b98SWilliam A. Kennington III expected=(0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8) 113*70264b98SWilliam A. Kennington III expect_array_numeq out expected 114*70264b98SWilliam A. Kennington III out=() 115*70264b98SWilliam A. Kennington III} 116*70264b98SWilliam A. Kennington III 1171e268105SWilliam A. Kennington IIItest_ipv6_pfx_concat() { 1181e268105SWilliam A. Kennington III # Invalid inputs 1191e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd/64' '1234:5678:90af' 1201e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::' '1234:5678:90af' 1211e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01:' '1234:5678:90af' 1221e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::/a0' '1234:5678:90af' 1231e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::/64' ':1234:5678:90af' 1241e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::/64' '::' 1251e268105SWilliam A. Kennington III 1261e268105SWilliam A. Kennington III # Too many address bits 1271e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01:1:1:1:1::/64' '1234:5678:90af' 1281e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::/64' '1:0:1234:5678:90af' 1291e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::/65' '1:1234:5678:90af' 1301e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_concat 'fd01::/72' '1:1234:5678:90af' 1311e268105SWilliam A. Kennington III 1321e268105SWilliam A. Kennington III str="$(ipv6_pfx_concat 'fd01::/64' '1')" || fail 1331e268105SWilliam A. Kennington III expect_streq "$str" 'fd01::1/64' 1341e268105SWilliam A. Kennington III str="$(ipv6_pfx_concat 'fd01::/72' '1234:5678:90af')" || fail 1351e268105SWilliam A. Kennington III expect_streq "$str" 'fd01::1234:5678:90af/72' 1361e268105SWilliam A. Kennington III str="$(ipv6_pfx_concat 'fd01:eeee:aaaa:cccc::/64' 'a:1234:5678:90af')" || fail 1371e268105SWilliam A. Kennington III expect_streq "$str" 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/64' 1381e268105SWilliam A. Kennington III} 1391e268105SWilliam A. Kennington III 1401e268105SWilliam A. Kennington IIItest_ipv6_pfx_to_cidr() { 1411e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_to_cidr 'z/64' 1421e268105SWilliam A. Kennington III expect_err 1 ipv6_pfx_to_cidr '64' 1431e268105SWilliam A. Kennington III 1441e268105SWilliam A. Kennington III cidr="$(ipv6_pfx_to_cidr 'fd01::/64')" || fail 1451e268105SWilliam A. Kennington III expect_numeq "$cidr" 64 1461e268105SWilliam A. Kennington III cidr="$(ipv6_pfx_to_cidr 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/128')" || fail 1471e268105SWilliam A. Kennington III expect_numeq "$cidr" 128 1481e268105SWilliam A. Kennington III} 1491e268105SWilliam A. Kennington III 1501e268105SWilliam A. Kennington IIIreturn 0 2>/dev/null 1511e268105SWilliam A. Kennington IIImain 152