mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.0 KiB
Go
117 lines
3.0 KiB
Go
2 years ago
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||
4 years ago
|
|
||
6 months ago
|
package netmon
|
||
4 years ago
|
|
||
3 years ago
|
import (
|
||
3 years ago
|
"errors"
|
||
3 years ago
|
"fmt"
|
||
3 years ago
|
"io/fs"
|
||
3 years ago
|
"os"
|
||
|
"path/filepath"
|
||
|
"testing"
|
||
2 years ago
|
|
||
|
"tailscale.com/tstest"
|
||
3 years ago
|
)
|
||
|
|
||
|
// test the specific /proc/net/route path as found on Google Cloud Run instances
|
||
|
func TestGoogleCloudRunDefaultRouteInterface(t *testing.T) {
|
||
|
dir := t.TempDir()
|
||
2 years ago
|
tstest.Replace(t, &procNetRoutePath, filepath.Join(dir, "CloudRun"))
|
||
3 years ago
|
buf := []byte("Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\tMTU\tWindow\tIRTT\n" +
|
||
|
"eth0\t8008FEA9\t00000000\t0001\t0\t0\t0\t01FFFFFF\t0\t0\t0\n" +
|
||
|
"eth1\t00000000\t00000000\t0001\t0\t0\t0\t00000000\t0\t0\t0\n")
|
||
2 years ago
|
err := os.WriteFile(procNetRoutePath, buf, 0644)
|
||
3 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
got, err := DefaultRouteInterface()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
if got != "eth1" {
|
||
|
t.Fatalf("got %s, want eth1", got)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// we read chunks of /proc/net/route at a time, test that files longer than the chunk
|
||
|
// size can be handled.
|
||
|
func TestExtremelyLongProcNetRoute(t *testing.T) {
|
||
|
dir := t.TempDir()
|
||
2 years ago
|
tstest.Replace(t, &procNetRoutePath, filepath.Join(dir, "VeryLong"))
|
||
3 years ago
|
f, err := os.Create(procNetRoutePath)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
_, err = f.Write([]byte("Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\tMTU\tWindow\tIRTT\n"))
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
3 years ago
|
for n := 0; n <= 900; n++ {
|
||
3 years ago
|
line := fmt.Sprintf("eth%d\t8008FEA9\t00000000\t0001\t0\t0\t0\t01FFFFFF\t0\t0\t0\n", n)
|
||
|
_, err := f.Write([]byte(line))
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
_, err = f.Write([]byte("tokenring1\t00000000\t00000000\t0001\t0\t0\t0\t00000000\t0\t0\t0\n"))
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
got, err := DefaultRouteInterface()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
if got != "tokenring1" {
|
||
|
t.Fatalf("got %q, want tokenring1", got)
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
3 years ago
|
// test the specific /proc/net/route path as found on AWS App Runner instances
|
||
|
func TestAwsAppRunnerDefaultRouteInterface(t *testing.T) {
|
||
|
dir := t.TempDir()
|
||
2 years ago
|
tstest.Replace(t, &procNetRoutePath, filepath.Join(dir, "CloudRun"))
|
||
3 years ago
|
buf := []byte("Iface\tDestination\tGateway\tFlags\tRefCnt\tUse\tMetric\tMask\tMTU\tWindow\tIRTT\n" +
|
||
|
"eth0\t00000000\tF9AFFEA9\t0003\t0\t0\t0\t00000000\t0\t0\t0\n" +
|
||
|
"*\tFEA9FEA9\t00000000\t0005\t0\t0\t0\tFFFFFFFF\t0\t0\t0\n" +
|
||
|
"ecs-eth0\t02AAFEA9\t01ACFEA9\t0007\t0\t0\t0\tFFFFFFFF\t0\t0\t0\n" +
|
||
|
"ecs-eth0\t00ACFEA9\t00000000\t0001\t0\t0\t0\t00FFFFFF\t0\t0\t0\n" +
|
||
|
"eth0\t00AFFEA9\t00000000\t0001\t0\t0\t0\t00FFFFFF\t0\t0\t0\n")
|
||
2 years ago
|
err := os.WriteFile(procNetRoutePath, buf, 0644)
|
||
3 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
got, err := DefaultRouteInterface()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
if got != "eth0" {
|
||
|
t.Fatalf("got %s, want eth0", got)
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
func BenchmarkDefaultRouteInterface(b *testing.B) {
|
||
|
b.ReportAllocs()
|
||
7 months ago
|
for range b.N {
|
||
4 years ago
|
if _, err := DefaultRouteInterface(); err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
3 years ago
|
|
||
|
func TestRouteLinuxNetlink(t *testing.T) {
|
||
|
d, err := defaultRouteFromNetlink()
|
||
|
if errors.Is(err, fs.ErrPermission) {
|
||
|
t.Skip(err)
|
||
|
}
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
t.Logf("Got: %+v", d)
|
||
|
}
|