lldpRem_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package snmp
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/gosnmp/gosnmp"
  6. )
  7. // Fixture grabada del Linksys LGS310C real (3-Oct-2026, #931): un vecino
  8. // (PC con lldpd) en el puerto local 5. Los OIDs son los literales del
  9. // snmpwalk para que el test pinae el formato exacto del índice.
  10. func lgs310cRemPdus() []gosnmp.SnmpPDU {
  11. const base = OidLldpRemTable
  12. return []gosnmp.SnmpPDU{
  13. {Name: base + ".4.9900.5.1", Type: gosnmp.Integer, Value: 4}, // chassisIdSubtype = macAddress
  14. {Name: base + ".5.9900.5.1", Type: gosnmp.OctetString, Value: []byte{0x78, 0x55, 0x36, 0x02, 0x58, 0xBA}},
  15. {Name: base + ".6.9900.5.1", Type: gosnmp.Integer, Value: 3}, // portIdSubtype = macAddress
  16. {Name: base + ".7.9900.5.1", Type: gosnmp.OctetString, Value: []byte{0x78, 0x55, 0x36, 0x02, 0x58, 0xBA}},
  17. {Name: base + ".8.9900.5.1", Type: gosnmp.OctetString, Value: []byte("enp195s0")}, // portDesc
  18. {Name: base + ".9.9900.5.1", Type: gosnmp.OctetString, Value: []byte("ryzen-ai")}, // sysName
  19. {Name: base + ".10.9900.5.1", Type: gosnmp.OctetString, Value: []byte("CachyOS Linux")}, // sysDesc (ignorado)
  20. {Name: base + ".11.9900.5.1", Type: gosnmp.OctetString, Value: []byte{0x39, 0x00}}, // capsSupported
  21. {Name: base + ".12.9900.5.1", Type: gosnmp.OctetString, Value: []byte{0x10, 0x00}}, // capsEnabled
  22. }
  23. }
  24. // Fixture del lldpRemManAddrTable del mismo switch: IPv4 e IPv6 del vecino
  25. // (5.1), la dirección viaja en el índice.
  26. func lgs310cManAddrPdus() []gosnmp.SnmpPDU {
  27. const base = OidLldpRemManAddr
  28. return []gosnmp.SnmpPDU{
  29. {Name: base + ".3.9900.5.1.1.4.192.168.1.80", Type: gosnmp.Integer, Value: 2},
  30. {Name: base + ".3.9900.5.1.2.16.254.128.0.0.0.0.0.0.220.231.67.22.10.131.233.175", Type: gosnmp.Integer, Value: 2},
  31. {Name: base + ".5.9900.5.1.1.4.192.168.1.80", Type: gosnmp.ObjectIdentifier, Value: ".0.0"},
  32. }
  33. }
  34. func TestLldpRemEntriesLgs310c(t *testing.T) {
  35. portMap := map[int]int{5: 5}
  36. mgmt := lldpMgmtAddrs(lgs310cManAddrPdus())
  37. got := lldpRemEntries(lgs310cRemPdus(), portMap, mgmt)
  38. if len(got) != 1 {
  39. t.Fatalf("esperaba 1 vecino, obtuve %d: %+v", len(got), got)
  40. }
  41. e := got[0]
  42. want := LldpRemEntry{
  43. LocalPortNum: 5,
  44. IfIndex: 5,
  45. ChassisMac: "78:55:36:02:58:BA",
  46. Chassis: "ryzen-ai",
  47. Mgmt: "192.168.1.80",
  48. Caps: []string{"Wlan"},
  49. PortDesc: "enp195s0",
  50. }
  51. if !reflect.DeepEqual(e, want) {
  52. t.Fatalf("vecino inesperado:\n got %+v\nwant %+v", e, want)
  53. }
  54. }
  55. // Sin mapeo dot1dBasePortIfIndex: el IfIndex cae al número de puerto de
  56. // bridge (paridad con el FDB, #661).
  57. func TestLldpRemEntriesIfIndexFallback(t *testing.T) {
  58. got := lldpRemEntries(lgs310cRemPdus(), map[int]int{7: 70}, nil)
  59. if len(got) != 1 {
  60. t.Fatalf("esperaba 1 vecino, obtuve %d", len(got))
  61. }
  62. if got[0].IfIndex != 5 {
  63. t.Fatalf("IfIndex fallback: %d, esperaba 5", got[0].IfIndex)
  64. }
  65. if got[0].Mgmt != "" {
  66. t.Fatalf("Mgmt sin tabla manaddr: %q, esperaba vacío", got[0].Mgmt)
  67. }
  68. }
  69. // Chasis NO-MAC (subtype 7 = local) sin sysName: el id viaja a Chassis y
  70. // ChassisMac queda vacío; con sysName este gana al id.
  71. func TestLldpRemEntriesChassisNoMac(t *testing.T) {
  72. const base = OidLldpRemTable
  73. pdus := []gosnmp.SnmpPDU{
  74. {Name: base + ".4.100.2.1", Type: gosnmp.Integer, Value: 7},
  75. {Name: base + ".5.100.2.1", Type: gosnmp.OctetString, Value: []byte("switch-sotano")},
  76. }
  77. got := lldpRemEntries(pdus, nil, nil)
  78. if len(got) != 1 {
  79. t.Fatalf("esperaba 1 vecino, obtuve %d", len(got))
  80. }
  81. if got[0].Chassis != "switch-sotano" || got[0].ChassisMac != "" {
  82. t.Fatalf("chasis inesperado: %+v", got[0])
  83. }
  84. pdus = append(pdus,
  85. gosnmp.SnmpPDU{Name: base + ".9.100.2.1", Type: gosnmp.OctetString, Value: []byte("SW1.lan")},
  86. )
  87. got = lldpRemEntries(pdus, nil, nil)
  88. if got[0].Chassis != "SW1.lan" {
  89. t.Fatalf("sysName debe ganar al id: %+v", got[0])
  90. }
  91. }
  92. // PDUs basura (OID de columna sin instancia, índices cortos/largos,
  93. // columnas fuera de rango, valores no enteros) no producen entradas ni
  94. // pánico.
  95. func TestLldpRemEntriesSkipsUnusablePdus(t *testing.T) {
  96. const base = OidLldpRemTable
  97. pdus := []gosnmp.SnmpPDU{
  98. {Name: base, Type: gosnmp.Integer, Value: 4},
  99. {Name: base + ".4.9900.5", Type: gosnmp.Integer, Value: 4}, // índice corto
  100. {Name: base + ".4.9900.5.1.9", Type: gosnmp.Integer, Value: 4}, // índice largo
  101. {Name: base + ".3.9900.5.1", Type: gosnmp.Integer, Value: 4}, // columna 3 (índice, not-accessible)
  102. {Name: base + ".13.9900.5.1", Type: gosnmp.Integer, Value: 4}, // columna inexistente
  103. {Name: base + ".4.9900.0.1", Type: gosnmp.Integer, Value: 4}, // puerto 0
  104. {Name: base + ".4.9900.x.1", Type: gosnmp.Integer, Value: 4}, // componente no entero
  105. {Name: ".1.0.8802.1.1.2.1.4.1.2.4.9900.5.1", Type: gosnmp.Integer, Value: 4}, // prefijo distinto
  106. }
  107. if got := lldpRemEntries(pdus, nil, nil); len(got) != 0 {
  108. t.Fatalf("esperaba 0 entradas, obtuve %d: %+v", len(got), got)
  109. }
  110. }
  111. // Dos vecinos en puertos distintos conservan orden y campos independientes.
  112. func TestLldpRemEntriesDosVecinos(t *testing.T) {
  113. const base = OidLldpRemTable
  114. pdus := []gosnmp.SnmpPDU{
  115. {Name: base + ".4.9900.5.1", Type: gosnmp.Integer, Value: 4},
  116. {Name: base + ".5.9900.5.1", Type: gosnmp.OctetString, Value: []byte{0x78, 0x55, 0x36, 0x02, 0x58, 0xBA}},
  117. {Name: base + ".9.9900.5.1", Type: gosnmp.OctetString, Value: []byte("ryzen-ai")},
  118. {Name: base + ".4.9900.8.1", Type: gosnmp.Integer, Value: 4},
  119. {Name: base + ".5.9900.8.1", Type: gosnmp.OctetString, Value: []byte{0xD8, 0xEC, 0x5E, 0x60, 0xC7, 0x82}},
  120. {Name: base + ".9.9900.8.1", Type: gosnmp.OctetString, Value: []byte("LGS310C-2")},
  121. }
  122. got := lldpRemEntries(pdus, map[int]int{5: 5, 8: 8}, nil)
  123. if len(got) != 2 {
  124. t.Fatalf("esperaba 2 vecinos, obtuve %d", len(got))
  125. }
  126. if got[0].LocalPortNum != 5 || got[0].Chassis != "ryzen-ai" {
  127. t.Fatalf("vecino 0 inesperado: %+v", got[0])
  128. }
  129. if got[1].LocalPortNum != 8 || got[1].ChassisMac != "D8:EC:5E:60:C7:82" {
  130. t.Fatalf("vecino 1 inesperado: %+v", got[1])
  131. }
  132. }
  133. func TestLldpMgmtAddrs(t *testing.T) {
  134. got := lldpMgmtAddrs(lgs310cManAddrPdus())
  135. if got["5.1"] != "192.168.1.80" {
  136. t.Fatalf("mgmt 5.1 inesperada: %q", got["5.1"])
  137. }
  138. if len(got) != 1 {
  139. t.Fatalf("solo IPv4 debe indexarse: %+v", got)
  140. }
  141. }
  142. func TestLldpCapsFromBits(t *testing.T) {
  143. cases := []struct {
  144. b []byte
  145. want []string
  146. }{
  147. {[]byte{0x10, 0x00}, []string{"Wlan"}}, // LGS310C real (lldpd PC)
  148. {[]byte{0x28, 0x00}, []string{"Bridge", "Router"}}, // switch/router típico
  149. {[]byte{0x01, 0x00}, []string{"Station"}},
  150. {[]byte{0x39, 0x00}, []string{"Bridge", "Wlan", "Router", "Station"}},
  151. {nil, nil},
  152. {[]byte{0x00, 0x00}, nil},
  153. }
  154. for i, c := range cases {
  155. if got := lldpCapsFromBits(c.b); !reflect.DeepEqual(got, c.want) {
  156. t.Errorf("caso %d: got %v, want %v", i, got, c.want)
  157. }
  158. }
  159. }
  160. // Fixture del caso Omada real (#1279, snmpwalk de sw1 del contribuidor): la
  161. // rem table SOLO expone chassisIdSubtype (7=networkAddress, 4=macAddress)
  162. // sin NINGÚN valor de identidad detrás; la tabla local de puertos sí está
  163. // completa y su descripción lleva el nombre del vecino que el admin anotó
  164. // (o la copia del propio id si no la tocó).
  165. func omadaRemPdus() []gosnmp.SnmpPDU {
  166. const base = OidLldpRemTable
  167. return []gosnmp.SnmpPDU{
  168. {Name: base + ".4.1055607100.20.1", Type: gosnmp.Integer, Value: 7},
  169. {Name: base + ".4.1056751200.21.1", Type: gosnmp.Integer, Value: 7},
  170. {Name: base + ".4.527503400.22.1", Type: gosnmp.Integer, Value: 4},
  171. }
  172. }
  173. func omadaLocPdus() []gosnmp.SnmpPDU {
  174. const base = OidLldpLocPortTable
  175. return []gosnmp.SnmpPDU{
  176. {Name: base + ".2.13", Type: gosnmp.Integer, Value: 5},
  177. {Name: base + ".3.13", Type: gosnmp.OctetString, Value: []byte("two-gigabitEthernet 1/0/13")},
  178. {Name: base + ".4.13", Type: gosnmp.OctetString, Value: []byte("two-gigabitEthernet 1/0/13")}, // copia por defecto
  179. {Name: base + ".2.20", Type: gosnmp.Integer, Value: 5},
  180. {Name: base + ".3.20", Type: gosnmp.OctetString, Value: []byte("two-gigabitEthernet 1/0/20")},
  181. {Name: base + ".4.20", Type: gosnmp.OctetString, Value: []byte("gateway")},
  182. {Name: base + ".2.21", Type: gosnmp.Integer, Value: 5},
  183. {Name: base + ".3.21", Type: gosnmp.OctetString, Value: []byte("two-gigabitEthernet 1/0/21")},
  184. {Name: base + ".4.21", Type: gosnmp.OctetString, Value: []byte("ap1")},
  185. {Name: base + ".2.22", Type: gosnmp.Integer, Value: 5},
  186. {Name: base + ".3.22", Type: gosnmp.OctetString, Value: []byte("two-gigabitEthernet 1/0/22")},
  187. {Name: base + ".4.22", Type: gosnmp.OctetString, Value: []byte("sw2")},
  188. }
  189. }
  190. func TestLldpLocPortsOmada(t *testing.T) {
  191. loc := lldpLocPorts(omadaLocPdus())
  192. if len(loc) != 4 {
  193. t.Fatalf("esperaba 4 puertos locales, obtuve %d: %+v", len(loc), loc)
  194. }
  195. if loc[20].ID != "two-gigabitEthernet 1/0/20" || loc[20].Desc != "gateway" {
  196. t.Fatalf("puerto 20: %+v", loc[20])
  197. }
  198. if loc[13].Desc != loc[13].ID {
  199. t.Fatalf("la copia por defecto debe conservarse tal cual para que applyLocPortIdentity la descarte: %+v", loc[13])
  200. }
  201. }
  202. // #1279: una rem table sin identidad + la local completa = vecinos con
  203. // nombre. Las entradas con identidad propia no se pisan y PortDesc (puerto
  204. // REMOTO) no se toca.
  205. func TestApplyLocPortIdentityOmada(t *testing.T) {
  206. entries := lldpRemEntries(omadaRemPdus(), map[int]int{}, nil)
  207. if len(entries) != 3 {
  208. t.Fatalf("esperaba 3 vecinos (solo subtypes), obtuve %d: %+v", len(entries), entries)
  209. }
  210. applyLocPortIdentity(entries, lldpLocPorts(omadaLocPdus()))
  211. byPort := map[int]LldpRemEntry{}
  212. for _, e := range entries {
  213. byPort[e.LocalPortNum] = e
  214. }
  215. for port, want := range map[int]string{20: "gateway", 21: "ap1", 22: "sw2"} {
  216. if byPort[port].Chassis != want {
  217. t.Fatalf("puerto %d: Chassis %q, want %q", port, byPort[port].Chassis, want)
  218. }
  219. if !byPort[port].ChassisFromLocDesc {
  220. t.Fatalf("puerto %d: ChassisFromLocDesc debe marcarse (vino de locPortDesc)", port)
  221. }
  222. if byPort[port].PortDesc != "" {
  223. t.Fatalf("puerto %d: PortDesc %q debe seguir vacío (el local no es el remoto)", port, byPort[port].PortDesc)
  224. }
  225. }
  226. // Puerto con descripción = copia del id (sin anotar): queda huérfano.
  227. // Identidad propia se respeta.
  228. entries = append(entries, LldpRemEntry{LocalPortNum: 20, Chassis: "sysname-propio"})
  229. applyLocPortIdentity(entries, lldpLocPorts(omadaLocPdus()))
  230. if entries[3].Chassis != "sysname-propio" {
  231. t.Fatalf("la identidad propia no debe pisarse: %q", entries[3].Chassis)
  232. }
  233. if entries[3].ChassisFromLocDesc {
  234. t.Fatal("la identidad propia no debe marcarse como venida de locPortDesc")
  235. }
  236. }