4
0

iwdev_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package probe
  2. // iwdev_test.go - #1560: the neighbour scan ran once per interface, so a radio
  3. // with several BSSIDs left the channel once per BSSID. Field report #1557: a
  4. // GL-MT6000 with two 2.4 GHz SSIDs (LAN plus VLAN 10) dropped clients 44s
  5. // apart, which is two scans of the same radio in one cycle.
  6. import (
  7. "context"
  8. "log/slog"
  9. "strings"
  10. "sync"
  11. "testing"
  12. "time"
  13. )
  14. // iwDevFlint2 is a real `iw dev` from our Flint2 (GL-MT6000, GL.iNet stock
  15. // firmware): one BSSID per radio, which is why our fleet never hit the double
  16. // scan.
  17. const iwDevFlint2 = `phy#1
  18. Interface wlan1
  19. ifindex 19
  20. wdev 0x100000001
  21. addr 94:83:c4:ba:bf:ab
  22. ssid temiscira
  23. type AP
  24. channel 36 (5180 MHz), width: 80 MHz, center1: 5210 MHz
  25. txpower 23.00 dBm
  26. phy#0
  27. Interface wlan0
  28. ifindex 18
  29. wdev 0x1
  30. addr 32:4e:8e:ac:c6:16
  31. ssid temiscira
  32. type AP
  33. channel 1 (2412 MHz), width: 20 MHz, center1: 2412 MHz
  34. txpower 20.00 dBm
  35. `
  36. // iwDevMultiBSSID is the layout reported in #1557 for the same box: radio 0
  37. // carries NETGEAR on the LAN and WiFi_10 on VLAN 10. Same `iw dev` shape as
  38. // iwDevFlint2, with the second BSSID of that radio added.
  39. const iwDevMultiBSSID = `phy#1
  40. Interface phy1-ap0
  41. ifindex 19
  42. addr 94:83:c4:ba:bf:c0
  43. ssid NETGEAR
  44. type AP
  45. channel 36 (5180 MHz), width: 80 MHz, center1: 5210 MHz
  46. phy#0
  47. Interface phy0-ap0
  48. ifindex 18
  49. addr 94:83:c4:ba:bf:ab
  50. ssid NETGEAR
  51. type AP
  52. channel 1 (2412 MHz), width: 20 MHz, center1: 2412 MHz
  53. Interface phy0-ap1
  54. ifindex 20
  55. addr 96:83:c4:ba:bf:ab
  56. ssid WiFi_10
  57. type AP
  58. channel 1 (2412 MHz), width: 20 MHz, center1: 2412 MHz
  59. `
  60. // iwDevNoPhy is the output without phy headers (trimmed or old iw): the
  61. // grouping cannot know which radio an interface belongs to.
  62. const iwDevNoPhy = ` Interface wlan1
  63. ssid temiscira
  64. type AP
  65. Interface wlan0
  66. ssid temiscira
  67. type AP
  68. `
  69. func TestParseIwDevDetailedKeepsRadio(t *testing.T) {
  70. got := ParseIwDevDetailed(iwDevMultiBSSID)
  71. if len(got) != 3 {
  72. t.Fatalf("interfaces: %+v", got)
  73. }
  74. want := []IwDevIface{
  75. {Iface: "phy1-ap0", Phy: 1, Type: "AP"},
  76. {Iface: "phy0-ap0", Phy: 0, Type: "AP"},
  77. {Iface: "phy0-ap1", Phy: 0, Type: "AP"},
  78. }
  79. for i := range want {
  80. if got[i] != want[i] {
  81. t.Fatalf("iface %d: got %+v want %+v", i, got[i], want[i])
  82. }
  83. }
  84. }
  85. // TestScanTargetsOnePerRadio: two BSSIDs on phy#0 must collapse into one
  86. // target, and the AP interface of that radio is the one picked.
  87. func TestScanTargetsOnePerRadio(t *testing.T) {
  88. targets := IwDevScanTargets(ParseIwDevDetailed(iwDevMultiBSSID))
  89. if len(targets) != 2 {
  90. t.Fatalf("dos radios deben dar dos targets: %+v", targets)
  91. }
  92. if targets[0].Iface != "phy1-ap0" || targets[0].Phy != 1 {
  93. t.Fatalf("orden de aparición perdido: %+v", targets)
  94. }
  95. if targets[1].Iface != "phy0-ap0" || targets[1].Phy != 0 {
  96. t.Fatalf("radio 0 debe escanearse una vez: %+v", targets)
  97. }
  98. }
  99. func TestScanTargetsOnePerRadioOnOurFleet(t *testing.T) {
  100. targets := IwDevScanTargets(ParseIwDevDetailed(iwDevFlint2))
  101. if len(targets) != 2 || targets[0].Iface != "wlan1" || targets[1].Iface != "wlan0" {
  102. t.Fatalf("una BSS por radio debe seguir dando un target por radio: %+v", targets)
  103. }
  104. }
  105. // TestScanTargetsPrefersAP: a monitor or P2P interface sharing the radio with
  106. // the AP must not be the one scanned.
  107. func TestScanTargetsPrefersAP(t *testing.T) {
  108. iwDev := "phy#0\n" +
  109. "\tInterface mon0\n\t\ttype monitor\n" +
  110. "\tInterface p2p-dev-wlan0\n\t\ttype P2P-device\n" +
  111. "\tInterface phy0-ap0\n\t\ttype AP\n"
  112. targets := IwDevScanTargets(ParseIwDevDetailed(iwDev))
  113. if len(targets) != 1 || targets[0].Iface != "phy0-ap0" {
  114. t.Fatalf("se debe escanear el AP: %+v", targets)
  115. }
  116. }
  117. // TestScanTargetsFallsBackWithoutPhy: sin secciones phy# se mantiene el
  118. // comportamiento anterior (una interfaz por target).
  119. func TestScanTargetsFallsBackWithoutPhy(t *testing.T) {
  120. targets := IwDevScanTargets(ParseIwDevDetailed(iwDevNoPhy))
  121. if len(targets) != 2 || targets[0].Iface != "wlan1" || targets[1].Iface != "wlan0" {
  122. t.Fatalf("fallback por interfaz: %+v", targets)
  123. }
  124. for _, tgt := range targets {
  125. if tgt.Phy != -1 {
  126. t.Fatalf("sin phy# el radio debe ser -1: %+v", tgt)
  127. }
  128. }
  129. }
  130. // recordingRunner keeps every command the prober runs, in order.
  131. type recordingRunner struct {
  132. mu sync.Mutex
  133. cmds []string
  134. outs map[string]string
  135. }
  136. func (r *recordingRunner) Run(_ context.Context, cmd string, _ time.Duration) (string, error) {
  137. r.mu.Lock()
  138. r.cmds = append(r.cmds, cmd)
  139. r.mu.Unlock()
  140. if out, ok := r.outs[cmd]; ok {
  141. return out, nil
  142. }
  143. return "", &fakeErr{cmd}
  144. }
  145. func (r *recordingRunner) scanCmds() []string {
  146. r.mu.Lock()
  147. defer r.mu.Unlock()
  148. var res []string
  149. for _, c := range r.cmds {
  150. if strings.HasPrefix(c, "iw dev \"") && strings.Contains(c, " scan") {
  151. res = append(res, c)
  152. }
  153. }
  154. return res
  155. }
  156. // TestBuildScansOncePerRadio is the regression test for #1560: the same
  157. // multi-BSSID unit must run ONE scan, not one per BSSID.
  158. func TestBuildScansOncePerRadio(t *testing.T) {
  159. run := &recordingRunner{outs: map[string]string{
  160. CmdIwDev: iwDevMultiBSSID,
  161. CmdScanIface("phy1-ap0"): scanVecino24,
  162. CmdScanIface("phy0-ap0"): scanVecino24,
  163. CmdScanIface("phy0-ap1"): scanVecino24,
  164. }}
  165. p := NewProber(run, Options{})
  166. p.Build(context.Background(), "mt6000", "test")
  167. got := run.scanCmds()
  168. if len(got) != 2 {
  169. t.Fatalf("dos radios deben dar dos scans, got %d: %v", len(got), got)
  170. }
  171. if !strings.Contains(got[0], "phy1-ap0") || !strings.Contains(got[1], "phy0-ap0") {
  172. t.Fatalf("scans inesperados: %v", got)
  173. }
  174. for _, c := range got {
  175. if strings.Contains(c, "phy0-ap1") {
  176. t.Fatalf("la segunda BSS de la misma radio no se escanea: %v", got)
  177. }
  178. }
  179. }
  180. // scanVecino24 is a real single-neighbour scan output (rt2, 2.4 GHz).
  181. const scanVecino24 = `BSS c4:eb:39:0c:2b:c1(on phy0-ap0)
  182. freq: 2437.0
  183. signal: -67.00 dBm
  184. SSID: Vecino_24
  185. `
  186. // TestScanLogsCycle covers #1559: a scan must leave a trace (start, per-radio
  187. // duration and result, total) so a client drop can be lined up with it.
  188. func TestScanLogsCycle(t *testing.T) {
  189. var buf strings.Builder
  190. prev := slog.Default()
  191. slog.SetDefault(slog.New(slog.NewTextHandler(&buf, nil)))
  192. defer slog.SetDefault(prev)
  193. run := &recordingRunner{outs: map[string]string{
  194. CmdIwDev: iwDevMultiBSSID,
  195. CmdScanIface("phy1-ap0"): scanVecino24,
  196. CmdScanIface("phy0-ap0"): scanVecino24,
  197. }}
  198. p := NewProber(run, Options{})
  199. p.Build(context.Background(), "mt6000", "test")
  200. log := buf.String()
  201. for _, want := range []string{
  202. "neighbor scan start",
  203. "radios=2",
  204. "neighbor scan radio",
  205. `iface=phy0-ap0`,
  206. "bss=1",
  207. "duration_ms=",
  208. "neighbor scan done",
  209. "bss=2",
  210. } {
  211. if !strings.Contains(log, want) {
  212. t.Fatalf("log sin %q:\n%s", want, log)
  213. }
  214. }
  215. }