scan_fallback_1839_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package probe
  2. // scan_fallback_1839_test.go — #1839: en Asuswrt (dhd/Broadcom) cada radio
  3. // expone una VAP AP y una managed. La AP contesta al scan con "Operation not
  4. // supported (-95)" y la managed si devuelve BSSes. El agente elegia la AP
  5. // (para que el nombre de interfaz en wifi_scans fuera el de siempre) y se
  6. // quedaba sin vecinos sin dejar rastro. Ahora, si la elegida falla, se
  7. // reintenta en el mismo ciclo con otra interfaz del mismo phy, y el resultado
  8. // por radio viaja en scanOutcomes.
  9. import (
  10. "context"
  11. "strings"
  12. "testing"
  13. "time"
  14. )
  15. // iwDevAsuswrt reproduce una radio con VAP AP y managed en el mismo phy, más
  16. // una segunda radio distinta que no debe entrar en los reintentos.
  17. const iwDevAsuswrt = "phy#1\n" +
  18. "\tInterface wl1.1\n" +
  19. "\t\taddr 62:22:32:5b:5c:99\n" +
  20. "\t\tssid I1_LAN\n" +
  21. "\t\ttype AP\n" +
  22. "\t\tchannel 48 (5240 MHz), width: 80 MHz, center1: 5210 MHz\n" +
  23. "\tInterface eth6\n" +
  24. "\t\taddr 62:22:32:5b:5c:9a\n" +
  25. "\t\ttype managed\n" +
  26. "\t\tchannel 48 (5240 MHz), width: 80 MHz, center1: 5210 MHz\n" +
  27. "phy#0\n" +
  28. "\tInterface wl0.1\n" +
  29. "\t\taddr 62:22:32:5b:5c:98\n" +
  30. "\t\tssid I1_LAN\n" +
  31. "\t\ttype AP\n" +
  32. "\t\tchannel 11 (2462 MHz), width: 20 MHz, center1: 2462 MHz\n"
  33. // scan5g es la salida real que devuelve la VAP managed en el equipo del
  34. // reportero: freqs 5200/5240, o sea el mismo bloque de 80 MHz.
  35. const scan5g = "BSS a4:2b:b0:11:22:33(on eth6)\n" +
  36. "\tlast seen: 275247.799s [boottime]\n" +
  37. "\tfreq: 5240.0\n" +
  38. "\tcapability: ESS (0x1011)\n" +
  39. "\tsignal: -58.00 dBm\n" +
  40. "\tlast seen: 120 ms ago\n" +
  41. "\tSSID: Vecino_5G\n"
  42. // runnerVapApRechaza simula la AP que rechaza el scan y la managed que
  43. // responde.
  44. type runnerVapApRechaza struct {
  45. fakeRunner
  46. fail map[string]bool
  47. }
  48. func (r runnerVapApRechaza) Run(ctx context.Context, cmd string, timeout time.Duration) (string, error) {
  49. if r.fail[cmd] {
  50. return "", &fakeErr{cmd}
  51. }
  52. return r.fakeRunner.Run(ctx, cmd, timeout)
  53. }
  54. func TestIwDevScanAlternates(t *testing.T) {
  55. ifaces := ParseIwDevDetailed(iwDevAsuswrt)
  56. chosen := IwDevScanTargets(ifaces)[0]
  57. if chosen.Iface != "wl1.1" {
  58. t.Fatalf("se esperaba la AP como objetivo: %+v", chosen)
  59. }
  60. alts := IwDevScanAlternates(ifaces, chosen)
  61. if len(alts) != 1 || alts[0].Iface != "eth6" {
  62. t.Fatalf("solo la managed del mismo phy vale como reintento: %+v", alts)
  63. }
  64. // Monitor va despues de managed.
  65. withMonitor := append(append([]IwDevIface{}, ifaces...), IwDevIface{Iface: "mon0", Phy: 1, Type: "monitor"})
  66. alts = IwDevScanAlternates(withMonitor, chosen)
  67. if len(alts) != 2 || alts[0].Iface != "eth6" || alts[1].Iface != "mon0" {
  68. t.Fatalf("orden de reintentos: %+v", alts)
  69. }
  70. if got := IwDevScanAlternates(ifaces, IwDevIface{Iface: "wlan0", Phy: -1}); got != nil {
  71. t.Fatalf("sin phy no hay reintento posible: %+v", got)
  72. }
  73. }
  74. func TestScanRechazadoReintentaEnLaManaged(t *testing.T) {
  75. run := runnerVapApRechaza{
  76. fakeRunner: fakeRunner{outs: map[string]string{
  77. CmdIwDev: iwDevAsuswrt,
  78. CmdScanIface("eth6"): scan5g,
  79. CmdScanIface("wl0.1"): "",
  80. CmdScanIface("wl1.1"): "",
  81. CmdScanIface("phy1-ap0"): "",
  82. }},
  83. fail: map[string]bool{CmdScanIface("wl1.1"): true},
  84. }
  85. p := NewProber(run, Options{})
  86. pl := p.Build(context.Background(), "rt-ax92u", "test")
  87. if pl.Data.Wireless == nil {
  88. t.Fatal("sección wireless ausente")
  89. }
  90. w := pl.Data.Wireless
  91. if len(w.Scans) != 1 || w.Scans[0].Iface != "eth6" || w.Scans[0].SSID != "Vecino_5G" {
  92. t.Fatalf("el vecino debe venir de la managed: %+v", w.Scans)
  93. }
  94. if len(w.ScanOutcomes) != 2 {
  95. t.Fatalf("un outcome por radio: %+v", w.ScanOutcomes)
  96. }
  97. if o := w.ScanOutcomes[0]; o.Iface != "eth6" || o.Phy != 1 || o.BSS != 1 || o.Error != "" {
  98. t.Fatalf("outcome de la radio 5 GHz: %+v", o)
  99. }
  100. }
  101. func TestScanRechazadoRegistraElMotivo(t *testing.T) {
  102. run := runnerVapApRechaza{
  103. fakeRunner: fakeRunner{outs: map[string]string{CmdIwDev: iwDevAsuswrt}},
  104. fail: map[string]bool{
  105. CmdScanIface("wl1.1"): true,
  106. CmdScanIface("eth6"): true,
  107. CmdScanIface("wl0.1"): true,
  108. },
  109. }
  110. p := NewProber(run, Options{})
  111. pl := p.Build(context.Background(), "rt-ax92u", "test")
  112. if pl.Data.Wireless == nil {
  113. t.Fatal("sección wireless ausente")
  114. }
  115. w := pl.Data.Wireless
  116. if len(w.Scans) != 0 {
  117. t.Fatalf("sin respuesta no hay vecinos: %+v", w.Scans)
  118. }
  119. if len(w.ScanOutcomes) != 2 {
  120. t.Fatalf("un outcome por radio: %+v", w.ScanOutcomes)
  121. }
  122. for _, o := range w.ScanOutcomes {
  123. if o.Error == "" {
  124. t.Fatalf("el motivo debe quedar registrado: %+v", o)
  125. }
  126. }
  127. }
  128. // TestShellRunnerExponeElStderr cubre la otra mitad del issue: el motivo lo
  129. // escribe iw en stderr y Output() lo descartaba.
  130. func TestShellRunnerExponeElStderr(t *testing.T) {
  131. _, err := ShellRunner{}.Run(context.Background(),
  132. "printf 'command failed: Operation not supported (-95)\\n' >&2; exit 1", 5*time.Second)
  133. if err == nil {
  134. t.Fatal("se esperaba error")
  135. }
  136. if !strings.Contains(err.Error(), "Operation not supported (-95)") {
  137. t.Fatalf("el error debe llevar el motivo del stderr: %v", err)
  138. }
  139. }