survey_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package adapters
  2. import (
  3. "math"
  4. "testing"
  5. )
  6. // TestFreqToChannel cubre la conversión freq MHz → número de canal.
  7. func TestFreqToChannel(t *testing.T) {
  8. cases := map[int]int{
  9. 2412: 1, // 2.4 GHz ch1
  10. 2417: 2, // ch2
  11. 2437: 6, // ch6
  12. 2462: 11, // ch11
  13. 2472: 13, // ch13
  14. 2484: 14, // ch14 (Japón)
  15. 5180: 36, // 5 GHz UNII-1 ch36
  16. 5200: 40,
  17. 5240: 48,
  18. 5745: 149, // UNII-3
  19. 5825: 165,
  20. }
  21. for freq, want := range cases {
  22. got := freqToChannel(freq)
  23. if got != want {
  24. t.Errorf("freqToChannel(%d) = %d, want %d", freq, got, want)
  25. }
  26. }
  27. }
  28. func TestFreqToBand(t *testing.T) {
  29. cases := map[int]string{
  30. 2412: "2.4 GHz",
  31. 2484: "2.4 GHz",
  32. 5180: "5 GHz",
  33. 5825: "5 GHz",
  34. 6135: "6 GHz",
  35. 9999: "",
  36. }
  37. for freq, want := range cases {
  38. got := freqToBand(freq)
  39. if got != want {
  40. t.Errorf("freqToBand(%d) = %q, want %q", freq, got, want)
  41. }
  42. }
  43. }
  44. // TestParseIwSurveyFlint2 usa la salida real de `iw dev wlan0 survey dump`
  45. // y `iw dev wlan1 survey dump` del Flint2 como fixture.
  46. func TestParseIwSurveyFlint2(t *testing.T) {
  47. out := `Survey data from wlan0
  48. frequency: 2412 MHz [in use]
  49. noise: -90 dBm
  50. channel active time: 3632796925 ms
  51. channel busy time: 878259766 ms
  52. channel receive time: 440957725 ms
  53. channel BSS receive time: 173056055 ms
  54. channel transmit time: 340995043 ms
  55. Survey data from wlan0
  56. frequency: 2417 MHz
  57. noise: -92 dBm
  58. channel active time: 72 ms
  59. channel busy time: 20 ms
  60. channel receive time: 10 ms
  61. channel transmit time: 0 ms
  62. Survey data from wlan1
  63. frequency: 5180 MHz [in use]
  64. noise: -92 dBm
  65. channel active time: 3632802379 ms
  66. channel busy time: 146150367 ms
  67. channel receive time: 67413467 ms
  68. channel transmit time: 76785952 ms
  69. Survey data from wlan1
  70. frequency: 5200 MHz
  71. noise: -92 dBm
  72. channel active time: 191 ms
  73. channel busy time: 0 ms
  74. channel receive time: 0 ms
  75. channel transmit time: 0 ms
  76. `
  77. result := ParseIwSurvey(out)
  78. if len(result) != 2 {
  79. t.Fatalf("expected 2 devices, got %d: %v", len(result), result)
  80. }
  81. if len(result["wlan0"]) != 2 {
  82. t.Errorf("wlan0 channels: %d, want 2", len(result["wlan0"]))
  83. }
  84. if len(result["wlan1"]) != 2 {
  85. t.Errorf("wlan1 channels: %d, want 2", len(result["wlan1"]))
  86. }
  87. // Canal in-use wlan0: 2412 MHz = ch1, noise -90.
  88. ch := result["wlan0"][0]
  89. if ch.Freq != 2412 || ch.Channel != 1 || !ch.InUse || ch.NoiseDbm != -90 {
  90. t.Errorf("wlan0[0] wrong: %+v", ch)
  91. }
  92. // busyPct = 878259766 / 3632796925 * 100 ≈ 24.18%
  93. expectedBusy := 878259766.0 / 3632796925.0 * 100
  94. if math.Abs(ch.BusyPct-expectedBusy) > 0.01 {
  95. t.Errorf("wlan0[0] BusyPct = %.4f, want %.4f", ch.BusyPct, expectedBusy)
  96. }
  97. // Canal NO in-use wlan0: 2417 = ch2, noise -92.
  98. ch = result["wlan0"][1]
  99. if ch.Freq != 2417 || ch.Channel != 2 || ch.InUse || ch.NoiseDbm != -92 {
  100. t.Errorf("wlan0[1] wrong: %+v", ch)
  101. }
  102. // busyPct = 20 / 72 * 100 ≈ 27.78%
  103. expectedBusy = 20.0 / 72.0 * 100
  104. if math.Abs(ch.BusyPct-expectedBusy) > 0.01 {
  105. t.Errorf("wlan0[1] BusyPct = %.4f, want %.4f", ch.BusyPct, expectedBusy)
  106. }
  107. // Canal in-use wlan1: 5180 MHz = ch36.
  108. ch = result["wlan1"][0]
  109. if ch.Freq != 5180 || ch.Channel != 36 || !ch.InUse {
  110. t.Errorf("wlan1[0] wrong: %+v", ch)
  111. }
  112. }
  113. // TestParseIwSurveyEdge cubre entradas degeneradas.
  114. func TestParseIwSurveyEdge(t *testing.T) {
  115. cases := map[string]string{
  116. "empty": "",
  117. "no-survey": "unrelated line\nanother\n",
  118. "only-header": "Survey data from wlan0\n",
  119. }
  120. for name, in := range cases {
  121. t.Run(name, func(t *testing.T) {
  122. got := ParseIwSurvey(in)
  123. if name == "only-header" {
  124. // 1 device con 0 channels (no se hace flush sin campos).
  125. if len(got["wlan0"]) != 0 {
  126. t.Fatalf("wlan0 channels: %d, want 0", len(got["wlan0"]))
  127. }
  128. return
  129. }
  130. if len(got) != 0 {
  131. t.Fatalf("expected empty map for %q, got %v", name, got)
  132. }
  133. })
  134. }
  135. }
  136. // TestParseIwSurveyChannelNoActive cubre un canal sin "channel active time"
  137. // (no debe panicar, busyPct=0).
  138. func TestParseIwSurveyChannelNoActive(t *testing.T) {
  139. out := `Survey data from wlan0
  140. frequency: 2412 MHz
  141. noise: -85 dBm
  142. `
  143. result := ParseIwSurvey(out)
  144. if len(result["wlan0"]) != 1 {
  145. t.Fatalf("expected 1 channel, got %d", len(result["wlan0"]))
  146. }
  147. ch := result["wlan0"][0]
  148. if ch.BusyPct != 0 || ch.RxPct != 0 || ch.TxPct != 0 {
  149. t.Errorf("busyPct should be 0 without active time, got busy=%.2f rx=%.2f tx=%.2f",
  150. ch.BusyPct, ch.RxPct, ch.TxPct)
  151. }
  152. if ch.NoiseDbm != -85 {
  153. t.Errorf("noise = %d, want -85", ch.NoiseDbm)
  154. }
  155. }