2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-03-14 03:53:58 +00:00
|
|
|
|
|
|
|
package portlist
|
|
|
|
|
2020-03-18 03:19:39 +00:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
2020-05-19 06:48:55 +01:00
|
|
|
|
|
|
|
"tailscale.com/tstest"
|
2020-03-18 03:19:39 +00:00
|
|
|
)
|
2020-03-14 03:53:58 +00:00
|
|
|
|
|
|
|
func TestGetList(t *testing.T) {
|
2021-02-02 19:30:46 +00:00
|
|
|
tstest.ResourceCheck(t)
|
2020-05-19 06:48:55 +01:00
|
|
|
|
2022-10-22 05:30:40 +01:00
|
|
|
var p Poller
|
2023-06-06 04:25:53 +01:00
|
|
|
pl, _, err := p.Poll()
|
2020-03-14 03:53:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, p := range pl {
|
|
|
|
t.Logf("[%d] %+v", i, p)
|
|
|
|
}
|
2023-06-06 04:25:53 +01:00
|
|
|
t.Logf("As String: %s", List(pl))
|
2020-03-14 03:53:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 03:19:39 +00:00
|
|
|
func TestIgnoreLocallyBoundPorts(t *testing.T) {
|
2021-02-02 19:30:46 +00:00
|
|
|
tstest.ResourceCheck(t)
|
2020-05-19 06:48:55 +01:00
|
|
|
|
2020-03-18 03:19:39 +00:00
|
|
|
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
|
2022-10-22 05:30:40 +01:00
|
|
|
var p Poller
|
2023-06-06 04:25:53 +01:00
|
|
|
pl, _, err := p.Poll()
|
2020-03-18 03:19:39 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-06 04:25:53 +01:00
|
|
|
func TestPoller(t *testing.T) {
|
2022-10-24 04:56:28 +01:00
|
|
|
var p Poller
|
2023-05-24 17:52:45 +01:00
|
|
|
p.IncludeLocalhost = true
|
2022-10-24 04:56:28 +01:00
|
|
|
get := func(t *testing.T) []Port {
|
|
|
|
t.Helper()
|
2023-06-06 04:25:53 +01:00
|
|
|
s, _, err := p.Poll()
|
2022-10-24 04:56:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-06-06 04:25:53 +01:00
|
|
|
return s
|
2022-10-24 04:56:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
p1 := get(t)
|
2023-05-24 17:52:45 +01:00
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
2022-10-24 04:56:28 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 02:02:02 +01:00
|
|
|
func TestEqualLessThan(t *testing.T) {
|
2021-01-10 19:52:11 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
a, b Port
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"Port a < b",
|
2022-10-24 02:02:02 +01:00
|
|
|
Port{Proto: "tcp", Port: 100, Process: "proc1"},
|
|
|
|
Port{Proto: "tcp", Port: 101, Process: "proc1"},
|
2021-01-10 19:52:11 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Port a > b",
|
2022-10-24 02:02:02 +01:00
|
|
|
Port{Proto: "tcp", Port: 101, Process: "proc1"},
|
|
|
|
Port{Proto: "tcp", Port: 100, Process: "proc1"},
|
2021-01-10 19:52:11 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Proto a < b",
|
2022-10-24 02:02:02 +01:00
|
|
|
Port{Proto: "tcp", Port: 100, Process: "proc1"},
|
|
|
|
Port{Proto: "udp", Port: 100, Process: "proc1"},
|
2021-01-10 19:52:11 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Proto a < b",
|
2022-10-24 02:02:02 +01:00
|
|
|
Port{Proto: "udp", Port: 100, Process: "proc1"},
|
|
|
|
Port{Proto: "tcp", Port: 100, Process: "proc1"},
|
2021-01-10 19:52:11 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Process a < b",
|
2022-10-24 02:02:02 +01:00
|
|
|
Port{Proto: "tcp", Port: 100, Process: "proc1"},
|
|
|
|
Port{Proto: "tcp", Port: 100, Process: "proc2"},
|
2021-01-10 19:52:11 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Process a > b",
|
2022-10-24 02:02:02 +01:00
|
|
|
Port{Proto: "tcp", Port: 100, Process: "proc2"},
|
|
|
|
Port{Proto: "tcp", Port: 100, Process: "proc1"},
|
2021-01-10 19:52:11 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Port evaluated first",
|
2022-10-24 02:02:02 +01:00
|
|
|
Port{Proto: "udp", Port: 100, Process: "proc2"},
|
|
|
|
Port{Proto: "tcp", Port: 101, Process: "proc1"},
|
2021-01-10 19:52:11 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Proto evaluated second",
|
2022-10-24 02:02:02 +01:00
|
|
|
Port{Proto: "tcp", Port: 100, Process: "proc2"},
|
|
|
|
Port{Proto: "udp", Port: 100, Process: "proc1"},
|
2021-01-10 19:52:11 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Process evaluated fourth",
|
2022-10-24 02:02:02 +01:00
|
|
|
Port{Proto: "tcp", Port: 100, Process: "proc1"},
|
|
|
|
Port{Proto: "tcp", Port: 100, Process: "proc2"},
|
2021-01-10 19:52:11 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"equal",
|
2022-10-24 02:02:02 +01:00
|
|
|
Port{Proto: "tcp", Port: 100, Process: "proc1"},
|
|
|
|
Port{Proto: "tcp", Port: 100, Process: "proc1"},
|
2021-01-10 19:52:11 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
got := tt.a.lessThan(&tt.b)
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("%s: Equal = %v; want %v", tt.name, got, tt.want)
|
|
|
|
}
|
2022-10-24 02:02:02 +01:00
|
|
|
lessBack := tt.b.lessThan(&tt.a)
|
|
|
|
if got && lessBack {
|
|
|
|
t.Errorf("%s: both a and b report being less than each other", tt.name)
|
|
|
|
}
|
|
|
|
wantEqual := !got && !lessBack
|
|
|
|
gotEqual := tt.a.equal(&tt.b)
|
|
|
|
if gotEqual != wantEqual {
|
|
|
|
t.Errorf("%s: equal = %v; want %v", tt.name, gotEqual, wantEqual)
|
2021-01-10 20:14:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 17:33:59 +01:00
|
|
|
func TestClose(t *testing.T) {
|
|
|
|
var p Poller
|
|
|
|
err := p.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
p = Poller{}
|
|
|
|
_, _, err = p.Poll()
|
|
|
|
if err != nil {
|
|
|
|
t.Skipf("skipping due to poll error: %v", err)
|
|
|
|
}
|
|
|
|
err = p.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 03:53:58 +00:00
|
|
|
func BenchmarkGetList(b *testing.B) {
|
2022-10-23 04:30:17 +01:00
|
|
|
benchmarkGetList(b, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGetListIncremental(b *testing.B) {
|
|
|
|
benchmarkGetList(b, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkGetList(b *testing.B, incremental bool) {
|
2020-03-14 03:53:58 +00:00
|
|
|
b.ReportAllocs()
|
2022-10-22 05:30:40 +01:00
|
|
|
var p Poller
|
2023-06-06 04:25:53 +01:00
|
|
|
p.init()
|
|
|
|
if p.initErr != nil {
|
|
|
|
b.Skip(p.initErr)
|
|
|
|
}
|
|
|
|
b.Cleanup(func() { p.Close() })
|
2024-04-16 21:15:13 +01:00
|
|
|
for range b.N {
|
2022-10-23 04:30:17 +01:00
|
|
|
pl, err := p.getList()
|
2020-03-14 03:53:58 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2022-10-23 04:30:17 +01:00
|
|
|
if incremental {
|
|
|
|
p.prev = pl
|
|
|
|
}
|
2020-03-14 03:53:58 +00:00
|
|
|
}
|
|
|
|
}
|