test_p2p_discovery.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # P2P device discovery test cases
  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 hwsim_utils
  9. from wpasupplicant import WpaSupplicant
  10. def test_discovery(dev):
  11. """P2P device discovery and provision discovery"""
  12. addr0 = dev[0].p2p_dev_addr()
  13. addr1 = dev[1].p2p_dev_addr()
  14. logger.info("Start device discovery")
  15. dev[0].p2p_find(social=True)
  16. if not dev[1].discover_peer(addr0):
  17. raise Exception("Device discovery timed out")
  18. if not dev[0].discover_peer(addr1):
  19. raise Exception("Device discovery timed out")
  20. logger.info("Test provision discovery for display")
  21. dev[0].global_request("P2P_PROV_DISC " + addr1 + " display")
  22. ev1 = dev[1].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=15)
  23. if ev1 is None:
  24. raise Exception("Provision discovery timed out (display/dev1)")
  25. if addr0 not in ev1:
  26. raise Exception("Dev0 not in provision discovery event")
  27. ev0 = dev[0].wait_global_event(["P2P-PROV-DISC-ENTER-PIN",
  28. "P2P-PROV-DISC-FAILURE"], timeout=15)
  29. if ev0 is None:
  30. raise Exception("Provision discovery timed out (display/dev0)")
  31. if "P2P-PROV-DISC-FAILURE" in ev0:
  32. raise Exception("Provision discovery failed (display/dev0)")
  33. if addr1 not in ev0:
  34. raise Exception("Dev1 not in provision discovery event")
  35. logger.info("Test provision discovery for keypad")
  36. dev[0].global_request("P2P_PROV_DISC " + addr1 + " keypad")
  37. ev1 = dev[1].wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout=15)
  38. if ev1 is None:
  39. raise Exception("Provision discovery timed out (keypad/dev1)")
  40. if addr0 not in ev1:
  41. raise Exception("Dev0 not in provision discovery event")
  42. ev0 = dev[0].wait_global_event(["P2P-PROV-DISC-SHOW-PIN",
  43. "P2P-PROV-DISC-FAILURE"],
  44. timeout=15)
  45. if ev0 is None:
  46. raise Exception("Provision discovery timed out (keypad/dev0)")
  47. if "P2P-PROV-DISC-FAILURE" in ev0:
  48. raise Exception("Provision discovery failed (keypad/dev0)")
  49. if addr1 not in ev0:
  50. raise Exception("Dev1 not in provision discovery event")
  51. logger.info("Test provision discovery for push button")
  52. dev[0].global_request("P2P_PROV_DISC " + addr1 + " pbc")
  53. ev1 = dev[1].wait_global_event(["P2P-PROV-DISC-PBC-REQ"], timeout=15)
  54. if ev1 is None:
  55. raise Exception("Provision discovery timed out (pbc/dev1)")
  56. if addr0 not in ev1:
  57. raise Exception("Dev0 not in provision discovery event")
  58. ev0 = dev[0].wait_global_event(["P2P-PROV-DISC-PBC-RESP",
  59. "P2P-PROV-DISC-FAILURE"],
  60. timeout=15)
  61. if ev0 is None:
  62. raise Exception("Provision discovery timed out (pbc/dev0)")
  63. if "P2P-PROV-DISC-FAILURE" in ev0:
  64. raise Exception("Provision discovery failed (pbc/dev0)")
  65. if addr1 not in ev0:
  66. raise Exception("Dev1 not in provision discovery event")
  67. dev[0].p2p_stop_find
  68. dev[1].p2p_stop_find
  69. def test_discovery_pd_retries(dev):
  70. """P2P device discovery and provision discovery retries"""
  71. addr0 = dev[0].p2p_dev_addr()
  72. addr1 = dev[1].p2p_dev_addr()
  73. dev[1].p2p_listen()
  74. if not dev[0].discover_peer(addr1):
  75. raise Exception("Device discovery timed out")
  76. dev[1].p2p_stop_find()
  77. dev[0].p2p_stop_find()
  78. dev[0].global_request("P2P_PROV_DISC " + addr1 + " display")
  79. ev = dev[0].wait_event(["P2P-PROV-DISC-FAILURE"], timeout=60)
  80. if ev is None:
  81. raise Exception("No PD failure reported")
  82. def test_discovery_group_client(dev):
  83. """P2P device discovery for a client in a group"""
  84. logger.info("Start autonomous GO " + dev[0].ifname)
  85. res = dev[0].p2p_start_go(freq="2422")
  86. logger.debug("res: " + str(res))
  87. logger.info("Connect a client to the GO")
  88. pin = dev[1].wps_read_pin()
  89. dev[0].p2p_go_authorize_client(pin)
  90. dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
  91. logger.info("Client connected")
  92. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  93. logger.info("Try to discover a P2P client in a group")
  94. if not dev[2].discover_peer(dev[1].p2p_dev_addr(), social=False):
  95. raise Exception("Could not discover group client")
  96. # This is not really perfect, but something to get a bit more testing
  97. # coverage.. For proper discoverability mechanism validation, the P2P
  98. # client would need to go to sleep to avoid acknowledging the GO Negotiation
  99. # Request frame. Offchannel Listen mode operation on the P2P Client with
  100. # mac80211_hwsim is apparently not enough to avoid the acknowledgement on
  101. # the operating channel, so need to disconnect from the group which removes
  102. # the GO-to-P2P Client part of the discoverability exchange in practice.
  103. pin = dev[2].wps_read_pin()
  104. # make group client non-responsive on operating channel
  105. dev[1].dump_monitor()
  106. dev[1].group_request("DISCONNECT")
  107. ev = dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  108. if ev is None:
  109. raise Exception("Timeout on waiting disconnection")
  110. dev[2].request("P2P_CONNECT {} {} display".format(dev[1].p2p_dev_addr(),
  111. pin))
  112. ev = dev[1].wait_event(["P2P-GO-NEG-REQUEST"], timeout=2)
  113. if ev:
  114. raise Exception("Unexpected frame RX on P2P client")
  115. # make group client available on operating channe
  116. dev[1].request("REASSOCIATE")
  117. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED", "P2P-GO-NEG-REQUEST"])
  118. if ev is None:
  119. raise Exception("Timeout on reconnection to group")
  120. if "P2P-GO-NEG-REQUEST" not in ev:
  121. ev = dev[1].wait_event(["P2P-GO-NEG-REQUEST"])
  122. if ev is None:
  123. raise Exception("Timeout on waiting for GO Negotiation Request")
  124. def test_discovery_dev_type(dev):
  125. """P2P device discovery with Device Type filter"""
  126. dev[1].request("SET sec_device_type 1-0050F204-2")
  127. dev[1].p2p_listen()
  128. dev[0].p2p_find(social=True, dev_type="5-0050F204-1")
  129. ev = dev[0].wait_event(['P2P-DEVICE-FOUND'], timeout=1)
  130. if ev:
  131. raise Exception("Unexpected P2P device found")
  132. dev[0].p2p_find(social=True, dev_type="1-0050F204-2")
  133. ev = dev[0].wait_event(['P2P-DEVICE-FOUND'], timeout=2)
  134. if ev is None:
  135. raise Exception("P2P device not found")
  136. def test_discovery_dev_type_go(dev):
  137. """P2P device discovery with Device Type filter on GO"""
  138. addr1 = dev[1].p2p_dev_addr()
  139. dev[1].request("SET sec_device_type 1-0050F204-2")
  140. res = dev[0].p2p_start_go(freq="2412")
  141. pin = dev[1].wps_read_pin()
  142. dev[0].p2p_go_authorize_client(pin)
  143. dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
  144. dev[2].p2p_find(social=True, dev_type="5-0050F204-1")
  145. ev = dev[2].wait_event(['P2P-DEVICE-FOUND'], timeout=1)
  146. if ev:
  147. raise Exception("Unexpected P2P device found")
  148. dev[2].p2p_find(social=True, dev_type="1-0050F204-2")
  149. ev = dev[2].wait_event(['P2P-DEVICE-FOUND ' + addr1], timeout=2)
  150. if ev is None:
  151. raise Exception("P2P device not found")
  152. def test_discovery_dev_id(dev):
  153. """P2P device discovery with Device ID filter"""
  154. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  155. wpas.interface_add("wlan5")
  156. wpas.request("P2P_LISTEN 1")
  157. status = wpas.global_request("STATUS")
  158. if "p2p_state=LISTEN_ONLY" not in status:
  159. raise Exception("Unexpected status: " + status)
  160. addr1 = dev[1].p2p_dev_addr()
  161. dev[1].p2p_listen()
  162. dev[0].p2p_find(social=True, dev_id="02:03:04:05:06:07")
  163. ev = dev[0].wait_event(['P2P-DEVICE-FOUND'], timeout=1)
  164. if ev:
  165. raise Exception("Unexpected P2P device found")
  166. dev[0].p2p_find(social=True, dev_id=addr1)
  167. ev = dev[0].wait_event(['P2P-DEVICE-FOUND'], timeout=2)
  168. if ev is None:
  169. raise Exception("P2P device not found")
  170. if addr1 not in ev:
  171. raise Exception("Unexpected P2P peer found")
  172. status = wpas.global_request("STATUS")
  173. for i in range(0, 2):
  174. if "p2p_state=IDLE" in status:
  175. break
  176. time.sleep(0.5)
  177. status = wpas.global_request("STATUS")
  178. if "p2p_state=IDLE" not in status:
  179. raise Exception("Unexpected status: " + status)
  180. def test_discovery_dev_id_go(dev):
  181. """P2P device discovery with Device ID filter on GO"""
  182. addr1 = dev[1].p2p_dev_addr()
  183. res = dev[0].p2p_start_go(freq="2412")
  184. pin = dev[1].wps_read_pin()
  185. dev[0].p2p_go_authorize_client(pin)
  186. dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
  187. dev[2].p2p_find(social=True, dev_id="02:03:04:05:06:07")
  188. ev = dev[2].wait_event(['P2P-DEVICE-FOUND'], timeout=1)
  189. if ev:
  190. raise Exception("Unexpected P2P device found")
  191. dev[2].p2p_find(social=True, dev_id=addr1)
  192. ev = dev[2].wait_event(['P2P-DEVICE-FOUND ' + addr1], timeout=2)
  193. if ev is None:
  194. raise Exception("P2P device not found")