blob: 2803c097855be757ce8418ac83a1599dbc68c4d1 [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
William A. Kennington III6ca70332021-05-10 03:14:42 -070053test_mac_to_eui48() {
William A. Kennington III1e268102021-03-08 13:00:12 -080054 str="$(mac_to_eui48 '12:34:56:78:90:af')" || fail
William A. Kennington III6ca70332021-05-10 03:14:42 -070055 expect_streq "$str" '::1234:5678:90af'
William A. Kennington III1e268102021-03-08 13:00:12 -080056}
57
William A. Kennington III6ca70332021-05-10 03:14:42 -070058test_mac_to_eui64() {
William A. Kennington III1e268102021-03-08 13:00:12 -080059 str="$(mac_to_eui64 '12:34:56:78:90:af')" || fail
William A. Kennington III6ca70332021-05-10 03:14:42 -070060 expect_streq "$str" '::1334:56ff:fe78:90af'
William A. Kennington III1e268102021-03-08 13:00:12 -080061}
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 III80776782021-05-10 03:07:14 -0700117test_ip4_bytes_str() {
118 in=(10 0 255 1)
119 str="$(ip_bytes_to_str in)" || fail
120 expect_streq "$str" '10.0.255.1'
121}
122
123test_ip6_bytes_str() {
124 in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
125 str="$(ip_bytes_to_str in)" || fail
126 expect_streq "$str" '::'
127 in=(0xfd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
128 str="$(ip_bytes_to_str in)" || fail
129 expect_streq "$str" 'fd00::'
130 in=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0xfd)
131 str="$(ip_bytes_to_str in)" || fail
132 expect_streq "$str" '::fd'
133 in=(0xfd 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1)
134 str="$(ip_bytes_to_str in)" || fail
135 expect_streq "$str" 'fd01::1'
136 in=(0xfd 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1)
137 str="$(ip_bytes_to_str in)" || fail
138 expect_streq "$str" 'fd01::1:0:0:1'
139 in=(0xfd 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1)
140 str="$(ip_bytes_to_str in)" || fail
141 expect_streq "$str" 'fd01:0:0:1:1::1'
142 in=(0 1 0 1 0xdd 0xdd 0 1 0 1 0 1 0 1 0 1)
143 str="$(ip_bytes_to_str in)" || fail
144 expect_streq "$str" '1:1:dddd:1:1:1:1:1'
145}
146
William A. Kennington III6ca70332021-05-10 03:14:42 -0700147test_ip_pfx_concat() {
William A. Kennington III1e268102021-03-08 13:00:12 -0800148 # Invalid inputs
William A. Kennington III6ca70332021-05-10 03:14:42 -0700149 expect_err 1 ip_pfx_concat 'fd/64' '::1234:5678:90af'
150 expect_err 1 ip_pfx_concat 'fd01::' '::1234:5678:90af'
151 expect_err 1 ip_pfx_concat 'fd01:' '::1234:5678:90af'
152 expect_err 1 ip_pfx_concat 'fd01::/a0' '::1234:5678:90af'
153 expect_err 1 ip_pfx_concat 'fd01::/64' ':1234:5678:90af'
154 expect_err 1 ip_pfx_concat 'fd01::/64' ''
155 expect_err 1 ip_pfx_concat 'fd01::/129' '::1'
William A. Kennington III1e268102021-03-08 13:00:12 -0800156
157 # Too many address bits
William A. Kennington III6ca70332021-05-10 03:14:42 -0700158 expect_err 1 ip_pfx_concat 'fd01:1:1:1:1::/64' '::1234:5678:90af'
159 expect_err 1 ip_pfx_concat 'fd01::/64' '::1:0:1234:5678:90af'
160 expect_err 1 ip_pfx_concat 'fd01::/79' '::3:1234:5678:90af'
161 expect_err 1 ip_pfx_concat 'fd01::/15' '::3:1234:5678:90af'
162 expect_err 1 ip_pfx_concat '10.0.0.1/31' '0.0.0.0'
William A. Kennington III1e268102021-03-08 13:00:12 -0800163
William A. Kennington III6ca70332021-05-10 03:14:42 -0700164 str="$(ip_pfx_concat '::1/128' '::0')" || fail
165 expect_streq "$str" '::1/128'
166 str="$(ip_pfx_concat 'fd01::/64' '::1')" || fail
William A. Kennington III1e268102021-03-08 13:00:12 -0800167 expect_streq "$str" 'fd01::1/64'
William A. Kennington III6ca70332021-05-10 03:14:42 -0700168 str="$(ip_pfx_concat 'fd01::/127' '::1')" || fail
169 expect_streq "$str" 'fd01::1/127'
170 str="$(ip_pfx_concat 'fd02::/15' '::1')" || fail
171 expect_streq "$str" 'fd02::1/15'
172 str="$(ip_pfx_concat 'fd01::/72' '::1234:5678:90af')" || fail
William A. Kennington III1e268102021-03-08 13:00:12 -0800173 expect_streq "$str" 'fd01::1234:5678:90af/72'
William A. Kennington III6ca70332021-05-10 03:14:42 -0700174 str="$(ip_pfx_concat 'fd01:eeee:aaaa:cccc::/64' '::a:1234:5678:90af')" || fail
William A. Kennington III1e268102021-03-08 13:00:12 -0800175 expect_streq "$str" 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/64'
William A. Kennington III6ca70332021-05-10 03:14:42 -0700176 str="$(ip_pfx_concat 'fd01::fd00:0:0:0/80' '::1')" || fail
177 expect_streq "$str" 'fd01::fd00:0:0:1/80'
178
179 str="$(ip_pfx_concat '10.0.0.0/24' '0.0.0.1')" || fail
180 expect_streq "$str" '10.0.0.1/24'
William A. Kennington III1e268102021-03-08 13:00:12 -0800181}
182
William A. Kennington III6ca70332021-05-10 03:14:42 -0700183test_ip_pfx_to_cidr() {
184 expect_err 1 ip_pfx_to_cidr 'z/64'
185 expect_err 1 ip_pfx_to_cidr '64'
William A. Kennington III1e268102021-03-08 13:00:12 -0800186
William A. Kennington III6ca70332021-05-10 03:14:42 -0700187 cidr="$(ip_pfx_to_cidr 'fd01::/64')" || fail
William A. Kennington III1e268102021-03-08 13:00:12 -0800188 expect_numeq "$cidr" 64
William A. Kennington III6ca70332021-05-10 03:14:42 -0700189 cidr="$(ip_pfx_to_cidr 'fd01:eeee:aaaa:cccc:a:1234:5678:90af/128')" || fail
William A. Kennington III1e268102021-03-08 13:00:12 -0800190 expect_numeq "$cidr" 128
William A. Kennington III6ca70332021-05-10 03:14:42 -0700191 cidr="$(ip_pfx_to_cidr '10.0.0.1/24')" || fail
192 expect_numeq "$cidr" 24
193}
194
195test_normalize_ip() {
196 ip="$(normalize_ip 'fd01:1::0:0:1')" || fail
197 expect_streq "$ip" 'fd01:1::1'
William A. Kennington III1e268102021-03-08 13:00:12 -0800198}
199
200return 0 2>/dev/null
201main