2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-08-10 20:40:00 +01:00
|
|
|
|
|
|
|
package interfaces
|
|
|
|
|
2021-06-03 16:18:59 +01:00
|
|
|
import (
|
2022-02-28 22:49:37 +00:00
|
|
|
"errors"
|
2021-06-03 16:18:59 +01:00
|
|
|
"fmt"
|
2022-02-28 22:49:37 +00:00
|
|
|
"io/fs"
|
2021-06-03 16:18:59 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2023-03-04 20:04:55 +00:00
|
|
|
|
|
|
|
"tailscale.com/tstest"
|
2021-06-03 16:18:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// test the specific /proc/net/route path as found on Google Cloud Run instances
|
|
|
|
func TestGoogleCloudRunDefaultRouteInterface(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
2023-03-04 20:04:55 +00:00
|
|
|
tstest.Replace(t, &procNetRoutePath, filepath.Join(dir, "CloudRun"))
|
2021-06-03 16:18:59 +01:00
|
|
|
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")
|
2022-09-15 13:06:59 +01:00
|
|
|
err := os.WriteFile(procNetRoutePath, buf, 0644)
|
2021-06-03 16:18:59 +01:00
|
|
|
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()
|
2023-03-04 20:04:55 +00:00
|
|
|
tstest.Replace(t, &procNetRoutePath, filepath.Join(dir, "VeryLong"))
|
2021-06-03 16:18:59 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-02-03 17:07:40 +00:00
|
|
|
for n := 0; n <= 900; n++ {
|
2021-06-03 16:18:59 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-08-10 20:40:00 +01:00
|
|
|
|
2021-06-24 01:13:04 +01:00
|
|
|
// test the specific /proc/net/route path as found on AWS App Runner instances
|
|
|
|
func TestAwsAppRunnerDefaultRouteInterface(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
2023-03-04 20:04:55 +00:00
|
|
|
tstest.Replace(t, &procNetRoutePath, filepath.Join(dir, "CloudRun"))
|
2021-06-24 01:13:04 +01:00
|
|
|
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")
|
2022-09-15 13:06:59 +01:00
|
|
|
err := os.WriteFile(procNetRoutePath, buf, 0644)
|
2021-06-24 01:13:04 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 20:40:00 +01:00
|
|
|
func BenchmarkDefaultRouteInterface(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if _, err := DefaultRouteInterface(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-28 22:49:37 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|