wireguard_disconnect_test.go 5.1 KB

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