test_rfkill.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # rfkill tests
  2. # Copyright (c) 2014, 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 logging
  7. logger = logging.getLogger()
  8. import time
  9. import hostapd
  10. from hostapd import HostapdGlobal
  11. import hwsim_utils
  12. from wpasupplicant import WpaSupplicant
  13. from rfkill import RFKill
  14. from utils import HwsimSkip
  15. from hwsim import HWSimRadio
  16. def get_rfkill(dev):
  17. phy = dev.get_driver_status_field("phyname")
  18. try:
  19. for r, s, h in RFKill.list():
  20. if r.name == phy:
  21. return r
  22. except Exception, e:
  23. raise HwsimSkip("No rfkill available: " + str(e))
  24. raise HwsimSkip("No rfkill match found for the interface")
  25. def test_rfkill_open(dev, apdev):
  26. """rfkill block/unblock during open mode connection"""
  27. rfk = get_rfkill(dev[0])
  28. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  29. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  30. try:
  31. logger.info("rfkill block")
  32. rfk.block()
  33. dev[0].wait_disconnected(timeout=10,
  34. error="Missing disconnection event on rfkill block")
  35. if "FAIL" not in dev[0].request("REASSOCIATE"):
  36. raise Exception("REASSOCIATE accepted while disabled")
  37. if "FAIL" not in dev[0].request("REATTACH"):
  38. raise Exception("REATTACH accepted while disabled")
  39. if "FAIL" not in dev[0].request("RECONNECT"):
  40. raise Exception("RECONNECT accepted while disabled")
  41. if "FAIL" not in dev[0].request("FETCH_OSU"):
  42. raise Exception("FETCH_OSU accepted while disabled")
  43. logger.info("rfkill unblock")
  44. rfk.unblock()
  45. dev[0].wait_connected(timeout=10,
  46. error="Missing connection event on rfkill unblock")
  47. hwsim_utils.test_connectivity(dev[0], hapd)
  48. finally:
  49. rfk.unblock()
  50. def test_rfkill_wpa2_psk(dev, apdev):
  51. """rfkill block/unblock during WPA2-PSK connection"""
  52. rfk = get_rfkill(dev[0])
  53. ssid = "test-wpa2-psk"
  54. passphrase = 'qwertyuiop'
  55. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  56. hapd = hostapd.add_ap(apdev[0], params)
  57. dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
  58. try:
  59. logger.info("rfkill block")
  60. rfk.block()
  61. dev[0].wait_disconnected(timeout=10,
  62. error="Missing disconnection event on rfkill block")
  63. logger.info("rfkill unblock")
  64. rfk.unblock()
  65. dev[0].wait_connected(timeout=10,
  66. error="Missing connection event on rfkill unblock")
  67. hwsim_utils.test_connectivity(dev[0], hapd)
  68. finally:
  69. rfk.unblock()
  70. def test_rfkill_autogo(dev, apdev):
  71. """rfkill block/unblock for autonomous P2P GO"""
  72. rfk0 = get_rfkill(dev[0])
  73. rfk1 = get_rfkill(dev[1])
  74. dev[0].p2p_start_go()
  75. dev[1].request("SET p2p_no_group_iface 0")
  76. dev[1].p2p_start_go()
  77. try:
  78. logger.info("rfkill block 0")
  79. rfk0.block()
  80. ev = dev[0].wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
  81. if ev is None:
  82. raise Exception("Group removal not reported")
  83. if "reason=UNAVAILABLE" not in ev:
  84. raise Exception("Unexpected group removal reason: " + ev)
  85. if "FAIL" not in dev[0].request("P2P_LISTEN 1"):
  86. raise Exception("P2P_LISTEN accepted unexpectedly")
  87. if "FAIL" not in dev[0].request("P2P_LISTEN"):
  88. raise Exception("P2P_LISTEN accepted unexpectedly")
  89. logger.info("rfkill block 1")
  90. rfk1.block()
  91. ev = dev[1].wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
  92. if ev is None:
  93. raise Exception("Group removal not reported")
  94. if "reason=UNAVAILABLE" not in ev:
  95. raise Exception("Unexpected group removal reason: " + ev)
  96. logger.info("rfkill unblock 0")
  97. rfk0.unblock()
  98. logger.info("rfkill unblock 1")
  99. rfk1.unblock()
  100. time.sleep(1)
  101. finally:
  102. rfk0.unblock()
  103. rfk1.unblock()
  104. def _test_rfkill_p2p_discovery(dev0, dev1):
  105. """rfkill block/unblock P2P Discovery"""
  106. rfk0 = get_rfkill(dev0)
  107. rfk1 = get_rfkill(dev1)
  108. try:
  109. addr0 = dev0.p2p_dev_addr()
  110. logger.info("rfkill block 0")
  111. rfk0.block()
  112. logger.info("rfkill block 1")
  113. rfk1.block()
  114. for i in range(10):
  115. time.sleep(0.1)
  116. if dev0.get_status_field("wpa_state") == "INTERFACE_DISABLED" and dev1.get_status_field("wpa_state") == "INTERFACE_DISABLED":
  117. break
  118. if "OK" in dev0.p2p_listen():
  119. raise Exception("P2P Listen success although in rfkill")
  120. if "OK" in dev1.p2p_find():
  121. raise Exception("P2P Find success although in rfkill")
  122. dev0.dump_monitor()
  123. dev1.dump_monitor()
  124. logger.info("rfkill unblock 0")
  125. rfk0.unblock()
  126. logger.info("rfkill unblock 1")
  127. rfk1.unblock()
  128. for i in range(10):
  129. time.sleep(0.1)
  130. if dev0.get_status_field("wpa_state") != "INTERFACE_DISABLED" and dev1.get_status_field("wpa_state") != "INTERFACE_DISABLED":
  131. break
  132. if not "OK" in dev0.p2p_listen():
  133. raise Exception("P2P Listen failed after unblocking rfkill")
  134. if not dev1.discover_peer(addr0, social=True):
  135. raise Exception("Failed to discover peer after unblocking rfkill")
  136. finally:
  137. rfk0.unblock()
  138. rfk1.unblock()
  139. dev0.p2p_stop_find()
  140. dev1.p2p_stop_find()
  141. dev0.dump_monitor()
  142. dev1.dump_monitor()
  143. def test_rfkill_p2p_discovery(dev, apdev):
  144. """rfkill block/unblock P2P Discovery"""
  145. _test_rfkill_p2p_discovery(dev[0], dev[1])
  146. def test_rfkill_p2p_discovery_p2p_dev(dev, apdev):
  147. """rfkill block/unblock P2P Discovery with P2P Device"""
  148. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  149. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  150. wpas.interface_add(iface)
  151. _test_rfkill_p2p_discovery(dev[0], wpas)
  152. _test_rfkill_p2p_discovery(wpas, dev[1])
  153. def test_rfkill_hostapd(dev, apdev):
  154. """rfkill block/unblock during and prior to hostapd operations"""
  155. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  156. rfk = get_rfkill(hapd)
  157. try:
  158. rfk.block()
  159. ev = hapd.wait_event(["INTERFACE-DISABLED"], timeout=5)
  160. if ev is None:
  161. raise Exception("INTERFACE-DISABLED event not seen")
  162. rfk.unblock()
  163. ev = hapd.wait_event(["INTERFACE-ENABLED"], timeout=5)
  164. if ev is None:
  165. raise Exception("INTERFACE-ENABLED event not seen")
  166. # hostapd does not current re-enable beaconing automatically
  167. hapd.disable()
  168. hapd.enable()
  169. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  170. rfk.block()
  171. ev = hapd.wait_event(["INTERFACE-DISABLED"], timeout=5)
  172. if ev is None:
  173. raise Exception("INTERFACE-DISABLED event not seen")
  174. dev[0].wait_disconnected(timeout=10)
  175. dev[0].request("DISCONNECT")
  176. hapd.disable()
  177. hglobal = HostapdGlobal(apdev[0])
  178. hglobal.flush()
  179. hglobal.remove(apdev[0]['ifname'])
  180. hapd = hostapd.add_ap(apdev[0], { "ssid": "open2" },
  181. no_enable=True)
  182. if "FAIL" not in hapd.request("ENABLE"):
  183. raise Exception("ENABLE succeeded unexpectedly (rfkill)")
  184. finally:
  185. rfk.unblock()
  186. def test_rfkill_wpas(dev, apdev):
  187. """rfkill block prior to wpa_supplicant start"""
  188. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  189. wpas.interface_add("wlan5")
  190. rfk = get_rfkill(wpas)
  191. wpas.interface_remove("wlan5")
  192. try:
  193. rfk.block()
  194. wpas.interface_add("wlan5")
  195. time.sleep(0.5)
  196. state = wpas.get_status_field("wpa_state")
  197. if state != "INTERFACE_DISABLED":
  198. raise Exception("Unexpected state with rfkill blocked: " + state)
  199. rfk.unblock()
  200. time.sleep(0.5)
  201. state = wpas.get_status_field("wpa_state")
  202. if state == "INTERFACE_DISABLED":
  203. raise Exception("Unexpected state with rfkill unblocked: " + state)
  204. finally:
  205. rfk.unblock()