test_nfc_p2p.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. # P2P+NFC tests
  2. # Copyright (c) 2013, Qualcomm Atheros, Inc.
  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 time
  8. import logging
  9. logger = logging.getLogger(__name__)
  10. import hwsim_utils
  11. from utils import alloc_fail
  12. grpform_events = ["P2P-GROUP-STARTED",
  13. "P2P-GO-NEG-FAILURE",
  14. "P2P-GROUP-FORMATION-FAILURE",
  15. "WPS-PIN-NEEDED",
  16. "WPS-M2D",
  17. "WPS-FAIL"]
  18. def set_ip_addr_info(dev):
  19. dev.global_request("SET ip_addr_go 192.168.42.1")
  20. dev.global_request("SET ip_addr_mask 255.255.255.0")
  21. dev.global_request("SET ip_addr_start 192.168.42.100")
  22. dev.global_request("SET ip_addr_end 192.168.42.199")
  23. def check_ip_addr(res):
  24. if 'ip_addr' not in res:
  25. raise Exception("Did not receive IP address from GO")
  26. if '192.168.42.' not in res['ip_addr']:
  27. raise Exception("Unexpected IP address received from GO")
  28. if 'ip_mask' not in res:
  29. raise Exception("Did not receive IP address mask from GO")
  30. if '255.255.255.' not in res['ip_mask']:
  31. raise Exception("Unexpected IP address mask received from GO")
  32. if 'go_ip_addr' not in res:
  33. raise Exception("Did not receive GO IP address from GO")
  34. if '192.168.42.' not in res['go_ip_addr']:
  35. raise Exception("Unexpected GO IP address received from GO")
  36. def test_nfc_p2p_go_neg(dev):
  37. """NFC connection handover to form a new P2P group (initiator becomes GO)"""
  38. try:
  39. _test_nfc_p2p_go_neg(dev)
  40. finally:
  41. dev[0].global_request("SET p2p_go_intent 7")
  42. def _test_nfc_p2p_go_neg(dev):
  43. set_ip_addr_info(dev[0])
  44. ip = dev[0].p2pdev_request("GET ip_addr_go")
  45. if ip != "192.168.42.1":
  46. raise Exception("Unexpected ip_addr_go returned: " + ip)
  47. dev[0].global_request("SET p2p_go_intent 10")
  48. logger.info("Perform NFC connection handover")
  49. req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
  50. if "FAIL" in req:
  51. raise Exception("Failed to generate NFC connection handover request")
  52. sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
  53. if "FAIL" in sel:
  54. raise Exception("Failed to generate NFC connection handover select")
  55. dev[0].dump_monitor()
  56. dev[1].dump_monitor()
  57. res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
  58. if "FAIL" in res:
  59. raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
  60. res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
  61. if "FAIL" in res:
  62. raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
  63. ev = dev[0].wait_global_event(["P2P-GROUP-STARTED",
  64. "P2P-GO-NEG-FAILURE",
  65. "P2P-GROUP-FORMATION-FAILURE",
  66. "WPS-PIN-NEEDED"], timeout=15)
  67. if ev is None:
  68. raise Exception("Group formation timed out")
  69. res0 = dev[0].group_form_result(ev)
  70. ev = dev[1].wait_global_event(["P2P-GROUP-STARTED",
  71. "P2P-GO-NEG-FAILURE",
  72. "P2P-GROUP-FORMATION-FAILURE",
  73. "WPS-PIN-NEEDED"], timeout=1)
  74. if ev is None:
  75. raise Exception("Group formation timed out")
  76. res1 = dev[1].group_form_result(ev)
  77. logger.info("Group formed")
  78. if res1['role'] != 'client' or res0['role'] != 'GO':
  79. raise Exception("Unexpected roles negotiated")
  80. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  81. check_ip_addr(res1)
  82. def test_nfc_p2p_go_neg_ip_pool_oom(dev):
  83. """NFC connection handover to form a new P2P group and IP pool OOM"""
  84. try:
  85. _test_nfc_p2p_go_neg_ip_pool_oom(dev)
  86. finally:
  87. dev[0].global_request("SET p2p_go_intent 7")
  88. def _test_nfc_p2p_go_neg_ip_pool_oom(dev):
  89. set_ip_addr_info(dev[0])
  90. ip = dev[0].p2pdev_request("GET ip_addr_go")
  91. if ip != "192.168.42.1":
  92. raise Exception("Unexpected ip_addr_go returned: " + ip)
  93. dev[0].global_request("SET p2p_go_intent 10")
  94. logger.info("Perform NFC connection handover")
  95. req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
  96. if "FAIL" in req:
  97. raise Exception("Failed to generate NFC connection handover request")
  98. sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
  99. if "FAIL" in sel:
  100. raise Exception("Failed to generate NFC connection handover select")
  101. dev[0].dump_monitor()
  102. dev[1].dump_monitor()
  103. with alloc_fail(dev[0], 1, "bitfield_alloc;wpa_init"):
  104. res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
  105. if "FAIL" in res:
  106. raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
  107. res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
  108. if "FAIL" in res:
  109. raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
  110. ev = dev[0].wait_global_event(["P2P-GROUP-STARTED",
  111. "P2P-GO-NEG-FAILURE",
  112. "P2P-GROUP-FORMATION-FAILURE",
  113. "WPS-PIN-NEEDED"], timeout=15)
  114. if ev is None:
  115. raise Exception("Group formation timed out")
  116. res0 = dev[0].group_form_result(ev)
  117. ev = dev[1].wait_global_event(["P2P-GROUP-STARTED",
  118. "P2P-GO-NEG-FAILURE",
  119. "P2P-GROUP-FORMATION-FAILURE",
  120. "WPS-PIN-NEEDED"], timeout=1)
  121. if ev is None:
  122. raise Exception("Group formation timed out")
  123. res1 = dev[1].group_form_result(ev)
  124. logger.info("Group formed")
  125. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  126. if 'ip_addr' in res1:
  127. raise Exception("Unexpectedly received IP address from GO")
  128. def test_nfc_p2p_go_neg_reverse(dev):
  129. """NFC connection handover to form a new P2P group (responder becomes GO)"""
  130. try:
  131. _test_nfc_p2p_go_neg_reverse(dev)
  132. finally:
  133. dev[0].global_request("SET p2p_go_intent 7")
  134. def _test_nfc_p2p_go_neg_reverse(dev):
  135. set_ip_addr_info(dev[1])
  136. dev[0].global_request("SET p2p_go_intent 3")
  137. logger.info("Perform NFC connection handover")
  138. req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
  139. if "FAIL" in req:
  140. raise Exception("Failed to generate NFC connection handover request")
  141. sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
  142. if "FAIL" in sel:
  143. raise Exception("Failed to generate NFC connection handover select")
  144. dev[0].dump_monitor()
  145. dev[1].dump_monitor()
  146. res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
  147. if "FAIL" in res:
  148. raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
  149. res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
  150. if "FAIL" in res:
  151. raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
  152. ev = dev[0].wait_global_event(["P2P-GROUP-STARTED",
  153. "P2P-GO-NEG-FAILURE",
  154. "P2P-GROUP-FORMATION-FAILURE",
  155. "WPS-PIN-NEEDED"], timeout=15)
  156. if ev is None:
  157. raise Exception("Group formation timed out")
  158. res0 = dev[0].group_form_result(ev)
  159. ev = dev[1].wait_global_event(["P2P-GROUP-STARTED",
  160. "P2P-GO-NEG-FAILURE",
  161. "P2P-GROUP-FORMATION-FAILURE",
  162. "WPS-PIN-NEEDED"], timeout=1)
  163. if ev is None:
  164. raise Exception("Group formation timed out")
  165. res1 = dev[1].group_form_result(ev)
  166. logger.info("Group formed")
  167. if res0['role'] != 'client' or res1['role'] != 'GO':
  168. raise Exception("Unexpected roles negotiated")
  169. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  170. check_ip_addr(res0)
  171. def test_nfc_p2p_initiator_go(dev):
  172. """NFC connection handover with initiator already GO"""
  173. set_ip_addr_info(dev[0])
  174. logger.info("Start autonomous GO")
  175. dev[0].p2p_start_go()
  176. logger.info("Perform NFC connection handover")
  177. req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
  178. if "FAIL" in req:
  179. raise Exception("Failed to generate NFC connection handover request")
  180. sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
  181. if "FAIL" in sel:
  182. raise Exception("Failed to generate NFC connection handover select")
  183. dev[0].dump_monitor()
  184. dev[1].dump_monitor()
  185. res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
  186. if "FAIL" in res:
  187. raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
  188. res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
  189. if "FAIL" in res:
  190. raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
  191. ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  192. if ev is None:
  193. raise Exception("Connection to the group timed out")
  194. res1 = dev[1].group_form_result(ev)
  195. if res1['result'] != 'success':
  196. raise Exception("Unexpected connection failure")
  197. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  198. check_ip_addr(res1)
  199. def test_nfc_p2p_responder_go(dev):
  200. """NFC connection handover with responder already GO"""
  201. set_ip_addr_info(dev[1])
  202. logger.info("Start autonomous GO")
  203. dev[1].p2p_start_go()
  204. logger.info("Perform NFC connection handover")
  205. req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
  206. if "FAIL" in req:
  207. raise Exception("Failed to generate NFC connection handover request")
  208. sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
  209. if "FAIL" in sel:
  210. raise Exception("Failed to generate NFC connection handover select")
  211. dev[0].dump_monitor()
  212. dev[1].dump_monitor()
  213. res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
  214. if "FAIL" in res:
  215. raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
  216. res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
  217. if "FAIL" in res:
  218. raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
  219. ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
  220. if ev is None:
  221. raise Exception("Connection to the group timed out")
  222. res0 = dev[0].group_form_result(ev)
  223. if res0['result'] != 'success':
  224. raise Exception("Unexpected connection failure")
  225. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  226. check_ip_addr(res0)
  227. def test_nfc_p2p_both_go(dev):
  228. """NFC connection handover with both devices already GOs"""
  229. set_ip_addr_info(dev[0])
  230. set_ip_addr_info(dev[1])
  231. logger.info("Start autonomous GOs")
  232. dev[0].p2p_start_go()
  233. dev[1].p2p_start_go()
  234. logger.info("Perform NFC connection handover")
  235. req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
  236. if "FAIL" in req:
  237. raise Exception("Failed to generate NFC connection handover request")
  238. sel = dev[1].request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
  239. if "FAIL" in sel:
  240. raise Exception("Failed to generate NFC connection handover select")
  241. dev[0].dump_monitor()
  242. dev[1].dump_monitor()
  243. res = dev[1].request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
  244. if "FAIL" in res:
  245. raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
  246. res = dev[0].request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
  247. if "FAIL" in res:
  248. raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
  249. ev = dev[0].wait_event(["P2P-NFC-BOTH-GO"], timeout=15)
  250. if ev is None:
  251. raise Exception("Time out waiting for P2P-NFC-BOTH-GO (dev0)")
  252. ev = dev[1].wait_event(["P2P-NFC-BOTH-GO"], timeout=1)
  253. if ev is None:
  254. raise Exception("Time out waiting for P2P-NFC-BOTH-GO (dev1)")
  255. dev[0].remove_group()
  256. dev[1].remove_group()
  257. def test_nfc_p2p_client(dev):
  258. """NFC connection handover when one device is P2P client"""
  259. logger.info("Start autonomous GOs")
  260. go_res = dev[0].p2p_start_go()
  261. logger.info("Connect one device as a P2P client")
  262. pin = dev[1].wps_read_pin()
  263. dev[0].p2p_go_authorize_client(pin)
  264. dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), pin,
  265. freq=int(go_res['freq']), timeout=60)
  266. logger.info("Client connected")
  267. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  268. logger.info("NFC connection handover between P2P client and P2P device")
  269. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
  270. if "FAIL" in req:
  271. raise Exception("Failed to generate NFC connection handover request")
  272. sel = dev[2].request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
  273. if "FAIL" in sel:
  274. raise Exception("Failed to generate NFC connection handover select")
  275. dev[1].dump_monitor()
  276. dev[2].dump_monitor()
  277. res = dev[2].request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
  278. if "FAIL" in res:
  279. raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
  280. res = dev[1].request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
  281. if "FAIL" in res:
  282. raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
  283. ev = dev[1].wait_event(["P2P-NFC-WHILE-CLIENT"], timeout=15)
  284. if ev is None:
  285. raise Exception("Time out waiting for P2P-NFC-WHILE-CLIENT")
  286. ev = dev[2].wait_event(["P2P-NFC-PEER-CLIENT"], timeout=1)
  287. if ev is None:
  288. raise Exception("Time out waiting for P2P-NFC-PEER-CLIENT")
  289. logger.info("Connect to group based on upper layer trigger")
  290. pin = dev[2].wps_read_pin()
  291. dev[0].p2p_go_authorize_client(pin)
  292. dev[2].p2p_connect_group(dev[0].p2p_dev_addr(), pin,
  293. freq=int(go_res['freq']), timeout=60)
  294. logger.info("Client connected")
  295. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  296. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  297. dev[2].remove_group()
  298. dev[1].remove_group()
  299. dev[0].remove_group()
  300. def test_nfc_p2p_static_handover_tagdev_client(dev):
  301. """NFC static handover to form a new P2P group (NFC Tag device becomes P2P Client)"""
  302. try:
  303. _test_nfc_p2p_static_handover_tagdev_client(dev)
  304. finally:
  305. dev[0].global_request("SET p2p_go_intent 7")
  306. def _test_nfc_p2p_static_handover_tagdev_client(dev):
  307. set_ip_addr_info(dev[0])
  308. logger.info("Perform NFC connection handover")
  309. res = dev[1].global_request("SET p2p_listen_reg_class 81")
  310. res2 = dev[1].global_request("SET p2p_listen_channel 6")
  311. if "FAIL" in res or "FAIL" in res2:
  312. raise Exception("Could not set Listen channel")
  313. pw = dev[1].global_request("WPS_NFC_TOKEN NDEF").rstrip()
  314. if "FAIL" in pw:
  315. raise Exception("Failed to generate password token")
  316. res = dev[1].global_request("P2P_SET nfc_tag 1").rstrip()
  317. if "FAIL" in res:
  318. raise Exception("Failed to enable NFC Tag for P2P static handover")
  319. sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
  320. if "FAIL" in sel:
  321. raise Exception("Failed to generate NFC connection handover select")
  322. res = dev[1].global_request("P2P_LISTEN")
  323. if "FAIL" in res:
  324. raise Exception("Failed to start Listen mode")
  325. dev[1].dump_monitor()
  326. dev[0].dump_monitor()
  327. dev[0].global_request("SET p2p_go_intent 10")
  328. res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
  329. if "FAIL" in res:
  330. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  331. ev = dev[0].wait_global_event(grpform_events, timeout=15)
  332. if ev is None:
  333. raise Exception("Group formation timed out")
  334. res0 = dev[0].group_form_result(ev)
  335. ev = dev[1].wait_global_event(grpform_events, timeout=1)
  336. if ev is None:
  337. raise Exception("Group formation timed out")
  338. res1 = dev[1].group_form_result(ev)
  339. logger.info("Group formed")
  340. if res1['role'] != 'client' or res0['role'] != 'GO':
  341. raise Exception("Unexpected roles negotiated")
  342. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  343. check_ip_addr(res1)
  344. def test_nfc_p2p_static_handover_tagdev_client_group_iface(dev):
  345. """NFC static handover to form a new P2P group (NFC Tag device becomes P2P Client with group iface)"""
  346. try:
  347. _test_nfc_p2p_static_handover_tagdev_client_group_iface(dev)
  348. finally:
  349. dev[0].global_request("SET p2p_go_intent 7")
  350. def _test_nfc_p2p_static_handover_tagdev_client_group_iface(dev):
  351. set_ip_addr_info(dev[0])
  352. logger.info("Perform NFC connection handover")
  353. res = dev[1].global_request("SET p2p_listen_reg_class 81")
  354. res2 = dev[1].global_request("SET p2p_listen_channel 6")
  355. if "FAIL" in res or "FAIL" in res2:
  356. raise Exception("Could not set Listen channel")
  357. pw = dev[1].global_request("WPS_NFC_TOKEN NDEF").rstrip()
  358. if "FAIL" in pw:
  359. raise Exception("Failed to generate password token")
  360. dev[1].global_request("SET p2p_no_group_iface 0")
  361. res = dev[1].global_request("P2P_SET nfc_tag 1").rstrip()
  362. if "FAIL" in res:
  363. raise Exception("Failed to enable NFC Tag for P2P static handover")
  364. sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
  365. if "FAIL" in sel:
  366. raise Exception("Failed to generate NFC connection handover select")
  367. res = dev[1].global_request("P2P_LISTEN")
  368. if "FAIL" in res:
  369. raise Exception("Failed to start Listen mode")
  370. dev[1].dump_monitor()
  371. dev[0].dump_monitor()
  372. dev[0].global_request("SET p2p_go_intent 10")
  373. res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
  374. if "FAIL" in res:
  375. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  376. ev = dev[0].wait_global_event(grpform_events, timeout=15)
  377. if ev is None:
  378. raise Exception("Group formation timed out")
  379. res0 = dev[0].group_form_result(ev)
  380. ev = dev[1].wait_global_event(grpform_events, timeout=1)
  381. if ev is None:
  382. raise Exception("Group formation timed out")
  383. res1 = dev[1].group_form_result(ev)
  384. logger.info("Group formed")
  385. if res1['role'] != 'client' or res0['role'] != 'GO':
  386. raise Exception("Unexpected roles negotiated")
  387. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  388. check_ip_addr(res1)
  389. def test_nfc_p2p_static_handover_tagdev_go(dev):
  390. """NFC static handover to form a new P2P group (NFC Tag device becomes GO)"""
  391. try:
  392. _test_nfc_p2p_static_handover_tagdev_go(dev)
  393. finally:
  394. dev[0].global_request("SET p2p_go_intent 7")
  395. def _test_nfc_p2p_static_handover_tagdev_go(dev):
  396. set_ip_addr_info(dev[1])
  397. logger.info("Perform NFC connection handover")
  398. res = dev[1].global_request("SET p2p_listen_reg_class 81")
  399. res2 = dev[1].global_request("SET p2p_listen_channel 6")
  400. if "FAIL" in res or "FAIL" in res2:
  401. raise Exception("Could not set Listen channel")
  402. pw = dev[1].global_request("WPS_NFC_TOKEN NDEF").rstrip()
  403. if "FAIL" in pw:
  404. raise Exception("Failed to generate password token")
  405. res = dev[1].global_request("P2P_SET nfc_tag 1").rstrip()
  406. if "FAIL" in res:
  407. raise Exception("Failed to enable NFC Tag for P2P static handover")
  408. sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
  409. if "FAIL" in sel:
  410. raise Exception("Failed to generate NFC connection handover select")
  411. res = dev[1].global_request("P2P_LISTEN")
  412. if "FAIL" in res:
  413. raise Exception("Failed to start Listen mode")
  414. dev[1].dump_monitor()
  415. dev[0].dump_monitor()
  416. dev[0].global_request("SET p2p_go_intent 3")
  417. res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
  418. if "FAIL" in res:
  419. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  420. ev = dev[0].wait_global_event(grpform_events, timeout=15)
  421. if ev is None:
  422. raise Exception("Group formation timed out")
  423. res0 = dev[0].group_form_result(ev)
  424. ev = dev[1].wait_global_event(grpform_events, timeout=1)
  425. if ev is None:
  426. raise Exception("Group formation timed out")
  427. res1 = dev[1].group_form_result(ev)
  428. logger.info("Group formed")
  429. if res0['role'] != 'client' or res1['role'] != 'GO':
  430. raise Exception("Unexpected roles negotiated")
  431. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  432. check_ip_addr(res0)
  433. def test_nfc_p2p_static_handover_tagdev_go_forced_freq(dev):
  434. """NFC static handover to form a new P2P group on forced channel (NFC Tag device becomes GO)"""
  435. try:
  436. _test_nfc_p2p_static_handover_tagdev_go_forced_freq(dev)
  437. finally:
  438. dev[0].global_request("SET p2p_go_intent 7")
  439. def _test_nfc_p2p_static_handover_tagdev_go_forced_freq(dev):
  440. set_ip_addr_info(dev[1])
  441. logger.info("Perform NFC connection handover")
  442. res = dev[1].global_request("SET p2p_listen_reg_class 81")
  443. res2 = dev[1].global_request("SET p2p_listen_channel 6")
  444. if "FAIL" in res or "FAIL" in res2:
  445. raise Exception("Could not set Listen channel")
  446. pw = dev[1].global_request("WPS_NFC_TOKEN NDEF").rstrip()
  447. if "FAIL" in pw:
  448. raise Exception("Failed to generate password token")
  449. res = dev[1].global_request("P2P_SET nfc_tag 1").rstrip()
  450. if "FAIL" in res:
  451. raise Exception("Failed to enable NFC Tag for P2P static handover")
  452. sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
  453. if "FAIL" in sel:
  454. raise Exception("Failed to generate NFC connection handover select")
  455. res = dev[1].global_request("P2P_LISTEN")
  456. if "FAIL" in res:
  457. raise Exception("Failed to start Listen mode")
  458. dev[1].dump_monitor()
  459. dev[0].dump_monitor()
  460. dev[0].global_request("SET p2p_go_intent 3")
  461. res = dev[0].global_request("WPS_NFC_TAG_READ " + sel + " freq=2442")
  462. if "FAIL" in res:
  463. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  464. ev = dev[0].wait_global_event(grpform_events, timeout=15)
  465. if ev is None:
  466. raise Exception("Group formation timed out")
  467. res0 = dev[0].group_form_result(ev)
  468. ev = dev[1].wait_global_event(grpform_events, timeout=1)
  469. if ev is None:
  470. raise Exception("Group formation timed out")
  471. res1 = dev[1].group_form_result(ev)
  472. logger.info("Group formed")
  473. if res0['role'] != 'client' or res1['role'] != 'GO':
  474. raise Exception("Unexpected roles negotiated")
  475. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  476. check_ip_addr(res0)
  477. def test_nfc_p2p_static_handover_join_tagdev_go(dev):
  478. """NFC static handover to join a P2P group (NFC Tag device is the GO)"""
  479. logger.info("Start autonomous GO")
  480. set_ip_addr_info(dev[0])
  481. dev[0].p2p_start_go()
  482. logger.info("Write NFC Tag on the GO")
  483. pw = dev[0].request("WPS_NFC_TOKEN NDEF").rstrip()
  484. if "FAIL" in pw:
  485. raise Exception("Failed to generate password token")
  486. res = dev[0].request("P2P_SET nfc_tag 1").rstrip()
  487. if "FAIL" in res:
  488. raise Exception("Failed to enable NFC Tag for P2P static handover")
  489. sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
  490. if "FAIL" in sel:
  491. raise Exception("Failed to generate NFC connection handover select")
  492. logger.info("Read NFC Tag on a P2P Device to join a group")
  493. res = dev[1].request("WPS_NFC_TAG_READ " + sel)
  494. if "FAIL" in res:
  495. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  496. ev = dev[1].wait_event(grpform_events, timeout=30)
  497. if ev is None:
  498. raise Exception("Joining the group timed out")
  499. res = dev[1].group_form_result(ev)
  500. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  501. check_ip_addr(res)
  502. logger.info("Read NFC Tag on another P2P Device to join a group")
  503. res = dev[2].request("WPS_NFC_TAG_READ " + sel)
  504. if "FAIL" in res:
  505. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  506. ev = dev[2].wait_event(grpform_events, timeout=30)
  507. if ev is None:
  508. raise Exception("Joining the group timed out")
  509. res = dev[2].group_form_result(ev)
  510. hwsim_utils.test_connectivity_p2p(dev[0], dev[2])
  511. check_ip_addr(res)
  512. def test_nfc_p2p_static_handover_join_tagdev_client(dev):
  513. """NFC static handover to join a P2P group (NFC Tag device is the P2P Client)"""
  514. try:
  515. _test_nfc_p2p_static_handover_join_tagdev_client(dev)
  516. finally:
  517. dev[1].global_request("SET ignore_old_scan_res 0")
  518. dev[2].global_request("SET ignore_old_scan_res 0")
  519. def _test_nfc_p2p_static_handover_join_tagdev_client(dev):
  520. set_ip_addr_info(dev[0])
  521. logger.info("Start autonomous GO")
  522. dev[0].p2p_start_go()
  523. dev[1].global_request("SET ignore_old_scan_res 1")
  524. dev[2].global_request("SET ignore_old_scan_res 1")
  525. logger.info("Write NFC Tag on the P2P Client")
  526. res = dev[1].global_request("P2P_LISTEN")
  527. if "FAIL" in res:
  528. raise Exception("Failed to start Listen mode")
  529. pw = dev[1].global_request("WPS_NFC_TOKEN NDEF").rstrip()
  530. if "FAIL" in pw:
  531. raise Exception("Failed to generate password token")
  532. res = dev[1].global_request("P2P_SET nfc_tag 1").rstrip()
  533. if "FAIL" in res:
  534. raise Exception("Failed to enable NFC Tag for P2P static handover")
  535. sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
  536. if "FAIL" in sel:
  537. raise Exception("Failed to generate NFC connection handover select")
  538. logger.info("Read NFC Tag on the GO to trigger invitation")
  539. res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
  540. if "FAIL" in res:
  541. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  542. ev = dev[1].wait_global_event(grpform_events, timeout=30)
  543. if ev is None:
  544. raise Exception("Joining the group timed out")
  545. res = dev[1].group_form_result(ev)
  546. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  547. check_ip_addr(res)
  548. logger.info("Write NFC Tag on another P2P Client")
  549. res = dev[2].global_request("P2P_LISTEN")
  550. if "FAIL" in res:
  551. raise Exception("Failed to start Listen mode")
  552. pw = dev[2].global_request("WPS_NFC_TOKEN NDEF").rstrip()
  553. if "FAIL" in pw:
  554. raise Exception("Failed to generate password token")
  555. res = dev[2].global_request("P2P_SET nfc_tag 1").rstrip()
  556. if "FAIL" in res:
  557. raise Exception("Failed to enable NFC Tag for P2P static handover")
  558. sel = dev[2].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
  559. if "FAIL" in sel:
  560. raise Exception("Failed to generate NFC connection handover select")
  561. logger.info("Read NFC Tag on the GO to trigger invitation")
  562. res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
  563. if "FAIL" in res:
  564. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  565. ev = dev[2].wait_global_event(grpform_events, timeout=30)
  566. if ev is None:
  567. raise Exception("Joining the group timed out")
  568. res = dev[2].group_form_result(ev)
  569. hwsim_utils.test_connectivity_p2p(dev[0], dev[2])
  570. check_ip_addr(res)
  571. def test_nfc_p2p_go_legacy_config_token(dev):
  572. """NFC config token from P2P GO to legacy WPS STA"""
  573. logger.info("Start autonomous GOs")
  574. dev[0].p2p_start_go()
  575. logger.info("Connect legacy WPS STA with configuration token")
  576. conf = dev[0].group_request("WPS_NFC_CONFIG_TOKEN NDEF").rstrip()
  577. if "FAIL" in conf:
  578. raise Exception("Failed to generate configuration token")
  579. dev[1].dump_monitor()
  580. res = dev[1].request("WPS_NFC_TAG_READ " + conf)
  581. if "FAIL" in res:
  582. raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
  583. dev[1].wait_connected(timeout=15, error="Joining the group timed out")
  584. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  585. dev[1].request("DISCONNECT")
  586. dev[0].remove_group()
  587. def test_nfc_p2p_go_legacy_handover(dev):
  588. """NFC token from legacy WPS STA to P2P GO"""
  589. logger.info("Start autonomous GOs")
  590. dev[0].p2p_start_go()
  591. logger.info("Connect legacy WPS STA with connection handover")
  592. req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
  593. if "FAIL" in req:
  594. raise Exception("Failed to generate NFC connection handover request")
  595. sel = dev[0].group_request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
  596. if "FAIL" in sel:
  597. raise Exception("Failed to generate NFC connection handover select")
  598. res = dev[0].group_request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
  599. if "FAIL" in res:
  600. raise Exception("Failed to report NFC connection handover to wpa_supplicant (GO)")
  601. dev[1].dump_monitor()
  602. res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
  603. if "FAIL" in res:
  604. raise Exception("Failed to report NFC connection handover to wpa_supplicant (legacy STA)")
  605. dev[1].wait_connected(timeout=15, error="Joining the group timed out")
  606. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  607. dev[1].request("DISCONNECT")
  608. dev[0].remove_group()
  609. def test_nfc_p2p_ip_addr_assignment(dev):
  610. """NFC connection handover and legacy station IP address assignment"""
  611. try:
  612. _test_nfc_p2p_ip_addr_assignment(dev)
  613. finally:
  614. dev[0].global_request("SET p2p_go_intent 7")
  615. def _test_nfc_p2p_ip_addr_assignment(dev):
  616. set_ip_addr_info(dev[1])
  617. dev[0].global_request("SET p2p_go_intent 3")
  618. logger.info("Perform NFC connection handover")
  619. req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
  620. if "FAIL" in req:
  621. raise Exception("Failed to generate NFC connection handover request")
  622. sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
  623. if "FAIL" in sel:
  624. raise Exception("Failed to generate NFC connection handover select")
  625. dev[0].dump_monitor()
  626. dev[1].dump_monitor()
  627. res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
  628. if "FAIL" in res:
  629. raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
  630. res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
  631. if "FAIL" in res:
  632. raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
  633. ev = dev[0].wait_global_event(["P2P-GROUP-STARTED",
  634. "P2P-GO-NEG-FAILURE",
  635. "P2P-GROUP-FORMATION-FAILURE",
  636. "WPS-PIN-NEEDED"], timeout=15)
  637. if ev is None:
  638. raise Exception("Group formation timed out")
  639. res0 = dev[0].group_form_result(ev)
  640. ev = dev[1].wait_global_event(["P2P-GROUP-STARTED",
  641. "P2P-GO-NEG-FAILURE",
  642. "P2P-GROUP-FORMATION-FAILURE",
  643. "WPS-PIN-NEEDED"], timeout=1)
  644. if ev is None:
  645. raise Exception("Group formation timed out")
  646. res1 = dev[1].group_form_result(ev)
  647. logger.info("Group formed")
  648. if res0['role'] != 'client' or res1['role'] != 'GO':
  649. raise Exception("Unexpected roles negotiated")
  650. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  651. check_ip_addr(res0)
  652. logger.info("Connect legacy P2P client that does not use new IP address assignment")
  653. res = dev[2].global_request("P2P_SET disable_ip_addr_req 1")
  654. if "FAIL" in res:
  655. raise Exception("Failed to disable IP address assignment request")
  656. pin = dev[2].wps_read_pin()
  657. dev[1].p2p_go_authorize_client(pin)
  658. res = dev[2].p2p_connect_group(dev[1].p2p_dev_addr(), pin, timeout=60)
  659. logger.info("Client connected")
  660. res = dev[2].global_request("P2P_SET disable_ip_addr_req 0")
  661. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  662. if 'ip_addr' in res:
  663. raise Exception("Unexpected IP address assignment")
  664. def test_nfc_p2p_ip_addr_assignment2(dev):
  665. """NFC connection handover and IP address assignment for two clients"""
  666. try:
  667. _test_nfc_p2p_ip_addr_assignment2(dev)
  668. finally:
  669. dev[0].global_request("SET p2p_go_intent 7")
  670. def _test_nfc_p2p_ip_addr_assignment2(dev):
  671. set_ip_addr_info(dev[1])
  672. dev[0].global_request("SET p2p_go_intent 3")
  673. logger.info("Perform NFC connection handover")
  674. req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
  675. if "FAIL" in req:
  676. raise Exception("Failed to generate NFC connection handover request")
  677. sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
  678. if "FAIL" in sel:
  679. raise Exception("Failed to generate NFC connection handover select")
  680. dev[0].dump_monitor()
  681. dev[1].dump_monitor()
  682. res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
  683. if "FAIL" in res:
  684. raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
  685. res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
  686. if "FAIL" in res:
  687. raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
  688. ev = dev[0].wait_global_event(["P2P-GROUP-STARTED",
  689. "P2P-GO-NEG-FAILURE",
  690. "P2P-GROUP-FORMATION-FAILURE",
  691. "WPS-PIN-NEEDED"], timeout=15)
  692. if ev is None:
  693. raise Exception("Group formation timed out")
  694. res0 = dev[0].group_form_result(ev)
  695. ev = dev[1].wait_global_event(["P2P-GROUP-STARTED",
  696. "P2P-GO-NEG-FAILURE",
  697. "P2P-GROUP-FORMATION-FAILURE",
  698. "WPS-PIN-NEEDED"], timeout=1)
  699. if ev is None:
  700. raise Exception("Group formation timed out")
  701. res1 = dev[1].group_form_result(ev)
  702. logger.info("Group formed")
  703. if res0['role'] != 'client' or res1['role'] != 'GO':
  704. raise Exception("Unexpected roles negotiated")
  705. hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
  706. check_ip_addr(res0)
  707. logger.info("Client 1 IP address: " + res0['ip_addr'])
  708. logger.info("Connect a P2P client")
  709. pin = dev[2].wps_read_pin()
  710. dev[1].p2p_go_authorize_client(pin)
  711. res = dev[2].p2p_connect_group(dev[1].p2p_dev_addr(), pin, timeout=60)
  712. logger.info("Client connected")
  713. hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
  714. check_ip_addr(res)
  715. logger.info("Client 2 IP address: " + res['ip_addr'])
  716. if res['ip_addr'] == res0['ip_addr']:
  717. raise Exception("Same IP address assigned to both clients")
  718. @remote_compatible
  719. def test_nfc_p2p_tag_enable_disable(dev):
  720. """NFC tag enable/disable for P2P"""
  721. if "FAIL" in dev[0].request("WPS_NFC_TOKEN NDEF").rstrip():
  722. raise Exception("Failed to generate password token")
  723. if "OK" not in dev[0].request("P2P_SET nfc_tag 1"):
  724. raise Exception("Failed to enable NFC Tag for P2P static handover")
  725. if "OK" not in dev[0].request("P2P_SET nfc_tag 0"):
  726. raise Exception("Failed to disable NFC Tag for P2P static handover")
  727. dev[0].request("SET p2p_no_group_iface 0")
  728. if "OK" not in dev[0].request("P2P_SET nfc_tag 1"):
  729. raise Exception("Failed to enable NFC Tag for P2P static handover")
  730. if "OK" not in dev[0].request("P2P_SET nfc_tag 0"):
  731. raise Exception("Failed to disable NFC Tag for P2P static handover")
  732. if "OK" not in dev[0].request("P2P_SET nfc_tag 1"):
  733. raise Exception("Failed to enable NFC Tag for P2P static handover")
  734. if "OK" not in dev[0].request("P2P_SET nfc_tag 0"):
  735. raise Exception("Failed to disable NFC Tag for P2P static handover")
  736. @remote_compatible
  737. def test_nfc_p2p_static_handover_invalid(dev):
  738. """NFC static handover with invalid contents"""
  739. logger.info("Unknown OOB GO Neg channel")
  740. sel = "D217A36170706C69636174696F6E2F766E642E7766612E7032700071102100012010230001201024000120102C0036C3B2ADB8D26F53CE1CB7F000BEEDA762922FF5307E87CCE484EF4B5DAD440D0A4752579767610AD1293F7A76A66B09A7C9D58A66994E103C000103104200012010470010572CF82FC95756539B16B5CFB298ABF11049000600372A000120002E02020025000D1D000200000001001108000000000000000000101100084465766963652042130600585804ff0B00"
  741. if "FAIL" not in dev[0].global_request("WPS_NFC_TAG_READ " + sel):
  742. raise Exception("Invalid tag contents accepted (1)")
  743. logger.info("No OOB GO Neg channel attribute")
  744. sel = "D2179A6170706C69636174696F6E2F766E642E7766612E7032700071102100012010230001201024000120102C0036C3B2ADB8D26F53CE1CB7F000BEEDA762922FF5307E87CCE484EF4B5DAD440D0A4752579767610AD1293F7A76A66B09A7C9D58A66994E103C000103104200012010470010572CF82FC95756539B16B5CFB298ABF11049000600372A000120002502020025000D1D000200000001001108000000000000000000101100084465766963652042"
  745. if "FAIL" not in dev[0].global_request("WPS_NFC_TAG_READ " + sel):
  746. raise Exception("Invalid tag contents accepted (2)")
  747. logger.info("No Device Info attribute")
  748. sel = "D217836170706C69636174696F6E2F766E642E7766612E7032700071102100012010230001201024000120102C0036C3B2ADB8D26F53CE1CB7F000BEEDA762922FF5307E87CCE484EF4B5DAD440D0A4752579767610AD1293F7A76A66B09A7C9D58A66994E103C000103104200012010470010572CF82FC95756539B16B5CFB298ABF11049000600372A000120000E0202002500130600585804510B00"
  749. if "FAIL" not in dev[0].global_request("WPS_NFC_TAG_READ " + sel):
  750. raise Exception("Invalid tag contents accepted (3)")