test_p2p_device.py 9.7 KB

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