test_scan.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. # Scanning tests
  2. # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import time
  7. import logging
  8. logger = logging.getLogger()
  9. import os
  10. import subprocess
  11. import hostapd
  12. from wpasupplicant import WpaSupplicant
  13. def check_scan(dev, params, other_started=False, test_busy=False):
  14. if not other_started:
  15. dev.dump_monitor()
  16. id = dev.request("SCAN " + params)
  17. if "FAIL" in id:
  18. raise Exception("Failed to start scan")
  19. id = int(id)
  20. if test_busy:
  21. if "FAIL-BUSY" not in dev.request("SCAN"):
  22. raise Exception("SCAN command while already scanning not rejected")
  23. if other_started:
  24. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  25. if ev is None:
  26. raise Exception("Other scan did not start")
  27. if "id=" + str(id) in ev:
  28. raise Exception("Own scan id unexpectedly included in start event")
  29. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  30. if ev is None:
  31. raise Exception("Other scan did not complete")
  32. if "id=" + str(id) in ev:
  33. raise Exception("Own scan id unexpectedly included in completed event")
  34. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  35. if ev is None:
  36. raise Exception("Scan did not start")
  37. if "id=" + str(id) not in ev:
  38. raise Exception("Scan id not included in start event")
  39. if test_busy:
  40. if "FAIL-BUSY" not in dev.request("SCAN"):
  41. raise Exception("SCAN command while already scanning not rejected")
  42. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  43. if ev is None:
  44. raise Exception("Scan did not complete")
  45. if "id=" + str(id) not in ev:
  46. raise Exception("Scan id not included in completed event")
  47. def check_scan_retry(dev, params, bssid):
  48. for i in range(0, 5):
  49. check_scan(dev, "freq=2412-2462,5180 use_id=1")
  50. if int(dev.get_bss(bssid)['age']) <= 1:
  51. return
  52. raise Exception("Unexpectedly old BSS entry")
  53. def test_scan(dev, apdev):
  54. """Control interface behavior on scan parameters"""
  55. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  56. bssid = apdev[0]['bssid']
  57. logger.info("Full scan")
  58. check_scan(dev[0], "use_id=1", test_busy=True)
  59. logger.info("Limited channel scan")
  60. check_scan_retry(dev[0], "freq=2412-2462,5180 use_id=1", bssid)
  61. # wait long enough to allow next scans to be verified not to find the AP
  62. time.sleep(2)
  63. logger.info("Passive single-channel scan")
  64. check_scan(dev[0], "freq=2457 passive=1 use_id=1")
  65. logger.info("Active single-channel scan")
  66. check_scan(dev[0], "freq=2452 passive=0 use_id=1")
  67. if int(dev[0].get_bss(bssid)['age']) < 2:
  68. raise Exception("Unexpectedly updated BSS entry")
  69. logger.info("Active single-channel scan on AP's operating channel")
  70. check_scan_retry(dev[0], "freq=2412 passive=0 use_id=1", bssid)
  71. def test_scan_only(dev, apdev):
  72. """Control interface behavior on scan parameters with type=only"""
  73. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  74. bssid = apdev[0]['bssid']
  75. logger.info("Full scan")
  76. check_scan(dev[0], "type=only use_id=1")
  77. logger.info("Limited channel scan")
  78. check_scan_retry(dev[0], "type=only freq=2412-2462,5180 use_id=1", bssid)
  79. # wait long enough to allow next scans to be verified not to find the AP
  80. time.sleep(2)
  81. logger.info("Passive single-channel scan")
  82. check_scan(dev[0], "type=only freq=2457 passive=1 use_id=1")
  83. logger.info("Active single-channel scan")
  84. check_scan(dev[0], "type=only freq=2452 passive=0 use_id=1")
  85. if int(dev[0].get_bss(bssid)['age']) < 2:
  86. raise Exception("Unexpectedly updated BSS entry")
  87. logger.info("Active single-channel scan on AP's operating channel")
  88. check_scan_retry(dev[0], "type=only freq=2412 passive=0 use_id=1", bssid)
  89. def test_scan_external_trigger(dev, apdev):
  90. """Avoid operations during externally triggered scan"""
  91. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  92. bssid = apdev[0]['bssid']
  93. subprocess.call(['sudo', 'iw', dev[0].ifname, 'scan', 'trigger'])
  94. check_scan(dev[0], "use_id=1", other_started=True)
  95. def test_scan_bss_expiration_count(dev, apdev):
  96. """BSS entry expiration based on scan results without match"""
  97. if "FAIL" not in dev[0].request("BSS_EXPIRE_COUNT 0"):
  98. raise Exception("Invalid BSS_EXPIRE_COUNT accepted")
  99. if "OK" not in dev[0].request("BSS_EXPIRE_COUNT 2"):
  100. raise Exception("BSS_EXPIRE_COUNT failed")
  101. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  102. bssid = apdev[0]['bssid']
  103. dev[0].scan(freq="2412", only_new=True)
  104. if bssid not in dev[0].request("SCAN_RESULTS"):
  105. raise Exception("BSS not found in initial scan")
  106. hapd.request("DISABLE")
  107. dev[0].scan(freq="2412", only_new=True)
  108. if bssid not in dev[0].request("SCAN_RESULTS"):
  109. raise Exception("BSS not found in first scan without match")
  110. dev[0].scan(freq="2412", only_new=True)
  111. if bssid in dev[0].request("SCAN_RESULTS"):
  112. raise Exception("BSS found after two scans without match")
  113. def test_scan_bss_expiration_age(dev, apdev):
  114. """BSS entry expiration based on age"""
  115. try:
  116. if "FAIL" not in dev[0].request("BSS_EXPIRE_AGE COUNT 9"):
  117. raise Exception("Invalid BSS_EXPIRE_AGE accepted")
  118. if "OK" not in dev[0].request("BSS_EXPIRE_AGE 10"):
  119. raise Exception("BSS_EXPIRE_AGE failed")
  120. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  121. bssid = apdev[0]['bssid']
  122. dev[0].scan(freq="2412")
  123. if bssid not in dev[0].request("SCAN_RESULTS"):
  124. raise Exception("BSS not found in initial scan")
  125. hapd.request("DISABLE")
  126. logger.info("Waiting for BSS entry to expire")
  127. time.sleep(7)
  128. if bssid not in dev[0].request("SCAN_RESULTS"):
  129. raise Exception("BSS expired too quickly")
  130. ev = dev[0].wait_event(["CTRL-EVENT-BSS-REMOVED"], timeout=15)
  131. if ev is None:
  132. raise Exception("BSS entry expiration timed out")
  133. if bssid in dev[0].request("SCAN_RESULTS"):
  134. raise Exception("BSS not removed after expiration time")
  135. finally:
  136. dev[0].request("BSS_EXPIRE_AGE 180")
  137. def test_scan_filter(dev, apdev):
  138. """Filter scan results based on SSID"""
  139. try:
  140. if "OK" not in dev[0].request("SET filter_ssids 1"):
  141. raise Exception("SET failed")
  142. dev[0].connect("test-scan", key_mgmt="NONE", only_add_network=True)
  143. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  144. bssid = apdev[0]['bssid']
  145. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "test-scan2" })
  146. bssid2 = apdev[1]['bssid']
  147. dev[0].scan(freq="2412", only_new=True)
  148. if bssid not in dev[0].request("SCAN_RESULTS"):
  149. raise Exception("BSS not found in scan results")
  150. if bssid2 in dev[0].request("SCAN_RESULTS"):
  151. raise Exception("Unexpected BSS found in scan results")
  152. finally:
  153. dev[0].request("SET filter_ssids 0")
  154. def test_scan_int(dev, apdev):
  155. """scan interval configuration"""
  156. try:
  157. if "FAIL" not in dev[0].request("SCAN_INTERVAL -1"):
  158. raise Exception("Accepted invalid scan interval")
  159. if "OK" not in dev[0].request("SCAN_INTERVAL 1"):
  160. raise Exception("Failed to set scan interval")
  161. dev[0].connect("not-used", key_mgmt="NONE", scan_freq="2412",
  162. wait_connect=False)
  163. times = {}
  164. for i in range(0, 3):
  165. logger.info("Waiting for scan to start")
  166. start = os.times()[4]
  167. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  168. if ev is None:
  169. raise Exception("did not start a scan")
  170. stop = os.times()[4]
  171. times[i] = stop - start
  172. logger.info("Waiting for scan to complete")
  173. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
  174. if ev is None:
  175. raise Exception("did not complete a scan")
  176. print times
  177. if times[0] > 1 or times[1] < 0.5 or times[1] > 1.5 or times[2] < 0.5 or times[2] > 1.5:
  178. raise Exception("Unexpected scan timing: " + str(times))
  179. finally:
  180. dev[0].request("SCAN_INTERVAL 5")
  181. def test_scan_bss_operations(dev, apdev):
  182. """Control interface behavior on BSS parameters"""
  183. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  184. bssid = apdev[0]['bssid']
  185. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "test2-scan" })
  186. bssid2 = apdev[1]['bssid']
  187. dev[0].scan(freq="2412")
  188. dev[0].scan(freq="2412")
  189. dev[0].scan(freq="2412")
  190. id1 = dev[0].request("BSS FIRST MASK=0x1").splitlines()[0].split('=')[1]
  191. id2 = dev[0].request("BSS LAST MASK=0x1").splitlines()[0].split('=')[1]
  192. res = dev[0].request("BSS RANGE=ALL MASK=0x20001")
  193. if "id=" + id1 not in res:
  194. raise Exception("Missing BSS " + id1)
  195. if "id=" + id2 not in res:
  196. raise Exception("Missing BSS " + id2)
  197. if "====" not in res:
  198. raise Exception("Missing delim")
  199. if "####" not in res:
  200. raise Exception("Missing end")
  201. res = dev[0].request("BSS RANGE=ALL MASK=0")
  202. if "id=" + id1 not in res:
  203. raise Exception("Missing BSS " + id1)
  204. if "id=" + id2 not in res:
  205. raise Exception("Missing BSS " + id2)
  206. if "====" in res:
  207. raise Exception("Unexpected delim")
  208. if "####" in res:
  209. raise Exception("Unexpected end delim")
  210. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  211. if len(res) != 2:
  212. raise Exception("Unexpected result")
  213. res = dev[0].request("BSS FIRST MASK=0x1")
  214. if "id=" + id1 not in res:
  215. raise Exception("Unexpected result: " + res)
  216. res = dev[0].request("BSS LAST MASK=0x1")
  217. if "id=" + id2 not in res:
  218. raise Exception("Unexpected result: " + res)
  219. res = dev[0].request("BSS ID-" + id1 + " MASK=0x1")
  220. if "id=" + id1 not in res:
  221. raise Exception("Unexpected result: " + res)
  222. res = dev[0].request("BSS NEXT-" + id1 + " MASK=0x1")
  223. if "id=" + id2 not in res:
  224. raise Exception("Unexpected result: " + res)
  225. res = dev[0].request("BSS NEXT-" + id2 + " MASK=0x1")
  226. if "id=" in res:
  227. raise Exception("Unexpected result: " + res)
  228. if len(dev[0].request("BSS RANGE=" + id2 + " MASK=0x1").splitlines()) != 0:
  229. raise Exception("Unexpected RANGE=1 result")
  230. if len(dev[0].request("BSS RANGE=" + id1 + "- MASK=0x1").splitlines()) != 2:
  231. raise Exception("Unexpected RANGE=0- result")
  232. if len(dev[0].request("BSS RANGE=-" + id2 + " MASK=0x1").splitlines()) != 2:
  233. raise Exception("Unexpected RANGE=-1 result")
  234. if len(dev[0].request("BSS RANGE=" + id1 + "-" + id2 + " MASK=0x1").splitlines()) != 2:
  235. raise Exception("Unexpected RANGE=0-1 result")
  236. if len(dev[0].request("BSS RANGE=" + id2 + "-" + id2 + " MASK=0x1").splitlines()) != 1:
  237. raise Exception("Unexpected RANGE=1-1 result")
  238. if len(dev[0].request("BSS RANGE=" + str(int(id2) + 1) + "-" + str(int(id2) + 10) + " MASK=0x1").splitlines()) != 0:
  239. raise Exception("Unexpected RANGE=2-10 result")
  240. if len(dev[0].request("BSS RANGE=0-" + str(int(id2) + 10) + " MASK=0x1").splitlines()) != 2:
  241. raise Exception("Unexpected RANGE=0-10 result")
  242. if len(dev[0].request("BSS RANGE=" + id1 + "-" + id1 + " MASK=0x1").splitlines()) != 1:
  243. raise Exception("Unexpected RANGE=0-0 result")
  244. res = dev[0].request("BSS p2p_dev_addr=FOO")
  245. if "FAIL" in res or "id=" in res:
  246. raise Exception("Unexpected result: " + res)
  247. res = dev[0].request("BSS p2p_dev_addr=00:11:22:33:44:55")
  248. if "FAIL" in res or "id=" in res:
  249. raise Exception("Unexpected result: " + res)
  250. dev[0].request("BSS_FLUSH 1000")
  251. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  252. if len(res) != 2:
  253. raise Exception("Unexpected result after BSS_FLUSH 1000")
  254. dev[0].request("BSS_FLUSH 0")
  255. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  256. if len(res) != 0:
  257. raise Exception("Unexpected result after BSS_FLUSH 0")
  258. def test_scan_and_interface_disabled(dev, apdev):
  259. """Scan operation when interface gets disabled"""
  260. try:
  261. dev[0].request("SCAN")
  262. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  263. if ev is None:
  264. raise Exception("Scan did not start")
  265. dev[0].request("DRIVER_EVENT INTERFACE_DISABLED")
  266. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=7)
  267. if ev is not None:
  268. raise Exception("Scan completed unexpectedly")
  269. # verify that scan is rejected
  270. if "FAIL" not in dev[0].request("SCAN"):
  271. raise Exception("New scan request was accepted unexpectedly")
  272. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  273. dev[0].scan(freq="2412")
  274. finally:
  275. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  276. def test_scan_for_auth(dev, apdev):
  277. """cfg80211 workaround with scan-for-auth"""
  278. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  279. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  280. # Block sme-connect radio work with an external radio work item, so that
  281. # SELECT_NETWORK can decide to use fast associate without a new scan while
  282. # cfg80211 still has the matching BSS entry, but the actual connection is
  283. # not yet started.
  284. id = dev[0].request("RADIO_WORK add block-work")
  285. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  286. if ev is None:
  287. raise Exception("Timeout while waiting radio work to start")
  288. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  289. wait_connect=False)
  290. dev[0].dump_monitor()
  291. # Clear cfg80211 BSS table.
  292. subprocess.call(['iw', dev[0].ifname, 'scan', 'trigger',
  293. 'freq', '2457', 'flush'])
  294. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  295. if ev is None:
  296. raise Exception("External flush scan timed out")
  297. # Release blocking radio work to allow connection to go through with the
  298. # cfg80211 BSS entry missing.
  299. dev[0].request("RADIO_WORK done " + id)
  300. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  301. if ev is None:
  302. raise Exception("Association with the AP timed out")
  303. def test_scan_hidden(dev, apdev):
  304. """Control interface behavior on scan parameters"""
  305. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan",
  306. "ignore_broadcast_ssid": "1" })
  307. bssid = apdev[0]['bssid']
  308. check_scan(dev[0], "freq=2412 use_id=1")
  309. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  310. raise Exception("BSS unexpectedly found in initial scan")
  311. id1 = dev[0].connect("foo", key_mgmt="NONE", scan_ssid="1",
  312. only_add_network=True)
  313. id2 = dev[0].connect("test-scan", key_mgmt="NONE", scan_ssid="1",
  314. only_add_network=True)
  315. id3 = dev[0].connect("bar", key_mgmt="NONE", only_add_network=True)
  316. check_scan(dev[0], "freq=2412 use_id=1")
  317. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  318. raise Exception("BSS unexpectedly found in scan")
  319. check_scan(dev[0], "scan_id=%d,%d,%d freq=2412 use_id=1" % (id1, id2, id3))
  320. if "test-scan" not in dev[0].request("SCAN_RESULTS"):
  321. raise Exception("BSS not found in scan")
  322. if "FAIL" not in dev[0].request("SCAN scan_id=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17"):
  323. raise Exception("Too many scan_id values accepted")
  324. def test_scan_and_bss_entry_removed(dev, apdev):
  325. """Last scan result and connect work processing on BSS entry update"""
  326. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open",
  327. "eap_server": "1",
  328. "wps_state": "2" })
  329. bssid = apdev[0]['bssid']
  330. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  331. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  332. # Add a BSS entry
  333. dev[0].scan_for_bss(bssid, freq="2412")
  334. wpas.scan_for_bss(bssid, freq="2412")
  335. # Start a connect radio work with a blocking entry preventing this from
  336. # proceeding; this stores a pointer to the selected BSS entry.
  337. id = dev[0].request("RADIO_WORK add block-work")
  338. w_id = wpas.request("RADIO_WORK add block-work")
  339. dev[0].wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  340. wpas.wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  341. nid = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  342. wait_connect=False)
  343. w_nid = wpas.connect("open", key_mgmt="NONE", scan_freq="2412",
  344. wait_connect=False)
  345. time.sleep(0.1)
  346. # Remove the BSS entry
  347. dev[0].request("BSS_FLUSH 0")
  348. wpas.request("BSS_FLUSH 0")
  349. # Allow the connect radio work to continue. The bss entry stored in the
  350. # pending connect work is now stale. This will result in the connection
  351. # attempt failing since the BSS entry does not exist.
  352. dev[0].request("RADIO_WORK done " + id)
  353. wpas.request("RADIO_WORK done " + w_id)
  354. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  355. if ev is not None:
  356. raise Exception("Unexpected connection")
  357. dev[0].remove_network(nid)
  358. ev = wpas.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  359. if ev is not None:
  360. raise Exception("Unexpected connection")
  361. wpas.remove_network(w_nid)
  362. time.sleep(0.5)
  363. dev[0].request("BSS_FLUSH 0")
  364. wpas.request("BSS_FLUSH 0")
  365. # Add a BSS entry
  366. dev[0].scan_for_bss(bssid, freq="2412")
  367. wpas.scan_for_bss(bssid, freq="2412")
  368. # Start a connect radio work with a blocking entry preventing this from
  369. # proceeding; this stores a pointer to the selected BSS entry.
  370. id = dev[0].request("RADIO_WORK add block-work")
  371. w_id = wpas.request("RADIO_WORK add block-work")
  372. dev[0].wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  373. wpas.wait_event(["EXT-RADIO-WORK-START"], timeout=1)
  374. # Schedule a connection based on the current BSS entry.
  375. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  376. wait_connect=False)
  377. wpas.connect("open", key_mgmt="NONE", scan_freq="2412",
  378. wait_connect=False)
  379. # Update scan results with results that have longer set of IEs so that new
  380. # memory needs to be allocated for the BSS entry.
  381. hapd.request("WPS_PBC")
  382. time.sleep(0.1)
  383. subprocess.call(['iw', dev[0].ifname, 'scan', 'trigger', 'freq', '2412'])
  384. subprocess.call(['iw', wpas.ifname, 'scan', 'trigger', 'freq', '2412'])
  385. time.sleep(0.1)
  386. # Allow the connect radio work to continue. The bss entry stored in the
  387. # pending connect work becomes stale during the scan and it must have been
  388. # updated for the connection to work.
  389. dev[0].request("RADIO_WORK done " + id)
  390. wpas.request("RADIO_WORK done " + w_id)
  391. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  392. if ev is None:
  393. raise Exception("No connection (sme-connect)")
  394. ev = wpas.wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  395. if ev is None:
  396. raise Exception("No connection (connect)")