neighborssid_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package adapters
  2. // neighborssid_test.go — #1359: new-neighbor-ssid con dwell y one-shot.
  3. import (
  4. "testing"
  5. "time"
  6. )
  7. func newSsidAlerts(l *Live) []AlertEvent {
  8. var out []AlertEvent
  9. for _, a := range l.engine.List() {
  10. if a.Type == "new-neighbor-ssid" {
  11. out = append(out, a)
  12. }
  13. }
  14. return out
  15. }
  16. func TestNewSsidSeedingDoesNotAlert(t *testing.T) {
  17. l, _ := newImpostorLive(t) // flota: "temiscira" propio
  18. now := time.Now()
  19. // Vecino de siempre (lleva 10h): en la primera evaluación se siembra,
  20. // no alerta (anti-avalancha de arranque).
  21. insertScan(t, l, "ap1", "11:22:33:44:55:01", "vecino-historico", now.Add(-10*time.Hour))
  22. insertScan(t, l, "ap1", "11:22:33:44:55:01", "vecino-historico", now)
  23. l.EvalNewNeighborSsids()
  24. if got := newSsidAlerts(l); len(got) != 0 {
  25. t.Fatalf("la siembra no debe alertar: %+v", got)
  26. }
  27. var seeded int
  28. _ = l.db.QueryRow(`SELECT COUNT(1) FROM kv WHERE key='ssid_known_seeded'`).Scan(&seeded)
  29. if seeded != 1 {
  30. t.Fatal("kv ssid_known_seeded no escrito")
  31. }
  32. _ = l.db.QueryRow(`SELECT COUNT(1) FROM kv WHERE key='ssid_known:vecino-historico'`).Scan(&seeded)
  33. if seeded != 1 {
  34. t.Fatal("vecino histórico no quedó sembrado como conocido")
  35. }
  36. }
  37. func TestNewSsidTransientStaysQuiet(t *testing.T) {
  38. l, _ := newImpostorLive(t)
  39. now := time.Now()
  40. // Siembra con la flota sola.
  41. l.EvalNewNeighborSsids()
  42. // Hotspot de paso: 2 sightings separados 1h (< dwell 4h), ya no activo.
  43. insertScan(t, l, "ap1", "11:22:33:44:55:02", "hotspot-movil", now.Add(-90*time.Minute))
  44. insertScan(t, l, "ap1", "11:22:33:44:55:02", "hotspot-movil", now.Add(-30*time.Minute))
  45. l.EvalNewNeighborSsids()
  46. if got := newSsidAlerts(l); len(got) != 0 {
  47. t.Fatalf("transitorio no debe alertar: %+v", got)
  48. }
  49. }
  50. func TestNewSsidAlertsAfterDwellOnce(t *testing.T) {
  51. l, _ := newImpostorLive(t)
  52. now := time.Now()
  53. l.EvalNewNeighborSsids() // siembra
  54. // SSID nuevo que persiste 5h y sigue activo.
  55. insertScan(t, l, "ap1", "11:22:33:44:55:03", "iot-pairing", now.Add(-5*time.Hour))
  56. insertScan(t, l, "ap1", "11:22:33:44:55:03", "iot-pairing", now.Add(-3*time.Hour))
  57. l.EvalNewNeighborSsids()
  58. if got := newSsidAlerts(l); len(got) != 0 {
  59. t.Fatalf("a los 3h de dwell no debe alertar aún... espera, esto ya son 5h")
  60. }
  61. insertScan(t, l, "ap1", "11:22:33:44:55:03", "iot-pairing", now)
  62. l.EvalNewNeighborSsids()
  63. got := newSsidAlerts(l)
  64. if len(got) != 1 {
  65. t.Fatalf("dwell cumplido y activo debe alertar: %d", len(got))
  66. }
  67. a := got[0]
  68. if a.Urgent || a.Severity != "info" || a.Category != "clients" {
  69. t.Fatalf("info/no urgente/clients: %+v", a)
  70. }
  71. if a.Vars["ssid"] != "iot-pairing" || a.Vars["router"] != "AP Uno" || a.Vars["signal"] != "-55" {
  72. t.Fatalf("vars: %+v", a.Vars)
  73. }
  74. // One-shot: re-evaluar con más sightings no repite.
  75. insertScan(t, l, "ap1", "11:22:33:44:55:03", "iot-pairing", now.Add(1*time.Minute))
  76. l.EvalNewNeighborSsids()
  77. if got := newSsidAlerts(l); len(got) != 1 {
  78. t.Fatalf("one-shot: no debe repetir; alertas: %d", len(got))
  79. }
  80. // Persistido en kv (sobrevive a la poda de 48h y a reinicios).
  81. var n int
  82. _ = l.db.QueryRow(`SELECT COUNT(1) FROM kv WHERE key='ssid_alerted:iot-pairing'`).Scan(&n)
  83. if n != 1 {
  84. t.Fatal("ssid_alerted no persistido en kv")
  85. }
  86. }
  87. func TestNewSsidFleetAndEmptyIgnored(t *testing.T) {
  88. l, _ := newImpostorLive(t)
  89. now := time.Now()
  90. l.EvalNewNeighborSsids() // siembra
  91. // SSID propio de la flota y SSID vacío (red oculta): nunca alertan.
  92. insertScan(t, l, "ap1", "AA:BB:CC:00:00:09", "temiscira", now.Add(-6*time.Hour))
  93. insertScan(t, l, "ap1", "AA:BB:CC:00:00:09", "temiscira", now)
  94. insertScan(t, l, "ap1", "11:22:33:44:55:04", "", now.Add(-6*time.Hour))
  95. insertScan(t, l, "ap1", "11:22:33:44:55:04", "", now)
  96. l.EvalNewNeighborSsids()
  97. if got := newSsidAlerts(l); len(got) != 0 {
  98. t.Fatalf("flota/vacío no alertan: %+v", got)
  99. }
  100. }