mirror of
https://github.com/tailscale/tailscale.git
synced 2024-12-04 23:45:34 +00:00
71029cea2d
This updates all source files to use a new standard header for copyright and license declaration. Notably, copyright no longer includes a date, and we now use the standard SPDX-License-Identifier header. This commit was done almost entirely mechanically with perl, and then some minimal manual fixes. Updates #6865 Signed-off-by: Will Norris <will@tailscale.com>
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build linux
|
|
|
|
package routetable
|
|
|
|
import (
|
|
"fmt"
|
|
"net/netip"
|
|
"testing"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func TestGetRouteTable(t *testing.T) {
|
|
routes, err := Get(1000)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Basic assertion: we have at least one 'default' route in the main table
|
|
var (
|
|
hasDefault bool
|
|
)
|
|
for _, route := range routes {
|
|
if route.Dst == defaultRouteIPv4 && route.Sys.(RouteEntryLinux).Table == unix.RT_TABLE_MAIN {
|
|
hasDefault = true
|
|
}
|
|
}
|
|
if !hasDefault {
|
|
t.Errorf("expected at least one default route; routes=%v", routes)
|
|
}
|
|
}
|
|
|
|
func TestRouteEntryFormatting(t *testing.T) {
|
|
testCases := []struct {
|
|
re RouteEntry
|
|
want string
|
|
}{
|
|
{
|
|
re: RouteEntry{
|
|
Family: 4,
|
|
Type: RouteTypeMulticast,
|
|
Dst: RouteDestination{Prefix: netip.MustParsePrefix("100.64.0.0/10")},
|
|
Gateway: netip.MustParseAddr("1.2.3.1"),
|
|
Interface: "tailscale0",
|
|
Sys: RouteEntryLinux{
|
|
Type: unix.RTN_UNICAST,
|
|
Table: 52,
|
|
Proto: unix.RTPROT_STATIC,
|
|
Src: netip.MustParseAddr("1.2.3.4"),
|
|
Priority: 555,
|
|
},
|
|
},
|
|
want: `{Family: IPv4, Type: multicast, Dst: 100.64.0.0/10, Gateway: 1.2.3.1, Interface: tailscale0, Sys: {Type: unicast, Table: 52, Proto: static, Src: 1.2.3.4, Priority: 555}}`,
|
|
},
|
|
{
|
|
re: RouteEntry{
|
|
Family: 4,
|
|
Type: RouteTypeUnicast,
|
|
Dst: RouteDestination{Prefix: netip.MustParsePrefix("1.2.3.0/24")},
|
|
Gateway: netip.MustParseAddr("1.2.3.1"),
|
|
Sys: RouteEntryLinux{
|
|
Type: unix.RTN_UNICAST,
|
|
Table: unix.RT_TABLE_MAIN,
|
|
Proto: unix.RTPROT_BOOT,
|
|
},
|
|
},
|
|
want: `{Family: IPv4, Dst: 1.2.3.0/24, Gateway: 1.2.3.1, Sys: {Type: unicast}}`,
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
t.Run("", func(t *testing.T) {
|
|
got := fmt.Sprint(tc.re)
|
|
if got != tc.want {
|
|
t.Fatalf("RouteEntry.String() = %q; want %q", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|