Files
tailscale/tailcfg/derpmap_test.go
Luo Chen dc69869769 net/netcheck: never prefer a STUN-only region as the home DERP (#21976)
The PreferredDERP selection loop only considers RegionLatency, which
STUN probes populate for STUN-only regions too. When such a region is
the only one with a latency sample (e.g. self-hosted DERP behind a
TCP-only tunnel while a public STUN server is reachable), it gets
selected as the preferred DERP and all relay traffic blackholes, since
the region has no DERP server to relay through.

Filter STUN-only regions in the selection loop by reusing the existing
regionHasDERPNode predicate (renamed to CanServeDERP), which was
already applied to the HTTPS/ICMP fallback probing but not here.

Regions missing from the map, or present without any nodes, keep the
old behavior, as some tests use latency-only DERP maps.

Fixes #21975

Signed-off-by: Luo Chen <[email protected]>
2026-11-04 21:22:51 -08:00

67 lines
1.2 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package tailcfg
import "testing"
func TestDERPRegionCanServeDERP(t *testing.T) {
tests := []struct {
name string
region *DERPRegion
want bool
}{
{
name: "nil_region",
region: (*DERPRegion)(nil),
want: false,
},
{
name: "no_nodes",
region: &DERPRegion{},
want: false,
},
{
name: "stun_only",
region: &DERPRegion{
Nodes: []*DERPNode{
{Name: "1a", STUNOnly: true},
{Name: "1b", STUNOnly: true},
},
},
want: false,
},
{
name: "mixed",
region: &DERPRegion{
Nodes: []*DERPNode{
{Name: "1a", STUNOnly: true},
{Name: "1b"},
},
},
want: true,
},
{
name: "derp_only",
region: &DERPRegion{
Nodes: []*DERPNode{{Name: "1a"}},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.region.CanServeDERP(); got != tt.want {
t.Errorf("CanServeDERP() = %v, want %v", got, tt.want)
}
var v DERPRegionView
if tt.region != nil {
v = tt.region.View()
}
if got := v.CanServeDERP(); got != tt.want {
t.Errorf("DERPRegionView.CanServeDERP() = %v, want %v", got, tt.want)
}
})
}
}