summaryrefslogtreecommitdiffstats
path: root/net/pbr/tests/06_network/01_gateway_discovery
blob: 0a71d70a985564485d1ee3c084e0e7aa32525ef2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/bin/bash
# shunit2 tests for pbr_get_gateway4 and pbr_get_gateway6 gateway-discovery.
# Covers: successful discovery via each code path, sentinel "empty" values,
# warningInterfaceRoutingUnknownGateway when every fallback produces no
# gateway, and point-to-point suppression of that warning — for both stacks.

# ---------------------------------------------------------------------------
# Load both functions under test with all external symbols stubbed
# ---------------------------------------------------------------------------

_load_functions_under_test() {
	# Uplink-remap guards – both return 1 so the iface argument passes through.
	is_uplink6() { return 1; }
	is_uplink4() { return 1; }

	# Gateway-lookup stubs – safe no-ops; individual tests override.
	network_get_gateway()  { return 0; }
	network_get_gateway6() { return 0; }

	# json stub – individual tests override; overridden again in setUp.
	json() { :; }

	# --- IPv4 ---
	pbr_get_gateway4() {
		local iface="$2" dev="$3" gw
		is_uplink6 "$iface" && iface="$uplink_interface4"
		network_get_gateway gw "$iface" true
		if [ -z "$gw" ] || [ "$gw" = '0.0.0.0' ]; then
			gw="$(ip -4 route show dev "$dev" table all 2>/dev/null | awk '$1=="default" {for (i=1; i<NF; i++) if ($i=="via") {print $(i+1); exit}}')"
			[ -z "$gw" ] && gw="$(ip -4 route show dev "$dev" table all 2>/dev/null | awk '{for (i=1; i<=NF; i++) if ($i=="via") {print $(i+1); exit}}')"
			[ -z "$gw" ] && gw="$(ip -4 route get 1.1.1.1 oif "$dev" 2>/dev/null | awk '/via/ {print $3; exit}')"
			{ [ -z "$gw" ] && ! ip address show dev "$dev" 2>/dev/null | grep -q "POINTOPOINT"; } && json add warning 'warningInterfaceRoutingUnknownGateway' "$dev"
		fi
		eval "$1"='$gw'
	}

	# --- IPv6 ---
	pbr_get_gateway6() {
		local iface="$2" dev="$3" gw
		is_uplink4 "$iface" && iface="$uplink_interface6"
		network_get_gateway6 gw "$iface" true
		if [ -z "$gw" ] || [ "$gw" = '::/0' ] || [ "$gw" = '::0/0' ] || [ "$gw" = '::' ]; then
			gw="$(ip -6 route show dev "$dev" table all 2>/dev/null | awk '$1=="default" {for (i=1; i<NF; i++) if ($i=="via") {print $(i+1); exit}}')"
			[ -z "$gw" ] && gw="$(ip -6 route show dev "$dev" table all 2>/dev/null | awk '{for (i=1; i<=NF; i++) if ($i=="via") {print $(i+1); exit}}')"
			[ -z "$gw" ] && gw="$(ip -6 neigh show dev "$dev" 2>/dev/null | awk '/^fe80:.*router/ {print $1; exit}')"
			{ [ -z "$gw" ] && ! ip address show dev "$dev" 2>/dev/null | grep -q "POINTOPOINT"; } && json add warning 'warningInterfaceRoutingUnknownGateway' "$dev"
		fi
		eval "$1"='$gw'
	}
}

# ---------------------------------------------------------------------------
# Per-test setup
# ---------------------------------------------------------------------------

setUp() {
	_JSON_CALLS=""
	_GW_RESULT=""

	network_get_gateway()  { return 0; }
	network_get_gateway6() { return 0; }
	ip()                   { :; }
	json()                 { _JSON_CALLS="${_JSON_CALLS}$*|"; }
}

# ===========================================================================
# IPv4 tests  (prefix: test_4_)
# ===========================================================================

# ---------------------------------------------------------------------------
# Happy path: network_get_gateway returns a valid address
# ---------------------------------------------------------------------------

test_4_successful_gateway_via_network_get_gateway() {
	network_get_gateway() { eval "$1='192.168.1.1'"; }

	pbr_get_gateway4 _GW_RESULT eth0 eth0

	assertEquals "gw should be populated"      "192.168.1.1" "$_GW_RESULT"
	assertEquals "no warning should be emitted" ""            "$_JSON_CALLS"
}

