test_p2p_device.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # cfg80211 P2P Device
  2. # Copyright (c) 2013-2015, 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. from wpasupplicant import WpaSupplicant
  10. from p2p_utils import *
  11. from test_nfc_p2p import set_ip_addr_info, check_ip_addr, grpform_events
  12. from hwsim import HWSimRadio
  13. import hostapd
  14. import hwsim_utils
  15. def test_p2p_device_grpform(dev, apdev):
  16. """P2P group formation with driver using cfg80211 P2P Device"""
  17. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  18. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  19. wpas.interface_add(iface)
  20. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  21. r_dev=wpas, r_intent=0)
  22. check_grpform_results(i_res, r_res)
  23. wpas.dump_monitor()
  24. remove_group(dev[0], wpas)
  25. wpas.dump_monitor()
  26. res = wpas.global_request("IFNAME=p2p-dev-" + iface + " STATUS-DRIVER")
  27. lines = res.splitlines()
  28. found = False
  29. for l in lines:
  30. try:
  31. [name,value] = l.split('=', 1)
  32. if name == "wdev_id":
  33. found = True
  34. break
  35. except ValueError:
  36. pass
  37. if not found:
  38. raise Exception("wdev_id not found")
  39. def test_p2p_device_grpform2(dev, apdev):
  40. """P2P group formation with driver using cfg80211 P2P Device (reverse)"""
  41. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  42. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  43. wpas.interface_add(iface)
  44. [i_res, r_res] = go_neg_pin_authorized(i_dev=wpas, i_intent=15,
  45. r_dev=dev[0], r_intent=0)
  46. check_grpform_results(i_res, r_res)
  47. wpas.dump_monitor()
  48. remove_group(wpas, dev[0])
  49. wpas.dump_monitor()
  50. def test_p2p_device_group_remove(dev, apdev):
  51. """P2P group removal via the P2P ctrl interface with driver using cfg80211 P2P Device"""
  52. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  53. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  54. wpas.interface_add(iface)
  55. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  56. r_dev=wpas, r_intent=0)
  57. check_grpform_results(i_res, r_res)
  58. # Issue the remove request on the interface which will be removed
  59. p2p_iface_wpas = WpaSupplicant(ifname=r_res['ifname'])
  60. res = p2p_iface_wpas.request("P2P_GROUP_REMOVE *")
  61. if "OK" not in res:
  62. raise Exception("Failed to remove P2P group")
  63. ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
  64. if ev is None:
  65. raise Exception("Group removal event not received")
  66. if not wpas.global_ping():
  67. raise Exception("Could not ping global ctrl_iface after group removal")
  68. def test_p2p_device_concurrent_scan(dev, apdev):
  69. """Concurrent P2P and station mode scans with driver using cfg80211 P2P Device"""
  70. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  71. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  72. wpas.interface_add(iface)
  73. wpas.p2p_find()
  74. time.sleep(0.1)
  75. wpas.request("SCAN")
  76. ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=15)
  77. if ev is None:
  78. raise Exception("Station mode scan did not start")
  79. def test_p2p_device_nfc_invite(dev, apdev):
  80. """P2P NFC invitiation with driver using cfg80211 P2P Device"""
  81. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  82. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  83. wpas.interface_add(iface)
  84. set_ip_addr_info(dev[0])
  85. logger.info("Start autonomous GO")
  86. dev[0].p2p_start_go()
  87. logger.info("Write NFC Tag on the P2P Client")
  88. res = wpas.global_request("P2P_LISTEN")
  89. if "FAIL" in res:
  90. raise Exception("Failed to start Listen mode")
  91. wpas.dump_monitor()
  92. pw = wpas.global_request("WPS_NFC_TOKEN NDEF").rstrip()
  93. if "FAIL" in pw:
  94. raise Exception("Failed to generate password token")
  95. res = wpas.global_request("P2P_SET nfc_tag 1").rstrip()
  96. if "FAIL" in res:
  97. raise Exception("Failed to enable NFC Tag for P2P static handover")
  98. sel = wpas.global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
  99. if "FAIL" in sel:
  100. raise Exception("Failed to generate NFC connection handover select")
  101. wpas.dump_monitor()
  102. logger.info("Read NFC Tag on the GO to trigger invitation")
  103. res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
  104. if "FAIL" in res:
  105. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  106. ev = wpas.wait_global_event(grpform_events, timeout=20)
  107. if ev is None:
  108. raise Exception("Joining the group timed out")
  109. res = wpas.group_form_result(ev)
  110. wpas.dump_monitor()
  111. hwsim_utils.test_connectivity_p2p(dev[0], wpas)
  112. check_ip_addr(res)
  113. wpas.dump_monitor()
  114. def test_p2p_device_misuses(dev, apdev):
  115. """cfg80211 P2P Device misuses"""
  116. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  117. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  118. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  119. wpas.interface_add(iface)
  120. # Add a normal network profile to the P2P Device management only
  121. # interface to verify that it does not get used.
  122. id = int(wpas.global_request('IFNAME=p2p-dev-%s ADD_NETWORK' % iface).strip())
  123. wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d ssid "open"' % (iface, id))
  124. wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d key_mgmt NONE' % (iface, id))
  125. wpas.global_request('IFNAME=p2p-dev-%s ENABLE_NETWORK %d' % (iface, id))
  126. # Scan requests get ignored on p2p-dev
  127. wpas.global_request('IFNAME=p2p-dev-%s SCAN' % iface)
  128. dev[0].p2p_start_go(freq=2412)
  129. addr = dev[0].p2p_interface_addr()
  130. wpas.scan_for_bss(addr, freq=2412)
  131. wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
  132. hwsim_utils.test_connectivity(wpas, hapd)
  133. pin = wpas.wps_read_pin()
  134. dev[0].p2p_go_authorize_client(pin)
  135. res = wpas.p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60,
  136. social=True, freq=2412)
  137. hwsim_utils.test_connectivity_p2p(dev[0], wpas)
  138. # Optimize scan-after-disconnect
  139. wpas.group_request("SET_NETWORK 0 scan_freq 2412")
  140. dev[0].group_request("DISASSOCIATE " + wpas.p2p_interface_addr())
  141. ev = wpas.wait_group_event(["CTRL-EVENT-DISCONNECT"])
  142. if ev is None:
  143. raise Exception("Did not see disconnect event on P2P group interface")
  144. dev[0].remove_group()
  145. ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  146. if ev is None:
  147. raise Exception("Scan not started")
  148. ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
  149. if ev is None:
  150. raise Exception("Scan not completed")
  151. time.sleep(1)
  152. hwsim_utils.test_connectivity(wpas, hapd)
  153. ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=0.1)
  154. if ev is not None:
  155. raise Exception("Unexpected disconnection event received from hostapd")
  156. ev = wpas.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.1)
  157. if ev is not None:
  158. raise Exception("Unexpected disconnection event received from wpa_supplicant")
  159. wpas.request("DISCONNECT")
  160. wpas.wait_disconnected()
  161. def test_p2p_device_incorrect_command_interface(dev, apdev):
  162. """cfg80211 P2P Device and P2P_* command on incorrect interface"""
  163. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  164. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  165. wpas.interface_add(iface)
  166. dev[0].p2p_listen()
  167. wpas.request('P2P_FIND type=social')
  168. ev = wpas.wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
  169. if ev is None:
  170. raise Exception("Peer not found")
  171. ev = wpas.wait_event(["P2P-DEVICE-FOUND"], timeout=0.1)
  172. if ev is not None:
  173. raise Exception("Unexpected P2P-DEVICE-FOUND event on station interface")
  174. wpas.dump_monitor()
  175. pin = wpas.wps_read_pin()
  176. dev[0].p2p_go_neg_auth(wpas.p2p_dev_addr(), pin, "enter", go_intent=14,
  177. freq=2412)
  178. wpas.request('P2P_STOP_FIND')
  179. wpas.dump_monitor()
  180. if "OK" not in wpas.request('P2P_CONNECT ' + dev[0].p2p_dev_addr() + ' ' + pin + ' display go_intent=1'):
  181. raise Exception("P2P_CONNECT failed")
  182. ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  183. if ev is None:
  184. raise Exception("Group formation timed out")
  185. wpas.group_form_result(ev)
  186. wpas.dump_monitor()
  187. ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  188. if ev is None:
  189. raise Exception("Group formation timed out(2)")
  190. dev[0].group_form_result(ev)
  191. dev[0].remove_group()
  192. wpas.wait_go_ending_session()
  193. wpas.dump_monitor()
  194. def test_p2p_device_incorrect_command_interface2(dev, apdev):
  195. """cfg80211 P2P Device and P2P_GROUP_ADD command on incorrect interface"""
  196. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  197. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  198. wpas.interface_add(iface)
  199. if "OK" not in wpas.request('P2P_GROUP_ADD'):
  200. raise Exception("P2P_GROUP_ADD failed")
  201. ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  202. if ev is None:
  203. raise Exception("Group formation timed out")
  204. res = wpas.group_form_result(ev)
  205. wpas.dump_monitor()
  206. logger.info("Group results: " + str(res))
  207. wpas.remove_group()
  208. if not res['ifname'].startswith('p2p-' + iface + '-'):
  209. raise Exception("Unexpected group ifname: " + res['ifname'])
  210. wpas.dump_monitor()