test_p2p_discovery.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 time
  9. import hwsim_utils
  10. from wpasupplicant import WpaSupplicant
  11. def test_discovery(dev):
  12. """P2P device discovery and provision discovery"""
  13. addr0 = dev[0].p2p_dev_addr()
  14. addr1 = dev[1].p2p_dev_addr()
  15. logger.info("Start device discovery")
  16. dev[0].p2p_find(social=True)
  17. if not dev[1].discover_peer(addr0):
  18. raise Exception("Device discovery timed out")
  19. if not dev[0].discover_peer(addr1):
  20. raise Exception("Device discovery timed out")
  21. logger.info("Test provision discovery for display")
  22. dev[0].global_request("P2P_PROV_DISC " + addr1 + " display")
  23. ev1 = dev[1].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=15)
  24. if ev1 is None:
  25. raise Exception("Provision discovery timed out (display/dev1)")
  26. if addr0 not in ev1:
  27. raise Exception("Dev0 not in provision discovery event")
  28. ev0 = dev[0].wait_global_event(["P2P-PROV-DISC-ENTER-PIN",
  29. "P2P-PROV-DISC-FAILURE"], timeout=15)
  30. if ev0 is None:
  31. raise Exception("Provision discovery timed out (display/dev0)")
  32. if "P2P-PROV-DISC-FAILURE" in ev0:
  33. raise Exception("Provision discovery failed (display/dev0)")
  34. if addr1 not in ev0:
  35. raise Exception("Dev1 not in provision discovery event")
  36. logger.info("Test provision discovery for keypad")
  37. dev[0].global_request("P2P_PROV_DISC " + addr1 + " keypad")
  38. ev1 = dev[1].wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout=15)
  39. if ev1 is None:
  40. raise Exception("Provision discovery timed out (keypad/dev1)")
  41. if addr0 not in ev1:
  42. raise Exception("Dev0 not in provision discovery event")
  43. ev0 = dev[0].wait_global_event(["P2P-PROV-DISC-SHOW-PIN",
  44. "P2P-PROV-DISC-FAILURE"],
  45. timeout=15)
  46. if ev0 is None:
  47. raise Exception("Provision discovery timed out (keypad/dev0)")
  48. if "P2P-PROV-DISC-FAILURE" in ev0:
  49. raise Exception("Provision discovery failed (keypad/dev0)")
  50. if addr1 not in ev0:
  51. raise Exception("Dev1 not in provision discovery event")
  52. logger.info("Test provision discovery for push button")
  53. dev[0].global_request("P2P_PROV_DISC " + addr1 + " pbc")
  54. ev1 = dev[1].wait_global_event(["P2P-PROV-DISC-PBC-REQ"], timeout=15)
  55. if ev1 is None:
  56. raise Exception("Provision discovery timed out (pbc/dev1)")
  57. if addr0 not in ev1:
  58. raise Exception("Dev0 not in provision discovery event")
  59. ev0 = dev[0].wait_global_event(["P2P-PROV-DISC-PBC-RESP",
  60. "P2P-PROV-DISC-FAILURE"],
  61. timeout=15)
  62. if ev0 is None:
  63. raise Exception("Provision discovery timed out (pbc/dev0)")
  64. if "P2P-PROV-DISC-FAILURE" in ev0:
  65. raise Exception("Provision discovery failed (pbc/dev0)")
  66. if addr1 not in ev0:
  67. raise Exception("Dev1 not in provision discovery event")
  68. dev[0].p2p_stop_find
  69. dev[1].p2p_stop_find
  70. def test_discovery_pd_retries(dev):
  71. """P2P device discovery and provision discovery retries"""
  72. addr0 = dev[0].p2p_dev_addr()
  73. addr1 = dev[1].p2p_dev_addr()
  74. dev[1].p2p_listen()
  75. if not dev[0].discover_peer(addr1):
  76. raise Exception("Device discovery timed out")
  77. dev[1].p2p_stop_find()
  78. dev[0].p2p_stop_find()
  79. dev[0].global_request("P2P_PROV_DISC " + addr1 + " display")
  80. ev = dev[0].wait_event(["P2P-PROV-DISC-FAILURE"], timeout=60)
  81. if ev is None:
  82. raise Exception("No PD failure reported")
  83. def test_discovery_group_client(dev):
  84. """P2P device discovery for a client in a group"""
  85. logger.info("Start autonomous GO " + dev[0].ifname)
  86. res = dev[0].p2p_start_go(freq="2422")
  87. logger.debug("res: " + str(res))
  88. logger.info("Connect a client to the GO")
  89. pin = dev[1].wps_read_pin()
  90. dev[0].p2p_go_authorize_client(pin)
  91. dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
  92. logger.info("Client connected")
  93. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  94. logger.info("Try to discover a P2P client in a group")
  95. if not dev[2].discover_peer(dev[1].p2p_dev_addr(), social=False, timeout=10):
  96. if not dev[2].discover_peer(dev[1].p2p_dev_addr(), social=False, timeout=10):
  97. if not dev[2].discover_peer(dev[1].p2p_dev_addr(), social=False, timeout=10):
  98. raise Exception("Could not discover group client")
  99. # This is not really perfect, but something to get a bit more testing
  100. # coverage.. For proper discoverability mechanism validation, the P2P
  101. # client would need to go to sleep to avoid acknowledging the GO Negotiation
  102. # Request frame. Offchannel Listen mode operation on the P2P Client with
  103. # mac80211_hwsim is apparently not enough to avoid the acknowledgement on
  104. # the operating channel, so need to disconnect from the group which removes
  105. # the GO-to-P2P Client part of the discoverability exchange in practice.
  106. pin = dev[2].wps_read_pin()
  107. # make group client non-responsive on operating channel
  108. dev[1].dump_monitor()
  109. dev[1].group_request("DISCONNECT")
  110. ev = dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  111. if ev is None:
  112. raise Exception("Timeout on waiting disconnection")
  113. dev[2].request("P2P_CONNECT {} {} display".format(dev[1].p2p_dev_addr(),
  114. pin))
  115. ev = dev[1].wait_event(["P2P-GO-NEG-REQUEST"], timeout=2)
  116. if ev:
  117. raise Exception("Unexpected frame RX on P2P client")
  118. # make group client available on operating channe
  119. dev[1].request("REASSOCIATE")
  120. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED", "P2P-GO-NEG-REQUEST"])
  121. if ev is None:
  122. raise Exception("Timeout on reconnection to group")
  123. if "P2P-GO-NEG-REQUEST" not in ev:
  124. ev = dev[1].wait_event(["P2P-GO-NEG-REQUEST"])
  125. if ev is None:
  126. raise Exception("Timeout on waiting for GO Negotiation Request")
  127. def test_discovery_dev_type(dev):
  128. """P2P device discovery with Device Type filter"""
  129. dev[1].request("SET sec_device_type 1-0050F204-2")
  130. dev[1].p2p_listen()
  131. dev[0].p2p_find(social=True, dev_type="5-0050F204-1")
  132. ev = dev[0].wait_event(['P2P-DEVICE-FOUND'], timeout=1)
  133. if ev:
  134. raise Exception("Unexpected P2P device found")
  135. dev[0].p2p_find(social=True, dev_type="1-0050F204-2")
  136. ev = dev[0].wait_event(['P2P-DEVICE-FOUND'], timeout=2)
  137. if ev is None:
  138. raise Exception("P2P device not found")
  139. peer = dev[0].get_peer(dev[1].p2p_dev_addr())
  140. if "1-0050F204-2" not in peer['sec_dev_type']:
  141. raise Exception("sec_device_type not reported properly")
  142. def test_discovery_dev_type_go(dev):
  143. """P2P device discovery with Device Type filter on GO"""
  144. addr1 = dev[1].p2p_dev_addr()
  145. dev[1].request("SET sec_device_type 1-0050F204-2")
  146. res = dev[0].p2p_start_go(freq="2412")
  147. pin = dev[1].wps_read_pin()
  148. dev[0].p2p_go_authorize_client(pin)
  149. dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
  150. dev[2].p2p_find(social=True, dev_type="5-0050F204-1")
  151. ev = dev[2].wait_event(['P2P-DEVICE-FOUND'], timeout=1)
  152. if ev:
  153. raise Exception("Unexpected P2P device found")
  154. dev[2].p2p_find(social=True, dev_type="1-0050F204-2")
  155. ev = dev[2].wait_event(['P2P-DEVICE-FOUND ' + addr1], timeout=2)
  156. if ev is None:
  157. raise Exception("P2P device not found")
  158. def test_discovery_dev_id(dev):
  159. """P2P device discovery with Device ID filter"""
  160. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  161. wpas.interface_add("wlan5")
  162. wpas.request("P2P_LISTEN 1")
  163. status = wpas.global_request("STATUS")
  164. if "p2p_state=LISTEN_ONLY" not in status:
  165. raise Exception("Unexpected status: " + status)
  166. addr1 = dev[1].p2p_dev_addr()
  167. dev[1].p2p_listen()
  168. dev[0].p2p_find(social=True, dev_id="02:03:04:05:06:07")
  169. ev = dev[0].wait_event(['P2P-DEVICE-FOUND'], timeout=1)
  170. if ev:
  171. raise Exception("Unexpected P2P device found")
  172. dev[0].p2p_find(social=True, dev_id=addr1)
  173. ev = dev[0].wait_event(['P2P-DEVICE-FOUND'], timeout=2)
  174. if ev is None:
  175. raise Exception("P2P device not found")
  176. if addr1 not in ev:
  177. raise Exception("Unexpected P2P peer found")
  178. status = wpas.global_request("STATUS")
  179. for i in range(0, 2):
  180. if "p2p_state=IDLE" in status:
  181. break
  182. time.sleep(0.5)
  183. status = wpas.global_request("STATUS")
  184. if "p2p_state=IDLE" not in status:
  185. raise Exception("Unexpected status: " + status)
  186. def test_discovery_dev_id_go(dev):
  187. """P2P device discovery with Device ID filter on GO"""
  188. addr1 = dev[1].p2p_dev_addr()
  189. res = dev[0].p2p_start_go(freq="2412")
  190. pin = dev[1].wps_read_pin()
  191. dev[0].p2p_go_authorize_client(pin)
  192. dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
  193. dev[2].p2p_find(social=True, dev_id="02:03:04:05:06:07")
  194. ev = dev[2].wait_event(['P2P-DEVICE-FOUND'], timeout=1)
  195. if ev:
  196. raise Exception("Unexpected P2P device found")
  197. dev[2].p2p_find(social=True, dev_id=addr1)
  198. ev = dev[2].wait_event(['P2P-DEVICE-FOUND ' + addr1], timeout=2)
  199. if ev is None:
  200. raise Exception("P2P device not found")
  201. def test_discovery_social_plus_one(dev):
  202. """P2P device discovery with social-plus-one"""
  203. logger.info("Start autonomous GO " + dev[0].ifname)
  204. dev[1].p2p_find(social=True)
  205. dev[0].p2p_find(progressive=True)
  206. logger.info("Wait for initial progressive find phases")
  207. dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  208. dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  209. go = dev[2].p2p_dev_addr()
  210. dev[2].p2p_start_go(freq="2422")
  211. logger.info("Verify whether the GO on non-social channel can be found")
  212. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=15)
  213. if ev is None:
  214. raise Exception("Peer not found")
  215. if go not in ev:
  216. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=15)
  217. if ev is None:
  218. raise Exception("Peer not found")
  219. ev = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=15)
  220. if ev is None:
  221. raise Exception("Peer not found")
  222. dev[0].p2p_stop_find()
  223. dev[1].p2p_stop_find()
  224. if not dev[0].peer_known(go):
  225. raise Exception("GO not found in progressive scan")
  226. if dev[1].peer_known(go):
  227. raise Exception("GO found in social-only scan")
  228. def test_discovery_and_interface_disabled(dev):
  229. """P2P device discovery with interface getting didabled"""
  230. try:
  231. if "OK" not in dev[0].p2p_find():
  232. raise Exception("Failed to start P2P find")
  233. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  234. if ev is None:
  235. raise Exception("Scan did not start")
  236. dev[0].request("DRIVER_EVENT INTERFACE_DISABLED")
  237. time.sleep(1)
  238. # verify that P2P_FIND is rejected
  239. if "FAIL" not in dev[0].p2p_find():
  240. raise Exception("New P2P_FIND request was accepted unexpectedly")
  241. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  242. time.sleep(3)
  243. dev[0].scan(freq="2412")
  244. if "OK" not in dev[0].p2p_find():
  245. raise Exception("Failed to start P2P find")
  246. dev[0].dump_monitor()
  247. dev[1].p2p_listen()
  248. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=15)
  249. if ev is None:
  250. raise Exception("Peer not found")
  251. finally:
  252. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")