2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-10-18 17:51:12 +00:00
|
|
|
|
|
|
|
package prober
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-28 20:27:44 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/tls"
|
2022-10-18 17:51:12 +00:00
|
|
|
"encoding/json"
|
2024-02-28 20:27:44 +00:00
|
|
|
"net"
|
2022-10-18 17:51:12 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2024-02-28 20:27:44 +00:00
|
|
|
"time"
|
2022-10-18 17:51:12 +00:00
|
|
|
|
2024-02-28 20:27:44 +00:00
|
|
|
"tailscale.com/derp"
|
|
|
|
"tailscale.com/derp/derphttp"
|
2024-04-27 05:06:20 +00:00
|
|
|
"tailscale.com/net/netmon"
|
2022-10-18 17:51:12 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2024-02-28 20:27:44 +00:00
|
|
|
"tailscale.com/types/key"
|
2022-10-18 17:51:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDerpProber(t *testing.T) {
|
|
|
|
dm := &tailcfg.DERPMap{
|
|
|
|
Regions: map[int]*tailcfg.DERPRegion{
|
|
|
|
0: {
|
|
|
|
RegionID: 0,
|
|
|
|
RegionCode: "zero",
|
|
|
|
Nodes: []*tailcfg.DERPNode{
|
|
|
|
{
|
|
|
|
Name: "n1",
|
|
|
|
RegionID: 0,
|
|
|
|
HostName: "derpn1.tailscale.test",
|
|
|
|
IPv4: "1.1.1.1",
|
|
|
|
IPv6: "::1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "n2",
|
|
|
|
RegionID: 0,
|
|
|
|
HostName: "derpn2.tailscale.test",
|
|
|
|
IPv4: "1.1.1.1",
|
|
|
|
IPv6: "::1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
resp, err := json.Marshal(dm)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
w.Write(resp)
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
clk := newFakeTime()
|
|
|
|
p := newForTest(clk.Now, clk.NewTicker)
|
|
|
|
dp := &derpProber{
|
2024-02-28 20:27:44 +00:00
|
|
|
p: p,
|
|
|
|
derpMapURL: srv.URL,
|
|
|
|
tlsInterval: time.Second,
|
2024-03-27 15:13:34 +00:00
|
|
|
tlsProbeFn: func(_ string) ProbeClass { return FuncProbe(func(context.Context) error { return nil }) },
|
2024-02-28 20:27:44 +00:00
|
|
|
udpInterval: time.Second,
|
2024-03-27 15:13:34 +00:00
|
|
|
udpProbeFn: func(_ string, _ int) ProbeClass { return FuncProbe(func(context.Context) error { return nil }) },
|
2024-02-28 20:27:44 +00:00
|
|
|
meshInterval: time.Second,
|
2024-03-27 15:13:34 +00:00
|
|
|
meshProbeFn: func(_, _ string) ProbeClass { return FuncProbe(func(context.Context) error { return nil }) },
|
2024-02-28 20:27:44 +00:00
|
|
|
nodes: make(map[string]*tailcfg.DERPNode),
|
|
|
|
probes: make(map[string]*Probe),
|
2022-10-18 17:51:12 +00:00
|
|
|
}
|
2024-03-27 15:13:34 +00:00
|
|
|
if err := dp.probeMapFn(context.Background()); err != nil {
|
|
|
|
t.Errorf("unexpected probeMapFn() error: %s", err)
|
2022-10-18 17:51:12 +00:00
|
|
|
}
|
2024-02-28 20:27:44 +00:00
|
|
|
if len(dp.nodes) != 2 || dp.nodes["n1"] == nil || dp.nodes["n2"] == nil {
|
2022-10-18 17:51:12 +00:00
|
|
|
t.Errorf("unexpected nodes: %+v", dp.nodes)
|
|
|
|
}
|
|
|
|
// Probes expected for two nodes:
|
|
|
|
// - 3 regular probes per node (TLS, UDPv4, UDPv6)
|
|
|
|
// - 4 mesh probes (N1->N2, N1->N1, N2->N1, N2->N2)
|
|
|
|
if len(dp.probes) != 10 {
|
|
|
|
t.Errorf("unexpected probes: %+v", dp.probes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add one more node and check that probes got created.
|
|
|
|
dm.Regions[0].Nodes = append(dm.Regions[0].Nodes, &tailcfg.DERPNode{
|
|
|
|
Name: "n3",
|
|
|
|
RegionID: 0,
|
|
|
|
HostName: "derpn3.tailscale.test",
|
|
|
|
IPv4: "1.1.1.1",
|
|
|
|
IPv6: "::1",
|
|
|
|
})
|
2024-03-27 15:13:34 +00:00
|
|
|
if err := dp.probeMapFn(context.Background()); err != nil {
|
|
|
|
t.Errorf("unexpected probeMapFn() error: %s", err)
|
2022-10-18 17:51:12 +00:00
|
|
|
}
|
|
|
|
if len(dp.nodes) != 3 {
|
|
|
|
t.Errorf("unexpected nodes: %+v", dp.nodes)
|
|
|
|
}
|
|
|
|
// 9 regular probes + 9 mesh probes
|
|
|
|
if len(dp.probes) != 18 {
|
|
|
|
t.Errorf("unexpected probes: %+v", dp.probes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove 2 nodes and check that probes have been destroyed.
|
|
|
|
dm.Regions[0].Nodes = dm.Regions[0].Nodes[:1]
|
2024-03-27 15:13:34 +00:00
|
|
|
if err := dp.probeMapFn(context.Background()); err != nil {
|
|
|
|
t.Errorf("unexpected probeMapFn() error: %s", err)
|
2022-10-18 17:51:12 +00:00
|
|
|
}
|
|
|
|
if len(dp.nodes) != 1 {
|
|
|
|
t.Errorf("unexpected nodes: %+v", dp.nodes)
|
|
|
|
}
|
|
|
|
// 3 regular probes + 1 mesh probe
|
|
|
|
if len(dp.probes) != 4 {
|
|
|
|
t.Errorf("unexpected probes: %+v", dp.probes)
|
|
|
|
}
|
|
|
|
}
|
2024-02-28 20:27:44 +00:00
|
|
|
|
|
|
|
func TestRunDerpProbeNodePair(t *testing.T) {
|
|
|
|
// os.Setenv("DERP_DEBUG_LOGS", "true")
|
|
|
|
serverPrivateKey := key.NewNode()
|
|
|
|
s := derp.NewServer(serverPrivateKey, t.Logf)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
httpsrv := &http.Server{
|
|
|
|
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
|
|
|
|
Handler: derphttp.Handler(s),
|
|
|
|
}
|
|
|
|
ln, err := net.Listen("tcp4", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
serverURL := "http://" + ln.Addr().String()
|
|
|
|
t.Logf("server URL: %s", serverURL)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := httpsrv.Serve(ln); err != nil {
|
|
|
|
if err == http.ErrServerClosed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
newClient := func() *derphttp.Client {
|
2024-04-27 05:06:20 +00:00
|
|
|
c, err := derphttp.NewClient(key.NewNode(), serverURL, t.Logf, netmon.NewStatic())
|
2024-02-28 20:27:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("NewClient: %v", err)
|
|
|
|
}
|
|
|
|
m, err := c.Recv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Recv: %v", err)
|
|
|
|
}
|
|
|
|
switch m.(type) {
|
|
|
|
case derp.ServerInfoMessage:
|
|
|
|
default:
|
|
|
|
t.Fatalf("unexpected first message type %T", m)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
c1 := newClient()
|
|
|
|
defer c1.Close()
|
|
|
|
c2 := newClient()
|
|
|
|
defer c2.Close()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = runDerpProbeNodePair(ctx, &tailcfg.DERPNode{Name: "c1"}, &tailcfg.DERPNode{Name: "c2"}, c1, c2, 100_000_000)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_packetsForSize(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
size int
|
|
|
|
wantPackets int
|
|
|
|
wantUnique bool
|
|
|
|
}{
|
|
|
|
{"small_unqiue", 8, 1, true},
|
|
|
|
{"8k_unique", 8192, 1, true},
|
|
|
|
{"full_size_packet", derp.MaxPacketSize, 1, true},
|
|
|
|
{"larger_than_one", derp.MaxPacketSize + 1, 2, false},
|
|
|
|
{"large", 500000, 8, false},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
hashes := make(map[string]int)
|
2024-04-16 20:15:13 +00:00
|
|
|
for range 5 {
|
2024-02-28 20:27:44 +00:00
|
|
|
pkts := packetsForSize(int64(tt.size))
|
|
|
|
if len(pkts) != tt.wantPackets {
|
|
|
|
t.Errorf("packetsForSize(%d) got %d packets, want %d", tt.size, len(pkts), tt.wantPackets)
|
|
|
|
}
|
|
|
|
var total int
|
|
|
|
hash := sha256.New()
|
|
|
|
for _, p := range pkts {
|
|
|
|
hash.Write(p)
|
|
|
|
total += len(p)
|
|
|
|
}
|
|
|
|
hashes[string(hash.Sum(nil))]++
|
|
|
|
if total != tt.size {
|
|
|
|
t.Errorf("packetsForSize(%d) returned %d bytes total", tt.size, total)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unique := len(hashes) > 1
|
|
|
|
if unique != tt.wantUnique {
|
|
|
|
t.Errorf("packetsForSize(%d) is unique=%v (returned %d different answers); want unique=%v", tt.size, unique, len(hashes), unique)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|