Files
coder/cli/portforward_internal_test.go
Jake Howell 2e94c7498c fix(cli): terminate port ranges at the maximum port (#32578)
> 🤖 This PR was written by Coder Agents on behalf of Jake Howell.

Fix an infinite loop in `coder port-forward` when a local or remote port
range ends at `65535`, including a single-port specification of `65535`.
The `uint16` counter previously wrapped to zero and kept appending
ports, causing unbounded memory growth in the local CLI before
forwarding started.

Stop after appending the final port, before incrementing the counter.
Add regression cases to the existing parser test table for a single
maximum TCP port and a UDP destination range ending at the maximum port.

<details>
<summary>Validation evidence</summary>

- Both new cases time out inside `parsePortRange` with the old loop
restored; each negative-control run used a 100 ms test timeout.
- With the fix restored, the parser and port-forward tests pass: `go
test -tags=testsmallbatch ./cli -run
"^(Test_parsePortForwards|TestPortForward.*)$" -count=1 -timeout=5m`.
- CLI lint, formatting, diff checks, and documentation self-check pass.
No documentation changes are needed because the existing port-range
syntax is unchanged.

</details>
2026-11-23 12:12:02 +11:00

194 lines
3.9 KiB
Go

package cli
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_parsePortForwards(t *testing.T) {
t.Parallel()
type args struct {
tcpSpecs []string
udpSpecs []string
}
tests := []struct {
name string
args args
want []portForwardSpec
wantErr bool
}{
{
name: "TCP mixed ports and ranges",
args: args{
tcpSpecs: []string{
"8000,8080:8081,9000-9002,9003-9004:9005-9006",
"10000",
"4444-4444",
},
},
want: []portForwardSpec{
{"tcp", noAddr, 8000, 8000},
{"tcp", noAddr, 8080, 8081},
{"tcp", noAddr, 9000, 9000},
{"tcp", noAddr, 9001, 9001},
{"tcp", noAddr, 9002, 9002},
{"tcp", noAddr, 9003, 9005},
{"tcp", noAddr, 9004, 9006},
{"tcp", noAddr, 10000, 10000},
{"tcp", noAddr, 4444, 4444},
},
},
{
name: "TCP maximum port",
args: args{
tcpSpecs: []string{"65535"},
},
want: []portForwardSpec{
{"tcp", noAddr, 65535, 65535},
},
},
{
name: "UDP range to maximum remote port",
args: args{
udpSpecs: []string{"8000-8001:65534-65535"},
},
want: []portForwardSpec{
{"udp", noAddr, 8000, 65534},
{"udp", noAddr, 8001, 65535},
},
},
{
name: "TCP IPv4 local",
args: args{
tcpSpecs: []string{"127.0.0.1:8080:8081"},
},
want: []portForwardSpec{
{"tcp", ipv4Loopback, 8080, 8081},
},
},
{
name: "TCP IPv6 local",
args: args{
tcpSpecs: []string{"[::1]:8080:8081"},
},
want: []portForwardSpec{
{"tcp", ipv6Loopback, 8080, 8081},
},
},
{
name: "UDP with port range",
args: args{
udpSpecs: []string{"8000,8080-8081"},
},
want: []portForwardSpec{
{"udp", noAddr, 8000, 8000},
{"udp", noAddr, 8080, 8080},
{"udp", noAddr, 8081, 8081},
},
},
{
name: "UDP IPv4 local",
args: args{
udpSpecs: []string{"127.0.0.1:8080:8081"},
},
want: []portForwardSpec{
{"udp", ipv4Loopback, 8080, 8081},
},
},
{
name: "UDP IPv6 local",
args: args{
udpSpecs: []string{"[::1]:8080:8081"},
},
want: []portForwardSpec{
{"udp", ipv6Loopback, 8080, 8081},
},
},
{
name: "Bad port range",
args: args{
tcpSpecs: []string{"8000-7000"},
},
wantErr: true,
},
{
name: "Bad dest port range",
args: args{
tcpSpecs: []string{"8080-8081:9080-9082"},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := parsePortForwards(tt.args.tcpSpecs, tt.args.udpSpecs)
if (err != nil) != tt.wantErr {
t.Fatalf("parsePortForwards() error = %v, wantErr %v", err, tt.wantErr)
return
}
require.Equal(t, tt.want, got)
})
}
}
func TestPortForwardUsageRequest(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
// steps runs before each request. Each entry is one reporting tick.
steps []func(u *portForwardUsage, done *func())
want []bool
}{
{
name: "Idle",
steps: []func(*portForwardUsage, *func()){nil, nil},
want: []bool{false, false},
},
{
name: "OpenConnection",
steps: []func(*portForwardUsage, *func()){
func(u *portForwardUsage, done *func()) { *done = u.track() },
nil,
},
want: []bool{true, true},
},
{
name: "GoesIdleAfterClose",
steps: []func(*portForwardUsage, *func()){
func(u *portForwardUsage, done *func()) { *done = u.track() },
nil,
func(_ *portForwardUsage, done *func()) { (*done)() },
},
want: []bool{true, true, false},
},
{
name: "OpenOnlyBetweenTicks",
steps: []func(*portForwardUsage, *func()){
func(u *portForwardUsage, _ *func()) { u.track()() },
nil,
},
want: []bool{true, false},
},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var (
u portForwardUsage
done func()
)
for i, step := range tc.steps {
if step != nil {
step(&u, &done)
}
require.Equal(t, tc.want[i], u.inUse(), "tick %d", i)
}
})
}
}