test_ap_roam.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # Roaming 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. from remotehost import remote_compatible
  7. import time
  8. import logging
  9. logger = logging.getLogger()
  10. import hwsim_utils
  11. import hostapd
  12. from wpasupplicant import WpaSupplicant
  13. @remote_compatible
  14. def test_ap_roam_open(dev, apdev):
  15. """Roam between two open APs"""
  16. hapd0 = hostapd.add_ap(apdev[0], { "ssid": "test-open" })
  17. dev[0].connect("test-open", key_mgmt="NONE")
  18. hwsim_utils.test_connectivity(dev[0], hapd0)
  19. hapd1 = hostapd.add_ap(apdev[1], { "ssid": "test-open" })
  20. dev[0].scan(type="ONLY")
  21. dev[0].roam(apdev[1]['bssid'])
  22. hwsim_utils.test_connectivity(dev[0], hapd1)
  23. dev[0].roam(apdev[0]['bssid'])
  24. hwsim_utils.test_connectivity(dev[0], hapd0)
  25. @remote_compatible
  26. def test_ap_roam_open_failed(dev, apdev):
  27. """Roam failure due to rejected authentication"""
  28. hapd0 = hostapd.add_ap(apdev[0], { "ssid": "test-open" })
  29. dev[0].connect("test-open", key_mgmt="NONE", scan_freq="2412")
  30. hwsim_utils.test_connectivity(dev[0], hapd0)
  31. params = { "ssid": "test-open", "max_num_sta" : "0" }
  32. hapd1 = hostapd.add_ap(apdev[1], params)
  33. bssid = hapd1.own_addr()
  34. dev[0].scan_for_bss(bssid, freq=2412)
  35. dev[0].dump_monitor()
  36. if "OK" not in dev[0].request("ROAM " + bssid):
  37. raise Exception("ROAM failed")
  38. ev = dev[0].wait_event(["CTRL-EVENT-AUTH-REJECT"], 1)
  39. if not ev:
  40. raise Exception("CTRL-EVENT-AUTH-REJECT was not seen")
  41. dev[0].wait_connected(timeout=5)
  42. hwsim_utils.test_connectivity(dev[0], hapd0)
  43. @remote_compatible
  44. def test_ap_roam_wpa2_psk(dev, apdev):
  45. """Roam between two WPA2-PSK APs"""
  46. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  47. hapd0 = hostapd.add_ap(apdev[0], params)
  48. dev[0].connect("test-wpa2-psk", psk="12345678")
  49. hwsim_utils.test_connectivity(dev[0], hapd0)
  50. hapd1 = hostapd.add_ap(apdev[1], params)
  51. dev[0].scan(type="ONLY")
  52. dev[0].roam(apdev[1]['bssid'])
  53. hwsim_utils.test_connectivity(dev[0], hapd1)
  54. dev[0].roam(apdev[0]['bssid'])
  55. hwsim_utils.test_connectivity(dev[0], hapd0)
  56. def get_blacklist(dev):
  57. return dev.request("BLACKLIST").splitlines()
  58. def test_ap_reconnect_auth_timeout(dev, apdev, params):
  59. """Reconnect to 2nd AP and authentication times out"""
  60. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  61. wpas.interface_add("wlan5",
  62. drv_params="force_connect_cmd=1,force_bss_selection=1")
  63. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  64. hapd0 = hostapd.add_ap(apdev[0], params)
  65. bssid0 = hapd0.own_addr()
  66. hapd1 = hostapd.add_ap(apdev[1], params)
  67. bssid1 = hapd1.own_addr()
  68. wpas.scan_for_bss(bssid0, freq=2412)
  69. id = wpas.connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
  70. hwsim_utils.test_connectivity(wpas, hapd0)
  71. wpas.request("BLACKLIST " + bssid0)
  72. wpas.scan_for_bss(bssid1, freq=2412)
  73. wpas.request("DISCONNECT")
  74. if "OK" not in wpas.request("SET ignore_auth_resp 1"):
  75. raise Exception("SET ignore_auth_resp failed")
  76. if "OK" not in wpas.request("ENABLE_NETWORK " + str(id)):
  77. raise Exception("ENABLE_NETWORK failed")
  78. if "OK" not in wpas.request("SELECT_NETWORK " + str(id)):
  79. raise Exception("SELECT_NETWORK failed")
  80. logger.info("Wait ~10s for auth timeout...")
  81. time.sleep(10)
  82. ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], 12)
  83. if not ev:
  84. raise Exception("CTRL-EVENT-SCAN-STARTED not seen");
  85. b = get_blacklist(wpas)
  86. if '00:00:00:00:00:00' in b:
  87. raise Exception("Unexpected blacklist contents: " + str(b))
  88. if bssid1 not in b:
  89. raise Exception("Unexpected blacklist contents: " + str(b))
  90. def test_ap_roam_with_reassoc_auth_timeout(dev, apdev, params):
  91. """Roam using reassoc between two APs and authentication times out"""
  92. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  93. wpas.interface_add("wlan5",
  94. drv_params="force_connect_cmd=1,force_bss_selection=1")
  95. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  96. hapd0 = hostapd.add_ap(apdev[0], params)
  97. bssid0 = hapd0.own_addr()
  98. id = wpas.connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
  99. hwsim_utils.test_connectivity(wpas, hapd0)
  100. hapd1 = hostapd.add_ap(apdev[1], params)
  101. bssid1 = hapd1.own_addr()
  102. wpas.scan_for_bss(bssid1, freq=2412)
  103. if "OK" not in wpas.request("SET_NETWORK " + str(id) + " bssid " + bssid1):
  104. raise Exception("SET_NETWORK failed")
  105. if "OK" not in wpas.request("SET ignore_auth_resp 1"):
  106. raise Exception("SET ignore_auth_resp failed")
  107. if "OK" not in wpas.request("REASSOCIATE"):
  108. raise Exception("REASSOCIATE failed")
  109. logger.info("Wait ~10s for auth timeout...")
  110. time.sleep(10)
  111. ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], 12)
  112. if not ev:
  113. raise Exception("CTRL-EVENT-SCAN-STARTED not seen");
  114. b = get_blacklist(wpas)
  115. if bssid0 in b:
  116. raise Exception("Unexpected blacklist contents: " + str(b))
  117. def test_ap_roam_wpa2_psk_failed(dev, apdev, params):
  118. """Roam failure with WPA2-PSK AP due to wrong passphrase"""
  119. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  120. hapd0 = hostapd.add_ap(apdev[0], params)
  121. id = dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
  122. hwsim_utils.test_connectivity(dev[0], hapd0)
  123. params['wpa_passphrase'] = "22345678"
  124. hapd1 = hostapd.add_ap(apdev[1], params)
  125. bssid = hapd1.own_addr()
  126. dev[0].scan_for_bss(bssid, freq=2412)
  127. dev[0].dump_monitor()
  128. if "OK" not in dev[0].request("ROAM " + bssid):
  129. raise Exception("ROAM failed")
  130. ev = dev[0].wait_event(["CTRL-EVENT-SSID-TEMP-DISABLED",
  131. "CTRL-EVENT-CONNECTED"], 5)
  132. if "CTRL-EVENT-CONNECTED" in ev:
  133. raise Exception("Got unexpected CTRL-EVENT-CONNECTED")
  134. if "CTRL-EVENT-SSID-TEMP-DISABLED" not in ev:
  135. raise Exception("CTRL-EVENT-SSID-TEMP-DISABLED not seen")
  136. if "OK" not in dev[0].request("SELECT_NETWORK id=" + str(id)):
  137. raise Exception("SELECT_NETWORK failed")
  138. ev = dev[0].wait_event(["CTRL-EVENT-SSID-REENABLED"], 3)
  139. if not ev:
  140. raise Exception("CTRL-EVENT-SSID-REENABLED not seen")
  141. dev[0].wait_connected(timeout=5)
  142. hwsim_utils.test_connectivity(dev[0], hapd0)
  143. @remote_compatible
  144. def test_ap_reassociation_to_same_bss(dev, apdev):
  145. """Reassociate to the same BSS"""
  146. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-open" })
  147. dev[0].connect("test-open", key_mgmt="NONE")
  148. dev[0].request("REASSOCIATE")
  149. dev[0].wait_connected(timeout=10, error="Reassociation timed out")
  150. hwsim_utils.test_connectivity(dev[0], hapd)
  151. dev[0].request("REATTACH")
  152. dev[0].wait_connected(timeout=10, error="Reattach timed out")
  153. hwsim_utils.test_connectivity(dev[0], hapd)
  154. # Wait for previous scan results to expire to trigger new scan
  155. time.sleep(5)
  156. dev[0].request("REATTACH")
  157. dev[0].wait_connected(timeout=10, error="Reattach timed out")
  158. hwsim_utils.test_connectivity(dev[0], hapd)
  159. @remote_compatible
  160. def test_ap_roam_set_bssid(dev, apdev):
  161. """Roam control"""
  162. hostapd.add_ap(apdev[0], { "ssid": "test-open" })
  163. hostapd.add_ap(apdev[1], { "ssid": "test-open" })
  164. id = dev[0].connect("test-open", key_mgmt="NONE", bssid=apdev[1]['bssid'],
  165. scan_freq="2412")
  166. if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
  167. raise Exception("Unexpected BSS")
  168. # for now, these are just verifying that the code path to indicate
  169. # within-ESS roaming changes can be executed; the actual results of those
  170. # operations are not currently verified (that would require a test driver
  171. # that does BSS selection)
  172. dev[0].set_network(id, "bssid", "")
  173. dev[0].set_network(id, "bssid", apdev[0]['bssid'])
  174. dev[0].set_network(id, "bssid", apdev[1]['bssid'])
  175. @remote_compatible
  176. def test_ap_roam_wpa2_psk_race(dev, apdev):
  177. """Roam between two WPA2-PSK APs and try to hit a disconnection race"""
  178. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  179. hapd0 = hostapd.add_ap(apdev[0], params)
  180. dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
  181. hwsim_utils.test_connectivity(dev[0], hapd0)
  182. params['channel'] = '2'
  183. hapd1 = hostapd.add_ap(apdev[1], params)
  184. dev[0].scan_for_bss(apdev[1]['bssid'], freq=2417)
  185. dev[0].roam(apdev[1]['bssid'])
  186. hwsim_utils.test_connectivity(dev[0], hapd1)
  187. dev[0].roam(apdev[0]['bssid'])
  188. hwsim_utils.test_connectivity(dev[0], hapd0)
  189. # Wait at least two seconds to trigger the previous issue with the
  190. # disconnection callback.
  191. for i in range(3):
  192. time.sleep(0.8)
  193. hwsim_utils.test_connectivity(dev[0], hapd0)