From 575c5994102a7d889c7a18dd372c0a3fd0082065 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 23 Oct 2022 20:56:28 -0700 Subject: [PATCH] portlist: add a test that verifies changes are picked up over time To avoid annoying firewall dialogs on macOS and Windows, only run it on Linux by default without the flag. Change-Id: If8486c31d4243ade54b0131f673237c6c9184c08 Signed-off-by: Brad Fitzpatrick --- portlist/portlist_test.go | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/portlist/portlist_test.go b/portlist/portlist_test.go index a02e870e0..9e824ce4f 100644 --- a/portlist/portlist_test.go +++ b/portlist/portlist_test.go @@ -5,7 +5,9 @@ package portlist import ( + "flag" "net" + "runtime" "testing" "tailscale.com/tstest" @@ -47,6 +49,54 @@ func TestIgnoreLocallyBoundPorts(t *testing.T) { } } +var flagRunUnspecTests = flag.Bool("run-unspec-tests", + runtime.GOOS == "linux", // other OSes have annoying firewall GUI confirmation dialogs + "run tests that require listening on the the unspecified address") + +func TestChangesOverTime(t *testing.T) { + if !*flagRunUnspecTests { + t.Skip("skipping test without --run-unspec-tests") + } + + var p Poller + get := func(t *testing.T) []Port { + t.Helper() + s, err := p.getList() + if err != nil { + t.Fatal(err) + } + return append([]Port(nil), s...) + } + + p1 := get(t) + ln, err := net.Listen("tcp", ":0") + if err != nil { + t.Skipf("failed to bind: %v", err) + } + defer ln.Close() + port := uint16(ln.Addr().(*net.TCPAddr).Port) + containsPort := func(pl List) bool { + for _, p := range pl { + if p.Proto == "tcp" && p.Port == port { + return true + } + } + return false + } + if containsPort(p1) { + t.Error("unexpectedly found ephemeral port in p1, before it was opened", port) + } + p2 := get(t) + if !containsPort(p2) { + t.Error("didn't find ephemeral port in p2", port) + } + ln.Close() + p3 := get(t) + if containsPort(p3) { + t.Error("unexpectedly found ephemeral port in p3, after it was closed", port) + } +} + func TestEqualLessThan(t *testing.T) { tests := []struct { name string