test_connect_cmd.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # cfg80211 connect command (SME in the driver/firmware)
  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 logging
  7. logger = logging.getLogger()
  8. import time
  9. import hwsim_utils
  10. import hostapd
  11. from wpasupplicant import WpaSupplicant
  12. from p2p_utils import *
  13. def test_connect_cmd_open(dev, apdev):
  14. """Open connection using cfg80211 connect command"""
  15. params = { "ssid": "sta-connect",
  16. "manage_p2p": "1",
  17. "allow_cross_connection": "1" }
  18. hostapd.add_ap(apdev[0], params)
  19. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  20. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  21. wpas.connect("sta-connect", key_mgmt="NONE", scan_freq="2412",
  22. bg_scan_period="1")
  23. wpas.dump_monitor()
  24. wpas.request("DISCONNECT")
  25. wpas.wait_disconnected()
  26. wpas.dump_monitor()
  27. def test_connect_cmd_wep(dev, apdev):
  28. """WEP Open System using cfg80211 connect command"""
  29. params = { "ssid": "sta-connect-wep", "wep_key0": '"hello"' }
  30. hapd = hostapd.add_ap(apdev[0], params)
  31. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  32. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  33. wpas.connect("sta-connect-wep", key_mgmt="NONE", scan_freq="2412",
  34. wep_key0='"hello"')
  35. wpas.dump_monitor()
  36. hwsim_utils.test_connectivity(wpas, hapd)
  37. wpas.request("DISCONNECT")
  38. wpas.wait_disconnected()
  39. wpas.dump_monitor()
  40. def test_connect_cmd_wep_shared(dev, apdev):
  41. """WEP Shared key using cfg80211 connect command"""
  42. params = { "ssid": "sta-connect-wep", "wep_key0": '"hello"',
  43. "auth_algs": "2" }
  44. hapd = hostapd.add_ap(apdev[0], params)
  45. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  46. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  47. id = wpas.connect("sta-connect-wep", key_mgmt="NONE", scan_freq="2412",
  48. auth_alg="SHARED", wep_key0='"hello"')
  49. wpas.dump_monitor()
  50. hwsim_utils.test_connectivity(wpas, hapd)
  51. wpas.request("DISCONNECT")
  52. wpas.remove_network(id)
  53. wpas.connect("sta-connect-wep", key_mgmt="NONE", scan_freq="2412",
  54. auth_alg="OPEN SHARED", wep_key0='"hello"')
  55. wpas.dump_monitor()
  56. hwsim_utils.test_connectivity(wpas, hapd)
  57. wpas.request("DISCONNECT")
  58. wpas.wait_disconnected()
  59. wpas.dump_monitor()
  60. def test_connect_cmd_p2p_management(dev, apdev):
  61. """Open connection using cfg80211 connect command and AP using P2P management"""
  62. params = { "ssid": "sta-connect",
  63. "manage_p2p": "1",
  64. "allow_cross_connection": "0" }
  65. hostapd.add_ap(apdev[0], params)
  66. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  67. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  68. wpas.connect("sta-connect", key_mgmt="NONE", scan_freq="2412")
  69. wpas.dump_monitor()
  70. wpas.request("DISCONNECT")
  71. wpas.wait_disconnected()
  72. wpas.dump_monitor()
  73. def test_connect_cmd_wpa2_psk(dev, apdev):
  74. """WPA2-PSK connection using cfg80211 connect command"""
  75. params = hostapd.wpa2_params(ssid="sta-connect", passphrase="12345678")
  76. hostapd.add_ap(apdev[0], params)
  77. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  78. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  79. wpas.connect("sta-connect", psk="12345678", scan_freq="2412")
  80. wpas.dump_monitor()
  81. wpas.request("DISCONNECT")
  82. wpas.wait_disconnected()
  83. wpas.dump_monitor()
  84. def test_connect_cmd_concurrent_grpform_while_connecting(dev, apdev):
  85. """Concurrent P2P group formation while connecting to an AP using cfg80211 connect command"""
  86. logger.info("Start connection to an infrastructure AP")
  87. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-open" })
  88. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  89. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  90. wpas.connect("test-open", key_mgmt="NONE", wait_connect=False)
  91. wpas.dump_monitor()
  92. logger.info("Form a P2P group while connecting to an AP")
  93. wpas.request("SET p2p_no_group_iface 0")
  94. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_freq=2412,
  95. r_dev=wpas, r_freq=2412)
  96. check_grpform_results(i_res, r_res)
  97. remove_group(dev[0], wpas)
  98. wpas.dump_monitor()
  99. logger.info("Confirm AP connection after P2P group removal")
  100. hwsim_utils.test_connectivity(wpas, hapd)
  101. wpas.request("DISCONNECT")
  102. wpas.wait_disconnected()
  103. wpas.dump_monitor()
  104. def test_connect_cmd_reject_assoc(dev, apdev):
  105. """Connection using cfg80211 connect command getting rejected"""
  106. params = { "ssid": "sta-connect",
  107. "require_ht": "1" }
  108. hostapd.add_ap(apdev[0], params)
  109. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  110. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  111. wpas.connect("sta-connect", key_mgmt="NONE", scan_freq="2412",
  112. disable_ht="1", wait_connect=False)
  113. # Reject event gets reported twice since we force connect command to be used
  114. # with a driver that supports auth+assoc for testing purposes.
  115. for i in range(0, 2):
  116. ev = wpas.wait_event(["CTRL-EVENT-ASSOC-REJECT"], timeout=15)
  117. if ev is None:
  118. raise Exception("Association rejection timed out")
  119. if "status_code=27" not in ev:
  120. raise Exception("Unexpected rejection status code")
  121. wpas.request("DISCONNECT")
  122. wpas.dump_monitor()
  123. def test_connect_cmd_disconnect_event(dev, apdev):
  124. """Connection using cfg80211 connect command getting disconnected by the AP"""
  125. params = { "ssid": "sta-connect" }
  126. hapd = hostapd.add_ap(apdev[0], params)
  127. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  128. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  129. wpas.connect("sta-connect", key_mgmt="NONE", scan_freq="2412")
  130. if "OK" not in hapd.request("DEAUTHENTICATE " + wpas.p2p_interface_addr()):
  131. raise Exception("DEAUTHENTICATE command failed")
  132. ev = wpas.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=5)
  133. if ev is None:
  134. raise Exception("Disconnection event timed out")
  135. # This event was actually based on deauthenticate event since we force
  136. # connect command to be used with a driver that supports auth+assoc for
  137. # testing purposes. Anyway, wait some time to allow the debug log to capture
  138. # the following NL80211_CMD_DISCONNECT event.
  139. time.sleep(0.1)
  140. wpas.dump_monitor()
  141. # Clean up to avoid causing issue for following test cases
  142. wpas.request("REMOVE_NETWORK all")
  143. wpas.wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=2)
  144. wpas.flush_scan_cache()
  145. wpas.dump_monitor()
  146. wpas.interface_remove("wlan5")
  147. del wpas
  148. def test_connect_cmd_roam(dev, apdev):
  149. """cfg80211 connect command to trigger roam"""
  150. params = { "ssid": "sta-connect" }
  151. hostapd.add_ap(apdev[0], params)
  152. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  153. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  154. wpas.connect("sta-connect", key_mgmt="NONE", scan_freq="2412")
  155. wpas.dump_monitor()
  156. hostapd.add_ap(apdev[1], params)
  157. wpas.scan_for_bss(apdev[1]['bssid'], freq=2412, force_scan=True)
  158. wpas.roam(apdev[1]['bssid'])
  159. time.sleep(0.1)
  160. wpas.request("DISCONNECT")
  161. wpas.wait_disconnected()
  162. wpas.dump_monitor()
  163. def test_connect_cmd_bssid_hint(dev, apdev):
  164. """cfg80211 connect command with bssid_hint"""
  165. params = { "ssid": "sta-connect" }
  166. hostapd.add_ap(apdev[0], params)
  167. hostapd.add_ap(apdev[1], params)
  168. # This does not really give full coverage with mac80211_hwsim since the
  169. # driver does not end up claiming support for driver-based BSS selection.
  170. # Anyway, some test coverage can be achieved for setting the parameter and
  171. # checking that it does not prevent connection with another BSSID.
  172. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  173. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  174. wpas.connect("sta-connect", key_mgmt="NONE", scan_freq="2412",
  175. bssid_hint=apdev[0]['bssid'])
  176. wpas.request("REMOVE_NETWORK all")
  177. wpas.wait_disconnected()
  178. wpas.dump_monitor()
  179. wpas.request("BSS_FLUSH 0")
  180. wpas.connect("sta-connect", key_mgmt="NONE", scan_freq="2412",
  181. bssid_hint='22:33:44:55:66:77')
  182. wpas.request("REMOVE_NETWORK all")
  183. wpas.wait_disconnected()
  184. wpas.dump_monitor()
  185. # Additional coverage using ap_scan=2 to prevent scan entry -based selection
  186. # within wpa_supplicant from overriding bssid_hint.
  187. try:
  188. if "OK" not in wpas.request("AP_SCAN 2"):
  189. raise Exception("Failed to set AP_SCAN 2")
  190. wpas.request("BSS_FLUSH 0")
  191. wpas.connect("sta-connect", key_mgmt="NONE", scan_freq="2412",
  192. bssid_hint='22:33:44:55:66:77')
  193. wpas.request("REMOVE_NETWORK all")
  194. wpas.wait_disconnected()
  195. wpas.dump_monitor()
  196. finally:
  197. wpas.request("AP_SCAN 1")