2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-04-01 16:35:41 +00:00
|
|
|
|
|
|
|
package wgengine_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-12-09 23:12:20 +00:00
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
2021-04-01 16:35:41 +00:00
|
|
|
"tailscale.com/net/tstun"
|
2023-05-03 20:57:17 +00:00
|
|
|
"tailscale.com/tsd"
|
2024-03-19 22:32:34 +00:00
|
|
|
"tailscale.com/tstest"
|
2021-04-01 16:35:41 +00:00
|
|
|
"tailscale.com/types/logger"
|
|
|
|
"tailscale.com/wgengine"
|
|
|
|
"tailscale.com/wgengine/netstack"
|
|
|
|
"tailscale.com/wgengine/router"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsNetstack(t *testing.T) {
|
2023-05-03 20:57:17 +00:00
|
|
|
sys := new(tsd.System)
|
2024-03-19 22:32:34 +00:00
|
|
|
e, err := wgengine.NewUserspaceEngine(
|
|
|
|
tstest.WhileTestRunningLogger(t),
|
|
|
|
wgengine.Config{SetSubsystem: sys.Set},
|
|
|
|
)
|
2021-04-01 16:35:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer e.Close()
|
2023-05-03 20:57:17 +00:00
|
|
|
if !sys.IsNetstack() {
|
2021-04-01 16:35:41 +00:00
|
|
|
t.Errorf("IsNetstack = false; want true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsNetstackRouter(t *testing.T) {
|
|
|
|
tests := []struct {
|
2023-05-03 20:57:17 +00:00
|
|
|
name string
|
|
|
|
conf wgengine.Config
|
|
|
|
setNetstackRouter bool
|
|
|
|
want bool
|
2021-04-01 16:35:41 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no_netstack",
|
|
|
|
conf: wgengine.Config{
|
|
|
|
Tun: newFakeOSTUN(),
|
|
|
|
Router: newFakeOSRouter(),
|
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "netstack",
|
|
|
|
conf: wgengine.Config{},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hybrid_netstack",
|
|
|
|
conf: wgengine.Config{
|
|
|
|
Tun: newFakeOSTUN(),
|
|
|
|
Router: netstack.NewSubnetRouterWrapper(newFakeOSRouter()),
|
|
|
|
},
|
2023-05-03 20:57:17 +00:00
|
|
|
setNetstackRouter: true,
|
|
|
|
want: true,
|
2021-04-01 16:35:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-05-03 20:57:17 +00:00
|
|
|
sys := &tsd.System{}
|
|
|
|
if tt.setNetstackRouter {
|
|
|
|
sys.NetstackRouter.Set(true)
|
|
|
|
}
|
|
|
|
conf := tt.conf
|
|
|
|
conf.SetSubsystem = sys.Set
|
|
|
|
e, err := wgengine.NewUserspaceEngine(logger.Discard, conf)
|
2021-04-01 16:35:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer e.Close()
|
2023-05-03 20:57:17 +00:00
|
|
|
if got := sys.IsNetstackRouter(); got != tt.want {
|
2021-04-01 16:35:41 +00:00
|
|
|
t.Errorf("IsNetstackRouter = %v; want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFakeOSRouter() router.Router {
|
|
|
|
return someRandoOSRouter{router.NewFake(logger.Discard)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type someRandoOSRouter struct {
|
|
|
|
router.Router
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFakeOSTUN() tun.Device {
|
|
|
|
return someRandoOSTUN{tstun.NewFake()}
|
|
|
|
}
|
|
|
|
|
|
|
|
type someRandoOSTUN struct {
|
|
|
|
tun.Device
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns something that is not FakeTUN.
|
|
|
|
func (t someRandoOSTUN) Name() (string, error) { return "some_os_tun0", nil }
|