hostapd_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package probe
  2. import "testing"
  3. const hostapdOut = `==AP==hostapd.phy0-ap0
  4. {
  5. "freq": 2462,
  6. "clients": {
  7. "AA:BB:CC:DD:EE:FF": { "auth": true, "assoc": true, "authorized": true, "signal": -55 },
  8. "11:22:33:44:55:66": { "auth": true, "assoc": false, "authorized": false, "signal": -80 }
  9. }
  10. }
  11. ==AP==hostapd.phy1-ap0
  12. {
  13. "freq": 5180,
  14. "clients": {
  15. "AA:BB:CC:DD:EE:01": { "auth": true, "assoc": true, "authorized": true, "signal": -42 }
  16. }
  17. }`
  18. func TestParseHostapdClients(t *testing.T) {
  19. m := ParseHostapdClients(hostapdOut)
  20. // La estación sin assoc/authorized se filtra (paridad con assoclist).
  21. if len(m) != 2 {
  22. t.Fatalf("clientes: %+v (want 2, la pre-auth se filtra)", m)
  23. }
  24. c24 := m["AA:BB:CC:DD:EE:FF"]
  25. if c24.SignalDbm != -55 || c24.Band != "2.4 GHz" {
  26. t.Fatalf("2.4 GHz: %+v", c24)
  27. }
  28. c5 := m["AA:BB:CC:DD:EE:01"]
  29. if c5.SignalDbm != -42 || c5.Band != "5 GHz" {
  30. t.Fatalf("5 GHz: %+v", c5)
  31. }
  32. }
  33. func TestParseHostapdClientsBasura(t *testing.T) {
  34. if m := ParseHostapdClients("ubus: not found\n"); len(m) != 0 {
  35. t.Fatalf("output sin JSON debe dar vacío: %+v", m)
  36. }
  37. if m := ParseHostapdClients(""); len(m) != 0 {
  38. t.Fatalf("vacío: %+v", m)
  39. }
  40. }
  41. // #551: el parser de ubus hostapd get_clients debe conservar los contadores
  42. // rx/tx por estación (bytes.rx/tx) que el driver expone, para que el server
  43. // calcule el tráfico por cliente. Los drivers que NO los reportan dejan el
  44. // campo a cero (compatibilidad).
  45. func TestParseHostapdClientsBytes(t *testing.T) {
  46. withBytes := `==AP==hostapd.phy0-ap0
  47. {
  48. "freq": 2437,
  49. "clients": {
  50. "AA:BB:CC:DD:EE:FF": {
  51. "auth": true, "assoc": true, "authorized": true, "signal": -55,
  52. "bytes": { "rx": 123456, "tx": 654321 }
  53. },
  54. "11:22:33:44:55:66": {
  55. "auth": true, "assoc": true, "authorized": true, "signal": -70
  56. }
  57. }
  58. }`
  59. m := ParseHostapdClients(withBytes)
  60. if len(m) != 2 {
  61. t.Fatalf("clientes: %d (want 2)", len(m))
  62. }
  63. c := m["AA:BB:CC:DD:EE:FF"]
  64. if c.RxBytes != 123456 || c.TxBytes != 654321 {
  65. t.Fatalf("contadores no parseados: rx=%d tx=%d (want 123456/654321)", c.RxBytes, c.TxBytes)
  66. }
  67. if c2 := m["11:22:33:44:55:66"]; c2.RxBytes != 0 || c2.TxBytes != 0 {
  68. t.Fatalf("estación sin bytes debe quedar a 0: %+v", c2)
  69. }
  70. }
  71. func TestParseWirelessCombined(t *testing.T) {
  72. out := `R|2.437|6|HE20|22|2
  73. C|AA:BB:CC:DD:EE:FF|-55|2.437
  74. C|aa:bb:cc:dd:ee:02|-67|2.437
  75. R|5.240|48|HE80|23|1
  76. C|AA:BB:CC:DD:EE:01|-40|5.240
  77. `
  78. clients, radios := ParseWirelessCombined(out)
  79. if len(clients) != 3 {
  80. t.Fatalf("clientes: %+v", clients)
  81. }
  82. if clients["AA:BB:CC:DD:EE:02"].SignalDbm != -67 || clients["AA:BB:CC:DD:EE:02"].Band != "2.4 GHz" {
  83. t.Fatalf("cliente lower: %+v", clients["AA:BB:CC:DD:EE:02"])
  84. }
  85. if len(radios) != 2 {
  86. t.Fatalf("radios: %+v", radios)
  87. }
  88. var r24, r5 *Radio
  89. for i := range radios {
  90. switch radios[i].Name {
  91. case "2.4 GHz":
  92. r24 = &radios[i]
  93. case "5 GHz":
  94. r5 = &radios[i]
  95. }
  96. }
  97. if r24 == nil || r24.Clients != 2 || r24.Channel != 6 || r24.WidthMhz != 20 {
  98. t.Fatalf("radio 2.4: %+v", r24)
  99. }
  100. if r5 == nil || r5.Clients != 1 || r5.WidthMhz != 80 {
  101. t.Fatalf("radio 5: %+v", r5)
  102. }
  103. }
  104. func TestParseWirelessCombinedVacio(t *testing.T) {
  105. clients, radios := ParseWirelessCombined("")
  106. if len(clients) != 0 || len(radios) != 0 {
  107. t.Fatalf("vacío: %+v %+v", clients, radios)
  108. }
  109. }
  110. // TestProberWirelessUbusPath: con ubus hostapd disponible, el sondeo usa SU
  111. // output (el runner falso solo responde a comandos ubus; si el flujo llamara
  112. // a iwinfo recibiría error y el test fallaría).
  113. func TestProberWirelessUbusPath(t *testing.T) {
  114. run := fakeRunner{outs: map[string]string{
  115. CmdHostapdClients: `==AP==hostapd.phy0-ap0
  116. {
  117. "freq": 2437,
  118. "clients": {
  119. "EC:71:DB:44:12:8A": { "auth": true, "assoc": true, "authorized": true, "signal": -58 }
  120. }
  121. }`,
  122. }}
  123. p := NewProber(run, Options{})
  124. wd := p.probeWireless(nil, true)
  125. if wd == nil {
  126. t.Fatal("wireless nil en la vía ubus")
  127. }
  128. c := wd.Clients["EC:71:DB:44:12:8A"]
  129. if c.SignalDbm != -58 || c.Band != "2.4 GHz" {
  130. t.Fatalf("cliente ubus: %+v", c)
  131. }
  132. // Path de eventos: mismos clientes sin tocar iwinfo.
  133. wd2 := p.probeWireless(nil, false)
  134. if wd2 == nil || wd2.Clients["EC:71:DB:44:12:8A"].SignalDbm != -58 {
  135. t.Fatalf("path eventos: %+v", wd2)
  136. }
  137. }
  138. // TestProberWirelessRadiosCache: el path de eventos reutiliza las radios del
  139. // último sondeo completo dentro del TTL.
  140. func TestProberWirelessRadiosCache(t *testing.T) {
  141. run := fakeRunner{outs: map[string]string{
  142. CmdHostapdClients: `==AP==hostapd.phy1-ap0
  143. {
  144. "freq": 5240,
  145. "clients": {
  146. "EC:71:DB:44:12:8B": { "auth": true, "assoc": true, "authorized": true, "signal": -40 }
  147. }
  148. }`,
  149. CmdRadios: "5.24|48|HE80|23|1\n",
  150. }}
  151. p := NewProber(run, Options{})
  152. wd := p.probeWireless(nil, true)
  153. if len(wd.Radios) != 1 || wd.Radios[0].Channel != 48 {
  154. t.Fatalf("radios full: %+v", wd.Radios)
  155. }
  156. wd2 := p.probeWireless(nil, false)
  157. if len(wd2.Radios) != 1 || wd2.Radios[0].Channel != 48 {
  158. t.Fatalf("radios cacheadas en eventos: %+v", wd2.Radios)
  159. }
  160. }