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