# ---------------------------------------------------------------------------
# Sentinel '0.0.0.0' falls through; ip -4 route default awk finds a gateway
# ---------------------------------------------------------------------------

test_4_sentinel_zero_falls_through_to_route_default() {
	network_get_gateway() { eval "$1='0.0.0.0'"; }

	ip() { [ "$1 $2" = "-4 route" ] && echo "default via 10.0.0.1 dev eth0"; }

	pbr_get_gateway4 _GW_RESULT eth0 eth0

	assertEquals "gw should be 10.0.0.1"       "10.0.0.1" "$_GW_RESULT"
	assertEquals "no warning should be emitted" ""         "$_JSON_CALLS"
}

# ---------------------------------------------------------------------------
# First fallback: ip -4 route show default awk finds a gateway
# ---------------------------------------------------------------------------

test_4_successful_gateway_via_ip4_route_default() {
	ip() { [ "$1 $2" = "-4 route" ] && echo "default via 10.0.0.1 dev eth0"; }

	pbr_get_gateway4 _GW_RESULT eth0 eth0

	assertEquals "gw should be 10.0.0.1"       "10.0.0.1" "$_GW_RESULT"
	assertEquals "no warning should be emitted" ""         "$_JSON_CALLS"
}

# ---------------------------------------------------------------------------
# Second fallback: no default keyword; any-via awk finds a gateway
# ---------------------------------------------------------------------------

test_4_successful_gateway_via_ip4_route_any_via() {
	ip() { [ "$1 $2" = "-4 route" ] && echo "10.0.0.0/24 via 172.16.0.1 dev eth0"; }

	pbr_get_gateway4 _GW_RESULT eth0 eth0

	assertEquals "gw should be 172.16.0.1"     "172.16.0.1" "$_GW_RESULT"
	assertEquals "no warning should be emitted" ""           "$_JSON_CALLS"
}

# ---------------------------------------------------------------------------
# Third fallback: ip -4 route get succeeds
# ---------------------------------------------------------------------------

test_4_successful_gateway_via_ip4_route_get() {
	ip() {
		case "$1 $2 $3" in
			"-4 route show") : ;;
			"-4 route get")  echo "1.1.1.1 via 192.168.50.1 dev eth0 src 192.168.50.10" ;;
		esac
	}

	pbr_get_gateway4 _GW_RESULT eth0 eth0

	assertEquals "gw should be 192.168.50.1"   "192.168.50.1" "$_GW_RESULT"
	assertEquals "no warning should be emitted" ""             "$_JSON_CALLS"
}

# ---------------------------------------------------------------------------
# All fallbacks fail; interface is NOT point-to-point → warning must fire
# ---------------------------------------------------------------------------

test_4_warning_emitted_when_no_gateway_and_not_ptp() {
	ip() {
		case "$1 $2" in
			"-4 route")     : ;;
			"address show") echo "2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>" ;;
		esac
	}

	pbr_get_gateway4 _GW_RESULT eth0 eth0

	assertEquals "gw result should be empty" "" "$_GW_RESULT"
	echo "$_JSON_CALLS" | grep -q "warningInterfaceRoutingUnknownGateway"
	assertTrue "warning token should be present in json call" $?
	echo "$_JSON_CALLS" | grep -q "eth0"
	assertTrue "device name should appear in json call" $?
}

# ---------------------------------------------------------------------------
# All fallbacks fail; interface IS point-to-point → no warning
# ---------------------------------------------------------------------------

test_4_no_warning_when_no_gateway_but_ptp_interface() {
	ip() {
		case "$1 $2" in
			"-4 route")     : ;;
			"address show") echo "3: ppp0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP>" ;;
		esac
	}

	pbr_get_gateway4 _GW_RESULT ppp0 ppp0

	assertEquals "gw result should be empty"         "" "$_GW_RESULT"
	assertEquals "no warning for point-to-point link" "" "$_JSON_CALLS"
}

# ---------------------------------------------------------------------------
# Sentinel '0.0.0.0' from network_get_gateway, all fallbacks fail → warning
# ---------------------------------------------------------------------------

