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