ie_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package wifi
  2. import (
  3. "testing"
  4. )
  5. // ie builds one information element
  6. func ie(id byte, data ...byte) []byte {
  7. return append([]byte{id, byte(len(data))}, data...)
  8. }
  9. // ieExt builds one extension element
  10. func ieExt(ext byte, data ...byte) []byte {
  11. return ie(ieExtension, append([]byte{ext}, data...)...)
  12. }
  13. func concat(parts ...[]byte) []byte {
  14. out := []byte{}
  15. for _, p := range parts {
  16. out = append(out, p...)
  17. }
  18. return out
  19. }
  20. // rsnIE builds an RSN element with the given pairwise ciphers, AKMs and capabilities
  21. func rsnIE(ciphers []byte, akms []byte, caps uint16) []byte {
  22. d := []byte{1, 0, 0x00, 0x0F, 0xAC, 4}
  23. d = append(d, byte(len(ciphers)), 0)
  24. for _, c := range ciphers {
  25. d = append(d, 0x00, 0x0F, 0xAC, c)
  26. }
  27. d = append(d, byte(len(akms)), 0)
  28. for _, a := range akms {
  29. d = append(d, 0x00, 0x0F, 0xAC, a)
  30. }
  31. d = append(d, byte(caps), byte(caps>>8))
  32. return ie(ieRSN, d...)
  33. }
  34. func wpaIE(ciphers []byte, akms []byte) []byte {
  35. d := []byte{0x00, 0x50, 0xF2, 1, 1, 0, 0x00, 0x50, 0xF2, 2}
  36. d = append(d, byte(len(ciphers)), 0)
  37. for _, c := range ciphers {
  38. d = append(d, 0x00, 0x50, 0xF2, c)
  39. }
  40. d = append(d, byte(len(akms)), 0)
  41. for _, a := range akms {
  42. d = append(d, 0x00, 0x50, 0xF2, a)
  43. }
  44. return ie(ieVendorSpecific, d...)
  45. }
  46. // htCaps builds an HT capabilities element advertising n spatial streams
  47. func htCaps(streams int) []byte {
  48. d := make([]byte, 26)
  49. for i := 0; i < streams && i < 4; i++ {
  50. d[3+i] = 0xFF
  51. }
  52. return ie(ieHTCapabilities, d...)
  53. }
  54. // vhtCaps builds a VHT capabilities element: n streams, MCS map code for all
  55. func vhtCaps(streams int, code uint16) []byte {
  56. var m uint16 = 0xFFFF
  57. for i := 0; i < streams; i++ {
  58. m &^= 3 << (2 * i)
  59. m |= code << (2 * i)
  60. }
  61. d := make([]byte, 12)
  62. d[4] = byte(m)
  63. d[5] = byte(m >> 8)
  64. return ie(ieVHTCapabilities, d...)
  65. }
  66. func heCaps(streams int, code uint16) []byte {
  67. var m uint16 = 0xFFFF
  68. for i := 0; i < streams; i++ {
  69. m &^= 3 << (2 * i)
  70. m |= code << (2 * i)
  71. }
  72. d := make([]byte, 21)
  73. d[17] = byte(m)
  74. d[18] = byte(m >> 8)
  75. return ieExt(ieExtHECapabilities, d...)
  76. }
  77. func TestForEachIEStopsOnTruncation(t *testing.T) {
  78. cases := []struct {
  79. name string
  80. in []byte
  81. want int
  82. }{
  83. {"empty", nil, 0},
  84. {"one byte", []byte{0}, 0},
  85. {"two elements", concat(ie(0, 'a'), ie(3, 6)), 2},
  86. {"truncated second", append(ie(0, 'a'), 3, 5, 1), 1},
  87. {"zero length", ie(0), 1},
  88. }
  89. for _, tc := range cases {
  90. n := 0
  91. forEachIE(tc.in, func(id byte, d []byte) { n++ })
  92. if n != tc.want {
  93. t.Errorf("%s: got %d elements, want %d", tc.name, n, tc.want)
  94. }
  95. }
  96. }
  97. func TestSecurityName(t *testing.T) {
  98. cases := []struct {
  99. name string
  100. ies []byte
  101. privacy bool
  102. wantSec string
  103. wantCipher string
  104. wantPMF string
  105. }{
  106. {"open", nil, false, "Open", "", ""},
  107. {"wep", nil, true, "WEP", "WEP", ""},
  108. {"wpa2 personal", rsnIE([]byte{4}, []byte{2}, 0), true, "WPA2-Personal", "AES-CCMP", ""},
  109. {"wpa3 personal", rsnIE([]byte{4}, []byte{8}, 0x00C0), true, "WPA3-Personal", "AES-CCMP", "Required"},
  110. {"wpa2/wpa3 transition", rsnIE([]byte{4}, []byte{2, 8}, 0x0080), true, "WPA2/WPA3-Personal", "AES-CCMP", "Capable"},
  111. {"wpa/wpa2 mixed", concat(rsnIE([]byte{2, 4}, []byte{2}, 0), wpaIE([]byte{2}, []byte{2})), true, "WPA/WPA2-Personal", "TKIP + AES-CCMP", ""},
  112. {"wpa only", wpaIE([]byte{2}, []byte{2}), true, "WPA-Personal", "TKIP", ""},
  113. {"wpa enterprise", wpaIE([]byte{2}, []byte{1}), true, "WPA-Enterprise", "TKIP", ""},
  114. {"wpa2 enterprise", rsnIE([]byte{4}, []byte{1}, 0), true, "WPA2-Enterprise", "AES-CCMP", ""},
  115. {"wpa3 enterprise", rsnIE([]byte{4}, []byte{5}, 0x00C0), true, "WPA3-Enterprise", "AES-CCMP", "Required"},
  116. {"wpa3 192-bit", rsnIE([]byte{9}, []byte{12}, 0x00C0), true, "WPA3-Enterprise 192-bit", "AES-GCMP-256", "Required"},
  117. {"owe", rsnIE([]byte{4}, []byte{18}, 0x00C0), false, "Enhanced Open (OWE)", "AES-CCMP", "Required"},
  118. {"rsn version only", ie(ieRSN, 1, 0), true, "WPA2-Enterprise", "AES-CCMP", ""},
  119. }
  120. for _, tc := range cases {
  121. e := parseIEs(tc.ies)
  122. if got := e.securityName(tc.privacy); got != tc.wantSec {
  123. t.Errorf("%s: security = %q, want %q", tc.name, got, tc.wantSec)
  124. }
  125. if got := e.cipherName(tc.privacy); got != tc.wantCipher {
  126. t.Errorf("%s: cipher = %q, want %q", tc.name, got, tc.wantCipher)
  127. }
  128. if e.PMF != tc.wantPMF {
  129. t.Errorf("%s: PMF = %q, want %q", tc.name, e.PMF, tc.wantPMF)
  130. }
  131. }
  132. }
  133. func TestPhyInfo(t *testing.T) {
  134. cases := []struct {
  135. name string
  136. ies []byte
  137. band string
  138. legacyMax float64
  139. wantPHY string
  140. wantGen string
  141. wantWidth int
  142. wantCenter int
  143. wantStreams int
  144. wantMaxRate float64
  145. }{
  146. {"802.11b", nil, "2.4 GHz", 11, "802.11b", "", 20, 0, 0, 11},
  147. {"802.11g", nil, "2.4 GHz", 54, "802.11g", "", 20, 0, 0, 54},
  148. {"802.11a", nil, "5 GHz", 54, "802.11a", "", 20, 0, 0, 54},
  149. {"n 20MHz 2ss", concat(htCaps(2), ie(ieHTOperation, 6, 0, 0, 0, 0)), "2.4 GHz", 54, "802.11n", "Wi-Fi 4", 20, 0, 2, 144.4},
  150. {"n 40MHz above", concat(htCaps(2), ie(ieHTOperation, 36, 0x05, 0, 0, 0)), "5 GHz", 54, "802.11n", "Wi-Fi 4", 40, 38, 2, 300},
  151. {"n 40MHz below", concat(htCaps(1), ie(ieHTOperation, 40, 0x07, 0, 0, 0)), "5 GHz", 54, "802.11n", "Wi-Fi 4", 40, 38, 1, 150},
  152. {"ac 80MHz 3ss", concat(htCaps(3), ie(ieHTOperation, 149, 0x05), vhtCaps(3, 2), ie(ieVHTOperation, 1, 155, 0, 0, 0)), "5 GHz", 54, "802.11ac", "Wi-Fi 5", 80, 155, 3, 1300},
  153. {"ac 160MHz", concat(htCaps(2), ie(ieHTOperation, 36, 0x05), vhtCaps(2, 2), ie(ieVHTOperation, 1, 42, 50, 0, 0)), "5 GHz", 54, "802.11ac", "Wi-Fi 5", 160, 50, 2, 1733.3},
  154. {"vht on 2.4 is n", concat(htCaps(2), ie(ieHTOperation, 6, 0), vhtCaps(2, 2)), "2.4 GHz", 54, "802.11n", "Wi-Fi 4", 20, 0, 2, 144.4},
  155. {"ax 80MHz 2ss", concat(htCaps(2), ie(ieHTOperation, 36, 0x05), vhtCaps(2, 2), ie(ieVHTOperation, 1, 42, 0), heCaps(2, 2)), "5 GHz", 54, "802.11ax", "Wi-Fi 6", 80, 42, 2, 1201},
  156. {"ax 6GHz 160MHz", concat(heCaps(2, 2), ieExt(ieExtHEOperation, 0, 0, 0x02, 0, 0, 0, 37, 0x03, 39, 47, 0)), "6 GHz", 54, "802.11ax", "Wi-Fi 6E", 160, 47, 2, 2402},
  157. {"be 320MHz", concat(heCaps(2, 2), ieExt(ieExtEHTCapabilities, 0), ieExt(ieExtEHTOperation, 0x01, 0, 0, 0, 0, 0x04, 31, 63)), "6 GHz", 54, "802.11be", "Wi-Fi 7", 320, 63, 2, 5764.7},
  158. }
  159. for _, tc := range cases {
  160. e := parseIEs(tc.ies)
  161. phy, gen, width, center := e.phyInfo(tc.band, tc.legacyMax)
  162. if phy != tc.wantPHY || gen != tc.wantGen || width != tc.wantWidth || center != tc.wantCenter {
  163. t.Errorf("%s: phyInfo = (%q, %q, %d, %d), want (%q, %q, %d, %d)", tc.name, phy, gen, width, center, tc.wantPHY, tc.wantGen, tc.wantWidth, tc.wantCenter)
  164. }
  165. streams := e.spatialStreams()
  166. if streams != tc.wantStreams {
  167. t.Errorf("%s: streams = %d, want %d", tc.name, streams, tc.wantStreams)
  168. }
  169. maxMCS := e.heMaxMCS
  170. if phy == "802.11ac" {
  171. maxMCS = e.vhtMaxMCS
  172. }
  173. if got := estimateMaxRate(phy, width, streams, maxMCS, tc.legacyMax); got != tc.wantMaxRate {
  174. t.Errorf("%s: max rate = %v, want %v", tc.name, got, tc.wantMaxRate)
  175. }
  176. }
  177. }
  178. func TestParseIEsMisc(t *testing.T) {
  179. blob := concat(
  180. ie(ieSSID, 'H', 'o', 'm', 'e'),
  181. ie(ieDSParameterSet, 11),
  182. ie(ieCountry, 'T', 'W', ' '),
  183. ie(ieBSSLoad, 5, 0, 128, 0, 0),
  184. ie(ieMobilityDomain, 1, 2, 3),
  185. ie(ieRMEnabledCaps, 0, 0, 0, 0, 0),
  186. ie(ieExtCapabilities, 0, 0, 0x08),
  187. ie(ieVendorSpecific, 0x00, 0x50, 0xF2, 4, 0x10),
  188. )
  189. e := parseIEs(blob)
  190. if string(e.SSID) != "Home" || !e.HasSSID {
  191. t.Errorf("SSID = %q", e.SSID)
  192. }
  193. if e.DSChannel != 11 {
  194. t.Errorf("DSChannel = %d, want 11", e.DSChannel)
  195. }
  196. if e.Country != "TW" {
  197. t.Errorf("Country = %q, want TW", e.Country)
  198. }
  199. if e.StationCount == nil || *e.StationCount != 5 {
  200. t.Errorf("StationCount = %v, want 5", e.StationCount)
  201. }
  202. if e.ChannelUtilization == nil || *e.ChannelUtilization != 50 {
  203. t.Errorf("ChannelUtilization = %v, want 50", e.ChannelUtilization)
  204. }
  205. if !e.WPS {
  206. t.Error("WPS not detected")
  207. }
  208. want := []string{"802.11k", "802.11v", "802.11r"}
  209. if len(e.Roaming) != len(want) {
  210. t.Fatalf("Roaming = %v, want %v", e.Roaming, want)
  211. }
  212. for i := range want {
  213. if e.Roaming[i] != want[i] {
  214. t.Errorf("Roaming = %v, want %v", e.Roaming, want)
  215. }
  216. }
  217. //An invalid country code is ignored
  218. if c := parseIEs(ie(ieCountry, 0xFF, 'W', ' ')).Country; c != "" {
  219. t.Errorf("invalid country parsed as %q", c)
  220. }
  221. }
  222. // TestParseIEsTruncated feeds every prefix of a full IE blob, and every blob
  223. // with one length byte changed, to the parser: it must never panic.
  224. func TestParseIEsTruncated(t *testing.T) {
  225. blob := concat(
  226. ie(ieSSID, 'x'), htCaps(2), ie(ieHTOperation, 36, 0x05),
  227. rsnIE([]byte{4, 2}, []byte{2, 8}, 0xC0), wpaIE([]byte{2}, []byte{2}),
  228. vhtCaps(2, 2), ie(ieVHTOperation, 1, 42, 50), heCaps(2, 2),
  229. ieExt(ieExtHEOperation, 0, 0xC0, 0x02, 0, 0, 0, 0, 0, 0, 37, 0x03, 39, 47, 0),
  230. ieExt(ieExtEHTOperation, 0x01, 0, 0, 0, 0, 0x04, 31, 63),
  231. ie(ieBSSLoad, 1, 0, 9, 0, 0), ie(ieCountry, 'U', 'S'),
  232. )
  233. for i := 0; i <= len(blob); i++ {
  234. parseIEs(blob[:i])
  235. }
  236. for i := range blob {
  237. for _, v := range []byte{0, 1, 2, 3, 4, 0x7F, 0xFF} {
  238. mutated := append([]byte{}, blob...)
  239. mutated[i] = v
  240. parseIEs(mutated)
  241. ratesFromIE(mutated)
  242. }
  243. }
  244. //Element bodies on their own
  245. for i := 0; i < 40; i++ {
  246. body := make([]byte, i)
  247. for j := range body {
  248. body[j] = 0xFF
  249. }
  250. parseCipherSuites(body, true)
  251. parseCipherSuites(body, false)
  252. e := bssElements{}
  253. e.parseHEOperation(body)
  254. }
  255. }
  256. func TestLegacyRates(t *testing.T) {
  257. basic, all, max := legacyRates([]uint16{0x8002, 0x8004, 0x800B, 0x8016, 12, 18, 0, 0x8002})
  258. wantBasic := []string{"1", "2", "5.5", "11"}
  259. wantAll := []string{"1", "2", "5.5", "11", "6", "9"}
  260. if len(basic) != len(wantBasic) || len(all) != len(wantAll) {
  261. t.Fatalf("legacyRates = %v / %v", basic, all)
  262. }
  263. for i := range wantBasic {
  264. if basic[i] != wantBasic[i] {
  265. t.Errorf("basic = %v, want %v", basic, wantBasic)
  266. }
  267. }
  268. for i := range wantAll {
  269. if all[i] != wantAll[i] {
  270. t.Errorf("all = %v, want %v", all, wantAll)
  271. }
  272. }
  273. if max != 11 {
  274. t.Errorf("max = %v, want 11", max)
  275. }
  276. rates := ratesFromIE(concat(ie(ieSupportedRates, 0x82, 0x0C), ie(ieExtSupportedRate, 0x6C)))
  277. if len(rates) != 3 || rates[0] != 0x8002 || rates[1] != 12 || rates[2] != 108 {
  278. t.Errorf("ratesFromIE = %v", rates)
  279. }
  280. }
  281. func TestChannelHelpers(t *testing.T) {
  282. cases := []struct {
  283. mhz int
  284. band string
  285. channel int
  286. }{
  287. {2412, "2.4 GHz", 1},
  288. {2437, "2.4 GHz", 6},
  289. {2472, "2.4 GHz", 13},
  290. {2484, "2.4 GHz", 14},
  291. {5180, "5 GHz", 36},
  292. {5745, "5 GHz", 149},
  293. {5885, "5 GHz", 177},
  294. {4920, "5 GHz", 184},
  295. {5935, "6 GHz", 2},
  296. {5955, "6 GHz", 1},
  297. {6135, "6 GHz", 37},
  298. {7115, "6 GHz", 233},
  299. {60480, "60 GHz", 2},
  300. {1000, "", 0},
  301. }
  302. for _, tc := range cases {
  303. if got := bandOfFrequency(tc.mhz); got != tc.band {
  304. t.Errorf("bandOfFrequency(%d) = %q, want %q", tc.mhz, got, tc.band)
  305. }
  306. if got := channelOfFrequency(tc.mhz); got != tc.channel {
  307. t.Errorf("channelOfFrequency(%d) = %d, want %d", tc.mhz, got, tc.channel)
  308. }
  309. }
  310. freqCases := []struct {
  311. ch int
  312. sixGHz bool
  313. want int
  314. }{
  315. {1, false, 2412}, {14, false, 2484}, {36, false, 5180}, {165, false, 5825},
  316. {184, false, 4920}, {1, true, 5955}, {2, true, 5935}, {37, true, 6135}, {0, false, 0}, {300, false, 0},
  317. }
  318. for _, tc := range freqCases {
  319. if got := frequencyOfChannel(tc.ch, tc.sixGHz); got != tc.want {
  320. t.Errorf("frequencyOfChannel(%d, %v) = %d, want %d", tc.ch, tc.sixGHz, got, tc.want)
  321. }
  322. }
  323. }
  324. func TestQualityConversions(t *testing.T) {
  325. cases := []struct {
  326. dbm int
  327. quality int
  328. }{
  329. {0, -1}, {-30, 100}, {-50, 100}, {-60, 80}, {-75, 50}, {-100, 0}, {-110, 0},
  330. }
  331. for _, tc := range cases {
  332. if got := qualityFromRSSI(tc.dbm); got != tc.quality {
  333. t.Errorf("qualityFromRSSI(%d) = %d, want %d", tc.dbm, got, tc.quality)
  334. }
  335. }
  336. if got := rssiFromQuality(80); got != -60 {
  337. t.Errorf("rssiFromQuality(80) = %d, want -60", got)
  338. }
  339. if got := rssiFromQuality(-1); got != 0 {
  340. t.Errorf("rssiFromQuality(-1) = %d, want 0", got)
  341. }
  342. }
  343. func TestSSIDString(t *testing.T) {
  344. cases := []struct {
  345. in []byte
  346. want string
  347. hidden bool
  348. }{
  349. {nil, "", true},
  350. {[]byte{0, 0, 0}, "", true},
  351. {[]byte("Cafe"), "Cafe", false},
  352. {[]byte{0xE5, 0xAE, 0xB6}, "\u5bb6", false},
  353. {[]byte{'a', 0xFF}, "a\uFFFD", false},
  354. }
  355. for _, tc := range cases {
  356. got, hidden := ssidString(tc.in)
  357. if got != tc.want || hidden != tc.hidden {
  358. t.Errorf("ssidString(%v) = (%q, %v), want (%q, %v)", tc.in, got, hidden, tc.want, tc.hidden)
  359. }
  360. }
  361. }