test_4_warning_emitted_when_gw_is_zero_and_fallbacks_fail() {
	network_get_gateway() { eval "$1='0.0.0.0'"; }

	ip() {
		case "$1 $2" in
			"-4 route")     : ;;
			"address show") echo "2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP>" ;;
		esac
	}

	pbr_get_gateway4 _GW_RESULT eth1 eth1

	assertEquals "gw result should be empty" "" "$_GW_RESULT"
	echo "$_JSON_CALLS" | grep -q "warningInterfaceRoutingUnknownGateway"
	assertTrue "warning token should be present in json call" $?
	echo "$_JSON_CALLS" | grep -q "eth1"
	assertTrue "device name should appear in json call" $?
}

# ===========================================================================
# IPv6 tests  (prefix: test_6_)
# ===========================================================================

# ---------------------------------------------------------------------------
# Happy path: network_get_gateway6 returns a valid global unicast address
# ---------------------------------------------------------------------------

test_6_successful_gateway_via_network_get_gateway6() {
	network_get_gateway6() { eval "$1='2001:db8::1'"; }

	pbr_get_gateway6 _GW_RESULT eth0 eth0

	assertEquals "gw should be populated"      "2001:db8::1" "$_GW_RESULT"
	assertEquals "no warning should be emitted" ""            "$_JSON_CALLS"
}

# ---------------------------------------------------------------------------
# Each sentinel value is treated as absent; route default fallback succeeds
# ---------------------------------------------------------------------------

test_6_sentinel_colon_slash_zero_falls_through_to_route_default() {
	network_get_gateway6() { eval "$1='::/0'"; }

	ip() { [ "$1 $2" = "-6 route" ] && echo "default via fe80::1 dev eth0"; }

	pbr_get_gateway6 _GW_RESULT eth0 eth0

	assertEquals "gw should be fe80::1"        "fe80::1" "$_GW_RESULT"
	assertEquals "no warning should be emitted" ""        "$_JSON_CALLS"
}

test_6_sentinel_double_colon_zero_slash_zero_falls_through_to_route_default() {
	network_get_gateway6() { eval "$1='::0/0'"; }

	ip() { [ "$1 $2" = "-6 route" ] && echo "default via fe80::2 dev eth0"; }

	pbr_get_gateway6 _GW_RESULT eth0 eth0

	assertEquals "gw should be fe80::2"        "fe80::2" "$_GW_RESULT"
	assertEquals "no warning should be emitted" ""        "$_JSON_CALLS"
}

test_6_sentinel_double_colon_falls_through_to_route_default() {
	network_get_gateway6() { eval "$1='::'"; }

	ip() { [ "$1 $2" = "-6 route" ] && echo "default via fe80::3 dev eth0"; }

	pbr_get_gateway6 _GW_RESULT eth0 eth0

	assertEquals "gw should be fe80::3"        "fe80::3" "$_GW_RESULT"
	assertEquals "no warning should be emitted" ""        "$_JSON_CALLS"
}

# ---------------------------------------------------------------------------
# First fallback: ip -6 route show default awk finds a gateway
# ---------------------------------------------------------------------------

test_6_successful_gateway_via_ip6_route_default() {
	ip() { [ "$1 $2" = "-6 route" ] && echo "default via 2001:db8::fffe dev eth0 metric 100"; }

	pbr_get_gateway6 _GW_RESULT eth0 eth0

	assertEquals "gw should be 2001:db8::fffe" "2001:db8::fffe" "$_GW_RESULT"
	assertEquals "no warning should be emitted" ""               "$_JSON_CALLS"
}

# ---------------------------------------------------------------------------
# Second fallback: no default keyword; any-via awk finds a gateway
# ---------------------------------------------------------------------------

test_6_successful_gateway_via_ip6_route_any_via() {
	ip() { [ "$1 $2" = "-6 route" ] && echo "2001:db8::/32 via fe80::aabb dev eth0"; }

	pbr_get_gateway6 _GW_RESULT eth0 eth0

	assertEquals "gw should be fe80::aabb"     "fe80::aabb" "$_GW_RESULT"
	assertEquals "no warning should be emitted" ""           "$_JSON_CALLS"
}

# ---------------------------------------------------------------------------
# Third fallback: ip -6 neigh show surfaces a link-local router neighbor
# ---------------------------------------------------------------------------

