entity_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package snmp
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/gosnmp/gosnmp"
  6. )
  7. func entPDU(col, idx string, typ gosnmp.Asn1BER, value any) gosnmp.SnmpPDU {
  8. return gosnmp.SnmpPDU{Name: OidEntPhysicalTable + "." + col + "." + idx, Type: typ, Value: value}
  9. }
  10. func entClass(idx string, class int) gosnmp.SnmpPDU {
  11. return entPDU("5", idx, gosnmp.Integer, big.NewInt(int64(class)))
  12. }
  13. func entString(col, idx, value string) gosnmp.SnmpPDU {
  14. return entPDU(col, idx, gosnmp.OctetString, value)
  15. }
  16. // #1767: XS708T leaves entPhysicalFirmwareRev empty and publishes the
  17. // firmware in entPhysicalSoftwareRev on the chassis row.
  18. func TestParseEntitySoftwareRevOnChassis(t *testing.T) {
  19. info := ParseEntityTable([]gosnmp.SnmpPDU{
  20. entClass("1", 3),
  21. entString("9", "1", ""),
  22. entString("10", "1", "7.0.0.24"),
  23. entString("11", "1", "4KN3837U8009A"),
  24. entString("13", "1", "XS708T"),
  25. })
  26. if info.Firmware != "7.0.0.24" {
  27. t.Errorf("firmware = %q, want 7.0.0.24", info.Firmware)
  28. }
  29. if info.ModelShort != "XS708T" {
  30. t.Errorf("modelShort = %q, want XS708T", info.ModelShort)
  31. }
  32. }
  33. // #1767: S3300-28X-PoE+ publishes a stack(11) row first and the chassis
  34. // identity on row 2. Selection is by class, not by index.
  35. func TestParseEntityPicksChassisAfterStackRow(t *testing.T) {
  36. info := ParseEntityTable([]gosnmp.SnmpPDU{
  37. entClass("1", 11),
  38. entString("13", "1", "Stack row"),
  39. entClass("2", 3),
  40. entString("10", "2", "6.6.4.28"),
  41. entString("11", "2", "3TM1447L80041"),
  42. entString("13", "2", "S3300-28X-PoE+"),
  43. })
  44. if info.Firmware != "6.6.4.28" || info.ModelShort != "S3300-28X-PoE+" {
  45. t.Errorf("got %+v, want chassis row of S3300", info)
  46. }
  47. }
  48. // #1767: when softwareRev is empty, firmwareRev is the fallback.
  49. func TestParseEntityFirmwareRevFallback(t *testing.T) {
  50. info := ParseEntityTable([]gosnmp.SnmpPDU{
  51. entClass("1", 3),
  52. entString("9", "1", "3.6.11.55242"),
  53. entString("10", "1", ""),
  54. entString("13", "1", "FN-SWITCH-8 PRO"),
  55. })
  56. if info.Firmware != "3.6.11.55242" {
  57. t.Errorf("firmware = %q, want firmwareRev fallback", info.Firmware)
  58. }
  59. }
  60. // #1767: a missing table must not invent identity.
  61. func TestParseEntityEmpty(t *testing.T) {
  62. if info := ParseEntityTable(nil); info.Firmware != "" || info.ModelShort != "" {
  63. t.Errorf("empty table gave %+v", info)
  64. }
  65. }
  66. // An empty chassis row does not hide a later row that does publish identity.
  67. func TestParseEntityEmptyChassisFallsThrough(t *testing.T) {
  68. info := ParseEntityTable([]gosnmp.SnmpPDU{
  69. entClass("1", 3),
  70. entClass("2", 11),
  71. entString("10", "2", "1.2.3"),
  72. entString("13", "2", "Fallback"),
  73. })
  74. if info.Firmware != "1.2.3" || info.ModelShort != "Fallback" {
  75. t.Errorf("got %+v, want first populated row", info)
  76. }
  77. }