MCPcopy Index your code
hub / github.com/tailscale/tailscale / TestInjectInboundLeak

Function TestInjectInboundLeak

wgengine/netstack/netstack_test.go:43–98  ·  view source on GitHub ↗

TestInjectInboundLeak tests that injectInbound doesn't leak memory. See https://github.com/tailscale/tailscale/issues/3762

(t *testing.T)

Source from the content-addressed store, hash-verified

41// TestInjectInboundLeak tests that injectInbound doesn't leak memory.
42// See https://github.com/tailscale/tailscale/issues/3762
43func TestInjectInboundLeak(t *testing.T) {
44 tunDev := tstun.NewFake()
45 dialer := new(tsdial.Dialer)
46 logf := func(format string, args ...any) {
47 if !t.Failed() {
48 t.Logf(format, args...)
49 }
50 }
51 sys := tsd.NewSystem()
52 eng, err := wgengine.NewUserspaceEngine(logf, wgengine.Config{
53 Tun: tunDev,
54 Dialer: dialer,
55 SetSubsystem: sys.Set,
56 HealthTracker: sys.HealthTracker.Get(),
57 Metrics: sys.UserMetricsRegistry(),
58 EventBus: sys.Bus.Get(),
59 })
60 if err != nil {
61 t.Fatal(err)
62 }
63 defer eng.Close()
64 sys.Set(eng)
65 sys.Set(new(mem.Store))
66
67 tunWrap := sys.Tun.Get()
68 lb, err := ipnlocal.NewLocalBackend(logf, logid.PublicID{}, sys, 0)
69 if err != nil {
70 t.Fatal(err)
71 }
72 t.Cleanup(lb.Shutdown)
73
74 ns, err := Create(logf, tunWrap, eng, sys.MagicSock.Get(), dialer, sys.DNSManager.Get(), sys.ProxyMapper())
75 if err != nil {
76 t.Fatal(err)
77 }
78 defer ns.Close()
79 ns.ProcessLocalIPs = true
80 if err := ns.Start(lb); err != nil {
81 t.Fatalf("Start: %v", err)
82 }
83 ns.atomicIsLocalIPFunc.Store(func(netip.Addr) bool { return true })
84
85 pkt := &packet.Parsed{}
86 const N = 10_000
87 ms0 := getMemStats()
88 for range N {
89 outcome, _ := ns.injectInbound(pkt, tunWrap, nil)
90 if outcome != filter.DropSilently {
91 t.Fatalf("got outcome %v; want DropSilently", outcome)
92 }
93 }
94 ms1 := getMemStats()
95 if grew := int64(ms1.HeapObjects) - int64(ms0.HeapObjects); grew >= N {
96 t.Fatalf("grew by %v (which is too much and >= the %v packets we sent)", grew, N)
97 }
98}
99
100func getMemStats() (ms runtime.MemStats) {

Callers

nothing calls this directly

Calls 15

UserMetricsRegistryMethod · 0.95
SetMethod · 0.95
ProxyMapperMethod · 0.95
NewFakeFunction · 0.92
NewSystemFunction · 0.92
NewUserspaceEngineFunction · 0.92
NewLocalBackendFunction · 0.92
getMemStatsFunction · 0.85
CreateFunction · 0.70
FailedMethod · 0.65
LogfMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…