1#!/bin/bash
2# shellcheck disable=SC2317
3# Copyright 2021 Google LLC
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17cd "$(dirname "$0")" || exit
18if [ -e ../gbmc-ip-monitor.bb ]; then
19  # shellcheck source=meta-google/recipes-google/test/test-sh/lib.sh
20  source '../../test/test-sh/lib.sh'
21else
22  # shellcheck source=meta-google/recipes-google/test/test-sh/lib.sh
23  source "$SYSROOT/usr/share/test/lib.sh"
24fi
25# shellcheck source=meta-google/recipes-google/networking/files/gbmc-ip-monitor.sh
26source gbmc-ip-monitor.sh
27
28test_init_empty() {
29  ip() {
30    return 0
31  }
32  str="$(gbmc_ip_monitor_generate_init)" || fail
33  expect_streq "$str" '[INIT]'
34}
35
36test_init_link_populated() {
37  ip() {
38    if [ "$1" = 'link' ]; then
39      cat <<EOF
401: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
41    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
422: eno2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
43    link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff
44    altname enp0s31f6
45EOF
46    fi
47    return 0
48  }
49  str="$(gbmc_ip_monitor_generate_init)" || fail
50  expect_streq "$str" <<EOF
51[LINK]1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
52    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
53[LINK]2: eno2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
54    link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff
55    altname enp0s31f6
56[INIT]
57EOF
58}
59
60test_init_addr_populated() {
61  ip() {
62    if [ "$1" = 'addr' ]; then
63      cat <<EOF
641: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
65    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
66    inet 127.0.0.1/8 scope host lo
67       valid_lft forever preferred_lft forever
68    inet6 ::1/128 scope host
69       valid_lft forever preferred_lft forever
702: eno2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
71    link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff
72    altname enp0s31f6
73    inet 192.168.242.57/23 brd 192.168.243.255 scope global dynamic noprefixroute eno2
74       valid_lft 83967sec preferred_lft 83967sec
75    inet6 fd01:ff2:5687:4:cf03:45f3:983a:96eb/64 scope global temporary dynamic
76       valid_lft 518788sec preferred_lft 183sec
77EOF
78    fi
79    return 0
80  }
81  str="$(gbmc_ip_monitor_generate_init)" || fail
82  expect_streq "$str" <<EOF
83[ADDR]1: lo inet 127.0.0.1/8 scope host lo
84[ADDR]1: lo inet6 ::1/128 scope host
85[ADDR]2: eno2 inet 192.168.242.57/23 brd 192.168.243.255 scope global dynamic noprefixroute eno2
86[ADDR]2: eno2 inet6 fd01:ff2:5687:4:cf03:45f3:983a:96eb/64 scope global temporary dynamic
87[INIT]
88EOF
89}
90
91test_init_route_populated() {
92  ip() {
93    if [[ "$1" == "-4" && "${2-}" == 'route' ]]; then
94      cat <<EOF
95default via 192.168.243.254 dev eno2 proto dhcp metric 100
96192.168.242.0/23 dev eno2 proto kernel scope link src 192.168.242.57 metric 100
97EOF
98    elif [[ "$1" == "-6" && "${2-}" == 'route' ]]; then
99      cat <<EOF
100::1 dev lo proto kernel metric 256 pref medium
101fd01:ff2:5687:4::/64 dev eno2 proto ra metric 100 pref medium
102fe80::/64 dev eno2 proto kernel metric 100 pref medium
103EOF
104    fi
105    return 0
106  }
107  str="$(gbmc_ip_monitor_generate_init)" || fail
108  expect_streq "$str" <<EOF
109[ROUTE]default via 192.168.243.254 dev eno2 proto dhcp metric 100
110[ROUTE]192.168.242.0/23 dev eno2 proto kernel scope link src 192.168.242.57 metric 100
111[ROUTE]::1 dev lo proto kernel metric 256 pref medium
112[ROUTE]fd01:ff2:5687:4::/64 dev eno2 proto ra metric 100 pref medium
113[ROUTE]fe80::/64 dev eno2 proto kernel metric 100 pref medium
114[INIT]
115EOF
116}
117
118testParseNonTag() {
119  expect_err 2 gbmc_ip_monitor_parse_line ''
120  expect_err 2 gbmc_ip_monitor_parse_line '  '
121  expect_err 2 gbmc_ip_monitor_parse_line '  Data'
122  expect_err 2 gbmc_ip_monitor_parse_line '  [LINK]'
123  expect_err 2 gbmc_ip_monitor_parse_line '  [ROUTE]'
124}
125
126testParseInit() {
127  expect_err 0 gbmc_ip_monitor_parse_line '[INIT]'
128  expect_streq "$change" 'init'
129}
130
131testParseAddrAdd() {
132  expect_err 0 gbmc_ip_monitor_parse_line '[ADDR]2: eno2 inet6 fd01:ff2:5687:4:cf03:45f3:983a:96eb/128 metric 1024 scope global temporary dynamic'
133  expect_streq "$change" 'addr'
134  expect_streq "$action" 'add'
135  expect_streq "$intf" 'eno2'
136  expect_streq "$fam" 'inet6'
137  expect_streq "$ip" 'fd01:ff2:5687:4:cf03:45f3:983a:96eb'
138  expect_streq "$scope" 'global'
139  expect_streq "$flags" 'temporary dynamic'
140}
141
142testParseAddrDel() {
143  expect_err 0 gbmc_ip_monitor_parse_line '[ADDR]Deleted 2: eno2 inet6 fe80::aaaa:aaff:feaa:aaaa/64 scope link'
144  expect_streq "$change" 'addr'
145  expect_streq "$action" 'del'
146  expect_streq "$intf" 'eno2'
147  expect_streq "$fam" 'inet6'
148  expect_streq "$ip" 'fe80::aaaa:aaff:feaa:aaaa'
149  expect_streq "$scope" 'link'
150  expect_streq "$flags" ''
151}
152
153testParseRouteAdd() {
154  expect_err 0 gbmc_ip_monitor_parse_line "[ROUTE]fd01:ff2:5687:4::/64 dev eno2 proto ra metric 100 pref medium"
155  expect_streq "$change" 'route'
156  expect_streq "$action" 'add'
157  expect_streq "$route" 'fd01:ff2:5687:4::/64 dev eno2 proto ra metric 100 pref medium'
158}
159
160testParseRouteDel() {
161  expect_err 0 gbmc_ip_monitor_parse_line "[ROUTE]Deleted fd01:ff2:5687:4::/64 dev eno2 proto ra metric 100 pref medium"
162  expect_streq "$change" 'route'
163  expect_streq "$action" 'del'
164  expect_streq "$route" 'fd01:ff2:5687:4::/64 dev eno2 proto ra metric 100 pref medium'
165}
166
167testParseLinkAdd() {
168  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" \
169    < <(echo 'link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff')
170  expect_streq "$change" 'link'
171  expect_streq "$action" 'add'
172  expect_streq "$intf" 'eno2'
173  expect_streq "$mac" 'aa:aa:aa:aa:aa:aa'
174}
175
176testParseLinkDel() {
177  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" \
178    < <(echo 'link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff')
179  expect_streq "$change" 'link'
180  expect_streq "$action" 'del'
181  expect_streq "$intf" 'eno2'
182  expect_streq "$mac" 'aa:aa:aa:aa:aa:aa'
183}
184
185main
186