xref: /openbmc/openbmc/meta-google/recipes-google/networking/network-sh/test.sh (revision f442540acb1cd478286a2c1899e4f3528c817684)
1#!/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")" || exit
17if [ -e ../network-sh.bb ]; then
18  # shellcheck source=meta-google/recipes-google/test/test-sh/lib.sh
19  source '../../test/test-sh/lib.sh'
20else
21  # shellcheck source=meta-google/recipes-google/test/test-sh/lib.sh
22  source "$SYSROOT/usr/share/test/lib.sh"
23fi
24# shellcheck source=meta-google/recipes-google/networking/network-sh/lib.sh
25source lib.sh
26
27expect_array_numeq() {
28  local -n a1="$1"
29  local -n a2="$2"
30
31  if (( "${#a1[@]}" != "${#a2[@]}" )); then
32    echo "  Line ${BASH_LINENO[0]} Array Size ${#a1[@]} != ${#a2[@]}" >&2
33    test_err=1
34  else
35    local i
36    for (( i=0; i < ${#a1[@]}; ++i )); do
37      expect_numeq "${a1[$i]}" "${a2[$i]}"
38    done
39  fi
40}
41
42test_mac_to_bytes() {
43  out=()
44  expect_err 1 mac_to_bytes out ''
45  expect_err 1 mac_to_bytes out '00'
46  expect_err 1 mac_to_bytes out '12:34:56:78:90:'
47  expect_err 1 mac_to_bytes out ':12:34:56:78:90'
48  expect_err 1 mac_to_bytes out '12:34:56:78:90:0:'
49  expect_err 1 mac_to_bytes out '12:34:56:78:90:0:2'
50
51  expect_err 0 mac_to_bytes out 'a2:0:f:de:0:29'
52  expected=(0xa2 0 0xf 0xde 0 0x29)
53  expect_array_numeq out expected
54}
55
56test_mac_to_eui48() {
57  str="$(mac_to_eui48 '12:34:56:78:90:af')" || fail
58  expect_streq "$str" '::1234:5678:90af'
59}
60
61test_mac_to_eui64() {
62  str="$(mac_to_eui64 '12:34:56:78:90:af')" || fail
63  expect_streq "$str" '::1034:56ff:fe78:90af'
64}
65
66test_mac_to_eui64_tweak() {
67  str="$(mac_to_eui64 '12:34:56:78:90:af' '0x11' '0x22')" || fail
68  expect_streq "$str" '::1034:5611:2278:90af'
69}
70
71test_ip4_to_bytes() {
72  out=()
73  expect_err 1 ip_to_bytes out ''
74  expect_err 1 ip_to_bytes out '10.0.0.'
75  expect_err 1 ip_to_bytes out '.0.1.1'
76  expect_err 1 ip_to_bytes out '10.0.0'
77  expect_err 1 ip_to_bytes out '10.0..0'
78  expect_err 1 ip_to_bytes out '.10.0.0.0'
79  expect_err 1 ip_to_bytes out '10.0.0.0.'
80  expect_err 1 ip_to_bytes out '10.0.0.256'
81  expect_err 1 ip_to_bytes out '10.0.0.0.256'
82  expect_err 1 ip_to_bytes out '10.0.0.0.1'
83
84  expect_err 0 ip_to_bytes out '10.0.0.1'
85  expected=(10 0 0 1)
86  expect_array_numeq out expected
87}
88
89test_ip6_to_bytes() {
90  out=()
91  expect_err 1 ip_to_bytes out ''
92  expect_err 1 ip_to_bytes out ':::'
93  expect_err 1 ip_to_bytes out '::z'
94  expect_err 1 ip_to_bytes out '1::1::1'
95  expect_err 1 ip_to_bytes out '1:1:1'
96  expect_err 1 ip_to_bytes out ':1::1'
97  expect_err 1 ip_to_bytes out '1::1:'
98
99  expect_err 0 ip_to_bytes out '::'
100  expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
101  expect_array_numeq out expected
102  out=()
103
104  expect_err 0 ip_to_bytes out '::1'
105  expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1)
106  expect_array_numeq out expected
107  out=()
108
109  expect_err 0 ip_to_bytes out 'fd00::'
110  expected=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
111  expect_array_numeq out expected
112  out=()
113
114  expect_err 0 ip_to_bytes out 'fd00:ffee::ddff:22'
115  expected=(0xfd 0 0xff 0xee 0 0 0 0 0 0 0 0 0xdd 0xff 0 0x22)
116  expect_array_numeq out expected
117  out=()
118
119  expect_err 0 ip_to_bytes out '1:2:3:4:5:6:7:8'
120  expected=(0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8)
121  expect_array_numeq out expected
122  # shellcheck disable=SC2034
123  out=()
124}
125
126test_ip4_bytes_str() {
127  in=(10 0 255 1)
128  str="$(ip_bytes_to_str in)" || fail
129  expect_streq "$str" '10.0.255.1'
130}
131
132test_ip6_bytes_str() {
133  in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
134  str="$(ip_bytes_to_str in)" || fail
135  expect_streq "$str" '::'
136  in=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
137  str="$(ip_bytes_to_str in)" || fail
138  expect_streq "$str" 'fd00::'
139  in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0xfd)
140  str="$(ip_bytes_to_str in)" || fail
141  expect_streq "$str" '::fd'
142  in=(0xfd 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1)
143  str="$(ip_bytes_to_str in)" || fail
144  expect_streq "$str" 'fd01::1'
145  in=(0xfd 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1)
146  str="$(ip_bytes_to_str in)" || fail
147  expect_streq "$str" 'fd01::1:0:0:1'
148  in=(0xfd 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1)
149  str="$(ip_bytes_to_str in)" || fail
150  expect_streq "$str" 'fd01:0:0:1:1::1'
151  # shellcheck disable=SC2034
152  in=(0 1 0 1 0xdd 0xdd 0 1 0 1 0 1 0 1 0 1)
153  str="$(ip_bytes_to_str in)" || fail
154  expect_streq "$str" '1:1:dddd:1:1:1:1:1'
155}
156
157test_ip_pfx_concat() {
158  # Invalid inputs
159  expect_err 1 ip_pfx_concat 'fd/64' '::1234:5678:90af'
160  expect_err 1 ip_pfx_concat 'fd01::' '::1234:5678:90af'
161  expect_err 1 ip_pfx_concat 'fd01:' '::1234:5678:90af'
162  expect_err 1 ip_pfx_concat 'fd01::/a0' '::1234:5678:90af'
163  expect_err 1 ip_pfx_concat 'fd01::/64' ':1234:5678:90af'
164  expect_err 1 ip_pfx_concat 'fd01::/64' ''
165  expect_err 1 ip_pfx_concat 'fd01::/129' '::1'
166
167  # Too many address bits
168  expect_err 1 ip_pfx_concat 'fd01:1:1:1:1::/64' '::1234:5678:90af'
169  expect_err 1 ip_pfx_concat 'fd01::/64' '::1:0:1234:5678:90af'
170  expect_err 1 ip_pfx_concat 'fd01::/79' '::3:1234:5678:90af'
171  expect_err 1 ip_pfx_concat 'fd01::/15' '::3:1234:5678:90af'
172  expect_err 1 ip_pfx_concat '10.0.0.1/31' '0.0.0.0'
173
174  str="$(ip_pfx_concat '::1/128' '::0')" || fail
175  expect_streq "$str" '::1/128'
176  str="$(ip_pfx_concat 'fd01::/64' '::1')" || fail
177  expect_streq "$str" 'fd01::1/64'
178  str="$(ip_pfx_concat 'fd01::/127' '::1')" || fail
179  expect_streq "$str" 'fd01::1/127'
180  str="$(ip_pfx_concat 'fd02::/15' '::1')" || fail
181  expect_streq "$str" 'fd02::1/15'
182  str="$(ip_pfx_concat 'fd01::/72' '::1234:5678:90af')" || fail
183  expect_streq "$str" 'fd01::1234:5678:90af/72'
184  str="$(ip_pfx_concat 'fd01:eeee:aaaa:cccc::/64' '::a:1234:5678:90af')" || fail
185  expect_streq "$str" 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/64'
186  str="$(ip_pfx_concat 'fd01::fd00:0:0:0/80' '::1')" || fail
187  expect_streq "$str" 'fd01::fd00:0:0:1/80'
188
189  str="$(ip_pfx_concat '10.0.0.0/24' '0.0.0.1')" || fail
190  expect_streq "$str" '10.0.0.1/24'
191}
192
193test_ip_pfx_to_cidr() {
194  expect_err 1 ip_pfx_to_cidr 'z/64'
195  expect_err 1 ip_pfx_to_cidr '64'
196
197  cidr="$(ip_pfx_to_cidr 'fd01::/64')" || fail
198  expect_numeq "$cidr" 64
199  cidr="$(ip_pfx_to_cidr 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/128')" || fail
200  expect_numeq "$cidr" 128
201  cidr="$(ip_pfx_to_cidr '10.0.0.1/24')" || fail
202  expect_numeq "$cidr" 24
203}
204
205test_normalize_ip() {
206  ip="$(normalize_ip 'fd01:1::0:0:1')" || fail
207  expect_streq "$ip" 'fd01:1::1'
208}
209
210main
211