1*1e268105SWilliam A. Kennington III#!/bin/bash
2*1e268105SWilliam A. Kennington III# Copyright 2021 Google LLC
3*1e268105SWilliam A. Kennington III#
4*1e268105SWilliam A. Kennington III# Licensed under the Apache License, Version 2.0 (the "License");
5*1e268105SWilliam A. Kennington III# you may not use this file except in compliance with the License.
6*1e268105SWilliam A. Kennington III# You may obtain a copy of the License at
7*1e268105SWilliam A. Kennington III#
8*1e268105SWilliam A. Kennington III#      http://www.apache.org/licenses/LICENSE-2.0
9*1e268105SWilliam A. Kennington III#
10*1e268105SWilliam A. Kennington III# Unless required by applicable law or agreed to in writing, software
11*1e268105SWilliam A. Kennington III# distributed under the License is distributed on an "AS IS" BASIS,
12*1e268105SWilliam A. Kennington III# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*1e268105SWilliam A. Kennington III# See the License for the specific language governing permissions and
14*1e268105SWilliam A. Kennington III# limitations under the License.
15*1e268105SWilliam A. Kennington III
16*1e268105SWilliam A. Kennington IIIcd "$(dirname "$0")"
17*1e268105SWilliam A. Kennington IIIif [ -e ../network-sh.bb ]; then
18*1e268105SWilliam A. Kennington III  source '../../test/test-sh/lib.sh'
19*1e268105SWilliam A. Kennington IIIelse
20*1e268105SWilliam A. Kennington III  source "$SYSROOT/usr/share/test/lib.sh"
21*1e268105SWilliam A. Kennington IIIfi
22*1e268105SWilliam A. Kennington IIIsource lib.sh
23*1e268105SWilliam A. Kennington III
24*1e268105SWilliam A. Kennington IIItest_mac_to_bytes() {
25*1e268105SWilliam A. Kennington III  out=()
26*1e268105SWilliam A. Kennington III  expect_err 1 mac_to_bytes out ''
27*1e268105SWilliam A. Kennington III  expect_err 1 mac_to_bytes out '00'
28*1e268105SWilliam A. Kennington III  expect_err 1 mac_to_bytes out '12:34:56:78:90:'
29*1e268105SWilliam A. Kennington III  expect_err 1 mac_to_bytes out ':12:34:56:78:90'
30*1e268105SWilliam A. Kennington III  expect_err 1 mac_to_bytes out '12:34:56:78:90:0:'
31*1e268105SWilliam A. Kennington III  expect_err 1 mac_to_bytes out '12:34:56:78:90:0:2'
32*1e268105SWilliam A. Kennington III
33*1e268105SWilliam A. Kennington III  expect_err 0 mac_to_bytes out 'a2:0:f:de:0:29'
34*1e268105SWilliam A. Kennington III  expected=(0xa2 0 0xf 0xde 0 0x29)
35*1e268105SWilliam A. Kennington III  for (( i=0; i < ${#expected[@]}; ++i )); do
36*1e268105SWilliam A. Kennington III    expect_numeq "${out[$i]}" "${expected[$i]}"
37*1e268105SWilliam A. Kennington III  done
38*1e268105SWilliam A. Kennington III}
39*1e268105SWilliam A. Kennington III
40*1e268105SWilliam A. Kennington IIItest_mac_to_eui_48() {
41*1e268105SWilliam A. Kennington III  str="$(mac_to_eui48 '12:34:56:78:90:af')" || fail
42*1e268105SWilliam A. Kennington III  expect_streq "$str" '1234:5678:90af'
43*1e268105SWilliam A. Kennington III}
44*1e268105SWilliam A. Kennington III
45*1e268105SWilliam A. Kennington IIItest_eui_64() {
46*1e268105SWilliam A. Kennington III  str="$(mac_to_eui64 '12:34:56:78:90:af')" || fail
47*1e268105SWilliam A. Kennington III  expect_streq "$str" '1334:56ff:fe78:90af'
48*1e268105SWilliam A. Kennington III}
49*1e268105SWilliam A. Kennington III
50*1e268105SWilliam A. Kennington IIItest_ipv6_pfx_concat() {
51*1e268105SWilliam A. Kennington III  # Invalid inputs
52*1e268105SWilliam A. Kennington III  expect_err 1 ipv6_pfx_concat 'fd/64' '1234:5678:90af'
53*1e268105SWilliam A. Kennington III  expect_err 1 ipv6_pfx_concat 'fd01::' '1234:5678:90af'
54*1e268105SWilliam A. Kennington III  expect_err 1 ipv6_pfx_concat 'fd01:' '1234:5678:90af'
55*1e268105SWilliam A. Kennington III  expect_err 1 ipv6_pfx_concat 'fd01::/a0' '1234:5678:90af'
56*1e268105SWilliam A. Kennington III  expect_err 1 ipv6_pfx_concat 'fd01::/64' ':1234:5678:90af'
57*1e268105SWilliam A. Kennington III  expect_err 1 ipv6_pfx_concat 'fd01::/64' '::'
58*1e268105SWilliam A. Kennington III
59*1e268105SWilliam A. Kennington III  # Too many address bits
60*1e268105SWilliam A. Kennington III  expect_err 1 ipv6_pfx_concat 'fd01:1:1:1:1::/64' '1234:5678:90af'
61*1e268105SWilliam A. Kennington III  expect_err 1 ipv6_pfx_concat 'fd01::/64' '1:0:1234:5678:90af'
62*1e268105SWilliam A. Kennington III  expect_err 1 ipv6_pfx_concat 'fd01::/65' '1:1234:5678:90af'
63*1e268105SWilliam A. Kennington III  expect_err 1 ipv6_pfx_concat 'fd01::/72' '1:1234:5678:90af'
64*1e268105SWilliam A. Kennington III
65*1e268105SWilliam A. Kennington III  str="$(ipv6_pfx_concat 'fd01::/64' '1')" || fail
66*1e268105SWilliam A. Kennington III  expect_streq "$str" 'fd01::1/64'
67*1e268105SWilliam A. Kennington III  str="$(ipv6_pfx_concat 'fd01::/72' '1234:5678:90af')" || fail
68*1e268105SWilliam A. Kennington III  expect_streq "$str" 'fd01::1234:5678:90af/72'
69*1e268105SWilliam A. Kennington III  str="$(ipv6_pfx_concat 'fd01:eeee:aaaa:cccc::/64' 'a:1234:5678:90af')" || fail
70*1e268105SWilliam A. Kennington III  expect_streq "$str" 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/64'
71*1e268105SWilliam A. Kennington III}
72*1e268105SWilliam A. Kennington III
73*1e268105SWilliam A. Kennington IIItest_ipv6_pfx_to_cidr() {
74*1e268105SWilliam A. Kennington III  expect_err 1 ipv6_pfx_to_cidr 'z/64'
75*1e268105SWilliam A. Kennington III  expect_err 1 ipv6_pfx_to_cidr '64'
76*1e268105SWilliam A. Kennington III
77*1e268105SWilliam A. Kennington III  cidr="$(ipv6_pfx_to_cidr 'fd01::/64')" || fail
78*1e268105SWilliam A. Kennington III  expect_numeq "$cidr" 64
79*1e268105SWilliam A. Kennington III  cidr="$(ipv6_pfx_to_cidr 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/128')" || fail
80*1e268105SWilliam A. Kennington III  expect_numeq "$cidr" 128
81*1e268105SWilliam A. Kennington III}
82*1e268105SWilliam A. Kennington III
83*1e268105SWilliam A. Kennington IIIreturn 0 2>/dev/null
84*1e268105SWilliam A. Kennington IIImain
85