2 # Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
8 logger = logging.getLogger()
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
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)
28 remove_group(dev[0], wpas)
30 if not r_res['ifname'].startswith('p2p-' + iface):
31 raise Exception("Unexpected group ifname: " + r_res['ifname'])
33 res = wpas.global_request("IFNAME=p2p-dev-" + iface + " STATUS-DRIVER")
34 lines = res.splitlines()
38 [name,value] = l.split('=', 1)
45 raise Exception("wdev_id not found")
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)
56 remove_group(wpas, dev[0])
58 if not i_res['ifname'].startswith('p2p-' + iface):
59 raise Exception("Unexpected group ifname: " + i_res['ifname'])
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)
71 remove_group(dev[0], wpas)
73 if r_res['ifname'] != iface:
74 raise Exception("Unexpected group ifname: " + r_res['ifname'])
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)
86 remove_group(dev[0], wpas)
88 if i_res['ifname'] != iface:
89 raise Exception("Unexpected group ifname: " + i_res['ifname'])
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 *")
103 raise Exception("Failed to remove P2P group")
104 ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
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")
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)
118 ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=15)
120 raise Exception("Station mode scan did not start")
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)
128 set_ip_addr_info(dev[0])
129 logger.info("Start autonomous GO")
130 dev[0].p2p_start_go()
132 logger.info("Write NFC Tag on the P2P Client")
133 res = wpas.global_request("P2P_LISTEN")
135 raise Exception("Failed to start Listen mode")
137 pw = wpas.global_request("WPS_NFC_TOKEN NDEF").rstrip()
139 raise Exception("Failed to generate password token")
140 res = wpas.global_request("P2P_SET nfc_tag 1").rstrip()
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()
145 raise Exception("Failed to generate NFC connection handover select")
148 logger.info("Read NFC Tag on the GO to trigger invitation")
149 res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
151 raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
153 ev = wpas.wait_global_event(grpform_events, timeout=20)
155 raise Exception("Joining the group timed out")
156 res = wpas.group_form_result(ev)
158 hwsim_utils.test_connectivity_p2p(dev[0], wpas)
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)
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))
176 # Scan requests get ignored on p2p-dev
177 wpas.global_request('IFNAME=p2p-dev-%s SCAN' % iface)
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)
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)
191 # Optimize scan-after-disconnect
192 wpas.group_request("SET_NETWORK 0 scan_freq 2412")
194 dev[0].group_request("DISASSOCIATE " + wpas.p2p_interface_addr())
195 ev = wpas.wait_group_event(["CTRL-EVENT-DISCONNECT"])
197 raise Exception("Did not see disconnect event on P2P group interface")
198 dev[0].remove_group()
200 ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
202 raise Exception("Scan not started")
203 ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
205 raise Exception("Scan not completed")
207 hwsim_utils.test_connectivity(wpas, hapd)
209 ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=0.1)
211 raise Exception("Unexpected disconnection event received from hostapd")
212 ev = wpas.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.1)
214 raise Exception("Unexpected disconnection event received from wpa_supplicant")
216 wpas.request("DISCONNECT")
217 wpas.wait_disconnected()
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)
226 wpas.request('P2P_FIND type=social')
227 ev = wpas.wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
229 raise Exception("Peer not found")
230 ev = wpas.wait_event(["P2P-DEVICE-FOUND"], timeout=0.1)
232 raise Exception("Unexpected P2P-DEVICE-FOUND event on station interface")
235 pin = wpas.wps_read_pin()
236 dev[0].p2p_go_neg_auth(wpas.p2p_dev_addr(), pin, "enter", go_intent=14,
238 wpas.request('P2P_STOP_FIND')
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")
243 ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
245 raise Exception("Group formation timed out")
246 wpas.group_form_result(ev)
249 ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
251 raise Exception("Group formation timed out(2)")
252 dev[0].group_form_result(ev)
254 dev[0].remove_group()
255 wpas.wait_go_ending_session()
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)
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)
268 raise Exception("Group formation timed out")
269 res = wpas.group_form_result(ev)
271 logger.info("Group results: " + str(res))
273 if not res['ifname'].startswith('p2p-' + iface + '-'):
274 raise Exception("Unexpected group ifname: " + res['ifname'])
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()
285 dev[0].discover_peer(addr5)
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)
293 raise Exception("GO Negotiation did not succeed")
294 ev = dev[0].wait_global_event(["WPS-SUCCESS"], timeout=10)
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")
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)
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)
308 raise Exception("Group formation timeout not seen on client")
309 ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=5)
311 raise Exception("Group removal not seen on client")
312 wpas.p2p_cancel_ext_listen()
314 ifaces = wpas.global_request("INTERFACES")
315 logger.info("Remaining interfaces: " + ifaces)
317 if "p2p-" + iface + "-" in ifaces:
318 raise Exception("Group interface still present after failure")
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()
328 dev[0].discover_peer(addr5)
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)
336 raise Exception("GO Negotiation did not succeed")
337 ev = dev[0].wait_global_event(["WPS-SUCCESS"], timeout=10)
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")
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)
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)
352 raise Exception("Group formation timeout not seen on GO")
353 ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=5)
355 raise Exception("Group removal not seen on GO")
356 wpas.p2p_cancel_ext_listen()
358 ifaces = wpas.global_request("INTERFACES")
359 logger.info("Remaining interfaces: " + ifaces)
361 if "p2p-" + iface + "-" in ifaces:
362 raise Exception("Group interface still present after failure")
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)
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')
375 dev[0].scan_for_bss(bssid, res['freq'])
376 connect_cli(wpas, dev[0], freq=res['freq'])
377 terminate_group(wpas, dev[0])
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")
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')
391 dev[0].scan_for_bss(bssid, res['freq'])
392 connect_cli(wpas, dev[0], freq=res['freq'])
393 terminate_group(wpas, dev[0])
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)
401 res = dev[0].p2p_start_go()
402 bssid = dev[0].get_group_status_field('bssid')
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'])
409 terminate_group(dev[0], wpas)
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")
418 res = dev[0].p2p_start_go()
419 bssid = dev[0].get_group_status_field('bssid')
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'])
426 terminate_group(dev[0], wpas)