portlist: ignore ports bound to localhost

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2020-03-17 20:19:39 -07:00
committed by Brad Fitzpatrick
parent c706731dc7
commit f8d67bb591
5 changed files with 63 additions and 33 deletions

View File

@@ -4,7 +4,10 @@
package portlist
import "testing"
import (
"net"
"testing"
)
func TestGetList(t *testing.T) {
pl, err := GetList(nil)
@@ -17,6 +20,25 @@ func TestGetList(t *testing.T) {
t.Logf("As String: %v", pl.String())
}
func TestIgnoreLocallyBoundPorts(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Skipf("failed to bind: %v", err)
}
defer ln.Close()
ta := ln.Addr().(*net.TCPAddr)
port := ta.Port
pl, err := GetList(nil)
if err != nil {
t.Fatal(err)
}
for _, p := range pl {
if p.Proto == "tcp" && int(p.Port) == port {
t.Fatal("didn't expect to find test's localhost ephemeral port")
}
}
}
func BenchmarkGetList(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {