tests: Make grpform_pbc_overlap* more likely to clear state
[mech_eap.git] / 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
13 import hostapd
14 import hwsim_utils
15 import utils
16
17 def check_grpform_results(i_res, r_res):
18     if i_res['result'] != 'success' or r_res['result'] != 'success':
19         raise Exception("Failed group formation")
20     if i_res['ssid'] != r_res['ssid']:
21         raise Exception("SSID mismatch")
22     if i_res['freq'] != r_res['freq']:
23         raise Exception("freq mismatch")
24     if 'go_neg_freq' in r_res and i_res['go_neg_freq'] != r_res['go_neg_freq']:
25         raise Exception("go_neg_freq mismatch")
26     if i_res['freq'] != i_res['go_neg_freq']:
27         raise Exception("freq/go_neg_freq mismatch")
28     if i_res['role'] != i_res['go_neg_role']:
29         raise Exception("role/go_neg_role mismatch")
30     if 'go_neg_role' in r_res and r_res['role'] != r_res['go_neg_role']:
31         raise Exception("role/go_neg_role mismatch")
32     if i_res['go_dev_addr'] != r_res['go_dev_addr']:
33         raise Exception("GO Device Address mismatch")
34
35 def go_neg_init(i_dev, r_dev, pin, i_method, i_intent, res):
36     logger.debug("Initiate GO Negotiation from i_dev")
37     try:
38         i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), pin, i_method, timeout=20, go_intent=i_intent)
39         logger.debug("i_res: " + str(i_res))
40     except Exception, e:
41         i_res = None
42         logger.info("go_neg_init thread caught an exception from p2p_go_neg_init: " + str(e))
43     res.put(i_res)
44
45 def go_neg_pin(i_dev, r_dev, i_intent=None, r_intent=None, i_method='enter', r_method='display'):
46     r_dev.p2p_listen()
47     i_dev.p2p_listen()
48     pin = r_dev.wps_read_pin()
49     logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
50     r_dev.dump_monitor()
51     res = Queue.Queue()
52     t = threading.Thread(target=go_neg_init, args=(i_dev, r_dev, pin, i_method, i_intent, res))
53     t.start()
54     logger.debug("Wait for GO Negotiation Request on r_dev")
55     ev = r_dev.wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
56     if ev is None:
57         raise Exception("GO Negotiation timed out")
58     r_dev.dump_monitor()
59     logger.debug("Re-initiate GO Negotiation from r_dev")
60     r_res = r_dev.p2p_go_neg_init(i_dev.p2p_dev_addr(), pin, r_method, go_intent=r_intent, timeout=20)
61     logger.debug("r_res: " + str(r_res))
62     r_dev.dump_monitor()
63     t.join()
64     i_res = res.get()
65     if i_res is None:
66         raise Exception("go_neg_init thread failed")
67     logger.debug("i_res: " + str(i_res))
68     logger.info("Group formed")
69     hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
70     i_dev.dump_monitor()
71     return [i_res, r_res]
72
73 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):
74     i_dev.p2p_listen()
75     pin = r_dev.wps_read_pin()
76     logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
77     r_dev.p2p_go_neg_auth(i_dev.p2p_dev_addr(), pin, r_method, go_intent=r_intent, freq=r_freq)
78     r_dev.p2p_listen()
79     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)
80     r_res = r_dev.p2p_go_neg_auth_result(expect_failure=expect_failure)
81     logger.debug("i_res: " + str(i_res))
82     logger.debug("r_res: " + str(r_res))
83     r_dev.dump_monitor()
84     i_dev.dump_monitor()
85     if i_go_neg_status:
86         if i_res['result'] != 'go-neg-failed':
87             raise Exception("Expected GO Negotiation failure not reported")
88         if i_res['status'] != i_go_neg_status:
89             raise Exception("Expected GO Negotiation status not seen")
90     if expect_failure:
91         return
92     logger.info("Group formed")
93     if test_data:
94         hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
95     return [i_res, r_res]
96
97 def go_neg_init_pbc(i_dev, r_dev, i_intent, res, freq, provdisc):
98     logger.debug("Initiate GO Negotiation from i_dev")
99     try:
100         i_res = i_dev.p2p_go_neg_init(r_dev.p2p_dev_addr(), None, "pbc",
101                                       timeout=20, go_intent=i_intent, freq=freq,
102                                       provdisc=provdisc)
103         logger.debug("i_res: " + str(i_res))
104     except Exception, e:
105         i_res = None
106         logger.info("go_neg_init_pbc thread caught an exception from p2p_go_neg_init: " + str(e))
107     res.put(i_res)
108
109 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):
110     if r_listen:
111         r_dev.p2p_listen()
112     else:
113         r_dev.p2p_find(social=True)
114     i_dev.p2p_find(social=True)
115     logger.info("Start GO negotiation " + i_dev.ifname + " -> " + r_dev.ifname)
116     r_dev.dump_monitor()
117     res = Queue.Queue()
118     t = threading.Thread(target=go_neg_init_pbc, args=(i_dev, r_dev, i_intent, res, i_freq, provdisc))
119     t.start()
120     logger.debug("Wait for GO Negotiation Request on r_dev")
121     ev = r_dev.wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
122     if ev is None:
123         raise Exception("GO Negotiation timed out")
124     r_dev.dump_monitor()
125     logger.debug("Re-initiate GO Negotiation from r_dev")
126     r_res = r_dev.p2p_go_neg_init(i_dev.p2p_dev_addr(), None, "pbc",
127                                   go_intent=r_intent, timeout=20, freq=r_freq)
128     logger.debug("r_res: " + str(r_res))
129     r_dev.dump_monitor()
130     t.join()
131     i_res = res.get()
132     if i_res is None:
133         raise Exception("go_neg_init_pbc thread failed")
134     logger.debug("i_res: " + str(i_res))
135     logger.info("Group formed")
136     hwsim_utils.test_connectivity_p2p(r_dev, i_dev)
137     i_dev.dump_monitor()
138     return [i_res, r_res]
139
140 def remove_group(dev1, dev2):
141     dev1.remove_group()
142     try:
143         dev2.remove_group()
144     except:
145         pass
146
147 def test_grpform(dev):
148     """P2P group formation using PIN and authorized connection (init -> GO)"""
149     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
150                                            r_dev=dev[1], r_intent=0)
151     check_grpform_results(i_res, r_res)
152     remove_group(dev[0], dev[1])
153
154 def test_grpform_a(dev):
155     """P2P group formation using PIN and authorized connection (init -> GO) (init: group iface)"""
156     dev[0].request("SET p2p_no_group_iface 0")
157     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
158                                            r_dev=dev[1], r_intent=0)
159     if "p2p-wlan" not in i_res['ifname']:
160         raise Exception("Unexpected group interface name")
161     check_grpform_results(i_res, r_res)
162     remove_group(dev[0], dev[1])
163     if i_res['ifname'] in utils.get_ifnames():
164         raise Exception("Group interface netdev was not removed")
165
166 def test_grpform_b(dev):
167     """P2P group formation using PIN and authorized connection (init -> GO) (resp: group iface)"""
168     dev[1].request("SET p2p_no_group_iface 0")
169     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
170                                            r_dev=dev[1], r_intent=0)
171     if "p2p-wlan" not in r_res['ifname']:
172         raise Exception("Unexpected group interface name")
173     check_grpform_results(i_res, r_res)
174     remove_group(dev[0], dev[1])
175     if r_res['ifname'] in utils.get_ifnames():
176         raise Exception("Group interface netdev was not removed")
177
178 def test_grpform_c(dev):
179     """P2P group formation using PIN and authorized connection (init -> GO) (group iface)"""
180     dev[0].request("SET p2p_no_group_iface 0")
181     dev[1].request("SET p2p_no_group_iface 0")
182     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
183                                            r_dev=dev[1], r_intent=0)
184     if "p2p-wlan" not in i_res['ifname']:
185         raise Exception("Unexpected group interface name")
186     if "p2p-wlan" not in r_res['ifname']:
187         raise Exception("Unexpected group interface name")
188     check_grpform_results(i_res, r_res)
189     remove_group(dev[0], dev[1])
190     if i_res['ifname'] in utils.get_ifnames():
191         raise Exception("Group interface netdev was not removed")
192     if r_res['ifname'] in utils.get_ifnames():
193         raise Exception("Group interface netdev was not removed")
194
195 def test_grpform2(dev):
196     """P2P group formation using PIN and authorized connection (resp -> GO)"""
197     go_neg_pin_authorized(i_dev=dev[0], i_intent=0, r_dev=dev[1], r_intent=15)
198     remove_group(dev[0], dev[1])
199
200 def test_grpform2_c(dev):
201     """P2P group formation using PIN and authorized connection (resp -> GO) (group iface)"""
202     dev[0].request("SET p2p_no_group_iface 0")
203     dev[1].request("SET p2p_no_group_iface 0")
204     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0, r_dev=dev[1], r_intent=15)
205     remove_group(dev[0], dev[1])
206     if i_res['ifname'] in utils.get_ifnames():
207         raise Exception("Group interface netdev was not removed")
208     if r_res['ifname'] in utils.get_ifnames():
209         raise Exception("Group interface netdev was not removed")
210
211 def test_grpform3(dev):
212     """P2P group formation using PIN and re-init GO Negotiation"""
213     go_neg_pin(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
214     remove_group(dev[0], dev[1])
215
216 def test_grpform3_c(dev):
217     """P2P group formation using PIN and re-init GO Negotiation (group iface)"""
218     dev[0].request("SET p2p_no_group_iface 0")
219     dev[1].request("SET p2p_no_group_iface 0")
220     [i_res, r_res] = go_neg_pin(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
221     remove_group(dev[0], dev[1])
222     if i_res['ifname'] in utils.get_ifnames():
223         raise Exception("Group interface netdev was not removed")
224     if r_res['ifname'] in utils.get_ifnames():
225         raise Exception("Group interface netdev was not removed")
226
227 def test_grpform_pbc(dev):
228     """P2P group formation using PBC and re-init GO Negotiation"""
229     [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
230     check_grpform_results(i_res, r_res)
231     if i_res['role'] != 'GO' or r_res['role'] != 'client':
232         raise Exception("Unexpected device roles")
233     remove_group(dev[0], dev[1])
234
235 def test_grpform_pd(dev):
236     """P2P group formation with PD-before-GO-Neg workaround"""
237     [i_res, r_res] = go_neg_pbc(i_dev=dev[0], provdisc=True, r_dev=dev[1], r_listen=True)
238     check_grpform_results(i_res, r_res)
239     remove_group(dev[0], dev[1])
240
241 def test_grpform_ext_listen(dev):
242     """P2P group formation with extended listen timing enabled"""
243     try:
244         if "OK" not in dev[0].global_request("P2P_EXT_LISTEN 100 50000"):
245             raise Exception("Failed to set extended listen timing")
246         if "OK" not in dev[1].global_request("P2P_EXT_LISTEN 200 40000"):
247             raise Exception("Failed to set extended listen timing")
248         [i_res, r_res] = go_neg_pbc(i_dev=dev[0], provdisc=True, r_dev=dev[1], r_listen=True)
249         check_grpform_results(i_res, r_res)
250         peer1 = dev[0].get_peer(dev[1].p2p_dev_addr())
251         if peer1['ext_listen_interval'] != "40000":
252             raise Exception("Extended listen interval not discovered correctly")
253         if peer1['ext_listen_period'] != "200":
254             raise Exception("Extended listen period not discovered correctly")
255         peer0 = dev[1].get_peer(dev[0].p2p_dev_addr())
256         if peer0['ext_listen_interval'] != "50000":
257             raise Exception("Extended listen interval not discovered correctly")
258         if peer0['ext_listen_period'] != "100":
259             raise Exception("Extended listen period not discovered correctly")
260         remove_group(dev[0], dev[1])
261     finally:
262         if "OK" not in dev[0].global_request("P2P_EXT_LISTEN"):
263             raise Exception("Failed to clear extended listen timing")
264         if "OK" not in dev[1].global_request("P2P_EXT_LISTEN"):
265             raise Exception("Failed to clear extended listen timing")
266
267 def test_both_go_intent_15(dev):
268     """P2P GO Negotiation with both devices using GO intent 15"""
269     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)
270
271 def test_both_go_neg_display(dev):
272     """P2P GO Negotiation with both devices trying to display PIN"""
273     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')
274
275 def test_both_go_neg_enter(dev):
276     """P2P GO Negotiation with both devices trying to enter PIN"""
277     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')
278
279 def test_go_neg_pbc_vs_pin(dev):
280     """P2P GO Negotiation with one device using PBC and the other PIN"""
281     addr0 = dev[0].p2p_dev_addr()
282     addr1 = dev[1].p2p_dev_addr()
283     dev[1].p2p_listen()
284     if not dev[0].discover_peer(addr1):
285         raise Exception("Could not discover peer")
286     dev[0].p2p_listen()
287     if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " pbc auth"):
288         raise Exception("Failed to authorize GO Neg")
289     if not dev[1].discover_peer(addr0):
290         raise Exception("Could not discover peer")
291     if "OK" not in dev[1].request("P2P_CONNECT " + addr0 + " 12345670 display"):
292         raise Exception("Failed to initiate GO Neg")
293     ev = dev[1].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=10)
294     if ev is None:
295         raise Exception("GO Negotiation failure timed out")
296     if "status=10" not in ev:
297         raise Exception("Unexpected failure reason: " + ev)
298
299 def test_go_neg_pin_vs_pbc(dev):
300     """P2P GO Negotiation with one device using PIN and the other PBC"""
301     addr0 = dev[0].p2p_dev_addr()
302     addr1 = dev[1].p2p_dev_addr()
303     dev[1].p2p_listen()
304     if not dev[0].discover_peer(addr1):
305         raise Exception("Could not discover peer")
306     dev[0].p2p_listen()
307     if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " 12345670 display auth"):
308         raise Exception("Failed to authorize GO Neg")
309     if not dev[1].discover_peer(addr0):
310         raise Exception("Could not discover peer")
311     if "OK" not in dev[1].request("P2P_CONNECT " + addr0 + " pbc"):
312         raise Exception("Failed to initiate GO Neg")
313     ev = dev[1].wait_global_event(["P2P-GO-NEG-FAILURE"], timeout=10)
314     if ev is None:
315         raise Exception("GO Negotiation failure timed out")
316     if "status=10" not in ev:
317         raise Exception("Unexpected failure reason: " + ev)
318
319 def test_grpform_per_sta_psk(dev):
320     """P2P group formation with per-STA PSKs"""
321     dev[0].request("P2P_SET per_sta_psk 1")
322     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
323     check_grpform_results(i_res, r_res)
324
325     pin = dev[2].wps_read_pin()
326     dev[0].p2p_go_authorize_client(pin)
327     c_res = dev[2].p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60)
328     check_grpform_results(i_res, c_res)
329
330     if r_res['psk'] == c_res['psk']:
331         raise Exception("Same PSK assigned for both clients")
332
333     hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
334
335     dev[0].remove_group()
336     dev[1].wait_go_ending_session()
337     dev[2].wait_go_ending_session()
338
339 def test_grpform_per_sta_psk_wps(dev):
340     """P2P group formation with per-STA PSKs with non-P2P WPS STA"""
341     dev[0].request("P2P_SET per_sta_psk 1")
342     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15, r_dev=dev[1], r_intent=0)
343     check_grpform_results(i_res, r_res)
344
345     dev[0].p2p_go_authorize_client_pbc()
346     dev[2].request("WPS_PBC")
347     ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
348     if ev is None:
349         raise Exception("Association with the GO timed out")
350
351     hwsim_utils.test_connectivity_p2p_sta(dev[1], dev[2])
352
353     dev[0].remove_group()
354     dev[2].request("DISCONNECT")
355     dev[1].wait_go_ending_session()
356
357 def test_grpform_force_chan_go(dev):
358     """P2P group formation forced channel selection by GO"""
359     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
360                                            i_freq=2432,
361                                            r_dev=dev[1], r_intent=0,
362                                            test_data=False)
363     check_grpform_results(i_res, r_res)
364     if i_res['freq'] != "2432":
365         raise Exception("Unexpected channel - did not follow GO's forced channel")
366     remove_group(dev[0], dev[1])
367
368 def test_grpform_force_chan_cli(dev):
369     """P2P group formation forced channel selection by client"""
370     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
371                                            i_freq=2417,
372                                            r_dev=dev[1], r_intent=15,
373                                            test_data=False)
374     check_grpform_results(i_res, r_res)
375     if i_res['freq'] != "2417":
376         raise Exception("Unexpected channel - did not follow GO's forced channel")
377     remove_group(dev[0], dev[1])
378
379 def test_grpform_force_chan_conflict(dev):
380     """P2P group formation fails due to forced channel mismatch"""
381     go_neg_pin_authorized(i_dev=dev[0], i_intent=0, i_freq=2422,
382                           r_dev=dev[1], r_intent=15, r_freq=2427,
383                           expect_failure=True, i_go_neg_status=7)
384
385 def test_grpform_pref_chan_go(dev):
386     """P2P group formation preferred channel selection by GO"""
387     dev[0].request("SET p2p_pref_chan 81:7")
388     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
389                                            r_dev=dev[1], r_intent=0,
390                                            test_data=False)
391     check_grpform_results(i_res, r_res)
392     if i_res['freq'] != "2442":
393         raise Exception("Unexpected channel - did not follow GO's p2p_pref_chan")
394     remove_group(dev[0], dev[1])
395
396 def test_grpform_pref_chan_go_overridden(dev):
397     """P2P group formation preferred channel selection by GO overridden by client"""
398     dev[1].request("SET p2p_pref_chan 81:7")
399     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
400                                            i_freq=2422,
401                                            r_dev=dev[1], r_intent=15,
402                                            test_data=False)
403     check_grpform_results(i_res, r_res)
404     if i_res['freq'] != "2422":
405         raise Exception("Unexpected channel - did not follow client's forced channel")
406     remove_group(dev[0], dev[1])
407
408 def test_grpform_no_go_freq_forcing_chan(dev):
409     """P2P group formation with no-GO freq forcing channel"""
410     dev[1].request("SET p2p_no_go_freq 100-200,300,4000-6000")
411     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
412                                            r_dev=dev[1], r_intent=15,
413                                            test_data=False)
414     check_grpform_results(i_res, r_res)
415     if int(i_res['freq']) > 4000:
416         raise Exception("Unexpected channel - did not follow no-GO freq")
417     remove_group(dev[0], dev[1])
418
419 def test_grpform_no_go_freq_conflict(dev):
420     """P2P group formation fails due to no-GO range forced by client"""
421     dev[1].request("SET p2p_no_go_freq 2000-3000")
422     go_neg_pin_authorized(i_dev=dev[0], i_intent=0, i_freq=2422,
423                           r_dev=dev[1], r_intent=15,
424                           expect_failure=True, i_go_neg_status=7)
425
426 def test_grpform_no_5ghz_world_roaming(dev):
427     """P2P group formation with world roaming regulatory"""
428     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
429                                            r_dev=dev[1], r_intent=15,
430                                            test_data=False)
431     check_grpform_results(i_res, r_res)
432     if int(i_res['freq']) > 4000:
433         raise Exception("Unexpected channel - did not follow world roaming rules")
434     remove_group(dev[0], dev[1])
435
436 def test_grpform_no_5ghz_add_cli(dev):
437     """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1"""
438     dev[0].request("SET p2p_add_cli_chan 1")
439     dev[1].request("SET p2p_add_cli_chan 1")
440     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
441                                            r_dev=dev[1], r_intent=14,
442                                            test_data=False)
443     check_grpform_results(i_res, r_res)
444     if int(i_res['freq']) > 4000:
445         raise Exception("Unexpected channel - did not follow world roaming rules")
446     remove_group(dev[0], dev[1])
447
448 def test_grpform_no_5ghz_add_cli2(dev):
449     """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (reverse)"""
450     dev[0].request("SET p2p_add_cli_chan 1")
451     dev[1].request("SET p2p_add_cli_chan 1")
452     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=14,
453                                            r_dev=dev[1], r_intent=0,
454                                            test_data=False)
455     check_grpform_results(i_res, r_res)
456     if int(i_res['freq']) > 4000:
457         raise Exception("Unexpected channel - did not follow world roaming rules")
458     remove_group(dev[0], dev[1])
459
460 def test_grpform_no_5ghz_add_cli3(dev):
461     """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (intent 15)"""
462     dev[0].request("SET p2p_add_cli_chan 1")
463     dev[1].request("SET p2p_add_cli_chan 1")
464     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=0,
465                                            r_dev=dev[1], r_intent=15,
466                                            test_data=False)
467     check_grpform_results(i_res, r_res)
468     if int(i_res['freq']) > 4000:
469         raise Exception("Unexpected channel - did not follow world roaming rules")
470     remove_group(dev[0], dev[1])
471
472 def test_grpform_no_5ghz_add_cli4(dev):
473     """P2P group formation with passive scan 5 GHz and p2p_add_cli_chan=1 (reverse; intent 15)"""
474     dev[0].request("SET p2p_add_cli_chan 1")
475     dev[1].request("SET p2p_add_cli_chan 1")
476     [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
477                                            r_dev=dev[1], r_intent=0,
478                                            test_data=False)
479     check_grpform_results(i_res, r_res)
480     if int(i_res['freq']) > 4000:
481         raise Exception("Unexpected channel - did not follow world roaming rules")
482     remove_group(dev[0], dev[1])
483
484 def test_grpform_incorrect_pin(dev):
485     """P2P GO Negotiation with incorrect PIN"""
486     dev[1].p2p_listen()
487     pin = dev[1].wps_read_pin()
488     addr1 = dev[1].p2p_dev_addr()
489     if not dev[0].discover_peer(addr1):
490         raise Exception("Peer not found")
491     dev[1].p2p_go_neg_auth(dev[0].p2p_dev_addr(), pin, 'display', go_intent=0)
492     dev[0].request("P2P_CONNECT " + addr1 + " 00000000 enter go_intent=15")
493     ev = dev[1].wait_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=10)
494     if ev is None:
495         raise Exception("Group formation failure timed out")
496     ev = dev[0].wait_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=5)
497     if ev is None:
498         raise Exception("Group formation failure timed out")
499
500 def test_grpform_reject(dev):
501     """User rejecting group formation attempt by a P2P peer"""
502     addr0 = dev[0].p2p_dev_addr()
503     dev[0].p2p_listen()
504     dev[1].p2p_go_neg_init(addr0, None, "pbc")
505     ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=15)
506     if ev is None:
507         raise Exception("GO Negotiation timed out")
508     if "FAIL" in dev[0].global_request("P2P_REJECT " + ev.split(' ')[1]):
509         raise Exception("P2P_REJECT failed")
510     dev[1].request("P2P_STOP_FIND")
511     dev[1].p2p_go_neg_init(addr0, None, "pbc")
512     ev = dev[1].wait_global_event(["GO-NEG-FAILURE"], timeout=10)
513     if ev is None:
514         raise Exception("Rejection not reported")
515     if "status=11" not in ev:
516         raise Exception("Unexpected status code in rejection")
517
518 def test_grpform_pd_no_probe_resp(dev):
519     """GO Negotiation after PD, but no Probe Response"""
520     addr0 = dev[0].p2p_dev_addr()
521     addr1 = dev[1].p2p_dev_addr()
522     dev[0].p2p_listen()
523     if not dev[1].discover_peer(addr0):
524         raise Exception("Peer not found")
525     dev[1].p2p_stop_find()
526     dev[0].p2p_stop_find()
527     peer = dev[0].get_peer(addr1)
528     if peer['listen_freq'] == '0':
529         raise Exception("Peer listen frequency not learned from Probe Request")
530     time.sleep(0.3)
531     dev[0].request("P2P_FLUSH")
532     dev[0].p2p_listen()
533     dev[1].global_request("P2P_PROV_DISC " + addr0 + " display")
534     ev = dev[0].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout=5)
535     if ev is None:
536         raise Exception("PD Request timed out")
537     ev = dev[1].wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout=5)
538     if ev is None:
539         raise Exception("PD Response timed out")
540     peer = dev[0].get_peer(addr1)
541     if peer['listen_freq'] != '0':
542         raise Exception("Peer listen frequency learned unexpectedly from PD Request")
543
544     pin = dev[0].wps_read_pin()
545     if "FAIL" in dev[1].request("P2P_CONNECT " + addr0 + " " + pin + " enter"):
546         raise Exception("P2P_CONNECT on initiator failed")
547     ev = dev[0].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=5)
548     if ev is None:
549         raise Exception("GO Negotiation start timed out")
550     peer = dev[0].get_peer(addr1)
551     if peer['listen_freq'] == '0':
552         raise Exception("Peer listen frequency not learned from PD followed by GO Neg Req")
553     if "FAIL" in dev[0].request("P2P_CONNECT " + addr1 + " " + pin + " display"):
554         raise Exception("P2P_CONNECT on responder failed")
555     ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
556     if ev is None:
557         raise Exception("Group formation timed out")
558     ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
559     if ev is None:
560         raise Exception("Group formation timed out")
561
562 def test_go_neg_two_peers(dev):
563     """P2P GO Negotiation rejected due to already started negotiation with another peer"""
564     addr0 = dev[0].p2p_dev_addr()
565     addr1 = dev[1].p2p_dev_addr()
566     addr2 = dev[2].p2p_dev_addr()
567     dev[1].p2p_listen()
568     dev[2].p2p_listen()
569     if not dev[0].discover_peer(addr1):
570         raise Exception("Could not discover peer")
571     if not dev[0].discover_peer(addr2):
572         raise Exception("Could not discover peer")
573     if "OK" not in dev[0].request("P2P_CONNECT " + addr2 + " pbc auth"):
574         raise Exception("Failed to authorize GO Neg")
575     dev[0].p2p_listen()
576     if not dev[2].discover_peer(addr0):
577         raise Exception("Could not discover peer")
578     if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " pbc"):
579         raise Exception("Failed to initiate GO Neg")
580     ev = dev[1].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout=5)
581     if ev is None:
582         raise Exception("timeout on GO Neg RX event")
583     dev[2].request("P2P_CONNECT " + addr0 + " pbc")
584     ev = dev[2].wait_global_event(["GO-NEG-FAILURE"], timeout=10)
585     if ev is None:
586         raise Exception("Rejection not reported")
587     if "status=5" not in ev:
588         raise Exception("Unexpected status code in rejection: " + ev)
589
590 def clear_pbc_overlap(dev, ifname):
591     hapd_global = hostapd.HostapdGlobal()
592     hapd_global.remove(ifname)
593     dev[0].p2p_stop_find()
594     dev[1].p2p_stop_find()
595     dev[0].dump_monitor()
596     dev[1].dump_monitor()
597     time.sleep(0.1)
598     dev[0].request("BSS_FLUSH 0")
599     dev[0].request("SCAN freq=2412 only_new=1")
600     dev[1].request("BSS_FLUSH 0")
601     dev[1].request("SCAN freq=2412 only_new=1")
602     time.sleep(1)
603
604 def test_grpform_pbc_overlap(dev, apdev):
605     """P2P group formation during PBC overlap"""
606     params = { "ssid": "wps", "eap_server": "1", "wps_state": "1" }
607     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
608     hapd.request("WPS_PBC")
609     time.sleep(0.1)
610
611     addr0 = dev[0].p2p_dev_addr()
612     addr1 = dev[1].p2p_dev_addr()
613     dev[0].p2p_listen()
614     if not dev[1].discover_peer(addr0):
615         raise Exception("Could not discover peer")
616     dev[1].p2p_listen()
617     if not dev[0].discover_peer(addr1):
618         raise Exception("Could not discover peer")
619     dev[0].p2p_listen()
620     if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " pbc auth go_intent=0"):
621         raise Exception("Failed to authorize GO Neg")
622     if "OK" not in dev[1].request("P2P_CONNECT " + addr0 + " pbc go_intent=15 freq=2412"):
623         raise Exception("Failed to initiate GO Neg")
624     ev = dev[0].wait_global_event(["WPS-OVERLAP-DETECTED"], timeout=15)
625     if ev is None:
626         raise Exception("PBC overlap not reported")
627
628     clear_pbc_overlap(dev, apdev[0]['ifname'])
629
630 def test_grpform_pbc_overlap_group_iface(dev, apdev):
631     """P2P group formation during PBC overlap using group interfaces"""
632     # Note: Need to include P2P IE from the AP to get the P2P interface BSS
633     # update use this information.
634     params = { "ssid": "wps", "eap_server": "1", "wps_state": "1",
635                'manage_p2p': '1' }
636     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
637     hapd.request("WPS_PBC")
638
639     dev[0].request("SET p2p_no_group_iface 0")
640     dev[1].request("SET p2p_no_group_iface 0")
641
642     addr0 = dev[0].p2p_dev_addr()
643     addr1 = dev[1].p2p_dev_addr()
644     dev[0].p2p_listen()
645     if not dev[1].discover_peer(addr0):
646         raise Exception("Could not discover peer")
647     dev[1].p2p_listen()
648     if not dev[0].discover_peer(addr1):
649         raise Exception("Could not discover peer")
650     dev[0].p2p_stop_find()
651     dev[0].scan(freq="2412")
652     dev[0].p2p_listen()
653     if "OK" not in dev[0].request("P2P_CONNECT " + addr1 + " pbc auth go_intent=0"):
654         raise Exception("Failed to authorize GO Neg")
655     if "OK" not in dev[1].request("P2P_CONNECT " + addr0 + " pbc go_intent=15 freq=2412"):
656         raise Exception("Failed to initiate GO Neg")
657     ev = dev[0].wait_global_event(["WPS-OVERLAP-DETECTED",
658                                    "P2P-GROUP-FORMATION-SUCCESS"], timeout=15)
659     if ev is None or "WPS-OVERLAP-DETECTED" not in ev:
660         raise Exception("PBC overlap not reported")
661
662     clear_pbc_overlap(dev, apdev[0]['ifname'])