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