fdbTable_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package snmp
  2. import (
  3. "testing"
  4. "github.com/gosnmp/gosnmp"
  5. )
  6. func fdbPdu(name string, port uint) gosnmp.SnmpPDU {
  7. return gosnmp.SnmpPDU{Name: name, Type: gosnmp.Integer, Value: port}
  8. }
  9. // #948: el walk dot1d puede devolver PDUs que no producen ninguna entrada
  10. // utilizable (p.ej. el propio OID de la columna sin instancia, o índices con
  11. // un número de octetos distinto de 6). fdbEntries debe descartarlos sin
  12. // inventar entradas.
  13. func TestFdbEntriesDot1dSkipsUnusablePdus(t *testing.T) {
  14. portMap := map[int]int{3: 103}
  15. pdus := []gosnmp.SnmpPDU{
  16. // OID de la columna sin índice (caso leaf del fallback GetRequest de gosnmp).
  17. fdbPdu(OidDot1dTpFdbPort, 3),
  18. // Índice de 5 octetos: no es una MAC.
  19. fdbPdu(OidDot1dTpFdbPort+".1.2.3.4.5", 3),
  20. // Octeto fuera de rango.
  21. fdbPdu(OidDot1dTpFdbPort+".1.2.3.4.5.999", 3),
  22. // Prefijo distinto (otra columna de la tabla).
  23. fdbPdu(".1.3.6.1.2.1.17.4.3.1.3.1.2.3.4.5.6", 3),
  24. }
  25. if got := fdbEntries(portMap, pdus, false); len(got) != 0 {
  26. t.Fatalf("esperaba 0 entradas, obtuve %d: %+v", len(got), got)
  27. }
  28. }
  29. func TestFdbEntriesDot1d(t *testing.T) {
  30. portMap := map[int]int{3: 103}
  31. pdus := []gosnmp.SnmpPDU{
  32. fdbPdu(OidDot1dTpFdbPort+".140.22.24.187.30.12", 3),
  33. // bridgePort sin mapeo a ifIndex: cae al propio bridgePort (#661).
  34. fdbPdu(OidDot1dTpFdbPort+".0.12.34.56.78.90", 7),
  35. // #950: índice real de TP-Link Omada (SG3428X-M2, issue #928):
  36. // <vlan=1>.<mac> en la tabla dot1d.
  37. fdbPdu(OidDot1dTpFdbPort+".1.0.4.75.233.178.29", 49167),
  38. }
  39. got := fdbEntries(portMap, pdus, false)
  40. if len(got) != 3 {
  41. t.Fatalf("esperaba 3 entradas, obtuve %d: %+v", len(got), got)
  42. }
  43. if got[0].MAC != "8C:16:18:BB:1E:0C" || got[0].BridgePortIndex != 3 || got[0].IfIndex != 103 {
  44. t.Errorf("entrada 0 inesperada: %+v", got[0])
  45. }
  46. if got[1].MAC != "00:0C:22:38:4E:5A" || got[1].BridgePortIndex != 7 || got[1].IfIndex != 7 {
  47. t.Errorf("entrada 1 inesperada: %+v", got[1])
  48. }
  49. if got[2].MAC != "00:04:4B:E9:B2:1D" || got[2].BridgePortIndex != 49167 || got[2].IfIndex != 49167 {
  50. t.Errorf("entrada 2 inesperada: %+v", got[2])
  51. }
  52. }
  53. func TestFdbEntriesDot1q(t *testing.T) {
  54. portMap := map[int]int{5: 205}
  55. pdus := []gosnmp.SnmpPDU{
  56. // Índice compuesto <vlan>.M.M.M.M.M.M: la MAC son los últimos 6 octetos.
  57. fdbPdu(OidDot1qTpFdbPort+".100.140.22.24.187.30.12", 5),
  58. // Prefijo distinto: descartado.
  59. fdbPdu(".1.3.6.1.2.1.17.7.1.2.2.1.3.100.140.22.24.187.30.12", 5),
  60. }
  61. got := fdbEntries(portMap, pdus, true)
  62. if len(got) != 1 {
  63. t.Fatalf("esperaba 1 entrada, obtuve %d: %+v", len(got), got)
  64. }
  65. if got[0].MAC != "8C:16:18:BB:1E:0C" || got[0].BridgePortIndex != 5 || got[0].IfIndex != 205 {
  66. t.Errorf("entrada inesperada: %+v", got[0])
  67. }
  68. }