William A. Kennington III | e33ec59 | 2021-03-10 17:43:48 -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 | source gbmc-ip-monitor.sh |
| 18 | if [ -e ../gbmc-ip-monitor.bb ]; then |
| 19 | source '../../test/test-sh/lib.sh' |
| 20 | else |
| 21 | source "$SYSROOT/usr/share/test/lib.sh" |
| 22 | fi |
| 23 | |
| 24 | test_init_empty() { |
| 25 | ip() { |
| 26 | return 0 |
| 27 | } |
| 28 | str="$(gbmc_ip_monitor_generate_init)" || fail |
| 29 | expect_streq "$str" '[INIT]' |
| 30 | } |
| 31 | |
| 32 | test_init_link_populated() { |
| 33 | ip() { |
| 34 | if [ "$1" = 'link' ]; then |
| 35 | cat <<EOF |
| 36 | 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 |
| 37 | link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 |
| 38 | 2: eno2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 |
| 39 | link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff |
| 40 | altname enp0s31f6 |
| 41 | EOF |
| 42 | fi |
| 43 | return 0 |
| 44 | } |
| 45 | str="$(gbmc_ip_monitor_generate_init)" || fail |
| 46 | expect_streq "$str" <<EOF |
| 47 | [LINK]1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 |
| 48 | link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 |
| 49 | [LINK]2: eno2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 |
| 50 | link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff |
| 51 | altname enp0s31f6 |
| 52 | [INIT] |
| 53 | EOF |
| 54 | } |
| 55 | |
| 56 | test_init_addr_populated() { |
| 57 | ip() { |
| 58 | if [ "$1" = 'addr' ]; then |
| 59 | cat <<EOF |
| 60 | 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 |
| 61 | link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 |
| 62 | inet 127.0.0.1/8 scope host lo |
| 63 | valid_lft forever preferred_lft forever |
| 64 | inet6 ::1/128 scope host |
| 65 | valid_lft forever preferred_lft forever |
| 66 | 2: eno2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 |
| 67 | link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff |
| 68 | altname enp0s31f6 |
| 69 | inet 192.168.242.57/23 brd 192.168.243.255 scope global dynamic noprefixroute eno2 |
| 70 | valid_lft 83967sec preferred_lft 83967sec |
| 71 | inet6 fd01:ff2:5687:4:cf03:45f3:983a:96eb/64 scope global temporary dynamic |
| 72 | valid_lft 518788sec preferred_lft 183sec |
| 73 | EOF |
| 74 | fi |
| 75 | return 0 |
| 76 | } |
| 77 | str="$(gbmc_ip_monitor_generate_init)" || fail |
| 78 | expect_streq "$str" <<EOF |
| 79 | [ADDR]1: lo inet 127.0.0.1/8 scope host lo |
| 80 | [ADDR]1: lo inet6 ::1/128 scope host |
| 81 | [ADDR]2: eno2 inet 192.168.242.57/23 brd 192.168.243.255 scope global dynamic noprefixroute eno2 |
| 82 | [ADDR]2: eno2 inet6 fd01:ff2:5687:4:cf03:45f3:983a:96eb/64 scope global temporary dynamic |
| 83 | [INIT] |
| 84 | EOF |
| 85 | } |
| 86 | |
| 87 | test_init_route_populated() { |
| 88 | ip() { |
| 89 | if [ "$1" = "-4" -a "${2-}" = 'route' ]; then |
| 90 | cat <<EOF |
| 91 | default via 192.168.243.254 dev eno2 proto dhcp metric 100 |
| 92 | 192.168.242.0/23 dev eno2 proto kernel scope link src 192.168.242.57 metric 100 |
| 93 | EOF |
| 94 | elif [ "$1" = "-6" -a "${2-}" = 'route' ]; then |
| 95 | cat <<EOF |
| 96 | ::1 dev lo proto kernel metric 256 pref medium |
| 97 | fd01:ff2:5687:4::/64 dev eno2 proto ra metric 100 pref medium |
| 98 | fe80::/64 dev eno2 proto kernel metric 100 pref medium |
| 99 | EOF |
| 100 | fi |
| 101 | return 0 |
| 102 | } |
| 103 | str="$(gbmc_ip_monitor_generate_init)" || fail |
| 104 | expect_streq "$str" <<EOF |
| 105 | [ROUTE]default via 192.168.243.254 dev eno2 proto dhcp metric 100 |
| 106 | [ROUTE]192.168.242.0/23 dev eno2 proto kernel scope link src 192.168.242.57 metric 100 |
| 107 | [ROUTE]::1 dev lo proto kernel metric 256 pref medium |
| 108 | [ROUTE]fd01:ff2:5687:4::/64 dev eno2 proto ra metric 100 pref medium |
| 109 | [ROUTE]fe80::/64 dev eno2 proto kernel metric 100 pref medium |
| 110 | [INIT] |
| 111 | EOF |
| 112 | } |
| 113 | |
| 114 | testParseNonTag() { |
| 115 | expect_err 2 gbmc_ip_monitor_parse_line '' |
| 116 | expect_err 2 gbmc_ip_monitor_parse_line ' ' |
| 117 | expect_err 2 gbmc_ip_monitor_parse_line ' Data' |
| 118 | expect_err 2 gbmc_ip_monitor_parse_line ' [LINK]' |
| 119 | expect_err 2 gbmc_ip_monitor_parse_line ' [ROUTE]' |
| 120 | } |
| 121 | |
| 122 | testParseInit() { |
| 123 | expect_err 0 gbmc_ip_monitor_parse_line '[INIT]' |
| 124 | expect_streq "$change" 'init' |
| 125 | } |
| 126 | |
| 127 | testParseAddrAdd() { |
| 128 | expect_err 0 gbmc_ip_monitor_parse_line '[ADDR]2: eno2 inet6 fd01:ff2:5687:4:cf03:45f3:983a:96eb/128 scope global temporary dynamic' |
| 129 | expect_streq "$change" 'addr' |
| 130 | expect_streq "$action" 'add' |
| 131 | expect_streq "$intf" 'eno2' |
| 132 | expect_streq "$fam" 'inet6' |
| 133 | expect_streq "$ip" 'fd01:ff2:5687:4:cf03:45f3:983a:96eb' |
| 134 | expect_streq "$scope" 'global' |
| 135 | expect_streq "$flags" 'temporary dynamic' |
| 136 | } |
| 137 | |
| 138 | testParseAddrDel() { |
| 139 | expect_err 0 gbmc_ip_monitor_parse_line '[ADDR]Deleted 2: eno2 inet6 fe80::aaaa:aaff:feaa:aaaa/64 scope link' |
| 140 | expect_streq "$change" 'addr' |
| 141 | expect_streq "$action" 'del' |
| 142 | expect_streq "$intf" 'eno2' |
| 143 | expect_streq "$fam" 'inet6' |
| 144 | expect_streq "$ip" 'fe80::aaaa:aaff:feaa:aaaa' |
| 145 | expect_streq "$scope" 'link' |
| 146 | expect_streq "$flags" '' |
| 147 | } |
| 148 | |
| 149 | testParseRouteAdd() { |
| 150 | expect_err 0 gbmc_ip_monitor_parse_line "[ROUTE]fd01:ff2:5687:4::/64 dev eno2 proto ra metric 100 pref medium" |
| 151 | expect_streq "$change" 'route' |
| 152 | expect_streq "$action" 'add' |
| 153 | expect_streq "$route" 'fd01:ff2:5687:4::/64 dev eno2 proto ra metric 100 pref medium' |
| 154 | } |
| 155 | |
| 156 | testParseRouteDel() { |
| 157 | expect_err 0 gbmc_ip_monitor_parse_line "[ROUTE]Deleted fd01:ff2:5687:4::/64 dev eno2 proto ra metric 100 pref medium" |
| 158 | expect_streq "$change" 'route' |
| 159 | expect_streq "$action" 'del' |
| 160 | expect_streq "$route" 'fd01:ff2:5687:4::/64 dev eno2 proto ra metric 100 pref medium' |
| 161 | } |
| 162 | |
| 163 | testParseLinkAdd() { |
| 164 | expect_err 0 gbmc_ip_monitor_parse_line "[LINK]2: eno2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000" \ |
| 165 | < <(echo 'link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff') |
| 166 | expect_streq "$change" 'link' |
| 167 | expect_streq "$action" 'add' |
| 168 | expect_streq "$intf" 'eno2' |
| 169 | expect_streq "$mac" 'aa:aa:aa:aa:aa:aa' |
| 170 | } |
| 171 | |
| 172 | testParseLinkDel() { |
| 173 | expect_err 0 gbmc_ip_monitor_parse_line "[LINK]Deleted 2: eno2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000" \ |
| 174 | < <(echo 'link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff') |
| 175 | expect_streq "$change" 'link' |
| 176 | expect_streq "$action" 'del' |
| 177 | expect_streq "$intf" 'eno2' |
| 178 | expect_streq "$mac" 'aa:aa:aa:aa:aa:aa' |
| 179 | } |
| 180 | |
| 181 | main |