wlanstruct_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package wifi
  2. import (
  3. "encoding/binary"
  4. "testing"
  5. "unicode/utf16"
  6. )
  7. func putUTF16(b []byte, s string) {
  8. for i, c := range utf16.Encode([]rune(s)) {
  9. binary.LittleEndian.PutUint16(b[i*2:], c)
  10. }
  11. }
  12. func putSSID(b []byte, ssid string) {
  13. binary.LittleEndian.PutUint32(b[0:4], uint32(len(ssid)))
  14. copy(b[4:36], ssid)
  15. }
  16. type testBSS struct {
  17. ssid string
  18. bssid [6]byte
  19. phy uint32
  20. rssi int32
  21. quality uint32
  22. beacon uint16
  23. tsf uint64
  24. caps uint16
  25. freqKHz uint32
  26. rates []uint16
  27. ies []byte
  28. }
  29. // buildBSSList lays out a WLAN_BSS_LIST the way wlansvc does: the fixed
  30. // entries first, then the information elements of every entry
  31. func buildBSSList(entries []testBSS) []byte {
  32. le := binary.LittleEndian
  33. fixed := 8 + len(entries)*wlanBSSEntrySize
  34. b := make([]byte, fixed)
  35. le.PutUint32(b[4:8], uint32(len(entries)))
  36. for i, e := range entries {
  37. start := 8 + i*wlanBSSEntrySize
  38. d := b[start : start+wlanBSSEntrySize]
  39. putSSID(d[0:36], e.ssid)
  40. copy(d[40:46], e.bssid[:])
  41. le.PutUint32(d[48:52], 1)
  42. le.PutUint32(d[52:56], e.phy)
  43. le.PutUint32(d[56:60], uint32(e.rssi))
  44. le.PutUint32(d[60:64], e.quality)
  45. d[64] = 1
  46. le.PutUint16(d[66:68], e.beacon)
  47. le.PutUint64(d[72:80], e.tsf)
  48. le.PutUint16(d[88:90], e.caps)
  49. le.PutUint32(d[92:96], e.freqKHz)
  50. le.PutUint32(d[96:100], uint32(len(e.rates)))
  51. for r, rate := range e.rates {
  52. le.PutUint16(d[100+r*2:], rate)
  53. }
  54. le.PutUint32(d[352:356], uint32(len(b)-start))
  55. le.PutUint32(d[356:360], uint32(len(e.ies)))
  56. b = append(b, e.ies...)
  57. for len(b)%8 != 0 {
  58. b = append(b, 0)
  59. }
  60. }
  61. le.PutUint32(b[0:4], uint32(len(b)))
  62. return b
  63. }
  64. func TestParseWlanBSSList(t *testing.T) {
  65. list := buildBSSList([]testBSS{
  66. {
  67. ssid: "Home-5G", bssid: [6]byte{0xAE, 0x20, 0x2E, 0xF8, 0x39, 0x30}, phy: 8, rssi: -36, quality: 100,
  68. beacon: 100, tsf: 1041878 * 1000000, caps: 0x0011, freqKHz: 5745000,
  69. rates: []uint16{0x800C, 18, 0x8018, 36, 48, 72, 96, 108},
  70. ies: concat(ie(ieSSID, []byte("Home-5G")...), ie(ieCountry, 'T', 'W', 4), htCaps(3), ie(ieHTOperation, 149, 0x05),
  71. vhtCaps(3, 2), ie(ieVHTOperation, 1, 155, 0), rsnIE([]byte{4}, []byte{2}, 0)),
  72. },
  73. {
  74. ssid: "", bssid: [6]byte{1, 2, 3, 4, 5, 6}, phy: 7, rssi: -80, quality: 40,
  75. caps: 0x0001, freqKHz: 2437000, rates: []uint16{0x8002, 0x8004},
  76. },
  77. })
  78. entries, err := parseWlanBSSList(list)
  79. if err != nil {
  80. t.Fatalf("parseWlanBSSList: %v", err)
  81. }
  82. if len(entries) != 2 {
  83. t.Fatalf("got %d entries, want 2", len(entries))
  84. }
  85. info := bssInfoFromEntry(entries[0])
  86. checks := []struct {
  87. name string
  88. got interface{}
  89. want interface{}
  90. }{
  91. {"ESSID", info.ESSID, "Home-5G"},
  92. {"Address", info.Address, "ae:20:2e:f8:39:30"},
  93. {"Channel", info.Channel, 149},
  94. {"Band", info.Band, "5 GHz"},
  95. {"Frequency", info.Frequency, "5.745 GHz"},
  96. {"SignalDBm", info.SignalDBm, -36},
  97. {"SignalLevel", info.SignalLevel, "-36 dBm"},
  98. {"Quality", info.Quality, "100/100"},
  99. {"ChannelWidth", info.ChannelWidth, 80},
  100. {"CenterChannel", info.CenterChannel, 155},
  101. {"PHYType", info.PHYType, "802.11ac"},
  102. {"Generation", info.Generation, "Wi-Fi 5"},
  103. {"Security", info.Security, "WPA2-Personal"},
  104. {"Cipher", info.Cipher, "AES-CCMP"},
  105. {"EncryptionKey", info.EncryptionKey, true},
  106. {"SpatialStreams", info.SpatialStreams, 3},
  107. {"MaxRateMbps", info.MaxRateMbps, 1300.0},
  108. {"Country", info.Country, "TW"},
  109. {"BeaconInterval", info.BeaconInterval, 100},
  110. {"UptimeSeconds", info.UptimeSeconds, int64(1041878)},
  111. {"BSSType", info.BSSType, "Infrastructure"},
  112. {"BasicRates", len(info.BasicRates), 2},
  113. {"SupportedRates", len(info.SupportedRates), 8},
  114. }
  115. for _, c := range checks {
  116. if c.got != c.want {
  117. t.Errorf("%s = %v, want %v", c.name, c.got, c.want)
  118. }
  119. }
  120. hidden := bssInfoFromEntry(entries[1])
  121. if !hidden.Hidden || hidden.ESSID != "" {
  122. t.Errorf("hidden entry: Hidden=%v ESSID=%q", hidden.Hidden, hidden.ESSID)
  123. }
  124. if hidden.PHYType != "802.11n" || hidden.Security != "Open" || hidden.Channel != 6 {
  125. t.Errorf("hidden entry: PHY=%q Security=%q Channel=%d", hidden.PHYType, hidden.Security, hidden.Channel)
  126. }
  127. }
  128. // TestParseWlanBSSListHostile checks that wrong counts and IE offsets never
  129. // read out of bounds
  130. func TestParseWlanBSSListHostile(t *testing.T) {
  131. list := buildBSSList([]testBSS{{ssid: "a", freqKHz: 2412000, ies: ie(ieSSID, 'a')}})
  132. le := binary.LittleEndian
  133. cases := []struct {
  134. name string
  135. mutate func(b []byte)
  136. ok bool
  137. }{
  138. {"valid", func(b []byte) {}, true},
  139. {"count too large", func(b []byte) { le.PutUint32(b[4:8], 1000) }, false},
  140. {"total too large", func(b []byte) { le.PutUint32(b[0:4], 0xFFFFFFFF) }, true},
  141. {"ie offset past end", func(b []byte) { le.PutUint32(b[8+352:], 0xFFFFFFF0) }, true},
  142. {"ie size past end", func(b []byte) { le.PutUint32(b[8+356:], 0xFFFFFFF0) }, true},
  143. {"ie offset inside entry", func(b []byte) { le.PutUint32(b[8+352:], 4) }, true},
  144. {"rate count huge", func(b []byte) { le.PutUint32(b[8+96:], 0xFFFFFFFF) }, true},
  145. {"ssid length huge", func(b []byte) { le.PutUint32(b[8:], 0xFFFFFFFF) }, true},
  146. }
  147. for _, tc := range cases {
  148. b := append([]byte{}, list...)
  149. tc.mutate(b)
  150. entries, err := parseWlanBSSList(b)
  151. if (err == nil) != tc.ok {
  152. t.Errorf("%s: err = %v, want ok=%v", tc.name, err, tc.ok)
  153. }
  154. for _, e := range entries {
  155. bssInfoFromEntry(e)
  156. }
  157. }
  158. for i := 0; i <= len(list); i++ {
  159. entries, _ := parseWlanBSSList(list[:i])
  160. for _, e := range entries {
  161. bssInfoFromEntry(e)
  162. }
  163. }
  164. }
  165. func TestParseWlanInterfaceList(t *testing.T) {
  166. b := make([]byte, 8+2*wlanInterfaceInfoSize)
  167. binary.LittleEndian.PutUint32(b[0:4], 2)
  168. guid := []byte{0x87, 0xAB, 0xF7, 0x0B, 0xB4, 0x4A, 0xFB, 0x42, 0xA6, 0x2E, 0xD7, 0xE7, 0x20, 0xF1, 0x5A, 0x7C}
  169. copy(b[8:24], guid)
  170. putUTF16(b[24:536], "Realtek 8811CU Wireless LAN 802.11ac USB NIC")
  171. binary.LittleEndian.PutUint32(b[8+528:], 4)
  172. binary.LittleEndian.PutUint32(b[8+wlanInterfaceInfoSize+528:], 1)
  173. list, err := parseWlanInterfaceList(b)
  174. if err != nil || len(list) != 2 {
  175. t.Fatalf("parseWlanInterfaceList = %v, %v", list, err)
  176. }
  177. if list[0].Description != "Realtek 8811CU Wireless LAN 802.11ac USB NIC" {
  178. t.Errorf("Description = %q", list[0].Description)
  179. }
  180. if g := guidString(list[0].GUID); g != "{0BF7AB87-4AB4-42FB-A62E-D7E720F15A7C}" {
  181. t.Errorf("GUID = %s", g)
  182. }
  183. if wlanInterfaceStateName(list[0].State) != "disconnected" || wlanInterfaceStateName(list[1].State) != "connected" {
  184. t.Errorf("States = %d, %d", list[0].State, list[1].State)
  185. }
  186. if _, err := parseWlanInterfaceList(b[:100]); err == nil {
  187. t.Error("truncated interface list accepted")
  188. }
  189. if _, err := parseWlanInterfaceList(nil); err == nil {
  190. t.Error("empty interface list accepted")
  191. }
  192. }
  193. func TestParseWlanConnectionAttributes(t *testing.T) {
  194. le := binary.LittleEndian
  195. b := make([]byte, wlanConnectionAttrSize)
  196. le.PutUint32(b[0:4], 1)
  197. le.PutUint32(b[4:8], 0)
  198. putUTF16(b[8:520], "Home-5G")
  199. putSSID(b[520:556], "Home-5G")
  200. le.PutUint32(b[556:560], 1)
  201. copy(b[560:566], []byte{0xAE, 0x20, 0x2E, 0xF8, 0x39, 0x30})
  202. le.PutUint32(b[568:572], 8)
  203. le.PutUint32(b[576:580], 92)
  204. le.PutUint32(b[580:584], 866700)
  205. le.PutUint32(b[584:588], 650000)
  206. le.PutUint32(b[588:592], 1)
  207. le.PutUint32(b[596:600], 7)
  208. le.PutUint32(b[600:604], 4)
  209. c, err := parseWlanConnectionAttributes(b)
  210. if err != nil {
  211. t.Fatalf("parseWlanConnectionAttributes: %v", err)
  212. }
  213. if c.ProfileName != "Home-5G" || string(c.SSID) != "Home-5G" {
  214. t.Errorf("profile / SSID = %q / %q", c.ProfileName, c.SSID)
  215. }
  216. if formatMAC(c.BSSID[:]) != "ae:20:2e:f8:39:30" {
  217. t.Errorf("BSSID = %s", formatMAC(c.BSSID[:]))
  218. }
  219. if dot11PhyName(c.PhyType) != "802.11ac" || c.SignalQuality != 92 || c.RxRateKbps != 866700 || c.TxRateKbps != 650000 {
  220. t.Errorf("association = %+v", c)
  221. }
  222. if !c.SecurityEnabled || dot11AuthName(c.AuthAlgorithm) != "WPA2-Personal" || dot11CipherName(c.CipherAlgorithm) != "AES-CCMP" {
  223. t.Errorf("security = %+v", c)
  224. }
  225. if wlanConnectionModeName(c.Mode) != "Profile" {
  226. t.Errorf("mode = %d", c.Mode)
  227. }
  228. if _, err := parseWlanConnectionAttributes(b[:600]); err == nil {
  229. t.Error("truncated connection attributes accepted")
  230. }
  231. }
  232. func TestParseWlanRadioState(t *testing.T) {
  233. le := binary.LittleEndian
  234. b := make([]byte, 4+64*12)
  235. le.PutUint32(b[0:4], 2)
  236. le.PutUint32(b[4:8], 0)
  237. le.PutUint32(b[8:12], 2)
  238. le.PutUint32(b[12:16], 1)
  239. le.PutUint32(b[16:20], 1)
  240. le.PutUint32(b[20:24], 1)
  241. le.PutUint32(b[24:28], 1)
  242. states, err := parseWlanRadioState(b)
  243. if err != nil || len(states) != 2 {
  244. t.Fatalf("parseWlanRadioState = %v, %v", states, err)
  245. }
  246. if dot11RadioStateName(states[0].Software) != "off" || dot11RadioStateName(states[1].Software) != "on" {
  247. t.Errorf("states = %+v", states)
  248. }
  249. le.PutUint32(b[0:4], 1000)
  250. if states, err := parseWlanRadioState(b); err != nil || len(states) != 64 {
  251. t.Errorf("oversized count: %d states, %v", len(states), err)
  252. }
  253. if _, err := parseWlanRadioState(b[:2]); err == nil {
  254. t.Error("truncated radio state accepted")
  255. }
  256. }
  257. func TestParseWlanAvailableNetworkList(t *testing.T) {
  258. le := binary.LittleEndian
  259. b := make([]byte, 8+wlanAvailableNetworkSize)
  260. le.PutUint32(b[0:4], 1)
  261. e := b[8:]
  262. putUTF16(e[0:512], "Home-5G")
  263. putSSID(e[512:548], "Home-5G")
  264. le.PutUint32(e[548:552], 1)
  265. le.PutUint32(e[604:608], 88)
  266. le.PutUint32(e[608:612], 1)
  267. le.PutUint32(e[612:616], 9)
  268. le.PutUint32(e[616:620], 4)
  269. le.PutUint32(e[620:624], wlanAvailableNetworkConnected|wlanAvailableNetworkHasProfile)
  270. list, err := parseWlanAvailableNetworkList(b)
  271. if err != nil || len(list) != 1 {
  272. t.Fatalf("parseWlanAvailableNetworkList = %v, %v", list, err)
  273. }
  274. n := list[0]
  275. if n.ProfileName != "Home-5G" || string(n.SSID) != "Home-5G" || n.SignalQuality != 88 || !n.SecurityEnabled {
  276. t.Errorf("network = %+v", n)
  277. }
  278. if dot11AuthName(n.AuthAlgorithm) != "WPA3-Personal" || n.Flags&wlanAvailableNetworkHasProfile == 0 {
  279. t.Errorf("network = %+v", n)
  280. }
  281. if _, err := parseWlanAvailableNetworkList(b[:500]); err == nil {
  282. t.Error("truncated network list accepted")
  283. }
  284. }
  285. func TestEnumNames(t *testing.T) {
  286. cases := []struct {
  287. got string
  288. want string
  289. }{
  290. {dot11PhyName(10), "802.11ax"},
  291. {dot11PhyName(99), ""},
  292. {dot11BSSTypeName(2), "Ad hoc"},
  293. {dot11AuthName(0x80000001), "Vendor specific"},
  294. {dot11AuthName(10), "Enhanced Open (OWE)"},
  295. {dot11CipherName(0x101), "WEP"},
  296. {dot11CipherName(0), "None"},
  297. {wlanInterfaceStateName(99), "unknown"},
  298. {wlanConnectionModeName(4), "Automatic"},
  299. {dot11RadioStateName(0), "unknown"},
  300. }
  301. for i, c := range cases {
  302. if c.got != c.want {
  303. t.Errorf("case %d: got %q, want %q", i, c.got, c.want)
  304. }
  305. }
  306. }