跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

port-forwarding.bats

741 行 · 25.7 KB历史下载
1# SPDX-FileCopyrightText: Copyright The Lima Authors
2# SPDX-License-Identifier: Apache-2.0
3
4# Tests for the TCP port-forwarding rules of the hostagent.
5#
6# Every test follows the same pattern:
7#
8# 1. Start a socat listener on a specific address inside the guest.
9# 2. Wait for the hostagent to emit a "forwarding" (or "not-forwarding") event
10# for that address. The events are recorded by a `limactl watch --json`
11# process started in local_setup_file.
12# 3. Send a message from the host to the forwarded address. It must arrive at
13# the guest listener verbatim (or, for ignored listeners, must not arrive).
14#
15# The port forwarder is agnostic to the guest template, so this file only runs
16# with the default template. Its behavior does depend on the host OS though,
17# so it should be run on every supported host platform.
18#
19# Host requirements: socat and jq.
20
21load "../helpers/load"
22
23NAME=bats-port-forwarding
24
25# The instance is created from this base template, with the portForwards rules
26# below added. CI overrides it to test the WSL2 driver on Windows hosts.
27BASE_TEMPLATE=${LIMA_BATS_PORT_FORWARDING_BASE_TEMPLATE:-template:default}
28
29EVENT_TIMEOUT=30 # seconds to wait for a hostagent port-forwarding event
30RECEIVE_TIMEOUT=10 # seconds to wait for a message to arrive in the guest
31SETTLE_TIMEOUT=2 # seconds to wait before concluding that a message did not arrive
32
33# The portForwards rules under test. Rule order matters: the first rule matching
34# a guest listener address wins. The @test cases below are in the same order as
35# the rules they exercise.
36#
37# HOST_LAN_IP is replaced with the actual LAN IP address of the host, to test
38# forwarding to a host address other than 127.0.0.1.
39#
40# Untestable here: that the sshLocalPort itself is never forwarded (the
41# guestagent ignores it from startup, before the event recorder is running),
42# and connecting to a 0.0.0.0 forward from a non-loopback host interface.
43PORT_FORWARDING_RULES="$(cat <<'YAML'
44portForwards:
45- guestIP: 127.0.0.2
46 guestPortRange: [3000, 3009]
47 hostPortRange: [2000, 2009]
48 ignore: true
49
50- guestIP: 0.0.0.0
51 guestIPMustBeZero: false
52 guestPortRange: [3010, 3019]
53 hostPortRange: [2010, 2019]
54 ignore: true
55
56- guestIP: 0.0.0.0
57 guestIPMustBeZero: false
58 guestPortRange: [3000, 3029]
59 hostPortRange: [2000, 2029]
60
61# This rule is completely shadowed by the previous rule and has no effect.
62- guestIP: 0.0.0.0
63 guestIPMustBeZero: false
64 guestPortRange: [3020, 3029]
65 hostPortRange: [2020, 2029]
66 ignore: true
67
68- guestPortRange: [3030, 3039]
69 hostPortRange: [2030, 2039]
70 hostIP: HOST_LAN_IP
71
72# The host ports of the following four rules are privileged (below 1024);
73# the hostagent can bind them only on macOS.
74- guestPortRange: [300, 304]
75
76- guestPortRange: [305, 309]
77 guestIPMustBeZero: false
78
79- guestPortRange: [310, 314]
80 hostIP: 0.0.0.0
81
82- guestPortRange: [315, 319]
83 guestIPMustBeZero: false
84 hostIP: 0.0.0.0
85
86# 192.168.5.15 is the guest address on the user-mode network.
87- guestIP: "192.168.5.15"
88 guestPortRange: [4000, 4009]
89 hostIP: HOST_LAN_IP
90
91- guestIP: "::1"
92 guestPortRange: [4010, 4019]
93 hostIP: "::"
94
95- guestIP: "::"
96 guestPortRange: [4020, 4029]
97 hostIP: HOST_LAN_IP
98
99- guestIP: "0.0.0.0"
100 guestIPMustBeZero: false
101 guestPortRange: [4030, 4039]
102 hostIP: HOST_LAN_IP
103
104- guestIPMustBeZero: true
105 guestPortRange: [4040, 4049]
106
107- guestIP: "0.0.0.0"
108 guestIPMustBeZero: false
109 guestPortRange: [4040, 4049]
110 ignore: true
111
112# Relative hostSocket paths are created inside the "sock" subdirectory of the
113# instance directory.
114- guestPort: 5000
115 hostSocket: port5000.sock
116
117- guestPort: 5001
118 hostSocket: port5001.sock
119
120- guestPort: 5002
121 guestIPMustBeZero: false
122 hostSocket: port5002.sock
123
124- guestPort: 5003
125 guestIPMustBeZero: false
126 hostSocket: port5003.sock
127YAML
128)"
129
130local_setup_file() {
131 local cmd
132 for cmd in socat jq; do
133 if ! command -v "$cmd" >/dev/null; then
134 echo "'$cmd' is required on the host to run these tests" >&2
135 return 1
136 fi
137 done
138
139 # A loopback fallback keeps the rest of the matrix testable on hosts without
140 # a resolvable LAN IP, but then the HOST_LAN_IP tests no longer verify
141 # non-loopback bindings, so at least make the degradation visible.
142 if [[ $(host_lan_ip) == 127.* ]]; then
143 echo "# WARNING: could not determine a LAN IP for the host;" \
144 "HOST_LAN_IP tests will use $(host_lan_ip) instead" >&3
145 fi
146
147 if ! reusing_running_instance; then
148 limactl unprotect "$NAME" || :
149 limactl delete --force "$NAME" || :
150 local template=$BATS_FILE_TMPDIR/$NAME.yaml
151 echo "base: $BASE_TEMPLATE" >"$template"
152 echo "${PORT_FORWARDING_RULES//HOST_LAN_IP/$(host_lan_ip)}" >>"$template"
153 # LIMACTL_CREATE_ARGS is a list of arguments, so it must not be quoted.
154 # shellcheck disable=SC2086
155 limactl start --yes --name "$NAME" ${LIMACTL_CREATE_ARGS:-} "$template" 3>&- 4>&-
156 fi
157
158 # The listeners for the tests run inside the guest, but the guest image may
159 # not include socat.
160 install_socat_in_guest
161 # Kill listeners left over from a previous run (only relevant with LIMA_BATS_REUSE_INSTANCE).
162 limactl shell "$NAME" -- sh -c 'sudo pkill -x socat; rm -f /tmp/port-forwarding.*.out; true'
163 # The guestagent polls every 3s and only reports the difference between two
164 # snapshots, so a port closed here and rebound by the first test within the
165 # same tick produces no event at all. Wait out a full tick before starting.
166 [[ -z ${LIMA_BATS_REUSE_INSTANCE:-} ]] || sleep 5
167
168 # Record hostagent events for wait_for_port_forwarding_event.
169 limactl watch --json "$NAME" >"$BATS_FILE_TMPDIR/events" 2>"$BATS_FILE_TMPDIR/watch.log" 3>&- 4>&- &
170 echo $! >"$BATS_FILE_TMPDIR/watch.pid"
171
172 # The watcher discards events recorded before its own startup timestamp, so
173 # don't let the first test start a listener until the watcher is ready, or
174 # the resulting forwarding event could be discarded as historical.
175 local deadline=$((SECONDS + 30))
176 until grep -q 'Watching for events' "$BATS_FILE_TMPDIR/watch.log"; do
177 if ! kill -0 "$(cat "$BATS_FILE_TMPDIR/watch.pid")" 2>/dev/null; then
178 echo "'limactl watch' exited prematurely:" >&2
179 cat "$BATS_FILE_TMPDIR/watch.log" >&2
180 return 1
181 fi
182 if ((SECONDS >= deadline)); then
183 echo "timed out waiting for 'limactl watch' to become ready" >&2
184 return 1
185 fi
186 sleep 0.2
187 done
188 # The readiness line is printed even when the instance could not be inspected,
189 # in which case the watcher runs forever without tailing anything.
190 if grep -q 'Failed to inspect instance' "$BATS_FILE_TMPDIR/watch.log"; then
191 echo "'limactl watch' did not attach to instance $NAME:" >&2
192 cat "$BATS_FILE_TMPDIR/watch.log" >&2
193 return 1
194 fi
195}
196
197local_teardown_file() {
198 if [[ -s $BATS_FILE_TMPDIR/watch.pid ]]; then
199 kill "$(cat "$BATS_FILE_TMPDIR/watch.pid")" || :
200 fi
201 delete_instance "$NAME"
202}
203
204# When a test fails, show the events received so far to aid debugging.
205local_teardown() {
206 if [[ -z ${BATS_TEST_COMPLETED:-} && -z ${BATS_TEST_SKIPPED:-} && -s $BATS_FILE_TMPDIR/events ]]; then
207 echo "hostagent port-forwarding events received so far:"
208 # "|| :" because the watch process is still writing, so the last line may
209 # be a partial JSON object that makes jq fail under pipefail.
210 jq -c '.event.status.portForward | select(. != null)' "$BATS_FILE_TMPDIR/events" | tail -n 25 || :
211 fi
212}
213
214reusing_running_instance() {
215 [[ -n ${LIMA_BATS_REUSE_INSTANCE:-} ]] || return 1
216 run limactl list --format '{{.Status}}' "$NAME"
217 [[ $status == 0 && $output == "Running" ]]
218}
219
220install_socat_in_guest() {
221 limactl shell "$NAME" -- sh -c '
222 command -v socat >/dev/null && exit 0
223 if command -v apt-get >/dev/null; then sudo apt-get install -y socat
224 elif command -v dnf >/dev/null; then sudo dnf install -y socat
225 elif command -v apk >/dev/null; then sudo apk add socat
226 elif command -v pacman >/dev/null; then sudo pacman -Sy --noconfirm socat
227 elif command -v zypper >/dev/null; then sudo zypper in -y socat
228 else echo "do not know how to install socat in the guest" >&2; exit 1
229 fi'
230}
231
232# Returns the IPv4 address of the host LAN interface.
233host_lan_ip() {
234 local cache=$BATS_FILE_TMPDIR/host_lan_ip ip=""
235 if [[ ! -s $cache ]]; then
236 if [[ $OSTYPE == darwin* ]]; then
237 # macOS GitHub runners use "localhost" as the hostname, so ask system_profiler instead.
238 ip=$(system_profiler SPNetworkDataType -json |
239 jq -r 'first(.SPNetworkDataType[] | select(.ip_address) | .ip_address) | first') || ip=""
240 [[ $ip == null ]] && ip=""
241 else
242 ip=$(getent ahostsv4 "$(hostname)" 2>/dev/null | awk '{print $1; exit}') || ip=""
243 fi
244 echo "${ip:-127.0.0.1}" >"$cache"
245 fi
246 cat "$cache"
247}
248
249instance_dir() {
250 local cache=$BATS_FILE_TMPDIR/instance_dir
251 if [[ ! -s $cache ]]; then
252 limactl list --format '{{.Dir}}' "$NAME" >"$cache"
253 fi
254 cat "$cache"
255}
256
257# The hostagent runs unprivileged and can bind host ports below 1024 only on macOS.
258skip_unless_host_can_bind_privileged_ports() {
259 if [[ $OSTYPE != darwin* ]]; then
260 skip "the hostagent cannot bind host ports below 1024 on this host OS"
261 fi
262}
263
264vm_type() {
265 local cache=$BATS_FILE_TMPDIR/vm_type
266 if [[ ! -s $cache ]]; then
267 limactl list --format '{{.VMType}}' "$NAME" >"$cache"
268 fi
269 cat "$cache"
270}
271
272skip_if_wsl2() {
273 if [[ $(vm_type) == wsl2 ]]; then
274 skip "$1"
275 fi
276}
277
278skip_if_windows_host() {
279 case $OSTYPE in
280 cygwin | msys) skip "UNIX sockets are not supported on Windows hosts" ;;
281 esac
282}
283
284# Format an address the way Go's net.JoinHostPort() does, e.g. "127.0.0.1:80" or "[::1]:80".
285join_host_port() {
286 local ip=$1 port=$2
287 [[ $ip == *:* ]] && ip="[$ip]"
288 echo "$ip:$port"
289}
290
291# The socat address to connect to ip:port from the host.
292tcp_dest() {
293 local ip=$1 port=$2
294 if [[ $ip == *:* ]]; then
295 echo "TCP6:[$ip]:$port,connect-timeout=5"
296 else
297 echo "TCP4:$ip:$port,connect-timeout=5"
298 fi
299}
300
301guest_outfile() {
302 echo "/tmp/port-forwarding.$1.out"
303}
304
305# Start a listener inside the guest that writes everything it receives to a
306# file. socat exits after the first connection is closed. Ports below 1024
307# require root to bind.
308start_listener_in_guest() {
309 local ip=$1 port=$2 proto=TCP sudo=""
310 [[ $ip == *:* ]] && proto=TCP6
311 ((port < 1024)) && sudo=sudo
312 limactl shell "$NAME" -- sh -c \
313 "nohup $sudo socat -u $proto-LISTEN:$port,bind=$ip STDOUT >$(guest_outfile "$port") 2>/dev/null </dev/null &"
314}
315
316# Wait until the hostagent reports a port-forwarding event of the given type
317# ("forwarding" or "not-forwarding") for the guest address, and print it.
318wait_for_port_forwarding_event() {
319 local type=$1 guest_addr=$2 event
320 local deadline=$((SECONDS + EVENT_TIMEOUT))
321 while ((SECONDS < deadline)); do
322 # The last line of the events file may still be incomplete; ignore jq failures.
323 event=$(jq -n --arg type "$type" --arg addr "$guest_addr" \
324 'first(inputs | .event.status.portForward | select(.type == $type and .guestAddr == $addr))' \
325 "$BATS_FILE_TMPDIR/events" 2>/dev/null) || event=""
326 if [[ -n $event ]]; then
327 echo "$event"
328 return 0
329 fi
330 sleep 0.5
331 done
332 fail "timed out waiting for the hostagent to report a '$type' event for $guest_addr"
333}
334
335send_from_host() {
336 local message=$1 dest=$2
337 socat -u - "$dest" <<<"$message"
338}
339
340# retry COUNT CMD [ARGS...]: retry a command with a 1 second pause until it succeeds.
341retry() {
342 local count=$1 i
343 shift
344 for ((i = 1; i < count; i++)); do
345 if "$@"; then return 0; fi
346 sleep 1
347 done
348 "$@"
349}
350
351read_guest_outfile() {
352 # The guest path must be embedded in the command string: a bare /tmp/... argument
353 # would be rewritten to C:/msys64/tmp/... by MSYS2 path conversion on Windows hosts.
354 limactl shell "$NAME" -- sh -c "cat $(guest_outfile "$1")"
355}
356
357# Assert that the guest listener on the given port has received exactly the
358# expected message. The message travels asynchronously, so keep polling while
359# the listener output file is still empty.
360assert_guest_received() {
361 local port=$1 expected=$2 received
362 local deadline=$((SECONDS + RECEIVE_TIMEOUT))
363 received=$(read_guest_outfile "$port")
364 while [[ -z $received ]] && ((SECONDS < deadline)); do
365 sleep 0.5
366 received=$(read_guest_outfile "$port")
367 done
368 assert_equal "$received" "$expected"
369}
370
371# Assert that nothing arrived at the guest listener on the given port. Unlike
372# assert_guest_received there is no state to poll for, so wait out a settle
373# window first: a message that was wrongly forwarded may still be in flight.
374assert_guest_received_nothing() {
375 local port=$1
376 sleep "$SETTLE_TIMEOUT"
377 assert_equal "$(read_guest_outfile "$port")" ""
378}
379
380# Assert that a listener on GUEST_IP:GUEST_PORT is forwarded to HOST_IP:HOST_PORT:
381# the hostagent must report a "forwarding" event to the expected host address,
382# and a message sent to the host address must arrive at the guest listener.
383assert_forwarded() {
384 local guest_ip=$1 guest_port=$2 host_ip=$3 host_port=$4
385 local guest_addr host_addr event message
386 guest_addr=$(join_host_port "$guest_ip" "$guest_port")
387 host_addr=$(join_host_port "$host_ip" "$host_port")
388 message="message from host $host_addr to guest $guest_addr"
389
390 start_listener_in_guest "$guest_ip" "$guest_port"
391 event=$(wait_for_port_forwarding_event forwarding "$guest_addr")
392 assert_equal "$(jq -r .hostAddr <<<"$event")" "$host_addr"
393
394 # The "forwarding" event is emitted just before the hostagent binds the
395 # host address, so the first connection attempt may be too early.
396 retry 5 send_from_host "$message" "$(tcp_dest "$host_ip" "$host_port")"
397 assert_guest_received "$guest_port" "$message"
398}
399
400# Assert that a listener on GUEST_IP:GUEST_PORT is NOT forwarded: the hostagent
401# must report a "not-forwarding" event, and a message sent to the host address
402# that would have been used must not arrive at the guest listener.
403assert_not_forwarded() {
404 local guest_ip=$1 guest_port=$2 host_ip=$3 host_port=$4
405 local guest_addr
406 guest_addr=$(join_host_port "$guest_ip" "$guest_port")
407
408 start_listener_in_guest "$guest_ip" "$guest_port"
409 wait_for_port_forwarding_event not-forwarding "$guest_addr" >/dev/null
410
411 # Something else may be listening on the host address, so ignore connection errors.
412 send_from_host "unexpected message to guest $guest_addr" "$(tcp_dest "$host_ip" "$host_port")" || :
413 assert_guest_received_nothing "$guest_port"
414}
415
416# Like assert_forwarded, but the forwarding target is a UNIX socket in the
417# "sock" subdirectory of the instance directory.
418assert_forwarded_to_socket() {
419 local guest_ip=$1 guest_port=$2 socket=$3
420 local guest_addr socket_path event message
421 guest_addr=$(join_host_port "$guest_ip" "$guest_port")
422 socket_path=$(instance_dir)/sock/$socket
423 message="message from host socket $socket to guest $guest_addr"
424
425 start_listener_in_guest "$guest_ip" "$guest_port"
426 event=$(wait_for_port_forwarding_event forwarding "$guest_addr")
427 assert_equal "$(jq -r .hostAddr <<<"$event")" "$socket_path"
428
429 retry 5 send_from_host "$message" "UNIX-CONNECT:$socket_path"
430 assert_guest_received "$guest_port" "$message"
431}
432
433# Like assert_not_forwarded, but for a UNIX socket target: the socket must not
434# have been created on the host.
435assert_not_forwarded_to_socket() {
436 local guest_ip=$1 guest_port=$2 socket=$3
437 local guest_addr
438 guest_addr=$(join_host_port "$guest_ip" "$guest_port")
439
440 start_listener_in_guest "$guest_ip" "$guest_port"
441 wait_for_port_forwarding_event not-forwarding "$guest_addr" >/dev/null
442
443 assert_socket_not_exists "$(instance_dir)/sock/$socket"
444 assert_guest_received_nothing "$guest_port"
445}
446
447# ---------------------------------------------------------------------------
448# An ignore rule only applies to listeners matching its exact guestIP; other
449# addresses are still forwarded by later rules. An ignore rule for 0.0.0.0
450# with guestIPMustBeZero false blocks listeners on ALL addresses.
451
452@test 'ignore 127.0.0.2:3000 (exact match of an ignore rule)' {
453 assert_not_forwarded 127.0.0.2 3000 127.0.0.1 2000
454}
455
456@test 'forward 127.0.0.3:3001 -> 127.0.0.1:2001 (ignore rule for 127.0.0.2 does not match 127.0.0.3)' {
457 assert_forwarded 127.0.0.3 3001 127.0.0.1 2001
458}
459
460@test 'forward 0.0.0.0:3002 -> 127.0.0.1:2002 (ignore rule for 127.0.0.2 cannot block a wildcard listener)' {
461 assert_forwarded 0.0.0.0 3002 127.0.0.1 2002
462}
463
464@test 'ignore 0.0.0.0:3010 (exact match of the ignore rule for 0.0.0.0)' {
465 skip_if_wsl2 'ignore rules for localhost ports cannot be enforced on WSL2'
466 assert_not_forwarded 0.0.0.0 3010 127.0.0.1 2010
467}
468
469@test 'ignore 127.0.0.1:3011 (ignore rule for 0.0.0.0 blocks all addresses when guestIPMustBeZero is false)' {
470 skip_if_wsl2 'ignore rules for localhost ports cannot be enforced on WSL2'
471 assert_not_forwarded 127.0.0.1 3011 127.0.0.1 2011
472}
473
474# The forward rule for 0.0.0.0 [3000-3029] matches listeners on any address,
475# including IPv6, and shadows the ignore rule for [3020-3029] that follows it.
476
477@test 'forward 127.0.0.2:3020 -> 127.0.0.1:2020 (earlier forward rule shadows later ignore rule)' {
478 assert_forwarded 127.0.0.2 3020 127.0.0.1 2020
479}
480
481@test 'forward 127.0.0.1:3021 -> 127.0.0.1:2021' {
482 assert_forwarded 127.0.0.1 3021 127.0.0.1 2021
483}
484
485@test 'forward 0.0.0.0:3022 -> 127.0.0.1:2022' {
486 assert_forwarded 0.0.0.0 3022 127.0.0.1 2022
487}
488
489@test 'forward [::]:3023 -> 127.0.0.1:2023 (wildcard rule also matches IPv6 listeners)' {
490 assert_forwarded :: 3023 127.0.0.1 2023
491}
492
493@test 'forward [::1]:3024 -> 127.0.0.1:2024' {
494 assert_forwarded ::1 3024 127.0.0.1 2024
495}
496
497# ---------------------------------------------------------------------------
498# Forwarding to the host LAN address instead of localhost.
499
500@test 'forward 127.0.0.1:3030 -> HOST_LAN_IP:2030' {
501 assert_forwarded 127.0.0.1 3030 "$(host_lan_ip)" 2030
502}
503
504@test 'forward 0.0.0.0:3031 -> HOST_LAN_IP:2031' {
505 assert_forwarded 0.0.0.0 3031 "$(host_lan_ip)" 2031
506}
507
508@test 'forward [::]:3032 -> HOST_LAN_IP:2032' {
509 assert_forwarded :: 3032 "$(host_lan_ip)" 2032
510}
511
512@test 'forward [::1]:3033 -> HOST_LAN_IP:2033' {
513 assert_forwarded ::1 3033 "$(host_lan_ip)" 2033
514}
515
516# ---------------------------------------------------------------------------
517# Privileged host ports (below 1024). The default guestIP for a rule is
518# 127.0.0.1, which also matches wildcard and IPv6 loopback listeners, but not
519# the user-mode network address.
520
521@test 'forward 127.0.0.1:300 -> 127.0.0.1:300 (privileged host port)' {
522 skip_unless_host_can_bind_privileged_ports
523 assert_forwarded 127.0.0.1 300 127.0.0.1 300
524}
525
526@test 'forward 0.0.0.0:301 -> 127.0.0.1:301' {
527 skip_unless_host_can_bind_privileged_ports
528 assert_forwarded 0.0.0.0 301 127.0.0.1 301
529}
530
531@test 'forward [::]:302 -> 127.0.0.1:302' {
532 skip_unless_host_can_bind_privileged_ports
533 assert_forwarded :: 302 127.0.0.1 302
534}
535
536@test 'forward [::1]:303 -> 127.0.0.1:303' {
537 skip_unless_host_can_bind_privileged_ports
538 assert_forwarded ::1 303 127.0.0.1 303
539}
540
541@test 'ignore 192.168.5.15:304 (default guestIP 127.0.0.1 does not match the user-mode network address)' {
542 skip_if_wsl2 'the guest has no user-mode network address on WSL2'
543 assert_not_forwarded 192.168.5.15 304 127.0.0.1 304
544}
545
546# Same as the previous rule, but with an explicit "guestIPMustBeZero: false".
547
548@test 'forward 127.0.0.1:305 -> 127.0.0.1:305 (privileged host port)' {
549 skip_unless_host_can_bind_privileged_ports
550 assert_forwarded 127.0.0.1 305 127.0.0.1 305
551}
552
553@test 'forward 0.0.0.0:306 -> 127.0.0.1:306' {
554 skip_unless_host_can_bind_privileged_ports
555 assert_forwarded 0.0.0.0 306 127.0.0.1 306
556}
557
558@test 'forward [::]:307 -> 127.0.0.1:307' {
559 skip_unless_host_can_bind_privileged_ports
560 assert_forwarded :: 307 127.0.0.1 307
561}
562
563@test 'forward [::1]:308 -> 127.0.0.1:308' {
564 skip_unless_host_can_bind_privileged_ports
565 assert_forwarded ::1 308 127.0.0.1 308
566}
567
568@test 'ignore 192.168.5.15:309' {
569 skip_if_wsl2 'the guest has no user-mode network address on WSL2'
570 assert_not_forwarded 192.168.5.15 309 127.0.0.1 309
571}
572
573# hostIP 0.0.0.0 binds the forward on all host interfaces.
574
575@test 'forward 127.0.0.1:310 -> 0.0.0.0:310 (hostIP 0.0.0.0 binds all host interfaces)' {
576 skip_unless_host_can_bind_privileged_ports
577 assert_forwarded 127.0.0.1 310 0.0.0.0 310
578}
579
580@test 'forward 0.0.0.0:311 -> 0.0.0.0:311' {
581 skip_unless_host_can_bind_privileged_ports
582 assert_forwarded 0.0.0.0 311 0.0.0.0 311
583}
584
585@test 'forward [::]:312 -> 0.0.0.0:312' {
586 skip_unless_host_can_bind_privileged_ports
587 assert_forwarded :: 312 0.0.0.0 312
588}
589
590@test 'forward [::1]:313 -> 0.0.0.0:313' {
591 skip_unless_host_can_bind_privileged_ports
592 assert_forwarded ::1 313 0.0.0.0 313
593}
594
595@test 'ignore 192.168.5.15:314' {
596 skip_if_wsl2 'the guest has no user-mode network address on WSL2'
597 assert_not_forwarded 192.168.5.15 314 0.0.0.0 314
598}
599
600# Same as the previous rule, but with an explicit "guestIPMustBeZero: false".
601
602@test 'forward 127.0.0.1:315 -> 0.0.0.0:315 (privileged host port)' {
603 skip_unless_host_can_bind_privileged_ports
604 assert_forwarded 127.0.0.1 315 0.0.0.0 315
605}
606
607@test 'forward 0.0.0.0:316 -> 0.0.0.0:316' {
608 skip_unless_host_can_bind_privileged_ports
609 assert_forwarded 0.0.0.0 316 0.0.0.0 316
610}
611
612@test 'forward [::]:317 -> 0.0.0.0:317' {
613 skip_unless_host_can_bind_privileged_ports
614 assert_forwarded :: 317 0.0.0.0 317
615}
616
617@test 'forward [::1]:318 -> 0.0.0.0:318' {
618 skip_unless_host_can_bind_privileged_ports
619 assert_forwarded ::1 318 0.0.0.0 318
620}
621
622@test 'ignore 192.168.5.15:319' {
623 skip_if_wsl2 'the guest has no user-mode network address on WSL2'
624 assert_not_forwarded 192.168.5.15 319 0.0.0.0 319
625}
626
627# ---------------------------------------------------------------------------
628# guestIP set to the user-mode network address of the guest.
629
630@test 'forward 192.168.5.15:4000 -> HOST_LAN_IP:4000 (guestIP is the user-mode network address)' {
631 skip_if_wsl2 'the guest has no user-mode network address on WSL2'
632 assert_forwarded 192.168.5.15 4000 "$(host_lan_ip)" 4000
633}
634
635# ---------------------------------------------------------------------------
636# IPv6 guestIP and hostIP.
637
638@test 'forward [::1]:4010 -> [::]:4010 (IPv6 guestIP and hostIP)' {
639 assert_forwarded ::1 4010 :: 4010
640}
641
642# ---------------------------------------------------------------------------
643# guestIP "::" (with the default guestIPMustBeZero false) matches listeners on
644# any address.
645
646@test 'forward 127.0.0.1:4020 -> HOST_LAN_IP:4020 (guestIP :: matches any listener address)' {
647 assert_forwarded 127.0.0.1 4020 "$(host_lan_ip)" 4020
648}
649
650@test 'forward 127.0.0.2:4021 -> HOST_LAN_IP:4021' {
651 assert_forwarded 127.0.0.2 4021 "$(host_lan_ip)" 4021
652}
653
654@test 'forward 192.168.5.15:4022 -> HOST_LAN_IP:4022' {
655 skip_if_wsl2 'the guest has no user-mode network address on WSL2'
656 assert_forwarded 192.168.5.15 4022 "$(host_lan_ip)" 4022
657}
658
659@test 'forward 0.0.0.0:4023 -> HOST_LAN_IP:4023' {
660 assert_forwarded 0.0.0.0 4023 "$(host_lan_ip)" 4023
661}
662
663@test 'forward [::]:4024 -> HOST_LAN_IP:4024' {
664 assert_forwarded :: 4024 "$(host_lan_ip)" 4024
665}
666
667@test 'forward [::1]:4025 -> HOST_LAN_IP:4025' {
668 assert_forwarded ::1 4025 "$(host_lan_ip)" 4025
669}
670
671# guestIP "0.0.0.0" with guestIPMustBeZero false behaves exactly like "::".
672
673@test 'forward 127.0.0.1:4030 -> HOST_LAN_IP:4030 (guestIP 0.0.0.0 matches any listener address)' {
674 assert_forwarded 127.0.0.1 4030 "$(host_lan_ip)" 4030
675}
676
677@test 'forward 127.0.0.2:4031 -> HOST_LAN_IP:4031' {
678 assert_forwarded 127.0.0.2 4031 "$(host_lan_ip)" 4031
679}
680
681@test 'forward 192.168.5.15:4032 -> HOST_LAN_IP:4032' {
682 skip_if_wsl2 'the guest has no user-mode network address on WSL2'
683 assert_forwarded 192.168.5.15 4032 "$(host_lan_ip)" 4032
684}
685
686@test 'forward 0.0.0.0:4033 -> HOST_LAN_IP:4033' {
687 assert_forwarded 0.0.0.0 4033 "$(host_lan_ip)" 4033
688}
689
690@test 'forward [::]:4034 -> HOST_LAN_IP:4034' {
691 assert_forwarded :: 4034 "$(host_lan_ip)" 4034
692}
693
694@test 'forward [::1]:4035 -> HOST_LAN_IP:4035' {
695 assert_forwarded ::1 4035 "$(host_lan_ip)" 4035
696}
697
698# ---------------------------------------------------------------------------
699# With guestIPMustBeZero true only wildcard listeners match; listeners on any
700# other address fall through to the next rule, which ignores them.
701
702@test 'forward 0.0.0.0:4040 -> 127.0.0.1:4040 (wildcard listener matches guestIPMustBeZero rule)' {
703 assert_forwarded 0.0.0.0 4040 127.0.0.1 4040
704}
705
706@test 'forward [::]:4041 -> 127.0.0.1:4041 (IPv6 wildcard listener matches guestIPMustBeZero rule)' {
707 assert_forwarded :: 4041 127.0.0.1 4041
708}
709
710@test 'ignore 127.0.0.1:4043 (loopback listener does not match a guestIPMustBeZero rule)' {
711 skip_if_wsl2 'ignore rules for localhost ports cannot be enforced on WSL2'
712 assert_not_forwarded 127.0.0.1 4043 127.0.0.1 4043
713}
714
715@test 'ignore 192.168.5.15:4044' {
716 skip_if_wsl2 'the guest has no user-mode network address on WSL2'
717 assert_not_forwarded 192.168.5.15 4044 127.0.0.1 4044
718}
719
720# ---------------------------------------------------------------------------
721# Forwarding to a UNIX socket on the host.
722
723@test 'forward 127.0.0.1:5000 -> sock/port5000.sock' {
724 skip_if_windows_host
725 assert_forwarded_to_socket 127.0.0.1 5000 port5000.sock
726}
727
728@test 'ignore 192.168.5.15:5001 (no socket is created for an unmatched listener)' {
729 skip_if_windows_host
730 assert_not_forwarded_to_socket 192.168.5.15 5001 port5001.sock
731}
732
733@test 'forward 127.0.0.1:5002 -> sock/port5002.sock (guestIPMustBeZero false)' {
734 skip_if_windows_host
735 assert_forwarded_to_socket 127.0.0.1 5002 port5002.sock
736}
737
738@test 'ignore 192.168.5.15:5003 (no socket is created for an unmatched listener)' {
739 skip_if_windows_host
740 assert_not_forwarded_to_socket 192.168.5.15 5003 port5003.sock
741}