test_bgscan.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # bgscan tests
  2. # Copyright (c) 2014-2017, 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 hostapd
  11. from utils import alloc_fail, fail_test
  12. def test_bgscan_simple(dev, apdev):
  13. """bgscan_simple"""
  14. hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  15. hostapd.add_ap(apdev[1], { "ssid": "bgscan" })
  16. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  17. bgscan="simple:1:-20:2")
  18. dev[1].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  19. bgscan="simple:1:-45:2")
  20. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  21. bgscan="simple:1:-45")
  22. dev[2].request("REMOVE_NETWORK all")
  23. dev[2].wait_disconnected()
  24. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  25. bgscan="simple:0:0")
  26. dev[2].request("REMOVE_NETWORK all")
  27. dev[2].wait_disconnected()
  28. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  29. bgscan="simple")
  30. dev[2].request("REMOVE_NETWORK all")
  31. dev[2].wait_disconnected()
  32. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  33. bgscan="simple:1")
  34. dev[2].request("REMOVE_NETWORK all")
  35. dev[2].wait_disconnected()
  36. ev = dev[0].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
  37. if ev is None:
  38. raise Exception("dev0 did not indicate signal change event")
  39. if "above=0" not in ev:
  40. raise Exception("Unexpected signal change event contents from dev0: " + ev)
  41. ev = dev[1].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
  42. if ev is None:
  43. raise Exception("dev1 did not indicate signal change event")
  44. if "above=1" not in ev:
  45. raise Exception("Unexpected signal change event contents from dev1: " + ev)
  46. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
  47. if ev is None:
  48. raise Exception("dev0 did not start a scan")
  49. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
  50. if ev is None:
  51. raise Exception("dev1 did not start a scan")
  52. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  53. if ev is None:
  54. raise Exception("dev0 did not complete a scan")
  55. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  56. if ev is None:
  57. raise Exception("dev1 did not complete a scan")
  58. def test_bgscan_simple_beacon_loss(dev, apdev):
  59. """bgscan_simple and beacon loss"""
  60. hapd = hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  61. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  62. bgscan="simple:1:-20:2")
  63. hapd.set("ext_mgmt_frame_handling", "1")
  64. if "OK" not in hapd.request("STOP_AP"):
  65. raise Exception("Failed to stop AP")
  66. ev = dev[0].wait_event(["CTRL-EVENT-BEACON-LOSS"], timeout=10)
  67. if ev is None:
  68. raise Exception("Beacon loss not reported")
  69. def test_bgscan_simple_scan_failure(dev, apdev):
  70. """bgscan_simple and scan failure"""
  71. hapd = hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  72. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  73. bgscan="simple:1:-20:2")
  74. with alloc_fail(dev[0], 1,
  75. "wpa_supplicant_trigger_scan;bgscan_simple_timeout"):
  76. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  77. if ev is None:
  78. raise Exception("No scan failure reported")
  79. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
  80. if ev is None:
  81. raise Exception("Scanning not continued after failure")
  82. def test_bgscan_simple_scanning(dev, apdev):
  83. """bgscan_simple and scanning behavior"""
  84. hapd = hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  85. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  86. bgscan="simple:1:-20:2")
  87. # Go through seven bgscan_simple_timeout calls for code coverage. This falls
  88. # back from short to long scan interval and then reduces short_scan_count
  89. # back to zero.
  90. for i in range(7):
  91. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
  92. if ev is None:
  93. raise Exception("Scanning not continued")
  94. def test_bgscan_simple_same_scan_int(dev, apdev):
  95. """bgscan_simple and same short/long scan interval"""
  96. hapd = hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  97. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  98. bgscan="simple:1:-20:1")
  99. for i in range(2):
  100. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
  101. if ev is None:
  102. raise Exception("Scanning not continued")
  103. def test_bgscan_simple_oom(dev, apdev):
  104. """bgscan_simple OOM"""
  105. hapd = hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  106. with alloc_fail(dev[0], 1, "bgscan_simple_init"):
  107. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  108. bgscan="simple:1:-20:2")
  109. def test_bgscan_simple_driver_conf_failure(dev, apdev):
  110. """bgscan_simple driver configuration failure"""
  111. hapd = hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  112. with fail_test(dev[0], 1, "bgscan_simple_init"):
  113. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  114. bgscan="simple:1:-20:2")
  115. def test_bgscan_learn(dev, apdev):
  116. """bgscan_learn"""
  117. hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  118. hostapd.add_ap(apdev[1], { "ssid": "bgscan" })
  119. try:
  120. os.remove("/tmp/test_bgscan_learn.bgscan")
  121. except:
  122. pass
  123. try:
  124. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  125. bgscan="learn:1:-20:2")
  126. id = dev[1].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  127. bgscan="learn:1:-45:2:/tmp/test_bgscan_learn.bgscan")
  128. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  129. bgscan="learn:1:-45")
  130. dev[2].request("REMOVE_NETWORK all")
  131. dev[2].wait_disconnected()
  132. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  133. bgscan="learn:0:0")
  134. dev[2].request("REMOVE_NETWORK all")
  135. dev[2].wait_disconnected()
  136. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  137. bgscan="learn")
  138. dev[2].request("REMOVE_NETWORK all")
  139. dev[2].wait_disconnected()
  140. dev[2].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  141. bgscan="learn:1")
  142. dev[2].request("REMOVE_NETWORK all")
  143. dev[2].wait_disconnected()
  144. ev = dev[0].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
  145. if ev is None:
  146. raise Exception("dev0 did not indicate signal change event")
  147. if "above=0" not in ev:
  148. raise Exception("Unexpected signal change event contents from dev0: " + ev)
  149. ev = dev[1].wait_event(["CTRL-EVENT-SIGNAL-CHANGE"], timeout=10)
  150. if ev is None:
  151. raise Exception("dev1 did not indicate signal change event")
  152. if "above=1" not in ev:
  153. raise Exception("Unexpected signal change event contents from dev1: " + ev)
  154. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
  155. if ev is None:
  156. raise Exception("dev0 did not start a scan")
  157. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=3)
  158. if ev is None:
  159. raise Exception("dev1 did not start a scan")
  160. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  161. if ev is None:
  162. raise Exception("dev0 did not complete a scan")
  163. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  164. if ev is None:
  165. raise Exception("dev1 did not complete a scan")
  166. dev[0].request("DISCONNECT")
  167. dev[1].request("DISCONNECT")
  168. dev[0].request("REMOVE_NETWORK all")
  169. with open("/tmp/test_bgscan_learn.bgscan", "r") as f:
  170. lines = f.read().splitlines()
  171. if lines[0] != "wpa_supplicant-bgscan-learn":
  172. raise Exception("Unexpected bgscan header line")
  173. if 'BSS 02:00:00:00:03:00 2412' not in lines:
  174. raise Exception("Missing BSS1")
  175. if 'BSS 02:00:00:00:04:00 2412' not in lines:
  176. raise Exception("Missing BSS2")
  177. if 'NEIGHBOR 02:00:00:00:03:00 02:00:00:00:04:00' not in lines:
  178. raise Exception("Missing BSS1->BSS2 neighbor entry")
  179. if 'NEIGHBOR 02:00:00:00:04:00 02:00:00:00:03:00' not in lines:
  180. raise Exception("Missing BSS2->BSS1 neighbor entry")
  181. dev[1].set_network(id, "scan_freq", "")
  182. dev[1].connect_network(id)
  183. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=10)
  184. if ev is None:
  185. raise Exception("dev1 did not start a scan")
  186. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
  187. if ev is None:
  188. raise Exception("dev1 did not complete a scan")
  189. dev[1].request("REMOVE_NETWORK all")
  190. finally:
  191. try:
  192. os.remove("/tmp/test_bgscan_learn.bgscan")
  193. except:
  194. pass
  195. def test_bgscan_learn_beacon_loss(dev, apdev):
  196. """bgscan_simple and beacon loss"""
  197. hapd = hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  198. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  199. bgscan="learn:1:-20:2")
  200. hapd.set("ext_mgmt_frame_handling", "1")
  201. if "OK" not in hapd.request("STOP_AP"):
  202. raise Exception("Failed to stop AP")
  203. ev = dev[0].wait_event(["CTRL-EVENT-BEACON-LOSS"], timeout=10)
  204. if ev is None:
  205. raise Exception("Beacon loss not reported")
  206. def test_bgscan_learn_scan_failure(dev, apdev):
  207. """bgscan_learn and scan failure"""
  208. hapd = hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  209. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  210. bgscan="learn:1:-20:2")
  211. with alloc_fail(dev[0], 1,
  212. "wpa_supplicant_trigger_scan;bgscan_learn_timeout"):
  213. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-FAILED"], timeout=10)
  214. if ev is None:
  215. raise Exception("No scan failure reported")
  216. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
  217. if ev is None:
  218. raise Exception("Scanning not continued after failure")
  219. def test_bgscan_learn_oom(dev, apdev):
  220. """bgscan_learn OOM"""
  221. hapd = hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  222. with alloc_fail(dev[0], 1, "bgscan_learn_init"):
  223. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  224. bgscan="learn:1:-20:2")
  225. def test_bgscan_learn_driver_conf_failure(dev, apdev):
  226. """bgscan_learn driver configuration failure"""
  227. hapd = hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  228. with fail_test(dev[0], 1, "bgscan_learn_init"):
  229. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  230. bgscan="learn:1:-20:2")
  231. def test_bgscan_unknown_module(dev, apdev):
  232. """bgscan init failing due to unknown module"""
  233. hapd = hostapd.add_ap(apdev[0], { "ssid": "bgscan" })
  234. dev[0].connect("bgscan", key_mgmt="NONE", scan_freq="2412",
  235. bgscan="unknown:-20:2")