test_scan.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. def check_scan(dev, params, other_started=False):
  13. if not other_started:
  14. dev.dump_monitor()
  15. id = dev.request("SCAN " + params)
  16. if "FAIL" in id:
  17. raise Exception("Failed to start scan")
  18. id = int(id)
  19. if other_started:
  20. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  21. if ev is None:
  22. raise Exception("Other scan did not start")
  23. if "id=" + str(id) in ev:
  24. raise Exception("Own scan id unexpectedly included in start event")
  25. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  26. if ev is None:
  27. raise Exception("Other scan did not complete")
  28. if "id=" + str(id) in ev:
  29. raise Exception("Own scan id unexpectedly included in completed event")
  30. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  31. if ev is None:
  32. raise Exception("Scan did not start")
  33. if "id=" + str(id) not in ev:
  34. raise Exception("Scan id not included in start event")
  35. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  36. if ev is None:
  37. raise Exception("Scan did not complete")
  38. if "id=" + str(id) not in ev:
  39. raise Exception("Scan id not included in completed event")
  40. def check_scan_retry(dev, params, bssid):
  41. for i in range(0, 5):
  42. check_scan(dev, "freq=2412-2462,5180 use_id=1")
  43. if int(dev.get_bss(bssid)['age']) <= 1:
  44. return
  45. raise Exception("Unexpectedly old BSS entry")
  46. def test_scan(dev, apdev):
  47. """Control interface behavior on scan parameters"""
  48. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  49. bssid = apdev[0]['bssid']
  50. logger.info("Full scan")
  51. check_scan(dev[0], "use_id=1")
  52. logger.info("Limited channel scan")
  53. check_scan_retry(dev[0], "freq=2412-2462,5180 use_id=1", bssid)
  54. # wait long enough to allow next scans to be verified not to find the AP
  55. time.sleep(2)
  56. logger.info("Passive single-channel scan")
  57. check_scan(dev[0], "freq=2457 passive=1 use_id=1")
  58. logger.info("Active single-channel scan")
  59. check_scan(dev[0], "freq=2452 passive=0 use_id=1")
  60. if int(dev[0].get_bss(bssid)['age']) < 2:
  61. raise Exception("Unexpectedly updated BSS entry")
  62. logger.info("Active single-channel scan on AP's operating channel")
  63. check_scan_retry(dev[0], "freq=2412 passive=0 use_id=1", bssid)
  64. def test_scan_only(dev, apdev):
  65. """Control interface behavior on scan parameters with type=only"""
  66. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  67. bssid = apdev[0]['bssid']
  68. logger.info("Full scan")
  69. check_scan(dev[0], "type=only use_id=1")
  70. logger.info("Limited channel scan")
  71. check_scan_retry(dev[0], "type=only freq=2412-2462,5180 use_id=1", bssid)
  72. # wait long enough to allow next scans to be verified not to find the AP
  73. time.sleep(2)
  74. logger.info("Passive single-channel scan")
  75. check_scan(dev[0], "type=only freq=2457 passive=1 use_id=1")
  76. logger.info("Active single-channel scan")
  77. check_scan(dev[0], "type=only freq=2452 passive=0 use_id=1")
  78. if int(dev[0].get_bss(bssid)['age']) < 2:
  79. raise Exception("Unexpectedly updated BSS entry")
  80. logger.info("Active single-channel scan on AP's operating channel")
  81. check_scan_retry(dev[0], "type=only freq=2412 passive=0 use_id=1", bssid)
  82. def test_scan_external_trigger(dev, apdev):
  83. """Avoid operations during externally triggered scan"""
  84. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  85. bssid = apdev[0]['bssid']
  86. subprocess.call(['sudo', 'iw', dev[0].ifname, 'scan', 'trigger'])
  87. check_scan(dev[0], "use_id=1", other_started=True)
  88. def test_scan_bss_expiration_count(dev, apdev):
  89. """BSS entry expiration based on scan results without match"""
  90. if "FAIL" not in dev[0].request("BSS_EXPIRE_COUNT 0"):
  91. raise Exception("Invalid BSS_EXPIRE_COUNT accepted")
  92. if "OK" not in dev[0].request("BSS_EXPIRE_COUNT 2"):
  93. raise Exception("BSS_EXPIRE_COUNT failed")
  94. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  95. bssid = apdev[0]['bssid']
  96. dev[0].scan(freq="2412", only_new=True)
  97. if bssid not in dev[0].request("SCAN_RESULTS"):
  98. raise Exception("BSS not found in initial scan")
  99. hapd.request("DISABLE")
  100. dev[0].scan(freq="2412", only_new=True)
  101. if bssid not in dev[0].request("SCAN_RESULTS"):
  102. raise Exception("BSS not found in first scan without match")
  103. dev[0].scan(freq="2412", only_new=True)
  104. if bssid in dev[0].request("SCAN_RESULTS"):
  105. raise Exception("BSS found after two scans without match")
  106. def test_scan_bss_expiration_age(dev, apdev):
  107. """BSS entry expiration based on age"""
  108. try:
  109. if "FAIL" not in dev[0].request("BSS_EXPIRE_AGE COUNT 9"):
  110. raise Exception("Invalid BSS_EXPIRE_AGE accepted")
  111. if "OK" not in dev[0].request("BSS_EXPIRE_AGE 10"):
  112. raise Exception("BSS_EXPIRE_AGE failed")
  113. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  114. bssid = apdev[0]['bssid']
  115. dev[0].scan(freq="2412")
  116. if bssid not in dev[0].request("SCAN_RESULTS"):
  117. raise Exception("BSS not found in initial scan")
  118. hapd.request("DISABLE")
  119. logger.info("Waiting for BSS entry to expire")
  120. time.sleep(7)
  121. if bssid not in dev[0].request("SCAN_RESULTS"):
  122. raise Exception("BSS expired too quickly")
  123. ev = dev[0].wait_event(["CTRL-EVENT-BSS-REMOVED"], timeout=15)
  124. if ev is None:
  125. raise Exception("BSS entry expiration timed out")
  126. if bssid in dev[0].request("SCAN_RESULTS"):
  127. raise Exception("BSS not removed after expiration time")
  128. finally:
  129. dev[0].request("BSS_EXPIRE_AGE 180")
  130. def test_scan_filter(dev, apdev):
  131. """Filter scan results based on SSID"""
  132. try:
  133. if "OK" not in dev[0].request("SET filter_ssids 1"):
  134. raise Exception("SET failed")
  135. dev[0].connect("test-scan", key_mgmt="NONE", only_add_network=True)
  136. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  137. bssid = apdev[0]['bssid']
  138. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "test-scan2" })
  139. bssid2 = apdev[1]['bssid']
  140. dev[0].scan(freq="2412", only_new=True)
  141. if bssid not in dev[0].request("SCAN_RESULTS"):
  142. raise Exception("BSS not found in scan results")
  143. if bssid2 in dev[0].request("SCAN_RESULTS"):
  144. raise Exception("Unexpected BSS found in scan results")
  145. finally:
  146. dev[0].request("SET filter_ssids 0")
  147. def test_scan_int(dev, apdev):
  148. """scan interval configuration"""
  149. try:
  150. if "FAIL" not in dev[0].request("SCAN_INTERVAL -1"):
  151. raise Exception("Accepted invalid scan interval")
  152. if "OK" not in dev[0].request("SCAN_INTERVAL 1"):
  153. raise Exception("Failed to set scan interval")
  154. dev[0].connect("not-used", key_mgmt="NONE", scan_freq="2412",
  155. wait_connect=False)
  156. times = {}
  157. for i in range(0, 3):
  158. logger.info("Waiting for scan to start")
  159. start = os.times()[4]
  160. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  161. if ev is None:
  162. raise Exception("did not start a scan")
  163. stop = os.times()[4]
  164. times[i] = stop - start
  165. logger.info("Waiting for scan to complete")
  166. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
  167. if ev is None:
  168. raise Exception("did not complete a scan")
  169. print times
  170. if times[0] > 1 or times[1] < 0.5 or times[1] > 1.5 or times[2] < 0.5 or times[2] > 1.5:
  171. raise Exception("Unexpected scan timing: " + str(times))
  172. finally:
  173. dev[0].request("SCAN_INTERVAL 5")
  174. def test_scan_bss_operations(dev, apdev):
  175. """Control interface behavior on BSS parameters"""
  176. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  177. bssid = apdev[0]['bssid']
  178. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "test2-scan" })
  179. bssid2 = apdev[1]['bssid']
  180. dev[0].scan(freq="2412")
  181. dev[0].scan(freq="2412")
  182. dev[0].scan(freq="2412")
  183. id1 = dev[0].request("BSS FIRST MASK=0x1").splitlines()[0].split('=')[1]
  184. id2 = dev[0].request("BSS LAST MASK=0x1").splitlines()[0].split('=')[1]
  185. res = dev[0].request("BSS RANGE=ALL MASK=0x20001")
  186. if "id=" + id1 not in res:
  187. raise Exception("Missing BSS " + id1)
  188. if "id=" + id2 not in res:
  189. raise Exception("Missing BSS " + id2)
  190. if "====" not in res:
  191. raise Exception("Missing delim")
  192. if "####" not in res:
  193. raise Exception("Missing end")
  194. res = dev[0].request("BSS RANGE=ALL MASK=0x1").splitlines()
  195. if len(res) != 2:
  196. raise Exception("Unexpected result")
  197. res = dev[0].request("BSS FIRST MASK=0x1")
  198. if "id=" + id1 not in res:
  199. raise Exception("Unexpected result: " + res)
  200. res = dev[0].request("BSS LAST MASK=0x1")
  201. if "id=" + id2 not in res:
  202. raise Exception("Unexpected result: " + res)
  203. res = dev[0].request("BSS ID-" + id1 + " MASK=0x1")
  204. if "id=" + id1 not in res:
  205. raise Exception("Unexpected result: " + res)
  206. res = dev[0].request("BSS NEXT-" + id1 + " MASK=0x1")
  207. if "id=" + id2 not in res:
  208. raise Exception("Unexpected result: " + res)
  209. if len(dev[0].request("BSS RANGE=" + id2 + " MASK=0x1").splitlines()) != 0:
  210. raise Exception("Unexpected RANGE=1 result")
  211. if len(dev[0].request("BSS RANGE=" + id1 + "- MASK=0x1").splitlines()) != 2:
  212. raise Exception("Unexpected RANGE=0- result")
  213. if len(dev[0].request("BSS RANGE=-" + id2 + " MASK=0x1").splitlines()) != 2:
  214. raise Exception("Unexpected RANGE=-1 result")
  215. if len(dev[0].request("BSS RANGE=" + id1 + "-" + id2 + " MASK=0x1").splitlines()) != 2:
  216. raise Exception("Unexpected RANGE=0-1 result")
  217. if len(dev[0].request("BSS RANGE=" + id2 + "-" + id2 + " MASK=0x1").splitlines()) != 1:
  218. raise Exception("Unexpected RANGE=1-1 result")
  219. if len(dev[0].request("BSS RANGE=" + str(int(id2) + 1) + "-" + str(int(id2) + 10) + " MASK=0x1").splitlines()) != 0:
  220. raise Exception("Unexpected RANGE=2-10 result")
  221. if len(dev[0].request("BSS RANGE=0-" + str(int(id2) + 10) + " MASK=0x1").splitlines()) != 2:
  222. raise Exception("Unexpected RANGE=0-10 result")
  223. def test_scan_and_interface_disabled(dev, apdev):
  224. """Scan operation when interface gets disabled"""
  225. try:
  226. dev[0].request("SCAN")
  227. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  228. if ev is None:
  229. raise Exception("Scan did not start")
  230. dev[0].request("DRIVER_EVENT INTERFACE_DISABLED")
  231. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=7)
  232. if ev is not None:
  233. raise Exception("Scan completed unexpectedly")
  234. # verify that scan is rejected
  235. if "FAIL" not in dev[0].request("SCAN"):
  236. raise Exception("New scan request was accepted unexpectedly")
  237. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  238. dev[0].scan(freq="2412")
  239. finally:
  240. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  241. def test_scan_for_auth(dev, apdev):
  242. """cfg80211 workaround with scan-for-auth"""
  243. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  244. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  245. # Block sme-connect radio work with an external radio work item, so that
  246. # SELECT_NETWORK can decide to use fast associate without a new scan while
  247. # cfg80211 still has the matching BSS entry, but the actual connection is
  248. # not yet started.
  249. id = dev[0].request("RADIO_WORK add block-work")
  250. ev = dev[0].wait_event(["EXT-RADIO-WORK-START"])
  251. if ev is None:
  252. raise Exception("Timeout while waiting radio work to start")
  253. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  254. wait_connect=False)
  255. # Clear cfg80211 BSS table.
  256. subprocess.call(['sudo', 'iw', dev[0].ifname, 'scan', 'trigger',
  257. 'freq', '2462', 'flush'])
  258. time.sleep(0.1)
  259. # Release blocking radio work to allow connection to go through with the
  260. # cfg80211 BSS entry missing.
  261. dev[0].request("RADIO_WORK done " + id)
  262. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  263. if ev is None:
  264. raise Exception("Association with the AP timed out")
  265. def test_scan_hidden(dev, apdev):
  266. """Control interface behavior on scan parameters"""
  267. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan",
  268. "ignore_broadcast_ssid": "1" })
  269. bssid = apdev[0]['bssid']
  270. check_scan(dev[0], "freq=2412 use_id=1")
  271. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  272. raise Exception("BSS unexpectedly found in initial scan")
  273. id1 = dev[0].connect("foo", key_mgmt="NONE", scan_ssid="1",
  274. only_add_network=True)
  275. id2 = dev[0].connect("test-scan", key_mgmt="NONE", scan_ssid="1",
  276. only_add_network=True)
  277. id3 = dev[0].connect("bar", key_mgmt="NONE", only_add_network=True)
  278. check_scan(dev[0], "freq=2412 use_id=1")
  279. if "test-scan" in dev[0].request("SCAN_RESULTS"):
  280. raise Exception("BSS unexpectedly found in scan")
  281. check_scan(dev[0], "scan_id=%d,%d,%d freq=2412 use_id=1" % (id1, id2, id3))
  282. if "test-scan" not in dev[0].request("SCAN_RESULTS"):
  283. raise Exception("BSS not found in scan")