// beacon_lldp_2184_test.go — #2184: sección opcional de vecinos LLDP en el // beacon. Un switch alimentado por beacon es el único receptor de esos frames, // así que sin esta sección la topología pierde la única evidencia disponible. package httpapi import ( "fmt" "strings" "testing" ) // La sección se traduce al contrato compartido: puerto local con prefijo lan, // chasis MAC en forma canónica, nombre recortado y capacidades acotadas. func TestBeaconLldpSectionFeedsPayload(t *testing.T) { s, token := newBeaconTestServer(t) addr, err := s.startBeaconListener("127.0.0.1:0") if err != nil { t.Fatalf("listener: %v", err) } defer s.beaconConn.Close() sendUDP(t, addr, fmt.Sprintf(`{"v":1,"seq":11,"slug":"switch16","token":"%s",`+ `"ports":[{"n":1,"l":3}],`+ `"lldp":[`+ `{"p":1,"cmac":"aa:bb:cc:dd:ee:01","cn":"pve-01","mgmt":"192.168.1.100","caps":["Bridge","Router"],"pd":"eth0"},`+ `{"p":3,"cmac":"AA-BB-CC-DD-EE-03","cn":" pve 03 ","caps":["Bridge"],"pd":"enp1s0"}]}`, token)) p := waitFresh(t, s, "switch16") if p.Data.Lldp == nil || !p.Data.Lldp.Available { t.Fatalf("sección LLDP: %+v", p.Data.Lldp) } nbs := p.Data.Lldp.Neighbors if len(nbs) != 2 { t.Fatalf("vecinos: %d (%+v)", len(nbs), nbs) } if nbs[0].Port != "lan1" || nbs[0].ChassisMac != "AA:BB:CC:DD:EE:01" || nbs[0].Chassis != "pve-01" || nbs[0].Mgmt != "192.168.1.100" || nbs[0].PortDesc != "eth0" || len(nbs[0].Caps) != 2 { t.Fatalf("vecino 1: %+v", nbs[0]) } if nbs[1].Port != "lan3" || nbs[1].ChassisMac != "AA:BB:CC:DD:EE:03" || nbs[1].Chassis != "pve 03" { t.Fatalf("vecino 2: %+v", nbs[1]) } } // Sin la sección el payload queda como hoy: el servidor conserva la última // sección buena en vez de inventar cero vecinos. func TestBeaconWithoutLldpSectionKeepsPayloadShape(t *testing.T) { s, token := newBeaconTestServer(t) addr, err := s.startBeaconListener("127.0.0.1:0") if err != nil { t.Fatalf("listener: %v", err) } defer s.beaconConn.Close() sendUDP(t, addr, fmt.Sprintf( `{"v":1,"seq":12,"slug":"switch16","token":"%s","ports":[{"n":1,"l":3}]}`, token)) p := waitFresh(t, s, "switch16") if p.Data.Lldp != nil { t.Fatalf("un beacon sin sección no puede inventar LLDP: %+v", p.Data.Lldp) } } // Una lista vacía sí es un dato: cero vecinos, distinto de "no lo sé". func TestBeaconEmptyLldpSectionIsHonestZero(t *testing.T) { s, token := newBeaconTestServer(t) addr, err := s.startBeaconListener("127.0.0.1:0") if err != nil { t.Fatalf("listener: %v", err) } defer s.beaconConn.Close() sendUDP(t, addr, fmt.Sprintf( `{"v":1,"seq":13,"slug":"switch16","token":"%s","ports":[{"n":1,"l":3}],"lldp":[]}`, token)) p := waitFresh(t, s, "switch16") if p.Data.Lldp == nil || !p.Data.Lldp.Available { t.Fatalf("sección vacía: %+v", p.Data.Lldp) } if p.Data.Lldp.Neighbors == nil || len(p.Data.Lldp.Neighbors) != 0 { t.Fatalf("vecinos de una sección vacía: %+v", p.Data.Lldp.Neighbors) } } // Entradas imposibles o sin identidad se descartan una a una sin tirar el // resto del datagrama, y los textos se acotan. func TestBeaconLldpSkipsBadEntriesKeepsGoodOnes(t *testing.T) { s, token := newBeaconTestServer(t) addr, err := s.startBeaconListener("127.0.0.1:0") if err != nil { t.Fatalf("listener: %v", err) } defer s.beaconConn.Close() long := strings.Repeat("x", 300) sendUDP(t, addr, fmt.Sprintf(`{"v":1,"seq":14,"slug":"switch16","token":"%s","ports":[{"n":1,"l":3}],`+ `"lldp":[`+ `{"p":0,"cn":"puerto cero"},`+ `{"p":99,"cn":"puerto fuera"},`+ `{"p":2},`+ `{"p":2,"cmac":"no-es-mac","cn":""},`+ `{"p":2,"cmac":"AA:BB:CC:DD:EE:02","cn":"switch\u0001central","mgmt":"no-ip"},`+ `{"p":4,"cmac":"AA:BB:CC:DD:EE:04","cn":"%s"}]}`, token, long)) p := waitFresh(t, s, "switch16") nbs := p.Data.Lldp.Neighbors if len(nbs) != 2 { t.Fatalf("vecinos válidos: %d (%+v)", len(nbs), nbs) } if nbs[0].Port != "lan2" || nbs[0].Chassis != "switchcentral" || nbs[0].Mgmt != "" { t.Fatalf("vecino saneado: %+v", nbs[0]) } if got := len([]rune(nbs[1].Chassis)); got != maxBeaconLldpRunes { t.Fatalf("nombre sin acotar: %d runas", got) } } // El tope de vecinos evita que un datagrama grande se convierta en estado // ilimitado en memoria y en la API. func TestBeaconLldpCapsNeighbourCount(t *testing.T) { entries := make([]beaconLldp, 0, maxBeaconLldpNeighbors+10) for i := 0; i < maxBeaconLldpNeighbors+10; i++ { entries = append(entries, beaconLldp{P: i%32 + 1, Cname: "n"}) } ld := beaconLldpData(&entries) if ld == nil || len(ld.Neighbors) != maxBeaconLldpNeighbors { t.Fatalf("tope de vecinos: %d", len(ld.Neighbors)) } }