173a8ca6b1074df96bd684d7121ac593a9ae07d7
[mech_eap.git] / tests / hwsim / test_p2p_device.py
1 # cfg80211 P2P Device
2 # Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 import logging
8 logger = logging.getLogger()
9 import time
10
11 from wpasupplicant import WpaSupplicant
12 from p2p_utils import *
13 from test_nfc_p2p import set_ip_addr_info, check_ip_addr, grpform_events
14 from hwsim import HWSimRadio
15 from utils import HwsimSkip
16 import hostapd
17 import hwsim_utils
18
19 def test_p2p_device_grpform(dev, apdev):
20     """P2P group formation with driver using cfg80211 P2P Device"""
21     with HWSimRadio(use_p2p_device=True) as (radio, iface):
22         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
23         wpas.interface_add(iface)
24         [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
25                                                r_dev=wpas, r_intent=0)
26         check_grpform_results(i_res, r_res)
27         wpas.dump_monitor()
28         remove_group(dev[0], wpas)
29         wpas.dump_monitor()
30         if not r_res['ifname'].startswith('p2p-' + iface):
31             raise Exception("Unexpected group ifname: " + r_res['ifname'])
32
33         res = wpas.global_request("IFNAME=p2p-dev-" + iface + " STATUS-DRIVER")
34         lines = res.splitlines()
35         found = False
36         for l in lines:
37             try:
38                 [name,value] = l.split('=', 1)
39                 if name == "wdev_id":
40                     found = True
41                     break
42             except ValueError:
43                 pass
44         if not found:
45             raise Exception("wdev_id not found")
46
47 def test_p2p_device_grpform2(dev, apdev):
48     """P2P group formation with driver using cfg80211 P2P Device (reverse)"""
49     with HWSimRadio(use_p2p_device=True) as (radio, iface):
50         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
51         wpas.interface_add(iface)
52         [i_res, r_res] = go_neg_pin_authorized(i_dev=wpas, i_intent=15,
53                                                r_dev=dev[0], r_intent=0)
54         check_grpform_results(i_res, r_res)
55         wpas.dump_monitor()
56         remove_group(wpas, dev[0])
57         wpas.dump_monitor()
58         if not i_res['ifname'].startswith('p2p-' + iface):
59             raise Exception("Unexpected group ifname: " + i_res['ifname'])
60
61 def test_p2p_device_grpform_no_group_iface(dev, apdev):
62     """P2P group formation with driver using cfg80211 P2P Device but no separate group interface"""
63     with HWSimRadio(use_p2p_device=True) as (radio, iface):
64         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
65         wpas.interface_add(iface)
66         wpas.global_request("SET p2p_no_group_iface 1")
67         [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
68                                                r_dev=wpas, r_intent=0)
69         check_grpform_results(i_res, r_res)
70         wpas.dump_monitor()
71         remove_group(dev[0], wpas)
72         wpas.dump_monitor()
73         if r_res['ifname'] != iface:
74             raise Exception("Unexpected group ifname: " + r_res['ifname'])
75
76 def test_p2p_device_grpform_no_group_iface2(dev, apdev):
77     """P2P group formation with driver using cfg80211 P2P Device but no separate group interface (reverse)"""
78     with HWSimRadio(use_p2p_device=True) as (radio, iface):
79         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
80         wpas.interface_add(iface)
81         wpas.global_request("SET p2p_no_group_iface 1")
82         [i_res, r_res] = go_neg_pin_authorized(i_dev=wpas, i_intent=15,
83                                                r_dev=dev[0], r_intent=0)
84         check_grpform_results(i_res, r_res)
85         wpas.dump_monitor()
86         remove_group(dev[0], wpas)
87         wpas.dump_monitor()
88         if i_res['ifname'] != iface:
89             raise Exception("Unexpected group ifname: " + i_res['ifname'])
90
91 def test_p2p_device_group_remove(dev, apdev):
92     """P2P group removal via the P2P ctrl interface with driver using cfg80211 P2P Device"""
93     with HWSimRadio(use_p2p_device=True) as (radio, iface):
94         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
95         wpas.interface_add(iface)
96         [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
97                                                r_dev=wpas, r_intent=0)
98         check_grpform_results(i_res, r_res)
99         # Issue the remove request on the interface which will be removed
100         p2p_iface_wpas = WpaSupplicant(ifname=r_res['ifname'])
101         res = p2p_iface_wpas.request("P2P_GROUP_REMOVE *")
102         if "OK" not in res:
103             raise Exception("Failed to remove P2P group")
104         ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
105         if ev is None:
106             raise Exception("Group removal event not received")
107         if not wpas.global_ping():
108             raise Exception("Could not ping global ctrl_iface after group removal")
109
110 def test_p2p_device_concurrent_scan(dev, apdev):
111     """Concurrent P2P and station mode scans with driver using cfg80211 P2P Device"""
112     with HWSimRadio(use_p2p_device=True) as (radio, iface):
113         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
114         wpas.interface_add(iface)
115         wpas.p2p_find()
116         time.sleep(0.1)
117         wpas.request("SCAN")
118         ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=15)
119         if ev is None:
120             raise Exception("Station mode scan did not start")
121
122 def test_p2p_device_nfc_invite(dev, apdev):
123     """P2P NFC invitiation with driver using cfg80211 P2P Device"""
124     with HWSimRadio(use_p2p_device=True) as (radio, iface):
125         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
126         wpas.interface_add(iface)
127
128         set_ip_addr_info(dev[0])
129         logger.info("Start autonomous GO")
130         dev[0].p2p_start_go()
131
132         logger.info("Write NFC Tag on the P2P Client")
133         res = wpas.global_request("P2P_LISTEN")
134         if "FAIL" in res:
135             raise Exception("Failed to start Listen mode")
136         wpas.dump_monitor()
137         pw = wpas.global_request("WPS_NFC_TOKEN NDEF").rstrip()
138         if "FAIL" in pw:
139             raise Exception("Failed to generate password token")
140         res = wpas.global_request("P2P_SET nfc_tag 1").rstrip()
141         if "FAIL" in res:
142             raise Exception("Failed to enable NFC Tag for P2P static handover")
143         sel = wpas.global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
144         if "FAIL" in sel:
145             raise Exception("Failed to generate NFC connection handover select")
146         wpas.dump_monitor()
147
148         logger.info("Read NFC Tag on the GO to trigger invitation")
149         res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
150         if "FAIL" in res:
151             raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
152
153         ev = wpas.wait_global_event(grpform_events, timeout=20)
154         if ev is None:
155             raise Exception("Joining the group timed out")
156         res = wpas.group_form_result(ev)
157         wpas.dump_monitor()
158         hwsim_utils.test_connectivity_p2p(dev[0], wpas)
159         check_ip_addr(res)
160         wpas.dump_monitor()
161
162 def test_p2p_device_misuses(dev, apdev):
163     """cfg80211 P2P Device misuses"""
164     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
165     with HWSimRadio(use_p2p_device=True) as (radio, iface):
166         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
167         wpas.interface_add(iface)
168
169         # Add a normal network profile to the P2P Device management only
170         # interface to verify that it does not get used.
171         id = int(wpas.global_request('IFNAME=p2p-dev-%s ADD_NETWORK' % iface).strip())
172         wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d ssid "open"' % (iface, id))
173         wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d key_mgmt NONE' % (iface, id))
174         wpas.global_request('IFNAME=p2p-dev-%s ENABLE_NETWORK %d' % (iface, id))
175
176         # Scan requests get ignored on p2p-dev
177         wpas.global_request('IFNAME=p2p-dev-%s SCAN' % iface)
178
179         dev[0].p2p_start_go(freq=2412)
180         addr = dev[0].p2p_interface_addr()
181         wpas.scan_for_bss(addr, freq=2412)
182         wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
183         hwsim_utils.test_connectivity(wpas, hapd)
184
185         pin = wpas.wps_read_pin()
186         dev[0].p2p_go_authorize_client(pin)
187         res = wpas.p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60,
188                                      social=True, freq=2412)
189         hwsim_utils.test_connectivity_p2p(dev[0], wpas)
190
191         # Optimize scan-after-disconnect
192         wpas.group_request("SET_NETWORK 0 scan_freq 2412")
193
194         dev[0].group_request("DISASSOCIATE " + wpas.p2p_interface_addr())
195         ev = wpas.wait_group_event(["CTRL-EVENT-DISCONNECT"])
196         if ev is None:
197             raise Exception("Did not see disconnect event on P2P group interface")
198         dev[0].remove_group()
199
200         ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
201         if ev is None:
202             raise Exception("Scan not started")
203         ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
204         if ev is None:
205             raise Exception("Scan not completed")
206         time.sleep(1)
207         hwsim_utils.test_connectivity(wpas, hapd)
208
209         ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=0.1)
210         if ev is not None:
211             raise Exception("Unexpected disconnection event received from hostapd")
212         ev = wpas.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.1)
213         if ev is not None:
214             raise Exception("Unexpected disconnection event received from wpa_supplicant")
215
216         wpas.request("DISCONNECT")
217         wpas.wait_disconnected()
218
219 def test_p2p_device_incorrect_command_interface(dev, apdev):
220     """cfg80211 P2P Device and P2P_* command on incorrect interface"""
221     with HWSimRadio(use_p2p_device=True) as (radio, iface):
222         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
223         wpas.interface_add(iface)
224
225         dev[0].p2p_listen()
226         wpas.request('P2P_FIND type=social')
227         ev = wpas.wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
228         if ev is None:
229             raise Exception("Peer not found")
230         ev = wpas.wait_event(["P2P-DEVICE-FOUND"], timeout=0.1)
231         if ev is not None:
232             raise Exception("Unexpected P2P-DEVICE-FOUND event on station interface")
233         wpas.dump_monitor()
234
235         pin = wpas.wps_read_pin()
236         dev[0].p2p_go_neg_auth(wpas.p2p_dev_addr(), pin, "enter", go_intent=14,
237                                freq=2412)
238         wpas.request('P2P_STOP_FIND')
239         wpas.dump_monitor()
240         if "OK" not in wpas.request('P2P_CONNECT ' + dev[0].p2p_dev_addr() + ' ' + pin + ' display go_intent=1'):
241             raise Exception("P2P_CONNECT failed")
242
243         ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
244         if ev is None:
245             raise Exception("Group formation timed out")
246         wpas.group_form_result(ev)
247         wpas.dump_monitor()
248
249         ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
250         if ev is None:
251             raise Exception("Group formation timed out(2)")
252         dev[0].group_form_result(ev)
253
254         dev[0].remove_group()
255         wpas.wait_go_ending_session()
256         wpas.dump_monitor()
257
258 def test_p2p_device_incorrect_command_interface2(dev, apdev):
259     """cfg80211 P2P Device and P2P_GROUP_ADD command on incorrect interface"""
260     with HWSimRadio(use_p2p_device=True) as (radio, iface):
261         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
262         wpas.interface_add(iface)
263
264         if "OK" not in wpas.request('P2P_GROUP_ADD'):
265             raise Exception("P2P_GROUP_ADD failed")
266         ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
267         if ev is None:
268             raise Exception("Group formation timed out")
269         res = wpas.group_form_result(ev)
270         wpas.dump_monitor()
271         logger.info("Group results: " + str(res))
272         wpas.remove_group()
273         if not res['ifname'].startswith('p2p-' + iface + '-'):
274             raise Exception("Unexpected group ifname: " + res['ifname'])
275         wpas.dump_monitor()
276
277 def test_p2p_device_grpform_timeout_client(dev, apdev):
278     """P2P group formation timeout on client with cfg80211 P2P Device"""
279     with HWSimRadio(use_p2p_device=True) as (radio, iface):
280         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
281         wpas.interface_add(iface)
282         addr0 = dev[0].p2p_dev_addr()
283         addr5 = wpas.p2p_dev_addr()
284         wpas.p2p_listen()
285         dev[0].discover_peer(addr5)
286         dev[0].p2p_listen()
287         wpas.discover_peer(addr0)
288         wpas.p2p_ext_listen(100, 150)
289         dev[0].global_request("P2P_CONNECT " + addr5 + " 12345670 enter go_intent=15 auth")
290         wpas.global_request("P2P_CONNECT " + addr0 + " 12345670 display go_intent=0")
291         ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=5)
292         if ev is None:
293             raise Exception("GO Negotiation did not succeed")
294         ev = dev[0].wait_global_event(["WPS-SUCCESS"], timeout=10)
295         if ev is None:
296             raise Exception("WPS did not succeed (GO)")
297         if "OK" not in dev[0].global_request("P2P_CANCEL"):
298             wpas.global_request("P2P_CANCEL")
299             del wpas
300             raise HwsimSkip("Did not manage to cancel group formation")
301         dev[0].dump_monitor()
302         ev = wpas.wait_global_event(["WPS-SUCCESS"], timeout=10)
303         if ev is None:
304             raise Exception("WPS did not succeed (Client)")
305         dev[0].dump_monitor()
306         ev = wpas.wait_global_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=20)
307         if ev is None:
308             raise Exception("Group formation timeout not seen on client")
309         ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=5)
310         if ev is None:
311             raise Exception("Group removal not seen on client")
312         wpas.p2p_cancel_ext_listen()
313         time.sleep(0.1)
314         ifaces = wpas.global_request("INTERFACES")
315         logger.info("Remaining interfaces: " + ifaces)
316         del wpas
317         if "p2p-" + iface + "-" in ifaces:
318             raise Exception("Group interface still present after failure")
319
320 def test_p2p_device_grpform_timeout_go(dev, apdev):
321     """P2P group formation timeout on GO with cfg80211 P2P Device"""
322     with HWSimRadio(use_p2p_device=True) as (radio, iface):
323         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
324         wpas.interface_add(iface)
325         addr0 = dev[0].p2p_dev_addr()
326         addr5 = wpas.p2p_dev_addr()
327         wpas.p2p_listen()
328         dev[0].discover_peer(addr5)
329         dev[0].p2p_listen()
330         wpas.discover_peer(addr0)
331         wpas.p2p_ext_listen(100, 150)
332         dev[0].global_request("P2P_CONNECT " + addr5 + " 12345670 enter go_intent=0 auth")
333         wpas.global_request("P2P_CONNECT " + addr0 + " 12345670 display go_intent=15")
334         ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=5)
335         if ev is None:
336             raise Exception("GO Negotiation did not succeed")
337         ev = dev[0].wait_global_event(["WPS-SUCCESS"], timeout=10)
338         if ev is None:
339             raise Exception("WPS did not succeed (Client)")
340         if "OK" not in dev[0].global_request("P2P_CANCEL"):
341             if "OK" not in dev[0].global_request("P2P_GROUP_REMOVE *"):
342                 wpas.global_request("P2P_CANCEL")
343                 del wpas
344                 raise HwsimSkip("Did not manage to cancel group formation")
345         dev[0].dump_monitor()
346         ev = wpas.wait_global_event(["WPS-SUCCESS"], timeout=10)
347         if ev is None:
348             raise Exception("WPS did not succeed (GO)")
349         dev[0].dump_monitor()
350         ev = wpas.wait_global_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=20)
351         if ev is None:
352             raise Exception("Group formation timeout not seen on GO")
353         ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=5)
354         if ev is None:
355             raise Exception("Group removal not seen on GO")
356         wpas.p2p_cancel_ext_listen()
357         time.sleep(0.1)
358         ifaces = wpas.global_request("INTERFACES")
359         logger.info("Remaining interfaces: " + ifaces)
360         del wpas
361         if "p2p-" + iface + "-" in ifaces:
362             raise Exception("Group interface still present after failure")
363
364 def test_p2p_device_autogo(dev, apdev):
365     """P2P autogo using cfg80211 P2P Device"""
366     with HWSimRadio(use_p2p_device=True) as (radio, iface):
367         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
368         wpas.interface_add(iface)
369
370         res = wpas.p2p_start_go()
371         if not res['ifname'].startswith('p2p-' + iface):
372             raise Exception("Unexpected group ifname: " + res['ifname'])
373         bssid = wpas.get_group_status_field('bssid')
374
375         dev[0].scan_for_bss(bssid, res['freq'])
376         connect_cli(wpas, dev[0], freq=res['freq'])
377         terminate_group(wpas, dev[0])
378
379 def test_p2p_device_autogo_no_group_iface(dev, apdev):
380     """P2P autogo using cfg80211 P2P Device (no separate group interface)"""
381     with HWSimRadio(use_p2p_device=True) as (radio, iface):
382         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
383         wpas.interface_add(iface)
384         wpas.global_request("SET p2p_no_group_iface 1")
385
386         res = wpas.p2p_start_go()
387         if res['ifname'] != iface:
388             raise Exception("Unexpected group ifname: " + res['ifname'])
389         bssid = wpas.get_group_status_field('bssid')
390
391         dev[0].scan_for_bss(bssid, res['freq'])
392         connect_cli(wpas, dev[0], freq=res['freq'])
393         terminate_group(wpas, dev[0])
394
395 def test_p2p_device_join(dev, apdev):
396     """P2P join-group using cfg80211 P2P Device"""
397     with HWSimRadio(use_p2p_device=True) as (radio, iface):
398         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
399         wpas.interface_add(iface)
400
401         res = dev[0].p2p_start_go()
402         bssid = dev[0].get_group_status_field('bssid')
403
404         wpas.scan_for_bss(bssid, res['freq'])
405         res2 = connect_cli(dev[0], wpas, freq=res['freq'])
406         if not res2['ifname'].startswith('p2p-' + iface):
407             raise Exception("Unexpected group ifname: " + res2['ifname'])
408
409         terminate_group(dev[0], wpas)
410
411 def test_p2p_device_join_no_group_iface(dev, apdev):
412     """P2P join-group using cfg80211 P2P Device (no separate group interface)"""
413     with HWSimRadio(use_p2p_device=True) as (radio, iface):
414         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
415         wpas.interface_add(iface)
416         wpas.global_request("SET p2p_no_group_iface 1")
417
418         res = dev[0].p2p_start_go()
419         bssid = dev[0].get_group_status_field('bssid')
420
421         wpas.scan_for_bss(bssid, res['freq'])
422         res2 = connect_cli(dev[0], wpas, freq=res['freq'])
423         if res2['ifname'] != iface:
424             raise Exception("Unexpected group ifname: " + res2['ifname'])
425
426         terminate_group(dev[0], wpas)