2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package magicsock
|
|
|
|
|
|
|
|
import (
|
2020-03-03 15:39:40 +00:00
|
|
|
"bytes"
|
2020-03-03 21:50:47 +00:00
|
|
|
crand "crypto/rand"
|
|
|
|
"crypto/tls"
|
2020-03-09 16:13:28 +00:00
|
|
|
"encoding/binary"
|
2020-02-05 22:16:58 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2020-03-03 21:50:47 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-03-06 21:35:59 +00:00
|
|
|
"os"
|
2020-02-05 22:16:58 +00:00
|
|
|
"strings"
|
2020-03-03 11:51:31 +00:00
|
|
|
"sync"
|
2020-02-05 22:16:58 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2020-03-03 11:51:31 +00:00
|
|
|
|
2020-03-06 18:37:57 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-03-03 15:39:40 +00:00
|
|
|
"github.com/tailscale/wireguard-go/device"
|
|
|
|
"github.com/tailscale/wireguard-go/tun/tuntest"
|
|
|
|
"github.com/tailscale/wireguard-go/wgcfg"
|
2020-03-03 21:50:47 +00:00
|
|
|
"tailscale.com/derp"
|
|
|
|
"tailscale.com/derp/derphttp"
|
2020-03-09 22:20:33 +00:00
|
|
|
"tailscale.com/derp/derpmap"
|
2020-03-03 11:51:31 +00:00
|
|
|
"tailscale.com/stun"
|
2020-03-03 21:50:47 +00:00
|
|
|
"tailscale.com/types/key"
|
2020-03-07 01:50:36 +00:00
|
|
|
"tailscale.com/types/logger"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestListen(t *testing.T) {
|
2020-03-03 21:50:47 +00:00
|
|
|
// TODO(crawshaw): when offline this test spends a while trying to connect to real derp servers.
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
epCh := make(chan string, 16)
|
|
|
|
epFunc := func(endpoints []string) {
|
|
|
|
for _, ep := range endpoints {
|
|
|
|
epCh <- ep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 11:51:31 +00:00
|
|
|
stunAddr, stunCleanupFn := serveSTUN(t)
|
|
|
|
defer stunCleanupFn()
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
port := pickPort(t)
|
|
|
|
conn, err := Listen(Options{
|
|
|
|
Port: port,
|
2020-03-09 22:20:33 +00:00
|
|
|
STUN: []string{stunAddr},
|
2020-02-05 22:16:58 +00:00
|
|
|
EndpointsFunc: epFunc,
|
2020-03-09 22:20:33 +00:00
|
|
|
Logf: t.Logf,
|
2020-02-05 22:16:58 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
go func() {
|
2020-02-18 16:57:11 +00:00
|
|
|
var pkt [64 << 10]byte
|
2020-02-05 22:16:58 +00:00
|
|
|
for {
|
|
|
|
_, _, _, err := conn.ReceiveIPv4(pkt[:])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-03-06 20:37:19 +00:00
|
|
|
timeout := time.After(10 * time.Second)
|
2020-02-05 22:16:58 +00:00
|
|
|
var endpoints []string
|
|
|
|
suffix := fmt.Sprintf(":%d", port)
|
|
|
|
collectEndpoints:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ep := <-epCh:
|
|
|
|
endpoints = append(endpoints, ep)
|
|
|
|
if strings.HasSuffix(ep, suffix) {
|
|
|
|
break collectEndpoints
|
|
|
|
}
|
2020-03-06 20:37:19 +00:00
|
|
|
case <-timeout:
|
2020-02-05 22:16:58 +00:00
|
|
|
t.Fatalf("timeout with endpoints: %v", endpoints)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pickPort(t *testing.T) uint16 {
|
|
|
|
t.Helper()
|
|
|
|
conn, err := net.ListenPacket("udp4", ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
return uint16(conn.LocalAddr().(*net.UDPAddr).Port)
|
|
|
|
}
|
2020-02-18 21:32:04 +00:00
|
|
|
|
|
|
|
func TestDerpIPConstant(t *testing.T) {
|
2020-03-04 01:46:03 +00:00
|
|
|
if DerpMagicIP != derpMagicIP.String() {
|
|
|
|
t.Errorf("str %q != IP %v", DerpMagicIP, derpMagicIP)
|
|
|
|
}
|
|
|
|
if len(derpMagicIP) != 4 {
|
|
|
|
t.Errorf("derpMagicIP is len %d; want 4", len(derpMagicIP))
|
2020-02-18 21:32:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-04 06:21:56 +00:00
|
|
|
|
|
|
|
func TestPickDERPFallback(t *testing.T) {
|
2020-03-09 22:20:33 +00:00
|
|
|
c := &Conn{
|
|
|
|
derps: derpmap.Prod(),
|
2020-03-04 06:21:56 +00:00
|
|
|
}
|
|
|
|
a := c.pickDERPFallback()
|
|
|
|
if a == 0 {
|
|
|
|
t.Fatalf("pickDERPFallback returned 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that it's consistent.
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
b := c.pickDERPFallback()
|
|
|
|
if a != b {
|
|
|
|
t.Fatalf("got inconsistent %d vs %d values", a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that that the pointer value of c is blended in and
|
|
|
|
// distribution over nodes works.
|
|
|
|
got := map[int]int{}
|
|
|
|
for i := 0; i < 50; i++ {
|
2020-03-09 22:20:33 +00:00
|
|
|
c = &Conn{derps: derpmap.Prod()}
|
2020-03-04 06:21:56 +00:00
|
|
|
got[c.pickDERPFallback()]++
|
|
|
|
}
|
|
|
|
t.Logf("distribution: %v", got)
|
|
|
|
if len(got) < 2 {
|
|
|
|
t.Errorf("expected more than 1 node; got %v", got)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that stickiness works.
|
|
|
|
const someNode = 123456
|
|
|
|
c.myDerp = someNode
|
|
|
|
if got := c.pickDERPFallback(); got != someNode {
|
|
|
|
t.Errorf("not sticky: got %v; want %v", got, someNode)
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 11:51:31 +00:00
|
|
|
|
|
|
|
type stunStats struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
readIPv4 int
|
|
|
|
readIPv6 int
|
|
|
|
}
|
|
|
|
|
2020-03-09 22:20:33 +00:00
|
|
|
func serveSTUN(t *testing.T) (addr string, cleanupFn func()) {
|
2020-03-03 11:51:31 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// TODO(crawshaw): use stats to test re-STUN logic
|
|
|
|
var stats stunStats
|
|
|
|
|
|
|
|
pc, err := net.ListenPacket("udp4", ":3478")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to open STUN listener: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-09 22:20:33 +00:00
|
|
|
stunAddr := pc.LocalAddr().String()
|
|
|
|
stunAddr = strings.Replace(stunAddr, "0.0.0.0:", "localhost:", 1)
|
|
|
|
|
2020-03-11 00:30:28 +00:00
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go runSTUN(t, pc, &stats, doneCh)
|
|
|
|
return stunAddr, func() {
|
|
|
|
pc.Close()
|
|
|
|
<-doneCh
|
|
|
|
}
|
2020-03-03 11:51:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 00:30:28 +00:00
|
|
|
func runSTUN(t *testing.T, pc net.PacketConn, stats *stunStats, done chan struct{}) {
|
|
|
|
defer func() { done <- struct{}{} }()
|
|
|
|
|
2020-03-03 11:51:31 +00:00
|
|
|
var buf [64 << 10]byte
|
|
|
|
for {
|
|
|
|
n, addr, err := pc.ReadFrom(buf[:])
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "closed network connection") {
|
2020-03-07 21:11:52 +00:00
|
|
|
t.Logf("STUN server shutdown")
|
2020-03-03 11:51:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ua := addr.(*net.UDPAddr)
|
|
|
|
pkt := buf[:n]
|
|
|
|
if !stun.Is(pkt) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
txid, err := stun.ParseBindingRequest(pkt)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.mu.Lock()
|
|
|
|
if ua.IP.To4() != nil {
|
|
|
|
stats.readIPv4++
|
|
|
|
} else {
|
|
|
|
stats.readIPv6++
|
|
|
|
}
|
|
|
|
stats.mu.Unlock()
|
|
|
|
|
|
|
|
res := stun.Response(txid, ua.IP, uint16(ua.Port))
|
|
|
|
if _, err := pc.WriteTo(res, addr); err != nil {
|
2020-03-07 21:11:52 +00:00
|
|
|
t.Logf("STUN server write failed: %v", err)
|
2020-03-03 11:51:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 15:39:40 +00:00
|
|
|
|
|
|
|
func makeConfigs(t *testing.T, ports []uint16) []wgcfg.Config {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
var privKeys []wgcfg.PrivateKey
|
|
|
|
var addresses [][]wgcfg.CIDR
|
|
|
|
|
|
|
|
for i := range ports {
|
|
|
|
privKey, err := wgcfg.NewPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
privKeys = append(privKeys, privKey)
|
|
|
|
|
|
|
|
addresses = append(addresses, []wgcfg.CIDR{
|
|
|
|
parseCIDR(t, fmt.Sprintf("1.0.0.%d/32", i+1)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfgs []wgcfg.Config
|
|
|
|
for i, port := range ports {
|
|
|
|
cfg := wgcfg.Config{
|
|
|
|
Name: fmt.Sprintf("peer%d", i+1),
|
|
|
|
PrivateKey: privKeys[i],
|
|
|
|
Addresses: addresses[i],
|
|
|
|
ListenPort: port,
|
|
|
|
}
|
|
|
|
for peerNum, port := range ports {
|
|
|
|
if peerNum == i {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
peer := wgcfg.Peer{
|
|
|
|
PublicKey: privKeys[peerNum].Public(),
|
|
|
|
AllowedIPs: addresses[peerNum],
|
|
|
|
Endpoints: []wgcfg.Endpoint{{
|
|
|
|
Host: "127.0.0.1",
|
|
|
|
Port: port,
|
|
|
|
}},
|
2020-03-03 21:50:47 +00:00
|
|
|
PersistentKeepalive: 25,
|
2020-03-03 15:39:40 +00:00
|
|
|
}
|
|
|
|
cfg.Peers = append(cfg.Peers, peer)
|
|
|
|
}
|
|
|
|
cfgs = append(cfgs, cfg)
|
|
|
|
}
|
|
|
|
return cfgs
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCIDR(t *testing.T, addr string) wgcfg.CIDR {
|
|
|
|
t.Helper()
|
|
|
|
cidr, err := wgcfg.ParseCIDR(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return *cidr
|
|
|
|
}
|
|
|
|
|
2020-03-03 21:50:47 +00:00
|
|
|
func runDERP(t *testing.T) (s *derp.Server, addr string, cleanupFn func()) {
|
|
|
|
var serverPrivateKey key.Private
|
|
|
|
if _, err := crand.Read(serverPrivateKey[:]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s = derp.NewServer(serverPrivateKey, t.Logf)
|
|
|
|
|
|
|
|
httpsrv := httptest.NewUnstartedServer(derphttp.Handler(s))
|
|
|
|
httpsrv.Config.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
|
|
|
|
httpsrv.StartTLS()
|
|
|
|
t.Logf("DERP server URL: %s", httpsrv.URL)
|
|
|
|
|
|
|
|
addr = strings.TrimPrefix(httpsrv.URL, "https://")
|
|
|
|
cleanupFn = func() {
|
2020-03-11 00:31:27 +00:00
|
|
|
httpsrv.CloseClientConnections()
|
|
|
|
httpsrv.Close()
|
2020-03-03 21:50:47 +00:00
|
|
|
s.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, addr, cleanupFn
|
|
|
|
}
|
|
|
|
|
2020-03-07 01:50:36 +00:00
|
|
|
// devLogger returns a wireguard-go device.Logger that writes
|
|
|
|
// wireguard logs to the test logger.
|
|
|
|
func devLogger(t *testing.T, prefix string) *device.Logger {
|
|
|
|
pfx := []interface{}{prefix}
|
|
|
|
logf := func(format string, args ...interface{}) {
|
2020-03-11 00:32:38 +00:00
|
|
|
t.Helper()
|
2020-03-07 01:50:36 +00:00
|
|
|
t.Logf("%s: "+format, append(pfx, args...)...)
|
|
|
|
}
|
|
|
|
return &device.Logger{
|
|
|
|
Debug: logger.StdLogger(logf),
|
|
|
|
Info: logger.StdLogger(logf),
|
|
|
|
Error: logger.StdLogger(logf),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestDeviceStartStop exercises the startup and shutdown logic of
|
|
|
|
// wireguard-go, which is intimately intertwined with magicsock's own
|
|
|
|
// lifecycle. We seem to be good at generating deadlocks here, so if
|
|
|
|
// this test fails you should suspect a deadlock somewhere in startup
|
|
|
|
// or shutdown. It may be an infrequent flake, so run with
|
|
|
|
// -count=10000 to be sure.
|
|
|
|
func TestDeviceStartStop(t *testing.T) {
|
|
|
|
conn, err := Listen(Options{
|
|
|
|
EndpointsFunc: func(eps []string) {},
|
2020-03-09 22:20:33 +00:00
|
|
|
Logf: t.Logf,
|
2020-03-07 01:50:36 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
tun := tuntest.NewChannelTUN()
|
|
|
|
dev := device.NewDevice(tun.TUN(), &device.DeviceOptions{
|
|
|
|
Logger: devLogger(t, "dev"),
|
|
|
|
CreateEndpoint: conn.CreateEndpoint,
|
|
|
|
CreateBind: conn.CreateBind,
|
|
|
|
SkipBindUpdate: true,
|
|
|
|
})
|
|
|
|
dev.Up()
|
|
|
|
dev.Close()
|
|
|
|
}
|
|
|
|
|
2020-03-03 15:39:40 +00:00
|
|
|
func TestTwoDevicePing(t *testing.T) {
|
2020-03-03 21:50:47 +00:00
|
|
|
// Wipe default DERP list, add local server.
|
|
|
|
// (Do it now, or derpHost will try to connect to derp1.tailscale.com.)
|
|
|
|
derpServer, derpAddr, derpCleanupFn := runDERP(t)
|
|
|
|
defer derpCleanupFn()
|
|
|
|
|
2020-03-03 15:39:40 +00:00
|
|
|
stunAddr, stunCleanupFn := serveSTUN(t)
|
|
|
|
defer stunCleanupFn()
|
|
|
|
|
2020-03-09 22:20:33 +00:00
|
|
|
derps := derpmap.NewTestWorldWith(&derpmap.Server{
|
|
|
|
ID: 1,
|
|
|
|
HostHTTPS: derpAddr,
|
|
|
|
STUN4: stunAddr,
|
|
|
|
Geo: "Testopolis",
|
|
|
|
})
|
|
|
|
|
2020-03-03 15:39:40 +00:00
|
|
|
epCh1 := make(chan []string, 16)
|
|
|
|
conn1, err := Listen(Options{
|
2020-03-09 22:20:33 +00:00
|
|
|
Logf: logger.WithPrefix(t.Logf, "conn1: "),
|
|
|
|
STUN: []string{stunAddr},
|
2020-03-03 15:39:40 +00:00
|
|
|
EndpointsFunc: func(eps []string) {
|
|
|
|
epCh1 <- eps
|
|
|
|
},
|
2020-03-07 00:20:05 +00:00
|
|
|
derpTLSConfig: &tls.Config{InsecureSkipVerify: true},
|
2020-03-03 15:39:40 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-09 22:20:33 +00:00
|
|
|
conn1.derps = derps
|
2020-03-03 15:39:40 +00:00
|
|
|
defer conn1.Close()
|
|
|
|
|
|
|
|
epCh2 := make(chan []string, 16)
|
|
|
|
conn2, err := Listen(Options{
|
2020-03-09 22:20:33 +00:00
|
|
|
Logf: logger.WithPrefix(t.Logf, "conn2: "),
|
|
|
|
STUN: []string{stunAddr},
|
2020-03-03 15:39:40 +00:00
|
|
|
EndpointsFunc: func(eps []string) {
|
|
|
|
epCh2 <- eps
|
|
|
|
},
|
2020-03-07 00:20:05 +00:00
|
|
|
derpTLSConfig: &tls.Config{InsecureSkipVerify: true},
|
2020-03-03 15:39:40 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-09 22:20:33 +00:00
|
|
|
conn2.derps = derps
|
2020-03-03 15:39:40 +00:00
|
|
|
defer conn2.Close()
|
|
|
|
|
|
|
|
ports := []uint16{conn1.LocalPort(), conn2.LocalPort()}
|
|
|
|
cfgs := makeConfigs(t, ports)
|
|
|
|
|
2020-03-03 21:50:47 +00:00
|
|
|
if err := conn1.SetPrivateKey(cfgs[0].PrivateKey); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := conn2.SetPrivateKey(cfgs[1].PrivateKey); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-03-06 20:37:19 +00:00
|
|
|
//uapi1, _ := cfgs[0].ToUAPI()
|
|
|
|
//t.Logf("cfg0: %v", uapi1)
|
|
|
|
//uapi2, _ := cfgs[1].ToUAPI()
|
|
|
|
//t.Logf("cfg1: %v", uapi2)
|
|
|
|
|
2020-03-03 15:39:40 +00:00
|
|
|
tun1 := tuntest.NewChannelTUN()
|
|
|
|
dev1 := device.NewDevice(tun1.TUN(), &device.DeviceOptions{
|
2020-03-07 01:50:36 +00:00
|
|
|
Logger: devLogger(t, "dev1"),
|
2020-03-03 15:39:40 +00:00
|
|
|
CreateEndpoint: conn1.CreateEndpoint,
|
|
|
|
CreateBind: conn1.CreateBind,
|
|
|
|
SkipBindUpdate: true,
|
|
|
|
})
|
|
|
|
dev1.Up()
|
|
|
|
if err := dev1.Reconfig(&cfgs[0]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-10 18:57:14 +00:00
|
|
|
defer dev1.Close()
|
2020-03-03 15:39:40 +00:00
|
|
|
|
|
|
|
tun2 := tuntest.NewChannelTUN()
|
|
|
|
dev2 := device.NewDevice(tun2.TUN(), &device.DeviceOptions{
|
2020-03-07 01:50:36 +00:00
|
|
|
Logger: devLogger(t, "dev2"),
|
2020-03-03 15:39:40 +00:00
|
|
|
CreateEndpoint: conn2.CreateEndpoint,
|
|
|
|
CreateBind: conn2.CreateBind,
|
|
|
|
SkipBindUpdate: true,
|
|
|
|
})
|
|
|
|
dev2.Up()
|
2020-03-10 18:57:14 +00:00
|
|
|
defer dev2.Close()
|
2020-03-03 21:50:47 +00:00
|
|
|
|
2020-03-03 15:39:40 +00:00
|
|
|
if err := dev2.Reconfig(&cfgs[1]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ping1 := func(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
msg2to1 := tuntest.Ping(net.ParseIP("1.0.0.1"), net.ParseIP("1.0.0.2"))
|
|
|
|
tun2.Outbound <- msg2to1
|
|
|
|
select {
|
|
|
|
case msgRecv := <-tun1.Inbound:
|
|
|
|
if !bytes.Equal(msg2to1, msgRecv) {
|
|
|
|
t.Error("ping did not transit correctly")
|
|
|
|
}
|
2020-03-06 20:37:19 +00:00
|
|
|
case <-time.After(3 * time.Second):
|
2020-03-03 15:39:40 +00:00
|
|
|
t.Error("ping did not transit")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ping2 := func(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
msg1to2 := tuntest.Ping(net.ParseIP("1.0.0.2"), net.ParseIP("1.0.0.1"))
|
|
|
|
tun1.Outbound <- msg1to2
|
|
|
|
select {
|
|
|
|
case msgRecv := <-tun2.Inbound:
|
|
|
|
if !bytes.Equal(msg1to2, msgRecv) {
|
|
|
|
t.Error("return ping did not transit correctly")
|
|
|
|
}
|
2020-03-06 20:37:19 +00:00
|
|
|
case <-time.After(3 * time.Second):
|
2020-03-03 15:39:40 +00:00
|
|
|
t.Error("return ping did not transit")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("ping 1.0.0.1", func(t *testing.T) { ping1(t) })
|
|
|
|
t.Run("ping 1.0.0.2", func(t *testing.T) { ping2(t) })
|
|
|
|
t.Run("ping 1.0.0.2 via SendPacket", func(t *testing.T) {
|
|
|
|
msg1to2 := tuntest.Ping(net.ParseIP("1.0.0.2"), net.ParseIP("1.0.0.1"))
|
|
|
|
if err := dev1.SendPacket(msg1to2); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case msgRecv := <-tun2.Inbound:
|
|
|
|
if !bytes.Equal(msg1to2, msgRecv) {
|
|
|
|
t.Error("return ping did not transit correctly")
|
|
|
|
}
|
2020-03-06 20:37:19 +00:00
|
|
|
case <-time.After(3 * time.Second):
|
2020-03-03 15:39:40 +00:00
|
|
|
t.Error("return ping did not transit")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no-op dev1 reconfig", func(t *testing.T) {
|
|
|
|
if err := dev1.Reconfig(&cfgs[0]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ping1(t)
|
|
|
|
ping2(t)
|
|
|
|
})
|
2020-03-03 16:42:31 +00:00
|
|
|
|
2020-03-08 13:25:45 +00:00
|
|
|
if os.Getenv("RUN_CURSED_TESTS") == "" {
|
|
|
|
t.Skip("test is very broken, don't run in CI until it's reliable.")
|
|
|
|
}
|
|
|
|
|
2020-03-06 20:43:49 +00:00
|
|
|
pingSeq := func(t *testing.T, count int, totalTime time.Duration, strict bool) {
|
2020-03-03 16:42:31 +00:00
|
|
|
msg := func(i int) []byte {
|
|
|
|
b := tuntest.Ping(net.ParseIP("1.0.0.2"), net.ParseIP("1.0.0.1"))
|
|
|
|
b[len(b)-1] = byte(i) // set seq num
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-03-06 18:37:57 +00:00
|
|
|
// Space out ping transmissions so that the overall
|
|
|
|
// transmission happens in totalTime.
|
|
|
|
//
|
|
|
|
// We do this because the packet spray logic in magicsock is
|
|
|
|
// time-based to allow for reliable NAT traversal. However,
|
|
|
|
// for the packet spraying test further down, there needs to
|
|
|
|
// be at least 1 sprayed packet that is not the handshake, in
|
|
|
|
// case the handshake gets eaten by the race resolution logic.
|
|
|
|
//
|
|
|
|
// This is an inherent "race by design" in our current
|
|
|
|
// magicsock+wireguard-go codebase: sometimes, racing
|
|
|
|
// handshakes will result in a sub-optimal path for a few
|
|
|
|
// hundred milliseconds, until a subsequent spray corrects the
|
|
|
|
// issue. In order for the test to reflect that magicsock
|
|
|
|
// works as designed, we have to space out packet transmission
|
|
|
|
// here.
|
|
|
|
interPacketGap := totalTime / time.Duration(count)
|
|
|
|
if interPacketGap < 1*time.Millisecond {
|
|
|
|
interPacketGap = 0
|
|
|
|
}
|
|
|
|
|
2020-03-03 16:42:31 +00:00
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
b := msg(i)
|
|
|
|
tun1.Outbound <- b
|
2020-03-06 18:37:57 +00:00
|
|
|
time.Sleep(interPacketGap)
|
2020-03-03 16:42:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
b := msg(i)
|
|
|
|
select {
|
|
|
|
case msgRecv := <-tun2.Inbound:
|
|
|
|
if !bytes.Equal(b, msgRecv) {
|
2020-03-06 20:43:49 +00:00
|
|
|
if strict {
|
|
|
|
t.Errorf("return ping %d did not transit correctly: %s", i, cmp.Diff(b, msgRecv))
|
|
|
|
}
|
2020-03-03 16:42:31 +00:00
|
|
|
}
|
2020-03-06 20:37:19 +00:00
|
|
|
case <-time.After(3 * time.Second):
|
2020-03-06 20:43:49 +00:00
|
|
|
if strict {
|
2020-03-07 21:11:52 +00:00
|
|
|
t.Errorf("return ping %d did not transit", i)
|
2020-03-06 20:43:49 +00:00
|
|
|
}
|
2020-03-03 16:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 21:50:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("ping 1.0.0.1 x50", func(t *testing.T) {
|
2020-03-06 20:43:49 +00:00
|
|
|
pingSeq(t, 50, 0, true)
|
2020-03-03 21:50:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Add DERP relay.
|
|
|
|
derpEp := wgcfg.Endpoint{Host: "127.3.3.40", Port: 1}
|
|
|
|
ep0 := cfgs[0].Peers[0].Endpoints
|
|
|
|
ep0 = append([]wgcfg.Endpoint{derpEp}, ep0...)
|
|
|
|
cfgs[0].Peers[0].Endpoints = ep0
|
|
|
|
ep1 := cfgs[1].Peers[0].Endpoints
|
|
|
|
ep1 = append([]wgcfg.Endpoint{derpEp}, ep1...)
|
|
|
|
cfgs[1].Peers[0].Endpoints = ep1
|
|
|
|
if err := dev1.Reconfig(&cfgs[0]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := dev2.Reconfig(&cfgs[1]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("add DERP", func(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
t.Logf("DERP vars: %s", derpServer.ExpVar().String())
|
|
|
|
}()
|
2020-03-06 20:43:49 +00:00
|
|
|
pingSeq(t, 20, 0, true)
|
2020-03-03 21:50:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Disable real route.
|
|
|
|
cfgs[0].Peers[0].Endpoints = []wgcfg.Endpoint{derpEp}
|
|
|
|
cfgs[1].Peers[0].Endpoints = []wgcfg.Endpoint{derpEp}
|
|
|
|
if err := dev1.Reconfig(&cfgs[0]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := dev2.Reconfig(&cfgs[1]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
time.Sleep(250 * time.Millisecond) // TODO remove
|
|
|
|
|
|
|
|
t.Run("all traffic over DERP", func(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
t.Logf("DERP vars: %s", derpServer.ExpVar().String())
|
|
|
|
if t.Failed() || true {
|
|
|
|
uapi1, _ := cfgs[0].ToUAPI()
|
|
|
|
t.Logf("cfg0: %v", uapi1)
|
|
|
|
uapi2, _ := cfgs[1].ToUAPI()
|
|
|
|
t.Logf("cfg1: %v", uapi2)
|
|
|
|
}
|
|
|
|
}()
|
2020-03-06 20:43:49 +00:00
|
|
|
pingSeq(t, 20, 0, true)
|
2020-03-03 21:50:47 +00:00
|
|
|
})
|
|
|
|
|
2020-03-06 18:37:57 +00:00
|
|
|
dev1.RemoveAllPeers()
|
|
|
|
dev2.RemoveAllPeers()
|
|
|
|
|
|
|
|
// Give one peer a non-DERP endpoint. We expect the other to
|
|
|
|
// accept it via roamAddr.
|
2020-03-03 21:50:47 +00:00
|
|
|
cfgs[0].Peers[0].Endpoints = ep0
|
|
|
|
if ep2 := cfgs[1].Peers[0].Endpoints; len(ep2) != 1 {
|
|
|
|
t.Errorf("unexpected peer endpoints in dev2: %v", ep2)
|
|
|
|
}
|
2020-03-06 18:37:57 +00:00
|
|
|
if err := dev2.Reconfig(&cfgs[1]); err != nil {
|
2020-03-03 21:50:47 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-06 18:37:57 +00:00
|
|
|
if err := dev1.Reconfig(&cfgs[0]); err != nil {
|
2020-03-03 21:50:47 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-03-06 18:37:57 +00:00
|
|
|
// Dear future human debugging a test failure here: this test is
|
|
|
|
// flaky, and very infrequently will drop 1-2 of the 50 ping
|
|
|
|
// packets. This does not affect normal operation of tailscaled,
|
|
|
|
// but makes this test fail.
|
|
|
|
//
|
|
|
|
// TODO(danderson): finish root-causing and de-flake this test.
|
2020-03-03 21:50:47 +00:00
|
|
|
t.Run("one real route is enough thanks to spray", func(t *testing.T) {
|
2020-03-06 20:43:49 +00:00
|
|
|
pingSeq(t, 50, 700*time.Millisecond, false)
|
2020-03-03 21:50:47 +00:00
|
|
|
|
|
|
|
ep2 := dev2.Config().Peers[0].Endpoints
|
|
|
|
if len(ep2) != 2 {
|
|
|
|
t.Error("handshake spray failed to find real route")
|
|
|
|
}
|
2020-03-03 16:42:31 +00:00
|
|
|
})
|
2020-03-03 15:39:40 +00:00
|
|
|
}
|
2020-03-09 16:13:28 +00:00
|
|
|
|
|
|
|
// TestAddrSet tests AddrSet appendDests and UpdateDst.
|
|
|
|
func TestAddrSet(t *testing.T) {
|
|
|
|
mustUDPAddr := func(s string) *net.UDPAddr {
|
|
|
|
t.Helper()
|
|
|
|
ua, err := net.ResolveUDPAddr("udp", s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return ua
|
|
|
|
}
|
|
|
|
udpAddrs := func(ss ...string) (ret []net.UDPAddr) {
|
|
|
|
t.Helper()
|
|
|
|
for _, s := range ss {
|
|
|
|
ret = append(ret, *mustUDPAddr(s))
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
joinUDPs := func(in []*net.UDPAddr) string {
|
|
|
|
var sb strings.Builder
|
|
|
|
for i, ua := range in {
|
|
|
|
if i > 0 {
|
|
|
|
sb.WriteByte(',')
|
|
|
|
}
|
|
|
|
sb.WriteString(ua.String())
|
|
|
|
}
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
regPacket = []byte("some regular packet")
|
|
|
|
sprayPacket = []byte("0000")
|
|
|
|
)
|
|
|
|
binary.LittleEndian.PutUint32(sprayPacket[:4], device.MessageInitiationType)
|
|
|
|
if !shouldSprayPacket(sprayPacket) {
|
|
|
|
t.Fatal("sprayPacket should be classified as a spray packet for testing")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A step is either a b+want appendDests tests, or an
|
|
|
|
// UpdateDst call, depending on which fields are set.
|
|
|
|
type step struct {
|
|
|
|
// advance is the time to advance the fake clock
|
|
|
|
// before the step.
|
|
|
|
advance time.Duration
|
|
|
|
|
|
|
|
// updateDst, if set, does an UpdateDst call and
|
|
|
|
// b+want are ignored.
|
|
|
|
updateDst *net.UDPAddr
|
|
|
|
|
|
|
|
b []byte
|
|
|
|
want string // comma-separated
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
as *AddrSet
|
|
|
|
steps []step
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "reg_packet_no_curaddr",
|
|
|
|
as: &AddrSet{
|
|
|
|
addrs: udpAddrs("127.3.3.40:1", "123.45.67.89:123", "10.0.0.1:123"),
|
|
|
|
curAddr: -1, // unknown
|
|
|
|
roamAddr: nil,
|
|
|
|
},
|
|
|
|
steps: []step{
|
|
|
|
{b: regPacket, want: "127.3.3.40:1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reg_packet_have_curaddr",
|
|
|
|
as: &AddrSet{
|
|
|
|
addrs: udpAddrs("127.3.3.40:1", "123.45.67.89:123", "10.0.0.1:123"),
|
|
|
|
curAddr: 1, // global IP
|
|
|
|
roamAddr: nil,
|
|
|
|
},
|
|
|
|
steps: []step{
|
|
|
|
{b: regPacket, want: "123.45.67.89:123"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "reg_packet_have_roamaddr",
|
|
|
|
as: &AddrSet{
|
|
|
|
addrs: udpAddrs("127.3.3.40:1", "123.45.67.89:123", "10.0.0.1:123"),
|
|
|
|
curAddr: 2, // should be ignored
|
|
|
|
roamAddr: mustUDPAddr("5.6.7.8:123"),
|
|
|
|
},
|
|
|
|
steps: []step{
|
|
|
|
{b: regPacket, want: "5.6.7.8:123"},
|
|
|
|
{updateDst: mustUDPAddr("10.0.0.1:123")}, // no more roaming
|
|
|
|
{b: regPacket, want: "10.0.0.1:123"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "start_roaming",
|
|
|
|
as: &AddrSet{
|
|
|
|
addrs: udpAddrs("127.3.3.40:1", "123.45.67.89:123", "10.0.0.1:123"),
|
|
|
|
curAddr: 2,
|
|
|
|
},
|
|
|
|
steps: []step{
|
|
|
|
{b: regPacket, want: "10.0.0.1:123"},
|
|
|
|
{updateDst: mustUDPAddr("4.5.6.7:123")},
|
|
|
|
{b: regPacket, want: "4.5.6.7:123"},
|
|
|
|
{updateDst: mustUDPAddr("5.6.7.8:123")},
|
|
|
|
{b: regPacket, want: "5.6.7.8:123"},
|
|
|
|
{updateDst: mustUDPAddr("123.45.67.89:123")}, // end roaming
|
|
|
|
{b: regPacket, want: "123.45.67.89:123"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "spray_packet",
|
|
|
|
as: &AddrSet{
|
|
|
|
addrs: udpAddrs("127.3.3.40:1", "123.45.67.89:123", "10.0.0.1:123"),
|
|
|
|
curAddr: 2, // should be ignored
|
|
|
|
roamAddr: mustUDPAddr("5.6.7.8:123"),
|
|
|
|
},
|
|
|
|
steps: []step{
|
|
|
|
{b: sprayPacket, want: "127.3.3.40:1,123.45.67.89:123,10.0.0.1:123,5.6.7.8:123"},
|
|
|
|
{advance: 300 * time.Millisecond, b: regPacket, want: "127.3.3.40:1,123.45.67.89:123,10.0.0.1:123,5.6.7.8:123"},
|
|
|
|
{advance: 300 * time.Millisecond, b: regPacket, want: "127.3.3.40:1,123.45.67.89:123,10.0.0.1:123,5.6.7.8:123"},
|
|
|
|
{advance: 3, b: regPacket, want: "5.6.7.8:123"},
|
|
|
|
{advance: 2 * time.Millisecond, updateDst: mustUDPAddr("10.0.0.1:123")},
|
|
|
|
{advance: 3, b: regPacket, want: "10.0.0.1:123"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
faket := time.Unix(0, 0)
|
|
|
|
tt.as.Logf = t.Logf
|
|
|
|
tt.as.clock = func() time.Time { return faket }
|
|
|
|
for i, st := range tt.steps {
|
|
|
|
faket = faket.Add(st.advance)
|
|
|
|
|
|
|
|
if st.updateDst != nil {
|
|
|
|
if err := tt.as.UpdateDst(st.updateDst); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
got, _ := tt.as.appendDests(nil, st.b)
|
|
|
|
if gotStr := joinUDPs(got); gotStr != st.want {
|
|
|
|
t.Errorf("step %d: got %v; want %v", i, gotStr, st.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|