Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / test_p2p_grpform.py
1 # P2P group formation test cases
2 # Copyright (c) 2013-2014, 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 import threading
11 import Queue
12 import os
13
14 import hostapd
15 import hwsim_utils
16 import utils
17 from utils import HwsimSkip
18 from wpasupplicant import WpaSupplicant
19
20 def check_grpform_results(i_res, r_res):
21     if i_res['result'] != 'success' or r_res['result'] != 'success':
22         raise Exception("Failed group formation")
23     if i_res['ssid'] != r_res['ssid']:
24         raise Exception("SSID mismatch")
25     if i_res['freq'] != r_res['freq']:
26         raise Exception("freq mismatch")
27     if 'go_neg_freq' in r_res and i_res['go_neg_freq'] != r_res['go_neg_freq']:
28         raise Exception("go_neg_freq mismatch")
29     if i_res['freq'] != i_res['go_neg_freq']:
30         raise Exception("freq/go_neg_freq mismatch")
31     if i_res['role'] != i_res['go_neg_role']:
32         raise Exception("role/go_neg_role mismatch")
33     if 'go_neg_role' in r_res and r_res['role'] != r_res['go_neg_role']:
34         raise Exception("role/go_neg_role mismatch")
35     if i_res['go_dev_addr'] != r_res['go_dev_addr']:
36         raise Exception("GO Device Address mismatch")
37
38 def go_neg_init(i_dev, r_dev, pin, i_method, i_intent, res):
39     logger.debug("Initiate GO Negotiation from i_dev")
40     try:
41         i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), pin, i_method, timeout=20, go_intent=i_intent)
42         logger.debug("i_res: " + str(i_res))
43     except Exception, e:
44         i_res = None
45         logger.info("go_neg_init thread caught an exception from p2p_go_neg_init: " + str(e))
46     res.put(i_res)
47
48 def go_neg_pin(i_dev, r_dev, i_intent=None, r_intent=None, i_method='enter', r_method='display'):
49     r_dev.p2p_listen()
50     i_dev.p2p_listen()
51     pin = r_dev.wps_read_pin()
52     logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
53     r_dev.dump_monitor()
54     res = Queue.Queue()
55     t = threading.Thread(target=go_neg_init, args=(i_dev, r_dev, pin, i_method, i_intent, res))
56     t.start()
57     logger.debug("Wait for GO Negotiation Request on r_dev")
58     ev = r_dev.wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
59     if ev is None:
60         raise Exception("GO Negotiation timed out")
61     r_dev.dump_monitor()
62     logger.debug("Re-initiate GO Negotiation from r_dev")
63     r_res = r_dev.p2p_go_neg_init(i_dev.p2p_dev_addr(), pin, r_method, go_intent=r_intent, timeout=20)
64     logger.debug("r_res: " + str(r_res))
65     r_dev.dump_monitor()
66     t.join()
67     i_res = res.get()
68     if i_res is None:
69         raise Exception("go_neg_init thread failed")
70     logger.debug("i_res: " + str(i_res))
71     logger.info("Group formed")
72     hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
73     i_dev.dump_monitor()
74     return [i_res, r_res]
75
76 def go_neg_pin_authorized(i_dev, r_dev, i_intent=None, r_intent=None, expect_failure=False, i_go_neg_status=None, i_method='enter', r_method='display', test_data=True, i_freq=None, r_freq=None):
77     i_dev.p2p_listen()
78     pin = r_dev.wps_read_pin()
79     logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
80     r_dev.p2p_go_neg_auth(i_dev.p2p_dev_addr(), pin, r_method, go_intent=r_intent, freq=r_freq)
81     r_dev.p2p_listen()
82     i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), pin, i_method, timeout=20, go_intent=i_intent, expect_failure=expect_failure, freq=i_freq)
83     r_res = r_dev.p2p_go_neg_auth_result(expect_failure=expect_failure)
84     logger.debug("i_res: " + str(i_res))
85     logger.debug("r_res: " + str(r_res))
86     r_dev.dump_monitor()
87     i_dev.dump_monitor()
88     if i_go_neg_status:
89         if i_res['result'] != 'go-neg-failed':
90             raise Exception("Expected GO Negotiation failure not reported")
91         if i_res['status'] != i_go_neg_status:
92             raise Exception("Expected GO Negotiation status not seen")
93     if expect_failure:
94         return
95     logger.info("Group formed")
96     if test_data:
97         hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
98     return [i_res, r_res]
99
100 def go_neg_init_pbc(i_dev, r_dev, i_intent, res, freq, provdisc):
101     logger.debug("Initiate GO Negotiation from i_dev")
102     try:
103         i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), None, "pbc",
104                                       timeout=20, go_intent=i_intent, freq=freq,
105                                       provdisc=provdisc)
106         logger.debug("i_res: " + str(i_res))
107     except Exception, e:
108         i_res = None
109         logger.info("go_neg_init_pbc thread caught an exception from p2p_go_neg_init: " + str(e))
110     res.put(i_res)
111
112 def go_neg_pbc(i_dev, r_dev, i_intent=None, r_intent=None, i_freq=None, r_freq=None, provdisc=False, r_listen=False):
113     if r_listen:
114         r_dev.p2p_listen()
115     else:
116         r_dev.p2p_find(social=True)
117     i_dev.p2p_find(social=True)
118     logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
119     r_dev.dump_monitor()
120     res = Queue.Queue()
121     t = threading.Thread(target=go_neg_init_pbc, args=(i_dev, r_dev, i_intent, res, i_freq, provdisc))
122     t.start()
123     logger.debug("Wait for GO Negotiation Request on r_dev")
124     ev = r_dev.wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
125     if ev is None:
126         raise Exception("GO Negotiation timed out")
127     r_dev.dump_monitor()
128     # Allow some time for the GO Neg Resp to go out before initializing new
129     # GO Negotiation.
130     time.sleep(0.2)
131     logger.debug("Re-initiate GO Negotiation from r_dev")
132     r_res = r_dev.p2p_go_neg_init(i_dev.p2p_dev_addr(), None, "pbc",
133                                   go_intent=r_intent, timeout=20, freq=r_freq)
134     logger.debug("r_res: " + str(r_res))
135     r_dev.dump_monitor()
136     t.join()
137     i_res = res.get()
138     if i_res is None:
139         raise Exception("go_neg_init_pbc thread failed")
140     logger.debug("i_res: " + str(i_res))
141     logger.info("Group formed")
142     hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
143     i_dev.dump_monitor()
144     return [i_res, r_res]
145
146 def go_neg_pbc_authorized(i_dev, r_dev, i_intent=None, r_intent=None,
147                           expect_failure=False, i_freq=None, r_freq=None):
148     i_dev.p2p_listen()
149     logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
150     r_dev.p2p_go_neg_auth(i_dev.p2p_dev_addr(), None, "pbc",
151                           go_intent=r_intent, freq=r_freq)
152     r_dev.p2p_listen()
153     i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), None, "pbc", timeout=20,
154                                   go_intent=i_intent,
155                                   expect_failure=expect_failure, freq=i_freq)
156     r_res = r_dev.p2p_go_neg_auth_result(expect_failure=expect_failure)
157     logger.debug("i_res: " + str(i_res))
158     logger.debug("r_res: " + str(r_res))
159     r_dev.dump_monitor()
160     i_dev.dump_monitor()
161     if expect_failure:
162         return
163     logger.info("Group formed")
164     return [i_res, r_res]
165
166 def remove_group(dev1, dev2):
167     dev1.remove_group()
168     try:
169         dev2.remove_group()
170     except:
171         pass
172
173 def test_grpform(dev):
174     """P2P group formation using PIN and authorized connection (init -> GO)"""
175     try:
176         dev[0].global_request("SET p2p_group_idle 2")
177         [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
178                                                r_dev=dev[1], r_intent=0)
179         check_grpform_results(i_res, r_res)
180         dev[1].remove_group()
181         ev = dev[0].wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
182         if ev is None:
183             raise Exception("GO did not remove group on idle timeout")
184         if "GO reason=IDLE" not in ev:
185             raise Exception("Unexpected group removal event: " + ev)
186     finally:
187         dev[0].global_request("SET p2p_group_idle 0")
188
189 def test_grpform_a(dev):
190     """P2P group formation using PIN and authorized connection (init -> GO) (init: group iface)"""
191     dev[0].global_request("SET p2p_no_group_iface 0")
192     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
193                                            r_dev=dev[1], r_intent=0)
194     if "p2p-wlan" not in i_res['ifname']:
195         raise Exception("Unexpected group interface name")
196     check_grpform_results(i_res, r_res)
197     remove_group(dev[0], dev[1])
198     if i_res['ifname'] in utils.get_ifnames():
199         raise Exception("Group interface netdev was not removed")
200
201 def test_grpform_b(dev):
202     """P2P group formation using PIN and authorized connection (init -> GO) (resp: group iface)"""
203     dev[1].global_request("SET p2p_no_group_iface 0")
204     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
205                                            r_dev=dev[1], r_intent=0)
206     if "p2p-wlan" not in r_res['ifname']:
207         raise Exception("Unexpected group interface name")
208     check_grpform_results(i_res, r_res)
209     remove_group(dev[0], dev[1])
210     if r_res['ifname'] in utils.get_ifnames():
211         raise Exception("Group interface netdev was not removed")
212
213 def test_grpform_c(dev):
214     """P2P group formation using PIN and authorized connection (init -> GO) (group iface)"""
215     dev[0].global_request("SET p2p_no_group_iface 0")
216     dev[1].global_request("SET p2p_no_group_iface 0")
217     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
218                                            r_dev=dev[1], r_intent=0)
219     if "p2p-wlan" not in i_res['ifname']:
220         raise Exception("Unexpected group interface name")
221     if "p2p-wlan" not in r_res['ifname']:
222         raise Exception("Unexpected group interface name")
223     check_grpform_results(i_res, r_res)
224     remove_group(dev[0], dev[1])
225     if i_res['ifname'] in utils.get_ifnames():
226         raise Exception("Group interface netdev was not removed")
227     if r_res['ifname'] in utils.get_ifnames():
228         raise Exception("Group interface netdev was not removed")
229
230 def test_grpform2(dev):
231     """P2P group formation using PIN and authorized connection (resp -> GO)"""
232     go_neg_pin_authorized(i_dev=dev[0], i_intent=0, r_dev=dev[1], r_intent=15)
233     remove_group(dev[0], dev[1])
234
235 def test_grpform2_c(dev):
236     """P2P group formation using PIN and authorized connection (resp -> GO) (group iface)"""
237     dev[0].global_request("SET p2p_no_group_iface 0")
238     dev[1].global_request("SET p2p_no_group_iface 0")
239     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0, r_dev=dev[1], r_intent=15)
240     remove_group(dev[0], dev[1])
241     if i_res['ifname'] in utils.get_ifnames():
242         raise Exception("Group interface netdev was not removed")
243     if r_res['ifname'] in utils.get_ifnames():
244         raise Exception("Group interface netdev was not removed")
245
246 def test_grpform3(dev):
247     """P2P group formation using PIN and re-init GO Negotiation"""
248     go_neg_pin(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
249     remove_group(dev[0], dev[1])
250
251 def test_grpform3_c(dev):
252     """P2P group formation using PIN and re-init GO Negotiation (group iface)"""
253     dev[0].global_request("SET p2p_no_group_iface 0")
254     dev[1].global_request("SET p2p_no_group_iface 0")
255     [i_res, r_res] = go_neg_pin(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
256     remove_group(dev[0], dev[1])
257     if i_res['ifname'] in utils.get_ifnames():
258         raise Exception("Group interface netdev was not removed")
259     if r_res['ifname'] in utils.get_ifnames():
260         raise Exception("Group interface netdev was not removed")
261
262 def test_grpform4(dev):
263     """P2P group formation response during p2p_find"""
264     addr1 = dev[1].p2p_dev_addr()
265     dev[1].p2p_listen()
266     dev[0].discover_peer(addr1)
267     dev[1].p2p_find(social=True)
268     time.sleep(0.4)
269     dev[0].global_request("P2P_CONNECT " + addr1 + " 12345670 display")
270     ev = dev[1].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
271     if ev is None:
272         raise Exception("GO Negotiation RX timed out")
273     time.sleep(0.5)
274     dev[1].p2p_stop_find()
275     dev[0].p2p_stop_find()
276
277 def test_grpform_pbc(dev):
278     """P2P group formation using PBC and re-init GO Negotiation"""
279     [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
280     check_grpform_results(i_res, r_res)
281     if i_res['role'] != 'GO' or r_res['role'] != 'client':
282         raise Exception("Unexpected device roles")
283     remove_group(dev[0], dev[1])
284
285 def test_grpform_pd(dev):
286     """P2P group formation with PD-before-GO-Neg workaround"""
287     [i_res, r_res] = go_neg_pbc(i_dev=dev[0], provdisc=True, r_dev=dev[1], r_listen=True)
288     check_grpform_results(i_res, r_res)
289     remove_group(dev[0], dev[1])
290
291 def test_grpform_ext_listen(dev):
292     """P2P group formation with extended listen timing enabled"""
293     addr0 = dev[0].p2p_dev_addr()
294     try:
295         if "FAIL" not in dev[0].global_request("P2P_EXT_LISTEN 100"):
296             raise Exception("Invalid P2P_EXT_LISTEN accepted")
297         if "OK" not in dev[0].global_request("P2P_EXT_LISTEN 300 1000"):
298             raise Exception("Failed to set extended listen timing")
299         if "OK" not in dev[1].global_request("P2P_EXT_LISTEN 200 40000"):
300             raise Exception("Failed to set extended listen timing")
301         [i_res, r_res] = go_neg_pbc(i_dev=dev[0], provdisc=True, r_dev=dev[1],
302                                     r_listen=True, i_freq="2417", r_freq="2417",
303                                     i_intent=1, r_intent=15)
304         check_grpform_results(i_res, r_res)
305         peer1 = dev[0].get_peer(dev[1].p2p_dev_addr())
306         if peer1['ext_listen_interval'] != "40000":
307             raise Exception("Extended listen interval not discovered correctly")
308         if peer1['ext_listen_period'] != "200":
309             raise Exception("Extended listen period not discovered correctly")
310         peer0 = dev[1].get_peer(dev[0].p2p_dev_addr())
311         if peer0['ext_listen_interval'] != "1000":
312             raise Exception("Extended listen interval not discovered correctly")
313         if peer0['ext_listen_period'] != "300":
314             raise Exception("Extended listen period not discovered correctly")
315         if not dev[2].discover_peer(addr0):
316             raise Exception("Could not discover peer during ext listen")
317         remove_group(dev[0], dev[1])
318     finally:
319         if "OK" not in dev[0].global_request("P2P_EXT_LISTEN"):
320             raise Exception("Failed to clear extended listen timing")
321         if "OK" not in dev[1].global_request("P2P_EXT_LISTEN"):
322             raise Exception("Failed to clear extended listen timing")
323
324 def test_grpform_ext_listen_oper(dev):
325     """P2P extended listen timing operations"""
326     try:
327         _test_grpform_ext_listen_oper(dev)
328     finally:
329         dev[0].global_request("P2P_EXT_LISTEN")
330
331 def _test_grpform_ext_listen_oper(dev):
332     addr0 = dev[0].p2p_dev_addr()
333     dev[0].global_request("SET p2p_no_group_iface 0")
334     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
335     wpas.interface_add("wlan5")
336     addr1 = wpas.p2p_dev_addr()
337     wpas.request("P2P_SET listen_channel 1")
338     wpas.global_request("SET p2p_no_group_iface 0")
339     wpas.request("P2P_LISTEN")
340     if not dev[0].discover_peer(addr1):
341         raise Exception("Could not discover peer")
342     dev[0].request("P2P_LISTEN")
343     if not wpas.discover_peer(addr0):
344         raise Exception("Could not discover peer (2)")
345
346     dev[0].global_request("P2P_EXT_LISTEN 300 500")
347     dev[0].global_request("P2P_CONNECT " + addr1 + " 12345670 display auth go_intent=0 freq=2417")
348     wpas.global_request("P2P_CONNECT " + addr0 + " 12345670 enter go_intent=15 freq=2417")
349     ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=15)
350     if ev is None:
351         raise Exception("GO Negotiation failed")
352     ifaces = wpas.request("INTERFACES").splitlines()
353     iface = ifaces[0] if "p2p-wlan" in ifaces[0] else ifaces[1]
354     wpas.group_ifname = iface
355     if "OK" not in wpas.group_request("STOP_AP"):
356         raise Exception("STOP_AP failed")
357     wpas.group_request("SET ext_mgmt_frame_handling 1")
358     dev[1].p2p_find(social=True)
359     time.sleep(1)
360     if dev[1].peer_known(addr0):
361         raise Exception("Unexpected peer discovery")
362     ifaces = dev[0].request("INTERFACES").splitlines()
363     iface = ifaces[0] if "p2p-wlan" in ifaces[0] else ifaces[1]
364     if "OK" not in dev[0].global_request("P2P_GROUP_REMOVE " + iface):
365         raise Exception("Failed to request group removal")
366     wpas.remove_group()
367
368     count = 0
369     timeout = 15
370     found = False
371     while count < timeout * 4:
372         time.sleep(0.25)
373         count = count + 1
374         if dev[1].peer_known(addr0):
375             found = True
376             break
377     dev[1].p2p_stop_find()
378     if not found:
379         raise Exception("Could not discover peer that was supposed to use extended listen")
380
381 def test_both_go_intent_15(dev):
382     """P2P GO Negotiation with both devices using GO intent 15"""
383     go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=15, expect_failure=True, i_go_neg_status=9)
384
385 def test_both_go_neg_display(dev):
386     """P2P GO Negotiation with both devices trying to display PIN"""
387     go_neg_pin_authorized(i_dev=dev[0], r_dev=dev[1], expect_failure=True, i_go_neg_status=10, i_method='display', r_method='display')
388
389 def test_both_go_neg_enter(dev):
390     """P2P GO Negotiation with both devices trying to enter PIN"""
391     go_neg_pin_authorized(i_dev=dev[0], r_dev=dev[1], expect_failure=True, i_go_neg_status=10, i_method='enter', r_method='enter')
392
393 def test_go_neg_pbc_vs_pin(dev):
394     """P2P GO Negotiation with one device using PBC and the other PIN"""
395     addr0 = dev[0].p2p_dev_addr()
396     addr1 = dev[1].p2p_dev_addr()
397     dev[1].p2p_listen()
398     if not dev[0].discover_peer(addr1):
399         raise Exception("Could not discover peer")
400     dev[0].p2p_listen()
401     if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " pbc auth"):
402         raise Exception("Failed to authorize GO Neg")
403     if not dev[1].discover_peer(addr0):
404         raise Exception("Could not discover peer")
405     if "OK" not in dev[1].request("P2P_CONNECT " + addr0 + " 12345670 display"):
406         raise Exception("Failed to initiate GO Neg")
407     ev = dev[1].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=10)
408     if ev is None:
409         raise Exception("GO Negotiation failure timed out")
410     if "status=10" not in ev:
411         raise Exception("Unexpected failure reason: " + ev)
412
413 def test_go_neg_pin_vs_pbc(dev):
414     """P2P GO Negotiation with one device using PIN and the other PBC"""
415     addr0 = dev[0].p2p_dev_addr()
416     addr1 = dev[1].p2p_dev_addr()
417     dev[1].p2p_listen()
418     if not dev[0].discover_peer(addr1):
419         raise Exception("Could not discover peer")
420     dev[0].p2p_listen()
421     if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " 12345670 display auth"):
422         raise Exception("Failed to authorize GO Neg")
423     if not dev[1].discover_peer(addr0):
424         raise Exception("Could not discover peer")
425     if "OK" not in dev[1].request("P2P_CONNECT " + addr0 + " pbc"):
426         raise Exception("Failed to initiate GO Neg")
427     ev = dev[1].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=10)
428     if ev is None:
429         raise Exception("GO Negotiation failure timed out")
430     if "status=10" not in ev:
431         raise Exception("Unexpected failure reason: " + ev)
432
433 def test_grpform_per_sta_psk(dev):
434     """P2P group formation with per-STA PSKs"""
435     dev[0].global_request("P2P_SET per_sta_psk 1")
436     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
437     check_grpform_results(i_res, r_res)
438
439     pin = dev[2].wps_read_pin()
440     dev[0].p2p_go_authorize_client(pin)
441     c_res = dev[2].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
442     check_grpform_results(i_res, c_res)
443
444     if r_res['psk'] == c_res['psk']:
445         raise Exception("Same PSK assigned for both clients")
446
447     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
448
449     dev[0].remove_group()
450     dev[1].wait_go_ending_session()
451     dev[2].wait_go_ending_session()
452
453 def test_grpform_per_sta_psk_wps(dev):
454     """P2P group formation with per-STA PSKs with non-P2P WPS STA"""
455     dev[0].global_request("P2P_SET per_sta_psk 1")
456     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
457     check_grpform_results(i_res, r_res)
458
459     dev[0].p2p_go_authorize_client_pbc()
460     dev[2].request("WPS_PBC")
461     dev[2].wait_connected(timeout=30)
462
463     hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
464
465     dev[0].remove_group()
466     dev[2].request("DISCONNECT")
467     dev[1].wait_go_ending_session()
468
469 def test_grpform_force_chan_go(dev):
470     """P2P group formation forced channel selection by GO"""
471     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
472                                            i_freq=2432,
473                                            r_dev=dev[1], r_intent=0,
474                                            test_data=False)
475     check_grpform_results(i_res, r_res)
476     if i_res['freq'] != "2432":
477         raise Exception("Unexpected channel - did not follow GO's forced channel")
478     remove_group(dev[0], dev[1])
479
480 def test_grpform_force_chan_cli(dev):
481     """P2P group formation forced channel selection by client"""
482     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
483                                            i_freq=2417,
484                                            r_dev=dev[1], r_intent=15,
485                                            test_data=False)
486     check_grpform_results(i_res, r_res)
487     if i_res['freq'] != "2417":
488         raise Exception("Unexpected channel - did not follow GO's forced channel")
489     remove_group(dev[0], dev[1])
490
491 def test_grpform_force_chan_conflict(dev):
492     """P2P group formation fails due to forced channel mismatch"""
493     go_neg_pin_authorized(i_dev=dev[0], i_intent=0, i_freq=2422,
494                           r_dev=dev[1], r_intent=15, r_freq=2427,
495                           expect_failure=True, i_go_neg_status=7)
496
497 def test_grpform_pref_chan_go(dev):
498     """P2P group formation preferred channel selection by GO"""
499     dev[0].request("SET p2p_pref_chan 81:7")
500     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
501                                            r_dev=dev[1], r_intent=0,
502                                            test_data=False)
503     check_grpform_results(i_res, r_res)
504     if i_res['freq'] != "2442":
505         raise Exception("Unexpected channel - did not follow GO's p2p_pref_chan")
506     remove_group(dev[0], dev[1])
507
508 def test_grpform_pref_chan_go_overridden(dev):
509     """P2P group formation preferred channel selection by GO overridden by client"""
510     dev[1].request("SET p2p_pref_chan 81:7")
511     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
512                                            i_freq=2422,
513                                            r_dev=dev[1], r_intent=15,
514                                            test_data=False)
515     check_grpform_results(i_res, r_res)
516     if i_res['freq'] != "2422":
517         raise Exception("Unexpected channel - did not follow client's forced channel")
518     remove_group(dev[0], dev[1])
519
520 def test_grpform_no_go_freq_forcing_chan(dev):
521     """P2P group formation with no-GO freq forcing channel"""
522     dev[1].request("SET p2p_no_go_freq 100-200,300,4000-6000")
523     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
524                                            r_dev=dev[1], r_intent=15,
525                                            test_data=False)
526     check_grpform_results(i_res, r_res)
527     if int(i_res['freq']) > 4000:
528         raise Exception("Unexpected channel - did not follow no-GO freq")
529     remove_group(dev[0], dev[1])
530
531 def test_grpform_no_go_freq_conflict(dev):
532     """P2P group formation fails due to no-GO range forced by client"""
533     dev[1].request("SET p2p_no_go_freq 2000-3000")
534     go_neg_pin_authorized(i_dev=dev[0], i_intent=0, i_freq=2422,
535                           r_dev=dev[1], r_intent=15,
536                           expect_failure=True, i_go_neg_status=7)
537
538 def test_grpform_no_5ghz_world_roaming(dev):
539     """P2P group formation with world roaming regulatory"""
540     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
541                                            r_dev=dev[1], r_intent=15,
542                                            test_data=False)
543     check_grpform_results(i_res, r_res)
544     if int(i_res['freq']) > 4000:
545         raise Exception("Unexpected channel - did not follow world roaming rules")
546     remove_group(dev[0], dev[1])
547
548 def test_grpform_no_5ghz_add_cli(dev):
549     """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1"""
550     dev[0].request("SET p2p_add_cli_chan 1")
551     dev[1].request("SET p2p_add_cli_chan 1")
552     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
553                                            r_dev=dev[1], r_intent=14,
554                                            test_data=False)
555     check_grpform_results(i_res, r_res)
556     if int(i_res['freq']) > 4000:
557         raise Exception("Unexpected channel - did not follow world roaming rules")
558     remove_group(dev[0], dev[1])
559
560 def test_grpform_no_5ghz_add_cli2(dev):
561     """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (reverse)"""
562     dev[0].request("SET p2p_add_cli_chan 1")
563     dev[1].request("SET p2p_add_cli_chan 1")
564     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=14,
565                                            r_dev=dev[1], r_intent=0,
566                                            test_data=False)
567     check_grpform_results(i_res, r_res)
568     if int(i_res['freq']) > 4000:
569         raise Exception("Unexpected channel - did not follow world roaming rules")
570     remove_group(dev[0], dev[1])
571
572 def test_grpform_no_5ghz_add_cli3(dev):
573     """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (intent 15)"""
574     dev[0].request("SET p2p_add_cli_chan 1")
575     dev[1].request("SET p2p_add_cli_chan 1")
576     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
577                                            r_dev=dev[1], r_intent=15,
578                                            test_data=False)
579     check_grpform_results(i_res, r_res)
580     if int(i_res['freq']) > 4000:
581         raise Exception("Unexpected channel - did not follow world roaming rules")
582     remove_group(dev[0], dev[1])
583
584 def test_grpform_no_5ghz_add_cli4(dev):
585     """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (reverse; intent 15)"""
586     dev[0].request("SET p2p_add_cli_chan 1")
587     dev[1].request("SET p2p_add_cli_chan 1")
588     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
589                                            r_dev=dev[1], r_intent=0,
590                                            test_data=False)
591     check_grpform_results(i_res, r_res)
592     if int(i_res['freq']) > 4000:
593         raise Exception("Unexpected channel - did not follow world roaming rules")
594     remove_group(dev[0], dev[1])
595
596 def test_grpform_incorrect_pin(dev):
597     """P2P GO Negotiation with incorrect PIN"""
598     dev[1].p2p_listen()
599     addr1 = dev[1].p2p_dev_addr()
600     if not dev[0].discover_peer(addr1):
601         raise Exception("Peer not found")
602     res = dev[1].global_request("P2P_CONNECT " + dev[0].p2p_dev_addr() + " pin auth go_intent=0")
603     if "FAIL" in res:
604         raise Exception("P2P_CONNECT failed to generate PIN")
605     logger.info("PIN from P2P_CONNECT: " + res)
606     dev[0].global_request("P2P_CONNECT " + addr1 + " 00000000 enter go_intent=15")
607     ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=15)
608     if ev is None:
609         raise Exception("GO Negotiation did not complete successfully(0)")
610     ev = dev[1].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=15)
611     if ev is None:
612         raise Exception("GO Negotiation did not complete successfully(1)")
613     ev = dev[1].wait_global_event(["WPS-FAIL"], timeout=15)
614     if ev is None:
615         raise Exception("WPS failure not reported(1)")
616     if "msg=8 config_error=18" not in ev:
617         raise Exception("Unexpected WPS failure(1): " + ev)
618     ev = dev[0].wait_global_event(["WPS-FAIL"], timeout=15)
619     if ev is None:
620         raise Exception("WPS failure not reported")
621     if "msg=8 config_error=18" not in ev:
622         raise Exception("Unexpected WPS failure: " + ev)
623     ev = dev[1].wait_global_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=10)
624     if ev is None:
625         raise Exception("Group formation failure timed out")
626     ev = dev[0].wait_global_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=5)
627     if ev is None:
628         raise Exception("Group formation failure timed out")
629
630 def test_grpform_reject(dev):
631     """User rejecting group formation attempt by a P2P peer"""
632     addr0 = dev[0].p2p_dev_addr()
633     dev[0].p2p_listen()
634     dev[1].p2p_go_neg_init(addr0, None, "pbc")
635     ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
636     if ev is None:
637         raise Exception("GO Negotiation timed out")
638     if "OK" in dev[0].global_request("P2P_REJECT foo"):
639         raise Exception("Invalid P2P_REJECT accepted")
640     if "FAIL" in dev[0].global_request("P2P_REJECT " + ev.split(' ')[1]):
641         raise Exception("P2P_REJECT failed")
642     dev[1].request("P2P_STOP_FIND")
643     dev[1].p2p_go_neg_init(addr0, None, "pbc")
644     ev = dev[1].wait_global_event(["GO-NEG-FAILURE"], timeout=10)
645     if ev is None:
646         raise Exception("Rejection not reported")
647     if "status=11" not in ev:
648         raise Exception("Unexpected status code in rejection")
649
650 def test_grpform_pd_no_probe_resp(dev):
651     """GO Negotiation after PD, but no Probe Response"""
652     addr0 = dev[0].p2p_dev_addr()
653     addr1 = dev[1].p2p_dev_addr()
654     dev[0].p2p_listen()
655     if not dev[1].discover_peer(addr0):
656         raise Exception("Peer not found")
657     dev[1].p2p_stop_find()
658     dev[0].p2p_stop_find()
659     peer = dev[0].get_peer(addr1)
660     if peer['listen_freq'] == '0':
661         raise Exception("Peer listen frequency not learned from Probe Request")
662     time.sleep(0.3)
663     dev[0].request("P2P_FLUSH")
664     dev[0].p2p_listen()
665     dev[1].global_request("P2P_PROV_DISC " + addr0 + " display")
666     ev = dev[0].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=5)
667     if ev is None:
668         raise Exception("PD Request timed out")
669     ev = dev[1].wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout=5)
670     if ev is None:
671         raise Exception("PD Response timed out")
672     peer = dev[0].get_peer(addr1)
673     if peer['listen_freq'] != '0':
674         raise Exception("Peer listen frequency learned unexpectedly from PD Request")
675
676     pin = dev[0].wps_read_pin()
677     if "FAIL" in dev[1].global_request("P2P_CONNECT " + addr0 + " " + pin + " enter"):
678         raise Exception("P2P_CONNECT on initiator failed")
679     ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=5)
680     if ev is None:
681         raise Exception("GO Negotiation start timed out")
682     peer = dev[0].get_peer(addr1)
683     if peer['listen_freq'] == '0':
684         raise Exception("Peer listen frequency not learned from PD followed by GO Neg Req")
685     if "FAIL" in dev[0].global_request("P2P_CONNECT " + addr1 + " " + pin + " display"):
686         raise Exception("P2P_CONNECT on responder failed")
687     ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
688     if ev is None:
689         raise Exception("Group formation timed out")
690     ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
691     if ev is None:
692         raise Exception("Group formation timed out")
693
694 def test_go_neg_two_peers(dev):
695     """P2P GO Negotiation rejected due to already started negotiation with another peer"""
696     addr0 = dev[0].p2p_dev_addr()
697     addr1 = dev[1].p2p_dev_addr()
698     addr2 = dev[2].p2p_dev_addr()
699     dev[1].p2p_listen()
700     dev[2].p2p_listen()
701     if not dev[0].discover_peer(addr1):
702         raise Exception("Could not discover peer")
703     if not dev[0].discover_peer(addr2):
704         raise Exception("Could not discover peer")
705     if "OK" not in dev[0].request("P2P_CONNECT " + addr2 + " pbc auth"):
706         raise Exception("Failed to authorize GO Neg")
707     dev[0].p2p_listen()
708     if not dev[2].discover_peer(addr0):
709         raise Exception("Could not discover peer")
710     if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " pbc"):
711         raise Exception("Failed to initiate GO Neg")
712     ev = dev[1].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=5)
713     if ev is None:
714         raise Exception("timeout on GO Neg RX event")
715     dev[2].request("P2P_CONNECT " + addr0 + " pbc")
716     ev = dev[2].wait_global_event(["GO-NEG-FAILURE"], timeout=10)
717     if ev is None:
718         raise Exception("Rejection not reported")
719     if "status=5" not in ev:
720         raise Exception("Unexpected status code in rejection: " + ev)
721
722 def clear_pbc_overlap(dev, ifname):
723     hapd_global = hostapd.HostapdGlobal()
724     hapd_global.remove(ifname)
725     dev[0].request("P2P_CANCEL")
726     dev[1].request("P2P_CANCEL")
727     dev[0].p2p_stop_find()
728     dev[1].p2p_stop_find()
729     dev[0].dump_monitor()
730     dev[1].dump_monitor()
731     time.sleep(0.1)
732     dev[0].flush_scan_cache()
733     dev[1].flush_scan_cache()
734     time.sleep(0.1)
735
736 def test_grpform_pbc_overlap(dev, apdev):
737     """P2P group formation during PBC overlap"""
738     params = { "ssid": "wps", "eap_server": "1", "wps_state": "1" }
739     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
740     hapd.request("WPS_PBC")
741     time.sleep(0.1)
742
743     # Since P2P Client scan case is now optimzied to use a specific SSID, the
744     # WPS AP will not reply to that and the scan after GO Negotiation can quite
745     # likely miss the AP due to dwell time being short enoguh to miss the Beacon
746     # frame. This has made the test case somewhat pointless, but keep it here
747     # for now with an additional scan to confirm that PBC detection works if
748     # there is a BSS entry for a overlapping AP.
749     for i in range(0, 5):
750         dev[0].scan(freq="2412")
751         if dev[0].get_bss(apdev[0]['bssid']) is not None:
752             break
753
754     addr0 = dev[0].p2p_dev_addr()
755     addr1 = dev[1].p2p_dev_addr()
756     dev[0].p2p_listen()
757     if not dev[1].discover_peer(addr0):
758         raise Exception("Could not discover peer")
759     dev[1].p2p_listen()
760     if not dev[0].discover_peer(addr1):
761         raise Exception("Could not discover peer")
762     dev[0].p2p_listen()
763     if "OK" not in dev[0].global_request("P2P_CONNECT " + addr1 + " pbc auth go_intent=0"):
764         raise Exception("Failed to authorize GO Neg")
765     if "OK" not in dev[1].global_request("P2P_CONNECT " + addr0 + " pbc go_intent=15 freq=2412"):
766         raise Exception("Failed to initiate GO Neg")
767     ev = dev[0].wait_global_event(["WPS-OVERLAP-DETECTED"], timeout=15)
768     if ev is None:
769         raise Exception("PBC overlap not reported")
770
771     clear_pbc_overlap(dev, apdev[0]['ifname'])
772
773 def test_grpform_pbc_overlap_group_iface(dev, apdev):
774     """P2P group formation during PBC overlap using group interfaces"""
775     # Note: Need to include P2P IE from the AP to get the P2P interface BSS
776     # update use this information.
777     params = { "ssid": "wps", "eap_server": "1", "wps_state": "1",
778                "beacon_int": "15", 'manage_p2p': '1' }
779     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
780     hapd.request("WPS_PBC")
781
782     dev[0].request("SET p2p_no_group_iface 0")
783     dev[1].request("SET p2p_no_group_iface 0")
784
785     addr0 = dev[0].p2p_dev_addr()
786     addr1 = dev[1].p2p_dev_addr()
787     dev[0].p2p_listen()
788     if not dev[1].discover_peer(addr0):
789         raise Exception("Could not discover peer")
790     dev[1].p2p_listen()
791     if not dev[0].discover_peer(addr1):
792         raise Exception("Could not discover peer")
793     dev[0].p2p_stop_find()
794     dev[0].scan(freq="2412")
795     dev[0].p2p_listen()
796     if "OK" not in dev[0].global_request("P2P_CONNECT " + addr1 + " pbc auth go_intent=0"):
797         raise Exception("Failed to authorize GO Neg")
798     if "OK" not in dev[1].global_request("P2P_CONNECT " + addr0 + " pbc go_intent=15 freq=2412"):
799         raise Exception("Failed to initiate GO Neg")
800     ev = dev[0].wait_global_event(["WPS-OVERLAP-DETECTED",
801                                    "P2P-GROUP-FORMATION-SUCCESS"], timeout=15)
802     if ev is None or "WPS-OVERLAP-DETECTED" not in ev:
803         # Do not report this as failure since the P2P group formation case
804         # using a separate group interface has limited chances of "seeing" the
805         # overlapping AP due to a per-SSID scan and no prior scan operations on
806         # the group interface.
807         logger.info("PBC overlap not reported")
808
809     clear_pbc_overlap(dev, apdev[0]['ifname'])
810
811 def test_grpform_goneg_fail_with_group_iface(dev):
812     """P2P group formation fails while using group interface"""
813     dev[0].request("SET p2p_no_group_iface 0")
814     dev[1].p2p_listen()
815     peer = dev[1].p2p_dev_addr()
816     if not dev[0].discover_peer(peer):
817         raise Exception("Peer " + peer + " not found")
818     if "OK" not in dev[1].request("P2P_REJECT " + dev[0].p2p_dev_addr()):
819         raise Exception("P2P_REJECT failed")
820     if "OK" not in dev[0].request("P2P_CONNECT " + peer + " pbc"):
821         raise Exception("P2P_CONNECT failed")
822     ev = dev[0].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=10)
823     if ev is None:
824         raise Exception("GO Negotiation failure timed out")
825
826 def test_grpform_cred_ready_timeout(dev, apdev, params):
827     """P2P GO Negotiation wait for credentials to become ready [long]"""
828     if not params['long']:
829         raise HwsimSkip("Skip test case with long duration due to --long not specified")
830
831     dev[1].p2p_listen()
832     addr1 = dev[1].p2p_dev_addr()
833     if not dev[0].discover_peer(addr1):
834         raise Exception("Peer " + addr1 + " not found")
835     if not dev[2].discover_peer(addr1):
836         raise Exception("Peer " + addr1 + " not found(2)")
837
838     start = os.times()[4]
839
840     cmd = "P2P_CONNECT " + addr1 + " 12345670 display"
841     if "OK" not in dev[0].global_request(cmd):
842         raise Exception("Failed to initiate GO Neg")
843
844     if "OK" not in dev[2].global_request(cmd):
845         raise Exception("Failed to initiate GO Neg(2)")
846
847     # First, check with p2p_find
848     ev = dev[2].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=30)
849     if ev is not None:
850         raise Exception("Too early GO Negotiation timeout reported(2)")
851     dev[2].dump_monitor()
852     logger.info("Starting p2p_find to change state")
853     dev[2].p2p_find()
854     ev = dev[2].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=100)
855     if ev is None:
856         raise Exception("GO Negotiation failure timed out(2)")
857     dev[2].dump_monitor()
858     end = os.times()[4]
859     logger.info("GO Negotiation wait time: {} seconds(2)".format(end - start))
860     if end - start < 120:
861         raise Exception("Too short GO Negotiation wait time(2): {}".format(end - start))
862
863     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
864     wpas.interface_add("wlan5")
865
866     wpas.p2p_listen()
867     ev = dev[2].wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
868     if ev is None:
869         raise Exception("Did not discover new device after GO Negotiation failure")
870     if wpas.p2p_dev_addr() not in ev:
871         raise Exception("Unexpected device found: " + ev)
872     dev[2].p2p_stop_find()
873     wpas.p2p_stop_find()
874
875     # Finally, verify without p2p_find
876     ev = dev[0].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=120)
877     if ev is None:
878         raise Exception("GO Negotiation failure timed out")
879     end = os.times()[4]
880     logger.info("GO Negotiation wait time: {} seconds".format(end - start))
881     if end - start < 120:
882         raise Exception("Too short GO Negotiation wait time: {}".format(end - start))
883
884 def test_grpform_no_wsc_done(dev):
885     """P2P group formation with WSC-Done not sent"""
886     addr0 = dev[0].p2p_dev_addr()
887     addr1 = dev[1].p2p_dev_addr()
888
889     for i in range(0, 2):
890         dev[0].request("SET ext_eapol_frame_io 1")
891         dev[1].request("SET ext_eapol_frame_io 1")
892         dev[0].p2p_listen()
893         dev[1].p2p_go_neg_auth(addr0, "12345670", "display", 0)
894         dev[1].p2p_listen()
895         dev[0].p2p_go_neg_init(addr1, "12345670", "enter", timeout=20,
896                                go_intent=15, wait_group=False)
897
898         mode = None
899         while True:
900             ev = dev[0].wait_event(["EAPOL-TX"], timeout=15)
901             if ev is None:
902                 raise Exception("Timeout on EAPOL-TX from GO")
903             if not mode:
904                 mode = dev[0].get_status_field("mode")
905             res = dev[1].request("EAPOL_RX " + addr0 + " " + ev.split(' ')[2])
906             if "OK" not in res:
907                 raise Exception("EAPOL_RX failed")
908             ev = dev[1].wait_event(["EAPOL-TX"], timeout=15)
909             if ev is None:
910                 raise Exception("Timeout on EAPOL-TX from P2P Client")
911             msg = ev.split(' ')[2]
912             if msg[46:56] == "102200010f":
913                 logger.info("Drop WSC_Done")
914                 dev[0].request("SET ext_eapol_frame_io 0")
915                 dev[1].request("SET ext_eapol_frame_io 0")
916                 # Fake EAP-Failure to complete session on the client
917                 id = msg[10:12]
918                 dev[1].request("EAPOL_RX " + addr0 + " 0300000404" + id + "0004")
919                 break
920             res = dev[0].request("EAPOL_RX " + addr1 + " " + msg)
921             if "OK" not in res:
922                 raise Exception("EAPOL_RX failed")
923
924         ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
925         if ev is None:
926             raise Exception("Group formation timed out on GO")
927         ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
928         if ev is None:
929             raise Exception("Group formation timed out on P2P Client")
930         dev[0].remove_group()
931         dev[1].wait_go_ending_session()
932
933         if mode != "P2P GO - group formation":
934             raise Exception("Unexpected mode on GO during group formation: " + mode)
935
936 def test_grpform_wait_peer(dev):
937     """P2P group formation wait for peer to become ready"""
938     addr0 = dev[0].p2p_dev_addr()
939     addr1 = dev[1].p2p_dev_addr()
940     dev[1].p2p_listen()
941     if not dev[0].discover_peer(addr1):
942         raise Exception("Peer " + addr1 + " not found")
943     dev[0].request("SET extra_roc_dur 500")
944     if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " 12345670 display go_intent=15"):
945         raise Exception("Failed to initiate GO Neg")
946     time.sleep(3)
947     dev[1].request("P2P_CONNECT " + addr0 + " 12345670 enter go_intent=0")
948
949     ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
950     if ev is None:
951         raise Exception("Group formation timed out")
952     dev[0].group_form_result(ev)
953
954     dev[0].request("SET extra_roc_dur 0")
955     ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
956     if ev is None:
957         raise Exception("Group formation timed out")
958     dev[0].remove_group()
959
960 def test_invalid_p2p_connect_command(dev):
961     """P2P_CONNECT error cases"""
962     id = dev[0].add_network()
963     for cmd in [ "foo",
964                  "00:11:22:33:44:55",
965                  "00:11:22:33:44:55 pbc persistent=123",
966                  "00:11:22:33:44:55 pbc persistent=%d" % id,
967                  "00:11:22:33:44:55 pbc go_intent=-1",
968                  "00:11:22:33:44:55 pbc go_intent=16",
969                  "00:11:22:33:44:55 pin",
970                  "00:11:22:33:44:55 pbc freq=0" ]:
971         if "FAIL" not in dev[0].request("P2P_CONNECT " + cmd):
972             raise Exception("Invalid P2P_CONNECT command accepted: " + cmd)
973
974     if "FAIL-INVALID-PIN" not in dev[0].request("P2P_CONNECT 00:11:22:33:44:55 1234567"):
975         raise Exception("Invalid PIN was not rejected")
976     if "FAIL-INVALID-PIN" not in dev[0].request("P2P_CONNECT 00:11:22:33:44:55 12345678a"):
977         raise Exception("Invalid PIN was not rejected")
978
979     if "FAIL-CHANNEL-UNSUPPORTED" not in dev[0].request("P2P_CONNECT 00:11:22:33:44:55 pin freq=3000"):
980         raise Exception("Unsupported channel not reported")
981
982 def test_p2p_unauthorize(dev):
983     """P2P_UNAUTHORIZE to unauthorize a peer"""
984     if "FAIL" not in dev[0].request("P2P_UNAUTHORIZE foo"):
985         raise Exception("Invalid P2P_UNAUTHORIZE accepted")
986     if "FAIL" not in dev[0].request("P2P_UNAUTHORIZE 00:11:22:33:44:55"):
987         raise Exception("P2P_UNAUTHORIZE for unknown peer accepted")
988
989     addr0 = dev[0].p2p_dev_addr()
990     addr1 = dev[1].p2p_dev_addr()
991     dev[1].p2p_listen()
992     pin = dev[0].wps_read_pin()
993     dev[0].p2p_go_neg_auth(addr1, pin, "display")
994     dev[0].p2p_listen()
995     if "OK" not in dev[0].request("P2P_UNAUTHORIZE " + addr1):
996         raise Exception("P2P_UNAUTHORIZE failed")
997     dev[1].p2p_go_neg_init(addr0, pin, "keypad", timeout=0)
998     ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=10)
999     if ev is None:
1000         raise Exception("No GO Negotiation Request RX reported")
1001
1002 def test_grpform_pbc_multiple(dev):
1003     """P2P group formation using PBC multiple times in a row"""
1004     try:
1005         dev[1].request("SET passive_scan 1")
1006         for i in range(5):
1007             [i_res, r_res] = go_neg_pbc_authorized(i_dev=dev[0], i_intent=15,
1008                                                    r_dev=dev[1], r_intent=0)
1009             remove_group(dev[0], dev[1])
1010     finally:
1011         dev[1].request("SET passive_scan 0")
1012         dev[1].flush_scan_cache()
1013
1014 def test_grpform_not_ready(dev):
1015     """Not ready for GO Negotiation (listen)"""
1016     addr0 = dev[0].p2p_dev_addr()
1017     addr2 = dev[2].p2p_dev_addr()
1018     dev[0].p2p_listen()
1019     if not dev[1].discover_peer(addr0):
1020         raise Exception("Could not discover peer")
1021     dev[1].global_request("P2P_CONNECT " + addr0 + " pbc")
1022     ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=5)
1023     if ev is None:
1024         raise Exception("No P2P-GO-NEG-REQUEST event")
1025     dev[0].dump_monitor()
1026     time.sleep(5)
1027     if not dev[2].discover_peer(addr0):
1028         raise Exception("Could not discover peer(2)")
1029     for i in range(3):
1030         dev[i].p2p_stop_find()
1031
1032 def test_grpform_not_ready2(dev):
1033     """Not ready for GO Negotiation (search)"""
1034     addr0 = dev[0].p2p_dev_addr()
1035     addr2 = dev[2].p2p_dev_addr()
1036     dev[0].p2p_find(social=True)
1037     if not dev[1].discover_peer(addr0):
1038         raise Exception("Could not discover peer")
1039     dev[1].global_request("P2P_CONNECT " + addr0 + " pbc")
1040     ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=5)
1041     if ev is None:
1042         raise Exception("No P2P-GO-NEG-REQUEST event")
1043     dev[0].dump_monitor()
1044     time.sleep(1)
1045     dev[2].p2p_listen()
1046     ev = dev[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
1047     if ev is None:
1048         raise Exception("Peer not discovered after GO Neg Resp(status=1) TX")
1049     if addr2 not in ev:
1050         raise Exception("Unexpected peer discovered: " + ev)
1051     for i in range(3):
1052         dev[i].p2p_stop_find()