snmp_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package snmp
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/gosnmp/gosnmp"
  6. )
  7. func TestTrailingIndex(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. prefix string
  11. want int
  12. }{
  13. {".1.3.6.1.2.1.2.2.1.1.5", OidIfIndex, 5},
  14. {".1.3.6.1.2.1.2.2.1.1.42", OidIfIndex, 42},
  15. {".1.3.6.1.2.1.2.2.1.1", OidIfIndex, 0},
  16. {".1.3.6.1.2.1.31.1.1.1.1.7", OidIfName, 7},
  17. {".9.9.9", OidIfIndex, 0},
  18. }
  19. for _, tt := range tests {
  20. got := trailingIndex(tt.name, tt.prefix)
  21. if got != tt.want {
  22. t.Errorf("trailingIndex(%q, %q) = %d; want %d", tt.name, tt.prefix, got, tt.want)
  23. }
  24. }
  25. }
  26. func TestExtractMacFromOid(t *testing.T) {
  27. prefix := OidDot1dTpFdbPort
  28. tests := []struct {
  29. name string
  30. want string
  31. }{
  32. {prefix + ".0.17.34.51.68.85", "00:11:22:33:44:55"},
  33. {prefix + ".255.255.255.255.255.255", "FF:FF:FF:FF:FF:FF"},
  34. // #950: índice con prefijo VLAN (TP-Link Omada indexa la dot1d por
  35. // <vlan>.<mac>); la MAC son los últimos 6 octetos.
  36. {prefix + ".1.0.4.75.233.178.29", "00:04:4B:E9:B2:1D"},
  37. {prefix + ".1.2.3.4.5.6.7", "02:03:04:05:06:07"},
  38. {prefix + ".1.2.3.4.5", ""},
  39. {prefix + ".0.17.34.51.68.256", ""},
  40. {prefix + ".5.0.17.34.51.68.256", ""},
  41. {"other.0.17.34.51.68.85", ""},
  42. }
  43. for _, tt := range tests {
  44. got := extractMacFromOid(tt.name, prefix)
  45. if got != tt.want {
  46. t.Errorf("extractMacFromOid(%q) = %q; want %q", tt.name, got, tt.want)
  47. }
  48. }
  49. }
  50. // #661: el índice compuesto de la tabla dot1q es <vlan>.M.M.M.M.M.M; la MAC
  51. // son los últimos 6 octetos (misma regla unificada de extractMacFromOid).
  52. func TestExtractMacFromOidDot1q(t *testing.T) {
  53. prefix := OidDot1qTpFdbPort
  54. tests := []struct {
  55. name string
  56. want string
  57. }{
  58. {prefix + ".5.0.17.34.51.68.85", "00:11:22:33:44:55"},
  59. {prefix + ".0.17.34.51.68.85", "00:11:22:33:44:55"},
  60. {prefix + ".100.1.2.3.4.5.6", "01:02:03:04:05:06"},
  61. {prefix + ".5.0.17.34.51.68.256", ""},
  62. {prefix + ".1.2.3.4.5", ""},
  63. {"other.5.0.17.34.51.68.85", ""},
  64. }
  65. for _, tt := range tests {
  66. got := extractMacFromOid(tt.name, prefix)
  67. if got != tt.want {
  68. t.Errorf("extractMacFromOid(%q) = %q; want %q", tt.name, got, tt.want)
  69. }
  70. }
  71. }
  72. func TestPortStatsSpeedString(t *testing.T) {
  73. tests := []struct {
  74. name string
  75. ps PortStats
  76. want string
  77. }{
  78. {"down", PortStats{OperUp: false}, ""},
  79. {"1G highspeed", PortStats{OperUp: true, HighSpeedMbps: 1000}, "1 Gbps"},
  80. {"10G highspeed", PortStats{OperUp: true, HighSpeedMbps: 10000}, "10 Gbps"},
  81. {"100M ifSpeed", PortStats{OperUp: true, SpeedBps: 100_000_000}, "100 Mbps"},
  82. {"2.5G highspeed", PortStats{OperUp: true, HighSpeedMbps: 2500}, "2.5 Gbps"}, // #1280: ya no trunca
  83. {"zero speed", PortStats{OperUp: true}, ""},
  84. }
  85. for _, tt := range tests {
  86. got := tt.ps.SpeedString()
  87. if got != tt.want {
  88. t.Errorf("SpeedString(%s) = %q; want %q", tt.name, got, tt.want)
  89. }
  90. }
  91. }
  92. func TestPortStatsDisplayName(t *testing.T) {
  93. tests := []struct {
  94. ps PortStats
  95. want string
  96. }{
  97. {PortStats{Alias: "Uplink"}, "Uplink"},
  98. {PortStats{Name: "eth0"}, "eth0"},
  99. {PortStats{Descr: "GigabitEthernet0/1"}, "GigabitEthernet0/1"},
  100. {PortStats{Index: 5}, "port-5"},
  101. {PortStats{Alias: "Uplink", Name: "eth0"}, "Uplink"},
  102. {PortStats{Alias: " ", Name: "gi1/0/8"}, "gi1/0/8"},
  103. {PortStats{Alias: " "}, "port-0"},
  104. {PortStats{Alias: " Uplink ", Name: "eth0"}, "Uplink"},
  105. }
  106. for _, tt := range tests {
  107. got := tt.ps.DisplayName()
  108. if got != tt.want {
  109. t.Errorf("DisplayName(%+v) = %q; want %q", tt.ps, got, tt.want)
  110. }
  111. }
  112. }
  113. func TestApplyPortField(t *testing.T) {
  114. ps := &PortStats{Index: 1}
  115. applyPortField(ps, OidIfOperStatus, gosnmp.SnmpPDU{Value: 1})
  116. if !ps.OperUp {
  117. t.Error("expected OperUp=true for value 1")
  118. }
  119. applyPortField(ps, OidIfOperStatus, gosnmp.SnmpPDU{Value: 2})
  120. if ps.OperUp {
  121. t.Error("expected OperUp=false for value 2")
  122. }
  123. applyPortField(ps, OidIfSpeed, gosnmp.SnmpPDU{Value: uint(1000000000)})
  124. if ps.SpeedBps != 1_000_000_000 {
  125. t.Errorf("SpeedBps = %d; want 1000000000", ps.SpeedBps)
  126. }
  127. applyPortField(ps, OidIfName, gosnmp.SnmpPDU{Value: "eth0"})
  128. if ps.Name != "eth0" {
  129. t.Errorf("Name = %q; want eth0", ps.Name)
  130. }
  131. applyPortField(ps, OidIfHighSpeed, gosnmp.SnmpPDU{Value: uint(10000)})
  132. if ps.HighSpeedMbps != 10000 {
  133. t.Errorf("HighSpeedMbps = %d; want 10000", ps.HighSpeedMbps)
  134. }
  135. applyPortField(ps, OidIfInOctets, gosnmp.SnmpPDU{Value: big.NewInt(123456789)})
  136. if ps.RxBytes != 123456789 {
  137. t.Errorf("RxBytes = %d; want 123456789", ps.RxBytes)
  138. }
  139. // #1077: un ifAlias de solo espacios se normaliza a vacío en el parseo.
  140. applyPortField(ps, OidIfAlias, gosnmp.SnmpPDU{Value: " "})
  141. if ps.Alias != "" {
  142. t.Errorf("Alias = %q; want empty for whitespace-only ifAlias", ps.Alias)
  143. }
  144. applyPortField(ps, OidIfAlias, gosnmp.SnmpPDU{Value: " Uplink "})
  145. if ps.Alias != "Uplink" {
  146. t.Errorf("Alias = %q; want Uplink (trimmed)", ps.Alias)
  147. }
  148. }
  149. func TestSystemInfoParsing(t *testing.T) {
  150. pdus := []gosnmp.SnmpPDU{
  151. {Name: OidSysDescr, Value: "Linux switch 5.15"},
  152. {Name: OidSysName, Value: "core-switch"},
  153. {Name: OidSysUpTime, Value: uint(360000)},
  154. }
  155. info := &SystemInfo{}
  156. for _, v := range pdus {
  157. switch v.Name {
  158. case OidSysDescr:
  159. info.Descr = stringVal(v)
  160. case OidSysName:
  161. info.Name = stringVal(v)
  162. case OidSysUpTime:
  163. info.UpTimeSec = uint64Val(v) / 100
  164. }
  165. }
  166. if info.Descr != "Linux switch 5.15" {
  167. t.Errorf("Descr = %q", info.Descr)
  168. }
  169. if info.Name != "core-switch" {
  170. t.Errorf("Name = %q", info.Name)
  171. }
  172. if info.UpTimeSec != 3600 {
  173. t.Errorf("UpTimeSec = %d; want 3600", info.UpTimeSec)
  174. }
  175. }
  176. func TestStringVal(t *testing.T) {
  177. if got := stringVal(gosnmp.SnmpPDU{Value: "hello", Type: gosnmp.OctetString}); got != "hello" {
  178. t.Errorf("stringVal string = %q", got)
  179. }
  180. if got := stringVal(gosnmp.SnmpPDU{Value: []byte("bytes"), Type: gosnmp.OctetString}); got != "bytes" {
  181. t.Errorf("stringVal bytes = %q", got)
  182. }
  183. if got := stringVal(gosnmp.SnmpPDU{Type: gosnmp.NoSuchObject}); got != "" {
  184. t.Errorf("stringVal NoSuchObject = %q", got)
  185. }
  186. }
  187. func TestUint64Val(t *testing.T) {
  188. if got := uint64Val(gosnmp.SnmpPDU{Value: uint(42), Type: gosnmp.Gauge32}); got != 42 {
  189. t.Errorf("uint64Val = %d", got)
  190. }
  191. if got := uint64Val(gosnmp.SnmpPDU{Value: big.NewInt(999999), Type: gosnmp.Counter64}); got != 999999 {
  192. t.Errorf("uint64Val big = %d", got)
  193. }
  194. if got := uint64Val(gosnmp.SnmpPDU{Type: gosnmp.NoSuchInstance}); got != 0 {
  195. t.Errorf("uint64Val NoSuchInstance = %d", got)
  196. }
  197. }
  198. func TestSortByIndex(t *testing.T) {
  199. ps := []PortStats{{Index: 3}, {Index: 1}, {Index: 2}}
  200. sortByIndex(ps)
  201. for i, p := range ps {
  202. if p.Index != i+1 {
  203. t.Errorf("sortByIndex: ps[%d].Index = %d; want %d", i, p.Index, i+1)
  204. }
  205. }
  206. }
  207. // #1125 (alternativa a la exclusión de la #1115): el ifTable conserva TODAS
  208. // las interfaces y cada una se clasifica por familia; las no físicas (LAG,
  209. // VLAN, bridge, túnel, virtual) van al grupo colapsado de la tarjeta en el
  210. // frontend en vez de desaparecer. Verificado en un LGS310C real: po1-8
  211. // (ieee8023adLag) y vlan1 (l3ipvlan) clasificados; un ifType no listado o no
  212. // reportado (Type=0 por walk fallido) sigue siendo físico (defensa #1115).
  213. func TestIfFamilyClassification(t *testing.T) {
  214. cases := []struct {
  215. typ int
  216. family string
  217. }{
  218. {6, ""}, // ethernetCsmacd: física
  219. {117, ""}, // gigabitEthernet: física
  220. {161, "lag"}, // ieee8023adLag (LGS310C poN)
  221. {136, "vlan"}, // l3ipvlan (LGS310C vlan1)
  222. {135, "vlan"}, // l2vlan
  223. {209, "bridge"}, // bridge
  224. {131, "tunnel"}, // tunnel
  225. {24, "virtual"}, // softwareLoopback
  226. {53, "virtual"}, // propVirtual
  227. {0, ""}, // sin ifType: físico (walk fallido conserva todo)
  228. {9999, ""}, // desconocido: se trata como físico por exclusión
  229. }
  230. for _, c := range cases {
  231. if got := ifFamily(c.typ); got != c.family {
  232. t.Errorf("ifFamily(%d) = %q, esperaba %q", c.typ, got, c.family)
  233. }
  234. }
  235. }