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_ip4_to_bytes() {
67  out=()
68  expect_err 1 ip_to_bytes out ''
69  expect_err 1 ip_to_bytes out '10.0.0.'
70  expect_err 1 ip_to_bytes out '.0.1.1'
71  expect_err 1 ip_to_bytes out '10.0.0'
72  expect_err 1 ip_to_bytes out '10.0..0'
73  expect_err 1 ip_to_bytes out '.10.0.0.0'
74  expect_err 1 ip_to_bytes out '10.0.0.0.'
75  expect_err 1 ip_to_bytes out '10.0.0.256'
76  expect_err 1 ip_to_bytes out '10.0.0.0.256'
77  expect_err 1 ip_to_bytes out '10.0.0.0.1'
78
79  expect_err 0 ip_to_bytes out '10.0.0.1'
80  expected=(10 0 0 1)
81  expect_array_numeq out expected
82}
83
84test_ip6_to_bytes() {
85  out=()
86  expect_err 1 ip_to_bytes out ''
87  expect_err 1 ip_to_bytes out ':::'
88  expect_err 1 ip_to_bytes out '::z'
89  expect_err 1 ip_to_bytes out '1::1::1'
90  expect_err 1 ip_to_bytes out '1:1:1'
91  expect_err 1 ip_to_bytes out ':1::1'
92  expect_err 1 ip_to_bytes out '1::1:'
93
94  expect_err 0 ip_to_bytes out '::'
95  expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
96  expect_array_numeq out expected
97  out=()
98
99  expect_err 0 ip_to_bytes out '::1'
100  expected=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1)
101  expect_array_numeq out expected
102  out=()
103
104  expect_err 0 ip_to_bytes out 'fd00::'
105  expected=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
106  expect_array_numeq out expected
107  out=()
108
109  expect_err 0 ip_to_bytes out 'fd00:ffee::ddff:22'
110  expected=(0xfd 0 0xff 0xee 0 0 0 0 0 0 0 0 0xdd 0xff 0 0x22)
111  expect_array_numeq out expected
112  out=()
113
114  expect_err 0 ip_to_bytes out '1:2:3:4:5:6:7:8'
115  expected=(0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8)
116  expect_array_numeq out expected
117  # shellcheck disable=SC2034
118  out=()
119}
120
121test_ip4_bytes_str() {
122  in=(10 0 255 1)
123  str="$(ip_bytes_to_str in)" || fail
124  expect_streq "$str" '10.0.255.1'
125}
126
127test_ip6_bytes_str() {
128  in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
129  str="$(ip_bytes_to_str in)" || fail
130  expect_streq "$str" '::'
131  in=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
132  str="$(ip_bytes_to_str in)" || fail
133  expect_streq "$str" 'fd00::'
134  in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0xfd)
135  str="$(ip_bytes_to_str in)" || fail
136  expect_streq "$str" '::fd'
137  in=(0xfd 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1)
138  str="$(ip_bytes_to_str in)" || fail
139  expect_streq "$str" 'fd01::1'
140  in=(0xfd 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1)
141  str="$(ip_bytes_to_str in)" || fail
142  expect_streq "$str" 'fd01::1:0:0:1'
143  in=(0xfd 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1)
144  str="$(ip_bytes_to_str in)" || fail
145  expect_streq "$str" 'fd01:0:0:1:1::1'
146  # shellcheck disable=SC2034
147  in=(0 1 0 1 0xdd 0xdd 0 1 0 1 0 1 0 1 0 1)
148  str="$(ip_bytes_to_str in)" || fail
149  expect_streq "$str" '1:1:dddd:1:1:1:1:1'
150}
151
152test_ip_pfx_concat() {
153  # Invalid inputs
154  expect_err 1 ip_pfx_concat 'fd/64' '::1234:5678:90af'
155  expect_err 1 ip_pfx_concat 'fd01::' '::1234:5678:90af'
156  expect_err 1 ip_pfx_concat 'fd01:' '::1234:5678:90af'
157  expect_err 1 ip_pfx_concat 'fd01::/a0' '::1234:5678:90af'
158  expect_err 1 ip_pfx_concat 'fd01::/64' ':1234:5678:90af'
159  expect_err 1 ip_pfx_concat 'fd01::/64' ''
160  expect_err 1 ip_pfx_concat 'fd01::/129' '::1'
161
162  # Too many address bits
163  expect_err 1 ip_pfx_concat 'fd01:1:1:1:1::/64' '::1234:5678:90af'
164  expect_err 1 ip_pfx_concat 'fd01::/64' '::1:0:1234:5678:90af'
165  expect_err 1 ip_pfx_concat 'fd01::/79' '::3:1234:5678:90af'
166  expect_err 1 ip_pfx_concat 'fd01::/15' '::3:1234:5678:90af'
167  expect_err 1 ip_pfx_concat '10.0.0.1/31' '0.0.0.0'
168
169  str="$(ip_pfx_concat '::1/128' '::0')" || fail
170  expect_streq "$str" '::1/128'
171  str="$(ip_pfx_concat 'fd01::/64' '::1')" || fail
172  expect_streq "$str" 'fd01::1/64'
173  str="$(ip_pfx_concat 'fd01::/127' '::1')" || fail
174  expect_streq "$str" 'fd01::1/127'
175  str="$(ip_pfx_concat 'fd02::/15' '::1')" || fail
176  expect_streq "$str" 'fd02::1/15'
177  str="$(ip_pfx_concat 'fd01::/72' '::1234:5678:90af')" || fail
178  expect_streq "$str" 'fd01::1234:5678:90af/72'
179  str="$(ip_pfx_concat 'fd01:eeee:aaaa:cccc::/64' '::a:1234:5678:90af')" || fail
180  expect_streq "$str" 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/64'
181  str="$(ip_pfx_concat 'fd01::fd00:0:0:0/80' '::1')" || fail
182  expect_streq "$str" 'fd01::fd00:0:0:1/80'
183
184  str="$(ip_pfx_concat '10.0.0.0/24' '0.0.0.1')" || fail
185  expect_streq "$str" '10.0.0.1/24'
186}
187
188test_ip_pfx_to_cidr() {
189  expect_err 1 ip_pfx_to_cidr 'z/64'
190  expect_err 1 ip_pfx_to_cidr '64'
191
192  cidr="$(ip_pfx_to_cidr 'fd01::/64')" || fail
193  expect_numeq "$cidr" 64
194  cidr="$(ip_pfx_to_cidr 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/128')" || fail
195  expect_numeq "$cidr" 128
196  cidr="$(ip_pfx_to_cidr '10.0.0.1/24')" || fail
197  expect_numeq "$cidr" 24
198}
199
200test_normalize_ip() {
201  ip="$(normalize_ip 'fd01:1::0:0:1')" || fail
202  expect_streq "$ip" 'fd01:1::1'
203}
204
205main
206