William A. Kennington III | 1e26810 | 2021-03-08 13:00:12 -0800 | [diff] [blame] | 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 | |
| 16 | cd "$(dirname "$0")" |
| 17 | if [ -e ../network-sh.bb ]; then |
| 18 | source '../../test/test-sh/lib.sh' |
| 19 | else |
| 20 | source "$SYSROOT/usr/share/test/lib.sh" |
| 21 | fi |
| 22 | source lib.sh |
| 23 | |
| 24 | test_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 | |
| 40 | test_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 | |
| 45 | test_eui_64() { |
| 46 | str="$(mac_to_eui64 '12:34:56:78:90:af')" || fail |
| 47 | expect_streq "$str" '1334:56ff:fe78:90af' |
| 48 | } |
| 49 | |
| 50 | test_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 | |
| 73 | test_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 | |
| 83 | return 0 2>/dev/null |
| 84 | main |