topology_phantom_test.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package adapters
  2. // topology_phantom_test.go — #1372: guarda contra vecinos LLDP fantasma
  3. // (switch intermedio que inunda la multicast reservada 01:80:c2:00:00:0e,
  4. // no 802.1AB-compliant). Varios candidatos de flota llegan por el MISMO
  5. // puerto raíz; la corroboración FDB por bridge-MAC decide.
  6. import "testing"
  7. const (
  8. macGw = "94:83:C4:00:00:01"
  9. macRT2 = "22:22:22:22:22:02"
  10. macRT3 = "33:33:33:33:33:03"
  11. macSwCor = "44:44:44:44:44:04"
  12. macSwFar = "55:55:55:55:55:05"
  13. )
  14. // phantomPolled: rt4 oye en lan1 a los vecinos dados (en ese orden, como
  15. // los devuelve lldpcli) y su FDB aprende al gateway por lan1 (root port).
  16. func phantomPolled(neighbors []LldpNeighbor, fdb map[string]string) map[string]*routerPolled {
  17. return map[string]*routerPolled{
  18. "gw": {cfg: RouterConfig{ID: "gw", IsGateway: true}, brMac: macGw},
  19. "rt2": {cfg: RouterConfig{ID: "rt2", Type: "openwrt"}, brMac: macRT2},
  20. "rt3": {cfg: RouterConfig{ID: "rt3", Type: "openwrt"}, brMac: macRT3},
  21. "swCore": {cfg: RouterConfig{ID: "swCore", Type: "managed-switch"}, brMac: macSwCor},
  22. "swFar": {cfg: RouterConfig{ID: "swFar", Type: "managed-switch"}, brMac: macSwFar},
  23. "rt4": {cfg: RouterConfig{ID: "rt4", Type: "openwrt"}, brMac: "44:00:00:00:00:44",
  24. fdb: fdb,
  25. lldp: neighbors,
  26. },
  27. }
  28. }
  29. // Phantom puro (sin tráfico directo: su MAC no está en la FDB) llega PRIMERO
  30. // en la lista pero no debe ganar al candidato corroborado.
  31. func TestPhantomFilteredByFdbCorroboration(t *testing.T) {
  32. neighbors := []LldpNeighbor{
  33. {Port: "lan1", Chassis: "rt3", ChassisMac: macRT3, PortDesc: "lan1"}, // phantom primero
  34. {Port: "lan1", Chassis: "rt2", ChassisMac: macRT2, PortDesc: "lan2"}, // peer real
  35. }
  36. fdb := map[string]string{
  37. macGw: "lan1", // root port
  38. macRT2: "lan1", // rt2 corroborado (tráfico real vía lan1)
  39. // macRT3 ausente: phantom de 2º salto sin tráfico directo
  40. }
  41. out := fleetLldpEvidence(phantomPolled(neighbors, fdb), "gw")
  42. if out["rt4"].parent != "rt2" {
  43. t.Fatalf("phantom sin corroboración no debe ganar: parent=%q", out["rt4"].parent)
  44. }
  45. if out["rt4"].port != "lan2" {
  46. t.Fatalf("puerto del padre: %q", out["rt4"].port)
  47. }
  48. }
  49. // Dos switches gestionados oídos en el mismo puerto (cadena vista a través
  50. // de flooding): gana el corroborado, no el primero de la lista.
  51. func TestPhantomTwoSwitchesCorroboratedWins(t *testing.T) {
  52. neighbors := []LldpNeighbor{
  53. {Port: "lan1", Chassis: "swFar", ChassisMac: macSwFar, PortDesc: "ge9"}, // phantom primero
  54. {Port: "lan1", Chassis: "swCore", ChassisMac: macSwCor, PortDesc: "ge5"}, // peer real
  55. }
  56. fdb := map[string]string{
  57. macGw: "lan1", // root port: el guard corre
  58. macSwCor: "lan1", // swCore corroborado
  59. }
  60. out := fleetLldpEvidence(phantomPolled(neighbors, fdb), "gw")
  61. if out["rt4"].parent != "swCore" {
  62. t.Fatalf("dos switches: gana el corroborado; parent=%q", out["rt4"].parent)
  63. }
  64. }
  65. // LAN plana: switch y AP corroborados a la vez en el root port → el switch
  66. // gestionado manda entre los corroborados (hacia el core), sea cual sea el
  67. // orden del scan.
  68. func TestPhantomCorroboratedTiePrefersSwitch(t *testing.T) {
  69. neighbors := []LldpNeighbor{
  70. {Port: "lan1", Chassis: "rt3", ChassisMac: macRT3, PortDesc: "lan1"}, // AP primero
  71. {Port: "lan1", Chassis: "swCore", ChassisMac: macSwCor, PortDesc: "ge5"}, // switch
  72. }
  73. fdb := map[string]string{
  74. macGw: "lan1",
  75. macRT3: "lan1", // ambos corroborados
  76. macSwCor: "lan1",
  77. }
  78. out := fleetLldpEvidence(phantomPolled(neighbors, fdb), "gw")
  79. if out["rt4"].parent != "swCore" {
  80. t.Fatalf("empate corroborado: switch preferido; parent=%q", out["rt4"].parent)
  81. }
  82. }