blob: 2361cfe3e87fa416903739efb2aa8be1c267247f [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
William A. Kennington III70264b92021-05-07 03:07:31 -070024expect_array_numeq() {
25 local -n a1="$1"
26 local -n a2="$2"
27
28 if (( "${#a1[@]}" != "${#a2[@]}" )); then
29 echo " Line ${BASH_LINENO[0]} Array Size ${#a1[@]} != ${#a2[@]}" >&2
30 test_err=1
31 else
32 local i
33 for (( i=0; i < ${#a1[@]}; ++i )); do
34 expect_numeq "${a1[$i]}" "${a2[$i]}"
35 done
36 fi
37}
38
William A. Kennington III1e268102021-03-08 13:00:12 -080039test_mac_to_bytes() {
40 out=()
41 expect_err 1 mac_to_bytes out ''
42 expect_err 1 mac_to_bytes out '00'
43 expect_err 1 mac_to_bytes out '12:34:56:78:90:'
44 expect_err 1 mac_to_bytes out ':12:34:56:78:90'
45 expect_err 1 mac_to_bytes out '12:34:56:78:90:0:'
46 expect_err 1 mac_to_bytes out '12:34:56:78:90:0:2'
47
48 expect_err 0 mac_to_bytes out 'a2:0:f:de:0:29'
49 expected=(0xa2 0 0xf 0xde 0 0x29)
William A. Kennington III70264b92021-05-07 03:07:31 -070050 expect_array_numeq out expected
William A. Kennington III1e268102021-03-08 13:00:12 -080051}
52
53test_mac_to_eui_48() {
54 str="$(mac_to_eui48 '12:34:56:78:90:af')" || fail
55 expect_streq "$str" '1234:5678:90af'
56}
57
58test_eui_64() {
59 str="$(mac_to_eui64 '12:34:56:78:90:af')" || fail
60 expect_streq "$str" '1334:56ff:fe78:90af'
61}
62
William A. Kennington III70264b92021-05-07 03:07:31 -070063test_ip4_to_bytes() {
64 out=()
65 expect_err 1 ip_to_bytes out ''
66 expect_err 1 ip_to_bytes out '10.0.0.'
67 expect_err 1 ip_to_bytes out '.0.1.1'
68 expect_err 1 ip_to_bytes out '10.0.0'
69 expect_err 1 ip_to_bytes out '10.0..0'
70 expect_err 1 ip_to_bytes out '.10.0.0.0'
71 expect_err 1 ip_to_bytes out '10.0.0.0.'
72 expect_err 1 ip_to_bytes out '10.0.0.256'
73 expect_err 1 ip_to_bytes out '10.0.0.0.256'
74 expect_err 1 ip_to_bytes out '10.0.0.0.1'
75
76 expect_err 0 ip_to_bytes out '10.0.0.1'
77 expected=(10 0 0 1)
78 expect_array_numeq out expected
79}
80
81test_ip6_to_bytes() {
82 out=()
83 expect_err 1 ip_to_bytes out ''
84 expect_err 1 ip_to_bytes out ':::'
85 expect_err 1 ip_to_bytes out '::z'
86 expect_err 1 ip_to_bytes out '1::1::1'
87 expect_err 1 ip_to_bytes out '1:1:1'
88 expect_err 1 ip_to_bytes out ':1::1'
89 expect_err 1 ip_to_bytes out '1::1:'
90
91 expect_err 0 ip_to_bytes out '::'
92 expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
93 expect_array_numeq out expected
94 out=()
95
96 expect_err 0 ip_to_bytes out '::1'
97 expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1)
98 expect_array_numeq out expected
99 out=()
100
101 expect_err 0 ip_to_bytes out 'fd00::'
102 expected=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
103 expect_array_numeq out expected
104 out=()
105
106 expect_err 0 ip_to_bytes out 'fd00:ffee::ddff:22'
107 expected=(0xfd 0 0xff 0xee 0 0 0 0 0 0 0 0 0xdd 0xff 0 0x22)
108 expect_array_numeq out expected
109 out=()
110
111 expect_err 0 ip_to_bytes out '1:2:3:4:5:6:7:8'
112 expected=(0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8)
113 expect_array_numeq out expected
114 out=()
115}
116
William A. Kennington III1e268102021-03-08 13:00:12 -0800117test_ipv6_pfx_concat() {
118 # Invalid inputs
119 expect_err 1 ipv6_pfx_concat 'fd/64' '1234:5678:90af'
120 expect_err 1 ipv6_pfx_concat 'fd01::' '1234:5678:90af'
121 expect_err 1 ipv6_pfx_concat 'fd01:' '1234:5678:90af'
122 expect_err 1 ipv6_pfx_concat 'fd01::/a0' '1234:5678:90af'
123 expect_err 1 ipv6_pfx_concat 'fd01::/64' ':1234:5678:90af'
124 expect_err 1 ipv6_pfx_concat 'fd01::/64' '::'
125
126 # Too many address bits
127 expect_err 1 ipv6_pfx_concat 'fd01:1:1:1:1::/64' '1234:5678:90af'
128 expect_err 1 ipv6_pfx_concat 'fd01::/64' '1:0:1234:5678:90af'
129 expect_err 1 ipv6_pfx_concat 'fd01::/65' '1:1234:5678:90af'
130 expect_err 1 ipv6_pfx_concat 'fd01::/72' '1:1234:5678:90af'
131
132 str="$(ipv6_pfx_concat 'fd01::/64' '1')" || fail
133 expect_streq "$str" 'fd01::1/64'
134 str="$(ipv6_pfx_concat 'fd01::/72' '1234:5678:90af')" || fail
135 expect_streq "$str" 'fd01::1234:5678:90af/72'
136 str="$(ipv6_pfx_concat 'fd01:eeee:aaaa:cccc::/64' 'a:1234:5678:90af')" || fail
137 expect_streq "$str" 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/64'
138}
139
140test_ipv6_pfx_to_cidr() {
141 expect_err 1 ipv6_pfx_to_cidr 'z/64'
142 expect_err 1 ipv6_pfx_to_cidr '64'
143
144 cidr="$(ipv6_pfx_to_cidr 'fd01::/64')" || fail
145 expect_numeq "$cidr" 64
146 cidr="$(ipv6_pfx_to_cidr 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/128')" || fail
147 expect_numeq "$cidr" 128
148}
149
150return 0 2>/dev/null
151main