2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-03-04 04:12:09 +00:00
|
|
|
|
|
|
|
package portlist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2021-09-11 23:45:12 +00:00
|
|
|
"io"
|
2021-03-04 04:12:09 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
)
|
|
|
|
|
2021-09-13 02:49:37 +00:00
|
|
|
func TestFieldIndex(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in string
|
|
|
|
field int
|
|
|
|
want int
|
|
|
|
}{
|
|
|
|
{"foo", 0, 0},
|
|
|
|
{" foo", 0, 2},
|
|
|
|
{"foo bar", 1, 5},
|
|
|
|
{" foo bar", 1, 6},
|
|
|
|
{" foo bar", 2, -1},
|
|
|
|
{" foo bar ", 2, -1},
|
|
|
|
{" foo bar x", 2, 10},
|
|
|
|
{" 1: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 34062 1 0000000000000000 100 0 0 10 0",
|
|
|
|
2, 19},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
if got := fieldIndex([]byte(tt.in), tt.field); got != tt.want {
|
|
|
|
t.Errorf("fieldIndex(%q, %v) = %v; want %v", tt.in, tt.field, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 04:12:09 +00:00
|
|
|
func TestParsePorts(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
in string
|
2021-09-13 02:49:37 +00:00
|
|
|
file string
|
2022-10-24 01:02:02 +00:00
|
|
|
want map[string]*portMeta
|
2021-03-04 04:12:09 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
in: "header line (ignored)\n",
|
2022-10-24 01:02:02 +00:00
|
|
|
want: map[string]*portMeta{},
|
2021-03-04 04:12:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ipv4",
|
2021-09-13 02:49:37 +00:00
|
|
|
file: "tcp",
|
2021-03-04 04:12:09 +00:00
|
|
|
in: `header line
|
|
|
|
0: 0100007F:0277 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 22303 1 0000000000000000 100 0 0 10 0
|
|
|
|
1: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 34062 1 0000000000000000 100 0 0 10 0
|
|
|
|
2: 5501A8C0:ADD4 B25E9536:01BB 01 00000000:00000000 02:00000B2B 00000000 1000 0 155276677 2 0000000000000000 22 4 30 10 -1
|
|
|
|
`,
|
2022-10-24 01:02:02 +00:00
|
|
|
want: map[string]*portMeta{
|
2023-04-17 22:38:24 +00:00
|
|
|
"socket:[34062]": {
|
2022-10-24 01:02:02 +00:00
|
|
|
port: Port{Proto: "tcp", Port: 22},
|
|
|
|
},
|
2021-03-04 04:12:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ipv6",
|
2021-09-13 02:49:37 +00:00
|
|
|
file: "tcp6",
|
2021-03-04 04:12:09 +00:00
|
|
|
in: ` sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
|
|
|
|
0: 00000000000000000000000001000000:0277 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 35720 1 0000000000000000 100 0 0 10 0
|
|
|
|
1: 00000000000000000000000000000000:1F91 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 142240557 1 0000000000000000 100 0 0 10 0
|
|
|
|
2: 00000000000000000000000000000000:0016 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 34064 1 0000000000000000 100 0 0 10 0
|
|
|
|
3: 69050120005716BC64906EBE009ECD4D:D506 0047062600000000000000006E171268:01BB 01 00000000:00000000 02:0000009E 00000000 1000 0 151042856 2 0000000000000000 21 4 28 10 -1
|
|
|
|
`,
|
2022-10-24 01:02:02 +00:00
|
|
|
want: map[string]*portMeta{
|
2023-04-17 22:38:24 +00:00
|
|
|
"socket:[142240557]": {
|
2022-10-24 01:02:02 +00:00
|
|
|
port: Port{Proto: "tcp", Port: 8081},
|
|
|
|
},
|
2023-04-17 22:38:24 +00:00
|
|
|
"socket:[34064]": {
|
2022-10-24 01:02:02 +00:00
|
|
|
port: Port{Proto: "tcp", Port: 22},
|
|
|
|
},
|
2021-03-04 04:12:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-13 02:49:37 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
buf := bytes.NewBufferString(tt.in)
|
2021-03-04 04:12:09 +00:00
|
|
|
r := bufio.NewReader(buf)
|
2021-09-13 02:49:37 +00:00
|
|
|
file := "tcp"
|
|
|
|
if tt.file != "" {
|
|
|
|
file = tt.file
|
|
|
|
}
|
2023-05-24 16:52:45 +00:00
|
|
|
li := newLinuxImplBase(false)
|
2022-10-24 01:02:02 +00:00
|
|
|
err := li.parseProcNetFile(r, file)
|
2021-03-04 04:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-10-24 01:02:02 +00:00
|
|
|
for _, pm := range tt.want {
|
|
|
|
pm.keep = true
|
|
|
|
pm.needsProcName = true
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(li.known, tt.want, cmp.AllowUnexported(Port{}), cmp.AllowUnexported(portMeta{})); diff != "" {
|
2021-03-04 04:12:09 +00:00
|
|
|
t.Errorf("unexpected parsed ports (-got+want):\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-09-11 23:45:12 +00:00
|
|
|
|
|
|
|
func BenchmarkParsePorts(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
var contents bytes.Buffer
|
|
|
|
contents.WriteString(` sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
|
|
|
|
0: 00000000000000000000000001000000:0277 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 35720 1 0000000000000000 100 0 0 10 0
|
|
|
|
1: 00000000000000000000000000000000:1F91 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 1000 0 142240557 1 0000000000000000 100 0 0 10 0
|
|
|
|
2: 00000000000000000000000000000000:0016 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 34064 1 0000000000000000 100 0 0 10 0
|
|
|
|
`)
|
2024-04-16 20:15:13 +00:00
|
|
|
for range 50000 {
|
2021-09-11 23:45:12 +00:00
|
|
|
contents.WriteString(" 3: 69050120005716BC64906EBE009ECD4D:D506 0047062600000000000000006E171268:01BB 01 00000000:00000000 02:0000009E 00000000 1000 0 151042856 2 0000000000000000 21 4 28 10 -1\n")
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:52:45 +00:00
|
|
|
li := newLinuxImplBase(false)
|
2021-09-11 23:45:12 +00:00
|
|
|
|
|
|
|
r := bytes.NewReader(contents.Bytes())
|
|
|
|
br := bufio.NewReader(&contents)
|
|
|
|
b.ResetTimer()
|
2024-04-16 20:15:13 +00:00
|
|
|
for range b.N {
|
2021-09-11 23:45:12 +00:00
|
|
|
r.Seek(0, io.SeekStart)
|
|
|
|
br.Reset(r)
|
2022-10-24 01:02:02 +00:00
|
|
|
err := li.parseProcNetFile(br, "tcp6")
|
2021-09-11 23:45:12 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2022-10-24 01:02:02 +00:00
|
|
|
if len(li.known) != 2 {
|
|
|
|
b.Fatalf("wrong results; want 2 parsed got %d", len(li.known))
|
2021-09-13 04:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-05 21:26:29 +00:00
|
|
|
|
|
|
|
func BenchmarkFindProcessNames(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
li := &linuxImpl{}
|
|
|
|
need := map[string]*portMeta{
|
|
|
|
"something-we'll-never-find": new(portMeta),
|
|
|
|
}
|
2024-04-16 20:15:13 +00:00
|
|
|
for range b.N {
|
2022-11-05 21:26:29 +00:00
|
|
|
if err := li.findProcessNames(need); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|