test_scan.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 test_scan(dev, apdev):
  41. """Control interface behavior on scan parameters"""
  42. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  43. bssid = apdev[0]['bssid']
  44. logger.info("Full scan")
  45. check_scan(dev[0], "use_id=1")
  46. logger.info("Limited channel scan")
  47. check_scan(dev[0], "freq=2412-2462,5180 use_id=1")
  48. if int(dev[0].get_bss(bssid)['age']) > 1:
  49. raise Exception("Unexpectedly old BSS entry")
  50. # wait long enough to allow next scans to be verified not to find the AP
  51. time.sleep(2)
  52. logger.info("Passive single-channel scan")
  53. check_scan(dev[0], "freq=2457 passive=1 use_id=1")
  54. logger.info("Active single-channel scan")
  55. check_scan(dev[0], "freq=2452 passive=0 use_id=1")
  56. if int(dev[0].get_bss(bssid)['age']) < 2:
  57. raise Exception("Unexpectedly updated BSS entry")
  58. logger.info("Active single-channel scan on AP's operating channel")
  59. check_scan(dev[0], "freq=2412 passive=0 use_id=1")
  60. if int(dev[0].get_bss(bssid)['age']) > 1:
  61. raise Exception("Unexpectedly old BSS entry")
  62. def test_scan_only(dev, apdev):
  63. """Control interface behavior on scan parameters with type=only"""
  64. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  65. bssid = apdev[0]['bssid']
  66. logger.info("Full scan")
  67. check_scan(dev[0], "type=only use_id=1")
  68. logger.info("Limited channel scan")
  69. check_scan(dev[0], "type=only freq=2412-2462,5180 use_id=1")
  70. if int(dev[0].get_bss(bssid)['age']) > 1:
  71. raise Exception("Unexpectedly old BSS entry")
  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(dev[0], "type=only freq=2412 passive=0 use_id=1")
  82. if int(dev[0].get_bss(bssid)['age']) > 1:
  83. raise Exception("Unexpectedly old BSS entry")
  84. def test_scan_external_trigger(dev, apdev):
  85. """Avoid operations during externally triggered scan"""
  86. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  87. bssid = apdev[0]['bssid']
  88. subprocess.call(['sudo', 'iw', dev[0].ifname, 'scan', 'trigger'])
  89. check_scan(dev[0], "use_id=1", other_started=True)
  90. def test_scan_bss_expiration_count(dev, apdev):
  91. """BSS entry expiration based on scan results without match"""
  92. if "FAIL" not in dev[0].request("BSS_EXPIRE_COUNT 0"):
  93. raise Exception("Invalid BSS_EXPIRE_COUNT accepted")
  94. if "OK" not in dev[0].request("BSS_EXPIRE_COUNT 2"):
  95. raise Exception("BSS_EXPIRE_COUNT failed")
  96. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  97. bssid = apdev[0]['bssid']
  98. dev[0].scan(freq="2412", only_new=True)
  99. if bssid not in dev[0].request("SCAN_RESULTS"):
  100. raise Exception("BSS not found in initial scan")
  101. hapd.request("DISABLE")
  102. dev[0].scan(freq="2412", only_new=True)
  103. if bssid not in dev[0].request("SCAN_RESULTS"):
  104. raise Exception("BSS not found in first scan without match")
  105. dev[0].scan(freq="2412", only_new=True)
  106. if bssid in dev[0].request("SCAN_RESULTS"):
  107. raise Exception("BSS found after two scans without match")
  108. def test_scan_bss_expiration_age(dev, apdev):
  109. """BSS entry expiration based on age"""
  110. try:
  111. if "FAIL" not in dev[0].request("BSS_EXPIRE_AGE COUNT 9"):
  112. raise Exception("Invalid BSS_EXPIRE_AGE accepted")
  113. if "OK" not in dev[0].request("BSS_EXPIRE_AGE 10"):
  114. raise Exception("BSS_EXPIRE_AGE failed")
  115. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  116. bssid = apdev[0]['bssid']
  117. dev[0].scan(freq="2412")
  118. if bssid not in dev[0].request("SCAN_RESULTS"):
  119. raise Exception("BSS not found in initial scan")
  120. hapd.request("DISABLE")
  121. logger.info("Waiting for BSS entry to expire")
  122. time.sleep(7)
  123. if bssid not in dev[0].request("SCAN_RESULTS"):
  124. raise Exception("BSS expired too quickly")
  125. ev = dev[0].wait_event(["CTRL-EVENT-BSS-REMOVED"], timeout=15)
  126. if ev is None:
  127. raise Exception("BSS entry expiration timed out")
  128. if bssid in dev[0].request("SCAN_RESULTS"):
  129. raise Exception("BSS not removed after expiration time")
  130. finally:
  131. dev[0].request("BSS_EXPIRE_AGE 180")
  132. def test_scan_filter(dev, apdev):
  133. """Filter scan results based on SSID"""
  134. try:
  135. if "OK" not in dev[0].request("SET filter_ssids 1"):
  136. raise Exception("SET failed")
  137. dev[0].connect("test-scan", key_mgmt="NONE", only_add_network=True)
  138. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  139. bssid = apdev[0]['bssid']
  140. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "test-scan2" })
  141. bssid2 = apdev[1]['bssid']
  142. dev[0].scan(freq="2412", only_new=True)
  143. if bssid not in dev[0].request("SCAN_RESULTS"):
  144. raise Exception("BSS not found in scan results")
  145. if bssid2 in dev[0].request("SCAN_RESULTS"):
  146. raise Exception("Unexpected BSS found in scan results")
  147. finally:
  148. dev[0].request("SET filter_ssids 0")
  149. def test_scan_int(dev, apdev):
  150. """scan interval configuration"""
  151. try:
  152. if "FAIL" not in dev[0].request("SCAN_INTERVAL -1"):
  153. raise Exception("Accepted invalid scan interval")
  154. if "OK" not in dev[0].request("SCAN_INTERVAL 1"):
  155. raise Exception("Failed to set scan interval")
  156. dev[0].connect("not-used", key_mgmt="NONE", scan_freq="2412",
  157. wait_connect=False)
  158. times = {}
  159. for i in range(0, 3):
  160. logger.info("Waiting for scan to start")
  161. start = os.times()[4]
  162. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  163. if ev is None:
  164. raise Exception("did not start a scan")
  165. stop = os.times()[4]
  166. times[i] = stop - start
  167. logger.info("Waiting for scan to complete")
  168. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
  169. if ev is None:
  170. raise Exception("did not complete a scan")
  171. print times
  172. if times[0] > 1 or times[1] < 0.5 or times[1] > 1.5 or times[2] < 0.5 or times[2] > 1.5:
  173. raise Exception("Unexpected scan timing: " + str(times))
  174. finally:
  175. dev[0].request("SCAN_INTERVAL 5")
  176. def test_scan_bss_operations(dev, apdev):
  177. """Control interface behavior on BSS parameters"""
  178. hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan" })
  179. bssid = apdev[0]['bssid']
  180. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "test2-scan" })
  181. bssid2 = apdev[1]['bssid']
  182. dev[0].scan(freq="2412")
  183. dev[0].scan(freq="2412")
  184. dev[0].scan(freq="2412")
  185. res = dev[0].request("BSS RANGE=ALL MASK=0x20001")
  186. if "id=0" not in res:
  187. raise Exception("Missing BSS 0")
  188. if "id=1" not in res:
  189. raise Exception("Missing BSS 1")
  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=0" not in res:
  199. raise Exception("Unexpected result: " + res)
  200. res = dev[0].request("BSS LAST MASK=0x1")
  201. if "id=1" not in res:
  202. raise Exception("Unexpected result: " + res)
  203. res = dev[0].request("BSS ID-0 MASK=0x1")
  204. if "id=0" not in res:
  205. raise Exception("Unexpected result: " + res)
  206. res = dev[0].request("BSS NEXT-0 MASK=0x1")
  207. if "id=1" not in res:
  208. raise Exception("Unexpected result: " + res)
  209. if len(dev[0].request("BSS RANGE=1 MASK=0x1").splitlines()) != 0:
  210. raise Exception("Unexpected RANGE=1 result")
  211. if len(dev[0].request("BSS RANGE=0- MASK=0x1").splitlines()) != 2:
  212. raise Exception("Unexpected RANGE=0- result")
  213. if len(dev[0].request("BSS RANGE=-1 MASK=0x1").splitlines()) != 2:
  214. raise Exception("Unexpected RANGE=-1 result")
  215. if len(dev[0].request("BSS RANGE=0-1 MASK=0x1").splitlines()) != 2:
  216. raise Exception("Unexpected RANGE=0-1 result")
  217. if len(dev[0].request("BSS RANGE=1-1 MASK=0x1").splitlines()) != 1:
  218. raise Exception("Unexpected RANGE=1-1 result")
  219. if len(dev[0].request("BSS RANGE=2-10 MASK=0x1").splitlines()) != 0:
  220. raise Exception("Unexpected RANGE=2-10 result")
  221. if len(dev[0].request("BSS RANGE=0-10 MASK=0x1").splitlines()) != 2:
  222. raise Exception("Unexpected RANGE=0-10 result")