asuswrt_fallback_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // asuswrt_fallback_test.go — fallbacks portables del #1481 (fase 1):
  2. // uptime por /proc/uptime, clientes/radios por `iw` (nl80211) y vecinos
  3. // LLDP en texto plano. Los fixtures son capturas REALES recortadas de un
  4. // ASUS GT-BE98 Pro (Asuswrt, kernel 4.19, lldpd 0.9.8, iw sobre nl80211).
  5. package probe
  6. import (
  7. "context"
  8. "testing"
  9. )
  10. const uptimeProcFixture = "70273.29 269277.55\n"
  11. // iwDevFixture: `iw dev` real recortado a dos phys (uno 2.4 GHz con una
  12. // interfaz AP, uno 5 GHz con otra) para exercising el agrupado por phy.
  13. const iwDevFixture = `phy#3
  14. Interface wl3.1
  15. ifindex 32
  16. wdev 0x300000002
  17. addr 9a:7c:61:8f:37:e9
  18. ssid VB
  19. type AP
  20. channel 3 (2422 MHz), width: 20 MHz, center1: 2422 MHz
  21. phy#0
  22. Interface wl0
  23. ifindex 24
  24. wdev 0x1
  25. addr 10:7c:61:8f:37:ec
  26. ssid D321F96D28F481AF28FEB9152F1B213A
  27. type AP
  28. channel 48 (5240 MHz), width: 160 MHz, center1: 5250 MHz
  29. `
  30. // iwStationFixture: salida combinada de CmdIwStationDump con el ==DEV==
  31. // arriba y dos estaciones en wl3.1 + una en wl0 (captura real recortada).
  32. const iwStationFixture = "==DEV==\n" + iwDevFixture +
  33. `==IF==wl3.1
  34. Station 24:9e:7d:0a:c3:1a (on wl3.1)
  35. inactive time: 1000 ms
  36. rx bytes: 1889632
  37. rx packets: 6810
  38. tx bytes: 10787545
  39. tx packets: 40873
  40. tx failed: 23
  41. signal: -56 [-62] dBm
  42. signal avg: -54 [-61] dBm
  43. tx bitrate: 65.0 MBit/s MCS 6 short GI
  44. rx bitrate: 72.2 MBit/s MCS 7 short GI
  45. Station ec:0b:ae:f6:db:0a (on wl3.1)
  46. inactive time: 1000 ms
  47. rx bytes: 3177417
  48. tx bytes: 49829241
  49. signal: -65 [-64] dBm
  50. signal avg: -65 [-64] dBm
  51. tx bitrate: 54.0 MBit/s
  52. rx bitrate: 54.0 MBit/s
  53. ==IF==wl0
  54. Station 1c:5e:e6:38:36:7c (on wl0)
  55. inactive time: 4000 ms
  56. rx bytes: 1040745835
  57. tx bytes: 80697335
  58. signal: -42 [-41] dBm
  59. signal avg: -42 [-41] dBm
  60. tx bitrate: 108.3 MBit/s MCS 7
  61. rx bitrate: 114.7 MBit/s MCS 7
  62. `
  63. // lldpTextFixture: `lldpcli show neighbors` real recortado (lldpd 0.9.8):
  64. // un vecino con chassis mac + sysname + mgmt + caps y otro con chassis
  65. // "local" y port id por mac.
  66. const lldpTextFixture = `-------------------------------------------------------------------------------
  67. LLDP neighbors:
  68. -------------------------------------------------------------------------------
  69. Interface: eth0, via: LLDP, RID: 2, Time: 0 day, 10:10:04
  70. Chassis:
  71. ChassisID: mac 08:bd:43:75:50:cd
  72. SysName: XS708T
  73. SysDescr: XS708T 8-Port 10-Gigabit Copper Smart Managed Pro Switch
  74. MgmtIP: 192.168.2.6
  75. Capability: Bridge, on
  76. Capability: Router, off
  77. Port:
  78. PortID: local xg2
  79. PortDescr: xg2
  80. TTL: 120
  81. -------------------------------------------------------------------------------
  82. Interface: eth3, via: LLDP, RID: 1, Time: 0 day, 10:10:11
  83. Chassis:
  84. ChassisID: local EVANGELION-X
  85. Port:
  86. PortID: mac b0:6e:bf:82:9e:69
  87. TTL: 3601
  88. -------------------------------------------------------------------------------
  89. `
  90. func TestParseUptimeSecPortatil(t *testing.T) {
  91. if got := ParseUptimeSec(uptimeProcFixture); got != 70273.29 {
  92. t.Fatalf("uptime = %v, want 70273.29", got)
  93. }
  94. for _, bad := range []string{"", "\n", "basura\n", "-3 0\n"} {
  95. if got := ParseUptimeSec(bad); got != 0 {
  96. t.Fatalf("ParseUptimeSec(%q) = %v, want 0", bad, got)
  97. }
  98. }
  99. }
  100. func TestParseIwWirelessAsuswrt(t *testing.T) {
  101. clients, radios := ParseIwWireless(iwStationFixture)
  102. if len(clients) != 3 {
  103. t.Fatalf("clientes = %d, want 3: %+v", len(clients), clients)
  104. }
  105. c := clients["24:9E:7D:0A:C3:1A"]
  106. if c.SignalDbm != -56 || c.Band != "2.4 GHz" || c.RxBytes != 1889632 || c.TxBytes != 10787545 {
  107. t.Fatalf("estación wl3.1 mal parseada: %+v", c)
  108. }
  109. if c := clients["1C:5E:E6:38:36:7C"]; c.Band != "5 GHz" || c.SignalDbm != -42 {
  110. t.Fatalf("estación wl0 mal parseada: %+v", c)
  111. }
  112. if len(radios) != 2 {
  113. t.Fatalf("radios = %d, want 2: %+v", len(radios), radios)
  114. }
  115. r24, r5 := radios[0], radios[1]
  116. if r24.Name != "2.4 GHz" || r24.Channel != 3 || r24.WidthMhz != 20 || r24.Clients != 2 || r24.BSSID != "9A:7C:61:8F:37:E9" {
  117. t.Fatalf("radio 2.4 GHz mal agrupada: %+v", r24)
  118. }
  119. if r5.Name != "5 GHz" || r5.Channel != 48 || r5.WidthMhz != 160 || r5.Clients != 1 {
  120. t.Fatalf("radio 5 GHz mal agrupada: %+v", r5)
  121. }
  122. }
  123. func TestParseLldpNeighborsTextAsuswrt(t *testing.T) {
  124. nbs := ParseLldpNeighborsText(lldpTextFixture)
  125. if len(nbs) != 2 {
  126. t.Fatalf("vecinos = %d, want 2: %+v", len(nbs), nbs)
  127. }
  128. n := nbs[0]
  129. if n.Port != "eth0" || n.ChassisMac != "08:BD:43:75:50:CD" || n.Chassis != "XS708T" ||
  130. n.Mgmt != "192.168.2.6" || n.PortDesc != "xg2" {
  131. t.Fatalf("vecino 1 mal parseado: %+v", n)
  132. }
  133. if len(n.Caps) != 1 || n.Caps[0] != "Bridge" {
  134. t.Fatalf("caps solo deben contar las 'on': %+v", n.Caps)
  135. }
  136. m := nbs[1]
  137. if m.Port != "eth3" || m.Chassis != "EVANGELION-X" || m.ChassisMac != "" || m.PortDesc != "b0:6e:bf:82:9e:69" {
  138. t.Fatalf("vecino 2 mal parseado: %+v", m)
  139. }
  140. }
  141. // TestProberFallbacksAsuswrt: sin ubus, sin iwinfo y con el JSON de lldpcli
  142. // roto, el prober completo levanta uptime, clientes/radios y vecinos por
  143. // los fallbacks portables del #1481.
  144. func TestProberFallbacksAsuswrt(t *testing.T) {
  145. run := fakeRunner{outs: map[string]string{
  146. CmdProcUptime: uptimeProcFixture,
  147. CmdProcStat: "cpu 4705 356 584 3699 23 0 23 0 0 0\n",
  148. CmdNetDev: " eth0: 1000000 0 0 0 0 0 0 0 500000 0\n",
  149. CmdBridgeMAC: "10:7c:61:8f:37:e8\n",
  150. CmdPortStates: "eth0 up 1000\neth1 up 1000\n",
  151. CmdIwStationDump: iwStationFixture,
  152. CmdLldpCheck: "/usr/sbin/lldpcli\n",
  153. CmdLldpNeighbors: "lldpcli: JSON output not supported\n", // no vacío, inparseable
  154. CmdLldpNeighborsText: lldpTextFixture,
  155. }}
  156. p := NewProber(run, Options{GwPingTarget: "192.168.2.1", ScanInterval: ScanDisabled})
  157. pl := p.Build(context.Background(), "gt-be98-pro", "3.0.19")
  158. if pl.Data.System == nil || pl.Data.System.SysInfo == nil || pl.Data.System.SysInfo.Uptime != 70273.29 {
  159. t.Fatalf("uptime por /proc/uptime no llegó: %+v", pl.Data.System)
  160. }
  161. if pl.Data.Wireless == nil || len(pl.Data.Wireless.Clients) != 3 || len(pl.Data.Wireless.Radios) != 2 {
  162. t.Fatalf("wireless por iw no llegó: %+v", pl.Data.Wireless)
  163. }
  164. if pl.Data.Lldp == nil || !pl.Data.Lldp.Available || len(pl.Data.Lldp.Neighbors) != 2 {
  165. t.Fatalf("lldp en texto no llegó: %+v", pl.Data.Lldp)
  166. }
  167. }
  168. // TestProberLldpSinVecinosNoGastaTexto (review #1491): con el JSON vacío
  169. // (cero vecinos honestos) el texto NO se consulta aunque devolvería
  170. // vecinos: nada de spawns extra por ronda en unidades sin vecinos.
  171. func TestProberLldpSinVecinosNoGastaTexto(t *testing.T) {
  172. run := fakeRunner{outs: map[string]string{
  173. CmdProcStat: "cpu 4705 356 584 3699 23 0 23 0 0 0\n",
  174. CmdLldpCheck: "/usr/sbin/lldpcli\n",
  175. CmdLldpNeighbors: "", // cero vecinos
  176. CmdLldpNeighborsText: lldpTextFixture,
  177. }}
  178. p := NewProber(run, Options{GwPingTarget: "192.168.2.1", ScanInterval: ScanDisabled})
  179. pl := p.Build(context.Background(), "lab", "3.0.19")
  180. if pl.Data.Lldp == nil || !pl.Data.Lldp.Available || pl.Data.Lldp.Neighbors != nil {
  181. t.Fatalf("JSON vacío debe dar Neighbors nil sin tocar el texto: %+v", pl.Data.Lldp)
  182. }
  183. }