test_p2p_device.py 10 KB

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