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
16*03206951SWilliam A. Kennington IIIcd "$(dirname "$0")" || exit
171e268105SWilliam A. Kennington IIIif [ -e ../network-sh.bb ]; then
18*03206951SWilliam A. Kennington III  # shellcheck source=meta-google/recipes-google/test/test-sh/lib.sh
191e268105SWilliam A. Kennington III  source '../../test/test-sh/lib.sh'
201e268105SWilliam A. Kennington IIIelse
21*03206951SWilliam A. Kennington III  # shellcheck source=meta-google/recipes-google/test/test-sh/lib.sh
221e268105SWilliam A. Kennington III  source "$SYSROOT/usr/share/test/lib.sh"
231e268105SWilliam A. Kennington IIIfi
24*03206951SWilliam A. Kennington III# shellcheck source=meta-google/recipes-google/networking/network-sh/lib.sh
251e268105SWilliam A. Kennington IIIsource lib.sh
261e268105SWilliam A. Kennington III
2770264b98SWilliam A. Kennington IIIexpect_array_numeq() {
2870264b98SWilliam A. Kennington III  local -n a1="$1"
2970264b98SWilliam A. Kennington III  local -n a2="$2"
3070264b98SWilliam A. Kennington III
3170264b98SWilliam A. Kennington III  if (( "${#a1[@]}" != "${#a2[@]}" )); then
3270264b98SWilliam A. Kennington III    echo "  Line ${BASH_LINENO[0]} Array Size ${#a1[@]} != ${#a2[@]}" >&2
3370264b98SWilliam A. Kennington III    test_err=1
3470264b98SWilliam A. Kennington III  else
3570264b98SWilliam A. Kennington III    local i
3670264b98SWilliam A. Kennington III    for (( i=0; i < ${#a1[@]}; ++i )); do
3770264b98SWilliam A. Kennington III      expect_numeq "${a1[$i]}" "${a2[$i]}"
3870264b98SWilliam A. Kennington III    done
3970264b98SWilliam A. Kennington III  fi
4070264b98SWilliam A. Kennington III}
4170264b98SWilliam A. Kennington III
421e268105SWilliam A. Kennington IIItest_mac_to_bytes() {
431e268105SWilliam A. Kennington III  out=()
441e268105SWilliam A. Kennington III  expect_err 1 mac_to_bytes out ''
451e268105SWilliam A. Kennington III  expect_err 1 mac_to_bytes out '00'
461e268105SWilliam A. Kennington III  expect_err 1 mac_to_bytes out '12:34:56:78:90:'
471e268105SWilliam A. Kennington III  expect_err 1 mac_to_bytes out ':12:34:56:78:90'
481e268105SWilliam A. Kennington III  expect_err 1 mac_to_bytes out '12:34:56:78:90:0:'
491e268105SWilliam A. Kennington III  expect_err 1 mac_to_bytes out '12:34:56:78:90:0:2'
501e268105SWilliam A. Kennington III
511e268105SWilliam A. Kennington III  expect_err 0 mac_to_bytes out 'a2:0:f:de:0:29'
521e268105SWilliam A. Kennington III  expected=(0xa2 0 0xf 0xde 0 0x29)
5370264b98SWilliam A. Kennington III  expect_array_numeq out expected
541e268105SWilliam A. Kennington III}
551e268105SWilliam A. Kennington III
566ca70339SWilliam A. Kennington IIItest_mac_to_eui48() {
571e268105SWilliam A. Kennington III  str="$(mac_to_eui48 '12:34:56:78:90:af')" || fail
586ca70339SWilliam A. Kennington III  expect_streq "$str" '::1234:5678:90af'
591e268105SWilliam A. Kennington III}
601e268105SWilliam A. Kennington III
616ca70339SWilliam A. Kennington IIItest_mac_to_eui64() {
621e268105SWilliam A. Kennington III  str="$(mac_to_eui64 '12:34:56:78:90:af')" || fail
63fc8c436cSWilliam A. Kennington III  expect_streq "$str" '::1034:56ff:fe78:90af'
641e268105SWilliam A. Kennington III}
651e268105SWilliam A. Kennington III
6670264b98SWilliam A. Kennington IIItest_ip4_to_bytes() {
6770264b98SWilliam A. Kennington III  out=()
6870264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out ''
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 '.0.1.1'
7170264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out '10.0.0'
7270264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out '10.0..0'
7370264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out '.10.0.0.0'
7470264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out '10.0.0.0.'
7570264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out '10.0.0.256'
7670264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out '10.0.0.0.256'
7770264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out '10.0.0.0.1'
7870264b98SWilliam A. Kennington III
7970264b98SWilliam A. Kennington III  expect_err 0 ip_to_bytes out '10.0.0.1'
8070264b98SWilliam A. Kennington III  expected=(10 0 0 1)
8170264b98SWilliam A. Kennington III  expect_array_numeq out expected
8270264b98SWilliam A. Kennington III}
8370264b98SWilliam A. Kennington III
8470264b98SWilliam A. Kennington IIItest_ip6_to_bytes() {
8570264b98SWilliam A. Kennington III  out=()
8670264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out ''
8770264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out ':::'
8870264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out '::z'
8970264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out '1::1::1'
9070264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out '1:1:1'
9170264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out ':1::1'
9270264b98SWilliam A. Kennington III  expect_err 1 ip_to_bytes out '1::1:'
9370264b98SWilliam A. Kennington III
9470264b98SWilliam A. Kennington III  expect_err 0 ip_to_bytes out '::'
9570264b98SWilliam A. Kennington III  expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
9670264b98SWilliam A. Kennington III  expect_array_numeq out expected
9770264b98SWilliam A. Kennington III  out=()
9870264b98SWilliam A. Kennington III
9970264b98SWilliam A. Kennington III  expect_err 0 ip_to_bytes out '::1'
10070264b98SWilliam A. Kennington III  expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1)
10170264b98SWilliam A. Kennington III  expect_array_numeq out expected
10270264b98SWilliam A. Kennington III  out=()
10370264b98SWilliam A. Kennington III
10470264b98SWilliam A. Kennington III  expect_err 0 ip_to_bytes out 'fd00::'
10570264b98SWilliam A. Kennington III  expected=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
10670264b98SWilliam A. Kennington III  expect_array_numeq out expected
10770264b98SWilliam A. Kennington III  out=()
10870264b98SWilliam A. Kennington III
10970264b98SWilliam A. Kennington III  expect_err 0 ip_to_bytes out 'fd00:ffee::ddff:22'
11070264b98SWilliam A. Kennington III  expected=(0xfd 0 0xff 0xee 0 0 0 0 0 0 0 0 0xdd 0xff 0 0x22)
11170264b98SWilliam A. Kennington III  expect_array_numeq out expected
11270264b98SWilliam A. Kennington III  out=()
11370264b98SWilliam A. Kennington III
11470264b98SWilliam A. Kennington III  expect_err 0 ip_to_bytes out '1:2:3:4:5:6:7:8'
11570264b98SWilliam A. Kennington III  expected=(0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8)
11670264b98SWilliam A. Kennington III  expect_array_numeq out expected
117*03206951SWilliam A. Kennington III  # shellcheck disable=SC2034
11870264b98SWilliam A. Kennington III  out=()
11970264b98SWilliam A. Kennington III}
12070264b98SWilliam A. Kennington III
12180776788SWilliam A. Kennington IIItest_ip4_bytes_str() {
12280776788SWilliam A. Kennington III  in=(10 0 255 1)
12380776788SWilliam A. Kennington III  str="$(ip_bytes_to_str in)" || fail
12480776788SWilliam A. Kennington III  expect_streq "$str" '10.0.255.1'
12580776788SWilliam A. Kennington III}
12680776788SWilliam A. Kennington III
12780776788SWilliam A. Kennington IIItest_ip6_bytes_str() {
12880776788SWilliam A. Kennington III  in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
12980776788SWilliam A. Kennington III  str="$(ip_bytes_to_str in)" || fail
13080776788SWilliam A. Kennington III  expect_streq "$str" '::'
13180776788SWilliam A. Kennington III  in=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
13280776788SWilliam A. Kennington III  str="$(ip_bytes_to_str in)" || fail
13380776788SWilliam A. Kennington III  expect_streq "$str" 'fd00::'
13480776788SWilliam A. Kennington III  in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0xfd)
13580776788SWilliam A. Kennington III  str="$(ip_bytes_to_str in)" || fail
13680776788SWilliam A. Kennington III  expect_streq "$str" '::fd'
13780776788SWilliam A. Kennington III  in=(0xfd 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1)
13880776788SWilliam A. Kennington III  str="$(ip_bytes_to_str in)" || fail
13980776788SWilliam A. Kennington III  expect_streq "$str" 'fd01::1'
14080776788SWilliam A. Kennington III  in=(0xfd 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1)
14180776788SWilliam A. Kennington III  str="$(ip_bytes_to_str in)" || fail
14280776788SWilliam A. Kennington III  expect_streq "$str" 'fd01::1:0:0:1'
14380776788SWilliam A. Kennington III  in=(0xfd 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1)
14480776788SWilliam A. Kennington III  str="$(ip_bytes_to_str in)" || fail
14580776788SWilliam A. Kennington III  expect_streq "$str" 'fd01:0:0:1:1::1'
146*03206951SWilliam A. Kennington III  # shellcheck disable=SC2034
14780776788SWilliam A. Kennington III  in=(0 1 0 1 0xdd 0xdd 0 1 0 1 0 1 0 1 0 1)
14880776788SWilliam A. Kennington III  str="$(ip_bytes_to_str in)" || fail
14980776788SWilliam A. Kennington III  expect_streq "$str" '1:1:dddd:1:1:1:1:1'
15080776788SWilliam A. Kennington III}
15180776788SWilliam A. Kennington III
1526ca70339SWilliam A. Kennington IIItest_ip_pfx_concat() {
1531e268105SWilliam A. Kennington III  # Invalid inputs
1546ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_concat 'fd/64' '::1234:5678:90af'
1556ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_concat 'fd01::' '::1234:5678:90af'
1566ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_concat 'fd01:' '::1234:5678:90af'
1576ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_concat 'fd01::/a0' '::1234:5678:90af'
1586ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_concat 'fd01::/64' ':1234:5678:90af'
1596ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_concat 'fd01::/64' ''
1606ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_concat 'fd01::/129' '::1'
1611e268105SWilliam A. Kennington III
1621e268105SWilliam A. Kennington III  # Too many address bits
1636ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_concat 'fd01:1:1:1:1::/64' '::1234:5678:90af'
1646ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_concat 'fd01::/64' '::1:0:1234:5678:90af'
1656ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_concat 'fd01::/79' '::3:1234:5678:90af'
1666ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_concat 'fd01::/15' '::3:1234:5678:90af'
1676ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_concat '10.0.0.1/31' '0.0.0.0'
1681e268105SWilliam A. Kennington III
1696ca70339SWilliam A. Kennington III  str="$(ip_pfx_concat '::1/128' '::0')" || fail
1706ca70339SWilliam A. Kennington III  expect_streq "$str" '::1/128'
1716ca70339SWilliam A. Kennington III  str="$(ip_pfx_concat 'fd01::/64' '::1')" || fail
1721e268105SWilliam A. Kennington III  expect_streq "$str" 'fd01::1/64'
1736ca70339SWilliam A. Kennington III  str="$(ip_pfx_concat 'fd01::/127' '::1')" || fail
1746ca70339SWilliam A. Kennington III  expect_streq "$str" 'fd01::1/127'
1756ca70339SWilliam A. Kennington III  str="$(ip_pfx_concat 'fd02::/15' '::1')" || fail
1766ca70339SWilliam A. Kennington III  expect_streq "$str" 'fd02::1/15'
1776ca70339SWilliam A. Kennington III  str="$(ip_pfx_concat 'fd01::/72' '::1234:5678:90af')" || fail
1781e268105SWilliam A. Kennington III  expect_streq "$str" 'fd01::1234:5678:90af/72'
1796ca70339SWilliam A. Kennington III  str="$(ip_pfx_concat 'fd01:eeee:aaaa:cccc::/64' '::a:1234:5678:90af')" || fail
1801e268105SWilliam A. Kennington III  expect_streq "$str" 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/64'
1816ca70339SWilliam A. Kennington III  str="$(ip_pfx_concat 'fd01::fd00:0:0:0/80' '::1')" || fail
1826ca70339SWilliam A. Kennington III  expect_streq "$str" 'fd01::fd00:0:0:1/80'
1836ca70339SWilliam A. Kennington III
1846ca70339SWilliam A. Kennington III  str="$(ip_pfx_concat '10.0.0.0/24' '0.0.0.1')" || fail
1856ca70339SWilliam A. Kennington III  expect_streq "$str" '10.0.0.1/24'
1861e268105SWilliam A. Kennington III}
1871e268105SWilliam A. Kennington III
1886ca70339SWilliam A. Kennington IIItest_ip_pfx_to_cidr() {
1896ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_to_cidr 'z/64'
1906ca70339SWilliam A. Kennington III  expect_err 1 ip_pfx_to_cidr '64'
1911e268105SWilliam A. Kennington III
1926ca70339SWilliam A. Kennington III  cidr="$(ip_pfx_to_cidr 'fd01::/64')" || fail
1931e268105SWilliam A. Kennington III  expect_numeq "$cidr" 64
1946ca70339SWilliam A. Kennington III  cidr="$(ip_pfx_to_cidr 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/128')" || fail
1951e268105SWilliam A. Kennington III  expect_numeq "$cidr" 128
1966ca70339SWilliam A. Kennington III  cidr="$(ip_pfx_to_cidr '10.0.0.1/24')" || fail
1976ca70339SWilliam A. Kennington III  expect_numeq "$cidr" 24
1986ca70339SWilliam A. Kennington III}
1996ca70339SWilliam A. Kennington III
2006ca70339SWilliam A. Kennington IIItest_normalize_ip() {
2016ca70339SWilliam A. Kennington III  ip="$(normalize_ip 'fd01:1::0:0:1')" || fail
2026ca70339SWilliam A. Kennington III  expect_streq "$ip" 'fd01:1::1'
2031e268105SWilliam A. Kennington III}
2041e268105SWilliam A. Kennington III
2051e268105SWilliam A. Kennington IIImain
206