test_p2p_discovery.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. from remotehost import remote_compatible
  7. import logging
  8. logger = logging.getLogger()
  9. import os
  10. import time
  11. import hwsim_utils
  12. from wpasupplicant import WpaSupplicant
  13. from p2p_utils import *
  14. from hwsim import HWSimRadio
  15. from tshark import run_tshark
  16. from test_gas import start_ap
  17. from test_cfg80211 import nl80211_remain_on_channel
  18. from test_p2p_channel import set_country
  19. @remote_compatible
  20. def test_discovery(dev):
  21. """P2P device discovery and provision discovery"""
  22. addr0 = dev[0].p2p_dev_addr()
  23. addr1 = dev[1].p2p_dev_addr()
  24. logger.info("Start device discovery")
  25. dev[0].p2p_find(social=True, delay=1)
  26. if not dev[1].discover_peer(addr0):
  27. raise Exception("Device discovery timed out")
  28. if not dev[0].discover_peer(addr1):
  29. raise Exception("Device discovery timed out")
  30. logger.info("Test provision discovery for display")
  31. dev[0].global_request("P2P_PROV_DISC " + addr1 + " display")
  32. ev1 = dev[1].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=15)
  33. if ev1 is None:
  34. raise Exception("Provision discovery timed out (display/dev1)")
  35. if addr0 not in ev1:
  36. raise Exception("Dev0 not in provision discovery event")
  37. ev0 = dev[0].wait_global_event(["P2P-PROV-DISC-ENTER-PIN",
  38. "P2P-PROV-DISC-FAILURE"], timeout=15)
  39. if ev0 is None:
  40. raise Exception("Provision discovery timed out (display/dev0)")
  41. if "P2P-PROV-DISC-FAILURE" in ev0:
  42. raise Exception("Provision discovery failed (display/dev0)")
  43. if addr1 not in ev0:
  44. raise Exception("Dev1 not in provision discovery event")
  45. logger.info("Test provision discovery for keypad")
  46. dev[0].global_request("P2P_PROV_DISC " + addr1 + " keypad")
  47. ev1 = dev[1].wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout=15)
  48. if ev1 is None:
  49. raise Exception("Provision discovery timed out (keypad/dev1)")
  50. if addr0 not in ev1:
  51. raise Exception("Dev0 not in provision discovery event")
  52. ev0 = dev[0].wait_global_event(["P2P-PROV-DISC-SHOW-PIN",
  53. "P2P-PROV-DISC-FAILURE"],
  54. timeout=15)
  55. if ev0 is None:
  56. raise Exception("Provision discovery timed out (keypad/dev0)")
  57. if "P2P-PROV-DISC-FAILURE" in ev0:
  58. raise Exception("Provision discovery failed (keypad/dev0)")
  59. if addr1 not in ev0:
  60. raise Exception("Dev1 not in provision discovery event")
  61. logger.info("Test provision discovery for push button")
  62. dev[0].global_request("P2P_PROV_DISC " + addr1 + " pbc")
  63. ev1 = dev[1].wait_global_event(["P2P-PROV-DISC-PBC-REQ"], timeout=15)
  64. if ev1 is None:
  65. raise Exception("Provision discovery timed out (pbc/dev1)")
  66. if addr0 not in ev1:
  67. raise Exception("Dev0 not in provision discovery event")
  68. ev0 = dev[0].wait_global_event(["P2P-PROV-DISC-PBC-RESP",
  69. "P2P-PROV-DISC-FAILURE"],
  70. timeout=15)
  71. if ev0 is None:
  72. raise Exception("Provision discovery timed out (pbc/dev0)")
  73. if "P2P-PROV-DISC-FAILURE" in ev0:
  74. raise Exception("Provision discovery failed (pbc/dev0)")
  75. if addr1 not in ev0:
  76. raise Exception("Dev1 not in provision discovery event")
  77. dev[0].p2p_stop_find
  78. dev[1].p2p_stop_find
  79. if "FAIL" not in dev[0].p2p_find(dev_id="foo"):
  80. raise Exception("P2P_FIND with invalid dev_id accepted")
  81. if "FAIL" not in dev[0].p2p_find(dev_type="foo"):
  82. raise Exception("P2P_FIND with invalid dev_type accepted")
  83. if "FAIL" not in dev[0].p2p_find(dev_type="1-foo-2"):
  84. raise Exception("P2P_FIND with invalid dev_type accepted")
  85. if "FAIL" not in dev[0].p2p_find(dev_type="1-11223344"):
  86. raise Exception("P2P_FIND with invalid dev_type accepted")
  87. if "FAIL" not in dev[0].global_request("P2P_PROV_DISC foo pbc"):
  88. raise Exception("Invalid P2P_PROV_DISC accepted")
  89. if "FAIL" not in dev[0].global_request("P2P_PROV_DISC 00:11:22:33:44:55"):
  90. raise Exception("Invalid P2P_PROV_DISC accepted")
  91. if "FAIL" not in dev[0].global_request("P2P_PROV_DISC 00:11:22:33:44:55 pbc join"):
  92. raise Exception("Invalid P2P_PROV_DISC accepted")
  93. if "FAIL" not in dev[0].global_request("P2P_PROV_DISC 00:11:22:33:44:55 foo"):
  94. raise Exception("Invalid P2P_PROV_DISC accepted")
  95. @remote_compatible
  96. def test_discovery_pd_retries(dev):
  97. """P2P device discovery and provision discovery retries"""
  98. addr0 = dev[0].p2p_dev_addr()
  99. addr1 = dev[1].p2p_dev_addr()
  100. dev[1].p2p_listen()
  101. if not dev[0].discover_peer(addr1):
  102. raise Exception("Device discovery timed out")
  103. dev[1].p2p_stop_find()
  104. dev[0].p2p_stop_find()
  105. dev[0].global_request("P2P_PROV_DISC " + addr1 + " display")
  106. ev = dev[0].wait_global_event(["P2P-PROV-DISC-FAILURE"], timeout=60)
  107. if ev is None:
  108. raise Exception("No PD failure reported")
  109. def test_discovery_group_client(dev):
  110. """P2P device discovery for a client in a group"""
  111. logger.info("Start autonomous GO " + dev[0].ifname)
  112. res = dev[0].p2p_start_go(freq="2422")
  113. logger.debug("res: " + str(res))
  114. logger.info("Connect a client to the GO")
  115. pin = dev[1].wps_read_pin()
  116. dev[0].p2p_go_authorize_client(pin)
  117. dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), pin, freq=int(res['freq']),
  118. timeout=60)
  119. logger.info("Client connected")
  120. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  121. logger.info("Try to discover a P2P client in a group")
  122. if not dev[2].discover_peer(dev[1].p2p_dev_addr(), social=False, timeout=10):
  123. stop_p2p_find_and_wait(dev[2])
  124. if not dev[2].discover_peer(dev[1].p2p_dev_addr(), social=False, timeout=10):
  125. stop_p2p_find_and_wait(dev[2])
  126. if not dev[2].discover_peer(dev[1].p2p_dev_addr(), social=False, timeout=10):
  127. raise Exception("Could not discover group client")
  128. # This is not really perfect, but something to get a bit more testing
  129. # coverage.. For proper discoverability mechanism validation, the P2P
  130. # client would need to go to sleep to avoid acknowledging the GO Negotiation
  131. # Request frame. Offchannel Listen mode operation on the P2P Client with
  132. # mac80211_hwsim is apparently not enough to avoid the acknowledgement on
  133. # the operating channel, so need to disconnect from the group which removes
  134. # the GO-to-P2P Client part of the discoverability exchange in practice.
  135. pin = dev[2].wps_read_pin()
  136. # make group client non-responsive on operating channel
  137. dev[1].dump_monitor()
  138. dev[1].group_request("DISCONNECT")
  139. ev = dev[1].wait_group_event(["CTRL-EVENT-DISCONNECTED"], timeout=10)
  140. if ev is None:
  141. raise Exception("Timeout on waiting disconnection")
  142. dev[2].request("P2P_CONNECT {} {} display".format(dev[1].p2p_dev_addr(),
  143. pin))
  144. ev = dev[1].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=2)
  145. if ev:
  146. raise Exception("Unexpected frame RX on P2P client")
  147. # make group client available on operating channe
  148. dev[1].group_request("REASSOCIATE")
  149. ev = dev[1].wait_global_event(["CTRL-EVENT-CONNECTED",
  150. "P2P-GO-NEG-REQUEST"], timeout=10)
  151. if ev is None:
  152. raise Exception("Timeout on reconnection to group")
  153. if "P2P-GO-NEG-REQUEST" not in ev:
  154. ev = dev[1].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=10)
  155. if ev is None:
  156. raise Exception("Timeout on waiting for GO Negotiation Request")
  157. def stop_p2p_find_and_wait(dev):
  158. dev.request("P2P_STOP_FIND")
  159. for i in range(10):
  160. res = dev.get_driver_status_field("scan_state")
  161. if "SCAN_STARTED" not in res and "SCAN_REQUESTED" not in res:
  162. break
  163. logger.debug("Waiting for final P2P_FIND scan to complete")
  164. time.sleep(0.02)
  165. def test_discovery_ctrl_char_in_devname(dev):
  166. """P2P device discovery and control character in Device Name"""
  167. try:
  168. _test_discovery_ctrl_char_in_devname(dev)
  169. finally:
  170. dev[1].global_request("SET device_name Device B")
  171. def _test_discovery_ctrl_char_in_devname(dev):
  172. dev[1].global_request("SET device_name Device\tB")
  173. addr0 = dev[0].p2p_dev_addr()
  174. addr1 = dev[1].p2p_dev_addr()
  175. res = dev[0].p2p_start_go(freq=2422)
  176. bssid = dev[0].p2p_interface_addr()
  177. pin = dev[1].wps_read_pin()
  178. dev[0].p2p_go_authorize_client(pin)
  179. dev[1].scan_for_bss(bssid, freq=2422)
  180. dev[1].p2p_connect_group(addr0, pin, timeout=60, freq=2422)
  181. if not dev[2].discover_peer(addr1, social=False, freq=2422, timeout=5):
  182. stop_p2p_find_and_wait(dev[2])
  183. if not dev[2].discover_peer(addr1, social=False, freq=2422, timeout=5):
  184. stop_p2p_find_and_wait(dev[2])
  185. if not dev[2].discover_peer(addr1, social=False, freq=2422,
  186. timeout=5):
  187. raise Exception("Could not discover group client")
  188. devname = dev[2].get_peer(addr1)['device_name']
  189. dev[2].p2p_stop_find()
  190. if devname != "Device_B":
  191. raise Exception("Unexpected device_name from group client: " + devname)
  192. terminate_group(dev[0], dev[1])
  193. dev[2].request("P2P_FLUSH")
  194. dev[1].p2p_listen()
  195. if not dev[2].discover_peer(addr1, social=True, timeout=10):
  196. raise Exception("Could not discover peer")
  197. devname = dev[2].get_peer(addr1)['device_name']
  198. dev[2].p2p_stop_find()
  199. if devname != "Device_B":
  200. raise Exception("Unexpected device_name from peer: " + devname)
  201. @remote_compatible
  202. def test_discovery_dev_type(dev):
  203. """P2P device discovery with Device Type filter"""
  204. dev[1].request("SET sec_device_type 1-0050F204-2")
  205. dev[1].p2p_listen()
  206. dev[0].p2p_find(social=True, dev_type="5-0050F204-1")
  207. ev = dev[0].wait_global_event(['P2P-DEVICE-FOUND'], timeout=1)
  208. if ev:
  209. raise Exception("Unexpected P2P device found")
  210. dev[0].p2p_find(social=True, dev_type="1-0050F204-2")
  211. ev = dev[0].wait_global_event(['P2P-DEVICE-FOUND'], timeout=2)
  212. if ev is None:
  213. raise Exception("P2P device not found")
  214. peer = dev[0].get_peer(dev[1].p2p_dev_addr())
  215. if "1-0050F204-2" not in peer['sec_dev_type']:
  216. raise Exception("sec_device_type not reported properly")
  217. def test_discovery_dev_type_go(dev):
  218. """P2P device discovery with Device Type filter on GO"""
  219. addr1 = dev[1].p2p_dev_addr()
  220. dev[1].request("SET sec_device_type 1-0050F204-2")
  221. res = dev[0].p2p_start_go(freq="2412")
  222. pin = dev[1].wps_read_pin()
  223. dev[0].p2p_go_authorize_client(pin)
  224. dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
  225. dev[2].p2p_find(social=True, dev_type="5-0050F204-1")
  226. ev = dev[2].wait_global_event(['P2P-DEVICE-FOUND'], timeout=1)
  227. if ev:
  228. raise Exception("Unexpected P2P device found")
  229. dev[2].p2p_find(social=True, dev_type="1-0050F204-2")
  230. ev = dev[2].wait_global_event(['P2P-DEVICE-FOUND ' + addr1], timeout=2)
  231. if ev is None:
  232. raise Exception("P2P device not found")
  233. def test_discovery_dev_id(dev):
  234. """P2P device discovery with Device ID filter"""
  235. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  236. wpas.interface_add("wlan5")
  237. wpas.request("P2P_LISTEN 1")
  238. status = wpas.global_request("STATUS")
  239. if "p2p_state=LISTEN_ONLY" not in status:
  240. raise Exception("Unexpected status: " + status)
  241. addr1 = dev[1].p2p_dev_addr()
  242. dev[1].p2p_listen()
  243. dev[0].p2p_find(social=True, dev_id="02:03:04:05:06:07")
  244. ev = dev[0].wait_global_event(['P2P-DEVICE-FOUND'], timeout=1)
  245. if ev:
  246. raise Exception("Unexpected P2P device found")
  247. dev[0].p2p_find(social=True, dev_id=addr1)
  248. ev = dev[0].wait_global_event(['P2P-DEVICE-FOUND'], timeout=5)
  249. if ev is None:
  250. raise Exception("P2P device not found")
  251. if addr1 not in ev:
  252. raise Exception("Unexpected P2P peer found")
  253. status = wpas.global_request("STATUS")
  254. for i in range(0, 2):
  255. if "p2p_state=IDLE" in status:
  256. break
  257. time.sleep(0.5)
  258. status = wpas.global_request("STATUS")
  259. if "p2p_state=IDLE" not in status:
  260. raise Exception("Unexpected status: " + status)
  261. def test_discovery_dev_id_go(dev):
  262. """P2P device discovery with Device ID filter on GO"""
  263. addr1 = dev[1].p2p_dev_addr()
  264. res = dev[0].p2p_start_go(freq="2412")
  265. pin = dev[1].wps_read_pin()
  266. dev[0].p2p_go_authorize_client(pin)
  267. dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
  268. dev[2].p2p_find(social=True, dev_id="02:03:04:05:06:07")
  269. ev = dev[2].wait_global_event(['P2P-DEVICE-FOUND'], timeout=1)
  270. if ev:
  271. raise Exception("Unexpected P2P device found")
  272. dev[2].p2p_find(social=True, dev_id=addr1)
  273. ev = dev[2].wait_global_event(['P2P-DEVICE-FOUND ' + addr1], timeout=2)
  274. if ev is None:
  275. raise Exception("P2P device not found")
  276. def test_discovery_social_plus_one(dev):
  277. """P2P device discovery with social-plus-one"""
  278. logger.info("Start autonomous GO " + dev[0].ifname)
  279. dev[1].p2p_find(social=True)
  280. dev[0].p2p_find(progressive=True)
  281. logger.info("Wait for initial progressive find phases")
  282. dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  283. dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  284. go = dev[2].p2p_dev_addr()
  285. dev[2].p2p_start_go(freq="2422")
  286. logger.info("Verify whether the GO on non-social channel can be found")
  287. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=15)
  288. if ev is None:
  289. raise Exception("Peer not found")
  290. if go not in ev:
  291. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=15)
  292. if ev is None:
  293. raise Exception("Peer not found")
  294. ev = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=15)
  295. if ev is None:
  296. raise Exception("Peer not found")
  297. dev[0].p2p_stop_find()
  298. dev[1].p2p_stop_find()
  299. if not dev[0].peer_known(go):
  300. raise Exception("GO not found in progressive scan")
  301. if dev[1].peer_known(go):
  302. raise Exception("GO found in social-only scan")
  303. def _test_discovery_and_interface_disabled(dev, delay=1):
  304. try:
  305. if "OK" not in dev[0].p2p_find():
  306. raise Exception("Failed to start P2P find")
  307. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
  308. if ev is None:
  309. raise Exception("Scan did not start")
  310. dev[0].request("DRIVER_EVENT INTERFACE_DISABLED")
  311. time.sleep(delay)
  312. # verify that P2P_FIND is rejected
  313. if "FAIL" not in dev[0].p2p_find():
  314. raise Exception("New P2P_FIND request was accepted unexpectedly")
  315. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  316. time.sleep(3)
  317. dev[0].scan(freq="2412")
  318. if "OK" not in dev[0].p2p_find():
  319. raise Exception("Failed to start P2P find")
  320. dev[0].dump_monitor()
  321. dev[1].p2p_listen()
  322. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=15)
  323. if ev is None:
  324. raise Exception("Peer not found")
  325. finally:
  326. dev[0].request("DRIVER_EVENT INTERFACE_ENABLED")
  327. def test_discovery_and_interface_disabled(dev):
  328. """P2P device discovery with interface getting disabled"""
  329. _test_discovery_and_interface_disabled(dev, delay=1)
  330. _test_discovery_and_interface_disabled(dev, delay=5)
  331. def test_discovery_auto(dev):
  332. """P2P device discovery and provision discovery with auto GO/dev selection"""
  333. dev[0].flush_scan_cache()
  334. addr0 = dev[0].p2p_dev_addr()
  335. addr1 = dev[1].p2p_dev_addr()
  336. addr2 = dev[2].p2p_dev_addr()
  337. dev[2].p2p_start_go(freq="2412")
  338. logger.info("Start device discovery")
  339. dev[0].p2p_listen()
  340. if not dev[1].discover_peer(addr0):
  341. raise Exception("Device discovery timed out")
  342. dev[1].p2p_listen()
  343. if not dev[0].discover_peer(addr1):
  344. raise Exception("Device discovery timed out")
  345. if not dev[0].discover_peer(addr2):
  346. raise Exception("Device discovery timed out")
  347. logger.info("Test provision discovery for display (device)")
  348. dev[0].global_request("P2P_PROV_DISC " + addr1 + " display auto")
  349. ev1 = dev[1].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=15)
  350. if ev1 is None:
  351. raise Exception("Provision discovery timed out (display/dev1)")
  352. if addr0 not in ev1:
  353. raise Exception("Dev0 not in provision discovery event")
  354. if " group=" in ev1:
  355. raise Exception("Unexpected group parameter from non-GO")
  356. ev0 = dev[0].wait_global_event(["P2P-PROV-DISC-ENTER-PIN",
  357. "P2P-PROV-DISC-FAILURE"], timeout=15)
  358. if ev0 is None:
  359. raise Exception("Provision discovery timed out (display/dev0)")
  360. if "P2P-PROV-DISC-FAILURE" in ev0:
  361. raise Exception("Provision discovery failed (display/dev0)")
  362. if addr1 not in ev0:
  363. raise Exception("Dev1 not in provision discovery event")
  364. if "peer_go=0" not in ev0:
  365. raise Exception("peer_go incorrect in PD response from non-GO")
  366. logger.info("Test provision discovery for display (GO)")
  367. dev[0].global_request("P2P_PROV_DISC " + addr2 + " display auto")
  368. ev2 = dev[2].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=15)
  369. if ev2 is None:
  370. raise Exception("Provision discovery timed out (display/dev2)")
  371. if addr0 not in ev2:
  372. raise Exception("Dev0 not in provision discovery event")
  373. if " group=" not in ev2:
  374. raise Exception("Group parameter missing from GO")
  375. ev0 = dev[0].wait_global_event(["P2P-PROV-DISC-ENTER-PIN",
  376. "P2P-PROV-DISC-FAILURE"], timeout=15)
  377. if ev0 is None:
  378. raise Exception("Provision discovery timed out (display/dev0)")
  379. if "P2P-PROV-DISC-FAILURE" in ev0:
  380. raise Exception("Provision discovery failed (display/dev0)")
  381. if addr2 not in ev0:
  382. raise Exception("Dev1 not in provision discovery event")
  383. if "peer_go=1" not in ev0:
  384. raise Exception("peer_go incorrect in PD response from GO")
  385. def test_discovery_stop(dev):
  386. """P2P device discovery and p2p_stop_find"""
  387. addr0 = dev[0].p2p_dev_addr()
  388. addr1 = dev[1].p2p_dev_addr()
  389. dev[1].p2p_listen()
  390. dev[2].p2p_listen()
  391. dev[0].p2p_find(social=False)
  392. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=0.5)
  393. if ev is None:
  394. logger.info("No CTRL-EVENT-SCAN-STARTED event")
  395. dev[0].p2p_stop_find()
  396. ev = dev[0].wait_global_event(["P2P-FIND-STOPPED"], timeout=1)
  397. if ev is None:
  398. raise Exception("P2P_STOP not reported")
  399. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=5)
  400. if ev is not None:
  401. raise Exception("Peer found unexpectedly: " + ev)
  402. dev[0].p2p_find(social=False)
  403. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=0.5)
  404. if ev is None:
  405. logger.info("No CTRL-EVENT-SCAN-STARTED event")
  406. dev[0].global_request("P2P_FLUSH")
  407. ev = dev[0].wait_global_event(["P2P-FIND-STOPPED"], timeout=1)
  408. if ev is None:
  409. raise Exception("P2P_STOP not reported")
  410. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=5)
  411. if ev is not None:
  412. raise Exception("Peer found unexpectedly: " + ev)
  413. def test_discovery_restart(dev):
  414. """P2P device discovery and p2p_find restart"""
  415. autogo(dev[1], freq=2457)
  416. dev[0].p2p_find(social=True)
  417. dev[0].p2p_stop_find()
  418. dev[0].p2p_find(social=False)
  419. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=7)
  420. if ev is None:
  421. dev[0].p2p_find(social=False)
  422. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=7)
  423. if ev is None:
  424. raise Exception("Peer not found")
  425. def test_discovery_restart_progressive(dev):
  426. """P2P device discovery and p2p_find type=progressive restart"""
  427. try:
  428. set_country("US", dev[1])
  429. autogo(dev[1], freq=5805)
  430. dev[0].p2p_find(social=True)
  431. dev[0].p2p_stop_find()
  432. dev[0].p2p_find(progressive=True)
  433. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=20)
  434. dev[1].remove_group()
  435. if ev is None:
  436. raise Exception("Peer not found")
  437. finally:
  438. set_country("00")
  439. dev[1].flush_scan_cache()
  440. def test_p2p_peer_command(dev):
  441. """P2P_PEER command"""
  442. addr0 = dev[0].p2p_dev_addr()
  443. addr1 = dev[1].p2p_dev_addr()
  444. addr2 = dev[2].p2p_dev_addr()
  445. dev[1].p2p_listen()
  446. dev[2].p2p_listen()
  447. if not dev[0].discover_peer(addr1):
  448. raise Exception("Device discovery timed out")
  449. if not dev[0].discover_peer(addr2):
  450. raise Exception("Device discovery timed out")
  451. dev[0].p2p_stop_find()
  452. dev[1].p2p_stop_find()
  453. dev[2].p2p_stop_find()
  454. res0 = dev[0].request("P2P_PEER FIRST")
  455. peer = res0.splitlines()[0]
  456. if peer not in [ addr1, addr2 ]:
  457. raise Exception("Unexpected P2P_PEER FIRST address")
  458. res1 = dev[0].request("P2P_PEER NEXT-" + peer)
  459. peer2 = res1.splitlines()[0]
  460. if peer2 not in [ addr1, addr2 ] or peer == peer2:
  461. raise Exception("Unexpected P2P_PEER NEXT address")
  462. if "FAIL" not in dev[0].request("P2P_PEER NEXT-foo"):
  463. raise Exception("Invalid P2P_PEER command accepted")
  464. if "FAIL" not in dev[0].request("P2P_PEER foo"):
  465. raise Exception("Invalid P2P_PEER command accepted")
  466. if "FAIL" not in dev[0].request("P2P_PEER 00:11:22:33:44:55"):
  467. raise Exception("P2P_PEER command for unknown peer accepted")
  468. def test_p2p_listen_and_offchannel_tx(dev):
  469. """P2P_LISTEN behavior with offchannel TX"""
  470. addr0 = dev[0].p2p_dev_addr()
  471. addr1 = dev[1].p2p_dev_addr()
  472. addr2 = dev[2].p2p_dev_addr()
  473. dev[1].p2p_listen()
  474. if not dev[0].discover_peer(addr1):
  475. raise Exception("Device discovery timed out")
  476. dev[0].p2p_listen()
  477. dev[0].global_request("P2P_PROV_DISC " + addr1 + " display")
  478. ev = dev[0].wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout=15)
  479. if ev is None:
  480. raise Exception("No PD result reported")
  481. dev[1].p2p_stop_find()
  482. if not dev[2].discover_peer(addr0):
  483. raise Exception("Device discovery timed out after PD exchange")
  484. dev[2].p2p_stop_find()
  485. dev[0].p2p_stop_find()
  486. @remote_compatible
  487. def test_p2p_listen_and_scan(dev):
  488. """P2P_LISTEN and scan"""
  489. dev[0].p2p_listen()
  490. if "OK" not in dev[0].request("SCAN freq=2412"):
  491. raise Exception("Failed to request a scan")
  492. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 3)
  493. if ev is not None:
  494. raise Exception("Unexpected scan results")
  495. dev[0].p2p_stop_find()
  496. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
  497. if ev is None:
  498. raise Exception("Scan timed out")
  499. def test_p2p_config_methods(dev):
  500. """P2P and WPS config method update"""
  501. addr0 = dev[0].p2p_dev_addr()
  502. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  503. wpas.interface_add("wlan5")
  504. addr1 = wpas.p2p_dev_addr()
  505. if "OK" not in wpas.request("SET config_methods keypad virtual_push_button"):
  506. raise Exception("Failed to set config_methods")
  507. wpas.p2p_listen()
  508. if not dev[0].discover_peer(addr1):
  509. raise Exception("Device discovery timed out")
  510. dev[0].p2p_stop_find()
  511. peer = dev[0].get_peer(addr1)
  512. if peer['config_methods'] != '0x180':
  513. raise Exception("Unexpected peer config methods(1): " + peer['config_methods'])
  514. dev[0].global_request("P2P_FLUSH")
  515. if "OK" not in wpas.request("SET config_methods virtual_display"):
  516. raise Exception("Failed to set config_methods")
  517. if not dev[0].discover_peer(addr1):
  518. raise Exception("Device discovery timed out")
  519. dev[0].p2p_stop_find()
  520. peer = dev[0].get_peer(addr1)
  521. if peer['config_methods'] != '0x8':
  522. raise Exception("Unexpected peer config methods(2): " + peer['config_methods'])
  523. wpas.p2p_stop_find()
  524. @remote_compatible
  525. def test_discovery_after_gas(dev, apdev):
  526. """P2P device discovery after GAS/ANQP exchange"""
  527. hapd = start_ap(apdev[0])
  528. hapd.set("gas_frag_limit", "50")
  529. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  530. dev[0].request("FETCH_ANQP")
  531. ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
  532. if ev is None:
  533. raise Exception("No ANQP-QUERY-DONE event")
  534. dev[0].dump_monitor()
  535. start = os.times()[4]
  536. dev[0].p2p_listen()
  537. dev[1].p2p_find(social=True)
  538. ev = dev[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout=5)
  539. if ev is None:
  540. raise Exception("Peer not discovered")
  541. end = os.times()[4]
  542. dev[0].dump_monitor()
  543. dev[0].p2p_stop_find()
  544. dev[1].p2p_stop_find()
  545. logger.info("Device discovery after fragmented GAS took %f seconds" % (end - start))
  546. if end - start > 1.3:
  547. raise Exception("Device discovery took unexpectedly long time")
  548. @remote_compatible
  549. def test_discovery_listen_find(dev):
  550. """P2P_LISTEN immediately followed by P2P_FIND"""
  551. # Request an external remain-on-channel operation to delay start of the ROC
  552. # for the following p2p_listen() enough to get p2p_find() processed before
  553. # the ROC started event shows up. This is done to test a code path where the
  554. # p2p_find() needs to clear the wait for the pending listen operation
  555. # (p2p->pending_listen_freq).
  556. ifindex = int(dev[0].get_driver_status_field("ifindex"))
  557. nl80211_remain_on_channel(dev[0], ifindex, 2417, 200)
  558. addr0 = dev[0].p2p_dev_addr()
  559. dev[0].p2p_listen()
  560. dev[0].p2p_find(social=True)
  561. time.sleep(0.4)
  562. dev[1].p2p_listen()
  563. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=1.2)
  564. if not dev[1].discover_peer(addr0):
  565. raise Exception("Device discovery timed out")
  566. if ev is None:
  567. raise Exception("Did not find peer quickly enough after stopped P2P_LISTEN")
  568. def test_discovery_long_listen(dev):
  569. """Long P2P_LISTEN and offchannel TX"""
  570. addr0 = dev[0].p2p_dev_addr()
  571. dev[0].p2p_listen()
  572. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  573. wpas.interface_add("wlan5")
  574. addr = wpas.p2p_dev_addr()
  575. if not wpas.discover_peer(addr0):
  576. raise Exception("Device discovery timed out")
  577. peer = wpas.get_peer(addr0)
  578. chan = '1' if peer['listen_freq'] == '2462' else '11'
  579. wpas.request("P2P_SET listen_channel " + chan)
  580. wpas.request("P2P_LISTEN 10")
  581. if not dev[0].discover_peer(addr):
  582. raise Exception("Device discovery timed out (2)")
  583. time.sleep(0.1)
  584. wpas.global_request("P2P_PROV_DISC " + addr0 + " display")
  585. ev = dev[0].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=15)
  586. if ev is None:
  587. raise Exception("Provision discovery timed out")
  588. dev[0].p2p_stop_find()
  589. # Verify that the long listen period is still continuing after off-channel
  590. # TX of Provision Discovery frames.
  591. if not dev[1].discover_peer(addr):
  592. raise Exception("Device discovery timed out (3)")
  593. dev[1].p2p_stop_find()
  594. wpas.p2p_stop_find()
  595. def pd_test(dev, addr):
  596. if not dev.discover_peer(addr, freq=2412):
  597. raise Exception("Device discovery timed out")
  598. dev.global_request("P2P_PROV_DISC " + addr + " display")
  599. ev0 = dev.wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout=15)
  600. if ev0 is None:
  601. raise Exception("Provision discovery timed out (display)")
  602. dev.p2p_stop_find()
  603. def run_discovery_while_go(wpas, dev, params):
  604. wpas.request("P2P_SET listen_channel 1")
  605. wpas.p2p_start_go(freq="2412")
  606. addr = wpas.p2p_dev_addr()
  607. pin = dev[0].wps_read_pin()
  608. wpas.p2p_go_authorize_client(pin)
  609. dev[1].p2p_connect_group(addr, pin, freq=2412, timeout=30)
  610. pd_test(dev[0], addr)
  611. wpas.p2p_listen()
  612. pd_test(dev[2], addr)
  613. wpas.p2p_stop_find()
  614. terminate_group(wpas, dev[1])
  615. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  616. "wifi_p2p.public_action.subtype == 8", [ "wlan.da" ])
  617. da = out.splitlines()
  618. logger.info("PD Response DAs: " + str(da))
  619. if len(da) != 3:
  620. raise Exception("Unexpected DA count for PD Response")
  621. def test_discovery_while_go(dev, apdev, params):
  622. """P2P provision discovery from GO"""
  623. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  624. wpas.interface_add("wlan5")
  625. run_discovery_while_go(wpas, dev, params)
  626. def test_discovery_while_go_p2p_dev(dev, apdev, params):
  627. """P2P provision discovery from GO (using P2P Device interface)"""
  628. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  629. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  630. wpas.interface_add(iface)
  631. run_discovery_while_go(wpas, dev, params)
  632. def run_discovery_while_cli(wpas, dev, params):
  633. wpas.request("P2P_SET listen_channel 1")
  634. dev[1].p2p_start_go(freq="2412")
  635. addr = wpas.p2p_dev_addr()
  636. pin = wpas.wps_read_pin()
  637. dev[1].p2p_go_authorize_client(pin)
  638. wpas.p2p_connect_group(dev[1].p2p_dev_addr(), pin, freq=2412, timeout=30)
  639. pd_test(dev[0], addr)
  640. wpas.p2p_listen()
  641. pd_test(dev[2], addr)
  642. wpas.p2p_stop_find()
  643. terminate_group(dev[1], wpas)
  644. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  645. "wifi_p2p.public_action.subtype == 8", [ "wlan.da" ])
  646. da = out.splitlines()
  647. logger.info("PD Response DAs: " + str(da))
  648. if len(da) != 3:
  649. raise Exception("Unexpected DA count for PD Response")
  650. def test_discovery_while_cli(dev, apdev, params):
  651. """P2P provision discovery from CLI"""
  652. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  653. wpas.interface_add("wlan5")
  654. run_discovery_while_cli(wpas, dev, params)
  655. def test_discovery_while_cli_p2p_dev(dev, apdev, params):
  656. """P2P provision discovery from CLI (using P2P Device interface)"""
  657. with HWSimRadio(use_p2p_device=True) as (radio, iface):
  658. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  659. wpas.interface_add(iface)
  660. run_discovery_while_cli(wpas, dev, params)
  661. def test_discovery_device_name_change(dev):
  662. """P2P device discovery and peer changing device name"""
  663. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  664. wpas.interface_add("wlan5")
  665. wpas.set("device_name", "test-a")
  666. wpas.p2p_listen()
  667. dev[0].p2p_find(social=True)
  668. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=15)
  669. if ev is None:
  670. raise Exception("Peer not found")
  671. if "new=1" not in ev:
  672. raise Exception("Incorrect new event: " + ev)
  673. if "name='test-a'" not in ev:
  674. raise Exception("Unexpected device name(1): " + ev)
  675. # Verify that new P2P-DEVICE-FOUND event is indicated when the peer changes
  676. # its device name.
  677. wpas.set("device_name", "test-b")
  678. ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=15)
  679. if ev is None:
  680. raise Exception("Peer update not seen")
  681. if "new=0" not in ev:
  682. raise Exception("Incorrect update event: " + ev)
  683. if "name='test-b'" not in ev:
  684. raise Exception("Unexpected device name(2): " + ev)
  685. wpas.p2p_stop_find()
  686. dev[0].p2p_stop_find()