test_6_successful_gateway_via_ip6_neigh_router() {
	ip() {
		case "$1 $2" in
			"-6 route") : ;;
			"-6 neigh")  echo "fe80::cafe dev eth0 lladdr aa:bb:cc:dd:ee:ff router" ;;
		esac
	}

	pbr_get_gateway6 _GW_RESULT eth0 eth0

	assertEquals "gw should be fe80::cafe"     "fe80::cafe" "$_GW_RESULT"
	assertEquals "no warning should be emitted" ""           "$_JSON_CALLS"
}

# ---------------------------------------------------------------------------
# Neighbor present but NOT flagged router → not used; warning fires
# (the awk pattern /^fe80:.*router/ requires the 'router' keyword)
# ---------------------------------------------------------------------------

test_6_neigh_without_router_flag_not_used_as_gateway() {
	ip() {
		case "$1 $2" in
			"-6 route")     : ;;
			"-6 neigh")      echo "fe80::dead dev eth0 lladdr aa:bb:cc:dd:ee:ff STALE" ;;
			"address show") echo "2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>" ;;
		esac
	}

	pbr_get_gateway6 _GW_RESULT eth0 eth0

	assertEquals "gw result should be empty" "" "$_GW_RESULT"
	echo "$_JSON_CALLS" | grep -q "warningInterfaceRoutingUnknownGateway"
	assertTrue "warning should fire when non-router neigh is the only candidate" $?
}

# ---------------------------------------------------------------------------
# All fallbacks fail; interface is NOT point-to-point → warning must fire
# ---------------------------------------------------------------------------

test_6_warning_emitted_when_no_gateway_and_not_ptp() {
	ip() {
		case "$1 $2" in
			"-6 route")     : ;;
			"-6 neigh")      : ;;
			"address show") echo "2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>" ;;
		esac
	}

	pbr_get_gateway6 _GW_RESULT eth0 eth0

	assertEquals "gw result should be empty" "" "$_GW_RESULT"
	echo "$_JSON_CALLS" | grep -q "warningInterfaceRoutingUnknownGateway"
	assertTrue "warning token should be present in json call" $?
	echo "$_JSON_CALLS" | grep -q "eth0"
	assertTrue "device name should appear in json call" $?
}

# ---------------------------------------------------------------------------
# All fallbacks fail; interface IS point-to-point → no warning
# ---------------------------------------------------------------------------

test_6_no_warning_when_no_gateway_but_ptp_interface() {
	ip() {
		case "$1 $2" in
			"-6 route")     : ;;
			"-6 neigh")      : ;;
			"address show") echo "3: ppp0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP>" ;;
		esac
	}

	pbr_get_gateway6 _GW_RESULT ppp0 ppp0

	assertEquals "gw result should be empty"         "" "$_GW_RESULT"
	assertEquals "no warning for point-to-point link" "" "$_JSON_CALLS"
}

# ---------------------------------------------------------------------------
# All three IPv6 sentinel values → each triggers the fallback chain and, when
# that also fails, the warning.  Parametric helper keeps it DRY.
# ---------------------------------------------------------------------------

_assert_6_sentinel_triggers_warning() {
	local sentinel="$1" dev="eth2"

	network_get_gateway6() { eval "$1='$sentinel'"; }
	ip() {
		case "$1 $2" in
			"-6 route")     : ;;
			"-6 neigh")      : ;;
			"address show") echo "2: $dev: <BROADCAST,MULTICAST,UP,LOWER_UP>" ;;
		esac
	}
	_JSON_CALLS=""
	_GW_RESULT=""

	pbr_get_gateway6 _GW_RESULT "$dev" "$dev"

	assertEquals "gw empty for sentinel '$sentinel'"  ""  "$_GW_RESULT"
	echo "$_JSON_CALLS" | grep -q "warningInterfaceRoutingUnknownGateway"
	assertTrue   "warning fires for sentinel '$sentinel'" $?
}

test_6_all_three_sentinels_each_trigger_warning() {
	_assert_6_sentinel_triggers_warning '::/0'
	_assert_6_sentinel_triggers_warning '::0/0'
	_assert_6_sentinel_triggers_warning '::'
}

# ---------------------------------------------------------------------------

_load_functions_under_test

# shellcheck disable=SC1091
. shunit2