2020-03-14 03:53:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package portlist
|
|
|
|
|
2020-03-18 03:19:39 +00:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
2020-05-19 05:48:55 +00: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 05:48:55 +00:00
|
|
|
|
2022-10-22 04:30:40 +00:00
|
|
|
var p Poller
|
|
|
|
pl, err := p.getList()
|
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)
|
|
|
|
}
|
|
|
|
t.Logf("As String: %v", pl.String())
|
|
|
|
}
|
|
|
|
|
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 05:48:55 +00: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 04:30:40 +00:00
|
|
|
var p Poller
|
|
|
|
pl, err := p.getList()
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 01:02:02 +00: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 01:02:02 +00: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 01:02:02 +00: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 01:02:02 +00: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 01:02:02 +00: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 01:02:02 +00: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 01:02:02 +00: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 01:02:02 +00: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 01:02:02 +00: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 01:02:02 +00: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 01:02:02 +00: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 01:02:02 +00: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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 03:53:58 +00:00
|
|
|
func BenchmarkGetList(b *testing.B) {
|
2022-10-23 03:30:17 +00: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 04:30:40 +00:00
|
|
|
var p Poller
|
2020-03-14 03:53:58 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2022-10-23 03:30:17 +00:00
|
|
|
pl, err := p.getList()
|
2020-03-14 03:53:58 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2022-10-23 03:30:17 +00:00
|
|
|
if incremental {
|
|
|
|
p.prev = pl
|
|
|
|
}
|
2020-03-14 03:53:58 +00:00
|
|
|
}
|
|
|
|
}
|