From 713c5c9ab151a6091782aed1c54b77ca85220ad6 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 3 Jun 2021 08:18:59 -0700 Subject: [PATCH] net/{interfaces,netns}: change which build tag means mac/ios Network/System Extension We used to use "redo" for that, but it was pretty vague. Also, fix the build tags broken in interfaces_default_route_test.go from a9745a0b684bb92ccb1965709adea6e9a98c0cd6, moving those Linux-specific tests to interfaces_linux_test.go. Signed-off-by: Brad Fitzpatrick --- ipn/ipnlocal/peerapi_macios_ext.go | 2 +- .../interfaces_default_route_test.go | 67 +----------------- net/interfaces/interfaces_linux_test.go | 69 ++++++++++++++++++- net/netns/netns_darwin_tailscaled.go | 2 +- net/netns/netns_default.go | 2 +- 5 files changed, 72 insertions(+), 70 deletions(-) diff --git a/ipn/ipnlocal/peerapi_macios_ext.go b/ipn/ipnlocal/peerapi_macios_ext.go index 4915d5581..081430bcf 100644 --- a/ipn/ipnlocal/peerapi_macios_ext.go +++ b/ipn/ipnlocal/peerapi_macios_ext.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin,redo ios,redo +// +build darwin,ts_macext ios,ts_macext package ipnlocal diff --git a/net/interfaces/interfaces_default_route_test.go b/net/interfaces/interfaces_default_route_test.go index b3d4d2904..b8b2a3558 100644 --- a/net/interfaces/interfaces_default_route_test.go +++ b/net/interfaces/interfaces_default_route_test.go @@ -2,15 +2,11 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build linux,!redo +// +build linux darwin,!ts_macext package interfaces import ( - "fmt" - "io/ioutil" - "os" - "path/filepath" "testing" ) @@ -23,64 +19,3 @@ func TestDefaultRouteInterface(t *testing.T) { } t.Logf("got %q", v) } - -// test the specific /proc/net/route path as found on Google Cloud Run instances -func TestGoogleCloudRunDefaultRouteInterface(t *testing.T) { - dir := t.TempDir() - savedProcNetRoutePath := procNetRoutePath - defer func() { procNetRoutePath = savedProcNetRoutePath }() - procNetRoutePath = filepath.Join(dir, "CloudRun") - 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") - err := ioutil.WriteFile(procNetRoutePath, buf, 0644) - 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() - savedProcNetRoutePath := procNetRoutePath - defer func() { procNetRoutePath = savedProcNetRoutePath }() - procNetRoutePath = filepath.Join(dir, "VeryLong") - 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) - } - - for n := 0; n <= 1000; n++ { - 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) - } -} diff --git a/net/interfaces/interfaces_linux_test.go b/net/interfaces/interfaces_linux_test.go index 3aa69d2f6..dde9987b0 100644 --- a/net/interfaces/interfaces_linux_test.go +++ b/net/interfaces/interfaces_linux_test.go @@ -4,7 +4,74 @@ package interfaces -import "testing" +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "testing" +) + +// test the specific /proc/net/route path as found on Google Cloud Run instances +func TestGoogleCloudRunDefaultRouteInterface(t *testing.T) { + dir := t.TempDir() + savedProcNetRoutePath := procNetRoutePath + defer func() { procNetRoutePath = savedProcNetRoutePath }() + procNetRoutePath = filepath.Join(dir, "CloudRun") + 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") + err := ioutil.WriteFile(procNetRoutePath, buf, 0644) + 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() + savedProcNetRoutePath := procNetRoutePath + defer func() { procNetRoutePath = savedProcNetRoutePath }() + procNetRoutePath = filepath.Join(dir, "VeryLong") + 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) + } + + for n := 0; n <= 1000; n++ { + 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) + } +} func BenchmarkDefaultRouteInterface(b *testing.B) { b.ReportAllocs() diff --git a/net/netns/netns_darwin_tailscaled.go b/net/netns/netns_darwin_tailscaled.go index a5a323fd2..268f19558 100644 --- a/net/netns/netns_darwin_tailscaled.go +++ b/net/netns/netns_darwin_tailscaled.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin,!redo +// +build darwin,!ts_macext package netns diff --git a/net/netns/netns_default.go b/net/netns/netns_default.go index 0a0e0179b..e0714d026 100644 --- a/net/netns/netns_default.go +++ b/net/netns/netns_default.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build !linux,!windows,!darwin darwin,redo +// +build !linux,!windows,!darwin darwin,ts_macext package netns