|
| 1 | +// wireguard_disconnect_test.go - WireGuard disconnect alerts (#1348). |
| 2 | +package adapters |
| 3 | + |
| 4 | +import ( |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/gnacho/netpulse/server-go/internal/alerts" |
| 10 | +) |
| 11 | + |
| 12 | +// wgAlertLive builds a Live whose engine accepts VPN alerts. |
| 13 | +func wgAlertLive(t *testing.T) *Live { |
| 14 | + t.Helper() |
| 15 | + l := NewLive(nil, nil, nil, nil) |
| 16 | + if err := l.engine.SetConfig(map[string]string{alerts.CatVPN: alerts.LevelAll}); err != nil { |
| 17 | + t.Fatalf("SetConfig vpn: %v", err) |
| 18 | + } |
| 19 | + return l |
| 20 | +} |
| 21 | + |
| 22 | +func wgDisconnects(l *Live) []alerts.AlertEvent { |
| 23 | + out := []alerts.AlertEvent{} |
| 24 | + for _, ev := range l.engine.List() { |
| 25 | + if ev.Type == alerts.TypeWireguardDisconnect { |
| 26 | + out = append(out, ev) |
| 27 | + } |
| 28 | + } |
| 29 | + return out |
| 30 | +} |
| 31 | + |
| 32 | +func TestWireGuardDisconnectOnActiveToInactive(t *testing.T) { |
| 33 | + l := wgAlertLive(t) |
| 34 | + l.wgActive = map[string]bool{"peerA": true} |
| 35 | + l.wgActiveSince = map[string]time.Time{"peerA": time.Now().Add(-90 * time.Minute)} |
| 36 | + |
| 37 | + l.applyWireGuardTransitions(&WireGuardStats{Peers: []WGPeer{ |
| 38 | + {ID: "peerA", Name: "Alice", Active: false}, |
| 39 | + }}, "gw") |
| 40 | + |
| 41 | + got := wgDisconnects(l) |
| 42 | + if len(got) != 1 { |
| 43 | + t.Fatalf("disconnects: %d, want 1", len(got)) |
| 44 | + } |
| 45 | + ev := got[0] |
| 46 | + if ev.Category != alerts.CatVPN { |
| 47 | + t.Errorf("category: %q, want %q", ev.Category, alerts.CatVPN) |
| 48 | + } |
| 49 | + if ev.Urgent { |
| 50 | + t.Errorf("urgent: true, want false") |
| 51 | + } |
| 52 | + if ev.Severity != "info" { |
| 53 | + t.Errorf("severity: %q, want info", ev.Severity) |
| 54 | + } |
| 55 | + if ev.Title != "WireGuard disconnect" { |
| 56 | + t.Errorf("title: %q, want %q", ev.Title, "WireGuard disconnect") |
| 57 | + } |
| 58 | + if ev.Vars["peer"] != "Alice" { |
| 59 | + t.Errorf("vars.peer: %q, want Alice", ev.Vars["peer"]) |
| 60 | + } |
| 61 | + if ev.RouterID != "gw" { |
| 62 | + t.Errorf("routerId: %q, want gw", ev.RouterID) |
| 63 | + } |
| 64 | + if !strings.HasPrefix(ev.ID, "alert-wg-down-") { |
| 65 | + t.Errorf("id: %q, want prefix alert-wg-down-", ev.ID) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestWireGuardNoDisconnectWhileActive(t *testing.T) { |
| 70 | + l := wgAlertLive(t) |
| 71 | + l.wgActive = map[string]bool{"peerA": true} |
| 72 | + l.wgActiveSince = map[string]time.Time{"peerA": time.Now().Add(-90 * time.Minute)} |
| 73 | + |
| 74 | + l.applyWireGuardTransitions(&WireGuardStats{Peers: []WGPeer{ |
| 75 | + {ID: "peerA", Name: "Alice", Active: true}, |
| 76 | + }}, "gw") |
| 77 | + |
| 78 | + if got := wgDisconnects(l); len(got) != 0 { |
| 79 | + t.Fatalf("disconnects: %d, want 0", len(got)) |
| 80 | + } |
| 81 | + if !l.wgActive["peerA"] { |
| 82 | + t.Errorf("peerA no longer active") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestWireGuardNoDisconnectForNeverActive(t *testing.T) { |
| 87 | + l := wgAlertLive(t) // wgActive empty (fresh boot) |
| 88 | + |
| 89 | + l.applyWireGuardTransitions(&WireGuardStats{Peers: []WGPeer{ |
| 90 | + {ID: "peerA", Name: "Alice", Active: false}, |
| 91 | + }}, "gw") |
| 92 | + |
| 93 | + if got := wgDisconnects(l); len(got) != 0 { |
| 94 | + t.Fatalf("disconnects: %d, want 0", len(got)) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestWireGuardDisconnectDuration(t *testing.T) { |
| 99 | + l := wgAlertLive(t) |
| 100 | + l.wgActive = map[string]bool{"peerA": true} |
| 101 | + l.wgActiveSince = map[string]time.Time{"peerA": time.Now().Add(-90 * time.Minute)} |
| 102 | + |
| 103 | + l.applyWireGuardTransitions(&WireGuardStats{Peers: []WGPeer{ |
| 104 | + {ID: "peerA", Name: "Alice", Active: false}, |
| 105 | + }}, "gw") |
| 106 | + |
| 107 | + got := wgDisconnects(l) |
| 108 | + if len(got) != 1 { |
| 109 | + t.Fatalf("disconnects: %d, want 1", len(got)) |
| 110 | + } |
| 111 | + if !strings.Contains(got[0].Description, "after 1h 30m") { |
| 112 | + t.Errorf("description: %q, want to contain %q", got[0].Description, "after 1h 30m") |
| 113 | + } |
| 114 | + if got[0].Vars["duration"] != "1h 30m" { |
| 115 | + t.Errorf("vars.duration: %q, want 1h 30m", got[0].Vars["duration"]) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestWireGuardActiveSinceConsistency(t *testing.T) { |
| 120 | + l := wgAlertLive(t) |
| 121 | + base := time.Now().Add(-90 * time.Minute) |
| 122 | + l.wgActive = map[string]bool{"peerA": true} |
| 123 | + l.wgActiveSince = map[string]time.Time{"peerA": base} |
| 124 | + |
| 125 | + // Disconnect: peer removed from both maps. |
| 126 | + l.applyWireGuardTransitions(&WireGuardStats{Peers: []WGPeer{ |
| 127 | + {ID: "peerA", Name: "Alice", Active: false}, |
| 128 | + }}, "gw") |
| 129 | + |
| 130 | + if _, ok := l.wgActive["peerA"]; ok { |
| 131 | + t.Errorf("peerA still in wgActive after disconnect") |
| 132 | + } |
| 133 | + if _, ok := l.wgActiveSince["peerA"]; ok { |
| 134 | + t.Errorf("peerA still in wgActiveSince after disconnect") |
| 135 | + } |
| 136 | + |
| 137 | + // Reconnect: peer tracked again with a fresh since timestamp. |
| 138 | + l.applyWireGuardTransitions(&WireGuardStats{Peers: []WGPeer{ |
| 139 | + {ID: "peerA", Name: "Alice", Active: true}, |
| 140 | + }}, "gw") |
| 141 | + |
| 142 | + if !l.wgActive["peerA"] { |
| 143 | + t.Errorf("peerA not active after reconnect") |
| 144 | + } |
| 145 | + since, ok := l.wgActiveSince["peerA"] |
| 146 | + if !ok { |
| 147 | + t.Fatalf("peerA missing since after reconnect") |
| 148 | + } |
| 149 | + if !since.After(base) { |
| 150 | + t.Errorf("since: %v, want after %v", since, base) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +// Regression: the connect path still emits a handshake for a newly active peer |
| 155 | +// and tracks its since timestamp. |
| 156 | +func TestWireGuardHandshakeStillEmitted(t *testing.T) { |
| 157 | + l := wgAlertLive(t) |
| 158 | + l.wgActive = map[string]bool{"peerB": true} |
| 159 | + l.wgActiveSince = map[string]time.Time{"peerB": time.Now().Add(-5 * time.Minute)} |
| 160 | + |
| 161 | + l.applyWireGuardTransitions(&WireGuardStats{Peers: []WGPeer{ |
| 162 | + {ID: "peerA", Name: "Alice", Active: true}, |
| 163 | + {ID: "peerB", Name: "Bob", Active: true}, |
| 164 | + }}, "gw") |
| 165 | + |
| 166 | + handshakes := 0 |
| 167 | + for _, ev := range l.engine.List() { |
| 168 | + if ev.Type == alerts.TypeWireguardHandshake && ev.Vars["peer"] == "Alice" { |
| 169 | + handshakes++ |
| 170 | + } |
| 171 | + } |
| 172 | + if handshakes != 1 { |
| 173 | + t.Fatalf("handshakes for Alice: %d, want 1", handshakes) |
| 174 | + } |
| 175 | + if _, ok := l.wgActiveSince["peerA"]; !ok { |
| 176 | + t.Errorf("peerA missing since") |
| 177 | + } |
| 178 | +} |
0 commit comments